aboutsummaryrefslogtreecommitdiffstats
path: root/arch
diff options
context:
space:
mode:
authorHollis Blanchard <hollisb@us.ibm.com>2009-01-03 17:23:10 -0500
committerAvi Kivity <avi@redhat.com>2009-03-24 05:02:58 -0400
commitbc8080cbcc8870178f0910edd537d0cb5706d703 (patch)
tree7e929e8585a32c3d93a373553d3179e182c7eb5f /arch
parent17c885eb5c38f39a2bb3143a67687bd905dcd0fa (diff)
KVM: ppc: E500 core-specific code
Signed-off-by: Liu Yu <yu.liu@freescale.com> Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com> Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'arch')
-rw-r--r--arch/powerpc/include/asm/kvm_e500.h67
-rw-r--r--arch/powerpc/kvm/Kconfig13
-rw-r--r--arch/powerpc/kvm/Makefile9
-rw-r--r--arch/powerpc/kvm/e500.c151
-rw-r--r--arch/powerpc/kvm/e500_emulate.c169
-rw-r--r--arch/powerpc/kvm/e500_tlb.c737
-rw-r--r--arch/powerpc/kvm/e500_tlb.h184
7 files changed, 1330 insertions, 0 deletions
diff --git a/arch/powerpc/include/asm/kvm_e500.h b/arch/powerpc/include/asm/kvm_e500.h
new file mode 100644
index 000000000000..9d497ce49726
--- /dev/null
+++ b/arch/powerpc/include/asm/kvm_e500.h
@@ -0,0 +1,67 @@
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 derived from arch/powerpc/include/asm/kvm_44x.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 __ASM_KVM_E500_H__
16#define __ASM_KVM_E500_H__
17
18#include <linux/kvm_host.h>
19
20#define BOOKE_INTERRUPT_SIZE 36
21
22#define E500_PID_NUM 3
23#define E500_TLB_NUM 2
24
25struct tlbe{
26 u32 mas1;
27 u32 mas2;
28 u32 mas3;
29 u32 mas7;
30};
31
32struct kvmppc_vcpu_e500 {
33 /* Unmodified copy of the guest's TLB. */
34 struct tlbe *guest_tlb[E500_TLB_NUM];
35 /* TLB that's actually used when the guest is running. */
36 struct tlbe *shadow_tlb[E500_TLB_NUM];
37 /* Pages which are referenced in the shadow TLB. */
38 struct page **shadow_pages[E500_TLB_NUM];
39
40 unsigned int guest_tlb_size[E500_TLB_NUM];
41 unsigned int shadow_tlb_size[E500_TLB_NUM];
42 unsigned int guest_tlb_nv[E500_TLB_NUM];
43
44 u32 host_pid[E500_PID_NUM];
45 u32 pid[E500_PID_NUM];
46
47 u32 mas0;
48 u32 mas1;
49 u32 mas2;
50 u32 mas3;
51 u32 mas4;
52 u32 mas5;
53 u32 mas6;
54 u32 mas7;
55 u32 l1csr1;
56 u32 hid0;
57 u32 hid1;
58
59 struct kvm_vcpu vcpu;
60};
61
62static inline struct kvmppc_vcpu_e500 *to_e500(struct kvm_vcpu *vcpu)
63{
64 return container_of(vcpu, struct kvmppc_vcpu_e500, vcpu);
65}
66
67#endif /* __ASM_KVM_E500_H__ */
diff --git a/arch/powerpc/kvm/Kconfig b/arch/powerpc/kvm/Kconfig
index 6dbdc4817d80..146570550142 100644
--- a/arch/powerpc/kvm/Kconfig
+++ b/arch/powerpc/kvm/Kconfig
@@ -43,6 +43,19 @@ config KVM_EXIT_TIMING
43 43
44 If unsure, say N. 44 If unsure, say N.
45 45
46config KVM_E500
47 bool "KVM support for PowerPC E500 processors"
48 depends on EXPERIMENTAL && E500
49 select KVM
50 ---help---
51 Support running unmodified E500 guest kernels in virtual machines on
52 E500 host processors.
53
54 This module provides access to the hardware capabilities through
55 a character device node named /dev/kvm.
56
57 If unsure, say N.
58
46config KVM_TRACE 59config KVM_TRACE
47 bool "KVM trace support" 60 bool "KVM trace support"
48 depends on KVM && MARKERS && SYSFS 61 depends on KVM && MARKERS && SYSFS
diff --git a/arch/powerpc/kvm/Makefile b/arch/powerpc/kvm/Makefile
index 3ef5261828e2..4b2df66c79d8 100644
--- a/arch/powerpc/kvm/Makefile
+++ b/arch/powerpc/kvm/Makefile
@@ -22,3 +22,12 @@ kvm-440-objs := \
22 44x_tlb.o \ 22 44x_tlb.o \
23 44x_emulate.o 23 44x_emulate.o
24obj-$(CONFIG_KVM_440) += kvm-440.o 24obj-$(CONFIG_KVM_440) += kvm-440.o
25
26kvm-e500-objs := \
27 booke.o \
28 booke_emulate.o \
29 booke_interrupts.o \
30 e500.o \
31 e500_tlb.o \
32 e500_emulate.o
33obj-$(CONFIG_KVM_E500) += kvm-e500.o
diff --git a/arch/powerpc/kvm/e500.c b/arch/powerpc/kvm/e500.c
new file mode 100644
index 000000000000..7992da497cd4
--- /dev/null
+++ b/arch/powerpc/kvm/e500.c
@@ -0,0 +1,151 @@
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 derived from arch/powerpc/kvm/44x.c,
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#include <linux/kvm_host.h>
16#include <linux/err.h>
17
18#include <asm/reg.h>
19#include <asm/cputable.h>
20#include <asm/tlbflush.h>
21#include <asm/kvm_e500.h>
22#include <asm/kvm_ppc.h>
23
24#include "e500_tlb.h"
25
26void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
27{
28}
29
30void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
31{
32}
33
34void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
35{
36 kvmppc_e500_tlb_load(vcpu, cpu);
37}
38
39void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
40{
41 kvmppc_e500_tlb_put(vcpu);
42}
43
44int kvmppc_core_check_processor_compat(void)
45{
46 int r;
47
48 if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
49 r = 0;
50 else
51 r = -ENOTSUPP;
52
53 return r;
54}
55
56int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
57{
58 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
59
60 kvmppc_e500_tlb_setup(vcpu_e500);
61
62 /* Use the same core vertion as host's */
63 vcpu->arch.pvr = mfspr(SPRN_PVR);
64
65 return 0;
66}
67
68/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
69int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
70 struct kvm_translation *tr)
71{
72 int index;
73 gva_t eaddr;
74 u8 pid;
75 u8 as;
76
77 eaddr = tr->linear_address;
78 pid = (tr->linear_address >> 32) & 0xff;
79 as = (tr->linear_address >> 40) & 0x1;
80
81 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
82 if (index < 0) {
83 tr->valid = 0;
84 return 0;
85 }
86
87 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
88 /* XXX what does "writeable" and "usermode" even mean? */
89 tr->valid = 1;
90
91 return 0;
92}
93
94struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
95{
96 struct kvmppc_vcpu_e500 *vcpu_e500;
97 struct kvm_vcpu *vcpu;
98 int err;
99
100 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
101 if (!vcpu_e500) {
102 err = -ENOMEM;
103 goto out;
104 }
105
106 vcpu = &vcpu_e500->vcpu;
107 err = kvm_vcpu_init(vcpu, kvm, id);
108 if (err)
109 goto free_vcpu;
110
111 err = kvmppc_e500_tlb_init(vcpu_e500);
112 if (err)
113 goto uninit_vcpu;
114
115 return vcpu;
116
117uninit_vcpu:
118 kvm_vcpu_uninit(vcpu);
119free_vcpu:
120 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
121out:
122 return ERR_PTR(err);
123}
124
125void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
126{
127 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
128
129 kvmppc_e500_tlb_uninit(vcpu_e500);
130 kvm_vcpu_uninit(vcpu);
131 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
132}
133
134static int kvmppc_e500_init(void)
135{
136 int r;
137
138 r = kvmppc_booke_init();
139 if (r)
140 return r;
141
142 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), THIS_MODULE);
143}
144
145static void kvmppc_e500_exit(void)
146{
147 kvmppc_booke_exit();
148}
149
150module_init(kvmppc_e500_init);
151module_exit(kvmppc_e500_exit);
diff --git a/arch/powerpc/kvm/e500_emulate.c b/arch/powerpc/kvm/e500_emulate.c
new file mode 100644
index 000000000000..a47f44cd2cfd
--- /dev/null
+++ b/arch/powerpc/kvm/e500_emulate.c
@@ -0,0 +1,169 @@
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 derived from arch/powerpc/kvm/44x_emulate.c,
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#include <asm/kvm_ppc.h>
16#include <asm/disassemble.h>
17#include <asm/kvm_e500.h>
18
19#include "booke.h"
20#include "e500_tlb.h"
21
22#define XOP_TLBIVAX 786
23#define XOP_TLBSX 914
24#define XOP_TLBRE 946
25#define XOP_TLBWE 978
26
27int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
28 unsigned int inst, int *advance)
29{
30 int emulated = EMULATE_DONE;
31 int ra;
32 int rb;
33 int rs;
34 int rt;
35
36 switch (get_op(inst)) {
37 case 31:
38 switch (get_xop(inst)) {
39
40 case XOP_TLBRE:
41 emulated = kvmppc_e500_emul_tlbre(vcpu);
42 break;
43
44 case XOP_TLBWE:
45 emulated = kvmppc_e500_emul_tlbwe(vcpu);
46 break;
47
48 case XOP_TLBSX:
49 rb = get_rb(inst);
50 emulated = kvmppc_e500_emul_tlbsx(vcpu,rb);
51 break;
52
53 case XOP_TLBIVAX:
54 ra = get_ra(inst);
55 rb = get_rb(inst);
56 emulated = kvmppc_e500_emul_tlbivax(vcpu, ra, rb);
57 break;
58
59 default:
60 emulated = EMULATE_FAIL;
61 }
62
63 break;
64
65 default:
66 emulated = EMULATE_FAIL;
67 }
68
69 if (emulated == EMULATE_FAIL)
70 emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance);
71
72 return emulated;
73}
74
75int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
76{
77 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
78 int emulated = EMULATE_DONE;
79
80 switch (sprn) {
81 case SPRN_PID:
82 vcpu_e500->pid[0] = vcpu->arch.shadow_pid =
83 vcpu->arch.pid = vcpu->arch.gpr[rs];
84 break;
85 case SPRN_PID1:
86 vcpu_e500->pid[1] = vcpu->arch.gpr[rs]; break;
87 case SPRN_PID2:
88 vcpu_e500->pid[2] = vcpu->arch.gpr[rs]; break;
89 case SPRN_MAS0:
90 vcpu_e500->mas0 = vcpu->arch.gpr[rs]; break;
91 case SPRN_MAS1:
92 vcpu_e500->mas1 = vcpu->arch.gpr[rs]; break;
93 case SPRN_MAS2:
94 vcpu_e500->mas2 = vcpu->arch.gpr[rs]; break;
95 case SPRN_MAS3:
96 vcpu_e500->mas3 = vcpu->arch.gpr[rs]; break;
97 case SPRN_MAS4:
98 vcpu_e500->mas4 = vcpu->arch.gpr[rs]; break;
99 case SPRN_MAS6:
100 vcpu_e500->mas6 = vcpu->arch.gpr[rs]; break;
101 case SPRN_MAS7:
102 vcpu_e500->mas7 = vcpu->arch.gpr[rs]; break;
103 case SPRN_L1CSR1:
104 vcpu_e500->l1csr1 = vcpu->arch.gpr[rs]; break;
105 case SPRN_HID0:
106 vcpu_e500->hid0 = vcpu->arch.gpr[rs]; break;
107 case SPRN_HID1:
108 vcpu_e500->hid1 = vcpu->arch.gpr[rs]; break;
109
110 default:
111 emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, rs);
112 }
113
114 return emulated;
115}
116
117int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
118{
119 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
120 int emulated = EMULATE_DONE;
121
122 switch (sprn) {
123 case SPRN_PID:
124 vcpu->arch.gpr[rt] = vcpu_e500->pid[0]; break;
125 case SPRN_PID1:
126 vcpu->arch.gpr[rt] = vcpu_e500->pid[1]; break;
127 case SPRN_PID2:
128 vcpu->arch.gpr[rt] = vcpu_e500->pid[2]; break;
129 case SPRN_MAS0:
130 vcpu->arch.gpr[rt] = vcpu_e500->mas0; break;
131 case SPRN_MAS1:
132 vcpu->arch.gpr[rt] = vcpu_e500->mas1; break;
133 case SPRN_MAS2:
134 vcpu->arch.gpr[rt] = vcpu_e500->mas2; break;
135 case SPRN_MAS3:
136 vcpu->arch.gpr[rt] = vcpu_e500->mas3; break;
137 case SPRN_MAS4:
138 vcpu->arch.gpr[rt] = vcpu_e500->mas4; break;
139 case SPRN_MAS6:
140 vcpu->arch.gpr[rt] = vcpu_e500->mas6; break;
141 case SPRN_MAS7:
142 vcpu->arch.gpr[rt] = vcpu_e500->mas7; break;
143
144 case SPRN_TLB0CFG:
145 vcpu->arch.gpr[rt] = mfspr(SPRN_TLB0CFG);
146 vcpu->arch.gpr[rt] &= ~0xfffUL;
147 vcpu->arch.gpr[rt] |= vcpu_e500->guest_tlb_size[0];
148 break;
149
150 case SPRN_TLB1CFG:
151 vcpu->arch.gpr[rt] = mfspr(SPRN_TLB1CFG);
152 vcpu->arch.gpr[rt] &= ~0xfffUL;
153 vcpu->arch.gpr[rt] |= vcpu_e500->guest_tlb_size[1];
154 break;
155
156 case SPRN_L1CSR1:
157 vcpu->arch.gpr[rt] = vcpu_e500->l1csr1; break;
158 case SPRN_HID0:
159 vcpu->arch.gpr[rt] = vcpu_e500->hid0; break;
160 case SPRN_HID1:
161 vcpu->arch.gpr[rt] = vcpu_e500->hid1; break;
162
163 default:
164 emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, rt);
165 }
166
167 return emulated;
168}
169
diff --git a/arch/powerpc/kvm/e500_tlb.c b/arch/powerpc/kvm/e500_tlb.c
new file mode 100644
index 000000000000..6a50340f575e
--- /dev/null
+++ b/arch/powerpc/kvm/e500_tlb.c
@@ -0,0 +1,737 @@
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.c,
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#include <linux/types.h>
16#include <linux/string.h>
17#include <linux/kvm.h>
18#include <linux/kvm_host.h>
19#include <linux/highmem.h>
20#include <asm/kvm_ppc.h>
21#include <asm/kvm_e500.h>
22
23#include "e500_tlb.h"
24
25#define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1)
26
27static unsigned int tlb1_entry_num;
28
29void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
30{
31 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
32 struct tlbe *tlbe;
33 int i, tlbsel;
34
35 printk("| %8s | %8s | %8s | %8s | %8s |\n",
36 "nr", "mas1", "mas2", "mas3", "mas7");
37
38 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
39 printk("Guest TLB%d:\n", tlbsel);
40 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
41 tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
42 if (tlbe->mas1 & MAS1_VALID)
43 printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
44 tlbsel, i, tlbe->mas1, tlbe->mas2,
45 tlbe->mas3, tlbe->mas7);
46 }
47 }
48
49 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
50 printk("Shadow TLB%d:\n", tlbsel);
51 for (i = 0; i < vcpu_e500->shadow_tlb_size[tlbsel]; i++) {
52 tlbe = &vcpu_e500->shadow_tlb[tlbsel][i];
53 if (tlbe->mas1 & MAS1_VALID)
54 printk(" S[%d][%3d] | %08X | %08X | %08X | %08X |\n",
55 tlbsel, i, tlbe->mas1, tlbe->mas2,
56 tlbe->mas3, tlbe->mas7);
57 }
58 }
59}
60
61static inline unsigned int tlb0_get_next_victim(
62 struct kvmppc_vcpu_e500 *vcpu_e500)
63{
64 unsigned int victim;
65
66 victim = vcpu_e500->guest_tlb_nv[0]++;
67 if (unlikely(vcpu_e500->guest_tlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
68 vcpu_e500->guest_tlb_nv[0] = 0;
69
70 return victim;
71}
72
73static inline unsigned int tlb1_max_shadow_size(void)
74{
75 return tlb1_entry_num - tlbcam_index;
76}
77
78static inline int tlbe_is_writable(struct tlbe *tlbe)
79{
80 return tlbe->mas3 & (MAS3_SW|MAS3_UW);
81}
82
83static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
84{
85 /* Mask off reserved bits. */
86 mas3 &= MAS3_ATTRIB_MASK;
87
88 if (!usermode) {
89 /* Guest is in supervisor mode,
90 * so we need to translate guest
91 * supervisor permissions into user permissions. */
92 mas3 &= ~E500_TLB_USER_PERM_MASK;
93 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
94 }
95
96 return mas3 | E500_TLB_SUPER_PERM_MASK;
97}
98
99static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
100{
101 return mas2 & MAS2_ATTRIB_MASK;
102}
103
104/*
105 * writing shadow tlb entry to host TLB
106 */
107static inline void __write_host_tlbe(struct tlbe *stlbe)
108{
109 mtspr(SPRN_MAS1, stlbe->mas1);
110 mtspr(SPRN_MAS2, stlbe->mas2);
111 mtspr(SPRN_MAS3, stlbe->mas3);
112 mtspr(SPRN_MAS7, stlbe->mas7);
113 __asm__ __volatile__ ("tlbwe\n" : : );
114}
115
116static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
117 int tlbsel, int esel)
118{
119 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
120
121 local_irq_disable();
122 if (tlbsel == 0) {
123 __write_host_tlbe(stlbe);
124 } else {
125 unsigned register mas0;
126
127 mas0 = mfspr(SPRN_MAS0);
128
129 mtspr(SPRN_MAS0, MAS0_TLBSEL(1) | MAS0_ESEL(to_htlb1_esel(esel)));
130 __write_host_tlbe(stlbe);
131
132 mtspr(SPRN_MAS0, mas0);
133 }
134 local_irq_enable();
135}
136
137void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
138{
139 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
140 int i;
141 unsigned register mas0;
142
143 /* Load all valid TLB1 entries to reduce guest tlb miss fault */
144 local_irq_disable();
145 mas0 = mfspr(SPRN_MAS0);
146 for (i = 0; i < tlb1_max_shadow_size(); i++) {
147 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
148
149 if (get_tlb_v(stlbe)) {
150 mtspr(SPRN_MAS0, MAS0_TLBSEL(1)
151 | MAS0_ESEL(to_htlb1_esel(i)));
152 __write_host_tlbe(stlbe);
153 }
154 }
155 mtspr(SPRN_MAS0, mas0);
156 local_irq_enable();
157}
158
159void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
160{
161 _tlbia();
162}
163
164/* Search the guest TLB for a matching entry. */
165static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
166 gva_t eaddr, int tlbsel, unsigned int pid, int as)
167{
168 int i;
169
170 /* XXX Replace loop with fancy data structures. */
171 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
172 struct tlbe *tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
173 unsigned int tid;
174
175 if (eaddr < get_tlb_eaddr(tlbe))
176 continue;
177
178 if (eaddr > get_tlb_end(tlbe))
179 continue;
180
181 tid = get_tlb_tid(tlbe);
182 if (tid && (tid != pid))
183 continue;
184
185 if (!get_tlb_v(tlbe))
186 continue;
187
188 if (get_tlb_ts(tlbe) != as && as != -1)
189 continue;
190
191 return i;
192 }
193
194 return -1;
195}
196
197static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500 *vcpu_e500,
198 int tlbsel, int esel)
199{
200 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
201 struct page *page = vcpu_e500->shadow_pages[tlbsel][esel];
202
203 if (page) {
204 vcpu_e500->shadow_pages[tlbsel][esel] = NULL;
205
206 if (get_tlb_v(stlbe)) {
207 if (tlbe_is_writable(stlbe))
208 kvm_release_page_dirty(page);
209 else
210 kvm_release_page_clean(page);
211 }
212 }
213}
214
215static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
216 int tlbsel, int esel)
217{
218 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
219
220 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
221 stlbe->mas1 = 0;
222 KVMTRACE_5D(STLB_INVAL, &vcpu_e500->vcpu, index_of(tlbsel, esel),
223 stlbe->mas1, stlbe->mas2, stlbe->mas3, stlbe->mas7,
224 handler);
225}
226
227static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
228 gva_t eaddr, gva_t eend, u32 tid)
229{
230 unsigned int pid = tid & 0xff;
231 unsigned int i;
232
233 /* XXX Replace loop with fancy data structures. */
234 for (i = 0; i < vcpu_e500->guest_tlb_size[1]; i++) {
235 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
236 unsigned int tid;
237
238 if (!get_tlb_v(stlbe))
239 continue;
240
241 if (eend < get_tlb_eaddr(stlbe))
242 continue;
243
244 if (eaddr > get_tlb_end(stlbe))
245 continue;
246
247 tid = get_tlb_tid(stlbe);
248 if (tid && (tid != pid))
249 continue;
250
251 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
252 write_host_tlbe(vcpu_e500, 1, i);
253 }
254}
255
256static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
257 unsigned int eaddr, int as)
258{
259 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
260 unsigned int victim, pidsel, tsized;
261 int tlbsel;
262
263 /* since we only have tow TLBs, only lower bit is used. */
264 tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
265 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
266 pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
267 tsized = (vcpu_e500->mas4 >> 8) & 0xf;
268
269 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
270 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
271 vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
272 | MAS1_TID(vcpu_e500->pid[pidsel])
273 | MAS1_TSIZE(tsized);
274 vcpu_e500->mas2 = (eaddr & MAS2_EPN)
275 | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
276 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
277 vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
278 | (get_cur_pid(vcpu) << 16)
279 | (as ? MAS6_SAS : 0);
280 vcpu_e500->mas7 = 0;
281}
282
283static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
284 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel)
285{
286 struct page *new_page;
287 struct tlbe *stlbe;
288 hpa_t hpaddr;
289
290 stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
291
292 /* Get reference to new page. */
293 new_page = gfn_to_page(vcpu_e500->vcpu.kvm, gfn);
294 if (is_error_page(new_page)) {
295 printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
296 kvm_release_page_clean(new_page);
297 return;
298 }
299 hpaddr = page_to_phys(new_page);
300
301 /* Drop reference to old page. */
302 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
303
304 vcpu_e500->shadow_pages[tlbsel][esel] = new_page;
305
306 /* Force TS=1 IPROT=0 TSIZE=4KB for all guest mappings. */
307 stlbe->mas1 = MAS1_TSIZE(BOOKE_PAGESZ_4K)
308 | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID;
309 stlbe->mas2 = (gvaddr & MAS2_EPN)
310 | e500_shadow_mas2_attrib(gtlbe->mas2,
311 vcpu_e500->vcpu.arch.msr & MSR_PR);
312 stlbe->mas3 = (hpaddr & MAS3_RPN)
313 | e500_shadow_mas3_attrib(gtlbe->mas3,
314 vcpu_e500->vcpu.arch.msr & MSR_PR);
315 stlbe->mas7 = (hpaddr >> 32) & MAS7_RPN;
316
317 KVMTRACE_5D(STLB_WRITE, &vcpu_e500->vcpu, index_of(tlbsel, esel),
318 stlbe->mas1, stlbe->mas2, stlbe->mas3, stlbe->mas7,
319 handler);
320}
321
322/* XXX only map the one-one case, for now use TLB0 */
323static int kvmppc_e500_stlbe_map(struct kvmppc_vcpu_e500 *vcpu_e500,
324 int tlbsel, int esel)
325{
326 struct tlbe *gtlbe;
327
328 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
329
330 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
331 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
332 gtlbe, tlbsel, esel);
333
334 return esel;
335}
336
337/* Caller must ensure that the specified guest TLB entry is safe to insert into
338 * the shadow TLB. */
339/* XXX for both one-one and one-to-many , for now use TLB1 */
340static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
341 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe)
342{
343 unsigned int victim;
344
345 victim = vcpu_e500->guest_tlb_nv[1]++;
346
347 if (unlikely(vcpu_e500->guest_tlb_nv[1] >= tlb1_max_shadow_size()))
348 vcpu_e500->guest_tlb_nv[1] = 0;
349
350 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim);
351
352 return victim;
353}
354
355/* Invalidate all guest kernel mappings when enter usermode,
356 * so that when they fault back in they will get the
357 * proper permission bits. */
358void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
359{
360 if (usermode) {
361 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
362 int i;
363
364 /* XXX Replace loop with fancy data structures. */
365 /* needn't set modified since tlbia will make TLB1 coherent */
366 for (i = 0; i < tlb1_max_shadow_size(); i++)
367 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
368
369 _tlbia();
370 }
371}
372
373static int kvmppc_e500_gtlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
374 int tlbsel, int esel)
375{
376 struct tlbe *gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
377
378 if (unlikely(get_tlb_iprot(gtlbe)))
379 return -1;
380
381 if (tlbsel == 1) {
382 kvmppc_e500_tlb1_invalidate(vcpu_e500, get_tlb_eaddr(gtlbe),
383 get_tlb_end(gtlbe),
384 get_tlb_tid(gtlbe));
385 } else {
386 kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
387 }
388
389 gtlbe->mas1 = 0;
390
391 return 0;
392}
393
394int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
395{
396 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
397 unsigned int ia;
398 int esel, tlbsel;
399 gva_t ea;
400
401 ea = ((ra) ? vcpu->arch.gpr[ra] : 0) + vcpu->arch.gpr[rb];
402
403 ia = (ea >> 2) & 0x1;
404
405 /* since we only have tow TLBs, only lower bit is used. */
406 tlbsel = (ea >> 3) & 0x1;
407
408 if (ia) {
409 /* invalidate all entries */
410 for (esel = 0; esel < vcpu_e500->guest_tlb_size[tlbsel]; esel++)
411 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
412 } else {
413 ea &= 0xfffff000;
414 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
415 get_cur_pid(vcpu), -1);
416 if (esel >= 0)
417 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
418 }
419
420 _tlbia();
421
422 return EMULATE_DONE;
423}
424
425int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
426{
427 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
428 int tlbsel, esel;
429 struct tlbe *gtlbe;
430
431 tlbsel = get_tlb_tlbsel(vcpu_e500);
432 esel = get_tlb_esel(vcpu_e500, tlbsel);
433
434 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
435 vcpu_e500->mas0 &= MAS0_NV(0);
436 vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
437 vcpu_e500->mas1 = gtlbe->mas1;
438 vcpu_e500->mas2 = gtlbe->mas2;
439 vcpu_e500->mas3 = gtlbe->mas3;
440 vcpu_e500->mas7 = gtlbe->mas7;
441
442 return EMULATE_DONE;
443}
444
445int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
446{
447 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
448 int as = !!get_cur_sas(vcpu_e500);
449 unsigned int pid = get_cur_spid(vcpu_e500);
450 int esel, tlbsel;
451 struct tlbe *gtlbe = NULL;
452 gva_t ea;
453
454 ea = vcpu->arch.gpr[rb];
455
456 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
457 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
458 if (esel >= 0) {
459 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
460 break;
461 }
462 }
463
464 if (gtlbe) {
465 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
466 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
467 vcpu_e500->mas1 = gtlbe->mas1;
468 vcpu_e500->mas2 = gtlbe->mas2;
469 vcpu_e500->mas3 = gtlbe->mas3;
470 vcpu_e500->mas7 = gtlbe->mas7;
471 } else {
472 int victim;
473
474 /* since we only have tow TLBs, only lower bit is used. */
475 tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
476 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
477
478 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
479 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
480 vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
481 | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
482 | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
483 vcpu_e500->mas2 &= MAS2_EPN;
484 vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
485 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
486 vcpu_e500->mas7 = 0;
487 }
488
489 return EMULATE_DONE;
490}
491
492int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
493{
494 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
495 u64 eaddr;
496 u64 raddr;
497 u32 tid;
498 struct tlbe *gtlbe;
499 int tlbsel, esel, stlbsel, sesel;
500
501 tlbsel = get_tlb_tlbsel(vcpu_e500);
502 esel = get_tlb_esel(vcpu_e500, tlbsel);
503
504 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
505
506 if (get_tlb_v(gtlbe) && tlbsel == 1) {
507 eaddr = get_tlb_eaddr(gtlbe);
508 tid = get_tlb_tid(gtlbe);
509 kvmppc_e500_tlb1_invalidate(vcpu_e500, eaddr,
510 get_tlb_end(gtlbe), tid);
511 }
512
513 gtlbe->mas1 = vcpu_e500->mas1;
514 gtlbe->mas2 = vcpu_e500->mas2;
515 gtlbe->mas3 = vcpu_e500->mas3;
516 gtlbe->mas7 = vcpu_e500->mas7;
517
518 KVMTRACE_5D(GTLB_WRITE, vcpu, vcpu_e500->mas0,
519 gtlbe->mas1, gtlbe->mas2, gtlbe->mas3, gtlbe->mas7,
520 handler);
521
522 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
523 if (tlbe_is_host_safe(vcpu, gtlbe)) {
524 switch (tlbsel) {
525 case 0:
526 /* TLB0 */
527 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
528 gtlbe->mas1 |= MAS1_TSIZE(BOOKE_PAGESZ_4K);
529
530 stlbsel = 0;
531 sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel);
532
533 break;
534
535 case 1:
536 /* TLB1 */
537 eaddr = get_tlb_eaddr(gtlbe);
538 raddr = get_tlb_raddr(gtlbe);
539
540 /* Create a 4KB mapping on the host.
541 * If the guest wanted a large page,
542 * only the first 4KB is mapped here and the rest
543 * are mapped on the fly. */
544 stlbsel = 1;
545 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
546 raddr >> PAGE_SHIFT, gtlbe);
547 break;
548
549 default:
550 BUG();
551 }
552 write_host_tlbe(vcpu_e500, stlbsel, sesel);
553 }
554
555 return EMULATE_DONE;
556}
557
558int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
559{
560 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
561
562 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
563}
564
565int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
566{
567 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
568
569 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
570}
571
572void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
573{
574 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
575
576 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
577}
578
579void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
580{
581 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
582
583 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
584}
585
586gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
587 gva_t eaddr)
588{
589 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
590 struct tlbe *gtlbe =
591 &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)];
592 u64 pgmask = get_tlb_bytes(gtlbe) - 1;
593
594 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
595}
596
597void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
598{
599 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
600 int tlbsel, i;
601
602 for (tlbsel = 0; tlbsel < 2; tlbsel++)
603 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++)
604 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i);
605
606 /* discard all guest mapping */
607 _tlbia();
608}
609
610void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
611 unsigned int index)
612{
613 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
614 int tlbsel = tlbsel_of(index);
615 int esel = esel_of(index);
616 int stlbsel, sesel;
617
618 switch (tlbsel) {
619 case 0:
620 stlbsel = 0;
621 sesel = esel;
622 break;
623
624 case 1: {
625 gfn_t gfn = gpaddr >> PAGE_SHIFT;
626 struct tlbe *gtlbe
627 = &vcpu_e500->guest_tlb[tlbsel][esel];
628
629 stlbsel = 1;
630 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe);
631 break;
632 }
633
634 default:
635 BUG();
636 break;
637 }
638 write_host_tlbe(vcpu_e500, stlbsel, sesel);
639}
640
641int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
642 gva_t eaddr, unsigned int pid, int as)
643{
644 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
645 int esel, tlbsel;
646
647 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
648 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
649 if (esel >= 0)
650 return index_of(tlbsel, esel);
651 }
652
653 return -1;
654}
655
656void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
657{
658 struct tlbe *tlbe;
659
660 /* Insert large initial mapping for guest. */
661 tlbe = &vcpu_e500->guest_tlb[1][0];
662 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOKE_PAGESZ_256M);
663 tlbe->mas2 = 0;
664 tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
665 tlbe->mas7 = 0;
666
667 /* 4K map for serial output. Used by kernel wrapper. */
668 tlbe = &vcpu_e500->guest_tlb[1][1];
669 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOKE_PAGESZ_4K);
670 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
671 tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
672 tlbe->mas7 = 0;
673}
674
675int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
676{
677 tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
678
679 vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE;
680 vcpu_e500->guest_tlb[0] =
681 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
682 if (vcpu_e500->guest_tlb[0] == NULL)
683 goto err_out;
684
685 vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE;
686 vcpu_e500->shadow_tlb[0] =
687 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
688 if (vcpu_e500->shadow_tlb[0] == NULL)
689 goto err_out_guest0;
690
691 vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE;
692 vcpu_e500->guest_tlb[1] =
693 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
694 if (vcpu_e500->guest_tlb[1] == NULL)
695 goto err_out_shadow0;
696
697 vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num;
698 vcpu_e500->shadow_tlb[1] =
699 kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL);
700 if (vcpu_e500->shadow_tlb[1] == NULL)
701 goto err_out_guest1;
702
703 vcpu_e500->shadow_pages[0] = (struct page **)
704 kzalloc(sizeof(struct page *) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
705 if (vcpu_e500->shadow_pages[0] == NULL)
706 goto err_out_shadow1;
707
708 vcpu_e500->shadow_pages[1] = (struct page **)
709 kzalloc(sizeof(struct page *) * tlb1_entry_num, GFP_KERNEL);
710 if (vcpu_e500->shadow_pages[1] == NULL)
711 goto err_out_page0;
712
713 return 0;
714
715err_out_page0:
716 kfree(vcpu_e500->shadow_pages[0]);
717err_out_shadow1:
718 kfree(vcpu_e500->shadow_tlb[1]);
719err_out_guest1:
720 kfree(vcpu_e500->guest_tlb[1]);
721err_out_shadow0:
722 kfree(vcpu_e500->shadow_tlb[0]);
723err_out_guest0:
724 kfree(vcpu_e500->guest_tlb[0]);
725err_out:
726 return -1;
727}
728
729void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
730{
731 kfree(vcpu_e500->shadow_pages[1]);
732 kfree(vcpu_e500->shadow_pages[0]);
733 kfree(vcpu_e500->shadow_tlb[1]);
734 kfree(vcpu_e500->guest_tlb[1]);
735 kfree(vcpu_e500->shadow_tlb[0]);
736 kfree(vcpu_e500->guest_tlb[0]);
737}
diff --git a/arch/powerpc/kvm/e500_tlb.h b/arch/powerpc/kvm/e500_tlb.h
new file mode 100644
index 000000000000..d8833f955163
--- /dev/null
+++ b/arch/powerpc/kvm/e500_tlb.h
@@ -0,0 +1,184 @@
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>
19#include <asm/mmu-fsl-booke.h>
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 \
41 (MAS2_X0 | MAS2_X1 | MAS2_W | MAS2_I | MAS2_M | MAS2_G | MAS2_E)
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 *);
47extern int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *);
48extern int kvmppc_e500_emul_tlbre(struct kvm_vcpu *);
49extern int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *, int, int);
50extern int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *, int);
51extern int kvmppc_e500_tlb_search(struct kvm_vcpu *, gva_t, unsigned int, int);
52extern void kvmppc_e500_tlb_put(struct kvm_vcpu *);
53extern void kvmppc_e500_tlb_load(struct kvm_vcpu *, int);
54extern int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *);
55extern void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *);
56extern void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *);
57
58/* TLB helper functions */
59static inline unsigned int get_tlb_size(const struct tlbe *tlbe)
60{
61 return (tlbe->mas1 >> 8) & 0xf;
62}
63
64static inline gva_t get_tlb_eaddr(const struct tlbe *tlbe)
65{
66 return tlbe->mas2 & 0xfffff000;
67}
68
69static inline u64 get_tlb_bytes(const struct tlbe *tlbe)
70{
71 unsigned int pgsize = get_tlb_size(tlbe);
72 return 1ULL << 10 << (pgsize << 1);
73}
74
75static inline gva_t get_tlb_end(const struct tlbe *tlbe)
76{
77 u64 bytes = get_tlb_bytes(tlbe);
78 return get_tlb_eaddr(tlbe) + bytes - 1;
79}
80
81static inline u64 get_tlb_raddr(const struct tlbe *tlbe)
82{
83 u64 rpn = tlbe->mas7;
84 return (rpn << 32) | (tlbe->mas3 & 0xfffff000);
85}
86
87static inline unsigned int get_tlb_tid(const struct tlbe *tlbe)
88{
89 return (tlbe->mas1 >> 16) & 0xff;
90}
91
92static inline unsigned int get_tlb_ts(const struct tlbe *tlbe)
93{
94 return (tlbe->mas1 >> 12) & 0x1;
95}
96
97static inline unsigned int get_tlb_v(const struct tlbe *tlbe)
98{
99 return (tlbe->mas1 >> 31) & 0x1;
100}
101
102static inline unsigned int get_tlb_iprot(const struct tlbe *tlbe)
103{
104 return (tlbe->mas1 >> 30) & 0x1;
105}
106
107static inline unsigned int get_cur_pid(struct kvm_vcpu *vcpu)
108{
109 return vcpu->arch.pid & 0xff;
110}
111
112static inline unsigned int get_cur_spid(
113 const struct kvmppc_vcpu_e500 *vcpu_e500)
114{
115 return (vcpu_e500->mas6 >> 16) & 0xff;
116}
117
118static inline unsigned int get_cur_sas(
119 const struct kvmppc_vcpu_e500 *vcpu_e500)
120{
121 return vcpu_e500->mas6 & 0x1;
122}
123
124static inline unsigned int get_tlb_tlbsel(
125 const struct kvmppc_vcpu_e500 *vcpu_e500)
126{
127 /*
128 * Manual says that tlbsel has 2 bits wide.
129 * Since we only have tow TLBs, only lower bit is used.
130 */
131 return (vcpu_e500->mas0 >> 28) & 0x1;
132}
133
134static inline unsigned int get_tlb_nv_bit(
135 const struct kvmppc_vcpu_e500 *vcpu_e500)
136{
137 return vcpu_e500->mas0 & 0xfff;
138}
139
140static inline unsigned int get_tlb_esel_bit(
141 const struct kvmppc_vcpu_e500 *vcpu_e500)
142{
143 return (vcpu_e500->mas0 >> 16) & 0xfff;
144}
145
146static inline unsigned int get_tlb_esel(
147 const struct kvmppc_vcpu_e500 *vcpu_e500,
148 int tlbsel)
149{
150 unsigned int esel = get_tlb_esel_bit(vcpu_e500);
151
152 if (tlbsel == 0) {
153 esel &= KVM_E500_TLB0_WAY_NUM_MASK;
154 esel |= ((vcpu_e500->mas2 >> 12) & KVM_E500_TLB0_WAY_SIZE_MASK)
155 << KVM_E500_TLB0_WAY_NUM_BIT;
156 } else {
157 esel &= KVM_E500_TLB1_SIZE - 1;
158 }
159
160 return esel;
161}
162
163static inline int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
164 const struct tlbe *tlbe)
165{
166 gpa_t gpa;
167
168 if (!get_tlb_v(tlbe))
169 return 0;
170
171 /* Does it match current guest AS? */
172 /* XXX what about IS != DS? */
173 if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS))
174 return 0;
175
176 gpa = get_tlb_raddr(tlbe);
177 if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
178 /* Mapping is not for RAM. */
179 return 0;
180
181 return 1;
182}
183
184#endif /* __KVM_E500_TLB_H__ */