Merge branch 'x86-uv-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / powerpc / kvm / e500_tlb.h
CommitLineData
bc8080cb
HB
1/*
2 * Copyright (C) 2008 Freescale Semiconductor, Inc. All rights reserved.
3 *
4 * Author: Yu Liu, yu.liu@freescale.com
5 *
6 * Description:
7 * This file is based on arch/powerpc/kvm/44x_tlb.h,
8 * by Hollis Blanchard <hollisb@us.ibm.com>.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License, version 2, as
12 * published by the Free Software Foundation.
13 */
14
15#ifndef __KVM_E500_TLB_H__
16#define __KVM_E500_TLB_H__
17
18#include <linux/kvm_host.h>
0cfb50e5 19#include <asm/mmu-book3e.h>
bc8080cb
HB
20#include <asm/tlb.h>
21#include <asm/kvm_e500.h>
22
23#define KVM_E500_TLB0_WAY_SIZE_BIT 7 /* Fixed */
24#define KVM_E500_TLB0_WAY_SIZE (1UL << KVM_E500_TLB0_WAY_SIZE_BIT)
25#define KVM_E500_TLB0_WAY_SIZE_MASK (KVM_E500_TLB0_WAY_SIZE - 1)
26
27#define KVM_E500_TLB0_WAY_NUM_BIT 1 /* No greater than 7 */
28#define KVM_E500_TLB0_WAY_NUM (1UL << KVM_E500_TLB0_WAY_NUM_BIT)
29#define KVM_E500_TLB0_WAY_NUM_MASK (KVM_E500_TLB0_WAY_NUM - 1)
30
31#define KVM_E500_TLB0_SIZE (KVM_E500_TLB0_WAY_SIZE * KVM_E500_TLB0_WAY_NUM)
32#define KVM_E500_TLB1_SIZE 16
33
34#define index_of(tlbsel, esel) (((tlbsel) << 16) | ((esel) & 0xFFFF))
35#define tlbsel_of(index) ((index) >> 16)
36#define esel_of(index) ((index) & 0xFFFF)
37
38#define E500_TLB_USER_PERM_MASK (MAS3_UX|MAS3_UR|MAS3_UW)
39#define E500_TLB_SUPER_PERM_MASK (MAS3_SX|MAS3_SR|MAS3_SW)
40#define MAS2_ATTRIB_MASK \
046a48b3 41 (MAS2_X0 | MAS2_X1)
bc8080cb
HB
42#define MAS3_ATTRIB_MASK \
43 (MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3 \
44 | E500_TLB_USER_PERM_MASK | E500_TLB_SUPER_PERM_MASK)
45
46extern void kvmppc_dump_tlbs(struct kvm_vcpu *);
b0a1835d 47extern int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *, ulong);
bc8080cb
HB
48extern int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *);
49extern int kvmppc_e500_emul_tlbre(struct kvm_vcpu *);
50extern int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *, int, int);
51extern int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *, int);
52extern int kvmppc_e500_tlb_search(struct kvm_vcpu *, gva_t, unsigned int, int);
53extern void kvmppc_e500_tlb_put(struct kvm_vcpu *);
54extern void kvmppc_e500_tlb_load(struct kvm_vcpu *, int);
55extern int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *);
56extern void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *);
57extern void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *);
58
59/* TLB helper functions */
60static inline unsigned int get_tlb_size(const struct tlbe *tlbe)
61{
0cfb50e5 62 return (tlbe->mas1 >> 7) & 0x1f;
bc8080cb
HB
63}
64
65static inline gva_t get_tlb_eaddr(const struct tlbe *tlbe)
66{
67 return tlbe->mas2 & 0xfffff000;
68}
69
70static inline u64 get_tlb_bytes(const struct tlbe *tlbe)
71{
72 unsigned int pgsize = get_tlb_size(tlbe);
0cfb50e5 73 return 1ULL << 10 << pgsize;
bc8080cb
HB
74}
75
76static inline gva_t get_tlb_end(const struct tlbe *tlbe)
77{
78 u64 bytes = get_tlb_bytes(tlbe);
79 return get_tlb_eaddr(tlbe) + bytes - 1;
80}
81
82static inline u64 get_tlb_raddr(const struct tlbe *tlbe)
83{
84 u64 rpn = tlbe->mas7;
85 return (rpn << 32) | (tlbe->mas3 & 0xfffff000);
86}
87
88static inline unsigned int get_tlb_tid(const struct tlbe *tlbe)
89{
90 return (tlbe->mas1 >> 16) & 0xff;
91}
92
93static inline unsigned int get_tlb_ts(const struct tlbe *tlbe)
94{
95 return (tlbe->mas1 >> 12) & 0x1;
96}
97
98static inline unsigned int get_tlb_v(const struct tlbe *tlbe)
99{
100 return (tlbe->mas1 >> 31) & 0x1;
101}
102
103static inline unsigned int get_tlb_iprot(const struct tlbe *tlbe)
104{
105 return (tlbe->mas1 >> 30) & 0x1;
106}
107
108static inline unsigned int get_cur_pid(struct kvm_vcpu *vcpu)
109{
110 return vcpu->arch.pid & 0xff;
111}
112
113static inline unsigned int get_cur_spid(
114 const struct kvmppc_vcpu_e500 *vcpu_e500)
115{
116 return (vcpu_e500->mas6 >> 16) & 0xff;
117}
118
119static inline unsigned int get_cur_sas(
120 const struct kvmppc_vcpu_e500 *vcpu_e500)
121{
122 return vcpu_e500->mas6 & 0x1;
123}
124
125static inline unsigned int get_tlb_tlbsel(
126 const struct kvmppc_vcpu_e500 *vcpu_e500)
127{
128 /*
129 * Manual says that tlbsel has 2 bits wide.
fb2838d4 130 * Since we only have two TLBs, only lower bit is used.
bc8080cb
HB
131 */
132 return (vcpu_e500->mas0 >> 28) & 0x1;
133}
134
135static inline unsigned int get_tlb_nv_bit(
136 const struct kvmppc_vcpu_e500 *vcpu_e500)
137{
138 return vcpu_e500->mas0 & 0xfff;
139}
140
141static inline unsigned int get_tlb_esel_bit(
142 const struct kvmppc_vcpu_e500 *vcpu_e500)
143{
144 return (vcpu_e500->mas0 >> 16) & 0xfff;
145}
146
147static inline unsigned int get_tlb_esel(
148 const struct kvmppc_vcpu_e500 *vcpu_e500,
149 int tlbsel)
150{
151 unsigned int esel = get_tlb_esel_bit(vcpu_e500);
152
153 if (tlbsel == 0) {
154 esel &= KVM_E500_TLB0_WAY_NUM_MASK;
155 esel |= ((vcpu_e500->mas2 >> 12) & KVM_E500_TLB0_WAY_SIZE_MASK)
156 << KVM_E500_TLB0_WAY_NUM_BIT;
157 } else {
158 esel &= KVM_E500_TLB1_SIZE - 1;
159 }
160
161 return esel;
162}
163
164static inline int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
165 const struct tlbe *tlbe)
166{
167 gpa_t gpa;
168
169 if (!get_tlb_v(tlbe))
170 return 0;
171
172 /* Does it match current guest AS? */
173 /* XXX what about IS != DS? */
174 if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
175 return 0;
176
177 gpa = get_tlb_raddr(tlbe);
178 if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
179 /* Mapping is not for RAM. */
180 return 0;
181
182 return 1;
183}
184
185#endif /* __KVM_E500_TLB_H__ */