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