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);
21301756099SPaul Mackerras extern int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
21401756099SPaul Mackerras 			unsigned long gfn);
21501756099SPaul Mackerras extern int kvm_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
21601756099SPaul Mackerras 			unsigned long gfn);
21701756099SPaul Mackerras extern int 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);
261*67145ef4SNicholas Piggin extern unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm,
262*67145ef4SNicholas Piggin 			unsigned long lpcr);
263a0144e2aSPaul Mackerras extern void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr,
264a0144e2aSPaul Mackerras 			unsigned long mask);
2658e6afa36SAlexander Graf extern void kvmppc_set_fscr(struct kvm_vcpu *vcpu, u64 fscr);
2664e342025SAlexander Graf 
2674bb3c7a0SPaul Mackerras extern int kvmhv_p9_tm_emulation_early(struct kvm_vcpu *vcpu);
2684bb3c7a0SPaul Mackerras extern int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu);
2694bb3c7a0SPaul Mackerras extern void kvmhv_emulate_tm_rollback(struct kvm_vcpu *vcpu);
2704bb3c7a0SPaul Mackerras 
27102143947SPaul Mackerras extern void kvmppc_entry_trampoline(void);
272de56a948SPaul Mackerras extern void kvmppc_hv_entry_trampoline(void);
273ca7f4203SAlexander Graf extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
274ca7f4203SAlexander Graf extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
2750254f074SAlexander Graf extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
276699a0ea0SPaul Mackerras extern void kvmppc_pr_init_default_hcalls(struct kvm *kvm);
277ae2113a4SPaul Mackerras extern int kvmppc_hcall_impl_pr(unsigned long cmd);
278ae2113a4SPaul Mackerras extern int kvmppc_hcall_impl_hv_realmode(unsigned long cmd);
27907ae5389SAlexander Graf extern void kvmppc_copy_to_svcpu(struct kvm_vcpu *vcpu);
28007ae5389SAlexander Graf extern void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu);
2818d2e2fc5SSimon Guo 
282ce275179SCédric Le Goater long kvmppc_read_intr(void);
283ce275179SCédric Le Goater void kvmppc_bad_interrupt(struct pt_regs *regs);
284ce275179SCédric Le Goater void kvmhv_p9_set_lpcr(struct kvm_split_mode *sip);
285ce275179SCédric Le Goater void kvmhv_p9_restore_lpcr(struct kvm_split_mode *sip);
286ce275179SCédric Le Goater void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr);
287ce275179SCédric Le Goater void kvmppc_inject_interrupt_hv(struct kvm_vcpu *vcpu, int vec, u64 srr1_flags);
288ce275179SCédric Le Goater 
2898d2e2fc5SSimon Guo #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2908d2e2fc5SSimon Guo void kvmppc_save_tm_pr(struct kvm_vcpu *vcpu);
2918d2e2fc5SSimon Guo void kvmppc_restore_tm_pr(struct kvm_vcpu *vcpu);
292e32c53d1SSimon Guo void kvmppc_save_tm_sprs(struct kvm_vcpu *vcpu);
2935706340aSSimon Guo void kvmppc_restore_tm_sprs(struct kvm_vcpu *vcpu);
2948d2e2fc5SSimon Guo #else
2958d2e2fc5SSimon Guo static inline void kvmppc_save_tm_pr(struct kvm_vcpu *vcpu) {}
2968d2e2fc5SSimon Guo static inline void kvmppc_restore_tm_pr(struct kvm_vcpu *vcpu) {}
297e32c53d1SSimon Guo static inline void kvmppc_save_tm_sprs(struct kvm_vcpu *vcpu) {}
2985706340aSSimon Guo static inline void kvmppc_restore_tm_sprs(struct kvm_vcpu *vcpu) {}
2998d2e2fc5SSimon Guo #endif
3008d2e2fc5SSimon Guo 
3018e3f5fc1SPaul Mackerras long kvmhv_nested_init(void);
3028e3f5fc1SPaul Mackerras void kvmhv_nested_exit(void);
3038e3f5fc1SPaul Mackerras void kvmhv_vm_nested_init(struct kvm *kvm);
3048e3f5fc1SPaul Mackerras long kvmhv_set_partition_table(struct kvm_vcpu *vcpu);
3056ff887b8SSuraj Jitindar Singh long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu);
3068e3f5fc1SPaul Mackerras void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1);
3078e3f5fc1SPaul Mackerras void kvmhv_release_all_nested(struct kvm *kvm);
308360cae31SPaul Mackerras long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu);
309e3b6b466SSuraj Jitindar Singh long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu);
3108c99d345STianjia Zhang int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu,
311360cae31SPaul Mackerras 			  u64 time_limit, unsigned long lpcr);
312360cae31SPaul Mackerras void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr);
313360cae31SPaul Mackerras void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
314360cae31SPaul Mackerras 				   struct hv_guest_state *hr);
3158c99d345STianjia Zhang long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu);
3168e3f5fc1SPaul Mackerras 
3177284ca8aSSimon Guo void kvmppc_giveup_fac(struct kvm_vcpu *vcpu, ulong fac);
3187284ca8aSSimon Guo 
319644abbb2SSuresh Warrier extern int kvm_irq_bypass;
3204e342025SAlexander Graf 
3214e342025SAlexander Graf static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
3224e342025SAlexander Graf {
3233ff95502SPaul Mackerras 	return vcpu->arch.book3s;
3244e342025SAlexander Graf }
3254e342025SAlexander Graf 
326de56a948SPaul Mackerras /* Also add subarch specific defines */
327de56a948SPaul Mackerras 
328de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
329de56a948SPaul Mackerras #include <asm/kvm_book3s_32.h>
330de56a948SPaul Mackerras #endif
331de56a948SPaul Mackerras #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
332de56a948SPaul Mackerras #include <asm/kvm_book3s_64.h>
333de56a948SPaul Mackerras #endif
334de56a948SPaul Mackerras 
335de56a948SPaul Mackerras static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
336de56a948SPaul Mackerras {
3371143a706SSimon Guo 	vcpu->arch.regs.gpr[num] = val;
338de56a948SPaul Mackerras }
339de56a948SPaul Mackerras 
340de56a948SPaul Mackerras static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
341de56a948SPaul Mackerras {
3421143a706SSimon Guo 	return vcpu->arch.regs.gpr[num];
343de56a948SPaul Mackerras }
344de56a948SPaul Mackerras 
345de56a948SPaul Mackerras static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
346de56a948SPaul Mackerras {
347fd0944baSPaul Mackerras 	vcpu->arch.regs.ccr = val;
348de56a948SPaul Mackerras }
349de56a948SPaul Mackerras 
350de56a948SPaul Mackerras static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
351de56a948SPaul Mackerras {
352fd0944baSPaul Mackerras 	return vcpu->arch.regs.ccr;
353de56a948SPaul Mackerras }
354de56a948SPaul Mackerras 
355c63517c2SSam bobroff static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, ulong val)
356de56a948SPaul Mackerras {
357173c520aSSimon Guo 	vcpu->arch.regs.xer = val;
358de56a948SPaul Mackerras }
359de56a948SPaul Mackerras 
360c63517c2SSam bobroff static inline ulong kvmppc_get_xer(struct kvm_vcpu *vcpu)
361de56a948SPaul Mackerras {
362173c520aSSimon Guo 	return vcpu->arch.regs.xer;
363de56a948SPaul Mackerras }
364de56a948SPaul Mackerras 
365de56a948SPaul Mackerras static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
366de56a948SPaul Mackerras {
367173c520aSSimon Guo 	vcpu->arch.regs.ctr = val;
368de56a948SPaul Mackerras }
369de56a948SPaul Mackerras 
370de56a948SPaul Mackerras static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
371de56a948SPaul Mackerras {
372173c520aSSimon Guo 	return vcpu->arch.regs.ctr;
373de56a948SPaul Mackerras }
374de56a948SPaul Mackerras 
375de56a948SPaul Mackerras static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
376de56a948SPaul Mackerras {
377173c520aSSimon Guo 	vcpu->arch.regs.link = val;
378de56a948SPaul Mackerras }
379de56a948SPaul Mackerras 
380de56a948SPaul Mackerras static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
381de56a948SPaul Mackerras {
382173c520aSSimon Guo 	return vcpu->arch.regs.link;
383de56a948SPaul Mackerras }
384de56a948SPaul Mackerras 
385de56a948SPaul Mackerras static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
386de56a948SPaul Mackerras {
387173c520aSSimon Guo 	vcpu->arch.regs.nip = val;
388de56a948SPaul Mackerras }
389de56a948SPaul Mackerras 
390de56a948SPaul Mackerras static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
391de56a948SPaul Mackerras {
392173c520aSSimon Guo 	return vcpu->arch.regs.nip;
393de56a948SPaul Mackerras }
394de56a948SPaul Mackerras 
3955deb8e7aSAlexander Graf static inline u64 kvmppc_get_msr(struct kvm_vcpu *vcpu);
39673601775SCédric Le Goater static inline bool kvmppc_need_byteswap(struct kvm_vcpu *vcpu)
397de56a948SPaul Mackerras {
3985deb8e7aSAlexander Graf 	return (kvmppc_get_msr(vcpu) & MSR_LE) != (MSR_KERNEL & MSR_LE);
39973601775SCédric Le Goater }
400de56a948SPaul Mackerras 
401de56a948SPaul Mackerras static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
402de56a948SPaul Mackerras {
403de56a948SPaul Mackerras 	return vcpu->arch.fault_dar;
404de56a948SPaul Mackerras }
405de56a948SPaul Mackerras 
406e59d24e6SGreg Kurz static inline bool is_kvmppc_resume_guest(int r)
407e59d24e6SGreg Kurz {
408e59d24e6SGreg Kurz 	return (r == RESUME_GUEST || r == RESUME_GUEST_NV);
409e59d24e6SGreg Kurz }
410e59d24e6SGreg Kurz 
411c12fb43cSAlexander Graf static inline bool is_kvmppc_hv_enabled(struct kvm *kvm);
412c12fb43cSAlexander Graf static inline bool kvmppc_supports_magic_page(struct kvm_vcpu *vcpu)
413c12fb43cSAlexander Graf {
414c12fb43cSAlexander Graf 	/* Only PR KVM supports the magic page */
415c12fb43cSAlexander Graf 	return !is_kvmppc_hv_enabled(vcpu->kvm);
416c12fb43cSAlexander Graf }
417c12fb43cSAlexander Graf 
41899342cf8SDavid Gibson extern int kvmppc_h_logical_ci_load(struct kvm_vcpu *vcpu);
41999342cf8SDavid Gibson extern int kvmppc_h_logical_ci_store(struct kvm_vcpu *vcpu);
42099342cf8SDavid Gibson 
421ad0a048bSAlexander Graf /* Magic register values loaded into r3 and r4 before the 'sc' assembly
422ad0a048bSAlexander Graf  * instruction for the OSI hypercalls */
423ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R3			0x113724FA
424ad0a048bSAlexander Graf #define OSI_SC_MAGIC_R4			0x77810F9B
425ad0a048bSAlexander Graf 
4264e342025SAlexander Graf #define INS_DCBZ			0x7c0007ec
4278c32a2eaSBharat Bhushan /* TO = 31 for unconditional trap */
4288c32a2eaSBharat Bhushan #define INS_TW				0x7fe00008
4294e342025SAlexander Graf 
430c01e3f66SAlexander Graf #define SPLIT_HACK_MASK			0xff000000
431c01e3f66SAlexander Graf #define SPLIT_HACK_OFFS			0xfb000000
432c01e3f66SAlexander Graf 
4331e175d2eSSam Bobroff /*
4341e175d2eSSam Bobroff  * This packs a VCPU ID from the [0..KVM_MAX_VCPU_ID) space down to the
4351e175d2eSSam Bobroff  * [0..KVM_MAX_VCPUS) space, using knowledge of the guest's core stride
4361e175d2eSSam Bobroff  * (but not its actual threading mode, which is not available) to avoid
4371e175d2eSSam Bobroff  * collisions.
4381e175d2eSSam Bobroff  *
4391e175d2eSSam Bobroff  * The implementation leaves VCPU IDs from the range [0..KVM_MAX_VCPUS) (block
4401e175d2eSSam Bobroff  * 0) unchanged: if the guest is filling each VCORE completely then it will be
4411e175d2eSSam Bobroff  * using consecutive IDs and it will fill the space without any packing.
4421e175d2eSSam Bobroff  *
4431e175d2eSSam Bobroff  * For higher VCPU IDs, the packed ID is based on the VCPU ID modulo
4441e175d2eSSam Bobroff  * KVM_MAX_VCPUS (effectively masking off the top bits) and then an offset is
4451e175d2eSSam Bobroff  * added to avoid collisions.
4461e175d2eSSam Bobroff  *
4471e175d2eSSam Bobroff  * VCPU IDs in the range [KVM_MAX_VCPUS..(KVM_MAX_VCPUS*2)) (block 1) are only
4481e175d2eSSam Bobroff  * possible if the guest is leaving at least 1/2 of each VCORE empty, so IDs
4491e175d2eSSam Bobroff  * can be safely packed into the second half of each VCORE by adding an offset
4501e175d2eSSam Bobroff  * of (stride / 2).
4511e175d2eSSam Bobroff  *
4521e175d2eSSam Bobroff  * Similarly, if VCPU IDs in the range [(KVM_MAX_VCPUS*2)..(KVM_MAX_VCPUS*4))
4531e175d2eSSam Bobroff  * (blocks 2 and 3) are seen, the guest must be leaving at least 3/4 of each
4541e175d2eSSam Bobroff  * VCORE empty so packed IDs can be offset by (stride / 4) and (stride * 3 / 4).
4551e175d2eSSam Bobroff  *
4561e175d2eSSam Bobroff  * Finally, VCPU IDs from blocks 5..7 will only be seen if the guest is using a
4571e175d2eSSam Bobroff  * stride of 8 and 1 thread per core so the remaining offsets of 1, 5, 3 and 7
4581e175d2eSSam Bobroff  * must be free to use.
4591e175d2eSSam Bobroff  *
4601e175d2eSSam Bobroff  * (The offsets for each block are stored in block_offsets[], indexed by the
4611e175d2eSSam Bobroff  * block number if the stride is 8. For cases where the guest's stride is less
4621e175d2eSSam Bobroff  * than 8, we can re-use the block_offsets array by multiplying the block
4631e175d2eSSam Bobroff  * number by (MAX_SMT_THREADS / stride) to reach the correct entry.)
4641e175d2eSSam Bobroff  */
4651e175d2eSSam Bobroff static inline u32 kvmppc_pack_vcpu_id(struct kvm *kvm, u32 id)
4661e175d2eSSam Bobroff {
4671e175d2eSSam Bobroff 	const int block_offsets[MAX_SMT_THREADS] = {0, 4, 2, 6, 1, 5, 3, 7};
4681e175d2eSSam Bobroff 	int stride = kvm->arch.emul_smt_mode;
4691e175d2eSSam Bobroff 	int block = (id / KVM_MAX_VCPUS) * (MAX_SMT_THREADS / stride);
4701e175d2eSSam Bobroff 	u32 packed_id;
4711e175d2eSSam Bobroff 
4721e175d2eSSam Bobroff 	if (WARN_ONCE(block >= MAX_SMT_THREADS, "VCPU ID too large to pack"))
4731e175d2eSSam Bobroff 		return 0;
4741e175d2eSSam Bobroff 	packed_id = (id % KVM_MAX_VCPUS) + block_offsets[block];
4751e175d2eSSam Bobroff 	if (WARN_ONCE(packed_id >= KVM_MAX_VCPUS, "VCPU ID packing failed"))
4761e175d2eSSam Bobroff 		return 0;
4771e175d2eSSam Bobroff 	return packed_id;
4781e175d2eSSam Bobroff }
4791e175d2eSSam Bobroff 
4804e342025SAlexander Graf #endif /* __ASM_KVM_BOOK3S_H__ */
481