1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * Copyright IBM Corp. 2008
9 #ifndef __POWERPC_KVM_PPC_H__
10 #define __POWERPC_KVM_PPC_H__
12 /* This file exists just so we can dereference kvm_vcpu, avoiding nested header
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>
24 #include <asm/kvm_booke.h>
26 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
29 #include <asm/cpu_has_feature.h>
34 * KVMPPC_INST_SW_BREAKPOINT is debug Instruction
35 * for supporting software breakpoint.
37 #define KVMPPC_INST_SW_BREAKPOINT 0x00dddd00
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 */
47 enum instruction_fetch_type {
49 INST_SC, /* system call */
53 XLATE_INST, /* translate instruction address */
54 XLATE_DATA /* translate data address */
57 enum xlate_readwrite {
58 XLATE_READ, /* check for read permissions */
59 XLATE_WRITE /* check for write permissions */
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);
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);
87 extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
88 enum instruction_fetch_type type,
91 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
93 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
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);
105 /* Core-specific hooks */
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,
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);
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);
126 extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
127 extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
129 extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
130 extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
132 extern void kvmppc_core_queue_machine_check(struct kvm_vcpu *vcpu,
134 extern void kvmppc_core_queue_syscall(struct kvm_vcpu *vcpu);
135 extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu,
137 extern void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu,
139 extern void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu,
141 extern void kvmppc_core_queue_vsx_unavail(struct kvm_vcpu *vcpu,
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,
151 extern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
155 extern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu);
156 extern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
159 extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
160 extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
162 extern int kvmppc_booke_init(void);
163 extern void kvmppc_booke_exit(void);
165 extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
166 extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
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);
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,
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);
219 extern int kvmppc_bookehv_init(void);
220 extern void kvmppc_bookehv_exit(void);
222 extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
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);
230 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
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);
236 extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
238 extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
240 extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
241 extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
243 void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu);
244 void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu);
246 union kvmppc_one_reg {
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,
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,
315 int (*store_to_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
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);
325 extern struct kvmppc_ops *kvmppc_hv_ops;
326 extern struct kvmppc_ops *kvmppc_pr_ops;
328 static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
329 enum instruction_fetch_type type, ppc_inst_t *inst)
331 int ret = EMULATE_DONE;
334 /* Load the instruction manually if it failed to do so in the
336 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
337 ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
339 /* Write fetch_failed unswapped if the fetch failed */
340 if (ret != EMULATE_DONE) {
341 *inst = ppc_inst(KVM_INST_FETCH_FAILED);
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);
354 *inst = ppc_inst_prefix(prefix, suffix);
359 fetched_inst = kvmppc_need_byteswap(vcpu) ?
360 swab32(vcpu->arch.last_inst) :
361 vcpu->arch.last_inst;
362 *inst = ppc_inst(fetched_inst);
366 static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
368 return kvm->arch.kvm_ops == kvmppc_hv_ops;
371 extern int kvmppc_hwrng_present(void);
374 * Cuts out inst bits with ordering according to spec.
375 * That means the leftmost bit is zero. All given bits are included.
377 static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
384 mask = (1 << (lsb - msb + 1)) - 1;
385 r = (inst >> (63 - lsb)) & mask;
391 * Replaces inst bits with ordering according to spec.
393 static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
400 mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
401 r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
406 #define one_reg_size(id) \
407 (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
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; \
420 #define set_reg_val(id, val) ({ \
422 switch (one_reg_size(id)) { \
423 case 4: __v = (val).wval; break; \
424 case 8: __v = (val).dval; break; \
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);
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);
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 *);
441 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
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)
449 paca_ptrs[cpu]->kvm_hstate.xics_phys = (void __iomem *)addr;
452 static inline void kvmppc_set_xive_tima(int cpu,
453 unsigned long phys_addr,
454 void __iomem *virt_addr)
456 paca_ptrs[cpu]->kvm_hstate.xive_tima_phys = (void __iomem *)phys_addr;
457 paca_ptrs[cpu]->kvm_hstate.xive_tima_virt = virt_addr;
460 static inline u32 kvmppc_get_xics_latch(void)
464 xirr = get_paca()->kvm_hstate.saved_xirr;
465 get_paca()->kvm_hstate.saved_xirr = 0;
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).
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.
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:
488 * X: smp_muxed_ipi_set_message():
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
497 * 105: smp_muxed_ipi_set_message():
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
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()):
518 * X: smp_muxed_ipi_set_message():
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
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():
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
543 static inline void kvmppc_set_host_ipi(int cpu)
546 * order stores of IPI messages vs. setting of host_ipi flag
548 * pairs with the barrier in kvmppc_clear_host_ipi()
551 WRITE_ONCE(paca_ptrs[cpu]->kvm_hstate.host_ipi, 1);
554 static inline void kvmppc_clear_host_ipi(int cpu)
556 WRITE_ONCE(paca_ptrs[cpu]->kvm_hstate.host_ipi, 0);
558 * order clearing of host_ipi flag vs. processing of IPI messages
560 * pairs with the barrier in kvmppc_set_host_ipi()
565 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
567 vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
570 extern void kvm_hv_vm_activated(void);
571 extern void kvm_hv_vm_deactivated(void);
572 extern bool kvm_hv_mode_active(void);
574 extern void kvmppc_check_need_tlb_flush(struct kvm *kvm, int pcpu);
577 static inline void __init kvm_cma_reserve(void)
580 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
583 static inline void kvmppc_set_xive_tima(int cpu,
584 unsigned long phys_addr,
585 void __iomem *virt_addr)
588 static inline u32 kvmppc_get_xics_latch(void)
593 static inline void kvmppc_set_host_ipi(int cpu)
596 static inline void kvmppc_clear_host_ipi(int cpu)
599 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
604 static inline bool kvm_hv_mode_active(void) { return false; }
608 #ifdef CONFIG_PPC_PSERIES
609 static inline bool kvmhv_on_pseries(void)
611 return !cpu_has_feature(CPU_FTR_HVMODE);
614 static inline bool kvmhv_on_pseries(void)
620 #ifdef CONFIG_KVM_XICS
621 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
623 return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
626 static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
629 if (kvm && kvm_irq_bypass)
630 return kvm->arch.pimap;
634 extern void kvmppc_alloc_host_rm_ops(void);
635 extern void kvmppc_free_host_rm_ops(void);
636 extern void kvmppc_free_pimap(struct kvm *kvm);
637 extern int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall);
638 extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
639 extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
640 extern int kvmppc_xive_xics_hcall(struct kvm_vcpu *vcpu, u32 req);
641 extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
642 extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
643 extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
644 struct kvm_vcpu *vcpu, u32 cpu);
645 extern void kvmppc_xics_ipi_action(void);
646 extern void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long guest_irq,
647 unsigned long host_irq);
648 extern void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
649 unsigned long host_irq);
650 extern long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu, __be32 xirr,
651 struct kvmppc_irq_map *irq_map,
652 struct kvmppc_passthru_irqmap *pimap,
655 extern int kvmppc_xics_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
656 int level, bool line_status);
658 extern int h_ipi_redirect;
660 static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
663 static inline void kvmppc_alloc_host_rm_ops(void) {}
664 static inline void kvmppc_free_host_rm_ops(void) {}
665 static inline void kvmppc_free_pimap(struct kvm *kvm) {}
666 static inline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
668 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
670 static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
671 static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
673 static inline int kvmppc_xive_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
677 #ifdef CONFIG_KVM_XIVE
679 * Below the first "xive" is the "eXternal Interrupt Virtualization Engine"
680 * ie. P9 new interrupt controller, while the second "xive" is the legacy
681 * "eXternal Interrupt Vector Entry" which is the configuration of an
682 * interrupt on the "xics" interrupt controller on P8 and earlier. Those
683 * two function consume or produce a legacy "XIVE" state from the
684 * new "XIVE" interrupt controller.
686 extern int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
688 extern int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
690 extern int kvmppc_xive_int_on(struct kvm *kvm, u32 irq);
691 extern int kvmppc_xive_int_off(struct kvm *kvm, u32 irq);
693 extern int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
694 struct kvm_vcpu *vcpu, u32 cpu);
695 extern void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu);
696 extern int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
697 unsigned long host_irq);
698 extern int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
699 unsigned long host_irq);
700 extern u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu);
701 extern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
703 extern int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
704 int level, bool line_status);
705 extern void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu);
706 extern void kvmppc_xive_pull_vcpu(struct kvm_vcpu *vcpu);
707 extern bool kvmppc_xive_rearm_escalation(struct kvm_vcpu *vcpu);
709 static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
711 return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
714 extern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
715 struct kvm_vcpu *vcpu, u32 cpu);
716 extern void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu);
717 extern int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu,
718 union kvmppc_one_reg *val);
719 extern int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu,
720 union kvmppc_one_reg *val);
721 extern bool kvmppc_xive_native_supported(void);
724 static inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
725 u32 priority) { return -1; }
726 static inline int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
727 u32 *priority) { return -1; }
728 static inline int kvmppc_xive_int_on(struct kvm *kvm, u32 irq) { return -1; }
729 static inline int kvmppc_xive_int_off(struct kvm *kvm, u32 irq) { return -1; }
731 static inline int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
732 struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
733 static inline void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
734 static inline int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
735 struct irq_desc *host_desc) { return -ENODEV; }
736 static inline int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
737 struct irq_desc *host_desc) { return -ENODEV; }
738 static inline u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu) { return 0; }
739 static inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { return -ENOENT; }
741 static inline int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
742 int level, bool line_status) { return -ENODEV; }
743 static inline void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) { }
744 static inline void kvmppc_xive_pull_vcpu(struct kvm_vcpu *vcpu) { }
745 static inline bool kvmppc_xive_rearm_escalation(struct kvm_vcpu *vcpu) { return true; }
747 static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
749 static inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
750 struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
751 static inline void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
752 static inline int kvmppc_xive_native_get_vp(struct kvm_vcpu *vcpu,
753 union kvmppc_one_reg *val)
755 static inline int kvmppc_xive_native_set_vp(struct kvm_vcpu *vcpu,
756 union kvmppc_one_reg *val)
759 #endif /* CONFIG_KVM_XIVE */
761 #if defined(CONFIG_PPC_POWERNV) && defined(CONFIG_KVM_BOOK3S_64_HANDLER)
762 static inline bool xics_on_xive(void)
764 return xive_enabled() && cpu_has_feature(CPU_FTR_HVMODE);
767 static inline bool xics_on_xive(void)
774 * Prototypes for functions called only from assembler code.
775 * Having prototypes reduces sparse errors.
777 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
778 unsigned long ioba, unsigned long tce);
779 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
780 unsigned long liobn, unsigned long ioba,
781 unsigned long tce_list, unsigned long npages);
782 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
783 unsigned long liobn, unsigned long ioba,
784 unsigned long tce_value, unsigned long npages);
785 long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
786 unsigned int yield_count);
787 long kvmppc_rm_h_random(struct kvm_vcpu *vcpu);
788 void kvmhv_commence_exit(int trap);
789 void kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu);
790 void kvmppc_subcore_enter_guest(void);
791 void kvmppc_subcore_exit_guest(void);
792 long kvmppc_realmode_hmi_handler(void);
793 long kvmppc_p9_realmode_hmi_handler(struct kvm_vcpu *vcpu);
794 long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
795 long pte_index, unsigned long pteh, unsigned long ptel);
796 long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
797 unsigned long pte_index, unsigned long avpn);
798 long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu);
799 long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
800 unsigned long pte_index, unsigned long avpn);
801 long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
802 unsigned long pte_index);
803 long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
804 unsigned long pte_index);
805 long kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
806 unsigned long pte_index);
807 long kvmppc_rm_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
808 unsigned long dest, unsigned long src);
809 long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
810 unsigned long slb_v, unsigned int status, bool data);
811 void kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu);
814 * Host-side operations we want to set up while running in real
815 * mode in the guest operating on the xics.
816 * Currently only VCPU wakeup is supported.
819 union kvmppc_rm_state {
827 struct kvmppc_host_rm_core {
828 union kvmppc_rm_state rm_state;
833 struct kvmppc_host_rm_ops {
834 struct kvmppc_host_rm_core *rm_core;
835 void (*vcpu_kick)(struct kvm_vcpu *vcpu);
838 extern struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
840 static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
842 #ifdef CONFIG_KVM_BOOKE_HV
843 return mfspr(SPRN_GEPR);
844 #elif defined(CONFIG_BOOKE)
845 return vcpu->arch.epr;
851 static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
853 #ifdef CONFIG_KVM_BOOKE_HV
854 mtspr(SPRN_GEPR, epr);
855 #elif defined(CONFIG_BOOKE)
856 vcpu->arch.epr = epr;
860 #ifdef CONFIG_KVM_MPIC
862 void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
863 int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
865 void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
869 static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
873 static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
874 struct kvm_vcpu *vcpu, u32 cpu)
879 static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
880 struct kvm_vcpu *vcpu)
884 #endif /* CONFIG_KVM_MPIC */
886 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
887 struct kvm_config_tlb *cfg);
888 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
889 struct kvm_dirty_tlb *cfg);
891 long kvmppc_alloc_lpid(void);
892 void kvmppc_free_lpid(long lpid);
893 void kvmppc_init_lpid(unsigned long nr_lpids);
895 static inline void kvmppc_mmu_flush_icache(kvm_pfn_t pfn)
899 * We can only access pages that the kernel maps
900 * as memory. Bail out for unmapped ones.
905 /* Clear i-cache for new pages */
906 page = pfn_to_page(pfn);
907 if (!test_bit(PG_dcache_clean, &page->flags)) {
908 flush_dcache_icache_page(page);
909 set_bit(PG_dcache_clean, &page->flags);
914 * Shared struct helpers. The shared struct can be little or big endian,
915 * depending on the guest endianness. So expose helpers to all of them.
917 static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
919 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
920 /* Only Book3S_64 PR supports bi-endian for now */
921 return vcpu->arch.shared_big_endian;
922 #elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
923 /* Book3s_64 HV on little endian is always little endian */
930 #define SPRNG_WRAPPER_GET(reg, bookehv_spr) \
931 static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
933 return mfspr(bookehv_spr); \
936 #define SPRNG_WRAPPER_SET(reg, bookehv_spr) \
937 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \
939 mtspr(bookehv_spr, val); \
942 #define SHARED_WRAPPER_GET(reg, size) \
943 static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
945 if (kvmppc_shared_big_endian(vcpu)) \
946 return be##size##_to_cpu(vcpu->arch.shared->reg); \
948 return le##size##_to_cpu(vcpu->arch.shared->reg); \
951 #define SHARED_WRAPPER_SET(reg, size) \
952 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \
954 if (kvmppc_shared_big_endian(vcpu)) \
955 vcpu->arch.shared->reg = cpu_to_be##size(val); \
957 vcpu->arch.shared->reg = cpu_to_le##size(val); \
960 #define SHARED_WRAPPER(reg, size) \
961 SHARED_WRAPPER_GET(reg, size) \
962 SHARED_WRAPPER_SET(reg, size) \
964 #define SPRNG_WRAPPER(reg, bookehv_spr) \
965 SPRNG_WRAPPER_GET(reg, bookehv_spr) \
966 SPRNG_WRAPPER_SET(reg, bookehv_spr) \
968 #ifdef CONFIG_KVM_BOOKE_HV
970 #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
971 SPRNG_WRAPPER(reg, bookehv_spr) \
975 #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
976 SHARED_WRAPPER(reg, size) \
980 SHARED_WRAPPER(critical, 64)
981 SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
982 SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
983 SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
984 SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
985 SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
986 SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
987 SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
988 SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
989 SHARED_WRAPPER_GET(msr, 64)
990 static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
992 if (kvmppc_shared_big_endian(vcpu))
993 vcpu->arch.shared->msr = cpu_to_be64(val);
995 vcpu->arch.shared->msr = cpu_to_le64(val);
997 SHARED_WRAPPER(dsisr, 32)
998 SHARED_WRAPPER(int_pending, 32)
999 SHARED_WRAPPER(sprg4, 64)
1000 SHARED_WRAPPER(sprg5, 64)
1001 SHARED_WRAPPER(sprg6, 64)
1002 SHARED_WRAPPER(sprg7, 64)
1004 static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
1006 if (kvmppc_shared_big_endian(vcpu))
1007 return be32_to_cpu(vcpu->arch.shared->sr[nr]);
1009 return le32_to_cpu(vcpu->arch.shared->sr[nr]);
1012 static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
1014 if (kvmppc_shared_big_endian(vcpu))
1015 vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
1017 vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
1021 * Please call after prepare_to_enter. This function puts the lazy ee and irq
1022 * disabled tracking state back to normal mode, without actually enabling
1025 static inline void kvmppc_fix_ee_before_entry(void)
1027 trace_hardirqs_on();
1031 * To avoid races, the caller must have gone directly from having
1032 * interrupts fully-enabled to hard-disabled.
1034 WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
1036 /* Only need to enable IRQs by hard enabling them after this */
1037 local_paca->irq_happened = 0;
1038 irq_soft_mask_set(IRQS_ENABLED);
1042 static inline void kvmppc_fix_ee_after_exit(void)
1045 /* Only need to enable IRQs by hard enabling them after this */
1046 local_paca->irq_happened = PACA_IRQ_HARD_DIS;
1047 irq_soft_mask_set(IRQS_ALL_DISABLED);
1050 trace_hardirqs_off();
1054 static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
1057 ulong msr_64bit = 0;
1059 ea = kvmppc_get_gpr(vcpu, rb);
1061 ea += kvmppc_get_gpr(vcpu, ra);
1063 #if defined(CONFIG_PPC_BOOK3E_64)
1065 #elif defined(CONFIG_PPC_BOOK3S_64)
1069 if (!(kvmppc_get_msr(vcpu) & msr_64bit))
1075 extern void xics_wake_cpu(int cpu);
1077 #endif /* __POWERPC_KVM_PPC_H__ */