blob: 9d1e28d443c4040dcd8bc76347b3e2095c5f884f [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);
Liu Yu08b7fa92011-06-14 18:34:59 -050044 for (i = 0; i < vcpu_e500->gtlb_size[tlbsel]; i++) {
45 tlbe = &vcpu_e500->gtlb_arch[tlbsel][i];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060046 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 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060052}
53
54static inline unsigned int tlb0_get_next_victim(
55 struct kvmppc_vcpu_e500 *vcpu_e500)
56{
57 unsigned int victim;
58
Liu Yu08b7fa92011-06-14 18:34:59 -050059 victim = vcpu_e500->gtlb_nv[0]++;
60 if (unlikely(vcpu_e500->gtlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
61 vcpu_e500->gtlb_nv[0] = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060062
63 return victim;
64}
65
66static inline unsigned int tlb1_max_shadow_size(void)
67{
Scott Wooda4cd8b22011-06-14 18:34:41 -050068 /* reserve one entry for magic page */
69 return tlb1_entry_num - tlbcam_index - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060070}
71
72static inline int tlbe_is_writable(struct tlbe *tlbe)
73{
74 return tlbe->mas3 & (MAS3_SW|MAS3_UW);
75}
76
77static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
78{
79 /* Mask off reserved bits. */
80 mas3 &= MAS3_ATTRIB_MASK;
81
82 if (!usermode) {
83 /* Guest is in supervisor mode,
84 * so we need to translate guest
85 * supervisor permissions into user permissions. */
86 mas3 &= ~E500_TLB_USER_PERM_MASK;
87 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
88 }
89
90 return mas3 | E500_TLB_SUPER_PERM_MASK;
91}
92
93static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
94{
Liu Yu046a48b2009-03-17 16:57:46 +080095#ifdef CONFIG_SMP
96 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
97#else
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060098 return mas2 & MAS2_ATTRIB_MASK;
Liu Yu046a48b2009-03-17 16:57:46 +080099#endif
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600100}
101
102/*
103 * writing shadow tlb entry to host TLB
104 */
Scott Wood0ef3099562011-06-14 18:34:35 -0500105static inline void __write_host_tlbe(struct tlbe *stlbe, uint32_t mas0)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600106{
Scott Wood0ef3099562011-06-14 18:34:35 -0500107 unsigned long flags;
108
109 local_irq_save(flags);
110 mtspr(SPRN_MAS0, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600111 mtspr(SPRN_MAS1, stlbe->mas1);
112 mtspr(SPRN_MAS2, stlbe->mas2);
113 mtspr(SPRN_MAS3, stlbe->mas3);
114 mtspr(SPRN_MAS7, stlbe->mas7);
Scott Wood0ef3099562011-06-14 18:34:35 -0500115 asm volatile("isync; tlbwe" : : : "memory");
116 local_irq_restore(flags);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600117}
118
119static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
Liu Yu08b7fa92011-06-14 18:34:59 -0500120 int tlbsel, int esel, struct tlbe *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600121{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600122 if (tlbsel == 0) {
Scott Wood0ef3099562011-06-14 18:34:35 -0500123 __write_host_tlbe(stlbe,
124 MAS0_TLBSEL(0) |
125 MAS0_ESEL(esel & (KVM_E500_TLB0_WAY_NUM - 1)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600126 } else {
Scott Wood0ef3099562011-06-14 18:34:35 -0500127 __write_host_tlbe(stlbe,
128 MAS0_TLBSEL(1) |
129 MAS0_ESEL(to_htlb1_esel(esel)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600130 }
Liu Yu08b7fa92011-06-14 18:34:59 -0500131 trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
132 stlbe->mas3, stlbe->mas7);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600133}
134
Scott Wooda4cd8b22011-06-14 18:34:41 -0500135void kvmppc_map_magic(struct kvm_vcpu *vcpu)
136{
137 struct tlbe magic;
138 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
139 pfn_t pfn;
140
141 pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
142 get_page(pfn_to_page(pfn));
143
144 magic.mas1 = MAS1_VALID | MAS1_TS |
145 MAS1_TSIZE(BOOK3E_PAGESZ_4K);
146 magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
147 magic.mas3 = (pfn << PAGE_SHIFT) |
148 MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
149 magic.mas7 = pfn >> (32 - PAGE_SHIFT);
150
151 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
152}
153
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600154void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
155{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600156}
157
158void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
159{
Liu Yu9aa4dd52009-01-14 10:47:38 -0600160 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600161}
162
163/* Search the guest TLB for a matching entry. */
164static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
165 gva_t eaddr, int tlbsel, unsigned int pid, int as)
166{
167 int i;
168
169 /* XXX Replace loop with fancy data structures. */
Liu Yu08b7fa92011-06-14 18:34:59 -0500170 for (i = 0; i < vcpu_e500->gtlb_size[tlbsel]; i++) {
171 struct tlbe *tlbe = &vcpu_e500->gtlb_arch[tlbsel][i];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600172 unsigned int tid;
173
174 if (eaddr < get_tlb_eaddr(tlbe))
175 continue;
176
177 if (eaddr > get_tlb_end(tlbe))
178 continue;
179
180 tid = get_tlb_tid(tlbe);
181 if (tid && (tid != pid))
182 continue;
183
184 if (!get_tlb_v(tlbe))
185 continue;
186
187 if (get_tlb_ts(tlbe) != as && as != -1)
188 continue;
189
190 return i;
191 }
192
193 return -1;
194}
195
Liu Yu08b7fa92011-06-14 18:34:59 -0500196static inline void kvmppc_e500_priv_setup(struct tlbe_priv *priv,
197 struct tlbe *gtlbe,
198 pfn_t pfn)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600199{
Liu Yu08b7fa92011-06-14 18:34:59 -0500200 priv->pfn = pfn;
201 priv->flags = E500_TLB_VALID;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600202
Liu Yu08b7fa92011-06-14 18:34:59 -0500203 if (tlbe_is_writable(gtlbe))
204 priv->flags |= E500_TLB_DIRTY;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600205}
206
Liu Yu08b7fa92011-06-14 18:34:59 -0500207static inline void kvmppc_e500_priv_release(struct tlbe_priv *priv)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600208{
Liu Yu08b7fa92011-06-14 18:34:59 -0500209 if (priv->flags & E500_TLB_VALID) {
210 if (priv->flags & E500_TLB_DIRTY)
211 kvm_release_pfn_dirty(priv->pfn);
212 else
213 kvm_release_pfn_clean(priv->pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600214
Liu Yu08b7fa92011-06-14 18:34:59 -0500215 priv->flags = 0;
216 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600217}
218
219static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
Liu Yu08b7fa92011-06-14 18:34:59 -0500220 int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600221{
Liu Yu08b7fa92011-06-14 18:34:59 -0500222 mtspr(SPRN_MMUCSR0, MMUCSR0_TLB1FI);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600223}
224
225static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
226 unsigned int eaddr, int as)
227{
228 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
229 unsigned int victim, pidsel, tsized;
230 int tlbsel;
231
Liu Yufb2838d2009-01-14 10:47:37 -0600232 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600233 tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
234 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
235 pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
Liu Yu0cfb50e2009-06-05 14:54:29 +0800236 tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600237
238 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500239 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600240 vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
241 | MAS1_TID(vcpu_e500->pid[pidsel])
242 | MAS1_TSIZE(tsized);
243 vcpu_e500->mas2 = (eaddr & MAS2_EPN)
244 | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
245 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
246 vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
247 | (get_cur_pid(vcpu) << 16)
248 | (as ? MAS6_SAS : 0);
249 vcpu_e500->mas7 = 0;
250}
251
Liu Yu08b7fa92011-06-14 18:34:59 -0500252static inline void kvmppc_e500_setup_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
253 struct tlbe *gtlbe, int tsize,
254 struct tlbe_priv *priv,
255 u64 gvaddr, struct tlbe *stlbe)
256{
257 pfn_t pfn = priv->pfn;
258
259 /* Force TS=1 IPROT=0 for all guest mappings. */
260 stlbe->mas1 = MAS1_TSIZE(tsize)
261 | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID;
262 stlbe->mas2 = (gvaddr & MAS2_EPN)
263 | e500_shadow_mas2_attrib(gtlbe->mas2,
264 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
265 stlbe->mas3 = ((pfn << PAGE_SHIFT) & MAS3_RPN)
266 | e500_shadow_mas3_attrib(gtlbe->mas3,
267 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
268 stlbe->mas7 = (pfn >> (32 - PAGE_SHIFT)) & MAS7_RPN;
269}
270
271
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600272static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Liu Yu08b7fa92011-06-14 18:34:59 -0500273 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel,
274 struct tlbe *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600275{
Scott Wood9973d542011-06-14 18:34:39 -0500276 struct kvm_memory_slot *slot;
Scott Wood9973d542011-06-14 18:34:39 -0500277 unsigned long pfn, hva;
278 int pfnmap = 0;
279 int tsize = BOOK3E_PAGESZ_4K;
Liu Yu08b7fa92011-06-14 18:34:59 -0500280 struct tlbe_priv *priv;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600281
Scott Wood59c1f4e2011-06-14 18:34:37 -0500282 /*
283 * Translate guest physical to true physical, acquiring
284 * a page reference if it is normal, non-reserved memory.
Scott Wood9973d542011-06-14 18:34:39 -0500285 *
286 * gfn_to_memslot() must succeed because otherwise we wouldn't
287 * have gotten this far. Eventually we should just pass the slot
288 * pointer through from the first lookup.
Scott Wood59c1f4e2011-06-14 18:34:37 -0500289 */
Scott Wood9973d542011-06-14 18:34:39 -0500290 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
291 hva = gfn_to_hva_memslot(slot, gfn);
292
293 if (tlbsel == 1) {
294 struct vm_area_struct *vma;
295 down_read(&current->mm->mmap_sem);
296
297 vma = find_vma(current->mm, hva);
298 if (vma && hva >= vma->vm_start &&
299 (vma->vm_flags & VM_PFNMAP)) {
300 /*
301 * This VMA is a physically contiguous region (e.g.
302 * /dev/mem) that bypasses normal Linux page
303 * management. Find the overlap between the
304 * vma and the memslot.
305 */
306
307 unsigned long start, end;
308 unsigned long slot_start, slot_end;
309
310 pfnmap = 1;
311
312 start = vma->vm_pgoff;
313 end = start +
314 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
315
316 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
317
318 slot_start = pfn - (gfn - slot->base_gfn);
319 slot_end = slot_start + slot->npages;
320
321 if (start < slot_start)
322 start = slot_start;
323 if (end > slot_end)
324 end = slot_end;
325
326 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
327 MAS1_TSIZE_SHIFT;
328
329 /*
330 * e500 doesn't implement the lowest tsize bit,
331 * or 1K pages.
332 */
333 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
334
335 /*
336 * Now find the largest tsize (up to what the guest
337 * requested) that will cover gfn, stay within the
338 * range, and for which gfn and pfn are mutually
339 * aligned.
340 */
341
342 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
343 unsigned long gfn_start, gfn_end, tsize_pages;
344 tsize_pages = 1 << (tsize - 2);
345
346 gfn_start = gfn & ~(tsize_pages - 1);
347 gfn_end = gfn_start + tsize_pages;
348
349 if (gfn_start + pfn - gfn < start)
350 continue;
351 if (gfn_end + pfn - gfn > end)
352 continue;
353 if ((gfn & (tsize_pages - 1)) !=
354 (pfn & (tsize_pages - 1)))
355 continue;
356
357 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
358 pfn &= ~(tsize_pages - 1);
359 break;
360 }
361 }
362
363 up_read(&current->mm->mmap_sem);
364 }
365
366 if (likely(!pfnmap)) {
367 pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
368 if (is_error_pfn(pfn)) {
369 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
370 (long)gfn);
371 kvm_release_pfn_clean(pfn);
372 return;
373 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600374 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600375
Liu Yu08b7fa92011-06-14 18:34:59 -0500376 /* Drop old priv and setup new one. */
377 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
378 kvmppc_e500_priv_release(priv);
379 kvmppc_e500_priv_setup(priv, gtlbe, pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600380
Liu Yu08b7fa92011-06-14 18:34:59 -0500381 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, priv, gvaddr, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600382}
383
384/* XXX only map the one-one case, for now use TLB0 */
Liu Yu08b7fa92011-06-14 18:34:59 -0500385static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
386 int esel, struct tlbe *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600387{
388 struct tlbe *gtlbe;
389
Liu Yu08b7fa92011-06-14 18:34:59 -0500390 gtlbe = &vcpu_e500->gtlb_arch[0][esel];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600391
392 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
393 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
Liu Yu08b7fa92011-06-14 18:34:59 -0500394 gtlbe, 0, esel, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600395
396 return esel;
397}
398
399/* Caller must ensure that the specified guest TLB entry is safe to insert into
400 * the shadow TLB. */
401/* XXX for both one-one and one-to-many , for now use TLB1 */
402static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Liu Yu08b7fa92011-06-14 18:34:59 -0500403 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, struct tlbe *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600404{
405 unsigned int victim;
406
Liu Yu08b7fa92011-06-14 18:34:59 -0500407 victim = vcpu_e500->gtlb_nv[1]++;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600408
Liu Yu08b7fa92011-06-14 18:34:59 -0500409 if (unlikely(vcpu_e500->gtlb_nv[1] >= tlb1_max_shadow_size()))
410 vcpu_e500->gtlb_nv[1] = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600411
Liu Yu08b7fa92011-06-14 18:34:59 -0500412 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600413
414 return victim;
415}
416
417/* Invalidate all guest kernel mappings when enter usermode,
418 * so that when they fault back in they will get the
419 * proper permission bits. */
420void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
421{
422 if (usermode) {
Liu Yu9aa4dd52009-01-14 10:47:38 -0600423 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600424 }
425}
426
Liu Yu08b7fa92011-06-14 18:34:59 -0500427static inline int kvmppc_e500_gtlbe_invalidate(
428 struct kvmppc_vcpu_e500 *vcpu_e500,
429 int tlbsel, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600430{
Liu Yu08b7fa92011-06-14 18:34:59 -0500431 struct tlbe *gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600432
433 if (unlikely(get_tlb_iprot(gtlbe)))
434 return -1;
435
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600436 gtlbe->mas1 = 0;
437
438 return 0;
439}
440
Liu Yub0a18352009-02-17 16:52:08 +0800441int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
442{
443 int esel;
444
445 if (value & MMUCSR0_TLB0FI)
Liu Yu08b7fa92011-06-14 18:34:59 -0500446 for (esel = 0; esel < vcpu_e500->gtlb_size[0]; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800447 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
448 if (value & MMUCSR0_TLB1FI)
Liu Yu08b7fa92011-06-14 18:34:59 -0500449 for (esel = 0; esel < vcpu_e500->gtlb_size[1]; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800450 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
451
452 _tlbil_all();
453
454 return EMULATE_DONE;
455}
456
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600457int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
458{
459 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
460 unsigned int ia;
461 int esel, tlbsel;
462 gva_t ea;
463
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100464 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600465
466 ia = (ea >> 2) & 0x1;
467
Liu Yufb2838d2009-01-14 10:47:37 -0600468 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600469 tlbsel = (ea >> 3) & 0x1;
470
471 if (ia) {
472 /* invalidate all entries */
Liu Yu08b7fa92011-06-14 18:34:59 -0500473 for (esel = 0; esel < vcpu_e500->gtlb_size[tlbsel]; esel++)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600474 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
475 } else {
476 ea &= 0xfffff000;
477 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
478 get_cur_pid(vcpu), -1);
479 if (esel >= 0)
480 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
481 }
482
Liu Yu9aa4dd52009-01-14 10:47:38 -0600483 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600484
485 return EMULATE_DONE;
486}
487
488int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
489{
490 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
491 int tlbsel, esel;
492 struct tlbe *gtlbe;
493
494 tlbsel = get_tlb_tlbsel(vcpu_e500);
495 esel = get_tlb_esel(vcpu_e500, tlbsel);
496
Liu Yu08b7fa92011-06-14 18:34:59 -0500497 gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
Liu Yubc35cbc2009-03-17 16:57:45 +0800498 vcpu_e500->mas0 &= ~MAS0_NV(~0);
Liu Yu08b7fa92011-06-14 18:34:59 -0500499 vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600500 vcpu_e500->mas1 = gtlbe->mas1;
501 vcpu_e500->mas2 = gtlbe->mas2;
502 vcpu_e500->mas3 = gtlbe->mas3;
503 vcpu_e500->mas7 = gtlbe->mas7;
504
505 return EMULATE_DONE;
506}
507
508int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
509{
510 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
511 int as = !!get_cur_sas(vcpu_e500);
512 unsigned int pid = get_cur_spid(vcpu_e500);
513 int esel, tlbsel;
514 struct tlbe *gtlbe = NULL;
515 gva_t ea;
516
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100517 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600518
519 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
520 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
521 if (esel >= 0) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500522 gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600523 break;
524 }
525 }
526
527 if (gtlbe) {
528 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
Liu Yu08b7fa92011-06-14 18:34:59 -0500529 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600530 vcpu_e500->mas1 = gtlbe->mas1;
531 vcpu_e500->mas2 = gtlbe->mas2;
532 vcpu_e500->mas3 = gtlbe->mas3;
533 vcpu_e500->mas7 = gtlbe->mas7;
534 } else {
535 int victim;
536
Liu Yufb2838d2009-01-14 10:47:37 -0600537 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600538 tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
539 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
540
541 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500542 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600543 vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
544 | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
545 | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
546 vcpu_e500->mas2 &= MAS2_EPN;
547 vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
548 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
549 vcpu_e500->mas7 = 0;
550 }
551
Scott Wood49ea0692011-03-28 15:01:24 -0500552 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600553 return EMULATE_DONE;
554}
555
556int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
557{
558 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600559 struct tlbe *gtlbe;
Liu Yu08b7fa92011-06-14 18:34:59 -0500560 int tlbsel, esel;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600561
562 tlbsel = get_tlb_tlbsel(vcpu_e500);
563 esel = get_tlb_esel(vcpu_e500, tlbsel);
564
Liu Yu08b7fa92011-06-14 18:34:59 -0500565 gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600566
Liu Yu08b7fa92011-06-14 18:34:59 -0500567 if (get_tlb_v(gtlbe) && tlbsel == 1)
568 kvmppc_e500_tlb1_invalidate(vcpu_e500, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600569
570 gtlbe->mas1 = vcpu_e500->mas1;
571 gtlbe->mas2 = vcpu_e500->mas2;
572 gtlbe->mas3 = vcpu_e500->mas3;
573 gtlbe->mas7 = vcpu_e500->mas7;
574
Marcelo Tosatti46f43c62009-06-18 11:47:27 -0300575 trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
576 gtlbe->mas3, gtlbe->mas7);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600577
578 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
579 if (tlbe_is_host_safe(vcpu, gtlbe)) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500580 struct tlbe stlbe;
581 int stlbsel, sesel;
582 u64 eaddr;
583 u64 raddr;
584
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600585 switch (tlbsel) {
586 case 0:
587 /* TLB0 */
588 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800589 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600590
591 stlbsel = 0;
Liu Yu08b7fa92011-06-14 18:34:59 -0500592 sesel = kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600593
594 break;
595
596 case 1:
597 /* TLB1 */
598 eaddr = get_tlb_eaddr(gtlbe);
599 raddr = get_tlb_raddr(gtlbe);
600
601 /* Create a 4KB mapping on the host.
602 * If the guest wanted a large page,
603 * only the first 4KB is mapped here and the rest
604 * are mapped on the fly. */
605 stlbsel = 1;
606 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
Liu Yu08b7fa92011-06-14 18:34:59 -0500607 raddr >> PAGE_SHIFT, gtlbe, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600608 break;
609
610 default:
611 BUG();
612 }
Liu Yu08b7fa92011-06-14 18:34:59 -0500613 write_host_tlbe(vcpu_e500, stlbsel, sesel, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600614 }
615
Scott Wood49ea0692011-03-28 15:01:24 -0500616 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600617 return EMULATE_DONE;
618}
619
620int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
621{
Alexander Graf666e7252010-07-29 14:47:43 +0200622 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600623
624 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
625}
626
627int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
628{
Alexander Graf666e7252010-07-29 14:47:43 +0200629 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600630
631 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
632}
633
634void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
635{
Alexander Graf666e7252010-07-29 14:47:43 +0200636 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600637
638 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
639}
640
641void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
642{
Alexander Graf666e7252010-07-29 14:47:43 +0200643 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600644
645 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
646}
647
648gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
649 gva_t eaddr)
650{
651 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
652 struct tlbe *gtlbe =
Liu Yu08b7fa92011-06-14 18:34:59 -0500653 &vcpu_e500->gtlb_arch[tlbsel_of(index)][esel_of(index)];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600654 u64 pgmask = get_tlb_bytes(gtlbe) - 1;
655
656 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
657}
658
659void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
660{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600661}
662
663void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
664 unsigned int index)
665{
666 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Liu Yu08b7fa92011-06-14 18:34:59 -0500667 struct tlbe_priv *priv;
668 struct tlbe *gtlbe, stlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600669 int tlbsel = tlbsel_of(index);
670 int esel = esel_of(index);
671 int stlbsel, sesel;
672
Liu Yu08b7fa92011-06-14 18:34:59 -0500673 gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
674
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600675 switch (tlbsel) {
676 case 0:
677 stlbsel = 0;
678 sesel = esel;
Liu Yu08b7fa92011-06-14 18:34:59 -0500679 priv = &vcpu_e500->gtlb_priv[stlbsel][sesel];
680
681 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K,
682 priv, eaddr, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600683 break;
684
685 case 1: {
686 gfn_t gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600687
688 stlbsel = 1;
Liu Yu08b7fa92011-06-14 18:34:59 -0500689 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
690 gtlbe, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600691 break;
692 }
693
694 default:
695 BUG();
696 break;
697 }
Liu Yu08b7fa92011-06-14 18:34:59 -0500698
699 write_host_tlbe(vcpu_e500, stlbsel, sesel, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600700}
701
702int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
703 gva_t eaddr, unsigned int pid, int as)
704{
705 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
706 int esel, tlbsel;
707
708 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
709 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
710 if (esel >= 0)
711 return index_of(tlbsel, esel);
712 }
713
714 return -1;
715}
716
Scott Wood5ce941e2011-04-27 17:24:21 -0500717void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
718{
719 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
720
721 vcpu_e500->pid[0] = vcpu->arch.shadow_pid =
722 vcpu->arch.pid = pid;
723}
724
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600725void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
726{
727 struct tlbe *tlbe;
728
729 /* Insert large initial mapping for guest. */
Liu Yu08b7fa92011-06-14 18:34:59 -0500730 tlbe = &vcpu_e500->gtlb_arch[1][0];
Liu Yu0cfb50e2009-06-05 14:54:29 +0800731 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600732 tlbe->mas2 = 0;
733 tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
734 tlbe->mas7 = 0;
735
736 /* 4K map for serial output. Used by kernel wrapper. */
Liu Yu08b7fa92011-06-14 18:34:59 -0500737 tlbe = &vcpu_e500->gtlb_arch[1][1];
Liu Yu0cfb50e2009-06-05 14:54:29 +0800738 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600739 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
740 tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
741 tlbe->mas7 = 0;
742}
743
744int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
745{
746 tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
747
Liu Yu08b7fa92011-06-14 18:34:59 -0500748 vcpu_e500->gtlb_size[0] = KVM_E500_TLB0_SIZE;
749 vcpu_e500->gtlb_arch[0] =
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600750 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
Liu Yu08b7fa92011-06-14 18:34:59 -0500751 if (vcpu_e500->gtlb_arch[0] == NULL)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600752 goto err_out;
753
Liu Yu08b7fa92011-06-14 18:34:59 -0500754 vcpu_e500->gtlb_size[1] = KVM_E500_TLB1_SIZE;
755 vcpu_e500->gtlb_arch[1] =
756 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
757 if (vcpu_e500->gtlb_arch[1] == NULL)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600758 goto err_out_guest0;
759
Liu Yu08b7fa92011-06-14 18:34:59 -0500760 vcpu_e500->gtlb_priv[0] = (struct tlbe_priv *)
761 kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
762 if (vcpu_e500->gtlb_priv[0] == NULL)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600763 goto err_out_guest1;
Liu Yu08b7fa92011-06-14 18:34:59 -0500764 vcpu_e500->gtlb_priv[1] = (struct tlbe_priv *)
765 kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
766
767 if (vcpu_e500->gtlb_priv[1] == NULL)
768 goto err_out_priv0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600769
Liu Yuda15bf42010-01-22 19:36:53 +0800770 /* Init TLB configuration register */
771 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
Liu Yu08b7fa92011-06-14 18:34:59 -0500772 vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_size[0];
Liu Yuda15bf42010-01-22 19:36:53 +0800773 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
Liu Yu08b7fa92011-06-14 18:34:59 -0500774 vcpu_e500->tlb1cfg |= vcpu_e500->gtlb_size[1];
Liu Yuda15bf42010-01-22 19:36:53 +0800775
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600776 return 0;
777
Liu Yu08b7fa92011-06-14 18:34:59 -0500778err_out_priv0:
779 kfree(vcpu_e500->gtlb_priv[0]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600780err_out_guest1:
Liu Yu08b7fa92011-06-14 18:34:59 -0500781 kfree(vcpu_e500->gtlb_arch[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600782err_out_guest0:
Liu Yu08b7fa92011-06-14 18:34:59 -0500783 kfree(vcpu_e500->gtlb_arch[0]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600784err_out:
785 return -1;
786}
787
788void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
789{
Liu Yu08b7fa92011-06-14 18:34:59 -0500790 int stlbsel, i;
791
792 /* release all privs */
793 for (stlbsel = 0; stlbsel < 2; stlbsel++)
794 for (i = 0; i < vcpu_e500->gtlb_size[stlbsel]; i++) {
795 struct tlbe_priv *priv =
796 &vcpu_e500->gtlb_priv[stlbsel][i];
797 kvmppc_e500_priv_release(priv);
798 }
799
800 /* discard all guest mapping */
801 _tlbil_all();
802
803 kfree(vcpu_e500->gtlb_arch[1]);
804 kfree(vcpu_e500->gtlb_arch[0]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600805}