xref: /openbmc/linux/arch/powerpc/include/asm/kvm_ppc.h (revision 337cbeb2)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *
4  * Copyright IBM Corp. 2008
5  *
6  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
7  */
8 
9 #ifndef __POWERPC_KVM_PPC_H__
10 #define __POWERPC_KVM_PPC_H__
11 
12 /* This file exists just so we can dereference kvm_vcpu, avoiding nested header
13  * dependencies. */
14 
15 #include <linux/mutex.h>
16 #include <linux/timer.h>
17 #include <linux/types.h>
18 #include <linux/kvm_types.h>
19 #include <linux/kvm_host.h>
20 #include <linux/bug.h>
21 #ifdef CONFIG_PPC_BOOK3S
22 #include <asm/kvm_book3s.h>
23 #else
24 #include <asm/kvm_booke.h>
25 #endif
26 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
27 #include <asm/paca.h>
28 #include <asm/xive.h>
29 #include <asm/cpu_has_feature.h>
30 #endif
31 
32 /*
33  * KVMPPC_INST_SW_BREAKPOINT is debug Instruction
34  * for supporting software breakpoint.
35  */
36 #define KVMPPC_INST_SW_BREAKPOINT	0x00dddd00
37 
38 enum emulation_result {
39 	EMULATE_DONE,         /* no further processing */
40 	EMULATE_DO_MMIO,      /* kvm_run filled with MMIO request */
41 	EMULATE_FAIL,         /* can't emulate this instruction */
42 	EMULATE_AGAIN,        /* something went wrong. go again */
43 	EMULATE_EXIT_USER,    /* emulation requires exit to user-space */
44 };
45 
46 enum instruction_fetch_type {
47 	INST_GENERIC,
48 	INST_SC,		/* system call */
49 };
50 
51 enum xlate_instdata {
52 	XLATE_INST,		/* translate instruction address */
53 	XLATE_DATA		/* translate data address */
54 };
55 
56 enum xlate_readwrite {
57 	XLATE_READ,		/* check for read permissions */
58 	XLATE_WRITE		/* check for write permissions */
59 };
60 
61 extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
62 extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
63 extern void kvmppc_handler_highmem(void);
64 
65 extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
66 extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
67                               unsigned int rt, unsigned int bytes,
68 			      int is_default_endian);
69 extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
70                                unsigned int rt, unsigned int bytes,
71 			       int is_default_endian);
72 extern int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
73 				unsigned int rt, unsigned int bytes,
74 			int is_default_endian, int mmio_sign_extend);
75 extern int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
76 		unsigned int rt, unsigned int bytes, int is_default_endian);
77 extern int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
78 		unsigned int rs, unsigned int bytes, int is_default_endian);
79 extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
80 			       u64 val, unsigned int bytes,
81 			       int is_default_endian);
82 extern int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
83 				int rs, unsigned int bytes,
84 				int is_default_endian);
85 
86 extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
87 				 enum instruction_fetch_type type, u32 *inst);
88 
89 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
90 		     bool data);
91 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
92 		     bool data);
93 extern int kvmppc_emulate_instruction(struct kvm_run *run,
94                                       struct kvm_vcpu *vcpu);
95 extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
96 extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
97 extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
98 extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
99 extern void kvmppc_decrementer_func(struct kvm_vcpu *vcpu);
100 extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
101 extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
102 extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
103 
104 /* Core-specific hooks */
105 
106 extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
107                            unsigned int gtlb_idx);
108 extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
109 extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
110 extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
111 extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
112 extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
113                               gva_t eaddr);
114 extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
115 extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
116 extern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr,
117 			enum xlate_instdata xlid, enum xlate_readwrite xlrw,
118 			struct kvmppc_pte *pte);
119 
120 extern int kvmppc_core_vcpu_create(struct kvm_vcpu *vcpu);
121 extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
122 extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
123 extern int kvmppc_core_check_processor_compat(void);
124 extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
125                                       struct kvm_translation *tr);
126 
127 extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
128 extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
129 
130 extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
131 extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
132 extern void kvmppc_core_queue_machine_check(struct kvm_vcpu *vcpu, ulong flags);
133 extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
134 extern void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu);
135 extern void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu);
136 extern void kvmppc_core_queue_vsx_unavail(struct kvm_vcpu *vcpu);
137 extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
138 extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
139 extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
140                                        struct kvm_interrupt *irq);
141 extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
142 extern void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, ulong dear_flags,
143 					ulong esr_flags);
144 extern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
145 					   ulong dear_flags,
146 					   ulong esr_flags);
147 extern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu);
148 extern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
149 					   ulong esr_flags);
150 extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
151 extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
152 
153 extern int kvmppc_booke_init(void);
154 extern void kvmppc_booke_exit(void);
155 
156 extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
157 extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
158 extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
159 
160 extern int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order);
161 extern void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info);
162 extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order);
163 extern void kvmppc_free_hpt(struct kvm_hpt_info *info);
164 extern void kvmppc_rmap_reset(struct kvm *kvm);
165 extern long kvmppc_prepare_vrma(struct kvm *kvm,
166 				struct kvm_userspace_memory_region *mem);
167 extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
168 			struct kvm_memory_slot *memslot, unsigned long porder);
169 extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
170 extern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
171 		struct iommu_group *grp);
172 extern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
173 		struct iommu_group *grp);
174 extern int kvmppc_switch_mmu_to_hpt(struct kvm *kvm);
175 extern int kvmppc_switch_mmu_to_radix(struct kvm *kvm);
176 extern void kvmppc_setup_partition_table(struct kvm *kvm);
177 
178 extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
179 				struct kvm_create_spapr_tce_64 *args);
180 extern struct kvmppc_spapr_tce_table *kvmppc_find_table(
181 		struct kvm *kvm, unsigned long liobn);
182 #define kvmppc_ioba_validate(stt, ioba, npages)                         \
183 		(iommu_tce_check_ioba((stt)->page_shift, (stt)->offset, \
184 				(stt)->size, (ioba), (npages)) ?        \
185 				H_PARAMETER : H_SUCCESS)
186 extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
187 			     unsigned long ioba, unsigned long tce);
188 extern long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
189 		unsigned long liobn, unsigned long ioba,
190 		unsigned long tce_list, unsigned long npages);
191 extern long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
192 		unsigned long liobn, unsigned long ioba,
193 		unsigned long tce_value, unsigned long npages);
194 extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
195 			     unsigned long ioba);
196 extern struct page *kvm_alloc_hpt_cma(unsigned long nr_pages);
197 extern void kvm_free_hpt_cma(struct page *page, unsigned long nr_pages);
198 extern int kvmppc_core_init_vm(struct kvm *kvm);
199 extern void kvmppc_core_destroy_vm(struct kvm *kvm);
200 extern void kvmppc_core_free_memslot(struct kvm *kvm,
201 				     struct kvm_memory_slot *slot);
202 extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
203 				struct kvm_memory_slot *memslot,
204 				const struct kvm_userspace_memory_region *mem,
205 				enum kvm_mr_change change);
206 extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
207 				const struct kvm_userspace_memory_region *mem,
208 				const struct kvm_memory_slot *old,
209 				const struct kvm_memory_slot *new,
210 				enum kvm_mr_change change);
211 extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
212 				      struct kvm_ppc_smmu_info *info);
213 extern void kvmppc_core_flush_memslot(struct kvm *kvm,
214 				      struct kvm_memory_slot *memslot);
215 
216 extern int kvmppc_bookehv_init(void);
217 extern void kvmppc_bookehv_exit(void);
218 
219 extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
220 
221 extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
222 extern long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
223 					    struct kvm_ppc_resize_hpt *rhpt);
224 extern long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
225 					   struct kvm_ppc_resize_hpt *rhpt);
226 
227 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
228 
229 extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
230 extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
231 extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
232 
233 extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
234 				u32 priority);
235 extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
236 				u32 *priority);
237 extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
238 extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
239 
240 void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu);
241 void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu);
242 
243 union kvmppc_one_reg {
244 	u32	wval;
245 	u64	dval;
246 	vector128 vval;
247 	u64	vsxval[2];
248 	u32	vsx32val[4];
249 	u16	vsx16val[8];
250 	u8	vsx8val[16];
251 	struct {
252 		u64	addr;
253 		u64	length;
254 	}	vpaval;
255 	u64	xive_timaval[2];
256 };
257 
258 struct kvmppc_ops {
259 	struct module *owner;
260 	int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
261 	int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
262 	int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
263 			   union kvmppc_one_reg *val);
264 	int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
265 			   union kvmppc_one_reg *val);
266 	void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
267 	void (*vcpu_put)(struct kvm_vcpu *vcpu);
268 	void (*inject_interrupt)(struct kvm_vcpu *vcpu, int vec, u64 srr1_flags);
269 	void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
270 	int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
271 	int (*vcpu_create)(struct kvm_vcpu *vcpu);
272 	void (*vcpu_free)(struct kvm_vcpu *vcpu);
273 	int (*check_requests)(struct kvm_vcpu *vcpu);
274 	int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
275 	void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
276 	int (*prepare_memory_region)(struct kvm *kvm,
277 				     struct kvm_memory_slot *memslot,
278 				     const struct kvm_userspace_memory_region *mem,
279 				     enum kvm_mr_change change);
280 	void (*commit_memory_region)(struct kvm *kvm,
281 				     const struct kvm_userspace_memory_region *mem,
282 				     const struct kvm_memory_slot *old,
283 				     const struct kvm_memory_slot *new,
284 				     enum kvm_mr_change change);
285 	int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
286 			   unsigned long end);
287 	int (*age_hva)(struct kvm *kvm, unsigned long start, unsigned long end);
288 	int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
289 	void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
290 	void (*free_memslot)(struct kvm_memory_slot *slot);
291 	int (*init_vm)(struct kvm *kvm);
292 	void (*destroy_vm)(struct kvm *kvm);
293 	int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
294 	int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
295 			  unsigned int inst, int *advance);
296 	int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
297 	int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
298 	void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
299 	long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
300 			      unsigned long arg);
301 	int (*hcall_implemented)(unsigned long hcall);
302 	int (*irq_bypass_add_producer)(struct irq_bypass_consumer *,
303 				       struct irq_bypass_producer *);
304 	void (*irq_bypass_del_producer)(struct irq_bypass_consumer *,
305 					struct irq_bypass_producer *);
306 	int (*configure_mmu)(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg);
307 	int (*get_rmmu_info)(struct kvm *kvm, struct kvm_ppc_rmmu_info *info);
308 	int (*set_smt_mode)(struct kvm *kvm, unsigned long mode,
309 			    unsigned long flags);
310 	void (*giveup_ext)(struct kvm_vcpu *vcpu, ulong msr);
311 	int (*enable_nested)(struct kvm *kvm);
312 	int (*load_from_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
313 			       int size);
314 	int (*store_to_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
315 			      int size);
316 	int (*enable_svm)(struct kvm *kvm);
317 	int (*svm_off)(struct kvm *kvm);
318 };
319 
320 extern struct kvmppc_ops *kvmppc_hv_ops;
321 extern struct kvmppc_ops *kvmppc_pr_ops;
322 
323 static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
324 				enum instruction_fetch_type type, u32 *inst)
325 {
326 	int ret = EMULATE_DONE;
327 	u32 fetched_inst;
328 
329 	/* Load the instruction manually if it failed to do so in the
330 	 * exit path */
331 	if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
332 		ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
333 
334 	/*  Write fetch_failed unswapped if the fetch failed */
335 	if (ret == EMULATE_DONE)
336 		fetched_inst = kvmppc_need_byteswap(vcpu) ?
337 				swab32(vcpu->arch.last_inst) :
338 				vcpu->arch.last_inst;
339 	else
340 		fetched_inst = vcpu->arch.last_inst;
341 
342 	*inst = fetched_inst;
343 	return ret;
344 }
345 
346 static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
347 {
348 	return kvm->arch.kvm_ops == kvmppc_hv_ops;
349 }
350 
351 extern int kvmppc_hwrng_present(void);
352 
353 /*
354  * Cuts out inst bits with ordering according to spec.
355  * That means the leftmost bit is zero. All given bits are included.
356  */
357 static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
358 {
359 	u32 r;
360 	u32 mask;
361 
362 	BUG_ON(msb > lsb);
363 
364 	mask = (1 << (lsb - msb + 1)) - 1;
365 	r = (inst >> (63 - lsb)) & mask;
366 
367 	return r;
368 }
369 
370 /*
371  * Replaces inst bits with ordering according to spec.
372  */
373 static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
374 {
375 	u32 r;
376 	u32 mask;
377 
378 	BUG_ON(msb > lsb);
379 
380 	mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
381 	r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
382 
383 	return r;
384 }
385 
386 #define one_reg_size(id)	\
387 	(1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
388 
389 #define get_reg_val(id, reg)	({		\
390 	union kvmppc_one_reg __u;		\
391 	switch (one_reg_size(id)) {		\
392 	case 4: __u.wval = (reg); break;	\
393 	case 8: __u.dval = (reg); break;	\
394 	default: BUG();				\
395 	}					\
396 	__u;					\
397 })
398 
399 
400 #define set_reg_val(id, val)	({		\
401 	u64 __v;				\
402 	switch (one_reg_size(id)) {		\
403 	case 4: __v = (val).wval; break;	\
404 	case 8: __v = (val).dval; break;	\
405 	default: BUG();				\
406 	}					\
407 	__v;					\
408 })
409 
410 int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
411 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
412 
413 int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
414 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
415 
416 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
417 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
418 int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
419 int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
420 
421 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
422 
423 struct openpic;
424 
425 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
426 extern void kvm_cma_reserve(void) __init;
427 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
428 {
429 	paca_ptrs[cpu]->kvm_hstate.xics_phys = (void __iomem *)addr;
430 }
431 
432 static inline void kvmppc_set_xive_tima(int cpu,
433 					unsigned long phys_addr,
434 					void __iomem *virt_addr)
435 {
436 	paca_ptrs[cpu]->kvm_hstate.xive_tima_phys = (void __iomem *)phys_addr;
437 	paca_ptrs[cpu]->kvm_hstate.xive_tima_virt = virt_addr;
438 }
439 
440 static inline u32 kvmppc_get_xics_latch(void)
441 {
442 	u32 xirr;
443 
444 	xirr = get_paca()->kvm_hstate.saved_xirr;
445 	get_paca()->kvm_hstate.saved_xirr = 0;
446 	return xirr;
447 }
448 
449 /*
450  * To avoid the need to unnecessarily exit fully to the host kernel, an IPI to
451  * a CPU thread that's running/napping inside of a guest is by default regarded
452  * as a request to wake the CPU (if needed) and continue execution within the
453  * guest, potentially to process new state like externally-generated
454  * interrupts or IPIs sent from within the guest itself (e.g. H_PROD/H_IPI).
455  *
456  * To force an exit to the host kernel, kvmppc_set_host_ipi() must be called
457  * prior to issuing the IPI to set the corresponding 'host_ipi' flag in the
458  * target CPU's PACA. To avoid unnecessary exits to the host, this flag should
459  * be immediately cleared via kvmppc_clear_host_ipi() by the IPI handler on
460  * the receiving side prior to processing the IPI work.
461  *
462  * NOTE:
463  *
464  * We currently issue an smp_mb() at the beginning of kvmppc_set_host_ipi().
465  * This is to guard against sequences such as the following:
466  *
467  *      CPU
468  *        X: smp_muxed_ipi_set_message():
469  *        X:   smp_mb()
470  *        X:   message[RESCHEDULE] = 1
471  *        X: doorbell_global_ipi(42):
472  *        X:   kvmppc_set_host_ipi(42)
473  *        X:   ppc_msgsnd_sync()/smp_mb()
474  *        X:   ppc_msgsnd() -> 42
475  *       42: doorbell_exception(): // from CPU X
476  *       42:   ppc_msgsync()
477  *      105: smp_muxed_ipi_set_message():
478  *      105:   smb_mb()
479  *           // STORE DEFERRED DUE TO RE-ORDERING
480  *    --105:   message[CALL_FUNCTION] = 1
481  *    | 105: doorbell_global_ipi(42):
482  *    | 105:   kvmppc_set_host_ipi(42)
483  *    |  42:   kvmppc_clear_host_ipi(42)
484  *    |  42: smp_ipi_demux_relaxed()
485  *    |  42: // returns to executing guest
486  *    |      // RE-ORDERED STORE COMPLETES
487  *    ->105:   message[CALL_FUNCTION] = 1
488  *      105:   ppc_msgsnd_sync()/smp_mb()
489  *      105:   ppc_msgsnd() -> 42
490  *       42: local_paca->kvm_hstate.host_ipi == 0 // IPI ignored
491  *      105: // hangs waiting on 42 to process messages/call_single_queue
492  *
493  * We also issue an smp_mb() at the end of kvmppc_clear_host_ipi(). This is
494  * to guard against sequences such as the following (as well as to create
495  * a read-side pairing with the barrier in kvmppc_set_host_ipi()):
496  *
497  *      CPU
498  *        X: smp_muxed_ipi_set_message():
499  *        X:   smp_mb()
500  *        X:   message[RESCHEDULE] = 1
501  *        X: doorbell_global_ipi(42):
502  *        X:   kvmppc_set_host_ipi(42)
503  *        X:   ppc_msgsnd_sync()/smp_mb()
504  *        X:   ppc_msgsnd() -> 42
505  *       42: doorbell_exception(): // from CPU X
506  *       42:   ppc_msgsync()
507  *           // STORE DEFERRED DUE TO RE-ORDERING
508  *    -- 42:   kvmppc_clear_host_ipi(42)
509  *    |  42: smp_ipi_demux_relaxed()
510  *    | 105: smp_muxed_ipi_set_message():
511  *    | 105:   smb_mb()
512  *    | 105:   message[CALL_FUNCTION] = 1
513  *    | 105: doorbell_global_ipi(42):
514  *    | 105:   kvmppc_set_host_ipi(42)
515  *    |      // RE-ORDERED STORE COMPLETES
516  *    -> 42:   kvmppc_clear_host_ipi(42)
517  *       42: // returns to executing guest
518  *      105:   ppc_msgsnd_sync()/smp_mb()
519  *      105:   ppc_msgsnd() -> 42
520  *       42: local_paca->kvm_hstate.host_ipi == 0 // IPI ignored
521  *      105: // hangs waiting on 42 to process messages/call_single_queue
522  */
523 static inline void kvmppc_set_host_ipi(int cpu)
524 {
525 	/*
526 	 * order stores of IPI messages vs. setting of host_ipi flag
527 	 *
528 	 * pairs with the barrier in kvmppc_clear_host_ipi()
529 	 */
530 	smp_mb();
531 	paca_ptrs[cpu]->kvm_hstate.host_ipi = 1;
532 }
533 
534 static inline void kvmppc_clear_host_ipi(int cpu)
535 {
536 	paca_ptrs[cpu]->kvm_hstate.host_ipi = 0;
537 	/*
538 	 * order clearing of host_ipi flag vs. processing of IPI messages
539 	 *
540 	 * pairs with the barrier in kvmppc_set_host_ipi()
541 	 */
542 	smp_mb();
543 }
544 
545 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
546 {
547 	vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
548 }
549 
550 extern void kvm_hv_vm_activated(void);
551 extern void kvm_hv_vm_deactivated(void);
552 extern bool kvm_hv_mode_active(void);
553 
554 extern void kvmppc_check_need_tlb_flush(struct kvm *kvm, int pcpu,
555 					struct kvm_nested_guest *nested);
556 
557 #else
558 static inline void __init kvm_cma_reserve(void)
559 {}
560 
561 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
562 {}
563 
564 static inline void kvmppc_set_xive_tima(int cpu,
565 					unsigned long phys_addr,
566 					void __iomem *virt_addr)
567 {}
568 
569 static inline u32 kvmppc_get_xics_latch(void)
570 {
571 	return 0;
572 }
573 
574 static inline void kvmppc_set_host_ipi(int cpu)
575 {}
576 
577 static inline void kvmppc_clear_host_ipi(int cpu)
578 {}
579 
580 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
581 {
582 	kvm_vcpu_kick(vcpu);
583 }
584 
585 static inline bool kvm_hv_mode_active(void)		{ return false; }
586 
587 #endif
588 
589 #ifdef CONFIG_KVM_XICS
590 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
591 {
592 	return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
593 }
594 
595 static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
596 				struct kvm *kvm)
597 {
598 	if (kvm && kvm_irq_bypass)
599 		return kvm->arch.pimap;
600 	return NULL;
601 }
602 
603 extern void kvmppc_alloc_host_rm_ops(void);
604 extern void kvmppc_free_host_rm_ops(void);
605 extern void kvmppc_free_pimap(struct kvm *kvm);
606 extern int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall);
607 extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
608 extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
609 extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
610 extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
611 extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
612 			struct kvm_vcpu *vcpu, u32 cpu);
613 extern void kvmppc_xics_ipi_action(void);
614 extern void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long guest_irq,
615 				   unsigned long host_irq);
616 extern void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
617 				   unsigned long host_irq);
618 extern long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu, __be32 xirr,
619 					struct kvmppc_irq_map *irq_map,
620 					struct kvmppc_passthru_irqmap *pimap,
621 					bool *again);
622 
623 extern int kvmppc_xics_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
624 			       int level, bool line_status);
625 
626 extern int h_ipi_redirect;
627 #else
628 static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
629 				struct kvm *kvm)
630 	{ return NULL; }
631 static inline void kvmppc_alloc_host_rm_ops(void) {};
632 static inline void kvmppc_free_host_rm_ops(void) {};
633 static inline void kvmppc_free_pimap(struct kvm *kvm) {};
634 static inline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
635 	{ return 0; }
636 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
637 	{ return 0; }
638 static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
639 static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
640 	{ return 0; }
641 #endif
642 
643 #ifdef CONFIG_KVM_XIVE
644 /*
645  * Below the first "xive" is the "eXternal Interrupt Virtualization Engine"
646  * ie. P9 new interrupt controller, while the second "xive" is the legacy
647  * "eXternal Interrupt Vector Entry" which is the configuration of an
648  * interrupt on the "xics" interrupt controller on P8 and earlier. Those
649  * two function consume or produce a legacy "XIVE" state from the
650  * new "XIVE" interrupt controller.
651  */
652 extern int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
653 				u32 priority);
654 extern int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
655 				u32 *priority);
656 extern int kvmppc_xive_int_on(struct kvm *kvm, u32 irq);
657 extern int kvmppc_xive_int_off(struct kvm *kvm, u32 irq);
658 extern void kvmppc_xive_init_module(void);
659 extern void kvmppc_xive_exit_module(void);
660 
661 extern int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
662 				    struct kvm_vcpu *vcpu, u32 cpu);
663 extern void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu);
664 extern int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
665 				  struct irq_desc *host_desc);
666 extern int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
667 				  struct irq_desc *host_desc);
668 extern u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu);
669 extern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
670 
671 extern int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
672 			       int level, bool line_status);
673 extern void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu);
674 
675 static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
676 {
677 	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
678 }
679 
680 extern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
681 					   struct kvm_vcpu *vcpu, u32 cpu);
682 extern void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu);
683 extern void kvmppc_xive_native_init_module(void);
684 extern void kvmppc_xive_native_exit_module(void);
685 extern int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu,
686 				     union kvmppc_one_reg *val);
687 extern int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu,
688 				     union kvmppc_one_reg *val);
689 extern bool kvmppc_xive_native_supported(void);
690 
691 #else
692 static inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
693 				       u32 priority) { return -1; }
694 static inline int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
695 				       u32 *priority) { return -1; }
696 static inline int kvmppc_xive_int_on(struct kvm *kvm, u32 irq) { return -1; }
697 static inline int kvmppc_xive_int_off(struct kvm *kvm, u32 irq) { return -1; }
698 static inline void kvmppc_xive_init_module(void) { }
699 static inline void kvmppc_xive_exit_module(void) { }
700 
701 static inline int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
702 					   struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
703 static inline void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
704 static inline int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
705 					 struct irq_desc *host_desc) { return -ENODEV; }
706 static inline int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
707 					 struct irq_desc *host_desc) { return -ENODEV; }
708 static inline u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu) { return 0; }
709 static inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { return -ENOENT; }
710 
711 static inline int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
712 				      int level, bool line_status) { return -ENODEV; }
713 static inline void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) { }
714 
715 static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
716 	{ return 0; }
717 static inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
718 			  struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
719 static inline void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
720 static inline void kvmppc_xive_native_init_module(void) { }
721 static inline void kvmppc_xive_native_exit_module(void) { }
722 static inline int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu,
723 					    union kvmppc_one_reg *val)
724 { return 0; }
725 static inline int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu,
726 					    union kvmppc_one_reg *val)
727 { return -ENOENT; }
728 
729 #endif /* CONFIG_KVM_XIVE */
730 
731 #if defined(CONFIG_PPC_POWERNV) && defined(CONFIG_KVM_BOOK3S_64_HANDLER)
732 static inline bool xics_on_xive(void)
733 {
734 	return xive_enabled() && cpu_has_feature(CPU_FTR_HVMODE);
735 }
736 #else
737 static inline bool xics_on_xive(void)
738 {
739 	return false;
740 }
741 #endif
742 
743 /*
744  * Prototypes for functions called only from assembler code.
745  * Having prototypes reduces sparse errors.
746  */
747 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
748 			 unsigned long ioba, unsigned long tce);
749 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
750 				  unsigned long liobn, unsigned long ioba,
751 				  unsigned long tce_list, unsigned long npages);
752 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
753 			   unsigned long liobn, unsigned long ioba,
754 			   unsigned long tce_value, unsigned long npages);
755 long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
756                             unsigned int yield_count);
757 long kvmppc_h_random(struct kvm_vcpu *vcpu);
758 void kvmhv_commence_exit(int trap);
759 void kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu);
760 void kvmppc_subcore_enter_guest(void);
761 void kvmppc_subcore_exit_guest(void);
762 long kvmppc_realmode_hmi_handler(void);
763 long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
764                     long pte_index, unsigned long pteh, unsigned long ptel);
765 long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
766                      unsigned long pte_index, unsigned long avpn);
767 long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu);
768 long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
769                       unsigned long pte_index, unsigned long avpn,
770                       unsigned long va);
771 long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
772                    unsigned long pte_index);
773 long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
774                         unsigned long pte_index);
775 long kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
776                         unsigned long pte_index);
777 long kvmppc_rm_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
778 			   unsigned long dest, unsigned long src);
779 long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
780                           unsigned long slb_v, unsigned int status, bool data);
781 unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu);
782 unsigned long kvmppc_rm_h_xirr_x(struct kvm_vcpu *vcpu);
783 unsigned long kvmppc_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
784 int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
785                     unsigned long mfrr);
786 int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
787 int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
788 void kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu);
789 
790 /*
791  * Host-side operations we want to set up while running in real
792  * mode in the guest operating on the xics.
793  * Currently only VCPU wakeup is supported.
794  */
795 
796 union kvmppc_rm_state {
797 	unsigned long raw;
798 	struct {
799 		u32 in_host;
800 		u32 rm_action;
801 	};
802 };
803 
804 struct kvmppc_host_rm_core {
805 	union kvmppc_rm_state rm_state;
806 	void *rm_data;
807 	char pad[112];
808 };
809 
810 struct kvmppc_host_rm_ops {
811 	struct kvmppc_host_rm_core	*rm_core;
812 	void		(*vcpu_kick)(struct kvm_vcpu *vcpu);
813 };
814 
815 extern struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
816 
817 static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
818 {
819 #ifdef CONFIG_KVM_BOOKE_HV
820 	return mfspr(SPRN_GEPR);
821 #elif defined(CONFIG_BOOKE)
822 	return vcpu->arch.epr;
823 #else
824 	return 0;
825 #endif
826 }
827 
828 static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
829 {
830 #ifdef CONFIG_KVM_BOOKE_HV
831 	mtspr(SPRN_GEPR, epr);
832 #elif defined(CONFIG_BOOKE)
833 	vcpu->arch.epr = epr;
834 #endif
835 }
836 
837 #ifdef CONFIG_KVM_MPIC
838 
839 void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
840 int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
841 			     u32 cpu);
842 void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
843 
844 #else
845 
846 static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
847 {
848 }
849 
850 static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
851 		struct kvm_vcpu *vcpu, u32 cpu)
852 {
853 	return -EINVAL;
854 }
855 
856 static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
857 		struct kvm_vcpu *vcpu)
858 {
859 }
860 
861 #endif /* CONFIG_KVM_MPIC */
862 
863 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
864 			      struct kvm_config_tlb *cfg);
865 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
866 			     struct kvm_dirty_tlb *cfg);
867 
868 long kvmppc_alloc_lpid(void);
869 void kvmppc_claim_lpid(long lpid);
870 void kvmppc_free_lpid(long lpid);
871 void kvmppc_init_lpid(unsigned long nr_lpids);
872 
873 static inline void kvmppc_mmu_flush_icache(kvm_pfn_t pfn)
874 {
875 	struct page *page;
876 	/*
877 	 * We can only access pages that the kernel maps
878 	 * as memory. Bail out for unmapped ones.
879 	 */
880 	if (!pfn_valid(pfn))
881 		return;
882 
883 	/* Clear i-cache for new pages */
884 	page = pfn_to_page(pfn);
885 	if (!test_bit(PG_arch_1, &page->flags)) {
886 		flush_dcache_icache_page(page);
887 		set_bit(PG_arch_1, &page->flags);
888 	}
889 }
890 
891 /*
892  * Shared struct helpers. The shared struct can be little or big endian,
893  * depending on the guest endianness. So expose helpers to all of them.
894  */
895 static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
896 {
897 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
898 	/* Only Book3S_64 PR supports bi-endian for now */
899 	return vcpu->arch.shared_big_endian;
900 #elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
901 	/* Book3s_64 HV on little endian is always little endian */
902 	return false;
903 #else
904 	return true;
905 #endif
906 }
907 
908 #define SPRNG_WRAPPER_GET(reg, bookehv_spr)				\
909 static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu)		\
910 {									\
911 	return mfspr(bookehv_spr);					\
912 }									\
913 
914 #define SPRNG_WRAPPER_SET(reg, bookehv_spr)				\
915 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val)	\
916 {									\
917 	mtspr(bookehv_spr, val);						\
918 }									\
919 
920 #define SHARED_WRAPPER_GET(reg, size)					\
921 static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu)		\
922 {									\
923 	if (kvmppc_shared_big_endian(vcpu))				\
924 	       return be##size##_to_cpu(vcpu->arch.shared->reg);	\
925 	else								\
926 	       return le##size##_to_cpu(vcpu->arch.shared->reg);	\
927 }									\
928 
929 #define SHARED_WRAPPER_SET(reg, size)					\
930 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val)	\
931 {									\
932 	if (kvmppc_shared_big_endian(vcpu))				\
933 	       vcpu->arch.shared->reg = cpu_to_be##size(val);		\
934 	else								\
935 	       vcpu->arch.shared->reg = cpu_to_le##size(val);		\
936 }									\
937 
938 #define SHARED_WRAPPER(reg, size)					\
939 	SHARED_WRAPPER_GET(reg, size)					\
940 	SHARED_WRAPPER_SET(reg, size)					\
941 
942 #define SPRNG_WRAPPER(reg, bookehv_spr)					\
943 	SPRNG_WRAPPER_GET(reg, bookehv_spr)				\
944 	SPRNG_WRAPPER_SET(reg, bookehv_spr)				\
945 
946 #ifdef CONFIG_KVM_BOOKE_HV
947 
948 #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr)			\
949 	SPRNG_WRAPPER(reg, bookehv_spr)					\
950 
951 #else
952 
953 #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr)			\
954 	SHARED_WRAPPER(reg, size)					\
955 
956 #endif
957 
958 SHARED_WRAPPER(critical, 64)
959 SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
960 SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
961 SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
962 SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
963 SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
964 SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
965 SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
966 SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
967 SHARED_WRAPPER_GET(msr, 64)
968 static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
969 {
970 	if (kvmppc_shared_big_endian(vcpu))
971 	       vcpu->arch.shared->msr = cpu_to_be64(val);
972 	else
973 	       vcpu->arch.shared->msr = cpu_to_le64(val);
974 }
975 SHARED_WRAPPER(dsisr, 32)
976 SHARED_WRAPPER(int_pending, 32)
977 SHARED_WRAPPER(sprg4, 64)
978 SHARED_WRAPPER(sprg5, 64)
979 SHARED_WRAPPER(sprg6, 64)
980 SHARED_WRAPPER(sprg7, 64)
981 
982 static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
983 {
984 	if (kvmppc_shared_big_endian(vcpu))
985 	       return be32_to_cpu(vcpu->arch.shared->sr[nr]);
986 	else
987 	       return le32_to_cpu(vcpu->arch.shared->sr[nr]);
988 }
989 
990 static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
991 {
992 	if (kvmppc_shared_big_endian(vcpu))
993 	       vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
994 	else
995 	       vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
996 }
997 
998 /*
999  * Please call after prepare_to_enter. This function puts the lazy ee and irq
1000  * disabled tracking state back to normal mode, without actually enabling
1001  * interrupts.
1002  */
1003 static inline void kvmppc_fix_ee_before_entry(void)
1004 {
1005 	trace_hardirqs_on();
1006 
1007 #ifdef CONFIG_PPC64
1008 	/*
1009 	 * To avoid races, the caller must have gone directly from having
1010 	 * interrupts fully-enabled to hard-disabled.
1011 	 */
1012 	WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
1013 
1014 	/* Only need to enable IRQs by hard enabling them after this */
1015 	local_paca->irq_happened = 0;
1016 	irq_soft_mask_set(IRQS_ENABLED);
1017 #endif
1018 }
1019 
1020 static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
1021 {
1022 	ulong ea;
1023 	ulong msr_64bit = 0;
1024 
1025 	ea = kvmppc_get_gpr(vcpu, rb);
1026 	if (ra)
1027 		ea += kvmppc_get_gpr(vcpu, ra);
1028 
1029 #if defined(CONFIG_PPC_BOOK3E_64)
1030 	msr_64bit = MSR_CM;
1031 #elif defined(CONFIG_PPC_BOOK3S_64)
1032 	msr_64bit = MSR_SF;
1033 #endif
1034 
1035 	if (!(kvmppc_get_msr(vcpu) & msr_64bit))
1036 		ea = (uint32_t)ea;
1037 
1038 	return ea;
1039 }
1040 
1041 extern void xics_wake_cpu(int cpu);
1042 
1043 #endif /* __POWERPC_KVM_PPC_H__ */
1044