blob: de8ea29409f2df3a05b73e61b99861beea36deb7 [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 Graf862d31f2012-07-31 00:19:50 +0200306 if (tlbe_is_writable(gtlbe)) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500307 ref->flags |= E500_TLB_DIRTY;
Alexander Graf862d31f2012-07-31 00:19:50 +0200308 kvm_set_pfn_dirty(pfn);
309 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600310}
311
Scott Wood0164c0f2011-08-18 15:25:18 -0500312static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600313{
Scott Wood0164c0f2011-08-18 15:25:18 -0500314 if (ref->flags & E500_TLB_VALID) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500315 ref->flags = 0;
Liu Yu08b7fa92011-06-14 18:34:59 -0500316 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600317}
318
Scott Wood4f802fe2011-12-20 15:34:37 +0000319static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
320{
321 if (vcpu_e500->g2h_tlb1_map)
Alan Coxe8143cc2012-08-14 12:10:09 +0000322 memset(vcpu_e500->g2h_tlb1_map, 0,
323 sizeof(u64) * vcpu_e500->gtlb_params[1].entries);
Scott Wood4f802fe2011-12-20 15:34:37 +0000324 if (vcpu_e500->h2g_tlb1_rmap)
Alan Coxe8143cc2012-08-14 12:10:09 +0000325 memset(vcpu_e500->h2g_tlb1_rmap, 0,
326 sizeof(unsigned int) * host_tlb_params[1].entries);
Scott Wood4f802fe2011-12-20 15:34:37 +0000327}
328
Scott Wood0164c0f2011-08-18 15:25:18 -0500329static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
330{
331 int tlbsel = 0;
332 int i;
333
Scott Wooddc83b8b2011-08-18 15:25:21 -0500334 for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500335 struct tlbe_ref *ref =
336 &vcpu_e500->gtlb_priv[tlbsel][i].ref;
337 kvmppc_e500_ref_release(ref);
338 }
339}
340
341static void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500)
342{
343 int stlbsel = 1;
344 int i;
345
Scott Wood8fdd21a22011-12-20 15:34:34 +0000346 kvmppc_e500_tlbil_all(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -0500347
Scott Wood0164c0f2011-08-18 15:25:18 -0500348 for (i = 0; i < host_tlb_params[stlbsel].entries; i++) {
349 struct tlbe_ref *ref =
350 &vcpu_e500->tlb_refs[stlbsel][i];
351 kvmppc_e500_ref_release(ref);
352 }
353
354 clear_tlb_privs(vcpu_e500);
355}
356
Alexander Graf862d31f2012-07-31 00:19:50 +0200357void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu)
358{
359 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
360 clear_tlb_refs(vcpu_e500);
361 clear_tlb1_bitmap(vcpu_e500);
362}
363
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600364static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu,
365 unsigned int eaddr, int as)
366{
367 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000368 unsigned int victim, tsized;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600369 int tlbsel;
370
Liu Yufb2838d2009-01-14 10:47:37 -0600371 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600372 tlbsel = (vcpu->arch.shared->mas4 >> 28) & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500373 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Scott Woodb5904972011-11-08 18:23:30 -0600374 tsized = (vcpu->arch.shared->mas4 >> 7) & 0x1f;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600375
Scott Woodb5904972011-11-08 18:23:30 -0600376 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500377 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600378 vcpu->arch.shared->mas1 = MAS1_VALID | (as ? MAS1_TS : 0)
Scott Wood8fdd21a22011-12-20 15:34:34 +0000379 | MAS1_TID(get_tlbmiss_tid(vcpu))
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600380 | MAS1_TSIZE(tsized);
Scott Woodb5904972011-11-08 18:23:30 -0600381 vcpu->arch.shared->mas2 = (eaddr & MAS2_EPN)
382 | (vcpu->arch.shared->mas4 & MAS2_ATTRIB_MASK);
383 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3;
384 vcpu->arch.shared->mas6 = (vcpu->arch.shared->mas6 & MAS6_SPID1)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600385 | (get_cur_pid(vcpu) << 16)
386 | (as ? MAS6_SAS : 0);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600387}
388
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500389/* TID must be supplied by the caller */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500390static inline void kvmppc_e500_setup_stlbe(
Scott Wood8fdd21a22011-12-20 15:34:34 +0000391 struct kvm_vcpu *vcpu,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500392 struct kvm_book3e_206_tlb_entry *gtlbe,
393 int tsize, struct tlbe_ref *ref, u64 gvaddr,
394 struct kvm_book3e_206_tlb_entry *stlbe)
Liu Yu08b7fa92011-06-14 18:34:59 -0500395{
Scott Wood0164c0f2011-08-18 15:25:18 -0500396 pfn_t pfn = ref->pfn;
Scott Wood8fdd21a22011-12-20 15:34:34 +0000397 u32 pr = vcpu->arch.shared->msr & MSR_PR;
Scott Wood0164c0f2011-08-18 15:25:18 -0500398
399 BUG_ON(!(ref->flags & E500_TLB_VALID));
Liu Yu08b7fa92011-06-14 18:34:59 -0500400
Scott Wood8fdd21a22011-12-20 15:34:34 +0000401 /* Force IPROT=0 for all guest mappings. */
402 stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
403 stlbe->mas2 = (gvaddr & MAS2_EPN) |
404 e500_shadow_mas2_attrib(gtlbe->mas2, pr);
405 stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
406 e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
Scott Wood73196cd2011-12-20 15:34:47 +0000407
408#ifdef CONFIG_KVM_BOOKE_HV
409 stlbe->mas8 = MAS8_TGS | vcpu->kvm->arch.lpid;
410#endif
Liu Yu08b7fa92011-06-14 18:34:59 -0500411}
412
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600413static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500414 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood57013522011-11-29 10:40:23 +0000415 int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500416 struct tlbe_ref *ref)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600417{
Scott Wood9973d542011-06-14 18:34:39 -0500418 struct kvm_memory_slot *slot;
Scott Wood9973d542011-06-14 18:34:39 -0500419 unsigned long pfn, hva;
420 int pfnmap = 0;
421 int tsize = BOOK3E_PAGESZ_4K;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600422
Scott Wood59c1f4e2011-06-14 18:34:37 -0500423 /*
424 * Translate guest physical to true physical, acquiring
425 * a page reference if it is normal, non-reserved memory.
Scott Wood9973d542011-06-14 18:34:39 -0500426 *
427 * gfn_to_memslot() must succeed because otherwise we wouldn't
428 * have gotten this far. Eventually we should just pass the slot
429 * pointer through from the first lookup.
Scott Wood59c1f4e2011-06-14 18:34:37 -0500430 */
Scott Wood9973d542011-06-14 18:34:39 -0500431 slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
432 hva = gfn_to_hva_memslot(slot, gfn);
433
434 if (tlbsel == 1) {
435 struct vm_area_struct *vma;
436 down_read(&current->mm->mmap_sem);
437
438 vma = find_vma(current->mm, hva);
439 if (vma && hva >= vma->vm_start &&
440 (vma->vm_flags & VM_PFNMAP)) {
441 /*
442 * This VMA is a physically contiguous region (e.g.
443 * /dev/mem) that bypasses normal Linux page
444 * management. Find the overlap between the
445 * vma and the memslot.
446 */
447
448 unsigned long start, end;
449 unsigned long slot_start, slot_end;
450
451 pfnmap = 1;
452
453 start = vma->vm_pgoff;
454 end = start +
455 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
456
457 pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
458
459 slot_start = pfn - (gfn - slot->base_gfn);
460 slot_end = slot_start + slot->npages;
461
462 if (start < slot_start)
463 start = slot_start;
464 if (end > slot_end)
465 end = slot_end;
466
467 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
468 MAS1_TSIZE_SHIFT;
469
470 /*
471 * e500 doesn't implement the lowest tsize bit,
472 * or 1K pages.
473 */
474 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
475
476 /*
477 * Now find the largest tsize (up to what the guest
478 * requested) that will cover gfn, stay within the
479 * range, and for which gfn and pfn are mutually
480 * aligned.
481 */
482
483 for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
484 unsigned long gfn_start, gfn_end, tsize_pages;
485 tsize_pages = 1 << (tsize - 2);
486
487 gfn_start = gfn & ~(tsize_pages - 1);
488 gfn_end = gfn_start + tsize_pages;
489
490 if (gfn_start + pfn - gfn < start)
491 continue;
492 if (gfn_end + pfn - gfn > end)
493 continue;
494 if ((gfn & (tsize_pages - 1)) !=
495 (pfn & (tsize_pages - 1)))
496 continue;
497
498 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
499 pfn &= ~(tsize_pages - 1);
500 break;
501 }
Alexander Graf95325e62011-09-20 01:31:48 +0200502 } else if (vma && hva >= vma->vm_start &&
503 (vma->vm_flags & VM_HUGETLB)) {
504 unsigned long psize = vma_kernel_pagesize(vma);
505
506 tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
507 MAS1_TSIZE_SHIFT;
508
509 /*
510 * Take the largest page size that satisfies both host
511 * and guest mapping
512 */
513 tsize = min(__ilog2(psize) - 10, tsize);
514
515 /*
516 * e500 doesn't implement the lowest tsize bit,
517 * or 1K pages.
518 */
519 tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
Scott Wood9973d542011-06-14 18:34:39 -0500520 }
521
522 up_read(&current->mm->mmap_sem);
523 }
524
525 if (likely(!pfnmap)) {
Alexander Graf95325e62011-09-20 01:31:48 +0200526 unsigned long tsize_pages = 1 << (tsize + 10 - PAGE_SHIFT);
Xiao Guangrongd5661042012-07-17 21:56:16 +0800527 pfn = gfn_to_pfn_memslot(slot, gfn);
Scott Wood9973d542011-06-14 18:34:39 -0500528 if (is_error_pfn(pfn)) {
529 printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
530 (long)gfn);
Scott Wood9973d542011-06-14 18:34:39 -0500531 return;
532 }
Alexander Graf95325e62011-09-20 01:31:48 +0200533
534 /* Align guest and physical address to page map boundaries */
535 pfn &= ~(tsize_pages - 1);
536 gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600537 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600538
Scott Wood0164c0f2011-08-18 15:25:18 -0500539 /* Drop old ref and setup new one. */
540 kvmppc_e500_ref_release(ref);
541 kvmppc_e500_ref_setup(ref, gtlbe, pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600542
Scott Wood8fdd21a22011-12-20 15:34:34 +0000543 kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
544 ref, gvaddr, stlbe);
Alexander Graf249ba1e2012-08-03 13:56:33 +0200545
546 /* Clear i-cache for new pages */
547 kvmppc_mmu_flush_icache(pfn);
Alexander Graf862d31f2012-07-31 00:19:50 +0200548
549 /* Drop refcount on page, so that mmu notifiers can clear it */
550 kvm_release_pfn_clean(pfn);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600551}
552
553/* XXX only map the one-one case, for now use TLB0 */
Scott Wood57013522011-11-29 10:40:23 +0000554static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
555 int esel,
556 struct kvm_book3e_206_tlb_entry *stlbe)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600557{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500558 struct kvm_book3e_206_tlb_entry *gtlbe;
Scott Wood0164c0f2011-08-18 15:25:18 -0500559 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600560
Scott Wooddc83b8b2011-08-18 15:25:21 -0500561 gtlbe = get_entry(vcpu_e500, 0, esel);
Scott Wood0164c0f2011-08-18 15:25:18 -0500562 ref = &vcpu_e500->gtlb_priv[0][esel].ref;
563
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600564 kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
565 get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
Scott Wood57013522011-11-29 10:40:23 +0000566 gtlbe, 0, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600567}
568
569/* Caller must ensure that the specified guest TLB entry is safe to insert into
570 * the shadow TLB. */
571/* XXX for both one-one and one-to-many , for now use TLB1 */
572static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500573 u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
Scott Wood4f802fe2011-12-20 15:34:37 +0000574 struct kvm_book3e_206_tlb_entry *stlbe, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600575{
Scott Wood0164c0f2011-08-18 15:25:18 -0500576 struct tlbe_ref *ref;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600577 unsigned int victim;
578
Scott Wood0164c0f2011-08-18 15:25:18 -0500579 victim = vcpu_e500->host_tlb1_nv++;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600580
Scott Wood0164c0f2011-08-18 15:25:18 -0500581 if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
582 vcpu_e500->host_tlb1_nv = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600583
Scott Wood0164c0f2011-08-18 15:25:18 -0500584 ref = &vcpu_e500->tlb_refs[1][victim];
Scott Wood57013522011-11-29 10:40:23 +0000585 kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600586
Scott Wood4f802fe2011-12-20 15:34:37 +0000587 vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << victim;
588 vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
589 if (vcpu_e500->h2g_tlb1_rmap[victim]) {
590 unsigned int idx = vcpu_e500->h2g_tlb1_rmap[victim];
591 vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << victim);
592 }
593 vcpu_e500->h2g_tlb1_rmap[victim] = esel;
594
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600595 return victim;
596}
597
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000598static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500)
599{
600 int size = vcpu_e500->gtlb_params[1].entries;
601 unsigned int offset;
602 gva_t eaddr;
603 int i;
604
605 vcpu_e500->tlb1_min_eaddr = ~0UL;
606 vcpu_e500->tlb1_max_eaddr = 0;
607 offset = vcpu_e500->gtlb_offset[1];
608
609 for (i = 0; i < size; i++) {
610 struct kvm_book3e_206_tlb_entry *tlbe =
611 &vcpu_e500->gtlb_arch[offset + i];
612
613 if (!get_tlb_v(tlbe))
614 continue;
615
616 eaddr = get_tlb_eaddr(tlbe);
617 vcpu_e500->tlb1_min_eaddr =
618 min(vcpu_e500->tlb1_min_eaddr, eaddr);
619
620 eaddr = get_tlb_end(tlbe);
621 vcpu_e500->tlb1_max_eaddr =
622 max(vcpu_e500->tlb1_max_eaddr, eaddr);
623 }
624}
625
626static int kvmppc_need_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500,
627 struct kvm_book3e_206_tlb_entry *gtlbe)
628{
629 unsigned long start, end, size;
630
631 size = get_tlb_bytes(gtlbe);
632 start = get_tlb_eaddr(gtlbe) & ~(size - 1);
633 end = start + size - 1;
634
635 return vcpu_e500->tlb1_min_eaddr == start ||
636 vcpu_e500->tlb1_max_eaddr == end;
637}
638
639/* This function is supposed to be called for a adding a new valid tlb entry */
640static void kvmppc_set_tlb1map_range(struct kvm_vcpu *vcpu,
641 struct kvm_book3e_206_tlb_entry *gtlbe)
642{
643 unsigned long start, end, size;
644 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
645
646 if (!get_tlb_v(gtlbe))
647 return;
648
649 size = get_tlb_bytes(gtlbe);
650 start = get_tlb_eaddr(gtlbe) & ~(size - 1);
651 end = start + size - 1;
652
653 vcpu_e500->tlb1_min_eaddr = min(vcpu_e500->tlb1_min_eaddr, start);
654 vcpu_e500->tlb1_max_eaddr = max(vcpu_e500->tlb1_max_eaddr, end);
655}
656
Liu Yu08b7fa92011-06-14 18:34:59 -0500657static inline int kvmppc_e500_gtlbe_invalidate(
658 struct kvmppc_vcpu_e500 *vcpu_e500,
659 int tlbsel, int esel)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600660{
Scott Wooddc83b8b2011-08-18 15:25:21 -0500661 struct kvm_book3e_206_tlb_entry *gtlbe =
662 get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600663
664 if (unlikely(get_tlb_iprot(gtlbe)))
665 return -1;
666
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000667 if (tlbsel == 1 && kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
668 kvmppc_recalc_tlb1map_range(vcpu_e500);
669
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600670 gtlbe->mas1 = 0;
671
672 return 0;
673}
674
Liu Yub0a18352009-02-17 16:52:08 +0800675int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value)
676{
677 int esel;
678
679 if (value & MMUCSR0_TLB0FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500680 for (esel = 0; esel < vcpu_e500->gtlb_params[0].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800681 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel);
682 if (value & MMUCSR0_TLB1FI)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500683 for (esel = 0; esel < vcpu_e500->gtlb_params[1].entries; esel++)
Liu Yub0a18352009-02-17 16:52:08 +0800684 kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel);
685
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500686 /* Invalidate all vcpu id mappings */
Scott Wood8fdd21a22011-12-20 15:34:34 +0000687 kvmppc_e500_tlbil_all(vcpu_e500);
Liu Yub0a18352009-02-17 16:52:08 +0800688
689 return EMULATE_DONE;
690}
691
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600692int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb)
693{
694 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
695 unsigned int ia;
696 int esel, tlbsel;
697 gva_t ea;
698
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100699 ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600700
701 ia = (ea >> 2) & 0x1;
702
Liu Yufb2838d2009-01-14 10:47:37 -0600703 /* since we only have two TLBs, only lower bit is used. */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600704 tlbsel = (ea >> 3) & 0x1;
705
706 if (ia) {
707 /* invalidate all entries */
Scott Wooddc83b8b2011-08-18 15:25:21 -0500708 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries;
709 esel++)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600710 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
711 } else {
712 ea &= 0xfffff000;
713 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel,
714 get_cur_pid(vcpu), -1);
715 if (esel >= 0)
716 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
717 }
718
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500719 /* Invalidate all vcpu id mappings */
Scott Wood8fdd21a22011-12-20 15:34:34 +0000720 kvmppc_e500_tlbil_all(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600721
722 return EMULATE_DONE;
723}
724
Scott Woodab9fc402011-12-20 15:34:39 +0000725static void tlbilx_all(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
726 int pid, int rt)
727{
728 struct kvm_book3e_206_tlb_entry *tlbe;
729 int tid, esel;
730
731 /* invalidate all entries */
732 for (esel = 0; esel < vcpu_e500->gtlb_params[tlbsel].entries; esel++) {
733 tlbe = get_entry(vcpu_e500, tlbsel, esel);
734 tid = get_tlb_tid(tlbe);
735 if (rt == 0 || tid == pid) {
736 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
737 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
738 }
739 }
740}
741
742static void tlbilx_one(struct kvmppc_vcpu_e500 *vcpu_e500, int pid,
743 int ra, int rb)
744{
745 int tlbsel, esel;
746 gva_t ea;
747
748 ea = kvmppc_get_gpr(&vcpu_e500->vcpu, rb);
749 if (ra)
750 ea += kvmppc_get_gpr(&vcpu_e500->vcpu, ra);
751
752 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
753 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, -1);
754 if (esel >= 0) {
755 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
756 kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel);
757 break;
758 }
759 }
760}
761
762int kvmppc_e500_emul_tlbilx(struct kvm_vcpu *vcpu, int rt, int ra, int rb)
763{
764 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
765 int pid = get_cur_spid(vcpu);
766
767 if (rt == 0 || rt == 1) {
768 tlbilx_all(vcpu_e500, 0, pid, rt);
769 tlbilx_all(vcpu_e500, 1, pid, rt);
770 } else if (rt == 3) {
771 tlbilx_one(vcpu_e500, pid, ra, rb);
772 }
773
774 return EMULATE_DONE;
775}
776
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600777int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu)
778{
779 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
780 int tlbsel, esel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500781 struct kvm_book3e_206_tlb_entry *gtlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600782
Scott Woodb5904972011-11-08 18:23:30 -0600783 tlbsel = get_tlb_tlbsel(vcpu);
784 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600785
Scott Wooddc83b8b2011-08-18 15:25:21 -0500786 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Scott Woodb5904972011-11-08 18:23:30 -0600787 vcpu->arch.shared->mas0 &= ~MAS0_NV(~0);
788 vcpu->arch.shared->mas0 |= MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
789 vcpu->arch.shared->mas1 = gtlbe->mas1;
790 vcpu->arch.shared->mas2 = gtlbe->mas2;
791 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600792
793 return EMULATE_DONE;
794}
795
796int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb)
797{
798 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Woodb5904972011-11-08 18:23:30 -0600799 int as = !!get_cur_sas(vcpu);
800 unsigned int pid = get_cur_spid(vcpu);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600801 int esel, tlbsel;
Scott Wooddc83b8b2011-08-18 15:25:21 -0500802 struct kvm_book3e_206_tlb_entry *gtlbe = NULL;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600803 gva_t ea;
804
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100805 ea = kvmppc_get_gpr(vcpu, rb);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600806
807 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
808 esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as);
809 if (esel >= 0) {
Scott Wooddc83b8b2011-08-18 15:25:21 -0500810 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600811 break;
812 }
813 }
814
815 if (gtlbe) {
Scott Wood303b7c92011-08-18 15:25:23 -0500816 esel &= vcpu_e500->gtlb_params[tlbsel].ways - 1;
817
Scott Woodb5904972011-11-08 18:23:30 -0600818 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel)
Liu Yu08b7fa92011-06-14 18:34:59 -0500819 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600820 vcpu->arch.shared->mas1 = gtlbe->mas1;
821 vcpu->arch.shared->mas2 = gtlbe->mas2;
822 vcpu->arch.shared->mas7_3 = gtlbe->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600823 } else {
824 int victim;
825
Liu Yufb2838d2009-01-14 10:47:37 -0600826 /* since we only have two TLBs, only lower bit is used. */
Scott Woodb5904972011-11-08 18:23:30 -0600827 tlbsel = vcpu->arch.shared->mas4 >> 28 & 0x1;
Scott Wood0164c0f2011-08-18 15:25:18 -0500828 victim = (tlbsel == 0) ? gtlb0_get_next_victim(vcpu_e500) : 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600829
Scott Woodb5904972011-11-08 18:23:30 -0600830 vcpu->arch.shared->mas0 = MAS0_TLBSEL(tlbsel)
831 | MAS0_ESEL(victim)
Liu Yu08b7fa92011-06-14 18:34:59 -0500832 | MAS0_NV(vcpu_e500->gtlb_nv[tlbsel]);
Scott Woodb5904972011-11-08 18:23:30 -0600833 vcpu->arch.shared->mas1 =
834 (vcpu->arch.shared->mas6 & MAS6_SPID0)
835 | (vcpu->arch.shared->mas6 & (MAS6_SAS ? MAS1_TS : 0))
836 | (vcpu->arch.shared->mas4 & MAS4_TSIZED(~0));
837 vcpu->arch.shared->mas2 &= MAS2_EPN;
838 vcpu->arch.shared->mas2 |= vcpu->arch.shared->mas4 &
839 MAS2_ATTRIB_MASK;
840 vcpu->arch.shared->mas7_3 &= MAS3_U0 | MAS3_U1 |
841 MAS3_U2 | MAS3_U3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600842 }
843
Scott Wood49ea0692011-03-28 15:01:24 -0500844 kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600845 return EMULATE_DONE;
846}
847
Scott Wood57013522011-11-29 10:40:23 +0000848/* sesel is for tlb1 only */
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500849static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
Scott Wooddc83b8b2011-08-18 15:25:21 -0500850 struct kvm_book3e_206_tlb_entry *gtlbe,
851 struct kvm_book3e_206_tlb_entry *stlbe,
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500852 int stlbsel, int sesel)
853{
854 int stid;
855
856 preempt_disable();
Scott Wood8fdd21a22011-12-20 15:34:34 +0000857 stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500858
859 stlbe->mas1 |= MAS1_TID(stid);
860 write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
861 preempt_enable();
862}
863
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600864int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
865{
866 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wood8fdd21a22011-12-20 15:34:34 +0000867 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
868 int tlbsel, esel, stlbsel, sesel;
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000869 int recal = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600870
Scott Woodb5904972011-11-08 18:23:30 -0600871 tlbsel = get_tlb_tlbsel(vcpu);
872 esel = get_tlb_esel(vcpu, tlbsel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600873
Scott Wooddc83b8b2011-08-18 15:25:21 -0500874 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600875
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000876 if (get_tlb_v(gtlbe)) {
Scott Wood0164c0f2011-08-18 15:25:18 -0500877 inval_gtlbe_on_host(vcpu_e500, tlbsel, esel);
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000878 if ((tlbsel == 1) &&
879 kvmppc_need_recalc_tlb1map_range(vcpu_e500, gtlbe))
880 recal = 1;
881 }
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600882
Scott Woodb5904972011-11-08 18:23:30 -0600883 gtlbe->mas1 = vcpu->arch.shared->mas1;
884 gtlbe->mas2 = vcpu->arch.shared->mas2;
885 gtlbe->mas7_3 = vcpu->arch.shared->mas7_3;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600886
Liu Yud37b1a02011-12-20 14:42:56 +0000887 trace_kvm_booke206_gtlb_write(vcpu->arch.shared->mas0, gtlbe->mas1,
888 gtlbe->mas2, gtlbe->mas7_3);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600889
Bharat Bhushancc902ad2012-03-22 18:39:11 +0000890 if (tlbsel == 1) {
891 /*
892 * If a valid tlb1 entry is overwritten then recalculate the
893 * min/max TLB1 map address range otherwise no need to look
894 * in tlb1 array.
895 */
896 if (recal)
897 kvmppc_recalc_tlb1map_range(vcpu_e500);
898 else
899 kvmppc_set_tlb1map_range(vcpu, gtlbe);
900 }
901
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600902 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
903 if (tlbe_is_host_safe(vcpu, gtlbe)) {
Liu Yu08b7fa92011-06-14 18:34:59 -0500904 u64 eaddr;
905 u64 raddr;
906
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600907 switch (tlbsel) {
908 case 0:
909 /* TLB0 */
910 gtlbe->mas1 &= ~MAS1_TSIZE(~0);
Liu Yu0cfb50e2009-06-05 14:54:29 +0800911 gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600912
913 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +0000914 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
915 sesel = 0; /* unused */
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600916
917 break;
918
919 case 1:
920 /* TLB1 */
921 eaddr = get_tlb_eaddr(gtlbe);
922 raddr = get_tlb_raddr(gtlbe);
923
924 /* Create a 4KB mapping on the host.
925 * If the guest wanted a large page,
926 * only the first 4KB is mapped here and the rest
927 * are mapped on the fly. */
928 stlbsel = 1;
929 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
Scott Wood4f802fe2011-12-20 15:34:37 +0000930 raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600931 break;
932
933 default:
934 BUG();
935 }
Scott Wood3bf3cdc2011-08-18 15:25:14 -0500936
937 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600938 }
939
Scott Wood49ea0692011-03-28 15:01:24 -0500940 kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600941 return EMULATE_DONE;
942}
943
Scott Wood8fdd21a22011-12-20 15:34:34 +0000944static int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu,
945 gva_t eaddr, unsigned int pid, int as)
946{
947 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
948 int esel, tlbsel;
949
950 for (tlbsel = 0; tlbsel < 2; tlbsel++) {
951 esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as);
952 if (esel >= 0)
953 return index_of(tlbsel, esel);
954 }
955
956 return -1;
957}
958
959/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
960int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
961 struct kvm_translation *tr)
962{
963 int index;
964 gva_t eaddr;
965 u8 pid;
966 u8 as;
967
968 eaddr = tr->linear_address;
969 pid = (tr->linear_address >> 32) & 0xff;
970 as = (tr->linear_address >> 40) & 0x1;
971
972 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
973 if (index < 0) {
974 tr->valid = 0;
975 return 0;
976 }
977
978 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
979 /* XXX what does "writeable" and "usermode" even mean? */
980 tr->valid = 1;
981
982 return 0;
983}
984
985
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600986int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
987{
Alexander Graf666e7252010-07-29 14:47:43 +0200988 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600989
990 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
991}
992
993int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr)
994{
Alexander Graf666e7252010-07-29 14:47:43 +0200995 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600996
997 return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as);
998}
999
1000void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu)
1001{
Alexander Graf666e7252010-07-29 14:47:43 +02001002 unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001003
1004 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as);
1005}
1006
1007void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu)
1008{
Alexander Graf666e7252010-07-29 14:47:43 +02001009 unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001010
1011 kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as);
1012}
1013
1014gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index,
1015 gva_t eaddr)
1016{
1017 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001018 struct kvm_book3e_206_tlb_entry *gtlbe;
1019 u64 pgmask;
1020
1021 gtlbe = get_entry(vcpu_e500, tlbsel_of(index), esel_of(index));
1022 pgmask = get_tlb_bytes(gtlbe) - 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001023
1024 return get_tlb_raddr(gtlbe) | (eaddr & pgmask);
1025}
1026
1027void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1028{
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001029}
1030
1031void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
1032 unsigned int index)
1033{
1034 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Liu Yu08b7fa92011-06-14 18:34:59 -05001035 struct tlbe_priv *priv;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001036 struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001037 int tlbsel = tlbsel_of(index);
1038 int esel = esel_of(index);
1039 int stlbsel, sesel;
1040
Scott Wooddc83b8b2011-08-18 15:25:21 -05001041 gtlbe = get_entry(vcpu_e500, tlbsel, esel);
Liu Yu08b7fa92011-06-14 18:34:59 -05001042
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001043 switch (tlbsel) {
1044 case 0:
1045 stlbsel = 0;
Scott Wood57013522011-11-29 10:40:23 +00001046 sesel = 0; /* unused */
Scott Wood0164c0f2011-08-18 15:25:18 -05001047 priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
Liu Yu08b7fa92011-06-14 18:34:59 -05001048
Alexander Graf2bb890f2012-08-02 13:38:49 +02001049 /* Only triggers after clear_tlb_refs */
1050 if (unlikely(!(priv->ref.flags & E500_TLB_VALID)))
1051 kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
1052 else
1053 kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
1054 &priv->ref, eaddr, &stlbe);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001055 break;
1056
1057 case 1: {
1058 gfn_t gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001059
1060 stlbsel = 1;
Liu Yu08b7fa92011-06-14 18:34:59 -05001061 sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
Scott Wood4f802fe2011-12-20 15:34:37 +00001062 gtlbe, &stlbe, esel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001063 break;
1064 }
1065
1066 default:
1067 BUG();
1068 break;
1069 }
Liu Yu08b7fa92011-06-14 18:34:59 -05001070
Scott Wood3bf3cdc2011-08-18 15:25:14 -05001071 write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001072}
1073
Alexander Graf862d31f2012-07-31 00:19:50 +02001074/************* MMU Notifiers *************/
1075
1076int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1077{
1078 /*
1079 * Flush all shadow tlb entries everywhere. This is slow, but
1080 * we are 100% sure that we catch the to be unmapped page
1081 */
1082 kvm_flush_remote_tlbs(kvm);
1083
1084 return 0;
1085}
1086
1087int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
1088{
1089 /* kvm_unmap_hva flushes everything anyways */
1090 kvm_unmap_hva(kvm, start);
1091
1092 return 0;
1093}
1094
1095int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1096{
1097 /* XXX could be more clever ;) */
1098 return 0;
1099}
1100
1101int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1102{
1103 /* XXX could be more clever ;) */
1104 return 0;
1105}
1106
1107void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1108{
1109 /* The page will get remapped properly on its next fault */
1110 kvm_unmap_hva(kvm, hva);
1111}
1112
1113/*****************************************/
1114
Scott Wooddc83b8b2011-08-18 15:25:21 -05001115static void free_gtlb(struct kvmppc_vcpu_e500 *vcpu_e500)
1116{
1117 int i;
1118
Scott Wood4f802fe2011-12-20 15:34:37 +00001119 clear_tlb1_bitmap(vcpu_e500);
1120 kfree(vcpu_e500->g2h_tlb1_map);
1121
Scott Wooddc83b8b2011-08-18 15:25:21 -05001122 clear_tlb_refs(vcpu_e500);
1123 kfree(vcpu_e500->gtlb_priv[0]);
1124 kfree(vcpu_e500->gtlb_priv[1]);
1125
1126 if (vcpu_e500->shared_tlb_pages) {
1127 vfree((void *)(round_down((uintptr_t)vcpu_e500->gtlb_arch,
1128 PAGE_SIZE)));
1129
1130 for (i = 0; i < vcpu_e500->num_shared_tlb_pages; i++) {
1131 set_page_dirty_lock(vcpu_e500->shared_tlb_pages[i]);
1132 put_page(vcpu_e500->shared_tlb_pages[i]);
1133 }
1134
1135 vcpu_e500->num_shared_tlb_pages = 0;
1136 vcpu_e500->shared_tlb_pages = NULL;
1137 } else {
1138 kfree(vcpu_e500->gtlb_arch);
1139 }
1140
1141 vcpu_e500->gtlb_arch = NULL;
1142}
1143
Scott Wood8fdd21a22011-12-20 15:34:34 +00001144void kvmppc_get_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1145{
1146 sregs->u.e.mas0 = vcpu->arch.shared->mas0;
1147 sregs->u.e.mas1 = vcpu->arch.shared->mas1;
1148 sregs->u.e.mas2 = vcpu->arch.shared->mas2;
1149 sregs->u.e.mas7_3 = vcpu->arch.shared->mas7_3;
1150 sregs->u.e.mas4 = vcpu->arch.shared->mas4;
1151 sregs->u.e.mas6 = vcpu->arch.shared->mas6;
1152
1153 sregs->u.e.mmucfg = vcpu->arch.mmucfg;
1154 sregs->u.e.tlbcfg[0] = vcpu->arch.tlbcfg[0];
1155 sregs->u.e.tlbcfg[1] = vcpu->arch.tlbcfg[1];
1156 sregs->u.e.tlbcfg[2] = 0;
1157 sregs->u.e.tlbcfg[3] = 0;
1158}
1159
1160int kvmppc_set_sregs_e500_tlb(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1161{
1162 if (sregs->u.e.features & KVM_SREGS_E_ARCH206_MMU) {
1163 vcpu->arch.shared->mas0 = sregs->u.e.mas0;
1164 vcpu->arch.shared->mas1 = sregs->u.e.mas1;
1165 vcpu->arch.shared->mas2 = sregs->u.e.mas2;
1166 vcpu->arch.shared->mas7_3 = sregs->u.e.mas7_3;
1167 vcpu->arch.shared->mas4 = sregs->u.e.mas4;
1168 vcpu->arch.shared->mas6 = sregs->u.e.mas6;
1169 }
1170
1171 return 0;
1172}
1173
Scott Wooddc83b8b2011-08-18 15:25:21 -05001174int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
1175 struct kvm_config_tlb *cfg)
1176{
1177 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
1178 struct kvm_book3e_206_tlb_params params;
1179 char *virt;
1180 struct page **pages;
1181 struct tlbe_priv *privs[2] = {};
Scott Wood4f802fe2011-12-20 15:34:37 +00001182 u64 *g2h_bitmap = NULL;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001183 size_t array_len;
1184 u32 sets;
1185 int num_pages, ret, i;
1186
1187 if (cfg->mmu_type != KVM_MMU_FSL_BOOKE_NOHV)
1188 return -EINVAL;
1189
1190 if (copy_from_user(&params, (void __user *)(uintptr_t)cfg->params,
1191 sizeof(params)))
1192 return -EFAULT;
1193
1194 if (params.tlb_sizes[1] > 64)
1195 return -EINVAL;
1196 if (params.tlb_ways[1] != params.tlb_sizes[1])
1197 return -EINVAL;
1198 if (params.tlb_sizes[2] != 0 || params.tlb_sizes[3] != 0)
1199 return -EINVAL;
1200 if (params.tlb_ways[2] != 0 || params.tlb_ways[3] != 0)
1201 return -EINVAL;
1202
1203 if (!is_power_of_2(params.tlb_ways[0]))
1204 return -EINVAL;
1205
1206 sets = params.tlb_sizes[0] >> ilog2(params.tlb_ways[0]);
1207 if (!is_power_of_2(sets))
1208 return -EINVAL;
1209
1210 array_len = params.tlb_sizes[0] + params.tlb_sizes[1];
1211 array_len *= sizeof(struct kvm_book3e_206_tlb_entry);
1212
1213 if (cfg->array_len < array_len)
1214 return -EINVAL;
1215
1216 num_pages = DIV_ROUND_UP(cfg->array + array_len - 1, PAGE_SIZE) -
1217 cfg->array / PAGE_SIZE;
1218 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1219 if (!pages)
1220 return -ENOMEM;
1221
1222 ret = get_user_pages_fast(cfg->array, num_pages, 1, pages);
1223 if (ret < 0)
1224 goto err_pages;
1225
1226 if (ret != num_pages) {
1227 num_pages = ret;
1228 ret = -EFAULT;
1229 goto err_put_page;
1230 }
1231
1232 virt = vmap(pages, num_pages, VM_MAP, PAGE_KERNEL);
1233 if (!virt)
1234 goto err_put_page;
1235
1236 privs[0] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[0],
1237 GFP_KERNEL);
1238 privs[1] = kzalloc(sizeof(struct tlbe_priv) * params.tlb_sizes[1],
1239 GFP_KERNEL);
1240
1241 if (!privs[0] || !privs[1])
1242 goto err_put_page;
1243
Scott Wood4f802fe2011-12-20 15:34:37 +00001244 g2h_bitmap = kzalloc(sizeof(u64) * params.tlb_sizes[1],
1245 GFP_KERNEL);
1246 if (!g2h_bitmap)
1247 goto err_put_page;
1248
Scott Wooddc83b8b2011-08-18 15:25:21 -05001249 free_gtlb(vcpu_e500);
1250
1251 vcpu_e500->gtlb_priv[0] = privs[0];
1252 vcpu_e500->gtlb_priv[1] = privs[1];
Scott Wood4f802fe2011-12-20 15:34:37 +00001253 vcpu_e500->g2h_tlb1_map = g2h_bitmap;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001254
1255 vcpu_e500->gtlb_arch = (struct kvm_book3e_206_tlb_entry *)
1256 (virt + (cfg->array & (PAGE_SIZE - 1)));
1257
1258 vcpu_e500->gtlb_params[0].entries = params.tlb_sizes[0];
1259 vcpu_e500->gtlb_params[1].entries = params.tlb_sizes[1];
1260
1261 vcpu_e500->gtlb_offset[0] = 0;
1262 vcpu_e500->gtlb_offset[1] = params.tlb_sizes[0];
1263
Scott Wood8fdd21a22011-12-20 15:34:34 +00001264 vcpu->arch.mmucfg = mfspr(SPRN_MMUCFG) & ~MMUCFG_LPIDSIZE;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001265
Scott Wood8fdd21a22011-12-20 15:34:34 +00001266 vcpu->arch.tlbcfg[0] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1267 if (params.tlb_sizes[0] <= 2048)
1268 vcpu->arch.tlbcfg[0] |= params.tlb_sizes[0];
1269 vcpu->arch.tlbcfg[0] |= params.tlb_ways[0] << TLBnCFG_ASSOC_SHIFT;
1270
1271 vcpu->arch.tlbcfg[1] &= ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
1272 vcpu->arch.tlbcfg[1] |= params.tlb_sizes[1];
1273 vcpu->arch.tlbcfg[1] |= params.tlb_ways[1] << TLBnCFG_ASSOC_SHIFT;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001274
1275 vcpu_e500->shared_tlb_pages = pages;
1276 vcpu_e500->num_shared_tlb_pages = num_pages;
1277
1278 vcpu_e500->gtlb_params[0].ways = params.tlb_ways[0];
1279 vcpu_e500->gtlb_params[0].sets = sets;
1280
1281 vcpu_e500->gtlb_params[1].ways = params.tlb_sizes[1];
1282 vcpu_e500->gtlb_params[1].sets = 1;
1283
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001284 kvmppc_recalc_tlb1map_range(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001285 return 0;
1286
1287err_put_page:
1288 kfree(privs[0]);
1289 kfree(privs[1]);
1290
1291 for (i = 0; i < num_pages; i++)
1292 put_page(pages[i]);
1293
1294err_pages:
1295 kfree(pages);
1296 return ret;
1297}
1298
1299int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
1300 struct kvm_dirty_tlb *dirty)
1301{
1302 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001303 kvmppc_recalc_tlb1map_range(vcpu_e500);
Scott Wooddc83b8b2011-08-18 15:25:21 -05001304 clear_tlb_refs(vcpu_e500);
1305 return 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001306}
1307
1308int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500)
1309{
Scott Wood8fdd21a22011-12-20 15:34:34 +00001310 struct kvm_vcpu *vcpu = &vcpu_e500->vcpu;
Scott Wooddc83b8b2011-08-18 15:25:21 -05001311 int entry_size = sizeof(struct kvm_book3e_206_tlb_entry);
1312 int entries = KVM_E500_TLB0_SIZE + KVM_E500_TLB1_SIZE;
1313
Scott Wood0164c0f2011-08-18 15:25:18 -05001314 host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
1315 host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
1316
1317 /*
1318 * This should never happen on real e500 hardware, but is
1319 * architecturally possible -- e.g. in some weird nested
1320 * virtualization case.
1321 */
1322 if (host_tlb_params[0].entries == 0 ||
1323 host_tlb_params[1].entries == 0) {
1324 pr_err("%s: need to know host tlb size\n", __func__);
1325 return -ENODEV;
1326 }
1327
1328 host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
1329 TLBnCFG_ASSOC_SHIFT;
1330 host_tlb_params[1].ways = host_tlb_params[1].entries;
1331
1332 if (!is_power_of_2(host_tlb_params[0].entries) ||
1333 !is_power_of_2(host_tlb_params[0].ways) ||
1334 host_tlb_params[0].entries < host_tlb_params[0].ways ||
1335 host_tlb_params[0].ways == 0) {
1336 pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
1337 __func__, host_tlb_params[0].entries,
1338 host_tlb_params[0].ways);
1339 return -ENODEV;
1340 }
1341
1342 host_tlb_params[0].sets =
1343 host_tlb_params[0].entries / host_tlb_params[0].ways;
1344 host_tlb_params[1].sets = 1;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001345
Scott Wooddc83b8b2011-08-18 15:25:21 -05001346 vcpu_e500->gtlb_params[0].entries = KVM_E500_TLB0_SIZE;
1347 vcpu_e500->gtlb_params[1].entries = KVM_E500_TLB1_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001348
Scott Wooddc83b8b2011-08-18 15:25:21 -05001349 vcpu_e500->gtlb_params[0].ways = KVM_E500_TLB0_WAY_NUM;
1350 vcpu_e500->gtlb_params[0].sets =
1351 KVM_E500_TLB0_SIZE / KVM_E500_TLB0_WAY_NUM;
1352
1353 vcpu_e500->gtlb_params[1].ways = KVM_E500_TLB1_SIZE;
1354 vcpu_e500->gtlb_params[1].sets = 1;
1355
1356 vcpu_e500->gtlb_arch = kmalloc(entries * entry_size, GFP_KERNEL);
1357 if (!vcpu_e500->gtlb_arch)
1358 return -ENOMEM;
1359
1360 vcpu_e500->gtlb_offset[0] = 0;
1361 vcpu_e500->gtlb_offset[1] = KVM_E500_TLB0_SIZE;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001362
Scott Wood0164c0f2011-08-18 15:25:18 -05001363 vcpu_e500->tlb_refs[0] =
1364 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[0].entries,
1365 GFP_KERNEL);
1366 if (!vcpu_e500->tlb_refs[0])
1367 goto err;
Liu Yu08b7fa92011-06-14 18:34:59 -05001368
Scott Wood0164c0f2011-08-18 15:25:18 -05001369 vcpu_e500->tlb_refs[1] =
1370 kzalloc(sizeof(struct tlbe_ref) * host_tlb_params[1].entries,
1371 GFP_KERNEL);
1372 if (!vcpu_e500->tlb_refs[1])
1373 goto err;
1374
Scott Wooddc83b8b2011-08-18 15:25:21 -05001375 vcpu_e500->gtlb_priv[0] = kzalloc(sizeof(struct tlbe_ref) *
1376 vcpu_e500->gtlb_params[0].entries,
1377 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001378 if (!vcpu_e500->gtlb_priv[0])
1379 goto err;
1380
Scott Wooddc83b8b2011-08-18 15:25:21 -05001381 vcpu_e500->gtlb_priv[1] = kzalloc(sizeof(struct tlbe_ref) *
1382 vcpu_e500->gtlb_params[1].entries,
1383 GFP_KERNEL);
Scott Wood0164c0f2011-08-18 15:25:18 -05001384 if (!vcpu_e500->gtlb_priv[1])
1385 goto err;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001386
Scott Wood4f802fe2011-12-20 15:34:37 +00001387 vcpu_e500->g2h_tlb1_map = kzalloc(sizeof(unsigned int) *
1388 vcpu_e500->gtlb_params[1].entries,
1389 GFP_KERNEL);
1390 if (!vcpu_e500->g2h_tlb1_map)
1391 goto err;
1392
1393 vcpu_e500->h2g_tlb1_rmap = kzalloc(sizeof(unsigned int) *
1394 host_tlb_params[1].entries,
1395 GFP_KERNEL);
1396 if (!vcpu_e500->h2g_tlb1_rmap)
1397 goto err;
1398
Liu Yuda15bf42010-01-22 19:36:53 +08001399 /* Init TLB configuration register */
Scott Wood8fdd21a22011-12-20 15:34:34 +00001400 vcpu->arch.tlbcfg[0] = mfspr(SPRN_TLB0CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001401 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Scott Wood8fdd21a22011-12-20 15:34:34 +00001402 vcpu->arch.tlbcfg[0] |= vcpu_e500->gtlb_params[0].entries;
1403 vcpu->arch.tlbcfg[0] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001404 vcpu_e500->gtlb_params[0].ways << TLBnCFG_ASSOC_SHIFT;
1405
Scott Wood8fdd21a22011-12-20 15:34:34 +00001406 vcpu->arch.tlbcfg[1] = mfspr(SPRN_TLB1CFG) &
Scott Wood7b11dc92011-11-28 15:20:02 +00001407 ~(TLBnCFG_N_ENTRY | TLBnCFG_ASSOC);
Alexander Grafc6b37332012-02-20 17:48:47 +01001408 vcpu->arch.tlbcfg[1] |= vcpu_e500->gtlb_params[1].entries;
1409 vcpu->arch.tlbcfg[1] |=
Scott Wood7b11dc92011-11-28 15:20:02 +00001410 vcpu_e500->gtlb_params[1].ways << TLBnCFG_ASSOC_SHIFT;
Liu Yuda15bf42010-01-22 19:36:53 +08001411
Bharat Bhushancc902ad2012-03-22 18:39:11 +00001412 kvmppc_recalc_tlb1map_range(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001413 return 0;
1414
Scott Wood0164c0f2011-08-18 15:25:18 -05001415err:
Scott Wooddc83b8b2011-08-18 15:25:21 -05001416 free_gtlb(vcpu_e500);
Scott Wood0164c0f2011-08-18 15:25:18 -05001417 kfree(vcpu_e500->tlb_refs[0]);
1418 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001419 return -1;
1420}
1421
1422void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
1423{
Scott Wooddc83b8b2011-08-18 15:25:21 -05001424 free_gtlb(vcpu_e500);
Scott Wood4f802fe2011-12-20 15:34:37 +00001425 kfree(vcpu_e500->h2g_tlb1_rmap);
Scott Wood0164c0f2011-08-18 15:25:18 -05001426 kfree(vcpu_e500->tlb_refs[0]);
1427 kfree(vcpu_e500->tlb_refs[1]);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001428}