2 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
4 * Author: Yu Liu, yu.liu@freescale.com
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/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kvm.h>
20 #include <linux/kvm_host.h>
21 #include <linux/highmem.h>
22 #include <linux/log2.h>
23 #include <linux/uaccess.h>
24 #include <linux/sched.h>
25 #include <linux/rwsem.h>
26 #include <linux/vmalloc.h>
27 #include <asm/kvm_ppc.h>
28 #include <asm/kvm_e500.h>
30 #include "../mm/mmu_decl.h"
35 #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
45 * This table provide mappings from:
46 * (guestAS,guestTID,guestPR) --> ID of physical cpu
51 * Each vcpu keeps one vcpu_id_table.
53 struct vcpu_id_table {
54 struct id id[2][NUM_TIDS][2];
58 * This table provide reversed mappings of vcpu_id_table:
59 * ID --> address of vcpu_id_table item.
60 * Each physical core has one pcpu_id_table.
62 struct pcpu_id_table {
63 struct id *entry[NUM_TIDS];
66 static DEFINE_PER_CPU(struct pcpu_id_table, pcpu_sids);
68 /* This variable keeps last used shadow ID on local core.
69 * The valid range of shadow ID is [1..255] */
70 static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid);
72 static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
74 static struct kvm_book3e_206_tlb_entry *get_entry(
75 struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel, int entry)
77 int offset = vcpu_e500->gtlb_offset[tlbsel];
78 return &vcpu_e500->gtlb_arch[offset + entry];
82 * Allocate a free shadow id and setup a valid sid mapping in given entry.
83 * A mapping is only valid when vcpu_id_table and pcpu_id_table are match.
85 * The caller must have preemption disabled, and keep it that way until
86 * it has finished with the returned shadow id (either written into the
87 * TLB or arch.shadow_pid, or discarded).
89 static inline int local_sid_setup_one(struct id *entry)
94 sid = ++(__get_cpu_var(pcpu_last_used_sid));
96 __get_cpu_var(pcpu_sids).entry[sid] = entry;
98 entry->pentry = &__get_cpu_var(pcpu_sids).entry[sid];
103 * If sid == NUM_TIDS, we've run out of sids. We return -1, and
104 * the caller will invalidate everything and start over.
106 * sid > NUM_TIDS indicates a race, which we disable preemption to
109 WARN_ON(sid > NUM_TIDS);
115 * Check if given entry contain a valid shadow id mapping.
116 * An ID mapping is considered valid only if
117 * both vcpu and pcpu know this mapping.
119 * The caller must have preemption disabled, and keep it that way until
120 * it has finished with the returned shadow id (either written into the
121 * TLB or arch.shadow_pid, or discarded).
123 static inline int local_sid_lookup(struct id *entry)
125 if (entry && entry->val != 0 &&
126 __get_cpu_var(pcpu_sids).entry[entry->val] == entry &&
127 entry->pentry == &__get_cpu_var(pcpu_sids).entry[entry->val])
132 /* Invalidate all id mappings on local core -- call with preempt disabled */
133 static inline void local_sid_destroy_all(void)
135 __get_cpu_var(pcpu_last_used_sid) = 0;
136 memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids)));
139 static void *kvmppc_e500_id_table_alloc(struct kvmppc_vcpu_e500 *vcpu_e500)
141 vcpu_e500->idt = kzalloc(sizeof(struct vcpu_id_table), GFP_KERNEL);
142 return vcpu_e500->idt;
145 static void kvmppc_e500_id_table_free(struct kvmppc_vcpu_e500 *vcpu_e500)
147 kfree(vcpu_e500->idt);
150 /* Invalidate all mappings on vcpu */
151 static void kvmppc_e500_id_table_reset_all(struct kvmppc_vcpu_e500 *vcpu_e500)
153 memset(vcpu_e500->idt, 0, sizeof(struct vcpu_id_table));
155 /* Update shadow pid when mappings are changed */
156 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
159 /* Invalidate one ID mapping on vcpu */
160 static inline void kvmppc_e500_id_table_reset_one(
161 struct kvmppc_vcpu_e500 *vcpu_e500,
162 int as, int pid, int pr)
164 struct vcpu_id_table *idt = vcpu_e500->idt;
167 BUG_ON(pid >= NUM_TIDS);
170 idt->id[as][pid][pr].val = 0;
171 idt->id[as][pid][pr].pentry = NULL;
173 /* Update shadow pid when mappings are changed */
174 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
178 * Map guest (vcpu,AS,ID,PR) to physical core shadow id.
179 * This function first lookup if a valid mapping exists,
180 * if not, then creates a new one.
182 * The caller must have preemption disabled, and keep it that way until
183 * it has finished with the returned shadow id (either written into the
184 * TLB or arch.shadow_pid, or discarded).
186 static unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500,
187 unsigned int as, unsigned int gid,
188 unsigned int pr, int avoid_recursion)
190 struct vcpu_id_table *idt = vcpu_e500->idt;
194 BUG_ON(gid >= NUM_TIDS);
197 sid = local_sid_lookup(&idt->id[as][gid][pr]);
201 sid = local_sid_setup_one(&idt->id[as][gid][pr]);
204 local_sid_destroy_all();
207 /* Update shadow pid when mappings are changed */
208 if (!avoid_recursion)
209 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
215 /* Map guest pid to shadow.
216 * We use PID to keep shadow of current guest non-zero PID,
217 * and use PID1 to keep shadow of guest zero PID.
218 * So that guest tlbe with TID=0 can be accessed at any time */
219 void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *vcpu_e500)
222 vcpu_e500->vcpu.arch.shadow_pid = kvmppc_e500_get_sid(vcpu_e500,
223 get_cur_as(&vcpu_e500->vcpu),
224 get_cur_pid(&vcpu_e500->vcpu),
225 get_cur_pr(&vcpu_e500->vcpu), 1);
226 vcpu_e500->vcpu.arch.shadow_pid1 = kvmppc_e500_get_sid(vcpu_e500,
227 get_cur_as(&vcpu_e500->vcpu), 0,
228 get_cur_pr(&vcpu_e500->vcpu), 1);
232 static inline unsigned int gtlb0_get_next_victim(
233 struct kvmppc_vcpu_e500 *vcpu_e500)
237 victim = vcpu_e500->gtlb_nv[0]++;
238 if (unlikely(vcpu_e500->gtlb_nv[0] >= vcpu_e500->gtlb_params[0].ways))
239 vcpu_e500->gtlb_nv[0] = 0;
244 static inline unsigned int tlb1_max_shadow_size(void)
246 /* reserve one entry for magic page */
247 return host_tlb_params[1].entries - tlbcam_index - 1;
250 static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
252 return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
255 static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
257 /* Mask off reserved bits. */
258 mas3 &= MAS3_ATTRIB_MASK;
261 /* Guest is in supervisor mode,
262 * so we need to translate guest
263 * supervisor permissions into user permissions. */
264 mas3 &= ~E500_TLB_USER_PERM_MASK;
265 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
268 return mas3 | E500_TLB_SUPER_PERM_MASK;
271 static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
274 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
276 return mas2 & MAS2_ATTRIB_MASK;
281 * writing shadow tlb entry to host TLB
283 static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
288 local_irq_save(flags);
289 mtspr(SPRN_MAS0, mas0);
290 mtspr(SPRN_MAS1, stlbe->mas1);
291 mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
292 mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
293 mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
294 asm volatile("isync; tlbwe" : : : "memory");
295 local_irq_restore(flags);
298 /* esel is index into set, not whole array */
299 static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
300 int tlbsel, int esel, struct kvm_book3e_206_tlb_entry *stlbe)
303 int way = esel & (vcpu_e500->gtlb_params[0].ways - 1);
304 __write_host_tlbe(stlbe, MAS0_TLBSEL(0) | MAS0_ESEL(way));
306 __write_host_tlbe(stlbe,
308 MAS0_ESEL(to_htlb1_esel(esel)));
310 trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
311 (u32)stlbe->mas7_3, (u32)(stlbe->mas7_3 >> 32));
314 void kvmppc_map_magic(struct kvm_vcpu *vcpu)
316 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
317 struct kvm_book3e_206_tlb_entry magic;
318 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
322 pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
323 get_page(pfn_to_page(pfn));
326 stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
328 magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
329 MAS1_TSIZE(BOOK3E_PAGESZ_4K);
330 magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
331 magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
332 MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
334 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
338 void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
340 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
342 /* Shadow PID may be expired on local core */
343 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
346 void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
350 static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
351 int tlbsel, int esel)
353 struct kvm_book3e_206_tlb_entry *gtlbe =
354 get_entry(vcpu_e500, tlbsel, esel);
355 struct vcpu_id_table *idt = vcpu_e500->idt;
356 unsigned int pr, tid, ts, pid;
360 ts = get_tlb_ts(gtlbe);
361 tid = get_tlb_tid(gtlbe);
365 /* One guest ID may be mapped to two shadow IDs */
366 for (pr = 0; pr < 2; pr++) {
368 * The shadow PID can have a valid mapping on at most one
369 * host CPU. In the common case, it will be valid on this
370 * CPU, in which case (for TLB0) we do a local invalidation
371 * of the specific address.
373 * If the shadow PID is not valid on the current host CPU, or
374 * if we're invalidating a TLB1 entry, we invalidate the
378 (pid = local_sid_lookup(&idt->id[ts][tid][pr])) <= 0) {
379 kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr);
384 * The guest is invalidating a TLB0 entry which is in a PID
385 * that has a valid shadow mapping on this host CPU. We
386 * search host TLB0 to invalidate it's shadow TLB entry,
387 * similar to __tlbil_va except that we need to look in AS1.
389 val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS;
390 eaddr = get_tlb_eaddr(gtlbe);
392 local_irq_save(flags);
394 mtspr(SPRN_MAS6, val);
395 asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr));
396 val = mfspr(SPRN_MAS1);
397 if (val & MAS1_VALID) {
398 mtspr(SPRN_MAS1, val & ~MAS1_VALID);
399 asm volatile("tlbwe");
402 local_irq_restore(flags);
408 static int tlb0_set_base(gva_t addr, int sets, int ways)
412 set_base = (addr >> PAGE_SHIFT) & (sets - 1);
418 static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
420 return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
421 vcpu_e500->gtlb_params[0].ways);
424 static int htlb0_set_base(gva_t addr)
426 return tlb0_set_base(addr, host_tlb_params[0].sets,
427 host_tlb_params[0].ways);
430 static unsigned int get_tlb_esel(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel)
432 unsigned int esel = get_tlb_esel_bit(vcpu_e500);
435 esel &= vcpu_e500->gtlb_params[0].ways - 1;
436 esel += gtlb0_set_base(vcpu_e500, vcpu_e500->mas2);
438 esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
444 /* Search the guest TLB for a matching entry. */
445 static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
446 gva_t eaddr, int tlbsel, unsigned int pid, int as)
448 int size = vcpu_e500->gtlb_params[tlbsel].entries;
449 unsigned int set_base, offset;
453 set_base = gtlb0_set_base(vcpu_e500, eaddr);
454 size = vcpu_e500->gtlb_params[0].ways;
459 offset = vcpu_e500->gtlb_offset[tlbsel];
461 for (i = 0; i < size; i++) {
462 struct kvm_book3e_206_tlb_entry *tlbe =
463 &vcpu_e500->gtlb_arch[offset + set_base + i];
466 if (eaddr < get_tlb_eaddr(tlbe))
469 if (eaddr > get_tlb_end(tlbe))
472 tid = get_tlb_tid(tlbe);
473 if (tid && (tid != pid))
476 if (!get_tlb_v(tlbe))
479 if (get_tlb_ts(tlbe) != as && as != -1)
488 static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
489 struct kvm_book3e_206_tlb_entry *gtlbe,
493 ref->flags = E500_TLB_VALID;
495 if (tlbe_is_writable(gtlbe))
496 ref->flags |= E500_TLB_DIRTY;
499 static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
501 if (ref->flags & E500_TLB_VALID) {
502 if (ref->flags & E500_TLB_DIRTY)
503 kvm_release_pfn_dirty(ref->pfn);
505 kvm_release_pfn_clean(ref->pfn);
511 static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
516 for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
517 struct tlbe_ref *ref =
518 &vcpu_e500->gtlb_priv[tlbsel][i].ref;
519 kvmppc_e500_ref_release(ref);
523 static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
528 kvmppc_e500_id_table_reset_all(vcpu_e500);
530 for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
531 struct tlbe_ref *ref =
532 &vcpu_e500->tlb_refs[stlbsel][i];
533 kvmppc_e500_ref_release(ref);
536 clear_tlb_privs(vcpu_e500);
539 static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
540 unsigned int eaddr, int as)
542 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
543 unsigned int victim, pidsel, tsized;
546 /* since we only have two TLBs, only lower bit is used. */
547 tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
548 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
549 pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
550 tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
552 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
553 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
554 vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
555 | MAS1_TID(vcpu_e500->pid[pidsel])
556 | MAS1_TSIZE(tsized);
557 vcpu_e500->mas2 = (eaddr & MAS2_EPN)
558 | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
559 vcpu_e500->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
560 vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
561 | (get_cur_pid(vcpu) << 16)
562 | (as ? MAS6_SAS : 0);
565 /* TID must be supplied by the caller */
566 static inline void kvmppc_e500_setup_stlbe(
567 struct kvmppc_vcpu_e500 *vcpu_e500,
568 struct kvm_book3e_206_tlb_entry *gtlbe,
569 int tsize, struct tlbe_ref *ref, u64 gvaddr,
570 struct kvm_book3e_206_tlb_entry *stlbe)
572 pfn_t pfn = ref->pfn;
574 BUG_ON(!(ref->flags & E500_TLB_VALID));
576 /* Force TS=1 IPROT=0 for all guest mappings. */
577 stlbe->mas1 = MAS1_TSIZE(tsize) | MAS1_TS | MAS1_VALID;
578 stlbe->mas2 = (gvaddr & MAS2_EPN)
579 | e500_shadow_mas2_attrib(gtlbe->mas2,
580 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
581 stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT)
582 | e500_shadow_mas3_attrib(gtlbe->mas7_3,
583 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
586 /* sesel is an index into the entire array, not just the set */
587 static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
588 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
589 int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe,
590 struct tlbe_ref *ref)
592 struct kvm_memory_slot *slot;
593 unsigned long pfn, hva;
595 int tsize = BOOK3E_PAGESZ_4K;
598 * Translate guest physical to true physical, acquiring
599 * a page reference if it is normal, non-reserved memory.
601 * gfn_to_memslot() must succeed because otherwise we wouldn't
602 * have gotten this far. Eventually we should just pass the slot
603 * pointer through from the first lookup.
605 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
606 hva = gfn_to_hva_memslot(slot, gfn);
609 struct vm_area_struct *vma;
610 down_read(¤t->mm->mmap_sem);
612 vma = find_vma(current->mm, hva);
613 if (vma && hva >= vma->vm_start &&
614 (vma->vm_flags & VM_PFNMAP)) {
616 * This VMA is a physically contiguous region (e.g.
617 * /dev/mem) that bypasses normal Linux page
618 * management. Find the overlap between the
619 * vma and the memslot.
622 unsigned long start, end;
623 unsigned long slot_start, slot_end;
627 start = vma->vm_pgoff;
629 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
631 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
633 slot_start = pfn - (gfn - slot->base_gfn);
634 slot_end = slot_start + slot->npages;
636 if (start < slot_start)
641 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
645 * e500 doesn't implement the lowest tsize bit,
648 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
651 * Now find the largest tsize (up to what the guest
652 * requested) that will cover gfn, stay within the
653 * range, and for which gfn and pfn are mutually
657 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
658 unsigned long gfn_start, gfn_end, tsize_pages;
659 tsize_pages = 1 << (tsize - 2);
661 gfn_start = gfn & ~(tsize_pages - 1);
662 gfn_end = gfn_start + tsize_pages;
664 if (gfn_start + pfn - gfn < start)
666 if (gfn_end + pfn - gfn > end)
668 if ((gfn & (tsize_pages - 1)) !=
669 (pfn & (tsize_pages - 1)))
672 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
673 pfn &= ~(tsize_pages - 1);
678 up_read(¤t->mm->mmap_sem);
681 if (likely(!pfnmap)) {
682 pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
683 if (is_error_pfn(pfn)) {
684 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
686 kvm_release_pfn_clean(pfn);
691 /* Drop old ref and setup new one. */
692 kvmppc_e500_ref_release(ref);
693 kvmppc_e500_ref_setup(ref, gtlbe, pfn);
695 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, ref, gvaddr, stlbe);
698 /* XXX only map the one-one case, for now use TLB0 */
699 static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
701 struct kvm_book3e_206_tlb_entry *stlbe)
703 struct kvm_book3e_206_tlb_entry *gtlbe;
704 struct tlbe_ref *ref;
705 int sesel = esel & (host_tlb_params[0].ways - 1);
709 gtlbe = get_entry(vcpu_e500, 0, esel);
710 ref = &vcpu_e500->gtlb_priv[0][esel].ref;
712 ea = get_tlb_eaddr(gtlbe);
713 sesel_base = htlb0_set_base(ea);
715 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
716 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
717 gtlbe, 0, sesel_base + sesel, stlbe, ref);
722 /* Caller must ensure that the specified guest TLB entry is safe to insert into
724 /* XXX for both one-one and one-to-many , for now use TLB1 */
725 static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
726 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
727 struct kvm_book3e_206_tlb_entry *stlbe)
729 struct tlbe_ref *ref;
732 victim = vcpu_e500->host_tlb1_nv++;
734 if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
735 vcpu_e500->host_tlb1_nv = 0;
737 ref = &vcpu_e500->tlb_refs[1][victim];
738 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1,
744 void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
746 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
748 /* Recalc shadow pid since MSR changes */
749 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
752 static inline int kvmppc_e500_gtlbe_invalidate(
753 struct kvmppc_vcpu_e500 *vcpu_e500,
754 int tlbsel, int esel)
756 struct kvm_book3e_206_tlb_entry *gtlbe =
757 get_entry(vcpu_e500, tlbsel, esel);
759 if (unlikely(get_tlb_iprot(gtlbe)))
767 int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
771 if (value & MMUCSR0_TLB0FI)
772 for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
773 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
774 if (value & MMUCSR0_TLB1FI)
775 for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
776 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
778 /* Invalidate all vcpu id mappings */
779 kvmppc_e500_id_table_reset_all(vcpu_e500);
784 int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
786 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
791 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
793 ia = (ea >> 2) & 0x1;
795 /* since we only have two TLBs, only lower bit is used. */
796 tlbsel = (ea >> 3) & 0x1;
799 /* invalidate all entries */
800 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
802 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
805 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
806 get_cur_pid(vcpu), -1);
808 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
811 /* Invalidate all vcpu id mappings */
812 kvmppc_e500_id_table_reset_all(vcpu_e500);
817 int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
819 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
821 struct kvm_book3e_206_tlb_entry *gtlbe;
823 tlbsel = get_tlb_tlbsel(vcpu_e500);
824 esel = get_tlb_esel(vcpu_e500, tlbsel);
826 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
827 vcpu_e500->mas0 &= ~MAS0_NV(~0);
828 vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
829 vcpu_e500->mas1 = gtlbe->mas1;
830 vcpu_e500->mas2 = gtlbe->mas2;
831 vcpu_e500->mas7_3 = gtlbe->mas7_3;
836 int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
838 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
839 int as = !!get_cur_sas(vcpu_e500);
840 unsigned int pid = get_cur_spid(vcpu_e500);
842 struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
845 ea = kvmppc_get_gpr(vcpu, rb);
847 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
848 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
850 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
856 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
857 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
858 vcpu_e500->mas1 = gtlbe->mas1;
859 vcpu_e500->mas2 = gtlbe->mas2;
860 vcpu_e500->mas7_3 = gtlbe->mas7_3;
864 /* since we only have two TLBs, only lower bit is used. */
865 tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
866 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
868 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
869 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
870 vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
871 | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
872 | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
873 vcpu_e500->mas2 &= MAS2_EPN;
874 vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
875 vcpu_e500->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
878 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
882 /* sesel is index into the set, not the whole array */
883 static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
884 struct kvm_book3e_206_tlb_entry *gtlbe,
885 struct kvm_book3e_206_tlb_entry *stlbe,
886 int stlbsel, int sesel)
891 stid = kvmppc_e500_get_sid(vcpu_e500, get_tlb_ts(gtlbe),
893 get_cur_pr(&vcpu_e500->vcpu), 0);
895 stlbe->mas1 |= MAS1_TID(stid);
896 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
900 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
902 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
903 struct kvm_book3e_206_tlb_entry *gtlbe;
906 tlbsel = get_tlb_tlbsel(vcpu_e500);
907 esel = get_tlb_esel(vcpu_e500, tlbsel);
909 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
911 if (get_tlb_v(gtlbe))
912 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
914 gtlbe->mas1 = vcpu_e500->mas1;
915 gtlbe->mas2 = vcpu_e500->mas2;
916 gtlbe->mas7_3 = vcpu_e500->mas7_3;
918 trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
919 (u32)gtlbe->mas7_3, (u32)(gtlbe->mas7_3 >> 32));
921 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
922 if (tlbe_is_host_safe(vcpu, gtlbe)) {
923 struct kvm_book3e_206_tlb_entry stlbe;
931 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
932 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
935 sesel = kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
941 eaddr = get_tlb_eaddr(gtlbe);
942 raddr = get_tlb_raddr(gtlbe);
944 /* Create a 4KB mapping on the host.
945 * If the guest wanted a large page,
946 * only the first 4KB is mapped here and the rest
947 * are mapped on the fly. */
949 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
950 raddr >> PAGE_SHIFT, gtlbe, &stlbe);
957 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
960 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
964 int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
966 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
968 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
971 int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
973 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
975 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
978 void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
980 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
982 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
985 void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
987 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
989 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
992 gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
995 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
996 struct kvm_book3e_206_tlb_entry *gtlbe;
999 gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
1000 pgmask = get_tlb_bytes(gtlbe) - 1;
1002 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
1005 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1009 void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
1012 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1013 struct tlbe_priv *priv;
1014 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
1015 int tlbsel = tlbsel_of(index);
1016 int esel = esel_of(index);
1019 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
1024 sesel = esel & (host_tlb_params[0].ways - 1);
1025 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
1027 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K,
1028 &priv->ref, eaddr, &stlbe);
1032 gfn_t gfn = gpaddr >> PAGE_SHIFT;
1035 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
1045 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
1048 int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
1049 gva_t eaddr, unsigned int pid, int as)
1051 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1054 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
1055 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
1057 return index_of(tlbsel, esel);
1063 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
1065 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1067 if (vcpu->arch.pid != pid) {
1068 vcpu_e500->pid[0] = vcpu->arch.pid = pid;
1069 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
1073 void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
1075 struct kvm_book3e_206_tlb_entry *tlbe;
1077 /* Insert large initial mapping for guest. */
1078 tlbe = get_entry(vcpu_e500, 1, 0);
1079 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
1081 tlbe->mas7_3 = E500_TLB_SUPER_PERM_MASK;
1083 /* 4K map for serial output. Used by kernel wrapper. */
1084 tlbe = get_entry(vcpu_e500, 1, 1);
1085 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
1086 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
1087 tlbe->mas7_3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
1090 static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
1094 clear_tlb_refs(vcpu_e500);
1095 kfree(vcpu_e500->gtlb_priv[0]);
1096 kfree(vcpu_e500->gtlb_priv[1]);
1098 if (vcpu_e500->shared_tlb_pages) {
1099 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
1102 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
1103 set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
1104 put_page(vcpu_e500->shared_tlb_pages[i]);
1107 vcpu_e500->num_shared_tlb_pages = 0;
1108 vcpu_e500->shared_tlb_pages = NULL;
1110 kfree(vcpu_e500->gtlb_arch);
1113 vcpu_e500->gtlb_arch = NULL;
1116 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
1117 struct kvm_config_tlb *cfg)
1119 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1120 struct kvm_book3e_206_tlb_params params;
1122 struct page **pages;
1123 struct tlbe_priv *privs[2] = {};
1126 int num_pages, ret, i;
1128 if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
1131 if (copy_from_user(¶ms, (void __user *)(uintptr_t)cfg->params,
1135 if (params.tlb_sizes[1] > 64)
1137 if (params.tlb_ways[1] != params.tlb_sizes[1])
1139 if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
1141 if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
1144 if (!is_power_of_2(params.tlb_ways[0]))
1147 sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1148 if (!is_power_of_2(sets))
1151 array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1152 array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1154 if (cfg->array_len < array_len)
1157 num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1158 cfg->array / PAGE_SIZE;
1159 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1163 ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1167 if (ret != num_pages) {
1173 virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1177 privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1179 privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1182 if (!privs[0] || !privs[1])
1185 free_gtlb(vcpu_e500);
1187 vcpu_e500->gtlb_priv[0] = privs[0];
1188 vcpu_e500->gtlb_priv[1] = privs[1];
1190 vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1191 (virt + (cfg->array & (PAGE_SIZE - 1)));
1193 vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1194 vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1196 vcpu_e500->gtlb_offset[0] = 0;
1197 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1199 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
1200 if (params.tlb_sizes[0] <= 2048)
1201 vcpu_e500->tlb0cfg |= params.tlb_sizes[0];
1203 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
1204 vcpu_e500->tlb1cfg |= params.tlb_sizes[1];
1206 vcpu_e500->shared_tlb_pages = pages;
1207 vcpu_e500->num_shared_tlb_pages = num_pages;
1209 vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1210 vcpu_e500->gtlb_params[0].sets = sets;
1212 vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1213 vcpu_e500->gtlb_params[1].sets = 1;
1221 for (i = 0; i < num_pages; i++)
1229 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1230 struct kvm_dirty_tlb *dirty)
1232 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1234 clear_tlb_refs(vcpu_e500);
1238 int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1240 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1241 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1243 host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1244 host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1247 * This should never happen on real e500 hardware, but is
1248 * architecturally possible -- e.g. in some weird nested
1249 * virtualization case.
1251 if (host_tlb_params[0].entries == 0 ||
1252 host_tlb_params[1].entries == 0) {
1253 pr_err("%s: need to know host tlb size\n", __func__);
1257 host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1258 TLBnCFG_ASSOC_SHIFT;
1259 host_tlb_params[1].ways = host_tlb_params[1].entries;
1261 if (!is_power_of_2(host_tlb_params[0].entries) ||
1262 !is_power_of_2(host_tlb_params[0].ways) ||
1263 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1264 host_tlb_params[0].ways == 0) {
1265 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1266 __func__, host_tlb_params[0].entries,
1267 host_tlb_params[0].ways);
1271 host_tlb_params[0].sets =
1272 host_tlb_params[0].entries / host_tlb_params[0].ways;
1273 host_tlb_params[1].sets = 1;
1275 vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1276 vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
1278 vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1279 vcpu_e500->gtlb_params[0].sets =
1280 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1282 vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1283 vcpu_e500->gtlb_params[1].sets = 1;
1285 vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1286 if (!vcpu_e500->gtlb_arch)
1289 vcpu_e500->gtlb_offset[0] = 0;
1290 vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
1292 vcpu_e500->tlb_refs[0] =
1293 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1295 if (!vcpu_e500->tlb_refs[0])
1298 vcpu_e500->tlb_refs[1] =
1299 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1301 if (!vcpu_e500->tlb_refs[1])
1304 vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1305 vcpu_e500->gtlb_params[0].entries,
1307 if (!vcpu_e500->gtlb_priv[0])
1310 vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1311 vcpu_e500->gtlb_params[1].entries,
1313 if (!vcpu_e500->gtlb_priv[1])
1316 if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
1319 /* Init TLB configuration register */
1320 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
1321 vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[0].entries;
1322 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
1323 vcpu_e500->tlb1cfg |= vcpu_e500->gtlb_params[1].entries;
1328 free_gtlb(vcpu_e500);
1329 kfree(vcpu_e500->tlb_refs[0]);
1330 kfree(vcpu_e500->tlb_refs[1]);
1334 void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1336 free_gtlb(vcpu_e500);
1337 kvmppc_e500_id_table_free(vcpu_e500);
1339 kfree(vcpu_e500->tlb_refs[0]);
1340 kfree(vcpu_e500->tlb_refs[1]);