1d94d71cbSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
24e342025SAlexander Graf /*
34e342025SAlexander Graf  *
44e342025SAlexander Graf  * Copyright SUSE Linux Products GmbH 2009
54e342025SAlexander Graf  *
64e342025SAlexander Graf  * Authors: Alexander Graf <agraf@suse.de>
74e342025SAlexander Graf  */
84e342025SAlexander Graf 
94e342025SAlexander Graf #ifndef __ASM_KVM_BOOK3S_H__
104e342025SAlexander Graf #define __ASM_KVM_BOOK3S_H__
114e342025SAlexander Graf 
124e342025SAlexander Graf #include <linux/types.h>
134e342025SAlexander Graf #include <linux/kvm_host.h>
142191d657SAlexander Graf #include <asm/kvm_book3s_asm.h>
154e342025SAlexander Graf 
164e342025SAlexander Graf struct kvmppc_bat {
17e15a1137SAlexander Graf 	u64 raw;
184e342025SAlexander Graf 	u32 bepi;
194e342025SAlexander Graf 	u32 bepi_mask;
204e342025SAlexander Graf 	u32 brpn;
214e342025SAlexander Graf 	u8 wimg;
224e342025SAlexander Graf 	u8 pp;
233ed9c6d2SAlexander Graf 	bool vs		: 1;
243ed9c6d2SAlexander Graf 	bool vp		: 1;
254e342025SAlexander Graf };
264e342025SAlexander Graf 
274e342025SAlexander Graf struct kvmppc_sid_map {
284e342025SAlexander Graf 	u64 guest_vsid;
294e342025SAlexander Graf 	u64 guest_esid;
304e342025SAlexander Graf 	u64 host_vsid;
313ed9c6d2SAlexander Graf 	bool valid	: 1;
324e342025SAlexander Graf };
334e342025SAlexander Graf 
344e342025SAlexander Graf #define SID_MAP_BITS    9
354e342025SAlexander Graf #define SID_MAP_NUM     (1 << SID_MAP_BITS)
364e342025SAlexander Graf #define SID_MAP_MASK    (SID_MAP_NUM - 1)
374e342025SAlexander Graf 
388b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_64
398b6db3bcSAlexander Graf #define SID_CONTEXTS	1
408b6db3bcSAlexander Graf #else
418b6db3bcSAlexander Graf #define SID_CONTEXTS	128
428b6db3bcSAlexander Graf #define VSID_POOL_SIZE	(SID_CONTEXTS * 16)
438b6db3bcSAlexander Graf #endif
448b6db3bcSAlexander Graf 
45c4befc58SPaul Mackerras struct hpte_cache {
46c4befc58SPaul Mackerras 	struct hlist_node list_pte;
47c4befc58SPaul Mackerras 	struct hlist_node list_pte_long;
48c4befc58SPaul Mackerras 	struct hlist_node list_vpte;
49c4befc58SPaul Mackerras 	struct hlist_node list_vpte_long;
50a4a0f252SPaul Mackerras #ifdef CONFIG_PPC_BOOK3S_64
51a4a0f252SPaul Mackerras 	struct hlist_node list_vpte_64k;
52a4a0f252SPaul Mackerras #endif
53c4befc58SPaul Mackerras 	struct rcu_head rcu_head;
545524a27dSAneesh Kumar K.V 	u64 host_vpn;
55c4befc58SPaul Mackerras 	u64 pfn;
56c4befc58SPaul Mackerras 	ulong slot;
57c4befc58SPaul Mackerras 	struct kvmppc_pte pte;
58c9029c34SPaul Mackerras 	int pagesize;
59c4befc58SPaul Mackerras };
60c4befc58SPaul Mackerras 
61e64fb7e2SSuraj Jitindar Singh /*
62e64fb7e2SSuraj Jitindar Singh  * Struct for a virtual core.
63e64fb7e2SSuraj Jitindar Singh  * Note: entry_exit_map combines a bitmap of threads that have entered
64e64fb7e2SSuraj Jitindar Singh  * in the bottom 8 bits and a bitmap of threads that have exited in the
65e64fb7e2SSuraj Jitindar Singh  * next 8 bits.  This is so that we can atomically set the entry bit
66e64fb7e2SSuraj Jitindar Singh  * iff the exit map is 0 without taking a lock.
67e64fb7e2SSuraj Jitindar Singh  */
68e64fb7e2SSuraj Jitindar Singh struct kvmppc_vcore {
69e64fb7e2SSuraj Jitindar Singh 	int n_runnable;
70e64fb7e2SSuraj Jitindar Singh 	int num_threads;
71e64fb7e2SSuraj Jitindar Singh 	int entry_exit_map;
72e64fb7e2SSuraj Jitindar Singh 	int napping_threads;
73e64fb7e2SSuraj Jitindar Singh 	int first_vcpuid;
74e64fb7e2SSuraj Jitindar Singh 	u16 pcpu;
75e64fb7e2SSuraj Jitindar Singh 	u16 last_cpu;
76e64fb7e2SSuraj Jitindar Singh 	u8 vcore_state;
77e64fb7e2SSuraj Jitindar Singh 	u8 in_guest;
787b5f8272SSuraj Jitindar Singh 	struct kvm_vcpu *runnable_threads[MAX_SMT_THREADS];
79e64fb7e2SSuraj Jitindar Singh 	struct list_head preempt_list;
80e64fb7e2SSuraj Jitindar Singh 	spinlock_t lock;
81da4ad88cSDavidlohr Bueso 	struct rcuwait wait;
82e64fb7e2SSuraj Jitindar Singh 	spinlock_t stoltb_lock;	/* protects stolen_tb and preempt_tb */
83e64fb7e2SSuraj Jitindar Singh 	u64 stolen_tb;
84e64fb7e2SSuraj Jitindar Singh 	u64 preempt_tb;
85e64fb7e2SSuraj Jitindar Singh 	struct kvm_vcpu *runner;
86e64fb7e2SSuraj Jitindar Singh 	struct kvm *kvm;
87e64fb7e2SSuraj Jitindar Singh 	u64 tb_offset;		/* guest timebase - host timebase */
8857b8daa7SPaul Mackerras 	u64 tb_offset_applied;	/* timebase offset currently in force */
89e64fb7e2SSuraj Jitindar Singh 	ulong lpcr;
90e64fb7e2SSuraj Jitindar Singh 	u32 arch_compat;
91e64fb7e2SSuraj Jitindar Singh 	ulong pcr;
92e64fb7e2SSuraj Jitindar Singh 	ulong dpdes;		/* doorbell state (POWER8) */
9388b02cf9SPaul Mackerras 	ulong vtb;		/* virtual timebase */
94e64fb7e2SSuraj Jitindar Singh 	ulong conferring_threads;
950cda69ddSSuraj Jitindar Singh 	unsigned int halt_poll_ns;
967aa15842SPaul Mackerras 	atomic_t online_count;
97e64fb7e2SSuraj Jitindar Singh };
98e64fb7e2SSuraj Jitindar Singh 
994e342025SAlexander Graf struct kvmppc_vcpu_book3s {
1004e342025SAlexander Graf 	struct kvmppc_sid_map sid_map[SID_MAP_NUM];
1014e342025SAlexander Graf 	struct {
1024e342025SAlexander Graf 		u64 esid;
1034e342025SAlexander Graf 		u64 vsid;
1044e342025SAlexander Graf 	} slb_shadow[64];
1054e342025SAlexander Graf 	u8 slb_shadow_max;
1064e342025SAlexander Graf 	struct kvmppc_bat ibat[8];
1074e342025SAlexander Graf 	struct kvmppc_bat dbat[8];
1084e342025SAlexander Graf 	u64 hid[6];
109d6d549b2SAlexander Graf 	u64 gqr[8];
1104e342025SAlexander Graf 	u64 sdr1;
1114e342025SAlexander Graf 	u64 hior;
1124e342025SAlexander Graf 	u64 msr_mask;
11388b02cf9SPaul Mackerras 	u64 vtb;
1148b6db3bcSAlexander Graf #ifdef CONFIG_PPC_BOOK3S_32
1158b6db3bcSAlexander Graf 	u32 vsid_pool[VSID_POOL_SIZE];
116ffe36492SBenjamin Herrenschmidt 	u32 vsid_next;
1178b6db3bcSAlexander Graf #else
118ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_first;
119ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_max;
120ffe36492SBenjamin Herrenschmidt 	u64 proto_vsid_next;
1218b6db3bcSAlexander Graf #endif
1228b6db3bcSAlexander Graf 	int context_id[SID_CONTEXTS];
123c4befc58SPaul Mackerras 
1241022fc3dSAlexander Graf 	bool hior_explicit;		/* HIOR is set by ioctl, not PVR */
1251022fc3dSAlexander Graf 
126c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
127c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
128c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
129c4befc58SPaul Mackerras 	struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
130a4a0f252SPaul Mackerras #ifdef CONFIG_PPC_BOOK3S_64
131a4a0f252SPaul Mackerras 	struct hlist_head hpte_hash_vpte_64k[HPTEG_HASH_NUM_VPTE_64K];
132a4a0f252SPaul Mackerras #endif
133c4befc58SPaul Mackerras 	int hpte_cache_count;
134c4befc58SPaul Mackerras 	spinlock_t mmu_lock;
1354e342025SAlexander Graf };
1364e342025SAlexander Graf 
137c9029c34SPaul Mackerras #define VSID_REAL	0x07ffffffffc00000ULL
138c9029c34SPaul Mackerras #define VSID_BAT	0x07ffffffffb00000ULL
139c9029c34SPaul Mackerras #define VSID_64K	0x0800000000000000ULL
1400f296829SPaul Mackerras #define VSID_1T		0x1000000000000000ULL
141f7bc74e1SAlexander Graf #define VSID_REAL_DR	0x2000000000000000ULL
142f7bc74e1SAlexander Graf #define VSID_REAL_IR	0x4000000000000000ULL
1435a1b419fSAlexander Graf #define VSID_PR		0x8000000000000000ULL
1444e342025SAlexander Graf 
145af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
1464e342025SAlexander Graf extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
147af7b4d10SAlexander Graf extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
1484e342025SAlexander Graf extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
1494e342025SAlexander Graf extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
1504e342025SAlexander Graf extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
151de56a948SPaul Mackerras extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
15293b159b4SPaul Mackerras extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
15393b159b4SPaul Mackerras 			       bool iswrite);
15493b159b4SPaul Mackerras extern void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
1554e342025SAlexander Graf extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
1560f296829SPaul Mackerras extern void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong eaddr, ulong seg_size);
1574e342025SAlexander Graf extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
1588c99d345STianjia Zhang extern int kvmppc_book3s_hv_page_fault(struct kvm_vcpu *vcpu,
1598c99d345STianjia Zhang 			unsigned long addr, unsigned long status);
160697d3899SPaul Mackerras extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr,
161697d3899SPaul Mackerras 			unsigned long slb_v, unsigned long valid);
1628c99d345STianjia Zhang extern int kvmppc_hv_emulate_mmio(struct kvm_vcpu *vcpu,
1635a319350SPaul Mackerras 			unsigned long gpa, gva_t ea, int is_store);
164fef093beSAlexander Graf 
165fef093beSAlexander Graf extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
166fef093beSAlexander Graf extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
167d78bca72SPaul Mackerras extern void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte);
168fef093beSAlexander Graf extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
169fef093beSAlexander Graf extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
170fef093beSAlexander Graf extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
171fef093beSAlexander Graf extern int kvmppc_mmu_hpte_sysinit(void);
172fef093beSAlexander Graf extern void kvmppc_mmu_hpte_sysexit(void);
173de56a948SPaul Mackerras extern int kvmppc_mmu_hv_init(void);
174ae2113a4SPaul Mackerras extern int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hc);
175fef093beSAlexander Graf 
1768c99d345STianjia Zhang extern int kvmppc_book3s_radix_page_fault(struct kvm_vcpu *vcpu,
1775a319350SPaul Mackerras 			unsigned long ea, unsigned long dsisr);
1786ff887b8SSuraj Jitindar Singh extern unsigned long __kvmhv_copy_tofrom_guest_radix(int lpid, int pid,
1796ff887b8SSuraj Jitindar Singh 					gva_t eaddr, void *to, void *from,
1806ff887b8SSuraj Jitindar Singh 					unsigned long n);
181d7b45615SSuraj Jitindar Singh extern long kvmhv_copy_from_guest_radix(struct kvm_vcpu *vcpu, gva_t eaddr,
182d7b45615SSuraj Jitindar Singh 					void *to, unsigned long n);
183d7b45615SSuraj Jitindar Singh extern long kvmhv_copy_to_guest_radix(struct kvm_vcpu *vcpu, gva_t eaddr,
184d7b45615SSuraj Jitindar Singh 				      void *from, unsigned long n);
185fd10be25SSuraj Jitindar Singh extern int kvmppc_mmu_walk_radix_tree(struct kvm_vcpu *vcpu, gva_t eaddr,
186fd10be25SSuraj Jitindar Singh 				      struct kvmppc_pte *gpte, u64 root,
187fd10be25SSuraj Jitindar Singh 				      u64 *pte_ret_p);
1889811c78eSSuraj Jitindar Singh extern int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr,
1899811c78eSSuraj Jitindar Singh 			struct kvmppc_pte *gpte, u64 table,
1909811c78eSSuraj Jitindar Singh 			int table_index, u64 *pte_ret_p);
1919e04ba69SPaul Mackerras extern int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
1929e04ba69SPaul Mackerras 			struct kvmppc_pte *gpte, bool data, bool iswrite);
19390165d3dSSuraj Jitindar Singh extern void kvmppc_radix_tlbie_page(struct kvm *kvm, unsigned long addr,
19490165d3dSSuraj Jitindar Singh 				    unsigned int pshift, unsigned int lpid);
1958cf531edSSuraj Jitindar Singh extern void kvmppc_unmap_pte(struct kvm *kvm, pte_t *pte, unsigned long gpa,
196c43c3a86SPaul Mackerras 			unsigned int shift,
197c43c3a86SPaul Mackerras 			const struct kvm_memory_slot *memslot,
1988cf531edSSuraj Jitindar Singh 			unsigned int lpid);
1996cdf3037SAneesh Kumar K.V extern bool kvmppc_hv_handle_set_rc(struct kvm *kvm, bool nested,
200fd10be25SSuraj Jitindar Singh 				    bool writing, unsigned long gpa,
201fd10be25SSuraj Jitindar Singh 				    unsigned int lpid);
202fd10be25SSuraj Jitindar Singh extern int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu,
203fd10be25SSuraj Jitindar Singh 				unsigned long gpa,
204fd10be25SSuraj Jitindar Singh 				struct kvm_memory_slot *memslot,
205fd10be25SSuraj Jitindar Singh 				bool writing, bool kvm_ro,
206fd10be25SSuraj Jitindar Singh 				pte_t *inserted_pte, unsigned int *levelp);
2078cf4ecc0SPaul Mackerras extern int kvmppc_init_vm_radix(struct kvm *kvm);
2085a319350SPaul Mackerras extern void kvmppc_free_radix(struct kvm *kvm);
209fd10be25SSuraj Jitindar Singh extern void kvmppc_free_pgtable_radix(struct kvm *kvm, pgd_t *pgd,
210fd10be25SSuraj Jitindar Singh 				      unsigned int lpid);
2115a319350SPaul Mackerras extern int kvmppc_radix_init(void);
2125a319350SPaul Mackerras extern void kvmppc_radix_exit(void);
213*b1c5356eSSean Christopherson extern bool kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
21401756099SPaul Mackerras 			    unsigned long gfn);
215*b1c5356eSSean Christopherson extern bool kvm_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
21601756099SPaul Mackerras 			  unsigned long gfn);
217*b1c5356eSSean Christopherson extern bool kvm_test_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
21801756099SPaul Mackerras 			       unsigned long gfn);
2198f7b79b8SPaul Mackerras extern long kvmppc_hv_get_dirty_log_radix(struct kvm *kvm,
2208f7b79b8SPaul Mackerras 			struct kvm_memory_slot *memslot, unsigned long *map);
2215af3e9d0SPaul Mackerras extern void kvmppc_radix_flush_memslot(struct kvm *kvm,
2225af3e9d0SPaul Mackerras 			const struct kvm_memory_slot *memslot);
2238cf4ecc0SPaul Mackerras extern int kvmhv_get_rmmu_info(struct kvm *kvm, struct kvm_ppc_rmmu_info *info);
2249e04ba69SPaul Mackerras 
22535c4a733SAlexander Graf /* XXX remove this export when load_last_inst() is generic */
2265467a97dSAlexander Graf extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
2274e342025SAlexander Graf extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
228bc5ad3f3SBenjamin Herrenschmidt extern void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
229bc5ad3f3SBenjamin Herrenschmidt 					  unsigned int vec);
230de56a948SPaul Mackerras extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
231533082aeSSimon Guo extern void kvmppc_trigger_fac_interrupt(struct kvm_vcpu *vcpu, ulong fac);
232e15a1137SAlexander Graf extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
233e15a1137SAlexander Graf 			   bool upper, u32 val);
234aba3bd7fSAlexander Graf extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
2358c99d345STianjia Zhang extern int kvmppc_emulate_paired_single(struct kvm_vcpu *vcpu);
236ba049e93SDan Williams extern kvm_pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa,
237ba049e93SDan Williams 			bool writing, bool *writable);
238342d3db7SPaul Mackerras extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
239342d3db7SPaul Mackerras 			unsigned long *rmap, long pte_index, int realmode);
240c43c3a86SPaul Mackerras extern void kvmppc_update_dirty_map(const struct kvm_memory_slot *memslot,
241e641a317SPaul Mackerras 			unsigned long gfn, unsigned long psize);
2426f22bd32SAlexander Graf extern void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep,
243342d3db7SPaul Mackerras 			unsigned long pte_index);
2446f22bd32SAlexander Graf void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep,
24555514893SPaul Mackerras 			unsigned long pte_index);
24693e60249SPaul Mackerras extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
24793e60249SPaul Mackerras 			unsigned long *nb_ret);
248c35635efSPaul Mackerras extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr,
249c35635efSPaul Mackerras 			unsigned long gpa, bool dirty);
2507ed661bfSPaul Mackerras extern long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
2517ed661bfSPaul Mackerras 			long pte_index, unsigned long pteh, unsigned long ptel,
2527ed661bfSPaul Mackerras 			pgd_t *pgdir, bool realmode, unsigned long *idx_ret);
2536b445ad4SPaul Mackerras extern long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
2546b445ad4SPaul Mackerras 			unsigned long pte_index, unsigned long avpn,
2556b445ad4SPaul Mackerras 			unsigned long *hpret);
2568f7b79b8SPaul Mackerras extern long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,
257dfe49dbdSPaul Mackerras 			struct kvm_memory_slot *memslot, unsigned long *map);
2588f7b79b8SPaul Mackerras extern void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa,
2598f7b79b8SPaul Mackerras 			struct kvm_memory_slot *memslot,
2608f7b79b8SPaul Mackerras 			unsigned long *map);
261a0144e2aSPaul Mackerras extern void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr,
262a0144e2aSPaul Mackerras 			unsigned long mask);
2638e6afa36SAlexander Graf extern void kvmppc_set_fscr(struct kvm_vcpu *vcpu, u64 fscr);
2644e342025SAlexander Graf 
2654bb3c7a0SPaul Mackerras extern int kvmhv_p9_tm_emulation_early(struct kvm_vcpu *vcpu);
2664bb3c7a0SPaul Mackerras extern int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu);
2674bb3c7a0SPaul Mackerras extern void kvmhv_emulate_tm_rollback(struct kvm_vcpu *vcpu);
2684bb3c7a0SPaul Mackerras 
26902143947SPaul Mackerras extern void kvmppc_entry_trampoline(void);
270de56a948SPaul Mackerras extern void kvmppc_hv_entry_trampoline(void);
271ca7f4203SAlexander Graf extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
272ca7f4203SAlexander Graf extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
2730254f074SAlexander Graf extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
274699a0ea0SPaul Mackerras extern void kvmppc_pr_init_default_hcalls(struct kvm *kvm);
275ae2113a4SPaul Mackerras extern int kvmppc_hcall_impl_pr(unsigned long cmd);
276ae2113a4SPaul Mackerras extern int kvmppc_hcall_impl_hv_realmode(unsigned long cmd);
27707ae5389SAlexander Graf extern void kvmppc_copy_to_svcpu(struct kvm_vcpu *vcpu);
27807ae5389SAlexander Graf extern void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu);
2798d2e2fc5SSimon Guo 
280ce275179SCédric Le Goater long kvmppc_read_intr(void);
281ce275179SCédric Le Goater void kvmppc_bad_interrupt(struct pt_regs *regs);
282ce275179SCédric Le Goater void kvmhv_p9_set_lpcr(struct kvm_split_mode *sip);
283ce275179SCédric Le Goater void kvmhv_p9_restore_lpcr(struct kvm_split_mode *sip);
284ce275179SCédric Le Goater void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr);
285ce275179SCédric Le Goater void kvmppc_inject_interrupt_hv(struct kvm_vcpu *vcpu, int vec, u64 srr1_flags);
286ce275179SCédric Le Goater 
2878d2e2fc5SSimon Guo #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2888d2e2fc5SSimon Guo void kvmppc_save_tm_pr(struct kvm_vcpu *vcpu);
2898d2e2fc5SSimon Guo void kvmppc_restore_tm_pr(struct kvm_vcpu *vcpu);
290e32c53d1SSimon Guo void kvmppc_save_tm_sprs(struct kvm_vcpu *vcpu);
2915706340aSSimon Guo void kvmppc_restore_tm_sprs(struct kvm_vcpu *vcpu);
2928d2e2fc5SSimon Guo #else
2938d2e2fc5SSimon Guo static inline void kvmppc_save_tm_pr(struct kvm_vcpu *vcpu) {}
2948d2e2fc5SSimon Guo static inline void kvmppc_restore_tm_pr(struct kvm_vcpu *vcpu) {}
295e32c53d1SSimon Guo static inline void kvmppc_save_tm_sprs(struct kvm_vcpu *vcpu) {}
2965706340aSSimon Guo static inline void kvmppc_restore_tm_sprs(struct kvm_vcpu *vcpu) {}
2978d2e2fc5SSimon Guo #endif
2988d2e2fc5SSimon Guo 
2998e3f5fc1SPaul Mackerras long kvmhv_nested_init(void);
3008e3f5fc1SPaul Mackerras void kvmhv_nested_exit(void);
3018e3f5fc1SPaul Mackerras void kvmhv_vm_nested_init(struct kvm *kvm);
3028e3f5fc1SPaul Mackerras long kvmhv_set_partition_table(struct kvm_vcpu *vcpu);
3036ff887b8SSuraj Jitindar Singh long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu);
3048e3f5fc1SPaul Mackerras void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1);
3058e3f5fc1SPaul Mackerras void kvmhv_release_all_nested(struct kvm *kvm);
306360cae31SPaul Mackerras long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu);
307e3b6b466SSuraj Jitindar Singh long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu);
3088c99d345STianjia Zhang int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu,
309360cae31SPaul Mackerras 			  u64 time_limit, unsigned long lpcr);
310360cae31SPaul Mackerras void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr);
311360cae31SPaul Mackerras void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
312360cae31SPaul Mackerras 				   struct hv_guest_state *hr);
3138c99d345STianjia Zhang long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu);
3148e3f5fc1SPaul Mackerras 
3157284ca8aSSimon Guo void kvmppc_giveup_fac(struct kvm_vcpu *vcpu, ulong fac);
3167284ca8aSSimon Guo 
317644abbb2SSuresh Warrier extern int kvm_irq_bypass;
3184e342025SAlexander Graf 
3194e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
3204e342025SAlexander Graf {
3213ff95502SPaul Mackerras 	return vcpu->arch.book3s;
3224e342025SAlexander Graf }
3234e342025SAlexander Graf 
324de56a948SPaul Mackerras /* Also add subarch specific defines */
325de56a948SPaul Mackerras 
326de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
327de56a948SPaul Mackerras #include <asm/kvm_book3s_32.h>
328de56a948SPaul Mackerras #endif
329de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
330de56a948SPaul Mackerras #include <asm/kvm_book3s_64.h>
331de56a948SPaul Mackerras #endif
332de56a948SPaul Mackerras 
333de56a948SPaul Mackerras static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
334de56a948SPaul Mackerras {
3351143a706SSimon Guo 	vcpu->arch.regs.gpr[num] = val;
336de56a948SPaul Mackerras }
337de56a948SPaul Mackerras 
338de56a948SPaul Mackerras static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
339de56a948SPaul Mackerras {
3401143a706SSimon Guo 	return vcpu->arch.regs.gpr[num];
341de56a948SPaul Mackerras }
342de56a948SPaul Mackerras 
343de56a948SPaul Mackerras static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
344de56a948SPaul Mackerras {
345fd0944baSPaul Mackerras 	vcpu->arch.regs.ccr = val;
346de56a948SPaul Mackerras }
347de56a948SPaul Mackerras 
348de56a948SPaul Mackerras static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
349de56a948SPaul Mackerras {
350fd0944baSPaul Mackerras 	return vcpu->arch.regs.ccr;
351de56a948SPaul Mackerras }
352de56a948SPaul Mackerras 
353c63517c2SSam bobroff static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, ulong val)
354de56a948SPaul Mackerras {
355173c520aSSimon Guo 	vcpu->arch.regs.xer = val;
356de56a948SPaul Mackerras }
357de56a948SPaul Mackerras 
358c63517c2SSam bobroff static inline ulong kvmppc_get_xer(struct kvm_vcpu *vcpu)
359de56a948SPaul Mackerras {
360173c520aSSimon Guo 	return vcpu->arch.regs.xer;
361de56a948SPaul Mackerras }
362de56a948SPaul Mackerras 
363de56a948SPaul Mackerras static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
364de56a948SPaul Mackerras {
365173c520aSSimon Guo 	vcpu->arch.regs.ctr = val;
366de56a948SPaul Mackerras }
367de56a948SPaul Mackerras 
368de56a948SPaul Mackerras static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
369de56a948SPaul Mackerras {
370173c520aSSimon Guo 	return vcpu->arch.regs.ctr;
371de56a948SPaul Mackerras }
372de56a948SPaul Mackerras 
373de56a948SPaul Mackerras static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
374de56a948SPaul Mackerras {
375173c520aSSimon Guo 	vcpu->arch.regs.link = val;
376de56a948SPaul Mackerras }
377de56a948SPaul Mackerras 
378de56a948SPaul Mackerras static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
379de56a948SPaul Mackerras {
380173c520aSSimon Guo 	return vcpu->arch.regs.link;
381de56a948SPaul Mackerras }
382de56a948SPaul Mackerras 
383de56a948SPaul Mackerras static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
384de56a948SPaul Mackerras {
385173c520aSSimon Guo 	vcpu->arch.regs.nip = val;
386de56a948SPaul Mackerras }
387de56a948SPaul Mackerras 
388de56a948SPaul Mackerras static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
389de56a948SPaul Mackerras {
390173c520aSSimon Guo 	return vcpu->arch.regs.nip;
391de56a948SPaul Mackerras }
392de56a948SPaul Mackerras 
3935deb8e7aSAlexander Graf static inline u64 kvmppc_get_msr(struct kvm_vcpu *vcpu);
39473601775SCédric Le Goater static inline bool kvmppc_need_byteswap(struct kvm_vcpu *vcpu)
395de56a948SPaul Mackerras {
3965deb8e7aSAlexander Graf 	return (kvmppc_get_msr(vcpu) & MSR_LE) != (MSR_KERNEL & MSR_LE);
39773601775SCédric Le Goater }
398de56a948SPaul Mackerras 
399de56a948SPaul Mackerras static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
400de56a948SPaul Mackerras {
401de56a948SPaul Mackerras 	return vcpu->arch.fault_dar;
402de56a948SPaul Mackerras }
403de56a948SPaul Mackerras 
404e59d24e6SGreg Kurz static inline bool is_kvmppc_resume_guest(int r)
405e59d24e6SGreg Kurz {
406e59d24e6SGreg Kurz 	return (r == RESUME_GUEST || r == RESUME_GUEST_NV);
407e59d24e6SGreg Kurz }
408e59d24e6SGreg Kurz 
409c12fb43cSAlexander Graf static inline bool is_kvmppc_hv_enabled(struct kvm *kvm);
410c12fb43cSAlexander Graf static inline bool kvmppc_supports_magic_page(struct kvm_vcpu *vcpu)
411c12fb43cSAlexander Graf {
412c12fb43cSAlexander Graf 	/* Only PR KVM supports the magic page */
413c12fb43cSAlexander Graf 	return !is_kvmppc_hv_enabled(vcpu->kvm);
414c12fb43cSAlexander Graf }
415c12fb43cSAlexander Graf 
41699342cf8SDavid Gibson extern int kvmppc_h_logical_ci_load(struct kvm_vcpu *vcpu);
41799342cf8SDavid Gibson extern int kvmppc_h_logical_ci_store(struct kvm_vcpu *vcpu);
41899342cf8SDavid Gibson 
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
4258c32a2eaSBharat Bhushan /* TO = 31 for unconditional trap */
4268c32a2eaSBharat Bhushan #define INS_TW				0x7fe00008
4274e342025SAlexander Graf 
428c01e3f66SAlexander Graf #define SPLIT_HACK_MASK			0xff000000
429c01e3f66SAlexander Graf #define SPLIT_HACK_OFFS			0xfb000000
430c01e3f66SAlexander Graf 
4311e175d2eSSam Bobroff /*
4321e175d2eSSam Bobroff  * This packs a VCPU ID from the [0..KVM_MAX_VCPU_ID) space down to the
4331e175d2eSSam Bobroff  * [0..KVM_MAX_VCPUS) space, using knowledge of the guest's core stride
4341e175d2eSSam Bobroff  * (but not its actual threading mode, which is not available) to avoid
4351e175d2eSSam Bobroff  * collisions.
4361e175d2eSSam Bobroff  *
4371e175d2eSSam Bobroff  * The implementation leaves VCPU IDs from the range [0..KVM_MAX_VCPUS) (block
4381e175d2eSSam Bobroff  * 0) unchanged: if the guest is filling each VCORE completely then it will be
4391e175d2eSSam Bobroff  * using consecutive IDs and it will fill the space without any packing.
4401e175d2eSSam Bobroff  *
4411e175d2eSSam Bobroff  * For higher VCPU IDs, the packed ID is based on the VCPU ID modulo
4421e175d2eSSam Bobroff  * KVM_MAX_VCPUS (effectively masking off the top bits) and then an offset is
4431e175d2eSSam Bobroff  * added to avoid collisions.
4441e175d2eSSam Bobroff  *
4451e175d2eSSam Bobroff  * VCPU IDs in the range [KVM_MAX_VCPUS..(KVM_MAX_VCPUS*2)) (block 1) are only
4461e175d2eSSam Bobroff  * possible if the guest is leaving at least 1/2 of each VCORE empty, so IDs
4471e175d2eSSam Bobroff  * can be safely packed into the second half of each VCORE by adding an offset
4481e175d2eSSam Bobroff  * of (stride / 2).
4491e175d2eSSam Bobroff  *
4501e175d2eSSam Bobroff  * Similarly, if VCPU IDs in the range [(KVM_MAX_VCPUS*2)..(KVM_MAX_VCPUS*4))
4511e175d2eSSam Bobroff  * (blocks 2 and 3) are seen, the guest must be leaving at least 3/4 of each
4521e175d2eSSam Bobroff  * VCORE empty so packed IDs can be offset by (stride / 4) and (stride * 3 / 4).
4531e175d2eSSam Bobroff  *
4541e175d2eSSam Bobroff  * Finally, VCPU IDs from blocks 5..7 will only be seen if the guest is using a
4551e175d2eSSam Bobroff  * stride of 8 and 1 thread per core so the remaining offsets of 1, 5, 3 and 7
4561e175d2eSSam Bobroff  * must be free to use.
4571e175d2eSSam Bobroff  *
4581e175d2eSSam Bobroff  * (The offsets for each block are stored in block_offsets[], indexed by the
4591e175d2eSSam Bobroff  * block number if the stride is 8. For cases where the guest's stride is less
4601e175d2eSSam Bobroff  * than 8, we can re-use the block_offsets array by multiplying the block
4611e175d2eSSam Bobroff  * number by (MAX_SMT_THREADS / stride) to reach the correct entry.)
4621e175d2eSSam Bobroff  */
4631e175d2eSSam Bobroff static inline u32 kvmppc_pack_vcpu_id(struct kvm *kvm, u32 id)
4641e175d2eSSam Bobroff {
4651e175d2eSSam Bobroff 	const int block_offsets[MAX_SMT_THREADS] = {0, 4, 2, 6, 1, 5, 3, 7};
4661e175d2eSSam Bobroff 	int stride = kvm->arch.emul_smt_mode;
4671e175d2eSSam Bobroff 	int block = (id / KVM_MAX_VCPUS) * (MAX_SMT_THREADS / stride);
4681e175d2eSSam Bobroff 	u32 packed_id;
4691e175d2eSSam Bobroff 
4701e175d2eSSam Bobroff 	if (WARN_ONCE(block >= MAX_SMT_THREADS, "VCPU ID too large to pack"))
4711e175d2eSSam Bobroff 		return 0;
4721e175d2eSSam Bobroff 	packed_id = (id % KVM_MAX_VCPUS) + block_offsets[block];
4731e175d2eSSam Bobroff 	if (WARN_ONCE(packed_id >= KVM_MAX_VCPUS, "VCPU ID packing failed"))
4741e175d2eSSam Bobroff 		return 0;
4751e175d2eSSam Bobroff 	return packed_id;
4761e175d2eSSam Bobroff }
4771e175d2eSSam Bobroff 
4784e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */
479