KVM: do not release the error pfn
[cascardo/linux.git] / arch / powerpc / kvm / e500_tlb.c
1 /*
2  * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
3  *
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
8  *
9  * Description:
10  * This file is based on arch/powerpc/kvm/44x_tlb.c,
11  * by Hollis Blanchard <hollisb@us.ibm.com>.
12  *
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.
16  */
17
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>
32
33 #include "e500.h"
34 #include "trace.h"
35 #include "timing.h"
36
37 #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
38
39 static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
40
41 static inline unsigned int gtlb0_get_next_victim(
42                 struct kvmppc_vcpu_e500 *vcpu_e500)
43 {
44         unsigned int victim;
45
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;
49
50         return victim;
51 }
52
53 static inline unsigned int tlb1_max_shadow_size(void)
54 {
55         /* reserve one entry for magic page */
56         return host_tlb_params[1].entries - tlbcam_index - 1;
57 }
58
59 static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
60 {
61         return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
62 }
63
64 static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
65 {
66         /* Mask off reserved bits. */
67         mas3 &= MAS3_ATTRIB_MASK;
68
69 #ifndef CONFIG_KVM_BOOKE_HV
70         if (!usermode) {
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;
76         }
77         mas3 |= E500_TLB_SUPER_PERM_MASK;
78 #endif
79         return mas3;
80 }
81
82 static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
83 {
84 #ifdef CONFIG_SMP
85         return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
86 #else
87         return mas2 & MAS2_ATTRIB_MASK;
88 #endif
89 }
90
91 /*
92  * writing shadow tlb entry to host TLB
93  */
94 static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
95                                      uint32_t mas0)
96 {
97         unsigned long flags;
98
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);
107 #endif
108         asm volatile("isync; tlbwe" : : : "memory");
109
110 #ifdef CONFIG_KVM_BOOKE_HV
111         /* Must clear mas8 for other host tlbwe's */
112         mtspr(SPRN_MAS8, 0);
113         isync();
114 #endif
115         local_irq_restore(flags);
116
117         trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
118                                       stlbe->mas2, stlbe->mas7_3);
119 }
120
121 /*
122  * Acquire a mas0 with victim hint, as if we just took a TLB miss.
123  *
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.
128  */
129 static u32 get_host_mas0(unsigned long eaddr)
130 {
131         unsigned long flags;
132         u32 mas0;
133
134         local_irq_save(flags);
135         mtspr(SPRN_MAS6, 0);
136         asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
137         mas0 = mfspr(SPRN_MAS0);
138         local_irq_restore(flags);
139
140         return mas0;
141 }
142
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)
146 {
147         u32 mas0;
148
149         if (tlbsel == 0) {
150                 mas0 = get_host_mas0(stlbe->mas2);
151                 __write_host_tlbe(stlbe, mas0);
152         } else {
153                 __write_host_tlbe(stlbe,
154                                   MAS0_TLBSEL(1) |
155                                   MAS0_ESEL(to_htlb1_esel(sesel)));
156         }
157 }
158
159 #ifdef CONFIG_KVM_E500V2
160 void kvmppc_map_magic(struct kvm_vcpu *vcpu)
161 {
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;
165         unsigned int stid;
166         pfn_t pfn;
167
168         pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
169         get_page(pfn_to_page(pfn));
170
171         preempt_disable();
172         stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
173
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;
179         magic.mas8 = 0;
180
181         __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
182         preempt_enable();
183 }
184 #endif
185
186 static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
187                                 int tlbsel, int esel)
188 {
189         struct kvm_book3e_206_tlb_entry *gtlbe =
190                 get_entry(vcpu_e500, tlbsel, esel);
191
192         if (tlbsel == 1 &&
193             vcpu_e500->gtlb_priv[1][esel].ref.flags & E500_TLB_BITMAP) {
194                 u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
195                 int hw_tlb_indx;
196                 unsigned long flags;
197
198                 local_irq_save(flags);
199                 while (tmp) {
200                         hw_tlb_indx = __ilog2_u64(tmp & -tmp);
201                         mtspr(SPRN_MAS0,
202                               MAS0_TLBSEL(1) |
203                               MAS0_ESEL(to_htlb1_esel(hw_tlb_indx)));
204                         mtspr(SPRN_MAS1, 0);
205                         asm volatile("tlbwe");
206                         vcpu_e500->h2g_tlb1_rmap[hw_tlb_indx] = 0;
207                         tmp &= tmp - 1;
208                 }
209                 mb();
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);
213
214                 return;
215         }
216
217         /* Guest tlbe is backed by at most one host tlbe per shadow pid. */
218         kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
219 }
220
221 static int tlb0_set_base(gva_t addr, int sets, int ways)
222 {
223         int set_base;
224
225         set_base = (addr >> PAGE_SHIFT) & (sets - 1);
226         set_base *= ways;
227
228         return set_base;
229 }
230
231 static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
232 {
233         return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
234                              vcpu_e500->gtlb_params[0].ways);
235 }
236
237 static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel)
238 {
239         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
240         int esel = get_tlb_esel_bit(vcpu);
241
242         if (tlbsel == 0) {
243                 esel &= vcpu_e500->gtlb_params[0].ways - 1;
244                 esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2);
245         } else {
246                 esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
247         }
248
249         return esel;
250 }
251
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)
255 {
256         int size = vcpu_e500->gtlb_params[tlbsel].entries;
257         unsigned int set_base, offset;
258         int i;
259
260         if (tlbsel == 0) {
261                 set_base = gtlb0_set_base(vcpu_e500, eaddr);
262                 size = vcpu_e500->gtlb_params[0].ways;
263         } else {
264                 if (eaddr < vcpu_e500->tlb1_min_eaddr ||
265                                 eaddr > vcpu_e500->tlb1_max_eaddr)
266                         return -1;
267                 set_base = 0;
268         }
269
270         offset = vcpu_e500->gtlb_offset[tlbsel];
271
272         for (i = 0; i < size; i++) {
273                 struct kvm_book3e_206_tlb_entry *tlbe =
274                         &vcpu_e500->gtlb_arch[offset + set_base + i];
275                 unsigned int tid;
276
277                 if (eaddr < get_tlb_eaddr(tlbe))
278                         continue;
279
280                 if (eaddr > get_tlb_end(tlbe))
281                         continue;
282
283                 tid = get_tlb_tid(tlbe);
284                 if (tid && (tid != pid))
285                         continue;
286
287                 if (!get_tlb_v(tlbe))
288                         continue;
289
290                 if (get_tlb_ts(tlbe) != as && as != -1)
291                         continue;
292
293                 return set_base + i;
294         }
295
296         return -1;
297 }
298
299 static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
300                                          struct kvm_book3e_206_tlb_entry *gtlbe,
301                                          pfn_t pfn)
302 {
303         ref->pfn = pfn;
304         ref->flags = E500_TLB_VALID;
305
306         if (tlbe_is_writable(gtlbe))
307                 ref->flags |= E500_TLB_DIRTY;
308 }
309
310 static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
311 {
312         if (ref->flags & E500_TLB_VALID) {
313                 if (ref->flags & E500_TLB_DIRTY)
314                         kvm_release_pfn_dirty(ref->pfn);
315                 else
316                         kvm_release_pfn_clean(ref->pfn);
317
318                 ref->flags = 0;
319         }
320 }
321
322 static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
323 {
324         if (vcpu_e500->g2h_tlb1_map)
325                 memset(vcpu_e500->g2h_tlb1_map,
326                        sizeof(u64) * vcpu_e500->gtlb_params[1].entries, 0);
327         if (vcpu_e500->h2g_tlb1_rmap)
328                 memset(vcpu_e500->h2g_tlb1_rmap,
329                        sizeof(unsigned int) * host_tlb_params[1].entries, 0);
330 }
331
332 static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
333 {
334         int tlbsel = 0;
335         int i;
336
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);
341         }
342 }
343
344 static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
345 {
346         int stlbsel = 1;
347         int i;
348
349         kvmppc_e500_tlbil_all(vcpu_e500);
350
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);
355         }
356
357         clear_tlb_privs(vcpu_e500);
358 }
359
360 static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
361                 unsigned int eaddr, int as)
362 {
363         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
364         unsigned int victim, tsized;
365         int tlbsel;
366
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;
371
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);
383 }
384
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)
391 {
392         pfn_t pfn = ref->pfn;
393         u32 pr = vcpu->arch.shared->msr & MSR_PR;
394
395         BUG_ON(!(ref->flags & E500_TLB_VALID));
396
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);
403
404 #ifdef CONFIG_KVM_BOOKE_HV
405         stlbe->mas8 = MAS8_TGS | vcpu->kvm->arch.lpid;
406 #endif
407 }
408
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)
413 {
414         struct kvm_memory_slot *slot;
415         unsigned long pfn, hva;
416         int pfnmap = 0;
417         int tsize = BOOK3E_PAGESZ_4K;
418
419         /*
420          * Translate guest physical to true physical, acquiring
421          * a page reference if it is normal, non-reserved memory.
422          *
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.
426          */
427         slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
428         hva = gfn_to_hva_memslot(slot, gfn);
429
430         if (tlbsel == 1) {
431                 struct vm_area_struct *vma;
432                 down_read(&current->mm->mmap_sem);
433
434                 vma = find_vma(current->mm, hva);
435                 if (vma && hva >= vma->vm_start &&
436                     (vma->vm_flags & VM_PFNMAP)) {
437                         /*
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.
442                          */
443
444                         unsigned long start, end;
445                         unsigned long slot_start, slot_end;
446
447                         pfnmap = 1;
448
449                         start = vma->vm_pgoff;
450                         end = start +
451                               ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
452
453                         pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
454
455                         slot_start = pfn - (gfn - slot->base_gfn);
456                         slot_end = slot_start + slot->npages;
457
458                         if (start < slot_start)
459                                 start = slot_start;
460                         if (end > slot_end)
461                                 end = slot_end;
462
463                         tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
464                                 MAS1_TSIZE_SHIFT;
465
466                         /*
467                          * e500 doesn't implement the lowest tsize bit,
468                          * or 1K pages.
469                          */
470                         tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
471
472                         /*
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
476                          * aligned.
477                          */
478
479                         for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
480                                 unsigned long gfn_start, gfn_end, tsize_pages;
481                                 tsize_pages = 1 << (tsize - 2);
482
483                                 gfn_start = gfn & ~(tsize_pages - 1);
484                                 gfn_end = gfn_start + tsize_pages;
485
486                                 if (gfn_start + pfn - gfn < start)
487                                         continue;
488                                 if (gfn_end + pfn - gfn > end)
489                                         continue;
490                                 if ((gfn & (tsize_pages - 1)) !=
491                                     (pfn & (tsize_pages - 1)))
492                                         continue;
493
494                                 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
495                                 pfn &= ~(tsize_pages - 1);
496                                 break;
497                         }
498                 } else if (vma && hva >= vma->vm_start &&
499                            (vma->vm_flags & VM_HUGETLB)) {
500                         unsigned long psize = vma_kernel_pagesize(vma);
501
502                         tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
503                                 MAS1_TSIZE_SHIFT;
504
505                         /*
506                          * Take the largest page size that satisfies both host
507                          * and guest mapping
508                          */
509                         tsize = min(__ilog2(psize) - 10, tsize);
510
511                         /*
512                          * e500 doesn't implement the lowest tsize bit,
513                          * or 1K pages.
514                          */
515                         tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
516                 }
517
518                 up_read(&current->mm->mmap_sem);
519         }
520
521         if (likely(!pfnmap)) {
522                 unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
523                 pfn = gfn_to_pfn_memslot(slot, gfn);
524                 if (is_error_pfn(pfn)) {
525                         printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
526                                         (long)gfn);
527                         return;
528                 }
529
530                 /* Align guest and physical address to page map boundaries */
531                 pfn &= ~(tsize_pages - 1);
532                 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
533         }
534
535         /* Drop old ref and setup new one. */
536         kvmppc_e500_ref_release(ref);
537         kvmppc_e500_ref_setup(ref, gtlbe, pfn);
538
539         kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
540                                 ref, gvaddr, stlbe);
541 }
542
543 /* XXX only map the one-one case, for now use TLB0 */
544 static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
545                                  int esel,
546                                  struct kvm_book3e_206_tlb_entry *stlbe)
547 {
548         struct kvm_book3e_206_tlb_entry *gtlbe;
549         struct tlbe_ref *ref;
550
551         gtlbe = get_entry(vcpu_e500, 0, esel);
552         ref = &vcpu_e500->gtlb_priv[0][esel].ref;
553
554         kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
555                         get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
556                         gtlbe, 0, stlbe, ref);
557 }
558
559 /* Caller must ensure that the specified guest TLB entry is safe to insert into
560  * the shadow TLB. */
561 /* XXX for both one-one and one-to-many , for now use TLB1 */
562 static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
563                 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
564                 struct kvm_book3e_206_tlb_entry *stlbe, int esel)
565 {
566         struct tlbe_ref *ref;
567         unsigned int victim;
568
569         victim = vcpu_e500->host_tlb1_nv++;
570
571         if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
572                 vcpu_e500->host_tlb1_nv = 0;
573
574         ref = &vcpu_e500->tlb_refs[1][victim];
575         kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
576
577         vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << victim;
578         vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
579         if (vcpu_e500->h2g_tlb1_rmap[victim]) {
580                 unsigned int idx = vcpu_e500->h2g_tlb1_rmap[victim];
581                 vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << victim);
582         }
583         vcpu_e500->h2g_tlb1_rmap[victim] = esel;
584
585         return victim;
586 }
587
588 static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500)
589 {
590         int size = vcpu_e500->gtlb_params[1].entries;
591         unsigned int offset;
592         gva_t eaddr;
593         int i;
594
595         vcpu_e500->tlb1_min_eaddr = ~0UL;
596         vcpu_e500->tlb1_max_eaddr = 0;
597         offset = vcpu_e500->gtlb_offset[1];
598
599         for (i = 0; i < size; i++) {
600                 struct kvm_book3e_206_tlb_entry *tlbe =
601                         &vcpu_e500->gtlb_arch[offset + i];
602
603                 if (!get_tlb_v(tlbe))
604                         continue;
605
606                 eaddr = get_tlb_eaddr(tlbe);
607                 vcpu_e500->tlb1_min_eaddr =
608                                 min(vcpu_e500->tlb1_min_eaddr, eaddr);
609
610                 eaddr = get_tlb_end(tlbe);
611                 vcpu_e500->tlb1_max_eaddr =
612                                 max(vcpu_e500->tlb1_max_eaddr, eaddr);
613         }
614 }
615
616 static int kvmppc_need_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500,
617                                 struct kvm_book3e_206_tlb_entry *gtlbe)
618 {
619         unsigned long start, end, size;
620
621         size = get_tlb_bytes(gtlbe);
622         start = get_tlb_eaddr(gtlbe) & ~(size - 1);
623         end = start + size - 1;
624
625         return vcpu_e500->tlb1_min_eaddr == start ||
626                         vcpu_e500->tlb1_max_eaddr == end;
627 }
628
629 /* This function is supposed to be called for a adding a new valid tlb entry */
630 static void kvmppc_set_tlb1map_range(struct kvm_vcpu *vcpu,
631                                 struct kvm_book3e_206_tlb_entry *gtlbe)
632 {
633         unsigned long start, end, size;
634         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
635
636         if (!get_tlb_v(gtlbe))
637                 return;
638
639         size = get_tlb_bytes(gtlbe);
640         start = get_tlb_eaddr(gtlbe) & ~(size - 1);
641         end = start + size - 1;
642
643         vcpu_e500->tlb1_min_eaddr = min(vcpu_e500->tlb1_min_eaddr, start);
644         vcpu_e500->tlb1_max_eaddr = max(vcpu_e500->tlb1_max_eaddr, end);
645 }
646
647 static inline int kvmppc_e500_gtlbe_invalidate(
648                                 struct kvmppc_vcpu_e500 *vcpu_e500,
649                                 int tlbsel, int esel)
650 {
651         struct kvm_book3e_206_tlb_entry *gtlbe =
652                 get_entry(vcpu_e500, tlbsel, esel);
653
654         if (unlikely(get_tlb_iprot(gtlbe)))
655                 return -1;
656
657         if (tlbsel == 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
658                 kvmppc_recalc_tlb1map_range(vcpu_e500);
659
660         gtlbe->mas1 = 0;
661
662         return 0;
663 }
664
665 int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
666 {
667         int esel;
668
669         if (value & MMUCSR0_TLB0FI)
670                 for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
671                         kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
672         if (value & MMUCSR0_TLB1FI)
673                 for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
674                         kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
675
676         /* Invalidate all vcpu id mappings */
677         kvmppc_e500_tlbil_all(vcpu_e500);
678
679         return EMULATE_DONE;
680 }
681
682 int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
683 {
684         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
685         unsigned int ia;
686         int esel, tlbsel;
687         gva_t ea;
688
689         ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
690
691         ia = (ea >> 2) & 0x1;
692
693         /* since we only have two TLBs, only lower bit is used. */
694         tlbsel = (ea >> 3) & 0x1;
695
696         if (ia) {
697                 /* invalidate all entries */
698                 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
699                      esel++)
700                         kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
701         } else {
702                 ea &= 0xfffff000;
703                 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
704                                 get_cur_pid(vcpu), -1);
705                 if (esel >= 0)
706                         kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
707         }
708
709         /* Invalidate all vcpu id mappings */
710         kvmppc_e500_tlbil_all(vcpu_e500);
711
712         return EMULATE_DONE;
713 }
714
715 static void tlbilx_all(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
716                        int pid, int rt)
717 {
718         struct kvm_book3e_206_tlb_entry *tlbe;
719         int tid, esel;
720
721         /* invalidate all entries */
722         for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; esel++) {
723                 tlbe = get_entry(vcpu_e500, tlbsel, esel);
724                 tid = get_tlb_tid(tlbe);
725                 if (rt == 0 || tid == pid) {
726                         inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
727                         kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
728                 }
729         }
730 }
731
732 static void tlbilx_one(struct kvmppc_vcpu_e500 *vcpu_e500, int pid,
733                        int ra, int rb)
734 {
735         int tlbsel, esel;
736         gva_t ea;
737
738         ea = kvmppc_get_gpr(&vcpu_e500->vcpu, rb);
739         if (ra)
740                 ea += kvmppc_get_gpr(&vcpu_e500->vcpu, ra);
741
742         for (tlbsel = 0; tlbsel < 2; tlbsel++) {
743                 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1);
744                 if (esel >= 0) {
745                         inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
746                         kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
747                         break;
748                 }
749         }
750 }
751
752 int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int rt, int ra, int rb)
753 {
754         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
755         int pid = get_cur_spid(vcpu);
756
757         if (rt == 0 || rt == 1) {
758                 tlbilx_all(vcpu_e500, 0, pid, rt);
759                 tlbilx_all(vcpu_e500, 1, pid, rt);
760         } else if (rt == 3) {
761                 tlbilx_one(vcpu_e500, pid, ra, rb);
762         }
763
764         return EMULATE_DONE;
765 }
766
767 int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
768 {
769         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
770         int tlbsel, esel;
771         struct kvm_book3e_206_tlb_entry *gtlbe;
772
773         tlbsel = get_tlb_tlbsel(vcpu);
774         esel = get_tlb_esel(vcpu, tlbsel);
775
776         gtlbe = get_entry(vcpu_e500, tlbsel, esel);
777         vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
778         vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
779         vcpu->arch.shared->mas1 = gtlbe->mas1;
780         vcpu->arch.shared->mas2 = gtlbe->mas2;
781         vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
782
783         return EMULATE_DONE;
784 }
785
786 int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
787 {
788         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
789         int as = !!get_cur_sas(vcpu);
790         unsigned int pid = get_cur_spid(vcpu);
791         int esel, tlbsel;
792         struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
793         gva_t ea;
794
795         ea = kvmppc_get_gpr(vcpu, rb);
796
797         for (tlbsel = 0; tlbsel < 2; tlbsel++) {
798                 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
799                 if (esel >= 0) {
800                         gtlbe = get_entry(vcpu_e500, tlbsel, esel);
801                         break;
802                 }
803         }
804
805         if (gtlbe) {
806                 esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
807
808                 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
809                         | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
810                 vcpu->arch.shared->mas1 = gtlbe->mas1;
811                 vcpu->arch.shared->mas2 = gtlbe->mas2;
812                 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
813         } else {
814                 int victim;
815
816                 /* since we only have two TLBs, only lower bit is used. */
817                 tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
818                 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
819
820                 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
821                         | MAS0_ESEL(victim)
822                         | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
823                 vcpu->arch.shared->mas1 =
824                           (vcpu->arch.shared->mas6 & MAS6_SPID0)
825                         | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
826                         | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
827                 vcpu->arch.shared->mas2 &= MAS2_EPN;
828                 vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
829                                            MAS2_ATTRIB_MASK;
830                 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
831                                              MAS3_U2 | MAS3_U3;
832         }
833
834         kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
835         return EMULATE_DONE;
836 }
837
838 /* sesel is for tlb1 only */
839 static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
840                         struct kvm_book3e_206_tlb_entry *gtlbe,
841                         struct kvm_book3e_206_tlb_entry *stlbe,
842                         int stlbsel, int sesel)
843 {
844         int stid;
845
846         preempt_disable();
847         stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
848
849         stlbe->mas1 |= MAS1_TID(stid);
850         write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
851         preempt_enable();
852 }
853
854 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
855 {
856         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
857         struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
858         int tlbsel, esel, stlbsel, sesel;
859         int recal = 0;
860
861         tlbsel = get_tlb_tlbsel(vcpu);
862         esel = get_tlb_esel(vcpu, tlbsel);
863
864         gtlbe = get_entry(vcpu_e500, tlbsel, esel);
865
866         if (get_tlb_v(gtlbe)) {
867                 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
868                 if ((tlbsel == 1) &&
869                         kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
870                         recal = 1;
871         }
872
873         gtlbe->mas1 = vcpu->arch.shared->mas1;
874         gtlbe->mas2 = vcpu->arch.shared->mas2;
875         gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
876
877         trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
878                                       gtlbe->mas2, gtlbe->mas7_3);
879
880         if (tlbsel == 1) {
881                 /*
882                  * If a valid tlb1 entry is overwritten then recalculate the
883                  * min/max TLB1 map address range otherwise no need to look
884                  * in tlb1 array.
885                  */
886                 if (recal)
887                         kvmppc_recalc_tlb1map_range(vcpu_e500);
888                 else
889                         kvmppc_set_tlb1map_range(vcpu, gtlbe);
890         }
891
892         /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
893         if (tlbe_is_host_safe(vcpu, gtlbe)) {
894                 u64 eaddr;
895                 u64 raddr;
896
897                 switch (tlbsel) {
898                 case 0:
899                         /* TLB0 */
900                         gtlbe->mas1 &= ~MAS1_TSIZE(~0);
901                         gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
902
903                         stlbsel = 0;
904                         kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
905                         sesel = 0; /* unused */
906
907                         break;
908
909                 case 1:
910                         /* TLB1 */
911                         eaddr = get_tlb_eaddr(gtlbe);
912                         raddr = get_tlb_raddr(gtlbe);
913
914                         /* Create a 4KB mapping on the host.
915                          * If the guest wanted a large page,
916                          * only the first 4KB is mapped here and the rest
917                          * are mapped on the fly. */
918                         stlbsel = 1;
919                         sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
920                                     raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel);
921                         break;
922
923                 default:
924                         BUG();
925                 }
926
927                 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
928         }
929
930         kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
931         return EMULATE_DONE;
932 }
933
934 static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
935                                   gva_t eaddr, unsigned int pid, int as)
936 {
937         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
938         int esel, tlbsel;
939
940         for (tlbsel = 0; tlbsel < 2; tlbsel++) {
941                 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
942                 if (esel >= 0)
943                         return index_of(tlbsel, esel);
944         }
945
946         return -1;
947 }
948
949 /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
950 int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
951                                struct kvm_translation *tr)
952 {
953         int index;
954         gva_t eaddr;
955         u8 pid;
956         u8 as;
957
958         eaddr = tr->linear_address;
959         pid = (tr->linear_address >> 32) & 0xff;
960         as = (tr->linear_address >> 40) & 0x1;
961
962         index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
963         if (index < 0) {
964                 tr->valid = 0;
965                 return 0;
966         }
967
968         tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
969         /* XXX what does "writeable" and "usermode" even mean? */
970         tr->valid = 1;
971
972         return 0;
973 }
974
975
976 int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
977 {
978         unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
979
980         return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
981 }
982
983 int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
984 {
985         unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
986
987         return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
988 }
989
990 void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
991 {
992         unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
993
994         kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
995 }
996
997 void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
998 {
999         unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
1000
1001         kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
1002 }
1003
1004 gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
1005                         gva_t eaddr)
1006 {
1007         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1008         struct kvm_book3e_206_tlb_entry *gtlbe;
1009         u64 pgmask;
1010
1011         gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
1012         pgmask = get_tlb_bytes(gtlbe) - 1;
1013
1014         return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
1015 }
1016
1017 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1018 {
1019 }
1020
1021 void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
1022                         unsigned int index)
1023 {
1024         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1025         struct tlbe_priv *priv;
1026         struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
1027         int tlbsel = tlbsel_of(index);
1028         int esel = esel_of(index);
1029         int stlbsel, sesel;
1030
1031         gtlbe = get_entry(vcpu_e500, tlbsel, esel);
1032
1033         switch (tlbsel) {
1034         case 0:
1035                 stlbsel = 0;
1036                 sesel = 0; /* unused */
1037                 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
1038
1039                 kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
1040                                         &priv->ref, eaddr, &stlbe);
1041                 break;
1042
1043         case 1: {
1044                 gfn_t gfn = gpaddr >> PAGE_SHIFT;
1045
1046                 stlbsel = 1;
1047                 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
1048                                              gtlbe, &stlbe, esel);
1049                 break;
1050         }
1051
1052         default:
1053                 BUG();
1054                 break;
1055         }
1056
1057         write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
1058 }
1059
1060 static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
1061 {
1062         int i;
1063
1064         clear_tlb1_bitmap(vcpu_e500);
1065         kfree(vcpu_e500->g2h_tlb1_map);
1066
1067         clear_tlb_refs(vcpu_e500);
1068         kfree(vcpu_e500->gtlb_priv[0]);
1069         kfree(vcpu_e500->gtlb_priv[1]);
1070
1071         if (vcpu_e500->shared_tlb_pages) {
1072                 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
1073                                           PAGE_SIZE)));
1074
1075                 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
1076                         set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
1077                         put_page(vcpu_e500->shared_tlb_pages[i]);
1078                 }
1079
1080                 vcpu_e500->num_shared_tlb_pages = 0;
1081                 vcpu_e500->shared_tlb_pages = NULL;
1082         } else {
1083                 kfree(vcpu_e500->gtlb_arch);
1084         }
1085
1086         vcpu_e500->gtlb_arch = NULL;
1087 }
1088
1089 void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1090 {
1091         sregs->u.e.mas0 = vcpu->arch.shared->mas0;
1092         sregs->u.e.mas1 = vcpu->arch.shared->mas1;
1093         sregs->u.e.mas2 = vcpu->arch.shared->mas2;
1094         sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
1095         sregs->u.e.mas4 = vcpu->arch.shared->mas4;
1096         sregs->u.e.mas6 = vcpu->arch.shared->mas6;
1097
1098         sregs->u.e.mmucfg = vcpu->arch.mmucfg;
1099         sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
1100         sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
1101         sregs->u.e.tlbcfg[2] = 0;
1102         sregs->u.e.tlbcfg[3] = 0;
1103 }
1104
1105 int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1106 {
1107         if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
1108                 vcpu->arch.shared->mas0 = sregs->u.e.mas0;
1109                 vcpu->arch.shared->mas1 = sregs->u.e.mas1;
1110                 vcpu->arch.shared->mas2 = sregs->u.e.mas2;
1111                 vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
1112                 vcpu->arch.shared->mas4 = sregs->u.e.mas4;
1113                 vcpu->arch.shared->mas6 = sregs->u.e.mas6;
1114         }
1115
1116         return 0;
1117 }
1118
1119 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
1120                               struct kvm_config_tlb *cfg)
1121 {
1122         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1123         struct kvm_book3e_206_tlb_params params;
1124         char *virt;
1125         struct page **pages;
1126         struct tlbe_priv *privs[2] = {};
1127         u64 *g2h_bitmap = NULL;
1128         size_t array_len;
1129         u32 sets;
1130         int num_pages, ret, i;
1131
1132         if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
1133                 return -EINVAL;
1134
1135         if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
1136                            sizeof(params)))
1137                 return -EFAULT;
1138
1139         if (params.tlb_sizes[1] > 64)
1140                 return -EINVAL;
1141         if (params.tlb_ways[1] != params.tlb_sizes[1])
1142                 return -EINVAL;
1143         if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
1144                 return -EINVAL;
1145         if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
1146                 return -EINVAL;
1147
1148         if (!is_power_of_2(params.tlb_ways[0]))
1149                 return -EINVAL;
1150
1151         sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1152         if (!is_power_of_2(sets))
1153                 return -EINVAL;
1154
1155         array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1156         array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1157
1158         if (cfg->array_len < array_len)
1159                 return -EINVAL;
1160
1161         num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1162                     cfg->array / PAGE_SIZE;
1163         pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1164         if (!pages)
1165                 return -ENOMEM;
1166
1167         ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1168         if (ret < 0)
1169                 goto err_pages;
1170
1171         if (ret != num_pages) {
1172                 num_pages = ret;
1173                 ret = -EFAULT;
1174                 goto err_put_page;
1175         }
1176
1177         virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1178         if (!virt)
1179                 goto err_put_page;
1180
1181         privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1182                            GFP_KERNEL);
1183         privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1184                            GFP_KERNEL);
1185
1186         if (!privs[0] || !privs[1])
1187                 goto err_put_page;
1188
1189         g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
1190                              GFP_KERNEL);
1191         if (!g2h_bitmap)
1192                 goto err_put_page;
1193
1194         free_gtlb(vcpu_e500);
1195
1196         vcpu_e500->gtlb_priv[0] = privs[0];
1197         vcpu_e500->gtlb_priv[1] = privs[1];
1198         vcpu_e500->g2h_tlb1_map = g2h_bitmap;
1199
1200         vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1201                 (virt + (cfg->array & (PAGE_SIZE - 1)));
1202
1203         vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1204         vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1205
1206         vcpu_e500->gtlb_offset[0] = 0;
1207         vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1208
1209         vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
1210
1211         vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1212         if (params.tlb_sizes[0] <= 2048)
1213                 vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
1214         vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
1215
1216         vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1217         vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
1218         vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
1219
1220         vcpu_e500->shared_tlb_pages = pages;
1221         vcpu_e500->num_shared_tlb_pages = num_pages;
1222
1223         vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1224         vcpu_e500->gtlb_params[0].sets = sets;
1225
1226         vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1227         vcpu_e500->gtlb_params[1].sets = 1;
1228
1229         kvmppc_recalc_tlb1map_range(vcpu_e500);
1230         return 0;
1231
1232 err_put_page:
1233         kfree(privs[0]);
1234         kfree(privs[1]);
1235
1236         for (i = 0; i < num_pages; i++)
1237                 put_page(pages[i]);
1238
1239 err_pages:
1240         kfree(pages);
1241         return ret;
1242 }
1243
1244 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1245                              struct kvm_dirty_tlb *dirty)
1246 {
1247         struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1248         kvmppc_recalc_tlb1map_range(vcpu_e500);
1249         clear_tlb_refs(vcpu_e500);
1250         return 0;
1251 }
1252
1253 int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1254 {
1255         struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
1256         int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1257         int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1258
1259         host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1260         host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1261
1262         /*
1263          * This should never happen on real e500 hardware, but is
1264          * architecturally possible -- e.g. in some weird nested
1265          * virtualization case.
1266          */
1267         if (host_tlb_params[0].entries == 0 ||
1268             host_tlb_params[1].entries == 0) {
1269                 pr_err("%s: need to know host tlb size\n", __func__);
1270                 return -ENODEV;
1271         }
1272
1273         host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1274                                   TLBnCFG_ASSOC_SHIFT;
1275         host_tlb_params[1].ways = host_tlb_params[1].entries;
1276
1277         if (!is_power_of_2(host_tlb_params[0].entries) ||
1278             !is_power_of_2(host_tlb_params[0].ways) ||
1279             host_tlb_params[0].entries < host_tlb_params[0].ways ||
1280             host_tlb_params[0].ways == 0) {
1281                 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1282                        __func__, host_tlb_params[0].entries,
1283                        host_tlb_params[0].ways);
1284                 return -ENODEV;
1285         }
1286
1287         host_tlb_params[0].sets =
1288                 host_tlb_params[0].entries / host_tlb_params[0].ways;
1289         host_tlb_params[1].sets = 1;
1290
1291         vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1292         vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
1293
1294         vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1295         vcpu_e500->gtlb_params[0].sets =
1296                 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1297
1298         vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1299         vcpu_e500->gtlb_params[1].sets = 1;
1300
1301         vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1302         if (!vcpu_e500->gtlb_arch)
1303                 return -ENOMEM;
1304
1305         vcpu_e500->gtlb_offset[0] = 0;
1306         vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
1307
1308         vcpu_e500->tlb_refs[0] =
1309                 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1310                         GFP_KERNEL);
1311         if (!vcpu_e500->tlb_refs[0])
1312                 goto err;
1313
1314         vcpu_e500->tlb_refs[1] =
1315                 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1316                         GFP_KERNEL);
1317         if (!vcpu_e500->tlb_refs[1])
1318                 goto err;
1319
1320         vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1321                                           vcpu_e500->gtlb_params[0].entries,
1322                                           GFP_KERNEL);
1323         if (!vcpu_e500->gtlb_priv[0])
1324                 goto err;
1325
1326         vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1327                                           vcpu_e500->gtlb_params[1].entries,
1328                                           GFP_KERNEL);
1329         if (!vcpu_e500->gtlb_priv[1])
1330                 goto err;
1331
1332         vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(unsigned int) *
1333                                           vcpu_e500->gtlb_params[1].entries,
1334                                           GFP_KERNEL);
1335         if (!vcpu_e500->g2h_tlb1_map)
1336                 goto err;
1337
1338         vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
1339                                            host_tlb_params[1].entries,
1340                                            GFP_KERNEL);
1341         if (!vcpu_e500->h2g_tlb1_rmap)
1342                 goto err;
1343
1344         /* Init TLB configuration register */
1345         vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
1346                              ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1347         vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
1348         vcpu->arch.tlbcfg[0] |=
1349                 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
1350
1351         vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
1352                              ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1353         vcpu->arch.tlbcfg[1] |= vcpu_e500->gtlb_params[1].entries;
1354         vcpu->arch.tlbcfg[1] |=
1355                 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
1356
1357         kvmppc_recalc_tlb1map_range(vcpu_e500);
1358         return 0;
1359
1360 err:
1361         free_gtlb(vcpu_e500);
1362         kfree(vcpu_e500->tlb_refs[0]);
1363         kfree(vcpu_e500->tlb_refs[1]);
1364         return -1;
1365 }
1366
1367 void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1368 {
1369         free_gtlb(vcpu_e500);
1370         kfree(vcpu_e500->h2g_tlb1_rmap);
1371         kfree(vcpu_e500->tlb_refs[0]);
1372         kfree(vcpu_e500->tlb_refs[1]);
1373 }