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