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 ref
->flags
|= E500_TLB_DIRTY
;
310 static inline void kvmppc_e500_ref_release(struct tlbe_ref
*ref
)
312 if (ref
->flags
& E500_TLB_VALID
) {
313 if (ref
->flags
& E500_TLB_DIRTY
)
314 kvm_release_pfn_dirty(ref
->pfn
);
316 kvm_release_pfn_clean(ref
->pfn
);
322 static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500
*vcpu_e500
)
324 if (vcpu_e500
->g2h_tlb1_map
)
325 memset(vcpu_e500
->g2h_tlb1_map
, 0,
326 sizeof(u64
) * vcpu_e500
->gtlb_params
[1].entries
);
327 if (vcpu_e500
->h2g_tlb1_rmap
)
328 memset(vcpu_e500
->h2g_tlb1_rmap
, 0,
329 sizeof(unsigned int) * host_tlb_params
[1].entries
);
332 static void clear_tlb_privs(struct kvmppc_vcpu_e500
*vcpu_e500
)
337 for (i
= 0; i
< vcpu_e500
->gtlb_params
[tlbsel
].entries
; i
++) {
338 struct tlbe_ref
*ref
=
339 &vcpu_e500
->gtlb_priv
[tlbsel
][i
].ref
;
340 kvmppc_e500_ref_release(ref
);
344 static void clear_tlb_refs(struct kvmppc_vcpu_e500
*vcpu_e500
)
349 kvmppc_e500_tlbil_all(vcpu_e500
);
351 for (i
= 0; i
< host_tlb_params
[stlbsel
].entries
; i
++) {
352 struct tlbe_ref
*ref
=
353 &vcpu_e500
->tlb_refs
[stlbsel
][i
];
354 kvmppc_e500_ref_release(ref
);
357 clear_tlb_privs(vcpu_e500
);
360 static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu
*vcpu
,
361 unsigned int eaddr
, int as
)
363 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
364 unsigned int victim
, tsized
;
367 /* since we only have two TLBs, only lower bit is used. */
368 tlbsel
= (vcpu
->arch
.shared
->mas4
>> 28) & 0x1;
369 victim
= (tlbsel
== 0) ? gtlb0_get_next_victim(vcpu_e500
) : 0;
370 tsized
= (vcpu
->arch
.shared
->mas4
>> 7) & 0x1f;
372 vcpu
->arch
.shared
->mas0
= MAS0_TLBSEL(tlbsel
) | MAS0_ESEL(victim
)
373 | MAS0_NV(vcpu_e500
->gtlb_nv
[tlbsel
]);
374 vcpu
->arch
.shared
->mas1
= MAS1_VALID
| (as
? MAS1_TS
: 0)
375 | MAS1_TID(get_tlbmiss_tid(vcpu
))
376 | MAS1_TSIZE(tsized
);
377 vcpu
->arch
.shared
->mas2
= (eaddr
& MAS2_EPN
)
378 | (vcpu
->arch
.shared
->mas4
& MAS2_ATTRIB_MASK
);
379 vcpu
->arch
.shared
->mas7_3
&= MAS3_U0
| MAS3_U1
| MAS3_U2
| MAS3_U3
;
380 vcpu
->arch
.shared
->mas6
= (vcpu
->arch
.shared
->mas6
& MAS6_SPID1
)
381 | (get_cur_pid(vcpu
) << 16)
382 | (as
? MAS6_SAS
: 0);
385 /* TID must be supplied by the caller */
386 static inline void kvmppc_e500_setup_stlbe(
387 struct kvm_vcpu
*vcpu
,
388 struct kvm_book3e_206_tlb_entry
*gtlbe
,
389 int tsize
, struct tlbe_ref
*ref
, u64 gvaddr
,
390 struct kvm_book3e_206_tlb_entry
*stlbe
)
392 pfn_t pfn
= ref
->pfn
;
393 u32 pr
= vcpu
->arch
.shared
->msr
& MSR_PR
;
395 BUG_ON(!(ref
->flags
& E500_TLB_VALID
));
397 /* Force IPROT=0 for all guest mappings. */
398 stlbe
->mas1
= MAS1_TSIZE(tsize
) | get_tlb_sts(gtlbe
) | MAS1_VALID
;
399 stlbe
->mas2
= (gvaddr
& MAS2_EPN
) |
400 e500_shadow_mas2_attrib(gtlbe
->mas2
, pr
);
401 stlbe
->mas7_3
= ((u64
)pfn
<< PAGE_SHIFT
) |
402 e500_shadow_mas3_attrib(gtlbe
->mas7_3
, pr
);
404 #ifdef CONFIG_KVM_BOOKE_HV
405 stlbe
->mas8
= MAS8_TGS
| vcpu
->kvm
->arch
.lpid
;
409 static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
410 u64 gvaddr
, gfn_t gfn
, struct kvm_book3e_206_tlb_entry
*gtlbe
,
411 int tlbsel
, struct kvm_book3e_206_tlb_entry
*stlbe
,
412 struct tlbe_ref
*ref
)
414 struct kvm_memory_slot
*slot
;
415 unsigned long pfn
, hva
;
417 int tsize
= BOOK3E_PAGESZ_4K
;
420 * Translate guest physical to true physical, acquiring
421 * a page reference if it is normal, non-reserved memory.
423 * gfn_to_memslot() must succeed because otherwise we wouldn't
424 * have gotten this far. Eventually we should just pass the slot
425 * pointer through from the first lookup.
427 slot
= gfn_to_memslot(vcpu_e500
->vcpu
.kvm
, gfn
);
428 hva
= gfn_to_hva_memslot(slot
, gfn
);
431 struct vm_area_struct
*vma
;
432 down_read(¤t
->mm
->mmap_sem
);
434 vma
= find_vma(current
->mm
, hva
);
435 if (vma
&& hva
>= vma
->vm_start
&&
436 (vma
->vm_flags
& VM_PFNMAP
)) {
438 * This VMA is a physically contiguous region (e.g.
439 * /dev/mem) that bypasses normal Linux page
440 * management. Find the overlap between the
441 * vma and the memslot.
444 unsigned long start
, end
;
445 unsigned long slot_start
, slot_end
;
449 start
= vma
->vm_pgoff
;
451 ((vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
);
453 pfn
= start
+ ((hva
- vma
->vm_start
) >> PAGE_SHIFT
);
455 slot_start
= pfn
- (gfn
- slot
->base_gfn
);
456 slot_end
= slot_start
+ slot
->npages
;
458 if (start
< slot_start
)
463 tsize
= (gtlbe
->mas1
& MAS1_TSIZE_MASK
) >>
467 * e500 doesn't implement the lowest tsize bit,
470 tsize
= max(BOOK3E_PAGESZ_4K
, tsize
& ~1);
473 * Now find the largest tsize (up to what the guest
474 * requested) that will cover gfn, stay within the
475 * range, and for which gfn and pfn are mutually
479 for (; tsize
> BOOK3E_PAGESZ_4K
; tsize
-= 2) {
480 unsigned long gfn_start
, gfn_end
, tsize_pages
;
481 tsize_pages
= 1 << (tsize
- 2);
483 gfn_start
= gfn
& ~(tsize_pages
- 1);
484 gfn_end
= gfn_start
+ tsize_pages
;
486 if (gfn_start
+ pfn
- gfn
< start
)
488 if (gfn_end
+ pfn
- gfn
> end
)
490 if ((gfn
& (tsize_pages
- 1)) !=
491 (pfn
& (tsize_pages
- 1)))
494 gvaddr
&= ~((tsize_pages
<< PAGE_SHIFT
) - 1);
495 pfn
&= ~(tsize_pages
- 1);
498 } else if (vma
&& hva
>= vma
->vm_start
&&
499 (vma
->vm_flags
& VM_HUGETLB
)) {
500 unsigned long psize
= vma_kernel_pagesize(vma
);
502 tsize
= (gtlbe
->mas1
& MAS1_TSIZE_MASK
) >>
506 * Take the largest page size that satisfies both host
509 tsize
= min(__ilog2(psize
) - 10, tsize
);
512 * e500 doesn't implement the lowest tsize bit,
515 tsize
= max(BOOK3E_PAGESZ_4K
, tsize
& ~1);
518 up_read(¤t
->mm
->mmap_sem
);
521 if (likely(!pfnmap
)) {
522 unsigned long tsize_pages
= 1 << (tsize
+ 10 - PAGE_SHIFT
);
523 pfn
= gfn_to_pfn_memslot(vcpu_e500
->vcpu
.kvm
, slot
, gfn
);
524 if (is_error_pfn(pfn
)) {
525 printk(KERN_ERR
"Couldn't get real page for gfn %lx!\n",
527 kvm_release_pfn_clean(pfn
);
531 /* Align guest and physical address to page map boundaries */
532 pfn
&= ~(tsize_pages
- 1);
533 gvaddr
&= ~((tsize_pages
<< PAGE_SHIFT
) - 1);
536 /* Drop old ref and setup new one. */
537 kvmppc_e500_ref_release(ref
);
538 kvmppc_e500_ref_setup(ref
, gtlbe
, pfn
);
540 kvmppc_e500_setup_stlbe(&vcpu_e500
->vcpu
, gtlbe
, tsize
,
543 /* Clear i-cache for new pages */
544 kvmppc_mmu_flush_icache(pfn
);
547 /* XXX only map the one-one case, for now use TLB0 */
548 static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
550 struct kvm_book3e_206_tlb_entry
*stlbe
)
552 struct kvm_book3e_206_tlb_entry
*gtlbe
;
553 struct tlbe_ref
*ref
;
555 gtlbe
= get_entry(vcpu_e500
, 0, esel
);
556 ref
= &vcpu_e500
->gtlb_priv
[0][esel
].ref
;
558 kvmppc_e500_shadow_map(vcpu_e500
, get_tlb_eaddr(gtlbe
),
559 get_tlb_raddr(gtlbe
) >> PAGE_SHIFT
,
560 gtlbe
, 0, stlbe
, ref
);
563 /* Caller must ensure that the specified guest TLB entry is safe to insert into
565 /* XXX for both one-one and one-to-many , for now use TLB1 */
566 static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
567 u64 gvaddr
, gfn_t gfn
, struct kvm_book3e_206_tlb_entry
*gtlbe
,
568 struct kvm_book3e_206_tlb_entry
*stlbe
, int esel
)
570 struct tlbe_ref
*ref
;
573 victim
= vcpu_e500
->host_tlb1_nv
++;
575 if (unlikely(vcpu_e500
->host_tlb1_nv
>= tlb1_max_shadow_size()))
576 vcpu_e500
->host_tlb1_nv
= 0;
578 ref
= &vcpu_e500
->tlb_refs
[1][victim
];
579 kvmppc_e500_shadow_map(vcpu_e500
, gvaddr
, gfn
, gtlbe
, 1, stlbe
, ref
);
581 vcpu_e500
->g2h_tlb1_map
[esel
] |= (u64
)1 << victim
;
582 vcpu_e500
->gtlb_priv
[1][esel
].ref
.flags
|= E500_TLB_BITMAP
;
583 if (vcpu_e500
->h2g_tlb1_rmap
[victim
]) {
584 unsigned int idx
= vcpu_e500
->h2g_tlb1_rmap
[victim
];
585 vcpu_e500
->g2h_tlb1_map
[idx
] &= ~(1ULL << victim
);
587 vcpu_e500
->h2g_tlb1_rmap
[victim
] = esel
;
592 static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500
*vcpu_e500
)
594 int size
= vcpu_e500
->gtlb_params
[1].entries
;
599 vcpu_e500
->tlb1_min_eaddr
= ~0UL;
600 vcpu_e500
->tlb1_max_eaddr
= 0;
601 offset
= vcpu_e500
->gtlb_offset
[1];
603 for (i
= 0; i
< size
; i
++) {
604 struct kvm_book3e_206_tlb_entry
*tlbe
=
605 &vcpu_e500
->gtlb_arch
[offset
+ i
];
607 if (!get_tlb_v(tlbe
))
610 eaddr
= get_tlb_eaddr(tlbe
);
611 vcpu_e500
->tlb1_min_eaddr
=
612 min(vcpu_e500
->tlb1_min_eaddr
, eaddr
);
614 eaddr
= get_tlb_end(tlbe
);
615 vcpu_e500
->tlb1_max_eaddr
=
616 max(vcpu_e500
->tlb1_max_eaddr
, eaddr
);
620 static int kvmppc_need_recalc_tlb1map_range(struct kvmppc_vcpu_e500
*vcpu_e500
,
621 struct kvm_book3e_206_tlb_entry
*gtlbe
)
623 unsigned long start
, end
, size
;
625 size
= get_tlb_bytes(gtlbe
);
626 start
= get_tlb_eaddr(gtlbe
) & ~(size
- 1);
627 end
= start
+ size
- 1;
629 return vcpu_e500
->tlb1_min_eaddr
== start
||
630 vcpu_e500
->tlb1_max_eaddr
== end
;
633 /* This function is supposed to be called for a adding a new valid tlb entry */
634 static void kvmppc_set_tlb1map_range(struct kvm_vcpu
*vcpu
,
635 struct kvm_book3e_206_tlb_entry
*gtlbe
)
637 unsigned long start
, end
, size
;
638 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
640 if (!get_tlb_v(gtlbe
))
643 size
= get_tlb_bytes(gtlbe
);
644 start
= get_tlb_eaddr(gtlbe
) & ~(size
- 1);
645 end
= start
+ size
- 1;
647 vcpu_e500
->tlb1_min_eaddr
= min(vcpu_e500
->tlb1_min_eaddr
, start
);
648 vcpu_e500
->tlb1_max_eaddr
= max(vcpu_e500
->tlb1_max_eaddr
, end
);
651 static inline int kvmppc_e500_gtlbe_invalidate(
652 struct kvmppc_vcpu_e500
*vcpu_e500
,
653 int tlbsel
, int esel
)
655 struct kvm_book3e_206_tlb_entry
*gtlbe
=
656 get_entry(vcpu_e500
, tlbsel
, esel
);
658 if (unlikely(get_tlb_iprot(gtlbe
)))
661 if (tlbsel
== 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500
, gtlbe
))
662 kvmppc_recalc_tlb1map_range(vcpu_e500
);
669 int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500
*vcpu_e500
, ulong value
)
673 if (value
& MMUCSR0_TLB0FI
)
674 for (esel
= 0; esel
< vcpu_e500
->gtlb_params
[0].entries
; esel
++)
675 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, 0, esel
);
676 if (value
& MMUCSR0_TLB1FI
)
677 for (esel
= 0; esel
< vcpu_e500
->gtlb_params
[1].entries
; esel
++)
678 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, 1, esel
);
680 /* Invalidate all vcpu id mappings */
681 kvmppc_e500_tlbil_all(vcpu_e500
);
686 int kvmppc_e500_emul_tlbivax(struct kvm_vcpu
*vcpu
, int ra
, int rb
)
688 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
693 ea
= ((ra
) ? kvmppc_get_gpr(vcpu
, ra
) : 0) + kvmppc_get_gpr(vcpu
, rb
);
695 ia
= (ea
>> 2) & 0x1;
697 /* since we only have two TLBs, only lower bit is used. */
698 tlbsel
= (ea
>> 3) & 0x1;
701 /* invalidate all entries */
702 for (esel
= 0; esel
< vcpu_e500
->gtlb_params
[tlbsel
].entries
;
704 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
707 esel
= kvmppc_e500_tlb_index(vcpu_e500
, ea
, tlbsel
,
708 get_cur_pid(vcpu
), -1);
710 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
713 /* Invalidate all vcpu id mappings */
714 kvmppc_e500_tlbil_all(vcpu_e500
);
719 static void tlbilx_all(struct kvmppc_vcpu_e500
*vcpu_e500
, int tlbsel
,
722 struct kvm_book3e_206_tlb_entry
*tlbe
;
725 /* invalidate all entries */
726 for (esel
= 0; esel
< vcpu_e500
->gtlb_params
[tlbsel
].entries
; esel
++) {
727 tlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
728 tid
= get_tlb_tid(tlbe
);
729 if (rt
== 0 || tid
== pid
) {
730 inval_gtlbe_on_host(vcpu_e500
, tlbsel
, esel
);
731 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
736 static void tlbilx_one(struct kvmppc_vcpu_e500
*vcpu_e500
, int pid
,
742 ea
= kvmppc_get_gpr(&vcpu_e500
->vcpu
, rb
);
744 ea
+= kvmppc_get_gpr(&vcpu_e500
->vcpu
, ra
);
746 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
747 esel
= kvmppc_e500_tlb_index(vcpu_e500
, ea
, tlbsel
, pid
, -1);
749 inval_gtlbe_on_host(vcpu_e500
, tlbsel
, esel
);
750 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
756 int kvmppc_e500_emul_tlbilx(struct kvm_vcpu
*vcpu
, int rt
, int ra
, int rb
)
758 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
759 int pid
= get_cur_spid(vcpu
);
761 if (rt
== 0 || rt
== 1) {
762 tlbilx_all(vcpu_e500
, 0, pid
, rt
);
763 tlbilx_all(vcpu_e500
, 1, pid
, rt
);
764 } else if (rt
== 3) {
765 tlbilx_one(vcpu_e500
, pid
, ra
, rb
);
771 int kvmppc_e500_emul_tlbre(struct kvm_vcpu
*vcpu
)
773 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
775 struct kvm_book3e_206_tlb_entry
*gtlbe
;
777 tlbsel
= get_tlb_tlbsel(vcpu
);
778 esel
= get_tlb_esel(vcpu
, tlbsel
);
780 gtlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
781 vcpu
->arch
.shared
->mas0
&= ~MAS0_NV(~0);
782 vcpu
->arch
.shared
->mas0
|= MAS0_NV(vcpu_e500
->gtlb_nv
[tlbsel
]);
783 vcpu
->arch
.shared
->mas1
= gtlbe
->mas1
;
784 vcpu
->arch
.shared
->mas2
= gtlbe
->mas2
;
785 vcpu
->arch
.shared
->mas7_3
= gtlbe
->mas7_3
;
790 int kvmppc_e500_emul_tlbsx(struct kvm_vcpu
*vcpu
, int rb
)
792 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
793 int as
= !!get_cur_sas(vcpu
);
794 unsigned int pid
= get_cur_spid(vcpu
);
796 struct kvm_book3e_206_tlb_entry
*gtlbe
= NULL
;
799 ea
= kvmppc_get_gpr(vcpu
, rb
);
801 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
802 esel
= kvmppc_e500_tlb_index(vcpu_e500
, ea
, tlbsel
, pid
, as
);
804 gtlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
810 esel
&= vcpu_e500
->gtlb_params
[tlbsel
].ways
- 1;
812 vcpu
->arch
.shared
->mas0
= MAS0_TLBSEL(tlbsel
) | MAS0_ESEL(esel
)
813 | MAS0_NV(vcpu_e500
->gtlb_nv
[tlbsel
]);
814 vcpu
->arch
.shared
->mas1
= gtlbe
->mas1
;
815 vcpu
->arch
.shared
->mas2
= gtlbe
->mas2
;
816 vcpu
->arch
.shared
->mas7_3
= gtlbe
->mas7_3
;
820 /* since we only have two TLBs, only lower bit is used. */
821 tlbsel
= vcpu
->arch
.shared
->mas4
>> 28 & 0x1;
822 victim
= (tlbsel
== 0) ? gtlb0_get_next_victim(vcpu_e500
) : 0;
824 vcpu
->arch
.shared
->mas0
= MAS0_TLBSEL(tlbsel
)
826 | MAS0_NV(vcpu_e500
->gtlb_nv
[tlbsel
]);
827 vcpu
->arch
.shared
->mas1
=
828 (vcpu
->arch
.shared
->mas6
& MAS6_SPID0
)
829 | (vcpu
->arch
.shared
->mas6
& (MAS6_SAS
? MAS1_TS
: 0))
830 | (vcpu
->arch
.shared
->mas4
& MAS4_TSIZED(~0));
831 vcpu
->arch
.shared
->mas2
&= MAS2_EPN
;
832 vcpu
->arch
.shared
->mas2
|= vcpu
->arch
.shared
->mas4
&
834 vcpu
->arch
.shared
->mas7_3
&= MAS3_U0
| MAS3_U1
|
838 kvmppc_set_exit_type(vcpu
, EMULATED_TLBSX_EXITS
);
842 /* sesel is for tlb1 only */
843 static void write_stlbe(struct kvmppc_vcpu_e500
*vcpu_e500
,
844 struct kvm_book3e_206_tlb_entry
*gtlbe
,
845 struct kvm_book3e_206_tlb_entry
*stlbe
,
846 int stlbsel
, int sesel
)
851 stid
= kvmppc_e500_get_tlb_stid(&vcpu_e500
->vcpu
, gtlbe
);
853 stlbe
->mas1
|= MAS1_TID(stid
);
854 write_host_tlbe(vcpu_e500
, stlbsel
, sesel
, stlbe
);
858 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu
*vcpu
)
860 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
861 struct kvm_book3e_206_tlb_entry
*gtlbe
, stlbe
;
862 int tlbsel
, esel
, stlbsel
, sesel
;
865 tlbsel
= get_tlb_tlbsel(vcpu
);
866 esel
= get_tlb_esel(vcpu
, tlbsel
);
868 gtlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
870 if (get_tlb_v(gtlbe
)) {
871 inval_gtlbe_on_host(vcpu_e500
, tlbsel
, esel
);
873 kvmppc_need_recalc_tlb1map_range(vcpu_e500
, gtlbe
))
877 gtlbe
->mas1
= vcpu
->arch
.shared
->mas1
;
878 gtlbe
->mas2
= vcpu
->arch
.shared
->mas2
;
879 gtlbe
->mas7_3
= vcpu
->arch
.shared
->mas7_3
;
881 trace_kvm_booke206_gtlb_write(vcpu
->arch
.shared
->mas0
, gtlbe
->mas1
,
882 gtlbe
->mas2
, gtlbe
->mas7_3
);
886 * If a valid tlb1 entry is overwritten then recalculate the
887 * min/max TLB1 map address range otherwise no need to look
891 kvmppc_recalc_tlb1map_range(vcpu_e500
);
893 kvmppc_set_tlb1map_range(vcpu
, gtlbe
);
896 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
897 if (tlbe_is_host_safe(vcpu
, gtlbe
)) {
904 gtlbe
->mas1
&= ~MAS1_TSIZE(~0);
905 gtlbe
->mas1
|= MAS1_TSIZE(BOOK3E_PAGESZ_4K
);
908 kvmppc_e500_tlb0_map(vcpu_e500
, esel
, &stlbe
);
909 sesel
= 0; /* unused */
915 eaddr
= get_tlb_eaddr(gtlbe
);
916 raddr
= get_tlb_raddr(gtlbe
);
918 /* Create a 4KB mapping on the host.
919 * If the guest wanted a large page,
920 * only the first 4KB is mapped here and the rest
921 * are mapped on the fly. */
923 sesel
= kvmppc_e500_tlb1_map(vcpu_e500
, eaddr
,
924 raddr
>> PAGE_SHIFT
, gtlbe
, &stlbe
, esel
);
931 write_stlbe(vcpu_e500
, gtlbe
, &stlbe
, stlbsel
, sesel
);
934 kvmppc_set_exit_type(vcpu
, EMULATED_TLBWE_EXITS
);
938 static int kvmppc_e500_tlb_search(struct kvm_vcpu
*vcpu
,
939 gva_t eaddr
, unsigned int pid
, int as
)
941 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
944 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
945 esel
= kvmppc_e500_tlb_index(vcpu_e500
, eaddr
, tlbsel
, pid
, as
);
947 return index_of(tlbsel
, esel
);
953 /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
954 int kvmppc_core_vcpu_translate(struct kvm_vcpu
*vcpu
,
955 struct kvm_translation
*tr
)
962 eaddr
= tr
->linear_address
;
963 pid
= (tr
->linear_address
>> 32) & 0xff;
964 as
= (tr
->linear_address
>> 40) & 0x1;
966 index
= kvmppc_e500_tlb_search(vcpu
, eaddr
, pid
, as
);
972 tr
->physical_address
= kvmppc_mmu_xlate(vcpu
, index
, eaddr
);
973 /* XXX what does "writeable" and "usermode" even mean? */
980 int kvmppc_mmu_itlb_index(struct kvm_vcpu
*vcpu
, gva_t eaddr
)
982 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_IS
);
984 return kvmppc_e500_tlb_search(vcpu
, eaddr
, get_cur_pid(vcpu
), as
);
987 int kvmppc_mmu_dtlb_index(struct kvm_vcpu
*vcpu
, gva_t eaddr
)
989 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_DS
);
991 return kvmppc_e500_tlb_search(vcpu
, eaddr
, get_cur_pid(vcpu
), as
);
994 void kvmppc_mmu_itlb_miss(struct kvm_vcpu
*vcpu
)
996 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_IS
);
998 kvmppc_e500_deliver_tlb_miss(vcpu
, vcpu
->arch
.pc
, as
);
1001 void kvmppc_mmu_dtlb_miss(struct kvm_vcpu
*vcpu
)
1003 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_DS
);
1005 kvmppc_e500_deliver_tlb_miss(vcpu
, vcpu
->arch
.fault_dear
, as
);
1008 gpa_t
kvmppc_mmu_xlate(struct kvm_vcpu
*vcpu
, unsigned int index
,
1011 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
1012 struct kvm_book3e_206_tlb_entry
*gtlbe
;
1015 gtlbe
= get_entry(vcpu_e500
, tlbsel_of(index
), esel_of(index
));
1016 pgmask
= get_tlb_bytes(gtlbe
) - 1;
1018 return get_tlb_raddr(gtlbe
) | (eaddr
& pgmask
);
1021 void kvmppc_mmu_destroy(struct kvm_vcpu
*vcpu
)
1025 void kvmppc_mmu_map(struct kvm_vcpu
*vcpu
, u64 eaddr
, gpa_t gpaddr
,
1028 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
1029 struct tlbe_priv
*priv
;
1030 struct kvm_book3e_206_tlb_entry
*gtlbe
, stlbe
;
1031 int tlbsel
= tlbsel_of(index
);
1032 int esel
= esel_of(index
);
1035 gtlbe
= get_entry(vcpu_e500
, tlbsel
, esel
);
1040 sesel
= 0; /* unused */
1041 priv
= &vcpu_e500
->gtlb_priv
[tlbsel
][esel
];
1043 kvmppc_e500_setup_stlbe(vcpu
, gtlbe
, BOOK3E_PAGESZ_4K
,
1044 &priv
->ref
, eaddr
, &stlbe
);
1048 gfn_t gfn
= gpaddr
>> PAGE_SHIFT
;
1051 sesel
= kvmppc_e500_tlb1_map(vcpu_e500
, eaddr
, gfn
,
1052 gtlbe
, &stlbe
, esel
);
1061 write_stlbe(vcpu_e500
, gtlbe
, &stlbe
, stlbsel
, sesel
);
1064 static void free_gtlb(struct kvmppc_vcpu_e500
*vcpu_e500
)
1068 clear_tlb1_bitmap(vcpu_e500
);
1069 kfree(vcpu_e500
->g2h_tlb1_map
);
1071 clear_tlb_refs(vcpu_e500
);
1072 kfree(vcpu_e500
->gtlb_priv
[0]);
1073 kfree(vcpu_e500
->gtlb_priv
[1]);
1075 if (vcpu_e500
->shared_tlb_pages
) {
1076 vfree((void *)(round_down((uintptr_t)vcpu_e500
->gtlb_arch
,
1079 for (i
= 0; i
< vcpu_e500
->num_shared_tlb_pages
; i
++) {
1080 set_page_dirty_lock(vcpu_e500
->shared_tlb_pages
[i
]);
1081 put_page(vcpu_e500
->shared_tlb_pages
[i
]);
1084 vcpu_e500
->num_shared_tlb_pages
= 0;
1085 vcpu_e500
->shared_tlb_pages
= NULL
;
1087 kfree(vcpu_e500
->gtlb_arch
);
1090 vcpu_e500
->gtlb_arch
= NULL
;
1093 void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu
*vcpu
, struct kvm_sregs
*sregs
)
1095 sregs
->u
.e
.mas0
= vcpu
->arch
.shared
->mas0
;
1096 sregs
->u
.e
.mas1
= vcpu
->arch
.shared
->mas1
;
1097 sregs
->u
.e
.mas2
= vcpu
->arch
.shared
->mas2
;
1098 sregs
->u
.e
.mas7_3
= vcpu
->arch
.shared
->mas7_3
;
1099 sregs
->u
.e
.mas4
= vcpu
->arch
.shared
->mas4
;
1100 sregs
->u
.e
.mas6
= vcpu
->arch
.shared
->mas6
;
1102 sregs
->u
.e
.mmucfg
= vcpu
->arch
.mmucfg
;
1103 sregs
->u
.e
.tlbcfg
[0] = vcpu
->arch
.tlbcfg
[0];
1104 sregs
->u
.e
.tlbcfg
[1] = vcpu
->arch
.tlbcfg
[1];
1105 sregs
->u
.e
.tlbcfg
[2] = 0;
1106 sregs
->u
.e
.tlbcfg
[3] = 0;
1109 int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu
*vcpu
, struct kvm_sregs
*sregs
)
1111 if (sregs
->u
.e
.features
& KVM_SREGS_E_ARCH206_MMU
) {
1112 vcpu
->arch
.shared
->mas0
= sregs
->u
.e
.mas0
;
1113 vcpu
->arch
.shared
->mas1
= sregs
->u
.e
.mas1
;
1114 vcpu
->arch
.shared
->mas2
= sregs
->u
.e
.mas2
;
1115 vcpu
->arch
.shared
->mas7_3
= sregs
->u
.e
.mas7_3
;
1116 vcpu
->arch
.shared
->mas4
= sregs
->u
.e
.mas4
;
1117 vcpu
->arch
.shared
->mas6
= sregs
->u
.e
.mas6
;
1123 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu
*vcpu
,
1124 struct kvm_config_tlb
*cfg
)
1126 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
1127 struct kvm_book3e_206_tlb_params params
;
1129 struct page
**pages
;
1130 struct tlbe_priv
*privs
[2] = {};
1131 u64
*g2h_bitmap
= NULL
;
1134 int num_pages
, ret
, i
;
1136 if (cfg
->mmu_type
!= KVM_MMU_FSL_BOOKE_NOHV
)
1139 if (copy_from_user(¶ms
, (void __user
*)(uintptr_t)cfg
->params
,
1143 if (params
.tlb_sizes
[1] > 64)
1145 if (params
.tlb_ways
[1] != params
.tlb_sizes
[1])
1147 if (params
.tlb_sizes
[2] != 0 || params
.tlb_sizes
[3] != 0)
1149 if (params
.tlb_ways
[2] != 0 || params
.tlb_ways
[3] != 0)
1152 if (!is_power_of_2(params
.tlb_ways
[0]))
1155 sets
= params
.tlb_sizes
[0] >> ilog2(params
.tlb_ways
[0]);
1156 if (!is_power_of_2(sets
))
1159 array_len
= params
.tlb_sizes
[0] + params
.tlb_sizes
[1];
1160 array_len
*= sizeof(struct kvm_book3e_206_tlb_entry
);
1162 if (cfg
->array_len
< array_len
)
1165 num_pages
= DIV_ROUND_UP(cfg
->array
+ array_len
- 1, PAGE_SIZE
) -
1166 cfg
->array
/ PAGE_SIZE
;
1167 pages
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
1171 ret
= get_user_pages_fast(cfg
->array
, num_pages
, 1, pages
);
1175 if (ret
!= num_pages
) {
1181 virt
= vmap(pages
, num_pages
, VM_MAP
, PAGE_KERNEL
);
1185 privs
[0] = kzalloc(sizeof(struct tlbe_priv
) * params
.tlb_sizes
[0],
1187 privs
[1] = kzalloc(sizeof(struct tlbe_priv
) * params
.tlb_sizes
[1],
1190 if (!privs
[0] || !privs
[1])
1193 g2h_bitmap
= kzalloc(sizeof(u64
) * params
.tlb_sizes
[1],
1198 free_gtlb(vcpu_e500
);
1200 vcpu_e500
->gtlb_priv
[0] = privs
[0];
1201 vcpu_e500
->gtlb_priv
[1] = privs
[1];
1202 vcpu_e500
->g2h_tlb1_map
= g2h_bitmap
;
1204 vcpu_e500
->gtlb_arch
= (struct kvm_book3e_206_tlb_entry
*)
1205 (virt
+ (cfg
->array
& (PAGE_SIZE
- 1)));
1207 vcpu_e500
->gtlb_params
[0].entries
= params
.tlb_sizes
[0];
1208 vcpu_e500
->gtlb_params
[1].entries
= params
.tlb_sizes
[1];
1210 vcpu_e500
->gtlb_offset
[0] = 0;
1211 vcpu_e500
->gtlb_offset
[1] = params
.tlb_sizes
[0];
1213 vcpu
->arch
.mmucfg
= mfspr(SPRN_MMUCFG
) & ~MMUCFG_LPIDSIZE
;
1215 vcpu
->arch
.tlbcfg
[0] &= ~(TLBnCFG_N_ENTRY
| TLBnCFG_ASSOC
);
1216 if (params
.tlb_sizes
[0] <= 2048)
1217 vcpu
->arch
.tlbcfg
[0] |= params
.tlb_sizes
[0];
1218 vcpu
->arch
.tlbcfg
[0] |= params
.tlb_ways
[0] << TLBnCFG_ASSOC_SHIFT
;
1220 vcpu
->arch
.tlbcfg
[1] &= ~(TLBnCFG_N_ENTRY
| TLBnCFG_ASSOC
);
1221 vcpu
->arch
.tlbcfg
[1] |= params
.tlb_sizes
[1];
1222 vcpu
->arch
.tlbcfg
[1] |= params
.tlb_ways
[1] << TLBnCFG_ASSOC_SHIFT
;
1224 vcpu_e500
->shared_tlb_pages
= pages
;
1225 vcpu_e500
->num_shared_tlb_pages
= num_pages
;
1227 vcpu_e500
->gtlb_params
[0].ways
= params
.tlb_ways
[0];
1228 vcpu_e500
->gtlb_params
[0].sets
= sets
;
1230 vcpu_e500
->gtlb_params
[1].ways
= params
.tlb_sizes
[1];
1231 vcpu_e500
->gtlb_params
[1].sets
= 1;
1233 kvmppc_recalc_tlb1map_range(vcpu_e500
);
1240 for (i
= 0; i
< num_pages
; i
++)
1248 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu
*vcpu
,
1249 struct kvm_dirty_tlb
*dirty
)
1251 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
1252 kvmppc_recalc_tlb1map_range(vcpu_e500
);
1253 clear_tlb_refs(vcpu_e500
);
1257 int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500
*vcpu_e500
)
1259 struct kvm_vcpu
*vcpu
= &vcpu_e500
->vcpu
;
1260 int entry_size
= sizeof(struct kvm_book3e_206_tlb_entry
);
1261 int entries
= KVM_E500_TLB0_SIZE
+ KVM_E500_TLB1_SIZE
;
1263 host_tlb_params
[0].entries
= mfspr(SPRN_TLB0CFG
) & TLBnCFG_N_ENTRY
;
1264 host_tlb_params
[1].entries
= mfspr(SPRN_TLB1CFG
) & TLBnCFG_N_ENTRY
;
1267 * This should never happen on real e500 hardware, but is
1268 * architecturally possible -- e.g. in some weird nested
1269 * virtualization case.
1271 if (host_tlb_params
[0].entries
== 0 ||
1272 host_tlb_params
[1].entries
== 0) {
1273 pr_err("%s: need to know host tlb size\n", __func__
);
1277 host_tlb_params
[0].ways
= (mfspr(SPRN_TLB0CFG
) & TLBnCFG_ASSOC
) >>
1278 TLBnCFG_ASSOC_SHIFT
;
1279 host_tlb_params
[1].ways
= host_tlb_params
[1].entries
;
1281 if (!is_power_of_2(host_tlb_params
[0].entries
) ||
1282 !is_power_of_2(host_tlb_params
[0].ways
) ||
1283 host_tlb_params
[0].entries
< host_tlb_params
[0].ways
||
1284 host_tlb_params
[0].ways
== 0) {
1285 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1286 __func__
, host_tlb_params
[0].entries
,
1287 host_tlb_params
[0].ways
);
1291 host_tlb_params
[0].sets
=
1292 host_tlb_params
[0].entries
/ host_tlb_params
[0].ways
;
1293 host_tlb_params
[1].sets
= 1;
1295 vcpu_e500
->gtlb_params
[0].entries
= KVM_E500_TLB0_SIZE
;
1296 vcpu_e500
->gtlb_params
[1].entries
= KVM_E500_TLB1_SIZE
;
1298 vcpu_e500
->gtlb_params
[0].ways
= KVM_E500_TLB0_WAY_NUM
;
1299 vcpu_e500
->gtlb_params
[0].sets
=
1300 KVM_E500_TLB0_SIZE
/ KVM_E500_TLB0_WAY_NUM
;
1302 vcpu_e500
->gtlb_params
[1].ways
= KVM_E500_TLB1_SIZE
;
1303 vcpu_e500
->gtlb_params
[1].sets
= 1;
1305 vcpu_e500
->gtlb_arch
= kmalloc(entries
* entry_size
, GFP_KERNEL
);
1306 if (!vcpu_e500
->gtlb_arch
)
1309 vcpu_e500
->gtlb_offset
[0] = 0;
1310 vcpu_e500
->gtlb_offset
[1] = KVM_E500_TLB0_SIZE
;
1312 vcpu_e500
->tlb_refs
[0] =
1313 kzalloc(sizeof(struct tlbe_ref
) * host_tlb_params
[0].entries
,
1315 if (!vcpu_e500
->tlb_refs
[0])
1318 vcpu_e500
->tlb_refs
[1] =
1319 kzalloc(sizeof(struct tlbe_ref
) * host_tlb_params
[1].entries
,
1321 if (!vcpu_e500
->tlb_refs
[1])
1324 vcpu_e500
->gtlb_priv
[0] = kzalloc(sizeof(struct tlbe_ref
) *
1325 vcpu_e500
->gtlb_params
[0].entries
,
1327 if (!vcpu_e500
->gtlb_priv
[0])
1330 vcpu_e500
->gtlb_priv
[1] = kzalloc(sizeof(struct tlbe_ref
) *
1331 vcpu_e500
->gtlb_params
[1].entries
,
1333 if (!vcpu_e500
->gtlb_priv
[1])
1336 vcpu_e500
->g2h_tlb1_map
= kzalloc(sizeof(unsigned int) *
1337 vcpu_e500
->gtlb_params
[1].entries
,
1339 if (!vcpu_e500
->g2h_tlb1_map
)
1342 vcpu_e500
->h2g_tlb1_rmap
= kzalloc(sizeof(unsigned int) *
1343 host_tlb_params
[1].entries
,
1345 if (!vcpu_e500
->h2g_tlb1_rmap
)
1348 /* Init TLB configuration register */
1349 vcpu
->arch
.tlbcfg
[0] = mfspr(SPRN_TLB0CFG
) &
1350 ~(TLBnCFG_N_ENTRY
| TLBnCFG_ASSOC
);
1351 vcpu
->arch
.tlbcfg
[0] |= vcpu_e500
->gtlb_params
[0].entries
;
1352 vcpu
->arch
.tlbcfg
[0] |=
1353 vcpu_e500
->gtlb_params
[0].ways
<< TLBnCFG_ASSOC_SHIFT
;
1355 vcpu
->arch
.tlbcfg
[1] = mfspr(SPRN_TLB1CFG
) &
1356 ~(TLBnCFG_N_ENTRY
| TLBnCFG_ASSOC
);
1357 vcpu
->arch
.tlbcfg
[1] |= vcpu_e500
->gtlb_params
[1].entries
;
1358 vcpu
->arch
.tlbcfg
[1] |=
1359 vcpu_e500
->gtlb_params
[1].ways
<< TLBnCFG_ASSOC_SHIFT
;
1361 kvmppc_recalc_tlb1map_range(vcpu_e500
);
1365 free_gtlb(vcpu_e500
);
1366 kfree(vcpu_e500
->tlb_refs
[0]);
1367 kfree(vcpu_e500
->tlb_refs
[1]);
1371 void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500
*vcpu_e500
)
1373 free_gtlb(vcpu_e500
);
1374 kfree(vcpu_e500
->h2g_tlb1_rmap
);
1375 kfree(vcpu_e500
->tlb_refs
[0]);
1376 kfree(vcpu_e500
->tlb_refs
[1]);