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 
931022fc3dSAlexander Graf 	bool hior_explicit;		/* HIOR is set by ioctl, not PVR */
941022fc3dSAlexander 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);
124697d3899SPaul Mackerras extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run,
125697d3899SPaul Mackerras 			struct kvm_vcpu *vcpu, unsigned long addr,
126697d3899SPaul Mackerras 			unsigned long status);
127697d3899SPaul Mackerras extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr,
128697d3899SPaul Mackerras 			unsigned long slb_v, unsigned long valid);
129fef093beSAlexander Graf 
130fef093beSAlexander Graf extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
131fef093beSAlexander Graf extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
132fef093beSAlexander Graf extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
133fef093beSAlexander Graf extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
134fef093beSAlexander Graf extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
135fef093beSAlexander Graf extern int kvmppc_mmu_hpte_sysinit(void);
136fef093beSAlexander Graf extern void kvmppc_mmu_hpte_sysexit(void);
137de56a948SPaul Mackerras extern int kvmppc_mmu_hv_init(void);
138fef093beSAlexander Graf 
1395467a97dSAlexander Graf extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1405467a97dSAlexander Graf extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1414e342025SAlexander Graf extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
142de56a948SPaul Mackerras extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
143e15a1137SAlexander Graf extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
144e15a1137SAlexander Graf 			   bool upper, u32 val);
145aba3bd7fSAlexander Graf extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
146831317b6SAlexander Graf extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
147e8508940SAlexander Graf extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
148342d3db7SPaul Mackerras extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
149342d3db7SPaul Mackerras 			unsigned long *rmap, long pte_index, int realmode);
150342d3db7SPaul Mackerras extern void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep,
151342d3db7SPaul Mackerras 			unsigned long pte_index);
15255514893SPaul Mackerras void kvmppc_clear_ref_hpte(struct kvm *kvm, unsigned long *hptep,
15355514893SPaul Mackerras 			unsigned long pte_index);
15493e60249SPaul Mackerras extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
15593e60249SPaul Mackerras 			unsigned long *nb_ret);
15693e60249SPaul Mackerras extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr);
157c77162deSPaul Mackerras extern long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
158c77162deSPaul Mackerras 			long pte_index, unsigned long pteh, unsigned long ptel);
159c77162deSPaul Mackerras extern long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
160c77162deSPaul Mackerras 			long pte_index, unsigned long pteh, unsigned long ptel);
16182ed3616SPaul Mackerras extern long kvmppc_hv_get_dirty_log(struct kvm *kvm,
16282ed3616SPaul Mackerras 			struct kvm_memory_slot *memslot);
1634e342025SAlexander Graf 
16402143947SPaul Mackerras extern void kvmppc_entry_trampoline(void);
165de56a948SPaul Mackerras extern void kvmppc_hv_entry_trampoline(void);
166d5e52813SAlexander Graf extern void kvmppc_load_up_fpu(void);
167d5e52813SAlexander Graf extern void kvmppc_load_up_altivec(void);
168d5e52813SAlexander Graf extern void kvmppc_load_up_vsx(void);
169ca7f4203SAlexander Graf extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
170ca7f4203SAlexander Graf extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
1710254f074SAlexander Graf extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
1724e342025SAlexander Graf 
1734e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
1744e342025SAlexander Graf {
1754e342025SAlexander Graf 	return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
1764e342025SAlexander Graf }
1774e342025SAlexander Graf 
178de56a948SPaul Mackerras extern void kvm_return_point(void);
179de56a948SPaul Mackerras 
180de56a948SPaul Mackerras /* Also add subarch specific defines */
181de56a948SPaul Mackerras 
182de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
183de56a948SPaul Mackerras #include <asm/kvm_book3s_32.h>
184de56a948SPaul Mackerras #endif
185de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
186de56a948SPaul Mackerras #include <asm/kvm_book3s_64.h>
187de56a948SPaul Mackerras #endif
188de56a948SPaul Mackerras 
189de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_PR
190de56a948SPaul Mackerras 
191f05ed4d5SPaul Mackerras static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
192f05ed4d5SPaul Mackerras {
193f05ed4d5SPaul Mackerras 	return to_book3s(vcpu)->hior;
194f05ed4d5SPaul Mackerras }
195f05ed4d5SPaul Mackerras 
196f05ed4d5SPaul Mackerras static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
197f05ed4d5SPaul Mackerras 			unsigned long pending_now, unsigned long old_pending)
198f05ed4d5SPaul Mackerras {
199f05ed4d5SPaul Mackerras 	if (pending_now)
200f05ed4d5SPaul Mackerras 		vcpu->arch.shared->int_pending = 1;
201f05ed4d5SPaul Mackerras 	else if (old_pending)
202f05ed4d5SPaul Mackerras 		vcpu->arch.shared->int_pending = 0;
203f05ed4d5SPaul Mackerras }
204f05ed4d5SPaul Mackerras 
205c7f38f46SAlexander Graf static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
206c7f38f46SAlexander Graf {
207c7f38f46SAlexander Graf 	if ( num < 14 ) {
208468a12c2SAlexander Graf 		struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
209468a12c2SAlexander Graf 		svcpu->gpr[num] = val;
210468a12c2SAlexander Graf 		svcpu_put(svcpu);
211c7f38f46SAlexander Graf 		to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
212c7f38f46SAlexander Graf 	} else
213c7f38f46SAlexander Graf 		vcpu->arch.gpr[num] = val;
214c7f38f46SAlexander Graf }
215c7f38f46SAlexander Graf 
216c7f38f46SAlexander Graf static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
217c7f38f46SAlexander Graf {
218468a12c2SAlexander Graf 	if ( num < 14 ) {
219468a12c2SAlexander Graf 		struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
220468a12c2SAlexander Graf 		ulong r = svcpu->gpr[num];
221468a12c2SAlexander Graf 		svcpu_put(svcpu);
222468a12c2SAlexander Graf 		return r;
223468a12c2SAlexander Graf 	} else
224c7f38f46SAlexander Graf 		return vcpu->arch.gpr[num];
225c7f38f46SAlexander Graf }
226c7f38f46SAlexander Graf 
227c7f38f46SAlexander Graf static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
228c7f38f46SAlexander Graf {
229468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
230468a12c2SAlexander Graf 	svcpu->cr = val;
231468a12c2SAlexander Graf 	svcpu_put(svcpu);
232c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->cr = val;
233c7f38f46SAlexander Graf }
234c7f38f46SAlexander Graf 
235c7f38f46SAlexander Graf static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
236c7f38f46SAlexander Graf {
237468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
238468a12c2SAlexander Graf 	u32 r;
239468a12c2SAlexander Graf 	r = svcpu->cr;
240468a12c2SAlexander Graf 	svcpu_put(svcpu);
241468a12c2SAlexander Graf 	return r;
242c7f38f46SAlexander Graf }
243c7f38f46SAlexander Graf 
244c7f38f46SAlexander Graf static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
245c7f38f46SAlexander Graf {
246468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
247468a12c2SAlexander Graf 	svcpu->xer = val;
248c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->xer = val;
249468a12c2SAlexander Graf 	svcpu_put(svcpu);
250c7f38f46SAlexander Graf }
251c7f38f46SAlexander Graf 
252c7f38f46SAlexander Graf static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
253c7f38f46SAlexander Graf {
254468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
255468a12c2SAlexander Graf 	u32 r;
256468a12c2SAlexander Graf 	r = svcpu->xer;
257468a12c2SAlexander Graf 	svcpu_put(svcpu);
258468a12c2SAlexander Graf 	return r;
259c7f38f46SAlexander Graf }
260c7f38f46SAlexander Graf 
261c7f38f46SAlexander Graf static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
262c7f38f46SAlexander Graf {
263468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
264468a12c2SAlexander Graf 	svcpu->ctr = val;
265468a12c2SAlexander Graf 	svcpu_put(svcpu);
266c7f38f46SAlexander Graf }
267c7f38f46SAlexander Graf 
268c7f38f46SAlexander Graf static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
269c7f38f46SAlexander Graf {
270468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
271468a12c2SAlexander Graf 	ulong r;
272468a12c2SAlexander Graf 	r = svcpu->ctr;
273468a12c2SAlexander Graf 	svcpu_put(svcpu);
274468a12c2SAlexander Graf 	return r;
275c7f38f46SAlexander Graf }
276c7f38f46SAlexander Graf 
277c7f38f46SAlexander Graf static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
278c7f38f46SAlexander Graf {
279468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
280468a12c2SAlexander Graf 	svcpu->lr = val;
281468a12c2SAlexander Graf 	svcpu_put(svcpu);
282c7f38f46SAlexander Graf }
283c7f38f46SAlexander Graf 
284c7f38f46SAlexander Graf static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
285c7f38f46SAlexander Graf {
286468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
287468a12c2SAlexander Graf 	ulong r;
288468a12c2SAlexander Graf 	r = svcpu->lr;
289468a12c2SAlexander Graf 	svcpu_put(svcpu);
290468a12c2SAlexander Graf 	return r;
291c7f38f46SAlexander Graf }
292c7f38f46SAlexander Graf 
293c7f38f46SAlexander Graf static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
294c7f38f46SAlexander Graf {
295468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
296468a12c2SAlexander Graf 	svcpu->pc = val;
297468a12c2SAlexander Graf 	svcpu_put(svcpu);
298c7f38f46SAlexander Graf }
299c7f38f46SAlexander Graf 
300c7f38f46SAlexander Graf static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
301c7f38f46SAlexander Graf {
302468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
303468a12c2SAlexander Graf 	ulong r;
304468a12c2SAlexander Graf 	r = svcpu->pc;
305468a12c2SAlexander Graf 	svcpu_put(svcpu);
306468a12c2SAlexander Graf 	return r;
307c7f38f46SAlexander Graf }
308c7f38f46SAlexander Graf 
309c7f38f46SAlexander Graf static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
310c7f38f46SAlexander Graf {
311c7f38f46SAlexander Graf 	ulong pc = kvmppc_get_pc(vcpu);
312468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
313468a12c2SAlexander Graf 	u32 r;
314c7f38f46SAlexander Graf 
315c7f38f46SAlexander Graf 	/* Load the instruction manually if it failed to do so in the
316c7f38f46SAlexander Graf 	 * exit path */
317c7f38f46SAlexander Graf 	if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
318c7f38f46SAlexander Graf 		kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
319c7f38f46SAlexander Graf 
320468a12c2SAlexander Graf 	r = svcpu->last_inst;
321468a12c2SAlexander Graf 	svcpu_put(svcpu);
322468a12c2SAlexander Graf 	return r;
323c7f38f46SAlexander Graf }
324c7f38f46SAlexander Graf 
325c7f38f46SAlexander Graf static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
326c7f38f46SAlexander Graf {
327468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
328468a12c2SAlexander Graf 	ulong r;
329468a12c2SAlexander Graf 	r = svcpu->fault_dar;
330468a12c2SAlexander Graf 	svcpu_put(svcpu);
331468a12c2SAlexander Graf 	return r;
332c7f38f46SAlexander Graf }
3334e342025SAlexander Graf 
334f05ed4d5SPaul Mackerras static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
335f05ed4d5SPaul Mackerras {
336f05ed4d5SPaul Mackerras 	ulong crit_raw = vcpu->arch.shared->critical;
337f05ed4d5SPaul Mackerras 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
338f05ed4d5SPaul Mackerras 	bool crit;
339f05ed4d5SPaul Mackerras 
340f05ed4d5SPaul Mackerras 	/* Truncate crit indicators in 32 bit mode */
341f05ed4d5SPaul Mackerras 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
342f05ed4d5SPaul Mackerras 		crit_raw &= 0xffffffff;
343f05ed4d5SPaul Mackerras 		crit_r1 &= 0xffffffff;
344f05ed4d5SPaul Mackerras 	}
345f05ed4d5SPaul Mackerras 
346f05ed4d5SPaul Mackerras 	/* Critical section when crit == r1 */
347f05ed4d5SPaul Mackerras 	crit = (crit_raw == crit_r1);
348f05ed4d5SPaul Mackerras 	/* ... and we're in supervisor mode */
349f05ed4d5SPaul Mackerras 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
350f05ed4d5SPaul Mackerras 
351f05ed4d5SPaul Mackerras 	return crit;
352f05ed4d5SPaul Mackerras }
353de56a948SPaul Mackerras #else /* CONFIG_KVM_BOOK3S_PR */
354de56a948SPaul Mackerras 
355de56a948SPaul Mackerras static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
356de56a948SPaul Mackerras {
357de56a948SPaul Mackerras 	return 0;
358de56a948SPaul Mackerras }
359de56a948SPaul Mackerras 
360de56a948SPaul Mackerras static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
361de56a948SPaul Mackerras 			unsigned long pending_now, unsigned long old_pending)
362de56a948SPaul Mackerras {
363de56a948SPaul Mackerras }
364de56a948SPaul Mackerras 
365de56a948SPaul Mackerras static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
366de56a948SPaul Mackerras {
367de56a948SPaul Mackerras 	vcpu->arch.gpr[num] = val;
368de56a948SPaul Mackerras }
369de56a948SPaul Mackerras 
370de56a948SPaul Mackerras static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
371de56a948SPaul Mackerras {
372de56a948SPaul Mackerras 	return vcpu->arch.gpr[num];
373de56a948SPaul Mackerras }
374de56a948SPaul Mackerras 
375de56a948SPaul Mackerras static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
376de56a948SPaul Mackerras {
377de56a948SPaul Mackerras 	vcpu->arch.cr = val;
378de56a948SPaul Mackerras }
379de56a948SPaul Mackerras 
380de56a948SPaul Mackerras static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
381de56a948SPaul Mackerras {
382de56a948SPaul Mackerras 	return vcpu->arch.cr;
383de56a948SPaul Mackerras }
384de56a948SPaul Mackerras 
385de56a948SPaul Mackerras static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
386de56a948SPaul Mackerras {
387de56a948SPaul Mackerras 	vcpu->arch.xer = val;
388de56a948SPaul Mackerras }
389de56a948SPaul Mackerras 
390de56a948SPaul Mackerras static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
391de56a948SPaul Mackerras {
392de56a948SPaul Mackerras 	return vcpu->arch.xer;
393de56a948SPaul Mackerras }
394de56a948SPaul Mackerras 
395de56a948SPaul Mackerras static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
396de56a948SPaul Mackerras {
397de56a948SPaul Mackerras 	vcpu->arch.ctr = val;
398de56a948SPaul Mackerras }
399de56a948SPaul Mackerras 
400de56a948SPaul Mackerras static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
401de56a948SPaul Mackerras {
402de56a948SPaul Mackerras 	return vcpu->arch.ctr;
403de56a948SPaul Mackerras }
404de56a948SPaul Mackerras 
405de56a948SPaul Mackerras static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
406de56a948SPaul Mackerras {
407de56a948SPaul Mackerras 	vcpu->arch.lr = val;
408de56a948SPaul Mackerras }
409de56a948SPaul Mackerras 
410de56a948SPaul Mackerras static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
411de56a948SPaul Mackerras {
412de56a948SPaul Mackerras 	return vcpu->arch.lr;
413de56a948SPaul Mackerras }
414de56a948SPaul Mackerras 
415de56a948SPaul Mackerras static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
416de56a948SPaul Mackerras {
417de56a948SPaul Mackerras 	vcpu->arch.pc = val;
418de56a948SPaul Mackerras }
419de56a948SPaul Mackerras 
420de56a948SPaul Mackerras static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
421de56a948SPaul Mackerras {
422de56a948SPaul Mackerras 	return vcpu->arch.pc;
423de56a948SPaul Mackerras }
424de56a948SPaul Mackerras 
425de56a948SPaul Mackerras static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
426de56a948SPaul Mackerras {
427de56a948SPaul Mackerras 	ulong pc = kvmppc_get_pc(vcpu);
428de56a948SPaul Mackerras 
429de56a948SPaul Mackerras 	/* Load the instruction manually if it failed to do so in the
430de56a948SPaul Mackerras 	 * exit path */
431de56a948SPaul Mackerras 	if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
432de56a948SPaul Mackerras 		kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
433de56a948SPaul Mackerras 
434de56a948SPaul Mackerras 	return vcpu->arch.last_inst;
435de56a948SPaul Mackerras }
436de56a948SPaul Mackerras 
437de56a948SPaul Mackerras static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
438de56a948SPaul Mackerras {
439de56a948SPaul Mackerras 	return vcpu->arch.fault_dar;
440de56a948SPaul Mackerras }
441de56a948SPaul Mackerras 
442de56a948SPaul Mackerras static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
443de56a948SPaul Mackerras {
444de56a948SPaul Mackerras 	return false;
445de56a948SPaul Mackerras }
446de56a948SPaul Mackerras #endif
447f05ed4d5SPaul Mackerras 
448ad0a048bSAlexander Graf /* Magic register values loaded into r3 and r4 before the 'sc' assembly
449ad0a048bSAlexander Graf  * instruction for the OSI hypercalls */
450ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R3			0x113724FA
451ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R4			0x77810F9B
452ad0a048bSAlexander Graf 
4534e342025SAlexander Graf #define INS_DCBZ			0x7c0007ec
4544e342025SAlexander Graf 
455043cc4d7SScott Wood /* LPIDs we support with this build -- runtime limit may be lower */
456043cc4d7SScott Wood #define KVMPPC_NR_LPIDS			(LPID_RSVD + 1)
457043cc4d7SScott Wood 
4584e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */
459