code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / arch / powerpc / kvm / e500_tlb.c
blob21011e12caeb9e159e258785b0f8709fe52bed2c
1 /*
2 * Copyright (C) 2008 Freescale Semiconductor, Inc. All rights reserved.
4 * Author: Yu Liu, yu.liu@freescale.com
6 * Description:
7 * This file is based on arch/powerpc/kvm/44x_tlb.c,
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 #include <linux/types.h>
16 #include <linux/slab.h>
17 #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>
24 #include "../mm/mmu_decl.h"
25 #include "e500_tlb.h"
26 #include "trace.h"
28 #define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1)
30 static unsigned int tlb1_entry_num;
32 void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
34 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
35 struct tlbe *tlbe;
36 int i, tlbsel;
38 printk("| %8s | %8s | %8s | %8s | %8s |\n",
39 "nr", "mas1", "mas2", "mas3", "mas7");
41 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
42 printk("Guest TLB%d:\n", tlbsel);
43 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
44 tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
45 if (tlbe->mas1 & MAS1_VALID)
46 printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
47 tlbsel, i, tlbe->mas1, tlbe->mas2,
48 tlbe->mas3, tlbe->mas7);
52 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
53 printk("Shadow TLB%d:\n", tlbsel);
54 for (i = 0; i < vcpu_e500->shadow_tlb_size[tlbsel]; i++) {
55 tlbe = &vcpu_e500->shadow_tlb[tlbsel][i];
56 if (tlbe->mas1 & MAS1_VALID)
57 printk(" S[%d][%3d] | %08X | %08X | %08X | %08X |\n",
58 tlbsel, i, tlbe->mas1, tlbe->mas2,
59 tlbe->mas3, tlbe->mas7);
64 static inline unsigned int tlb0_get_next_victim(
65 struct kvmppc_vcpu_e500 *vcpu_e500)
67 unsigned int victim;
69 victim = vcpu_e500->guest_tlb_nv[0]++;
70 if (unlikely(vcpu_e500->guest_tlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
71 vcpu_e500->guest_tlb_nv[0] = 0;
73 return victim;
76 static inline unsigned int tlb1_max_shadow_size(void)
78 return tlb1_entry_num - tlbcam_index;
81 static inline int tlbe_is_writable(struct tlbe *tlbe)
83 return tlbe->mas3 & (MAS3_SW|MAS3_UW);
86 static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
88 /* Mask off reserved bits. */
89 mas3 &= MAS3_ATTRIB_MASK;
91 if (!usermode) {
92 /* Guest is in supervisor mode,
93 * so we need to translate guest
94 * supervisor permissions into user permissions. */
95 mas3 &= ~E500_TLB_USER_PERM_MASK;
96 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
99 return mas3 | E500_TLB_SUPER_PERM_MASK;
102 static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
104 #ifdef CONFIG_SMP
105 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
106 #else
107 return mas2 & MAS2_ATTRIB_MASK;
108 #endif
112 * writing shadow tlb entry to host TLB
114 static inline void __write_host_tlbe(struct tlbe *stlbe)
116 mtspr(SPRN_MAS1, stlbe->mas1);
117 mtspr(SPRN_MAS2, stlbe->mas2);
118 mtspr(SPRN_MAS3, stlbe->mas3);
119 mtspr(SPRN_MAS7, stlbe->mas7);
120 __asm__ __volatile__ ("tlbwe\n" : : );
123 static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
124 int tlbsel, int esel)
126 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
128 local_irq_disable();
129 if (tlbsel == 0) {
130 __write_host_tlbe(stlbe);
131 } else {
132 unsigned register mas0;
134 mas0 = mfspr(SPRN_MAS0);
136 mtspr(SPRN_MAS0, MAS0_TLBSEL(1) | MAS0_ESEL(to_htlb1_esel(esel)));
137 __write_host_tlbe(stlbe);
139 mtspr(SPRN_MAS0, mas0);
141 local_irq_enable();
144 void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
146 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
147 int i;
148 unsigned register mas0;
150 /* Load all valid TLB1 entries to reduce guest tlb miss fault */
151 local_irq_disable();
152 mas0 = mfspr(SPRN_MAS0);
153 for (i = 0; i < tlb1_max_shadow_size(); i++) {
154 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
156 if (get_tlb_v(stlbe)) {
157 mtspr(SPRN_MAS0, MAS0_TLBSEL(1)
158 | MAS0_ESEL(to_htlb1_esel(i)));
159 __write_host_tlbe(stlbe);
162 mtspr(SPRN_MAS0, mas0);
163 local_irq_enable();
166 void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
168 _tlbil_all();
171 /* Search the guest TLB for a matching entry. */
172 static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
173 gva_t eaddr, int tlbsel, unsigned int pid, int as)
175 int i;
177 /* XXX Replace loop with fancy data structures. */
178 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
179 struct tlbe *tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
180 unsigned int tid;
182 if (eaddr < get_tlb_eaddr(tlbe))
183 continue;
185 if (eaddr > get_tlb_end(tlbe))
186 continue;
188 tid = get_tlb_tid(tlbe);
189 if (tid && (tid != pid))
190 continue;
192 if (!get_tlb_v(tlbe))
193 continue;
195 if (get_tlb_ts(tlbe) != as && as != -1)
196 continue;
198 return i;
201 return -1;
204 static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500 *vcpu_e500,
205 int tlbsel, int esel)
207 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
208 struct page *page = vcpu_e500->shadow_pages[tlbsel][esel];
210 if (page) {
211 vcpu_e500->shadow_pages[tlbsel][esel] = NULL;
213 if (get_tlb_v(stlbe)) {
214 if (tlbe_is_writable(stlbe))
215 kvm_release_page_dirty(page);
216 else
217 kvm_release_page_clean(page);
222 static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
223 int tlbsel, int esel)
225 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
227 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
228 stlbe->mas1 = 0;
229 trace_kvm_stlb_inval(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
230 stlbe->mas3, stlbe->mas7);
233 static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
234 gva_t eaddr, gva_t eend, u32 tid)
236 unsigned int pid = tid & 0xff;
237 unsigned int i;
239 /* XXX Replace loop with fancy data structures. */
240 for (i = 0; i < vcpu_e500->guest_tlb_size[1]; i++) {
241 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
242 unsigned int tid;
244 if (!get_tlb_v(stlbe))
245 continue;
247 if (eend < get_tlb_eaddr(stlbe))
248 continue;
250 if (eaddr > get_tlb_end(stlbe))
251 continue;
253 tid = get_tlb_tid(stlbe);
254 if (tid && (tid != pid))
255 continue;
257 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
258 write_host_tlbe(vcpu_e500, 1, i);
262 static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
263 unsigned int eaddr, int as)
265 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
266 unsigned int victim, pidsel, tsized;
267 int tlbsel;
269 /* since we only have two TLBs, only lower bit is used. */
270 tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
271 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
272 pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
273 tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
275 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
276 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
277 vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
278 | MAS1_TID(vcpu_e500->pid[pidsel])
279 | MAS1_TSIZE(tsized);
280 vcpu_e500->mas2 = (eaddr & MAS2_EPN)
281 | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
282 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
283 vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
284 | (get_cur_pid(vcpu) << 16)
285 | (as ? MAS6_SAS : 0);
286 vcpu_e500->mas7 = 0;
289 static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
290 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel)
292 struct page *new_page;
293 struct tlbe *stlbe;
294 hpa_t hpaddr;
296 stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
298 /* Get reference to new page. */
299 new_page = gfn_to_page(vcpu_e500->vcpu.kvm, gfn);
300 if (is_error_page(new_page)) {
301 printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
302 kvm_release_page_clean(new_page);
303 return;
305 hpaddr = page_to_phys(new_page);
307 /* Drop reference to old page. */
308 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
310 vcpu_e500->shadow_pages[tlbsel][esel] = new_page;
312 /* Force TS=1 IPROT=0 TSIZE=4KB for all guest mappings. */
313 stlbe->mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K)
314 | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID;
315 stlbe->mas2 = (gvaddr & MAS2_EPN)
316 | e500_shadow_mas2_attrib(gtlbe->mas2,
317 vcpu_e500->vcpu.arch.msr & MSR_PR);
318 stlbe->mas3 = (hpaddr & MAS3_RPN)
319 | e500_shadow_mas3_attrib(gtlbe->mas3,
320 vcpu_e500->vcpu.arch.msr & MSR_PR);
321 stlbe->mas7 = (hpaddr >> 32) & MAS7_RPN;
323 trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
324 stlbe->mas3, stlbe->mas7);
327 /* XXX only map the one-one case, for now use TLB0 */
328 static int kvmppc_e500_stlbe_map(struct kvmppc_vcpu_e500 *vcpu_e500,
329 int tlbsel, int esel)
331 struct tlbe *gtlbe;
333 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
335 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
336 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
337 gtlbe, tlbsel, esel);
339 return esel;
342 /* Caller must ensure that the specified guest TLB entry is safe to insert into
343 * the shadow TLB. */
344 /* XXX for both one-one and one-to-many , for now use TLB1 */
345 static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
346 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe)
348 unsigned int victim;
350 victim = vcpu_e500->guest_tlb_nv[1]++;
352 if (unlikely(vcpu_e500->guest_tlb_nv[1] >= tlb1_max_shadow_size()))
353 vcpu_e500->guest_tlb_nv[1] = 0;
355 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim);
357 return victim;
360 /* Invalidate all guest kernel mappings when enter usermode,
361 * so that when they fault back in they will get the
362 * proper permission bits. */
363 void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
365 if (usermode) {
366 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
367 int i;
369 /* XXX Replace loop with fancy data structures. */
370 for (i = 0; i < tlb1_max_shadow_size(); i++)
371 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
373 _tlbil_all();
377 static int kvmppc_e500_gtlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
378 int tlbsel, int esel)
380 struct tlbe *gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
382 if (unlikely(get_tlb_iprot(gtlbe)))
383 return -1;
385 if (tlbsel == 1) {
386 kvmppc_e500_tlb1_invalidate(vcpu_e500, get_tlb_eaddr(gtlbe),
387 get_tlb_end(gtlbe),
388 get_tlb_tid(gtlbe));
389 } else {
390 kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
393 gtlbe->mas1 = 0;
395 return 0;
398 int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
400 int esel;
402 if (value & MMUCSR0_TLB0FI)
403 for (esel = 0; esel < vcpu_e500->guest_tlb_size[0]; esel++)
404 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
405 if (value & MMUCSR0_TLB1FI)
406 for (esel = 0; esel < vcpu_e500->guest_tlb_size[1]; esel++)
407 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
409 _tlbil_all();
411 return EMULATE_DONE;
414 int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
416 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
417 unsigned int ia;
418 int esel, tlbsel;
419 gva_t ea;
421 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
423 ia = (ea >> 2) & 0x1;
425 /* since we only have two TLBs, only lower bit is used. */
426 tlbsel = (ea >> 3) & 0x1;
428 if (ia) {
429 /* invalidate all entries */
430 for (esel = 0; esel < vcpu_e500->guest_tlb_size[tlbsel]; esel++)
431 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
432 } else {
433 ea &= 0xfffff000;
434 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
435 get_cur_pid(vcpu), -1);
436 if (esel >= 0)
437 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
440 _tlbil_all();
442 return EMULATE_DONE;
445 int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
447 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
448 int tlbsel, esel;
449 struct tlbe *gtlbe;
451 tlbsel = get_tlb_tlbsel(vcpu_e500);
452 esel = get_tlb_esel(vcpu_e500, tlbsel);
454 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
455 vcpu_e500->mas0 &= ~MAS0_NV(~0);
456 vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
457 vcpu_e500->mas1 = gtlbe->mas1;
458 vcpu_e500->mas2 = gtlbe->mas2;
459 vcpu_e500->mas3 = gtlbe->mas3;
460 vcpu_e500->mas7 = gtlbe->mas7;
462 return EMULATE_DONE;
465 int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
467 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
468 int as = !!get_cur_sas(vcpu_e500);
469 unsigned int pid = get_cur_spid(vcpu_e500);
470 int esel, tlbsel;
471 struct tlbe *gtlbe = NULL;
472 gva_t ea;
474 ea = kvmppc_get_gpr(vcpu, rb);
476 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
477 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
478 if (esel >= 0) {
479 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
480 break;
484 if (gtlbe) {
485 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
486 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
487 vcpu_e500->mas1 = gtlbe->mas1;
488 vcpu_e500->mas2 = gtlbe->mas2;
489 vcpu_e500->mas3 = gtlbe->mas3;
490 vcpu_e500->mas7 = gtlbe->mas7;
491 } else {
492 int victim;
494 /* since we only have two TLBs, only lower bit is used. */
495 tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
496 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
498 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
499 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
500 vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
501 | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
502 | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
503 vcpu_e500->mas2 &= MAS2_EPN;
504 vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
505 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
506 vcpu_e500->mas7 = 0;
509 return EMULATE_DONE;
512 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
514 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
515 u64 eaddr;
516 u64 raddr;
517 u32 tid;
518 struct tlbe *gtlbe;
519 int tlbsel, esel, stlbsel, sesel;
521 tlbsel = get_tlb_tlbsel(vcpu_e500);
522 esel = get_tlb_esel(vcpu_e500, tlbsel);
524 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
526 if (get_tlb_v(gtlbe) && tlbsel == 1) {
527 eaddr = get_tlb_eaddr(gtlbe);
528 tid = get_tlb_tid(gtlbe);
529 kvmppc_e500_tlb1_invalidate(vcpu_e500, eaddr,
530 get_tlb_end(gtlbe), tid);
533 gtlbe->mas1 = vcpu_e500->mas1;
534 gtlbe->mas2 = vcpu_e500->mas2;
535 gtlbe->mas3 = vcpu_e500->mas3;
536 gtlbe->mas7 = vcpu_e500->mas7;
538 trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
539 gtlbe->mas3, gtlbe->mas7);
541 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
542 if (tlbe_is_host_safe(vcpu, gtlbe)) {
543 switch (tlbsel) {
544 case 0:
545 /* TLB0 */
546 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
547 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
549 stlbsel = 0;
550 sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel);
552 break;
554 case 1:
555 /* TLB1 */
556 eaddr = get_tlb_eaddr(gtlbe);
557 raddr = get_tlb_raddr(gtlbe);
559 /* Create a 4KB mapping on the host.
560 * If the guest wanted a large page,
561 * only the first 4KB is mapped here and the rest
562 * are mapped on the fly. */
563 stlbsel = 1;
564 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
565 raddr >> PAGE_SHIFT, gtlbe);
566 break;
568 default:
569 BUG();
571 write_host_tlbe(vcpu_e500, stlbsel, sesel);
574 return EMULATE_DONE;
577 int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
579 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
581 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
584 int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
586 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
588 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
591 void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
593 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
595 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
598 void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
600 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
602 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
605 gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
606 gva_t eaddr)
608 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
609 struct tlbe *gtlbe =
610 &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)];
611 u64 pgmask = get_tlb_bytes(gtlbe) - 1;
613 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
616 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
618 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
619 int tlbsel, i;
621 for (tlbsel = 0; tlbsel < 2; tlbsel++)
622 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++)
623 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i);
625 /* discard all guest mapping */
626 _tlbil_all();
629 void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
630 unsigned int index)
632 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
633 int tlbsel = tlbsel_of(index);
634 int esel = esel_of(index);
635 int stlbsel, sesel;
637 switch (tlbsel) {
638 case 0:
639 stlbsel = 0;
640 sesel = esel;
641 break;
643 case 1: {
644 gfn_t gfn = gpaddr >> PAGE_SHIFT;
645 struct tlbe *gtlbe
646 = &vcpu_e500->guest_tlb[tlbsel][esel];
648 stlbsel = 1;
649 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe);
650 break;
653 default:
654 BUG();
655 break;
657 write_host_tlbe(vcpu_e500, stlbsel, sesel);
660 int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
661 gva_t eaddr, unsigned int pid, int as)
663 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
664 int esel, tlbsel;
666 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
667 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
668 if (esel >= 0)
669 return index_of(tlbsel, esel);
672 return -1;
675 void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
677 struct tlbe *tlbe;
679 /* Insert large initial mapping for guest. */
680 tlbe = &vcpu_e500->guest_tlb[1][0];
681 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
682 tlbe->mas2 = 0;
683 tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
684 tlbe->mas7 = 0;
686 /* 4K map for serial output. Used by kernel wrapper. */
687 tlbe = &vcpu_e500->guest_tlb[1][1];
688 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
689 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
690 tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
691 tlbe->mas7 = 0;
694 int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
696 tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
698 vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE;
699 vcpu_e500->guest_tlb[0] =
700 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
701 if (vcpu_e500->guest_tlb[0] == NULL)
702 goto err_out;
704 vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE;
705 vcpu_e500->shadow_tlb[0] =
706 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
707 if (vcpu_e500->shadow_tlb[0] == NULL)
708 goto err_out_guest0;
710 vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE;
711 vcpu_e500->guest_tlb[1] =
712 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
713 if (vcpu_e500->guest_tlb[1] == NULL)
714 goto err_out_shadow0;
716 vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num;
717 vcpu_e500->shadow_tlb[1] =
718 kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL);
719 if (vcpu_e500->shadow_tlb[1] == NULL)
720 goto err_out_guest1;
722 vcpu_e500->shadow_pages[0] = (struct page **)
723 kzalloc(sizeof(struct page *) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
724 if (vcpu_e500->shadow_pages[0] == NULL)
725 goto err_out_shadow1;
727 vcpu_e500->shadow_pages[1] = (struct page **)
728 kzalloc(sizeof(struct page *) * tlb1_entry_num, GFP_KERNEL);
729 if (vcpu_e500->shadow_pages[1] == NULL)
730 goto err_out_page0;
732 /* Init TLB configuration register */
733 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
734 vcpu_e500->tlb0cfg |= vcpu_e500->guest_tlb_size[0];
735 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
736 vcpu_e500->tlb1cfg |= vcpu_e500->guest_tlb_size[1];
738 return 0;
740 err_out_page0:
741 kfree(vcpu_e500->shadow_pages[0]);
742 err_out_shadow1:
743 kfree(vcpu_e500->shadow_tlb[1]);
744 err_out_guest1:
745 kfree(vcpu_e500->guest_tlb[1]);
746 err_out_shadow0:
747 kfree(vcpu_e500->shadow_tlb[0]);
748 err_out_guest0:
749 kfree(vcpu_e500->guest_tlb[0]);
750 err_out:
751 return -1;
754 void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
756 kfree(vcpu_e500->shadow_pages[1]);
757 kfree(vcpu_e500->shadow_pages[0]);
758 kfree(vcpu_e500->shadow_tlb[1]);
759 kfree(vcpu_e500->guest_tlb[1]);
760 kfree(vcpu_e500->shadow_tlb[0]);
761 kfree(vcpu_e500->guest_tlb[0]);