1 /* 2 * Copyright 2008 IBM Corporation. 3 * Authors: Hollis Blanchard <hollisb@us.ibm.com> 4 * 5 * This work is licensed under the GNU GPL license version 2 or later. 6 * 7 */ 8 9 #ifndef KVM_PPC_H 10 #define KVM_PPC_H 11 12 #include "exec/hwaddr.h" 13 #include "cpu.h" 14 15 #define TYPE_HOST_POWERPC_CPU POWERPC_CPU_TYPE_NAME("host") 16 17 #ifdef CONFIG_KVM 18 19 uint32_t kvmppc_get_tbfreq(void); 20 uint64_t kvmppc_get_clockfreq(void); 21 bool kvmppc_get_host_model(char **buf); 22 bool kvmppc_get_host_serial(char **buf); 23 int kvmppc_get_hasidle(CPUPPCState *env); 24 int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len); 25 int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level); 26 void kvmppc_enable_logical_ci_hcalls(void); 27 void kvmppc_enable_set_mode_hcall(void); 28 void kvmppc_enable_clear_ref_mod_hcalls(void); 29 void kvmppc_enable_h_page_init(void); 30 void kvmppc_enable_h_rpt_invalidate(void); 31 void kvmppc_set_papr(PowerPCCPU *cpu); 32 int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr); 33 void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy); 34 bool kvmppc_get_fwnmi(void); 35 int kvmppc_set_fwnmi(PowerPCCPU *cpu); 36 int kvmppc_smt_threads(void); 37 void kvmppc_error_append_smt_possible_hint(Error *const *errp); 38 int kvmppc_set_smt_threads(int smt); 39 int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits); 40 int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits); 41 int kvmppc_set_tcr(PowerPCCPU *cpu); 42 int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu); 43 target_ulong kvmppc_configure_v3_mmu(PowerPCCPU *cpu, 44 bool radix, bool gtse, 45 uint64_t proc_tbl); 46 #ifndef CONFIG_USER_ONLY 47 bool kvmppc_spapr_use_multitce(void); 48 int kvmppc_spapr_enable_inkernel_multitce(void); 49 void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift, 50 uint64_t bus_offset, uint32_t nb_table, 51 int *pfd, bool need_vfio); 52 int kvmppc_remove_spapr_tce(void *table, int pfd, uint32_t window_size); 53 int kvmppc_reset_htab(int shift_hint); 54 uint64_t kvmppc_vrma_limit(unsigned int hash_shift); 55 bool kvmppc_has_cap_spapr_vfio(void); 56 #endif /* !CONFIG_USER_ONLY */ 57 bool kvmppc_has_cap_epr(void); 58 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function); 59 int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp); 60 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns); 61 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index, 62 uint16_t n_valid, uint16_t n_invalid, Error **errp); 63 void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n); 64 void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1); 65 bool kvmppc_has_cap_fixup_hcalls(void); 66 bool kvmppc_has_cap_htm(void); 67 bool kvmppc_has_cap_mmu_radix(void); 68 bool kvmppc_has_cap_mmu_hash_v3(void); 69 bool kvmppc_has_cap_xive(void); 70 int kvmppc_get_cap_safe_cache(void); 71 int kvmppc_get_cap_safe_bounds_check(void); 72 int kvmppc_get_cap_safe_indirect_branch(void); 73 int kvmppc_get_cap_count_cache_flush_assist(void); 74 bool kvmppc_has_cap_nested_kvm_hv(void); 75 int kvmppc_set_cap_nested_kvm_hv(int enable); 76 int kvmppc_get_cap_large_decr(void); 77 int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable); 78 int kvmppc_has_cap_rpt_invalidate(void); 79 bool kvmppc_supports_ail_3(void); 80 int kvmppc_enable_hwrng(void); 81 int kvmppc_put_books_sregs(PowerPCCPU *cpu); 82 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void); 83 void kvmppc_check_papr_resize_hpt(Error **errp); 84 int kvmppc_resize_hpt_prepare(PowerPCCPU *cpu, target_ulong flags, int shift); 85 int kvmppc_resize_hpt_commit(PowerPCCPU *cpu, target_ulong flags, int shift); 86 bool kvmppc_pvr_workaround_required(PowerPCCPU *cpu); 87 88 bool kvmppc_hpt_needs_host_contiguous_pages(void); 89 void kvm_check_mmu(PowerPCCPU *cpu, Error **errp); 90 void kvmppc_set_reg_ppc_online(PowerPCCPU *cpu, unsigned int online); 91 void kvmppc_set_reg_tb_offset(PowerPCCPU *cpu, int64_t tb_offset); 92 93 int kvm_handle_nmi(PowerPCCPU *cpu, struct kvm_run *run); 94 95 #else 96 97 static inline uint32_t kvmppc_get_tbfreq(void) 98 { 99 return 0; 100 } 101 102 static inline bool kvmppc_get_host_model(char **buf) 103 { 104 return false; 105 } 106 107 static inline bool kvmppc_get_host_serial(char **buf) 108 { 109 return false; 110 } 111 112 static inline uint64_t kvmppc_get_clockfreq(void) 113 { 114 return 0; 115 } 116 117 static inline uint32_t kvmppc_get_vmx(void) 118 { 119 return 0; 120 } 121 122 static inline uint32_t kvmppc_get_dfp(void) 123 { 124 return 0; 125 } 126 127 static inline int kvmppc_get_hasidle(CPUPPCState *env) 128 { 129 return 0; 130 } 131 132 static inline int kvmppc_get_hypercall(CPUPPCState *env, 133 uint8_t *buf, int buf_len) 134 { 135 return -1; 136 } 137 138 static inline int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level) 139 { 140 return -1; 141 } 142 143 static inline void kvmppc_enable_logical_ci_hcalls(void) 144 { 145 } 146 147 static inline void kvmppc_enable_set_mode_hcall(void) 148 { 149 } 150 151 static inline void kvmppc_enable_clear_ref_mod_hcalls(void) 152 { 153 } 154 155 static inline void kvmppc_enable_h_page_init(void) 156 { 157 } 158 159 static inline void kvmppc_enable_h_rpt_invalidate(void) 160 { 161 g_assert_not_reached(); 162 } 163 164 static inline void kvmppc_set_papr(PowerPCCPU *cpu) 165 { 166 } 167 168 static inline int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr) 169 { 170 return 0; 171 } 172 173 static inline void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy) 174 { 175 } 176 177 static inline bool kvmppc_get_fwnmi(void) 178 { 179 return false; 180 } 181 182 static inline int kvmppc_set_fwnmi(PowerPCCPU *cpu) 183 { 184 return -1; 185 } 186 187 static inline int kvmppc_smt_threads(void) 188 { 189 return 1; 190 } 191 192 static inline void kvmppc_error_append_smt_possible_hint(Error *const *errp) 193 { 194 return; 195 } 196 197 static inline int kvmppc_set_smt_threads(int smt) 198 { 199 return 0; 200 } 201 202 static inline int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) 203 { 204 return 0; 205 } 206 207 static inline int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) 208 { 209 return 0; 210 } 211 212 static inline int kvmppc_set_tcr(PowerPCCPU *cpu) 213 { 214 return 0; 215 } 216 217 static inline int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu) 218 { 219 return -1; 220 } 221 222 static inline target_ulong kvmppc_configure_v3_mmu(PowerPCCPU *cpu, 223 bool radix, bool gtse, 224 uint64_t proc_tbl) 225 { 226 return 0; 227 } 228 229 static inline void kvmppc_set_reg_ppc_online(PowerPCCPU *cpu, 230 unsigned int online) 231 { 232 return; 233 } 234 235 static inline void kvmppc_set_reg_tb_offset(PowerPCCPU *cpu, int64_t tb_offset) 236 { 237 } 238 239 #ifndef CONFIG_USER_ONLY 240 static inline bool kvmppc_spapr_use_multitce(void) 241 { 242 return false; 243 } 244 245 static inline int kvmppc_spapr_enable_inkernel_multitce(void) 246 { 247 return -1; 248 } 249 250 static inline void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift, 251 uint64_t bus_offset, 252 uint32_t nb_table, 253 int *pfd, bool need_vfio) 254 { 255 return NULL; 256 } 257 258 static inline int kvmppc_remove_spapr_tce(void *table, int pfd, 259 uint32_t nb_table) 260 { 261 return -1; 262 } 263 264 static inline int kvmppc_reset_htab(int shift_hint) 265 { 266 return 0; 267 } 268 269 static inline uint64_t kvmppc_vrma_limit(unsigned int hash_shift) 270 { 271 g_assert_not_reached(); 272 } 273 274 static inline bool kvmppc_hpt_needs_host_contiguous_pages(void) 275 { 276 return false; 277 } 278 279 static inline void kvm_check_mmu(PowerPCCPU *cpu, Error **errp) 280 { 281 } 282 283 static inline bool kvmppc_has_cap_spapr_vfio(void) 284 { 285 return false; 286 } 287 288 static inline void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, 289 hwaddr ptex, int n) 290 { 291 abort(); 292 } 293 294 static inline void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1) 295 { 296 abort(); 297 } 298 299 #endif /* !CONFIG_USER_ONLY */ 300 301 static inline bool kvmppc_has_cap_epr(void) 302 { 303 return false; 304 } 305 306 static inline int kvmppc_define_rtas_kernel_token(uint32_t token, 307 const char *function) 308 { 309 return -1; 310 } 311 312 static inline int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp) 313 { 314 return -1; 315 } 316 317 static inline int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, 318 int64_t max_ns) 319 { 320 abort(); 321 } 322 323 static inline int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index, 324 uint16_t n_valid, uint16_t n_invalid, 325 Error **errp) 326 { 327 abort(); 328 } 329 330 static inline bool kvmppc_has_cap_fixup_hcalls(void) 331 { 332 abort(); 333 } 334 335 static inline bool kvmppc_has_cap_htm(void) 336 { 337 return false; 338 } 339 340 static inline bool kvmppc_has_cap_mmu_radix(void) 341 { 342 return false; 343 } 344 345 static inline bool kvmppc_has_cap_mmu_hash_v3(void) 346 { 347 return false; 348 } 349 350 static inline bool kvmppc_has_cap_xive(void) 351 { 352 return false; 353 } 354 355 static inline int kvmppc_get_cap_safe_cache(void) 356 { 357 return 0; 358 } 359 360 static inline int kvmppc_get_cap_safe_bounds_check(void) 361 { 362 return 0; 363 } 364 365 static inline int kvmppc_get_cap_safe_indirect_branch(void) 366 { 367 return 0; 368 } 369 370 static inline int kvmppc_get_cap_count_cache_flush_assist(void) 371 { 372 return 0; 373 } 374 375 static inline bool kvmppc_has_cap_nested_kvm_hv(void) 376 { 377 return false; 378 } 379 380 static inline int kvmppc_set_cap_nested_kvm_hv(int enable) 381 { 382 return -1; 383 } 384 385 static inline int kvmppc_get_cap_large_decr(void) 386 { 387 return 0; 388 } 389 390 static inline int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable) 391 { 392 return -1; 393 } 394 395 static inline int kvmppc_has_cap_rpt_invalidate(void) 396 { 397 return false; 398 } 399 400 static inline bool kvmppc_supports_ail_3(void) 401 { 402 return false; 403 } 404 405 static inline int kvmppc_enable_hwrng(void) 406 { 407 return -1; 408 } 409 410 static inline int kvmppc_put_books_sregs(PowerPCCPU *cpu) 411 { 412 abort(); 413 } 414 415 static inline PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void) 416 { 417 return NULL; 418 } 419 420 static inline void kvmppc_check_papr_resize_hpt(Error **errp) 421 { 422 return; 423 } 424 425 static inline int kvmppc_resize_hpt_prepare(PowerPCCPU *cpu, 426 target_ulong flags, int shift) 427 { 428 return -ENOSYS; 429 } 430 431 static inline int kvmppc_resize_hpt_commit(PowerPCCPU *cpu, 432 target_ulong flags, int shift) 433 { 434 return -ENOSYS; 435 } 436 437 static inline bool kvmppc_pvr_workaround_required(PowerPCCPU *cpu) 438 { 439 return false; 440 } 441 442 #endif 443 444 #ifndef CONFIG_KVM 445 446 #define kvmppc_eieio() do { } while (0) 447 448 static inline void kvmppc_dcbst_range(PowerPCCPU *cpu, uint8_t *addr, int len) 449 { 450 } 451 452 static inline void kvmppc_icbi_range(PowerPCCPU *cpu, uint8_t *addr, int len) 453 { 454 } 455 456 #else /* CONFIG_KVM */ 457 458 #define kvmppc_eieio() \ 459 do { \ 460 if (kvm_enabled()) { \ 461 asm volatile("eieio" : : : "memory"); \ 462 } \ 463 } while (0) 464 465 /* Store data cache blocks back to memory */ 466 static inline void kvmppc_dcbst_range(PowerPCCPU *cpu, uint8_t *addr, int len) 467 { 468 uint8_t *p; 469 470 for (p = addr; p < addr + len; p += cpu->env.dcache_line_size) { 471 asm volatile("dcbst 0,%0" : : "r"(p) : "memory"); 472 } 473 } 474 475 /* Invalidate instruction cache blocks */ 476 static inline void kvmppc_icbi_range(PowerPCCPU *cpu, uint8_t *addr, int len) 477 { 478 uint8_t *p; 479 480 for (p = addr; p < addr + len; p += cpu->env.icache_line_size) { 481 asm volatile("icbi 0,%0" : : "r"(p)); 482 } 483 } 484 485 #endif /* CONFIG_KVM */ 486 487 #endif /* KVM_PPC_H */ 488