18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
27 #include <linux/sched.h>
37 #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
41 static inline unsigned int gtlb0_get_next_victim(
46 victim = vcpu_e500->
gtlb_nv[0]++;
53 static inline unsigned int tlb1_max_shadow_size(
void)
64 static inline u32 e500_shadow_mas3_attrib(
u32 mas3,
int usermode)
69 #ifndef CONFIG_KVM_BOOKE_HV
82 static inline u32 e500_shadow_mas2_attrib(
u32 mas2,
int usermode)
100 mtspr(SPRN_MAS0, mas0);
102 mtspr(SPRN_MAS2, (
unsigned long)stlbe->
mas2);
105 #ifdef CONFIG_KVM_BOOKE_HV
108 asm volatile(
"isync; tlbwe" : : :
"memory");
110 #ifdef CONFIG_KVM_BOOKE_HV
117 trace_kvm_booke206_stlb_write(mas0, stlbe->
mas8, stlbe->
mas1,
129 static u32 get_host_mas0(
unsigned long eaddr)
136 asm volatile(
"tlbsx 0, %0" : :
"b" (eaddr & ~CONFIG_PAGE_OFFSET));
137 mas0 =
mfspr(SPRN_MAS0);
150 mas0 = get_host_mas0(stlbe->
mas2);
151 __write_host_tlbe(stlbe, mas0);
153 __write_host_tlbe(stlbe,
159 #ifdef CONFIG_KVM_E500V2
187 int tlbsel,
int esel)
200 hw_tlb_indx = __ilog2_u64(tmp & -tmp);
205 asm volatile(
"tlbwe");
211 vcpu_e500->
gtlb_priv[1][esel].ref.flags &= ~E500_TLB_BITMAP;
221 static int tlb0_set_base(
gva_t addr,
int sets,
int ways)
233 return tlb0_set_base(addr, vcpu_e500->
gtlb_params[0].sets,
237 static unsigned int get_tlb_esel(
struct kvm_vcpu *vcpu,
int tlbsel)
240 int esel = get_tlb_esel_bit(vcpu);
244 esel += gtlb0_set_base(vcpu_e500, vcpu->
arch.shared->mas2);
246 esel &= vcpu_e500->
gtlb_params[tlbsel].entries - 1;
254 gva_t eaddr,
int tlbsel,
unsigned int pid,
int as)
257 unsigned int set_base,
offset;
261 set_base = gtlb0_set_base(vcpu_e500, eaddr);
272 for (i = 0; i <
size; 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,
306 if (tlbe_is_writable(gtlbe))
310 static inline void kvmppc_e500_ref_release(
struct tlbe_ref *ref)
329 sizeof(
unsigned int) * host_tlb_params[1].entries);
337 for (i = 0; i < vcpu_e500->
gtlb_params[tlbsel].entries; i++) {
340 kvmppc_e500_ref_release(ref);
351 for (i = 0; i < host_tlb_params[stlbsel].entries; 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)
364 unsigned int victim, tsized;
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;
381 | (get_cur_pid(vcpu) << 16)
386 static inline void kvmppc_e500_setup_stlbe(
393 u32 pr = vcpu->
arch.shared->msr & MSR_PR;
400 e500_shadow_mas2_attrib(gtlbe->
mas2, pr);
402 e500_shadow_mas3_attrib(gtlbe->
mas7_3, pr);
404 #ifdef CONFIG_KVM_BOOKE_HV
409 static inline void kvmppc_e500_shadow_map(
struct kvmppc_vcpu_e500 *vcpu_e500,
415 unsigned long pfn, hva;
445 unsigned long slot_start, slot_end;
455 slot_start = pfn - (gfn - slot->
base_gfn);
456 slot_end = slot_start + slot->
npages;
458 if (
start < slot_start)
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)))
495 pfn &= ~(tsize_pages - 1);
498 }
else if (vma && hva >= vma->
vm_start &&
509 tsize =
min(__ilog2(psize) - 10, tsize);
515 tsize =
max(BOOK3E_PAGESZ_4K, tsize & ~1);
522 unsigned long tsize_pages = 1 << (tsize + 10 -
PAGE_SHIFT);
524 if (is_error_pfn(pfn)) {
531 pfn &= ~(tsize_pages - 1);
536 kvmppc_e500_ref_release(ref);
537 kvmppc_e500_ref_setup(ref, gtlbe, pfn);
539 kvmppc_e500_setup_stlbe(&vcpu_e500->
vcpu, gtlbe, tsize,
543 kvmppc_mmu_flush_icache(pfn);
555 ref = &vcpu_e500->
gtlb_priv[0][esel].ref;
557 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
559 gtlbe, 0, stlbe, ref);
577 ref = &vcpu_e500->
tlb_refs[1][victim];
578 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
602 for (i = 0; i <
size; i++) {
606 if (!get_tlb_v(tlbe))
609 eaddr = get_tlb_eaddr(tlbe);
613 eaddr = get_tlb_end(tlbe);
619 static int kvmppc_need_recalc_tlb1map_range(
struct kvmppc_vcpu_e500 *vcpu_e500,
624 size = get_tlb_bytes(gtlbe);
625 start = get_tlb_eaddr(gtlbe) & ~(size - 1);
626 end = start + size - 1;
633 static void kvmppc_set_tlb1map_range(
struct kvm_vcpu *vcpu,
639 if (!get_tlb_v(gtlbe))
642 size = get_tlb_bytes(gtlbe);
643 start = get_tlb_eaddr(gtlbe) & ~(size - 1);
644 end = start + size - 1;
650 static inline int kvmppc_e500_gtlbe_invalidate(
652 int tlbsel,
int esel)
660 if (tlbsel == 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
661 kvmppc_recalc_tlb1map_range(vcpu_e500);
673 for (esel = 0; esel < vcpu_e500->
gtlb_params[0].entries; esel++)
674 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
676 for (esel = 0; esel < vcpu_e500->
gtlb_params[1].entries; esel++)
677 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
692 ea = ((
ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
694 ia = (ea >> 2) & 0x1;
697 tlbsel = (ea >> 3) & 0x1;
701 for (esel = 0; esel < vcpu_e500->
gtlb_params[tlbsel].entries;
703 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
706 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
707 get_cur_pid(vcpu), -1);
709 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
725 for (esel = 0; esel < vcpu_e500->
gtlb_params[tlbsel].entries; esel++) {
726 tlbe =
get_entry(vcpu_e500, tlbsel, esel);
727 tid = get_tlb_tid(tlbe);
728 if (rt == 0 || tid == pid) {
729 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
730 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
741 ea = kvmppc_get_gpr(&vcpu_e500->
vcpu, rb);
743 ea += kvmppc_get_gpr(&vcpu_e500->
vcpu, ra);
745 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
746 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1);
748 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
749 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
758 int pid = get_cur_spid(vcpu);
760 if (rt == 0 || rt == 1) {
761 tlbilx_all(vcpu_e500, 0, pid, rt);
762 tlbilx_all(vcpu_e500, 1, pid, rt);
763 }
else if (rt == 3) {
764 tlbilx_one(vcpu_e500, pid, ra, rb);
776 tlbsel = get_tlb_tlbsel(vcpu);
777 esel = get_tlb_esel(vcpu, tlbsel);
779 gtlbe =
get_entry(vcpu_e500, tlbsel, esel);
782 vcpu->
arch.shared->mas1 = gtlbe->
mas1;
783 vcpu->
arch.shared->mas2 = gtlbe->
mas2;
792 int as = !!get_cur_sas(vcpu);
793 unsigned int pid = get_cur_spid(vcpu);
798 ea = kvmppc_get_gpr(vcpu, rb);
800 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
801 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
803 gtlbe =
get_entry(vcpu_e500, tlbsel, esel);
813 vcpu->
arch.shared->mas1 = gtlbe->
mas1;
814 vcpu->
arch.shared->mas2 = gtlbe->
mas2;
820 tlbsel = vcpu->
arch.shared->mas4 >> 28 & 0x1;
821 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
826 vcpu->
arch.shared->mas1 =
831 vcpu->
arch.shared->mas2 |= vcpu->
arch.shared->mas4 &
845 int stlbsel,
int sesel)
853 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
861 int tlbsel, esel, stlbsel, sesel;
864 tlbsel = get_tlb_tlbsel(vcpu);
865 esel = get_tlb_esel(vcpu, tlbsel);
867 gtlbe =
get_entry(vcpu_e500, tlbsel, esel);
869 if (get_tlb_v(gtlbe)) {
870 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
872 kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
876 gtlbe->
mas1 = vcpu->
arch.shared->mas1;
877 gtlbe->
mas2 = vcpu->
arch.shared->mas2;
880 trace_kvm_booke206_gtlb_write(vcpu->
arch.shared->mas0, gtlbe->
mas1,
890 kvmppc_recalc_tlb1map_range(vcpu_e500);
892 kvmppc_set_tlb1map_range(vcpu, gtlbe);
896 if (tlbe_is_host_safe(vcpu, gtlbe)) {
907 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
914 eaddr = get_tlb_eaddr(gtlbe);
915 raddr = get_tlb_raddr(gtlbe);
922 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
930 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
937 static int kvmppc_e500_tlb_search(
struct kvm_vcpu *vcpu,
938 gva_t eaddr,
unsigned int pid,
int as)
943 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
944 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
965 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
981 unsigned int as = !!(vcpu->
arch.shared->msr & MSR_IS);
983 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
988 unsigned int as = !!(vcpu->
arch.shared->msr & MSR_DS);
990 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
995 unsigned int as = !!(vcpu->
arch.shared->msr & MSR_IS);
997 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->
arch.pc, as);
1002 unsigned int as = !!(vcpu->
arch.shared->msr & MSR_DS);
1004 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->
arch.fault_dear, as);
1015 pgmask = get_tlb_bytes(gtlbe) - 1;
1017 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
1034 gtlbe =
get_entry(vcpu_e500, tlbsel, esel);
1040 priv = &vcpu_e500->
gtlb_priv[tlbsel][esel];
1042 kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
1043 &priv->
ref, eaddr, &stlbe);
1050 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
1051 gtlbe, &stlbe, esel);
1060 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
1067 clear_tlb1_bitmap(vcpu_e500);
1070 clear_tlb_refs(vcpu_e500);
1094 sregs->
u.
e.mas0 = vcpu->
arch.shared->mas0;
1095 sregs->
u.
e.mas1 = vcpu->
arch.shared->mas1;
1096 sregs->
u.
e.mas2 = vcpu->
arch.shared->mas2;
1097 sregs->
u.
e.mas7_3 = vcpu->
arch.shared->mas7_3;
1098 sregs->
u.
e.mas4 = vcpu->
arch.shared->mas4;
1099 sregs->
u.
e.mas6 = vcpu->
arch.shared->mas6;
1101 sregs->
u.
e.mmucfg = vcpu->
arch.mmucfg;
1102 sregs->
u.
e.tlbcfg[0] = vcpu->
arch.tlbcfg[0];
1103 sregs->
u.
e.tlbcfg[1] = vcpu->
arch.tlbcfg[1];
1104 sregs->
u.
e.tlbcfg[2] = 0;
1105 sregs->
u.
e.tlbcfg[3] = 0;
1111 vcpu->
arch.shared->mas0 = sregs->
u.
e.mas0;
1112 vcpu->
arch.shared->mas1 = sregs->
u.
e.mas1;
1113 vcpu->
arch.shared->mas2 = sregs->
u.
e.mas2;
1114 vcpu->
arch.shared->mas7_3 = sregs->
u.
e.mas7_3;
1115 vcpu->
arch.shared->mas4 = sregs->
u.
e.mas4;
1116 vcpu->
arch.shared->mas6 = sregs->
u.
e.mas6;
1174 if (ret != num_pages) {
1189 if (!privs[0] || !privs[1])
1192 g2h_bitmap = kzalloc(
sizeof(
u64) * params.
tlb_sizes[1],
1197 free_gtlb(vcpu_e500);
1232 kvmppc_recalc_tlb1map_range(vcpu_e500);
1251 kvmppc_recalc_tlb1map_range(vcpu_e500);
1252 clear_tlb_refs(vcpu_e500);
1270 if (host_tlb_params[0].entries == 0 ||
1271 host_tlb_params[1].entries == 0) {
1272 pr_err(
"%s: need to know host tlb size\n", __func__);
1278 host_tlb_params[1].ways = host_tlb_params[1].entries;
1282 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1283 host_tlb_params[0].ways == 0) {
1284 pr_err(
"%s: bad tlb0 host config: %u entries %u ways\n",
1285 __func__, host_tlb_params[0].entries,
1286 host_tlb_params[0].ways);
1290 host_tlb_params[0].sets =
1291 host_tlb_params[0].entries / host_tlb_params[0].ways;
1292 host_tlb_params[1].sets = 1;
1312 kzalloc(
sizeof(
struct tlbe_ref) * host_tlb_params[0].entries,
1318 kzalloc(
sizeof(
struct tlbe_ref) * host_tlb_params[1].entries,
1335 vcpu_e500->
g2h_tlb1_map = kzalloc(
sizeof(
unsigned int) *
1342 host_tlb_params[1].entries,
1348 vcpu->
arch.tlbcfg[0] =
mfspr(SPRN_TLB0CFG) &
1351 vcpu->
arch.tlbcfg[0] |=
1354 vcpu->
arch.tlbcfg[1] =
mfspr(SPRN_TLB1CFG) &
1357 vcpu->
arch.tlbcfg[1] |=
1360 kvmppc_recalc_tlb1map_range(vcpu_e500);
1364 free_gtlb(vcpu_e500);
1372 free_gtlb(vcpu_e500);