blob: 0291c3cf5055b908a66072cc1fa0cc98a559651d [file] [log] [blame]
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001/*
Scott Wood49ea0692011-03-28 15:01:24 -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.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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060017#include <linux/string.h>
18#include <linux/kvm.h>
19#include <linux/kvm_host.h>
20#include <linux/highmem.h>
21#include <asm/kvm_ppc.h>
22#include <asm/kvm_e500.h>
23
Liu Yu9aa4dd52009-01-14 10:47:38 -060024#include "../mm/mmu_decl.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060025#include "e500_tlb.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030026#include "trace.h"
Scott Wood49ea0692011-03-28 15:01:24 -050027#include "timing.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060028
29#define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1)
30
31static unsigned int tlb1_entry_num;
32
33void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
34{
35 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
36 struct tlbe *tlbe;
37 int i, tlbsel;
38
39 printk("| %8s | %8s | %8s | %8s | %8s |\n",
40 "nr", "mas1", "mas2", "mas3", "mas7");
41
42 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
43 printk("Guest TLB%d:\n", tlbsel);
44 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
45 tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
46 if (tlbe->mas1 & MAS1_VALID)
47 printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
48 tlbsel, i, tlbe->mas1, tlbe->mas2,
49 tlbe->mas3, tlbe->mas7);
50 }
51 }
52
53 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
54 printk("Shadow TLB%d:\n", tlbsel);
55 for (i = 0; i < vcpu_e500->shadow_tlb_size[tlbsel]; i++) {
56 tlbe = &vcpu_e500->shadow_tlb[tlbsel][i];
57 if (tlbe->mas1 & MAS1_VALID)
58 printk(" S[%d][%3d] | %08X | %08X | %08X | %08X |\n",
59 tlbsel, i, tlbe->mas1, tlbe->mas2,
60 tlbe->mas3, tlbe->mas7);
61 }
62 }
63}
64
65static inline unsigned int tlb0_get_next_victim(
66 struct kvmppc_vcpu_e500 *vcpu_e500)
67{
68 unsigned int victim;
69
70 victim = vcpu_e500->guest_tlb_nv[0]++;
71 if (unlikely(vcpu_e500->guest_tlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
72 vcpu_e500->guest_tlb_nv[0] = 0;
73
74 return victim;
75}
76
77static inline unsigned int tlb1_max_shadow_size(void)
78{
79 return tlb1_entry_num - tlbcam_index;
80}
81
82static inline int tlbe_is_writable(struct tlbe *tlbe)
83{
84 return tlbe->mas3 & (MAS3_SW|MAS3_UW);
85}
86
87static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
88{
89 /* Mask off reserved bits. */
90 mas3 &= MAS3_ATTRIB_MASK;
91
92 if (!usermode) {
93 /* Guest is in supervisor mode,
94 * so we need to translate guest
95 * supervisor permissions into user permissions. */
96 mas3 &= ~E500_TLB_USER_PERM_MASK;
97 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
98 }
99
100 return mas3 | E500_TLB_SUPER_PERM_MASK;
101}
102
103static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
104{
Liu Yu046a48b2009-03-17 16:57:46 +0800105#ifdef CONFIG_SMP
106 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
107#else
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600108 return mas2 & MAS2_ATTRIB_MASK;
Liu Yu046a48b2009-03-17 16:57:46 +0800109#endif
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600110}
111
112/*
113 * writing shadow tlb entry to host TLB
114 */
Scott Wood0ef3099562011-06-14 18:34:35 -0500115static inline void __write_host_tlbe(struct tlbe *stlbe, uint32_t mas0)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600116{
Scott Wood0ef3099562011-06-14 18:34:35 -0500117 unsigned long flags;
118
119 local_irq_save(flags);
120 mtspr(SPRN_MAS0, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600121 mtspr(SPRN_MAS1, stlbe->mas1);
122 mtspr(SPRN_MAS2, stlbe->mas2);
123 mtspr(SPRN_MAS3, stlbe->mas3);
124 mtspr(SPRN_MAS7, stlbe->mas7);
Scott Wood0ef3099562011-06-14 18:34:35 -0500125 asm volatile("isync; tlbwe" : : : "memory");
126 local_irq_restore(flags);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600127}
128
129static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
130 int tlbsel, int esel)
131{
132 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
133
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600134 if (tlbsel == 0) {
Scott Wood0ef3099562011-06-14 18:34:35 -0500135 __write_host_tlbe(stlbe,
136 MAS0_TLBSEL(0) |
137 MAS0_ESEL(esel & (KVM_E500_TLB0_WAY_NUM - 1)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600138 } else {
Scott Wood0ef3099562011-06-14 18:34:35 -0500139 __write_host_tlbe(stlbe,
140 MAS0_TLBSEL(1) |
141 MAS0_ESEL(to_htlb1_esel(esel)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600142 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600143}
144
145void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
146{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600147}
148
149void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
150{
Liu Yu9aa4dd52009-01-14 10:47:38 -0600151 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600152}
153
154/* Search the guest TLB for a matching entry. */
155static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
156 gva_t eaddr, int tlbsel, unsigned int pid, int as)
157{
158 int i;
159
160 /* XXX Replace loop with fancy data structures. */
161 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
162 struct tlbe *tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
163 unsigned int tid;
164
165 if (eaddr < get_tlb_eaddr(tlbe))
166 continue;
167
168 if (eaddr > get_tlb_end(tlbe))
169 continue;
170
171 tid = get_tlb_tid(tlbe);
172 if (tid && (tid != pid))
173 continue;
174
175 if (!get_tlb_v(tlbe))
176 continue;
177
178 if (get_tlb_ts(tlbe) != as && as != -1)
179 continue;
180
181 return i;
182 }
183
184 return -1;
185}
186
187static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500 *vcpu_e500,
188 int tlbsel, int esel)
189{
190 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
Scott Wood59c1f4e2011-06-14 18:34:37 -0500191 unsigned long pfn;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600192
Scott Wood59c1f4e2011-06-14 18:34:37 -0500193 pfn = stlbe->mas3 >> PAGE_SHIFT;
194 pfn |= stlbe->mas7 << (32 - PAGE_SHIFT);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600195
Scott Wood59c1f4e2011-06-14 18:34:37 -0500196 if (get_tlb_v(stlbe)) {
197 if (tlbe_is_writable(stlbe))
198 kvm_release_pfn_dirty(pfn);
199 else
200 kvm_release_pfn_clean(pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600201 }
202}
203
204static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
205 int tlbsel, int esel)
206{
207 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
208
209 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
210 stlbe->mas1 = 0;
Kyle Moffett21e537b2010-08-30 11:38:39 -0400211 trace_kvm_stlb_inval(index_of(tlbsel, esel));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600212}
213
214static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
215 gva_t eaddr, gva_t eend, u32 tid)
216{
217 unsigned int pid = tid & 0xff;
218 unsigned int i;
219
220 /* XXX Replace loop with fancy data structures. */
221 for (i = 0; i < vcpu_e500->guest_tlb_size[1]; i++) {
222 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
223 unsigned int tid;
224
225 if (!get_tlb_v(stlbe))
226 continue;
227
228 if (eend < get_tlb_eaddr(stlbe))
229 continue;
230
231 if (eaddr > get_tlb_end(stlbe))
232 continue;
233
234 tid = get_tlb_tid(stlbe);
235 if (tid && (tid != pid))
236 continue;
237
238 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
239 write_host_tlbe(vcpu_e500, 1, i);
240 }
241}
242
243static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
244 unsigned int eaddr, int as)
245{
246 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
247 unsigned int victim, pidsel, tsized;
248 int tlbsel;
249
Liu Yufb2838d2009-01-14 10:47:37 -0600250 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600251 tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
252 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
253 pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
Liu Yu0cfb50e2009-06-05 14:54:29 +0800254 tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600255
256 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
257 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
258 vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
259 | MAS1_TID(vcpu_e500->pid[pidsel])
260 | MAS1_TSIZE(tsized);
261 vcpu_e500->mas2 = (eaddr & MAS2_EPN)
262 | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
263 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
264 vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
265 | (get_cur_pid(vcpu) << 16)
266 | (as ? MAS6_SAS : 0);
267 vcpu_e500->mas7 = 0;
268}
269
270static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
271 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel)
272{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600273 struct tlbe *stlbe;
Scott Wood59c1f4e2011-06-14 18:34:37 -0500274 unsigned long pfn;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600275
276 stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
277
Scott Wood59c1f4e2011-06-14 18:34:37 -0500278 /*
279 * Translate guest physical to true physical, acquiring
280 * a page reference if it is normal, non-reserved memory.
281 */
282 pfn = gfn_to_pfn(vcpu_e500->vcpu.kvm, gfn);
283 if (is_error_pfn(pfn)) {
284 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
Alexander Graf344941b2010-08-31 03:45:39 +0200285 (long)gfn);
Scott Wood59c1f4e2011-06-14 18:34:37 -0500286 kvm_release_pfn_clean(pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600287 return;
288 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600289
290 /* Drop reference to old page. */
291 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
292
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600293 /* Force TS=1 IPROT=0 TSIZE=4KB for all guest mappings. */
Liu Yu0cfb50e2009-06-05 14:54:29 +0800294 stlbe->mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600295 | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID;
296 stlbe->mas2 = (gvaddr & MAS2_EPN)
297 | e500_shadow_mas2_attrib(gtlbe->mas2,
Alexander Graf666e7252010-07-29 14:47:43 +0200298 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
Scott Wood59c1f4e2011-06-14 18:34:37 -0500299 stlbe->mas3 = ((pfn << PAGE_SHIFT) & MAS3_RPN)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600300 | e500_shadow_mas3_attrib(gtlbe->mas3,
Alexander Graf666e7252010-07-29 14:47:43 +0200301 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
Scott Wood59c1f4e2011-06-14 18:34:37 -0500302 stlbe->mas7 = (pfn >> (32 - PAGE_SHIFT)) & MAS7_RPN;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600303
Marcelo Tosatti46f43c62009-06-18 11:47:27 -0300304 trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
305 stlbe->mas3, stlbe->mas7);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600306}
307
308/* XXX only map the one-one case, for now use TLB0 */
309static int kvmppc_e500_stlbe_map(struct kvmppc_vcpu_e500 *vcpu_e500,
310 int tlbsel, int esel)
311{
312 struct tlbe *gtlbe;
313
314 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
315
316 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
317 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
318 gtlbe, tlbsel, esel);
319
320 return esel;
321}
322
323/* Caller must ensure that the specified guest TLB entry is safe to insert into
324 * the shadow TLB. */
325/* XXX for both one-one and one-to-many , for now use TLB1 */
326static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
327 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe)
328{
329 unsigned int victim;
330
331 victim = vcpu_e500->guest_tlb_nv[1]++;
332
333 if (unlikely(vcpu_e500->guest_tlb_nv[1] >= tlb1_max_shadow_size()))
334 vcpu_e500->guest_tlb_nv[1] = 0;
335
336 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim);
337
338 return victim;
339}
340
341/* Invalidate all guest kernel mappings when enter usermode,
342 * so that when they fault back in they will get the
343 * proper permission bits. */
344void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
345{
346 if (usermode) {
347 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
348 int i;
349
350 /* XXX Replace loop with fancy data structures. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600351 for (i = 0; i < tlb1_max_shadow_size(); i++)
352 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
353
Liu Yu9aa4dd52009-01-14 10:47:38 -0600354 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600355 }
356}
357
358static int kvmppc_e500_gtlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
359 int tlbsel, int esel)
360{
361 struct tlbe *gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
362
363 if (unlikely(get_tlb_iprot(gtlbe)))
364 return -1;
365
366 if (tlbsel == 1) {
367 kvmppc_e500_tlb1_invalidate(vcpu_e500, get_tlb_eaddr(gtlbe),
368 get_tlb_end(gtlbe),
369 get_tlb_tid(gtlbe));
370 } else {
371 kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
372 }
373
374 gtlbe->mas1 = 0;
375
376 return 0;
377}
378
Liu Yub0a18352009-02-17 16:52:08 +0800379int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
380{
381 int esel;
382
383 if (value & MMUCSR0_TLB0FI)
384 for (esel = 0; esel < vcpu_e500->guest_tlb_size[0]; esel++)
385 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
386 if (value & MMUCSR0_TLB1FI)
387 for (esel = 0; esel < vcpu_e500->guest_tlb_size[1]; esel++)
388 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
389
390 _tlbil_all();
391
392 return EMULATE_DONE;
393}
394
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600395int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
396{
397 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
398 unsigned int ia;
399 int esel, tlbsel;
400 gva_t ea;
401
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100402 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600403
404 ia = (ea >> 2) & 0x1;
405
Liu Yufb2838d2009-01-14 10:47:37 -0600406 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600407 tlbsel = (ea >> 3) & 0x1;
408
409 if (ia) {
410 /* invalidate all entries */
411 for (esel = 0; esel < vcpu_e500->guest_tlb_size[tlbsel]; esel++)
412 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
413 } else {
414 ea &= 0xfffff000;
415 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
416 get_cur_pid(vcpu), -1);
417 if (esel >= 0)
418 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
419 }
420
Liu Yu9aa4dd52009-01-14 10:47:38 -0600421 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600422
423 return EMULATE_DONE;
424}
425
426int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
427{
428 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
429 int tlbsel, esel;
430 struct tlbe *gtlbe;
431
432 tlbsel = get_tlb_tlbsel(vcpu_e500);
433 esel = get_tlb_esel(vcpu_e500, tlbsel);
434
435 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
Liu Yubc35cbc2009-03-17 16:57:45 +0800436 vcpu_e500->mas0 &= ~MAS0_NV(~0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600437 vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
438 vcpu_e500->mas1 = gtlbe->mas1;
439 vcpu_e500->mas2 = gtlbe->mas2;
440 vcpu_e500->mas3 = gtlbe->mas3;
441 vcpu_e500->mas7 = gtlbe->mas7;
442
443 return EMULATE_DONE;
444}
445
446int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
447{
448 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
449 int as = !!get_cur_sas(vcpu_e500);
450 unsigned int pid = get_cur_spid(vcpu_e500);
451 int esel, tlbsel;
452 struct tlbe *gtlbe = NULL;
453 gva_t ea;
454
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100455 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600456
457 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
458 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
459 if (esel >= 0) {
460 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
461 break;
462 }
463 }
464
465 if (gtlbe) {
466 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
467 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
468 vcpu_e500->mas1 = gtlbe->mas1;
469 vcpu_e500->mas2 = gtlbe->mas2;
470 vcpu_e500->mas3 = gtlbe->mas3;
471 vcpu_e500->mas7 = gtlbe->mas7;
472 } else {
473 int victim;
474
Liu Yufb2838d2009-01-14 10:47:37 -0600475 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600476 tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
477 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
478
479 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
480 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
481 vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
482 | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
483 | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
484 vcpu_e500->mas2 &= MAS2_EPN;
485 vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
486 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
487 vcpu_e500->mas7 = 0;
488 }
489
Scott Wood49ea0692011-03-28 15:01:24 -0500490 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600491 return EMULATE_DONE;
492}
493
494int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
495{
496 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
497 u64 eaddr;
498 u64 raddr;
499 u32 tid;
500 struct tlbe *gtlbe;
501 int tlbsel, esel, stlbsel, sesel;
502
503 tlbsel = get_tlb_tlbsel(vcpu_e500);
504 esel = get_tlb_esel(vcpu_e500, tlbsel);
505
506 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
507
508 if (get_tlb_v(gtlbe) && tlbsel == 1) {
509 eaddr = get_tlb_eaddr(gtlbe);
510 tid = get_tlb_tid(gtlbe);
511 kvmppc_e500_tlb1_invalidate(vcpu_e500, eaddr,
512 get_tlb_end(gtlbe), tid);
513 }
514
515 gtlbe->mas1 = vcpu_e500->mas1;
516 gtlbe->mas2 = vcpu_e500->mas2;
517 gtlbe->mas3 = vcpu_e500->mas3;
518 gtlbe->mas7 = vcpu_e500->mas7;
519
Marcelo Tosatti46f43c62009-06-18 11:47:27 -0300520 trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
521 gtlbe->mas3, gtlbe->mas7);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600522
523 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
524 if (tlbe_is_host_safe(vcpu, gtlbe)) {
525 switch (tlbsel) {
526 case 0:
527 /* TLB0 */
528 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800529 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600530
531 stlbsel = 0;
532 sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel);
533
534 break;
535
536 case 1:
537 /* TLB1 */
538 eaddr = get_tlb_eaddr(gtlbe);
539 raddr = get_tlb_raddr(gtlbe);
540
541 /* Create a 4KB mapping on the host.
542 * If the guest wanted a large page,
543 * only the first 4KB is mapped here and the rest
544 * are mapped on the fly. */
545 stlbsel = 1;
546 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
547 raddr >> PAGE_SHIFT, gtlbe);
548 break;
549
550 default:
551 BUG();
552 }
553 write_host_tlbe(vcpu_e500, stlbsel, sesel);
554 }
555
Scott Wood49ea0692011-03-28 15:01:24 -0500556 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600557 return EMULATE_DONE;
558}
559
560int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
561{
Alexander Graf666e7252010-07-29 14:47:43 +0200562 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600563
564 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
565}
566
567int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
568{
Alexander Graf666e7252010-07-29 14:47:43 +0200569 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600570
571 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
572}
573
574void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
575{
Alexander Graf666e7252010-07-29 14:47:43 +0200576 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600577
578 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
579}
580
581void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
582{
Alexander Graf666e7252010-07-29 14:47:43 +0200583 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600584
585 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
586}
587
588gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
589 gva_t eaddr)
590{
591 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
592 struct tlbe *gtlbe =
593 &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)];
594 u64 pgmask = get_tlb_bytes(gtlbe) - 1;
595
596 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
597}
598
599void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
600{
601 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
602 int tlbsel, i;
603
604 for (tlbsel = 0; tlbsel < 2; tlbsel++)
605 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++)
606 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i);
607
608 /* discard all guest mapping */
Liu Yu9aa4dd52009-01-14 10:47:38 -0600609 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600610}
611
612void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
613 unsigned int index)
614{
615 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
616 int tlbsel = tlbsel_of(index);
617 int esel = esel_of(index);
618 int stlbsel, sesel;
619
620 switch (tlbsel) {
621 case 0:
622 stlbsel = 0;
623 sesel = esel;
624 break;
625
626 case 1: {
627 gfn_t gfn = gpaddr >> PAGE_SHIFT;
628 struct tlbe *gtlbe
629 = &vcpu_e500->guest_tlb[tlbsel][esel];
630
631 stlbsel = 1;
632 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe);
633 break;
634 }
635
636 default:
637 BUG();
638 break;
639 }
640 write_host_tlbe(vcpu_e500, stlbsel, sesel);
641}
642
643int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
644 gva_t eaddr, unsigned int pid, int as)
645{
646 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
647 int esel, tlbsel;
648
649 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
650 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
651 if (esel >= 0)
652 return index_of(tlbsel, esel);
653 }
654
655 return -1;
656}
657
Scott Wood5ce941e2011-04-27 17:24:21 -0500658void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
659{
660 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
661
662 vcpu_e500->pid[0] = vcpu->arch.shadow_pid =
663 vcpu->arch.pid = pid;
664}
665
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600666void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
667{
668 struct tlbe *tlbe;
669
670 /* Insert large initial mapping for guest. */
671 tlbe = &vcpu_e500->guest_tlb[1][0];
Liu Yu0cfb50e2009-06-05 14:54:29 +0800672 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600673 tlbe->mas2 = 0;
674 tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
675 tlbe->mas7 = 0;
676
677 /* 4K map for serial output. Used by kernel wrapper. */
678 tlbe = &vcpu_e500->guest_tlb[1][1];
Liu Yu0cfb50e2009-06-05 14:54:29 +0800679 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600680 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
681 tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
682 tlbe->mas7 = 0;
683}
684
685int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
686{
687 tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
688
689 vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE;
690 vcpu_e500->guest_tlb[0] =
691 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
692 if (vcpu_e500->guest_tlb[0] == NULL)
693 goto err_out;
694
695 vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE;
696 vcpu_e500->shadow_tlb[0] =
697 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
698 if (vcpu_e500->shadow_tlb[0] == NULL)
699 goto err_out_guest0;
700
701 vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE;
702 vcpu_e500->guest_tlb[1] =
703 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
704 if (vcpu_e500->guest_tlb[1] == NULL)
705 goto err_out_shadow0;
706
707 vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num;
708 vcpu_e500->shadow_tlb[1] =
709 kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL);
710 if (vcpu_e500->shadow_tlb[1] == NULL)
711 goto err_out_guest1;
712
Liu Yuda15bf42010-01-22 19:36:53 +0800713 /* Init TLB configuration register */
714 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
715 vcpu_e500->tlb0cfg |= vcpu_e500->guest_tlb_size[0];
716 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
717 vcpu_e500->tlb1cfg |= vcpu_e500->guest_tlb_size[1];
718
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600719 return 0;
720
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600721err_out_guest1:
722 kfree(vcpu_e500->guest_tlb[1]);
723err_out_shadow0:
724 kfree(vcpu_e500->shadow_tlb[0]);
725err_out_guest0:
726 kfree(vcpu_e500->guest_tlb[0]);
727err_out:
728 return -1;
729}
730
731void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
732{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600733 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}