blob: ec933209e8af2c479519c84e187bf83d2e5c5435 [file] [log] [blame]
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001/*
2 * Copyright (C) 2008 Freescale Semiconductor, Inc. All rights reserved.
3 *
4 * Author: Yu Liu, yu.liu@freescale.com
5 *
6 * Description:
7 * This file is based on arch/powerpc/kvm/44x_tlb.c,
8 * by Hollis Blanchard <hollisb@us.ibm.com>.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License, version 2, as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/types.h>
16#include <linux/string.h>
17#include <linux/kvm.h>
18#include <linux/kvm_host.h>
19#include <linux/highmem.h>
20#include <asm/kvm_ppc.h>
21#include <asm/kvm_e500.h>
22
Liu Yu9aa4dd52009-01-14 10:47:38 -060023#include "../mm/mmu_decl.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060024#include "e500_tlb.h"
25
26#define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1)
27
28static unsigned int tlb1_entry_num;
29
30void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu)
31{
32 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
33 struct tlbe *tlbe;
34 int i, tlbsel;
35
36 printk("| %8s | %8s | %8s | %8s | %8s |\n",
37 "nr", "mas1", "mas2", "mas3", "mas7");
38
39 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
40 printk("Guest TLB%d:\n", tlbsel);
41 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
42 tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
43 if (tlbe->mas1 & MAS1_VALID)
44 printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
45 tlbsel, i, tlbe->mas1, tlbe->mas2,
46 tlbe->mas3, tlbe->mas7);
47 }
48 }
49
50 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
51 printk("Shadow TLB%d:\n", tlbsel);
52 for (i = 0; i < vcpu_e500->shadow_tlb_size[tlbsel]; i++) {
53 tlbe = &vcpu_e500->shadow_tlb[tlbsel][i];
54 if (tlbe->mas1 & MAS1_VALID)
55 printk(" S[%d][%3d] | %08X | %08X | %08X | %08X |\n",
56 tlbsel, i, tlbe->mas1, tlbe->mas2,
57 tlbe->mas3, tlbe->mas7);
58 }
59 }
60}
61
62static inline unsigned int tlb0_get_next_victim(
63 struct kvmppc_vcpu_e500 *vcpu_e500)
64{
65 unsigned int victim;
66
67 victim = vcpu_e500->guest_tlb_nv[0]++;
68 if (unlikely(vcpu_e500->guest_tlb_nv[0] >= KVM_E500_TLB0_WAY_NUM))
69 vcpu_e500->guest_tlb_nv[0] = 0;
70
71 return victim;
72}
73
74static inline unsigned int tlb1_max_shadow_size(void)
75{
76 return tlb1_entry_num - tlbcam_index;
77}
78
79static inline int tlbe_is_writable(struct tlbe *tlbe)
80{
81 return tlbe->mas3 & (MAS3_SW|MAS3_UW);
82}
83
84static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
85{
86 /* Mask off reserved bits. */
87 mas3 &= MAS3_ATTRIB_MASK;
88
89 if (!usermode) {
90 /* Guest is in supervisor mode,
91 * so we need to translate guest
92 * supervisor permissions into user permissions. */
93 mas3 &= ~E500_TLB_USER_PERM_MASK;
94 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
95 }
96
97 return mas3 | E500_TLB_SUPER_PERM_MASK;
98}
99
100static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
101{
102 return mas2 & MAS2_ATTRIB_MASK;
103}
104
105/*
106 * writing shadow tlb entry to host TLB
107 */
108static inline void __write_host_tlbe(struct tlbe *stlbe)
109{
110 mtspr(SPRN_MAS1, stlbe->mas1);
111 mtspr(SPRN_MAS2, stlbe->mas2);
112 mtspr(SPRN_MAS3, stlbe->mas3);
113 mtspr(SPRN_MAS7, stlbe->mas7);
114 __asm__ __volatile__ ("tlbwe\n" : : );
115}
116
117static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
118 int tlbsel, int esel)
119{
120 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
121
122 local_irq_disable();
123 if (tlbsel == 0) {
124 __write_host_tlbe(stlbe);
125 } else {
126 unsigned register mas0;
127
128 mas0 = mfspr(SPRN_MAS0);
129
130 mtspr(SPRN_MAS0, MAS0_TLBSEL(1) | MAS0_ESEL(to_htlb1_esel(esel)));
131 __write_host_tlbe(stlbe);
132
133 mtspr(SPRN_MAS0, mas0);
134 }
135 local_irq_enable();
136}
137
138void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu)
139{
140 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
141 int i;
142 unsigned register mas0;
143
144 /* Load all valid TLB1 entries to reduce guest tlb miss fault */
145 local_irq_disable();
146 mas0 = mfspr(SPRN_MAS0);
147 for (i = 0; i < tlb1_max_shadow_size(); i++) {
148 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
149
150 if (get_tlb_v(stlbe)) {
151 mtspr(SPRN_MAS0, MAS0_TLBSEL(1)
152 | MAS0_ESEL(to_htlb1_esel(i)));
153 __write_host_tlbe(stlbe);
154 }
155 }
156 mtspr(SPRN_MAS0, mas0);
157 local_irq_enable();
158}
159
160void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu)
161{
Liu Yu9aa4dd52009-01-14 10:47:38 -0600162 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600163}
164
165/* Search the guest TLB for a matching entry. */
166static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
167 gva_t eaddr, int tlbsel, unsigned int pid, int as)
168{
169 int i;
170
171 /* XXX Replace loop with fancy data structures. */
172 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) {
173 struct tlbe *tlbe = &vcpu_e500->guest_tlb[tlbsel][i];
174 unsigned int tid;
175
176 if (eaddr < get_tlb_eaddr(tlbe))
177 continue;
178
179 if (eaddr > get_tlb_end(tlbe))
180 continue;
181
182 tid = get_tlb_tid(tlbe);
183 if (tid && (tid != pid))
184 continue;
185
186 if (!get_tlb_v(tlbe))
187 continue;
188
189 if (get_tlb_ts(tlbe) != as && as != -1)
190 continue;
191
192 return i;
193 }
194
195 return -1;
196}
197
198static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500 *vcpu_e500,
199 int tlbsel, int esel)
200{
201 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
202 struct page *page = vcpu_e500->shadow_pages[tlbsel][esel];
203
204 if (page) {
205 vcpu_e500->shadow_pages[tlbsel][esel] = NULL;
206
207 if (get_tlb_v(stlbe)) {
208 if (tlbe_is_writable(stlbe))
209 kvm_release_page_dirty(page);
210 else
211 kvm_release_page_clean(page);
212 }
213 }
214}
215
216static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
217 int tlbsel, int esel)
218{
219 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
220
221 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
222 stlbe->mas1 = 0;
223 KVMTRACE_5D(STLB_INVAL, &vcpu_e500->vcpu, index_of(tlbsel, esel),
224 stlbe->mas1, stlbe->mas2, stlbe->mas3, stlbe->mas7,
225 handler);
226}
227
228static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
229 gva_t eaddr, gva_t eend, u32 tid)
230{
231 unsigned int pid = tid & 0xff;
232 unsigned int i;
233
234 /* XXX Replace loop with fancy data structures. */
235 for (i = 0; i < vcpu_e500->guest_tlb_size[1]; i++) {
236 struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i];
237 unsigned int tid;
238
239 if (!get_tlb_v(stlbe))
240 continue;
241
242 if (eend < get_tlb_eaddr(stlbe))
243 continue;
244
245 if (eaddr > get_tlb_end(stlbe))
246 continue;
247
248 tid = get_tlb_tid(stlbe);
249 if (tid && (tid != pid))
250 continue;
251
252 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
253 write_host_tlbe(vcpu_e500, 1, i);
254 }
255}
256
257static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
258 unsigned int eaddr, int as)
259{
260 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
261 unsigned int victim, pidsel, tsized;
262 int tlbsel;
263
Liu Yufb2838d2009-01-14 10:47:37 -0600264 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600265 tlbsel = (vcpu_e500->mas4 >> 28) & 0x1;
266 victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0;
267 pidsel = (vcpu_e500->mas4 >> 16) & 0xf;
268 tsized = (vcpu_e500->mas4 >> 8) & 0xf;
269
270 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
271 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
272 vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
273 | MAS1_TID(vcpu_e500->pid[pidsel])
274 | MAS1_TSIZE(tsized);
275 vcpu_e500->mas2 = (eaddr & MAS2_EPN)
276 | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK);
277 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
278 vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1)
279 | (get_cur_pid(vcpu) << 16)
280 | (as ? MAS6_SAS : 0);
281 vcpu_e500->mas7 = 0;
282}
283
284static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
285 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel)
286{
287 struct page *new_page;
288 struct tlbe *stlbe;
289 hpa_t hpaddr;
290
291 stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel];
292
293 /* Get reference to new page. */
294 new_page = gfn_to_page(vcpu_e500->vcpu.kvm, gfn);
295 if (is_error_page(new_page)) {
296 printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn);
297 kvm_release_page_clean(new_page);
298 return;
299 }
300 hpaddr = page_to_phys(new_page);
301
302 /* Drop reference to old page. */
303 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel);
304
305 vcpu_e500->shadow_pages[tlbsel][esel] = new_page;
306
307 /* Force TS=1 IPROT=0 TSIZE=4KB for all guest mappings. */
308 stlbe->mas1 = MAS1_TSIZE(BOOKE_PAGESZ_4K)
309 | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID;
310 stlbe->mas2 = (gvaddr & MAS2_EPN)
311 | e500_shadow_mas2_attrib(gtlbe->mas2,
312 vcpu_e500->vcpu.arch.msr & MSR_PR);
313 stlbe->mas3 = (hpaddr & MAS3_RPN)
314 | e500_shadow_mas3_attrib(gtlbe->mas3,
315 vcpu_e500->vcpu.arch.msr & MSR_PR);
316 stlbe->mas7 = (hpaddr >> 32) & MAS7_RPN;
317
318 KVMTRACE_5D(STLB_WRITE, &vcpu_e500->vcpu, index_of(tlbsel, esel),
319 stlbe->mas1, stlbe->mas2, stlbe->mas3, stlbe->mas7,
320 handler);
321}
322
323/* XXX only map the one-one case, for now use TLB0 */
324static int kvmppc_e500_stlbe_map(struct kvmppc_vcpu_e500 *vcpu_e500,
325 int tlbsel, int esel)
326{
327 struct tlbe *gtlbe;
328
329 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
330
331 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
332 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
333 gtlbe, tlbsel, esel);
334
335 return esel;
336}
337
338/* Caller must ensure that the specified guest TLB entry is safe to insert into
339 * the shadow TLB. */
340/* XXX for both one-one and one-to-many , for now use TLB1 */
341static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
342 u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe)
343{
344 unsigned int victim;
345
346 victim = vcpu_e500->guest_tlb_nv[1]++;
347
348 if (unlikely(vcpu_e500->guest_tlb_nv[1] >= tlb1_max_shadow_size()))
349 vcpu_e500->guest_tlb_nv[1] = 0;
350
351 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim);
352
353 return victim;
354}
355
356/* Invalidate all guest kernel mappings when enter usermode,
357 * so that when they fault back in they will get the
358 * proper permission bits. */
359void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
360{
361 if (usermode) {
362 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
363 int i;
364
365 /* XXX Replace loop with fancy data structures. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600366 for (i = 0; i < tlb1_max_shadow_size(); i++)
367 kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i);
368
Liu Yu9aa4dd52009-01-14 10:47:38 -0600369 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600370 }
371}
372
373static int kvmppc_e500_gtlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500,
374 int tlbsel, int esel)
375{
376 struct tlbe *gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
377
378 if (unlikely(get_tlb_iprot(gtlbe)))
379 return -1;
380
381 if (tlbsel == 1) {
382 kvmppc_e500_tlb1_invalidate(vcpu_e500, get_tlb_eaddr(gtlbe),
383 get_tlb_end(gtlbe),
384 get_tlb_tid(gtlbe));
385 } else {
386 kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel);
387 }
388
389 gtlbe->mas1 = 0;
390
391 return 0;
392}
393
Liu Yub0a18352009-02-17 16:52:08 +0800394int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
395{
396 int esel;
397
398 if (value & MMUCSR0_TLB0FI)
399 for (esel = 0; esel < vcpu_e500->guest_tlb_size[0]; esel++)
400 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
401 if (value & MMUCSR0_TLB1FI)
402 for (esel = 0; esel < vcpu_e500->guest_tlb_size[1]; esel++)
403 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
404
405 _tlbil_all();
406
407 return EMULATE_DONE;
408}
409
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600410int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
411{
412 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
413 unsigned int ia;
414 int esel, tlbsel;
415 gva_t ea;
416
417 ea = ((ra) ? vcpu->arch.gpr[ra] : 0) + vcpu->arch.gpr[rb];
418
419 ia = (ea >> 2) & 0x1;
420
Liu Yufb2838d2009-01-14 10:47:37 -0600421 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600422 tlbsel = (ea >> 3) & 0x1;
423
424 if (ia) {
425 /* invalidate all entries */
426 for (esel = 0; esel < vcpu_e500->guest_tlb_size[tlbsel]; esel++)
427 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
428 } else {
429 ea &= 0xfffff000;
430 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
431 get_cur_pid(vcpu), -1);
432 if (esel >= 0)
433 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
434 }
435
Liu Yu9aa4dd52009-01-14 10:47:38 -0600436 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600437
438 return EMULATE_DONE;
439}
440
441int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
442{
443 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
444 int tlbsel, esel;
445 struct tlbe *gtlbe;
446
447 tlbsel = get_tlb_tlbsel(vcpu_e500);
448 esel = get_tlb_esel(vcpu_e500, tlbsel);
449
450 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
Liu Yubc35cbc2009-03-17 16:57:45 +0800451 vcpu_e500->mas0 &= ~MAS0_NV(~0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600452 vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
453 vcpu_e500->mas1 = gtlbe->mas1;
454 vcpu_e500->mas2 = gtlbe->mas2;
455 vcpu_e500->mas3 = gtlbe->mas3;
456 vcpu_e500->mas7 = gtlbe->mas7;
457
458 return EMULATE_DONE;
459}
460
461int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
462{
463 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
464 int as = !!get_cur_sas(vcpu_e500);
465 unsigned int pid = get_cur_spid(vcpu_e500);
466 int esel, tlbsel;
467 struct tlbe *gtlbe = NULL;
468 gva_t ea;
469
470 ea = vcpu->arch.gpr[rb];
471
472 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
473 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
474 if (esel >= 0) {
475 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
476 break;
477 }
478 }
479
480 if (gtlbe) {
481 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
482 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
483 vcpu_e500->mas1 = gtlbe->mas1;
484 vcpu_e500->mas2 = gtlbe->mas2;
485 vcpu_e500->mas3 = gtlbe->mas3;
486 vcpu_e500->mas7 = gtlbe->mas7;
487 } else {
488 int victim;
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
494 vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
495 | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]);
496 vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0)
497 | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0))
498 | (vcpu_e500->mas4 & MAS4_TSIZED(~0));
499 vcpu_e500->mas2 &= MAS2_EPN;
500 vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK;
501 vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
502 vcpu_e500->mas7 = 0;
503 }
504
505 return EMULATE_DONE;
506}
507
508int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
509{
510 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
511 u64 eaddr;
512 u64 raddr;
513 u32 tid;
514 struct tlbe *gtlbe;
515 int tlbsel, esel, stlbsel, sesel;
516
517 tlbsel = get_tlb_tlbsel(vcpu_e500);
518 esel = get_tlb_esel(vcpu_e500, tlbsel);
519
520 gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel];
521
522 if (get_tlb_v(gtlbe) && tlbsel == 1) {
523 eaddr = get_tlb_eaddr(gtlbe);
524 tid = get_tlb_tid(gtlbe);
525 kvmppc_e500_tlb1_invalidate(vcpu_e500, eaddr,
526 get_tlb_end(gtlbe), tid);
527 }
528
529 gtlbe->mas1 = vcpu_e500->mas1;
530 gtlbe->mas2 = vcpu_e500->mas2;
531 gtlbe->mas3 = vcpu_e500->mas3;
532 gtlbe->mas7 = vcpu_e500->mas7;
533
534 KVMTRACE_5D(GTLB_WRITE, vcpu, vcpu_e500->mas0,
535 gtlbe->mas1, gtlbe->mas2, gtlbe->mas3, gtlbe->mas7,
536 handler);
537
538 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
539 if (tlbe_is_host_safe(vcpu, gtlbe)) {
540 switch (tlbsel) {
541 case 0:
542 /* TLB0 */
543 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
544 gtlbe->mas1 |= MAS1_TSIZE(BOOKE_PAGESZ_4K);
545
546 stlbsel = 0;
547 sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel);
548
549 break;
550
551 case 1:
552 /* TLB1 */
553 eaddr = get_tlb_eaddr(gtlbe);
554 raddr = get_tlb_raddr(gtlbe);
555
556 /* Create a 4KB mapping on the host.
557 * If the guest wanted a large page,
558 * only the first 4KB is mapped here and the rest
559 * are mapped on the fly. */
560 stlbsel = 1;
561 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
562 raddr >> PAGE_SHIFT, gtlbe);
563 break;
564
565 default:
566 BUG();
567 }
568 write_host_tlbe(vcpu_e500, stlbsel, sesel);
569 }
570
571 return EMULATE_DONE;
572}
573
574int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
575{
576 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
577
578 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
579}
580
581int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
582{
583 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
584
585 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
586}
587
588void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
589{
590 unsigned int as = !!(vcpu->arch.msr & MSR_IS);
591
592 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
593}
594
595void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
596{
597 unsigned int as = !!(vcpu->arch.msr & MSR_DS);
598
599 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
600}
601
602gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
603 gva_t eaddr)
604{
605 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
606 struct tlbe *gtlbe =
607 &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)];
608 u64 pgmask = get_tlb_bytes(gtlbe) - 1;
609
610 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
611}
612
613void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
614{
615 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
616 int tlbsel, i;
617
618 for (tlbsel = 0; tlbsel < 2; tlbsel++)
619 for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++)
620 kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i);
621
622 /* discard all guest mapping */
Liu Yu9aa4dd52009-01-14 10:47:38 -0600623 _tlbil_all();
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600624}
625
626void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
627 unsigned int index)
628{
629 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
630 int tlbsel = tlbsel_of(index);
631 int esel = esel_of(index);
632 int stlbsel, sesel;
633
634 switch (tlbsel) {
635 case 0:
636 stlbsel = 0;
637 sesel = esel;
638 break;
639
640 case 1: {
641 gfn_t gfn = gpaddr >> PAGE_SHIFT;
642 struct tlbe *gtlbe
643 = &vcpu_e500->guest_tlb[tlbsel][esel];
644
645 stlbsel = 1;
646 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe);
647 break;
648 }
649
650 default:
651 BUG();
652 break;
653 }
654 write_host_tlbe(vcpu_e500, stlbsel, sesel);
655}
656
657int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
658 gva_t eaddr, unsigned int pid, int as)
659{
660 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
661 int esel, tlbsel;
662
663 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
664 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
665 if (esel >= 0)
666 return index_of(tlbsel, esel);
667 }
668
669 return -1;
670}
671
672void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500)
673{
674 struct tlbe *tlbe;
675
676 /* Insert large initial mapping for guest. */
677 tlbe = &vcpu_e500->guest_tlb[1][0];
678 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOKE_PAGESZ_256M);
679 tlbe->mas2 = 0;
680 tlbe->mas3 = E500_TLB_SUPER_PERM_MASK;
681 tlbe->mas7 = 0;
682
683 /* 4K map for serial output. Used by kernel wrapper. */
684 tlbe = &vcpu_e500->guest_tlb[1][1];
685 tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOKE_PAGESZ_4K);
686 tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G;
687 tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK;
688 tlbe->mas7 = 0;
689}
690
691int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
692{
693 tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF;
694
695 vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE;
696 vcpu_e500->guest_tlb[0] =
697 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
698 if (vcpu_e500->guest_tlb[0] == NULL)
699 goto err_out;
700
701 vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE;
702 vcpu_e500->shadow_tlb[0] =
703 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
704 if (vcpu_e500->shadow_tlb[0] == NULL)
705 goto err_out_guest0;
706
707 vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE;
708 vcpu_e500->guest_tlb[1] =
709 kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL);
710 if (vcpu_e500->guest_tlb[1] == NULL)
711 goto err_out_shadow0;
712
713 vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num;
714 vcpu_e500->shadow_tlb[1] =
715 kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL);
716 if (vcpu_e500->shadow_tlb[1] == NULL)
717 goto err_out_guest1;
718
719 vcpu_e500->shadow_pages[0] = (struct page **)
720 kzalloc(sizeof(struct page *) * KVM_E500_TLB0_SIZE, GFP_KERNEL);
721 if (vcpu_e500->shadow_pages[0] == NULL)
722 goto err_out_shadow1;
723
724 vcpu_e500->shadow_pages[1] = (struct page **)
725 kzalloc(sizeof(struct page *) * tlb1_entry_num, GFP_KERNEL);
726 if (vcpu_e500->shadow_pages[1] == NULL)
727 goto err_out_page0;
728
729 return 0;
730
731err_out_page0:
732 kfree(vcpu_e500->shadow_pages[0]);
733err_out_shadow1:
734 kfree(vcpu_e500->shadow_tlb[1]);
735err_out_guest1:
736 kfree(vcpu_e500->guest_tlb[1]);
737err_out_shadow0:
738 kfree(vcpu_e500->shadow_tlb[0]);
739err_out_guest0:
740 kfree(vcpu_e500->guest_tlb[0]);
741err_out:
742 return -1;
743}
744
745void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
746{
747 kfree(vcpu_e500->shadow_pages[1]);
748 kfree(vcpu_e500->shadow_pages[0]);
749 kfree(vcpu_e500->shadow_tlb[1]);
750 kfree(vcpu_e500->guest_tlb[1]);
751 kfree(vcpu_e500->shadow_tlb[0]);
752 kfree(vcpu_e500->guest_tlb[0]);
753}