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