KVM: PPC: e500: Fix TLBnCFG in KVM_CONFIG_TLB
[linux-2.6-block.git] / arch / powerpc / kvm / e500_tlb.c
CommitLineData
bc8080cb 1/*
49ea0695 2 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
bc8080cb
HB
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
0164c0f0 15#include <linux/kernel.h>
bc8080cb 16#include <linux/types.h>
5a0e3ad6 17#include <linux/slab.h>
bc8080cb
HB
18#include <linux/string.h>
19#include <linux/kvm.h>
20#include <linux/kvm_host.h>
21#include <linux/highmem.h>
dc83b8bc
SW
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>
95325e6b 27#include <linux/hugetlb.h>
bc8080cb
HB
28#include <asm/kvm_ppc.h>
29#include <asm/kvm_e500.h>
30
9aa4dd5e 31#include "../mm/mmu_decl.h"
bc8080cb 32#include "e500_tlb.h"
46f43c6e 33#include "trace.h"
49ea0695 34#include "timing.h"
bc8080cb 35
0164c0f0 36#define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
bc8080cb 37
dd9ebf1f
LY
38struct 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 */
54struct 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 */
63struct pcpu_id_table {
64 struct id *entry[NUM_TIDS];
65};
66
67static 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] */
71static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid);
72
0164c0f0 73static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
bc8080cb 74
dc83b8bc
SW
75static 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
dd9ebf1f
LY
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 */
90static 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 */
124static 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
90b92a6f 133/* Invalidate all id mappings on local core -- call with preempt disabled */
dd9ebf1f
LY
134static inline void local_sid_destroy_all(void)
135{
dd9ebf1f
LY
136 __get_cpu_var(pcpu_last_used_sid) = 0;
137 memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids)));
dd9ebf1f
LY
138}
139
140static 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
146static 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 */
152static 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 */
161static 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 */
187static 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 */
220void 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
0164c0f0 233static inline unsigned int gtlb0_get_next_victim(
bc8080cb
HB
234 struct kvmppc_vcpu_e500 *vcpu_e500)
235{
236 unsigned int victim;
237
08b7fa92 238 victim = vcpu_e500->gtlb_nv[0]++;
dc83b8bc 239 if (unlikely(vcpu_e500->gtlb_nv[0] >= vcpu_e500->gtlb_params[0].ways))
08b7fa92 240 vcpu_e500->gtlb_nv[0] = 0;
bc8080cb
HB
241
242 return victim;
243}
244
245static inline unsigned int tlb1_max_shadow_size(void)
246{
a4cd8b23 247 /* reserve one entry for magic page */
0164c0f0 248 return host_tlb_params[1].entries - tlbcam_index - 1;
bc8080cb
HB
249}
250
dc83b8bc 251static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
bc8080cb 252{
dc83b8bc 253 return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
bc8080cb
HB
254}
255
256static 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
272static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
273{
046a48b3
LY
274#ifdef CONFIG_SMP
275 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
276#else
bc8080cb 277 return mas2 & MAS2_ATTRIB_MASK;
046a48b3 278#endif
bc8080cb
HB
279}
280
281/*
282 * writing shadow tlb entry to host TLB
283 */
dc83b8bc
SW
284static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
285 uint32_t mas0)
bc8080cb 286{
0ef30995
SW
287 unsigned long flags;
288
289 local_irq_save(flags);
290 mtspr(SPRN_MAS0, mas0);
bc8080cb 291 mtspr(SPRN_MAS1, stlbe->mas1);
dc83b8bc
SW
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));
0ef30995
SW
295 asm volatile("isync; tlbwe" : : : "memory");
296 local_irq_restore(flags);
bc8080cb
HB
297}
298
0164c0f0 299/* esel is index into set, not whole array */
bc8080cb 300static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
dc83b8bc 301 int tlbsel, int esel, struct kvm_book3e_206_tlb_entry *stlbe)
bc8080cb 302{
bc8080cb 303 if (tlbsel == 0) {
dc83b8bc
SW
304 int way = esel & (vcpu_e500->gtlb_params[0].ways - 1);
305 __write_host_tlbe(stlbe, MAS0_TLBSEL(0) | MAS0_ESEL(way));
bc8080cb 306 } else {
0ef30995
SW
307 __write_host_tlbe(stlbe,
308 MAS0_TLBSEL(1) |
309 MAS0_ESEL(to_htlb1_esel(esel)));
bc8080cb 310 }
08b7fa92 311 trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
dc83b8bc 312 (u32)stlbe->mas7_3, (u32)(stlbe->mas7_3 >> 32));
bc8080cb
HB
313}
314
a4cd8b23
SW
315void kvmppc_map_magic(struct kvm_vcpu *vcpu)
316{
dd9ebf1f 317 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
dc83b8bc 318 struct kvm_book3e_206_tlb_entry magic;
a4cd8b23 319 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
dd9ebf1f 320 unsigned int stid;
a4cd8b23
SW
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
dd9ebf1f
LY
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) |
a4cd8b23
SW
330 MAS1_TSIZE(BOOK3E_PAGESZ_4K);
331 magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
dc83b8bc
SW
332 magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
333 MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
a4cd8b23
SW
334
335 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
dd9ebf1f 336 preempt_enable();
a4cd8b23
SW
337}
338
bc8080cb
HB
339void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
340{
dd9ebf1f
LY
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);
bc8080cb
HB
345}
346
347void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
348{
dd9ebf1f
LY
349}
350
0164c0f0
SW
351static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
352 int tlbsel, int esel)
dd9ebf1f 353{
dc83b8bc
SW
354 struct kvm_book3e_206_tlb_entry *gtlbe =
355 get_entry(vcpu_e500, tlbsel, esel);
dd9ebf1f
LY
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();
bc8080cb
HB
407}
408
0164c0f0
SW
409static 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
419static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
420{
dc83b8bc
SW
421 return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
422 vcpu_e500->gtlb_params[0].ways);
0164c0f0
SW
423}
424
425static 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
b5904972 431static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel)
0164c0f0 432{
b5904972
SW
433 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
434 int esel = get_tlb_esel_bit(vcpu);
0164c0f0
SW
435
436 if (tlbsel == 0) {
dc83b8bc 437 esel &= vcpu_e500->gtlb_params[0].ways - 1;
b5904972 438 esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2);
0164c0f0 439 } else {
dc83b8bc 440 esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
0164c0f0
SW
441 }
442
443 return esel;
444}
445
bc8080cb
HB
446/* Search the guest TLB for a matching entry. */
447static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
448 gva_t eaddr, int tlbsel, unsigned int pid, int as)
449{
dc83b8bc
SW
450 int size = vcpu_e500->gtlb_params[tlbsel].entries;
451 unsigned int set_base, offset;
bc8080cb
HB
452 int i;
453
1aee47a0 454 if (tlbsel == 0) {
0164c0f0 455 set_base = gtlb0_set_base(vcpu_e500, eaddr);
dc83b8bc 456 size = vcpu_e500->gtlb_params[0].ways;
1aee47a0
SW
457 } else {
458 set_base = 0;
459 }
460
dc83b8bc
SW
461 offset = vcpu_e500->gtlb_offset[tlbsel];
462
1aee47a0 463 for (i = 0; i < size; i++) {
dc83b8bc
SW
464 struct kvm_book3e_206_tlb_entry *tlbe =
465 &vcpu_e500->gtlb_arch[offset + set_base + i];
bc8080cb
HB
466 unsigned int tid;
467
468 if (eaddr < get_tlb_eaddr(tlbe))
469 continue;
470
471 if (eaddr > get_tlb_end(tlbe))
472 continue;
473
474 tid = get_tlb_tid(tlbe);
475 if (tid && (tid != pid))
476 continue;
477
478 if (!get_tlb_v(tlbe))
479 continue;
480
481 if (get_tlb_ts(tlbe) != as && as != -1)
482 continue;
483
1aee47a0 484 return set_base + i;
bc8080cb
HB
485 }
486
487 return -1;
488}
489
0164c0f0 490static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
dc83b8bc 491 struct kvm_book3e_206_tlb_entry *gtlbe,
0164c0f0 492 pfn_t pfn)
bc8080cb 493{
0164c0f0
SW
494 ref->pfn = pfn;
495 ref->flags = E500_TLB_VALID;
bc8080cb 496
08b7fa92 497 if (tlbe_is_writable(gtlbe))
0164c0f0 498 ref->flags |= E500_TLB_DIRTY;
bc8080cb
HB
499}
500
0164c0f0 501static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
bc8080cb 502{
0164c0f0
SW
503 if (ref->flags & E500_TLB_VALID) {
504 if (ref->flags & E500_TLB_DIRTY)
505 kvm_release_pfn_dirty(ref->pfn);
08b7fa92 506 else
0164c0f0
SW
507 kvm_release_pfn_clean(ref->pfn);
508
509 ref->flags = 0;
510 }
511}
512
513static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
514{
515 int tlbsel = 0;
516 int i;
bc8080cb 517
dc83b8bc 518 for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
0164c0f0
SW
519 struct tlbe_ref *ref =
520 &vcpu_e500->gtlb_priv[tlbsel][i].ref;
521 kvmppc_e500_ref_release(ref);
08b7fa92 522 }
bc8080cb
HB
523}
524
0164c0f0
SW
525static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
526{
527 int stlbsel = 1;
528 int i;
529
dc83b8bc
SW
530 kvmppc_e500_id_table_reset_all(vcpu_e500);
531
0164c0f0
SW
532 for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
533 struct tlbe_ref *ref =
534 &vcpu_e500->tlb_refs[stlbsel][i];
535 kvmppc_e500_ref_release(ref);
536 }
537
538 clear_tlb_privs(vcpu_e500);
539}
540
bc8080cb
HB
541static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
542 unsigned int eaddr, int as)
543{
544 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
545 unsigned int victim, pidsel, tsized;
546 int tlbsel;
547
fb2838d4 548 /* since we only have two TLBs, only lower bit is used. */
b5904972 549 tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
0164c0f0 550 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
b5904972
SW
551 pidsel = (vcpu->arch.shared->mas4 >> 16) & 0xf;
552 tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
bc8080cb 553
b5904972 554 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
08b7fa92 555 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
b5904972 556 vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
bc8080cb
HB
557 | MAS1_TID(vcpu_e500->pid[pidsel])
558 | MAS1_TSIZE(tsized);
b5904972
SW
559 vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
560 | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
561 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
562 vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
bc8080cb
HB
563 | (get_cur_pid(vcpu) << 16)
564 | (as ? MAS6_SAS : 0);
bc8080cb
HB
565}
566
3bf3cdcc 567/* TID must be supplied by the caller */
dc83b8bc
SW
568static inline void kvmppc_e500_setup_stlbe(
569 struct kvmppc_vcpu_e500 *vcpu_e500,
570 struct kvm_book3e_206_tlb_entry *gtlbe,
571 int tsize, struct tlbe_ref *ref, u64 gvaddr,
572 struct kvm_book3e_206_tlb_entry *stlbe)
08b7fa92 573{
0164c0f0
SW
574 pfn_t pfn = ref->pfn;
575
576 BUG_ON(!(ref->flags & E500_TLB_VALID));
08b7fa92
LY
577
578 /* Force TS=1 IPROT=0 for all guest mappings. */
3bf3cdcc 579 stlbe->mas1 = MAS1_TSIZE(tsize) | MAS1_TS | MAS1_VALID;
08b7fa92
LY
580 stlbe->mas2 = (gvaddr & MAS2_EPN)
581 | e500_shadow_mas2_attrib(gtlbe->mas2,
582 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
dc83b8bc
SW
583 stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT)
584 | e500_shadow_mas3_attrib(gtlbe->mas7_3,
08b7fa92 585 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
08b7fa92
LY
586}
587
0164c0f0 588/* sesel is an index into the entire array, not just the set */
bc8080cb 589static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
dc83b8bc
SW
590 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
591 int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe,
592 struct tlbe_ref *ref)
bc8080cb 593{
9973d54e 594 struct kvm_memory_slot *slot;
9973d54e
SW
595 unsigned long pfn, hva;
596 int pfnmap = 0;
597 int tsize = BOOK3E_PAGESZ_4K;
bc8080cb 598
59c1f4e3
SW
599 /*
600 * Translate guest physical to true physical, acquiring
601 * a page reference if it is normal, non-reserved memory.
9973d54e
SW
602 *
603 * gfn_to_memslot() must succeed because otherwise we wouldn't
604 * have gotten this far. Eventually we should just pass the slot
605 * pointer through from the first lookup.
59c1f4e3 606 */
9973d54e
SW
607 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
608 hva = gfn_to_hva_memslot(slot, gfn);
609
610 if (tlbsel == 1) {
611 struct vm_area_struct *vma;
612 down_read(&current->mm->mmap_sem);
613
614 vma = find_vma(current->mm, hva);
615 if (vma && hva >= vma->vm_start &&
616 (vma->vm_flags & VM_PFNMAP)) {
617 /*
618 * This VMA is a physically contiguous region (e.g.
619 * /dev/mem) that bypasses normal Linux page
620 * management. Find the overlap between the
621 * vma and the memslot.
622 */
623
624 unsigned long start, end;
625 unsigned long slot_start, slot_end;
626
627 pfnmap = 1;
628
629 start = vma->vm_pgoff;
630 end = start +
631 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
632
633 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
634
635 slot_start = pfn - (gfn - slot->base_gfn);
636 slot_end = slot_start + slot->npages;
637
638 if (start < slot_start)
639 start = slot_start;
640 if (end > slot_end)
641 end = slot_end;
642
643 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
644 MAS1_TSIZE_SHIFT;
645
646 /*
647 * e500 doesn't implement the lowest tsize bit,
648 * or 1K pages.
649 */
650 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
651
652 /*
653 * Now find the largest tsize (up to what the guest
654 * requested) that will cover gfn, stay within the
655 * range, and for which gfn and pfn are mutually
656 * aligned.
657 */
658
659 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
660 unsigned long gfn_start, gfn_end, tsize_pages;
661 tsize_pages = 1 << (tsize - 2);
662
663 gfn_start = gfn & ~(tsize_pages - 1);
664 gfn_end = gfn_start + tsize_pages;
665
666 if (gfn_start + pfn - gfn < start)
667 continue;
668 if (gfn_end + pfn - gfn > end)
669 continue;
670 if ((gfn & (tsize_pages - 1)) !=
671 (pfn & (tsize_pages - 1)))
672 continue;
673
674 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
675 pfn &= ~(tsize_pages - 1);
676 break;
677 }
95325e6b
AG
678 } else if (vma && hva >= vma->vm_start &&
679 (vma->vm_flags & VM_HUGETLB)) {
680 unsigned long psize = vma_kernel_pagesize(vma);
681
682 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
683 MAS1_TSIZE_SHIFT;
684
685 /*
686 * Take the largest page size that satisfies both host
687 * and guest mapping
688 */
689 tsize = min(__ilog2(psize) - 10, tsize);
690
691 /*
692 * e500 doesn't implement the lowest tsize bit,
693 * or 1K pages.
694 */
695 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
9973d54e
SW
696 }
697
698 up_read(&current->mm->mmap_sem);
699 }
700
701 if (likely(!pfnmap)) {
95325e6b 702 unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
9973d54e
SW
703 pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
704 if (is_error_pfn(pfn)) {
705 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
706 (long)gfn);
707 kvm_release_pfn_clean(pfn);
708 return;
709 }
95325e6b
AG
710
711 /* Align guest and physical address to page map boundaries */
712 pfn &= ~(tsize_pages - 1);
713 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
bc8080cb 714 }
bc8080cb 715
0164c0f0
SW
716 /* Drop old ref and setup new one. */
717 kvmppc_e500_ref_release(ref);
718 kvmppc_e500_ref_setup(ref, gtlbe, pfn);
bc8080cb 719
0164c0f0 720 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, ref, gvaddr, stlbe);
bc8080cb
HB
721}
722
723/* XXX only map the one-one case, for now use TLB0 */
08b7fa92 724static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
dc83b8bc
SW
725 int esel,
726 struct kvm_book3e_206_tlb_entry *stlbe)
bc8080cb 727{
dc83b8bc 728 struct kvm_book3e_206_tlb_entry *gtlbe;
0164c0f0
SW
729 struct tlbe_ref *ref;
730 int sesel = esel & (host_tlb_params[0].ways - 1);
731 int sesel_base;
732 gva_t ea;
bc8080cb 733
dc83b8bc 734 gtlbe = get_entry(vcpu_e500, 0, esel);
0164c0f0
SW
735 ref = &vcpu_e500->gtlb_priv[0][esel].ref;
736
737 ea = get_tlb_eaddr(gtlbe);
738 sesel_base = htlb0_set_base(ea);
bc8080cb
HB
739
740 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
741 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
0164c0f0 742 gtlbe, 0, sesel_base + sesel, stlbe, ref);
bc8080cb 743
0164c0f0 744 return sesel;
bc8080cb
HB
745}
746
747/* Caller must ensure that the specified guest TLB entry is safe to insert into
748 * the shadow TLB. */
749/* XXX for both one-one and one-to-many , for now use TLB1 */
750static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
dc83b8bc
SW
751 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
752 struct kvm_book3e_206_tlb_entry *stlbe)
bc8080cb 753{
0164c0f0 754 struct tlbe_ref *ref;
bc8080cb
HB
755 unsigned int victim;
756
0164c0f0 757 victim = vcpu_e500->host_tlb1_nv++;
bc8080cb 758
0164c0f0
SW
759 if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
760 vcpu_e500->host_tlb1_nv = 0;
bc8080cb 761
0164c0f0
SW
762 ref = &vcpu_e500->tlb_refs[1][victim];
763 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1,
764 victim, stlbe, ref);
bc8080cb
HB
765
766 return victim;
767}
768
dd9ebf1f 769void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
bc8080cb 770{
dd9ebf1f
LY
771 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
772
773 /* Recalc shadow pid since MSR changes */
774 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
bc8080cb
HB
775}
776
08b7fa92
LY
777static inline int kvmppc_e500_gtlbe_invalidate(
778 struct kvmppc_vcpu_e500 *vcpu_e500,
779 int tlbsel, int esel)
bc8080cb 780{
dc83b8bc
SW
781 struct kvm_book3e_206_tlb_entry *gtlbe =
782 get_entry(vcpu_e500, tlbsel, esel);
bc8080cb
HB
783
784 if (unlikely(get_tlb_iprot(gtlbe)))
785 return -1;
786
bc8080cb
HB
787 gtlbe->mas1 = 0;
788
789 return 0;
790}
791
b0a1835d
LY
792int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
793{
794 int esel;
795
796 if (value & MMUCSR0_TLB0FI)
dc83b8bc 797 for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
b0a1835d
LY
798 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
799 if (value & MMUCSR0_TLB1FI)
dc83b8bc 800 for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
b0a1835d
LY
801 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
802
dd9ebf1f
LY
803 /* Invalidate all vcpu id mappings */
804 kvmppc_e500_id_table_reset_all(vcpu_e500);
b0a1835d
LY
805
806 return EMULATE_DONE;
807}
808
bc8080cb
HB
809int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
810{
811 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
812 unsigned int ia;
813 int esel, tlbsel;
814 gva_t ea;
815
8e5b26b5 816 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
bc8080cb
HB
817
818 ia = (ea >> 2) & 0x1;
819
fb2838d4 820 /* since we only have two TLBs, only lower bit is used. */
bc8080cb
HB
821 tlbsel = (ea >> 3) & 0x1;
822
823 if (ia) {
824 /* invalidate all entries */
dc83b8bc
SW
825 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
826 esel++)
bc8080cb
HB
827 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
828 } else {
829 ea &= 0xfffff000;
830 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
831 get_cur_pid(vcpu), -1);
832 if (esel >= 0)
833 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
834 }
835
dd9ebf1f
LY
836 /* Invalidate all vcpu id mappings */
837 kvmppc_e500_id_table_reset_all(vcpu_e500);
bc8080cb
HB
838
839 return EMULATE_DONE;
840}
841
842int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
843{
844 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
845 int tlbsel, esel;
dc83b8bc 846 struct kvm_book3e_206_tlb_entry *gtlbe;
bc8080cb 847
b5904972
SW
848 tlbsel = get_tlb_tlbsel(vcpu);
849 esel = get_tlb_esel(vcpu, tlbsel);
bc8080cb 850
dc83b8bc 851 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
b5904972
SW
852 vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
853 vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
854 vcpu->arch.shared->mas1 = gtlbe->mas1;
855 vcpu->arch.shared->mas2 = gtlbe->mas2;
856 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
bc8080cb
HB
857
858 return EMULATE_DONE;
859}
860
861int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
862{
863 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
b5904972
SW
864 int as = !!get_cur_sas(vcpu);
865 unsigned int pid = get_cur_spid(vcpu);
bc8080cb 866 int esel, tlbsel;
dc83b8bc 867 struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
bc8080cb
HB
868 gva_t ea;
869
8e5b26b5 870 ea = kvmppc_get_gpr(vcpu, rb);
bc8080cb
HB
871
872 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
873 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
874 if (esel >= 0) {
dc83b8bc 875 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
bc8080cb
HB
876 break;
877 }
878 }
879
880 if (gtlbe) {
303b7c97
SW
881 esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
882
b5904972 883 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
08b7fa92 884 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
b5904972
SW
885 vcpu->arch.shared->mas1 = gtlbe->mas1;
886 vcpu->arch.shared->mas2 = gtlbe->mas2;
887 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
bc8080cb
HB
888 } else {
889 int victim;
890
fb2838d4 891 /* since we only have two TLBs, only lower bit is used. */
b5904972 892 tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
0164c0f0 893 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
bc8080cb 894
b5904972
SW
895 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
896 | MAS0_ESEL(victim)
08b7fa92 897 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
b5904972
SW
898 vcpu->arch.shared->mas1 =
899 (vcpu->arch.shared->mas6 & MAS6_SPID0)
900 | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
901 | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
902 vcpu->arch.shared->mas2 &= MAS2_EPN;
903 vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
904 MAS2_ATTRIB_MASK;
905 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
906 MAS3_U2 | MAS3_U3;
bc8080cb
HB
907 }
908
49ea0695 909 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
bc8080cb
HB
910 return EMULATE_DONE;
911}
912
3bf3cdcc
SW
913/* sesel is index into the set, not the whole array */
914static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
dc83b8bc
SW
915 struct kvm_book3e_206_tlb_entry *gtlbe,
916 struct kvm_book3e_206_tlb_entry *stlbe,
3bf3cdcc
SW
917 int stlbsel, int sesel)
918{
919 int stid;
920
921 preempt_disable();
922 stid = kvmppc_e500_get_sid(vcpu_e500, get_tlb_ts(gtlbe),
923 get_tlb_tid(gtlbe),
924 get_cur_pr(&vcpu_e500->vcpu), 0);
925
926 stlbe->mas1 |= MAS1_TID(stid);
927 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
928 preempt_enable();
929}
930
bc8080cb
HB
931int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
932{
933 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
dc83b8bc 934 struct kvm_book3e_206_tlb_entry *gtlbe;
08b7fa92 935 int tlbsel, esel;
bc8080cb 936
b5904972
SW
937 tlbsel = get_tlb_tlbsel(vcpu);
938 esel = get_tlb_esel(vcpu, tlbsel);
bc8080cb 939
dc83b8bc 940 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
bc8080cb 941
dd9ebf1f 942 if (get_tlb_v(gtlbe))
0164c0f0 943 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
bc8080cb 944
b5904972
SW
945 gtlbe->mas1 = vcpu->arch.shared->mas1;
946 gtlbe->mas2 = vcpu->arch.shared->mas2;
947 gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
bc8080cb 948
b5904972 949 trace_kvm_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1, gtlbe->mas2,
dc83b8bc 950 (u32)gtlbe->mas7_3, (u32)(gtlbe->mas7_3 >> 32));
bc8080cb
HB
951
952 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
953 if (tlbe_is_host_safe(vcpu, gtlbe)) {
dc83b8bc 954 struct kvm_book3e_206_tlb_entry stlbe;
08b7fa92
LY
955 int stlbsel, sesel;
956 u64 eaddr;
957 u64 raddr;
958
bc8080cb
HB
959 switch (tlbsel) {
960 case 0:
961 /* TLB0 */
962 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
0cfb50e5 963 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
bc8080cb
HB
964
965 stlbsel = 0;
08b7fa92 966 sesel = kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
bc8080cb
HB
967
968 break;
969
970 case 1:
971 /* TLB1 */
972 eaddr = get_tlb_eaddr(gtlbe);
973 raddr = get_tlb_raddr(gtlbe);
974
975 /* Create a 4KB mapping on the host.
976 * If the guest wanted a large page,
977 * only the first 4KB is mapped here and the rest
978 * are mapped on the fly. */
979 stlbsel = 1;
980 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
08b7fa92 981 raddr >> PAGE_SHIFT, gtlbe, &stlbe);
bc8080cb
HB
982 break;
983
984 default:
985 BUG();
986 }
3bf3cdcc
SW
987
988 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
bc8080cb
HB
989 }
990
49ea0695 991 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
bc8080cb
HB
992 return EMULATE_DONE;
993}
994
995int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
996{
666e7252 997 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
bc8080cb
HB
998
999 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
1000}
1001
1002int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
1003{
666e7252 1004 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
bc8080cb
HB
1005
1006 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
1007}
1008
1009void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
1010{
666e7252 1011 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
bc8080cb
HB
1012
1013 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
1014}
1015
1016void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
1017{
666e7252 1018 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
bc8080cb
HB
1019
1020 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
1021}
1022
1023gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
1024 gva_t eaddr)
1025{
1026 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
dc83b8bc
SW
1027 struct kvm_book3e_206_tlb_entry *gtlbe;
1028 u64 pgmask;
1029
1030 gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
1031 pgmask = get_tlb_bytes(gtlbe) - 1;
bc8080cb
HB
1032
1033 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
1034}
1035
1036void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1037{
bc8080cb
HB
1038}
1039
1040void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
1041 unsigned int index)
1042{
1043 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
08b7fa92 1044 struct tlbe_priv *priv;
dc83b8bc 1045 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
bc8080cb
HB
1046 int tlbsel = tlbsel_of(index);
1047 int esel = esel_of(index);
1048 int stlbsel, sesel;
1049
dc83b8bc 1050 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
08b7fa92 1051
bc8080cb
HB
1052 switch (tlbsel) {
1053 case 0:
1054 stlbsel = 0;
0164c0f0
SW
1055 sesel = esel & (host_tlb_params[0].ways - 1);
1056 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
08b7fa92
LY
1057
1058 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K,
0164c0f0 1059 &priv->ref, eaddr, &stlbe);
bc8080cb
HB
1060 break;
1061
1062 case 1: {
1063 gfn_t gfn = gpaddr >> PAGE_SHIFT;
bc8080cb
HB
1064
1065 stlbsel = 1;
08b7fa92
LY
1066 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
1067 gtlbe, &stlbe);
bc8080cb
HB
1068 break;
1069 }
1070
1071 default:
1072 BUG();
1073 break;
1074 }
08b7fa92 1075
3bf3cdcc 1076 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
bc8080cb
HB
1077}
1078
1079int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
1080 gva_t eaddr, unsigned int pid, int as)
1081{
1082 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1083 int esel, tlbsel;
1084
1085 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
1086 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
1087 if (esel >= 0)
1088 return index_of(tlbsel, esel);
1089 }
1090
1091 return -1;
1092}
1093
5ce941ee
SW
1094void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
1095{
1096 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1097
dd9ebf1f
LY
1098 if (vcpu->arch.pid != pid) {
1099 vcpu_e500->pid[0] = vcpu->arch.pid = pid;
1100 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
1101 }
5ce941ee
SW
1102}
1103
bc8080cb
HB
1104void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
1105{
dc83b8bc 1106 struct kvm_book3e_206_tlb_entry *tlbe;
bc8080cb
HB
1107
1108 /* Insert large initial mapping for guest. */
dc83b8bc 1109 tlbe = get_entry(vcpu_e500, 1, 0);
0cfb50e5 1110 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
bc8080cb 1111 tlbe->mas2 = 0;
dc83b8bc 1112 tlbe->mas7_3 = E500_TLB_SUPER_PERM_MASK;
bc8080cb
HB
1113
1114 /* 4K map for serial output. Used by kernel wrapper. */
dc83b8bc 1115 tlbe = get_entry(vcpu_e500, 1, 1);
0cfb50e5 1116 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
bc8080cb 1117 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
dc83b8bc
SW
1118 tlbe->mas7_3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
1119}
1120
1121static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
1122{
1123 int i;
1124
1125 clear_tlb_refs(vcpu_e500);
1126 kfree(vcpu_e500->gtlb_priv[0]);
1127 kfree(vcpu_e500->gtlb_priv[1]);
1128
1129 if (vcpu_e500->shared_tlb_pages) {
1130 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
1131 PAGE_SIZE)));
1132
1133 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
1134 set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
1135 put_page(vcpu_e500->shared_tlb_pages[i]);
1136 }
1137
1138 vcpu_e500->num_shared_tlb_pages = 0;
1139 vcpu_e500->shared_tlb_pages = NULL;
1140 } else {
1141 kfree(vcpu_e500->gtlb_arch);
1142 }
1143
1144 vcpu_e500->gtlb_arch = NULL;
1145}
1146
1147int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
1148 struct kvm_config_tlb *cfg)
1149{
1150 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1151 struct kvm_book3e_206_tlb_params params;
1152 char *virt;
1153 struct page **pages;
1154 struct tlbe_priv *privs[2] = {};
1155 size_t array_len;
1156 u32 sets;
1157 int num_pages, ret, i;
1158
1159 if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
1160 return -EINVAL;
1161
1162 if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
1163 sizeof(params)))
1164 return -EFAULT;
1165
1166 if (params.tlb_sizes[1] > 64)
1167 return -EINVAL;
1168 if (params.tlb_ways[1] != params.tlb_sizes[1])
1169 return -EINVAL;
1170 if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
1171 return -EINVAL;
1172 if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
1173 return -EINVAL;
1174
1175 if (!is_power_of_2(params.tlb_ways[0]))
1176 return -EINVAL;
1177
1178 sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1179 if (!is_power_of_2(sets))
1180 return -EINVAL;
1181
1182 array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1183 array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1184
1185 if (cfg->array_len < array_len)
1186 return -EINVAL;
1187
1188 num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1189 cfg->array / PAGE_SIZE;
1190 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1191 if (!pages)
1192 return -ENOMEM;
1193
1194 ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1195 if (ret < 0)
1196 goto err_pages;
1197
1198 if (ret != num_pages) {
1199 num_pages = ret;
1200 ret = -EFAULT;
1201 goto err_put_page;
1202 }
1203
1204 virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1205 if (!virt)
1206 goto err_put_page;
1207
1208 privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1209 GFP_KERNEL);
1210 privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1211 GFP_KERNEL);
1212
1213 if (!privs[0] || !privs[1])
1214 goto err_put_page;
1215
1216 free_gtlb(vcpu_e500);
1217
1218 vcpu_e500->gtlb_priv[0] = privs[0];
1219 vcpu_e500->gtlb_priv[1] = privs[1];
1220
1221 vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1222 (virt + (cfg->array & (PAGE_SIZE - 1)));
1223
1224 vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1225 vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1226
1227 vcpu_e500->gtlb_offset[0] = 0;
1228 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1229
7b11dc99 1230 vcpu_e500->tlb0cfg &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
dc83b8bc
SW
1231 if (params.tlb_sizes[0] <= 2048)
1232 vcpu_e500->tlb0cfg |= params.tlb_sizes[0];
7b11dc99 1233 vcpu_e500->tlb0cfg |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
dc83b8bc 1234
7b11dc99 1235 vcpu_e500->tlb1cfg &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
dc83b8bc 1236 vcpu_e500->tlb1cfg |= params.tlb_sizes[1];
7b11dc99 1237 vcpu_e500->tlb1cfg |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
dc83b8bc
SW
1238
1239 vcpu_e500->shared_tlb_pages = pages;
1240 vcpu_e500->num_shared_tlb_pages = num_pages;
1241
1242 vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1243 vcpu_e500->gtlb_params[0].sets = sets;
1244
1245 vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1246 vcpu_e500->gtlb_params[1].sets = 1;
1247
1248 return 0;
1249
1250err_put_page:
1251 kfree(privs[0]);
1252 kfree(privs[1]);
1253
1254 for (i = 0; i < num_pages; i++)
1255 put_page(pages[i]);
1256
1257err_pages:
1258 kfree(pages);
1259 return ret;
1260}
1261
1262int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1263 struct kvm_dirty_tlb *dirty)
1264{
1265 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1266
1267 clear_tlb_refs(vcpu_e500);
1268 return 0;
bc8080cb
HB
1269}
1270
1271int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1272{
dc83b8bc
SW
1273 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1274 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1275
0164c0f0
SW
1276 host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1277 host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1278
1279 /*
1280 * This should never happen on real e500 hardware, but is
1281 * architecturally possible -- e.g. in some weird nested
1282 * virtualization case.
1283 */
1284 if (host_tlb_params[0].entries == 0 ||
1285 host_tlb_params[1].entries == 0) {
1286 pr_err("%s: need to know host tlb size\n", __func__);
1287 return -ENODEV;
1288 }
1289
1290 host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1291 TLBnCFG_ASSOC_SHIFT;
1292 host_tlb_params[1].ways = host_tlb_params[1].entries;
1293
1294 if (!is_power_of_2(host_tlb_params[0].entries) ||
1295 !is_power_of_2(host_tlb_params[0].ways) ||
1296 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1297 host_tlb_params[0].ways == 0) {
1298 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1299 __func__, host_tlb_params[0].entries,
1300 host_tlb_params[0].ways);
1301 return -ENODEV;
1302 }
1303
1304 host_tlb_params[0].sets =
1305 host_tlb_params[0].entries / host_tlb_params[0].ways;
1306 host_tlb_params[1].sets = 1;
bc8080cb 1307
dc83b8bc
SW
1308 vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1309 vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
bc8080cb 1310
dc83b8bc
SW
1311 vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1312 vcpu_e500->gtlb_params[0].sets =
1313 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1314
1315 vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1316 vcpu_e500->gtlb_params[1].sets = 1;
1317
1318 vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1319 if (!vcpu_e500->gtlb_arch)
1320 return -ENOMEM;
1321
1322 vcpu_e500->gtlb_offset[0] = 0;
1323 vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
0164c0f0
SW
1324
1325 vcpu_e500->tlb_refs[0] =
1326 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1327 GFP_KERNEL);
1328 if (!vcpu_e500->tlb_refs[0])
1329 goto err;
1330
1331 vcpu_e500->tlb_refs[1] =
1332 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1333 GFP_KERNEL);
1334 if (!vcpu_e500->tlb_refs[1])
1335 goto err;
1336
dc83b8bc
SW
1337 vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1338 vcpu_e500->gtlb_params[0].entries,
1339 GFP_KERNEL);
0164c0f0
SW
1340 if (!vcpu_e500->gtlb_priv[0])
1341 goto err;
1342
dc83b8bc
SW
1343 vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1344 vcpu_e500->gtlb_params[1].entries,
1345 GFP_KERNEL);
0164c0f0
SW
1346 if (!vcpu_e500->gtlb_priv[1])
1347 goto err;
bc8080cb 1348
dd9ebf1f 1349 if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
0164c0f0 1350 goto err;
dd9ebf1f 1351
da15bf43 1352 /* Init TLB configuration register */
7b11dc99
SW
1353 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) &
1354 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
dc83b8bc 1355 vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[0].entries;
7b11dc99
SW
1356 vcpu_e500->tlb0cfg |=
1357 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
1358
1359 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) &
1360 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1361 vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[1].entries;
1362 vcpu_e500->tlb0cfg |=
1363 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
da15bf43 1364
bc8080cb
HB
1365 return 0;
1366
0164c0f0 1367err:
dc83b8bc 1368 free_gtlb(vcpu_e500);
0164c0f0
SW
1369 kfree(vcpu_e500->tlb_refs[0]);
1370 kfree(vcpu_e500->tlb_refs[1]);
bc8080cb
HB
1371 return -1;
1372}
1373
1374void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1375{
dc83b8bc 1376 free_gtlb(vcpu_e500);
dd9ebf1f 1377 kvmppc_e500_id_table_free(vcpu_e500);
0164c0f0
SW
1378
1379 kfree(vcpu_e500->tlb_refs[0]);
1380 kfree(vcpu_e500->tlb_refs[1]);
bc8080cb 1381}