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