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;
848b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_32
858b6db3bcSAlexander Graf 	u32 vsid_pool[VSID_POOL_SIZE];
86ffe36492SBenjamin Herrenschmidt 	u32 vsid_next;
878b6db3bcSAlexander Graf #else
88ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_first;
89ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_max;
90ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_next;
918b6db3bcSAlexander Graf #endif
928b6db3bcSAlexander Graf 	int context_id[SID_CONTEXTS];
93c4befc58SPaul Mackerras 
941022fc3dSAlexander Graf 	bool hior_explicit;		/* HIOR is set by ioctl, not PVR */
951022fc3dSAlexander Graf 
96c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
97c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
98c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
99c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
100c4befc58SPaul Mackerras 	int hpte_cache_count;
101c4befc58SPaul Mackerras 	spinlock_t mmu_lock;
1024e342025SAlexander Graf };
1034e342025SAlexander Graf 
1044e342025SAlexander Graf #define CONTEXT_HOST		0
1054e342025SAlexander Graf #define CONTEXT_GUEST		1
1064e342025SAlexander Graf #define CONTEXT_GUEST_END	2
1074e342025SAlexander Graf 
108f7bc74e1SAlexander Graf #define VSID_REAL	0x1fffffffffc00000ULL
109f7bc74e1SAlexander Graf #define VSID_BAT	0x1fffffffffb00000ULL
110f7bc74e1SAlexander Graf #define VSID_REAL_DR	0x2000000000000000ULL
111f7bc74e1SAlexander Graf #define VSID_REAL_IR	0x4000000000000000ULL
1125a1b419fSAlexander Graf #define VSID_PR		0x8000000000000000ULL
1134e342025SAlexander Graf 
114af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
1154e342025SAlexander Graf extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
116af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
1174e342025SAlexander Graf extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
118f05ed4d5SPaul Mackerras extern void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr);
1194e342025SAlexander Graf extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
1204e342025SAlexander Graf extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
121de56a948SPaul Mackerras extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
1224e342025SAlexander Graf extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
1234e342025SAlexander Graf extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
1244e342025SAlexander Graf extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
125697d3899SPaul Mackerras extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run,
126697d3899SPaul Mackerras 			struct kvm_vcpu *vcpu, unsigned long addr,
127697d3899SPaul Mackerras 			unsigned long status);
128697d3899SPaul Mackerras extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr,
129697d3899SPaul Mackerras 			unsigned long slb_v, unsigned long valid);
130fef093beSAlexander Graf 
131fef093beSAlexander Graf extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
132fef093beSAlexander Graf extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
133fef093beSAlexander Graf extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
134fef093beSAlexander Graf extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
135fef093beSAlexander Graf extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
136fef093beSAlexander Graf extern int kvmppc_mmu_hpte_sysinit(void);
137fef093beSAlexander Graf extern void kvmppc_mmu_hpte_sysexit(void);
138de56a948SPaul Mackerras extern int kvmppc_mmu_hv_init(void);
139fef093beSAlexander Graf 
1405467a97dSAlexander Graf extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1415467a97dSAlexander Graf extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1424e342025SAlexander Graf extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
143de56a948SPaul Mackerras extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
144e15a1137SAlexander Graf extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
145e15a1137SAlexander Graf 			   bool upper, u32 val);
146aba3bd7fSAlexander Graf extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
147831317b6SAlexander Graf extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
148e8508940SAlexander Graf extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
149342d3db7SPaul Mackerras extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
150342d3db7SPaul Mackerras 			unsigned long *rmap, long pte_index, int realmode);
151342d3db7SPaul Mackerras extern void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep,
152342d3db7SPaul Mackerras 			unsigned long pte_index);
15355514893SPaul Mackerras void kvmppc_clear_ref_hpte(struct kvm *kvm, unsigned long *hptep,
15455514893SPaul Mackerras 			unsigned long pte_index);
15593e60249SPaul Mackerras extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
15693e60249SPaul Mackerras 			unsigned long *nb_ret);
15793e60249SPaul Mackerras extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr);
158c77162deSPaul Mackerras extern long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
159c77162deSPaul Mackerras 			long pte_index, unsigned long pteh, unsigned long ptel);
160c77162deSPaul Mackerras extern long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
161c77162deSPaul Mackerras 			long pte_index, unsigned long pteh, unsigned long ptel);
16282ed3616SPaul Mackerras extern long kvmppc_hv_get_dirty_log(struct kvm *kvm,
16382ed3616SPaul Mackerras 			struct kvm_memory_slot *memslot);
1644e342025SAlexander Graf 
16502143947SPaul Mackerras extern void kvmppc_entry_trampoline(void);
166de56a948SPaul Mackerras extern void kvmppc_hv_entry_trampoline(void);
167d5e52813SAlexander Graf extern void kvmppc_load_up_fpu(void);
168d5e52813SAlexander Graf extern void kvmppc_load_up_altivec(void);
169d5e52813SAlexander Graf extern void kvmppc_load_up_vsx(void);
170ca7f4203SAlexander Graf extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
171ca7f4203SAlexander Graf extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
1720254f074SAlexander Graf extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
1734e342025SAlexander Graf 
1744e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
1754e342025SAlexander Graf {
1764e342025SAlexander Graf 	return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
1774e342025SAlexander Graf }
1784e342025SAlexander Graf 
179de56a948SPaul Mackerras extern void kvm_return_point(void);
180de56a948SPaul Mackerras 
181de56a948SPaul Mackerras /* Also add subarch specific defines */
182de56a948SPaul Mackerras 
183de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
184de56a948SPaul Mackerras #include <asm/kvm_book3s_32.h>
185de56a948SPaul Mackerras #endif
186de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
187de56a948SPaul Mackerras #include <asm/kvm_book3s_64.h>
188de56a948SPaul Mackerras #endif
189de56a948SPaul Mackerras 
190de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_PR
191de56a948SPaul Mackerras 
192f05ed4d5SPaul Mackerras static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
193f05ed4d5SPaul Mackerras {
194f05ed4d5SPaul Mackerras 	return to_book3s(vcpu)->hior;
195f05ed4d5SPaul Mackerras }
196f05ed4d5SPaul Mackerras 
197f05ed4d5SPaul Mackerras static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
198f05ed4d5SPaul Mackerras 			unsigned long pending_now, unsigned long old_pending)
199f05ed4d5SPaul Mackerras {
200f05ed4d5SPaul Mackerras 	if (pending_now)
201f05ed4d5SPaul Mackerras 		vcpu->arch.shared->int_pending = 1;
202f05ed4d5SPaul Mackerras 	else if (old_pending)
203f05ed4d5SPaul Mackerras 		vcpu->arch.shared->int_pending = 0;
204f05ed4d5SPaul Mackerras }
205f05ed4d5SPaul Mackerras 
206c7f38f46SAlexander Graf static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
207c7f38f46SAlexander Graf {
208c7f38f46SAlexander Graf 	if ( num < 14 ) {
209468a12c2SAlexander Graf 		struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
210468a12c2SAlexander Graf 		svcpu->gpr[num] = val;
211468a12c2SAlexander Graf 		svcpu_put(svcpu);
212c7f38f46SAlexander Graf 		to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
213c7f38f46SAlexander Graf 	} else
214c7f38f46SAlexander Graf 		vcpu->arch.gpr[num] = val;
215c7f38f46SAlexander Graf }
216c7f38f46SAlexander Graf 
217c7f38f46SAlexander Graf static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
218c7f38f46SAlexander Graf {
219468a12c2SAlexander Graf 	if ( num < 14 ) {
220468a12c2SAlexander Graf 		struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
221468a12c2SAlexander Graf 		ulong r = svcpu->gpr[num];
222468a12c2SAlexander Graf 		svcpu_put(svcpu);
223468a12c2SAlexander Graf 		return r;
224468a12c2SAlexander Graf 	} else
225c7f38f46SAlexander Graf 		return vcpu->arch.gpr[num];
226c7f38f46SAlexander Graf }
227c7f38f46SAlexander Graf 
228c7f38f46SAlexander Graf static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
229c7f38f46SAlexander Graf {
230468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
231468a12c2SAlexander Graf 	svcpu->cr = val;
232468a12c2SAlexander Graf 	svcpu_put(svcpu);
233c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->cr = val;
234c7f38f46SAlexander Graf }
235c7f38f46SAlexander Graf 
236c7f38f46SAlexander Graf static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
237c7f38f46SAlexander Graf {
238468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
239468a12c2SAlexander Graf 	u32 r;
240468a12c2SAlexander Graf 	r = svcpu->cr;
241468a12c2SAlexander Graf 	svcpu_put(svcpu);
242468a12c2SAlexander Graf 	return r;
243c7f38f46SAlexander Graf }
244c7f38f46SAlexander Graf 
245c7f38f46SAlexander Graf static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
246c7f38f46SAlexander Graf {
247468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
248468a12c2SAlexander Graf 	svcpu->xer = val;
249c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->xer = val;
250468a12c2SAlexander Graf 	svcpu_put(svcpu);
251c7f38f46SAlexander Graf }
252c7f38f46SAlexander Graf 
253c7f38f46SAlexander Graf static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
254c7f38f46SAlexander Graf {
255468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
256468a12c2SAlexander Graf 	u32 r;
257468a12c2SAlexander Graf 	r = svcpu->xer;
258468a12c2SAlexander Graf 	svcpu_put(svcpu);
259468a12c2SAlexander Graf 	return r;
260c7f38f46SAlexander Graf }
261c7f38f46SAlexander Graf 
262c7f38f46SAlexander Graf static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
263c7f38f46SAlexander Graf {
264468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
265468a12c2SAlexander Graf 	svcpu->ctr = val;
266468a12c2SAlexander Graf 	svcpu_put(svcpu);
267c7f38f46SAlexander Graf }
268c7f38f46SAlexander Graf 
269c7f38f46SAlexander Graf static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
270c7f38f46SAlexander Graf {
271468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
272468a12c2SAlexander Graf 	ulong r;
273468a12c2SAlexander Graf 	r = svcpu->ctr;
274468a12c2SAlexander Graf 	svcpu_put(svcpu);
275468a12c2SAlexander Graf 	return r;
276c7f38f46SAlexander Graf }
277c7f38f46SAlexander Graf 
278c7f38f46SAlexander Graf static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
279c7f38f46SAlexander Graf {
280468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
281468a12c2SAlexander Graf 	svcpu->lr = val;
282468a12c2SAlexander Graf 	svcpu_put(svcpu);
283c7f38f46SAlexander Graf }
284c7f38f46SAlexander Graf 
285c7f38f46SAlexander Graf static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
286c7f38f46SAlexander Graf {
287468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
288468a12c2SAlexander Graf 	ulong r;
289468a12c2SAlexander Graf 	r = svcpu->lr;
290468a12c2SAlexander Graf 	svcpu_put(svcpu);
291468a12c2SAlexander Graf 	return r;
292c7f38f46SAlexander Graf }
293c7f38f46SAlexander Graf 
294c7f38f46SAlexander Graf static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
295c7f38f46SAlexander Graf {
296468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
297468a12c2SAlexander Graf 	svcpu->pc = val;
298468a12c2SAlexander Graf 	svcpu_put(svcpu);
299c7f38f46SAlexander Graf }
300c7f38f46SAlexander Graf 
301c7f38f46SAlexander Graf static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
302c7f38f46SAlexander Graf {
303468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
304468a12c2SAlexander Graf 	ulong r;
305468a12c2SAlexander Graf 	r = svcpu->pc;
306468a12c2SAlexander Graf 	svcpu_put(svcpu);
307468a12c2SAlexander Graf 	return r;
308c7f38f46SAlexander Graf }
309c7f38f46SAlexander Graf 
310c7f38f46SAlexander Graf static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
311c7f38f46SAlexander Graf {
312c7f38f46SAlexander Graf 	ulong pc = kvmppc_get_pc(vcpu);
313468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
314468a12c2SAlexander Graf 	u32 r;
315c7f38f46SAlexander Graf 
316c7f38f46SAlexander Graf 	/* Load the instruction manually if it failed to do so in the
317c7f38f46SAlexander Graf 	 * exit path */
318c7f38f46SAlexander Graf 	if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
319c7f38f46SAlexander Graf 		kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
320c7f38f46SAlexander Graf 
321468a12c2SAlexander Graf 	r = svcpu->last_inst;
322468a12c2SAlexander Graf 	svcpu_put(svcpu);
323468a12c2SAlexander Graf 	return r;
324c7f38f46SAlexander Graf }
325c7f38f46SAlexander Graf 
326c7f38f46SAlexander Graf static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
327c7f38f46SAlexander Graf {
328468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
329468a12c2SAlexander Graf 	ulong r;
330468a12c2SAlexander Graf 	r = svcpu->fault_dar;
331468a12c2SAlexander Graf 	svcpu_put(svcpu);
332468a12c2SAlexander Graf 	return r;
333c7f38f46SAlexander Graf }
3344e342025SAlexander Graf 
335f05ed4d5SPaul Mackerras static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
336f05ed4d5SPaul Mackerras {
337f05ed4d5SPaul Mackerras 	ulong crit_raw = vcpu->arch.shared->critical;
338f05ed4d5SPaul Mackerras 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
339f05ed4d5SPaul Mackerras 	bool crit;
340f05ed4d5SPaul Mackerras 
341f05ed4d5SPaul Mackerras 	/* Truncate crit indicators in 32 bit mode */
342f05ed4d5SPaul Mackerras 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
343f05ed4d5SPaul Mackerras 		crit_raw &= 0xffffffff;
344f05ed4d5SPaul Mackerras 		crit_r1 &= 0xffffffff;
345f05ed4d5SPaul Mackerras 	}
346f05ed4d5SPaul Mackerras 
347f05ed4d5SPaul Mackerras 	/* Critical section when crit == r1 */
348f05ed4d5SPaul Mackerras 	crit = (crit_raw == crit_r1);
349f05ed4d5SPaul Mackerras 	/* ... and we're in supervisor mode */
350f05ed4d5SPaul Mackerras 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
351f05ed4d5SPaul Mackerras 
352f05ed4d5SPaul Mackerras 	return crit;
353f05ed4d5SPaul Mackerras }
354de56a948SPaul Mackerras #else /* CONFIG_KVM_BOOK3S_PR */
355de56a948SPaul Mackerras 
356de56a948SPaul Mackerras static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
357de56a948SPaul Mackerras {
358de56a948SPaul Mackerras 	return 0;
359de56a948SPaul Mackerras }
360de56a948SPaul Mackerras 
361de56a948SPaul Mackerras static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
362de56a948SPaul Mackerras 			unsigned long pending_now, unsigned long old_pending)
363de56a948SPaul Mackerras {
364de56a948SPaul Mackerras }
365de56a948SPaul Mackerras 
366de56a948SPaul Mackerras static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
367de56a948SPaul Mackerras {
368de56a948SPaul Mackerras 	vcpu->arch.gpr[num] = val;
369de56a948SPaul Mackerras }
370de56a948SPaul Mackerras 
371de56a948SPaul Mackerras static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
372de56a948SPaul Mackerras {
373de56a948SPaul Mackerras 	return vcpu->arch.gpr[num];
374de56a948SPaul Mackerras }
375de56a948SPaul Mackerras 
376de56a948SPaul Mackerras static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
377de56a948SPaul Mackerras {
378de56a948SPaul Mackerras 	vcpu->arch.cr = val;
379de56a948SPaul Mackerras }
380de56a948SPaul Mackerras 
381de56a948SPaul Mackerras static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
382de56a948SPaul Mackerras {
383de56a948SPaul Mackerras 	return vcpu->arch.cr;
384de56a948SPaul Mackerras }
385de56a948SPaul Mackerras 
386de56a948SPaul Mackerras static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
387de56a948SPaul Mackerras {
388de56a948SPaul Mackerras 	vcpu->arch.xer = val;
389de56a948SPaul Mackerras }
390de56a948SPaul Mackerras 
391de56a948SPaul Mackerras static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
392de56a948SPaul Mackerras {
393de56a948SPaul Mackerras 	return vcpu->arch.xer;
394de56a948SPaul Mackerras }
395de56a948SPaul Mackerras 
396de56a948SPaul Mackerras static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
397de56a948SPaul Mackerras {
398de56a948SPaul Mackerras 	vcpu->arch.ctr = val;
399de56a948SPaul Mackerras }
400de56a948SPaul Mackerras 
401de56a948SPaul Mackerras static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
402de56a948SPaul Mackerras {
403de56a948SPaul Mackerras 	return vcpu->arch.ctr;
404de56a948SPaul Mackerras }
405de56a948SPaul Mackerras 
406de56a948SPaul Mackerras static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
407de56a948SPaul Mackerras {
408de56a948SPaul Mackerras 	vcpu->arch.lr = val;
409de56a948SPaul Mackerras }
410de56a948SPaul Mackerras 
411de56a948SPaul Mackerras static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
412de56a948SPaul Mackerras {
413de56a948SPaul Mackerras 	return vcpu->arch.lr;
414de56a948SPaul Mackerras }
415de56a948SPaul Mackerras 
416de56a948SPaul Mackerras static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
417de56a948SPaul Mackerras {
418de56a948SPaul Mackerras 	vcpu->arch.pc = val;
419de56a948SPaul Mackerras }
420de56a948SPaul Mackerras 
421de56a948SPaul Mackerras static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
422de56a948SPaul Mackerras {
423de56a948SPaul Mackerras 	return vcpu->arch.pc;
424de56a948SPaul Mackerras }
425de56a948SPaul Mackerras 
426de56a948SPaul Mackerras static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
427de56a948SPaul Mackerras {
428de56a948SPaul Mackerras 	ulong pc = kvmppc_get_pc(vcpu);
429de56a948SPaul Mackerras 
430de56a948SPaul Mackerras 	/* Load the instruction manually if it failed to do so in the
431de56a948SPaul Mackerras 	 * exit path */
432de56a948SPaul Mackerras 	if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
433de56a948SPaul Mackerras 		kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
434de56a948SPaul Mackerras 
435de56a948SPaul Mackerras 	return vcpu->arch.last_inst;
436de56a948SPaul Mackerras }
437de56a948SPaul Mackerras 
438de56a948SPaul Mackerras static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
439de56a948SPaul Mackerras {
440de56a948SPaul Mackerras 	return vcpu->arch.fault_dar;
441de56a948SPaul Mackerras }
442de56a948SPaul Mackerras 
443de56a948SPaul Mackerras static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
444de56a948SPaul Mackerras {
445de56a948SPaul Mackerras 	return false;
446de56a948SPaul Mackerras }
447de56a948SPaul Mackerras #endif
448f05ed4d5SPaul Mackerras 
449ad0a048bSAlexander Graf /* Magic register values loaded into r3 and r4 before the 'sc' assembly
450ad0a048bSAlexander Graf  * instruction for the OSI hypercalls */
451ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R3			0x113724FA
452ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R4			0x77810F9B
453ad0a048bSAlexander Graf 
4544e342025SAlexander Graf #define INS_DCBZ			0x7c0007ec
4554e342025SAlexander Graf 
4564e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */
457