xref: /linux/arch/powerpc/kvm/e500_mmu_host.c (revision 94171b19c3f1f4d9d4c0e3aaa1aa161def1ec7ea)
1b71c9e2fSAlexander Graf /*
2b71c9e2fSAlexander Graf  * Copyright (C) 2008-2013 Freescale Semiconductor, Inc. All rights reserved.
3b71c9e2fSAlexander Graf  *
4b71c9e2fSAlexander Graf  * Author: Yu Liu, yu.liu@freescale.com
5b71c9e2fSAlexander Graf  *         Scott Wood, scottwood@freescale.com
6b71c9e2fSAlexander Graf  *         Ashish Kalra, ashish.kalra@freescale.com
7b71c9e2fSAlexander Graf  *         Varun Sethi, varun.sethi@freescale.com
8b71c9e2fSAlexander Graf  *         Alexander Graf, agraf@suse.de
9b71c9e2fSAlexander Graf  *
10b71c9e2fSAlexander Graf  * Description:
11b71c9e2fSAlexander Graf  * This file is based on arch/powerpc/kvm/44x_tlb.c,
12b71c9e2fSAlexander Graf  * by Hollis Blanchard <hollisb@us.ibm.com>.
13b71c9e2fSAlexander Graf  *
14b71c9e2fSAlexander Graf  * This program is free software; you can redistribute it and/or modify
15b71c9e2fSAlexander Graf  * it under the terms of the GNU General Public License, version 2, as
16b71c9e2fSAlexander Graf  * published by the Free Software Foundation.
17b71c9e2fSAlexander Graf  */
18b71c9e2fSAlexander Graf 
19b71c9e2fSAlexander Graf #include <linux/kernel.h>
20b71c9e2fSAlexander Graf #include <linux/types.h>
21b71c9e2fSAlexander Graf #include <linux/slab.h>
22b71c9e2fSAlexander Graf #include <linux/string.h>
23b71c9e2fSAlexander Graf #include <linux/kvm.h>
24b71c9e2fSAlexander Graf #include <linux/kvm_host.h>
25b71c9e2fSAlexander Graf #include <linux/highmem.h>
26b71c9e2fSAlexander Graf #include <linux/log2.h>
27b71c9e2fSAlexander Graf #include <linux/uaccess.h>
28589ee628SIngo Molnar #include <linux/sched/mm.h>
29b71c9e2fSAlexander Graf #include <linux/rwsem.h>
30b71c9e2fSAlexander Graf #include <linux/vmalloc.h>
31b71c9e2fSAlexander Graf #include <linux/hugetlb.h>
32b71c9e2fSAlexander Graf #include <asm/kvm_ppc.h>
33*94171b19SAneesh Kumar K.V #include <asm/pte-walk.h>
34b71c9e2fSAlexander Graf 
35b71c9e2fSAlexander Graf #include "e500.h"
36b71c9e2fSAlexander Graf #include "timing.h"
37b71c9e2fSAlexander Graf #include "e500_mmu_host.h"
38b71c9e2fSAlexander Graf 
39dba291f2SAneesh Kumar K.V #include "trace_booke.h"
40dba291f2SAneesh Kumar K.V 
41b71c9e2fSAlexander Graf #define to_htlb1_esel(esel) (host_tlb_params[1].entries - (esel) - 1)
42b71c9e2fSAlexander Graf 
43b71c9e2fSAlexander Graf static struct kvmppc_e500_tlb_params host_tlb_params[E500_TLB_NUM];
44b71c9e2fSAlexander Graf 
45b71c9e2fSAlexander Graf static inline unsigned int tlb1_max_shadow_size(void)
46b71c9e2fSAlexander Graf {
47b71c9e2fSAlexander Graf 	/* reserve one entry for magic page */
48b71c9e2fSAlexander Graf 	return host_tlb_params[1].entries - tlbcam_index - 1;
49b71c9e2fSAlexander Graf }
50b71c9e2fSAlexander Graf 
51b71c9e2fSAlexander Graf static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode)
52b71c9e2fSAlexander Graf {
53b71c9e2fSAlexander Graf 	/* Mask off reserved bits. */
54b71c9e2fSAlexander Graf 	mas3 &= MAS3_ATTRIB_MASK;
55b71c9e2fSAlexander Graf 
56b71c9e2fSAlexander Graf #ifndef CONFIG_KVM_BOOKE_HV
57b71c9e2fSAlexander Graf 	if (!usermode) {
58b71c9e2fSAlexander Graf 		/* Guest is in supervisor mode,
59b71c9e2fSAlexander Graf 		 * so we need to translate guest
60b71c9e2fSAlexander Graf 		 * supervisor permissions into user permissions. */
61b71c9e2fSAlexander Graf 		mas3 &= ~E500_TLB_USER_PERM_MASK;
62b71c9e2fSAlexander Graf 		mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1;
63b71c9e2fSAlexander Graf 	}
64b71c9e2fSAlexander Graf 	mas3 |= E500_TLB_SUPER_PERM_MASK;
65b71c9e2fSAlexander Graf #endif
66b71c9e2fSAlexander Graf 	return mas3;
67b71c9e2fSAlexander Graf }
68b71c9e2fSAlexander Graf 
69b71c9e2fSAlexander Graf /*
70b71c9e2fSAlexander Graf  * writing shadow tlb entry to host TLB
71b71c9e2fSAlexander Graf  */
72b71c9e2fSAlexander Graf static inline void __write_host_tlbe(struct kvm_book3e_206_tlb_entry *stlbe,
73188e267cSMihai Caraman 				     uint32_t mas0,
74188e267cSMihai Caraman 				     uint32_t lpid)
75b71c9e2fSAlexander Graf {
76b71c9e2fSAlexander Graf 	unsigned long flags;
77b71c9e2fSAlexander Graf 
78b71c9e2fSAlexander Graf 	local_irq_save(flags);
79b71c9e2fSAlexander Graf 	mtspr(SPRN_MAS0, mas0);
80b71c9e2fSAlexander Graf 	mtspr(SPRN_MAS1, stlbe->mas1);
81b71c9e2fSAlexander Graf 	mtspr(SPRN_MAS2, (unsigned long)stlbe->mas2);
82b71c9e2fSAlexander Graf 	mtspr(SPRN_MAS3, (u32)stlbe->mas7_3);
83b71c9e2fSAlexander Graf 	mtspr(SPRN_MAS7, (u32)(stlbe->mas7_3 >> 32));
84b71c9e2fSAlexander Graf #ifdef CONFIG_KVM_BOOKE_HV
85188e267cSMihai Caraman 	mtspr(SPRN_MAS8, MAS8_TGS | get_thread_specific_lpid(lpid));
86b71c9e2fSAlexander Graf #endif
87b71c9e2fSAlexander Graf 	asm volatile("isync; tlbwe" : : : "memory");
88b71c9e2fSAlexander Graf 
89b71c9e2fSAlexander Graf #ifdef CONFIG_KVM_BOOKE_HV
90b71c9e2fSAlexander Graf 	/* Must clear mas8 for other host tlbwe's */
91b71c9e2fSAlexander Graf 	mtspr(SPRN_MAS8, 0);
92b71c9e2fSAlexander Graf 	isync();
93b71c9e2fSAlexander Graf #endif
94b71c9e2fSAlexander Graf 	local_irq_restore(flags);
95b71c9e2fSAlexander Graf 
96b71c9e2fSAlexander Graf 	trace_kvm_booke206_stlb_write(mas0, stlbe->mas8, stlbe->mas1,
97b71c9e2fSAlexander Graf 	                              stlbe->mas2, stlbe->mas7_3);
98b71c9e2fSAlexander Graf }
99b71c9e2fSAlexander Graf 
100b71c9e2fSAlexander Graf /*
101b71c9e2fSAlexander Graf  * Acquire a mas0 with victim hint, as if we just took a TLB miss.
102b71c9e2fSAlexander Graf  *
103b71c9e2fSAlexander Graf  * We don't care about the address we're searching for, other than that it's
104b71c9e2fSAlexander Graf  * in the right set and is not present in the TLB.  Using a zero PID and a
105b71c9e2fSAlexander Graf  * userspace address means we don't have to set and then restore MAS5, or
106b71c9e2fSAlexander Graf  * calculate a proper MAS6 value.
107b71c9e2fSAlexander Graf  */
108b71c9e2fSAlexander Graf static u32 get_host_mas0(unsigned long eaddr)
109b71c9e2fSAlexander Graf {
110b71c9e2fSAlexander Graf 	unsigned long flags;
111b71c9e2fSAlexander Graf 	u32 mas0;
112d57cef91SMihai Caraman 	u32 mas4;
113b71c9e2fSAlexander Graf 
114b71c9e2fSAlexander Graf 	local_irq_save(flags);
115b71c9e2fSAlexander Graf 	mtspr(SPRN_MAS6, 0);
116d57cef91SMihai Caraman 	mas4 = mfspr(SPRN_MAS4);
117d57cef91SMihai Caraman 	mtspr(SPRN_MAS4, mas4 & ~MAS4_TLBSEL_MASK);
118b71c9e2fSAlexander Graf 	asm volatile("tlbsx 0, %0" : : "b" (eaddr & ~CONFIG_PAGE_OFFSET));
119b71c9e2fSAlexander Graf 	mas0 = mfspr(SPRN_MAS0);
120d57cef91SMihai Caraman 	mtspr(SPRN_MAS4, mas4);
121b71c9e2fSAlexander Graf 	local_irq_restore(flags);
122b71c9e2fSAlexander Graf 
123b71c9e2fSAlexander Graf 	return mas0;
124b71c9e2fSAlexander Graf }
125b71c9e2fSAlexander Graf 
126b71c9e2fSAlexander Graf /* sesel is for tlb1 only */
127b71c9e2fSAlexander Graf static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
128b71c9e2fSAlexander Graf 		int tlbsel, int sesel, struct kvm_book3e_206_tlb_entry *stlbe)
129b71c9e2fSAlexander Graf {
130b71c9e2fSAlexander Graf 	u32 mas0;
131b71c9e2fSAlexander Graf 
132b71c9e2fSAlexander Graf 	if (tlbsel == 0) {
133b71c9e2fSAlexander Graf 		mas0 = get_host_mas0(stlbe->mas2);
134188e267cSMihai Caraman 		__write_host_tlbe(stlbe, mas0, vcpu_e500->vcpu.kvm->arch.lpid);
135b71c9e2fSAlexander Graf 	} else {
136b71c9e2fSAlexander Graf 		__write_host_tlbe(stlbe,
137b71c9e2fSAlexander Graf 				  MAS0_TLBSEL(1) |
138188e267cSMihai Caraman 				  MAS0_ESEL(to_htlb1_esel(sesel)),
139188e267cSMihai Caraman 				  vcpu_e500->vcpu.kvm->arch.lpid);
140b71c9e2fSAlexander Graf 	}
141b71c9e2fSAlexander Graf }
142b71c9e2fSAlexander Graf 
143b71c9e2fSAlexander Graf /* sesel is for tlb1 only */
144b71c9e2fSAlexander Graf static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
145b71c9e2fSAlexander Graf 			struct kvm_book3e_206_tlb_entry *gtlbe,
146b71c9e2fSAlexander Graf 			struct kvm_book3e_206_tlb_entry *stlbe,
147b71c9e2fSAlexander Graf 			int stlbsel, int sesel)
148b71c9e2fSAlexander Graf {
149b71c9e2fSAlexander Graf 	int stid;
150b71c9e2fSAlexander Graf 
151b71c9e2fSAlexander Graf 	preempt_disable();
152b71c9e2fSAlexander Graf 	stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
153b71c9e2fSAlexander Graf 
154b71c9e2fSAlexander Graf 	stlbe->mas1 |= MAS1_TID(stid);
155b71c9e2fSAlexander Graf 	write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
156b71c9e2fSAlexander Graf 	preempt_enable();
157b71c9e2fSAlexander Graf }
158b71c9e2fSAlexander Graf 
159b71c9e2fSAlexander Graf #ifdef CONFIG_KVM_E500V2
160b71c9e2fSAlexander Graf /* XXX should be a hook in the gva2hpa translation */
161b71c9e2fSAlexander Graf void kvmppc_map_magic(struct kvm_vcpu *vcpu)
162b71c9e2fSAlexander Graf {
163b71c9e2fSAlexander Graf 	struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
164b71c9e2fSAlexander Graf 	struct kvm_book3e_206_tlb_entry magic;
165b71c9e2fSAlexander Graf 	ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
166b71c9e2fSAlexander Graf 	unsigned int stid;
167ba049e93SDan Williams 	kvm_pfn_t pfn;
168b71c9e2fSAlexander Graf 
169ba049e93SDan Williams 	pfn = (kvm_pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT;
170b71c9e2fSAlexander Graf 	get_page(pfn_to_page(pfn));
171b71c9e2fSAlexander Graf 
172b71c9e2fSAlexander Graf 	preempt_disable();
173b71c9e2fSAlexander Graf 	stid = kvmppc_e500_get_sid(vcpu_e500, 0, 0, 0, 0);
174b71c9e2fSAlexander Graf 
175b71c9e2fSAlexander Graf 	magic.mas1 = MAS1_VALID | MAS1_TS | MAS1_TID(stid) |
176b71c9e2fSAlexander Graf 		     MAS1_TSIZE(BOOK3E_PAGESZ_4K);
177b71c9e2fSAlexander Graf 	magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M;
178b71c9e2fSAlexander Graf 	magic.mas7_3 = ((u64)pfn << PAGE_SHIFT) |
179b71c9e2fSAlexander Graf 		       MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR;
180b71c9e2fSAlexander Graf 	magic.mas8 = 0;
181b71c9e2fSAlexander Graf 
182188e267cSMihai Caraman 	__write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index), 0);
183b71c9e2fSAlexander Graf 	preempt_enable();
184b71c9e2fSAlexander Graf }
185b71c9e2fSAlexander Graf #endif
186b71c9e2fSAlexander Graf 
187b71c9e2fSAlexander Graf void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
188b71c9e2fSAlexander Graf 			 int esel)
189b71c9e2fSAlexander Graf {
190b71c9e2fSAlexander Graf 	struct kvm_book3e_206_tlb_entry *gtlbe =
191b71c9e2fSAlexander Graf 		get_entry(vcpu_e500, tlbsel, esel);
192b71c9e2fSAlexander Graf 	struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[tlbsel][esel].ref;
193b71c9e2fSAlexander Graf 
194b71c9e2fSAlexander Graf 	/* Don't bother with unmapped entries */
1954d2be6f7SScott Wood 	if (!(ref->flags & E500_TLB_VALID)) {
1964d2be6f7SScott Wood 		WARN(ref->flags & (E500_TLB_BITMAP | E500_TLB_TLB0),
1974d2be6f7SScott Wood 		     "%s: flags %x\n", __func__, ref->flags);
1984d2be6f7SScott Wood 		WARN_ON(tlbsel == 1 && vcpu_e500->g2h_tlb1_map[esel]);
1994d2be6f7SScott Wood 	}
200b71c9e2fSAlexander Graf 
201b71c9e2fSAlexander Graf 	if (tlbsel == 1 && ref->flags & E500_TLB_BITMAP) {
202b71c9e2fSAlexander Graf 		u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
203b71c9e2fSAlexander Graf 		int hw_tlb_indx;
204b71c9e2fSAlexander Graf 		unsigned long flags;
205b71c9e2fSAlexander Graf 
206b71c9e2fSAlexander Graf 		local_irq_save(flags);
207b71c9e2fSAlexander Graf 		while (tmp) {
208b71c9e2fSAlexander Graf 			hw_tlb_indx = __ilog2_u64(tmp & -tmp);
209b71c9e2fSAlexander Graf 			mtspr(SPRN_MAS0,
210b71c9e2fSAlexander Graf 			      MAS0_TLBSEL(1) |
211b71c9e2fSAlexander Graf 			      MAS0_ESEL(to_htlb1_esel(hw_tlb_indx)));
212b71c9e2fSAlexander Graf 			mtspr(SPRN_MAS1, 0);
213b71c9e2fSAlexander Graf 			asm volatile("tlbwe");
214b71c9e2fSAlexander Graf 			vcpu_e500->h2g_tlb1_rmap[hw_tlb_indx] = 0;
215b71c9e2fSAlexander Graf 			tmp &= tmp - 1;
216b71c9e2fSAlexander Graf 		}
217b71c9e2fSAlexander Graf 		mb();
218b71c9e2fSAlexander Graf 		vcpu_e500->g2h_tlb1_map[esel] = 0;
219b71c9e2fSAlexander Graf 		ref->flags &= ~(E500_TLB_BITMAP | E500_TLB_VALID);
220b71c9e2fSAlexander Graf 		local_irq_restore(flags);
221b71c9e2fSAlexander Graf 	}
222b71c9e2fSAlexander Graf 
223c015c62bSAlexander Graf 	if (tlbsel == 1 && ref->flags & E500_TLB_TLB0) {
224c015c62bSAlexander Graf 		/*
225c015c62bSAlexander Graf 		 * TLB1 entry is backed by 4k pages. This should happen
226c015c62bSAlexander Graf 		 * rarely and is not worth optimizing. Invalidate everything.
227c015c62bSAlexander Graf 		 */
228c015c62bSAlexander Graf 		kvmppc_e500_tlbil_all(vcpu_e500);
229c015c62bSAlexander Graf 		ref->flags &= ~(E500_TLB_TLB0 | E500_TLB_VALID);
230c015c62bSAlexander Graf 	}
231c015c62bSAlexander Graf 
23230a91fe2SBharat Bhushan 	/*
23330a91fe2SBharat Bhushan 	 * If TLB entry is still valid then it's a TLB0 entry, and thus
23430a91fe2SBharat Bhushan 	 * backed by at most one host tlbe per shadow pid
23530a91fe2SBharat Bhushan 	 */
23630a91fe2SBharat Bhushan 	if (ref->flags & E500_TLB_VALID)
237b71c9e2fSAlexander Graf 		kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
238b71c9e2fSAlexander Graf 
239b71c9e2fSAlexander Graf 	/* Mark the TLB as not backed by the host anymore */
24030a91fe2SBharat Bhushan 	ref->flags = 0;
241b71c9e2fSAlexander Graf }
242b71c9e2fSAlexander Graf 
243b71c9e2fSAlexander Graf static inline int tlbe_is_writable(struct kvm_book3e_206_tlb_entry *tlbe)
244b71c9e2fSAlexander Graf {
245b71c9e2fSAlexander Graf 	return tlbe->mas7_3 & (MAS3_SW|MAS3_UW);
246b71c9e2fSAlexander Graf }
247b71c9e2fSAlexander Graf 
248b71c9e2fSAlexander Graf static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
249b71c9e2fSAlexander Graf 					 struct kvm_book3e_206_tlb_entry *gtlbe,
250ba049e93SDan Williams 					 kvm_pfn_t pfn, unsigned int wimg)
251b71c9e2fSAlexander Graf {
252b71c9e2fSAlexander Graf 	ref->pfn = pfn;
25330a91fe2SBharat Bhushan 	ref->flags = E500_TLB_VALID;
254b71c9e2fSAlexander Graf 
25508c9a188SBharat Bhushan 	/* Use guest supplied MAS2_G and MAS2_E */
25608c9a188SBharat Bhushan 	ref->flags |= (gtlbe->mas2 & MAS2_ATTRIB_MASK) | wimg;
25708c9a188SBharat Bhushan 
25884e4d632SBharat Bhushan 	/* Mark the page accessed */
25984e4d632SBharat Bhushan 	kvm_set_pfn_accessed(pfn);
26084e4d632SBharat Bhushan 
261b71c9e2fSAlexander Graf 	if (tlbe_is_writable(gtlbe))
262b71c9e2fSAlexander Graf 		kvm_set_pfn_dirty(pfn);
263b71c9e2fSAlexander Graf }
264b71c9e2fSAlexander Graf 
265b71c9e2fSAlexander Graf static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
266b71c9e2fSAlexander Graf {
267b71c9e2fSAlexander Graf 	if (ref->flags & E500_TLB_VALID) {
2684d2be6f7SScott Wood 		/* FIXME: don't log bogus pfn for TLB1 */
269b71c9e2fSAlexander Graf 		trace_kvm_booke206_ref_release(ref->pfn, ref->flags);
270b71c9e2fSAlexander Graf 		ref->flags = 0;
271b71c9e2fSAlexander Graf 	}
272b71c9e2fSAlexander Graf }
273b71c9e2fSAlexander Graf 
274483ba97cSAlexander Graf static void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500)
275b71c9e2fSAlexander Graf {
276b71c9e2fSAlexander Graf 	if (vcpu_e500->g2h_tlb1_map)
277b71c9e2fSAlexander Graf 		memset(vcpu_e500->g2h_tlb1_map, 0,
278b71c9e2fSAlexander Graf 		       sizeof(u64) * vcpu_e500->gtlb_params[1].entries);
279b71c9e2fSAlexander Graf 	if (vcpu_e500->h2g_tlb1_rmap)
280b71c9e2fSAlexander Graf 		memset(vcpu_e500->h2g_tlb1_rmap, 0,
281b71c9e2fSAlexander Graf 		       sizeof(unsigned int) * host_tlb_params[1].entries);
282b71c9e2fSAlexander Graf }
283b71c9e2fSAlexander Graf 
284b71c9e2fSAlexander Graf static void clear_tlb_privs(struct kvmppc_vcpu_e500 *vcpu_e500)
285b71c9e2fSAlexander Graf {
2864d2be6f7SScott Wood 	int tlbsel;
287b71c9e2fSAlexander Graf 	int i;
288b71c9e2fSAlexander Graf 
2894d2be6f7SScott Wood 	for (tlbsel = 0; tlbsel <= 1; tlbsel++) {
290b71c9e2fSAlexander Graf 		for (i = 0; i < vcpu_e500->gtlb_params[tlbsel].entries; i++) {
291b71c9e2fSAlexander Graf 			struct tlbe_ref *ref =
292b71c9e2fSAlexander Graf 				&vcpu_e500->gtlb_priv[tlbsel][i].ref;
293b71c9e2fSAlexander Graf 			kvmppc_e500_ref_release(ref);
294b71c9e2fSAlexander Graf 		}
295b71c9e2fSAlexander Graf 	}
296b71c9e2fSAlexander Graf }
297b71c9e2fSAlexander Graf 
298b71c9e2fSAlexander Graf void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu)
299b71c9e2fSAlexander Graf {
300b71c9e2fSAlexander Graf 	struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
3014d2be6f7SScott Wood 	kvmppc_e500_tlbil_all(vcpu_e500);
3024d2be6f7SScott Wood 	clear_tlb_privs(vcpu_e500);
303b71c9e2fSAlexander Graf 	clear_tlb1_bitmap(vcpu_e500);
304b71c9e2fSAlexander Graf }
305b71c9e2fSAlexander Graf 
306b71c9e2fSAlexander Graf /* TID must be supplied by the caller */
307b71c9e2fSAlexander Graf static void kvmppc_e500_setup_stlbe(
308b71c9e2fSAlexander Graf 	struct kvm_vcpu *vcpu,
309b71c9e2fSAlexander Graf 	struct kvm_book3e_206_tlb_entry *gtlbe,
310b71c9e2fSAlexander Graf 	int tsize, struct tlbe_ref *ref, u64 gvaddr,
311b71c9e2fSAlexander Graf 	struct kvm_book3e_206_tlb_entry *stlbe)
312b71c9e2fSAlexander Graf {
313ba049e93SDan Williams 	kvm_pfn_t pfn = ref->pfn;
314b71c9e2fSAlexander Graf 	u32 pr = vcpu->arch.shared->msr & MSR_PR;
315b71c9e2fSAlexander Graf 
316b71c9e2fSAlexander Graf 	BUG_ON(!(ref->flags & E500_TLB_VALID));
317b71c9e2fSAlexander Graf 
318b71c9e2fSAlexander Graf 	/* Force IPROT=0 for all guest mappings. */
319b71c9e2fSAlexander Graf 	stlbe->mas1 = MAS1_TSIZE(tsize) | get_tlb_sts(gtlbe) | MAS1_VALID;
32008c9a188SBharat Bhushan 	stlbe->mas2 = (gvaddr & MAS2_EPN) | (ref->flags & E500_TLB_MAS2_ATTR);
321b71c9e2fSAlexander Graf 	stlbe->mas7_3 = ((u64)pfn << PAGE_SHIFT) |
322b71c9e2fSAlexander Graf 			e500_shadow_mas3_attrib(gtlbe->mas7_3, pr);
323b71c9e2fSAlexander Graf }
324b71c9e2fSAlexander Graf 
325b71c9e2fSAlexander Graf static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
326b71c9e2fSAlexander Graf 	u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
327b71c9e2fSAlexander Graf 	int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
328b71c9e2fSAlexander Graf 	struct tlbe_ref *ref)
329b71c9e2fSAlexander Graf {
330b71c9e2fSAlexander Graf 	struct kvm_memory_slot *slot;
331b71c9e2fSAlexander Graf 	unsigned long pfn = 0; /* silence GCC warning */
332b71c9e2fSAlexander Graf 	unsigned long hva;
333b71c9e2fSAlexander Graf 	int pfnmap = 0;
334b71c9e2fSAlexander Graf 	int tsize = BOOK3E_PAGESZ_4K;
33540fde70dSBharat Bhushan 	int ret = 0;
33640fde70dSBharat Bhushan 	unsigned long mmu_seq;
33740fde70dSBharat Bhushan 	struct kvm *kvm = vcpu_e500->vcpu.kvm;
33808c9a188SBharat Bhushan 	unsigned long tsize_pages = 0;
33908c9a188SBharat Bhushan 	pte_t *ptep;
34008c9a188SBharat Bhushan 	unsigned int wimg = 0;
34108c9a188SBharat Bhushan 	pgd_t *pgdir;
342691e95fdSAneesh Kumar K.V 	unsigned long flags;
34340fde70dSBharat Bhushan 
34440fde70dSBharat Bhushan 	/* used to check for invalidations in progress */
34540fde70dSBharat Bhushan 	mmu_seq = kvm->mmu_notifier_seq;
34640fde70dSBharat Bhushan 	smp_rmb();
347b71c9e2fSAlexander Graf 
348b71c9e2fSAlexander Graf 	/*
349b71c9e2fSAlexander Graf 	 * Translate guest physical to true physical, acquiring
350b71c9e2fSAlexander Graf 	 * a page reference if it is normal, non-reserved memory.
351b71c9e2fSAlexander Graf 	 *
352b71c9e2fSAlexander Graf 	 * gfn_to_memslot() must succeed because otherwise we wouldn't
353b71c9e2fSAlexander Graf 	 * have gotten this far.  Eventually we should just pass the slot
354b71c9e2fSAlexander Graf 	 * pointer through from the first lookup.
355b71c9e2fSAlexander Graf 	 */
356b71c9e2fSAlexander Graf 	slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn);
357b71c9e2fSAlexander Graf 	hva = gfn_to_hva_memslot(slot, gfn);
358b71c9e2fSAlexander Graf 
359b71c9e2fSAlexander Graf 	if (tlbsel == 1) {
360b71c9e2fSAlexander Graf 		struct vm_area_struct *vma;
361b71c9e2fSAlexander Graf 		down_read(&current->mm->mmap_sem);
362b71c9e2fSAlexander Graf 
363b71c9e2fSAlexander Graf 		vma = find_vma(current->mm, hva);
364b71c9e2fSAlexander Graf 		if (vma && hva >= vma->vm_start &&
365b71c9e2fSAlexander Graf 		    (vma->vm_flags & VM_PFNMAP)) {
366b71c9e2fSAlexander Graf 			/*
367b71c9e2fSAlexander Graf 			 * This VMA is a physically contiguous region (e.g.
368b71c9e2fSAlexander Graf 			 * /dev/mem) that bypasses normal Linux page
369b71c9e2fSAlexander Graf 			 * management.  Find the overlap between the
370b71c9e2fSAlexander Graf 			 * vma and the memslot.
371b71c9e2fSAlexander Graf 			 */
372b71c9e2fSAlexander Graf 
373b71c9e2fSAlexander Graf 			unsigned long start, end;
374b71c9e2fSAlexander Graf 			unsigned long slot_start, slot_end;
375b71c9e2fSAlexander Graf 
376b71c9e2fSAlexander Graf 			pfnmap = 1;
377b71c9e2fSAlexander Graf 
378b71c9e2fSAlexander Graf 			start = vma->vm_pgoff;
379b71c9e2fSAlexander Graf 			end = start +
380b71c9e2fSAlexander Graf 			      ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT);
381b71c9e2fSAlexander Graf 
382b71c9e2fSAlexander Graf 			pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT);
383b71c9e2fSAlexander Graf 
384b71c9e2fSAlexander Graf 			slot_start = pfn - (gfn - slot->base_gfn);
385b71c9e2fSAlexander Graf 			slot_end = slot_start + slot->npages;
386b71c9e2fSAlexander Graf 
387b71c9e2fSAlexander Graf 			if (start < slot_start)
388b71c9e2fSAlexander Graf 				start = slot_start;
389b71c9e2fSAlexander Graf 			if (end > slot_end)
390b71c9e2fSAlexander Graf 				end = slot_end;
391b71c9e2fSAlexander Graf 
392b71c9e2fSAlexander Graf 			tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
393b71c9e2fSAlexander Graf 				MAS1_TSIZE_SHIFT;
394b71c9e2fSAlexander Graf 
395b71c9e2fSAlexander Graf 			/*
396b71c9e2fSAlexander Graf 			 * e500 doesn't implement the lowest tsize bit,
397b71c9e2fSAlexander Graf 			 * or 1K pages.
398b71c9e2fSAlexander Graf 			 */
399b71c9e2fSAlexander Graf 			tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
400b71c9e2fSAlexander Graf 
401b71c9e2fSAlexander Graf 			/*
402b71c9e2fSAlexander Graf 			 * Now find the largest tsize (up to what the guest
403b71c9e2fSAlexander Graf 			 * requested) that will cover gfn, stay within the
404b71c9e2fSAlexander Graf 			 * range, and for which gfn and pfn are mutually
405b71c9e2fSAlexander Graf 			 * aligned.
406b71c9e2fSAlexander Graf 			 */
407b71c9e2fSAlexander Graf 
408b71c9e2fSAlexander Graf 			for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) {
40908c9a188SBharat Bhushan 				unsigned long gfn_start, gfn_end;
410224f3632STudor Laurentiu 				tsize_pages = 1UL << (tsize - 2);
411b71c9e2fSAlexander Graf 
412b71c9e2fSAlexander Graf 				gfn_start = gfn & ~(tsize_pages - 1);
413b71c9e2fSAlexander Graf 				gfn_end = gfn_start + tsize_pages;
414b71c9e2fSAlexander Graf 
415b71c9e2fSAlexander Graf 				if (gfn_start + pfn - gfn < start)
416b71c9e2fSAlexander Graf 					continue;
417b71c9e2fSAlexander Graf 				if (gfn_end + pfn - gfn > end)
418b71c9e2fSAlexander Graf 					continue;
419b71c9e2fSAlexander Graf 				if ((gfn & (tsize_pages - 1)) !=
420b71c9e2fSAlexander Graf 				    (pfn & (tsize_pages - 1)))
421b71c9e2fSAlexander Graf 					continue;
422b71c9e2fSAlexander Graf 
423b71c9e2fSAlexander Graf 				gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
424b71c9e2fSAlexander Graf 				pfn &= ~(tsize_pages - 1);
425b71c9e2fSAlexander Graf 				break;
426b71c9e2fSAlexander Graf 			}
427b71c9e2fSAlexander Graf 		} else if (vma && hva >= vma->vm_start &&
428b71c9e2fSAlexander Graf 			   (vma->vm_flags & VM_HUGETLB)) {
429b71c9e2fSAlexander Graf 			unsigned long psize = vma_kernel_pagesize(vma);
430b71c9e2fSAlexander Graf 
431b71c9e2fSAlexander Graf 			tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
432b71c9e2fSAlexander Graf 				MAS1_TSIZE_SHIFT;
433b71c9e2fSAlexander Graf 
434b71c9e2fSAlexander Graf 			/*
435b71c9e2fSAlexander Graf 			 * Take the largest page size that satisfies both host
436b71c9e2fSAlexander Graf 			 * and guest mapping
437b71c9e2fSAlexander Graf 			 */
438b71c9e2fSAlexander Graf 			tsize = min(__ilog2(psize) - 10, tsize);
439b71c9e2fSAlexander Graf 
440b71c9e2fSAlexander Graf 			/*
441b71c9e2fSAlexander Graf 			 * e500 doesn't implement the lowest tsize bit,
442b71c9e2fSAlexander Graf 			 * or 1K pages.
443b71c9e2fSAlexander Graf 			 */
444b71c9e2fSAlexander Graf 			tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
445b71c9e2fSAlexander Graf 		}
446b71c9e2fSAlexander Graf 
447b71c9e2fSAlexander Graf 		up_read(&current->mm->mmap_sem);
448b71c9e2fSAlexander Graf 	}
449b71c9e2fSAlexander Graf 
450b71c9e2fSAlexander Graf 	if (likely(!pfnmap)) {
451224f3632STudor Laurentiu 		tsize_pages = 1UL << (tsize + 10 - PAGE_SHIFT);
452b71c9e2fSAlexander Graf 		pfn = gfn_to_pfn_memslot(slot, gfn);
453b71c9e2fSAlexander Graf 		if (is_error_noslot_pfn(pfn)) {
45408c9a188SBharat Bhushan 			if (printk_ratelimit())
45508c9a188SBharat Bhushan 				pr_err("%s: real page not found for gfn %lx\n",
45608c9a188SBharat Bhushan 				       __func__, (long)gfn);
457b71c9e2fSAlexander Graf 			return -EINVAL;
458b71c9e2fSAlexander Graf 		}
459b71c9e2fSAlexander Graf 
460b71c9e2fSAlexander Graf 		/* Align guest and physical address to page map boundaries */
461b71c9e2fSAlexander Graf 		pfn &= ~(tsize_pages - 1);
462b71c9e2fSAlexander Graf 		gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
463b71c9e2fSAlexander Graf 	}
464b71c9e2fSAlexander Graf 
46540fde70dSBharat Bhushan 	spin_lock(&kvm->mmu_lock);
46640fde70dSBharat Bhushan 	if (mmu_notifier_retry(kvm, mmu_seq)) {
46740fde70dSBharat Bhushan 		ret = -EAGAIN;
46840fde70dSBharat Bhushan 		goto out;
46940fde70dSBharat Bhushan 	}
47040fde70dSBharat Bhushan 
47108c9a188SBharat Bhushan 
47208c9a188SBharat Bhushan 	pgdir = vcpu_e500->vcpu.arch.pgdir;
473691e95fdSAneesh Kumar K.V 	/*
474691e95fdSAneesh Kumar K.V 	 * We are just looking at the wimg bits, so we don't
475691e95fdSAneesh Kumar K.V 	 * care much about the trans splitting bit.
476691e95fdSAneesh Kumar K.V 	 * We are holding kvm->mmu_lock so a notifier invalidate
477691e95fdSAneesh Kumar K.V 	 * can't run hence pfn won't change.
478691e95fdSAneesh Kumar K.V 	 */
479691e95fdSAneesh Kumar K.V 	local_irq_save(flags);
480*94171b19SAneesh Kumar K.V 	ptep = find_linux_pte(pgdir, hva, NULL, NULL);
4815e1d44aeSAneesh Kumar K.V 	if (ptep) {
4825e1d44aeSAneesh Kumar K.V 		pte_t pte = READ_ONCE(*ptep);
4835e1d44aeSAneesh Kumar K.V 
484691e95fdSAneesh Kumar K.V 		if (pte_present(pte)) {
4855e1d44aeSAneesh Kumar K.V 			wimg = (pte_val(pte) >> PTE_WIMGE_SHIFT) &
4865e1d44aeSAneesh Kumar K.V 				MAS2_WIMGE_MASK;
487691e95fdSAneesh Kumar K.V 			local_irq_restore(flags);
488691e95fdSAneesh Kumar K.V 		} else {
489691e95fdSAneesh Kumar K.V 			local_irq_restore(flags);
4905e1d44aeSAneesh Kumar K.V 			pr_err_ratelimited("%s: pte not present: gfn %lx,pfn %lx\n",
49108c9a188SBharat Bhushan 					   __func__, (long)gfn, pfn);
492511c6681SMihai Caraman 			ret = -EINVAL;
493511c6681SMihai Caraman 			goto out;
49408c9a188SBharat Bhushan 		}
4955e1d44aeSAneesh Kumar K.V 	}
49608c9a188SBharat Bhushan 	kvmppc_e500_ref_setup(ref, gtlbe, pfn, wimg);
497b71c9e2fSAlexander Graf 
498b71c9e2fSAlexander Graf 	kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
499b71c9e2fSAlexander Graf 				ref, gvaddr, stlbe);
500b71c9e2fSAlexander Graf 
501b71c9e2fSAlexander Graf 	/* Clear i-cache for new pages */
502b71c9e2fSAlexander Graf 	kvmppc_mmu_flush_icache(pfn);
503b71c9e2fSAlexander Graf 
50440fde70dSBharat Bhushan out:
50540fde70dSBharat Bhushan 	spin_unlock(&kvm->mmu_lock);
50640fde70dSBharat Bhushan 
507b71c9e2fSAlexander Graf 	/* Drop refcount on page, so that mmu notifiers can clear it */
508b71c9e2fSAlexander Graf 	kvm_release_pfn_clean(pfn);
509b71c9e2fSAlexander Graf 
51040fde70dSBharat Bhushan 	return ret;
511b71c9e2fSAlexander Graf }
512b71c9e2fSAlexander Graf 
513b71c9e2fSAlexander Graf /* XXX only map the one-one case, for now use TLB0 */
514b71c9e2fSAlexander Graf static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500, int esel,
515b71c9e2fSAlexander Graf 				struct kvm_book3e_206_tlb_entry *stlbe)
516b71c9e2fSAlexander Graf {
517b71c9e2fSAlexander Graf 	struct kvm_book3e_206_tlb_entry *gtlbe;
518b71c9e2fSAlexander Graf 	struct tlbe_ref *ref;
519b71c9e2fSAlexander Graf 	int stlbsel = 0;
520b71c9e2fSAlexander Graf 	int sesel = 0;
521b71c9e2fSAlexander Graf 	int r;
522b71c9e2fSAlexander Graf 
523b71c9e2fSAlexander Graf 	gtlbe = get_entry(vcpu_e500, 0, esel);
524b71c9e2fSAlexander Graf 	ref = &vcpu_e500->gtlb_priv[0][esel].ref;
525b71c9e2fSAlexander Graf 
526b71c9e2fSAlexander Graf 	r = kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
527b71c9e2fSAlexander Graf 			get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
528b71c9e2fSAlexander Graf 			gtlbe, 0, stlbe, ref);
529b71c9e2fSAlexander Graf 	if (r)
530b71c9e2fSAlexander Graf 		return r;
531b71c9e2fSAlexander Graf 
532b71c9e2fSAlexander Graf 	write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
533b71c9e2fSAlexander Graf 
534b71c9e2fSAlexander Graf 	return 0;
535b71c9e2fSAlexander Graf }
536b71c9e2fSAlexander Graf 
537c015c62bSAlexander Graf static int kvmppc_e500_tlb1_map_tlb1(struct kvmppc_vcpu_e500 *vcpu_e500,
538c015c62bSAlexander Graf 				     struct tlbe_ref *ref,
539c015c62bSAlexander Graf 				     int esel)
540b71c9e2fSAlexander Graf {
541c015c62bSAlexander Graf 	unsigned int sesel = vcpu_e500->host_tlb1_nv++;
542b71c9e2fSAlexander Graf 
543b71c9e2fSAlexander Graf 	if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
544b71c9e2fSAlexander Graf 		vcpu_e500->host_tlb1_nv = 0;
545b71c9e2fSAlexander Graf 
546b71c9e2fSAlexander Graf 	if (vcpu_e500->h2g_tlb1_rmap[sesel]) {
5476b2ba1a9SScott Wood 		unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel] - 1;
548b71c9e2fSAlexander Graf 		vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
549b71c9e2fSAlexander Graf 	}
55066a5fecdSScott Wood 
55166a5fecdSScott Wood 	vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
55266a5fecdSScott Wood 	vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
5536b2ba1a9SScott Wood 	vcpu_e500->h2g_tlb1_rmap[sesel] = esel + 1;
5544d2be6f7SScott Wood 	WARN_ON(!(ref->flags & E500_TLB_VALID));
555b71c9e2fSAlexander Graf 
556c015c62bSAlexander Graf 	return sesel;
557c015c62bSAlexander Graf }
558c015c62bSAlexander Graf 
559c015c62bSAlexander Graf /* Caller must ensure that the specified guest TLB entry is safe to insert into
560c015c62bSAlexander Graf  * the shadow TLB. */
561c015c62bSAlexander Graf /* For both one-one and one-to-many */
562c015c62bSAlexander Graf static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
563c015c62bSAlexander Graf 		u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
564c015c62bSAlexander Graf 		struct kvm_book3e_206_tlb_entry *stlbe, int esel)
565c015c62bSAlexander Graf {
5664d2be6f7SScott Wood 	struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[1][esel].ref;
567c015c62bSAlexander Graf 	int sesel;
568c015c62bSAlexander Graf 	int r;
569c015c62bSAlexander Graf 
570c015c62bSAlexander Graf 	r = kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe,
5714d2be6f7SScott Wood 				   ref);
572c015c62bSAlexander Graf 	if (r)
573c015c62bSAlexander Graf 		return r;
574c015c62bSAlexander Graf 
575c015c62bSAlexander Graf 	/* Use TLB0 when we can only map a page with 4k */
576c015c62bSAlexander Graf 	if (get_tlb_tsize(stlbe) == BOOK3E_PAGESZ_4K) {
577c015c62bSAlexander Graf 		vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_TLB0;
578c015c62bSAlexander Graf 		write_stlbe(vcpu_e500, gtlbe, stlbe, 0, 0);
579c015c62bSAlexander Graf 		return 0;
580c015c62bSAlexander Graf 	}
581c015c62bSAlexander Graf 
582c015c62bSAlexander Graf 	/* Otherwise map into TLB1 */
5834d2be6f7SScott Wood 	sesel = kvmppc_e500_tlb1_map_tlb1(vcpu_e500, ref, esel);
584c015c62bSAlexander Graf 	write_stlbe(vcpu_e500, gtlbe, stlbe, 1, sesel);
585b71c9e2fSAlexander Graf 
586b71c9e2fSAlexander Graf 	return 0;
587b71c9e2fSAlexander Graf }
588b71c9e2fSAlexander Graf 
589b71c9e2fSAlexander Graf void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
590b71c9e2fSAlexander Graf 		    unsigned int index)
591b71c9e2fSAlexander Graf {
592b71c9e2fSAlexander Graf 	struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
593b71c9e2fSAlexander Graf 	struct tlbe_priv *priv;
594b71c9e2fSAlexander Graf 	struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
595b71c9e2fSAlexander Graf 	int tlbsel = tlbsel_of(index);
596b71c9e2fSAlexander Graf 	int esel = esel_of(index);
597b71c9e2fSAlexander Graf 
598b71c9e2fSAlexander Graf 	gtlbe = get_entry(vcpu_e500, tlbsel, esel);
599b71c9e2fSAlexander Graf 
600b71c9e2fSAlexander Graf 	switch (tlbsel) {
601b71c9e2fSAlexander Graf 	case 0:
602b71c9e2fSAlexander Graf 		priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
603b71c9e2fSAlexander Graf 
6044d2be6f7SScott Wood 		/* Triggers after clear_tlb_privs or on initial mapping */
605b71c9e2fSAlexander Graf 		if (!(priv->ref.flags & E500_TLB_VALID)) {
606b71c9e2fSAlexander Graf 			kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
607b71c9e2fSAlexander Graf 		} else {
608b71c9e2fSAlexander Graf 			kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
609b71c9e2fSAlexander Graf 						&priv->ref, eaddr, &stlbe);
610b71c9e2fSAlexander Graf 			write_stlbe(vcpu_e500, gtlbe, &stlbe, 0, 0);
611b71c9e2fSAlexander Graf 		}
612b71c9e2fSAlexander Graf 		break;
613b71c9e2fSAlexander Graf 
614b71c9e2fSAlexander Graf 	case 1: {
615b71c9e2fSAlexander Graf 		gfn_t gfn = gpaddr >> PAGE_SHIFT;
616b71c9e2fSAlexander Graf 		kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe, &stlbe,
617b71c9e2fSAlexander Graf 				     esel);
618b71c9e2fSAlexander Graf 		break;
619b71c9e2fSAlexander Graf 	}
620b71c9e2fSAlexander Graf 
621b71c9e2fSAlexander Graf 	default:
622b71c9e2fSAlexander Graf 		BUG();
623b71c9e2fSAlexander Graf 		break;
624b71c9e2fSAlexander Graf 	}
625b71c9e2fSAlexander Graf }
626b71c9e2fSAlexander Graf 
627f5250471SMihai Caraman #ifdef CONFIG_KVM_BOOKE_HV
628f5250471SMihai Caraman int kvmppc_load_last_inst(struct kvm_vcpu *vcpu, enum instruction_type type,
629f5250471SMihai Caraman 			  u32 *instr)
630f5250471SMihai Caraman {
631f5250471SMihai Caraman 	gva_t geaddr;
632f5250471SMihai Caraman 	hpa_t addr;
633f5250471SMihai Caraman 	hfn_t pfn;
634f5250471SMihai Caraman 	hva_t eaddr;
635f5250471SMihai Caraman 	u32 mas1, mas2, mas3;
636f5250471SMihai Caraman 	u64 mas7_mas3;
637f5250471SMihai Caraman 	struct page *page;
638f5250471SMihai Caraman 	unsigned int addr_space, psize_shift;
639f5250471SMihai Caraman 	bool pr;
640f5250471SMihai Caraman 	unsigned long flags;
641f5250471SMihai Caraman 
642f5250471SMihai Caraman 	/* Search TLB for guest pc to get the real address */
643f5250471SMihai Caraman 	geaddr = kvmppc_get_pc(vcpu);
644f5250471SMihai Caraman 
645f5250471SMihai Caraman 	addr_space = (vcpu->arch.shared->msr & MSR_IS) >> MSR_IR_LG;
646f5250471SMihai Caraman 
647f5250471SMihai Caraman 	local_irq_save(flags);
648f5250471SMihai Caraman 	mtspr(SPRN_MAS6, (vcpu->arch.pid << MAS6_SPID_SHIFT) | addr_space);
649188e267cSMihai Caraman 	mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(vcpu));
650f5250471SMihai Caraman 	asm volatile("tlbsx 0, %[geaddr]\n" : :
651f5250471SMihai Caraman 		     [geaddr] "r" (geaddr));
652f5250471SMihai Caraman 	mtspr(SPRN_MAS5, 0);
653f5250471SMihai Caraman 	mtspr(SPRN_MAS8, 0);
654f5250471SMihai Caraman 	mas1 = mfspr(SPRN_MAS1);
655f5250471SMihai Caraman 	mas2 = mfspr(SPRN_MAS2);
656f5250471SMihai Caraman 	mas3 = mfspr(SPRN_MAS3);
657f5250471SMihai Caraman #ifdef CONFIG_64BIT
658f5250471SMihai Caraman 	mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
659f5250471SMihai Caraman #else
660f5250471SMihai Caraman 	mas7_mas3 = ((u64)mfspr(SPRN_MAS7) << 32) | mas3;
661f5250471SMihai Caraman #endif
662f5250471SMihai Caraman 	local_irq_restore(flags);
663f5250471SMihai Caraman 
664f5250471SMihai Caraman 	/*
665f5250471SMihai Caraman 	 * If the TLB entry for guest pc was evicted, return to the guest.
666f5250471SMihai Caraman 	 * There are high chances to find a valid TLB entry next time.
667f5250471SMihai Caraman 	 */
668f5250471SMihai Caraman 	if (!(mas1 & MAS1_VALID))
669f5250471SMihai Caraman 		return EMULATE_AGAIN;
670f5250471SMihai Caraman 
671f5250471SMihai Caraman 	/*
672f5250471SMihai Caraman 	 * Another thread may rewrite the TLB entry in parallel, don't
673f5250471SMihai Caraman 	 * execute from the address if the execute permission is not set
674f5250471SMihai Caraman 	 */
675f5250471SMihai Caraman 	pr = vcpu->arch.shared->msr & MSR_PR;
676f5250471SMihai Caraman 	if (unlikely((pr && !(mas3 & MAS3_UX)) ||
677f5250471SMihai Caraman 		     (!pr && !(mas3 & MAS3_SX)))) {
678f5250471SMihai Caraman 		pr_err_ratelimited(
6796774def6SMasanari Iida 			"%s: Instruction emulation from guest address %08lx without execute permission\n",
680f5250471SMihai Caraman 			__func__, geaddr);
681f5250471SMihai Caraman 		return EMULATE_AGAIN;
682f5250471SMihai Caraman 	}
683f5250471SMihai Caraman 
684f5250471SMihai Caraman 	/*
685f5250471SMihai Caraman 	 * The real address will be mapped by a cacheable, memory coherent,
686f5250471SMihai Caraman 	 * write-back page. Check for mismatches when LRAT is used.
687f5250471SMihai Caraman 	 */
688f5250471SMihai Caraman 	if (has_feature(vcpu, VCPU_FTR_MMU_V2) &&
689f5250471SMihai Caraman 	    unlikely((mas2 & MAS2_I) || (mas2 & MAS2_W) || !(mas2 & MAS2_M))) {
690f5250471SMihai Caraman 		pr_err_ratelimited(
6916774def6SMasanari Iida 			"%s: Instruction emulation from guest address %08lx mismatches storage attributes\n",
692f5250471SMihai Caraman 			__func__, geaddr);
693f5250471SMihai Caraman 		return EMULATE_AGAIN;
694f5250471SMihai Caraman 	}
695f5250471SMihai Caraman 
696f5250471SMihai Caraman 	/* Get pfn */
697f5250471SMihai Caraman 	psize_shift = MAS1_GET_TSIZE(mas1) + 10;
698f5250471SMihai Caraman 	addr = (mas7_mas3 & (~0ULL << psize_shift)) |
699f5250471SMihai Caraman 	       (geaddr & ((1ULL << psize_shift) - 1ULL));
700f5250471SMihai Caraman 	pfn = addr >> PAGE_SHIFT;
701f5250471SMihai Caraman 
702f5250471SMihai Caraman 	/* Guard against emulation from devices area */
703f5250471SMihai Caraman 	if (unlikely(!page_is_ram(pfn))) {
7046774def6SMasanari Iida 		pr_err_ratelimited("%s: Instruction emulation from non-RAM host address %08llx is not supported\n",
705f5250471SMihai Caraman 			 __func__, addr);
706f5250471SMihai Caraman 		return EMULATE_AGAIN;
707f5250471SMihai Caraman 	}
708f5250471SMihai Caraman 
709f5250471SMihai Caraman 	/* Map a page and get guest's instruction */
710f5250471SMihai Caraman 	page = pfn_to_page(pfn);
711f5250471SMihai Caraman 	eaddr = (unsigned long)kmap_atomic(page);
712f5250471SMihai Caraman 	*instr = *(u32 *)(eaddr | (unsigned long)(addr & ~PAGE_MASK));
713f5250471SMihai Caraman 	kunmap_atomic((u32 *)eaddr);
714f5250471SMihai Caraman 
715f5250471SMihai Caraman 	return EMULATE_DONE;
716f5250471SMihai Caraman }
717f5250471SMihai Caraman #else
71851f04726SMihai Caraman int kvmppc_load_last_inst(struct kvm_vcpu *vcpu, enum instruction_type type,
71951f04726SMihai Caraman 			  u32 *instr)
72051f04726SMihai Caraman {
72151f04726SMihai Caraman 	return EMULATE_AGAIN;
72251f04726SMihai Caraman }
723f5250471SMihai Caraman #endif
72451f04726SMihai Caraman 
725b71c9e2fSAlexander Graf /************* MMU Notifiers *************/
726b71c9e2fSAlexander Graf 
727b71c9e2fSAlexander Graf int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
728b71c9e2fSAlexander Graf {
729b71c9e2fSAlexander Graf 	trace_kvm_unmap_hva(hva);
730b71c9e2fSAlexander Graf 
731b71c9e2fSAlexander Graf 	/*
732b71c9e2fSAlexander Graf 	 * Flush all shadow tlb entries everywhere. This is slow, but
733b71c9e2fSAlexander Graf 	 * we are 100% sure that we catch the to be unmapped page
734b71c9e2fSAlexander Graf 	 */
735b71c9e2fSAlexander Graf 	kvm_flush_remote_tlbs(kvm);
736b71c9e2fSAlexander Graf 
737b71c9e2fSAlexander Graf 	return 0;
738b71c9e2fSAlexander Graf }
739b71c9e2fSAlexander Graf 
740b71c9e2fSAlexander Graf int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
741b71c9e2fSAlexander Graf {
742b71c9e2fSAlexander Graf 	/* kvm_unmap_hva flushes everything anyways */
743b71c9e2fSAlexander Graf 	kvm_unmap_hva(kvm, start);
744b71c9e2fSAlexander Graf 
745b71c9e2fSAlexander Graf 	return 0;
746b71c9e2fSAlexander Graf }
747b71c9e2fSAlexander Graf 
74857128468SAndres Lagar-Cavilla int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
749b71c9e2fSAlexander Graf {
750b71c9e2fSAlexander Graf 	/* XXX could be more clever ;) */
751b71c9e2fSAlexander Graf 	return 0;
752b71c9e2fSAlexander Graf }
753b71c9e2fSAlexander Graf 
754b71c9e2fSAlexander Graf int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
755b71c9e2fSAlexander Graf {
756b71c9e2fSAlexander Graf 	/* XXX could be more clever ;) */
757b71c9e2fSAlexander Graf 	return 0;
758b71c9e2fSAlexander Graf }
759b71c9e2fSAlexander Graf 
760b71c9e2fSAlexander Graf void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
761b71c9e2fSAlexander Graf {
762b71c9e2fSAlexander Graf 	/* The page will get remapped properly on its next fault */
763b71c9e2fSAlexander Graf 	kvm_unmap_hva(kvm, hva);
764b71c9e2fSAlexander Graf }
765b71c9e2fSAlexander Graf 
766b71c9e2fSAlexander Graf /*****************************************/
767b71c9e2fSAlexander Graf 
768b71c9e2fSAlexander Graf int e500_mmu_host_init(struct kvmppc_vcpu_e500 *vcpu_e500)
769b71c9e2fSAlexander Graf {
770b71c9e2fSAlexander Graf 	host_tlb_params[0].entries = mfspr(SPRN_TLB0CFG) & TLBnCFG_N_ENTRY;
771b71c9e2fSAlexander Graf 	host_tlb_params[1].entries = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
772b71c9e2fSAlexander Graf 
773b71c9e2fSAlexander Graf 	/*
774b71c9e2fSAlexander Graf 	 * This should never happen on real e500 hardware, but is
775b71c9e2fSAlexander Graf 	 * architecturally possible -- e.g. in some weird nested
776b71c9e2fSAlexander Graf 	 * virtualization case.
777b71c9e2fSAlexander Graf 	 */
778b71c9e2fSAlexander Graf 	if (host_tlb_params[0].entries == 0 ||
779b71c9e2fSAlexander Graf 	    host_tlb_params[1].entries == 0) {
780b71c9e2fSAlexander Graf 		pr_err("%s: need to know host tlb size\n", __func__);
781b71c9e2fSAlexander Graf 		return -ENODEV;
782b71c9e2fSAlexander Graf 	}
783b71c9e2fSAlexander Graf 
784b71c9e2fSAlexander Graf 	host_tlb_params[0].ways = (mfspr(SPRN_TLB0CFG) & TLBnCFG_ASSOC) >>
785b71c9e2fSAlexander Graf 				  TLBnCFG_ASSOC_SHIFT;
786b71c9e2fSAlexander Graf 	host_tlb_params[1].ways = host_tlb_params[1].entries;
787b71c9e2fSAlexander Graf 
788b71c9e2fSAlexander Graf 	if (!is_power_of_2(host_tlb_params[0].entries) ||
789b71c9e2fSAlexander Graf 	    !is_power_of_2(host_tlb_params[0].ways) ||
790b71c9e2fSAlexander Graf 	    host_tlb_params[0].entries < host_tlb_params[0].ways ||
791b71c9e2fSAlexander Graf 	    host_tlb_params[0].ways == 0) {
792b71c9e2fSAlexander Graf 		pr_err("%s: bad tlb0 host config: %u entries %u ways\n",
793b71c9e2fSAlexander Graf 		       __func__, host_tlb_params[0].entries,
794b71c9e2fSAlexander Graf 		       host_tlb_params[0].ways);
795b71c9e2fSAlexander Graf 		return -ENODEV;
796b71c9e2fSAlexander Graf 	}
797b71c9e2fSAlexander Graf 
798b71c9e2fSAlexander Graf 	host_tlb_params[0].sets =
799b71c9e2fSAlexander Graf 		host_tlb_params[0].entries / host_tlb_params[0].ways;
800b71c9e2fSAlexander Graf 	host_tlb_params[1].sets = 1;
80137655490SMarkus Elfring 	vcpu_e500->h2g_tlb1_rmap = kcalloc(host_tlb_params[1].entries,
80237655490SMarkus Elfring 					   sizeof(*vcpu_e500->h2g_tlb1_rmap),
803b71c9e2fSAlexander Graf 					   GFP_KERNEL);
804b71c9e2fSAlexander Graf 	if (!vcpu_e500->h2g_tlb1_rmap)
8054d2be6f7SScott Wood 		return -EINVAL;
806b71c9e2fSAlexander Graf 
807b71c9e2fSAlexander Graf 	return 0;
808b71c9e2fSAlexander Graf }
809b71c9e2fSAlexander Graf 
810b71c9e2fSAlexander Graf void e500_mmu_host_uninit(struct kvmppc_vcpu_e500 *vcpu_e500)
811b71c9e2fSAlexander Graf {
812b71c9e2fSAlexander Graf 	kfree(vcpu_e500->h2g_tlb1_rmap);
813b71c9e2fSAlexander Graf }
814