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