2 * QEMU generic PowerPC hardware System Emulator
4 * Copyright (c) 2003-2007 Jocelyn Mayer
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
28 #include "hw/ppc/ppc.h"
29 #include "hw/ppc/ppc_e500.h"
30 #include "qemu/timer.h"
31 #include "sysemu/sysemu.h"
32 #include "sysemu/cpus.h"
34 #include "qemu/error-report.h"
35 #include "sysemu/kvm.h"
39 //#define PPC_DEBUG_IRQ
40 //#define PPC_DEBUG_TB
43 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
45 # define LOG_IRQ(...) do { } while (0)
50 # define LOG_TB(...) qemu_log(__VA_ARGS__)
52 # define LOG_TB(...) do { } while (0)
55 static void cpu_ppc_tb_stop (CPUPPCState *env);
56 static void cpu_ppc_tb_start (CPUPPCState *env);
58 void ppc_set_irq(PowerPCCPU *cpu, int n_IRQ, int level)
60 CPUState *cs = CPU(cpu);
61 CPUPPCState *env = &cpu->env;
62 unsigned int old_pending;
65 /* We may already have the BQL if coming from the reset path */
66 if (!qemu_mutex_iothread_locked()) {
68 qemu_mutex_lock_iothread();
71 old_pending = env->pending_interrupts;
74 env->pending_interrupts |= 1 << n_IRQ;
75 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
77 env->pending_interrupts &= ~(1 << n_IRQ);
78 if (env->pending_interrupts == 0) {
79 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
83 if (old_pending != env->pending_interrupts) {
85 kvmppc_set_interrupt(cpu, n_IRQ, level);
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);
95 qemu_mutex_unlock_iothread();
99 /* PowerPC 6xx / 7xx internal IRQ controller */
100 static void ppc6xx_set_irq(void *opaque, int pin, int level)
102 PowerPCCPU *cpu = opaque;
103 CPUPPCState *env = &cpu->env;
106 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
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);
114 case PPC6xx_INPUT_TBEN:
115 /* Level sensitive - active high */
116 LOG_IRQ("%s: %s the time base\n",
117 __func__, level ? "start" : "stop");
119 cpu_ppc_tb_start(env);
121 cpu_ppc_tb_stop(env);
123 case PPC6xx_INPUT_INT:
124 /* Level sensitive - active high */
125 LOG_IRQ("%s: set the external IRQ state to %d\n",
127 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
129 case PPC6xx_INPUT_SMI:
130 /* Level sensitive - active high */
131 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
133 ppc_set_irq(cpu, PPC_INTERRUPT_SMI, level);
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]
140 if (cur_level == 1 && level == 0) {
141 LOG_IRQ("%s: raise machine check state\n",
143 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1);
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 */
151 LOG_IRQ("%s: stop the CPU\n", __func__);
155 case PPC6xx_INPUT_HRESET:
156 /* Level sensitive - active low */
158 LOG_IRQ("%s: reset the CPU\n", __func__);
159 cpu_interrupt(cs, CPU_INTERRUPT_RESET);
162 case PPC6xx_INPUT_SRESET:
163 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
165 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level);
168 /* Unknown pin - do nothing */
169 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
173 env->irq_input_state |= 1 << pin;
175 env->irq_input_state &= ~(1 << pin);
179 void ppc6xx_irq_init(PowerPCCPU *cpu)
181 CPUPPCState *env = &cpu->env;
183 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, cpu,
187 #if defined(TARGET_PPC64)
188 /* PowerPC 970 internal IRQ controller */
189 static void ppc970_set_irq(void *opaque, int pin, int level)
191 PowerPCCPU *cpu = opaque;
192 CPUPPCState *env = &cpu->env;
195 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
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);
203 case PPC970_INPUT_INT:
204 /* Level sensitive - active high */
205 LOG_IRQ("%s: set the external IRQ state to %d\n",
207 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
209 case PPC970_INPUT_THINT:
210 /* Level sensitive - active high */
211 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__,
213 ppc_set_irq(cpu, PPC_INTERRUPT_THERM, level);
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]
220 if (cur_level == 1 && level == 0) {
221 LOG_IRQ("%s: raise machine check state\n",
223 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1);
226 case PPC970_INPUT_CKSTP:
227 /* Level sensitive - active low */
228 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
230 LOG_IRQ("%s: stop the CPU\n", __func__);
233 LOG_IRQ("%s: restart the CPU\n", __func__);
238 case PPC970_INPUT_HRESET:
239 /* Level sensitive - active low */
241 cpu_interrupt(cs, CPU_INTERRUPT_RESET);
244 case PPC970_INPUT_SRESET:
245 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
247 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level);
249 case PPC970_INPUT_TBEN:
250 LOG_IRQ("%s: set the TBEN state to %d\n", __func__,
255 /* Unknown pin - do nothing */
256 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
260 env->irq_input_state |= 1 << pin;
262 env->irq_input_state &= ~(1 << pin);
266 void ppc970_irq_init(PowerPCCPU *cpu)
268 CPUPPCState *env = &cpu->env;
270 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, cpu,
274 /* POWER7 internal IRQ controller */
275 static void power7_set_irq(void *opaque, int pin, int level)
277 PowerPCCPU *cpu = opaque;
278 CPUPPCState *env = &cpu->env;
280 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
284 case POWER7_INPUT_INT:
285 /* Level sensitive - active high */
286 LOG_IRQ("%s: set the external IRQ state to %d\n",
288 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
291 /* Unknown pin - do nothing */
292 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
296 env->irq_input_state |= 1 << pin;
298 env->irq_input_state &= ~(1 << pin);
302 void ppcPOWER7_irq_init(PowerPCCPU *cpu)
304 CPUPPCState *env = &cpu->env;
306 env->irq_inputs = (void **)qemu_allocate_irqs(&power7_set_irq, cpu,
310 /* POWER9 internal IRQ controller */
311 static void power9_set_irq(void *opaque, int pin, int level)
313 PowerPCCPU *cpu = opaque;
314 CPUPPCState *env = &cpu->env;
316 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
320 case POWER9_INPUT_INT:
321 /* Level sensitive - active high */
322 LOG_IRQ("%s: set the external IRQ state to %d\n",
324 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
326 case POWER9_INPUT_HINT:
327 /* Level sensitive - active high */
328 LOG_IRQ("%s: set the external IRQ state to %d\n",
330 ppc_set_irq(cpu, PPC_INTERRUPT_HVIRT, level);
333 /* Unknown pin - do nothing */
334 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
338 env->irq_input_state |= 1 << pin;
340 env->irq_input_state &= ~(1 << pin);
344 void ppcPOWER9_irq_init(PowerPCCPU *cpu)
346 CPUPPCState *env = &cpu->env;
348 env->irq_inputs = (void **)qemu_allocate_irqs(&power9_set_irq, cpu,
351 #endif /* defined(TARGET_PPC64) */
353 void ppc40x_core_reset(PowerPCCPU *cpu)
355 CPUPPCState *env = &cpu->env;
358 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC core\n");
359 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_RESET);
360 dbsr = env->spr[SPR_40x_DBSR];
363 env->spr[SPR_40x_DBSR] = dbsr;
366 void ppc40x_chip_reset(PowerPCCPU *cpu)
368 CPUPPCState *env = &cpu->env;
371 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC chip\n");
372 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_RESET);
373 /* XXX: TODO reset all internal peripherals */
374 dbsr = env->spr[SPR_40x_DBSR];
377 env->spr[SPR_40x_DBSR] = dbsr;
380 void ppc40x_system_reset(PowerPCCPU *cpu)
382 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC system\n");
383 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
386 void store_40x_dbcr0(CPUPPCState *env, uint32_t val)
388 PowerPCCPU *cpu = ppc_env_get_cpu(env);
390 switch ((val >> 28) & 0x3) {
396 ppc40x_core_reset(cpu);
400 ppc40x_chip_reset(cpu);
404 ppc40x_system_reset(cpu);
409 /* PowerPC 40x internal IRQ controller */
410 static void ppc40x_set_irq(void *opaque, int pin, int level)
412 PowerPCCPU *cpu = opaque;
413 CPUPPCState *env = &cpu->env;
416 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
418 cur_level = (env->irq_input_state >> pin) & 1;
419 /* Don't generate spurious events */
420 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
421 CPUState *cs = CPU(cpu);
424 case PPC40x_INPUT_RESET_SYS:
426 LOG_IRQ("%s: reset the PowerPC system\n",
428 ppc40x_system_reset(cpu);
431 case PPC40x_INPUT_RESET_CHIP:
433 LOG_IRQ("%s: reset the PowerPC chip\n", __func__);
434 ppc40x_chip_reset(cpu);
437 case PPC40x_INPUT_RESET_CORE:
438 /* XXX: TODO: update DBSR[MRR] */
440 LOG_IRQ("%s: reset the PowerPC core\n", __func__);
441 ppc40x_core_reset(cpu);
444 case PPC40x_INPUT_CINT:
445 /* Level sensitive - active high */
446 LOG_IRQ("%s: set the critical IRQ state to %d\n",
448 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level);
450 case PPC40x_INPUT_INT:
451 /* Level sensitive - active high */
452 LOG_IRQ("%s: set the external IRQ state to %d\n",
454 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
456 case PPC40x_INPUT_HALT:
457 /* Level sensitive - active low */
459 LOG_IRQ("%s: stop the CPU\n", __func__);
462 LOG_IRQ("%s: restart the CPU\n", __func__);
467 case PPC40x_INPUT_DEBUG:
468 /* Level sensitive - active high */
469 LOG_IRQ("%s: set the debug pin state to %d\n",
471 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level);
474 /* Unknown pin - do nothing */
475 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
479 env->irq_input_state |= 1 << pin;
481 env->irq_input_state &= ~(1 << pin);
485 void ppc40x_irq_init(PowerPCCPU *cpu)
487 CPUPPCState *env = &cpu->env;
489 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq,
490 cpu, PPC40x_INPUT_NB);
493 /* PowerPC E500 internal IRQ controller */
494 static void ppce500_set_irq(void *opaque, int pin, int level)
496 PowerPCCPU *cpu = opaque;
497 CPUPPCState *env = &cpu->env;
500 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
502 cur_level = (env->irq_input_state >> pin) & 1;
503 /* Don't generate spurious events */
504 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
506 case PPCE500_INPUT_MCK:
508 LOG_IRQ("%s: reset the PowerPC system\n",
510 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
513 case PPCE500_INPUT_RESET_CORE:
515 LOG_IRQ("%s: reset the PowerPC core\n", __func__);
516 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, level);
519 case PPCE500_INPUT_CINT:
520 /* Level sensitive - active high */
521 LOG_IRQ("%s: set the critical IRQ state to %d\n",
523 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level);
525 case PPCE500_INPUT_INT:
526 /* Level sensitive - active high */
527 LOG_IRQ("%s: set the core IRQ state to %d\n",
529 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
531 case PPCE500_INPUT_DEBUG:
532 /* Level sensitive - active high */
533 LOG_IRQ("%s: set the debug pin state to %d\n",
535 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level);
538 /* Unknown pin - do nothing */
539 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
543 env->irq_input_state |= 1 << pin;
545 env->irq_input_state &= ~(1 << pin);
549 void ppce500_irq_init(PowerPCCPU *cpu)
551 CPUPPCState *env = &cpu->env;
553 env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq,
554 cpu, PPCE500_INPUT_NB);
557 /* Enable or Disable the E500 EPR capability */
558 void ppce500_set_mpic_proxy(bool enabled)
563 PowerPCCPU *cpu = POWERPC_CPU(cs);
565 cpu->env.mpic_proxy = enabled;
567 kvmppc_set_mpic_proxy(cpu, enabled);
572 /*****************************************************************************/
573 /* PowerPC time base and decrementer emulation */
575 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset)
577 /* TB time in tb periods */
578 return muldiv64(vmclk, tb_env->tb_freq, NANOSECONDS_PER_SECOND) + tb_offset;
581 uint64_t cpu_ppc_load_tbl (CPUPPCState *env)
583 ppc_tb_t *tb_env = env->tb_env;
587 return env->spr[SPR_TBL];
590 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
591 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
596 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState *env)
598 ppc_tb_t *tb_env = env->tb_env;
601 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
602 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
607 uint32_t cpu_ppc_load_tbu (CPUPPCState *env)
610 return env->spr[SPR_TBU];
613 return _cpu_ppc_load_tbu(env);
616 static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk,
617 int64_t *tb_offsetp, uint64_t value)
619 *tb_offsetp = value -
620 muldiv64(vmclk, tb_env->tb_freq, NANOSECONDS_PER_SECOND);
622 LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n",
623 __func__, value, *tb_offsetp);
626 void cpu_ppc_store_tbl (CPUPPCState *env, uint32_t value)
628 ppc_tb_t *tb_env = env->tb_env;
631 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
632 tb &= 0xFFFFFFFF00000000ULL;
633 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
634 &tb_env->tb_offset, tb | (uint64_t)value);
637 static inline void _cpu_ppc_store_tbu(CPUPPCState *env, uint32_t value)
639 ppc_tb_t *tb_env = env->tb_env;
642 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
643 tb &= 0x00000000FFFFFFFFULL;
644 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
645 &tb_env->tb_offset, ((uint64_t)value << 32) | tb);
648 void cpu_ppc_store_tbu (CPUPPCState *env, uint32_t value)
650 _cpu_ppc_store_tbu(env, value);
653 uint64_t cpu_ppc_load_atbl (CPUPPCState *env)
655 ppc_tb_t *tb_env = env->tb_env;
658 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
659 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
664 uint32_t cpu_ppc_load_atbu (CPUPPCState *env)
666 ppc_tb_t *tb_env = env->tb_env;
669 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
670 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
675 void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value)
677 ppc_tb_t *tb_env = env->tb_env;
680 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
681 tb &= 0xFFFFFFFF00000000ULL;
682 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
683 &tb_env->atb_offset, tb | (uint64_t)value);
686 void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value)
688 ppc_tb_t *tb_env = env->tb_env;
691 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
692 tb &= 0x00000000FFFFFFFFULL;
693 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
694 &tb_env->atb_offset, ((uint64_t)value << 32) | tb);
697 static void cpu_ppc_tb_stop (CPUPPCState *env)
699 ppc_tb_t *tb_env = env->tb_env;
700 uint64_t tb, atb, vmclk;
702 /* If the time base is already frozen, do nothing */
703 if (tb_env->tb_freq != 0) {
704 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
705 /* Get the time base */
706 tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset);
707 /* Get the alternate time base */
708 atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset);
709 /* Store the time base value (ie compute the current offset) */
710 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
711 /* Store the alternate time base value (compute the current offset) */
712 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
713 /* Set the time base frequency to zero */
715 /* Now, the time bases are frozen to tb_offset / atb_offset value */
719 static void cpu_ppc_tb_start (CPUPPCState *env)
721 ppc_tb_t *tb_env = env->tb_env;
722 uint64_t tb, atb, vmclk;
724 /* If the time base is not frozen, do nothing */
725 if (tb_env->tb_freq == 0) {
726 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
727 /* Get the time base from tb_offset */
728 tb = tb_env->tb_offset;
729 /* Get the alternate time base from atb_offset */
730 atb = tb_env->atb_offset;
731 /* Restore the tb frequency from the decrementer frequency */
732 tb_env->tb_freq = tb_env->decr_freq;
733 /* Store the time base value */
734 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
735 /* Store the alternate time base value */
736 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
740 bool ppc_decr_clear_on_delivery(CPUPPCState *env)
742 ppc_tb_t *tb_env = env->tb_env;
743 int flags = PPC_DECR_UNDERFLOW_TRIGGERED | PPC_DECR_UNDERFLOW_LEVEL;
744 return ((tb_env->flags & flags) == PPC_DECR_UNDERFLOW_TRIGGERED);
747 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
749 ppc_tb_t *tb_env = env->tb_env;
753 diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
755 decr = muldiv64(diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND);
756 } else if (tb_env->flags & PPC_TIMER_BOOKE) {
759 decr = -muldiv64(-diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND);
761 LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
766 uint32_t cpu_ppc_load_decr (CPUPPCState *env)
768 ppc_tb_t *tb_env = env->tb_env;
771 return env->spr[SPR_DECR];
774 return _cpu_ppc_load_decr(env, tb_env->decr_next);
777 uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)
779 ppc_tb_t *tb_env = env->tb_env;
781 return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
784 uint64_t cpu_ppc_load_purr (CPUPPCState *env)
786 ppc_tb_t *tb_env = env->tb_env;
789 diff = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - tb_env->purr_start;
791 return tb_env->purr_load +
792 muldiv64(diff, tb_env->tb_freq, NANOSECONDS_PER_SECOND);
795 /* When decrementer expires,
796 * all we need to do is generate or queue a CPU exception
798 static inline void cpu_ppc_decr_excp(PowerPCCPU *cpu)
801 LOG_TB("raise decrementer exception\n");
802 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 1);
805 static inline void cpu_ppc_decr_lower(PowerPCCPU *cpu)
807 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 0);
810 static inline void cpu_ppc_hdecr_excp(PowerPCCPU *cpu)
812 CPUPPCState *env = &cpu->env;
815 LOG_TB("raise hv decrementer exception\n");
817 /* The architecture specifies that we don't deliver HDEC
818 * interrupts in a PM state. Not only they don't cause a
819 * wakeup but they also get effectively discarded.
821 if (!env->resume_as_sreset) {
822 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 1);
826 static inline void cpu_ppc_hdecr_lower(PowerPCCPU *cpu)
828 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
831 static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
833 void (*raise_excp)(void *),
834 void (*lower_excp)(PowerPCCPU *),
835 uint32_t decr, uint32_t value)
837 CPUPPCState *env = &cpu->env;
838 ppc_tb_t *tb_env = env->tb_env;
841 LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
845 /* KVM handles decrementer exceptions, we don't need our own timer */
850 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
853 * If we get a really small DEC value, we can assume that by the time we
854 * handled it we should inject an interrupt already.
856 * On MSB level based DEC implementations the MSB always means the interrupt
857 * is pending, so raise it on those.
859 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
860 * an edge interrupt, so raise it here too.
863 ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && (value & 0x80000000)) ||
864 ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && (value & 0x80000000)
865 && !(decr & 0x80000000))) {
870 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
871 if (!(value & 0x80000000) && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
875 /* Calculate the next timer event */
876 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
877 next = now + muldiv64(value, NANOSECONDS_PER_SECOND, tb_env->decr_freq);
881 timer_mod(timer, next);
884 static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t decr,
887 ppc_tb_t *tb_env = cpu->env.tb_env;
889 __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env->decr_timer,
890 tb_env->decr_timer->cb, &cpu_ppc_decr_lower, decr,
894 void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
896 PowerPCCPU *cpu = ppc_env_get_cpu(env);
898 _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value);
901 static void cpu_ppc_decr_cb(void *opaque)
903 PowerPCCPU *cpu = opaque;
905 cpu_ppc_decr_excp(cpu);
908 static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t hdecr,
911 ppc_tb_t *tb_env = cpu->env.tb_env;
913 if (tb_env->hdecr_timer != NULL) {
914 __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env->hdecr_timer,
915 tb_env->hdecr_timer->cb, &cpu_ppc_hdecr_lower,
920 void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
922 PowerPCCPU *cpu = ppc_env_get_cpu(env);
924 _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value);
927 static void cpu_ppc_hdecr_cb(void *opaque)
929 PowerPCCPU *cpu = opaque;
931 cpu_ppc_hdecr_excp(cpu);
934 static void cpu_ppc_store_purr(PowerPCCPU *cpu, uint64_t value)
936 ppc_tb_t *tb_env = cpu->env.tb_env;
938 tb_env->purr_load = value;
939 tb_env->purr_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
942 static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
944 CPUPPCState *env = opaque;
945 PowerPCCPU *cpu = ppc_env_get_cpu(env);
946 ppc_tb_t *tb_env = env->tb_env;
948 tb_env->tb_freq = freq;
949 tb_env->decr_freq = freq;
950 /* There is a bug in Linux 2.4 kernels:
951 * if a decrementer exception is pending when it enables msr_ee at startup,
952 * it's not ready to handle it...
954 _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
955 _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
956 cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
959 static void timebase_save(PPCTimebase *tb)
961 uint64_t ticks = cpu_get_host_ticks();
962 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
964 if (!first_ppc_cpu->env.tb_env) {
965 error_report("No timebase object");
969 /* not used anymore, we keep it for compatibility */
970 tb->time_of_the_day_ns = qemu_clock_get_ns(QEMU_CLOCK_HOST);
972 * tb_offset is only expected to be changed by QEMU so
973 * there is no need to update it from KVM here
975 tb->guest_timebase = ticks + first_ppc_cpu->env.tb_env->tb_offset;
978 static void timebase_load(PPCTimebase *tb)
981 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
982 int64_t tb_off_adj, tb_off;
985 if (!first_ppc_cpu->env.tb_env) {
986 error_report("No timebase object");
990 freq = first_ppc_cpu->env.tb_env->tb_freq;
992 tb_off_adj = tb->guest_timebase - cpu_get_host_ticks();
994 tb_off = first_ppc_cpu->env.tb_env->tb_offset;
995 trace_ppc_tb_adjust(tb_off, tb_off_adj, tb_off_adj - tb_off,
996 (tb_off_adj - tb_off) / freq);
998 /* Set new offset to all CPUs */
1000 PowerPCCPU *pcpu = POWERPC_CPU(cpu);
1001 pcpu->env.tb_env->tb_offset = tb_off_adj;
1002 #if defined(CONFIG_KVM)
1003 kvm_set_one_reg(cpu, KVM_REG_PPC_TB_OFFSET,
1004 &pcpu->env.tb_env->tb_offset);
1009 void cpu_ppc_clock_vm_state_change(void *opaque, int running,
1012 PPCTimebase *tb = opaque;
1022 * When migrating, read the clock just before migration,
1023 * so that the guest clock counts during the events
1030 * This reduces clock difference on migration from 5s
1031 * to 0.1s (when max_downtime == 5s), because sending the
1032 * final pages of memory (which happens between vm_stop()
1033 * and pre_save()) takes max_downtime.
1035 static int timebase_pre_save(void *opaque)
1037 PPCTimebase *tb = opaque;
1044 const VMStateDescription vmstate_ppc_timebase = {
1047 .minimum_version_id = 1,
1048 .minimum_version_id_old = 1,
1049 .pre_save = timebase_pre_save,
1050 .fields = (VMStateField []) {
1051 VMSTATE_UINT64(guest_timebase, PPCTimebase),
1052 VMSTATE_INT64(time_of_the_day_ns, PPCTimebase),
1053 VMSTATE_END_OF_LIST()
1057 /* Set up (once) timebase frequency (in Hz) */
1058 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
1060 PowerPCCPU *cpu = ppc_env_get_cpu(env);
1063 tb_env = g_malloc0(sizeof(ppc_tb_t));
1064 env->tb_env = tb_env;
1065 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
1066 if (env->insns_flags & PPC_SEGMENT_64B) {
1067 /* All Book3S 64bit CPUs implement level based DEC logic */
1068 tb_env->flags |= PPC_DECR_UNDERFLOW_LEVEL;
1070 /* Create new timer */
1071 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_decr_cb, cpu);
1072 if (env->has_hv_mode) {
1073 tb_env->hdecr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_hdecr_cb,
1076 tb_env->hdecr_timer = NULL;
1078 cpu_ppc_set_tb_clk(env, freq);
1080 return &cpu_ppc_set_tb_clk;
1083 /* Specific helpers for POWER & PowerPC 601 RTC */
1084 void cpu_ppc601_store_rtcu (CPUPPCState *env, uint32_t value)
1086 _cpu_ppc_store_tbu(env, value);
1089 uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env)
1091 return _cpu_ppc_load_tbu(env);
1094 void cpu_ppc601_store_rtcl (CPUPPCState *env, uint32_t value)
1096 cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
1099 uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env)
1101 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
1104 /*****************************************************************************/
1105 /* PowerPC 40x timers */
1107 /* PIT, FIT & WDT */
1108 typedef struct ppc40x_timer_t ppc40x_timer_t;
1109 struct ppc40x_timer_t {
1110 uint64_t pit_reload; /* PIT auto-reload value */
1111 uint64_t fit_next; /* Tick for next FIT interrupt */
1112 QEMUTimer *fit_timer;
1113 uint64_t wdt_next; /* Tick for next WDT interrupt */
1114 QEMUTimer *wdt_timer;
1116 /* 405 have the PIT, 440 have a DECR. */
1117 unsigned int decr_excp;
1120 /* Fixed interval timer */
1121 static void cpu_4xx_fit_cb (void *opaque)
1126 ppc40x_timer_t *ppc40x_timer;
1130 cpu = ppc_env_get_cpu(env);
1131 tb_env = env->tb_env;
1132 ppc40x_timer = tb_env->opaque;
1133 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1134 switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
1148 /* Cannot occur, but makes gcc happy */
1151 next = now + muldiv64(next, NANOSECONDS_PER_SECOND, tb_env->tb_freq);
1154 timer_mod(ppc40x_timer->fit_timer, next);
1155 env->spr[SPR_40x_TSR] |= 1 << 26;
1156 if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) {
1157 ppc_set_irq(cpu, PPC_INTERRUPT_FIT, 1);
1159 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
1160 (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
1161 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
1164 /* Programmable interval timer */
1165 static void start_stop_pit (CPUPPCState *env, ppc_tb_t *tb_env, int is_excp)
1167 ppc40x_timer_t *ppc40x_timer;
1170 ppc40x_timer = tb_env->opaque;
1171 if (ppc40x_timer->pit_reload <= 1 ||
1172 !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
1173 (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
1175 LOG_TB("%s: stop PIT\n", __func__);
1176 timer_del(tb_env->decr_timer);
1178 LOG_TB("%s: start PIT %016" PRIx64 "\n",
1179 __func__, ppc40x_timer->pit_reload);
1180 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1181 next = now + muldiv64(ppc40x_timer->pit_reload,
1182 NANOSECONDS_PER_SECOND, tb_env->decr_freq);
1184 next += tb_env->decr_next - now;
1187 timer_mod(tb_env->decr_timer, next);
1188 tb_env->decr_next = next;
1192 static void cpu_4xx_pit_cb (void *opaque)
1197 ppc40x_timer_t *ppc40x_timer;
1200 cpu = ppc_env_get_cpu(env);
1201 tb_env = env->tb_env;
1202 ppc40x_timer = tb_env->opaque;
1203 env->spr[SPR_40x_TSR] |= 1 << 27;
1204 if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) {
1205 ppc_set_irq(cpu, ppc40x_timer->decr_excp, 1);
1207 start_stop_pit(env, tb_env, 1);
1208 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " "
1209 "%016" PRIx64 "\n", __func__,
1210 (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
1211 (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
1212 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
1213 ppc40x_timer->pit_reload);
1216 /* Watchdog timer */
1217 static void cpu_4xx_wdt_cb (void *opaque)
1222 ppc40x_timer_t *ppc40x_timer;
1226 cpu = ppc_env_get_cpu(env);
1227 tb_env = env->tb_env;
1228 ppc40x_timer = tb_env->opaque;
1229 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1230 switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
1244 /* Cannot occur, but makes gcc happy */
1247 next = now + muldiv64(next, NANOSECONDS_PER_SECOND, tb_env->decr_freq);
1250 LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
1251 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
1252 switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
1255 timer_mod(ppc40x_timer->wdt_timer, next);
1256 ppc40x_timer->wdt_next = next;
1257 env->spr[SPR_40x_TSR] |= 1U << 31;
1260 timer_mod(ppc40x_timer->wdt_timer, next);
1261 ppc40x_timer->wdt_next = next;
1262 env->spr[SPR_40x_TSR] |= 1 << 30;
1263 if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) {
1264 ppc_set_irq(cpu, PPC_INTERRUPT_WDT, 1);
1268 env->spr[SPR_40x_TSR] &= ~0x30000000;
1269 env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
1270 switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
1274 case 0x1: /* Core reset */
1275 ppc40x_core_reset(cpu);
1277 case 0x2: /* Chip reset */
1278 ppc40x_chip_reset(cpu);
1280 case 0x3: /* System reset */
1281 ppc40x_system_reset(cpu);
1287 void store_40x_pit (CPUPPCState *env, target_ulong val)
1290 ppc40x_timer_t *ppc40x_timer;
1292 tb_env = env->tb_env;
1293 ppc40x_timer = tb_env->opaque;
1294 LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val);
1295 ppc40x_timer->pit_reload = val;
1296 start_stop_pit(env, tb_env, 0);
1299 target_ulong load_40x_pit (CPUPPCState *env)
1301 return cpu_ppc_load_decr(env);
1304 static void ppc_40x_set_tb_clk (void *opaque, uint32_t freq)
1306 CPUPPCState *env = opaque;
1307 ppc_tb_t *tb_env = env->tb_env;
1309 LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__,
1311 tb_env->tb_freq = freq;
1312 tb_env->decr_freq = freq;
1313 /* XXX: we should also update all timers */
1316 clk_setup_cb ppc_40x_timers_init (CPUPPCState *env, uint32_t freq,
1317 unsigned int decr_excp)
1320 ppc40x_timer_t *ppc40x_timer;
1322 tb_env = g_malloc0(sizeof(ppc_tb_t));
1323 env->tb_env = tb_env;
1324 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
1325 ppc40x_timer = g_malloc0(sizeof(ppc40x_timer_t));
1326 tb_env->tb_freq = freq;
1327 tb_env->decr_freq = freq;
1328 tb_env->opaque = ppc40x_timer;
1329 LOG_TB("%s freq %" PRIu32 "\n", __func__, freq);
1330 if (ppc40x_timer != NULL) {
1331 /* We use decr timer for PIT */
1332 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_pit_cb, env);
1333 ppc40x_timer->fit_timer =
1334 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_fit_cb, env);
1335 ppc40x_timer->wdt_timer =
1336 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_wdt_cb, env);
1337 ppc40x_timer->decr_excp = decr_excp;
1340 return &ppc_40x_set_tb_clk;
1343 /*****************************************************************************/
1344 /* Embedded PowerPC Device Control Registers */
1345 typedef struct ppc_dcrn_t ppc_dcrn_t;
1347 dcr_read_cb dcr_read;
1348 dcr_write_cb dcr_write;
1352 /* XXX: on 460, DCR addresses are 32 bits wide,
1353 * using DCRIPR to get the 22 upper bits of the DCR address
1355 #define DCRN_NB 1024
1357 ppc_dcrn_t dcrn[DCRN_NB];
1358 int (*read_error)(int dcrn);
1359 int (*write_error)(int dcrn);
1362 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
1366 if (dcrn < 0 || dcrn >= DCRN_NB)
1368 dcr = &dcr_env->dcrn[dcrn];
1369 if (dcr->dcr_read == NULL)
1371 *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
1376 if (dcr_env->read_error != NULL)
1377 return (*dcr_env->read_error)(dcrn);
1382 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
1386 if (dcrn < 0 || dcrn >= DCRN_NB)
1388 dcr = &dcr_env->dcrn[dcrn];
1389 if (dcr->dcr_write == NULL)
1391 (*dcr->dcr_write)(dcr->opaque, dcrn, val);
1396 if (dcr_env->write_error != NULL)
1397 return (*dcr_env->write_error)(dcrn);
1402 int ppc_dcr_register (CPUPPCState *env, int dcrn, void *opaque,
1403 dcr_read_cb dcr_read, dcr_write_cb dcr_write)
1408 dcr_env = env->dcr_env;
1409 if (dcr_env == NULL)
1411 if (dcrn < 0 || dcrn >= DCRN_NB)
1413 dcr = &dcr_env->dcrn[dcrn];
1414 if (dcr->opaque != NULL ||
1415 dcr->dcr_read != NULL ||
1416 dcr->dcr_write != NULL)
1418 dcr->opaque = opaque;
1419 dcr->dcr_read = dcr_read;
1420 dcr->dcr_write = dcr_write;
1425 int ppc_dcr_init (CPUPPCState *env, int (*read_error)(int dcrn),
1426 int (*write_error)(int dcrn))
1430 dcr_env = g_malloc0(sizeof(ppc_dcr_t));
1431 dcr_env->read_error = read_error;
1432 dcr_env->write_error = write_error;
1433 env->dcr_env = dcr_env;
1438 /*****************************************************************************/
1440 void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
1452 printf("Set loglevel to %04" PRIx32 "\n", val);
1453 qemu_set_log(val | 0x100);