blob: c09e642ee5373b95467db10942c8878f29d9177e [file] [log] [blame]
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001/*
Scott Wood49ea0692011-03-28 15:01:24 -05002 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06003 *
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/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060017#include <linux/string.h>
18#include <linux/kvm.h>
19#include <linux/kvm_host.h>
20#include <linux/highmem.h>
21#include <asm/kvm_ppc.h>
22#include <asm/kvm_e500.h>
23
Liu Yu9aa4dd52009-01-14 10:47:38 -060024#include "../mm/mmu_decl.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060025#include "e500_tlb.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030026#include "trace.h"
Scott Wood49ea0692011-03-28 15:01:24 -050027#include "timing.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060028
29#define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1)
30
31static unsigned int tlb1_entry_num;
32
33void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
34{
35 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
36 struct tlbe *tlbe;
37 int i, tlbsel;
38
39 printk("| %8s | %8s | %8s | %8s | %8s |\n",
40 "nr", "mas1", "mas2", "mas3", "mas7");
41
42 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
43 printk("Guest TLB%d:\n", tlbsel);
44 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
45 tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
46 if (tlbe->mas1 & MAS1_VALID)
47 printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
48 tlbsel, i, tlbe->mas1, tlbe->mas2,
49 tlbe->mas3, tlbe->mas7);
50 }
51 }
52
53 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
54 printk("Shadow TLB%d:\n", tlbsel);
55 for (i = 0; i < vcpu_e500->shadow_tlb_size[tlbsel]; i++) {
56 tlbe = &vcpu_e500->shadow_tlb[tlbsel][i];
57 if (tlbe->mas1 & MAS1_VALID)
58 printk(" S[%d][%3d] | %08X | %08X | %08X | %08X |\n",
59 tlbsel, i, tlbe->mas1, tlbe->mas2,
60 tlbe->mas3, tlbe->mas7);
61 }
62 }
63}
64
65static inline unsigned int tlb0_get_next_victim(
66 struct kvmppc_vcpu_e500 *vcpu_e500)
67{
68 unsigned int victim;
69
70 victim = vcpu_e500->guest_tlb_nv[0]++;
71 if (unlikely(vcpu_e500->guest_tlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
72 vcpu_e500->guest_tlb_nv[0] = 0;
73
74 return victim;
75}
76
77static inline unsigned int tlb1_max_shadow_size(void)
78{
Scott Wooda4cd8b22011-06-14 18:34:41 -050079 /* reserve one entry for magic page */
80 return tlb1_entry_num - tlbcam_index - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060081}
82
83static inline int tlbe_is_writable(struct tlbe *tlbe)
84{
85 return tlbe->mas3 & (MAS3_SW|MAS3_UW);
86}
87
88static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
89{
90 /* Mask off reserved bits. */
91 mas3 &= MAS3_ATTRIB_MASK;
92
93 if (!usermode) {
94 /* Guest is in supervisor mode,
95 * so we need to translate guest
96 * supervisor permissions into user permissions. */
97 mas3 &= ~E500_TLB_USER_PERM_MASK;
98 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
99 }
100
101 return mas3 | E500_TLB_SUPER_PERM_MASK;
102}
103
104static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
105{
Liu Yu046a48b2009-03-17 16:57:46 +0800106#ifdef CONFIG_SMP
107 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
108#else
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600109 return mas2 & MAS2_ATTRIB_MASK;
Liu Yu046a48b2009-03-17 16:57:46 +0800110#endif
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600111}
112
113/*
114 * writing shadow tlb entry to host TLB
115 */
Scott Wood0ef30992011-06-14 18:34:35 -0500116static inline void __write_host_tlbe(struct tlbe *stlbe, uint32_t mas0)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600117{
Scott Wood0ef30992011-06-14 18:34:35 -0500118 unsigned long flags;
119
120 local_irq_save(flags);
121 mtspr(SPRN_MAS0, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600122 mtspr(SPRN_MAS1, stlbe->mas1);
123 mtspr(SPRN_MAS2, stlbe->mas2);
124 mtspr(SPRN_MAS3, stlbe->mas3);
125 mtspr(SPRN_MAS7, stlbe->mas7);
Scott Wood0ef30992011-06-14 18:34:35 -0500126 asm volatile("isync; tlbwe" : : : "memory");
127 local_irq_restore(flags);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600128}
129
130static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
131 int tlbsel, int esel)
132{
133 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
134
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600135 if (tlbsel == 0) {
Scott Wood0ef30992011-06-14 18:34:35 -0500136 __write_host_tlbe(stlbe,
137 MAS0_TLBSEL(0) |
138 MAS0_ESEL(esel & (KVM_E500_TLB0_WAY_NUM - 1)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600139 } else {
Scott Wood0ef30992011-06-14 18:34:35 -0500140 __write_host_tlbe(stlbe,
141 MAS0_TLBSEL(1) |
142 MAS0_ESEL(to_htlb1_esel(esel)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600143 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600144}
145
Scott Wooda4cd8b22011-06-14 18:34:41 -0500146void kvmppc_map_magic(struct kvm_vcpu *vcpu)
147{
148 struct tlbe magic;
149 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
150 pfn_t pfn;
151
152 pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
153 get_page(pfn_to_page(pfn));
154
155 magic.mas1 = MAS1_VALID | MAS1_TS |
156 MAS1_TSIZE(BOOK3E_PAGESZ_4K);
157 magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
158 magic.mas3 = (pfn << PAGE_SHIFT) |
159 MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
160 magic.mas7 = pfn >> (32 - PAGE_SHIFT);
161
162 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
163}
164
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600165void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
166{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600167}
168
169void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
170{
Liu Yu9aa4dd52009-01-14 10:47:38 -0600171 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600172}
173
174/* Search the guest TLB for a matching entry. */
175static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
176 gva_t eaddr, int tlbsel, unsigned int pid, int as)
177{
178 int i;
179
180 /* XXX Replace loop with fancy data structures. */
181 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
182 struct tlbe *tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
183 unsigned int tid;
184
185 if (eaddr < get_tlb_eaddr(tlbe))
186 continue;
187
188 if (eaddr > get_tlb_end(tlbe))
189 continue;
190
191 tid = get_tlb_tid(tlbe);
192 if (tid && (tid != pid))
193 continue;
194
195 if (!get_tlb_v(tlbe))
196 continue;
197
198 if (get_tlb_ts(tlbe) != as && as != -1)
199 continue;
200
201 return i;
202 }
203
204 return -1;
205}
206
207static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500 *vcpu_e500,
208 int tlbsel, int esel)
209{
210 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
Scott Wood59c1f4e2011-06-14 18:34:37 -0500211 unsigned long pfn;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600212
Scott Wood59c1f4e2011-06-14 18:34:37 -0500213 pfn = stlbe->mas3 >> PAGE_SHIFT;
214 pfn |= stlbe->mas7 << (32 - PAGE_SHIFT);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600215
Scott Wood59c1f4e2011-06-14 18:34:37 -0500216 if (get_tlb_v(stlbe)) {
217 if (tlbe_is_writable(stlbe))
218 kvm_release_pfn_dirty(pfn);
219 else
220 kvm_release_pfn_clean(pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600221 }
222}
223
224static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
225 int tlbsel, int esel)
226{
227 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
228
229 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
230 stlbe->mas1 = 0;
Kyle Moffett21e537b2010-08-30 11:38:39 -0400231 trace_kvm_stlb_inval(index_of(tlbsel, esel));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600232}
233
234static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
235 gva_t eaddr, gva_t eend, u32 tid)
236{
237 unsigned int pid = tid & 0xff;
238 unsigned int i;
239
240 /* XXX Replace loop with fancy data structures. */
241 for (i = 0; i < vcpu_e500->guest_tlb_size[1]; i++) {
242 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
243 unsigned int tid;
244
245 if (!get_tlb_v(stlbe))
246 continue;
247
248 if (eend < get_tlb_eaddr(stlbe))
249 continue;
250
251 if (eaddr > get_tlb_end(stlbe))
252 continue;
253
254 tid = get_tlb_tid(stlbe);
255 if (tid && (tid != pid))
256 continue;
257
258 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
259 write_host_tlbe(vcpu_e500, 1, i);
260 }
261}
262
263static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
264 unsigned int eaddr, int as)
265{
266 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
267 unsigned int victim, pidsel, tsized;
268 int tlbsel;
269
Liu Yufb2838d2009-01-14 10:47:37 -0600270 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600271 tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
272 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
273 pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
Liu Yu0cfb50e2009-06-05 14:54:29 +0800274 tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600275
276 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
277 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
278 vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
279 | MAS1_TID(vcpu_e500->pid[pidsel])
280 | MAS1_TSIZE(tsized);
281 vcpu_e500->mas2 = (eaddr & MAS2_EPN)
282 | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
283 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
284 vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
285 | (get_cur_pid(vcpu) << 16)
286 | (as ? MAS6_SAS : 0);
287 vcpu_e500->mas7 = 0;
288}
289
290static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
291 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel)
292{
Scott Wood9973d542011-06-14 18:34:39 -0500293 struct kvm_memory_slot *slot;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600294 struct tlbe *stlbe;
Scott Wood9973d542011-06-14 18:34:39 -0500295 unsigned long pfn, hva;
296 int pfnmap = 0;
297 int tsize = BOOK3E_PAGESZ_4K;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600298
299 stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
300
Scott Wood59c1f4e2011-06-14 18:34:37 -0500301 /*
302 * Translate guest physical to true physical, acquiring
303 * a page reference if it is normal, non-reserved memory.
Scott Wood9973d542011-06-14 18:34:39 -0500304 *
305 * gfn_to_memslot() must succeed because otherwise we wouldn't
306 * have gotten this far. Eventually we should just pass the slot
307 * pointer through from the first lookup.
Scott Wood59c1f4e2011-06-14 18:34:37 -0500308 */
Scott Wood9973d542011-06-14 18:34:39 -0500309 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
310 hva = gfn_to_hva_memslot(slot, gfn);
311
312 if (tlbsel == 1) {
313 struct vm_area_struct *vma;
314 down_read(&current->mm->mmap_sem);
315
316 vma = find_vma(current->mm, hva);
317 if (vma && hva >= vma->vm_start &&
318 (vma->vm_flags & VM_PFNMAP)) {
319 /*
320 * This VMA is a physically contiguous region (e.g.
321 * /dev/mem) that bypasses normal Linux page
322 * management. Find the overlap between the
323 * vma and the memslot.
324 */
325
326 unsigned long start, end;
327 unsigned long slot_start, slot_end;
328
329 pfnmap = 1;
330
331 start = vma->vm_pgoff;
332 end = start +
333 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
334
335 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
336
337 slot_start = pfn - (gfn - slot->base_gfn);
338 slot_end = slot_start + slot->npages;
339
340 if (start < slot_start)
341 start = slot_start;
342 if (end > slot_end)
343 end = slot_end;
344
345 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
346 MAS1_TSIZE_SHIFT;
347
348 /*
349 * e500 doesn't implement the lowest tsize bit,
350 * or 1K pages.
351 */
352 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
353
354 /*
355 * Now find the largest tsize (up to what the guest
356 * requested) that will cover gfn, stay within the
357 * range, and for which gfn and pfn are mutually
358 * aligned.
359 */
360
361 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
362 unsigned long gfn_start, gfn_end, tsize_pages;
363 tsize_pages = 1 << (tsize - 2);
364
365 gfn_start = gfn & ~(tsize_pages - 1);
366 gfn_end = gfn_start + tsize_pages;
367
368 if (gfn_start + pfn - gfn < start)
369 continue;
370 if (gfn_end + pfn - gfn > end)
371 continue;
372 if ((gfn & (tsize_pages - 1)) !=
373 (pfn & (tsize_pages - 1)))
374 continue;
375
376 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
377 pfn &= ~(tsize_pages - 1);
378 break;
379 }
380 }
381
382 up_read(&current->mm->mmap_sem);
383 }
384
385 if (likely(!pfnmap)) {
386 pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
387 if (is_error_pfn(pfn)) {
388 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
389 (long)gfn);
390 kvm_release_pfn_clean(pfn);
391 return;
392 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600393 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600394
395 /* Drop reference to old page. */
396 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
397
Scott Wood9973d542011-06-14 18:34:39 -0500398 /* Force TS=1 IPROT=0 for all guest mappings. */
399 stlbe->mas1 = MAS1_TSIZE(tsize)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600400 | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID;
401 stlbe->mas2 = (gvaddr & MAS2_EPN)
402 | e500_shadow_mas2_attrib(gtlbe->mas2,
Alexander Graf666e7252010-07-29 14:47:43 +0200403 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
Scott Wood59c1f4e2011-06-14 18:34:37 -0500404 stlbe->mas3 = ((pfn << PAGE_SHIFT) & MAS3_RPN)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600405 | e500_shadow_mas3_attrib(gtlbe->mas3,
Alexander Graf666e7252010-07-29 14:47:43 +0200406 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
Scott Wood59c1f4e2011-06-14 18:34:37 -0500407 stlbe->mas7 = (pfn >> (32 - PAGE_SHIFT)) & MAS7_RPN;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600408
Marcelo Tosatti46f43c62009-06-18 11:47:27 -0300409 trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
410 stlbe->mas3, stlbe->mas7);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600411}
412
413/* XXX only map the one-one case, for now use TLB0 */
414static int kvmppc_e500_stlbe_map(struct kvmppc_vcpu_e500 *vcpu_e500,
415 int tlbsel, int esel)
416{
417 struct tlbe *gtlbe;
418
419 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
420
421 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
422 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
423 gtlbe, tlbsel, esel);
424
425 return esel;
426}
427
428/* Caller must ensure that the specified guest TLB entry is safe to insert into
429 * the shadow TLB. */
430/* XXX for both one-one and one-to-many , for now use TLB1 */
431static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
432 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe)
433{
434 unsigned int victim;
435
436 victim = vcpu_e500->guest_tlb_nv[1]++;
437
438 if (unlikely(vcpu_e500->guest_tlb_nv[1] >= tlb1_max_shadow_size()))
439 vcpu_e500->guest_tlb_nv[1] = 0;
440
441 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim);
442
443 return victim;
444}
445
446/* Invalidate all guest kernel mappings when enter usermode,
447 * so that when they fault back in they will get the
448 * proper permission bits. */
449void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
450{
451 if (usermode) {
452 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
453 int i;
454
455 /* XXX Replace loop with fancy data structures. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600456 for (i = 0; i < tlb1_max_shadow_size(); i++)
457 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
458
Liu Yu9aa4dd52009-01-14 10:47:38 -0600459 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600460 }
461}
462
463static int kvmppc_e500_gtlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
464 int tlbsel, int esel)
465{
466 struct tlbe *gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
467
468 if (unlikely(get_tlb_iprot(gtlbe)))
469 return -1;
470
471 if (tlbsel == 1) {
472 kvmppc_e500_tlb1_invalidate(vcpu_e500, get_tlb_eaddr(gtlbe),
473 get_tlb_end(gtlbe),
474 get_tlb_tid(gtlbe));
475 } else {
476 kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
477 }
478
479 gtlbe->mas1 = 0;
480
481 return 0;
482}
483
Liu Yub0a18352009-02-17 16:52:08 +0800484int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
485{
486 int esel;
487
488 if (value & MMUCSR0_TLB0FI)
489 for (esel = 0; esel < vcpu_e500->guest_tlb_size[0]; esel++)
490 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
491 if (value & MMUCSR0_TLB1FI)
492 for (esel = 0; esel < vcpu_e500->guest_tlb_size[1]; esel++)
493 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
494
495 _tlbil_all();
496
497 return EMULATE_DONE;
498}
499
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600500int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
501{
502 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
503 unsigned int ia;
504 int esel, tlbsel;
505 gva_t ea;
506
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100507 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600508
509 ia = (ea >> 2) & 0x1;
510
Liu Yufb2838d2009-01-14 10:47:37 -0600511 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600512 tlbsel = (ea >> 3) & 0x1;
513
514 if (ia) {
515 /* invalidate all entries */
516 for (esel = 0; esel < vcpu_e500->guest_tlb_size[tlbsel]; esel++)
517 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
518 } else {
519 ea &= 0xfffff000;
520 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
521 get_cur_pid(vcpu), -1);
522 if (esel >= 0)
523 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
524 }
525
Liu Yu9aa4dd52009-01-14 10:47:38 -0600526 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600527
528 return EMULATE_DONE;
529}
530
531int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
532{
533 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
534 int tlbsel, esel;
535 struct tlbe *gtlbe;
536
537 tlbsel = get_tlb_tlbsel(vcpu_e500);
538 esel = get_tlb_esel(vcpu_e500, tlbsel);
539
540 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
Liu Yubc35cbc2009-03-17 16:57:45 +0800541 vcpu_e500->mas0 &= ~MAS0_NV(~0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600542 vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
543 vcpu_e500->mas1 = gtlbe->mas1;
544 vcpu_e500->mas2 = gtlbe->mas2;
545 vcpu_e500->mas3 = gtlbe->mas3;
546 vcpu_e500->mas7 = gtlbe->mas7;
547
548 return EMULATE_DONE;
549}
550
551int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
552{
553 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
554 int as = !!get_cur_sas(vcpu_e500);
555 unsigned int pid = get_cur_spid(vcpu_e500);
556 int esel, tlbsel;
557 struct tlbe *gtlbe = NULL;
558 gva_t ea;
559
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100560 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600561
562 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
563 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
564 if (esel >= 0) {
565 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
566 break;
567 }
568 }
569
570 if (gtlbe) {
571 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
572 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
573 vcpu_e500->mas1 = gtlbe->mas1;
574 vcpu_e500->mas2 = gtlbe->mas2;
575 vcpu_e500->mas3 = gtlbe->mas3;
576 vcpu_e500->mas7 = gtlbe->mas7;
577 } else {
578 int victim;
579
Liu Yufb2838d2009-01-14 10:47:37 -0600580 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600581 tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
582 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
583
584 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
585 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
586 vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
587 | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
588 | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
589 vcpu_e500->mas2 &= MAS2_EPN;
590 vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
591 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
592 vcpu_e500->mas7 = 0;
593 }
594
Scott Wood49ea0692011-03-28 15:01:24 -0500595 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600596 return EMULATE_DONE;
597}
598
599int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
600{
601 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
602 u64 eaddr;
603 u64 raddr;
604 u32 tid;
605 struct tlbe *gtlbe;
606 int tlbsel, esel, stlbsel, sesel;
607
608 tlbsel = get_tlb_tlbsel(vcpu_e500);
609 esel = get_tlb_esel(vcpu_e500, tlbsel);
610
611 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
612
613 if (get_tlb_v(gtlbe) && tlbsel == 1) {
614 eaddr = get_tlb_eaddr(gtlbe);
615 tid = get_tlb_tid(gtlbe);
616 kvmppc_e500_tlb1_invalidate(vcpu_e500, eaddr,
617 get_tlb_end(gtlbe), tid);
618 }
619
620 gtlbe->mas1 = vcpu_e500->mas1;
621 gtlbe->mas2 = vcpu_e500->mas2;
622 gtlbe->mas3 = vcpu_e500->mas3;
623 gtlbe->mas7 = vcpu_e500->mas7;
624
Marcelo Tosatti46f43c62009-06-18 11:47:27 -0300625 trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
626 gtlbe->mas3, gtlbe->mas7);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600627
628 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
629 if (tlbe_is_host_safe(vcpu, gtlbe)) {
630 switch (tlbsel) {
631 case 0:
632 /* TLB0 */
633 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800634 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600635
636 stlbsel = 0;
637 sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel);
638
639 break;
640
641 case 1:
642 /* TLB1 */
643 eaddr = get_tlb_eaddr(gtlbe);
644 raddr = get_tlb_raddr(gtlbe);
645
646 /* Create a 4KB mapping on the host.
647 * If the guest wanted a large page,
648 * only the first 4KB is mapped here and the rest
649 * are mapped on the fly. */
650 stlbsel = 1;
651 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
652 raddr >> PAGE_SHIFT, gtlbe);
653 break;
654
655 default:
656 BUG();
657 }
658 write_host_tlbe(vcpu_e500, stlbsel, sesel);
659 }
660
Scott Wood49ea0692011-03-28 15:01:24 -0500661 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600662 return EMULATE_DONE;
663}
664
665int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
666{
Alexander Graf666e7252010-07-29 14:47:43 +0200667 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600668
669 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
670}
671
672int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
673{
Alexander Graf666e7252010-07-29 14:47:43 +0200674 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600675
676 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
677}
678
679void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
680{
Alexander Graf666e7252010-07-29 14:47:43 +0200681 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600682
683 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
684}
685
686void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
687{
Alexander Graf666e7252010-07-29 14:47:43 +0200688 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600689
690 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
691}
692
693gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
694 gva_t eaddr)
695{
696 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
697 struct tlbe *gtlbe =
698 &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)];
699 u64 pgmask = get_tlb_bytes(gtlbe) - 1;
700
701 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
702}
703
704void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
705{
706 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
707 int tlbsel, i;
708
709 for (tlbsel = 0; tlbsel < 2; tlbsel++)
710 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++)
711 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i);
712
713 /* discard all guest mapping */
Liu Yu9aa4dd52009-01-14 10:47:38 -0600714 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600715}
716
717void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
718 unsigned int index)
719{
720 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
721 int tlbsel = tlbsel_of(index);
722 int esel = esel_of(index);
723 int stlbsel, sesel;
724
725 switch (tlbsel) {
726 case 0:
727 stlbsel = 0;
728 sesel = esel;
729 break;
730
731 case 1: {
732 gfn_t gfn = gpaddr >> PAGE_SHIFT;
733 struct tlbe *gtlbe
734 = &vcpu_e500->guest_tlb[tlbsel][esel];
735
736 stlbsel = 1;
737 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe);
738 break;
739 }
740
741 default:
742 BUG();
743 break;
744 }
745 write_host_tlbe(vcpu_e500, stlbsel, sesel);
746}
747
748int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
749 gva_t eaddr, unsigned int pid, int as)
750{
751 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
752 int esel, tlbsel;
753
754 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
755 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
756 if (esel >= 0)
757 return index_of(tlbsel, esel);
758 }
759
760 return -1;
761}
762
Scott Wood5ce941e2011-04-27 17:24:21 -0500763void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
764{
765 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
766
767 vcpu_e500->pid[0] = vcpu->arch.shadow_pid =
768 vcpu->arch.pid = pid;
769}
770
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600771void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
772{
773 struct tlbe *tlbe;
774
775 /* Insert large initial mapping for guest. */
776 tlbe = &vcpu_e500->guest_tlb[1][0];
Liu Yu0cfb50e2009-06-05 14:54:29 +0800777 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600778 tlbe->mas2 = 0;
779 tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
780 tlbe->mas7 = 0;
781
782 /* 4K map for serial output. Used by kernel wrapper. */
783 tlbe = &vcpu_e500->guest_tlb[1][1];
Liu Yu0cfb50e2009-06-05 14:54:29 +0800784 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600785 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
786 tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
787 tlbe->mas7 = 0;
788}
789
790int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
791{
792 tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
793
794 vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE;
795 vcpu_e500->guest_tlb[0] =
796 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
797 if (vcpu_e500->guest_tlb[0] == NULL)
798 goto err_out;
799
800 vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE;
801 vcpu_e500->shadow_tlb[0] =
802 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
803 if (vcpu_e500->shadow_tlb[0] == NULL)
804 goto err_out_guest0;
805
806 vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE;
807 vcpu_e500->guest_tlb[1] =
808 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
809 if (vcpu_e500->guest_tlb[1] == NULL)
810 goto err_out_shadow0;
811
812 vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num;
813 vcpu_e500->shadow_tlb[1] =
814 kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL);
815 if (vcpu_e500->shadow_tlb[1] == NULL)
816 goto err_out_guest1;
817
Liu Yuda15bf42010-01-22 19:36:53 +0800818 /* Init TLB configuration register */
819 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
820 vcpu_e500->tlb0cfg |= vcpu_e500->guest_tlb_size[0];
821 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
822 vcpu_e500->tlb1cfg |= vcpu_e500->guest_tlb_size[1];
823
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600824 return 0;
825
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600826err_out_guest1:
827 kfree(vcpu_e500->guest_tlb[1]);
828err_out_shadow0:
829 kfree(vcpu_e500->shadow_tlb[0]);
830err_out_guest0:
831 kfree(vcpu_e500->guest_tlb[0]);
832err_out:
833 return -1;
834}
835
836void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
837{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600838 kfree(vcpu_e500->shadow_tlb[1]);
839 kfree(vcpu_e500->guest_tlb[1]);
840 kfree(vcpu_e500->shadow_tlb[0]);
841 kfree(vcpu_e500->guest_tlb[0]);
842}