xref: /linux/arch/powerpc/kvm/book3s_32_mmu_host.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1d94d71cbSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d32154f1SAlexander Graf /*
3d32154f1SAlexander Graf  * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
4d32154f1SAlexander Graf  *
5d32154f1SAlexander Graf  * Authors:
6d32154f1SAlexander Graf  *     Alexander Graf <agraf@suse.de>
7d32154f1SAlexander Graf  */
8d32154f1SAlexander Graf 
9d32154f1SAlexander Graf #include <linux/kvm_host.h>
10d32154f1SAlexander Graf 
11d32154f1SAlexander Graf #include <asm/kvm_ppc.h>
12d32154f1SAlexander Graf #include <asm/kvm_book3s.h>
13f64e8084SAneesh Kumar K.V #include <asm/book3s/32/mmu-hash.h>
14d32154f1SAlexander Graf #include <asm/machdep.h>
15d32154f1SAlexander Graf #include <asm/mmu_context.h>
16d32154f1SAlexander Graf #include <asm/hw_irq.h>
175358a963SThomas Huth #include "book3s.h"
18d32154f1SAlexander Graf 
19d32154f1SAlexander Graf /* #define DEBUG_MMU */
20d32154f1SAlexander Graf /* #define DEBUG_SR */
21d32154f1SAlexander Graf 
22d32154f1SAlexander Graf #ifdef DEBUG_MMU
23d32154f1SAlexander Graf #define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
24d32154f1SAlexander Graf #else
25d32154f1SAlexander Graf #define dprintk_mmu(a, ...) do { } while(0)
26d32154f1SAlexander Graf #endif
27d32154f1SAlexander Graf 
28d32154f1SAlexander Graf #ifdef DEBUG_SR
29d32154f1SAlexander Graf #define dprintk_sr(a, ...) printk(KERN_INFO a, __VA_ARGS__)
30d32154f1SAlexander Graf #else
31d32154f1SAlexander Graf #define dprintk_sr(a, ...) do { } while(0)
32d32154f1SAlexander Graf #endif
33d32154f1SAlexander Graf 
34d32154f1SAlexander Graf #if PAGE_SHIFT != 12
35d32154f1SAlexander Graf #error Unknown page size
36d32154f1SAlexander Graf #endif
37d32154f1SAlexander Graf 
38d32154f1SAlexander Graf #ifdef CONFIG_SMP
39d32154f1SAlexander Graf #error XXX need to grab mmu_hash_lock
40d32154f1SAlexander Graf #endif
41d32154f1SAlexander Graf 
42d32154f1SAlexander Graf #ifdef CONFIG_PTE_64BIT
43d32154f1SAlexander Graf #error Only 32 bit pages are supported for now
44d32154f1SAlexander Graf #endif
45d32154f1SAlexander Graf 
46251585b5SAlexander Graf static ulong htab;
47251585b5SAlexander Graf static u32 htabmask;
48251585b5SAlexander Graf 
kvmppc_mmu_invalidate_pte(struct kvm_vcpu * vcpu,struct hpte_cache * pte)49fef093beSAlexander Graf void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
50d32154f1SAlexander Graf {
51d32154f1SAlexander Graf 	volatile u32 *pteg;
52d32154f1SAlexander Graf 
53fef093beSAlexander Graf 	/* Remove from host HTAB */
54d32154f1SAlexander Graf 	pteg = (u32*)pte->slot;
55d32154f1SAlexander Graf 	pteg[0] = 0;
56fef093beSAlexander Graf 
57fef093beSAlexander Graf 	/* And make sure it's gone from the TLB too */
58d32154f1SAlexander Graf 	asm volatile ("sync");
59d32154f1SAlexander Graf 	asm volatile ("tlbie %0" : : "r" (pte->pte.eaddr) : "memory");
60d32154f1SAlexander Graf 	asm volatile ("sync");
61d32154f1SAlexander Graf 	asm volatile ("tlbsync");
62d32154f1SAlexander Graf }
63d32154f1SAlexander Graf 
64d32154f1SAlexander Graf /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
65d32154f1SAlexander Graf  * a hash, so we don't waste cycles on looping */
kvmppc_sid_hash(struct kvm_vcpu * vcpu,u64 gvsid)66d32154f1SAlexander Graf static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
67d32154f1SAlexander Graf {
68b9877ce2SAlexander Graf 	return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
69b9877ce2SAlexander Graf 		     ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
70b9877ce2SAlexander Graf 		     ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
71b9877ce2SAlexander Graf 		     ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
72b9877ce2SAlexander Graf 		     ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
73b9877ce2SAlexander Graf 		     ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
74b9877ce2SAlexander Graf 		     ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
75b9877ce2SAlexander Graf 		     ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
76d32154f1SAlexander Graf }
77d32154f1SAlexander Graf 
78d32154f1SAlexander Graf 
find_sid_vsid(struct kvm_vcpu * vcpu,u64 gvsid)79d32154f1SAlexander Graf static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
80d32154f1SAlexander Graf {
81d32154f1SAlexander Graf 	struct kvmppc_sid_map *map;
82d32154f1SAlexander Graf 	u16 sid_map_mask;
83d32154f1SAlexander Graf 
845deb8e7aSAlexander Graf 	if (kvmppc_get_msr(vcpu) & MSR_PR)
85d32154f1SAlexander Graf 		gvsid |= VSID_PR;
86d32154f1SAlexander Graf 
87d32154f1SAlexander Graf 	sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
88d32154f1SAlexander Graf 	map = &to_book3s(vcpu)->sid_map[sid_map_mask];
89d32154f1SAlexander Graf 	if (map->guest_vsid == gvsid) {
90d32154f1SAlexander Graf 		dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
91d32154f1SAlexander Graf 			    gvsid, map->host_vsid);
92d32154f1SAlexander Graf 		return map;
93d32154f1SAlexander Graf 	}
94d32154f1SAlexander Graf 
95d32154f1SAlexander Graf 	map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
96d32154f1SAlexander Graf 	if (map->guest_vsid == gvsid) {
97d32154f1SAlexander Graf 		dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",
98d32154f1SAlexander Graf 			    gvsid, map->host_vsid);
99d32154f1SAlexander Graf 		return map;
100d32154f1SAlexander Graf 	}
101d32154f1SAlexander Graf 
102d32154f1SAlexander Graf 	dprintk_sr("SR: Searching 0x%llx -> not found\n", gvsid);
103d32154f1SAlexander Graf 	return NULL;
104d32154f1SAlexander Graf }
105d32154f1SAlexander Graf 
kvmppc_mmu_get_pteg(struct kvm_vcpu * vcpu,u32 vsid,u32 eaddr,bool primary)106d32154f1SAlexander Graf static u32 *kvmppc_mmu_get_pteg(struct kvm_vcpu *vcpu, u32 vsid, u32 eaddr,
107d32154f1SAlexander Graf 				bool primary)
108d32154f1SAlexander Graf {
109251585b5SAlexander Graf 	u32 page, hash;
110251585b5SAlexander Graf 	ulong pteg = htab;
111d32154f1SAlexander Graf 
112d32154f1SAlexander Graf 	page = (eaddr & ~ESID_MASK) >> 12;
113d32154f1SAlexander Graf 
114d32154f1SAlexander Graf 	hash = ((vsid ^ page) << 6);
115d32154f1SAlexander Graf 	if (!primary)
116d32154f1SAlexander Graf 		hash = ~hash;
117d32154f1SAlexander Graf 
118d32154f1SAlexander Graf 	hash &= htabmask;
119d32154f1SAlexander Graf 
120d32154f1SAlexander Graf 	pteg |= hash;
121d32154f1SAlexander Graf 
122251585b5SAlexander Graf 	dprintk_mmu("htab: %lx | hash: %x | htabmask: %x | pteg: %lx\n",
123251585b5SAlexander Graf 		htab, hash, htabmask, pteg);
124d32154f1SAlexander Graf 
125d32154f1SAlexander Graf 	return (u32*)pteg;
126d32154f1SAlexander Graf }
127d32154f1SAlexander Graf 
128d32154f1SAlexander Graf extern char etext[];
129d32154f1SAlexander Graf 
kvmppc_mmu_map_page(struct kvm_vcpu * vcpu,struct kvmppc_pte * orig_pte,bool iswrite)13093b159b4SPaul Mackerras int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte,
13193b159b4SPaul Mackerras 			bool iswrite)
132d32154f1SAlexander Graf {
133ba049e93SDan Williams 	kvm_pfn_t hpaddr;
1345524a27dSAneesh Kumar K.V 	u64 vpn;
135d32154f1SAlexander Graf 	u64 vsid;
136d32154f1SAlexander Graf 	struct kvmppc_sid_map *map;
137d32154f1SAlexander Graf 	volatile u32 *pteg;
138d32154f1SAlexander Graf 	u32 eaddr = orig_pte->eaddr;
139d32154f1SAlexander Graf 	u32 pteg0, pteg1;
140d32154f1SAlexander Graf 	register int rr = 0;
141d32154f1SAlexander Graf 	bool primary = false;
142d32154f1SAlexander Graf 	bool evict = false;
143d32154f1SAlexander Graf 	struct hpte_cache *pte;
144468a12c2SAlexander Graf 	int r = 0;
14593b159b4SPaul Mackerras 	bool writable;
146d32154f1SAlexander Graf 
147d32154f1SAlexander Graf 	/* Get host physical address for gpa */
14889b68c96SAlexander Graf 	hpaddr = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable);
14981c52c56SXiao Guangrong 	if (is_error_noslot_pfn(hpaddr)) {
15089b68c96SAlexander Graf 		printk(KERN_INFO "Couldn't get guest page for gpa %lx!\n",
15189b68c96SAlexander Graf 				 orig_pte->raddr);
152468a12c2SAlexander Graf 		r = -EINVAL;
153468a12c2SAlexander Graf 		goto out;
154d32154f1SAlexander Graf 	}
155d32154f1SAlexander Graf 	hpaddr <<= PAGE_SHIFT;
156d32154f1SAlexander Graf 
157d32154f1SAlexander Graf 	/* and write the mapping ea -> hpa into the pt */
158d32154f1SAlexander Graf 	vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
159d32154f1SAlexander Graf 	map = find_sid_vsid(vcpu, vsid);
160d32154f1SAlexander Graf 	if (!map) {
161d32154f1SAlexander Graf 		kvmppc_mmu_map_segment(vcpu, eaddr);
162d32154f1SAlexander Graf 		map = find_sid_vsid(vcpu, vsid);
163d32154f1SAlexander Graf 	}
164d32154f1SAlexander Graf 	BUG_ON(!map);
165d32154f1SAlexander Graf 
166d32154f1SAlexander Graf 	vsid = map->host_vsid;
167ce236ab5SAneesh Kumar K.V 	vpn = (vsid << (SID_SHIFT - VPN_SHIFT)) |
168ce236ab5SAneesh Kumar K.V 		((eaddr & ~ESID_MASK) >> VPN_SHIFT);
169d32154f1SAlexander Graf next_pteg:
170d32154f1SAlexander Graf 	if (rr == 16) {
171d32154f1SAlexander Graf 		primary = !primary;
172d32154f1SAlexander Graf 		evict = true;
173d32154f1SAlexander Graf 		rr = 0;
174d32154f1SAlexander Graf 	}
175d32154f1SAlexander Graf 
176d32154f1SAlexander Graf 	pteg = kvmppc_mmu_get_pteg(vcpu, vsid, eaddr, primary);
177d32154f1SAlexander Graf 
178d32154f1SAlexander Graf 	/* not evicting yet */
179d32154f1SAlexander Graf 	if (!evict && (pteg[rr] & PTE_V)) {
180d32154f1SAlexander Graf 		rr += 2;
181d32154f1SAlexander Graf 		goto next_pteg;
182d32154f1SAlexander Graf 	}
183d32154f1SAlexander Graf 
184d32154f1SAlexander Graf 	dprintk_mmu("KVM: old PTEG: %p (%d)\n", pteg, rr);
185d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[0], pteg[1]);
186d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[2], pteg[3]);
187d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[4], pteg[5]);
188d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[6], pteg[7]);
189d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[8], pteg[9]);
190d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[10], pteg[11]);
191d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[12], pteg[13]);
192d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[14], pteg[15]);
193d32154f1SAlexander Graf 
194d32154f1SAlexander Graf 	pteg0 = ((eaddr & 0x0fffffff) >> 22) | (vsid << 7) | PTE_V |
195d32154f1SAlexander Graf 		(primary ? 0 : PTE_SEC);
196d32154f1SAlexander Graf 	pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;
197d32154f1SAlexander Graf 
19893b159b4SPaul Mackerras 	if (orig_pte->may_write && writable) {
199d32154f1SAlexander Graf 		pteg1 |= PP_RWRW;
200d32154f1SAlexander Graf 		mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
201d32154f1SAlexander Graf 	} else {
202d32154f1SAlexander Graf 		pteg1 |= PP_RWRX;
203d32154f1SAlexander Graf 	}
204d32154f1SAlexander Graf 
205249ba1eeSAlexander Graf 	if (orig_pte->may_execute)
206249ba1eeSAlexander Graf 		kvmppc_mmu_flush_icache(hpaddr >> PAGE_SHIFT);
207249ba1eeSAlexander Graf 
208d32154f1SAlexander Graf 	local_irq_disable();
209d32154f1SAlexander Graf 
210d32154f1SAlexander Graf 	if (pteg[rr]) {
211d32154f1SAlexander Graf 		pteg[rr] = 0;
212d32154f1SAlexander Graf 		asm volatile ("sync");
213d32154f1SAlexander Graf 	}
214d32154f1SAlexander Graf 	pteg[rr + 1] = pteg1;
215d32154f1SAlexander Graf 	pteg[rr] = pteg0;
216d32154f1SAlexander Graf 	asm volatile ("sync");
217d32154f1SAlexander Graf 
218d32154f1SAlexander Graf 	local_irq_enable();
219d32154f1SAlexander Graf 
220d32154f1SAlexander Graf 	dprintk_mmu("KVM: new PTEG: %p\n", pteg);
221d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[0], pteg[1]);
222d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[2], pteg[3]);
223d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[4], pteg[5]);
224d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[6], pteg[7]);
225d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[8], pteg[9]);
226d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[10], pteg[11]);
227d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[12], pteg[13]);
228d32154f1SAlexander Graf 	dprintk_mmu("KVM:   %08x - %08x\n", pteg[14], pteg[15]);
229d32154f1SAlexander Graf 
230d32154f1SAlexander Graf 
231d32154f1SAlexander Graf 	/* Now tell our Shadow PTE code about the new page */
232d32154f1SAlexander Graf 
233fef093beSAlexander Graf 	pte = kvmppc_mmu_hpte_cache_next(vcpu);
23447d45d9fSZhouyi Zhou 	if (!pte) {
23547d45d9fSZhouyi Zhou 		kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT);
23647d45d9fSZhouyi Zhou 		r = -EAGAIN;
23747d45d9fSZhouyi Zhou 		goto out;
23847d45d9fSZhouyi Zhou 	}
239d32154f1SAlexander Graf 
240d32154f1SAlexander Graf 	dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
241d32154f1SAlexander Graf 		    orig_pte->may_write ? 'w' : '-',
242d32154f1SAlexander Graf 		    orig_pte->may_execute ? 'x' : '-',
2435524a27dSAneesh Kumar K.V 		    orig_pte->eaddr, (ulong)pteg, vpn,
244d32154f1SAlexander Graf 		    orig_pte->vpage, hpaddr);
245d32154f1SAlexander Graf 
246d32154f1SAlexander Graf 	pte->slot = (ulong)&pteg[rr];
2475524a27dSAneesh Kumar K.V 	pte->host_vpn = vpn;
248d32154f1SAlexander Graf 	pte->pte = *orig_pte;
249d32154f1SAlexander Graf 	pte->pfn = hpaddr >> PAGE_SHIFT;
250d32154f1SAlexander Graf 
251fef093beSAlexander Graf 	kvmppc_mmu_hpte_cache_map(vcpu, pte);
252fef093beSAlexander Graf 
2539b0cb3c8SAlexander Graf 	kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT);
254468a12c2SAlexander Graf out:
255468a12c2SAlexander Graf 	return r;
256d32154f1SAlexander Graf }
257d32154f1SAlexander Graf 
kvmppc_mmu_unmap_page(struct kvm_vcpu * vcpu,struct kvmppc_pte * pte)25893b159b4SPaul Mackerras void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
25993b159b4SPaul Mackerras {
26093b159b4SPaul Mackerras 	kvmppc_mmu_pte_vflush(vcpu, pte->vpage, 0xfffffffffULL);
26193b159b4SPaul Mackerras }
26293b159b4SPaul Mackerras 
create_sid_map(struct kvm_vcpu * vcpu,u64 gvsid)263d32154f1SAlexander Graf static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
264d32154f1SAlexander Graf {
265d32154f1SAlexander Graf 	struct kvmppc_sid_map *map;
266d32154f1SAlexander Graf 	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
267d32154f1SAlexander Graf 	u16 sid_map_mask;
268d32154f1SAlexander Graf 	static int backwards_map = 0;
269d32154f1SAlexander Graf 
2705deb8e7aSAlexander Graf 	if (kvmppc_get_msr(vcpu) & MSR_PR)
271d32154f1SAlexander Graf 		gvsid |= VSID_PR;
272d32154f1SAlexander Graf 
273d32154f1SAlexander Graf 	/* We might get collisions that trap in preceding order, so let's
274d32154f1SAlexander Graf 	   map them differently */
275d32154f1SAlexander Graf 
276d32154f1SAlexander Graf 	sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
277d32154f1SAlexander Graf 	if (backwards_map)
278d32154f1SAlexander Graf 		sid_map_mask = SID_MAP_MASK - sid_map_mask;
279d32154f1SAlexander Graf 
280d32154f1SAlexander Graf 	map = &to_book3s(vcpu)->sid_map[sid_map_mask];
281d32154f1SAlexander Graf 
282d32154f1SAlexander Graf 	/* Make sure we're taking the other map next time */
283d32154f1SAlexander Graf 	backwards_map = !backwards_map;
284d32154f1SAlexander Graf 
285d32154f1SAlexander Graf 	/* Uh-oh ... out of mappings. Let's flush! */
2868b6db3bcSAlexander Graf 	if (vcpu_book3s->vsid_next >= VSID_POOL_SIZE) {
2878b6db3bcSAlexander Graf 		vcpu_book3s->vsid_next = 0;
288d32154f1SAlexander Graf 		memset(vcpu_book3s->sid_map, 0,
289d32154f1SAlexander Graf 		       sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
290d32154f1SAlexander Graf 		kvmppc_mmu_pte_flush(vcpu, 0, 0);
291d32154f1SAlexander Graf 		kvmppc_mmu_flush_segments(vcpu);
292d32154f1SAlexander Graf 	}
2938b6db3bcSAlexander Graf 	map->host_vsid = vcpu_book3s->vsid_pool[vcpu_book3s->vsid_next];
2948b6db3bcSAlexander Graf 	vcpu_book3s->vsid_next++;
295d32154f1SAlexander Graf 
296d32154f1SAlexander Graf 	map->guest_vsid = gvsid;
297d32154f1SAlexander Graf 	map->valid = true;
298d32154f1SAlexander Graf 
299d32154f1SAlexander Graf 	return map;
300d32154f1SAlexander Graf }
301d32154f1SAlexander Graf 
kvmppc_mmu_map_segment(struct kvm_vcpu * vcpu,ulong eaddr)302d32154f1SAlexander Graf int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
303d32154f1SAlexander Graf {
304d32154f1SAlexander Graf 	u32 esid = eaddr >> SID_SHIFT;
305d32154f1SAlexander Graf 	u64 gvsid;
306d32154f1SAlexander Graf 	u32 sr;
307d32154f1SAlexander Graf 	struct kvmppc_sid_map *map;
308468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
309468a12c2SAlexander Graf 	int r = 0;
310d32154f1SAlexander Graf 
311d32154f1SAlexander Graf 	if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
312d32154f1SAlexander Graf 		/* Invalidate an entry */
313d32154f1SAlexander Graf 		svcpu->sr[esid] = SR_INVALID;
314468a12c2SAlexander Graf 		r = -ENOENT;
315468a12c2SAlexander Graf 		goto out;
316d32154f1SAlexander Graf 	}
317d32154f1SAlexander Graf 
318d32154f1SAlexander Graf 	map = find_sid_vsid(vcpu, gvsid);
319d32154f1SAlexander Graf 	if (!map)
320d32154f1SAlexander Graf 		map = create_sid_map(vcpu, gvsid);
321d32154f1SAlexander Graf 
322d32154f1SAlexander Graf 	map->guest_esid = esid;
323d32154f1SAlexander Graf 	sr = map->host_vsid | SR_KP;
324d32154f1SAlexander Graf 	svcpu->sr[esid] = sr;
325d32154f1SAlexander Graf 
326d32154f1SAlexander Graf 	dprintk_sr("MMU: mtsr %d, 0x%x\n", esid, sr);
327d32154f1SAlexander Graf 
328468a12c2SAlexander Graf out:
329468a12c2SAlexander Graf 	svcpu_put(svcpu);
330468a12c2SAlexander Graf 	return r;
331d32154f1SAlexander Graf }
332d32154f1SAlexander Graf 
kvmppc_mmu_flush_segments(struct kvm_vcpu * vcpu)333d32154f1SAlexander Graf void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
334d32154f1SAlexander Graf {
335d32154f1SAlexander Graf 	int i;
336468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
337d32154f1SAlexander Graf 
338d32154f1SAlexander Graf 	dprintk_sr("MMU: flushing all segments (%d)\n", ARRAY_SIZE(svcpu->sr));
339d32154f1SAlexander Graf 	for (i = 0; i < ARRAY_SIZE(svcpu->sr); i++)
340d32154f1SAlexander Graf 		svcpu->sr[i] = SR_INVALID;
341468a12c2SAlexander Graf 
342468a12c2SAlexander Graf 	svcpu_put(svcpu);
343d32154f1SAlexander Graf }
344d32154f1SAlexander Graf 
kvmppc_mmu_destroy_pr(struct kvm_vcpu * vcpu)3453a167beaSAneesh Kumar K.V void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu)
346d32154f1SAlexander Graf {
3478b6db3bcSAlexander Graf 	int i;
3488b6db3bcSAlexander Graf 
349fef093beSAlexander Graf 	kvmppc_mmu_hpte_destroy(vcpu);
350d32154f1SAlexander Graf 	preempt_disable();
3518b6db3bcSAlexander Graf 	for (i = 0; i < SID_CONTEXTS; i++)
3528b6db3bcSAlexander Graf 		__destroy_context(to_book3s(vcpu)->context_id[i]);
353d32154f1SAlexander Graf 	preempt_enable();
354d32154f1SAlexander Graf }
355d32154f1SAlexander Graf 
kvmppc_mmu_init_pr(struct kvm_vcpu * vcpu)356*3f1268ddSGreg Kurz int kvmppc_mmu_init_pr(struct kvm_vcpu *vcpu)
357d32154f1SAlexander Graf {
358d32154f1SAlexander Graf 	struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
359d32154f1SAlexander Graf 	int err;
360251585b5SAlexander Graf 	ulong sdr1;
3618b6db3bcSAlexander Graf 	int i;
3628b6db3bcSAlexander Graf 	int j;
363d32154f1SAlexander Graf 
3648b6db3bcSAlexander Graf 	for (i = 0; i < SID_CONTEXTS; i++) {
365d32154f1SAlexander Graf 		err = __init_new_context();
366d32154f1SAlexander Graf 		if (err < 0)
3678b6db3bcSAlexander Graf 			goto init_fail;
3688b6db3bcSAlexander Graf 		vcpu3s->context_id[i] = err;
369d32154f1SAlexander Graf 
3708b6db3bcSAlexander Graf 		/* Remember context id for this combination */
3718b6db3bcSAlexander Graf 		for (j = 0; j < 16; j++)
3728b6db3bcSAlexander Graf 			vcpu3s->vsid_pool[(i * 16) + j] = CTX_TO_VSID(err, j);
3738b6db3bcSAlexander Graf 	}
374d32154f1SAlexander Graf 
3758b6db3bcSAlexander Graf 	vcpu3s->vsid_next = 0;
376d32154f1SAlexander Graf 
377251585b5SAlexander Graf 	/* Remember where the HTAB is */
378251585b5SAlexander Graf 	asm ( "mfsdr1 %0" : "=r"(sdr1) );
379251585b5SAlexander Graf 	htabmask = ((sdr1 & 0x1FF) << 16) | 0xFFC0;
380251585b5SAlexander Graf 	htab = (ulong)__va(sdr1 & 0xffff0000);
381251585b5SAlexander Graf 
382fef093beSAlexander Graf 	kvmppc_mmu_hpte_init(vcpu);
383fef093beSAlexander Graf 
384d32154f1SAlexander Graf 	return 0;
3858b6db3bcSAlexander Graf 
3868b6db3bcSAlexander Graf init_fail:
3878b6db3bcSAlexander Graf 	for (j = 0; j < i; j++) {
3888b6db3bcSAlexander Graf 		if (!vcpu3s->context_id[j])
3898b6db3bcSAlexander Graf 			continue;
3908b6db3bcSAlexander Graf 
3918b6db3bcSAlexander Graf 		__destroy_context(to_book3s(vcpu)->context_id[j]);
3928b6db3bcSAlexander Graf 	}
3938b6db3bcSAlexander Graf 
3948b6db3bcSAlexander Graf 	return -1;
395d32154f1SAlexander Graf }
396