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