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