blob: c510fc961302c2d1ae1284cc3d1aab1139002fbd [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 Wood73196cd2011-12-20 15:34:47 +00005 * Scott Wood, scottwood@freescale.com
Scott Wood4f802fe2011-12-20 15:34:37 +00006 * Ashish Kalra, ashish.kalra@freescale.com
Scott Wood73196cd2011-12-20 15:34:47 +00007 * Varun Sethi, varun.sethi@freescale.com
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06008 *
9 * Description:
10 * This file is based on arch/powerpc/kvm/44x_tlb.c,
11 * by Hollis Blanchard <hollisb@us.ibm.com>.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License, version 2, as
15 * published by the Free Software Foundation.
16 */
17
Scott Wood0164c0f2011-08-18 15:25:18 -050018#include <linux/kernel.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060019#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060021#include <linux/string.h>
22#include <linux/kvm.h>
23#include <linux/kvm_host.h>
24#include <linux/highmem.h>
Scott Wooddc83b8b2011-08-18 15:25:21 -050025#include <linux/log2.h>
26#include <linux/uaccess.h>
27#include <linux/sched.h>
28#include <linux/rwsem.h>
29#include <linux/vmalloc.h>
Alexander Graf95325e62011-09-20 01:31:48 +020030#include <linux/hugetlb.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060031#include <asm/kvm_ppc.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060032
Scott Wood29a5a6f2011-12-20 15:34:29 +000033#include "e500.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030034#include "trace.h"
Scott Wood49ea0692011-03-28 15:01:24 -050035#include "timing.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060036
Scott Wood0164c0f2011-08-18 15:25:18 -050037#define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060038
Scott Wood0164c0f2011-08-18 15:25:18 -050039static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060040
Scott Wood0164c0f2011-08-18 15:25:18 -050041static inline unsigned int gtlb0_get_next_victim(
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060042 struct kvmppc_vcpu_e500 *vcpu_e500)
43{
44 unsigned int victim;
45
Liu Yu08b7fa92011-06-14 18:34:59 -050046 victim = vcpu_e500->gtlb_nv[0]++;
Scott Wooddc83b8b2011-08-18 15:25:21 -050047 if (unlikely(vcpu_e500->gtlb_nv[0] >= vcpu_e500->gtlb_params[0].ways))
Liu Yu08b7fa92011-06-14 18:34:59 -050048 vcpu_e500->gtlb_nv[0] = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060049
50 return victim;
51}
52
53static inline unsigned int tlb1_max_shadow_size(void)
54{
Scott Wooda4cd8b22011-06-14 18:34:41 -050055 /* reserve one entry for magic page */
Scott Wood0164c0f2011-08-18 15:25:18 -050056 return host_tlb_params[1].entries - tlbcam_index - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060057}
58
Scott Wooddc83b8b2011-08-18 15:25:21 -050059static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060060{
Scott Wooddc83b8b2011-08-18 15:25:21 -050061 return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060062}
63
64static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
65{
66 /* Mask off reserved bits. */
67 mas3 &= MAS3_ATTRIB_MASK;
68
Scott Wood73196cd2011-12-20 15:34:47 +000069#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060070 if (!usermode) {
71 /* Guest is in supervisor mode,
72 * so we need to translate guest
73 * supervisor permissions into user permissions. */
74 mas3 &= ~E500_TLB_USER_PERM_MASK;
75 mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
76 }
Scott Wood73196cd2011-12-20 15:34:47 +000077 mas3 |= E500_TLB_SUPER_PERM_MASK;
78#endif
79 return mas3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060080}
81
82static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode)
83{
Liu Yu046a48b2009-03-17 16:57:46 +080084#ifdef CONFIG_SMP
85 return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M;
86#else
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060087 return mas2 & MAS2_ATTRIB_MASK;
Liu Yu046a48b2009-03-17 16:57:46 +080088#endif
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060089}
90
91/*
92 * writing shadow tlb entry to host TLB
93 */
Scott Wooddc83b8b2011-08-18 15:25:21 -050094static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
95 uint32_t mas0)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060096{
Scott Wood0ef30992011-06-14 18:34:35 -050097 unsigned long flags;
98
99 local_irq_save(flags);
100 mtspr(SPRN_MAS0, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600101 mtspr(SPRN_MAS1, stlbe->mas1);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500102 mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
103 mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
104 mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
Scott Wood73196cd2011-12-20 15:34:47 +0000105#ifdef CONFIG_KVM_BOOKE_HV
106 mtspr(SPRN_MAS8, stlbe->mas8);
107#endif
Scott Wood0ef30992011-06-14 18:34:35 -0500108 asm volatile("isync; tlbwe" : : : "memory");
Scott Wood73196cd2011-12-20 15:34:47 +0000109
110#ifdef CONFIG_KVM_BOOKE_HV
111 /* Must clear mas8 for other host tlbwe's */
112 mtspr(SPRN_MAS8, 0);
113 isync();
114#endif
Scott Wood0ef30992011-06-14 18:34:35 -0500115 local_irq_restore(flags);
Liu Yud37b1a02011-12-20 14:42:56 +0000116
117 trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
118 stlbe->mas2, stlbe->mas7_3);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600119}
120
Scott Wood57013522011-11-29 10:40:23 +0000121/*
122 * Acquire a mas0 with victim hint, as if we just took a TLB miss.
123 *
124 * We don't care about the address we're searching for, other than that it's
125 * in the right set and is not present in the TLB. Using a zero PID and a
126 * userspace address means we don't have to set and then restore MAS5, or
127 * calculate a proper MAS6 value.
128 */
129static u32 get_host_mas0(unsigned long eaddr)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600130{
Scott Wood57013522011-11-29 10:40:23 +0000131 unsigned long flags;
132 u32 mas0;
133
134 local_irq_save(flags);
135 mtspr(SPRN_MAS6, 0);
136 asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
137 mas0 = mfspr(SPRN_MAS0);
138 local_irq_restore(flags);
139
140 return mas0;
141}
142
143/* sesel is for tlb1 only */
144static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
145 int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
146{
147 u32 mas0;
148
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600149 if (tlbsel == 0) {
Scott Wood57013522011-11-29 10:40:23 +0000150 mas0 = get_host_mas0(stlbe->mas2);
151 __write_host_tlbe(stlbe, mas0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600152 } else {
Scott Wood0ef30992011-06-14 18:34:35 -0500153 __write_host_tlbe(stlbe,
154 MAS0_TLBSEL(1) |
Scott Wood57013522011-11-29 10:40:23 +0000155 MAS0_ESEL(to_htlb1_esel(sesel)));
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600156 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600157}
158
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000159#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -0500160void kvmppc_map_magic(struct kvm_vcpu *vcpu)
161{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500162 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500163 struct kvm_book3e_206_tlb_entry magic;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500164 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500165 unsigned int stid;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500166 pfn_t pfn;
167
168 pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
169 get_page(pfn_to_page(pfn));
170
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500171 preempt_disable();
172 stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
173
174 magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
Scott Wooda4cd8b22011-06-14 18:34:41 -0500175 MAS1_TSIZE(BOOK3E_PAGESZ_4K);
176 magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500177 magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
178 MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
Liu Yud37b1a02011-12-20 14:42:56 +0000179 magic.mas8 = 0;
Scott Wooda4cd8b22011-06-14 18:34:41 -0500180
181 __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index));
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500182 preempt_enable();
Scott Wooda4cd8b22011-06-14 18:34:41 -0500183}
Scott Wood8fdd21a22011-12-20 15:34:34 +0000184#endif
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500185
Scott Wood0164c0f2011-08-18 15:25:18 -0500186static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
187 int tlbsel, int esel)
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500188{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500189 struct kvm_book3e_206_tlb_entry *gtlbe =
190 get_entry(vcpu_e500, tlbsel, esel);
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500191
Scott Wood4f802fe2011-12-20 15:34:37 +0000192 if (tlbsel == 1 &&
193 vcpu_e500->gtlb_priv[1][esel].ref.flags & E500_TLB_BITMAP) {
194 u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
195 int hw_tlb_indx;
196 unsigned long flags;
197
198 local_irq_save(flags);
199 while (tmp) {
200 hw_tlb_indx = __ilog2_u64(tmp & -tmp);
201 mtspr(SPRN_MAS0,
202 MAS0_TLBSEL(1) |
203 MAS0_ESEL(to_htlb1_esel(hw_tlb_indx)));
204 mtspr(SPRN_MAS1, 0);
205 asm volatile("tlbwe");
206 vcpu_e500->h2g_tlb1_rmap[hw_tlb_indx] = 0;
207 tmp &= tmp - 1;
208 }
209 mb();
210 vcpu_e500->g2h_tlb1_map[esel] = 0;
211 vcpu_e500->gtlb_priv[1][esel].ref.flags &= ~E500_TLB_BITMAP;
212 local_irq_restore(flags);
213
Scott Wood8fdd21a22011-12-20 15:34:34 +0000214 return;
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500215 }
216
Scott Wood8fdd21a22011-12-20 15:34:34 +0000217 /* Guest tlbe is backed by at most one host tlbe per shadow pid. */
218 kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600219}
220
Scott Wood0164c0f2011-08-18 15:25:18 -0500221static int tlb0_set_base(gva_t addr, int sets, int ways)
222{
223 int set_base;
224
225 set_base = (addr >> PAGE_SHIFT) & (sets - 1);
226 set_base *= ways;
227
228 return set_base;
229}
230
231static int gtlb0_set_base(struct kvmppc_vcpu_e500 *vcpu_e500, gva_t addr)
232{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500233 return tlb0_set_base(addr, vcpu_e500->gtlb_params[0].sets,
234 vcpu_e500->gtlb_params[0].ways);
Scott Wood0164c0f2011-08-18 15:25:18 -0500235}
236
Scott Woodb5904972011-11-08 18:23:30 -0600237static unsigned int get_tlb_esel(struct kvm_vcpu *vcpu, int tlbsel)
Scott Wood0164c0f2011-08-18 15:25:18 -0500238{
Scott Woodb5904972011-11-08 18:23:30 -0600239 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
240 int esel = get_tlb_esel_bit(vcpu);
Scott Wood0164c0f2011-08-18 15:25:18 -0500241
242 if (tlbsel == 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500243 esel &= vcpu_e500->gtlb_params[0].ways - 1;
Scott Woodb5904972011-11-08 18:23:30 -0600244 esel += gtlb0_set_base(vcpu_e500, vcpu->arch.shared->mas2);
Scott Wood0164c0f2011-08-18 15:25:18 -0500245 } else {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500246 esel &= vcpu_e500->gtlb_params[tlbsel].entries - 1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500247 }
248
249 return esel;
250}
251
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600252/* Search the guest TLB for a matching entry. */
253static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500,
254 gva_t eaddr, int tlbsel, unsigned int pid, int as)
255{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500256 int size = vcpu_e500->gtlb_params[tlbsel].entries;
257 unsigned int set_base, offset;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600258 int i;
259
Scott Wood1aee47a2011-06-14 18:35:20 -0500260 if (tlbsel == 0) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500261 set_base = gtlb0_set_base(vcpu_e500, eaddr);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500262 size = vcpu_e500->gtlb_params[0].ways;
Scott Wood1aee47a2011-06-14 18:35:20 -0500263 } else {
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000264 if (eaddr < vcpu_e500->tlb1_min_eaddr ||
265 eaddr > vcpu_e500->tlb1_max_eaddr)
266 return -1;
Scott Wood1aee47a2011-06-14 18:35:20 -0500267 set_base = 0;
268 }
269
Scott Wooddc83b8b2011-08-18 15:25:21 -0500270 offset = vcpu_e500->gtlb_offset[tlbsel];
271
Scott Wood1aee47a2011-06-14 18:35:20 -0500272 for (i = 0; i < size; i++) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500273 struct kvm_book3e_206_tlb_entry *tlbe =
274 &vcpu_e500->gtlb_arch[offset + set_base + i];
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600275 unsigned int tid;
276
277 if (eaddr < get_tlb_eaddr(tlbe))
278 continue;
279
280 if (eaddr > get_tlb_end(tlbe))
281 continue;
282
283 tid = get_tlb_tid(tlbe);
284 if (tid && (tid != pid))
285 continue;
286
287 if (!get_tlb_v(tlbe))
288 continue;
289
290 if (get_tlb_ts(tlbe) != as && as != -1)
291 continue;
292
Scott Wood1aee47a2011-06-14 18:35:20 -0500293 return set_base + i;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600294 }
295
296 return -1;
297}
298
Scott Wood0164c0f2011-08-18 15:25:18 -0500299static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500300 struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood0164c0f2011-08-18 15:25:18 -0500301 pfn_t pfn)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600302{
Scott Wood0164c0f2011-08-18 15:25:18 -0500303 ref->pfn = pfn;
304 ref->flags = E500_TLB_VALID;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600305
Liu Yu08b7fa92011-06-14 18:34:59 -0500306 if (tlbe_is_writable(gtlbe))
Scott Wood0164c0f2011-08-18 15:25:18 -0500307 ref->flags |= E500_TLB_DIRTY;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600308}
309
Scott Wood0164c0f2011-08-18 15:25:18 -0500310static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600311{
Scott Wood0164c0f2011-08-18 15:25:18 -0500312 if (ref->flags & E500_TLB_VALID) {
313 if (ref->flags & E500_TLB_DIRTY)
314 kvm_release_pfn_dirty(ref->pfn);
Liu Yu08b7fa92011-06-14 18:34:59 -0500315 else
Scott Wood0164c0f2011-08-18 15:25:18 -0500316 kvm_release_pfn_clean(ref->pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600317
Scott Wood0164c0f2011-08-18 15:25:18 -0500318 ref->flags = 0;
Liu Yu08b7fa92011-06-14 18:34:59 -0500319 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600320}
321
Scott Wood4f802fe2011-12-20 15:34:37 +0000322static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
323{
324 if (vcpu_e500->g2h_tlb1_map)
325 memset(vcpu_e500->g2h_tlb1_map,
326 sizeof(u64) * vcpu_e500->gtlb_params[1].entries, 0);
327 if (vcpu_e500->h2g_tlb1_rmap)
328 memset(vcpu_e500->h2g_tlb1_rmap,
329 sizeof(unsigned int) * host_tlb_params[1].entries, 0);
330}
331
Scott Wood0164c0f2011-08-18 15:25:18 -0500332static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
333{
334 int tlbsel = 0;
335 int i;
336
Scott Wooddc83b8b2011-08-18 15:25:21 -0500337 for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500338 struct tlbe_ref *ref =
339 &vcpu_e500->gtlb_priv[tlbsel][i].ref;
340 kvmppc_e500_ref_release(ref);
341 }
342}
343
344static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
345{
346 int stlbsel = 1;
347 int i;
348
Scott Wood8fdd21a22011-12-20 15:34:34 +0000349 kvmppc_e500_tlbil_all(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500350
Scott Wood0164c0f2011-08-18 15:25:18 -0500351 for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
352 struct tlbe_ref *ref =
353 &vcpu_e500->tlb_refs[stlbsel][i];
354 kvmppc_e500_ref_release(ref);
355 }
356
357 clear_tlb_privs(vcpu_e500);
358}
359
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600360static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
361 unsigned int eaddr, int as)
362{
363 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000364 unsigned int victim, tsized;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600365 int tlbsel;
366
Liu Yufb2838d2009-01-14 10:47:37 -0600367 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600368 tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500369 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Scott Woodb5904972011-11-08 18:23:30 -0600370 tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600371
Scott Woodb5904972011-11-08 18:23:30 -0600372 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500373 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600374 vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
Scott Wood8fdd21a22011-12-20 15:34:34 +0000375 | MAS1_TID(get_tlbmiss_tid(vcpu))
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600376 | MAS1_TSIZE(tsized);
Scott Woodb5904972011-11-08 18:23:30 -0600377 vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
378 | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
379 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
380 vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600381 | (get_cur_pid(vcpu) << 16)
382 | (as ? MAS6_SAS : 0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600383}
384
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500385/* TID must be supplied by the caller */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500386static inline void kvmppc_e500_setup_stlbe(
Scott Wood8fdd21a22011-12-20 15:34:34 +0000387 struct kvm_vcpu *vcpu,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500388 struct kvm_book3e_206_tlb_entry *gtlbe,
389 int tsize, struct tlbe_ref *ref, u64 gvaddr,
390 struct kvm_book3e_206_tlb_entry *stlbe)
Liu Yu08b7fa92011-06-14 18:34:59 -0500391{
Scott Wood0164c0f2011-08-18 15:25:18 -0500392 pfn_t pfn = ref->pfn;
Scott Wood8fdd21a22011-12-20 15:34:34 +0000393 u32 pr = vcpu->arch.shared->msr & MSR_PR;
Scott Wood0164c0f2011-08-18 15:25:18 -0500394
395 BUG_ON(!(ref->flags & E500_TLB_VALID));
Liu Yu08b7fa92011-06-14 18:34:59 -0500396
Scott Wood8fdd21a22011-12-20 15:34:34 +0000397 /* Force IPROT=0 for all guest mappings. */
398 stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
399 stlbe->mas2 = (gvaddr & MAS2_EPN) |
400 e500_shadow_mas2_attrib(gtlbe->mas2, pr);
401 stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
402 e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
Scott Wood73196cd2011-12-20 15:34:47 +0000403
404#ifdef CONFIG_KVM_BOOKE_HV
405 stlbe->mas8 = MAS8_TGS | vcpu->kvm->arch.lpid;
406#endif
Liu Yu08b7fa92011-06-14 18:34:59 -0500407}
408
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600409static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500410 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood57013522011-11-29 10:40:23 +0000411 int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500412 struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600413{
Scott Wood9973d542011-06-14 18:34:39 -0500414 struct kvm_memory_slot *slot;
Scott Wood9973d542011-06-14 18:34:39 -0500415 unsigned long pfn, hva;
416 int pfnmap = 0;
417 int tsize = BOOK3E_PAGESZ_4K;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600418
Scott Wood59c1f4e2011-06-14 18:34:37 -0500419 /*
420 * Translate guest physical to true physical, acquiring
421 * a page reference if it is normal, non-reserved memory.
Scott Wood9973d542011-06-14 18:34:39 -0500422 *
423 * gfn_to_memslot() must succeed because otherwise we wouldn't
424 * have gotten this far. Eventually we should just pass the slot
425 * pointer through from the first lookup.
Scott Wood59c1f4e2011-06-14 18:34:37 -0500426 */
Scott Wood9973d542011-06-14 18:34:39 -0500427 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
428 hva = gfn_to_hva_memslot(slot, gfn);
429
430 if (tlbsel == 1) {
431 struct vm_area_struct *vma;
432 down_read(&current->mm->mmap_sem);
433
434 vma = find_vma(current->mm, hva);
435 if (vma && hva >= vma->vm_start &&
436 (vma->vm_flags & VM_PFNMAP)) {
437 /*
438 * This VMA is a physically contiguous region (e.g.
439 * /dev/mem) that bypasses normal Linux page
440 * management. Find the overlap between the
441 * vma and the memslot.
442 */
443
444 unsigned long start, end;
445 unsigned long slot_start, slot_end;
446
447 pfnmap = 1;
448
449 start = vma->vm_pgoff;
450 end = start +
451 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
452
453 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
454
455 slot_start = pfn - (gfn - slot->base_gfn);
456 slot_end = slot_start + slot->npages;
457
458 if (start < slot_start)
459 start = slot_start;
460 if (end > slot_end)
461 end = slot_end;
462
463 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
464 MAS1_TSIZE_SHIFT;
465
466 /*
467 * e500 doesn't implement the lowest tsize bit,
468 * or 1K pages.
469 */
470 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
471
472 /*
473 * Now find the largest tsize (up to what the guest
474 * requested) that will cover gfn, stay within the
475 * range, and for which gfn and pfn are mutually
476 * aligned.
477 */
478
479 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
480 unsigned long gfn_start, gfn_end, tsize_pages;
481 tsize_pages = 1 << (tsize - 2);
482
483 gfn_start = gfn & ~(tsize_pages - 1);
484 gfn_end = gfn_start + tsize_pages;
485
486 if (gfn_start + pfn - gfn < start)
487 continue;
488 if (gfn_end + pfn - gfn > end)
489 continue;
490 if ((gfn & (tsize_pages - 1)) !=
491 (pfn & (tsize_pages - 1)))
492 continue;
493
494 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
495 pfn &= ~(tsize_pages - 1);
496 break;
497 }
Alexander Graf95325e62011-09-20 01:31:48 +0200498 } else if (vma && hva >= vma->vm_start &&
499 (vma->vm_flags & VM_HUGETLB)) {
500 unsigned long psize = vma_kernel_pagesize(vma);
501
502 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
503 MAS1_TSIZE_SHIFT;
504
505 /*
506 * Take the largest page size that satisfies both host
507 * and guest mapping
508 */
509 tsize = min(__ilog2(psize) - 10, tsize);
510
511 /*
512 * e500 doesn't implement the lowest tsize bit,
513 * or 1K pages.
514 */
515 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
Scott Wood9973d542011-06-14 18:34:39 -0500516 }
517
518 up_read(&current->mm->mmap_sem);
519 }
520
521 if (likely(!pfnmap)) {
Alexander Graf95325e62011-09-20 01:31:48 +0200522 unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
Scott Wood9973d542011-06-14 18:34:39 -0500523 pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
524 if (is_error_pfn(pfn)) {
525 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
526 (long)gfn);
527 kvm_release_pfn_clean(pfn);
528 return;
529 }
Alexander Graf95325e62011-09-20 01:31:48 +0200530
531 /* Align guest and physical address to page map boundaries */
532 pfn &= ~(tsize_pages - 1);
533 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600534 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600535
Scott Wood0164c0f2011-08-18 15:25:18 -0500536 /* Drop old ref and setup new one. */
537 kvmppc_e500_ref_release(ref);
538 kvmppc_e500_ref_setup(ref, gtlbe, pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600539
Scott Wood8fdd21a22011-12-20 15:34:34 +0000540 kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
541 ref, gvaddr, stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600542}
543
544/* XXX only map the one-one case, for now use TLB0 */
Scott Wood57013522011-11-29 10:40:23 +0000545static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
546 int esel,
547 struct kvm_book3e_206_tlb_entry *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600548{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500549 struct kvm_book3e_206_tlb_entry *gtlbe;
Scott Wood0164c0f2011-08-18 15:25:18 -0500550 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600551
Scott Wooddc83b8b2011-08-18 15:25:21 -0500552 gtlbe = get_entry(vcpu_e500, 0, esel);
Scott Wood0164c0f2011-08-18 15:25:18 -0500553 ref = &vcpu_e500->gtlb_priv[0][esel].ref;
554
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600555 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
556 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
Scott Wood57013522011-11-29 10:40:23 +0000557 gtlbe, 0, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600558}
559
560/* Caller must ensure that the specified guest TLB entry is safe to insert into
561 * the shadow TLB. */
562/* XXX for both one-one and one-to-many , for now use TLB1 */
563static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500564 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood4f802fe2011-12-20 15:34:37 +0000565 struct kvm_book3e_206_tlb_entry *stlbe, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600566{
Scott Wood0164c0f2011-08-18 15:25:18 -0500567 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600568 unsigned int victim;
569
Scott Wood0164c0f2011-08-18 15:25:18 -0500570 victim = vcpu_e500->host_tlb1_nv++;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600571
Scott Wood0164c0f2011-08-18 15:25:18 -0500572 if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
573 vcpu_e500->host_tlb1_nv = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600574
Scott Wood0164c0f2011-08-18 15:25:18 -0500575 ref = &vcpu_e500->tlb_refs[1][victim];
Scott Wood57013522011-11-29 10:40:23 +0000576 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600577
Scott Wood4f802fe2011-12-20 15:34:37 +0000578 vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << victim;
579 vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
580 if (vcpu_e500->h2g_tlb1_rmap[victim]) {
581 unsigned int idx = vcpu_e500->h2g_tlb1_rmap[victim];
582 vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << victim);
583 }
584 vcpu_e500->h2g_tlb1_rmap[victim] = esel;
585
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600586 return victim;
587}
588
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000589static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500)
590{
591 int size = vcpu_e500->gtlb_params[1].entries;
592 unsigned int offset;
593 gva_t eaddr;
594 int i;
595
596 vcpu_e500->tlb1_min_eaddr = ~0UL;
597 vcpu_e500->tlb1_max_eaddr = 0;
598 offset = vcpu_e500->gtlb_offset[1];
599
600 for (i = 0; i < size; i++) {
601 struct kvm_book3e_206_tlb_entry *tlbe =
602 &vcpu_e500->gtlb_arch[offset + i];
603
604 if (!get_tlb_v(tlbe))
605 continue;
606
607 eaddr = get_tlb_eaddr(tlbe);
608 vcpu_e500->tlb1_min_eaddr =
609 min(vcpu_e500->tlb1_min_eaddr, eaddr);
610
611 eaddr = get_tlb_end(tlbe);
612 vcpu_e500->tlb1_max_eaddr =
613 max(vcpu_e500->tlb1_max_eaddr, eaddr);
614 }
615}
616
617static int kvmppc_need_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500,
618 struct kvm_book3e_206_tlb_entry *gtlbe)
619{
620 unsigned long start, end, size;
621
622 size = get_tlb_bytes(gtlbe);
623 start = get_tlb_eaddr(gtlbe) & ~(size - 1);
624 end = start + size - 1;
625
626 return vcpu_e500->tlb1_min_eaddr == start ||
627 vcpu_e500->tlb1_max_eaddr == end;
628}
629
630/* This function is supposed to be called for a adding a new valid tlb entry */
631static void kvmppc_set_tlb1map_range(struct kvm_vcpu *vcpu,
632 struct kvm_book3e_206_tlb_entry *gtlbe)
633{
634 unsigned long start, end, size;
635 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
636
637 if (!get_tlb_v(gtlbe))
638 return;
639
640 size = get_tlb_bytes(gtlbe);
641 start = get_tlb_eaddr(gtlbe) & ~(size - 1);
642 end = start + size - 1;
643
644 vcpu_e500->tlb1_min_eaddr = min(vcpu_e500->tlb1_min_eaddr, start);
645 vcpu_e500->tlb1_max_eaddr = max(vcpu_e500->tlb1_max_eaddr, end);
646}
647
Liu Yu08b7fa92011-06-14 18:34:59 -0500648static inline int kvmppc_e500_gtlbe_invalidate(
649 struct kvmppc_vcpu_e500 *vcpu_e500,
650 int tlbsel, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600651{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500652 struct kvm_book3e_206_tlb_entry *gtlbe =
653 get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600654
655 if (unlikely(get_tlb_iprot(gtlbe)))
656 return -1;
657
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000658 if (tlbsel == 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
659 kvmppc_recalc_tlb1map_range(vcpu_e500);
660
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600661 gtlbe->mas1 = 0;
662
663 return 0;
664}
665
Liu Yub0a18352009-02-17 16:52:08 +0800666int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
667{
668 int esel;
669
670 if (value & MMUCSR0_TLB0FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500671 for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800672 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
673 if (value & MMUCSR0_TLB1FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500674 for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800675 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
676
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500677 /* Invalidate all vcpu id mappings */
Scott Wood8fdd21a22011-12-20 15:34:34 +0000678 kvmppc_e500_tlbil_all(vcpu_e500);
Liu Yub0a18352009-02-17 16:52:08 +0800679
680 return EMULATE_DONE;
681}
682
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600683int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
684{
685 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
686 unsigned int ia;
687 int esel, tlbsel;
688 gva_t ea;
689
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100690 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600691
692 ia = (ea >> 2) & 0x1;
693
Liu Yufb2838d2009-01-14 10:47:37 -0600694 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600695 tlbsel = (ea >> 3) & 0x1;
696
697 if (ia) {
698 /* invalidate all entries */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500699 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
700 esel++)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600701 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
702 } else {
703 ea &= 0xfffff000;
704 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
705 get_cur_pid(vcpu), -1);
706 if (esel >= 0)
707 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
708 }
709
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500710 /* Invalidate all vcpu id mappings */
Scott Wood8fdd21a22011-12-20 15:34:34 +0000711 kvmppc_e500_tlbil_all(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600712
713 return EMULATE_DONE;
714}
715
Scott Woodab9fc402011-12-20 15:34:39 +0000716static void tlbilx_all(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
717 int pid, int rt)
718{
719 struct kvm_book3e_206_tlb_entry *tlbe;
720 int tid, esel;
721
722 /* invalidate all entries */
723 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; esel++) {
724 tlbe = get_entry(vcpu_e500, tlbsel, esel);
725 tid = get_tlb_tid(tlbe);
726 if (rt == 0 || tid == pid) {
727 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
728 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
729 }
730 }
731}
732
733static void tlbilx_one(struct kvmppc_vcpu_e500 *vcpu_e500, int pid,
734 int ra, int rb)
735{
736 int tlbsel, esel;
737 gva_t ea;
738
739 ea = kvmppc_get_gpr(&vcpu_e500->vcpu, rb);
740 if (ra)
741 ea += kvmppc_get_gpr(&vcpu_e500->vcpu, ra);
742
743 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
744 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1);
745 if (esel >= 0) {
746 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
747 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
748 break;
749 }
750 }
751}
752
753int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int rt, int ra, int rb)
754{
755 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
756 int pid = get_cur_spid(vcpu);
757
758 if (rt == 0 || rt == 1) {
759 tlbilx_all(vcpu_e500, 0, pid, rt);
760 tlbilx_all(vcpu_e500, 1, pid, rt);
761 } else if (rt == 3) {
762 tlbilx_one(vcpu_e500, pid, ra, rb);
763 }
764
765 return EMULATE_DONE;
766}
767
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600768int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
769{
770 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
771 int tlbsel, esel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500772 struct kvm_book3e_206_tlb_entry *gtlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600773
Scott Woodb5904972011-11-08 18:23:30 -0600774 tlbsel = get_tlb_tlbsel(vcpu);
775 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600776
Scott Wooddc83b8b2011-08-18 15:25:21 -0500777 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Scott Woodb5904972011-11-08 18:23:30 -0600778 vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
779 vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
780 vcpu->arch.shared->mas1 = gtlbe->mas1;
781 vcpu->arch.shared->mas2 = gtlbe->mas2;
782 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600783
784 return EMULATE_DONE;
785}
786
787int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
788{
789 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Woodb5904972011-11-08 18:23:30 -0600790 int as = !!get_cur_sas(vcpu);
791 unsigned int pid = get_cur_spid(vcpu);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600792 int esel, tlbsel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500793 struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600794 gva_t ea;
795
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100796 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600797
798 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
799 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
800 if (esel >= 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500801 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600802 break;
803 }
804 }
805
806 if (gtlbe) {
Scott Wood303b7c92011-08-18 15:25:23 -0500807 esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
808
Scott Woodb5904972011-11-08 18:23:30 -0600809 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
Liu Yu08b7fa92011-06-14 18:34:59 -0500810 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600811 vcpu->arch.shared->mas1 = gtlbe->mas1;
812 vcpu->arch.shared->mas2 = gtlbe->mas2;
813 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600814 } else {
815 int victim;
816
Liu Yufb2838d2009-01-14 10:47:37 -0600817 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600818 tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500819 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600820
Scott Woodb5904972011-11-08 18:23:30 -0600821 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
822 | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500823 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600824 vcpu->arch.shared->mas1 =
825 (vcpu->arch.shared->mas6 & MAS6_SPID0)
826 | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
827 | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
828 vcpu->arch.shared->mas2 &= MAS2_EPN;
829 vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
830 MAS2_ATTRIB_MASK;
831 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
832 MAS3_U2 | MAS3_U3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600833 }
834
Scott Wood49ea0692011-03-28 15:01:24 -0500835 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600836 return EMULATE_DONE;
837}
838
Scott Wood57013522011-11-29 10:40:23 +0000839/* sesel is for tlb1 only */
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500840static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500841 struct kvm_book3e_206_tlb_entry *gtlbe,
842 struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500843 int stlbsel, int sesel)
844{
845 int stid;
846
847 preempt_disable();
Scott Wood8fdd21a22011-12-20 15:34:34 +0000848 stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500849
850 stlbe->mas1 |= MAS1_TID(stid);
851 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
852 preempt_enable();
853}
854
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600855int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
856{
857 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000858 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
859 int tlbsel, esel, stlbsel, sesel;
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000860 int recal = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600861
Scott Woodb5904972011-11-08 18:23:30 -0600862 tlbsel = get_tlb_tlbsel(vcpu);
863 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600864
Scott Wooddc83b8b2011-08-18 15:25:21 -0500865 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600866
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000867 if (get_tlb_v(gtlbe)) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500868 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000869 if ((tlbsel == 1) &&
870 kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
871 recal = 1;
872 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600873
Scott Woodb5904972011-11-08 18:23:30 -0600874 gtlbe->mas1 = vcpu->arch.shared->mas1;
875 gtlbe->mas2 = vcpu->arch.shared->mas2;
876 gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600877
Liu Yud37b1a02011-12-20 14:42:56 +0000878 trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
879 gtlbe->mas2, gtlbe->mas7_3);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600880
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000881 if (tlbsel == 1) {
882 /*
883 * If a valid tlb1 entry is overwritten then recalculate the
884 * min/max TLB1 map address range otherwise no need to look
885 * in tlb1 array.
886 */
887 if (recal)
888 kvmppc_recalc_tlb1map_range(vcpu_e500);
889 else
890 kvmppc_set_tlb1map_range(vcpu, gtlbe);
891 }
892
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600893 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
894 if (tlbe_is_host_safe(vcpu, gtlbe)) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500895 u64 eaddr;
896 u64 raddr;
897
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600898 switch (tlbsel) {
899 case 0:
900 /* TLB0 */
901 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800902 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600903
904 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +0000905 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
906 sesel = 0; /* unused */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600907
908 break;
909
910 case 1:
911 /* TLB1 */
912 eaddr = get_tlb_eaddr(gtlbe);
913 raddr = get_tlb_raddr(gtlbe);
914
915 /* Create a 4KB mapping on the host.
916 * If the guest wanted a large page,
917 * only the first 4KB is mapped here and the rest
918 * are mapped on the fly. */
919 stlbsel = 1;
920 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
Scott Wood4f802fe2011-12-20 15:34:37 +0000921 raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600922 break;
923
924 default:
925 BUG();
926 }
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500927
928 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600929 }
930
Scott Wood49ea0692011-03-28 15:01:24 -0500931 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600932 return EMULATE_DONE;
933}
934
Scott Wood8fdd21a22011-12-20 15:34:34 +0000935static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
936 gva_t eaddr, unsigned int pid, int as)
937{
938 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
939 int esel, tlbsel;
940
941 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
942 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
943 if (esel >= 0)
944 return index_of(tlbsel, esel);
945 }
946
947 return -1;
948}
949
950/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
951int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
952 struct kvm_translation *tr)
953{
954 int index;
955 gva_t eaddr;
956 u8 pid;
957 u8 as;
958
959 eaddr = tr->linear_address;
960 pid = (tr->linear_address >> 32) & 0xff;
961 as = (tr->linear_address >> 40) & 0x1;
962
963 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
964 if (index < 0) {
965 tr->valid = 0;
966 return 0;
967 }
968
969 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
970 /* XXX what does "writeable" and "usermode" even mean? */
971 tr->valid = 1;
972
973 return 0;
974}
975
976
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600977int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
978{
Alexander Graf666e7252010-07-29 14:47:43 +0200979 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600980
981 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
982}
983
984int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
985{
Alexander Graf666e7252010-07-29 14:47:43 +0200986 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600987
988 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
989}
990
991void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
992{
Alexander Graf666e7252010-07-29 14:47:43 +0200993 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600994
995 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
996}
997
998void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
999{
Alexander Graf666e7252010-07-29 14:47:43 +02001000 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001001
1002 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
1003}
1004
1005gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
1006 gva_t eaddr)
1007{
1008 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001009 struct kvm_book3e_206_tlb_entry *gtlbe;
1010 u64 pgmask;
1011
1012 gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
1013 pgmask = get_tlb_bytes(gtlbe) - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001014
1015 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
1016}
1017
1018void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1019{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001020}
1021
1022void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
1023 unsigned int index)
1024{
1025 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Liu Yu08b7fa92011-06-14 18:34:59 -05001026 struct tlbe_priv *priv;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001027 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001028 int tlbsel = tlbsel_of(index);
1029 int esel = esel_of(index);
1030 int stlbsel, sesel;
1031
Scott Wooddc83b8b2011-08-18 15:25:21 -05001032 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Liu Yu08b7fa92011-06-14 18:34:59 -05001033
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001034 switch (tlbsel) {
1035 case 0:
1036 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +00001037 sesel = 0; /* unused */
Scott Wood0164c0f2011-08-18 15:25:18 -05001038 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
Liu Yu08b7fa92011-06-14 18:34:59 -05001039
Scott Wood8fdd21a22011-12-20 15:34:34 +00001040 kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
Scott Wood0164c0f2011-08-18 15:25:18 -05001041 &priv->ref, eaddr, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001042 break;
1043
1044 case 1: {
1045 gfn_t gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001046
1047 stlbsel = 1;
Liu Yu08b7fa92011-06-14 18:34:59 -05001048 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
Scott Wood4f802fe2011-12-20 15:34:37 +00001049 gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001050 break;
1051 }
1052
1053 default:
1054 BUG();
1055 break;
1056 }
Liu Yu08b7fa92011-06-14 18:34:59 -05001057
Scott Wood3bf3cdc2011-08-18 15:25:14 -05001058 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001059}
1060
Scott Wooddc83b8b2011-08-18 15:25:21 -05001061static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
1062{
1063 int i;
1064
Scott Wood4f802fe2011-12-20 15:34:37 +00001065 clear_tlb1_bitmap(vcpu_e500);
1066 kfree(vcpu_e500->g2h_tlb1_map);
1067
Scott Wooddc83b8b2011-08-18 15:25:21 -05001068 clear_tlb_refs(vcpu_e500);
1069 kfree(vcpu_e500->gtlb_priv[0]);
1070 kfree(vcpu_e500->gtlb_priv[1]);
1071
1072 if (vcpu_e500->shared_tlb_pages) {
1073 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
1074 PAGE_SIZE)));
1075
1076 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
1077 set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
1078 put_page(vcpu_e500->shared_tlb_pages[i]);
1079 }
1080
1081 vcpu_e500->num_shared_tlb_pages = 0;
1082 vcpu_e500->shared_tlb_pages = NULL;
1083 } else {
1084 kfree(vcpu_e500->gtlb_arch);
1085 }
1086
1087 vcpu_e500->gtlb_arch = NULL;
1088}
1089
Scott Wood8fdd21a22011-12-20 15:34:34 +00001090void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1091{
1092 sregs->u.e.mas0 = vcpu->arch.shared->mas0;
1093 sregs->u.e.mas1 = vcpu->arch.shared->mas1;
1094 sregs->u.e.mas2 = vcpu->arch.shared->mas2;
1095 sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
1096 sregs->u.e.mas4 = vcpu->arch.shared->mas4;
1097 sregs->u.e.mas6 = vcpu->arch.shared->mas6;
1098
1099 sregs->u.e.mmucfg = vcpu->arch.mmucfg;
1100 sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
1101 sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
1102 sregs->u.e.tlbcfg[2] = 0;
1103 sregs->u.e.tlbcfg[3] = 0;
1104}
1105
1106int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1107{
1108 if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
1109 vcpu->arch.shared->mas0 = sregs->u.e.mas0;
1110 vcpu->arch.shared->mas1 = sregs->u.e.mas1;
1111 vcpu->arch.shared->mas2 = sregs->u.e.mas2;
1112 vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
1113 vcpu->arch.shared->mas4 = sregs->u.e.mas4;
1114 vcpu->arch.shared->mas6 = sregs->u.e.mas6;
1115 }
1116
1117 return 0;
1118}
1119
Scott Wooddc83b8b2011-08-18 15:25:21 -05001120int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
1121 struct kvm_config_tlb *cfg)
1122{
1123 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1124 struct kvm_book3e_206_tlb_params params;
1125 char *virt;
1126 struct page **pages;
1127 struct tlbe_priv *privs[2] = {};
Scott Wood4f802fe2011-12-20 15:34:37 +00001128 u64 *g2h_bitmap = NULL;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001129 size_t array_len;
1130 u32 sets;
1131 int num_pages, ret, i;
1132
1133 if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
1134 return -EINVAL;
1135
1136 if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
1137 sizeof(params)))
1138 return -EFAULT;
1139
1140 if (params.tlb_sizes[1] > 64)
1141 return -EINVAL;
1142 if (params.tlb_ways[1] != params.tlb_sizes[1])
1143 return -EINVAL;
1144 if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
1145 return -EINVAL;
1146 if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
1147 return -EINVAL;
1148
1149 if (!is_power_of_2(params.tlb_ways[0]))
1150 return -EINVAL;
1151
1152 sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1153 if (!is_power_of_2(sets))
1154 return -EINVAL;
1155
1156 array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1157 array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1158
1159 if (cfg->array_len < array_len)
1160 return -EINVAL;
1161
1162 num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1163 cfg->array / PAGE_SIZE;
1164 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1165 if (!pages)
1166 return -ENOMEM;
1167
1168 ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1169 if (ret < 0)
1170 goto err_pages;
1171
1172 if (ret != num_pages) {
1173 num_pages = ret;
1174 ret = -EFAULT;
1175 goto err_put_page;
1176 }
1177
1178 virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1179 if (!virt)
1180 goto err_put_page;
1181
1182 privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1183 GFP_KERNEL);
1184 privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1185 GFP_KERNEL);
1186
1187 if (!privs[0] || !privs[1])
1188 goto err_put_page;
1189
Scott Wood4f802fe2011-12-20 15:34:37 +00001190 g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
1191 GFP_KERNEL);
1192 if (!g2h_bitmap)
1193 goto err_put_page;
1194
Scott Wooddc83b8b2011-08-18 15:25:21 -05001195 free_gtlb(vcpu_e500);
1196
1197 vcpu_e500->gtlb_priv[0] = privs[0];
1198 vcpu_e500->gtlb_priv[1] = privs[1];
Scott Wood4f802fe2011-12-20 15:34:37 +00001199 vcpu_e500->g2h_tlb1_map = g2h_bitmap;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001200
1201 vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1202 (virt + (cfg->array & (PAGE_SIZE - 1)));
1203
1204 vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1205 vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1206
1207 vcpu_e500->gtlb_offset[0] = 0;
1208 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1209
Scott Wood8fdd21a22011-12-20 15:34:34 +00001210 vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001211
Scott Wood8fdd21a22011-12-20 15:34:34 +00001212 vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1213 if (params.tlb_sizes[0] <= 2048)
1214 vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
1215 vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
1216
1217 vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1218 vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
1219 vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001220
1221 vcpu_e500->shared_tlb_pages = pages;
1222 vcpu_e500->num_shared_tlb_pages = num_pages;
1223
1224 vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1225 vcpu_e500->gtlb_params[0].sets = sets;
1226
1227 vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1228 vcpu_e500->gtlb_params[1].sets = 1;
1229
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001230 kvmppc_recalc_tlb1map_range(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001231 return 0;
1232
1233err_put_page:
1234 kfree(privs[0]);
1235 kfree(privs[1]);
1236
1237 for (i = 0; i < num_pages; i++)
1238 put_page(pages[i]);
1239
1240err_pages:
1241 kfree(pages);
1242 return ret;
1243}
1244
1245int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1246 struct kvm_dirty_tlb *dirty)
1247{
1248 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001249 kvmppc_recalc_tlb1map_range(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001250 clear_tlb_refs(vcpu_e500);
1251 return 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001252}
1253
1254int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1255{
Scott Wood8fdd21a22011-12-20 15:34:34 +00001256 struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001257 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1258 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1259
Scott Wood0164c0f2011-08-18 15:25:18 -05001260 host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1261 host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1262
1263 /*
1264 * This should never happen on real e500 hardware, but is
1265 * architecturally possible -- e.g. in some weird nested
1266 * virtualization case.
1267 */
1268 if (host_tlb_params[0].entries == 0 ||
1269 host_tlb_params[1].entries == 0) {
1270 pr_err("%s: need to know host tlb size\n", __func__);
1271 return -ENODEV;
1272 }
1273
1274 host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1275 TLBnCFG_ASSOC_SHIFT;
1276 host_tlb_params[1].ways = host_tlb_params[1].entries;
1277
1278 if (!is_power_of_2(host_tlb_params[0].entries) ||
1279 !is_power_of_2(host_tlb_params[0].ways) ||
1280 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1281 host_tlb_params[0].ways == 0) {
1282 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1283 __func__, host_tlb_params[0].entries,
1284 host_tlb_params[0].ways);
1285 return -ENODEV;
1286 }
1287
1288 host_tlb_params[0].sets =
1289 host_tlb_params[0].entries / host_tlb_params[0].ways;
1290 host_tlb_params[1].sets = 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001291
Scott Wooddc83b8b2011-08-18 15:25:21 -05001292 vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1293 vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001294
Scott Wooddc83b8b2011-08-18 15:25:21 -05001295 vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1296 vcpu_e500->gtlb_params[0].sets =
1297 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1298
1299 vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1300 vcpu_e500->gtlb_params[1].sets = 1;
1301
1302 vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1303 if (!vcpu_e500->gtlb_arch)
1304 return -ENOMEM;
1305
1306 vcpu_e500->gtlb_offset[0] = 0;
1307 vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001308
Scott Wood0164c0f2011-08-18 15:25:18 -05001309 vcpu_e500->tlb_refs[0] =
1310 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1311 GFP_KERNEL);
1312 if (!vcpu_e500->tlb_refs[0])
1313 goto err;
Liu Yu08b7fa92011-06-14 18:34:59 -05001314
Scott Wood0164c0f2011-08-18 15:25:18 -05001315 vcpu_e500->tlb_refs[1] =
1316 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1317 GFP_KERNEL);
1318 if (!vcpu_e500->tlb_refs[1])
1319 goto err;
1320
Scott Wooddc83b8b2011-08-18 15:25:21 -05001321 vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1322 vcpu_e500->gtlb_params[0].entries,
1323 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001324 if (!vcpu_e500->gtlb_priv[0])
1325 goto err;
1326
Scott Wooddc83b8b2011-08-18 15:25:21 -05001327 vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1328 vcpu_e500->gtlb_params[1].entries,
1329 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001330 if (!vcpu_e500->gtlb_priv[1])
1331 goto err;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001332
Scott Wood4f802fe2011-12-20 15:34:37 +00001333 vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(unsigned int) *
1334 vcpu_e500->gtlb_params[1].entries,
1335 GFP_KERNEL);
1336 if (!vcpu_e500->g2h_tlb1_map)
1337 goto err;
1338
1339 vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
1340 host_tlb_params[1].entries,
1341 GFP_KERNEL);
1342 if (!vcpu_e500->h2g_tlb1_rmap)
1343 goto err;
1344
Liu Yuda15bf42010-01-22 19:36:53 +08001345 /* Init TLB configuration register */
Scott Wood8fdd21a22011-12-20 15:34:34 +00001346 vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001347 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wood8fdd21a22011-12-20 15:34:34 +00001348 vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
1349 vcpu->arch.tlbcfg[0] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001350 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
1351
Scott Wood8fdd21a22011-12-20 15:34:34 +00001352 vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001353 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Alexander Grafc6b37332012-02-20 17:48:47 +01001354 vcpu->arch.tlbcfg[1] |= vcpu_e500->gtlb_params[1].entries;
1355 vcpu->arch.tlbcfg[1] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001356 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
Liu Yuda15bf42010-01-22 19:36:53 +08001357
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001358 kvmppc_recalc_tlb1map_range(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001359 return 0;
1360
Scott Wood0164c0f2011-08-18 15:25:18 -05001361err:
Scott Wooddc83b8b2011-08-18 15:25:21 -05001362 free_gtlb(vcpu_e500);
Scott Wood0164c0f2011-08-18 15:25:18 -05001363 kfree(vcpu_e500->tlb_refs[0]);
1364 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001365 return -1;
1366}
1367
1368void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1369{
Scott Wooddc83b8b2011-08-18 15:25:21 -05001370 free_gtlb(vcpu_e500);
Scott Wood4f802fe2011-12-20 15:34:37 +00001371 kfree(vcpu_e500->h2g_tlb1_rmap);
Scott Wood0164c0f2011-08-18 15:25:18 -05001372 kfree(vcpu_e500->tlb_refs[0]);
1373 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001374}