2 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
4 * Author: Yu Liu, yu.liu@freescale.com
5 * Scott Wood, scottwood@freescale.com
6 * Ashish Kalra, ashish.kalra@freescale.com
7 * Varun Sethi, varun.sethi@freescale.com
10 * This file is based on arch/powerpc/kvm/44x_tlb.c,
11 * by Hollis Blanchard <hollisb@us.ibm.com>.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License, version 2, as
15 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/kvm.h>
23 #include <linux/kvm_host.h>
24 #include <linux/highmem.h>
25 #include <linux/log2.h>
26 #include <linux/uaccess.h>
27 #include <linux/sched.h>
28 #include <linux/rwsem.h>
29 #include <linux/vmalloc.h>
30 #include <linux/hugetlb.h>
31 #include <asm/kvm_ppc.h>
37 #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
39 static struct kvmppc_e500_tlb_params host_tlb_params
[E500_TLB_NUM
];
41 static inline unsigned int gtlb0_get_next_victim(
42 struct kvmppc_vcpu_e500
*vcpu_e500
)
46 victim
= vcpu_e500
->gtlb_nv
[0]++;
47 if (unlikely(vcpu_e500
->gtlb_nv
[0] >= vcpu_e500
->gtlb_params
[0].ways
))
48 vcpu_e500
->gtlb_nv
[0] = 0;
53 static inline unsigned int tlb1_max_shadow_size(void)
55 /* reserve one entry for magic page */
56 return host_tlb_params
[1].entries
- tlbcam_index
- 1;
59 static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry
*tlbe
)
61 return tlbe
->mas7_3
& (MAS3_SW
|MAS3_UW
);
64 static inline u32
e500_shadow_mas3_attrib(u32 mas3
, int usermode
)
66 /* Mask off reserved bits. */
67 mas3
&= MAS3_ATTRIB_MASK
;
69 #ifndef CONFIG_KVM_BOOKE_HV
71 /* Guest is in supervisor mode,
72 * so we need to translate guest
73 * supervisor permissions into user permissions. */
74 mas3
&= ~E500_TLB_USER_PERM_MASK
;
75 mas3
|= (mas3
& E500_TLB_SUPER_PERM_MASK
) << 1;
77 mas3
|= E500_TLB_SUPER_PERM_MASK
;
82 static inline u32
e500_shadow_mas2_attrib(u32 mas2
, int usermode
)
85 return (mas2
& MAS2_ATTRIB_MASK
) | MAS2_M
;
87 return mas2
& MAS2_ATTRIB_MASK
;
92 * writing shadow tlb entry to host TLB
94 static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry
*stlbe
,
99 local_irq_save(flags
);
100 mtspr(SPRN_MAS0
, mas0
);
101 mtspr(SPRN_MAS1
, stlbe
->mas1
);
102 mtspr(SPRN_MAS2
, (unsigned long)stlbe
->mas2
);
103 mtspr(SPRN_MAS3
, (u32
)stlbe
->mas7_3
);
104 mtspr(SPRN_MAS7
, (u32
)(stlbe
->mas7_3
>> 32));
105 #ifdef CONFIG_KVM_BOOKE_HV
106 mtspr(SPRN_MAS8
, stlbe
->mas8
);
108 asm volatile("isync; tlbwe" : : : "memory");
110 #ifdef CONFIG_KVM_BOOKE_HV
111 /* Must clear mas8 for other host tlbwe's */
115 local_irq_restore(flags
);
117 trace_kvm_booke206_stlb_write(mas0
, stlbe
->mas8
, stlbe
->mas1
,
118 stlbe
->mas2
, stlbe
->mas7_3
);
122 * Acquire a mas0 with victim hint, as if we just took a TLB miss.
124 * We don't care about the address we're searching for, other than that it's
125 * in the right set and is not present in the TLB. Using a zero PID and a
126 * userspace address means we don't have to set and then restore MAS5, or
127 * calculate a proper MAS6 value.
129 static u32
get_host_mas0(unsigned long eaddr
)
134 local_irq_save(flags
);
136 asm volatile("tlbsx 0, %0" : : "b" (eaddr
& ~CONFIG_PAGE_OFFSET
));
137 mas0
= mfspr(SPRN_MAS0
);
138 local_irq_restore(flags
);
143 /* sesel is for tlb1 only */
144 static inline void write_host_tlbe(struct kvmppc_vcpu_e500
*vcpu_e500
,
145 int tlbsel
, int sesel
, struct kvm_book3e_206_tlb_entry
*stlbe
)
150 mas0
= get_host_mas0(stlbe
->mas2
);
151 __write_host_tlbe(stlbe
, mas0
);
153 __write_host_tlbe(stlbe
,
155 MAS0_ESEL(to_htlb1_esel(sesel
)));
159 #ifdef CONFIG_KVM_E500V2
160 void kvmppc_map_magic(struct kvm_vcpu
*vcpu
)
162 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
163 struct kvm_book3e_206_tlb_entry magic
;
164 ulong shared_page
= ((ulong
)vcpu
->arch
.shared
) & PAGE_MASK
;
168 pfn
= (pfn_t
)virt_to_phys((void *)shared_page
) >> PAGE_SHIFT
;
169 get_page(pfn_to_page(pfn
));
172 stid
= kvmppc_e500_get_sid(vcpu_e500
, 0, 0, 0, 0);
174 magic
.mas1
= MAS1_VALID
| MAS1_TS
| MAS1_TID(stid
) |
175 MAS1_TSIZE(BOOK3E_PAGESZ_4K
);
176 magic
.mas2
= vcpu
->arch
.magic_page_ea
| MAS2_M
;
177 magic
.mas7_3
= ((u64
)pfn
<< PAGE_SHIFT
) |
178 MAS3_SW
| MAS3_SR
| MAS3_UW
| MAS3_UR
;
181 __write_host_tlbe(&magic
, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index
));
186 static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500
*vcpu_e500
,
187 int tlbsel
, int esel
)
189 struct kvm_book3e_206_tlb_entry
*gtlbe
=
190 get_entry(vcpu_e500
, tlbsel
, esel
);
193 vcpu_e500
->gtlb_priv
[1][esel
].ref
.flags
& E500_TLB_BITMAP
) {
194 u64 tmp
= vcpu_e500
->g2h_tlb1_map
[esel
];
198 local_irq_save(flags
);
200 hw_tlb_indx
= __ilog2_u64(tmp
& -tmp
);
203 MAS0_ESEL(to_htlb1_esel(hw_tlb_indx
)));
205 asm volatile("tlbwe");
206 vcpu_e500
->h2g_tlb1_rmap
[hw_tlb_indx
] = 0;
210 vcpu_e500
->g2h_tlb1_map
[esel
] = 0;
211 vcpu_e500
->gtlb_priv
[1][esel
].ref
.flags
&= ~E500_TLB_BITMAP
;
212 local_irq_restore(flags
);
217 /* Guest tlbe is backed by at most one host tlbe per shadow pid. */
218 kvmppc_e500_tlbil_one(vcpu_e500
, gtlbe
);
221 static int tlb0_set_base(gva_t addr
, int sets
, int ways
)
225 set_base
= (addr
>> PAGE_SHIFT
) & (sets
- 1);
231 static int gtlb0_set_base(struct kvmppc_vcpu_e500
*vcpu_e500
, gva_t addr
)
233 return tlb0_set_base(addr
, vcpu_e500
->gtlb_params
[0].sets
,
234 vcpu_e500
->gtlb_params
[0].ways
);
237 static unsigned int get_tlb_esel(struct kvm_vcpu
*vcpu
, int tlbsel
)
239 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
240 int esel
= get_tlb_esel_bit(vcpu
);
243 esel
&= vcpu_e500
->gtlb_params
[0].ways
- 1;
244 esel
+= gtlb0_set_base(vcpu_e500
, vcpu
->arch
.shared
->mas2
);
246 esel
&= vcpu_e500
->gtlb_params
[tlbsel
].entries
- 1;
252 /* Search the guest TLB for a matching entry. */
253 static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500
*vcpu_e500
,
254 gva_t eaddr
, int tlbsel
, unsigned int pid
, int as
)
256 int size
= vcpu_e500
->gtlb_params
[tlbsel
].entries
;
257 unsigned int set_base
, offset
;
261 set_base
= gtlb0_set_base(vcpu_e500
, eaddr
);
262 size
= vcpu_e500
->gtlb_params
[0].ways
;
264 if (eaddr
< vcpu_e500
->tlb1_min_eaddr
||
265 eaddr
> vcpu_e500
->tlb1_max_eaddr
)
270 offset
= vcpu_e500
->gtlb_offset
[tlbsel
];
272 for (i
= 0; i
< size
; i
++) {
273 struct kvm_book3e_206_tlb_entry
*tlbe
=
274 &vcpu_e500
->gtlb_arch
[offset
+ set_base
+ i
];
277 if (eaddr
< get_tlb_eaddr(tlbe
))
280 if (eaddr
> get_tlb_end(tlbe
))
283 tid
= get_tlb_tid(tlbe
);
284 if (tid
&& (tid
!= pid
))
287 if (!get_tlb_v(tlbe
))
290 if (get_tlb_ts(tlbe
) != as
&& as
!= -1)
299 static inline void kvmppc_e500_ref_setup(struct tlbe_ref
*ref
,
300 struct kvm_book3e_206_tlb_entry
*gtlbe
,
304 ref
->flags
= E500_TLB_VALID
;
306 if (tlbe_is_writable(gtlbe
))
307 kvm_set_pfn_dirty(pfn
);
310 static inline void kvmppc_e500_ref_release(struct tlbe_ref
*ref
)
312 if (ref
->flags
& E500_TLB_VALID
) {
313 trace_kvm_booke206_ref_release(ref
->pfn
, ref
->flags
);
318 static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500
*vcpu_e500
)
320 if (vcpu_e500
->g2h_tlb1_map
)
321 memset(vcpu_e500
->g2h_tlb1_map
, 0,
322 sizeof(u64
) * vcpu_e500
->gtlb_params
[1].entries
);
323 if (vcpu_e500
->h2g_tlb1_rmap
)
324 memset(vcpu_e500
->h2g_tlb1_rmap
, 0,
325 sizeof(unsigned int) * host_tlb_params
[1].entries
);
328 static void clear_tlb_privs(struct kvmppc_vcpu_e500
*vcpu_e500
)
333 for (i
= 0; i
< vcpu_e500
->gtlb_params
[tlbsel
].entries
; i
++) {
334 struct tlbe_ref
*ref
=
335 &vcpu_e500
->gtlb_priv
[tlbsel
][i
].ref
;
336 kvmppc_e500_ref_release(ref
);
340 static void clear_tlb_refs(struct kvmppc_vcpu_e500
*vcpu_e500
)
345 kvmppc_e500_tlbil_all(vcpu_e500
);
347 for (i
= 0; i
< host_tlb_params
[stlbsel
].entries
; i
++) {
348 struct tlbe_ref
*ref
=
349 &vcpu_e500
->tlb_refs
[stlbsel
][i
];
350 kvmppc_e500_ref_release(ref
);
353 clear_tlb_privs(vcpu_e500
);
356 void kvmppc_core_flush_tlb(struct kvm_vcpu
*vcpu
)
358 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
359 clear_tlb_refs(vcpu_e500
);
360 clear_tlb1_bitmap(vcpu_e500
);
363 static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu
*vcpu
,
364 unsigned int eaddr
, int as
)
366 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
367 unsigned int victim
, tsized
;
370 /* since we only have two TLBs, only lower bit is used. */
371 tlbsel
= (vcpu
->arch
.shared
->mas4
>> 28) & 0x1;
372 victim
= (tlbsel
== 0) ? gtlb0_get_next_victim(vcpu_e500
) : 0;
373 tsized
= (vcpu
->arch
.shared
->mas4
>> 7) & 0x1f;
375 vcpu
->arch
.shared
->mas0
= MAS0_TLBSEL(tlbsel
) | MAS0_ESEL(victim
)
376 | MAS0_NV(vcpu_e500
->gtlb_nv
[tlbsel
]);
377 vcpu
->arch
.shared
->mas1
= MAS1_VALID
| (as
? MAS1_TS
: 0)
378 | MAS1_TID(get_tlbmiss_tid(vcpu
))
379 | MAS1_TSIZE(tsized
);
380 vcpu
->arch
.shared
->mas2
= (eaddr
& MAS2_EPN
)
381 | (vcpu
->arch
.shared
->mas4
& MAS2_ATTRIB_MASK
);
382 vcpu
->arch
.shared
->mas7_3
&= MAS3_U0
| MAS3_U1
| MAS3_U2
| MAS3_U3
;
383 vcpu
->arch
.shared
->mas6
= (vcpu
->arch
.shared
->mas6
& MAS6_SPID1
)
384 | (get_cur_pid(vcpu
) << 16)
385 | (as
? MAS6_SAS
: 0);
388 /* TID must be supplied by the caller */
389 static inline void kvmppc_e500_setup_stlbe(
390 struct kvm_vcpu
*vcpu
,
391 struct kvm_book3e_206_tlb_entry
*gtlbe
,
392 int tsize
, struct tlbe_ref
*ref
, u64 gvaddr
,
393 struct kvm_book3e_206_tlb_entry
*stlbe
)
395 pfn_t pfn
= ref
->pfn
;
396 u32 pr
= vcpu
->arch
.shared
->msr
& MSR_PR
;
398 BUG_ON(!(ref
->flags
& E500_TLB_VALID
));
400 /* Force IPROT=0 for all guest mappings. */
401 stlbe
->mas1
= MAS1_TSIZE(tsize
) | get_tlb_sts(gtlbe
) | MAS1_VALID
;
402 stlbe
->mas2
= (gvaddr
& MAS2_EPN
) |
403 e500_shadow_mas2_attrib(gtlbe
->mas2
, pr
);
404 stlbe
->mas7_3
= ((u64
)pfn
<< PAGE_SHIFT
) |
405 e500_shadow_mas3_attrib(gtlbe
->mas7_3
, pr
);
407 #ifdef CONFIG_KVM_BOOKE_HV
408 stlbe
->mas8
= MAS8_TGS
| vcpu
->kvm
->arch
.lpid
;
412 static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
413 u64 gvaddr
, gfn_t gfn
, struct kvm_book3e_206_tlb_entry
*gtlbe
,
414 int tlbsel
, struct kvm_book3e_206_tlb_entry
*stlbe
,
415 struct tlbe_ref
*ref
)
417 struct kvm_memory_slot
*slot
;
418 unsigned long pfn
, hva
;
420 int tsize
= BOOK3E_PAGESZ_4K
;
423 * Translate guest physical to true physical, acquiring
424 * a page reference if it is normal, non-reserved memory.
426 * gfn_to_memslot() must succeed because otherwise we wouldn't
427 * have gotten this far. Eventually we should just pass the slot
428 * pointer through from the first lookup.
430 slot
= gfn_to_memslot(vcpu_e500
->vcpu
.kvm
, gfn
);
431 hva
= gfn_to_hva_memslot(slot
, gfn
);
434 struct vm_area_struct
*vma
;
435 down_read(¤t
->mm
->mmap_sem
);
437 vma
= find_vma(current
->mm
, hva
);
438 if (vma
&& hva
>= vma
->vm_start
&&
439 (vma
->vm_flags
& VM_PFNMAP
)) {
441 * This VMA is a physically contiguous region (e.g.
442 * /dev/mem) that bypasses normal Linux page
443 * management. Find the overlap between the
444 * vma and the memslot.
447 unsigned long start
, end
;
448 unsigned long slot_start
, slot_end
;
452 start
= vma
->vm_pgoff
;
454 ((vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
);
456 pfn
= start
+ ((hva
- vma
->vm_start
) >> PAGE_SHIFT
);
458 slot_start
= pfn
- (gfn
- slot
->base_gfn
);
459 slot_end
= slot_start
+ slot
->npages
;
461 if (start
< slot_start
)
466 tsize
= (gtlbe
->mas1
& MAS1_TSIZE_MASK
) >>
470 * e500 doesn't implement the lowest tsize bit,
473 tsize
= max(BOOK3E_PAGESZ_4K
, tsize
& ~1);
476 * Now find the largest tsize (up to what the guest
477 * requested) that will cover gfn, stay within the
478 * range, and for which gfn and pfn are mutually
482 for (; tsize
> BOOK3E_PAGESZ_4K
; tsize
-= 2) {
483 unsigned long gfn_start
, gfn_end
, tsize_pages
;
484 tsize_pages
= 1 << (tsize
- 2);
486 gfn_start
= gfn
& ~(tsize_pages
- 1);
487 gfn_end
= gfn_start
+ tsize_pages
;
489 if (gfn_start
+ pfn
- gfn
< start
)
491 if (gfn_end
+ pfn
- gfn
> end
)
493 if ((gfn
& (tsize_pages
- 1)) !=
494 (pfn
& (tsize_pages
- 1)))
497 gvaddr
&= ~((tsize_pages
<< PAGE_SHIFT
) - 1);
498 pfn
&= ~(tsize_pages
- 1);
501 } else if (vma
&& hva
>= vma
->vm_start
&&
502 (vma
->vm_flags
& VM_HUGETLB
)) {
503 unsigned long psize
= vma_kernel_pagesize(vma
);
505 tsize
= (gtlbe
->mas1
& MAS1_TSIZE_MASK
) >>
509 * Take the largest page size that satisfies both host
512 tsize
= min(__ilog2(psize
) - 10, tsize
);
515 * e500 doesn't implement the lowest tsize bit,
518 tsize
= max(BOOK3E_PAGESZ_4K
, tsize
& ~1);
521 up_read(¤t
->mm
->mmap_sem
);
524 if (likely(!pfnmap
)) {
525 unsigned long tsize_pages
= 1 << (tsize
+ 10 - PAGE_SHIFT
);
526 pfn
= gfn_to_pfn_memslot(slot
, gfn
);
527 if (is_error_noslot_pfn(pfn
)) {
528 printk(KERN_ERR
"Couldn't get real page for gfn %lx!\n",
533 /* Align guest and physical address to page map boundaries */
534 pfn
&= ~(tsize_pages
- 1);
535 gvaddr
&= ~((tsize_pages
<< PAGE_SHIFT
) - 1);
538 /* Drop old ref and setup new one. */
539 kvmppc_e500_ref_release(ref
);
540 kvmppc_e500_ref_setup(ref
, gtlbe
, pfn
);
542 kvmppc_e500_setup_stlbe(&vcpu_e500
->vcpu
, gtlbe
, tsize
,
545 /* Clear i-cache for new pages */
546 kvmppc_mmu_flush_icache(pfn
);
548 /* Drop refcount on page, so that mmu notifiers can clear it */
549 kvm_release_pfn_clean(pfn
);
552 /* XXX only map the one-one case, for now use TLB0 */
553 static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
555 struct kvm_book3e_206_tlb_entry
*stlbe
)
557 struct kvm_book3e_206_tlb_entry
*gtlbe
;
558 struct tlbe_ref
*ref
;
560 gtlbe
= get_entry(vcpu_e500
, 0, esel
);
561 ref
= &vcpu_e500
->gtlb_priv
[0][esel
].ref
;
563 kvmppc_e500_shadow_map(vcpu_e500
, get_tlb_eaddr(gtlbe
),
564 get_tlb_raddr(gtlbe
) >> PAGE_SHIFT
,
565 gtlbe
, 0, stlbe
, ref
);
568 /* Caller must ensure that the specified guest TLB entry is safe to insert into
570 /* XXX for both one-one and one-to-many , for now use TLB1 */
571 static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
572 u64 gvaddr
, gfn_t gfn
, struct kvm_book3e_206_tlb_entry
*gtlbe
,
573 struct kvm_book3e_206_tlb_entry
*stlbe
, int esel
)
575 struct tlbe_ref
*ref
;
578 victim
= vcpu_e500
->host_tlb1_nv
++;
580 if (unlikely(vcpu_e500
->host_tlb1_nv
>= tlb1_max_shadow_size()))
581 vcpu_e500
->host_tlb1_nv
= 0;
583 ref
= &vcpu_e500
->tlb_refs
[1][victim
];
584 kvmppc_e500_shadow_map(vcpu_e500
, gvaddr
, gfn
, gtlbe
, 1, stlbe
, ref
);
586 vcpu_e500
->g2h_tlb1_map
[esel
] |= (u64
)1 << victim
;
587 vcpu_e500
->gtlb_priv
[1][esel
].ref
.flags
|= E500_TLB_BITMAP
;
588 if (vcpu_e500
->h2g_tlb1_rmap
[victim
]) {
589 unsigned int idx
= vcpu_e500
->h2g_tlb1_rmap
[victim
];
590 vcpu_e500
->g2h_tlb1_map
[idx
] &= ~(1ULL << victim
);
592 vcpu_e500
->h2g_tlb1_rmap
[victim
] = esel
;
597 static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500
*vcpu_e500
)
599 int size
= vcpu_e500
->gtlb_params
[1].entries
;
604 vcpu_e500
->tlb1_min_eaddr
= ~0UL;
605 vcpu_e500
->tlb1_max_eaddr
= 0;
606 offset
= vcpu_e500
->gtlb_offset
[1];
608 for (i
= 0; i
< size
; i
++) {
609 struct kvm_book3e_206_tlb_entry
*tlbe
=
610 &vcpu_e500
->gtlb_arch
[offset
+ i
];
612 if (!get_tlb_v(tlbe
))
615 eaddr
= get_tlb_eaddr(tlbe
);
616 vcpu_e500
->tlb1_min_eaddr
=
617 min(vcpu_e500
->tlb1_min_eaddr
, eaddr
);
619 eaddr
= get_tlb_end(tlbe
);
620 vcpu_e500
->tlb1_max_eaddr
=
621 max(vcpu_e500
->tlb1_max_eaddr
, eaddr
);
625 static int kvmppc_need_recalc_tlb1map_range(struct kvmppc_vcpu_e500
*vcpu_e500
,
626 struct kvm_book3e_206_tlb_entry
*gtlbe
)
628 unsigned long start
, end
, size
;
630 size
= get_tlb_bytes(gtlbe
);
631 start
= get_tlb_eaddr(gtlbe
) & ~(size
- 1);
632 end
= start
+ size
- 1;
634 return vcpu_e500
->tlb1_min_eaddr
== start
||
635 vcpu_e500
->tlb1_max_eaddr
== end
;
638 /* This function is supposed to be called for a adding a new valid tlb entry */
639 static void kvmppc_set_tlb1map_range(struct kvm_vcpu
*vcpu
,
640 struct kvm_book3e_206_tlb_entry
*gtlbe
)
642 unsigned long start
, end
, size
;
643 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
645 if (!get_tlb_v(gtlbe
))
648 size
= get_tlb_bytes(gtlbe
);
649 start
= get_tlb_eaddr(gtlbe
) & ~(size
- 1);
650 end
= start
+ size
- 1;
652 vcpu_e500
->tlb1_min_eaddr
= min(vcpu_e500
->tlb1_min_eaddr
, start
);
653 vcpu_e500
->tlb1_max_eaddr
= max(vcpu_e500
->tlb1_max_eaddr
, end
);
656 static inline int kvmppc_e500_gtlbe_invalidate(
657 struct kvmppc_vcpu_e500
*vcpu_e500
,
658 int tlbsel
, int esel
)
660 struct kvm_book3e_206_tlb_entry
*gtlbe
=
661 get_entry(vcpu_e500
, tlbsel
, esel
);
663 if (unlikely(get_tlb_iprot(gtlbe
)))
666 if (tlbsel
== 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500
, gtlbe
))
667 kvmppc_recalc_tlb1map_range(vcpu_e500
);
674 int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500
*vcpu_e500
, ulong value
)
678 if (value
& MMUCSR0_TLB0FI
)
679 for (esel
= 0; esel
< vcpu_e500
->gtlb_params
[0].entries
; esel
++)
680 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, 0, esel
);
681 if (value
& MMUCSR0_TLB1FI
)
682 for (esel
= 0; esel
< vcpu_e500
->gtlb_params
[1].entries
; esel
++)
683 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, 1, esel
);
685 /* Invalidate all vcpu id mappings */
686 kvmppc_e500_tlbil_all(vcpu_e500
);
691 int kvmppc_e500_emul_tlbivax(struct kvm_vcpu
*vcpu
, int ra
, int rb
)
693 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
698 ea
= ((ra
) ? kvmppc_get_gpr(vcpu
, ra
) : 0) + kvmppc_get_gpr(vcpu
, rb
);
700 ia
= (ea
>> 2) & 0x1;
702 /* since we only have two TLBs, only lower bit is used. */
703 tlbsel
= (ea
>> 3) & 0x1;
706 /* invalidate all entries */
707 for (esel
= 0; esel
< vcpu_e500
->gtlb_params
[tlbsel
].entries
;
709 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
712 esel
= kvmppc_e500_tlb_index(vcpu_e500
, ea
, tlbsel
,
713 get_cur_pid(vcpu
), -1);
715 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
718 /* Invalidate all vcpu id mappings */
719 kvmppc_e500_tlbil_all(vcpu_e500
);
724 static void tlbilx_all(struct kvmppc_vcpu_e500
*vcpu_e500
, int tlbsel
,
727 struct kvm_book3e_206_tlb_entry
*tlbe
;
730 /* invalidate all entries */
731 for (esel
= 0; esel
< vcpu_e500
->gtlb_params
[tlbsel
].entries
; esel
++) {
732 tlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
733 tid
= get_tlb_tid(tlbe
);
734 if (rt
== 0 || tid
== pid
) {
735 inval_gtlbe_on_host(vcpu_e500
, tlbsel
, esel
);
736 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
741 static void tlbilx_one(struct kvmppc_vcpu_e500
*vcpu_e500
, int pid
,
747 ea
= kvmppc_get_gpr(&vcpu_e500
->vcpu
, rb
);
749 ea
+= kvmppc_get_gpr(&vcpu_e500
->vcpu
, ra
);
751 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
752 esel
= kvmppc_e500_tlb_index(vcpu_e500
, ea
, tlbsel
, pid
, -1);
754 inval_gtlbe_on_host(vcpu_e500
, tlbsel
, esel
);
755 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
761 int kvmppc_e500_emul_tlbilx(struct kvm_vcpu
*vcpu
, int rt
, int ra
, int rb
)
763 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
764 int pid
= get_cur_spid(vcpu
);
766 if (rt
== 0 || rt
== 1) {
767 tlbilx_all(vcpu_e500
, 0, pid
, rt
);
768 tlbilx_all(vcpu_e500
, 1, pid
, rt
);
769 } else if (rt
== 3) {
770 tlbilx_one(vcpu_e500
, pid
, ra
, rb
);
776 int kvmppc_e500_emul_tlbre(struct kvm_vcpu
*vcpu
)
778 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
780 struct kvm_book3e_206_tlb_entry
*gtlbe
;
782 tlbsel
= get_tlb_tlbsel(vcpu
);
783 esel
= get_tlb_esel(vcpu
, tlbsel
);
785 gtlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
786 vcpu
->arch
.shared
->mas0
&= ~MAS0_NV(~0);
787 vcpu
->arch
.shared
->mas0
|= MAS0_NV(vcpu_e500
->gtlb_nv
[tlbsel
]);
788 vcpu
->arch
.shared
->mas1
= gtlbe
->mas1
;
789 vcpu
->arch
.shared
->mas2
= gtlbe
->mas2
;
790 vcpu
->arch
.shared
->mas7_3
= gtlbe
->mas7_3
;
795 int kvmppc_e500_emul_tlbsx(struct kvm_vcpu
*vcpu
, int rb
)
797 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
798 int as
= !!get_cur_sas(vcpu
);
799 unsigned int pid
= get_cur_spid(vcpu
);
801 struct kvm_book3e_206_tlb_entry
*gtlbe
= NULL
;
804 ea
= kvmppc_get_gpr(vcpu
, rb
);
806 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
807 esel
= kvmppc_e500_tlb_index(vcpu_e500
, ea
, tlbsel
, pid
, as
);
809 gtlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
815 esel
&= vcpu_e500
->gtlb_params
[tlbsel
].ways
- 1;
817 vcpu
->arch
.shared
->mas0
= MAS0_TLBSEL(tlbsel
) | MAS0_ESEL(esel
)
818 | MAS0_NV(vcpu_e500
->gtlb_nv
[tlbsel
]);
819 vcpu
->arch
.shared
->mas1
= gtlbe
->mas1
;
820 vcpu
->arch
.shared
->mas2
= gtlbe
->mas2
;
821 vcpu
->arch
.shared
->mas7_3
= gtlbe
->mas7_3
;
825 /* since we only have two TLBs, only lower bit is used. */
826 tlbsel
= vcpu
->arch
.shared
->mas4
>> 28 & 0x1;
827 victim
= (tlbsel
== 0) ? gtlb0_get_next_victim(vcpu_e500
) : 0;
829 vcpu
->arch
.shared
->mas0
= MAS0_TLBSEL(tlbsel
)
831 | MAS0_NV(vcpu_e500
->gtlb_nv
[tlbsel
]);
832 vcpu
->arch
.shared
->mas1
=
833 (vcpu
->arch
.shared
->mas6
& MAS6_SPID0
)
834 | (vcpu
->arch
.shared
->mas6
& (MAS6_SAS
? MAS1_TS
: 0))
835 | (vcpu
->arch
.shared
->mas4
& MAS4_TSIZED(~0));
836 vcpu
->arch
.shared
->mas2
&= MAS2_EPN
;
837 vcpu
->arch
.shared
->mas2
|= vcpu
->arch
.shared
->mas4
&
839 vcpu
->arch
.shared
->mas7_3
&= MAS3_U0
| MAS3_U1
|
843 kvmppc_set_exit_type(vcpu
, EMULATED_TLBSX_EXITS
);
847 /* sesel is for tlb1 only */
848 static void write_stlbe(struct kvmppc_vcpu_e500
*vcpu_e500
,
849 struct kvm_book3e_206_tlb_entry
*gtlbe
,
850 struct kvm_book3e_206_tlb_entry
*stlbe
,
851 int stlbsel
, int sesel
)
856 stid
= kvmppc_e500_get_tlb_stid(&vcpu_e500
->vcpu
, gtlbe
);
858 stlbe
->mas1
|= MAS1_TID(stid
);
859 write_host_tlbe(vcpu_e500
, stlbsel
, sesel
, stlbe
);
863 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu
*vcpu
)
865 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
866 struct kvm_book3e_206_tlb_entry
*gtlbe
, stlbe
;
867 int tlbsel
, esel
, stlbsel
, sesel
;
870 tlbsel
= get_tlb_tlbsel(vcpu
);
871 esel
= get_tlb_esel(vcpu
, tlbsel
);
873 gtlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
875 if (get_tlb_v(gtlbe
)) {
876 inval_gtlbe_on_host(vcpu_e500
, tlbsel
, esel
);
878 kvmppc_need_recalc_tlb1map_range(vcpu_e500
, gtlbe
))
882 gtlbe
->mas1
= vcpu
->arch
.shared
->mas1
;
883 gtlbe
->mas2
= vcpu
->arch
.shared
->mas2
;
884 gtlbe
->mas7_3
= vcpu
->arch
.shared
->mas7_3
;
886 trace_kvm_booke206_gtlb_write(vcpu
->arch
.shared
->mas0
, gtlbe
->mas1
,
887 gtlbe
->mas2
, gtlbe
->mas7_3
);
891 * If a valid tlb1 entry is overwritten then recalculate the
892 * min/max TLB1 map address range otherwise no need to look
896 kvmppc_recalc_tlb1map_range(vcpu_e500
);
898 kvmppc_set_tlb1map_range(vcpu
, gtlbe
);
901 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
902 if (tlbe_is_host_safe(vcpu
, gtlbe
)) {
909 gtlbe
->mas1
&= ~MAS1_TSIZE(~0);
910 gtlbe
->mas1
|= MAS1_TSIZE(BOOK3E_PAGESZ_4K
);
913 kvmppc_e500_tlb0_map(vcpu_e500
, esel
, &stlbe
);
914 sesel
= 0; /* unused */
920 eaddr
= get_tlb_eaddr(gtlbe
);
921 raddr
= get_tlb_raddr(gtlbe
);
923 /* Create a 4KB mapping on the host.
924 * If the guest wanted a large page,
925 * only the first 4KB is mapped here and the rest
926 * are mapped on the fly. */
928 sesel
= kvmppc_e500_tlb1_map(vcpu_e500
, eaddr
,
929 raddr
>> PAGE_SHIFT
, gtlbe
, &stlbe
, esel
);
936 write_stlbe(vcpu_e500
, gtlbe
, &stlbe
, stlbsel
, sesel
);
939 kvmppc_set_exit_type(vcpu
, EMULATED_TLBWE_EXITS
);
943 static int kvmppc_e500_tlb_search(struct kvm_vcpu
*vcpu
,
944 gva_t eaddr
, unsigned int pid
, int as
)
946 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
949 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
950 esel
= kvmppc_e500_tlb_index(vcpu_e500
, eaddr
, tlbsel
, pid
, as
);
952 return index_of(tlbsel
, esel
);
958 /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
959 int kvmppc_core_vcpu_translate(struct kvm_vcpu
*vcpu
,
960 struct kvm_translation
*tr
)
967 eaddr
= tr
->linear_address
;
968 pid
= (tr
->linear_address
>> 32) & 0xff;
969 as
= (tr
->linear_address
>> 40) & 0x1;
971 index
= kvmppc_e500_tlb_search(vcpu
, eaddr
, pid
, as
);
977 tr
->physical_address
= kvmppc_mmu_xlate(vcpu
, index
, eaddr
);
978 /* XXX what does "writeable" and "usermode" even mean? */
985 int kvmppc_mmu_itlb_index(struct kvm_vcpu
*vcpu
, gva_t eaddr
)
987 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_IS
);
989 return kvmppc_e500_tlb_search(vcpu
, eaddr
, get_cur_pid(vcpu
), as
);
992 int kvmppc_mmu_dtlb_index(struct kvm_vcpu
*vcpu
, gva_t eaddr
)
994 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_DS
);
996 return kvmppc_e500_tlb_search(vcpu
, eaddr
, get_cur_pid(vcpu
), as
);
999 void kvmppc_mmu_itlb_miss(struct kvm_vcpu
*vcpu
)
1001 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_IS
);
1003 kvmppc_e500_deliver_tlb_miss(vcpu
, vcpu
->arch
.pc
, as
);
1006 void kvmppc_mmu_dtlb_miss(struct kvm_vcpu
*vcpu
)
1008 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_DS
);
1010 kvmppc_e500_deliver_tlb_miss(vcpu
, vcpu
->arch
.fault_dear
, as
);
1013 gpa_t
kvmppc_mmu_xlate(struct kvm_vcpu
*vcpu
, unsigned int index
,
1016 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
1017 struct kvm_book3e_206_tlb_entry
*gtlbe
;
1020 gtlbe
= get_entry(vcpu_e500
, tlbsel_of(index
), esel_of(index
));
1021 pgmask
= get_tlb_bytes(gtlbe
) - 1;
1023 return get_tlb_raddr(gtlbe
) | (eaddr
& pgmask
);
1026 void kvmppc_mmu_destroy(struct kvm_vcpu
*vcpu
)
1030 void kvmppc_mmu_map(struct kvm_vcpu
*vcpu
, u64 eaddr
, gpa_t gpaddr
,
1033 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
1034 struct tlbe_priv
*priv
;
1035 struct kvm_book3e_206_tlb_entry
*gtlbe
, stlbe
;
1036 int tlbsel
= tlbsel_of(index
);
1037 int esel
= esel_of(index
);
1040 gtlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
1045 sesel
= 0; /* unused */
1046 priv
= &vcpu_e500
->gtlb_priv
[tlbsel
][esel
];
1048 /* Only triggers after clear_tlb_refs */
1049 if (unlikely(!(priv
->ref
.flags
& E500_TLB_VALID
)))
1050 kvmppc_e500_tlb0_map(vcpu_e500
, esel
, &stlbe
);
1052 kvmppc_e500_setup_stlbe(vcpu
, gtlbe
, BOOK3E_PAGESZ_4K
,
1053 &priv
->ref
, eaddr
, &stlbe
);
1057 gfn_t gfn
= gpaddr
>> PAGE_SHIFT
;
1060 sesel
= kvmppc_e500_tlb1_map(vcpu_e500
, eaddr
, gfn
,
1061 gtlbe
, &stlbe
, esel
);
1070 write_stlbe(vcpu_e500
, gtlbe
, &stlbe
, stlbsel
, sesel
);
1073 /************* MMU Notifiers *************/
1075 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
1077 trace_kvm_unmap_hva(hva
);
1080 * Flush all shadow tlb entries everywhere. This is slow, but
1081 * we are 100% sure that we catch the to be unmapped page
1083 kvm_flush_remote_tlbs(kvm
);
1088 int kvm_unmap_hva_range(struct kvm
*kvm
, unsigned long start
, unsigned long end
)
1090 /* kvm_unmap_hva flushes everything anyways */
1091 kvm_unmap_hva(kvm
, start
);
1096 int kvm_age_hva(struct kvm
*kvm
, unsigned long hva
)
1098 /* XXX could be more clever ;) */
1102 int kvm_test_age_hva(struct kvm
*kvm
, unsigned long hva
)
1104 /* XXX could be more clever ;) */
1108 void kvm_set_spte_hva(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
1110 /* The page will get remapped properly on its next fault */
1111 kvm_unmap_hva(kvm
, hva
);
1114 /*****************************************/
1116 static void free_gtlb(struct kvmppc_vcpu_e500
*vcpu_e500
)
1120 clear_tlb1_bitmap(vcpu_e500
);
1121 kfree(vcpu_e500
->g2h_tlb1_map
);
1123 clear_tlb_refs(vcpu_e500
);
1124 kfree(vcpu_e500
->gtlb_priv
[0]);
1125 kfree(vcpu_e500
->gtlb_priv
[1]);
1127 if (vcpu_e500
->shared_tlb_pages
) {
1128 vfree((void *)(round_down((uintptr_t)vcpu_e500
->gtlb_arch
,
1131 for (i
= 0; i
< vcpu_e500
->num_shared_tlb_pages
; i
++) {
1132 set_page_dirty_lock(vcpu_e500
->shared_tlb_pages
[i
]);
1133 put_page(vcpu_e500
->shared_tlb_pages
[i
]);
1136 vcpu_e500
->num_shared_tlb_pages
= 0;
1138 kfree(vcpu_e500
->shared_tlb_pages
);
1139 vcpu_e500
->shared_tlb_pages
= NULL
;
1141 kfree(vcpu_e500
->gtlb_arch
);
1144 vcpu_e500
->gtlb_arch
= NULL
;
1147 void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu
*vcpu
, struct kvm_sregs
*sregs
)
1149 sregs
->u
.e
.mas0
= vcpu
->arch
.shared
->mas0
;
1150 sregs
->u
.e
.mas1
= vcpu
->arch
.shared
->mas1
;
1151 sregs
->u
.e
.mas2
= vcpu
->arch
.shared
->mas2
;
1152 sregs
->u
.e
.mas7_3
= vcpu
->arch
.shared
->mas7_3
;
1153 sregs
->u
.e
.mas4
= vcpu
->arch
.shared
->mas4
;
1154 sregs
->u
.e
.mas6
= vcpu
->arch
.shared
->mas6
;
1156 sregs
->u
.e
.mmucfg
= vcpu
->arch
.mmucfg
;
1157 sregs
->u
.e
.tlbcfg
[0] = vcpu
->arch
.tlbcfg
[0];
1158 sregs
->u
.e
.tlbcfg
[1] = vcpu
->arch
.tlbcfg
[1];
1159 sregs
->u
.e
.tlbcfg
[2] = 0;
1160 sregs
->u
.e
.tlbcfg
[3] = 0;
1163 int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu
*vcpu
, struct kvm_sregs
*sregs
)
1165 if (sregs
->u
.e
.features
& KVM_SREGS_E_ARCH206_MMU
) {
1166 vcpu
->arch
.shared
->mas0
= sregs
->u
.e
.mas0
;
1167 vcpu
->arch
.shared
->mas1
= sregs
->u
.e
.mas1
;
1168 vcpu
->arch
.shared
->mas2
= sregs
->u
.e
.mas2
;
1169 vcpu
->arch
.shared
->mas7_3
= sregs
->u
.e
.mas7_3
;
1170 vcpu
->arch
.shared
->mas4
= sregs
->u
.e
.mas4
;
1171 vcpu
->arch
.shared
->mas6
= sregs
->u
.e
.mas6
;
1177 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu
*vcpu
,
1178 struct kvm_config_tlb
*cfg
)
1180 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
1181 struct kvm_book3e_206_tlb_params params
;
1183 struct page
**pages
;
1184 struct tlbe_priv
*privs
[2] = {};
1185 u64
*g2h_bitmap
= NULL
;
1188 int num_pages
, ret
, i
;
1190 if (cfg
->mmu_type
!= KVM_MMU_FSL_BOOKE_NOHV
)
1193 if (copy_from_user(¶ms
, (void __user
*)(uintptr_t)cfg
->params
,
1197 if (params
.tlb_sizes
[1] > 64)
1199 if (params
.tlb_ways
[1] != params
.tlb_sizes
[1])
1201 if (params
.tlb_sizes
[2] != 0 || params
.tlb_sizes
[3] != 0)
1203 if (params
.tlb_ways
[2] != 0 || params
.tlb_ways
[3] != 0)
1206 if (!is_power_of_2(params
.tlb_ways
[0]))
1209 sets
= params
.tlb_sizes
[0] >> ilog2(params
.tlb_ways
[0]);
1210 if (!is_power_of_2(sets
))
1213 array_len
= params
.tlb_sizes
[0] + params
.tlb_sizes
[1];
1214 array_len
*= sizeof(struct kvm_book3e_206_tlb_entry
);
1216 if (cfg
->array_len
< array_len
)
1219 num_pages
= DIV_ROUND_UP(cfg
->array
+ array_len
- 1, PAGE_SIZE
) -
1220 cfg
->array
/ PAGE_SIZE
;
1221 pages
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
1225 ret
= get_user_pages_fast(cfg
->array
, num_pages
, 1, pages
);
1229 if (ret
!= num_pages
) {
1235 virt
= vmap(pages
, num_pages
, VM_MAP
, PAGE_KERNEL
);
1241 privs
[0] = kzalloc(sizeof(struct tlbe_priv
) * params
.tlb_sizes
[0],
1243 privs
[1] = kzalloc(sizeof(struct tlbe_priv
) * params
.tlb_sizes
[1],
1246 if (!privs
[0] || !privs
[1]) {
1251 g2h_bitmap
= kzalloc(sizeof(u64
) * params
.tlb_sizes
[1],
1258 free_gtlb(vcpu_e500
);
1260 vcpu_e500
->gtlb_priv
[0] = privs
[0];
1261 vcpu_e500
->gtlb_priv
[1] = privs
[1];
1262 vcpu_e500
->g2h_tlb1_map
= g2h_bitmap
;
1264 vcpu_e500
->gtlb_arch
= (struct kvm_book3e_206_tlb_entry
*)
1265 (virt
+ (cfg
->array
& (PAGE_SIZE
- 1)));
1267 vcpu_e500
->gtlb_params
[0].entries
= params
.tlb_sizes
[0];
1268 vcpu_e500
->gtlb_params
[1].entries
= params
.tlb_sizes
[1];
1270 vcpu_e500
->gtlb_offset
[0] = 0;
1271 vcpu_e500
->gtlb_offset
[1] = params
.tlb_sizes
[0];
1273 vcpu
->arch
.mmucfg
= mfspr(SPRN_MMUCFG
) & ~MMUCFG_LPIDSIZE
;
1275 vcpu
->arch
.tlbcfg
[0] &= ~(TLBnCFG_N_ENTRY
| TLBnCFG_ASSOC
);
1276 if (params
.tlb_sizes
[0] <= 2048)
1277 vcpu
->arch
.tlbcfg
[0] |= params
.tlb_sizes
[0];
1278 vcpu
->arch
.tlbcfg
[0] |= params
.tlb_ways
[0] << TLBnCFG_ASSOC_SHIFT
;
1280 vcpu
->arch
.tlbcfg
[1] &= ~(TLBnCFG_N_ENTRY
| TLBnCFG_ASSOC
);
1281 vcpu
->arch
.tlbcfg
[1] |= params
.tlb_sizes
[1];
1282 vcpu
->arch
.tlbcfg
[1] |= params
.tlb_ways
[1] << TLBnCFG_ASSOC_SHIFT
;
1284 vcpu_e500
->shared_tlb_pages
= pages
;
1285 vcpu_e500
->num_shared_tlb_pages
= num_pages
;
1287 vcpu_e500
->gtlb_params
[0].ways
= params
.tlb_ways
[0];
1288 vcpu_e500
->gtlb_params
[0].sets
= sets
;
1290 vcpu_e500
->gtlb_params
[1].ways
= params
.tlb_sizes
[1];
1291 vcpu_e500
->gtlb_params
[1].sets
= 1;
1293 kvmppc_recalc_tlb1map_range(vcpu_e500
);
1301 for (i
= 0; i
< num_pages
; i
++)
1309 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu
*vcpu
,
1310 struct kvm_dirty_tlb
*dirty
)
1312 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
1313 kvmppc_recalc_tlb1map_range(vcpu_e500
);
1314 clear_tlb_refs(vcpu_e500
);
1318 int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500
*vcpu_e500
)
1320 struct kvm_vcpu
*vcpu
= &vcpu_e500
->vcpu
;
1321 int entry_size
= sizeof(struct kvm_book3e_206_tlb_entry
);
1322 int entries
= KVM_E500_TLB0_SIZE
+ KVM_E500_TLB1_SIZE
;
1324 host_tlb_params
[0].entries
= mfspr(SPRN_TLB0CFG
) & TLBnCFG_N_ENTRY
;
1325 host_tlb_params
[1].entries
= mfspr(SPRN_TLB1CFG
) & TLBnCFG_N_ENTRY
;
1328 * This should never happen on real e500 hardware, but is
1329 * architecturally possible -- e.g. in some weird nested
1330 * virtualization case.
1332 if (host_tlb_params
[0].entries
== 0 ||
1333 host_tlb_params
[1].entries
== 0) {
1334 pr_err("%s: need to know host tlb size\n", __func__
);
1338 host_tlb_params
[0].ways
= (mfspr(SPRN_TLB0CFG
) & TLBnCFG_ASSOC
) >>
1339 TLBnCFG_ASSOC_SHIFT
;
1340 host_tlb_params
[1].ways
= host_tlb_params
[1].entries
;
1342 if (!is_power_of_2(host_tlb_params
[0].entries
) ||
1343 !is_power_of_2(host_tlb_params
[0].ways
) ||
1344 host_tlb_params
[0].entries
< host_tlb_params
[0].ways
||
1345 host_tlb_params
[0].ways
== 0) {
1346 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1347 __func__
, host_tlb_params
[0].entries
,
1348 host_tlb_params
[0].ways
);
1352 host_tlb_params
[0].sets
=
1353 host_tlb_params
[0].entries
/ host_tlb_params
[0].ways
;
1354 host_tlb_params
[1].sets
= 1;
1356 vcpu_e500
->gtlb_params
[0].entries
= KVM_E500_TLB0_SIZE
;
1357 vcpu_e500
->gtlb_params
[1].entries
= KVM_E500_TLB1_SIZE
;
1359 vcpu_e500
->gtlb_params
[0].ways
= KVM_E500_TLB0_WAY_NUM
;
1360 vcpu_e500
->gtlb_params
[0].sets
=
1361 KVM_E500_TLB0_SIZE
/ KVM_E500_TLB0_WAY_NUM
;
1363 vcpu_e500
->gtlb_params
[1].ways
= KVM_E500_TLB1_SIZE
;
1364 vcpu_e500
->gtlb_params
[1].sets
= 1;
1366 vcpu_e500
->gtlb_arch
= kmalloc(entries
* entry_size
, GFP_KERNEL
);
1367 if (!vcpu_e500
->gtlb_arch
)
1370 vcpu_e500
->gtlb_offset
[0] = 0;
1371 vcpu_e500
->gtlb_offset
[1] = KVM_E500_TLB0_SIZE
;
1373 vcpu_e500
->tlb_refs
[0] =
1374 kzalloc(sizeof(struct tlbe_ref
) * host_tlb_params
[0].entries
,
1376 if (!vcpu_e500
->tlb_refs
[0])
1379 vcpu_e500
->tlb_refs
[1] =
1380 kzalloc(sizeof(struct tlbe_ref
) * host_tlb_params
[1].entries
,
1382 if (!vcpu_e500
->tlb_refs
[1])
1385 vcpu_e500
->gtlb_priv
[0] = kzalloc(sizeof(struct tlbe_ref
) *
1386 vcpu_e500
->gtlb_params
[0].entries
,
1388 if (!vcpu_e500
->gtlb_priv
[0])
1391 vcpu_e500
->gtlb_priv
[1] = kzalloc(sizeof(struct tlbe_ref
) *
1392 vcpu_e500
->gtlb_params
[1].entries
,
1394 if (!vcpu_e500
->gtlb_priv
[1])
1397 vcpu_e500
->g2h_tlb1_map
= kzalloc(sizeof(u64
) *
1398 vcpu_e500
->gtlb_params
[1].entries
,
1400 if (!vcpu_e500
->g2h_tlb1_map
)
1403 vcpu_e500
->h2g_tlb1_rmap
= kzalloc(sizeof(unsigned int) *
1404 host_tlb_params
[1].entries
,
1406 if (!vcpu_e500
->h2g_tlb1_rmap
)
1409 /* Init TLB configuration register */
1410 vcpu
->arch
.tlbcfg
[0] = mfspr(SPRN_TLB0CFG
) &
1411 ~(TLBnCFG_N_ENTRY
| TLBnCFG_ASSOC
);
1412 vcpu
->arch
.tlbcfg
[0] |= vcpu_e500
->gtlb_params
[0].entries
;
1413 vcpu
->arch
.tlbcfg
[0] |=
1414 vcpu_e500
->gtlb_params
[0].ways
<< TLBnCFG_ASSOC_SHIFT
;
1416 vcpu
->arch
.tlbcfg
[1] = mfspr(SPRN_TLB1CFG
) &
1417 ~(TLBnCFG_N_ENTRY
| TLBnCFG_ASSOC
);
1418 vcpu
->arch
.tlbcfg
[1] |= vcpu_e500
->gtlb_params
[1].entries
;
1419 vcpu
->arch
.tlbcfg
[1] |=
1420 vcpu_e500
->gtlb_params
[1].ways
<< TLBnCFG_ASSOC_SHIFT
;
1422 kvmppc_recalc_tlb1map_range(vcpu_e500
);
1426 free_gtlb(vcpu_e500
);
1427 kfree(vcpu_e500
->tlb_refs
[0]);
1428 kfree(vcpu_e500
->tlb_refs
[1]);
1432 void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500
*vcpu_e500
)
1434 free_gtlb(vcpu_e500
);
1435 kfree(vcpu_e500
->h2g_tlb1_rmap
);
1436 kfree(vcpu_e500
->tlb_refs
[0]);
1437 kfree(vcpu_e500
->tlb_refs
[1]);