14e342025SAlexander Graf /*
24e342025SAlexander Graf  * This program is free software; you can redistribute it and/or modify
34e342025SAlexander Graf  * it under the terms of the GNU General Public License, version 2, as
44e342025SAlexander Graf  * published by the Free Software Foundation.
54e342025SAlexander Graf  *
64e342025SAlexander Graf  * This program is distributed in the hope that it will be useful,
74e342025SAlexander Graf  * but WITHOUT ANY WARRANTY; without even the implied warranty of
84e342025SAlexander Graf  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
94e342025SAlexander Graf  * GNU General Public License for more details.
104e342025SAlexander Graf  *
114e342025SAlexander Graf  * You should have received a copy of the GNU General Public License
124e342025SAlexander Graf  * along with this program; if not, write to the Free Software
134e342025SAlexander Graf  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
144e342025SAlexander Graf  *
154e342025SAlexander Graf  * Copyright SUSE Linux Products GmbH 2009
164e342025SAlexander Graf  *
174e342025SAlexander Graf  * Authors: Alexander Graf <agraf@suse.de>
184e342025SAlexander Graf  */
194e342025SAlexander Graf 
204e342025SAlexander Graf #ifndef __ASM_KVM_BOOK3S_H__
214e342025SAlexander Graf #define __ASM_KVM_BOOK3S_H__
224e342025SAlexander Graf 
234e342025SAlexander Graf #include <linux/types.h>
244e342025SAlexander Graf #include <linux/kvm_host.h>
252191d657SAlexander Graf #include <asm/kvm_book3s_asm.h>
264e342025SAlexander Graf 
274e342025SAlexander Graf struct kvmppc_bat {
28e15a1137SAlexander Graf 	u64 raw;
294e342025SAlexander Graf 	u32 bepi;
304e342025SAlexander Graf 	u32 bepi_mask;
314e342025SAlexander Graf 	u32 brpn;
324e342025SAlexander Graf 	u8 wimg;
334e342025SAlexander Graf 	u8 pp;
343ed9c6d2SAlexander Graf 	bool vs		: 1;
353ed9c6d2SAlexander Graf 	bool vp		: 1;
364e342025SAlexander Graf };
374e342025SAlexander Graf 
384e342025SAlexander Graf struct kvmppc_sid_map {
394e342025SAlexander Graf 	u64 guest_vsid;
404e342025SAlexander Graf 	u64 guest_esid;
414e342025SAlexander Graf 	u64 host_vsid;
423ed9c6d2SAlexander Graf 	bool valid	: 1;
434e342025SAlexander Graf };
444e342025SAlexander Graf 
454e342025SAlexander Graf #define SID_MAP_BITS    9
464e342025SAlexander Graf #define SID_MAP_NUM     (1 << SID_MAP_BITS)
474e342025SAlexander Graf #define SID_MAP_MASK    (SID_MAP_NUM - 1)
484e342025SAlexander Graf 
498b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_64
508b6db3bcSAlexander Graf #define SID_CONTEXTS	1
518b6db3bcSAlexander Graf #else
528b6db3bcSAlexander Graf #define SID_CONTEXTS	128
538b6db3bcSAlexander Graf #define VSID_POOL_SIZE	(SID_CONTEXTS * 16)
548b6db3bcSAlexander Graf #endif
558b6db3bcSAlexander Graf 
56c4befc58SPaul Mackerras struct hpte_cache {
57c4befc58SPaul Mackerras 	struct hlist_node list_pte;
58c4befc58SPaul Mackerras 	struct hlist_node list_pte_long;
59c4befc58SPaul Mackerras 	struct hlist_node list_vpte;
60c4befc58SPaul Mackerras 	struct hlist_node list_vpte_long;
61c4befc58SPaul Mackerras 	struct rcu_head rcu_head;
62c4befc58SPaul Mackerras 	u64 host_va;
63c4befc58SPaul Mackerras 	u64 pfn;
64c4befc58SPaul Mackerras 	ulong slot;
65c4befc58SPaul Mackerras 	struct kvmppc_pte pte;
66c4befc58SPaul Mackerras };
67c4befc58SPaul Mackerras 
684e342025SAlexander Graf struct kvmppc_vcpu_book3s {
694e342025SAlexander Graf 	struct kvm_vcpu vcpu;
70c7f38f46SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *shadow_vcpu;
714e342025SAlexander Graf 	struct kvmppc_sid_map sid_map[SID_MAP_NUM];
724e342025SAlexander Graf 	struct {
734e342025SAlexander Graf 		u64 esid;
744e342025SAlexander Graf 		u64 vsid;
754e342025SAlexander Graf 	} slb_shadow[64];
764e342025SAlexander Graf 	u8 slb_shadow_max;
774e342025SAlexander Graf 	struct kvmppc_bat ibat[8];
784e342025SAlexander Graf 	struct kvmppc_bat dbat[8];
794e342025SAlexander Graf 	u64 hid[6];
80d6d549b2SAlexander Graf 	u64 gqr[8];
814e342025SAlexander Graf 	u64 sdr1;
824e342025SAlexander Graf 	u64 hior;
834e342025SAlexander Graf 	u64 msr_mask;
844e342025SAlexander Graf 	u64 vsid_next;
858b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_32
868b6db3bcSAlexander Graf 	u32 vsid_pool[VSID_POOL_SIZE];
878b6db3bcSAlexander Graf #else
888b6db3bcSAlexander Graf 	u64 vsid_first;
894e342025SAlexander Graf 	u64 vsid_max;
908b6db3bcSAlexander Graf #endif
918b6db3bcSAlexander Graf 	int context_id[SID_CONTEXTS];
92c4befc58SPaul Mackerras 
93a15bd354SAlexander Graf 	bool hior_sregs;		/* HIOR is set by SREGS, not PVR */
94a15bd354SAlexander Graf 
95c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
96c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
97c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
98c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
99c4befc58SPaul Mackerras 	int hpte_cache_count;
100c4befc58SPaul Mackerras 	spinlock_t mmu_lock;
1014e342025SAlexander Graf };
1024e342025SAlexander Graf 
1034e342025SAlexander Graf #define CONTEXT_HOST		0
1044e342025SAlexander Graf #define CONTEXT_GUEST		1
1054e342025SAlexander Graf #define CONTEXT_GUEST_END	2
1064e342025SAlexander Graf 
107f7bc74e1SAlexander Graf #define VSID_REAL	0x1fffffffffc00000ULL
108f7bc74e1SAlexander Graf #define VSID_BAT	0x1fffffffffb00000ULL
109f7bc74e1SAlexander Graf #define VSID_REAL_DR	0x2000000000000000ULL
110f7bc74e1SAlexander Graf #define VSID_REAL_IR	0x4000000000000000ULL
1115a1b419fSAlexander Graf #define VSID_PR		0x8000000000000000ULL
1124e342025SAlexander Graf 
113af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
1144e342025SAlexander Graf extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
115af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
1164e342025SAlexander Graf extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
117f05ed4d5SPaul Mackerras extern void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr);
1184e342025SAlexander Graf extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
1194e342025SAlexander Graf extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
120de56a948SPaul Mackerras extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
1214e342025SAlexander Graf extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
1224e342025SAlexander Graf extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
1234e342025SAlexander Graf extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
124fef093beSAlexander Graf 
125fef093beSAlexander Graf extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
126fef093beSAlexander Graf extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
127fef093beSAlexander Graf extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
128fef093beSAlexander Graf extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
129fef093beSAlexander Graf extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
130fef093beSAlexander Graf extern int kvmppc_mmu_hpte_sysinit(void);
131fef093beSAlexander Graf extern void kvmppc_mmu_hpte_sysexit(void);
132de56a948SPaul Mackerras extern int kvmppc_mmu_hv_init(void);
133fef093beSAlexander Graf 
1345467a97dSAlexander Graf extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1355467a97dSAlexander Graf extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1364e342025SAlexander Graf extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
137de56a948SPaul Mackerras extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
138e15a1137SAlexander Graf extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
139e15a1137SAlexander Graf 			   bool upper, u32 val);
140aba3bd7fSAlexander Graf extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
141831317b6SAlexander Graf extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
142e8508940SAlexander Graf extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
1434e342025SAlexander Graf 
14402143947SPaul Mackerras extern void kvmppc_entry_trampoline(void);
145de56a948SPaul Mackerras extern void kvmppc_hv_entry_trampoline(void);
146d5e52813SAlexander Graf extern void kvmppc_load_up_fpu(void);
147d5e52813SAlexander Graf extern void kvmppc_load_up_altivec(void);
148d5e52813SAlexander Graf extern void kvmppc_load_up_vsx(void);
149ca7f4203SAlexander Graf extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
150ca7f4203SAlexander Graf extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
1510254f074SAlexander Graf extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
1524e342025SAlexander Graf 
1534e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
1544e342025SAlexander Graf {
1554e342025SAlexander Graf 	return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
1564e342025SAlexander Graf }
1574e342025SAlexander Graf 
158de56a948SPaul Mackerras extern void kvm_return_point(void);
159de56a948SPaul Mackerras 
160de56a948SPaul Mackerras /* Also add subarch specific defines */
161de56a948SPaul Mackerras 
162de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
163de56a948SPaul Mackerras #include <asm/kvm_book3s_32.h>
164de56a948SPaul Mackerras #endif
165de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
166de56a948SPaul Mackerras #include <asm/kvm_book3s_64.h>
167de56a948SPaul Mackerras #endif
168de56a948SPaul Mackerras 
169de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_PR
170de56a948SPaul Mackerras 
171f05ed4d5SPaul Mackerras static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
172f05ed4d5SPaul Mackerras {
173f05ed4d5SPaul Mackerras 	return to_book3s(vcpu)->hior;
174f05ed4d5SPaul Mackerras }
175f05ed4d5SPaul Mackerras 
176f05ed4d5SPaul Mackerras static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
177f05ed4d5SPaul Mackerras 			unsigned long pending_now, unsigned long old_pending)
178f05ed4d5SPaul Mackerras {
179f05ed4d5SPaul Mackerras 	if (pending_now)
180f05ed4d5SPaul Mackerras 		vcpu->arch.shared->int_pending = 1;
181f05ed4d5SPaul Mackerras 	else if (old_pending)
182f05ed4d5SPaul Mackerras 		vcpu->arch.shared->int_pending = 0;
183f05ed4d5SPaul Mackerras }
184f05ed4d5SPaul Mackerras 
185c7f38f46SAlexander Graf static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
186c7f38f46SAlexander Graf {
187c7f38f46SAlexander Graf 	if ( num < 14 ) {
188c7f38f46SAlexander Graf 		to_svcpu(vcpu)->gpr[num] = val;
189c7f38f46SAlexander Graf 		to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
190c7f38f46SAlexander Graf 	} else
191c7f38f46SAlexander Graf 		vcpu->arch.gpr[num] = val;
192c7f38f46SAlexander Graf }
193c7f38f46SAlexander Graf 
194c7f38f46SAlexander Graf static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
195c7f38f46SAlexander Graf {
196c7f38f46SAlexander Graf 	if ( num < 14 )
197c7f38f46SAlexander Graf 		return to_svcpu(vcpu)->gpr[num];
198c7f38f46SAlexander Graf 	else
199c7f38f46SAlexander Graf 		return vcpu->arch.gpr[num];
200c7f38f46SAlexander Graf }
201c7f38f46SAlexander Graf 
202c7f38f46SAlexander Graf static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
203c7f38f46SAlexander Graf {
204c7f38f46SAlexander Graf 	to_svcpu(vcpu)->cr = val;
205c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->cr = val;
206c7f38f46SAlexander Graf }
207c7f38f46SAlexander Graf 
208c7f38f46SAlexander Graf static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
209c7f38f46SAlexander Graf {
210c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->cr;
211c7f38f46SAlexander Graf }
212c7f38f46SAlexander Graf 
213c7f38f46SAlexander Graf static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
214c7f38f46SAlexander Graf {
215c7f38f46SAlexander Graf 	to_svcpu(vcpu)->xer = val;
216c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->xer = val;
217c7f38f46SAlexander Graf }
218c7f38f46SAlexander Graf 
219c7f38f46SAlexander Graf static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
220c7f38f46SAlexander Graf {
221c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->xer;
222c7f38f46SAlexander Graf }
223c7f38f46SAlexander Graf 
224c7f38f46SAlexander Graf static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
225c7f38f46SAlexander Graf {
226c7f38f46SAlexander Graf 	to_svcpu(vcpu)->ctr = val;
227c7f38f46SAlexander Graf }
228c7f38f46SAlexander Graf 
229c7f38f46SAlexander Graf static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
230c7f38f46SAlexander Graf {
231c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->ctr;
232c7f38f46SAlexander Graf }
233c7f38f46SAlexander Graf 
234c7f38f46SAlexander Graf static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
235c7f38f46SAlexander Graf {
236c7f38f46SAlexander Graf 	to_svcpu(vcpu)->lr = val;
237c7f38f46SAlexander Graf }
238c7f38f46SAlexander Graf 
239c7f38f46SAlexander Graf static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
240c7f38f46SAlexander Graf {
241c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->lr;
242c7f38f46SAlexander Graf }
243c7f38f46SAlexander Graf 
244c7f38f46SAlexander Graf static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
245c7f38f46SAlexander Graf {
246c7f38f46SAlexander Graf 	to_svcpu(vcpu)->pc = val;
247c7f38f46SAlexander Graf }
248c7f38f46SAlexander Graf 
249c7f38f46SAlexander Graf static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
250c7f38f46SAlexander Graf {
251c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->pc;
252c7f38f46SAlexander Graf }
253c7f38f46SAlexander Graf 
254c7f38f46SAlexander Graf static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
255c7f38f46SAlexander Graf {
256c7f38f46SAlexander Graf 	ulong pc = kvmppc_get_pc(vcpu);
257c7f38f46SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
258c7f38f46SAlexander Graf 
259c7f38f46SAlexander Graf 	/* Load the instruction manually if it failed to do so in the
260c7f38f46SAlexander Graf 	 * exit path */
261c7f38f46SAlexander Graf 	if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
262c7f38f46SAlexander Graf 		kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
263c7f38f46SAlexander Graf 
264c7f38f46SAlexander Graf 	return svcpu->last_inst;
265c7f38f46SAlexander Graf }
266c7f38f46SAlexander Graf 
267c7f38f46SAlexander Graf static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
268c7f38f46SAlexander Graf {
269c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->fault_dar;
270c7f38f46SAlexander Graf }
2714e342025SAlexander Graf 
272f05ed4d5SPaul Mackerras static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
273f05ed4d5SPaul Mackerras {
274f05ed4d5SPaul Mackerras 	ulong crit_raw = vcpu->arch.shared->critical;
275f05ed4d5SPaul Mackerras 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
276f05ed4d5SPaul Mackerras 	bool crit;
277f05ed4d5SPaul Mackerras 
278f05ed4d5SPaul Mackerras 	/* Truncate crit indicators in 32 bit mode */
279f05ed4d5SPaul Mackerras 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
280f05ed4d5SPaul Mackerras 		crit_raw &= 0xffffffff;
281f05ed4d5SPaul Mackerras 		crit_r1 &= 0xffffffff;
282f05ed4d5SPaul Mackerras 	}
283f05ed4d5SPaul Mackerras 
284f05ed4d5SPaul Mackerras 	/* Critical section when crit == r1 */
285f05ed4d5SPaul Mackerras 	crit = (crit_raw == crit_r1);
286f05ed4d5SPaul Mackerras 	/* ... and we're in supervisor mode */
287f05ed4d5SPaul Mackerras 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
288f05ed4d5SPaul Mackerras 
289f05ed4d5SPaul Mackerras 	return crit;
290f05ed4d5SPaul Mackerras }
291de56a948SPaul Mackerras #else /* CONFIG_KVM_BOOK3S_PR */
292de56a948SPaul Mackerras 
293de56a948SPaul Mackerras static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
294de56a948SPaul Mackerras {
295de56a948SPaul Mackerras 	return 0;
296de56a948SPaul Mackerras }
297de56a948SPaul Mackerras 
298de56a948SPaul Mackerras static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
299de56a948SPaul Mackerras 			unsigned long pending_now, unsigned long old_pending)
300de56a948SPaul Mackerras {
301de56a948SPaul Mackerras }
302de56a948SPaul Mackerras 
303de56a948SPaul Mackerras static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
304de56a948SPaul Mackerras {
305de56a948SPaul Mackerras 	vcpu->arch.gpr[num] = val;
306de56a948SPaul Mackerras }
307de56a948SPaul Mackerras 
308de56a948SPaul Mackerras static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
309de56a948SPaul Mackerras {
310de56a948SPaul Mackerras 	return vcpu->arch.gpr[num];
311de56a948SPaul Mackerras }
312de56a948SPaul Mackerras 
313de56a948SPaul Mackerras static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
314de56a948SPaul Mackerras {
315de56a948SPaul Mackerras 	vcpu->arch.cr = val;
316de56a948SPaul Mackerras }
317de56a948SPaul Mackerras 
318de56a948SPaul Mackerras static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
319de56a948SPaul Mackerras {
320de56a948SPaul Mackerras 	return vcpu->arch.cr;
321de56a948SPaul Mackerras }
322de56a948SPaul Mackerras 
323de56a948SPaul Mackerras static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
324de56a948SPaul Mackerras {
325de56a948SPaul Mackerras 	vcpu->arch.xer = val;
326de56a948SPaul Mackerras }
327de56a948SPaul Mackerras 
328de56a948SPaul Mackerras static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
329de56a948SPaul Mackerras {
330de56a948SPaul Mackerras 	return vcpu->arch.xer;
331de56a948SPaul Mackerras }
332de56a948SPaul Mackerras 
333de56a948SPaul Mackerras static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
334de56a948SPaul Mackerras {
335de56a948SPaul Mackerras 	vcpu->arch.ctr = val;
336de56a948SPaul Mackerras }
337de56a948SPaul Mackerras 
338de56a948SPaul Mackerras static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
339de56a948SPaul Mackerras {
340de56a948SPaul Mackerras 	return vcpu->arch.ctr;
341de56a948SPaul Mackerras }
342de56a948SPaul Mackerras 
343de56a948SPaul Mackerras static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
344de56a948SPaul Mackerras {
345de56a948SPaul Mackerras 	vcpu->arch.lr = val;
346de56a948SPaul Mackerras }
347de56a948SPaul Mackerras 
348de56a948SPaul Mackerras static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
349de56a948SPaul Mackerras {
350de56a948SPaul Mackerras 	return vcpu->arch.lr;
351de56a948SPaul Mackerras }
352de56a948SPaul Mackerras 
353de56a948SPaul Mackerras static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
354de56a948SPaul Mackerras {
355de56a948SPaul Mackerras 	vcpu->arch.pc = val;
356de56a948SPaul Mackerras }
357de56a948SPaul Mackerras 
358de56a948SPaul Mackerras static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
359de56a948SPaul Mackerras {
360de56a948SPaul Mackerras 	return vcpu->arch.pc;
361de56a948SPaul Mackerras }
362de56a948SPaul Mackerras 
363de56a948SPaul Mackerras static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
364de56a948SPaul Mackerras {
365de56a948SPaul Mackerras 	ulong pc = kvmppc_get_pc(vcpu);
366de56a948SPaul Mackerras 
367de56a948SPaul Mackerras 	/* Load the instruction manually if it failed to do so in the
368de56a948SPaul Mackerras 	 * exit path */
369de56a948SPaul Mackerras 	if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
370de56a948SPaul Mackerras 		kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
371de56a948SPaul Mackerras 
372de56a948SPaul Mackerras 	return vcpu->arch.last_inst;
373de56a948SPaul Mackerras }
374de56a948SPaul Mackerras 
375de56a948SPaul Mackerras static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
376de56a948SPaul Mackerras {
377de56a948SPaul Mackerras 	return vcpu->arch.fault_dar;
378de56a948SPaul Mackerras }
379de56a948SPaul Mackerras 
380de56a948SPaul Mackerras static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
381de56a948SPaul Mackerras {
382de56a948SPaul Mackerras 	return false;
383de56a948SPaul Mackerras }
384de56a948SPaul Mackerras #endif
385f05ed4d5SPaul Mackerras 
386db507c30SAlexander Graf static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
387db507c30SAlexander Graf 					     unsigned long pte_index)
388db507c30SAlexander Graf {
389db507c30SAlexander Graf 	unsigned long rb, va_low;
390db507c30SAlexander Graf 
391db507c30SAlexander Graf 	rb = (v & ~0x7fUL) << 16;		/* AVA field */
392db507c30SAlexander Graf 	va_low = pte_index >> 3;
393db507c30SAlexander Graf 	if (v & HPTE_V_SECONDARY)
394db507c30SAlexander Graf 		va_low = ~va_low;
395db507c30SAlexander Graf 	/* xor vsid from AVA */
396db507c30SAlexander Graf 	if (!(v & HPTE_V_1TB_SEG))
397db507c30SAlexander Graf 		va_low ^= v >> 12;
398db507c30SAlexander Graf 	else
399db507c30SAlexander Graf 		va_low ^= v >> 24;
400db507c30SAlexander Graf 	va_low &= 0x7ff;
401db507c30SAlexander Graf 	if (v & HPTE_V_LARGE) {
402db507c30SAlexander Graf 		rb |= 1;			/* L field */
403db507c30SAlexander Graf 		if (cpu_has_feature(CPU_FTR_ARCH_206) &&
404db507c30SAlexander Graf 		    (r & 0xff000)) {
405db507c30SAlexander Graf 			/* non-16MB large page, must be 64k */
406db507c30SAlexander Graf 			/* (masks depend on page size) */
407db507c30SAlexander Graf 			rb |= 0x1000;		/* page encoding in LP field */
408db507c30SAlexander Graf 			rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
409db507c30SAlexander Graf 			rb |= (va_low & 0xfe);	/* AVAL field (P7 doesn't seem to care) */
410db507c30SAlexander Graf 		}
411db507c30SAlexander Graf 	} else {
412db507c30SAlexander Graf 		/* 4kB page */
413db507c30SAlexander Graf 		rb |= (va_low & 0x7ff) << 12;	/* remaining 11b of VA */
414db507c30SAlexander Graf 	}
415db507c30SAlexander Graf 	rb |= (v >> 54) & 0x300;		/* B field */
416db507c30SAlexander Graf 	return rb;
417db507c30SAlexander Graf }
418db507c30SAlexander Graf 
419ad0a048bSAlexander Graf /* Magic register values loaded into r3 and r4 before the 'sc' assembly
420ad0a048bSAlexander Graf  * instruction for the OSI hypercalls */
421ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R3			0x113724FA
422ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R4			0x77810F9B
423ad0a048bSAlexander Graf 
4244e342025SAlexander Graf #define INS_DCBZ			0x7c0007ec
4254e342025SAlexander Graf 
4264e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */
427