blob: 6eb5d655bdb4e7a77ca60c051212538c80cc5b39 [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
Scott Woodab9fc402011-12-20 15:34:39 +0000634static void tlbilx_all(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
635 int pid, int rt)
636{
637 struct kvm_book3e_206_tlb_entry *tlbe;
638 int tid, esel;
639
640 /* invalidate all entries */
641 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; esel++) {
642 tlbe = get_entry(vcpu_e500, tlbsel, esel);
643 tid = get_tlb_tid(tlbe);
644 if (rt == 0 || tid == pid) {
645 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
646 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
647 }
648 }
649}
650
651static void tlbilx_one(struct kvmppc_vcpu_e500 *vcpu_e500, int pid,
652 int ra, int rb)
653{
654 int tlbsel, esel;
655 gva_t ea;
656
657 ea = kvmppc_get_gpr(&vcpu_e500->vcpu, rb);
658 if (ra)
659 ea += kvmppc_get_gpr(&vcpu_e500->vcpu, ra);
660
661 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
662 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1);
663 if (esel >= 0) {
664 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
665 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
666 break;
667 }
668 }
669}
670
671int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int rt, int ra, int rb)
672{
673 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
674 int pid = get_cur_spid(vcpu);
675
676 if (rt == 0 || rt == 1) {
677 tlbilx_all(vcpu_e500, 0, pid, rt);
678 tlbilx_all(vcpu_e500, 1, pid, rt);
679 } else if (rt == 3) {
680 tlbilx_one(vcpu_e500, pid, ra, rb);
681 }
682
683 return EMULATE_DONE;
684}
685
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600686int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
687{
688 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
689 int tlbsel, esel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500690 struct kvm_book3e_206_tlb_entry *gtlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600691
Scott Woodb5904972011-11-08 18:23:30 -0600692 tlbsel = get_tlb_tlbsel(vcpu);
693 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600694
Scott Wooddc83b8b2011-08-18 15:25:21 -0500695 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Scott Woodb5904972011-11-08 18:23:30 -0600696 vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
697 vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
698 vcpu->arch.shared->mas1 = gtlbe->mas1;
699 vcpu->arch.shared->mas2 = gtlbe->mas2;
700 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600701
702 return EMULATE_DONE;
703}
704
705int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
706{
707 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Woodb5904972011-11-08 18:23:30 -0600708 int as = !!get_cur_sas(vcpu);
709 unsigned int pid = get_cur_spid(vcpu);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600710 int esel, tlbsel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500711 struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600712 gva_t ea;
713
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100714 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600715
716 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
717 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
718 if (esel >= 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500719 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600720 break;
721 }
722 }
723
724 if (gtlbe) {
Scott Wood303b7c92011-08-18 15:25:23 -0500725 esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
726
Scott Woodb5904972011-11-08 18:23:30 -0600727 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
Liu Yu08b7fa92011-06-14 18:34:59 -0500728 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600729 vcpu->arch.shared->mas1 = gtlbe->mas1;
730 vcpu->arch.shared->mas2 = gtlbe->mas2;
731 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600732 } else {
733 int victim;
734
Liu Yufb2838d2009-01-14 10:47:37 -0600735 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600736 tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500737 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600738
Scott Woodb5904972011-11-08 18:23:30 -0600739 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
740 | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500741 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600742 vcpu->arch.shared->mas1 =
743 (vcpu->arch.shared->mas6 & MAS6_SPID0)
744 | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
745 | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
746 vcpu->arch.shared->mas2 &= MAS2_EPN;
747 vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
748 MAS2_ATTRIB_MASK;
749 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
750 MAS3_U2 | MAS3_U3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600751 }
752
Scott Wood49ea0692011-03-28 15:01:24 -0500753 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600754 return EMULATE_DONE;
755}
756
Scott Wood57013522011-11-29 10:40:23 +0000757/* sesel is for tlb1 only */
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500758static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500759 struct kvm_book3e_206_tlb_entry *gtlbe,
760 struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500761 int stlbsel, int sesel)
762{
763 int stid;
764
765 preempt_disable();
Scott Wood8fdd21a22011-12-20 15:34:34 +0000766 stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500767
768 stlbe->mas1 |= MAS1_TID(stid);
769 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
770 preempt_enable();
771}
772
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600773int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
774{
775 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000776 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
777 int tlbsel, esel, stlbsel, sesel;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600778
Scott Woodb5904972011-11-08 18:23:30 -0600779 tlbsel = get_tlb_tlbsel(vcpu);
780 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600781
Scott Wooddc83b8b2011-08-18 15:25:21 -0500782 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600783
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500784 if (get_tlb_v(gtlbe))
Scott Wood0164c0f2011-08-18 15:25:18 -0500785 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600786
Scott Woodb5904972011-11-08 18:23:30 -0600787 gtlbe->mas1 = vcpu->arch.shared->mas1;
788 gtlbe->mas2 = vcpu->arch.shared->mas2;
789 gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600790
Liu Yud37b1a02011-12-20 14:42:56 +0000791 trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
792 gtlbe->mas2, gtlbe->mas7_3);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600793
794 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
795 if (tlbe_is_host_safe(vcpu, gtlbe)) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500796 u64 eaddr;
797 u64 raddr;
798
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600799 switch (tlbsel) {
800 case 0:
801 /* TLB0 */
802 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800803 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600804
805 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +0000806 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
807 sesel = 0; /* unused */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600808
809 break;
810
811 case 1:
812 /* TLB1 */
813 eaddr = get_tlb_eaddr(gtlbe);
814 raddr = get_tlb_raddr(gtlbe);
815
816 /* Create a 4KB mapping on the host.
817 * If the guest wanted a large page,
818 * only the first 4KB is mapped here and the rest
819 * are mapped on the fly. */
820 stlbsel = 1;
821 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
Scott Wood4f802fe2011-12-20 15:34:37 +0000822 raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600823 break;
824
825 default:
826 BUG();
827 }
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500828
829 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600830 }
831
Scott Wood49ea0692011-03-28 15:01:24 -0500832 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600833 return EMULATE_DONE;
834}
835
Scott Wood8fdd21a22011-12-20 15:34:34 +0000836static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
837 gva_t eaddr, unsigned int pid, int as)
838{
839 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
840 int esel, tlbsel;
841
842 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
843 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
844 if (esel >= 0)
845 return index_of(tlbsel, esel);
846 }
847
848 return -1;
849}
850
851/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
852int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
853 struct kvm_translation *tr)
854{
855 int index;
856 gva_t eaddr;
857 u8 pid;
858 u8 as;
859
860 eaddr = tr->linear_address;
861 pid = (tr->linear_address >> 32) & 0xff;
862 as = (tr->linear_address >> 40) & 0x1;
863
864 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
865 if (index < 0) {
866 tr->valid = 0;
867 return 0;
868 }
869
870 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
871 /* XXX what does "writeable" and "usermode" even mean? */
872 tr->valid = 1;
873
874 return 0;
875}
876
877
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600878int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
879{
Alexander Graf666e7252010-07-29 14:47:43 +0200880 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600881
882 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
883}
884
885int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
886{
Alexander Graf666e7252010-07-29 14:47:43 +0200887 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600888
889 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
890}
891
892void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
893{
Alexander Graf666e7252010-07-29 14:47:43 +0200894 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600895
896 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
897}
898
899void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
900{
Alexander Graf666e7252010-07-29 14:47:43 +0200901 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600902
903 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
904}
905
906gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
907 gva_t eaddr)
908{
909 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500910 struct kvm_book3e_206_tlb_entry *gtlbe;
911 u64 pgmask;
912
913 gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
914 pgmask = get_tlb_bytes(gtlbe) - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600915
916 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
917}
918
919void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
920{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600921}
922
923void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
924 unsigned int index)
925{
926 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Liu Yu08b7fa92011-06-14 18:34:59 -0500927 struct tlbe_priv *priv;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500928 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600929 int tlbsel = tlbsel_of(index);
930 int esel = esel_of(index);
931 int stlbsel, sesel;
932
Scott Wooddc83b8b2011-08-18 15:25:21 -0500933 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Liu Yu08b7fa92011-06-14 18:34:59 -0500934
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600935 switch (tlbsel) {
936 case 0:
937 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +0000938 sesel = 0; /* unused */
Scott Wood0164c0f2011-08-18 15:25:18 -0500939 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
Liu Yu08b7fa92011-06-14 18:34:59 -0500940
Scott Wood8fdd21a22011-12-20 15:34:34 +0000941 kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
Scott Wood0164c0f2011-08-18 15:25:18 -0500942 &priv->ref, eaddr, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600943 break;
944
945 case 1: {
946 gfn_t gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600947
948 stlbsel = 1;
Liu Yu08b7fa92011-06-14 18:34:59 -0500949 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
Scott Wood4f802fe2011-12-20 15:34:37 +0000950 gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600951 break;
952 }
953
954 default:
955 BUG();
956 break;
957 }
Liu Yu08b7fa92011-06-14 18:34:59 -0500958
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500959 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600960}
961
Scott Wooddc83b8b2011-08-18 15:25:21 -0500962static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
963{
964 int i;
965
Scott Wood4f802fe2011-12-20 15:34:37 +0000966 clear_tlb1_bitmap(vcpu_e500);
967 kfree(vcpu_e500->g2h_tlb1_map);
968
Scott Wooddc83b8b2011-08-18 15:25:21 -0500969 clear_tlb_refs(vcpu_e500);
970 kfree(vcpu_e500->gtlb_priv[0]);
971 kfree(vcpu_e500->gtlb_priv[1]);
972
973 if (vcpu_e500->shared_tlb_pages) {
974 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
975 PAGE_SIZE)));
976
977 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
978 set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
979 put_page(vcpu_e500->shared_tlb_pages[i]);
980 }
981
982 vcpu_e500->num_shared_tlb_pages = 0;
983 vcpu_e500->shared_tlb_pages = NULL;
984 } else {
985 kfree(vcpu_e500->gtlb_arch);
986 }
987
988 vcpu_e500->gtlb_arch = NULL;
989}
990
Scott Wood8fdd21a22011-12-20 15:34:34 +0000991void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
992{
993 sregs->u.e.mas0 = vcpu->arch.shared->mas0;
994 sregs->u.e.mas1 = vcpu->arch.shared->mas1;
995 sregs->u.e.mas2 = vcpu->arch.shared->mas2;
996 sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
997 sregs->u.e.mas4 = vcpu->arch.shared->mas4;
998 sregs->u.e.mas6 = vcpu->arch.shared->mas6;
999
1000 sregs->u.e.mmucfg = vcpu->arch.mmucfg;
1001 sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
1002 sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
1003 sregs->u.e.tlbcfg[2] = 0;
1004 sregs->u.e.tlbcfg[3] = 0;
1005}
1006
1007int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1008{
1009 if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
1010 vcpu->arch.shared->mas0 = sregs->u.e.mas0;
1011 vcpu->arch.shared->mas1 = sregs->u.e.mas1;
1012 vcpu->arch.shared->mas2 = sregs->u.e.mas2;
1013 vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
1014 vcpu->arch.shared->mas4 = sregs->u.e.mas4;
1015 vcpu->arch.shared->mas6 = sregs->u.e.mas6;
1016 }
1017
1018 return 0;
1019}
1020
Scott Wooddc83b8b2011-08-18 15:25:21 -05001021int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
1022 struct kvm_config_tlb *cfg)
1023{
1024 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1025 struct kvm_book3e_206_tlb_params params;
1026 char *virt;
1027 struct page **pages;
1028 struct tlbe_priv *privs[2] = {};
Scott Wood4f802fe2011-12-20 15:34:37 +00001029 u64 *g2h_bitmap = NULL;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001030 size_t array_len;
1031 u32 sets;
1032 int num_pages, ret, i;
1033
1034 if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
1035 return -EINVAL;
1036
1037 if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
1038 sizeof(params)))
1039 return -EFAULT;
1040
1041 if (params.tlb_sizes[1] > 64)
1042 return -EINVAL;
1043 if (params.tlb_ways[1] != params.tlb_sizes[1])
1044 return -EINVAL;
1045 if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
1046 return -EINVAL;
1047 if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
1048 return -EINVAL;
1049
1050 if (!is_power_of_2(params.tlb_ways[0]))
1051 return -EINVAL;
1052
1053 sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1054 if (!is_power_of_2(sets))
1055 return -EINVAL;
1056
1057 array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1058 array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1059
1060 if (cfg->array_len < array_len)
1061 return -EINVAL;
1062
1063 num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1064 cfg->array / PAGE_SIZE;
1065 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1066 if (!pages)
1067 return -ENOMEM;
1068
1069 ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1070 if (ret < 0)
1071 goto err_pages;
1072
1073 if (ret != num_pages) {
1074 num_pages = ret;
1075 ret = -EFAULT;
1076 goto err_put_page;
1077 }
1078
1079 virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1080 if (!virt)
1081 goto err_put_page;
1082
1083 privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1084 GFP_KERNEL);
1085 privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1086 GFP_KERNEL);
1087
1088 if (!privs[0] || !privs[1])
1089 goto err_put_page;
1090
Scott Wood4f802fe2011-12-20 15:34:37 +00001091 g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
1092 GFP_KERNEL);
1093 if (!g2h_bitmap)
1094 goto err_put_page;
1095
Scott Wooddc83b8b2011-08-18 15:25:21 -05001096 free_gtlb(vcpu_e500);
1097
1098 vcpu_e500->gtlb_priv[0] = privs[0];
1099 vcpu_e500->gtlb_priv[1] = privs[1];
Scott Wood4f802fe2011-12-20 15:34:37 +00001100 vcpu_e500->g2h_tlb1_map = g2h_bitmap;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001101
1102 vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1103 (virt + (cfg->array & (PAGE_SIZE - 1)));
1104
1105 vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1106 vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1107
1108 vcpu_e500->gtlb_offset[0] = 0;
1109 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1110
Scott Wood8fdd21a22011-12-20 15:34:34 +00001111 vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001112
Scott Wood8fdd21a22011-12-20 15:34:34 +00001113 vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1114 if (params.tlb_sizes[0] <= 2048)
1115 vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
1116 vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
1117
1118 vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1119 vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
1120 vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001121
1122 vcpu_e500->shared_tlb_pages = pages;
1123 vcpu_e500->num_shared_tlb_pages = num_pages;
1124
1125 vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1126 vcpu_e500->gtlb_params[0].sets = sets;
1127
1128 vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1129 vcpu_e500->gtlb_params[1].sets = 1;
1130
1131 return 0;
1132
1133err_put_page:
1134 kfree(privs[0]);
1135 kfree(privs[1]);
1136
1137 for (i = 0; i < num_pages; i++)
1138 put_page(pages[i]);
1139
1140err_pages:
1141 kfree(pages);
1142 return ret;
1143}
1144
1145int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1146 struct kvm_dirty_tlb *dirty)
1147{
1148 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1149
1150 clear_tlb_refs(vcpu_e500);
1151 return 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001152}
1153
1154int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1155{
Scott Wood8fdd21a22011-12-20 15:34:34 +00001156 struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001157 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1158 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1159
Scott Wood0164c0f2011-08-18 15:25:18 -05001160 host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1161 host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1162
1163 /*
1164 * This should never happen on real e500 hardware, but is
1165 * architecturally possible -- e.g. in some weird nested
1166 * virtualization case.
1167 */
1168 if (host_tlb_params[0].entries == 0 ||
1169 host_tlb_params[1].entries == 0) {
1170 pr_err("%s: need to know host tlb size\n", __func__);
1171 return -ENODEV;
1172 }
1173
1174 host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1175 TLBnCFG_ASSOC_SHIFT;
1176 host_tlb_params[1].ways = host_tlb_params[1].entries;
1177
1178 if (!is_power_of_2(host_tlb_params[0].entries) ||
1179 !is_power_of_2(host_tlb_params[0].ways) ||
1180 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1181 host_tlb_params[0].ways == 0) {
1182 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1183 __func__, host_tlb_params[0].entries,
1184 host_tlb_params[0].ways);
1185 return -ENODEV;
1186 }
1187
1188 host_tlb_params[0].sets =
1189 host_tlb_params[0].entries / host_tlb_params[0].ways;
1190 host_tlb_params[1].sets = 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001191
Scott Wooddc83b8b2011-08-18 15:25:21 -05001192 vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1193 vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001194
Scott Wooddc83b8b2011-08-18 15:25:21 -05001195 vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1196 vcpu_e500->gtlb_params[0].sets =
1197 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1198
1199 vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1200 vcpu_e500->gtlb_params[1].sets = 1;
1201
1202 vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1203 if (!vcpu_e500->gtlb_arch)
1204 return -ENOMEM;
1205
1206 vcpu_e500->gtlb_offset[0] = 0;
1207 vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001208
Scott Wood0164c0f2011-08-18 15:25:18 -05001209 vcpu_e500->tlb_refs[0] =
1210 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1211 GFP_KERNEL);
1212 if (!vcpu_e500->tlb_refs[0])
1213 goto err;
Liu Yu08b7fa92011-06-14 18:34:59 -05001214
Scott Wood0164c0f2011-08-18 15:25:18 -05001215 vcpu_e500->tlb_refs[1] =
1216 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1217 GFP_KERNEL);
1218 if (!vcpu_e500->tlb_refs[1])
1219 goto err;
1220
Scott Wooddc83b8b2011-08-18 15:25:21 -05001221 vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1222 vcpu_e500->gtlb_params[0].entries,
1223 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001224 if (!vcpu_e500->gtlb_priv[0])
1225 goto err;
1226
Scott Wooddc83b8b2011-08-18 15:25:21 -05001227 vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1228 vcpu_e500->gtlb_params[1].entries,
1229 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001230 if (!vcpu_e500->gtlb_priv[1])
1231 goto err;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001232
Scott Wood4f802fe2011-12-20 15:34:37 +00001233 vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(unsigned int) *
1234 vcpu_e500->gtlb_params[1].entries,
1235 GFP_KERNEL);
1236 if (!vcpu_e500->g2h_tlb1_map)
1237 goto err;
1238
1239 vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
1240 host_tlb_params[1].entries,
1241 GFP_KERNEL);
1242 if (!vcpu_e500->h2g_tlb1_rmap)
1243 goto err;
1244
Liu Yuda15bf42010-01-22 19:36:53 +08001245 /* Init TLB configuration register */
Scott Wood8fdd21a22011-12-20 15:34:34 +00001246 vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001247 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wood8fdd21a22011-12-20 15:34:34 +00001248 vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
1249 vcpu->arch.tlbcfg[0] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001250 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
1251
Scott Wood8fdd21a22011-12-20 15:34:34 +00001252 vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001253 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wood8fdd21a22011-12-20 15:34:34 +00001254 vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[1].entries;
1255 vcpu->arch.tlbcfg[0] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001256 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
Liu Yuda15bf42010-01-22 19:36:53 +08001257
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001258 return 0;
1259
Scott Wood0164c0f2011-08-18 15:25:18 -05001260err:
Scott Wooddc83b8b2011-08-18 15:25:21 -05001261 free_gtlb(vcpu_e500);
Scott Wood0164c0f2011-08-18 15:25:18 -05001262 kfree(vcpu_e500->tlb_refs[0]);
1263 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001264 return -1;
1265}
1266
1267void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1268{
Scott Wooddc83b8b2011-08-18 15:25:21 -05001269 free_gtlb(vcpu_e500);
Scott Wood4f802fe2011-12-20 15:34:37 +00001270 kfree(vcpu_e500->h2g_tlb1_rmap);
Scott Wood0164c0f2011-08-18 15:25:18 -05001271 kfree(vcpu_e500->tlb_refs[0]);
1272 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001273}