blob: c8ce51d03a2fcbdbfb10a09fcf551d5f1c34d7a3 [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
Scott Wood4f802fe2011-12-20 15:34:37 +00005 * Ashish Kalra, ashish.kalra@freescale.com
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06006 *
7 * Description:
8 * This file is based on arch/powerpc/kvm/44x_tlb.c,
9 * by Hollis Blanchard <hollisb@us.ibm.com>.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License, version 2, as
13 * published by the Free Software Foundation.
14 */
15
Scott Wood0164c0f2011-08-18 15:25:18 -050016#include <linux/kernel.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060017#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060019#include <linux/string.h>
20#include <linux/kvm.h>
21#include <linux/kvm_host.h>
22#include <linux/highmem.h>
Scott Wooddc83b8b2011-08-18 15:25:21 -050023#include <linux/log2.h>
24#include <linux/uaccess.h>
25#include <linux/sched.h>
26#include <linux/rwsem.h>
27#include <linux/vmalloc.h>
Alexander Graf95325e62011-09-20 01:31:48 +020028#include <linux/hugetlb.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060029#include <asm/kvm_ppc.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060030
Scott Wood29a5a6f2011-12-20 15:34:29 +000031#include "e500.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030032#include "trace.h"
Scott Wood49ea0692011-03-28 15:01:24 -050033#include "timing.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060034
Scott Wood0164c0f2011-08-18 15:25:18 -050035#define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060036
Scott Wood0164c0f2011-08-18 15:25:18 -050037static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060038
Scott Wood0164c0f2011-08-18 15:25:18 -050039static inline unsigned int gtlb0_get_next_victim(
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060040 struct kvmppc_vcpu_e500 *vcpu_e500)
41{
42 unsigned int victim;
43
Liu Yu08b7fa92011-06-14 18:34:59 -050044 victim = vcpu_e500->gtlb_nv[0]++;
Scott Wooddc83b8b2011-08-18 15:25:21 -050045 if (unlikely(vcpu_e500->gtlb_nv[0] >= vcpu_e500->gtlb_params[0].ways))
Liu Yu08b7fa92011-06-14 18:34:59 -050046 vcpu_e500->gtlb_nv[0] = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060047
48 return victim;
49}
50
51static inline unsigned int tlb1_max_shadow_size(void)
52{
Scott Wooda4cd8b22011-06-14 18:34:41 -050053 /* reserve one entry for magic page */
Scott Wood0164c0f2011-08-18 15:25:18 -050054 return host_tlb_params[1].entries - tlbcam_index - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060055}
56
Scott Wooddc83b8b2011-08-18 15:25:21 -050057static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060058{
Scott Wooddc83b8b2011-08-18 15:25:21 -050059 return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060060}
61
62static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
63{
64 /* Mask off reserved bits. */
65 mas3 &= MAS3_ATTRIB_MASK;
66
67 if (!usermode) {
68 /* Guest is in supervisor mode,
69 * so we need to translate guest
70 * supervisor permissions into user permissions. */
71 mas3 &= ~E500_TLB_USER_PERM_MASK;
72 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
73 }
74
75 return mas3 | E500_TLB_SUPER_PERM_MASK;
76}
77
78static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
79{
Liu Yu046a48b2009-03-17 16:57:46 +080080#ifdef CONFIG_SMP
81 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
82#else
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060083 return mas2 & MAS2_ATTRIB_MASK;
Liu Yu046a48b2009-03-17 16:57:46 +080084#endif
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060085}
86
87/*
88 * writing shadow tlb entry to host TLB
89 */
Scott Wooddc83b8b2011-08-18 15:25:21 -050090static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
91 uint32_t mas0)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060092{
Scott Wood0ef30992011-06-14 18:34:35 -050093 unsigned long flags;
94
95 local_irq_save(flags);
96 mtspr(SPRN_MAS0, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060097 mtspr(SPRN_MAS1, stlbe->mas1);
Scott Wooddc83b8b2011-08-18 15:25:21 -050098 mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
99 mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
100 mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
Scott Wood0ef30992011-06-14 18:34:35 -0500101 asm volatile("isync; tlbwe" : : : "memory");
102 local_irq_restore(flags);
Liu Yud37b1a02011-12-20 14:42:56 +0000103
104 trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
105 stlbe->mas2, stlbe->mas7_3);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600106}
107
Scott Wood57013522011-11-29 10:40:23 +0000108/*
109 * Acquire a mas0 with victim hint, as if we just took a TLB miss.
110 *
111 * We don't care about the address we're searching for, other than that it's
112 * in the right set and is not present in the TLB. Using a zero PID and a
113 * userspace address means we don't have to set and then restore MAS5, or
114 * calculate a proper MAS6 value.
115 */
116static u32 get_host_mas0(unsigned long eaddr)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600117{
Scott Wood57013522011-11-29 10:40:23 +0000118 unsigned long flags;
119 u32 mas0;
120
121 local_irq_save(flags);
122 mtspr(SPRN_MAS6, 0);
123 asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
124 mas0 = mfspr(SPRN_MAS0);
125 local_irq_restore(flags);
126
127 return mas0;
128}
129
130/* sesel is for tlb1 only */
131static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
132 int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
133{
134 u32 mas0;
135
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600136 if (tlbsel == 0) {
Scott Wood57013522011-11-29 10:40:23 +0000137 mas0 = get_host_mas0(stlbe->mas2);
138 __write_host_tlbe(stlbe, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600139 } else {
Scott Wood0ef30992011-06-14 18:34:35 -0500140 __write_host_tlbe(stlbe,
141 MAS0_TLBSEL(1) |
Scott Wood57013522011-11-29 10:40:23 +0000142 MAS0_ESEL(to_htlb1_esel(sesel)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600143 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600144}
145
Scott Wood8fdd21a22011-12-20 15:34:34 +0000146#ifdef CONFIG_KVM_E500
Scott Wooda4cd8b22011-06-14 18:34:41 -0500147void kvmppc_map_magic(struct kvm_vcpu *vcpu)
148{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500149 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500150 struct kvm_book3e_206_tlb_entry magic;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500151 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500152 unsigned int stid;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500153 pfn_t pfn;
154
155 pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
156 get_page(pfn_to_page(pfn));
157
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500158 preempt_disable();
159 stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
160
161 magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
Scott Wooda4cd8b22011-06-14 18:34:41 -0500162 MAS1_TSIZE(BOOK3E_PAGESZ_4K);
163 magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500164 magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
165 MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
Liu Yud37b1a02011-12-20 14:42:56 +0000166 magic.mas8 = 0;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500167
168 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500169 preempt_enable();
Scott Wooda4cd8b22011-06-14 18:34:41 -0500170}
Scott Wood8fdd21a22011-12-20 15:34:34 +0000171#endif
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500172
Scott Wood0164c0f2011-08-18 15:25:18 -0500173static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
174 int tlbsel, int esel)
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500175{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500176 struct kvm_book3e_206_tlb_entry *gtlbe =
177 get_entry(vcpu_e500, tlbsel, esel);
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500178
Scott Wood4f802fe2011-12-20 15:34:37 +0000179 if (tlbsel == 1 &&
180 vcpu_e500->gtlb_priv[1][esel].ref.flags & E500_TLB_BITMAP) {
181 u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
182 int hw_tlb_indx;
183 unsigned long flags;
184
185 local_irq_save(flags);
186 while (tmp) {
187 hw_tlb_indx = __ilog2_u64(tmp & -tmp);
188 mtspr(SPRN_MAS0,
189 MAS0_TLBSEL(1) |
190 MAS0_ESEL(to_htlb1_esel(hw_tlb_indx)));
191 mtspr(SPRN_MAS1, 0);
192 asm volatile("tlbwe");
193 vcpu_e500->h2g_tlb1_rmap[hw_tlb_indx] = 0;
194 tmp &= tmp - 1;
195 }
196 mb();
197 vcpu_e500->g2h_tlb1_map[esel] = 0;
198 vcpu_e500->gtlb_priv[1][esel].ref.flags &= ~E500_TLB_BITMAP;
199 local_irq_restore(flags);
200
Scott Wood8fdd21a22011-12-20 15:34:34 +0000201 return;
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500202 }
203
Scott Wood8fdd21a22011-12-20 15:34:34 +0000204 /* Guest tlbe is backed by at most one host tlbe per shadow pid. */
205 kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600206}
207
Scott Wood0164c0f2011-08-18 15:25:18 -0500208static int tlb0_set_base(gva_t addr, int sets, int ways)
209{
210 int set_base;
211
212 set_base = (addr >> PAGE_SHIFT) & (sets - 1);
213 set_base *= ways;
214
215 return set_base;
216}
217
218static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
219{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500220 return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
221 vcpu_e500->gtlb_params[0].ways);
Scott Wood0164c0f2011-08-18 15:25:18 -0500222}
223
Scott Woodb5904972011-11-08 18:23:30 -0600224static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel)
Scott Wood0164c0f2011-08-18 15:25:18 -0500225{
Scott Woodb5904972011-11-08 18:23:30 -0600226 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
227 int esel = get_tlb_esel_bit(vcpu);
Scott Wood0164c0f2011-08-18 15:25:18 -0500228
229 if (tlbsel == 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500230 esel &= vcpu_e500->gtlb_params[0].ways - 1;
Scott Woodb5904972011-11-08 18:23:30 -0600231 esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2);
Scott Wood0164c0f2011-08-18 15:25:18 -0500232 } else {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500233 esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500234 }
235
236 return esel;
237}
238
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600239/* Search the guest TLB for a matching entry. */
240static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
241 gva_t eaddr, int tlbsel, unsigned int pid, int as)
242{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500243 int size = vcpu_e500->gtlb_params[tlbsel].entries;
244 unsigned int set_base, offset;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600245 int i;
246
Scott Wood1aee47a2011-06-14 18:35:20 -0500247 if (tlbsel == 0) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500248 set_base = gtlb0_set_base(vcpu_e500, eaddr);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500249 size = vcpu_e500->gtlb_params[0].ways;
Scott Wood1aee47a2011-06-14 18:35:20 -0500250 } else {
251 set_base = 0;
252 }
253
Scott Wooddc83b8b2011-08-18 15:25:21 -0500254 offset = vcpu_e500->gtlb_offset[tlbsel];
255
Scott Wood1aee47a2011-06-14 18:35:20 -0500256 for (i = 0; i < size; i++) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500257 struct kvm_book3e_206_tlb_entry *tlbe =
258 &vcpu_e500->gtlb_arch[offset + set_base + i];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600259 unsigned int tid;
260
261 if (eaddr < get_tlb_eaddr(tlbe))
262 continue;
263
264 if (eaddr > get_tlb_end(tlbe))
265 continue;
266
267 tid = get_tlb_tid(tlbe);
268 if (tid && (tid != pid))
269 continue;
270
271 if (!get_tlb_v(tlbe))
272 continue;
273
274 if (get_tlb_ts(tlbe) != as && as != -1)
275 continue;
276
Scott Wood1aee47a2011-06-14 18:35:20 -0500277 return set_base + i;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600278 }
279
280 return -1;
281}
282
Scott Wood0164c0f2011-08-18 15:25:18 -0500283static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500284 struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood0164c0f2011-08-18 15:25:18 -0500285 pfn_t pfn)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600286{
Scott Wood0164c0f2011-08-18 15:25:18 -0500287 ref->pfn = pfn;
288 ref->flags = E500_TLB_VALID;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600289
Liu Yu08b7fa92011-06-14 18:34:59 -0500290 if (tlbe_is_writable(gtlbe))
Scott Wood0164c0f2011-08-18 15:25:18 -0500291 ref->flags |= E500_TLB_DIRTY;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600292}
293
Scott Wood0164c0f2011-08-18 15:25:18 -0500294static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600295{
Scott Wood0164c0f2011-08-18 15:25:18 -0500296 if (ref->flags & E500_TLB_VALID) {
297 if (ref->flags & E500_TLB_DIRTY)
298 kvm_release_pfn_dirty(ref->pfn);
Liu Yu08b7fa92011-06-14 18:34:59 -0500299 else
Scott Wood0164c0f2011-08-18 15:25:18 -0500300 kvm_release_pfn_clean(ref->pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600301
Scott Wood0164c0f2011-08-18 15:25:18 -0500302 ref->flags = 0;
Liu Yu08b7fa92011-06-14 18:34:59 -0500303 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600304}
305
Scott Wood4f802fe2011-12-20 15:34:37 +0000306static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
307{
308 if (vcpu_e500->g2h_tlb1_map)
309 memset(vcpu_e500->g2h_tlb1_map,
310 sizeof(u64) * vcpu_e500->gtlb_params[1].entries, 0);
311 if (vcpu_e500->h2g_tlb1_rmap)
312 memset(vcpu_e500->h2g_tlb1_rmap,
313 sizeof(unsigned int) * host_tlb_params[1].entries, 0);
314}
315
Scott Wood0164c0f2011-08-18 15:25:18 -0500316static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
317{
318 int tlbsel = 0;
319 int i;
320
Scott Wooddc83b8b2011-08-18 15:25:21 -0500321 for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500322 struct tlbe_ref *ref =
323 &vcpu_e500->gtlb_priv[tlbsel][i].ref;
324 kvmppc_e500_ref_release(ref);
325 }
326}
327
328static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
329{
330 int stlbsel = 1;
331 int i;
332
Scott Wood8fdd21a22011-12-20 15:34:34 +0000333 kvmppc_e500_tlbil_all(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500334
Scott Wood0164c0f2011-08-18 15:25:18 -0500335 for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
336 struct tlbe_ref *ref =
337 &vcpu_e500->tlb_refs[stlbsel][i];
338 kvmppc_e500_ref_release(ref);
339 }
340
341 clear_tlb_privs(vcpu_e500);
342}
343
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600344static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
345 unsigned int eaddr, int as)
346{
347 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000348 unsigned int victim, tsized;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600349 int tlbsel;
350
Liu Yufb2838d2009-01-14 10:47:37 -0600351 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600352 tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500353 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Scott Woodb5904972011-11-08 18:23:30 -0600354 tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600355
Scott Woodb5904972011-11-08 18:23:30 -0600356 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500357 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600358 vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
Scott Wood8fdd21a22011-12-20 15:34:34 +0000359 | MAS1_TID(get_tlbmiss_tid(vcpu))
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600360 | MAS1_TSIZE(tsized);
Scott Woodb5904972011-11-08 18:23:30 -0600361 vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
362 | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
363 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
364 vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600365 | (get_cur_pid(vcpu) << 16)
366 | (as ? MAS6_SAS : 0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600367}
368
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500369/* TID must be supplied by the caller */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500370static inline void kvmppc_e500_setup_stlbe(
Scott Wood8fdd21a22011-12-20 15:34:34 +0000371 struct kvm_vcpu *vcpu,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500372 struct kvm_book3e_206_tlb_entry *gtlbe,
373 int tsize, struct tlbe_ref *ref, u64 gvaddr,
374 struct kvm_book3e_206_tlb_entry *stlbe)
Liu Yu08b7fa92011-06-14 18:34:59 -0500375{
Scott Wood0164c0f2011-08-18 15:25:18 -0500376 pfn_t pfn = ref->pfn;
Scott Wood8fdd21a22011-12-20 15:34:34 +0000377 u32 pr = vcpu->arch.shared->msr & MSR_PR;
Scott Wood0164c0f2011-08-18 15:25:18 -0500378
379 BUG_ON(!(ref->flags & E500_TLB_VALID));
Liu Yu08b7fa92011-06-14 18:34:59 -0500380
Scott Wood8fdd21a22011-12-20 15:34:34 +0000381 /* Force IPROT=0 for all guest mappings. */
382 stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
383 stlbe->mas2 = (gvaddr & MAS2_EPN) |
384 e500_shadow_mas2_attrib(gtlbe->mas2, pr);
385 stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
386 e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
Liu Yu08b7fa92011-06-14 18:34:59 -0500387}
388
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600389static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500390 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood57013522011-11-29 10:40:23 +0000391 int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500392 struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600393{
Scott Wood9973d542011-06-14 18:34:39 -0500394 struct kvm_memory_slot *slot;
Scott Wood9973d542011-06-14 18:34:39 -0500395 unsigned long pfn, hva;
396 int pfnmap = 0;
397 int tsize = BOOK3E_PAGESZ_4K;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600398
Scott Wood59c1f4e2011-06-14 18:34:37 -0500399 /*
400 * Translate guest physical to true physical, acquiring
401 * a page reference if it is normal, non-reserved memory.
Scott Wood9973d542011-06-14 18:34:39 -0500402 *
403 * gfn_to_memslot() must succeed because otherwise we wouldn't
404 * have gotten this far. Eventually we should just pass the slot
405 * pointer through from the first lookup.
Scott Wood59c1f4e2011-06-14 18:34:37 -0500406 */
Scott Wood9973d542011-06-14 18:34:39 -0500407 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
408 hva = gfn_to_hva_memslot(slot, gfn);
409
410 if (tlbsel == 1) {
411 struct vm_area_struct *vma;
412 down_read(&current->mm->mmap_sem);
413
414 vma = find_vma(current->mm, hva);
415 if (vma && hva >= vma->vm_start &&
416 (vma->vm_flags & VM_PFNMAP)) {
417 /*
418 * This VMA is a physically contiguous region (e.g.
419 * /dev/mem) that bypasses normal Linux page
420 * management. Find the overlap between the
421 * vma and the memslot.
422 */
423
424 unsigned long start, end;
425 unsigned long slot_start, slot_end;
426
427 pfnmap = 1;
428
429 start = vma->vm_pgoff;
430 end = start +
431 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
432
433 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
434
435 slot_start = pfn - (gfn - slot->base_gfn);
436 slot_end = slot_start + slot->npages;
437
438 if (start < slot_start)
439 start = slot_start;
440 if (end > slot_end)
441 end = slot_end;
442
443 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
444 MAS1_TSIZE_SHIFT;
445
446 /*
447 * e500 doesn't implement the lowest tsize bit,
448 * or 1K pages.
449 */
450 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
451
452 /*
453 * Now find the largest tsize (up to what the guest
454 * requested) that will cover gfn, stay within the
455 * range, and for which gfn and pfn are mutually
456 * aligned.
457 */
458
459 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
460 unsigned long gfn_start, gfn_end, tsize_pages;
461 tsize_pages = 1 << (tsize - 2);
462
463 gfn_start = gfn & ~(tsize_pages - 1);
464 gfn_end = gfn_start + tsize_pages;
465
466 if (gfn_start + pfn - gfn < start)
467 continue;
468 if (gfn_end + pfn - gfn > end)
469 continue;
470 if ((gfn & (tsize_pages - 1)) !=
471 (pfn & (tsize_pages - 1)))
472 continue;
473
474 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
475 pfn &= ~(tsize_pages - 1);
476 break;
477 }
Alexander Graf95325e62011-09-20 01:31:48 +0200478 } else if (vma && hva >= vma->vm_start &&
479 (vma->vm_flags & VM_HUGETLB)) {
480 unsigned long psize = vma_kernel_pagesize(vma);
481
482 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
483 MAS1_TSIZE_SHIFT;
484
485 /*
486 * Take the largest page size that satisfies both host
487 * and guest mapping
488 */
489 tsize = min(__ilog2(psize) - 10, tsize);
490
491 /*
492 * e500 doesn't implement the lowest tsize bit,
493 * or 1K pages.
494 */
495 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
Scott Wood9973d542011-06-14 18:34:39 -0500496 }
497
498 up_read(&current->mm->mmap_sem);
499 }
500
501 if (likely(!pfnmap)) {
Alexander Graf95325e62011-09-20 01:31:48 +0200502 unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
Scott Wood9973d542011-06-14 18:34:39 -0500503 pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
504 if (is_error_pfn(pfn)) {
505 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
506 (long)gfn);
507 kvm_release_pfn_clean(pfn);
508 return;
509 }
Alexander Graf95325e62011-09-20 01:31:48 +0200510
511 /* Align guest and physical address to page map boundaries */
512 pfn &= ~(tsize_pages - 1);
513 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600514 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600515
Scott Wood0164c0f2011-08-18 15:25:18 -0500516 /* Drop old ref and setup new one. */
517 kvmppc_e500_ref_release(ref);
518 kvmppc_e500_ref_setup(ref, gtlbe, pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600519
Scott Wood8fdd21a22011-12-20 15:34:34 +0000520 kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
521 ref, gvaddr, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600522}
523
524/* XXX only map the one-one case, for now use TLB0 */
Scott Wood57013522011-11-29 10:40:23 +0000525static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
526 int esel,
527 struct kvm_book3e_206_tlb_entry *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600528{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500529 struct kvm_book3e_206_tlb_entry *gtlbe;
Scott Wood0164c0f2011-08-18 15:25:18 -0500530 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600531
Scott Wooddc83b8b2011-08-18 15:25:21 -0500532 gtlbe = get_entry(vcpu_e500, 0, esel);
Scott Wood0164c0f2011-08-18 15:25:18 -0500533 ref = &vcpu_e500->gtlb_priv[0][esel].ref;
534
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600535 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
536 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
Scott Wood57013522011-11-29 10:40:23 +0000537 gtlbe, 0, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600538}
539
540/* Caller must ensure that the specified guest TLB entry is safe to insert into
541 * the shadow TLB. */
542/* XXX for both one-one and one-to-many , for now use TLB1 */
543static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500544 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood4f802fe2011-12-20 15:34:37 +0000545 struct kvm_book3e_206_tlb_entry *stlbe, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600546{
Scott Wood0164c0f2011-08-18 15:25:18 -0500547 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600548 unsigned int victim;
549
Scott Wood0164c0f2011-08-18 15:25:18 -0500550 victim = vcpu_e500->host_tlb1_nv++;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600551
Scott Wood0164c0f2011-08-18 15:25:18 -0500552 if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
553 vcpu_e500->host_tlb1_nv = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600554
Scott Wood0164c0f2011-08-18 15:25:18 -0500555 ref = &vcpu_e500->tlb_refs[1][victim];
Scott Wood57013522011-11-29 10:40:23 +0000556 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600557
Scott Wood4f802fe2011-12-20 15:34:37 +0000558 vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << victim;
559 vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
560 if (vcpu_e500->h2g_tlb1_rmap[victim]) {
561 unsigned int idx = vcpu_e500->h2g_tlb1_rmap[victim];
562 vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << victim);
563 }
564 vcpu_e500->h2g_tlb1_rmap[victim] = esel;
565
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600566 return victim;
567}
568
Liu Yu08b7fa92011-06-14 18:34:59 -0500569static inline int kvmppc_e500_gtlbe_invalidate(
570 struct kvmppc_vcpu_e500 *vcpu_e500,
571 int tlbsel, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600572{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500573 struct kvm_book3e_206_tlb_entry *gtlbe =
574 get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600575
576 if (unlikely(get_tlb_iprot(gtlbe)))
577 return -1;
578
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600579 gtlbe->mas1 = 0;
580
581 return 0;
582}
583
Liu Yub0a18352009-02-17 16:52:08 +0800584int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
585{
586 int esel;
587
588 if (value & MMUCSR0_TLB0FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500589 for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800590 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
591 if (value & MMUCSR0_TLB1FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500592 for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800593 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
594
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500595 /* Invalidate all vcpu id mappings */
Scott Wood8fdd21a22011-12-20 15:34:34 +0000596 kvmppc_e500_tlbil_all(vcpu_e500);
Liu Yub0a18352009-02-17 16:52:08 +0800597
598 return EMULATE_DONE;
599}
600
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600601int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
602{
603 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
604 unsigned int ia;
605 int esel, tlbsel;
606 gva_t ea;
607
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100608 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600609
610 ia = (ea >> 2) & 0x1;
611
Liu Yufb2838d2009-01-14 10:47:37 -0600612 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600613 tlbsel = (ea >> 3) & 0x1;
614
615 if (ia) {
616 /* invalidate all entries */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500617 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
618 esel++)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600619 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
620 } else {
621 ea &= 0xfffff000;
622 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
623 get_cur_pid(vcpu), -1);
624 if (esel >= 0)
625 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
626 }
627
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500628 /* Invalidate all vcpu id mappings */
Scott Wood8fdd21a22011-12-20 15:34:34 +0000629 kvmppc_e500_tlbil_all(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600630
631 return EMULATE_DONE;
632}
633
634int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
635{
636 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
637 int tlbsel, esel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500638 struct kvm_book3e_206_tlb_entry *gtlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600639
Scott Woodb5904972011-11-08 18:23:30 -0600640 tlbsel = get_tlb_tlbsel(vcpu);
641 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600642
Scott Wooddc83b8b2011-08-18 15:25:21 -0500643 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Scott Woodb5904972011-11-08 18:23:30 -0600644 vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
645 vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
646 vcpu->arch.shared->mas1 = gtlbe->mas1;
647 vcpu->arch.shared->mas2 = gtlbe->mas2;
648 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600649
650 return EMULATE_DONE;
651}
652
653int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
654{
655 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Woodb5904972011-11-08 18:23:30 -0600656 int as = !!get_cur_sas(vcpu);
657 unsigned int pid = get_cur_spid(vcpu);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600658 int esel, tlbsel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500659 struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600660 gva_t ea;
661
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100662 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600663
664 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
665 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
666 if (esel >= 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500667 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600668 break;
669 }
670 }
671
672 if (gtlbe) {
Scott Wood303b7c92011-08-18 15:25:23 -0500673 esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
674
Scott Woodb5904972011-11-08 18:23:30 -0600675 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
Liu Yu08b7fa92011-06-14 18:34:59 -0500676 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600677 vcpu->arch.shared->mas1 = gtlbe->mas1;
678 vcpu->arch.shared->mas2 = gtlbe->mas2;
679 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600680 } else {
681 int victim;
682
Liu Yufb2838d2009-01-14 10:47:37 -0600683 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600684 tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500685 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600686
Scott Woodb5904972011-11-08 18:23:30 -0600687 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
688 | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500689 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600690 vcpu->arch.shared->mas1 =
691 (vcpu->arch.shared->mas6 & MAS6_SPID0)
692 | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
693 | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
694 vcpu->arch.shared->mas2 &= MAS2_EPN;
695 vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
696 MAS2_ATTRIB_MASK;
697 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
698 MAS3_U2 | MAS3_U3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600699 }
700
Scott Wood49ea0692011-03-28 15:01:24 -0500701 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600702 return EMULATE_DONE;
703}
704
Scott Wood57013522011-11-29 10:40:23 +0000705/* sesel is for tlb1 only */
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500706static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500707 struct kvm_book3e_206_tlb_entry *gtlbe,
708 struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500709 int stlbsel, int sesel)
710{
711 int stid;
712
713 preempt_disable();
Scott Wood8fdd21a22011-12-20 15:34:34 +0000714 stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500715
716 stlbe->mas1 |= MAS1_TID(stid);
717 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
718 preempt_enable();
719}
720
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600721int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
722{
723 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000724 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
725 int tlbsel, esel, stlbsel, sesel;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600726
Scott Woodb5904972011-11-08 18:23:30 -0600727 tlbsel = get_tlb_tlbsel(vcpu);
728 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600729
Scott Wooddc83b8b2011-08-18 15:25:21 -0500730 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600731
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500732 if (get_tlb_v(gtlbe))
Scott Wood0164c0f2011-08-18 15:25:18 -0500733 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600734
Scott Woodb5904972011-11-08 18:23:30 -0600735 gtlbe->mas1 = vcpu->arch.shared->mas1;
736 gtlbe->mas2 = vcpu->arch.shared->mas2;
737 gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600738
Liu Yud37b1a02011-12-20 14:42:56 +0000739 trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
740 gtlbe->mas2, gtlbe->mas7_3);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600741
742 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
743 if (tlbe_is_host_safe(vcpu, gtlbe)) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500744 u64 eaddr;
745 u64 raddr;
746
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600747 switch (tlbsel) {
748 case 0:
749 /* TLB0 */
750 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800751 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600752
753 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +0000754 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
755 sesel = 0; /* unused */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600756
757 break;
758
759 case 1:
760 /* TLB1 */
761 eaddr = get_tlb_eaddr(gtlbe);
762 raddr = get_tlb_raddr(gtlbe);
763
764 /* Create a 4KB mapping on the host.
765 * If the guest wanted a large page,
766 * only the first 4KB is mapped here and the rest
767 * are mapped on the fly. */
768 stlbsel = 1;
769 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
Scott Wood4f802fe2011-12-20 15:34:37 +0000770 raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600771 break;
772
773 default:
774 BUG();
775 }
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500776
777 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600778 }
779
Scott Wood49ea0692011-03-28 15:01:24 -0500780 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600781 return EMULATE_DONE;
782}
783
Scott Wood8fdd21a22011-12-20 15:34:34 +0000784static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
785 gva_t eaddr, unsigned int pid, int as)
786{
787 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
788 int esel, tlbsel;
789
790 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
791 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
792 if (esel >= 0)
793 return index_of(tlbsel, esel);
794 }
795
796 return -1;
797}
798
799/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
800int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
801 struct kvm_translation *tr)
802{
803 int index;
804 gva_t eaddr;
805 u8 pid;
806 u8 as;
807
808 eaddr = tr->linear_address;
809 pid = (tr->linear_address >> 32) & 0xff;
810 as = (tr->linear_address >> 40) & 0x1;
811
812 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
813 if (index < 0) {
814 tr->valid = 0;
815 return 0;
816 }
817
818 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
819 /* XXX what does "writeable" and "usermode" even mean? */
820 tr->valid = 1;
821
822 return 0;
823}
824
825
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600826int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
827{
Alexander Graf666e7252010-07-29 14:47:43 +0200828 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600829
830 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
831}
832
833int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
834{
Alexander Graf666e7252010-07-29 14:47:43 +0200835 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600836
837 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
838}
839
840void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
841{
Alexander Graf666e7252010-07-29 14:47:43 +0200842 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600843
844 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
845}
846
847void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
848{
Alexander Graf666e7252010-07-29 14:47:43 +0200849 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600850
851 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
852}
853
854gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
855 gva_t eaddr)
856{
857 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500858 struct kvm_book3e_206_tlb_entry *gtlbe;
859 u64 pgmask;
860
861 gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
862 pgmask = get_tlb_bytes(gtlbe) - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600863
864 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
865}
866
867void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
868{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600869}
870
871void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
872 unsigned int index)
873{
874 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Liu Yu08b7fa92011-06-14 18:34:59 -0500875 struct tlbe_priv *priv;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500876 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600877 int tlbsel = tlbsel_of(index);
878 int esel = esel_of(index);
879 int stlbsel, sesel;
880
Scott Wooddc83b8b2011-08-18 15:25:21 -0500881 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Liu Yu08b7fa92011-06-14 18:34:59 -0500882
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600883 switch (tlbsel) {
884 case 0:
885 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +0000886 sesel = 0; /* unused */
Scott Wood0164c0f2011-08-18 15:25:18 -0500887 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
Liu Yu08b7fa92011-06-14 18:34:59 -0500888
Scott Wood8fdd21a22011-12-20 15:34:34 +0000889 kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
Scott Wood0164c0f2011-08-18 15:25:18 -0500890 &priv->ref, eaddr, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600891 break;
892
893 case 1: {
894 gfn_t gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600895
896 stlbsel = 1;
Liu Yu08b7fa92011-06-14 18:34:59 -0500897 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
Scott Wood4f802fe2011-12-20 15:34:37 +0000898 gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600899 break;
900 }
901
902 default:
903 BUG();
904 break;
905 }
Liu Yu08b7fa92011-06-14 18:34:59 -0500906
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500907 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600908}
909
Scott Wooddc83b8b2011-08-18 15:25:21 -0500910static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
911{
912 int i;
913
Scott Wood4f802fe2011-12-20 15:34:37 +0000914 clear_tlb1_bitmap(vcpu_e500);
915 kfree(vcpu_e500->g2h_tlb1_map);
916
Scott Wooddc83b8b2011-08-18 15:25:21 -0500917 clear_tlb_refs(vcpu_e500);
918 kfree(vcpu_e500->gtlb_priv[0]);
919 kfree(vcpu_e500->gtlb_priv[1]);
920
921 if (vcpu_e500->shared_tlb_pages) {
922 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
923 PAGE_SIZE)));
924
925 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
926 set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
927 put_page(vcpu_e500->shared_tlb_pages[i]);
928 }
929
930 vcpu_e500->num_shared_tlb_pages = 0;
931 vcpu_e500->shared_tlb_pages = NULL;
932 } else {
933 kfree(vcpu_e500->gtlb_arch);
934 }
935
936 vcpu_e500->gtlb_arch = NULL;
937}
938
Scott Wood8fdd21a22011-12-20 15:34:34 +0000939void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
940{
941 sregs->u.e.mas0 = vcpu->arch.shared->mas0;
942 sregs->u.e.mas1 = vcpu->arch.shared->mas1;
943 sregs->u.e.mas2 = vcpu->arch.shared->mas2;
944 sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
945 sregs->u.e.mas4 = vcpu->arch.shared->mas4;
946 sregs->u.e.mas6 = vcpu->arch.shared->mas6;
947
948 sregs->u.e.mmucfg = vcpu->arch.mmucfg;
949 sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
950 sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
951 sregs->u.e.tlbcfg[2] = 0;
952 sregs->u.e.tlbcfg[3] = 0;
953}
954
955int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
956{
957 if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
958 vcpu->arch.shared->mas0 = sregs->u.e.mas0;
959 vcpu->arch.shared->mas1 = sregs->u.e.mas1;
960 vcpu->arch.shared->mas2 = sregs->u.e.mas2;
961 vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
962 vcpu->arch.shared->mas4 = sregs->u.e.mas4;
963 vcpu->arch.shared->mas6 = sregs->u.e.mas6;
964 }
965
966 return 0;
967}
968
Scott Wooddc83b8b2011-08-18 15:25:21 -0500969int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
970 struct kvm_config_tlb *cfg)
971{
972 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
973 struct kvm_book3e_206_tlb_params params;
974 char *virt;
975 struct page **pages;
976 struct tlbe_priv *privs[2] = {};
Scott Wood4f802fe2011-12-20 15:34:37 +0000977 u64 *g2h_bitmap = NULL;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500978 size_t array_len;
979 u32 sets;
980 int num_pages, ret, i;
981
982 if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
983 return -EINVAL;
984
985 if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
986 sizeof(params)))
987 return -EFAULT;
988
989 if (params.tlb_sizes[1] > 64)
990 return -EINVAL;
991 if (params.tlb_ways[1] != params.tlb_sizes[1])
992 return -EINVAL;
993 if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
994 return -EINVAL;
995 if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
996 return -EINVAL;
997
998 if (!is_power_of_2(params.tlb_ways[0]))
999 return -EINVAL;
1000
1001 sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1002 if (!is_power_of_2(sets))
1003 return -EINVAL;
1004
1005 array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1006 array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1007
1008 if (cfg->array_len < array_len)
1009 return -EINVAL;
1010
1011 num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1012 cfg->array / PAGE_SIZE;
1013 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1014 if (!pages)
1015 return -ENOMEM;
1016
1017 ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1018 if (ret < 0)
1019 goto err_pages;
1020
1021 if (ret != num_pages) {
1022 num_pages = ret;
1023 ret = -EFAULT;
1024 goto err_put_page;
1025 }
1026
1027 virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1028 if (!virt)
1029 goto err_put_page;
1030
1031 privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1032 GFP_KERNEL);
1033 privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1034 GFP_KERNEL);
1035
1036 if (!privs[0] || !privs[1])
1037 goto err_put_page;
1038
Scott Wood4f802fe2011-12-20 15:34:37 +00001039 g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
1040 GFP_KERNEL);
1041 if (!g2h_bitmap)
1042 goto err_put_page;
1043
Scott Wooddc83b8b2011-08-18 15:25:21 -05001044 free_gtlb(vcpu_e500);
1045
1046 vcpu_e500->gtlb_priv[0] = privs[0];
1047 vcpu_e500->gtlb_priv[1] = privs[1];
Scott Wood4f802fe2011-12-20 15:34:37 +00001048 vcpu_e500->g2h_tlb1_map = g2h_bitmap;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001049
1050 vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1051 (virt + (cfg->array & (PAGE_SIZE - 1)));
1052
1053 vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1054 vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1055
1056 vcpu_e500->gtlb_offset[0] = 0;
1057 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1058
Scott Wood8fdd21a22011-12-20 15:34:34 +00001059 vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001060
Scott Wood8fdd21a22011-12-20 15:34:34 +00001061 vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1062 if (params.tlb_sizes[0] <= 2048)
1063 vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
1064 vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
1065
1066 vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1067 vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
1068 vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001069
1070 vcpu_e500->shared_tlb_pages = pages;
1071 vcpu_e500->num_shared_tlb_pages = num_pages;
1072
1073 vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1074 vcpu_e500->gtlb_params[0].sets = sets;
1075
1076 vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1077 vcpu_e500->gtlb_params[1].sets = 1;
1078
1079 return 0;
1080
1081err_put_page:
1082 kfree(privs[0]);
1083 kfree(privs[1]);
1084
1085 for (i = 0; i < num_pages; i++)
1086 put_page(pages[i]);
1087
1088err_pages:
1089 kfree(pages);
1090 return ret;
1091}
1092
1093int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1094 struct kvm_dirty_tlb *dirty)
1095{
1096 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1097
1098 clear_tlb_refs(vcpu_e500);
1099 return 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001100}
1101
1102int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1103{
Scott Wood8fdd21a22011-12-20 15:34:34 +00001104 struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001105 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1106 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1107
Scott Wood0164c0f2011-08-18 15:25:18 -05001108 host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1109 host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1110
1111 /*
1112 * This should never happen on real e500 hardware, but is
1113 * architecturally possible -- e.g. in some weird nested
1114 * virtualization case.
1115 */
1116 if (host_tlb_params[0].entries == 0 ||
1117 host_tlb_params[1].entries == 0) {
1118 pr_err("%s: need to know host tlb size\n", __func__);
1119 return -ENODEV;
1120 }
1121
1122 host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1123 TLBnCFG_ASSOC_SHIFT;
1124 host_tlb_params[1].ways = host_tlb_params[1].entries;
1125
1126 if (!is_power_of_2(host_tlb_params[0].entries) ||
1127 !is_power_of_2(host_tlb_params[0].ways) ||
1128 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1129 host_tlb_params[0].ways == 0) {
1130 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1131 __func__, host_tlb_params[0].entries,
1132 host_tlb_params[0].ways);
1133 return -ENODEV;
1134 }
1135
1136 host_tlb_params[0].sets =
1137 host_tlb_params[0].entries / host_tlb_params[0].ways;
1138 host_tlb_params[1].sets = 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001139
Scott Wooddc83b8b2011-08-18 15:25:21 -05001140 vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1141 vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001142
Scott Wooddc83b8b2011-08-18 15:25:21 -05001143 vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1144 vcpu_e500->gtlb_params[0].sets =
1145 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1146
1147 vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1148 vcpu_e500->gtlb_params[1].sets = 1;
1149
1150 vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1151 if (!vcpu_e500->gtlb_arch)
1152 return -ENOMEM;
1153
1154 vcpu_e500->gtlb_offset[0] = 0;
1155 vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001156
Scott Wood0164c0f2011-08-18 15:25:18 -05001157 vcpu_e500->tlb_refs[0] =
1158 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1159 GFP_KERNEL);
1160 if (!vcpu_e500->tlb_refs[0])
1161 goto err;
Liu Yu08b7fa92011-06-14 18:34:59 -05001162
Scott Wood0164c0f2011-08-18 15:25:18 -05001163 vcpu_e500->tlb_refs[1] =
1164 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1165 GFP_KERNEL);
1166 if (!vcpu_e500->tlb_refs[1])
1167 goto err;
1168
Scott Wooddc83b8b2011-08-18 15:25:21 -05001169 vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1170 vcpu_e500->gtlb_params[0].entries,
1171 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001172 if (!vcpu_e500->gtlb_priv[0])
1173 goto err;
1174
Scott Wooddc83b8b2011-08-18 15:25:21 -05001175 vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1176 vcpu_e500->gtlb_params[1].entries,
1177 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001178 if (!vcpu_e500->gtlb_priv[1])
1179 goto err;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001180
Scott Wood4f802fe2011-12-20 15:34:37 +00001181 vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(unsigned int) *
1182 vcpu_e500->gtlb_params[1].entries,
1183 GFP_KERNEL);
1184 if (!vcpu_e500->g2h_tlb1_map)
1185 goto err;
1186
1187 vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
1188 host_tlb_params[1].entries,
1189 GFP_KERNEL);
1190 if (!vcpu_e500->h2g_tlb1_rmap)
1191 goto err;
1192
Liu Yuda15bf42010-01-22 19:36:53 +08001193 /* Init TLB configuration register */
Scott Wood8fdd21a22011-12-20 15:34:34 +00001194 vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001195 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wood8fdd21a22011-12-20 15:34:34 +00001196 vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
1197 vcpu->arch.tlbcfg[0] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001198 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
1199
Scott Wood8fdd21a22011-12-20 15:34:34 +00001200 vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001201 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wood8fdd21a22011-12-20 15:34:34 +00001202 vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[1].entries;
1203 vcpu->arch.tlbcfg[0] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001204 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
Liu Yuda15bf42010-01-22 19:36:53 +08001205
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001206 return 0;
1207
Scott Wood0164c0f2011-08-18 15:25:18 -05001208err:
Scott Wooddc83b8b2011-08-18 15:25:21 -05001209 free_gtlb(vcpu_e500);
Scott Wood0164c0f2011-08-18 15:25:18 -05001210 kfree(vcpu_e500->tlb_refs[0]);
1211 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001212 return -1;
1213}
1214
1215void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1216{
Scott Wooddc83b8b2011-08-18 15:25:21 -05001217 free_gtlb(vcpu_e500);
Scott Wood4f802fe2011-12-20 15:34:37 +00001218 kfree(vcpu_e500->h2g_tlb1_rmap);
Scott Wood0164c0f2011-08-18 15:25:18 -05001219 kfree(vcpu_e500->tlb_refs[0]);
1220 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001221}