2 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
4 * Author: Yu Liu, yu.liu@freescale.com
7 * This file is based on arch/powerpc/kvm/44x_tlb.h,
8 * by Hollis Blanchard <hollisb@us.ibm.com>.
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.
15 #ifndef __KVM_E500_TLB_H__
16 #define __KVM_E500_TLB_H__
18 #include <linux/kvm_host.h>
19 #include <asm/mmu-book3e.h>
21 #include <asm/kvm_e500.h>
23 /* This geometry is the legacy default -- can be overridden by userspace */
24 #define KVM_E500_TLB0_WAY_SIZE 128
25 #define KVM_E500_TLB0_WAY_NUM 2
27 #define KVM_E500_TLB0_SIZE (KVM_E500_TLB0_WAY_SIZE * KVM_E500_TLB0_WAY_NUM)
28 #define KVM_E500_TLB1_SIZE 16
30 #define index_of(tlbsel, esel) (((tlbsel) << 16) | ((esel) & 0xFFFF))
31 #define tlbsel_of(index) ((index) >> 16)
32 #define esel_of(index) ((index) & 0xFFFF)
34 #define E500_TLB_USER_PERM_MASK (MAS3_UX|MAS3_UR|MAS3_UW)
35 #define E500_TLB_SUPER_PERM_MASK (MAS3_SX|MAS3_SR|MAS3_SW)
36 #define MAS2_ATTRIB_MASK \
38 #define MAS3_ATTRIB_MASK \
39 (MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3 \
40 | E500_TLB_USER_PERM_MASK | E500_TLB_SUPER_PERM_MASK)
42 extern void kvmppc_dump_tlbs(struct kvm_vcpu *);
43 extern int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *, ulong);
44 extern int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *);
45 extern int kvmppc_e500_emul_tlbre(struct kvm_vcpu *);
46 extern int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *, int, int);
47 extern int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *, int);
48 extern int kvmppc_e500_tlb_search(struct kvm_vcpu *, gva_t, unsigned int, int);
49 extern void kvmppc_e500_tlb_put(struct kvm_vcpu *);
50 extern void kvmppc_e500_tlb_load(struct kvm_vcpu *, int);
51 extern int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *);
52 extern void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *);
53 extern void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *);
54 extern void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *);
56 /* TLB helper functions */
57 static inline unsigned int
58 get_tlb_size(const struct kvm_book3e_206_tlb_entry *tlbe)
60 return (tlbe->mas1 >> 7) & 0x1f;
63 static inline gva_t get_tlb_eaddr(const struct kvm_book3e_206_tlb_entry *tlbe)
65 return tlbe->mas2 & 0xfffff000;
68 static inline u64 get_tlb_bytes(const struct kvm_book3e_206_tlb_entry *tlbe)
70 unsigned int pgsize = get_tlb_size(tlbe);
71 return 1ULL << 10 << pgsize;
74 static inline gva_t get_tlb_end(const struct kvm_book3e_206_tlb_entry *tlbe)
76 u64 bytes = get_tlb_bytes(tlbe);
77 return get_tlb_eaddr(tlbe) + bytes - 1;
80 static inline u64 get_tlb_raddr(const struct kvm_book3e_206_tlb_entry *tlbe)
82 return tlbe->mas7_3 & ~0xfffULL;
85 static inline unsigned int
86 get_tlb_tid(const struct kvm_book3e_206_tlb_entry *tlbe)
88 return (tlbe->mas1 >> 16) & 0xff;
91 static inline unsigned int
92 get_tlb_ts(const struct kvm_book3e_206_tlb_entry *tlbe)
94 return (tlbe->mas1 >> 12) & 0x1;
97 static inline unsigned int
98 get_tlb_v(const struct kvm_book3e_206_tlb_entry *tlbe)
100 return (tlbe->mas1 >> 31) & 0x1;
103 static inline unsigned int
104 get_tlb_iprot(const struct kvm_book3e_206_tlb_entry *tlbe)
106 return (tlbe->mas1 >> 30) & 0x1;
109 static inline unsigned int get_cur_pid(struct kvm_vcpu *vcpu)
111 return vcpu->arch.pid & 0xff;
114 static inline unsigned int get_cur_as(struct kvm_vcpu *vcpu)
116 return !!(vcpu->arch.shared->msr & (MSR_IS | MSR_DS));
119 static inline unsigned int get_cur_pr(struct kvm_vcpu *vcpu)
121 return !!(vcpu->arch.shared->msr & MSR_PR);
124 static inline unsigned int get_cur_spid(const struct kvm_vcpu *vcpu)
126 return (vcpu->arch.shared->mas6 >> 16) & 0xff;
129 static inline unsigned int get_cur_sas(const struct kvm_vcpu *vcpu)
131 return vcpu->arch.shared->mas6 & 0x1;
134 static inline unsigned int get_tlb_tlbsel(const struct kvm_vcpu *vcpu)
137 * Manual says that tlbsel has 2 bits wide.
138 * Since we only have two TLBs, only lower bit is used.
140 return (vcpu->arch.shared->mas0 >> 28) & 0x1;
143 static inline unsigned int get_tlb_nv_bit(const struct kvm_vcpu *vcpu)
145 return vcpu->arch.shared->mas0 & 0xfff;
148 static inline unsigned int get_tlb_esel_bit(const struct kvm_vcpu *vcpu)
150 return (vcpu->arch.shared->mas0 >> 16) & 0xfff;
153 static inline int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
154 const struct kvm_book3e_206_tlb_entry *tlbe)
158 if (!get_tlb_v(tlbe))
161 /* Does it match current guest AS? */
162 /* XXX what about IS != DS? */
163 if (get_tlb_ts(tlbe) != !!(vcpu->arch.shared->msr & MSR_IS))
166 gpa = get_tlb_raddr(tlbe);
167 if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
168 /* Mapping is not for RAM. */
174 #endif /* __KVM_E500_TLB_H__ */