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