blob: a27d134eef3674f98ba1535931582c8106e4291c [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
Alexander Graf430c7ff2012-08-15 11:42:07 +0200306 if (tlbe_is_writable(gtlbe))
Alexander Graf862d31f2012-07-31 00:19:50 +0200307 kvm_set_pfn_dirty(pfn);
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) {
Alexander Graf63460462012-08-08 00:44:52 +0200313 trace_kvm_booke206_ref_release(ref->pfn, ref->flags);
Scott Wood0164c0f2011-08-18 15:25:18 -0500314 ref->flags = 0;
Liu Yu08b7fa92011-06-14 18:34:59 -0500315 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600316}
317
Scott Wood4f802fe2011-12-20 15:34:37 +0000318static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
319{
320 if (vcpu_e500->g2h_tlb1_map)
Alan Coxe8143cc2012-08-14 12:10:09 +0000321 memset(vcpu_e500->g2h_tlb1_map, 0,
322 sizeof(u64) * vcpu_e500->gtlb_params[1].entries);
Scott Wood4f802fe2011-12-20 15:34:37 +0000323 if (vcpu_e500->h2g_tlb1_rmap)
Alan Coxe8143cc2012-08-14 12:10:09 +0000324 memset(vcpu_e500->h2g_tlb1_rmap, 0,
325 sizeof(unsigned int) * host_tlb_params[1].entries);
Scott Wood4f802fe2011-12-20 15:34:37 +0000326}
327
Scott Wood0164c0f2011-08-18 15:25:18 -0500328static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
329{
330 int tlbsel = 0;
331 int i;
332
Scott Wooddc83b8b2011-08-18 15:25:21 -0500333 for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500334 struct tlbe_ref *ref =
335 &vcpu_e500->gtlb_priv[tlbsel][i].ref;
336 kvmppc_e500_ref_release(ref);
337 }
338}
339
340static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
341{
342 int stlbsel = 1;
343 int i;
344
Scott Wood8fdd21a22011-12-20 15:34:34 +0000345 kvmppc_e500_tlbil_all(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500346
Scott Wood0164c0f2011-08-18 15:25:18 -0500347 for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
348 struct tlbe_ref *ref =
349 &vcpu_e500->tlb_refs[stlbsel][i];
350 kvmppc_e500_ref_release(ref);
351 }
352
353 clear_tlb_privs(vcpu_e500);
354}
355
Alexander Graf862d31f2012-07-31 00:19:50 +0200356void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu)
357{
358 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
359 clear_tlb_refs(vcpu_e500);
360 clear_tlb1_bitmap(vcpu_e500);
361}
362
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600363static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
364 unsigned int eaddr, int as)
365{
366 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000367 unsigned int victim, tsized;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600368 int tlbsel;
369
Liu Yufb2838d2009-01-14 10:47:37 -0600370 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600371 tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500372 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Scott Woodb5904972011-11-08 18:23:30 -0600373 tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600374
Scott Woodb5904972011-11-08 18:23:30 -0600375 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500376 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600377 vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
Scott Wood8fdd21a22011-12-20 15:34:34 +0000378 | MAS1_TID(get_tlbmiss_tid(vcpu))
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600379 | MAS1_TSIZE(tsized);
Scott Woodb5904972011-11-08 18:23:30 -0600380 vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
381 | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
382 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
383 vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600384 | (get_cur_pid(vcpu) << 16)
385 | (as ? MAS6_SAS : 0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600386}
387
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500388/* TID must be supplied by the caller */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500389static inline void kvmppc_e500_setup_stlbe(
Scott Wood8fdd21a22011-12-20 15:34:34 +0000390 struct kvm_vcpu *vcpu,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500391 struct kvm_book3e_206_tlb_entry *gtlbe,
392 int tsize, struct tlbe_ref *ref, u64 gvaddr,
393 struct kvm_book3e_206_tlb_entry *stlbe)
Liu Yu08b7fa92011-06-14 18:34:59 -0500394{
Scott Wood0164c0f2011-08-18 15:25:18 -0500395 pfn_t pfn = ref->pfn;
Scott Wood8fdd21a22011-12-20 15:34:34 +0000396 u32 pr = vcpu->arch.shared->msr & MSR_PR;
Scott Wood0164c0f2011-08-18 15:25:18 -0500397
398 BUG_ON(!(ref->flags & E500_TLB_VALID));
Liu Yu08b7fa92011-06-14 18:34:59 -0500399
Scott Wood8fdd21a22011-12-20 15:34:34 +0000400 /* Force IPROT=0 for all guest mappings. */
401 stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
402 stlbe->mas2 = (gvaddr & MAS2_EPN) |
403 e500_shadow_mas2_attrib(gtlbe->mas2, pr);
404 stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
405 e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
Scott Wood73196cd2011-12-20 15:34:47 +0000406
407#ifdef CONFIG_KVM_BOOKE_HV
408 stlbe->mas8 = MAS8_TGS | vcpu->kvm->arch.lpid;
409#endif
Liu Yu08b7fa92011-06-14 18:34:59 -0500410}
411
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600412static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500413 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood57013522011-11-29 10:40:23 +0000414 int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500415 struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600416{
Scott Wood9973d542011-06-14 18:34:39 -0500417 struct kvm_memory_slot *slot;
Scott Wood9973d542011-06-14 18:34:39 -0500418 unsigned long pfn, hva;
419 int pfnmap = 0;
420 int tsize = BOOK3E_PAGESZ_4K;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600421
Scott Wood59c1f4e2011-06-14 18:34:37 -0500422 /*
423 * Translate guest physical to true physical, acquiring
424 * a page reference if it is normal, non-reserved memory.
Scott Wood9973d542011-06-14 18:34:39 -0500425 *
426 * gfn_to_memslot() must succeed because otherwise we wouldn't
427 * have gotten this far. Eventually we should just pass the slot
428 * pointer through from the first lookup.
Scott Wood59c1f4e2011-06-14 18:34:37 -0500429 */
Scott Wood9973d542011-06-14 18:34:39 -0500430 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
431 hva = gfn_to_hva_memslot(slot, gfn);
432
433 if (tlbsel == 1) {
434 struct vm_area_struct *vma;
435 down_read(&current->mm->mmap_sem);
436
437 vma = find_vma(current->mm, hva);
438 if (vma && hva >= vma->vm_start &&
439 (vma->vm_flags & VM_PFNMAP)) {
440 /*
441 * This VMA is a physically contiguous region (e.g.
442 * /dev/mem) that bypasses normal Linux page
443 * management. Find the overlap between the
444 * vma and the memslot.
445 */
446
447 unsigned long start, end;
448 unsigned long slot_start, slot_end;
449
450 pfnmap = 1;
451
452 start = vma->vm_pgoff;
453 end = start +
454 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
455
456 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
457
458 slot_start = pfn - (gfn - slot->base_gfn);
459 slot_end = slot_start + slot->npages;
460
461 if (start < slot_start)
462 start = slot_start;
463 if (end > slot_end)
464 end = slot_end;
465
466 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
467 MAS1_TSIZE_SHIFT;
468
469 /*
470 * e500 doesn't implement the lowest tsize bit,
471 * or 1K pages.
472 */
473 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
474
475 /*
476 * Now find the largest tsize (up to what the guest
477 * requested) that will cover gfn, stay within the
478 * range, and for which gfn and pfn are mutually
479 * aligned.
480 */
481
482 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
483 unsigned long gfn_start, gfn_end, tsize_pages;
484 tsize_pages = 1 << (tsize - 2);
485
486 gfn_start = gfn & ~(tsize_pages - 1);
487 gfn_end = gfn_start + tsize_pages;
488
489 if (gfn_start + pfn - gfn < start)
490 continue;
491 if (gfn_end + pfn - gfn > end)
492 continue;
493 if ((gfn & (tsize_pages - 1)) !=
494 (pfn & (tsize_pages - 1)))
495 continue;
496
497 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
498 pfn &= ~(tsize_pages - 1);
499 break;
500 }
Alexander Graf95325e62011-09-20 01:31:48 +0200501 } else if (vma && hva >= vma->vm_start &&
502 (vma->vm_flags & VM_HUGETLB)) {
503 unsigned long psize = vma_kernel_pagesize(vma);
504
505 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
506 MAS1_TSIZE_SHIFT;
507
508 /*
509 * Take the largest page size that satisfies both host
510 * and guest mapping
511 */
512 tsize = min(__ilog2(psize) - 10, tsize);
513
514 /*
515 * e500 doesn't implement the lowest tsize bit,
516 * or 1K pages.
517 */
518 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
Scott Wood9973d542011-06-14 18:34:39 -0500519 }
520
521 up_read(&current->mm->mmap_sem);
522 }
523
524 if (likely(!pfnmap)) {
Alexander Graf95325e62011-09-20 01:31:48 +0200525 unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
Xiao Guangrongd5661042012-07-17 21:56:16 +0800526 pfn = gfn_to_pfn_memslot(slot, gfn);
Scott Wood9973d542011-06-14 18:34:39 -0500527 if (is_error_pfn(pfn)) {
528 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
529 (long)gfn);
Scott Wood9973d542011-06-14 18:34:39 -0500530 return;
531 }
Alexander Graf95325e62011-09-20 01:31:48 +0200532
533 /* Align guest and physical address to page map boundaries */
534 pfn &= ~(tsize_pages - 1);
535 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600536 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600537
Scott Wood0164c0f2011-08-18 15:25:18 -0500538 /* Drop old ref and setup new one. */
539 kvmppc_e500_ref_release(ref);
540 kvmppc_e500_ref_setup(ref, gtlbe, pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600541
Scott Wood8fdd21a22011-12-20 15:34:34 +0000542 kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
543 ref, gvaddr, stlbe);
Alexander Graf249ba1e2012-08-03 13:56:33 +0200544
545 /* Clear i-cache for new pages */
546 kvmppc_mmu_flush_icache(pfn);
Alexander Graf862d31f2012-07-31 00:19:50 +0200547
548 /* Drop refcount on page, so that mmu notifiers can clear it */
549 kvm_release_pfn_clean(pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600550}
551
552/* XXX only map the one-one case, for now use TLB0 */
Scott Wood57013522011-11-29 10:40:23 +0000553static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
554 int esel,
555 struct kvm_book3e_206_tlb_entry *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600556{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500557 struct kvm_book3e_206_tlb_entry *gtlbe;
Scott Wood0164c0f2011-08-18 15:25:18 -0500558 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600559
Scott Wooddc83b8b2011-08-18 15:25:21 -0500560 gtlbe = get_entry(vcpu_e500, 0, esel);
Scott Wood0164c0f2011-08-18 15:25:18 -0500561 ref = &vcpu_e500->gtlb_priv[0][esel].ref;
562
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600563 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
564 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
Scott Wood57013522011-11-29 10:40:23 +0000565 gtlbe, 0, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600566}
567
568/* Caller must ensure that the specified guest TLB entry is safe to insert into
569 * the shadow TLB. */
570/* XXX for both one-one and one-to-many , for now use TLB1 */
571static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500572 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood4f802fe2011-12-20 15:34:37 +0000573 struct kvm_book3e_206_tlb_entry *stlbe, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600574{
Scott Wood0164c0f2011-08-18 15:25:18 -0500575 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600576 unsigned int victim;
577
Scott Wood0164c0f2011-08-18 15:25:18 -0500578 victim = vcpu_e500->host_tlb1_nv++;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600579
Scott Wood0164c0f2011-08-18 15:25:18 -0500580 if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
581 vcpu_e500->host_tlb1_nv = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600582
Scott Wood0164c0f2011-08-18 15:25:18 -0500583 ref = &vcpu_e500->tlb_refs[1][victim];
Scott Wood57013522011-11-29 10:40:23 +0000584 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600585
Scott Wood4f802fe2011-12-20 15:34:37 +0000586 vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << victim;
587 vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
588 if (vcpu_e500->h2g_tlb1_rmap[victim]) {
589 unsigned int idx = vcpu_e500->h2g_tlb1_rmap[victim];
590 vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << victim);
591 }
592 vcpu_e500->h2g_tlb1_rmap[victim] = esel;
593
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600594 return victim;
595}
596
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000597static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500)
598{
599 int size = vcpu_e500->gtlb_params[1].entries;
600 unsigned int offset;
601 gva_t eaddr;
602 int i;
603
604 vcpu_e500->tlb1_min_eaddr = ~0UL;
605 vcpu_e500->tlb1_max_eaddr = 0;
606 offset = vcpu_e500->gtlb_offset[1];
607
608 for (i = 0; i < size; i++) {
609 struct kvm_book3e_206_tlb_entry *tlbe =
610 &vcpu_e500->gtlb_arch[offset + i];
611
612 if (!get_tlb_v(tlbe))
613 continue;
614
615 eaddr = get_tlb_eaddr(tlbe);
616 vcpu_e500->tlb1_min_eaddr =
617 min(vcpu_e500->tlb1_min_eaddr, eaddr);
618
619 eaddr = get_tlb_end(tlbe);
620 vcpu_e500->tlb1_max_eaddr =
621 max(vcpu_e500->tlb1_max_eaddr, eaddr);
622 }
623}
624
625static int kvmppc_need_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500,
626 struct kvm_book3e_206_tlb_entry *gtlbe)
627{
628 unsigned long start, end, size;
629
630 size = get_tlb_bytes(gtlbe);
631 start = get_tlb_eaddr(gtlbe) & ~(size - 1);
632 end = start + size - 1;
633
634 return vcpu_e500->tlb1_min_eaddr == start ||
635 vcpu_e500->tlb1_max_eaddr == end;
636}
637
638/* This function is supposed to be called for a adding a new valid tlb entry */
639static void kvmppc_set_tlb1map_range(struct kvm_vcpu *vcpu,
640 struct kvm_book3e_206_tlb_entry *gtlbe)
641{
642 unsigned long start, end, size;
643 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
644
645 if (!get_tlb_v(gtlbe))
646 return;
647
648 size = get_tlb_bytes(gtlbe);
649 start = get_tlb_eaddr(gtlbe) & ~(size - 1);
650 end = start + size - 1;
651
652 vcpu_e500->tlb1_min_eaddr = min(vcpu_e500->tlb1_min_eaddr, start);
653 vcpu_e500->tlb1_max_eaddr = max(vcpu_e500->tlb1_max_eaddr, end);
654}
655
Liu Yu08b7fa92011-06-14 18:34:59 -0500656static inline int kvmppc_e500_gtlbe_invalidate(
657 struct kvmppc_vcpu_e500 *vcpu_e500,
658 int tlbsel, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600659{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500660 struct kvm_book3e_206_tlb_entry *gtlbe =
661 get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600662
663 if (unlikely(get_tlb_iprot(gtlbe)))
664 return -1;
665
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000666 if (tlbsel == 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
667 kvmppc_recalc_tlb1map_range(vcpu_e500);
668
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600669 gtlbe->mas1 = 0;
670
671 return 0;
672}
673
Liu Yub0a18352009-02-17 16:52:08 +0800674int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
675{
676 int esel;
677
678 if (value & MMUCSR0_TLB0FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500679 for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800680 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
681 if (value & MMUCSR0_TLB1FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500682 for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800683 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
684
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500685 /* Invalidate all vcpu id mappings */
Scott Wood8fdd21a22011-12-20 15:34:34 +0000686 kvmppc_e500_tlbil_all(vcpu_e500);
Liu Yub0a18352009-02-17 16:52:08 +0800687
688 return EMULATE_DONE;
689}
690
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600691int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
692{
693 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
694 unsigned int ia;
695 int esel, tlbsel;
696 gva_t ea;
697
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100698 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600699
700 ia = (ea >> 2) & 0x1;
701
Liu Yufb2838d2009-01-14 10:47:37 -0600702 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600703 tlbsel = (ea >> 3) & 0x1;
704
705 if (ia) {
706 /* invalidate all entries */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500707 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
708 esel++)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600709 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
710 } else {
711 ea &= 0xfffff000;
712 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
713 get_cur_pid(vcpu), -1);
714 if (esel >= 0)
715 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
716 }
717
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500718 /* Invalidate all vcpu id mappings */
Scott Wood8fdd21a22011-12-20 15:34:34 +0000719 kvmppc_e500_tlbil_all(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600720
721 return EMULATE_DONE;
722}
723
Scott Woodab9fc402011-12-20 15:34:39 +0000724static void tlbilx_all(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
725 int pid, int rt)
726{
727 struct kvm_book3e_206_tlb_entry *tlbe;
728 int tid, esel;
729
730 /* invalidate all entries */
731 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; esel++) {
732 tlbe = get_entry(vcpu_e500, tlbsel, esel);
733 tid = get_tlb_tid(tlbe);
734 if (rt == 0 || tid == pid) {
735 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
736 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
737 }
738 }
739}
740
741static void tlbilx_one(struct kvmppc_vcpu_e500 *vcpu_e500, int pid,
742 int ra, int rb)
743{
744 int tlbsel, esel;
745 gva_t ea;
746
747 ea = kvmppc_get_gpr(&vcpu_e500->vcpu, rb);
748 if (ra)
749 ea += kvmppc_get_gpr(&vcpu_e500->vcpu, ra);
750
751 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
752 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1);
753 if (esel >= 0) {
754 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
755 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
756 break;
757 }
758 }
759}
760
761int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int rt, int ra, int rb)
762{
763 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
764 int pid = get_cur_spid(vcpu);
765
766 if (rt == 0 || rt == 1) {
767 tlbilx_all(vcpu_e500, 0, pid, rt);
768 tlbilx_all(vcpu_e500, 1, pid, rt);
769 } else if (rt == 3) {
770 tlbilx_one(vcpu_e500, pid, ra, rb);
771 }
772
773 return EMULATE_DONE;
774}
775
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600776int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
777{
778 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
779 int tlbsel, esel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500780 struct kvm_book3e_206_tlb_entry *gtlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600781
Scott Woodb5904972011-11-08 18:23:30 -0600782 tlbsel = get_tlb_tlbsel(vcpu);
783 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600784
Scott Wooddc83b8b2011-08-18 15:25:21 -0500785 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Scott Woodb5904972011-11-08 18:23:30 -0600786 vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
787 vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
788 vcpu->arch.shared->mas1 = gtlbe->mas1;
789 vcpu->arch.shared->mas2 = gtlbe->mas2;
790 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600791
792 return EMULATE_DONE;
793}
794
795int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
796{
797 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Woodb5904972011-11-08 18:23:30 -0600798 int as = !!get_cur_sas(vcpu);
799 unsigned int pid = get_cur_spid(vcpu);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600800 int esel, tlbsel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500801 struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600802 gva_t ea;
803
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100804 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600805
806 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
807 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
808 if (esel >= 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500809 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600810 break;
811 }
812 }
813
814 if (gtlbe) {
Scott Wood303b7c92011-08-18 15:25:23 -0500815 esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
816
Scott Woodb5904972011-11-08 18:23:30 -0600817 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
Liu Yu08b7fa92011-06-14 18:34:59 -0500818 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600819 vcpu->arch.shared->mas1 = gtlbe->mas1;
820 vcpu->arch.shared->mas2 = gtlbe->mas2;
821 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600822 } else {
823 int victim;
824
Liu Yufb2838d2009-01-14 10:47:37 -0600825 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600826 tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500827 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600828
Scott Woodb5904972011-11-08 18:23:30 -0600829 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
830 | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500831 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600832 vcpu->arch.shared->mas1 =
833 (vcpu->arch.shared->mas6 & MAS6_SPID0)
834 | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
835 | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
836 vcpu->arch.shared->mas2 &= MAS2_EPN;
837 vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
838 MAS2_ATTRIB_MASK;
839 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
840 MAS3_U2 | MAS3_U3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600841 }
842
Scott Wood49ea0692011-03-28 15:01:24 -0500843 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600844 return EMULATE_DONE;
845}
846
Scott Wood57013522011-11-29 10:40:23 +0000847/* sesel is for tlb1 only */
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500848static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500849 struct kvm_book3e_206_tlb_entry *gtlbe,
850 struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500851 int stlbsel, int sesel)
852{
853 int stid;
854
855 preempt_disable();
Scott Wood8fdd21a22011-12-20 15:34:34 +0000856 stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500857
858 stlbe->mas1 |= MAS1_TID(stid);
859 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
860 preempt_enable();
861}
862
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600863int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
864{
865 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000866 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
867 int tlbsel, esel, stlbsel, sesel;
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000868 int recal = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600869
Scott Woodb5904972011-11-08 18:23:30 -0600870 tlbsel = get_tlb_tlbsel(vcpu);
871 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600872
Scott Wooddc83b8b2011-08-18 15:25:21 -0500873 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600874
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000875 if (get_tlb_v(gtlbe)) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500876 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000877 if ((tlbsel == 1) &&
878 kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
879 recal = 1;
880 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600881
Scott Woodb5904972011-11-08 18:23:30 -0600882 gtlbe->mas1 = vcpu->arch.shared->mas1;
883 gtlbe->mas2 = vcpu->arch.shared->mas2;
884 gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600885
Liu Yud37b1a02011-12-20 14:42:56 +0000886 trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
887 gtlbe->mas2, gtlbe->mas7_3);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600888
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000889 if (tlbsel == 1) {
890 /*
891 * If a valid tlb1 entry is overwritten then recalculate the
892 * min/max TLB1 map address range otherwise no need to look
893 * in tlb1 array.
894 */
895 if (recal)
896 kvmppc_recalc_tlb1map_range(vcpu_e500);
897 else
898 kvmppc_set_tlb1map_range(vcpu, gtlbe);
899 }
900
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600901 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
902 if (tlbe_is_host_safe(vcpu, gtlbe)) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500903 u64 eaddr;
904 u64 raddr;
905
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600906 switch (tlbsel) {
907 case 0:
908 /* TLB0 */
909 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800910 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600911
912 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +0000913 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
914 sesel = 0; /* unused */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600915
916 break;
917
918 case 1:
919 /* TLB1 */
920 eaddr = get_tlb_eaddr(gtlbe);
921 raddr = get_tlb_raddr(gtlbe);
922
923 /* Create a 4KB mapping on the host.
924 * If the guest wanted a large page,
925 * only the first 4KB is mapped here and the rest
926 * are mapped on the fly. */
927 stlbsel = 1;
928 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
Scott Wood4f802fe2011-12-20 15:34:37 +0000929 raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600930 break;
931
932 default:
933 BUG();
934 }
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500935
936 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600937 }
938
Scott Wood49ea0692011-03-28 15:01:24 -0500939 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600940 return EMULATE_DONE;
941}
942
Scott Wood8fdd21a22011-12-20 15:34:34 +0000943static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
944 gva_t eaddr, unsigned int pid, int as)
945{
946 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
947 int esel, tlbsel;
948
949 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
950 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
951 if (esel >= 0)
952 return index_of(tlbsel, esel);
953 }
954
955 return -1;
956}
957
958/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
959int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
960 struct kvm_translation *tr)
961{
962 int index;
963 gva_t eaddr;
964 u8 pid;
965 u8 as;
966
967 eaddr = tr->linear_address;
968 pid = (tr->linear_address >> 32) & 0xff;
969 as = (tr->linear_address >> 40) & 0x1;
970
971 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
972 if (index < 0) {
973 tr->valid = 0;
974 return 0;
975 }
976
977 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
978 /* XXX what does "writeable" and "usermode" even mean? */
979 tr->valid = 1;
980
981 return 0;
982}
983
984
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600985int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
986{
Alexander Graf666e7252010-07-29 14:47:43 +0200987 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600988
989 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
990}
991
992int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
993{
Alexander Graf666e7252010-07-29 14:47:43 +0200994 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600995
996 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
997}
998
999void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
1000{
Alexander Graf666e7252010-07-29 14:47:43 +02001001 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001002
1003 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
1004}
1005
1006void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
1007{
Alexander Graf666e7252010-07-29 14:47:43 +02001008 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001009
1010 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
1011}
1012
1013gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
1014 gva_t eaddr)
1015{
1016 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001017 struct kvm_book3e_206_tlb_entry *gtlbe;
1018 u64 pgmask;
1019
1020 gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
1021 pgmask = get_tlb_bytes(gtlbe) - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001022
1023 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
1024}
1025
1026void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1027{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001028}
1029
1030void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
1031 unsigned int index)
1032{
1033 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Liu Yu08b7fa92011-06-14 18:34:59 -05001034 struct tlbe_priv *priv;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001035 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001036 int tlbsel = tlbsel_of(index);
1037 int esel = esel_of(index);
1038 int stlbsel, sesel;
1039
Scott Wooddc83b8b2011-08-18 15:25:21 -05001040 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Liu Yu08b7fa92011-06-14 18:34:59 -05001041
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001042 switch (tlbsel) {
1043 case 0:
1044 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +00001045 sesel = 0; /* unused */
Scott Wood0164c0f2011-08-18 15:25:18 -05001046 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
Liu Yu08b7fa92011-06-14 18:34:59 -05001047
Alexander Graf2bb890f2012-08-02 13:38:49 +02001048 /* Only triggers after clear_tlb_refs */
1049 if (unlikely(!(priv->ref.flags & E500_TLB_VALID)))
1050 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
1051 else
1052 kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
1053 &priv->ref, eaddr, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001054 break;
1055
1056 case 1: {
1057 gfn_t gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001058
1059 stlbsel = 1;
Liu Yu08b7fa92011-06-14 18:34:59 -05001060 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
Scott Wood4f802fe2011-12-20 15:34:37 +00001061 gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001062 break;
1063 }
1064
1065 default:
1066 BUG();
1067 break;
1068 }
Liu Yu08b7fa92011-06-14 18:34:59 -05001069
Scott Wood3bf3cdc2011-08-18 15:25:14 -05001070 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001071}
1072
Alexander Graf862d31f2012-07-31 00:19:50 +02001073/************* MMU Notifiers *************/
1074
1075int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1076{
Alexander Graf63460462012-08-08 00:44:52 +02001077 trace_kvm_unmap_hva(hva);
1078
Alexander Graf862d31f2012-07-31 00:19:50 +02001079 /*
1080 * Flush all shadow tlb entries everywhere. This is slow, but
1081 * we are 100% sure that we catch the to be unmapped page
1082 */
1083 kvm_flush_remote_tlbs(kvm);
1084
1085 return 0;
1086}
1087
1088int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
1089{
1090 /* kvm_unmap_hva flushes everything anyways */
1091 kvm_unmap_hva(kvm, start);
1092
1093 return 0;
1094}
1095
1096int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1097{
1098 /* XXX could be more clever ;) */
1099 return 0;
1100}
1101
1102int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1103{
1104 /* XXX could be more clever ;) */
1105 return 0;
1106}
1107
1108void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1109{
1110 /* The page will get remapped properly on its next fault */
1111 kvm_unmap_hva(kvm, hva);
1112}
1113
1114/*****************************************/
1115
Scott Wooddc83b8b2011-08-18 15:25:21 -05001116static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
1117{
1118 int i;
1119
Scott Wood4f802fe2011-12-20 15:34:37 +00001120 clear_tlb1_bitmap(vcpu_e500);
1121 kfree(vcpu_e500->g2h_tlb1_map);
1122
Scott Wooddc83b8b2011-08-18 15:25:21 -05001123 clear_tlb_refs(vcpu_e500);
1124 kfree(vcpu_e500->gtlb_priv[0]);
1125 kfree(vcpu_e500->gtlb_priv[1]);
1126
1127 if (vcpu_e500->shared_tlb_pages) {
1128 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
1129 PAGE_SIZE)));
1130
1131 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
1132 set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
1133 put_page(vcpu_e500->shared_tlb_pages[i]);
1134 }
1135
1136 vcpu_e500->num_shared_tlb_pages = 0;
1137 vcpu_e500->shared_tlb_pages = NULL;
1138 } else {
1139 kfree(vcpu_e500->gtlb_arch);
1140 }
1141
1142 vcpu_e500->gtlb_arch = NULL;
1143}
1144
Scott Wood8fdd21a22011-12-20 15:34:34 +00001145void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1146{
1147 sregs->u.e.mas0 = vcpu->arch.shared->mas0;
1148 sregs->u.e.mas1 = vcpu->arch.shared->mas1;
1149 sregs->u.e.mas2 = vcpu->arch.shared->mas2;
1150 sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
1151 sregs->u.e.mas4 = vcpu->arch.shared->mas4;
1152 sregs->u.e.mas6 = vcpu->arch.shared->mas6;
1153
1154 sregs->u.e.mmucfg = vcpu->arch.mmucfg;
1155 sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
1156 sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
1157 sregs->u.e.tlbcfg[2] = 0;
1158 sregs->u.e.tlbcfg[3] = 0;
1159}
1160
1161int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1162{
1163 if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
1164 vcpu->arch.shared->mas0 = sregs->u.e.mas0;
1165 vcpu->arch.shared->mas1 = sregs->u.e.mas1;
1166 vcpu->arch.shared->mas2 = sregs->u.e.mas2;
1167 vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
1168 vcpu->arch.shared->mas4 = sregs->u.e.mas4;
1169 vcpu->arch.shared->mas6 = sregs->u.e.mas6;
1170 }
1171
1172 return 0;
1173}
1174
Scott Wooddc83b8b2011-08-18 15:25:21 -05001175int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
1176 struct kvm_config_tlb *cfg)
1177{
1178 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1179 struct kvm_book3e_206_tlb_params params;
1180 char *virt;
1181 struct page **pages;
1182 struct tlbe_priv *privs[2] = {};
Scott Wood4f802fe2011-12-20 15:34:37 +00001183 u64 *g2h_bitmap = NULL;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001184 size_t array_len;
1185 u32 sets;
1186 int num_pages, ret, i;
1187
1188 if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
1189 return -EINVAL;
1190
1191 if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
1192 sizeof(params)))
1193 return -EFAULT;
1194
1195 if (params.tlb_sizes[1] > 64)
1196 return -EINVAL;
1197 if (params.tlb_ways[1] != params.tlb_sizes[1])
1198 return -EINVAL;
1199 if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
1200 return -EINVAL;
1201 if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
1202 return -EINVAL;
1203
1204 if (!is_power_of_2(params.tlb_ways[0]))
1205 return -EINVAL;
1206
1207 sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1208 if (!is_power_of_2(sets))
1209 return -EINVAL;
1210
1211 array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1212 array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1213
1214 if (cfg->array_len < array_len)
1215 return -EINVAL;
1216
1217 num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1218 cfg->array / PAGE_SIZE;
1219 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1220 if (!pages)
1221 return -ENOMEM;
1222
1223 ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1224 if (ret < 0)
1225 goto err_pages;
1226
1227 if (ret != num_pages) {
1228 num_pages = ret;
1229 ret = -EFAULT;
1230 goto err_put_page;
1231 }
1232
1233 virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1234 if (!virt)
1235 goto err_put_page;
1236
1237 privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1238 GFP_KERNEL);
1239 privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1240 GFP_KERNEL);
1241
1242 if (!privs[0] || !privs[1])
1243 goto err_put_page;
1244
Scott Wood4f802fe2011-12-20 15:34:37 +00001245 g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
1246 GFP_KERNEL);
1247 if (!g2h_bitmap)
1248 goto err_put_page;
1249
Scott Wooddc83b8b2011-08-18 15:25:21 -05001250 free_gtlb(vcpu_e500);
1251
1252 vcpu_e500->gtlb_priv[0] = privs[0];
1253 vcpu_e500->gtlb_priv[1] = privs[1];
Scott Wood4f802fe2011-12-20 15:34:37 +00001254 vcpu_e500->g2h_tlb1_map = g2h_bitmap;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001255
1256 vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1257 (virt + (cfg->array & (PAGE_SIZE - 1)));
1258
1259 vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1260 vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1261
1262 vcpu_e500->gtlb_offset[0] = 0;
1263 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1264
Scott Wood8fdd21a22011-12-20 15:34:34 +00001265 vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001266
Scott Wood8fdd21a22011-12-20 15:34:34 +00001267 vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1268 if (params.tlb_sizes[0] <= 2048)
1269 vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
1270 vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
1271
1272 vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1273 vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
1274 vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001275
1276 vcpu_e500->shared_tlb_pages = pages;
1277 vcpu_e500->num_shared_tlb_pages = num_pages;
1278
1279 vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1280 vcpu_e500->gtlb_params[0].sets = sets;
1281
1282 vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1283 vcpu_e500->gtlb_params[1].sets = 1;
1284
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001285 kvmppc_recalc_tlb1map_range(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001286 return 0;
1287
1288err_put_page:
1289 kfree(privs[0]);
1290 kfree(privs[1]);
1291
1292 for (i = 0; i < num_pages; i++)
1293 put_page(pages[i]);
1294
1295err_pages:
1296 kfree(pages);
1297 return ret;
1298}
1299
1300int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1301 struct kvm_dirty_tlb *dirty)
1302{
1303 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001304 kvmppc_recalc_tlb1map_range(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001305 clear_tlb_refs(vcpu_e500);
1306 return 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001307}
1308
1309int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1310{
Scott Wood8fdd21a22011-12-20 15:34:34 +00001311 struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001312 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1313 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1314
Scott Wood0164c0f2011-08-18 15:25:18 -05001315 host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1316 host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1317
1318 /*
1319 * This should never happen on real e500 hardware, but is
1320 * architecturally possible -- e.g. in some weird nested
1321 * virtualization case.
1322 */
1323 if (host_tlb_params[0].entries == 0 ||
1324 host_tlb_params[1].entries == 0) {
1325 pr_err("%s: need to know host tlb size\n", __func__);
1326 return -ENODEV;
1327 }
1328
1329 host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1330 TLBnCFG_ASSOC_SHIFT;
1331 host_tlb_params[1].ways = host_tlb_params[1].entries;
1332
1333 if (!is_power_of_2(host_tlb_params[0].entries) ||
1334 !is_power_of_2(host_tlb_params[0].ways) ||
1335 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1336 host_tlb_params[0].ways == 0) {
1337 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1338 __func__, host_tlb_params[0].entries,
1339 host_tlb_params[0].ways);
1340 return -ENODEV;
1341 }
1342
1343 host_tlb_params[0].sets =
1344 host_tlb_params[0].entries / host_tlb_params[0].ways;
1345 host_tlb_params[1].sets = 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001346
Scott Wooddc83b8b2011-08-18 15:25:21 -05001347 vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1348 vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001349
Scott Wooddc83b8b2011-08-18 15:25:21 -05001350 vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1351 vcpu_e500->gtlb_params[0].sets =
1352 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1353
1354 vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1355 vcpu_e500->gtlb_params[1].sets = 1;
1356
1357 vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1358 if (!vcpu_e500->gtlb_arch)
1359 return -ENOMEM;
1360
1361 vcpu_e500->gtlb_offset[0] = 0;
1362 vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001363
Scott Wood0164c0f2011-08-18 15:25:18 -05001364 vcpu_e500->tlb_refs[0] =
1365 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1366 GFP_KERNEL);
1367 if (!vcpu_e500->tlb_refs[0])
1368 goto err;
Liu Yu08b7fa92011-06-14 18:34:59 -05001369
Scott Wood0164c0f2011-08-18 15:25:18 -05001370 vcpu_e500->tlb_refs[1] =
1371 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1372 GFP_KERNEL);
1373 if (!vcpu_e500->tlb_refs[1])
1374 goto err;
1375
Scott Wooddc83b8b2011-08-18 15:25:21 -05001376 vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1377 vcpu_e500->gtlb_params[0].entries,
1378 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001379 if (!vcpu_e500->gtlb_priv[0])
1380 goto err;
1381
Scott Wooddc83b8b2011-08-18 15:25:21 -05001382 vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1383 vcpu_e500->gtlb_params[1].entries,
1384 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001385 if (!vcpu_e500->gtlb_priv[1])
1386 goto err;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001387
Scott Woode400e722012-08-22 15:04:23 +00001388 vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(u64) *
Scott Wood4f802fe2011-12-20 15:34:37 +00001389 vcpu_e500->gtlb_params[1].entries,
1390 GFP_KERNEL);
1391 if (!vcpu_e500->g2h_tlb1_map)
1392 goto err;
1393
1394 vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
1395 host_tlb_params[1].entries,
1396 GFP_KERNEL);
1397 if (!vcpu_e500->h2g_tlb1_rmap)
1398 goto err;
1399
Liu Yuda15bf42010-01-22 19:36:53 +08001400 /* Init TLB configuration register */
Scott Wood8fdd21a22011-12-20 15:34:34 +00001401 vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001402 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wood8fdd21a22011-12-20 15:34:34 +00001403 vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
1404 vcpu->arch.tlbcfg[0] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001405 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
1406
Scott Wood8fdd21a22011-12-20 15:34:34 +00001407 vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001408 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Alexander Grafc6b37332012-02-20 17:48:47 +01001409 vcpu->arch.tlbcfg[1] |= vcpu_e500->gtlb_params[1].entries;
1410 vcpu->arch.tlbcfg[1] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001411 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
Liu Yuda15bf42010-01-22 19:36:53 +08001412
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001413 kvmppc_recalc_tlb1map_range(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001414 return 0;
1415
Scott Wood0164c0f2011-08-18 15:25:18 -05001416err:
Scott Wooddc83b8b2011-08-18 15:25:21 -05001417 free_gtlb(vcpu_e500);
Scott Wood0164c0f2011-08-18 15:25:18 -05001418 kfree(vcpu_e500->tlb_refs[0]);
1419 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001420 return -1;
1421}
1422
1423void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1424{
Scott Wooddc83b8b2011-08-18 15:25:21 -05001425 free_gtlb(vcpu_e500);
Scott Wood4f802fe2011-12-20 15:34:37 +00001426 kfree(vcpu_e500->h2g_tlb1_rmap);
Scott Wood0164c0f2011-08-18 15:25:18 -05001427 kfree(vcpu_e500->tlb_refs[0]);
1428 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001429}