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;
625524a27dSAneesh Kumar K.V 	u64 host_vpn;
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;
84b0a94d4eSPaul Mackerras 	u64 purr_offset;
85b0a94d4eSPaul Mackerras 	u64 spurr_offset;
868b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_32
878b6db3bcSAlexander Graf 	u32 vsid_pool[VSID_POOL_SIZE];
88ffe36492SBenjamin Herrenschmidt 	u32 vsid_next;
898b6db3bcSAlexander Graf #else
90ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_first;
91ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_max;
92ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_next;
938b6db3bcSAlexander Graf #endif
948b6db3bcSAlexander Graf 	int context_id[SID_CONTEXTS];
95c4befc58SPaul Mackerras 
961022fc3dSAlexander Graf 	bool hior_explicit;		/* HIOR is set by ioctl, not PVR */
971022fc3dSAlexander Graf 
98c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
99c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
100c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
101c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
102c4befc58SPaul Mackerras 	int hpte_cache_count;
103c4befc58SPaul Mackerras 	spinlock_t mmu_lock;
1044e342025SAlexander Graf };
1054e342025SAlexander Graf 
1064e342025SAlexander Graf #define CONTEXT_HOST		0
1074e342025SAlexander Graf #define CONTEXT_GUEST		1
1084e342025SAlexander Graf #define CONTEXT_GUEST_END	2
1094e342025SAlexander Graf 
1100f296829SPaul Mackerras #define VSID_REAL	0x0fffffffffc00000ULL
1110f296829SPaul Mackerras #define VSID_BAT	0x0fffffffffb00000ULL
1120f296829SPaul Mackerras #define VSID_1T		0x1000000000000000ULL
113f7bc74e1SAlexander Graf #define VSID_REAL_DR	0x2000000000000000ULL
114f7bc74e1SAlexander Graf #define VSID_REAL_IR	0x4000000000000000ULL
1155a1b419fSAlexander Graf #define VSID_PR		0x8000000000000000ULL
1164e342025SAlexander Graf 
117af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
1184e342025SAlexander Graf extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
119af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
1204e342025SAlexander Graf extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
121f05ed4d5SPaul Mackerras extern void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr);
1224e342025SAlexander Graf extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
1234e342025SAlexander Graf extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
124de56a948SPaul Mackerras extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
1254e342025SAlexander Graf extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
1264e342025SAlexander Graf extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
1270f296829SPaul Mackerras extern void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong eaddr, ulong seg_size);
1284e342025SAlexander Graf extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
129697d3899SPaul Mackerras extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run,
130697d3899SPaul Mackerras 			struct kvm_vcpu *vcpu, unsigned long addr,
131697d3899SPaul Mackerras 			unsigned long status);
132697d3899SPaul Mackerras extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr,
133697d3899SPaul Mackerras 			unsigned long slb_v, unsigned long valid);
134fef093beSAlexander Graf 
135fef093beSAlexander Graf extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
136fef093beSAlexander Graf extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
137fef093beSAlexander Graf extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
138fef093beSAlexander Graf extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
139fef093beSAlexander Graf extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
140fef093beSAlexander Graf extern int kvmppc_mmu_hpte_sysinit(void);
141fef093beSAlexander Graf extern void kvmppc_mmu_hpte_sysexit(void);
142de56a948SPaul Mackerras extern int kvmppc_mmu_hv_init(void);
143fef093beSAlexander Graf 
1445467a97dSAlexander Graf extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1455467a97dSAlexander Graf extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1464e342025SAlexander Graf extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
147bc5ad3f3SBenjamin Herrenschmidt extern void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
148bc5ad3f3SBenjamin Herrenschmidt 					  unsigned int vec);
149de56a948SPaul Mackerras extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
150e15a1137SAlexander Graf extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
151e15a1137SAlexander Graf 			   bool upper, u32 val);
152aba3bd7fSAlexander Graf extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
153831317b6SAlexander Graf extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
154e8508940SAlexander Graf extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
155342d3db7SPaul Mackerras extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
156342d3db7SPaul Mackerras 			unsigned long *rmap, long pte_index, int realmode);
157342d3db7SPaul Mackerras extern void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep,
158342d3db7SPaul Mackerras 			unsigned long pte_index);
15955514893SPaul Mackerras void kvmppc_clear_ref_hpte(struct kvm *kvm, unsigned long *hptep,
16055514893SPaul Mackerras 			unsigned long pte_index);
16193e60249SPaul Mackerras extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
16293e60249SPaul Mackerras 			unsigned long *nb_ret);
163c35635efSPaul Mackerras extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr,
164c35635efSPaul Mackerras 			unsigned long gpa, bool dirty);
165c77162deSPaul Mackerras extern long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
166c77162deSPaul Mackerras 			long pte_index, unsigned long pteh, unsigned long ptel);
1677ed661bfSPaul Mackerras extern long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
1687ed661bfSPaul Mackerras 			long pte_index, unsigned long pteh, unsigned long ptel,
1697ed661bfSPaul Mackerras 			pgd_t *pgdir, bool realmode, unsigned long *idx_ret);
1706b445ad4SPaul Mackerras extern long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
1716b445ad4SPaul Mackerras 			unsigned long pte_index, unsigned long avpn,
1726b445ad4SPaul Mackerras 			unsigned long *hpret);
17382ed3616SPaul Mackerras extern long kvmppc_hv_get_dirty_log(struct kvm *kvm,
174dfe49dbdSPaul Mackerras 			struct kvm_memory_slot *memslot, unsigned long *map);
175a0144e2aSPaul Mackerras extern void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr,
176a0144e2aSPaul Mackerras 			unsigned long mask);
1774e342025SAlexander Graf 
17802143947SPaul Mackerras extern void kvmppc_entry_trampoline(void);
179de56a948SPaul Mackerras extern void kvmppc_hv_entry_trampoline(void);
180d5e52813SAlexander Graf extern void kvmppc_load_up_fpu(void);
181d5e52813SAlexander Graf extern void kvmppc_load_up_altivec(void);
182d5e52813SAlexander Graf extern void kvmppc_load_up_vsx(void);
183ca7f4203SAlexander Graf extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
184ca7f4203SAlexander Graf extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
1850254f074SAlexander Graf extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
1864e342025SAlexander Graf 
1874e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
1884e342025SAlexander Graf {
1894e342025SAlexander Graf 	return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
1904e342025SAlexander Graf }
1914e342025SAlexander Graf 
192de56a948SPaul Mackerras extern void kvm_return_point(void);
193de56a948SPaul Mackerras 
194de56a948SPaul Mackerras /* Also add subarch specific defines */
195de56a948SPaul Mackerras 
196de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
197de56a948SPaul Mackerras #include <asm/kvm_book3s_32.h>
198de56a948SPaul Mackerras #endif
199de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
200de56a948SPaul Mackerras #include <asm/kvm_book3s_64.h>
201de56a948SPaul Mackerras #endif
202de56a948SPaul Mackerras 
203de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_PR
204de56a948SPaul Mackerras 
205f05ed4d5SPaul Mackerras static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
206f05ed4d5SPaul Mackerras {
207f05ed4d5SPaul Mackerras 	return to_book3s(vcpu)->hior;
208f05ed4d5SPaul Mackerras }
209f05ed4d5SPaul Mackerras 
210f05ed4d5SPaul Mackerras static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
211f05ed4d5SPaul Mackerras 			unsigned long pending_now, unsigned long old_pending)
212f05ed4d5SPaul Mackerras {
213f05ed4d5SPaul Mackerras 	if (pending_now)
214f05ed4d5SPaul Mackerras 		vcpu->arch.shared->int_pending = 1;
215f05ed4d5SPaul Mackerras 	else if (old_pending)
216f05ed4d5SPaul Mackerras 		vcpu->arch.shared->int_pending = 0;
217f05ed4d5SPaul Mackerras }
218f05ed4d5SPaul Mackerras 
219c7f38f46SAlexander Graf static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
220c7f38f46SAlexander Graf {
221c7f38f46SAlexander Graf 	if ( num < 14 ) {
222468a12c2SAlexander Graf 		struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
223468a12c2SAlexander Graf 		svcpu->gpr[num] = val;
224468a12c2SAlexander Graf 		svcpu_put(svcpu);
225c7f38f46SAlexander Graf 		to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
226c7f38f46SAlexander Graf 	} else
227c7f38f46SAlexander Graf 		vcpu->arch.gpr[num] = val;
228c7f38f46SAlexander Graf }
229c7f38f46SAlexander Graf 
230c7f38f46SAlexander Graf static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
231c7f38f46SAlexander Graf {
232468a12c2SAlexander Graf 	if ( num < 14 ) {
233468a12c2SAlexander Graf 		struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
234468a12c2SAlexander Graf 		ulong r = svcpu->gpr[num];
235468a12c2SAlexander Graf 		svcpu_put(svcpu);
236468a12c2SAlexander Graf 		return r;
237468a12c2SAlexander Graf 	} else
238c7f38f46SAlexander Graf 		return vcpu->arch.gpr[num];
239c7f38f46SAlexander Graf }
240c7f38f46SAlexander Graf 
241c7f38f46SAlexander Graf static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
242c7f38f46SAlexander Graf {
243468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
244468a12c2SAlexander Graf 	svcpu->cr = val;
245468a12c2SAlexander Graf 	svcpu_put(svcpu);
246c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->cr = val;
247c7f38f46SAlexander Graf }
248c7f38f46SAlexander Graf 
249c7f38f46SAlexander Graf static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
250c7f38f46SAlexander Graf {
251468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
252468a12c2SAlexander Graf 	u32 r;
253468a12c2SAlexander Graf 	r = svcpu->cr;
254468a12c2SAlexander Graf 	svcpu_put(svcpu);
255468a12c2SAlexander Graf 	return r;
256c7f38f46SAlexander Graf }
257c7f38f46SAlexander Graf 
258c7f38f46SAlexander Graf static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
259c7f38f46SAlexander Graf {
260468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
261468a12c2SAlexander Graf 	svcpu->xer = val;
262c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->xer = val;
263468a12c2SAlexander Graf 	svcpu_put(svcpu);
264c7f38f46SAlexander Graf }
265c7f38f46SAlexander Graf 
266c7f38f46SAlexander Graf static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
267c7f38f46SAlexander Graf {
268468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
269468a12c2SAlexander Graf 	u32 r;
270468a12c2SAlexander Graf 	r = svcpu->xer;
271468a12c2SAlexander Graf 	svcpu_put(svcpu);
272468a12c2SAlexander Graf 	return r;
273c7f38f46SAlexander Graf }
274c7f38f46SAlexander Graf 
275c7f38f46SAlexander Graf static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
276c7f38f46SAlexander Graf {
277468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
278468a12c2SAlexander Graf 	svcpu->ctr = val;
279468a12c2SAlexander Graf 	svcpu_put(svcpu);
280c7f38f46SAlexander Graf }
281c7f38f46SAlexander Graf 
282c7f38f46SAlexander Graf static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
283c7f38f46SAlexander Graf {
284468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
285468a12c2SAlexander Graf 	ulong r;
286468a12c2SAlexander Graf 	r = svcpu->ctr;
287468a12c2SAlexander Graf 	svcpu_put(svcpu);
288468a12c2SAlexander Graf 	return r;
289c7f38f46SAlexander Graf }
290c7f38f46SAlexander Graf 
291c7f38f46SAlexander Graf static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
292c7f38f46SAlexander Graf {
293468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
294468a12c2SAlexander Graf 	svcpu->lr = val;
295468a12c2SAlexander Graf 	svcpu_put(svcpu);
296c7f38f46SAlexander Graf }
297c7f38f46SAlexander Graf 
298c7f38f46SAlexander Graf static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
299c7f38f46SAlexander Graf {
300468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
301468a12c2SAlexander Graf 	ulong r;
302468a12c2SAlexander Graf 	r = svcpu->lr;
303468a12c2SAlexander Graf 	svcpu_put(svcpu);
304468a12c2SAlexander Graf 	return r;
305c7f38f46SAlexander Graf }
306c7f38f46SAlexander Graf 
307c7f38f46SAlexander Graf static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
308c7f38f46SAlexander Graf {
309468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
310468a12c2SAlexander Graf 	svcpu->pc = val;
311468a12c2SAlexander Graf 	svcpu_put(svcpu);
312c7f38f46SAlexander Graf }
313c7f38f46SAlexander Graf 
314c7f38f46SAlexander Graf static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
315c7f38f46SAlexander Graf {
316468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
317468a12c2SAlexander Graf 	ulong r;
318468a12c2SAlexander Graf 	r = svcpu->pc;
319468a12c2SAlexander Graf 	svcpu_put(svcpu);
320468a12c2SAlexander Graf 	return r;
321c7f38f46SAlexander Graf }
322c7f38f46SAlexander Graf 
323c7f38f46SAlexander Graf static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
324c7f38f46SAlexander Graf {
325c7f38f46SAlexander Graf 	ulong pc = kvmppc_get_pc(vcpu);
326468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
327468a12c2SAlexander Graf 	u32 r;
328c7f38f46SAlexander Graf 
329c7f38f46SAlexander Graf 	/* Load the instruction manually if it failed to do so in the
330c7f38f46SAlexander Graf 	 * exit path */
331c7f38f46SAlexander Graf 	if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
332c7f38f46SAlexander Graf 		kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
333c7f38f46SAlexander Graf 
334468a12c2SAlexander Graf 	r = svcpu->last_inst;
335468a12c2SAlexander Graf 	svcpu_put(svcpu);
336468a12c2SAlexander Graf 	return r;
337c7f38f46SAlexander Graf }
338c7f38f46SAlexander Graf 
3398b23de29SPaul Mackerras /*
3408b23de29SPaul Mackerras  * Like kvmppc_get_last_inst(), but for fetching a sc instruction.
3418b23de29SPaul Mackerras  * Because the sc instruction sets SRR0 to point to the following
3428b23de29SPaul Mackerras  * instruction, we have to fetch from pc - 4.
3438b23de29SPaul Mackerras  */
3448b23de29SPaul Mackerras static inline u32 kvmppc_get_last_sc(struct kvm_vcpu *vcpu)
3458b23de29SPaul Mackerras {
3468b23de29SPaul Mackerras 	ulong pc = kvmppc_get_pc(vcpu) - 4;
3478b23de29SPaul Mackerras 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
3488b23de29SPaul Mackerras 	u32 r;
3498b23de29SPaul Mackerras 
3508b23de29SPaul Mackerras 	/* Load the instruction manually if it failed to do so in the
3518b23de29SPaul Mackerras 	 * exit path */
3528b23de29SPaul Mackerras 	if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
3538b23de29SPaul Mackerras 		kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
3548b23de29SPaul Mackerras 
3558b23de29SPaul Mackerras 	r = svcpu->last_inst;
3568b23de29SPaul Mackerras 	svcpu_put(svcpu);
3578b23de29SPaul Mackerras 	return r;
3588b23de29SPaul Mackerras }
3598b23de29SPaul Mackerras 
360c7f38f46SAlexander Graf static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
361c7f38f46SAlexander Graf {
362468a12c2SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
363468a12c2SAlexander Graf 	ulong r;
364468a12c2SAlexander Graf 	r = svcpu->fault_dar;
365468a12c2SAlexander Graf 	svcpu_put(svcpu);
366468a12c2SAlexander Graf 	return r;
367c7f38f46SAlexander Graf }
3684e342025SAlexander Graf 
369f05ed4d5SPaul Mackerras static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
370f05ed4d5SPaul Mackerras {
371f05ed4d5SPaul Mackerras 	ulong crit_raw = vcpu->arch.shared->critical;
372f05ed4d5SPaul Mackerras 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
373f05ed4d5SPaul Mackerras 	bool crit;
374f05ed4d5SPaul Mackerras 
375f05ed4d5SPaul Mackerras 	/* Truncate crit indicators in 32 bit mode */
376f05ed4d5SPaul Mackerras 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
377f05ed4d5SPaul Mackerras 		crit_raw &= 0xffffffff;
378f05ed4d5SPaul Mackerras 		crit_r1 &= 0xffffffff;
379f05ed4d5SPaul Mackerras 	}
380f05ed4d5SPaul Mackerras 
381f05ed4d5SPaul Mackerras 	/* Critical section when crit == r1 */
382f05ed4d5SPaul Mackerras 	crit = (crit_raw == crit_r1);
383f05ed4d5SPaul Mackerras 	/* ... and we're in supervisor mode */
384f05ed4d5SPaul Mackerras 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
385f05ed4d5SPaul Mackerras 
386f05ed4d5SPaul Mackerras 	return crit;
387f05ed4d5SPaul Mackerras }
388de56a948SPaul Mackerras #else /* CONFIG_KVM_BOOK3S_PR */
389de56a948SPaul Mackerras 
390de56a948SPaul Mackerras static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
391de56a948SPaul Mackerras {
392de56a948SPaul Mackerras 	return 0;
393de56a948SPaul Mackerras }
394de56a948SPaul Mackerras 
395de56a948SPaul Mackerras static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
396de56a948SPaul Mackerras 			unsigned long pending_now, unsigned long old_pending)
397de56a948SPaul Mackerras {
398de56a948SPaul Mackerras }
399de56a948SPaul Mackerras 
400de56a948SPaul Mackerras static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
401de56a948SPaul Mackerras {
402de56a948SPaul Mackerras 	vcpu->arch.gpr[num] = val;
403de56a948SPaul Mackerras }
404de56a948SPaul Mackerras 
405de56a948SPaul Mackerras static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
406de56a948SPaul Mackerras {
407de56a948SPaul Mackerras 	return vcpu->arch.gpr[num];
408de56a948SPaul Mackerras }
409de56a948SPaul Mackerras 
410de56a948SPaul Mackerras static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
411de56a948SPaul Mackerras {
412de56a948SPaul Mackerras 	vcpu->arch.cr = val;
413de56a948SPaul Mackerras }
414de56a948SPaul Mackerras 
415de56a948SPaul Mackerras static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
416de56a948SPaul Mackerras {
417de56a948SPaul Mackerras 	return vcpu->arch.cr;
418de56a948SPaul Mackerras }
419de56a948SPaul Mackerras 
420de56a948SPaul Mackerras static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
421de56a948SPaul Mackerras {
422de56a948SPaul Mackerras 	vcpu->arch.xer = val;
423de56a948SPaul Mackerras }
424de56a948SPaul Mackerras 
425de56a948SPaul Mackerras static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
426de56a948SPaul Mackerras {
427de56a948SPaul Mackerras 	return vcpu->arch.xer;
428de56a948SPaul Mackerras }
429de56a948SPaul Mackerras 
430de56a948SPaul Mackerras static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
431de56a948SPaul Mackerras {
432de56a948SPaul Mackerras 	vcpu->arch.ctr = val;
433de56a948SPaul Mackerras }
434de56a948SPaul Mackerras 
435de56a948SPaul Mackerras static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
436de56a948SPaul Mackerras {
437de56a948SPaul Mackerras 	return vcpu->arch.ctr;
438de56a948SPaul Mackerras }
439de56a948SPaul Mackerras 
440de56a948SPaul Mackerras static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
441de56a948SPaul Mackerras {
442de56a948SPaul Mackerras 	vcpu->arch.lr = val;
443de56a948SPaul Mackerras }
444de56a948SPaul Mackerras 
445de56a948SPaul Mackerras static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
446de56a948SPaul Mackerras {
447de56a948SPaul Mackerras 	return vcpu->arch.lr;
448de56a948SPaul Mackerras }
449de56a948SPaul Mackerras 
450de56a948SPaul Mackerras static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
451de56a948SPaul Mackerras {
452de56a948SPaul Mackerras 	vcpu->arch.pc = val;
453de56a948SPaul Mackerras }
454de56a948SPaul Mackerras 
455de56a948SPaul Mackerras static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
456de56a948SPaul Mackerras {
457de56a948SPaul Mackerras 	return vcpu->arch.pc;
458de56a948SPaul Mackerras }
459de56a948SPaul Mackerras 
460de56a948SPaul Mackerras static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
461de56a948SPaul Mackerras {
462de56a948SPaul Mackerras 	ulong pc = kvmppc_get_pc(vcpu);
463de56a948SPaul Mackerras 
464de56a948SPaul Mackerras 	/* Load the instruction manually if it failed to do so in the
465de56a948SPaul Mackerras 	 * exit path */
466de56a948SPaul Mackerras 	if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
467de56a948SPaul Mackerras 		kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
468de56a948SPaul Mackerras 
469de56a948SPaul Mackerras 	return vcpu->arch.last_inst;
470de56a948SPaul Mackerras }
471de56a948SPaul Mackerras 
4728b23de29SPaul Mackerras /*
4738b23de29SPaul Mackerras  * Like kvmppc_get_last_inst(), but for fetching a sc instruction.
4748b23de29SPaul Mackerras  * Because the sc instruction sets SRR0 to point to the following
4758b23de29SPaul Mackerras  * instruction, we have to fetch from pc - 4.
4768b23de29SPaul Mackerras  */
4778b23de29SPaul Mackerras static inline u32 kvmppc_get_last_sc(struct kvm_vcpu *vcpu)
4788b23de29SPaul Mackerras {
4798b23de29SPaul Mackerras 	ulong pc = kvmppc_get_pc(vcpu) - 4;
4808b23de29SPaul Mackerras 
4818b23de29SPaul Mackerras 	/* Load the instruction manually if it failed to do so in the
4828b23de29SPaul Mackerras 	 * exit path */
4838b23de29SPaul Mackerras 	if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
4848b23de29SPaul Mackerras 		kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
4858b23de29SPaul Mackerras 
4868b23de29SPaul Mackerras 	return vcpu->arch.last_inst;
4878b23de29SPaul Mackerras }
4888b23de29SPaul Mackerras 
489de56a948SPaul Mackerras static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
490de56a948SPaul Mackerras {
491de56a948SPaul Mackerras 	return vcpu->arch.fault_dar;
492de56a948SPaul Mackerras }
493de56a948SPaul Mackerras 
494de56a948SPaul Mackerras static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
495de56a948SPaul Mackerras {
496de56a948SPaul Mackerras 	return false;
497de56a948SPaul Mackerras }
498de56a948SPaul Mackerras #endif
499f05ed4d5SPaul Mackerras 
500ad0a048bSAlexander Graf /* Magic register values loaded into r3 and r4 before the 'sc' assembly
501ad0a048bSAlexander Graf  * instruction for the OSI hypercalls */
502ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R3			0x113724FA
503ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R4			0x77810F9B
504ad0a048bSAlexander Graf 
5054e342025SAlexander Graf #define INS_DCBZ			0x7c0007ec
5068c32a2eaSBharat Bhushan /* TO = 31 for unconditional trap */
5078c32a2eaSBharat Bhushan #define INS_TW				0x7fe00008
5084e342025SAlexander Graf 
509043cc4d7SScott Wood /* LPIDs we support with this build -- runtime limit may be lower */
510043cc4d7SScott Wood #define KVMPPC_NR_LPIDS			(LPID_RSVD + 1)
511043cc4d7SScott Wood 
5124e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */
513