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