1 /* 2 * QEMU generic PowerPC hardware System Emulator 3 * 4 * Copyright (c) 2003-2007 Jocelyn Mayer 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 #include "cpu.h" 27 #include "hw/irq.h" 28 #include "hw/ppc/ppc.h" 29 #include "hw/ppc/ppc_e500.h" 30 #include "qemu/timer.h" 31 #include "sysemu/cpus.h" 32 #include "qemu/log.h" 33 #include "qemu/main-loop.h" 34 #include "qemu/error-report.h" 35 #include "sysemu/kvm.h" 36 #include "sysemu/runstate.h" 37 #include "kvm_ppc.h" 38 #include "migration/vmstate.h" 39 #include "trace.h" 40 41 //#define PPC_DEBUG_IRQ 42 //#define PPC_DEBUG_TB 43 44 #ifdef PPC_DEBUG_IRQ 45 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__) 46 #else 47 # define LOG_IRQ(...) do { } while (0) 48 #endif 49 50 51 #ifdef PPC_DEBUG_TB 52 # define LOG_TB(...) qemu_log(__VA_ARGS__) 53 #else 54 # define LOG_TB(...) do { } while (0) 55 #endif 56 57 static void cpu_ppc_tb_stop (CPUPPCState *env); 58 static void cpu_ppc_tb_start (CPUPPCState *env); 59 60 void ppc_set_irq(PowerPCCPU *cpu, int n_IRQ, int level) 61 { 62 CPUState *cs = CPU(cpu); 63 CPUPPCState *env = &cpu->env; 64 unsigned int old_pending; 65 bool locked = false; 66 67 /* We may already have the BQL if coming from the reset path */ 68 if (!qemu_mutex_iothread_locked()) { 69 locked = true; 70 qemu_mutex_lock_iothread(); 71 } 72 73 old_pending = env->pending_interrupts; 74 75 if (level) { 76 env->pending_interrupts |= 1 << n_IRQ; 77 cpu_interrupt(cs, CPU_INTERRUPT_HARD); 78 } else { 79 env->pending_interrupts &= ~(1 << n_IRQ); 80 if (env->pending_interrupts == 0) { 81 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 82 } 83 } 84 85 if (old_pending != env->pending_interrupts) { 86 kvmppc_set_interrupt(cpu, n_IRQ, level); 87 } 88 89 90 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32 91 "req %08x\n", __func__, env, n_IRQ, level, 92 env->pending_interrupts, CPU(cpu)->interrupt_request); 93 94 if (locked) { 95 qemu_mutex_unlock_iothread(); 96 } 97 } 98 99 /* PowerPC 6xx / 7xx internal IRQ controller */ 100 static void ppc6xx_set_irq(void *opaque, int pin, int level) 101 { 102 PowerPCCPU *cpu = opaque; 103 CPUPPCState *env = &cpu->env; 104 int cur_level; 105 106 LOG_IRQ("%s: env %p pin %d level %d\n", __func__, 107 env, pin, level); 108 cur_level = (env->irq_input_state >> pin) & 1; 109 /* Don't generate spurious events */ 110 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { 111 CPUState *cs = CPU(cpu); 112 113 switch (pin) { 114 case PPC6xx_INPUT_TBEN: 115 /* Level sensitive - active high */ 116 LOG_IRQ("%s: %s the time base\n", 117 __func__, level ? "start" : "stop"); 118 if (level) { 119 cpu_ppc_tb_start(env); 120 } else { 121 cpu_ppc_tb_stop(env); 122 } 123 case PPC6xx_INPUT_INT: 124 /* Level sensitive - active high */ 125 LOG_IRQ("%s: set the external IRQ state to %d\n", 126 __func__, level); 127 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level); 128 break; 129 case PPC6xx_INPUT_SMI: 130 /* Level sensitive - active high */ 131 LOG_IRQ("%s: set the SMI IRQ state to %d\n", 132 __func__, level); 133 ppc_set_irq(cpu, PPC_INTERRUPT_SMI, level); 134 break; 135 case PPC6xx_INPUT_MCP: 136 /* Negative edge sensitive */ 137 /* XXX: TODO: actual reaction may depends on HID0 status 138 * 603/604/740/750: check HID0[EMCP] 139 */ 140 if (cur_level == 1 && level == 0) { 141 LOG_IRQ("%s: raise machine check state\n", 142 __func__); 143 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1); 144 } 145 break; 146 case PPC6xx_INPUT_CKSTP_IN: 147 /* Level sensitive - active low */ 148 /* XXX: TODO: relay the signal to CKSTP_OUT pin */ 149 /* XXX: Note that the only way to restart the CPU is to reset it */ 150 if (level) { 151 LOG_IRQ("%s: stop the CPU\n", __func__); 152 cs->halted = 1; 153 } 154 break; 155 case PPC6xx_INPUT_HRESET: 156 /* Level sensitive - active low */ 157 if (level) { 158 LOG_IRQ("%s: reset the CPU\n", __func__); 159 cpu_interrupt(cs, CPU_INTERRUPT_RESET); 160 } 161 break; 162 case PPC6xx_INPUT_SRESET: 163 LOG_IRQ("%s: set the RESET IRQ state to %d\n", 164 __func__, level); 165 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level); 166 break; 167 default: 168 /* Unknown pin - do nothing */ 169 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); 170 return; 171 } 172 if (level) 173 env->irq_input_state |= 1 << pin; 174 else 175 env->irq_input_state &= ~(1 << pin); 176 } 177 } 178 179 void ppc6xx_irq_init(PowerPCCPU *cpu) 180 { 181 CPUPPCState *env = &cpu->env; 182 183 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, cpu, 184 PPC6xx_INPUT_NB); 185 } 186 187 #if defined(TARGET_PPC64) 188 /* PowerPC 970 internal IRQ controller */ 189 static void ppc970_set_irq(void *opaque, int pin, int level) 190 { 191 PowerPCCPU *cpu = opaque; 192 CPUPPCState *env = &cpu->env; 193 int cur_level; 194 195 LOG_IRQ("%s: env %p pin %d level %d\n", __func__, 196 env, pin, level); 197 cur_level = (env->irq_input_state >> pin) & 1; 198 /* Don't generate spurious events */ 199 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { 200 CPUState *cs = CPU(cpu); 201 202 switch (pin) { 203 case PPC970_INPUT_INT: 204 /* Level sensitive - active high */ 205 LOG_IRQ("%s: set the external IRQ state to %d\n", 206 __func__, level); 207 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level); 208 break; 209 case PPC970_INPUT_THINT: 210 /* Level sensitive - active high */ 211 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__, 212 level); 213 ppc_set_irq(cpu, PPC_INTERRUPT_THERM, level); 214 break; 215 case PPC970_INPUT_MCP: 216 /* Negative edge sensitive */ 217 /* XXX: TODO: actual reaction may depends on HID0 status 218 * 603/604/740/750: check HID0[EMCP] 219 */ 220 if (cur_level == 1 && level == 0) { 221 LOG_IRQ("%s: raise machine check state\n", 222 __func__); 223 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1); 224 } 225 break; 226 case PPC970_INPUT_CKSTP: 227 /* Level sensitive - active low */ 228 /* XXX: TODO: relay the signal to CKSTP_OUT pin */ 229 if (level) { 230 LOG_IRQ("%s: stop the CPU\n", __func__); 231 cs->halted = 1; 232 } else { 233 LOG_IRQ("%s: restart the CPU\n", __func__); 234 cs->halted = 0; 235 qemu_cpu_kick(cs); 236 } 237 break; 238 case PPC970_INPUT_HRESET: 239 /* Level sensitive - active low */ 240 if (level) { 241 cpu_interrupt(cs, CPU_INTERRUPT_RESET); 242 } 243 break; 244 case PPC970_INPUT_SRESET: 245 LOG_IRQ("%s: set the RESET IRQ state to %d\n", 246 __func__, level); 247 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level); 248 break; 249 case PPC970_INPUT_TBEN: 250 LOG_IRQ("%s: set the TBEN state to %d\n", __func__, 251 level); 252 /* XXX: TODO */ 253 break; 254 default: 255 /* Unknown pin - do nothing */ 256 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); 257 return; 258 } 259 if (level) 260 env->irq_input_state |= 1 << pin; 261 else 262 env->irq_input_state &= ~(1 << pin); 263 } 264 } 265 266 void ppc970_irq_init(PowerPCCPU *cpu) 267 { 268 CPUPPCState *env = &cpu->env; 269 270 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, cpu, 271 PPC970_INPUT_NB); 272 } 273 274 /* POWER7 internal IRQ controller */ 275 static void power7_set_irq(void *opaque, int pin, int level) 276 { 277 PowerPCCPU *cpu = opaque; 278 279 LOG_IRQ("%s: env %p pin %d level %d\n", __func__, 280 &cpu->env, pin, level); 281 282 switch (pin) { 283 case POWER7_INPUT_INT: 284 /* Level sensitive - active high */ 285 LOG_IRQ("%s: set the external IRQ state to %d\n", 286 __func__, level); 287 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level); 288 break; 289 default: 290 /* Unknown pin - do nothing */ 291 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); 292 return; 293 } 294 } 295 296 void ppcPOWER7_irq_init(PowerPCCPU *cpu) 297 { 298 CPUPPCState *env = &cpu->env; 299 300 env->irq_inputs = (void **)qemu_allocate_irqs(&power7_set_irq, cpu, 301 POWER7_INPUT_NB); 302 } 303 304 /* POWER9 internal IRQ controller */ 305 static void power9_set_irq(void *opaque, int pin, int level) 306 { 307 PowerPCCPU *cpu = opaque; 308 309 LOG_IRQ("%s: env %p pin %d level %d\n", __func__, 310 &cpu->env, pin, level); 311 312 switch (pin) { 313 case POWER9_INPUT_INT: 314 /* Level sensitive - active high */ 315 LOG_IRQ("%s: set the external IRQ state to %d\n", 316 __func__, level); 317 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level); 318 break; 319 case POWER9_INPUT_HINT: 320 /* Level sensitive - active high */ 321 LOG_IRQ("%s: set the external IRQ state to %d\n", 322 __func__, level); 323 ppc_set_irq(cpu, PPC_INTERRUPT_HVIRT, level); 324 break; 325 default: 326 /* Unknown pin - do nothing */ 327 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); 328 return; 329 } 330 } 331 332 void ppcPOWER9_irq_init(PowerPCCPU *cpu) 333 { 334 CPUPPCState *env = &cpu->env; 335 336 env->irq_inputs = (void **)qemu_allocate_irqs(&power9_set_irq, cpu, 337 POWER9_INPUT_NB); 338 } 339 #endif /* defined(TARGET_PPC64) */ 340 341 void ppc40x_core_reset(PowerPCCPU *cpu) 342 { 343 CPUPPCState *env = &cpu->env; 344 target_ulong dbsr; 345 346 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC core\n"); 347 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_RESET); 348 dbsr = env->spr[SPR_40x_DBSR]; 349 dbsr &= ~0x00000300; 350 dbsr |= 0x00000100; 351 env->spr[SPR_40x_DBSR] = dbsr; 352 } 353 354 void ppc40x_chip_reset(PowerPCCPU *cpu) 355 { 356 CPUPPCState *env = &cpu->env; 357 target_ulong dbsr; 358 359 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC chip\n"); 360 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_RESET); 361 /* XXX: TODO reset all internal peripherals */ 362 dbsr = env->spr[SPR_40x_DBSR]; 363 dbsr &= ~0x00000300; 364 dbsr |= 0x00000200; 365 env->spr[SPR_40x_DBSR] = dbsr; 366 } 367 368 void ppc40x_system_reset(PowerPCCPU *cpu) 369 { 370 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC system\n"); 371 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 372 } 373 374 void store_40x_dbcr0(CPUPPCState *env, uint32_t val) 375 { 376 PowerPCCPU *cpu = env_archcpu(env); 377 378 switch ((val >> 28) & 0x3) { 379 case 0x0: 380 /* No action */ 381 break; 382 case 0x1: 383 /* Core reset */ 384 ppc40x_core_reset(cpu); 385 break; 386 case 0x2: 387 /* Chip reset */ 388 ppc40x_chip_reset(cpu); 389 break; 390 case 0x3: 391 /* System reset */ 392 ppc40x_system_reset(cpu); 393 break; 394 } 395 } 396 397 /* PowerPC 40x internal IRQ controller */ 398 static void ppc40x_set_irq(void *opaque, int pin, int level) 399 { 400 PowerPCCPU *cpu = opaque; 401 CPUPPCState *env = &cpu->env; 402 int cur_level; 403 404 LOG_IRQ("%s: env %p pin %d level %d\n", __func__, 405 env, pin, level); 406 cur_level = (env->irq_input_state >> pin) & 1; 407 /* Don't generate spurious events */ 408 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { 409 CPUState *cs = CPU(cpu); 410 411 switch (pin) { 412 case PPC40x_INPUT_RESET_SYS: 413 if (level) { 414 LOG_IRQ("%s: reset the PowerPC system\n", 415 __func__); 416 ppc40x_system_reset(cpu); 417 } 418 break; 419 case PPC40x_INPUT_RESET_CHIP: 420 if (level) { 421 LOG_IRQ("%s: reset the PowerPC chip\n", __func__); 422 ppc40x_chip_reset(cpu); 423 } 424 break; 425 case PPC40x_INPUT_RESET_CORE: 426 /* XXX: TODO: update DBSR[MRR] */ 427 if (level) { 428 LOG_IRQ("%s: reset the PowerPC core\n", __func__); 429 ppc40x_core_reset(cpu); 430 } 431 break; 432 case PPC40x_INPUT_CINT: 433 /* Level sensitive - active high */ 434 LOG_IRQ("%s: set the critical IRQ state to %d\n", 435 __func__, level); 436 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level); 437 break; 438 case PPC40x_INPUT_INT: 439 /* Level sensitive - active high */ 440 LOG_IRQ("%s: set the external IRQ state to %d\n", 441 __func__, level); 442 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level); 443 break; 444 case PPC40x_INPUT_HALT: 445 /* Level sensitive - active low */ 446 if (level) { 447 LOG_IRQ("%s: stop the CPU\n", __func__); 448 cs->halted = 1; 449 } else { 450 LOG_IRQ("%s: restart the CPU\n", __func__); 451 cs->halted = 0; 452 qemu_cpu_kick(cs); 453 } 454 break; 455 case PPC40x_INPUT_DEBUG: 456 /* Level sensitive - active high */ 457 LOG_IRQ("%s: set the debug pin state to %d\n", 458 __func__, level); 459 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level); 460 break; 461 default: 462 /* Unknown pin - do nothing */ 463 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); 464 return; 465 } 466 if (level) 467 env->irq_input_state |= 1 << pin; 468 else 469 env->irq_input_state &= ~(1 << pin); 470 } 471 } 472 473 void ppc40x_irq_init(PowerPCCPU *cpu) 474 { 475 CPUPPCState *env = &cpu->env; 476 477 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq, 478 cpu, PPC40x_INPUT_NB); 479 } 480 481 /* PowerPC E500 internal IRQ controller */ 482 static void ppce500_set_irq(void *opaque, int pin, int level) 483 { 484 PowerPCCPU *cpu = opaque; 485 CPUPPCState *env = &cpu->env; 486 int cur_level; 487 488 LOG_IRQ("%s: env %p pin %d level %d\n", __func__, 489 env, pin, level); 490 cur_level = (env->irq_input_state >> pin) & 1; 491 /* Don't generate spurious events */ 492 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { 493 switch (pin) { 494 case PPCE500_INPUT_MCK: 495 if (level) { 496 LOG_IRQ("%s: reset the PowerPC system\n", 497 __func__); 498 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 499 } 500 break; 501 case PPCE500_INPUT_RESET_CORE: 502 if (level) { 503 LOG_IRQ("%s: reset the PowerPC core\n", __func__); 504 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, level); 505 } 506 break; 507 case PPCE500_INPUT_CINT: 508 /* Level sensitive - active high */ 509 LOG_IRQ("%s: set the critical IRQ state to %d\n", 510 __func__, level); 511 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level); 512 break; 513 case PPCE500_INPUT_INT: 514 /* Level sensitive - active high */ 515 LOG_IRQ("%s: set the core IRQ state to %d\n", 516 __func__, level); 517 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level); 518 break; 519 case PPCE500_INPUT_DEBUG: 520 /* Level sensitive - active high */ 521 LOG_IRQ("%s: set the debug pin state to %d\n", 522 __func__, level); 523 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level); 524 break; 525 default: 526 /* Unknown pin - do nothing */ 527 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); 528 return; 529 } 530 if (level) 531 env->irq_input_state |= 1 << pin; 532 else 533 env->irq_input_state &= ~(1 << pin); 534 } 535 } 536 537 void ppce500_irq_init(PowerPCCPU *cpu) 538 { 539 CPUPPCState *env = &cpu->env; 540 541 env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq, 542 cpu, PPCE500_INPUT_NB); 543 } 544 545 /* Enable or Disable the E500 EPR capability */ 546 void ppce500_set_mpic_proxy(bool enabled) 547 { 548 CPUState *cs; 549 550 CPU_FOREACH(cs) { 551 PowerPCCPU *cpu = POWERPC_CPU(cs); 552 553 cpu->env.mpic_proxy = enabled; 554 if (kvm_enabled()) { 555 kvmppc_set_mpic_proxy(cpu, enabled); 556 } 557 } 558 } 559 560 /*****************************************************************************/ 561 /* PowerPC time base and decrementer emulation */ 562 563 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset) 564 { 565 /* TB time in tb periods */ 566 return muldiv64(vmclk, tb_env->tb_freq, NANOSECONDS_PER_SECOND) + tb_offset; 567 } 568 569 uint64_t cpu_ppc_load_tbl (CPUPPCState *env) 570 { 571 ppc_tb_t *tb_env = env->tb_env; 572 uint64_t tb; 573 574 if (kvm_enabled()) { 575 return env->spr[SPR_TBL]; 576 } 577 578 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset); 579 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); 580 581 return tb; 582 } 583 584 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState *env) 585 { 586 ppc_tb_t *tb_env = env->tb_env; 587 uint64_t tb; 588 589 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset); 590 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); 591 592 return tb >> 32; 593 } 594 595 uint32_t cpu_ppc_load_tbu (CPUPPCState *env) 596 { 597 if (kvm_enabled()) { 598 return env->spr[SPR_TBU]; 599 } 600 601 return _cpu_ppc_load_tbu(env); 602 } 603 604 static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk, 605 int64_t *tb_offsetp, uint64_t value) 606 { 607 *tb_offsetp = value - 608 muldiv64(vmclk, tb_env->tb_freq, NANOSECONDS_PER_SECOND); 609 610 LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n", 611 __func__, value, *tb_offsetp); 612 } 613 614 void cpu_ppc_store_tbl (CPUPPCState *env, uint32_t value) 615 { 616 ppc_tb_t *tb_env = env->tb_env; 617 uint64_t tb; 618 619 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset); 620 tb &= 0xFFFFFFFF00000000ULL; 621 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 622 &tb_env->tb_offset, tb | (uint64_t)value); 623 } 624 625 static inline void _cpu_ppc_store_tbu(CPUPPCState *env, uint32_t value) 626 { 627 ppc_tb_t *tb_env = env->tb_env; 628 uint64_t tb; 629 630 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset); 631 tb &= 0x00000000FFFFFFFFULL; 632 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 633 &tb_env->tb_offset, ((uint64_t)value << 32) | tb); 634 } 635 636 void cpu_ppc_store_tbu (CPUPPCState *env, uint32_t value) 637 { 638 _cpu_ppc_store_tbu(env, value); 639 } 640 641 uint64_t cpu_ppc_load_atbl (CPUPPCState *env) 642 { 643 ppc_tb_t *tb_env = env->tb_env; 644 uint64_t tb; 645 646 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset); 647 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); 648 649 return tb; 650 } 651 652 uint32_t cpu_ppc_load_atbu (CPUPPCState *env) 653 { 654 ppc_tb_t *tb_env = env->tb_env; 655 uint64_t tb; 656 657 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset); 658 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); 659 660 return tb >> 32; 661 } 662 663 void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value) 664 { 665 ppc_tb_t *tb_env = env->tb_env; 666 uint64_t tb; 667 668 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset); 669 tb &= 0xFFFFFFFF00000000ULL; 670 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 671 &tb_env->atb_offset, tb | (uint64_t)value); 672 } 673 674 void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value) 675 { 676 ppc_tb_t *tb_env = env->tb_env; 677 uint64_t tb; 678 679 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset); 680 tb &= 0x00000000FFFFFFFFULL; 681 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 682 &tb_env->atb_offset, ((uint64_t)value << 32) | tb); 683 } 684 685 static void cpu_ppc_tb_stop (CPUPPCState *env) 686 { 687 ppc_tb_t *tb_env = env->tb_env; 688 uint64_t tb, atb, vmclk; 689 690 /* If the time base is already frozen, do nothing */ 691 if (tb_env->tb_freq != 0) { 692 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 693 /* Get the time base */ 694 tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset); 695 /* Get the alternate time base */ 696 atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset); 697 /* Store the time base value (ie compute the current offset) */ 698 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb); 699 /* Store the alternate time base value (compute the current offset) */ 700 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb); 701 /* Set the time base frequency to zero */ 702 tb_env->tb_freq = 0; 703 /* Now, the time bases are frozen to tb_offset / atb_offset value */ 704 } 705 } 706 707 static void cpu_ppc_tb_start (CPUPPCState *env) 708 { 709 ppc_tb_t *tb_env = env->tb_env; 710 uint64_t tb, atb, vmclk; 711 712 /* If the time base is not frozen, do nothing */ 713 if (tb_env->tb_freq == 0) { 714 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 715 /* Get the time base from tb_offset */ 716 tb = tb_env->tb_offset; 717 /* Get the alternate time base from atb_offset */ 718 atb = tb_env->atb_offset; 719 /* Restore the tb frequency from the decrementer frequency */ 720 tb_env->tb_freq = tb_env->decr_freq; 721 /* Store the time base value */ 722 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb); 723 /* Store the alternate time base value */ 724 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb); 725 } 726 } 727 728 bool ppc_decr_clear_on_delivery(CPUPPCState *env) 729 { 730 ppc_tb_t *tb_env = env->tb_env; 731 int flags = PPC_DECR_UNDERFLOW_TRIGGERED | PPC_DECR_UNDERFLOW_LEVEL; 732 return ((tb_env->flags & flags) == PPC_DECR_UNDERFLOW_TRIGGERED); 733 } 734 735 static inline int64_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next) 736 { 737 ppc_tb_t *tb_env = env->tb_env; 738 int64_t decr, diff; 739 740 diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 741 if (diff >= 0) { 742 decr = muldiv64(diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND); 743 } else if (tb_env->flags & PPC_TIMER_BOOKE) { 744 decr = 0; 745 } else { 746 decr = -muldiv64(-diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND); 747 } 748 LOG_TB("%s: %016" PRIx64 "\n", __func__, decr); 749 750 return decr; 751 } 752 753 target_ulong cpu_ppc_load_decr(CPUPPCState *env) 754 { 755 ppc_tb_t *tb_env = env->tb_env; 756 uint64_t decr; 757 758 if (kvm_enabled()) { 759 return env->spr[SPR_DECR]; 760 } 761 762 decr = _cpu_ppc_load_decr(env, tb_env->decr_next); 763 764 /* 765 * If large decrementer is enabled then the decrementer is signed extened 766 * to 64 bits, otherwise it is a 32 bit value. 767 */ 768 if (env->spr[SPR_LPCR] & LPCR_LD) { 769 return decr; 770 } 771 return (uint32_t) decr; 772 } 773 774 target_ulong cpu_ppc_load_hdecr(CPUPPCState *env) 775 { 776 PowerPCCPU *cpu = env_archcpu(env); 777 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 778 ppc_tb_t *tb_env = env->tb_env; 779 uint64_t hdecr; 780 781 hdecr = _cpu_ppc_load_decr(env, tb_env->hdecr_next); 782 783 /* 784 * If we have a large decrementer (POWER9 or later) then hdecr is sign 785 * extended to 64 bits, otherwise it is 32 bits. 786 */ 787 if (pcc->lrg_decr_bits > 32) { 788 return hdecr; 789 } 790 return (uint32_t) hdecr; 791 } 792 793 uint64_t cpu_ppc_load_purr (CPUPPCState *env) 794 { 795 ppc_tb_t *tb_env = env->tb_env; 796 uint64_t diff; 797 798 diff = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - tb_env->purr_start; 799 800 return tb_env->purr_load + 801 muldiv64(diff, tb_env->tb_freq, NANOSECONDS_PER_SECOND); 802 } 803 804 /* When decrementer expires, 805 * all we need to do is generate or queue a CPU exception 806 */ 807 static inline void cpu_ppc_decr_excp(PowerPCCPU *cpu) 808 { 809 /* Raise it */ 810 LOG_TB("raise decrementer exception\n"); 811 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 1); 812 } 813 814 static inline void cpu_ppc_decr_lower(PowerPCCPU *cpu) 815 { 816 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 0); 817 } 818 819 static inline void cpu_ppc_hdecr_excp(PowerPCCPU *cpu) 820 { 821 CPUPPCState *env = &cpu->env; 822 823 /* Raise it */ 824 LOG_TB("raise hv decrementer exception\n"); 825 826 /* The architecture specifies that we don't deliver HDEC 827 * interrupts in a PM state. Not only they don't cause a 828 * wakeup but they also get effectively discarded. 829 */ 830 if (!env->resume_as_sreset) { 831 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 1); 832 } 833 } 834 835 static inline void cpu_ppc_hdecr_lower(PowerPCCPU *cpu) 836 { 837 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0); 838 } 839 840 static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp, 841 QEMUTimer *timer, 842 void (*raise_excp)(void *), 843 void (*lower_excp)(PowerPCCPU *), 844 target_ulong decr, target_ulong value, 845 int nr_bits) 846 { 847 CPUPPCState *env = &cpu->env; 848 ppc_tb_t *tb_env = env->tb_env; 849 uint64_t now, next; 850 bool negative; 851 852 /* Truncate value to decr_width and sign extend for simplicity */ 853 value &= ((1ULL << nr_bits) - 1); 854 negative = !!(value & (1ULL << (nr_bits - 1))); 855 if (negative) { 856 value |= (0xFFFFFFFFULL << nr_bits); 857 } 858 859 LOG_TB("%s: " TARGET_FMT_lx " => " TARGET_FMT_lx "\n", __func__, 860 decr, value); 861 862 if (kvm_enabled()) { 863 /* KVM handles decrementer exceptions, we don't need our own timer */ 864 return; 865 } 866 867 /* 868 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC 869 * interrupt. 870 * 871 * If we get a really small DEC value, we can assume that by the time we 872 * handled it we should inject an interrupt already. 873 * 874 * On MSB level based DEC implementations the MSB always means the interrupt 875 * is pending, so raise it on those. 876 * 877 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers 878 * an edge interrupt, so raise it here too. 879 */ 880 if ((value < 3) || 881 ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && negative) || 882 ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && negative 883 && !(decr & (1ULL << (nr_bits - 1))))) { 884 (*raise_excp)(cpu); 885 return; 886 } 887 888 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */ 889 if (!negative && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) { 890 (*lower_excp)(cpu); 891 } 892 893 /* Calculate the next timer event */ 894 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 895 next = now + muldiv64(value, NANOSECONDS_PER_SECOND, tb_env->decr_freq); 896 *nextp = next; 897 898 /* Adjust timer */ 899 timer_mod(timer, next); 900 } 901 902 static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, target_ulong decr, 903 target_ulong value, int nr_bits) 904 { 905 ppc_tb_t *tb_env = cpu->env.tb_env; 906 907 __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env->decr_timer, 908 tb_env->decr_timer->cb, &cpu_ppc_decr_lower, decr, 909 value, nr_bits); 910 } 911 912 void cpu_ppc_store_decr(CPUPPCState *env, target_ulong value) 913 { 914 PowerPCCPU *cpu = env_archcpu(env); 915 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 916 int nr_bits = 32; 917 918 if (env->spr[SPR_LPCR] & LPCR_LD) { 919 nr_bits = pcc->lrg_decr_bits; 920 } 921 922 _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value, nr_bits); 923 } 924 925 static void cpu_ppc_decr_cb(void *opaque) 926 { 927 PowerPCCPU *cpu = opaque; 928 929 cpu_ppc_decr_excp(cpu); 930 } 931 932 static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, target_ulong hdecr, 933 target_ulong value, int nr_bits) 934 { 935 ppc_tb_t *tb_env = cpu->env.tb_env; 936 937 if (tb_env->hdecr_timer != NULL) { 938 __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env->hdecr_timer, 939 tb_env->hdecr_timer->cb, &cpu_ppc_hdecr_lower, 940 hdecr, value, nr_bits); 941 } 942 } 943 944 void cpu_ppc_store_hdecr(CPUPPCState *env, target_ulong value) 945 { 946 PowerPCCPU *cpu = env_archcpu(env); 947 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 948 949 _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value, 950 pcc->lrg_decr_bits); 951 } 952 953 static void cpu_ppc_hdecr_cb(void *opaque) 954 { 955 PowerPCCPU *cpu = opaque; 956 957 cpu_ppc_hdecr_excp(cpu); 958 } 959 960 static void cpu_ppc_store_purr(PowerPCCPU *cpu, uint64_t value) 961 { 962 ppc_tb_t *tb_env = cpu->env.tb_env; 963 964 tb_env->purr_load = value; 965 tb_env->purr_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 966 } 967 968 static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq) 969 { 970 CPUPPCState *env = opaque; 971 PowerPCCPU *cpu = env_archcpu(env); 972 ppc_tb_t *tb_env = env->tb_env; 973 974 tb_env->tb_freq = freq; 975 tb_env->decr_freq = freq; 976 /* There is a bug in Linux 2.4 kernels: 977 * if a decrementer exception is pending when it enables msr_ee at startup, 978 * it's not ready to handle it... 979 */ 980 _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32); 981 _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 32); 982 cpu_ppc_store_purr(cpu, 0x0000000000000000ULL); 983 } 984 985 static void timebase_save(PPCTimebase *tb) 986 { 987 uint64_t ticks = cpu_get_host_ticks(); 988 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu); 989 990 if (!first_ppc_cpu->env.tb_env) { 991 error_report("No timebase object"); 992 return; 993 } 994 995 /* not used anymore, we keep it for compatibility */ 996 tb->time_of_the_day_ns = qemu_clock_get_ns(QEMU_CLOCK_HOST); 997 /* 998 * tb_offset is only expected to be changed by QEMU so 999 * there is no need to update it from KVM here 1000 */ 1001 tb->guest_timebase = ticks + first_ppc_cpu->env.tb_env->tb_offset; 1002 1003 tb->runstate_paused = runstate_check(RUN_STATE_PAUSED); 1004 } 1005 1006 static void timebase_load(PPCTimebase *tb) 1007 { 1008 CPUState *cpu; 1009 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu); 1010 int64_t tb_off_adj, tb_off; 1011 unsigned long freq; 1012 1013 if (!first_ppc_cpu->env.tb_env) { 1014 error_report("No timebase object"); 1015 return; 1016 } 1017 1018 freq = first_ppc_cpu->env.tb_env->tb_freq; 1019 1020 tb_off_adj = tb->guest_timebase - cpu_get_host_ticks(); 1021 1022 tb_off = first_ppc_cpu->env.tb_env->tb_offset; 1023 trace_ppc_tb_adjust(tb_off, tb_off_adj, tb_off_adj - tb_off, 1024 (tb_off_adj - tb_off) / freq); 1025 1026 /* Set new offset to all CPUs */ 1027 CPU_FOREACH(cpu) { 1028 PowerPCCPU *pcpu = POWERPC_CPU(cpu); 1029 pcpu->env.tb_env->tb_offset = tb_off_adj; 1030 kvmppc_set_reg_tb_offset(pcpu, pcpu->env.tb_env->tb_offset); 1031 } 1032 } 1033 1034 void cpu_ppc_clock_vm_state_change(void *opaque, int running, 1035 RunState state) 1036 { 1037 PPCTimebase *tb = opaque; 1038 1039 if (running) { 1040 timebase_load(tb); 1041 } else { 1042 timebase_save(tb); 1043 } 1044 } 1045 1046 /* 1047 * When migrating a running guest, read the clock just 1048 * before migration, so that the guest clock counts 1049 * during the events between: 1050 * 1051 * * vm_stop() 1052 * * 1053 * * pre_save() 1054 * 1055 * This reduces clock difference on migration from 5s 1056 * to 0.1s (when max_downtime == 5s), because sending the 1057 * final pages of memory (which happens between vm_stop() 1058 * and pre_save()) takes max_downtime. 1059 */ 1060 static int timebase_pre_save(void *opaque) 1061 { 1062 PPCTimebase *tb = opaque; 1063 1064 /* guest_timebase won't be overridden in case of paused guest */ 1065 if (!tb->runstate_paused) { 1066 timebase_save(tb); 1067 } 1068 1069 return 0; 1070 } 1071 1072 const VMStateDescription vmstate_ppc_timebase = { 1073 .name = "timebase", 1074 .version_id = 1, 1075 .minimum_version_id = 1, 1076 .minimum_version_id_old = 1, 1077 .pre_save = timebase_pre_save, 1078 .fields = (VMStateField []) { 1079 VMSTATE_UINT64(guest_timebase, PPCTimebase), 1080 VMSTATE_INT64(time_of_the_day_ns, PPCTimebase), 1081 VMSTATE_END_OF_LIST() 1082 }, 1083 }; 1084 1085 /* Set up (once) timebase frequency (in Hz) */ 1086 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq) 1087 { 1088 PowerPCCPU *cpu = env_archcpu(env); 1089 ppc_tb_t *tb_env; 1090 1091 tb_env = g_malloc0(sizeof(ppc_tb_t)); 1092 env->tb_env = tb_env; 1093 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED; 1094 if (is_book3s_arch2x(env)) { 1095 /* All Book3S 64bit CPUs implement level based DEC logic */ 1096 tb_env->flags |= PPC_DECR_UNDERFLOW_LEVEL; 1097 } 1098 /* Create new timer */ 1099 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_decr_cb, cpu); 1100 if (env->has_hv_mode) { 1101 tb_env->hdecr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_hdecr_cb, 1102 cpu); 1103 } else { 1104 tb_env->hdecr_timer = NULL; 1105 } 1106 cpu_ppc_set_tb_clk(env, freq); 1107 1108 return &cpu_ppc_set_tb_clk; 1109 } 1110 1111 /* Specific helpers for POWER & PowerPC 601 RTC */ 1112 void cpu_ppc601_store_rtcu (CPUPPCState *env, uint32_t value) 1113 { 1114 _cpu_ppc_store_tbu(env, value); 1115 } 1116 1117 uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env) 1118 { 1119 return _cpu_ppc_load_tbu(env); 1120 } 1121 1122 void cpu_ppc601_store_rtcl (CPUPPCState *env, uint32_t value) 1123 { 1124 cpu_ppc_store_tbl(env, value & 0x3FFFFF80); 1125 } 1126 1127 uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env) 1128 { 1129 return cpu_ppc_load_tbl(env) & 0x3FFFFF80; 1130 } 1131 1132 /*****************************************************************************/ 1133 /* PowerPC 40x timers */ 1134 1135 /* PIT, FIT & WDT */ 1136 typedef struct ppc40x_timer_t ppc40x_timer_t; 1137 struct ppc40x_timer_t { 1138 uint64_t pit_reload; /* PIT auto-reload value */ 1139 uint64_t fit_next; /* Tick for next FIT interrupt */ 1140 QEMUTimer *fit_timer; 1141 uint64_t wdt_next; /* Tick for next WDT interrupt */ 1142 QEMUTimer *wdt_timer; 1143 1144 /* 405 have the PIT, 440 have a DECR. */ 1145 unsigned int decr_excp; 1146 }; 1147 1148 /* Fixed interval timer */ 1149 static void cpu_4xx_fit_cb (void *opaque) 1150 { 1151 PowerPCCPU *cpu; 1152 CPUPPCState *env; 1153 ppc_tb_t *tb_env; 1154 ppc40x_timer_t *ppc40x_timer; 1155 uint64_t now, next; 1156 1157 env = opaque; 1158 cpu = env_archcpu(env); 1159 tb_env = env->tb_env; 1160 ppc40x_timer = tb_env->opaque; 1161 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1162 switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) { 1163 case 0: 1164 next = 1 << 9; 1165 break; 1166 case 1: 1167 next = 1 << 13; 1168 break; 1169 case 2: 1170 next = 1 << 17; 1171 break; 1172 case 3: 1173 next = 1 << 21; 1174 break; 1175 default: 1176 /* Cannot occur, but makes gcc happy */ 1177 return; 1178 } 1179 next = now + muldiv64(next, NANOSECONDS_PER_SECOND, tb_env->tb_freq); 1180 if (next == now) 1181 next++; 1182 timer_mod(ppc40x_timer->fit_timer, next); 1183 env->spr[SPR_40x_TSR] |= 1 << 26; 1184 if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) { 1185 ppc_set_irq(cpu, PPC_INTERRUPT_FIT, 1); 1186 } 1187 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__, 1188 (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1), 1189 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); 1190 } 1191 1192 /* Programmable interval timer */ 1193 static void start_stop_pit (CPUPPCState *env, ppc_tb_t *tb_env, int is_excp) 1194 { 1195 ppc40x_timer_t *ppc40x_timer; 1196 uint64_t now, next; 1197 1198 ppc40x_timer = tb_env->opaque; 1199 if (ppc40x_timer->pit_reload <= 1 || 1200 !((env->spr[SPR_40x_TCR] >> 26) & 0x1) || 1201 (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) { 1202 /* Stop PIT */ 1203 LOG_TB("%s: stop PIT\n", __func__); 1204 timer_del(tb_env->decr_timer); 1205 } else { 1206 LOG_TB("%s: start PIT %016" PRIx64 "\n", 1207 __func__, ppc40x_timer->pit_reload); 1208 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1209 next = now + muldiv64(ppc40x_timer->pit_reload, 1210 NANOSECONDS_PER_SECOND, tb_env->decr_freq); 1211 if (is_excp) 1212 next += tb_env->decr_next - now; 1213 if (next == now) 1214 next++; 1215 timer_mod(tb_env->decr_timer, next); 1216 tb_env->decr_next = next; 1217 } 1218 } 1219 1220 static void cpu_4xx_pit_cb (void *opaque) 1221 { 1222 PowerPCCPU *cpu; 1223 CPUPPCState *env; 1224 ppc_tb_t *tb_env; 1225 ppc40x_timer_t *ppc40x_timer; 1226 1227 env = opaque; 1228 cpu = env_archcpu(env); 1229 tb_env = env->tb_env; 1230 ppc40x_timer = tb_env->opaque; 1231 env->spr[SPR_40x_TSR] |= 1 << 27; 1232 if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) { 1233 ppc_set_irq(cpu, ppc40x_timer->decr_excp, 1); 1234 } 1235 start_stop_pit(env, tb_env, 1); 1236 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " " 1237 "%016" PRIx64 "\n", __func__, 1238 (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1), 1239 (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1), 1240 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR], 1241 ppc40x_timer->pit_reload); 1242 } 1243 1244 /* Watchdog timer */ 1245 static void cpu_4xx_wdt_cb (void *opaque) 1246 { 1247 PowerPCCPU *cpu; 1248 CPUPPCState *env; 1249 ppc_tb_t *tb_env; 1250 ppc40x_timer_t *ppc40x_timer; 1251 uint64_t now, next; 1252 1253 env = opaque; 1254 cpu = env_archcpu(env); 1255 tb_env = env->tb_env; 1256 ppc40x_timer = tb_env->opaque; 1257 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1258 switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) { 1259 case 0: 1260 next = 1 << 17; 1261 break; 1262 case 1: 1263 next = 1 << 21; 1264 break; 1265 case 2: 1266 next = 1 << 25; 1267 break; 1268 case 3: 1269 next = 1 << 29; 1270 break; 1271 default: 1272 /* Cannot occur, but makes gcc happy */ 1273 return; 1274 } 1275 next = now + muldiv64(next, NANOSECONDS_PER_SECOND, tb_env->decr_freq); 1276 if (next == now) 1277 next++; 1278 LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__, 1279 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); 1280 switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) { 1281 case 0x0: 1282 case 0x1: 1283 timer_mod(ppc40x_timer->wdt_timer, next); 1284 ppc40x_timer->wdt_next = next; 1285 env->spr[SPR_40x_TSR] |= 1U << 31; 1286 break; 1287 case 0x2: 1288 timer_mod(ppc40x_timer->wdt_timer, next); 1289 ppc40x_timer->wdt_next = next; 1290 env->spr[SPR_40x_TSR] |= 1 << 30; 1291 if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) { 1292 ppc_set_irq(cpu, PPC_INTERRUPT_WDT, 1); 1293 } 1294 break; 1295 case 0x3: 1296 env->spr[SPR_40x_TSR] &= ~0x30000000; 1297 env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000; 1298 switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) { 1299 case 0x0: 1300 /* No reset */ 1301 break; 1302 case 0x1: /* Core reset */ 1303 ppc40x_core_reset(cpu); 1304 break; 1305 case 0x2: /* Chip reset */ 1306 ppc40x_chip_reset(cpu); 1307 break; 1308 case 0x3: /* System reset */ 1309 ppc40x_system_reset(cpu); 1310 break; 1311 } 1312 } 1313 } 1314 1315 void store_40x_pit (CPUPPCState *env, target_ulong val) 1316 { 1317 ppc_tb_t *tb_env; 1318 ppc40x_timer_t *ppc40x_timer; 1319 1320 tb_env = env->tb_env; 1321 ppc40x_timer = tb_env->opaque; 1322 LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val); 1323 ppc40x_timer->pit_reload = val; 1324 start_stop_pit(env, tb_env, 0); 1325 } 1326 1327 target_ulong load_40x_pit (CPUPPCState *env) 1328 { 1329 return cpu_ppc_load_decr(env); 1330 } 1331 1332 static void ppc_40x_set_tb_clk (void *opaque, uint32_t freq) 1333 { 1334 CPUPPCState *env = opaque; 1335 ppc_tb_t *tb_env = env->tb_env; 1336 1337 LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__, 1338 freq); 1339 tb_env->tb_freq = freq; 1340 tb_env->decr_freq = freq; 1341 /* XXX: we should also update all timers */ 1342 } 1343 1344 clk_setup_cb ppc_40x_timers_init (CPUPPCState *env, uint32_t freq, 1345 unsigned int decr_excp) 1346 { 1347 ppc_tb_t *tb_env; 1348 ppc40x_timer_t *ppc40x_timer; 1349 1350 tb_env = g_malloc0(sizeof(ppc_tb_t)); 1351 env->tb_env = tb_env; 1352 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED; 1353 ppc40x_timer = g_malloc0(sizeof(ppc40x_timer_t)); 1354 tb_env->tb_freq = freq; 1355 tb_env->decr_freq = freq; 1356 tb_env->opaque = ppc40x_timer; 1357 LOG_TB("%s freq %" PRIu32 "\n", __func__, freq); 1358 if (ppc40x_timer != NULL) { 1359 /* We use decr timer for PIT */ 1360 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_pit_cb, env); 1361 ppc40x_timer->fit_timer = 1362 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_fit_cb, env); 1363 ppc40x_timer->wdt_timer = 1364 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_wdt_cb, env); 1365 ppc40x_timer->decr_excp = decr_excp; 1366 } 1367 1368 return &ppc_40x_set_tb_clk; 1369 } 1370 1371 /*****************************************************************************/ 1372 /* Embedded PowerPC Device Control Registers */ 1373 typedef struct ppc_dcrn_t ppc_dcrn_t; 1374 struct ppc_dcrn_t { 1375 dcr_read_cb dcr_read; 1376 dcr_write_cb dcr_write; 1377 void *opaque; 1378 }; 1379 1380 /* XXX: on 460, DCR addresses are 32 bits wide, 1381 * using DCRIPR to get the 22 upper bits of the DCR address 1382 */ 1383 #define DCRN_NB 1024 1384 struct ppc_dcr_t { 1385 ppc_dcrn_t dcrn[DCRN_NB]; 1386 int (*read_error)(int dcrn); 1387 int (*write_error)(int dcrn); 1388 }; 1389 1390 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp) 1391 { 1392 ppc_dcrn_t *dcr; 1393 1394 if (dcrn < 0 || dcrn >= DCRN_NB) 1395 goto error; 1396 dcr = &dcr_env->dcrn[dcrn]; 1397 if (dcr->dcr_read == NULL) 1398 goto error; 1399 *valp = (*dcr->dcr_read)(dcr->opaque, dcrn); 1400 1401 return 0; 1402 1403 error: 1404 if (dcr_env->read_error != NULL) 1405 return (*dcr_env->read_error)(dcrn); 1406 1407 return -1; 1408 } 1409 1410 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val) 1411 { 1412 ppc_dcrn_t *dcr; 1413 1414 if (dcrn < 0 || dcrn >= DCRN_NB) 1415 goto error; 1416 dcr = &dcr_env->dcrn[dcrn]; 1417 if (dcr->dcr_write == NULL) 1418 goto error; 1419 (*dcr->dcr_write)(dcr->opaque, dcrn, val); 1420 1421 return 0; 1422 1423 error: 1424 if (dcr_env->write_error != NULL) 1425 return (*dcr_env->write_error)(dcrn); 1426 1427 return -1; 1428 } 1429 1430 int ppc_dcr_register (CPUPPCState *env, int dcrn, void *opaque, 1431 dcr_read_cb dcr_read, dcr_write_cb dcr_write) 1432 { 1433 ppc_dcr_t *dcr_env; 1434 ppc_dcrn_t *dcr; 1435 1436 dcr_env = env->dcr_env; 1437 if (dcr_env == NULL) 1438 return -1; 1439 if (dcrn < 0 || dcrn >= DCRN_NB) 1440 return -1; 1441 dcr = &dcr_env->dcrn[dcrn]; 1442 if (dcr->opaque != NULL || 1443 dcr->dcr_read != NULL || 1444 dcr->dcr_write != NULL) 1445 return -1; 1446 dcr->opaque = opaque; 1447 dcr->dcr_read = dcr_read; 1448 dcr->dcr_write = dcr_write; 1449 1450 return 0; 1451 } 1452 1453 int ppc_dcr_init (CPUPPCState *env, int (*read_error)(int dcrn), 1454 int (*write_error)(int dcrn)) 1455 { 1456 ppc_dcr_t *dcr_env; 1457 1458 dcr_env = g_malloc0(sizeof(ppc_dcr_t)); 1459 dcr_env->read_error = read_error; 1460 dcr_env->write_error = write_error; 1461 env->dcr_env = dcr_env; 1462 1463 return 0; 1464 } 1465 1466 /*****************************************************************************/ 1467 /* Debug port */ 1468 void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val) 1469 { 1470 addr &= 0xF; 1471 switch (addr) { 1472 case 0: 1473 printf("%c", val); 1474 break; 1475 case 1: 1476 printf("\n"); 1477 fflush(stdout); 1478 break; 1479 case 2: 1480 printf("Set loglevel to %04" PRIx32 "\n", val); 1481 qemu_set_log(val | 0x100); 1482 break; 1483 } 1484 } 1485 1486 int ppc_cpu_pir(PowerPCCPU *cpu) 1487 { 1488 CPUPPCState *env = &cpu->env; 1489 return env->spr_cb[SPR_PIR].default_value; 1490 } 1491 1492 PowerPCCPU *ppc_get_vcpu_by_pir(int pir) 1493 { 1494 CPUState *cs; 1495 1496 CPU_FOREACH(cs) { 1497 PowerPCCPU *cpu = POWERPC_CPU(cs); 1498 1499 if (ppc_cpu_pir(cpu) == pir) { 1500 return cpu; 1501 } 1502 } 1503 1504 return NULL; 1505 } 1506 1507 void ppc_irq_reset(PowerPCCPU *cpu) 1508 { 1509 CPUPPCState *env = &cpu->env; 1510 1511 env->irq_input_state = 0; 1512 kvmppc_set_interrupt(cpu, PPC_INTERRUPT_EXT, 0); 1513 } 1514