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 "hw/loader.h"
33 #include "sysemu/kvm.h"
36 //#define PPC_DEBUG_IRQ
37 //#define PPC_DEBUG_TB
40 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
42 # define LOG_IRQ(...) do { } while (0)
47 # define LOG_TB(...) qemu_log(__VA_ARGS__)
49 # define LOG_TB(...) do { } while (0)
52 static void cpu_ppc_tb_stop (CPUPPCState *env);
53 static void cpu_ppc_tb_start (CPUPPCState *env);
55 void ppc_set_irq(PowerPCCPU *cpu, int n_IRQ, int level)
57 CPUState *cs = CPU(cpu);
58 CPUPPCState *env = &cpu->env;
59 unsigned int old_pending = env->pending_interrupts;
62 env->pending_interrupts |= 1 << n_IRQ;
63 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
65 env->pending_interrupts &= ~(1 << n_IRQ);
66 if (env->pending_interrupts == 0) {
67 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
71 if (old_pending != env->pending_interrupts) {
73 kvmppc_set_interrupt(cpu, n_IRQ, level);
77 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
78 "req %08x\n", __func__, env, n_IRQ, level,
79 env->pending_interrupts, CPU(cpu)->interrupt_request);
82 /* PowerPC 6xx / 7xx internal IRQ controller */
83 static void ppc6xx_set_irq(void *opaque, int pin, int level)
85 PowerPCCPU *cpu = opaque;
86 CPUPPCState *env = &cpu->env;
89 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
91 cur_level = (env->irq_input_state >> pin) & 1;
92 /* Don't generate spurious events */
93 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
94 CPUState *cs = CPU(cpu);
97 case PPC6xx_INPUT_TBEN:
98 /* Level sensitive - active high */
99 LOG_IRQ("%s: %s the time base\n",
100 __func__, level ? "start" : "stop");
102 cpu_ppc_tb_start(env);
104 cpu_ppc_tb_stop(env);
106 case PPC6xx_INPUT_INT:
107 /* Level sensitive - active high */
108 LOG_IRQ("%s: set the external IRQ state to %d\n",
110 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
112 case PPC6xx_INPUT_SMI:
113 /* Level sensitive - active high */
114 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
116 ppc_set_irq(cpu, PPC_INTERRUPT_SMI, level);
118 case PPC6xx_INPUT_MCP:
119 /* Negative edge sensitive */
120 /* XXX: TODO: actual reaction may depends on HID0 status
121 * 603/604/740/750: check HID0[EMCP]
123 if (cur_level == 1 && level == 0) {
124 LOG_IRQ("%s: raise machine check state\n",
126 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1);
129 case PPC6xx_INPUT_CKSTP_IN:
130 /* Level sensitive - active low */
131 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
132 /* XXX: Note that the only way to restart the CPU is to reset it */
134 LOG_IRQ("%s: stop the CPU\n", __func__);
138 case PPC6xx_INPUT_HRESET:
139 /* Level sensitive - active low */
141 LOG_IRQ("%s: reset the CPU\n", __func__);
142 cpu_interrupt(cs, CPU_INTERRUPT_RESET);
145 case PPC6xx_INPUT_SRESET:
146 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
148 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level);
151 /* Unknown pin - do nothing */
152 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
156 env->irq_input_state |= 1 << pin;
158 env->irq_input_state &= ~(1 << pin);
162 void ppc6xx_irq_init(CPUPPCState *env)
164 PowerPCCPU *cpu = ppc_env_get_cpu(env);
166 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, cpu,
170 #if defined(TARGET_PPC64)
171 /* PowerPC 970 internal IRQ controller */
172 static void ppc970_set_irq(void *opaque, int pin, int level)
174 PowerPCCPU *cpu = opaque;
175 CPUPPCState *env = &cpu->env;
178 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
180 cur_level = (env->irq_input_state >> pin) & 1;
181 /* Don't generate spurious events */
182 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
183 CPUState *cs = CPU(cpu);
186 case PPC970_INPUT_INT:
187 /* Level sensitive - active high */
188 LOG_IRQ("%s: set the external IRQ state to %d\n",
190 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
192 case PPC970_INPUT_THINT:
193 /* Level sensitive - active high */
194 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__,
196 ppc_set_irq(cpu, PPC_INTERRUPT_THERM, level);
198 case PPC970_INPUT_MCP:
199 /* Negative edge sensitive */
200 /* XXX: TODO: actual reaction may depends on HID0 status
201 * 603/604/740/750: check HID0[EMCP]
203 if (cur_level == 1 && level == 0) {
204 LOG_IRQ("%s: raise machine check state\n",
206 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1);
209 case PPC970_INPUT_CKSTP:
210 /* Level sensitive - active low */
211 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
213 LOG_IRQ("%s: stop the CPU\n", __func__);
216 LOG_IRQ("%s: restart the CPU\n", __func__);
221 case PPC970_INPUT_HRESET:
222 /* Level sensitive - active low */
224 cpu_interrupt(cs, CPU_INTERRUPT_RESET);
227 case PPC970_INPUT_SRESET:
228 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
230 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level);
232 case PPC970_INPUT_TBEN:
233 LOG_IRQ("%s: set the TBEN state to %d\n", __func__,
238 /* Unknown pin - do nothing */
239 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
243 env->irq_input_state |= 1 << pin;
245 env->irq_input_state &= ~(1 << pin);
249 void ppc970_irq_init(CPUPPCState *env)
251 PowerPCCPU *cpu = ppc_env_get_cpu(env);
253 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, cpu,
257 /* POWER7 internal IRQ controller */
258 static void power7_set_irq(void *opaque, int pin, int level)
260 PowerPCCPU *cpu = opaque;
261 CPUPPCState *env = &cpu->env;
263 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
267 case POWER7_INPUT_INT:
268 /* Level sensitive - active high */
269 LOG_IRQ("%s: set the external IRQ state to %d\n",
271 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
274 /* Unknown pin - do nothing */
275 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
279 env->irq_input_state |= 1 << pin;
281 env->irq_input_state &= ~(1 << pin);
285 void ppcPOWER7_irq_init(CPUPPCState *env)
287 PowerPCCPU *cpu = ppc_env_get_cpu(env);
289 env->irq_inputs = (void **)qemu_allocate_irqs(&power7_set_irq, cpu,
292 #endif /* defined(TARGET_PPC64) */
294 /* PowerPC 40x internal IRQ controller */
295 static void ppc40x_set_irq(void *opaque, int pin, int level)
297 PowerPCCPU *cpu = opaque;
298 CPUPPCState *env = &cpu->env;
301 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
303 cur_level = (env->irq_input_state >> pin) & 1;
304 /* Don't generate spurious events */
305 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
306 CPUState *cs = CPU(cpu);
309 case PPC40x_INPUT_RESET_SYS:
311 LOG_IRQ("%s: reset the PowerPC system\n",
313 ppc40x_system_reset(cpu);
316 case PPC40x_INPUT_RESET_CHIP:
318 LOG_IRQ("%s: reset the PowerPC chip\n", __func__);
319 ppc40x_chip_reset(cpu);
322 case PPC40x_INPUT_RESET_CORE:
323 /* XXX: TODO: update DBSR[MRR] */
325 LOG_IRQ("%s: reset the PowerPC core\n", __func__);
326 ppc40x_core_reset(cpu);
329 case PPC40x_INPUT_CINT:
330 /* Level sensitive - active high */
331 LOG_IRQ("%s: set the critical IRQ state to %d\n",
333 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level);
335 case PPC40x_INPUT_INT:
336 /* Level sensitive - active high */
337 LOG_IRQ("%s: set the external IRQ state to %d\n",
339 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
341 case PPC40x_INPUT_HALT:
342 /* Level sensitive - active low */
344 LOG_IRQ("%s: stop the CPU\n", __func__);
347 LOG_IRQ("%s: restart the CPU\n", __func__);
352 case PPC40x_INPUT_DEBUG:
353 /* Level sensitive - active high */
354 LOG_IRQ("%s: set the debug pin state to %d\n",
356 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level);
359 /* Unknown pin - do nothing */
360 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
364 env->irq_input_state |= 1 << pin;
366 env->irq_input_state &= ~(1 << pin);
370 void ppc40x_irq_init(CPUPPCState *env)
372 PowerPCCPU *cpu = ppc_env_get_cpu(env);
374 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq,
375 cpu, PPC40x_INPUT_NB);
378 /* PowerPC E500 internal IRQ controller */
379 static void ppce500_set_irq(void *opaque, int pin, int level)
381 PowerPCCPU *cpu = opaque;
382 CPUPPCState *env = &cpu->env;
385 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
387 cur_level = (env->irq_input_state >> pin) & 1;
388 /* Don't generate spurious events */
389 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
391 case PPCE500_INPUT_MCK:
393 LOG_IRQ("%s: reset the PowerPC system\n",
395 qemu_system_reset_request();
398 case PPCE500_INPUT_RESET_CORE:
400 LOG_IRQ("%s: reset the PowerPC core\n", __func__);
401 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, level);
404 case PPCE500_INPUT_CINT:
405 /* Level sensitive - active high */
406 LOG_IRQ("%s: set the critical IRQ state to %d\n",
408 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level);
410 case PPCE500_INPUT_INT:
411 /* Level sensitive - active high */
412 LOG_IRQ("%s: set the core IRQ state to %d\n",
414 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
416 case PPCE500_INPUT_DEBUG:
417 /* Level sensitive - active high */
418 LOG_IRQ("%s: set the debug pin state to %d\n",
420 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level);
423 /* Unknown pin - do nothing */
424 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
428 env->irq_input_state |= 1 << pin;
430 env->irq_input_state &= ~(1 << pin);
434 void ppce500_irq_init(CPUPPCState *env)
436 PowerPCCPU *cpu = ppc_env_get_cpu(env);
438 env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq,
439 cpu, PPCE500_INPUT_NB);
442 /* Enable or Disable the E500 EPR capability */
443 void ppce500_set_mpic_proxy(bool enabled)
448 PowerPCCPU *cpu = POWERPC_CPU(cs);
450 cpu->env.mpic_proxy = enabled;
452 kvmppc_set_mpic_proxy(cpu, enabled);
457 /*****************************************************************************/
458 /* PowerPC time base and decrementer emulation */
460 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset)
462 /* TB time in tb periods */
463 return muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()) + tb_offset;
466 uint64_t cpu_ppc_load_tbl (CPUPPCState *env)
468 ppc_tb_t *tb_env = env->tb_env;
472 return env->spr[SPR_TBL];
475 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
476 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
481 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState *env)
483 ppc_tb_t *tb_env = env->tb_env;
486 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
487 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
492 uint32_t cpu_ppc_load_tbu (CPUPPCState *env)
495 return env->spr[SPR_TBU];
498 return _cpu_ppc_load_tbu(env);
501 static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk,
502 int64_t *tb_offsetp, uint64_t value)
504 *tb_offsetp = value - muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec());
505 LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n",
506 __func__, value, *tb_offsetp);
509 void cpu_ppc_store_tbl (CPUPPCState *env, uint32_t value)
511 ppc_tb_t *tb_env = env->tb_env;
514 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
515 tb &= 0xFFFFFFFF00000000ULL;
516 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
517 &tb_env->tb_offset, tb | (uint64_t)value);
520 static inline void _cpu_ppc_store_tbu(CPUPPCState *env, uint32_t value)
522 ppc_tb_t *tb_env = env->tb_env;
525 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
526 tb &= 0x00000000FFFFFFFFULL;
527 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
528 &tb_env->tb_offset, ((uint64_t)value << 32) | tb);
531 void cpu_ppc_store_tbu (CPUPPCState *env, uint32_t value)
533 _cpu_ppc_store_tbu(env, value);
536 uint64_t cpu_ppc_load_atbl (CPUPPCState *env)
538 ppc_tb_t *tb_env = env->tb_env;
541 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
542 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
547 uint32_t cpu_ppc_load_atbu (CPUPPCState *env)
549 ppc_tb_t *tb_env = env->tb_env;
552 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
553 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
558 void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value)
560 ppc_tb_t *tb_env = env->tb_env;
563 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
564 tb &= 0xFFFFFFFF00000000ULL;
565 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
566 &tb_env->atb_offset, tb | (uint64_t)value);
569 void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value)
571 ppc_tb_t *tb_env = env->tb_env;
574 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
575 tb &= 0x00000000FFFFFFFFULL;
576 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
577 &tb_env->atb_offset, ((uint64_t)value << 32) | tb);
580 static void cpu_ppc_tb_stop (CPUPPCState *env)
582 ppc_tb_t *tb_env = env->tb_env;
583 uint64_t tb, atb, vmclk;
585 /* If the time base is already frozen, do nothing */
586 if (tb_env->tb_freq != 0) {
587 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
588 /* Get the time base */
589 tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset);
590 /* Get the alternate time base */
591 atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset);
592 /* Store the time base value (ie compute the current offset) */
593 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
594 /* Store the alternate time base value (compute the current offset) */
595 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
596 /* Set the time base frequency to zero */
598 /* Now, the time bases are frozen to tb_offset / atb_offset value */
602 static void cpu_ppc_tb_start (CPUPPCState *env)
604 ppc_tb_t *tb_env = env->tb_env;
605 uint64_t tb, atb, vmclk;
607 /* If the time base is not frozen, do nothing */
608 if (tb_env->tb_freq == 0) {
609 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
610 /* Get the time base from tb_offset */
611 tb = tb_env->tb_offset;
612 /* Get the alternate time base from atb_offset */
613 atb = tb_env->atb_offset;
614 /* Restore the tb frequency from the decrementer frequency */
615 tb_env->tb_freq = tb_env->decr_freq;
616 /* Store the time base value */
617 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
618 /* Store the alternate time base value */
619 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
623 bool ppc_decr_clear_on_delivery(CPUPPCState *env)
625 ppc_tb_t *tb_env = env->tb_env;
626 int flags = PPC_DECR_UNDERFLOW_TRIGGERED | PPC_DECR_UNDERFLOW_LEVEL;
627 return ((tb_env->flags & flags) == PPC_DECR_UNDERFLOW_TRIGGERED);
630 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
632 ppc_tb_t *tb_env = env->tb_env;
636 diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
638 decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec());
639 } else if (tb_env->flags & PPC_TIMER_BOOKE) {
642 decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec());
644 LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
649 uint32_t cpu_ppc_load_decr (CPUPPCState *env)
651 ppc_tb_t *tb_env = env->tb_env;
654 return env->spr[SPR_DECR];
657 return _cpu_ppc_load_decr(env, tb_env->decr_next);
660 uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)
662 ppc_tb_t *tb_env = env->tb_env;
664 return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
667 uint64_t cpu_ppc_load_purr (CPUPPCState *env)
669 ppc_tb_t *tb_env = env->tb_env;
672 diff = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - tb_env->purr_start;
674 return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, get_ticks_per_sec());
677 /* When decrementer expires,
678 * all we need to do is generate or queue a CPU exception
680 static inline void cpu_ppc_decr_excp(PowerPCCPU *cpu)
683 LOG_TB("raise decrementer exception\n");
684 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 1);
687 static inline void cpu_ppc_decr_lower(PowerPCCPU *cpu)
689 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 0);
692 static inline void cpu_ppc_hdecr_excp(PowerPCCPU *cpu)
695 LOG_TB("raise decrementer exception\n");
696 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 1);
699 static inline void cpu_ppc_hdecr_lower(PowerPCCPU *cpu)
701 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
704 static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
706 void (*raise_excp)(void *),
707 void (*lower_excp)(PowerPCCPU *),
708 uint32_t decr, uint32_t value)
710 CPUPPCState *env = &cpu->env;
711 ppc_tb_t *tb_env = env->tb_env;
714 LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
718 /* KVM handles decrementer exceptions, we don't need our own timer */
723 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
726 * If we get a really small DEC value, we can assume that by the time we
727 * handled it we should inject an interrupt already.
729 * On MSB level based DEC implementations the MSB always means the interrupt
730 * is pending, so raise it on those.
732 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
733 * an edge interrupt, so raise it here too.
736 ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && (value & 0x80000000)) ||
737 ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && (value & 0x80000000)
738 && !(decr & 0x80000000))) {
743 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
744 if (!(value & 0x80000000) && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
748 /* Calculate the next timer event */
749 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
750 next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
754 timer_mod(timer, next);
757 static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t decr,
760 ppc_tb_t *tb_env = cpu->env.tb_env;
762 __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env->decr_timer,
763 tb_env->decr_timer->cb, &cpu_ppc_decr_lower, decr,
767 void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
769 PowerPCCPU *cpu = ppc_env_get_cpu(env);
771 _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value);
774 static void cpu_ppc_decr_cb(void *opaque)
776 PowerPCCPU *cpu = opaque;
778 cpu_ppc_decr_excp(cpu);
781 static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t hdecr,
784 ppc_tb_t *tb_env = cpu->env.tb_env;
786 if (tb_env->hdecr_timer != NULL) {
787 __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env->hdecr_timer,
788 tb_env->hdecr_timer->cb, &cpu_ppc_hdecr_lower,
793 void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
795 PowerPCCPU *cpu = ppc_env_get_cpu(env);
797 _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value);
800 static void cpu_ppc_hdecr_cb(void *opaque)
802 PowerPCCPU *cpu = opaque;
804 cpu_ppc_hdecr_excp(cpu);
807 static void cpu_ppc_store_purr(PowerPCCPU *cpu, uint64_t value)
809 ppc_tb_t *tb_env = cpu->env.tb_env;
811 tb_env->purr_load = value;
812 tb_env->purr_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
815 static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
817 CPUPPCState *env = opaque;
818 PowerPCCPU *cpu = ppc_env_get_cpu(env);
819 ppc_tb_t *tb_env = env->tb_env;
821 tb_env->tb_freq = freq;
822 tb_env->decr_freq = freq;
823 /* There is a bug in Linux 2.4 kernels:
824 * if a decrementer exception is pending when it enables msr_ee at startup,
825 * it's not ready to handle it...
827 _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
828 _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
829 cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
832 /* Set up (once) timebase frequency (in Hz) */
833 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
835 PowerPCCPU *cpu = ppc_env_get_cpu(env);
838 tb_env = g_malloc0(sizeof(ppc_tb_t));
839 env->tb_env = tb_env;
840 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
841 if (env->insns_flags & PPC_SEGMENT_64B) {
842 /* All Book3S 64bit CPUs implement level based DEC logic */
843 tb_env->flags |= PPC_DECR_UNDERFLOW_LEVEL;
845 /* Create new timer */
846 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_decr_cb, cpu);
848 /* XXX: find a suitable condition to enable the hypervisor decrementer
850 tb_env->hdecr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_hdecr_cb,
853 tb_env->hdecr_timer = NULL;
855 cpu_ppc_set_tb_clk(env, freq);
857 return &cpu_ppc_set_tb_clk;
860 /* Specific helpers for POWER & PowerPC 601 RTC */
862 static clk_setup_cb cpu_ppc601_rtc_init (CPUPPCState *env)
864 return cpu_ppc_tb_init(env, 7812500);
868 void cpu_ppc601_store_rtcu (CPUPPCState *env, uint32_t value)
870 _cpu_ppc_store_tbu(env, value);
873 uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env)
875 return _cpu_ppc_load_tbu(env);
878 void cpu_ppc601_store_rtcl (CPUPPCState *env, uint32_t value)
880 cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
883 uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env)
885 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
888 /*****************************************************************************/
889 /* PowerPC 40x timers */
892 typedef struct ppc40x_timer_t ppc40x_timer_t;
893 struct ppc40x_timer_t {
894 uint64_t pit_reload; /* PIT auto-reload value */
895 uint64_t fit_next; /* Tick for next FIT interrupt */
896 QEMUTimer *fit_timer;
897 uint64_t wdt_next; /* Tick for next WDT interrupt */
898 QEMUTimer *wdt_timer;
900 /* 405 have the PIT, 440 have a DECR. */
901 unsigned int decr_excp;
904 /* Fixed interval timer */
905 static void cpu_4xx_fit_cb (void *opaque)
910 ppc40x_timer_t *ppc40x_timer;
914 cpu = ppc_env_get_cpu(env);
915 tb_env = env->tb_env;
916 ppc40x_timer = tb_env->opaque;
917 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
918 switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
932 /* Cannot occur, but makes gcc happy */
935 next = now + muldiv64(next, get_ticks_per_sec(), tb_env->tb_freq);
938 timer_mod(ppc40x_timer->fit_timer, next);
939 env->spr[SPR_40x_TSR] |= 1 << 26;
940 if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) {
941 ppc_set_irq(cpu, PPC_INTERRUPT_FIT, 1);
943 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
944 (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
945 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
948 /* Programmable interval timer */
949 static void start_stop_pit (CPUPPCState *env, ppc_tb_t *tb_env, int is_excp)
951 ppc40x_timer_t *ppc40x_timer;
954 ppc40x_timer = tb_env->opaque;
955 if (ppc40x_timer->pit_reload <= 1 ||
956 !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
957 (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
959 LOG_TB("%s: stop PIT\n", __func__);
960 timer_del(tb_env->decr_timer);
962 LOG_TB("%s: start PIT %016" PRIx64 "\n",
963 __func__, ppc40x_timer->pit_reload);
964 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
965 next = now + muldiv64(ppc40x_timer->pit_reload,
966 get_ticks_per_sec(), tb_env->decr_freq);
968 next += tb_env->decr_next - now;
971 timer_mod(tb_env->decr_timer, next);
972 tb_env->decr_next = next;
976 static void cpu_4xx_pit_cb (void *opaque)
981 ppc40x_timer_t *ppc40x_timer;
984 cpu = ppc_env_get_cpu(env);
985 tb_env = env->tb_env;
986 ppc40x_timer = tb_env->opaque;
987 env->spr[SPR_40x_TSR] |= 1 << 27;
988 if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) {
989 ppc_set_irq(cpu, ppc40x_timer->decr_excp, 1);
991 start_stop_pit(env, tb_env, 1);
992 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " "
993 "%016" PRIx64 "\n", __func__,
994 (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
995 (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
996 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
997 ppc40x_timer->pit_reload);
1000 /* Watchdog timer */
1001 static void cpu_4xx_wdt_cb (void *opaque)
1006 ppc40x_timer_t *ppc40x_timer;
1010 cpu = ppc_env_get_cpu(env);
1011 tb_env = env->tb_env;
1012 ppc40x_timer = tb_env->opaque;
1013 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1014 switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
1028 /* Cannot occur, but makes gcc happy */
1031 next = now + muldiv64(next, get_ticks_per_sec(), tb_env->decr_freq);
1034 LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
1035 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
1036 switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
1039 timer_mod(ppc40x_timer->wdt_timer, next);
1040 ppc40x_timer->wdt_next = next;
1041 env->spr[SPR_40x_TSR] |= 1U << 31;
1044 timer_mod(ppc40x_timer->wdt_timer, next);
1045 ppc40x_timer->wdt_next = next;
1046 env->spr[SPR_40x_TSR] |= 1 << 30;
1047 if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) {
1048 ppc_set_irq(cpu, PPC_INTERRUPT_WDT, 1);
1052 env->spr[SPR_40x_TSR] &= ~0x30000000;
1053 env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
1054 switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
1058 case 0x1: /* Core reset */
1059 ppc40x_core_reset(cpu);
1061 case 0x2: /* Chip reset */
1062 ppc40x_chip_reset(cpu);
1064 case 0x3: /* System reset */
1065 ppc40x_system_reset(cpu);
1071 void store_40x_pit (CPUPPCState *env, target_ulong val)
1074 ppc40x_timer_t *ppc40x_timer;
1076 tb_env = env->tb_env;
1077 ppc40x_timer = tb_env->opaque;
1078 LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val);
1079 ppc40x_timer->pit_reload = val;
1080 start_stop_pit(env, tb_env, 0);
1083 target_ulong load_40x_pit (CPUPPCState *env)
1085 return cpu_ppc_load_decr(env);
1088 static void ppc_40x_set_tb_clk (void *opaque, uint32_t freq)
1090 CPUPPCState *env = opaque;
1091 ppc_tb_t *tb_env = env->tb_env;
1093 LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__,
1095 tb_env->tb_freq = freq;
1096 tb_env->decr_freq = freq;
1097 /* XXX: we should also update all timers */
1100 clk_setup_cb ppc_40x_timers_init (CPUPPCState *env, uint32_t freq,
1101 unsigned int decr_excp)
1104 ppc40x_timer_t *ppc40x_timer;
1106 tb_env = g_malloc0(sizeof(ppc_tb_t));
1107 env->tb_env = tb_env;
1108 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
1109 ppc40x_timer = g_malloc0(sizeof(ppc40x_timer_t));
1110 tb_env->tb_freq = freq;
1111 tb_env->decr_freq = freq;
1112 tb_env->opaque = ppc40x_timer;
1113 LOG_TB("%s freq %" PRIu32 "\n", __func__, freq);
1114 if (ppc40x_timer != NULL) {
1115 /* We use decr timer for PIT */
1116 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_pit_cb, env);
1117 ppc40x_timer->fit_timer =
1118 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_fit_cb, env);
1119 ppc40x_timer->wdt_timer =
1120 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_wdt_cb, env);
1121 ppc40x_timer->decr_excp = decr_excp;
1124 return &ppc_40x_set_tb_clk;
1127 /*****************************************************************************/
1128 /* Embedded PowerPC Device Control Registers */
1129 typedef struct ppc_dcrn_t ppc_dcrn_t;
1131 dcr_read_cb dcr_read;
1132 dcr_write_cb dcr_write;
1136 /* XXX: on 460, DCR addresses are 32 bits wide,
1137 * using DCRIPR to get the 22 upper bits of the DCR address
1139 #define DCRN_NB 1024
1141 ppc_dcrn_t dcrn[DCRN_NB];
1142 int (*read_error)(int dcrn);
1143 int (*write_error)(int dcrn);
1146 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
1150 if (dcrn < 0 || dcrn >= DCRN_NB)
1152 dcr = &dcr_env->dcrn[dcrn];
1153 if (dcr->dcr_read == NULL)
1155 *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
1160 if (dcr_env->read_error != NULL)
1161 return (*dcr_env->read_error)(dcrn);
1166 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
1170 if (dcrn < 0 || dcrn >= DCRN_NB)
1172 dcr = &dcr_env->dcrn[dcrn];
1173 if (dcr->dcr_write == NULL)
1175 (*dcr->dcr_write)(dcr->opaque, dcrn, val);
1180 if (dcr_env->write_error != NULL)
1181 return (*dcr_env->write_error)(dcrn);
1186 int ppc_dcr_register (CPUPPCState *env, int dcrn, void *opaque,
1187 dcr_read_cb dcr_read, dcr_write_cb dcr_write)
1192 dcr_env = env->dcr_env;
1193 if (dcr_env == NULL)
1195 if (dcrn < 0 || dcrn >= DCRN_NB)
1197 dcr = &dcr_env->dcrn[dcrn];
1198 if (dcr->opaque != NULL ||
1199 dcr->dcr_read != NULL ||
1200 dcr->dcr_write != NULL)
1202 dcr->opaque = opaque;
1203 dcr->dcr_read = dcr_read;
1204 dcr->dcr_write = dcr_write;
1209 int ppc_dcr_init (CPUPPCState *env, int (*read_error)(int dcrn),
1210 int (*write_error)(int dcrn))
1214 dcr_env = g_malloc0(sizeof(ppc_dcr_t));
1215 dcr_env->read_error = read_error;
1216 dcr_env->write_error = write_error;
1217 env->dcr_env = dcr_env;
1222 /*****************************************************************************/
1224 void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
1236 printf("Set loglevel to %04" PRIx32 "\n", val);
1237 qemu_set_log(val | 0x100);
1242 /*****************************************************************************/
1244 static inline uint32_t nvram_read (nvram_t *nvram, uint32_t addr)
1246 return (*nvram->read_fn)(nvram->opaque, addr);
1249 static inline void nvram_write (nvram_t *nvram, uint32_t addr, uint32_t val)
1251 (*nvram->write_fn)(nvram->opaque, addr, val);
1254 static void NVRAM_set_byte(nvram_t *nvram, uint32_t addr, uint8_t value)
1256 nvram_write(nvram, addr, value);
1259 static uint8_t NVRAM_get_byte(nvram_t *nvram, uint32_t addr)
1261 return nvram_read(nvram, addr);
1264 static void NVRAM_set_word(nvram_t *nvram, uint32_t addr, uint16_t value)
1266 nvram_write(nvram, addr, value >> 8);
1267 nvram_write(nvram, addr + 1, value & 0xFF);
1270 static uint16_t NVRAM_get_word(nvram_t *nvram, uint32_t addr)
1274 tmp = nvram_read(nvram, addr) << 8;
1275 tmp |= nvram_read(nvram, addr + 1);
1280 static void NVRAM_set_lword(nvram_t *nvram, uint32_t addr, uint32_t value)
1282 nvram_write(nvram, addr, value >> 24);
1283 nvram_write(nvram, addr + 1, (value >> 16) & 0xFF);
1284 nvram_write(nvram, addr + 2, (value >> 8) & 0xFF);
1285 nvram_write(nvram, addr + 3, value & 0xFF);
1288 uint32_t NVRAM_get_lword (nvram_t *nvram, uint32_t addr)
1292 tmp = nvram_read(nvram, addr) << 24;
1293 tmp |= nvram_read(nvram, addr + 1) << 16;
1294 tmp |= nvram_read(nvram, addr + 2) << 8;
1295 tmp |= nvram_read(nvram, addr + 3);
1300 static void NVRAM_set_string(nvram_t *nvram, uint32_t addr, const char *str,
1305 for (i = 0; i < max && str[i] != '\0'; i++) {
1306 nvram_write(nvram, addr + i, str[i]);
1308 nvram_write(nvram, addr + i, str[i]);
1309 nvram_write(nvram, addr + max - 1, '\0');
1312 int NVRAM_get_string (nvram_t *nvram, uint8_t *dst, uint16_t addr, int max)
1316 memset(dst, 0, max);
1317 for (i = 0; i < max; i++) {
1318 dst[i] = NVRAM_get_byte(nvram, addr + i);
1326 static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
1329 uint16_t pd, pd1, pd2;
1334 pd2 = ((pd >> 4) & 0x000F) ^ pd1;
1335 tmp ^= (pd1 << 3) | (pd1 << 8);
1336 tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
1341 static uint16_t NVRAM_compute_crc (nvram_t *nvram, uint32_t start, uint32_t count)
1344 uint16_t crc = 0xFFFF;
1349 for (i = 0; i != count; i++) {
1350 crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
1353 crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
1359 #define CMDLINE_ADDR 0x017ff000
1361 int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size,
1363 uint32_t RAM_size, int boot_device,
1364 uint32_t kernel_image, uint32_t kernel_size,
1365 const char *cmdline,
1366 uint32_t initrd_image, uint32_t initrd_size,
1367 uint32_t NVRAM_image,
1368 int width, int height, int depth)
1372 /* Set parameters for Open Hack'Ware BIOS */
1373 NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
1374 NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
1375 NVRAM_set_word(nvram, 0x14, NVRAM_size);
1376 NVRAM_set_string(nvram, 0x20, arch, 16);
1377 NVRAM_set_lword(nvram, 0x30, RAM_size);
1378 NVRAM_set_byte(nvram, 0x34, boot_device);
1379 NVRAM_set_lword(nvram, 0x38, kernel_image);
1380 NVRAM_set_lword(nvram, 0x3C, kernel_size);
1382 /* XXX: put the cmdline in NVRAM too ? */
1383 pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR, cmdline);
1384 NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
1385 NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
1387 NVRAM_set_lword(nvram, 0x40, 0);
1388 NVRAM_set_lword(nvram, 0x44, 0);
1390 NVRAM_set_lword(nvram, 0x48, initrd_image);
1391 NVRAM_set_lword(nvram, 0x4C, initrd_size);
1392 NVRAM_set_lword(nvram, 0x50, NVRAM_image);
1394 NVRAM_set_word(nvram, 0x54, width);
1395 NVRAM_set_word(nvram, 0x56, height);
1396 NVRAM_set_word(nvram, 0x58, depth);
1397 crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
1398 NVRAM_set_word(nvram, 0xFC, crc);
1403 /* CPU device-tree ID helpers */
1404 int ppc_get_vcpu_dt_id(PowerPCCPU *cpu)
1406 return cpu->cpu_dt_id;
1409 PowerPCCPU *ppc_get_vcpu_by_dt_id(int cpu_dt_id)
1414 PowerPCCPU *cpu = POWERPC_CPU(cs);
1416 if (cpu->cpu_dt_id == cpu_dt_id) {