blob: 5c6d2d7bf058f8efd657c6f8dea558ba3500f44b [file] [log] [blame]
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001/*
Liu Yudd9ebf1f2011-06-14 18:35:14 -05002 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06003 *
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>
Liu Yu0cfb50e2009-06-05 14:54:29 +080019#include <asm/mmu-book3e.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060020#include <asm/tlb.h>
21#include <asm/kvm_e500.h>
22
Scott Wooddc83b8b2011-08-18 15:25:21 -050023/* 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
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060026
27#define KVM_E500_TLB0_SIZE (KVM_E500_TLB0_WAY_SIZE * KVM_E500_TLB0_WAY_NUM)
28#define KVM_E500_TLB1_SIZE 16
29
30#define index_of(tlbsel, esel) (((tlbsel) << 16) | ((esel) & 0xFFFF))
31#define tlbsel_of(index) ((index) >> 16)
32#define esel_of(index) ((index) & 0xFFFF)
33
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 \
Liu Yu046a48b2009-03-17 16:57:46 +080037 (MAS2_X0 | MAS2_X1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060038#define MAS3_ATTRIB_MASK \
39 (MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3 \
40 | E500_TLB_USER_PERM_MASK | E500_TLB_SUPER_PERM_MASK)
41
42extern void kvmppc_dump_tlbs(struct kvm_vcpu *);
Liu Yub0a18352009-02-17 16:52:08 +080043extern int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *, ulong);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060044extern int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *);
45extern int kvmppc_e500_emul_tlbre(struct kvm_vcpu *);
46extern int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *, int, int);
47extern int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *, int);
48extern int kvmppc_e500_tlb_search(struct kvm_vcpu *, gva_t, unsigned int, int);
49extern void kvmppc_e500_tlb_put(struct kvm_vcpu *);
50extern void kvmppc_e500_tlb_load(struct kvm_vcpu *, int);
51extern int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *);
52extern void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *);
53extern void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *);
Liu Yudd9ebf1f2011-06-14 18:35:14 -050054extern void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060055
56/* TLB helper functions */
Scott Wooddc83b8b2011-08-18 15:25:21 -050057static inline unsigned int
58get_tlb_size(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060059{
Liu Yu0cfb50e2009-06-05 14:54:29 +080060 return (tlbe->mas1 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060061}
62
Scott Wooddc83b8b2011-08-18 15:25:21 -050063static inline gva_t get_tlb_eaddr(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060064{
65 return tlbe->mas2 & 0xfffff000;
66}
67
Scott Wooddc83b8b2011-08-18 15:25:21 -050068static inline u64 get_tlb_bytes(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060069{
70 unsigned int pgsize = get_tlb_size(tlbe);
Liu Yu0cfb50e2009-06-05 14:54:29 +080071 return 1ULL << 10 << pgsize;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060072}
73
Scott Wooddc83b8b2011-08-18 15:25:21 -050074static inline gva_t get_tlb_end(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060075{
76 u64 bytes = get_tlb_bytes(tlbe);
77 return get_tlb_eaddr(tlbe) + bytes - 1;
78}
79
Scott Wooddc83b8b2011-08-18 15:25:21 -050080static inline u64 get_tlb_raddr(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060081{
Scott Wooddc83b8b2011-08-18 15:25:21 -050082 return tlbe->mas7_3 & ~0xfffULL;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060083}
84
Scott Wooddc83b8b2011-08-18 15:25:21 -050085static inline unsigned int
86get_tlb_tid(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060087{
88 return (tlbe->mas1 >> 16) & 0xff;
89}
90
Scott Wooddc83b8b2011-08-18 15:25:21 -050091static inline unsigned int
92get_tlb_ts(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060093{
94 return (tlbe->mas1 >> 12) & 0x1;
95}
96
Scott Wooddc83b8b2011-08-18 15:25:21 -050097static inline unsigned int
98get_tlb_v(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060099{
100 return (tlbe->mas1 >> 31) & 0x1;
101}
102
Scott Wooddc83b8b2011-08-18 15:25:21 -0500103static inline unsigned int
104get_tlb_iprot(const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600105{
106 return (tlbe->mas1 >> 30) & 0x1;
107}
108
109static inline unsigned int get_cur_pid(struct kvm_vcpu *vcpu)
110{
111 return vcpu->arch.pid & 0xff;
112}
113
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500114static inline unsigned int get_cur_as(struct kvm_vcpu *vcpu)
115{
116 return !!(vcpu->arch.shared->msr & (MSR_IS | MSR_DS));
117}
118
119static inline unsigned int get_cur_pr(struct kvm_vcpu *vcpu)
120{
121 return !!(vcpu->arch.shared->msr & MSR_PR);
122}
123
Scott Woodb5904972011-11-08 18:23:30 -0600124static inline unsigned int get_cur_spid(const struct kvm_vcpu *vcpu)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600125{
Scott Woodb5904972011-11-08 18:23:30 -0600126 return (vcpu->arch.shared->mas6 >> 16) & 0xff;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600127}
128
Scott Woodb5904972011-11-08 18:23:30 -0600129static inline unsigned int get_cur_sas(const struct kvm_vcpu *vcpu)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600130{
Scott Woodb5904972011-11-08 18:23:30 -0600131 return vcpu->arch.shared->mas6 & 0x1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600132}
133
Scott Woodb5904972011-11-08 18:23:30 -0600134static inline unsigned int get_tlb_tlbsel(const struct kvm_vcpu *vcpu)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600135{
136 /*
137 * Manual says that tlbsel has 2 bits wide.
Liu Yufb2838d2009-01-14 10:47:37 -0600138 * Since we only have two TLBs, only lower bit is used.
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600139 */
Scott Woodb5904972011-11-08 18:23:30 -0600140 return (vcpu->arch.shared->mas0 >> 28) & 0x1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600141}
142
Scott Woodb5904972011-11-08 18:23:30 -0600143static inline unsigned int get_tlb_nv_bit(const struct kvm_vcpu *vcpu)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600144{
Scott Woodb5904972011-11-08 18:23:30 -0600145 return vcpu->arch.shared->mas0 & 0xfff;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600146}
147
Scott Woodb5904972011-11-08 18:23:30 -0600148static inline unsigned int get_tlb_esel_bit(const struct kvm_vcpu *vcpu)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600149{
Scott Woodb5904972011-11-08 18:23:30 -0600150 return (vcpu->arch.shared->mas0 >> 16) & 0xfff;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600151}
152
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600153static inline int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500154 const struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600155{
156 gpa_t gpa;
157
158 if (!get_tlb_v(tlbe))
159 return 0;
160
161 /* Does it match current guest AS? */
162 /* XXX what about IS != DS? */
Alexander Graf666e7252010-07-29 14:47:43 +0200163 if (get_tlb_ts(tlbe) != !!(vcpu->arch.shared->msr & MSR_IS))
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600164 return 0;
165
166 gpa = get_tlb_raddr(tlbe);
167 if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
168 /* Mapping is not for RAM. */
169 return 0;
170
171 return 1;
172}
173
174#endif /* __KVM_E500_TLB_H__ */