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_slb {
284e342025SAlexander Graf 	u64 esid;
294e342025SAlexander Graf 	u64 vsid;
304e342025SAlexander Graf 	u64 orige;
314e342025SAlexander Graf 	u64 origv;
323ed9c6d2SAlexander Graf 	bool valid	: 1;
333ed9c6d2SAlexander Graf 	bool Ks		: 1;
343ed9c6d2SAlexander Graf 	bool Kp		: 1;
353ed9c6d2SAlexander Graf 	bool nx		: 1;
363ed9c6d2SAlexander Graf 	bool large	: 1;	/* PTEs are 16MB */
373ed9c6d2SAlexander Graf 	bool tb		: 1;	/* 1TB segment */
383ed9c6d2SAlexander Graf 	bool class	: 1;
394e342025SAlexander Graf };
404e342025SAlexander Graf 
414e342025SAlexander Graf struct kvmppc_sr {
424e342025SAlexander Graf 	u32 raw;
434e342025SAlexander Graf 	u32 vsid;
443ed9c6d2SAlexander Graf 	bool Ks		: 1;
453ed9c6d2SAlexander Graf 	bool Kp		: 1;
463ed9c6d2SAlexander Graf 	bool nx		: 1;
473ed9c6d2SAlexander Graf 	bool valid	: 1;
484e342025SAlexander Graf };
494e342025SAlexander Graf 
504e342025SAlexander Graf struct kvmppc_bat {
51e15a1137SAlexander Graf 	u64 raw;
524e342025SAlexander Graf 	u32 bepi;
534e342025SAlexander Graf 	u32 bepi_mask;
544e342025SAlexander Graf 	u32 brpn;
554e342025SAlexander Graf 	u8 wimg;
564e342025SAlexander Graf 	u8 pp;
573ed9c6d2SAlexander Graf 	bool vs		: 1;
583ed9c6d2SAlexander Graf 	bool vp		: 1;
594e342025SAlexander Graf };
604e342025SAlexander Graf 
614e342025SAlexander Graf struct kvmppc_sid_map {
624e342025SAlexander Graf 	u64 guest_vsid;
634e342025SAlexander Graf 	u64 guest_esid;
644e342025SAlexander Graf 	u64 host_vsid;
653ed9c6d2SAlexander Graf 	bool valid	: 1;
664e342025SAlexander Graf };
674e342025SAlexander Graf 
684e342025SAlexander Graf #define SID_MAP_BITS    9
694e342025SAlexander Graf #define SID_MAP_NUM     (1 << SID_MAP_BITS)
704e342025SAlexander Graf #define SID_MAP_MASK    (SID_MAP_NUM - 1)
714e342025SAlexander Graf 
724e342025SAlexander Graf struct kvmppc_vcpu_book3s {
734e342025SAlexander Graf 	struct kvm_vcpu vcpu;
74c7f38f46SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *shadow_vcpu;
754e342025SAlexander Graf 	struct kvmppc_sid_map sid_map[SID_MAP_NUM];
764e342025SAlexander Graf 	struct kvmppc_slb slb[64];
774e342025SAlexander Graf 	struct {
784e342025SAlexander Graf 		u64 esid;
794e342025SAlexander Graf 		u64 vsid;
804e342025SAlexander Graf 	} slb_shadow[64];
814e342025SAlexander Graf 	u8 slb_shadow_max;
824e342025SAlexander Graf 	struct kvmppc_sr sr[16];
834e342025SAlexander Graf 	struct kvmppc_bat ibat[8];
844e342025SAlexander Graf 	struct kvmppc_bat dbat[8];
854e342025SAlexander Graf 	u64 hid[6];
86d6d549b2SAlexander Graf 	u64 gqr[8];
874e342025SAlexander Graf 	int slb_nr;
88c8027f16SAlexander Graf 	u32 dsisr;
894e342025SAlexander Graf 	u64 sdr1;
904e342025SAlexander Graf 	u64 hior;
914e342025SAlexander Graf 	u64 msr_mask;
924e342025SAlexander Graf 	u64 vsid_first;
934e342025SAlexander Graf 	u64 vsid_next;
944e342025SAlexander Graf 	u64 vsid_max;
954e342025SAlexander Graf 	int context_id;
9625a8a02dSAlexander Graf 	ulong prog_flags; /* flags to inject when giving a 700 trap */
974e342025SAlexander Graf };
984e342025SAlexander Graf 
994e342025SAlexander Graf #define CONTEXT_HOST		0
1004e342025SAlexander Graf #define CONTEXT_GUEST		1
1014e342025SAlexander Graf #define CONTEXT_GUEST_END	2
1024e342025SAlexander Graf 
1035a1b419fSAlexander Graf #define VSID_REAL_DR	0x7ffffffffff00000ULL
1045a1b419fSAlexander Graf #define VSID_REAL_IR	0x7fffffffffe00000ULL
1055a1b419fSAlexander Graf #define VSID_SPLIT_MASK	0x7fffffffffe00000ULL
1065a1b419fSAlexander Graf #define VSID_REAL	0x7fffffffffc00000ULL
1075a1b419fSAlexander Graf #define VSID_BAT	0x7fffffffffb00000ULL
1085a1b419fSAlexander Graf #define VSID_PR		0x8000000000000000ULL
1094e342025SAlexander Graf 
1104e342025SAlexander Graf extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, u64 ea, u64 ea_mask);
1114e342025SAlexander Graf extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
1124e342025SAlexander Graf extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, u64 pa_start, u64 pa_end);
1134e342025SAlexander Graf extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
1144e342025SAlexander Graf extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
1154e342025SAlexander Graf extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
1164e342025SAlexander Graf extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
1174e342025SAlexander Graf extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
1184e342025SAlexander Graf extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
1194e342025SAlexander Graf extern struct kvmppc_pte *kvmppc_mmu_find_pte(struct kvm_vcpu *vcpu, u64 ea, bool data);
1205467a97dSAlexander Graf extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1215467a97dSAlexander Graf extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
1224e342025SAlexander Graf extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
123e15a1137SAlexander Graf extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
124e15a1137SAlexander Graf 			   bool upper, u32 val);
125aba3bd7fSAlexander Graf extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
126831317b6SAlexander Graf extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
1274e342025SAlexander Graf 
1284e342025SAlexander Graf extern u32 kvmppc_trampoline_lowmem;
1294e342025SAlexander Graf extern u32 kvmppc_trampoline_enter;
130021ec9c6SAlexander Graf extern void kvmppc_rmcall(ulong srr0, ulong srr1);
131d5e52813SAlexander Graf extern void kvmppc_load_up_fpu(void);
132d5e52813SAlexander Graf extern void kvmppc_load_up_altivec(void);
133d5e52813SAlexander Graf extern void kvmppc_load_up_vsx(void);
134ca7f4203SAlexander Graf extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
135ca7f4203SAlexander Graf extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
1364e342025SAlexander Graf 
1374e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
1384e342025SAlexander Graf {
1394e342025SAlexander Graf 	return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
1404e342025SAlexander Graf }
1414e342025SAlexander Graf 
1424e342025SAlexander Graf static inline ulong dsisr(void)
1434e342025SAlexander Graf {
1444e342025SAlexander Graf 	ulong r;
1454e342025SAlexander Graf 	asm ( "mfdsisr %0 " : "=r" (r) );
1464e342025SAlexander Graf 	return r;
1474e342025SAlexander Graf }
1484e342025SAlexander Graf 
1494e342025SAlexander Graf extern void kvm_return_point(void);
150c7f38f46SAlexander Graf static inline struct kvmppc_book3s_shadow_vcpu *to_svcpu(struct kvm_vcpu *vcpu);
151c7f38f46SAlexander Graf 
152c7f38f46SAlexander Graf static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
153c7f38f46SAlexander Graf {
154c7f38f46SAlexander Graf 	if ( num < 14 ) {
155c7f38f46SAlexander Graf 		to_svcpu(vcpu)->gpr[num] = val;
156c7f38f46SAlexander Graf 		to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
157c7f38f46SAlexander Graf 	} else
158c7f38f46SAlexander Graf 		vcpu->arch.gpr[num] = val;
159c7f38f46SAlexander Graf }
160c7f38f46SAlexander Graf 
161c7f38f46SAlexander Graf static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
162c7f38f46SAlexander Graf {
163c7f38f46SAlexander Graf 	if ( num < 14 )
164c7f38f46SAlexander Graf 		return to_svcpu(vcpu)->gpr[num];
165c7f38f46SAlexander Graf 	else
166c7f38f46SAlexander Graf 		return vcpu->arch.gpr[num];
167c7f38f46SAlexander Graf }
168c7f38f46SAlexander Graf 
169c7f38f46SAlexander Graf static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
170c7f38f46SAlexander Graf {
171c7f38f46SAlexander Graf 	to_svcpu(vcpu)->cr = val;
172c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->cr = val;
173c7f38f46SAlexander Graf }
174c7f38f46SAlexander Graf 
175c7f38f46SAlexander Graf static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
176c7f38f46SAlexander Graf {
177c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->cr;
178c7f38f46SAlexander Graf }
179c7f38f46SAlexander Graf 
180c7f38f46SAlexander Graf static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
181c7f38f46SAlexander Graf {
182c7f38f46SAlexander Graf 	to_svcpu(vcpu)->xer = val;
183c7f38f46SAlexander Graf 	to_book3s(vcpu)->shadow_vcpu->xer = val;
184c7f38f46SAlexander Graf }
185c7f38f46SAlexander Graf 
186c7f38f46SAlexander Graf static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
187c7f38f46SAlexander Graf {
188c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->xer;
189c7f38f46SAlexander Graf }
190c7f38f46SAlexander Graf 
191c7f38f46SAlexander Graf static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
192c7f38f46SAlexander Graf {
193c7f38f46SAlexander Graf 	to_svcpu(vcpu)->ctr = val;
194c7f38f46SAlexander Graf }
195c7f38f46SAlexander Graf 
196c7f38f46SAlexander Graf static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
197c7f38f46SAlexander Graf {
198c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->ctr;
199c7f38f46SAlexander Graf }
200c7f38f46SAlexander Graf 
201c7f38f46SAlexander Graf static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
202c7f38f46SAlexander Graf {
203c7f38f46SAlexander Graf 	to_svcpu(vcpu)->lr = val;
204c7f38f46SAlexander Graf }
205c7f38f46SAlexander Graf 
206c7f38f46SAlexander Graf static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
207c7f38f46SAlexander Graf {
208c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->lr;
209c7f38f46SAlexander Graf }
210c7f38f46SAlexander Graf 
211c7f38f46SAlexander Graf static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
212c7f38f46SAlexander Graf {
213c7f38f46SAlexander Graf 	to_svcpu(vcpu)->pc = val;
214c7f38f46SAlexander Graf }
215c7f38f46SAlexander Graf 
216c7f38f46SAlexander Graf static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
217c7f38f46SAlexander Graf {
218c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->pc;
219c7f38f46SAlexander Graf }
220c7f38f46SAlexander Graf 
221c7f38f46SAlexander Graf static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
222c7f38f46SAlexander Graf {
223c7f38f46SAlexander Graf 	ulong pc = kvmppc_get_pc(vcpu);
224c7f38f46SAlexander Graf 	struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
225c7f38f46SAlexander Graf 
226c7f38f46SAlexander Graf 	/* Load the instruction manually if it failed to do so in the
227c7f38f46SAlexander Graf 	 * exit path */
228c7f38f46SAlexander Graf 	if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
229c7f38f46SAlexander Graf 		kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
230c7f38f46SAlexander Graf 
231c7f38f46SAlexander Graf 	return svcpu->last_inst;
232c7f38f46SAlexander Graf }
233c7f38f46SAlexander Graf 
234c7f38f46SAlexander Graf static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
235c7f38f46SAlexander Graf {
236c7f38f46SAlexander Graf 	return to_svcpu(vcpu)->fault_dar;
237c7f38f46SAlexander Graf }
2384e342025SAlexander Graf 
239ad0a048bSAlexander Graf /* Magic register values loaded into r3 and r4 before the 'sc' assembly
240ad0a048bSAlexander Graf  * instruction for the OSI hypercalls */
241ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R3			0x113724FA
242ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R4			0x77810F9B
243ad0a048bSAlexander Graf 
2444e342025SAlexander Graf #define INS_DCBZ			0x7c0007ec
2454e342025SAlexander Graf 
246c7f38f46SAlexander Graf /* Also add subarch specific defines */
247c7f38f46SAlexander Graf 
248c7f38f46SAlexander Graf #ifdef CONFIG_PPC_BOOK3S_32
249c7f38f46SAlexander Graf #include <asm/kvm_book3s_32.h>
250c7f38f46SAlexander Graf #else
251c7f38f46SAlexander Graf #include <asm/kvm_book3s_64.h>
252c7f38f46SAlexander Graf #endif
253c7f38f46SAlexander Graf 
2544e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */
255