blob: 22624a7ae82179876a670119ab3dfe118f6086c6 [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
Liu Yudd9ebf1f2011-06-14 18:35:14 -050031struct id {
32 unsigned long val;
33 struct id **pentry;
34};
35
36#define NUM_TIDS 256
37
38/*
39 * This table provide mappings from:
40 * (guestAS,guestTID,guestPR) --> ID of physical cpu
41 * guestAS [0..1]
42 * guestTID [0..255]
43 * guestPR [0..1]
44 * ID [1..255]
45 * Each vcpu keeps one vcpu_id_table.
46 */
47struct vcpu_id_table {
48 struct id id[2][NUM_TIDS][2];
49};
50
51/*
52 * This table provide reversed mappings of vcpu_id_table:
53 * ID --> address of vcpu_id_table item.
54 * Each physical core has one pcpu_id_table.
55 */
56struct pcpu_id_table {
57 struct id *entry[NUM_TIDS];
58};
59
60static DEFINE_PER_CPU(struct pcpu_id_table, pcpu_sids);
61
62/* This variable keeps last used shadow ID on local core.
63 * The valid range of shadow ID is [1..255] */
64static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid);
65
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060066static unsigned int tlb1_entry_num;
67
Liu Yudd9ebf1f2011-06-14 18:35:14 -050068/*
69 * Allocate a free shadow id and setup a valid sid mapping in given entry.
70 * A mapping is only valid when vcpu_id_table and pcpu_id_table are match.
71 *
72 * The caller must have preemption disabled, and keep it that way until
73 * it has finished with the returned shadow id (either written into the
74 * TLB or arch.shadow_pid, or discarded).
75 */
76static inline int local_sid_setup_one(struct id *entry)
77{
78 unsigned long sid;
79 int ret = -1;
80
81 sid = ++(__get_cpu_var(pcpu_last_used_sid));
82 if (sid < NUM_TIDS) {
83 __get_cpu_var(pcpu_sids).entry[sid] = entry;
84 entry->val = sid;
85 entry->pentry = &__get_cpu_var(pcpu_sids).entry[sid];
86 ret = sid;
87 }
88
89 /*
90 * If sid == NUM_TIDS, we've run out of sids. We return -1, and
91 * the caller will invalidate everything and start over.
92 *
93 * sid > NUM_TIDS indicates a race, which we disable preemption to
94 * avoid.
95 */
96 WARN_ON(sid > NUM_TIDS);
97
98 return ret;
99}
100
101/*
102 * Check if given entry contain a valid shadow id mapping.
103 * An ID mapping is considered valid only if
104 * both vcpu and pcpu know this mapping.
105 *
106 * The caller must have preemption disabled, and keep it that way until
107 * it has finished with the returned shadow id (either written into the
108 * TLB or arch.shadow_pid, or discarded).
109 */
110static inline int local_sid_lookup(struct id *entry)
111{
112 if (entry && entry->val != 0 &&
113 __get_cpu_var(pcpu_sids).entry[entry->val] == entry &&
114 entry->pentry == &__get_cpu_var(pcpu_sids).entry[entry->val])
115 return entry->val;
116 return -1;
117}
118
119/* Invalidate all id mappings on local core */
120static inline void local_sid_destroy_all(void)
121{
122 preempt_disable();
123 __get_cpu_var(pcpu_last_used_sid) = 0;
124 memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids)));
125 preempt_enable();
126}
127
128static void *kvmppc_e500_id_table_alloc(struct kvmppc_vcpu_e500 *vcpu_e500)
129{
130 vcpu_e500->idt = kzalloc(sizeof(struct vcpu_id_table), GFP_KERNEL);
131 return vcpu_e500->idt;
132}
133
134static void kvmppc_e500_id_table_free(struct kvmppc_vcpu_e500 *vcpu_e500)
135{
136 kfree(vcpu_e500->idt);
137}
138
139/* Invalidate all mappings on vcpu */
140static void kvmppc_e500_id_table_reset_all(struct kvmppc_vcpu_e500 *vcpu_e500)
141{
142 memset(vcpu_e500->idt, 0, sizeof(struct vcpu_id_table));
143
144 /* Update shadow pid when mappings are changed */
145 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
146}
147
148/* Invalidate one ID mapping on vcpu */
149static inline void kvmppc_e500_id_table_reset_one(
150 struct kvmppc_vcpu_e500 *vcpu_e500,
151 int as, int pid, int pr)
152{
153 struct vcpu_id_table *idt = vcpu_e500->idt;
154
155 BUG_ON(as >= 2);
156 BUG_ON(pid >= NUM_TIDS);
157 BUG_ON(pr >= 2);
158
159 idt->id[as][pid][pr].val = 0;
160 idt->id[as][pid][pr].pentry = NULL;
161
162 /* Update shadow pid when mappings are changed */
163 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
164}
165
166/*
167 * Map guest (vcpu,AS,ID,PR) to physical core shadow id.
168 * This function first lookup if a valid mapping exists,
169 * if not, then creates a new one.
170 *
171 * The caller must have preemption disabled, and keep it that way until
172 * it has finished with the returned shadow id (either written into the
173 * TLB or arch.shadow_pid, or discarded).
174 */
175static unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500,
176 unsigned int as, unsigned int gid,
177 unsigned int pr, int avoid_recursion)
178{
179 struct vcpu_id_table *idt = vcpu_e500->idt;
180 int sid;
181
182 BUG_ON(as >= 2);
183 BUG_ON(gid >= NUM_TIDS);
184 BUG_ON(pr >= 2);
185
186 sid = local_sid_lookup(&idt->id[as][gid][pr]);
187
188 while (sid <= 0) {
189 /* No mapping yet */
190 sid = local_sid_setup_one(&idt->id[as][gid][pr]);
191 if (sid <= 0) {
192 _tlbil_all();
193 local_sid_destroy_all();
194 }
195
196 /* Update shadow pid when mappings are changed */
197 if (!avoid_recursion)
198 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
199 }
200
201 return sid;
202}
203
204/* Map guest pid to shadow.
205 * We use PID to keep shadow of current guest non-zero PID,
206 * and use PID1 to keep shadow of guest zero PID.
207 * So that guest tlbe with TID=0 can be accessed at any time */
208void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *vcpu_e500)
209{
210 preempt_disable();
211 vcpu_e500->vcpu.arch.shadow_pid = kvmppc_e500_get_sid(vcpu_e500,
212 get_cur_as(&vcpu_e500->vcpu),
213 get_cur_pid(&vcpu_e500->vcpu),
214 get_cur_pr(&vcpu_e500->vcpu), 1);
215 vcpu_e500->vcpu.arch.shadow_pid1 = kvmppc_e500_get_sid(vcpu_e500,
216 get_cur_as(&vcpu_e500->vcpu), 0,
217 get_cur_pr(&vcpu_e500->vcpu), 1);
218 preempt_enable();
219}
220
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600221void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
222{
223 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
224 struct tlbe *tlbe;
225 int i, tlbsel;
226
227 printk("| %8s | %8s | %8s | %8s | %8s |\n",
228 "nr", "mas1", "mas2", "mas3", "mas7");
229
230 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
231 printk("Guest TLB%d:\n", tlbsel);
Liu Yu08b7fa92011-06-14 18:34:59 -0500232 for (i = 0; i < vcpu_e500->gtlb_size[tlbsel]; i++) {
233 tlbe = &vcpu_e500->gtlb_arch[tlbsel][i];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600234 if (tlbe->mas1 & MAS1_VALID)
235 printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
236 tlbsel, i, tlbe->mas1, tlbe->mas2,
237 tlbe->mas3, tlbe->mas7);
238 }
239 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600240}
241
242static inline unsigned int tlb0_get_next_victim(
243 struct kvmppc_vcpu_e500 *vcpu_e500)
244{
245 unsigned int victim;
246
Liu Yu08b7fa92011-06-14 18:34:59 -0500247 victim = vcpu_e500->gtlb_nv[0]++;
248 if (unlikely(vcpu_e500->gtlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
249 vcpu_e500->gtlb_nv[0] = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600250
251 return victim;
252}
253
254static inline unsigned int tlb1_max_shadow_size(void)
255{
Scott Wooda4cd8b22011-06-14 18:34:41 -0500256 /* reserve one entry for magic page */
257 return tlb1_entry_num - tlbcam_index - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600258}
259
260static inline int tlbe_is_writable(struct tlbe *tlbe)
261{
262 return tlbe->mas3 & (MAS3_SW|MAS3_UW);
263}
264
265static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
266{
267 /* Mask off reserved bits. */
268 mas3 &= MAS3_ATTRIB_MASK;
269
270 if (!usermode) {
271 /* Guest is in supervisor mode,
272 * so we need to translate guest
273 * supervisor permissions into user permissions. */
274 mas3 &= ~E500_TLB_USER_PERM_MASK;
275 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
276 }
277
278 return mas3 | E500_TLB_SUPER_PERM_MASK;
279}
280
281static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
282{
Liu Yu046a48b2009-03-17 16:57:46 +0800283#ifdef CONFIG_SMP
284 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
285#else
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600286 return mas2 & MAS2_ATTRIB_MASK;
Liu Yu046a48b2009-03-17 16:57:46 +0800287#endif
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600288}
289
290/*
291 * writing shadow tlb entry to host TLB
292 */
Scott Wood0ef30992011-06-14 18:34:35 -0500293static inline void __write_host_tlbe(struct tlbe *stlbe, uint32_t mas0)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600294{
Scott Wood0ef30992011-06-14 18:34:35 -0500295 unsigned long flags;
296
297 local_irq_save(flags);
298 mtspr(SPRN_MAS0, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600299 mtspr(SPRN_MAS1, stlbe->mas1);
300 mtspr(SPRN_MAS2, stlbe->mas2);
301 mtspr(SPRN_MAS3, stlbe->mas3);
302 mtspr(SPRN_MAS7, stlbe->mas7);
Scott Wood0ef30992011-06-14 18:34:35 -0500303 asm volatile("isync; tlbwe" : : : "memory");
304 local_irq_restore(flags);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600305}
306
307static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
Liu Yu08b7fa92011-06-14 18:34:59 -0500308 int tlbsel, int esel, struct tlbe *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600309{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600310 if (tlbsel == 0) {
Scott Wood0ef30992011-06-14 18:34:35 -0500311 __write_host_tlbe(stlbe,
312 MAS0_TLBSEL(0) |
313 MAS0_ESEL(esel & (KVM_E500_TLB0_WAY_NUM - 1)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600314 } else {
Scott Wood0ef30992011-06-14 18:34:35 -0500315 __write_host_tlbe(stlbe,
316 MAS0_TLBSEL(1) |
317 MAS0_ESEL(to_htlb1_esel(esel)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600318 }
Liu Yu08b7fa92011-06-14 18:34:59 -0500319 trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
320 stlbe->mas3, stlbe->mas7);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600321}
322
Scott Wooda4cd8b22011-06-14 18:34:41 -0500323void kvmppc_map_magic(struct kvm_vcpu *vcpu)
324{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500325 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooda4cd8b22011-06-14 18:34:41 -0500326 struct tlbe magic;
327 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500328 unsigned int stid;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500329 pfn_t pfn;
330
331 pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
332 get_page(pfn_to_page(pfn));
333
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500334 preempt_disable();
335 stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
336
337 magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
Scott Wooda4cd8b22011-06-14 18:34:41 -0500338 MAS1_TSIZE(BOOK3E_PAGESZ_4K);
339 magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
340 magic.mas3 = (pfn << PAGE_SHIFT) |
341 MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
342 magic.mas7 = pfn >> (32 - PAGE_SHIFT);
343
344 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500345 preempt_enable();
Scott Wooda4cd8b22011-06-14 18:34:41 -0500346}
347
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600348void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
349{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500350 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
351
352 /* Shadow PID may be expired on local core */
353 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600354}
355
356void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
357{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500358}
359
360static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
361 int tlbsel, int esel)
362{
363 struct tlbe *gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
364 struct vcpu_id_table *idt = vcpu_e500->idt;
365 unsigned int pr, tid, ts, pid;
366 u32 val, eaddr;
367 unsigned long flags;
368
369 ts = get_tlb_ts(gtlbe);
370 tid = get_tlb_tid(gtlbe);
371
372 preempt_disable();
373
374 /* One guest ID may be mapped to two shadow IDs */
375 for (pr = 0; pr < 2; pr++) {
376 /*
377 * The shadow PID can have a valid mapping on at most one
378 * host CPU. In the common case, it will be valid on this
379 * CPU, in which case (for TLB0) we do a local invalidation
380 * of the specific address.
381 *
382 * If the shadow PID is not valid on the current host CPU, or
383 * if we're invalidating a TLB1 entry, we invalidate the
384 * entire shadow PID.
385 */
386 if (tlbsel == 1 ||
387 (pid = local_sid_lookup(&idt->id[ts][tid][pr])) <= 0) {
388 kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr);
389 continue;
390 }
391
392 /*
393 * The guest is invalidating a TLB0 entry which is in a PID
394 * that has a valid shadow mapping on this host CPU. We
395 * search host TLB0 to invalidate it's shadow TLB entry,
396 * similar to __tlbil_va except that we need to look in AS1.
397 */
398 val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS;
399 eaddr = get_tlb_eaddr(gtlbe);
400
401 local_irq_save(flags);
402
403 mtspr(SPRN_MAS6, val);
404 asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr));
405 val = mfspr(SPRN_MAS1);
406 if (val & MAS1_VALID) {
407 mtspr(SPRN_MAS1, val & ~MAS1_VALID);
408 asm volatile("tlbwe");
409 }
410
411 local_irq_restore(flags);
412 }
413
414 preempt_enable();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600415}
416
417/* Search the guest TLB for a matching entry. */
418static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
419 gva_t eaddr, int tlbsel, unsigned int pid, int as)
420{
Scott Wood1aee47a2011-06-14 18:35:20 -0500421 int size = vcpu_e500->gtlb_size[tlbsel];
422 int set_base;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600423 int i;
424
Scott Wood1aee47a2011-06-14 18:35:20 -0500425 if (tlbsel == 0) {
426 int mask = size / KVM_E500_TLB0_WAY_NUM - 1;
427 set_base = (eaddr >> PAGE_SHIFT) & mask;
428 set_base *= KVM_E500_TLB0_WAY_NUM;
429 size = KVM_E500_TLB0_WAY_NUM;
430 } else {
431 set_base = 0;
432 }
433
434 for (i = 0; i < size; i++) {
435 struct tlbe *tlbe = &vcpu_e500->gtlb_arch[tlbsel][set_base + i];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600436 unsigned int tid;
437
438 if (eaddr < get_tlb_eaddr(tlbe))
439 continue;
440
441 if (eaddr > get_tlb_end(tlbe))
442 continue;
443
444 tid = get_tlb_tid(tlbe);
445 if (tid && (tid != pid))
446 continue;
447
448 if (!get_tlb_v(tlbe))
449 continue;
450
451 if (get_tlb_ts(tlbe) != as && as != -1)
452 continue;
453
Scott Wood1aee47a2011-06-14 18:35:20 -0500454 return set_base + i;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600455 }
456
457 return -1;
458}
459
Liu Yu08b7fa92011-06-14 18:34:59 -0500460static inline void kvmppc_e500_priv_setup(struct tlbe_priv *priv,
461 struct tlbe *gtlbe,
462 pfn_t pfn)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600463{
Liu Yu08b7fa92011-06-14 18:34:59 -0500464 priv->pfn = pfn;
465 priv->flags = E500_TLB_VALID;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600466
Liu Yu08b7fa92011-06-14 18:34:59 -0500467 if (tlbe_is_writable(gtlbe))
468 priv->flags |= E500_TLB_DIRTY;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600469}
470
Liu Yu08b7fa92011-06-14 18:34:59 -0500471static inline void kvmppc_e500_priv_release(struct tlbe_priv *priv)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600472{
Liu Yu08b7fa92011-06-14 18:34:59 -0500473 if (priv->flags & E500_TLB_VALID) {
474 if (priv->flags & E500_TLB_DIRTY)
475 kvm_release_pfn_dirty(priv->pfn);
476 else
477 kvm_release_pfn_clean(priv->pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600478
Liu Yu08b7fa92011-06-14 18:34:59 -0500479 priv->flags = 0;
480 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600481}
482
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600483static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
484 unsigned int eaddr, int as)
485{
486 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
487 unsigned int victim, pidsel, tsized;
488 int tlbsel;
489
Liu Yufb2838d2009-01-14 10:47:37 -0600490 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600491 tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
492 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
493 pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
Liu Yu0cfb50e2009-06-05 14:54:29 +0800494 tsized = (vcpu_e500->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600495
496 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500497 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600498 vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
499 | MAS1_TID(vcpu_e500->pid[pidsel])
500 | MAS1_TSIZE(tsized);
501 vcpu_e500->mas2 = (eaddr & MAS2_EPN)
502 | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
503 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
504 vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
505 | (get_cur_pid(vcpu) << 16)
506 | (as ? MAS6_SAS : 0);
507 vcpu_e500->mas7 = 0;
508}
509
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500510/* TID must be supplied by the caller */
Liu Yu08b7fa92011-06-14 18:34:59 -0500511static inline void kvmppc_e500_setup_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
512 struct tlbe *gtlbe, int tsize,
513 struct tlbe_priv *priv,
514 u64 gvaddr, struct tlbe *stlbe)
515{
516 pfn_t pfn = priv->pfn;
517
518 /* Force TS=1 IPROT=0 for all guest mappings. */
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500519 stlbe->mas1 = MAS1_TSIZE(tsize) | MAS1_TS | MAS1_VALID;
Liu Yu08b7fa92011-06-14 18:34:59 -0500520 stlbe->mas2 = (gvaddr & MAS2_EPN)
521 | e500_shadow_mas2_attrib(gtlbe->mas2,
522 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
523 stlbe->mas3 = ((pfn << PAGE_SHIFT) & MAS3_RPN)
524 | e500_shadow_mas3_attrib(gtlbe->mas3,
525 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
526 stlbe->mas7 = (pfn >> (32 - PAGE_SHIFT)) & MAS7_RPN;
527}
528
529
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600530static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Liu Yu08b7fa92011-06-14 18:34:59 -0500531 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel,
532 struct tlbe *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600533{
Scott Wood9973d542011-06-14 18:34:39 -0500534 struct kvm_memory_slot *slot;
Scott Wood9973d542011-06-14 18:34:39 -0500535 unsigned long pfn, hva;
536 int pfnmap = 0;
537 int tsize = BOOK3E_PAGESZ_4K;
Liu Yu08b7fa92011-06-14 18:34:59 -0500538 struct tlbe_priv *priv;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600539
Scott Wood59c1f4e2011-06-14 18:34:37 -0500540 /*
541 * Translate guest physical to true physical, acquiring
542 * a page reference if it is normal, non-reserved memory.
Scott Wood9973d542011-06-14 18:34:39 -0500543 *
544 * gfn_to_memslot() must succeed because otherwise we wouldn't
545 * have gotten this far. Eventually we should just pass the slot
546 * pointer through from the first lookup.
Scott Wood59c1f4e2011-06-14 18:34:37 -0500547 */
Scott Wood9973d542011-06-14 18:34:39 -0500548 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
549 hva = gfn_to_hva_memslot(slot, gfn);
550
551 if (tlbsel == 1) {
552 struct vm_area_struct *vma;
553 down_read(&current->mm->mmap_sem);
554
555 vma = find_vma(current->mm, hva);
556 if (vma && hva >= vma->vm_start &&
557 (vma->vm_flags & VM_PFNMAP)) {
558 /*
559 * This VMA is a physically contiguous region (e.g.
560 * /dev/mem) that bypasses normal Linux page
561 * management. Find the overlap between the
562 * vma and the memslot.
563 */
564
565 unsigned long start, end;
566 unsigned long slot_start, slot_end;
567
568 pfnmap = 1;
569
570 start = vma->vm_pgoff;
571 end = start +
572 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
573
574 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
575
576 slot_start = pfn - (gfn - slot->base_gfn);
577 slot_end = slot_start + slot->npages;
578
579 if (start < slot_start)
580 start = slot_start;
581 if (end > slot_end)
582 end = slot_end;
583
584 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
585 MAS1_TSIZE_SHIFT;
586
587 /*
588 * e500 doesn't implement the lowest tsize bit,
589 * or 1K pages.
590 */
591 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
592
593 /*
594 * Now find the largest tsize (up to what the guest
595 * requested) that will cover gfn, stay within the
596 * range, and for which gfn and pfn are mutually
597 * aligned.
598 */
599
600 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
601 unsigned long gfn_start, gfn_end, tsize_pages;
602 tsize_pages = 1 << (tsize - 2);
603
604 gfn_start = gfn & ~(tsize_pages - 1);
605 gfn_end = gfn_start + tsize_pages;
606
607 if (gfn_start + pfn - gfn < start)
608 continue;
609 if (gfn_end + pfn - gfn > end)
610 continue;
611 if ((gfn & (tsize_pages - 1)) !=
612 (pfn & (tsize_pages - 1)))
613 continue;
614
615 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
616 pfn &= ~(tsize_pages - 1);
617 break;
618 }
619 }
620
621 up_read(&current->mm->mmap_sem);
622 }
623
624 if (likely(!pfnmap)) {
625 pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
626 if (is_error_pfn(pfn)) {
627 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
628 (long)gfn);
629 kvm_release_pfn_clean(pfn);
630 return;
631 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600632 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600633
Liu Yu08b7fa92011-06-14 18:34:59 -0500634 /* Drop old priv and setup new one. */
635 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
636 kvmppc_e500_priv_release(priv);
637 kvmppc_e500_priv_setup(priv, gtlbe, pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600638
Liu Yu08b7fa92011-06-14 18:34:59 -0500639 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, priv, gvaddr, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600640}
641
642/* XXX only map the one-one case, for now use TLB0 */
Liu Yu08b7fa92011-06-14 18:34:59 -0500643static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
644 int esel, struct tlbe *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600645{
646 struct tlbe *gtlbe;
647
Liu Yu08b7fa92011-06-14 18:34:59 -0500648 gtlbe = &vcpu_e500->gtlb_arch[0][esel];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600649
650 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
651 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
Liu Yu08b7fa92011-06-14 18:34:59 -0500652 gtlbe, 0, esel, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600653
654 return esel;
655}
656
657/* Caller must ensure that the specified guest TLB entry is safe to insert into
658 * the shadow TLB. */
659/* XXX for both one-one and one-to-many , for now use TLB1 */
660static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Liu Yu08b7fa92011-06-14 18:34:59 -0500661 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, struct tlbe *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600662{
663 unsigned int victim;
664
Liu Yu08b7fa92011-06-14 18:34:59 -0500665 victim = vcpu_e500->gtlb_nv[1]++;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600666
Liu Yu08b7fa92011-06-14 18:34:59 -0500667 if (unlikely(vcpu_e500->gtlb_nv[1] >= tlb1_max_shadow_size()))
668 vcpu_e500->gtlb_nv[1] = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600669
Liu Yu08b7fa92011-06-14 18:34:59 -0500670 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600671
672 return victim;
673}
674
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500675void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600676{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500677 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
678
679 /* Recalc shadow pid since MSR changes */
680 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600681}
682
Liu Yu08b7fa92011-06-14 18:34:59 -0500683static inline int kvmppc_e500_gtlbe_invalidate(
684 struct kvmppc_vcpu_e500 *vcpu_e500,
685 int tlbsel, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600686{
Liu Yu08b7fa92011-06-14 18:34:59 -0500687 struct tlbe *gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600688
689 if (unlikely(get_tlb_iprot(gtlbe)))
690 return -1;
691
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600692 gtlbe->mas1 = 0;
693
694 return 0;
695}
696
Liu Yub0a18352009-02-17 16:52:08 +0800697int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
698{
699 int esel;
700
701 if (value & MMUCSR0_TLB0FI)
Liu Yu08b7fa92011-06-14 18:34:59 -0500702 for (esel = 0; esel < vcpu_e500->gtlb_size[0]; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800703 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
704 if (value & MMUCSR0_TLB1FI)
Liu Yu08b7fa92011-06-14 18:34:59 -0500705 for (esel = 0; esel < vcpu_e500->gtlb_size[1]; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800706 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
707
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500708 /* Invalidate all vcpu id mappings */
709 kvmppc_e500_id_table_reset_all(vcpu_e500);
Liu Yub0a18352009-02-17 16:52:08 +0800710
711 return EMULATE_DONE;
712}
713
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600714int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
715{
716 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
717 unsigned int ia;
718 int esel, tlbsel;
719 gva_t ea;
720
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100721 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600722
723 ia = (ea >> 2) & 0x1;
724
Liu Yufb2838d2009-01-14 10:47:37 -0600725 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600726 tlbsel = (ea >> 3) & 0x1;
727
728 if (ia) {
729 /* invalidate all entries */
Liu Yu08b7fa92011-06-14 18:34:59 -0500730 for (esel = 0; esel < vcpu_e500->gtlb_size[tlbsel]; esel++)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600731 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
732 } else {
733 ea &= 0xfffff000;
734 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
735 get_cur_pid(vcpu), -1);
736 if (esel >= 0)
737 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
738 }
739
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500740 /* Invalidate all vcpu id mappings */
741 kvmppc_e500_id_table_reset_all(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600742
743 return EMULATE_DONE;
744}
745
746int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
747{
748 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
749 int tlbsel, esel;
750 struct tlbe *gtlbe;
751
752 tlbsel = get_tlb_tlbsel(vcpu_e500);
753 esel = get_tlb_esel(vcpu_e500, tlbsel);
754
Liu Yu08b7fa92011-06-14 18:34:59 -0500755 gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
Liu Yubc35cbc2009-03-17 16:57:45 +0800756 vcpu_e500->mas0 &= ~MAS0_NV(~0);
Liu Yu08b7fa92011-06-14 18:34:59 -0500757 vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600758 vcpu_e500->mas1 = gtlbe->mas1;
759 vcpu_e500->mas2 = gtlbe->mas2;
760 vcpu_e500->mas3 = gtlbe->mas3;
761 vcpu_e500->mas7 = gtlbe->mas7;
762
763 return EMULATE_DONE;
764}
765
766int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
767{
768 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
769 int as = !!get_cur_sas(vcpu_e500);
770 unsigned int pid = get_cur_spid(vcpu_e500);
771 int esel, tlbsel;
772 struct tlbe *gtlbe = NULL;
773 gva_t ea;
774
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100775 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600776
777 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
778 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
779 if (esel >= 0) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500780 gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600781 break;
782 }
783 }
784
785 if (gtlbe) {
786 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
Liu Yu08b7fa92011-06-14 18:34:59 -0500787 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600788 vcpu_e500->mas1 = gtlbe->mas1;
789 vcpu_e500->mas2 = gtlbe->mas2;
790 vcpu_e500->mas3 = gtlbe->mas3;
791 vcpu_e500->mas7 = gtlbe->mas7;
792 } else {
793 int victim;
794
Liu Yufb2838d2009-01-14 10:47:37 -0600795 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600796 tlbsel = vcpu_e500->mas4 >> 28 & 0x1;
797 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
798
799 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500800 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600801 vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
802 | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
803 | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
804 vcpu_e500->mas2 &= MAS2_EPN;
805 vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
806 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
807 vcpu_e500->mas7 = 0;
808 }
809
Scott Wood49ea0692011-03-28 15:01:24 -0500810 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600811 return EMULATE_DONE;
812}
813
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500814/* sesel is index into the set, not the whole array */
815static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
816 struct tlbe *gtlbe,
817 struct tlbe *stlbe,
818 int stlbsel, int sesel)
819{
820 int stid;
821
822 preempt_disable();
823 stid = kvmppc_e500_get_sid(vcpu_e500, get_tlb_ts(gtlbe),
824 get_tlb_tid(gtlbe),
825 get_cur_pr(&vcpu_e500->vcpu), 0);
826
827 stlbe->mas1 |= MAS1_TID(stid);
828 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
829 preempt_enable();
830}
831
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600832int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
833{
834 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600835 struct tlbe *gtlbe;
Liu Yu08b7fa92011-06-14 18:34:59 -0500836 int tlbsel, esel;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600837
838 tlbsel = get_tlb_tlbsel(vcpu_e500);
839 esel = get_tlb_esel(vcpu_e500, tlbsel);
840
Liu Yu08b7fa92011-06-14 18:34:59 -0500841 gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600842
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500843 if (get_tlb_v(gtlbe))
844 kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600845
846 gtlbe->mas1 = vcpu_e500->mas1;
847 gtlbe->mas2 = vcpu_e500->mas2;
848 gtlbe->mas3 = vcpu_e500->mas3;
849 gtlbe->mas7 = vcpu_e500->mas7;
850
Marcelo Tosatti46f43c62009-06-18 11:47:27 -0300851 trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2,
852 gtlbe->mas3, gtlbe->mas7);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600853
854 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
855 if (tlbe_is_host_safe(vcpu, gtlbe)) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500856 struct tlbe stlbe;
857 int stlbsel, sesel;
858 u64 eaddr;
859 u64 raddr;
860
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600861 switch (tlbsel) {
862 case 0:
863 /* TLB0 */
864 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800865 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600866
867 stlbsel = 0;
Liu Yu08b7fa92011-06-14 18:34:59 -0500868 sesel = kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600869
870 break;
871
872 case 1:
873 /* TLB1 */
874 eaddr = get_tlb_eaddr(gtlbe);
875 raddr = get_tlb_raddr(gtlbe);
876
877 /* Create a 4KB mapping on the host.
878 * If the guest wanted a large page,
879 * only the first 4KB is mapped here and the rest
880 * are mapped on the fly. */
881 stlbsel = 1;
882 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
Liu Yu08b7fa92011-06-14 18:34:59 -0500883 raddr >> PAGE_SHIFT, gtlbe, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600884 break;
885
886 default:
887 BUG();
888 }
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500889
890 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600891 }
892
Scott Wood49ea0692011-03-28 15:01:24 -0500893 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600894 return EMULATE_DONE;
895}
896
897int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
898{
Alexander Graf666e7252010-07-29 14:47:43 +0200899 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600900
901 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
902}
903
904int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
905{
Alexander Graf666e7252010-07-29 14:47:43 +0200906 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600907
908 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
909}
910
911void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
912{
Alexander Graf666e7252010-07-29 14:47:43 +0200913 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600914
915 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
916}
917
918void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
919{
Alexander Graf666e7252010-07-29 14:47:43 +0200920 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600921
922 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
923}
924
925gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
926 gva_t eaddr)
927{
928 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
929 struct tlbe *gtlbe =
Liu Yu08b7fa92011-06-14 18:34:59 -0500930 &vcpu_e500->gtlb_arch[tlbsel_of(index)][esel_of(index)];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600931 u64 pgmask = get_tlb_bytes(gtlbe) - 1;
932
933 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
934}
935
936void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
937{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600938}
939
940void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
941 unsigned int index)
942{
943 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Liu Yu08b7fa92011-06-14 18:34:59 -0500944 struct tlbe_priv *priv;
945 struct tlbe *gtlbe, stlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600946 int tlbsel = tlbsel_of(index);
947 int esel = esel_of(index);
948 int stlbsel, sesel;
949
Liu Yu08b7fa92011-06-14 18:34:59 -0500950 gtlbe = &vcpu_e500->gtlb_arch[tlbsel][esel];
951
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600952 switch (tlbsel) {
953 case 0:
954 stlbsel = 0;
955 sesel = esel;
Liu Yu08b7fa92011-06-14 18:34:59 -0500956 priv = &vcpu_e500->gtlb_priv[stlbsel][sesel];
957
958 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K,
959 priv, eaddr, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600960 break;
961
962 case 1: {
963 gfn_t gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600964
965 stlbsel = 1;
Liu Yu08b7fa92011-06-14 18:34:59 -0500966 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
967 gtlbe, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600968 break;
969 }
970
971 default:
972 BUG();
973 break;
974 }
Liu Yu08b7fa92011-06-14 18:34:59 -0500975
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500976 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600977}
978
979int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
980 gva_t eaddr, unsigned int pid, int as)
981{
982 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
983 int esel, tlbsel;
984
985 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
986 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
987 if (esel >= 0)
988 return index_of(tlbsel, esel);
989 }
990
991 return -1;
992}
993
Scott Wood5ce941e2011-04-27 17:24:21 -0500994void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
995{
996 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
997
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500998 if (vcpu->arch.pid != pid) {
999 vcpu_e500->pid[0] = vcpu->arch.pid = pid;
1000 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
1001 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001002}
1003
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001004void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
1005{
1006 struct tlbe *tlbe;
1007
1008 /* Insert large initial mapping for guest. */
Liu Yu08b7fa92011-06-14 18:34:59 -05001009 tlbe = &vcpu_e500->gtlb_arch[1][0];
Liu Yu0cfb50e2009-06-05 14:54:29 +08001010 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001011 tlbe->mas2 = 0;
1012 tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
1013 tlbe->mas7 = 0;
1014
1015 /* 4K map for serial output. Used by kernel wrapper. */
Liu Yu08b7fa92011-06-14 18:34:59 -05001016 tlbe = &vcpu_e500->gtlb_arch[1][1];
Liu Yu0cfb50e2009-06-05 14:54:29 +08001017 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001018 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
1019 tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
1020 tlbe->mas7 = 0;
1021}
1022
1023int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1024{
1025 tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
1026
Liu Yu08b7fa92011-06-14 18:34:59 -05001027 vcpu_e500->gtlb_size[0] = KVM_E500_TLB0_SIZE;
1028 vcpu_e500->gtlb_arch[0] =
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001029 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
Liu Yu08b7fa92011-06-14 18:34:59 -05001030 if (vcpu_e500->gtlb_arch[0] == NULL)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001031 goto err_out;
1032
Liu Yu08b7fa92011-06-14 18:34:59 -05001033 vcpu_e500->gtlb_size[1] = KVM_E500_TLB1_SIZE;
1034 vcpu_e500->gtlb_arch[1] =
1035 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
1036 if (vcpu_e500->gtlb_arch[1] == NULL)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001037 goto err_out_guest0;
1038
Liu Yu08b7fa92011-06-14 18:34:59 -05001039 vcpu_e500->gtlb_priv[0] = (struct tlbe_priv *)
1040 kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
1041 if (vcpu_e500->gtlb_priv[0] == NULL)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001042 goto err_out_guest1;
Liu Yu08b7fa92011-06-14 18:34:59 -05001043 vcpu_e500->gtlb_priv[1] = (struct tlbe_priv *)
1044 kzalloc(sizeof(struct tlbe_priv) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
1045
1046 if (vcpu_e500->gtlb_priv[1] == NULL)
1047 goto err_out_priv0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001048
Liu Yudd9ebf1f2011-06-14 18:35:14 -05001049 if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
1050 goto err_out_priv1;
1051
Liu Yuda15bf42010-01-22 19:36:53 +08001052 /* Init TLB configuration register */
1053 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL;
Liu Yu08b7fa92011-06-14 18:34:59 -05001054 vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_size[0];
Liu Yuda15bf42010-01-22 19:36:53 +08001055 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL;
Liu Yu08b7fa92011-06-14 18:34:59 -05001056 vcpu_e500->tlb1cfg |= vcpu_e500->gtlb_size[1];
Liu Yuda15bf42010-01-22 19:36:53 +08001057
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001058 return 0;
1059
Liu Yudd9ebf1f2011-06-14 18:35:14 -05001060err_out_priv1:
1061 kfree(vcpu_e500->gtlb_priv[1]);
Liu Yu08b7fa92011-06-14 18:34:59 -05001062err_out_priv0:
1063 kfree(vcpu_e500->gtlb_priv[0]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001064err_out_guest1:
Liu Yu08b7fa92011-06-14 18:34:59 -05001065 kfree(vcpu_e500->gtlb_arch[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001066err_out_guest0:
Liu Yu08b7fa92011-06-14 18:34:59 -05001067 kfree(vcpu_e500->gtlb_arch[0]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001068err_out:
1069 return -1;
1070}
1071
1072void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1073{
Liu Yu08b7fa92011-06-14 18:34:59 -05001074 int stlbsel, i;
1075
1076 /* release all privs */
1077 for (stlbsel = 0; stlbsel < 2; stlbsel++)
1078 for (i = 0; i < vcpu_e500->gtlb_size[stlbsel]; i++) {
1079 struct tlbe_priv *priv =
1080 &vcpu_e500->gtlb_priv[stlbsel][i];
1081 kvmppc_e500_priv_release(priv);
1082 }
1083
Liu Yudd9ebf1f2011-06-14 18:35:14 -05001084 kvmppc_e500_id_table_free(vcpu_e500);
Liu Yu08b7fa92011-06-14 18:34:59 -05001085 kfree(vcpu_e500->gtlb_arch[1]);
1086 kfree(vcpu_e500->gtlb_arch[0]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001087}