blob: 1746e677bf3711eec1c25ab1b54b3f0fee5d16c7 [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
Scott Wood0164c0f2011-08-18 15:25:18 -050015#include <linux/kernel.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060016#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060018#include <linux/string.h>
19#include <linux/kvm.h>
20#include <linux/kvm_host.h>
21#include <linux/highmem.h>
Scott Wooddc83b8b2011-08-18 15:25:21 -050022#include <linux/log2.h>
23#include <linux/uaccess.h>
24#include <linux/sched.h>
25#include <linux/rwsem.h>
26#include <linux/vmalloc.h>
Alexander Graf95325e62011-09-20 01:31:48 +020027#include <linux/hugetlb.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060028#include <asm/kvm_ppc.h>
29#include <asm/kvm_e500.h>
30
Liu Yu9aa4dd52009-01-14 10:47:38 -060031#include "../mm/mmu_decl.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060032#include "e500_tlb.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030033#include "trace.h"
Scott Wood49ea0692011-03-28 15:01:24 -050034#include "timing.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060035
Scott Wood0164c0f2011-08-18 15:25:18 -050036#define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060037
Liu Yudd9ebf1f2011-06-14 18:35:14 -050038struct id {
39 unsigned long val;
40 struct id **pentry;
41};
42
43#define NUM_TIDS 256
44
45/*
46 * This table provide mappings from:
47 * (guestAS,guestTID,guestPR) --> ID of physical cpu
48 * guestAS [0..1]
49 * guestTID [0..255]
50 * guestPR [0..1]
51 * ID [1..255]
52 * Each vcpu keeps one vcpu_id_table.
53 */
54struct vcpu_id_table {
55 struct id id[2][NUM_TIDS][2];
56};
57
58/*
59 * This table provide reversed mappings of vcpu_id_table:
60 * ID --> address of vcpu_id_table item.
61 * Each physical core has one pcpu_id_table.
62 */
63struct pcpu_id_table {
64 struct id *entry[NUM_TIDS];
65};
66
67static DEFINE_PER_CPU(struct pcpu_id_table, pcpu_sids);
68
69/* This variable keeps last used shadow ID on local core.
70 * The valid range of shadow ID is [1..255] */
71static DEFINE_PER_CPU(unsigned long, pcpu_last_used_sid);
72
Scott Wood0164c0f2011-08-18 15:25:18 -050073static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060074
Scott Wooddc83b8b2011-08-18 15:25:21 -050075static struct kvm_book3e_206_tlb_entry *get_entry(
76 struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel, int entry)
77{
78 int offset = vcpu_e500->gtlb_offset[tlbsel];
79 return &vcpu_e500->gtlb_arch[offset + entry];
80}
81
Liu Yudd9ebf1f2011-06-14 18:35:14 -050082/*
83 * Allocate a free shadow id and setup a valid sid mapping in given entry.
84 * A mapping is only valid when vcpu_id_table and pcpu_id_table are match.
85 *
86 * The caller must have preemption disabled, and keep it that way until
87 * it has finished with the returned shadow id (either written into the
88 * TLB or arch.shadow_pid, or discarded).
89 */
90static inline int local_sid_setup_one(struct id *entry)
91{
92 unsigned long sid;
93 int ret = -1;
94
95 sid = ++(__get_cpu_var(pcpu_last_used_sid));
96 if (sid < NUM_TIDS) {
97 __get_cpu_var(pcpu_sids).entry[sid] = entry;
98 entry->val = sid;
99 entry->pentry = &__get_cpu_var(pcpu_sids).entry[sid];
100 ret = sid;
101 }
102
103 /*
104 * If sid == NUM_TIDS, we've run out of sids. We return -1, and
105 * the caller will invalidate everything and start over.
106 *
107 * sid > NUM_TIDS indicates a race, which we disable preemption to
108 * avoid.
109 */
110 WARN_ON(sid > NUM_TIDS);
111
112 return ret;
113}
114
115/*
116 * Check if given entry contain a valid shadow id mapping.
117 * An ID mapping is considered valid only if
118 * both vcpu and pcpu know this mapping.
119 *
120 * The caller must have preemption disabled, and keep it that way until
121 * it has finished with the returned shadow id (either written into the
122 * TLB or arch.shadow_pid, or discarded).
123 */
124static inline int local_sid_lookup(struct id *entry)
125{
126 if (entry && entry->val != 0 &&
127 __get_cpu_var(pcpu_sids).entry[entry->val] == entry &&
128 entry->pentry == &__get_cpu_var(pcpu_sids).entry[entry->val])
129 return entry->val;
130 return -1;
131}
132
Scott Wood90b92a62011-08-18 15:25:16 -0500133/* Invalidate all id mappings on local core -- call with preempt disabled */
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500134static inline void local_sid_destroy_all(void)
135{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500136 __get_cpu_var(pcpu_last_used_sid) = 0;
137 memset(&__get_cpu_var(pcpu_sids), 0, sizeof(__get_cpu_var(pcpu_sids)));
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500138}
139
140static void *kvmppc_e500_id_table_alloc(struct kvmppc_vcpu_e500 *vcpu_e500)
141{
142 vcpu_e500->idt = kzalloc(sizeof(struct vcpu_id_table), GFP_KERNEL);
143 return vcpu_e500->idt;
144}
145
146static void kvmppc_e500_id_table_free(struct kvmppc_vcpu_e500 *vcpu_e500)
147{
148 kfree(vcpu_e500->idt);
149}
150
151/* Invalidate all mappings on vcpu */
152static void kvmppc_e500_id_table_reset_all(struct kvmppc_vcpu_e500 *vcpu_e500)
153{
154 memset(vcpu_e500->idt, 0, sizeof(struct vcpu_id_table));
155
156 /* Update shadow pid when mappings are changed */
157 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
158}
159
160/* Invalidate one ID mapping on vcpu */
161static inline void kvmppc_e500_id_table_reset_one(
162 struct kvmppc_vcpu_e500 *vcpu_e500,
163 int as, int pid, int pr)
164{
165 struct vcpu_id_table *idt = vcpu_e500->idt;
166
167 BUG_ON(as >= 2);
168 BUG_ON(pid >= NUM_TIDS);
169 BUG_ON(pr >= 2);
170
171 idt->id[as][pid][pr].val = 0;
172 idt->id[as][pid][pr].pentry = NULL;
173
174 /* Update shadow pid when mappings are changed */
175 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
176}
177
178/*
179 * Map guest (vcpu,AS,ID,PR) to physical core shadow id.
180 * This function first lookup if a valid mapping exists,
181 * if not, then creates a new one.
182 *
183 * The caller must have preemption disabled, and keep it that way until
184 * it has finished with the returned shadow id (either written into the
185 * TLB or arch.shadow_pid, or discarded).
186 */
187static unsigned int kvmppc_e500_get_sid(struct kvmppc_vcpu_e500 *vcpu_e500,
188 unsigned int as, unsigned int gid,
189 unsigned int pr, int avoid_recursion)
190{
191 struct vcpu_id_table *idt = vcpu_e500->idt;
192 int sid;
193
194 BUG_ON(as >= 2);
195 BUG_ON(gid >= NUM_TIDS);
196 BUG_ON(pr >= 2);
197
198 sid = local_sid_lookup(&idt->id[as][gid][pr]);
199
200 while (sid <= 0) {
201 /* No mapping yet */
202 sid = local_sid_setup_one(&idt->id[as][gid][pr]);
203 if (sid <= 0) {
204 _tlbil_all();
205 local_sid_destroy_all();
206 }
207
208 /* Update shadow pid when mappings are changed */
209 if (!avoid_recursion)
210 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
211 }
212
213 return sid;
214}
215
216/* Map guest pid to shadow.
217 * We use PID to keep shadow of current guest non-zero PID,
218 * and use PID1 to keep shadow of guest zero PID.
219 * So that guest tlbe with TID=0 can be accessed at any time */
220void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *vcpu_e500)
221{
222 preempt_disable();
223 vcpu_e500->vcpu.arch.shadow_pid = kvmppc_e500_get_sid(vcpu_e500,
224 get_cur_as(&vcpu_e500->vcpu),
225 get_cur_pid(&vcpu_e500->vcpu),
226 get_cur_pr(&vcpu_e500->vcpu), 1);
227 vcpu_e500->vcpu.arch.shadow_pid1 = kvmppc_e500_get_sid(vcpu_e500,
228 get_cur_as(&vcpu_e500->vcpu), 0,
229 get_cur_pr(&vcpu_e500->vcpu), 1);
230 preempt_enable();
231}
232
Scott Wood0164c0f2011-08-18 15:25:18 -0500233static inline unsigned int gtlb0_get_next_victim(
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600234 struct kvmppc_vcpu_e500 *vcpu_e500)
235{
236 unsigned int victim;
237
Liu Yu08b7fa92011-06-14 18:34:59 -0500238 victim = vcpu_e500->gtlb_nv[0]++;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500239 if (unlikely(vcpu_e500->gtlb_nv[0] >= vcpu_e500->gtlb_params[0].ways))
Liu Yu08b7fa92011-06-14 18:34:59 -0500240 vcpu_e500->gtlb_nv[0] = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600241
242 return victim;
243}
244
245static inline unsigned int tlb1_max_shadow_size(void)
246{
Scott Wooda4cd8b22011-06-14 18:34:41 -0500247 /* reserve one entry for magic page */
Scott Wood0164c0f2011-08-18 15:25:18 -0500248 return host_tlb_params[1].entries - tlbcam_index - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600249}
250
Scott Wooddc83b8b2011-08-18 15:25:21 -0500251static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600252{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500253 return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600254}
255
256static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
257{
258 /* Mask off reserved bits. */
259 mas3 &= MAS3_ATTRIB_MASK;
260
261 if (!usermode) {
262 /* Guest is in supervisor mode,
263 * so we need to translate guest
264 * supervisor permissions into user permissions. */
265 mas3 &= ~E500_TLB_USER_PERM_MASK;
266 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
267 }
268
269 return mas3 | E500_TLB_SUPER_PERM_MASK;
270}
271
272static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
273{
Liu Yu046a48b2009-03-17 16:57:46 +0800274#ifdef CONFIG_SMP
275 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
276#else
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600277 return mas2 & MAS2_ATTRIB_MASK;
Liu Yu046a48b2009-03-17 16:57:46 +0800278#endif
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600279}
280
281/*
282 * writing shadow tlb entry to host TLB
283 */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500284static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
285 uint32_t mas0)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600286{
Scott Wood0ef30992011-06-14 18:34:35 -0500287 unsigned long flags;
288
289 local_irq_save(flags);
290 mtspr(SPRN_MAS0, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600291 mtspr(SPRN_MAS1, stlbe->mas1);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500292 mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
293 mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
294 mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
Scott Wood0ef30992011-06-14 18:34:35 -0500295 asm volatile("isync; tlbwe" : : : "memory");
296 local_irq_restore(flags);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600297}
298
Scott Wood57013522011-11-29 10:40:23 +0000299/*
300 * Acquire a mas0 with victim hint, as if we just took a TLB miss.
301 *
302 * We don't care about the address we're searching for, other than that it's
303 * in the right set and is not present in the TLB. Using a zero PID and a
304 * userspace address means we don't have to set and then restore MAS5, or
305 * calculate a proper MAS6 value.
306 */
307static u32 get_host_mas0(unsigned long eaddr)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600308{
Scott Wood57013522011-11-29 10:40:23 +0000309 unsigned long flags;
310 u32 mas0;
311
312 local_irq_save(flags);
313 mtspr(SPRN_MAS6, 0);
314 asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
315 mas0 = mfspr(SPRN_MAS0);
316 local_irq_restore(flags);
317
318 return mas0;
319}
320
321/* sesel is for tlb1 only */
322static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
323 int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
324{
325 u32 mas0;
326
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600327 if (tlbsel == 0) {
Scott Wood57013522011-11-29 10:40:23 +0000328 mas0 = get_host_mas0(stlbe->mas2);
329 __write_host_tlbe(stlbe, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600330 } else {
Scott Wood0ef30992011-06-14 18:34:35 -0500331 __write_host_tlbe(stlbe,
332 MAS0_TLBSEL(1) |
Scott Wood57013522011-11-29 10:40:23 +0000333 MAS0_ESEL(to_htlb1_esel(sesel)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600334 }
Liu Yu08b7fa92011-06-14 18:34:59 -0500335 trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500336 (u32)stlbe->mas7_3, (u32)(stlbe->mas7_3 >> 32));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600337}
338
Scott Wooda4cd8b22011-06-14 18:34:41 -0500339void kvmppc_map_magic(struct kvm_vcpu *vcpu)
340{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500341 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500342 struct kvm_book3e_206_tlb_entry magic;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500343 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500344 unsigned int stid;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500345 pfn_t pfn;
346
347 pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
348 get_page(pfn_to_page(pfn));
349
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500350 preempt_disable();
351 stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
352
353 magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
Scott Wooda4cd8b22011-06-14 18:34:41 -0500354 MAS1_TSIZE(BOOK3E_PAGESZ_4K);
355 magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500356 magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
357 MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500358
359 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500360 preempt_enable();
Scott Wooda4cd8b22011-06-14 18:34:41 -0500361}
362
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600363void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
364{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500365 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
366
367 /* Shadow PID may be expired on local core */
368 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600369}
370
371void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
372{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500373}
374
Scott Wood0164c0f2011-08-18 15:25:18 -0500375static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
376 int tlbsel, int esel)
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500377{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500378 struct kvm_book3e_206_tlb_entry *gtlbe =
379 get_entry(vcpu_e500, tlbsel, esel);
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500380 struct vcpu_id_table *idt = vcpu_e500->idt;
381 unsigned int pr, tid, ts, pid;
382 u32 val, eaddr;
383 unsigned long flags;
384
385 ts = get_tlb_ts(gtlbe);
386 tid = get_tlb_tid(gtlbe);
387
388 preempt_disable();
389
390 /* One guest ID may be mapped to two shadow IDs */
391 for (pr = 0; pr < 2; pr++) {
392 /*
393 * The shadow PID can have a valid mapping on at most one
394 * host CPU. In the common case, it will be valid on this
395 * CPU, in which case (for TLB0) we do a local invalidation
396 * of the specific address.
397 *
398 * If the shadow PID is not valid on the current host CPU, or
399 * if we're invalidating a TLB1 entry, we invalidate the
400 * entire shadow PID.
401 */
402 if (tlbsel == 1 ||
403 (pid = local_sid_lookup(&idt->id[ts][tid][pr])) <= 0) {
404 kvmppc_e500_id_table_reset_one(vcpu_e500, ts, tid, pr);
405 continue;
406 }
407
408 /*
409 * The guest is invalidating a TLB0 entry which is in a PID
410 * that has a valid shadow mapping on this host CPU. We
411 * search host TLB0 to invalidate it's shadow TLB entry,
412 * similar to __tlbil_va except that we need to look in AS1.
413 */
414 val = (pid << MAS6_SPID_SHIFT) | MAS6_SAS;
415 eaddr = get_tlb_eaddr(gtlbe);
416
417 local_irq_save(flags);
418
419 mtspr(SPRN_MAS6, val);
420 asm volatile("tlbsx 0, %[eaddr]" : : [eaddr] "r" (eaddr));
421 val = mfspr(SPRN_MAS1);
422 if (val & MAS1_VALID) {
423 mtspr(SPRN_MAS1, val & ~MAS1_VALID);
424 asm volatile("tlbwe");
425 }
426
427 local_irq_restore(flags);
428 }
429
430 preempt_enable();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600431}
432
Scott Wood0164c0f2011-08-18 15:25:18 -0500433static int tlb0_set_base(gva_t addr, int sets, int ways)
434{
435 int set_base;
436
437 set_base = (addr >> PAGE_SHIFT) & (sets - 1);
438 set_base *= ways;
439
440 return set_base;
441}
442
443static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
444{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500445 return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
446 vcpu_e500->gtlb_params[0].ways);
Scott Wood0164c0f2011-08-18 15:25:18 -0500447}
448
Scott Woodb5904972011-11-08 18:23:30 -0600449static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel)
Scott Wood0164c0f2011-08-18 15:25:18 -0500450{
Scott Woodb5904972011-11-08 18:23:30 -0600451 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
452 int esel = get_tlb_esel_bit(vcpu);
Scott Wood0164c0f2011-08-18 15:25:18 -0500453
454 if (tlbsel == 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500455 esel &= vcpu_e500->gtlb_params[0].ways - 1;
Scott Woodb5904972011-11-08 18:23:30 -0600456 esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2);
Scott Wood0164c0f2011-08-18 15:25:18 -0500457 } else {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500458 esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500459 }
460
461 return esel;
462}
463
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600464/* Search the guest TLB for a matching entry. */
465static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
466 gva_t eaddr, int tlbsel, unsigned int pid, int as)
467{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500468 int size = vcpu_e500->gtlb_params[tlbsel].entries;
469 unsigned int set_base, offset;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600470 int i;
471
Scott Wood1aee47a2011-06-14 18:35:20 -0500472 if (tlbsel == 0) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500473 set_base = gtlb0_set_base(vcpu_e500, eaddr);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500474 size = vcpu_e500->gtlb_params[0].ways;
Scott Wood1aee47a2011-06-14 18:35:20 -0500475 } else {
476 set_base = 0;
477 }
478
Scott Wooddc83b8b2011-08-18 15:25:21 -0500479 offset = vcpu_e500->gtlb_offset[tlbsel];
480
Scott Wood1aee47a2011-06-14 18:35:20 -0500481 for (i = 0; i < size; i++) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500482 struct kvm_book3e_206_tlb_entry *tlbe =
483 &vcpu_e500->gtlb_arch[offset + set_base + i];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600484 unsigned int tid;
485
486 if (eaddr < get_tlb_eaddr(tlbe))
487 continue;
488
489 if (eaddr > get_tlb_end(tlbe))
490 continue;
491
492 tid = get_tlb_tid(tlbe);
493 if (tid && (tid != pid))
494 continue;
495
496 if (!get_tlb_v(tlbe))
497 continue;
498
499 if (get_tlb_ts(tlbe) != as && as != -1)
500 continue;
501
Scott Wood1aee47a2011-06-14 18:35:20 -0500502 return set_base + i;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600503 }
504
505 return -1;
506}
507
Scott Wood0164c0f2011-08-18 15:25:18 -0500508static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500509 struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood0164c0f2011-08-18 15:25:18 -0500510 pfn_t pfn)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600511{
Scott Wood0164c0f2011-08-18 15:25:18 -0500512 ref->pfn = pfn;
513 ref->flags = E500_TLB_VALID;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600514
Liu Yu08b7fa92011-06-14 18:34:59 -0500515 if (tlbe_is_writable(gtlbe))
Scott Wood0164c0f2011-08-18 15:25:18 -0500516 ref->flags |= E500_TLB_DIRTY;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600517}
518
Scott Wood0164c0f2011-08-18 15:25:18 -0500519static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600520{
Scott Wood0164c0f2011-08-18 15:25:18 -0500521 if (ref->flags & E500_TLB_VALID) {
522 if (ref->flags & E500_TLB_DIRTY)
523 kvm_release_pfn_dirty(ref->pfn);
Liu Yu08b7fa92011-06-14 18:34:59 -0500524 else
Scott Wood0164c0f2011-08-18 15:25:18 -0500525 kvm_release_pfn_clean(ref->pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600526
Scott Wood0164c0f2011-08-18 15:25:18 -0500527 ref->flags = 0;
Liu Yu08b7fa92011-06-14 18:34:59 -0500528 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600529}
530
Scott Wood0164c0f2011-08-18 15:25:18 -0500531static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
532{
533 int tlbsel = 0;
534 int i;
535
Scott Wooddc83b8b2011-08-18 15:25:21 -0500536 for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500537 struct tlbe_ref *ref =
538 &vcpu_e500->gtlb_priv[tlbsel][i].ref;
539 kvmppc_e500_ref_release(ref);
540 }
541}
542
543static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
544{
545 int stlbsel = 1;
546 int i;
547
Scott Wooddc83b8b2011-08-18 15:25:21 -0500548 kvmppc_e500_id_table_reset_all(vcpu_e500);
549
Scott Wood0164c0f2011-08-18 15:25:18 -0500550 for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
551 struct tlbe_ref *ref =
552 &vcpu_e500->tlb_refs[stlbsel][i];
553 kvmppc_e500_ref_release(ref);
554 }
555
556 clear_tlb_privs(vcpu_e500);
557}
558
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600559static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
560 unsigned int eaddr, int as)
561{
562 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
563 unsigned int victim, pidsel, tsized;
564 int tlbsel;
565
Liu Yufb2838d2009-01-14 10:47:37 -0600566 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600567 tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500568 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Scott Woodb5904972011-11-08 18:23:30 -0600569 pidsel = (vcpu->arch.shared->mas4 >> 16) & 0xf;
570 tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600571
Scott Woodb5904972011-11-08 18:23:30 -0600572 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500573 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600574 vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600575 | MAS1_TID(vcpu_e500->pid[pidsel])
576 | MAS1_TSIZE(tsized);
Scott Woodb5904972011-11-08 18:23:30 -0600577 vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
578 | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
579 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
580 vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600581 | (get_cur_pid(vcpu) << 16)
582 | (as ? MAS6_SAS : 0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600583}
584
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500585/* TID must be supplied by the caller */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500586static inline void kvmppc_e500_setup_stlbe(
587 struct kvmppc_vcpu_e500 *vcpu_e500,
588 struct kvm_book3e_206_tlb_entry *gtlbe,
589 int tsize, struct tlbe_ref *ref, u64 gvaddr,
590 struct kvm_book3e_206_tlb_entry *stlbe)
Liu Yu08b7fa92011-06-14 18:34:59 -0500591{
Scott Wood0164c0f2011-08-18 15:25:18 -0500592 pfn_t pfn = ref->pfn;
593
594 BUG_ON(!(ref->flags & E500_TLB_VALID));
Liu Yu08b7fa92011-06-14 18:34:59 -0500595
596 /* Force TS=1 IPROT=0 for all guest mappings. */
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500597 stlbe->mas1 = MAS1_TSIZE(tsize) | MAS1_TS | MAS1_VALID;
Liu Yu08b7fa92011-06-14 18:34:59 -0500598 stlbe->mas2 = (gvaddr & MAS2_EPN)
599 | e500_shadow_mas2_attrib(gtlbe->mas2,
600 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500601 stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT)
602 | e500_shadow_mas3_attrib(gtlbe->mas7_3,
Liu Yu08b7fa92011-06-14 18:34:59 -0500603 vcpu_e500->vcpu.arch.shared->msr & MSR_PR);
Liu Yu08b7fa92011-06-14 18:34:59 -0500604}
605
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600606static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500607 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood57013522011-11-29 10:40:23 +0000608 int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500609 struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600610{
Scott Wood9973d542011-06-14 18:34:39 -0500611 struct kvm_memory_slot *slot;
Scott Wood9973d542011-06-14 18:34:39 -0500612 unsigned long pfn, hva;
613 int pfnmap = 0;
614 int tsize = BOOK3E_PAGESZ_4K;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600615
Scott Wood59c1f4e2011-06-14 18:34:37 -0500616 /*
617 * Translate guest physical to true physical, acquiring
618 * a page reference if it is normal, non-reserved memory.
Scott Wood9973d542011-06-14 18:34:39 -0500619 *
620 * gfn_to_memslot() must succeed because otherwise we wouldn't
621 * have gotten this far. Eventually we should just pass the slot
622 * pointer through from the first lookup.
Scott Wood59c1f4e2011-06-14 18:34:37 -0500623 */
Scott Wood9973d542011-06-14 18:34:39 -0500624 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
625 hva = gfn_to_hva_memslot(slot, gfn);
626
627 if (tlbsel == 1) {
628 struct vm_area_struct *vma;
629 down_read(&current->mm->mmap_sem);
630
631 vma = find_vma(current->mm, hva);
632 if (vma && hva >= vma->vm_start &&
633 (vma->vm_flags & VM_PFNMAP)) {
634 /*
635 * This VMA is a physically contiguous region (e.g.
636 * /dev/mem) that bypasses normal Linux page
637 * management. Find the overlap between the
638 * vma and the memslot.
639 */
640
641 unsigned long start, end;
642 unsigned long slot_start, slot_end;
643
644 pfnmap = 1;
645
646 start = vma->vm_pgoff;
647 end = start +
648 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
649
650 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
651
652 slot_start = pfn - (gfn - slot->base_gfn);
653 slot_end = slot_start + slot->npages;
654
655 if (start < slot_start)
656 start = slot_start;
657 if (end > slot_end)
658 end = slot_end;
659
660 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
661 MAS1_TSIZE_SHIFT;
662
663 /*
664 * e500 doesn't implement the lowest tsize bit,
665 * or 1K pages.
666 */
667 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
668
669 /*
670 * Now find the largest tsize (up to what the guest
671 * requested) that will cover gfn, stay within the
672 * range, and for which gfn and pfn are mutually
673 * aligned.
674 */
675
676 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
677 unsigned long gfn_start, gfn_end, tsize_pages;
678 tsize_pages = 1 << (tsize - 2);
679
680 gfn_start = gfn & ~(tsize_pages - 1);
681 gfn_end = gfn_start + tsize_pages;
682
683 if (gfn_start + pfn - gfn < start)
684 continue;
685 if (gfn_end + pfn - gfn > end)
686 continue;
687 if ((gfn & (tsize_pages - 1)) !=
688 (pfn & (tsize_pages - 1)))
689 continue;
690
691 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
692 pfn &= ~(tsize_pages - 1);
693 break;
694 }
Alexander Graf95325e62011-09-20 01:31:48 +0200695 } else if (vma && hva >= vma->vm_start &&
696 (vma->vm_flags & VM_HUGETLB)) {
697 unsigned long psize = vma_kernel_pagesize(vma);
698
699 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
700 MAS1_TSIZE_SHIFT;
701
702 /*
703 * Take the largest page size that satisfies both host
704 * and guest mapping
705 */
706 tsize = min(__ilog2(psize) - 10, tsize);
707
708 /*
709 * e500 doesn't implement the lowest tsize bit,
710 * or 1K pages.
711 */
712 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
Scott Wood9973d542011-06-14 18:34:39 -0500713 }
714
715 up_read(&current->mm->mmap_sem);
716 }
717
718 if (likely(!pfnmap)) {
Alexander Graf95325e62011-09-20 01:31:48 +0200719 unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
Scott Wood9973d542011-06-14 18:34:39 -0500720 pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
721 if (is_error_pfn(pfn)) {
722 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
723 (long)gfn);
724 kvm_release_pfn_clean(pfn);
725 return;
726 }
Alexander Graf95325e62011-09-20 01:31:48 +0200727
728 /* Align guest and physical address to page map boundaries */
729 pfn &= ~(tsize_pages - 1);
730 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600731 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600732
Scott Wood0164c0f2011-08-18 15:25:18 -0500733 /* Drop old ref and setup new one. */
734 kvmppc_e500_ref_release(ref);
735 kvmppc_e500_ref_setup(ref, gtlbe, pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600736
Scott Wood0164c0f2011-08-18 15:25:18 -0500737 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, tsize, ref, gvaddr, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600738}
739
740/* XXX only map the one-one case, for now use TLB0 */
Scott Wood57013522011-11-29 10:40:23 +0000741static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
742 int esel,
743 struct kvm_book3e_206_tlb_entry *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600744{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500745 struct kvm_book3e_206_tlb_entry *gtlbe;
Scott Wood0164c0f2011-08-18 15:25:18 -0500746 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600747
Scott Wooddc83b8b2011-08-18 15:25:21 -0500748 gtlbe = get_entry(vcpu_e500, 0, esel);
Scott Wood0164c0f2011-08-18 15:25:18 -0500749 ref = &vcpu_e500->gtlb_priv[0][esel].ref;
750
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600751 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
752 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
Scott Wood57013522011-11-29 10:40:23 +0000753 gtlbe, 0, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600754}
755
756/* Caller must ensure that the specified guest TLB entry is safe to insert into
757 * the shadow TLB. */
758/* XXX for both one-one and one-to-many , for now use TLB1 */
759static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500760 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
761 struct kvm_book3e_206_tlb_entry *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600762{
Scott Wood0164c0f2011-08-18 15:25:18 -0500763 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600764 unsigned int victim;
765
Scott Wood0164c0f2011-08-18 15:25:18 -0500766 victim = vcpu_e500->host_tlb1_nv++;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600767
Scott Wood0164c0f2011-08-18 15:25:18 -0500768 if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
769 vcpu_e500->host_tlb1_nv = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600770
Scott Wood0164c0f2011-08-18 15:25:18 -0500771 ref = &vcpu_e500->tlb_refs[1][victim];
Scott Wood57013522011-11-29 10:40:23 +0000772 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600773
774 return victim;
775}
776
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500777void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600778{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500779 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
780
781 /* Recalc shadow pid since MSR changes */
782 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600783}
784
Liu Yu08b7fa92011-06-14 18:34:59 -0500785static inline int kvmppc_e500_gtlbe_invalidate(
786 struct kvmppc_vcpu_e500 *vcpu_e500,
787 int tlbsel, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600788{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500789 struct kvm_book3e_206_tlb_entry *gtlbe =
790 get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600791
792 if (unlikely(get_tlb_iprot(gtlbe)))
793 return -1;
794
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600795 gtlbe->mas1 = 0;
796
797 return 0;
798}
799
Liu Yub0a18352009-02-17 16:52:08 +0800800int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
801{
802 int esel;
803
804 if (value & MMUCSR0_TLB0FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500805 for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800806 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
807 if (value & MMUCSR0_TLB1FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500808 for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800809 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
810
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500811 /* Invalidate all vcpu id mappings */
812 kvmppc_e500_id_table_reset_all(vcpu_e500);
Liu Yub0a18352009-02-17 16:52:08 +0800813
814 return EMULATE_DONE;
815}
816
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600817int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
818{
819 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
820 unsigned int ia;
821 int esel, tlbsel;
822 gva_t ea;
823
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100824 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600825
826 ia = (ea >> 2) & 0x1;
827
Liu Yufb2838d2009-01-14 10:47:37 -0600828 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600829 tlbsel = (ea >> 3) & 0x1;
830
831 if (ia) {
832 /* invalidate all entries */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500833 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
834 esel++)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600835 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
836 } else {
837 ea &= 0xfffff000;
838 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
839 get_cur_pid(vcpu), -1);
840 if (esel >= 0)
841 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
842 }
843
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500844 /* Invalidate all vcpu id mappings */
845 kvmppc_e500_id_table_reset_all(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600846
847 return EMULATE_DONE;
848}
849
850int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
851{
852 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
853 int tlbsel, esel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500854 struct kvm_book3e_206_tlb_entry *gtlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600855
Scott Woodb5904972011-11-08 18:23:30 -0600856 tlbsel = get_tlb_tlbsel(vcpu);
857 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600858
Scott Wooddc83b8b2011-08-18 15:25:21 -0500859 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Scott Woodb5904972011-11-08 18:23:30 -0600860 vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
861 vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
862 vcpu->arch.shared->mas1 = gtlbe->mas1;
863 vcpu->arch.shared->mas2 = gtlbe->mas2;
864 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600865
866 return EMULATE_DONE;
867}
868
869int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
870{
871 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Woodb5904972011-11-08 18:23:30 -0600872 int as = !!get_cur_sas(vcpu);
873 unsigned int pid = get_cur_spid(vcpu);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600874 int esel, tlbsel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500875 struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600876 gva_t ea;
877
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100878 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600879
880 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
881 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
882 if (esel >= 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500883 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600884 break;
885 }
886 }
887
888 if (gtlbe) {
Scott Wood303b7c92011-08-18 15:25:23 -0500889 esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
890
Scott Woodb5904972011-11-08 18:23:30 -0600891 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
Liu Yu08b7fa92011-06-14 18:34:59 -0500892 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600893 vcpu->arch.shared->mas1 = gtlbe->mas1;
894 vcpu->arch.shared->mas2 = gtlbe->mas2;
895 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600896 } else {
897 int victim;
898
Liu Yufb2838d2009-01-14 10:47:37 -0600899 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600900 tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500901 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600902
Scott Woodb5904972011-11-08 18:23:30 -0600903 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
904 | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500905 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600906 vcpu->arch.shared->mas1 =
907 (vcpu->arch.shared->mas6 & MAS6_SPID0)
908 | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
909 | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
910 vcpu->arch.shared->mas2 &= MAS2_EPN;
911 vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
912 MAS2_ATTRIB_MASK;
913 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
914 MAS3_U2 | MAS3_U3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600915 }
916
Scott Wood49ea0692011-03-28 15:01:24 -0500917 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600918 return EMULATE_DONE;
919}
920
Scott Wood57013522011-11-29 10:40:23 +0000921/* sesel is for tlb1 only */
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500922static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500923 struct kvm_book3e_206_tlb_entry *gtlbe,
924 struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500925 int stlbsel, int sesel)
926{
927 int stid;
928
929 preempt_disable();
930 stid = kvmppc_e500_get_sid(vcpu_e500, get_tlb_ts(gtlbe),
931 get_tlb_tid(gtlbe),
932 get_cur_pr(&vcpu_e500->vcpu), 0);
933
934 stlbe->mas1 |= MAS1_TID(stid);
935 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
936 preempt_enable();
937}
938
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600939int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
940{
941 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500942 struct kvm_book3e_206_tlb_entry *gtlbe;
Liu Yu08b7fa92011-06-14 18:34:59 -0500943 int tlbsel, esel;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600944
Scott Woodb5904972011-11-08 18:23:30 -0600945 tlbsel = get_tlb_tlbsel(vcpu);
946 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600947
Scott Wooddc83b8b2011-08-18 15:25:21 -0500948 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600949
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500950 if (get_tlb_v(gtlbe))
Scott Wood0164c0f2011-08-18 15:25:18 -0500951 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600952
Scott Woodb5904972011-11-08 18:23:30 -0600953 gtlbe->mas1 = vcpu->arch.shared->mas1;
954 gtlbe->mas2 = vcpu->arch.shared->mas2;
955 gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600956
Scott Woodb5904972011-11-08 18:23:30 -0600957 trace_kvm_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1, gtlbe->mas2,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500958 (u32)gtlbe->mas7_3, (u32)(gtlbe->mas7_3 >> 32));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600959
960 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
961 if (tlbe_is_host_safe(vcpu, gtlbe)) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500962 struct kvm_book3e_206_tlb_entry stlbe;
Liu Yu08b7fa92011-06-14 18:34:59 -0500963 int stlbsel, sesel;
964 u64 eaddr;
965 u64 raddr;
966
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600967 switch (tlbsel) {
968 case 0:
969 /* TLB0 */
970 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800971 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600972
973 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +0000974 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
975 sesel = 0; /* unused */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600976
977 break;
978
979 case 1:
980 /* TLB1 */
981 eaddr = get_tlb_eaddr(gtlbe);
982 raddr = get_tlb_raddr(gtlbe);
983
984 /* Create a 4KB mapping on the host.
985 * If the guest wanted a large page,
986 * only the first 4KB is mapped here and the rest
987 * are mapped on the fly. */
988 stlbsel = 1;
989 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
Liu Yu08b7fa92011-06-14 18:34:59 -0500990 raddr >> PAGE_SHIFT, gtlbe, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600991 break;
992
993 default:
994 BUG();
995 }
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500996
997 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600998 }
999
Scott Wood49ea0692011-03-28 15:01:24 -05001000 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001001 return EMULATE_DONE;
1002}
1003
1004int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
1005{
Alexander Graf666e7252010-07-29 14:47:43 +02001006 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001007
1008 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
1009}
1010
1011int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
1012{
Alexander Graf666e7252010-07-29 14:47:43 +02001013 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001014
1015 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
1016}
1017
1018void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
1019{
Alexander Graf666e7252010-07-29 14:47:43 +02001020 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001021
1022 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
1023}
1024
1025void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
1026{
Alexander Graf666e7252010-07-29 14:47:43 +02001027 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001028
1029 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
1030}
1031
1032gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
1033 gva_t eaddr)
1034{
1035 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001036 struct kvm_book3e_206_tlb_entry *gtlbe;
1037 u64 pgmask;
1038
1039 gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
1040 pgmask = get_tlb_bytes(gtlbe) - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001041
1042 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
1043}
1044
1045void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1046{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001047}
1048
1049void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
1050 unsigned int index)
1051{
1052 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Liu Yu08b7fa92011-06-14 18:34:59 -05001053 struct tlbe_priv *priv;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001054 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001055 int tlbsel = tlbsel_of(index);
1056 int esel = esel_of(index);
1057 int stlbsel, sesel;
1058
Scott Wooddc83b8b2011-08-18 15:25:21 -05001059 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Liu Yu08b7fa92011-06-14 18:34:59 -05001060
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001061 switch (tlbsel) {
1062 case 0:
1063 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +00001064 sesel = 0; /* unused */
Scott Wood0164c0f2011-08-18 15:25:18 -05001065 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
Liu Yu08b7fa92011-06-14 18:34:59 -05001066
1067 kvmppc_e500_setup_stlbe(vcpu_e500, gtlbe, BOOK3E_PAGESZ_4K,
Scott Wood0164c0f2011-08-18 15:25:18 -05001068 &priv->ref, eaddr, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001069 break;
1070
1071 case 1: {
1072 gfn_t gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001073
1074 stlbsel = 1;
Liu Yu08b7fa92011-06-14 18:34:59 -05001075 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
1076 gtlbe, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001077 break;
1078 }
1079
1080 default:
1081 BUG();
1082 break;
1083 }
Liu Yu08b7fa92011-06-14 18:34:59 -05001084
Scott Wood3bf3cdc2011-08-18 15:25:14 -05001085 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001086}
1087
1088int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
1089 gva_t eaddr, unsigned int pid, int as)
1090{
1091 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1092 int esel, tlbsel;
1093
1094 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
1095 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
1096 if (esel >= 0)
1097 return index_of(tlbsel, esel);
1098 }
1099
1100 return -1;
1101}
1102
Scott Wood5ce941e2011-04-27 17:24:21 -05001103void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
1104{
1105 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1106
Liu Yudd9ebf1f2011-06-14 18:35:14 -05001107 if (vcpu->arch.pid != pid) {
1108 vcpu_e500->pid[0] = vcpu->arch.pid = pid;
1109 kvmppc_e500_recalc_shadow_pid(vcpu_e500);
1110 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001111}
1112
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001113void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
1114{
Scott Wooddc83b8b2011-08-18 15:25:21 -05001115 struct kvm_book3e_206_tlb_entry *tlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001116
1117 /* Insert large initial mapping for guest. */
Scott Wooddc83b8b2011-08-18 15:25:21 -05001118 tlbe = get_entry(vcpu_e500, 1, 0);
Liu Yu0cfb50e2009-06-05 14:54:29 +08001119 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001120 tlbe->mas2 = 0;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001121 tlbe->mas7_3 = E500_TLB_SUPER_PERM_MASK;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001122
1123 /* 4K map for serial output. Used by kernel wrapper. */
Scott Wooddc83b8b2011-08-18 15:25:21 -05001124 tlbe = get_entry(vcpu_e500, 1, 1);
Liu Yu0cfb50e2009-06-05 14:54:29 +08001125 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001126 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001127 tlbe->mas7_3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
1128}
1129
1130static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
1131{
1132 int i;
1133
1134 clear_tlb_refs(vcpu_e500);
1135 kfree(vcpu_e500->gtlb_priv[0]);
1136 kfree(vcpu_e500->gtlb_priv[1]);
1137
1138 if (vcpu_e500->shared_tlb_pages) {
1139 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
1140 PAGE_SIZE)));
1141
1142 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
1143 set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
1144 put_page(vcpu_e500->shared_tlb_pages[i]);
1145 }
1146
1147 vcpu_e500->num_shared_tlb_pages = 0;
1148 vcpu_e500->shared_tlb_pages = NULL;
1149 } else {
1150 kfree(vcpu_e500->gtlb_arch);
1151 }
1152
1153 vcpu_e500->gtlb_arch = NULL;
1154}
1155
1156int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
1157 struct kvm_config_tlb *cfg)
1158{
1159 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1160 struct kvm_book3e_206_tlb_params params;
1161 char *virt;
1162 struct page **pages;
1163 struct tlbe_priv *privs[2] = {};
1164 size_t array_len;
1165 u32 sets;
1166 int num_pages, ret, i;
1167
1168 if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
1169 return -EINVAL;
1170
1171 if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
1172 sizeof(params)))
1173 return -EFAULT;
1174
1175 if (params.tlb_sizes[1] > 64)
1176 return -EINVAL;
1177 if (params.tlb_ways[1] != params.tlb_sizes[1])
1178 return -EINVAL;
1179 if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
1180 return -EINVAL;
1181 if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
1182 return -EINVAL;
1183
1184 if (!is_power_of_2(params.tlb_ways[0]))
1185 return -EINVAL;
1186
1187 sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1188 if (!is_power_of_2(sets))
1189 return -EINVAL;
1190
1191 array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1192 array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1193
1194 if (cfg->array_len < array_len)
1195 return -EINVAL;
1196
1197 num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1198 cfg->array / PAGE_SIZE;
1199 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1200 if (!pages)
1201 return -ENOMEM;
1202
1203 ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1204 if (ret < 0)
1205 goto err_pages;
1206
1207 if (ret != num_pages) {
1208 num_pages = ret;
1209 ret = -EFAULT;
1210 goto err_put_page;
1211 }
1212
1213 virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1214 if (!virt)
1215 goto err_put_page;
1216
1217 privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1218 GFP_KERNEL);
1219 privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1220 GFP_KERNEL);
1221
1222 if (!privs[0] || !privs[1])
1223 goto err_put_page;
1224
1225 free_gtlb(vcpu_e500);
1226
1227 vcpu_e500->gtlb_priv[0] = privs[0];
1228 vcpu_e500->gtlb_priv[1] = privs[1];
1229
1230 vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1231 (virt + (cfg->array & (PAGE_SIZE - 1)));
1232
1233 vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1234 vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1235
1236 vcpu_e500->gtlb_offset[0] = 0;
1237 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1238
Scott Wood7b11dc92011-11-28 15:20:02 +00001239 vcpu_e500->tlb0cfg &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001240 if (params.tlb_sizes[0] <= 2048)
1241 vcpu_e500->tlb0cfg |= params.tlb_sizes[0];
Scott Wood7b11dc92011-11-28 15:20:02 +00001242 vcpu_e500->tlb0cfg |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001243
Scott Wood7b11dc92011-11-28 15:20:02 +00001244 vcpu_e500->tlb1cfg &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001245 vcpu_e500->tlb1cfg |= params.tlb_sizes[1];
Scott Wood7b11dc92011-11-28 15:20:02 +00001246 vcpu_e500->tlb1cfg |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001247
1248 vcpu_e500->shared_tlb_pages = pages;
1249 vcpu_e500->num_shared_tlb_pages = num_pages;
1250
1251 vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1252 vcpu_e500->gtlb_params[0].sets = sets;
1253
1254 vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1255 vcpu_e500->gtlb_params[1].sets = 1;
1256
1257 return 0;
1258
1259err_put_page:
1260 kfree(privs[0]);
1261 kfree(privs[1]);
1262
1263 for (i = 0; i < num_pages; i++)
1264 put_page(pages[i]);
1265
1266err_pages:
1267 kfree(pages);
1268 return ret;
1269}
1270
1271int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1272 struct kvm_dirty_tlb *dirty)
1273{
1274 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1275
1276 clear_tlb_refs(vcpu_e500);
1277 return 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001278}
1279
1280int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1281{
Scott Wooddc83b8b2011-08-18 15:25:21 -05001282 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1283 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1284
Scott Wood0164c0f2011-08-18 15:25:18 -05001285 host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1286 host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1287
1288 /*
1289 * This should never happen on real e500 hardware, but is
1290 * architecturally possible -- e.g. in some weird nested
1291 * virtualization case.
1292 */
1293 if (host_tlb_params[0].entries == 0 ||
1294 host_tlb_params[1].entries == 0) {
1295 pr_err("%s: need to know host tlb size\n", __func__);
1296 return -ENODEV;
1297 }
1298
1299 host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1300 TLBnCFG_ASSOC_SHIFT;
1301 host_tlb_params[1].ways = host_tlb_params[1].entries;
1302
1303 if (!is_power_of_2(host_tlb_params[0].entries) ||
1304 !is_power_of_2(host_tlb_params[0].ways) ||
1305 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1306 host_tlb_params[0].ways == 0) {
1307 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1308 __func__, host_tlb_params[0].entries,
1309 host_tlb_params[0].ways);
1310 return -ENODEV;
1311 }
1312
1313 host_tlb_params[0].sets =
1314 host_tlb_params[0].entries / host_tlb_params[0].ways;
1315 host_tlb_params[1].sets = 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001316
Scott Wooddc83b8b2011-08-18 15:25:21 -05001317 vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1318 vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001319
Scott Wooddc83b8b2011-08-18 15:25:21 -05001320 vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1321 vcpu_e500->gtlb_params[0].sets =
1322 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1323
1324 vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1325 vcpu_e500->gtlb_params[1].sets = 1;
1326
1327 vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1328 if (!vcpu_e500->gtlb_arch)
1329 return -ENOMEM;
1330
1331 vcpu_e500->gtlb_offset[0] = 0;
1332 vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001333
Scott Wood0164c0f2011-08-18 15:25:18 -05001334 vcpu_e500->tlb_refs[0] =
1335 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1336 GFP_KERNEL);
1337 if (!vcpu_e500->tlb_refs[0])
1338 goto err;
Liu Yu08b7fa92011-06-14 18:34:59 -05001339
Scott Wood0164c0f2011-08-18 15:25:18 -05001340 vcpu_e500->tlb_refs[1] =
1341 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1342 GFP_KERNEL);
1343 if (!vcpu_e500->tlb_refs[1])
1344 goto err;
1345
Scott Wooddc83b8b2011-08-18 15:25:21 -05001346 vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1347 vcpu_e500->gtlb_params[0].entries,
1348 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001349 if (!vcpu_e500->gtlb_priv[0])
1350 goto err;
1351
Scott Wooddc83b8b2011-08-18 15:25:21 -05001352 vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1353 vcpu_e500->gtlb_params[1].entries,
1354 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001355 if (!vcpu_e500->gtlb_priv[1])
1356 goto err;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001357
Liu Yudd9ebf1f2011-06-14 18:35:14 -05001358 if (kvmppc_e500_id_table_alloc(vcpu_e500) == NULL)
Scott Wood0164c0f2011-08-18 15:25:18 -05001359 goto err;
Liu Yudd9ebf1f2011-06-14 18:35:14 -05001360
Liu Yuda15bf42010-01-22 19:36:53 +08001361 /* Init TLB configuration register */
Scott Wood7b11dc92011-11-28 15:20:02 +00001362 vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) &
1363 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001364 vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[0].entries;
Scott Wood7b11dc92011-11-28 15:20:02 +00001365 vcpu_e500->tlb0cfg |=
1366 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
1367
1368 vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) &
1369 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1370 vcpu_e500->tlb0cfg |= vcpu_e500->gtlb_params[1].entries;
1371 vcpu_e500->tlb0cfg |=
1372 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
Liu Yuda15bf42010-01-22 19:36:53 +08001373
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001374 return 0;
1375
Scott Wood0164c0f2011-08-18 15:25:18 -05001376err:
Scott Wooddc83b8b2011-08-18 15:25:21 -05001377 free_gtlb(vcpu_e500);
Scott Wood0164c0f2011-08-18 15:25:18 -05001378 kfree(vcpu_e500->tlb_refs[0]);
1379 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001380 return -1;
1381}
1382
1383void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1384{
Scott Wooddc83b8b2011-08-18 15:25:21 -05001385 free_gtlb(vcpu_e500);
Liu Yudd9ebf1f2011-06-14 18:35:14 -05001386 kvmppc_e500_id_table_free(vcpu_e500);
Scott Wood0164c0f2011-08-18 15:25:18 -05001387
1388 kfree(vcpu_e500->tlb_refs[0]);
1389 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001390}