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
25 #include "hw/ppc/ppc.h"
26 #include "hw/ppc/ppc_e500.h"
27 #include "qemu/timer.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/cpus.h"
30 #include "hw/timer/m48t59.h"
32 #include "qemu/error-report.h"
33 #include "hw/loader.h"
34 #include "sysemu/kvm.h"
38 //#define PPC_DEBUG_IRQ
39 //#define PPC_DEBUG_TB
42 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
44 # define LOG_IRQ(...) do { } while (0)
49 # define LOG_TB(...) qemu_log(__VA_ARGS__)
51 # define LOG_TB(...) do { } while (0)
54 static void cpu_ppc_tb_stop (CPUPPCState *env);
55 static void cpu_ppc_tb_start (CPUPPCState *env);
57 void ppc_set_irq(PowerPCCPU *cpu, int n_IRQ, int level)
59 CPUState *cs = CPU(cpu);
60 CPUPPCState *env = &cpu->env;
61 unsigned int old_pending = env->pending_interrupts;
64 env->pending_interrupts |= 1 << n_IRQ;
65 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
67 env->pending_interrupts &= ~(1 << n_IRQ);
68 if (env->pending_interrupts == 0) {
69 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
73 if (old_pending != env->pending_interrupts) {
75 kvmppc_set_interrupt(cpu, n_IRQ, level);
79 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
80 "req %08x\n", __func__, env, n_IRQ, level,
81 env->pending_interrupts, CPU(cpu)->interrupt_request);
84 /* PowerPC 6xx / 7xx internal IRQ controller */
85 static void ppc6xx_set_irq(void *opaque, int pin, int level)
87 PowerPCCPU *cpu = opaque;
88 CPUPPCState *env = &cpu->env;
91 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
93 cur_level = (env->irq_input_state >> pin) & 1;
94 /* Don't generate spurious events */
95 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
96 CPUState *cs = CPU(cpu);
99 case PPC6xx_INPUT_TBEN:
100 /* Level sensitive - active high */
101 LOG_IRQ("%s: %s the time base\n",
102 __func__, level ? "start" : "stop");
104 cpu_ppc_tb_start(env);
106 cpu_ppc_tb_stop(env);
108 case PPC6xx_INPUT_INT:
109 /* Level sensitive - active high */
110 LOG_IRQ("%s: set the external IRQ state to %d\n",
112 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
114 case PPC6xx_INPUT_SMI:
115 /* Level sensitive - active high */
116 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
118 ppc_set_irq(cpu, PPC_INTERRUPT_SMI, level);
120 case PPC6xx_INPUT_MCP:
121 /* Negative edge sensitive */
122 /* XXX: TODO: actual reaction may depends on HID0 status
123 * 603/604/740/750: check HID0[EMCP]
125 if (cur_level == 1 && level == 0) {
126 LOG_IRQ("%s: raise machine check state\n",
128 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1);
131 case PPC6xx_INPUT_CKSTP_IN:
132 /* Level sensitive - active low */
133 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
134 /* XXX: Note that the only way to restart the CPU is to reset it */
136 LOG_IRQ("%s: stop the CPU\n", __func__);
140 case PPC6xx_INPUT_HRESET:
141 /* Level sensitive - active low */
143 LOG_IRQ("%s: reset the CPU\n", __func__);
144 cpu_interrupt(cs, CPU_INTERRUPT_RESET);
147 case PPC6xx_INPUT_SRESET:
148 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
150 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level);
153 /* Unknown pin - do nothing */
154 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
158 env->irq_input_state |= 1 << pin;
160 env->irq_input_state &= ~(1 << pin);
164 void ppc6xx_irq_init(CPUPPCState *env)
166 PowerPCCPU *cpu = ppc_env_get_cpu(env);
168 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, cpu,
172 #if defined(TARGET_PPC64)
173 /* PowerPC 970 internal IRQ controller */
174 static void ppc970_set_irq(void *opaque, int pin, int level)
176 PowerPCCPU *cpu = opaque;
177 CPUPPCState *env = &cpu->env;
180 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
182 cur_level = (env->irq_input_state >> pin) & 1;
183 /* Don't generate spurious events */
184 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
185 CPUState *cs = CPU(cpu);
188 case PPC970_INPUT_INT:
189 /* Level sensitive - active high */
190 LOG_IRQ("%s: set the external IRQ state to %d\n",
192 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
194 case PPC970_INPUT_THINT:
195 /* Level sensitive - active high */
196 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__,
198 ppc_set_irq(cpu, PPC_INTERRUPT_THERM, level);
200 case PPC970_INPUT_MCP:
201 /* Negative edge sensitive */
202 /* XXX: TODO: actual reaction may depends on HID0 status
203 * 603/604/740/750: check HID0[EMCP]
205 if (cur_level == 1 && level == 0) {
206 LOG_IRQ("%s: raise machine check state\n",
208 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1);
211 case PPC970_INPUT_CKSTP:
212 /* Level sensitive - active low */
213 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
215 LOG_IRQ("%s: stop the CPU\n", __func__);
218 LOG_IRQ("%s: restart the CPU\n", __func__);
223 case PPC970_INPUT_HRESET:
224 /* Level sensitive - active low */
226 cpu_interrupt(cs, CPU_INTERRUPT_RESET);
229 case PPC970_INPUT_SRESET:
230 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
232 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level);
234 case PPC970_INPUT_TBEN:
235 LOG_IRQ("%s: set the TBEN state to %d\n", __func__,
240 /* Unknown pin - do nothing */
241 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
245 env->irq_input_state |= 1 << pin;
247 env->irq_input_state &= ~(1 << pin);
251 void ppc970_irq_init(CPUPPCState *env)
253 PowerPCCPU *cpu = ppc_env_get_cpu(env);
255 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, cpu,
259 /* POWER7 internal IRQ controller */
260 static void power7_set_irq(void *opaque, int pin, int level)
262 PowerPCCPU *cpu = opaque;
263 CPUPPCState *env = &cpu->env;
265 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
269 case POWER7_INPUT_INT:
270 /* Level sensitive - active high */
271 LOG_IRQ("%s: set the external IRQ state to %d\n",
273 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
276 /* Unknown pin - do nothing */
277 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
281 env->irq_input_state |= 1 << pin;
283 env->irq_input_state &= ~(1 << pin);
287 void ppcPOWER7_irq_init(CPUPPCState *env)
289 PowerPCCPU *cpu = ppc_env_get_cpu(env);
291 env->irq_inputs = (void **)qemu_allocate_irqs(&power7_set_irq, cpu,
294 #endif /* defined(TARGET_PPC64) */
296 /* PowerPC 40x internal IRQ controller */
297 static void ppc40x_set_irq(void *opaque, int pin, int level)
299 PowerPCCPU *cpu = opaque;
300 CPUPPCState *env = &cpu->env;
303 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
305 cur_level = (env->irq_input_state >> pin) & 1;
306 /* Don't generate spurious events */
307 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
308 CPUState *cs = CPU(cpu);
311 case PPC40x_INPUT_RESET_SYS:
313 LOG_IRQ("%s: reset the PowerPC system\n",
315 ppc40x_system_reset(cpu);
318 case PPC40x_INPUT_RESET_CHIP:
320 LOG_IRQ("%s: reset the PowerPC chip\n", __func__);
321 ppc40x_chip_reset(cpu);
324 case PPC40x_INPUT_RESET_CORE:
325 /* XXX: TODO: update DBSR[MRR] */
327 LOG_IRQ("%s: reset the PowerPC core\n", __func__);
328 ppc40x_core_reset(cpu);
331 case PPC40x_INPUT_CINT:
332 /* Level sensitive - active high */
333 LOG_IRQ("%s: set the critical IRQ state to %d\n",
335 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level);
337 case PPC40x_INPUT_INT:
338 /* Level sensitive - active high */
339 LOG_IRQ("%s: set the external IRQ state to %d\n",
341 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
343 case PPC40x_INPUT_HALT:
344 /* Level sensitive - active low */
346 LOG_IRQ("%s: stop the CPU\n", __func__);
349 LOG_IRQ("%s: restart the CPU\n", __func__);
354 case PPC40x_INPUT_DEBUG:
355 /* Level sensitive - active high */
356 LOG_IRQ("%s: set the debug pin state to %d\n",
358 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level);
361 /* Unknown pin - do nothing */
362 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
366 env->irq_input_state |= 1 << pin;
368 env->irq_input_state &= ~(1 << pin);
372 void ppc40x_irq_init(CPUPPCState *env)
374 PowerPCCPU *cpu = ppc_env_get_cpu(env);
376 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq,
377 cpu, PPC40x_INPUT_NB);
380 /* PowerPC E500 internal IRQ controller */
381 static void ppce500_set_irq(void *opaque, int pin, int level)
383 PowerPCCPU *cpu = opaque;
384 CPUPPCState *env = &cpu->env;
387 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
389 cur_level = (env->irq_input_state >> pin) & 1;
390 /* Don't generate spurious events */
391 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
393 case PPCE500_INPUT_MCK:
395 LOG_IRQ("%s: reset the PowerPC system\n",
397 qemu_system_reset_request();
400 case PPCE500_INPUT_RESET_CORE:
402 LOG_IRQ("%s: reset the PowerPC core\n", __func__);
403 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, level);
406 case PPCE500_INPUT_CINT:
407 /* Level sensitive - active high */
408 LOG_IRQ("%s: set the critical IRQ state to %d\n",
410 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level);
412 case PPCE500_INPUT_INT:
413 /* Level sensitive - active high */
414 LOG_IRQ("%s: set the core IRQ state to %d\n",
416 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
418 case PPCE500_INPUT_DEBUG:
419 /* Level sensitive - active high */
420 LOG_IRQ("%s: set the debug pin state to %d\n",
422 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level);
425 /* Unknown pin - do nothing */
426 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
430 env->irq_input_state |= 1 << pin;
432 env->irq_input_state &= ~(1 << pin);
436 void ppce500_irq_init(CPUPPCState *env)
438 PowerPCCPU *cpu = ppc_env_get_cpu(env);
440 env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq,
441 cpu, PPCE500_INPUT_NB);
444 /* Enable or Disable the E500 EPR capability */
445 void ppce500_set_mpic_proxy(bool enabled)
450 PowerPCCPU *cpu = POWERPC_CPU(cs);
452 cpu->env.mpic_proxy = enabled;
454 kvmppc_set_mpic_proxy(cpu, enabled);
459 /*****************************************************************************/
460 /* PowerPC time base and decrementer emulation */
462 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset)
464 /* TB time in tb periods */
465 return muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()) + tb_offset;
468 uint64_t cpu_ppc_load_tbl (CPUPPCState *env)
470 ppc_tb_t *tb_env = env->tb_env;
474 return env->spr[SPR_TBL];
477 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
478 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
483 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState *env)
485 ppc_tb_t *tb_env = env->tb_env;
488 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
489 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
494 uint32_t cpu_ppc_load_tbu (CPUPPCState *env)
497 return env->spr[SPR_TBU];
500 return _cpu_ppc_load_tbu(env);
503 static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk,
504 int64_t *tb_offsetp, uint64_t value)
506 *tb_offsetp = value - muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec());
507 LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n",
508 __func__, value, *tb_offsetp);
511 void cpu_ppc_store_tbl (CPUPPCState *env, uint32_t value)
513 ppc_tb_t *tb_env = env->tb_env;
516 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
517 tb &= 0xFFFFFFFF00000000ULL;
518 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
519 &tb_env->tb_offset, tb | (uint64_t)value);
522 static inline void _cpu_ppc_store_tbu(CPUPPCState *env, uint32_t value)
524 ppc_tb_t *tb_env = env->tb_env;
527 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
528 tb &= 0x00000000FFFFFFFFULL;
529 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
530 &tb_env->tb_offset, ((uint64_t)value << 32) | tb);
533 void cpu_ppc_store_tbu (CPUPPCState *env, uint32_t value)
535 _cpu_ppc_store_tbu(env, value);
538 uint64_t cpu_ppc_load_atbl (CPUPPCState *env)
540 ppc_tb_t *tb_env = env->tb_env;
543 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
544 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
549 uint32_t cpu_ppc_load_atbu (CPUPPCState *env)
551 ppc_tb_t *tb_env = env->tb_env;
554 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
555 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
560 void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value)
562 ppc_tb_t *tb_env = env->tb_env;
565 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
566 tb &= 0xFFFFFFFF00000000ULL;
567 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
568 &tb_env->atb_offset, tb | (uint64_t)value);
571 void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value)
573 ppc_tb_t *tb_env = env->tb_env;
576 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
577 tb &= 0x00000000FFFFFFFFULL;
578 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
579 &tb_env->atb_offset, ((uint64_t)value << 32) | tb);
582 static void cpu_ppc_tb_stop (CPUPPCState *env)
584 ppc_tb_t *tb_env = env->tb_env;
585 uint64_t tb, atb, vmclk;
587 /* If the time base is already frozen, do nothing */
588 if (tb_env->tb_freq != 0) {
589 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
590 /* Get the time base */
591 tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset);
592 /* Get the alternate time base */
593 atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset);
594 /* Store the time base value (ie compute the current offset) */
595 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
596 /* Store the alternate time base value (compute the current offset) */
597 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
598 /* Set the time base frequency to zero */
600 /* Now, the time bases are frozen to tb_offset / atb_offset value */
604 static void cpu_ppc_tb_start (CPUPPCState *env)
606 ppc_tb_t *tb_env = env->tb_env;
607 uint64_t tb, atb, vmclk;
609 /* If the time base is not frozen, do nothing */
610 if (tb_env->tb_freq == 0) {
611 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
612 /* Get the time base from tb_offset */
613 tb = tb_env->tb_offset;
614 /* Get the alternate time base from atb_offset */
615 atb = tb_env->atb_offset;
616 /* Restore the tb frequency from the decrementer frequency */
617 tb_env->tb_freq = tb_env->decr_freq;
618 /* Store the time base value */
619 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
620 /* Store the alternate time base value */
621 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
625 bool ppc_decr_clear_on_delivery(CPUPPCState *env)
627 ppc_tb_t *tb_env = env->tb_env;
628 int flags = PPC_DECR_UNDERFLOW_TRIGGERED | PPC_DECR_UNDERFLOW_LEVEL;
629 return ((tb_env->flags & flags) == PPC_DECR_UNDERFLOW_TRIGGERED);
632 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
634 ppc_tb_t *tb_env = env->tb_env;
638 diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
640 decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec());
641 } else if (tb_env->flags & PPC_TIMER_BOOKE) {
644 decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec());
646 LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
651 uint32_t cpu_ppc_load_decr (CPUPPCState *env)
653 ppc_tb_t *tb_env = env->tb_env;
656 return env->spr[SPR_DECR];
659 return _cpu_ppc_load_decr(env, tb_env->decr_next);
662 uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)
664 ppc_tb_t *tb_env = env->tb_env;
666 return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
669 uint64_t cpu_ppc_load_purr (CPUPPCState *env)
671 ppc_tb_t *tb_env = env->tb_env;
674 diff = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - tb_env->purr_start;
676 return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, get_ticks_per_sec());
679 /* When decrementer expires,
680 * all we need to do is generate or queue a CPU exception
682 static inline void cpu_ppc_decr_excp(PowerPCCPU *cpu)
685 LOG_TB("raise decrementer exception\n");
686 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 1);
689 static inline void cpu_ppc_decr_lower(PowerPCCPU *cpu)
691 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 0);
694 static inline void cpu_ppc_hdecr_excp(PowerPCCPU *cpu)
697 LOG_TB("raise decrementer exception\n");
698 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 1);
701 static inline void cpu_ppc_hdecr_lower(PowerPCCPU *cpu)
703 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
706 static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
708 void (*raise_excp)(void *),
709 void (*lower_excp)(PowerPCCPU *),
710 uint32_t decr, uint32_t value)
712 CPUPPCState *env = &cpu->env;
713 ppc_tb_t *tb_env = env->tb_env;
716 LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
720 /* KVM handles decrementer exceptions, we don't need our own timer */
725 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
728 * If we get a really small DEC value, we can assume that by the time we
729 * handled it we should inject an interrupt already.
731 * On MSB level based DEC implementations the MSB always means the interrupt
732 * is pending, so raise it on those.
734 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
735 * an edge interrupt, so raise it here too.
738 ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && (value & 0x80000000)) ||
739 ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && (value & 0x80000000)
740 && !(decr & 0x80000000))) {
745 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
746 if (!(value & 0x80000000) && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
750 /* Calculate the next timer event */
751 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
752 next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
756 timer_mod(timer, next);
759 static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t decr,
762 ppc_tb_t *tb_env = cpu->env.tb_env;
764 __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env->decr_timer,
765 tb_env->decr_timer->cb, &cpu_ppc_decr_lower, decr,
769 void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
771 PowerPCCPU *cpu = ppc_env_get_cpu(env);
773 _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value);
776 static void cpu_ppc_decr_cb(void *opaque)
778 PowerPCCPU *cpu = opaque;
780 cpu_ppc_decr_excp(cpu);
783 static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t hdecr,
786 ppc_tb_t *tb_env = cpu->env.tb_env;
788 if (tb_env->hdecr_timer != NULL) {
789 __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env->hdecr_timer,
790 tb_env->hdecr_timer->cb, &cpu_ppc_hdecr_lower,
795 void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
797 PowerPCCPU *cpu = ppc_env_get_cpu(env);
799 _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value);
802 static void cpu_ppc_hdecr_cb(void *opaque)
804 PowerPCCPU *cpu = opaque;
806 cpu_ppc_hdecr_excp(cpu);
809 static void cpu_ppc_store_purr(PowerPCCPU *cpu, uint64_t value)
811 ppc_tb_t *tb_env = cpu->env.tb_env;
813 tb_env->purr_load = value;
814 tb_env->purr_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
817 static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
819 CPUPPCState *env = opaque;
820 PowerPCCPU *cpu = ppc_env_get_cpu(env);
821 ppc_tb_t *tb_env = env->tb_env;
823 tb_env->tb_freq = freq;
824 tb_env->decr_freq = freq;
825 /* There is a bug in Linux 2.4 kernels:
826 * if a decrementer exception is pending when it enables msr_ee at startup,
827 * it's not ready to handle it...
829 _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
830 _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
831 cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
834 static void timebase_pre_save(void *opaque)
836 PPCTimebase *tb = opaque;
837 uint64_t ticks = cpu_get_real_ticks();
838 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
840 if (!first_ppc_cpu->env.tb_env) {
841 error_report("No timebase object");
845 tb->time_of_the_day_ns = qemu_clock_get_ns(QEMU_CLOCK_HOST);
847 * tb_offset is only expected to be changed by migration so
848 * there is no need to update it from KVM here
850 tb->guest_timebase = ticks + first_ppc_cpu->env.tb_env->tb_offset;
853 static int timebase_post_load(void *opaque, int version_id)
855 PPCTimebase *tb_remote = opaque;
857 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
858 int64_t tb_off_adj, tb_off, ns_diff;
859 int64_t migration_duration_ns, migration_duration_tb, guest_tb, host_ns;
862 if (!first_ppc_cpu->env.tb_env) {
863 error_report("No timebase object");
867 freq = first_ppc_cpu->env.tb_env->tb_freq;
869 * Calculate timebase on the destination side of migration.
870 * The destination timebase must be not less than the source timebase.
871 * We try to adjust timebase by downtime if host clocks are not
872 * too much out of sync (1 second for now).
874 host_ns = qemu_clock_get_ns(QEMU_CLOCK_HOST);
875 ns_diff = MAX(0, host_ns - tb_remote->time_of_the_day_ns);
876 migration_duration_ns = MIN(NSEC_PER_SEC, ns_diff);
877 migration_duration_tb = muldiv64(migration_duration_ns, freq, NSEC_PER_SEC);
878 guest_tb = tb_remote->guest_timebase + MIN(0, migration_duration_tb);
880 tb_off_adj = guest_tb - cpu_get_real_ticks();
882 tb_off = first_ppc_cpu->env.tb_env->tb_offset;
883 trace_ppc_tb_adjust(tb_off, tb_off_adj, tb_off_adj - tb_off,
884 (tb_off_adj - tb_off) / freq);
886 /* Set new offset to all CPUs */
888 PowerPCCPU *pcpu = POWERPC_CPU(cpu);
889 pcpu->env.tb_env->tb_offset = tb_off_adj;
895 const VMStateDescription vmstate_ppc_timebase = {
898 .minimum_version_id = 1,
899 .minimum_version_id_old = 1,
900 .pre_save = timebase_pre_save,
901 .post_load = timebase_post_load,
902 .fields = (VMStateField []) {
903 VMSTATE_UINT64(guest_timebase, PPCTimebase),
904 VMSTATE_INT64(time_of_the_day_ns, PPCTimebase),
905 VMSTATE_END_OF_LIST()
909 /* Set up (once) timebase frequency (in Hz) */
910 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
912 PowerPCCPU *cpu = ppc_env_get_cpu(env);
915 tb_env = g_malloc0(sizeof(ppc_tb_t));
916 env->tb_env = tb_env;
917 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
918 if (env->insns_flags & PPC_SEGMENT_64B) {
919 /* All Book3S 64bit CPUs implement level based DEC logic */
920 tb_env->flags |= PPC_DECR_UNDERFLOW_LEVEL;
922 /* Create new timer */
923 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_decr_cb, cpu);
925 /* XXX: find a suitable condition to enable the hypervisor decrementer
927 tb_env->hdecr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_hdecr_cb,
930 tb_env->hdecr_timer = NULL;
932 cpu_ppc_set_tb_clk(env, freq);
934 return &cpu_ppc_set_tb_clk;
937 /* Specific helpers for POWER & PowerPC 601 RTC */
939 static clk_setup_cb cpu_ppc601_rtc_init (CPUPPCState *env)
941 return cpu_ppc_tb_init(env, 7812500);
945 void cpu_ppc601_store_rtcu (CPUPPCState *env, uint32_t value)
947 _cpu_ppc_store_tbu(env, value);
950 uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env)
952 return _cpu_ppc_load_tbu(env);
955 void cpu_ppc601_store_rtcl (CPUPPCState *env, uint32_t value)
957 cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
960 uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env)
962 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
965 /*****************************************************************************/
966 /* PowerPC 40x timers */
969 typedef struct ppc40x_timer_t ppc40x_timer_t;
970 struct ppc40x_timer_t {
971 uint64_t pit_reload; /* PIT auto-reload value */
972 uint64_t fit_next; /* Tick for next FIT interrupt */
973 QEMUTimer *fit_timer;
974 uint64_t wdt_next; /* Tick for next WDT interrupt */
975 QEMUTimer *wdt_timer;
977 /* 405 have the PIT, 440 have a DECR. */
978 unsigned int decr_excp;
981 /* Fixed interval timer */
982 static void cpu_4xx_fit_cb (void *opaque)
987 ppc40x_timer_t *ppc40x_timer;
991 cpu = ppc_env_get_cpu(env);
992 tb_env = env->tb_env;
993 ppc40x_timer = tb_env->opaque;
994 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
995 switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
1009 /* Cannot occur, but makes gcc happy */
1012 next = now + muldiv64(next, get_ticks_per_sec(), tb_env->tb_freq);
1015 timer_mod(ppc40x_timer->fit_timer, next);
1016 env->spr[SPR_40x_TSR] |= 1 << 26;
1017 if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) {
1018 ppc_set_irq(cpu, PPC_INTERRUPT_FIT, 1);
1020 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
1021 (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
1022 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
1025 /* Programmable interval timer */
1026 static void start_stop_pit (CPUPPCState *env, ppc_tb_t *tb_env, int is_excp)
1028 ppc40x_timer_t *ppc40x_timer;
1031 ppc40x_timer = tb_env->opaque;
1032 if (ppc40x_timer->pit_reload <= 1 ||
1033 !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
1034 (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
1036 LOG_TB("%s: stop PIT\n", __func__);
1037 timer_del(tb_env->decr_timer);
1039 LOG_TB("%s: start PIT %016" PRIx64 "\n",
1040 __func__, ppc40x_timer->pit_reload);
1041 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1042 next = now + muldiv64(ppc40x_timer->pit_reload,
1043 get_ticks_per_sec(), tb_env->decr_freq);
1045 next += tb_env->decr_next - now;
1048 timer_mod(tb_env->decr_timer, next);
1049 tb_env->decr_next = next;
1053 static void cpu_4xx_pit_cb (void *opaque)
1058 ppc40x_timer_t *ppc40x_timer;
1061 cpu = ppc_env_get_cpu(env);
1062 tb_env = env->tb_env;
1063 ppc40x_timer = tb_env->opaque;
1064 env->spr[SPR_40x_TSR] |= 1 << 27;
1065 if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) {
1066 ppc_set_irq(cpu, ppc40x_timer->decr_excp, 1);
1068 start_stop_pit(env, tb_env, 1);
1069 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " "
1070 "%016" PRIx64 "\n", __func__,
1071 (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
1072 (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
1073 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
1074 ppc40x_timer->pit_reload);
1077 /* Watchdog timer */
1078 static void cpu_4xx_wdt_cb (void *opaque)
1083 ppc40x_timer_t *ppc40x_timer;
1087 cpu = ppc_env_get_cpu(env);
1088 tb_env = env->tb_env;
1089 ppc40x_timer = tb_env->opaque;
1090 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1091 switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
1105 /* Cannot occur, but makes gcc happy */
1108 next = now + muldiv64(next, get_ticks_per_sec(), tb_env->decr_freq);
1111 LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
1112 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
1113 switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
1116 timer_mod(ppc40x_timer->wdt_timer, next);
1117 ppc40x_timer->wdt_next = next;
1118 env->spr[SPR_40x_TSR] |= 1U << 31;
1121 timer_mod(ppc40x_timer->wdt_timer, next);
1122 ppc40x_timer->wdt_next = next;
1123 env->spr[SPR_40x_TSR] |= 1 << 30;
1124 if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) {
1125 ppc_set_irq(cpu, PPC_INTERRUPT_WDT, 1);
1129 env->spr[SPR_40x_TSR] &= ~0x30000000;
1130 env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
1131 switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
1135 case 0x1: /* Core reset */
1136 ppc40x_core_reset(cpu);
1138 case 0x2: /* Chip reset */
1139 ppc40x_chip_reset(cpu);
1141 case 0x3: /* System reset */
1142 ppc40x_system_reset(cpu);
1148 void store_40x_pit (CPUPPCState *env, target_ulong val)
1151 ppc40x_timer_t *ppc40x_timer;
1153 tb_env = env->tb_env;
1154 ppc40x_timer = tb_env->opaque;
1155 LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val);
1156 ppc40x_timer->pit_reload = val;
1157 start_stop_pit(env, tb_env, 0);
1160 target_ulong load_40x_pit (CPUPPCState *env)
1162 return cpu_ppc_load_decr(env);
1165 static void ppc_40x_set_tb_clk (void *opaque, uint32_t freq)
1167 CPUPPCState *env = opaque;
1168 ppc_tb_t *tb_env = env->tb_env;
1170 LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__,
1172 tb_env->tb_freq = freq;
1173 tb_env->decr_freq = freq;
1174 /* XXX: we should also update all timers */
1177 clk_setup_cb ppc_40x_timers_init (CPUPPCState *env, uint32_t freq,
1178 unsigned int decr_excp)
1181 ppc40x_timer_t *ppc40x_timer;
1183 tb_env = g_malloc0(sizeof(ppc_tb_t));
1184 env->tb_env = tb_env;
1185 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
1186 ppc40x_timer = g_malloc0(sizeof(ppc40x_timer_t));
1187 tb_env->tb_freq = freq;
1188 tb_env->decr_freq = freq;
1189 tb_env->opaque = ppc40x_timer;
1190 LOG_TB("%s freq %" PRIu32 "\n", __func__, freq);
1191 if (ppc40x_timer != NULL) {
1192 /* We use decr timer for PIT */
1193 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_pit_cb, env);
1194 ppc40x_timer->fit_timer =
1195 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_fit_cb, env);
1196 ppc40x_timer->wdt_timer =
1197 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_wdt_cb, env);
1198 ppc40x_timer->decr_excp = decr_excp;
1201 return &ppc_40x_set_tb_clk;
1204 /*****************************************************************************/
1205 /* Embedded PowerPC Device Control Registers */
1206 typedef struct ppc_dcrn_t ppc_dcrn_t;
1208 dcr_read_cb dcr_read;
1209 dcr_write_cb dcr_write;
1213 /* XXX: on 460, DCR addresses are 32 bits wide,
1214 * using DCRIPR to get the 22 upper bits of the DCR address
1216 #define DCRN_NB 1024
1218 ppc_dcrn_t dcrn[DCRN_NB];
1219 int (*read_error)(int dcrn);
1220 int (*write_error)(int dcrn);
1223 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
1227 if (dcrn < 0 || dcrn >= DCRN_NB)
1229 dcr = &dcr_env->dcrn[dcrn];
1230 if (dcr->dcr_read == NULL)
1232 *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
1237 if (dcr_env->read_error != NULL)
1238 return (*dcr_env->read_error)(dcrn);
1243 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
1247 if (dcrn < 0 || dcrn >= DCRN_NB)
1249 dcr = &dcr_env->dcrn[dcrn];
1250 if (dcr->dcr_write == NULL)
1252 (*dcr->dcr_write)(dcr->opaque, dcrn, val);
1257 if (dcr_env->write_error != NULL)
1258 return (*dcr_env->write_error)(dcrn);
1263 int ppc_dcr_register (CPUPPCState *env, int dcrn, void *opaque,
1264 dcr_read_cb dcr_read, dcr_write_cb dcr_write)
1269 dcr_env = env->dcr_env;
1270 if (dcr_env == NULL)
1272 if (dcrn < 0 || dcrn >= DCRN_NB)
1274 dcr = &dcr_env->dcrn[dcrn];
1275 if (dcr->opaque != NULL ||
1276 dcr->dcr_read != NULL ||
1277 dcr->dcr_write != NULL)
1279 dcr->opaque = opaque;
1280 dcr->dcr_read = dcr_read;
1281 dcr->dcr_write = dcr_write;
1286 int ppc_dcr_init (CPUPPCState *env, int (*read_error)(int dcrn),
1287 int (*write_error)(int dcrn))
1291 dcr_env = g_malloc0(sizeof(ppc_dcr_t));
1292 dcr_env->read_error = read_error;
1293 dcr_env->write_error = write_error;
1294 env->dcr_env = dcr_env;
1299 /*****************************************************************************/
1301 void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
1313 printf("Set loglevel to %04" PRIx32 "\n", val);
1314 qemu_set_log(val | 0x100);
1319 /* CPU device-tree ID helpers */
1320 int ppc_get_vcpu_dt_id(PowerPCCPU *cpu)
1322 return cpu->cpu_dt_id;
1325 PowerPCCPU *ppc_get_vcpu_by_dt_id(int cpu_dt_id)
1330 PowerPCCPU *cpu = POWERPC_CPU(cs);
1332 if (cpu->cpu_dt_id == cpu_dt_id) {