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"
33 #include "hw/timer/m48t59.h"
35 #include "qemu/error-report.h"
36 #include "qapi/error.h"
37 #include "hw/loader.h"
38 #include "sysemu/kvm.h"
42 //#define PPC_DEBUG_IRQ
43 //#define PPC_DEBUG_TB
46 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
48 # define LOG_IRQ(...) do { } while (0)
53 # define LOG_TB(...) qemu_log(__VA_ARGS__)
55 # define LOG_TB(...) do { } while (0)
58 static void cpu_ppc_tb_stop (CPUPPCState
*env
);
59 static void cpu_ppc_tb_start (CPUPPCState
*env
);
61 void ppc_set_irq(PowerPCCPU
*cpu
, int n_IRQ
, int level
)
63 CPUState
*cs
= CPU(cpu
);
64 CPUPPCState
*env
= &cpu
->env
;
65 unsigned int old_pending
;
68 /* We may already have the BQL if coming from the reset path */
69 if (!qemu_mutex_iothread_locked()) {
71 qemu_mutex_lock_iothread();
74 old_pending
= env
->pending_interrupts
;
77 env
->pending_interrupts
|= 1 << n_IRQ
;
78 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
80 env
->pending_interrupts
&= ~(1 << n_IRQ
);
81 if (env
->pending_interrupts
== 0) {
82 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
86 if (old_pending
!= env
->pending_interrupts
) {
88 kvmppc_set_interrupt(cpu
, n_IRQ
, level
);
93 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
94 "req %08x\n", __func__
, env
, n_IRQ
, level
,
95 env
->pending_interrupts
, CPU(cpu
)->interrupt_request
);
98 qemu_mutex_unlock_iothread();
102 /* PowerPC 6xx / 7xx internal IRQ controller */
103 static void ppc6xx_set_irq(void *opaque
, int pin
, int level
)
105 PowerPCCPU
*cpu
= opaque
;
106 CPUPPCState
*env
= &cpu
->env
;
109 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
111 cur_level
= (env
->irq_input_state
>> pin
) & 1;
112 /* Don't generate spurious events */
113 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
114 CPUState
*cs
= CPU(cpu
);
117 case PPC6xx_INPUT_TBEN
:
118 /* Level sensitive - active high */
119 LOG_IRQ("%s: %s the time base\n",
120 __func__
, level
? "start" : "stop");
122 cpu_ppc_tb_start(env
);
124 cpu_ppc_tb_stop(env
);
126 case PPC6xx_INPUT_INT
:
127 /* Level sensitive - active high */
128 LOG_IRQ("%s: set the external IRQ state to %d\n",
130 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
132 case PPC6xx_INPUT_SMI
:
133 /* Level sensitive - active high */
134 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
136 ppc_set_irq(cpu
, PPC_INTERRUPT_SMI
, level
);
138 case PPC6xx_INPUT_MCP
:
139 /* Negative edge sensitive */
140 /* XXX: TODO: actual reaction may depends on HID0 status
141 * 603/604/740/750: check HID0[EMCP]
143 if (cur_level
== 1 && level
== 0) {
144 LOG_IRQ("%s: raise machine check state\n",
146 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
149 case PPC6xx_INPUT_CKSTP_IN
:
150 /* Level sensitive - active low */
151 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
152 /* XXX: Note that the only way to restart the CPU is to reset it */
154 LOG_IRQ("%s: stop the CPU\n", __func__
);
158 case PPC6xx_INPUT_HRESET
:
159 /* Level sensitive - active low */
161 LOG_IRQ("%s: reset the CPU\n", __func__
);
162 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
165 case PPC6xx_INPUT_SRESET
:
166 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
168 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
171 /* Unknown pin - do nothing */
172 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
176 env
->irq_input_state
|= 1 << pin
;
178 env
->irq_input_state
&= ~(1 << pin
);
182 void ppc6xx_irq_init(PowerPCCPU
*cpu
)
184 CPUPPCState
*env
= &cpu
->env
;
186 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, cpu
,
190 #if defined(TARGET_PPC64)
191 /* PowerPC 970 internal IRQ controller */
192 static void ppc970_set_irq(void *opaque
, int pin
, int level
)
194 PowerPCCPU
*cpu
= opaque
;
195 CPUPPCState
*env
= &cpu
->env
;
198 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
200 cur_level
= (env
->irq_input_state
>> pin
) & 1;
201 /* Don't generate spurious events */
202 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
203 CPUState
*cs
= CPU(cpu
);
206 case PPC970_INPUT_INT
:
207 /* Level sensitive - active high */
208 LOG_IRQ("%s: set the external IRQ state to %d\n",
210 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
212 case PPC970_INPUT_THINT
:
213 /* Level sensitive - active high */
214 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__
,
216 ppc_set_irq(cpu
, PPC_INTERRUPT_THERM
, level
);
218 case PPC970_INPUT_MCP
:
219 /* Negative edge sensitive */
220 /* XXX: TODO: actual reaction may depends on HID0 status
221 * 603/604/740/750: check HID0[EMCP]
223 if (cur_level
== 1 && level
== 0) {
224 LOG_IRQ("%s: raise machine check state\n",
226 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
229 case PPC970_INPUT_CKSTP
:
230 /* Level sensitive - active low */
231 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
233 LOG_IRQ("%s: stop the CPU\n", __func__
);
236 LOG_IRQ("%s: restart the CPU\n", __func__
);
241 case PPC970_INPUT_HRESET
:
242 /* Level sensitive - active low */
244 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
247 case PPC970_INPUT_SRESET
:
248 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
250 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
252 case PPC970_INPUT_TBEN
:
253 LOG_IRQ("%s: set the TBEN state to %d\n", __func__
,
258 /* Unknown pin - do nothing */
259 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
263 env
->irq_input_state
|= 1 << pin
;
265 env
->irq_input_state
&= ~(1 << pin
);
269 void ppc970_irq_init(PowerPCCPU
*cpu
)
271 CPUPPCState
*env
= &cpu
->env
;
273 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, cpu
,
277 /* POWER7 internal IRQ controller */
278 static void power7_set_irq(void *opaque
, int pin
, int level
)
280 PowerPCCPU
*cpu
= opaque
;
281 CPUPPCState
*env
= &cpu
->env
;
283 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
287 case POWER7_INPUT_INT
:
288 /* Level sensitive - active high */
289 LOG_IRQ("%s: set the external IRQ state to %d\n",
291 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
294 /* Unknown pin - do nothing */
295 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
299 env
->irq_input_state
|= 1 << pin
;
301 env
->irq_input_state
&= ~(1 << pin
);
305 void ppcPOWER7_irq_init(PowerPCCPU
*cpu
)
307 CPUPPCState
*env
= &cpu
->env
;
309 env
->irq_inputs
= (void **)qemu_allocate_irqs(&power7_set_irq
, cpu
,
312 #endif /* defined(TARGET_PPC64) */
314 /* PowerPC 40x internal IRQ controller */
315 static void ppc40x_set_irq(void *opaque
, int pin
, int level
)
317 PowerPCCPU
*cpu
= opaque
;
318 CPUPPCState
*env
= &cpu
->env
;
321 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
323 cur_level
= (env
->irq_input_state
>> pin
) & 1;
324 /* Don't generate spurious events */
325 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
326 CPUState
*cs
= CPU(cpu
);
329 case PPC40x_INPUT_RESET_SYS
:
331 LOG_IRQ("%s: reset the PowerPC system\n",
333 ppc40x_system_reset(cpu
);
336 case PPC40x_INPUT_RESET_CHIP
:
338 LOG_IRQ("%s: reset the PowerPC chip\n", __func__
);
339 ppc40x_chip_reset(cpu
);
342 case PPC40x_INPUT_RESET_CORE
:
343 /* XXX: TODO: update DBSR[MRR] */
345 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
346 ppc40x_core_reset(cpu
);
349 case PPC40x_INPUT_CINT
:
350 /* Level sensitive - active high */
351 LOG_IRQ("%s: set the critical IRQ state to %d\n",
353 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
355 case PPC40x_INPUT_INT
:
356 /* Level sensitive - active high */
357 LOG_IRQ("%s: set the external IRQ state to %d\n",
359 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
361 case PPC40x_INPUT_HALT
:
362 /* Level sensitive - active low */
364 LOG_IRQ("%s: stop the CPU\n", __func__
);
367 LOG_IRQ("%s: restart the CPU\n", __func__
);
372 case PPC40x_INPUT_DEBUG
:
373 /* Level sensitive - active high */
374 LOG_IRQ("%s: set the debug pin state to %d\n",
376 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
379 /* Unknown pin - do nothing */
380 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
384 env
->irq_input_state
|= 1 << pin
;
386 env
->irq_input_state
&= ~(1 << pin
);
390 void ppc40x_irq_init(PowerPCCPU
*cpu
)
392 CPUPPCState
*env
= &cpu
->env
;
394 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc40x_set_irq
,
395 cpu
, PPC40x_INPUT_NB
);
398 /* PowerPC E500 internal IRQ controller */
399 static void ppce500_set_irq(void *opaque
, int pin
, int level
)
401 PowerPCCPU
*cpu
= opaque
;
402 CPUPPCState
*env
= &cpu
->env
;
405 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
407 cur_level
= (env
->irq_input_state
>> pin
) & 1;
408 /* Don't generate spurious events */
409 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
411 case PPCE500_INPUT_MCK
:
413 LOG_IRQ("%s: reset the PowerPC system\n",
415 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
418 case PPCE500_INPUT_RESET_CORE
:
420 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
421 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, level
);
424 case PPCE500_INPUT_CINT
:
425 /* Level sensitive - active high */
426 LOG_IRQ("%s: set the critical IRQ state to %d\n",
428 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
430 case PPCE500_INPUT_INT
:
431 /* Level sensitive - active high */
432 LOG_IRQ("%s: set the core IRQ state to %d\n",
434 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
436 case PPCE500_INPUT_DEBUG
:
437 /* Level sensitive - active high */
438 LOG_IRQ("%s: set the debug pin state to %d\n",
440 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
443 /* Unknown pin - do nothing */
444 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
448 env
->irq_input_state
|= 1 << pin
;
450 env
->irq_input_state
&= ~(1 << pin
);
454 void ppce500_irq_init(PowerPCCPU
*cpu
)
456 CPUPPCState
*env
= &cpu
->env
;
458 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppce500_set_irq
,
459 cpu
, PPCE500_INPUT_NB
);
462 /* Enable or Disable the E500 EPR capability */
463 void ppce500_set_mpic_proxy(bool enabled
)
468 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
470 cpu
->env
.mpic_proxy
= enabled
;
472 kvmppc_set_mpic_proxy(cpu
, enabled
);
477 /*****************************************************************************/
478 /* PowerPC time base and decrementer emulation */
480 uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
, int64_t tb_offset
)
482 /* TB time in tb periods */
483 return muldiv64(vmclk
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
) + tb_offset
;
486 uint64_t cpu_ppc_load_tbl (CPUPPCState
*env
)
488 ppc_tb_t
*tb_env
= env
->tb_env
;
492 return env
->spr
[SPR_TBL
];
495 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
496 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
501 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState
*env
)
503 ppc_tb_t
*tb_env
= env
->tb_env
;
506 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
507 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
512 uint32_t cpu_ppc_load_tbu (CPUPPCState
*env
)
515 return env
->spr
[SPR_TBU
];
518 return _cpu_ppc_load_tbu(env
);
521 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
522 int64_t *tb_offsetp
, uint64_t value
)
524 *tb_offsetp
= value
-
525 muldiv64(vmclk
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
);
527 LOG_TB("%s: tb %016" PRIx64
" offset %08" PRIx64
"\n",
528 __func__
, value
, *tb_offsetp
);
531 void cpu_ppc_store_tbl (CPUPPCState
*env
, uint32_t value
)
533 ppc_tb_t
*tb_env
= env
->tb_env
;
536 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
537 tb
&= 0xFFFFFFFF00000000ULL
;
538 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
539 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
542 static inline void _cpu_ppc_store_tbu(CPUPPCState
*env
, uint32_t value
)
544 ppc_tb_t
*tb_env
= env
->tb_env
;
547 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
548 tb
&= 0x00000000FFFFFFFFULL
;
549 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
550 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
553 void cpu_ppc_store_tbu (CPUPPCState
*env
, uint32_t value
)
555 _cpu_ppc_store_tbu(env
, value
);
558 uint64_t cpu_ppc_load_atbl (CPUPPCState
*env
)
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 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
569 uint32_t cpu_ppc_load_atbu (CPUPPCState
*env
)
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 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
580 void cpu_ppc_store_atbl (CPUPPCState
*env
, uint32_t value
)
582 ppc_tb_t
*tb_env
= env
->tb_env
;
585 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
586 tb
&= 0xFFFFFFFF00000000ULL
;
587 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
588 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
591 void cpu_ppc_store_atbu (CPUPPCState
*env
, uint32_t value
)
593 ppc_tb_t
*tb_env
= env
->tb_env
;
596 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
597 tb
&= 0x00000000FFFFFFFFULL
;
598 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
599 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
602 static void cpu_ppc_tb_stop (CPUPPCState
*env
)
604 ppc_tb_t
*tb_env
= env
->tb_env
;
605 uint64_t tb
, atb
, vmclk
;
607 /* If the time base is already frozen, do nothing */
608 if (tb_env
->tb_freq
!= 0) {
609 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
610 /* Get the time base */
611 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
612 /* Get the alternate time base */
613 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
614 /* Store the time base value (ie compute the current offset) */
615 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
616 /* Store the alternate time base value (compute the current offset) */
617 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
618 /* Set the time base frequency to zero */
620 /* Now, the time bases are frozen to tb_offset / atb_offset value */
624 static void cpu_ppc_tb_start (CPUPPCState
*env
)
626 ppc_tb_t
*tb_env
= env
->tb_env
;
627 uint64_t tb
, atb
, vmclk
;
629 /* If the time base is not frozen, do nothing */
630 if (tb_env
->tb_freq
== 0) {
631 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
632 /* Get the time base from tb_offset */
633 tb
= tb_env
->tb_offset
;
634 /* Get the alternate time base from atb_offset */
635 atb
= tb_env
->atb_offset
;
636 /* Restore the tb frequency from the decrementer frequency */
637 tb_env
->tb_freq
= tb_env
->decr_freq
;
638 /* Store the time base value */
639 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
640 /* Store the alternate time base value */
641 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
645 bool ppc_decr_clear_on_delivery(CPUPPCState
*env
)
647 ppc_tb_t
*tb_env
= env
->tb_env
;
648 int flags
= PPC_DECR_UNDERFLOW_TRIGGERED
| PPC_DECR_UNDERFLOW_LEVEL
;
649 return ((tb_env
->flags
& flags
) == PPC_DECR_UNDERFLOW_TRIGGERED
);
652 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState
*env
, uint64_t next
)
654 ppc_tb_t
*tb_env
= env
->tb_env
;
658 diff
= next
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
660 decr
= muldiv64(diff
, tb_env
->decr_freq
, NANOSECONDS_PER_SECOND
);
661 } else if (tb_env
->flags
& PPC_TIMER_BOOKE
) {
664 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, NANOSECONDS_PER_SECOND
);
666 LOG_TB("%s: %08" PRIx32
"\n", __func__
, decr
);
671 uint32_t cpu_ppc_load_decr (CPUPPCState
*env
)
673 ppc_tb_t
*tb_env
= env
->tb_env
;
676 return env
->spr
[SPR_DECR
];
679 return _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
682 uint32_t cpu_ppc_load_hdecr (CPUPPCState
*env
)
684 ppc_tb_t
*tb_env
= env
->tb_env
;
686 return _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
689 uint64_t cpu_ppc_load_purr (CPUPPCState
*env
)
691 ppc_tb_t
*tb_env
= env
->tb_env
;
694 diff
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - tb_env
->purr_start
;
696 return tb_env
->purr_load
+
697 muldiv64(diff
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
);
700 /* When decrementer expires,
701 * all we need to do is generate or queue a CPU exception
703 static inline void cpu_ppc_decr_excp(PowerPCCPU
*cpu
)
706 LOG_TB("raise decrementer exception\n");
707 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 1);
710 static inline void cpu_ppc_decr_lower(PowerPCCPU
*cpu
)
712 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 0);
715 static inline void cpu_ppc_hdecr_excp(PowerPCCPU
*cpu
)
717 CPUPPCState
*env
= &cpu
->env
;
720 LOG_TB("raise hv decrementer exception\n");
722 /* The architecture specifies that we don't deliver HDEC
723 * interrupts in a PM state. Not only they don't cause a
724 * wakeup but they also get effectively discarded.
726 if (!env
->in_pm_state
) {
727 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 1);
731 static inline void cpu_ppc_hdecr_lower(PowerPCCPU
*cpu
)
733 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 0);
736 static void __cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint64_t *nextp
,
738 void (*raise_excp
)(void *),
739 void (*lower_excp
)(PowerPCCPU
*),
740 uint32_t decr
, uint32_t value
)
742 CPUPPCState
*env
= &cpu
->env
;
743 ppc_tb_t
*tb_env
= env
->tb_env
;
746 LOG_TB("%s: %08" PRIx32
" => %08" PRIx32
"\n", __func__
,
750 /* KVM handles decrementer exceptions, we don't need our own timer */
755 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
758 * If we get a really small DEC value, we can assume that by the time we
759 * handled it we should inject an interrupt already.
761 * On MSB level based DEC implementations the MSB always means the interrupt
762 * is pending, so raise it on those.
764 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
765 * an edge interrupt, so raise it here too.
768 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
) && (value
& 0x80000000)) ||
769 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_TRIGGERED
) && (value
& 0x80000000)
770 && !(decr
& 0x80000000))) {
775 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
776 if (!(value
& 0x80000000) && (tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
)) {
780 /* Calculate the next timer event */
781 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
782 next
= now
+ muldiv64(value
, NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
786 timer_mod(timer
, next
);
789 static inline void _cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint32_t decr
,
792 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
794 __cpu_ppc_store_decr(cpu
, &tb_env
->decr_next
, tb_env
->decr_timer
,
795 tb_env
->decr_timer
->cb
, &cpu_ppc_decr_lower
, decr
,
799 void cpu_ppc_store_decr (CPUPPCState
*env
, uint32_t value
)
801 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
803 _cpu_ppc_store_decr(cpu
, cpu_ppc_load_decr(env
), value
);
806 static void cpu_ppc_decr_cb(void *opaque
)
808 PowerPCCPU
*cpu
= opaque
;
810 cpu_ppc_decr_excp(cpu
);
813 static inline void _cpu_ppc_store_hdecr(PowerPCCPU
*cpu
, uint32_t hdecr
,
816 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
818 if (tb_env
->hdecr_timer
!= NULL
) {
819 __cpu_ppc_store_decr(cpu
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
820 tb_env
->hdecr_timer
->cb
, &cpu_ppc_hdecr_lower
,
825 void cpu_ppc_store_hdecr (CPUPPCState
*env
, uint32_t value
)
827 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
829 _cpu_ppc_store_hdecr(cpu
, cpu_ppc_load_hdecr(env
), value
);
832 static void cpu_ppc_hdecr_cb(void *opaque
)
834 PowerPCCPU
*cpu
= opaque
;
836 cpu_ppc_hdecr_excp(cpu
);
839 static void cpu_ppc_store_purr(PowerPCCPU
*cpu
, uint64_t value
)
841 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
843 tb_env
->purr_load
= value
;
844 tb_env
->purr_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
847 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
849 CPUPPCState
*env
= opaque
;
850 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
851 ppc_tb_t
*tb_env
= env
->tb_env
;
853 tb_env
->tb_freq
= freq
;
854 tb_env
->decr_freq
= freq
;
855 /* There is a bug in Linux 2.4 kernels:
856 * if a decrementer exception is pending when it enables msr_ee at startup,
857 * it's not ready to handle it...
859 _cpu_ppc_store_decr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF);
860 _cpu_ppc_store_hdecr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF);
861 cpu_ppc_store_purr(cpu
, 0x0000000000000000ULL
);
864 static void timebase_save(PPCTimebase
*tb
)
866 uint64_t ticks
= cpu_get_host_ticks();
867 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
869 if (!first_ppc_cpu
->env
.tb_env
) {
870 error_report("No timebase object");
874 /* not used anymore, we keep it for compatibility */
875 tb
->time_of_the_day_ns
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
877 * tb_offset is only expected to be changed by QEMU so
878 * there is no need to update it from KVM here
880 tb
->guest_timebase
= ticks
+ first_ppc_cpu
->env
.tb_env
->tb_offset
;
883 static void timebase_load(PPCTimebase
*tb
)
886 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
887 int64_t tb_off_adj
, tb_off
;
890 if (!first_ppc_cpu
->env
.tb_env
) {
891 error_report("No timebase object");
895 freq
= first_ppc_cpu
->env
.tb_env
->tb_freq
;
897 tb_off_adj
= tb
->guest_timebase
- cpu_get_host_ticks();
899 tb_off
= first_ppc_cpu
->env
.tb_env
->tb_offset
;
900 trace_ppc_tb_adjust(tb_off
, tb_off_adj
, tb_off_adj
- tb_off
,
901 (tb_off_adj
- tb_off
) / freq
);
903 /* Set new offset to all CPUs */
905 PowerPCCPU
*pcpu
= POWERPC_CPU(cpu
);
906 pcpu
->env
.tb_env
->tb_offset
= tb_off_adj
;
907 #if defined(CONFIG_KVM)
908 kvm_set_one_reg(cpu
, KVM_REG_PPC_TB_OFFSET
,
909 &pcpu
->env
.tb_env
->tb_offset
);
914 void cpu_ppc_clock_vm_state_change(void *opaque
, int running
,
917 PPCTimebase
*tb
= opaque
;
927 * When migrating, read the clock just before migration,
928 * so that the guest clock counts during the events
935 * This reduces clock difference on migration from 5s
936 * to 0.1s (when max_downtime == 5s), because sending the
937 * final pages of memory (which happens between vm_stop()
938 * and pre_save()) takes max_downtime.
940 static int timebase_pre_save(void *opaque
)
942 PPCTimebase
*tb
= opaque
;
949 const VMStateDescription vmstate_ppc_timebase
= {
952 .minimum_version_id
= 1,
953 .minimum_version_id_old
= 1,
954 .pre_save
= timebase_pre_save
,
955 .fields
= (VMStateField
[]) {
956 VMSTATE_UINT64(guest_timebase
, PPCTimebase
),
957 VMSTATE_INT64(time_of_the_day_ns
, PPCTimebase
),
958 VMSTATE_END_OF_LIST()
962 /* Set up (once) timebase frequency (in Hz) */
963 clk_setup_cb
cpu_ppc_tb_init (CPUPPCState
*env
, uint32_t freq
)
965 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
968 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
969 env
->tb_env
= tb_env
;
970 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
971 if (env
->insns_flags
& PPC_SEGMENT_64B
) {
972 /* All Book3S 64bit CPUs implement level based DEC logic */
973 tb_env
->flags
|= PPC_DECR_UNDERFLOW_LEVEL
;
975 /* Create new timer */
976 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_decr_cb
, cpu
);
977 if (env
->has_hv_mode
) {
978 tb_env
->hdecr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_hdecr_cb
,
981 tb_env
->hdecr_timer
= NULL
;
983 cpu_ppc_set_tb_clk(env
, freq
);
985 return &cpu_ppc_set_tb_clk
;
988 /* Specific helpers for POWER & PowerPC 601 RTC */
989 void cpu_ppc601_store_rtcu (CPUPPCState
*env
, uint32_t value
)
991 _cpu_ppc_store_tbu(env
, value
);
994 uint32_t cpu_ppc601_load_rtcu (CPUPPCState
*env
)
996 return _cpu_ppc_load_tbu(env
);
999 void cpu_ppc601_store_rtcl (CPUPPCState
*env
, uint32_t value
)
1001 cpu_ppc_store_tbl(env
, value
& 0x3FFFFF80);
1004 uint32_t cpu_ppc601_load_rtcl (CPUPPCState
*env
)
1006 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
1009 /*****************************************************************************/
1010 /* PowerPC 40x timers */
1012 /* PIT, FIT & WDT */
1013 typedef struct ppc40x_timer_t ppc40x_timer_t
;
1014 struct ppc40x_timer_t
{
1015 uint64_t pit_reload
; /* PIT auto-reload value */
1016 uint64_t fit_next
; /* Tick for next FIT interrupt */
1017 QEMUTimer
*fit_timer
;
1018 uint64_t wdt_next
; /* Tick for next WDT interrupt */
1019 QEMUTimer
*wdt_timer
;
1021 /* 405 have the PIT, 440 have a DECR. */
1022 unsigned int decr_excp
;
1025 /* Fixed interval timer */
1026 static void cpu_4xx_fit_cb (void *opaque
)
1031 ppc40x_timer_t
*ppc40x_timer
;
1035 cpu
= ppc_env_get_cpu(env
);
1036 tb_env
= env
->tb_env
;
1037 ppc40x_timer
= tb_env
->opaque
;
1038 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1039 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
1053 /* Cannot occur, but makes gcc happy */
1056 next
= now
+ muldiv64(next
, NANOSECONDS_PER_SECOND
, tb_env
->tb_freq
);
1059 timer_mod(ppc40x_timer
->fit_timer
, next
);
1060 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
1061 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1) {
1062 ppc_set_irq(cpu
, PPC_INTERRUPT_FIT
, 1);
1064 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
1065 (int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
1066 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1069 /* Programmable interval timer */
1070 static void start_stop_pit (CPUPPCState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
1072 ppc40x_timer_t
*ppc40x_timer
;
1075 ppc40x_timer
= tb_env
->opaque
;
1076 if (ppc40x_timer
->pit_reload
<= 1 ||
1077 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
1078 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
1080 LOG_TB("%s: stop PIT\n", __func__
);
1081 timer_del(tb_env
->decr_timer
);
1083 LOG_TB("%s: start PIT %016" PRIx64
"\n",
1084 __func__
, ppc40x_timer
->pit_reload
);
1085 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1086 next
= now
+ muldiv64(ppc40x_timer
->pit_reload
,
1087 NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
1089 next
+= tb_env
->decr_next
- now
;
1092 timer_mod(tb_env
->decr_timer
, next
);
1093 tb_env
->decr_next
= next
;
1097 static void cpu_4xx_pit_cb (void *opaque
)
1102 ppc40x_timer_t
*ppc40x_timer
;
1105 cpu
= ppc_env_get_cpu(env
);
1106 tb_env
= env
->tb_env
;
1107 ppc40x_timer
= tb_env
->opaque
;
1108 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
1109 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) {
1110 ppc_set_irq(cpu
, ppc40x_timer
->decr_excp
, 1);
1112 start_stop_pit(env
, tb_env
, 1);
1113 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
" "
1114 "%016" PRIx64
"\n", __func__
,
1115 (int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
1116 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
1117 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
1118 ppc40x_timer
->pit_reload
);
1121 /* Watchdog timer */
1122 static void cpu_4xx_wdt_cb (void *opaque
)
1127 ppc40x_timer_t
*ppc40x_timer
;
1131 cpu
= ppc_env_get_cpu(env
);
1132 tb_env
= env
->tb_env
;
1133 ppc40x_timer
= tb_env
->opaque
;
1134 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1135 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
1149 /* Cannot occur, but makes gcc happy */
1152 next
= now
+ muldiv64(next
, NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
1155 LOG_TB("%s: TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
1156 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1157 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
1160 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1161 ppc40x_timer
->wdt_next
= next
;
1162 env
->spr
[SPR_40x_TSR
] |= 1U << 31;
1165 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1166 ppc40x_timer
->wdt_next
= next
;
1167 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
1168 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1) {
1169 ppc_set_irq(cpu
, PPC_INTERRUPT_WDT
, 1);
1173 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
1174 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
1175 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
1179 case 0x1: /* Core reset */
1180 ppc40x_core_reset(cpu
);
1182 case 0x2: /* Chip reset */
1183 ppc40x_chip_reset(cpu
);
1185 case 0x3: /* System reset */
1186 ppc40x_system_reset(cpu
);
1192 void store_40x_pit (CPUPPCState
*env
, target_ulong val
)
1195 ppc40x_timer_t
*ppc40x_timer
;
1197 tb_env
= env
->tb_env
;
1198 ppc40x_timer
= tb_env
->opaque
;
1199 LOG_TB("%s val" TARGET_FMT_lx
"\n", __func__
, val
);
1200 ppc40x_timer
->pit_reload
= val
;
1201 start_stop_pit(env
, tb_env
, 0);
1204 target_ulong
load_40x_pit (CPUPPCState
*env
)
1206 return cpu_ppc_load_decr(env
);
1209 static void ppc_40x_set_tb_clk (void *opaque
, uint32_t freq
)
1211 CPUPPCState
*env
= opaque
;
1212 ppc_tb_t
*tb_env
= env
->tb_env
;
1214 LOG_TB("%s set new frequency to %" PRIu32
"\n", __func__
,
1216 tb_env
->tb_freq
= freq
;
1217 tb_env
->decr_freq
= freq
;
1218 /* XXX: we should also update all timers */
1221 clk_setup_cb
ppc_40x_timers_init (CPUPPCState
*env
, uint32_t freq
,
1222 unsigned int decr_excp
)
1225 ppc40x_timer_t
*ppc40x_timer
;
1227 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
1228 env
->tb_env
= tb_env
;
1229 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1230 ppc40x_timer
= g_malloc0(sizeof(ppc40x_timer_t
));
1231 tb_env
->tb_freq
= freq
;
1232 tb_env
->decr_freq
= freq
;
1233 tb_env
->opaque
= ppc40x_timer
;
1234 LOG_TB("%s freq %" PRIu32
"\n", __func__
, freq
);
1235 if (ppc40x_timer
!= NULL
) {
1236 /* We use decr timer for PIT */
1237 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_pit_cb
, env
);
1238 ppc40x_timer
->fit_timer
=
1239 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_fit_cb
, env
);
1240 ppc40x_timer
->wdt_timer
=
1241 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_wdt_cb
, env
);
1242 ppc40x_timer
->decr_excp
= decr_excp
;
1245 return &ppc_40x_set_tb_clk
;
1248 /*****************************************************************************/
1249 /* Embedded PowerPC Device Control Registers */
1250 typedef struct ppc_dcrn_t ppc_dcrn_t
;
1252 dcr_read_cb dcr_read
;
1253 dcr_write_cb dcr_write
;
1257 /* XXX: on 460, DCR addresses are 32 bits wide,
1258 * using DCRIPR to get the 22 upper bits of the DCR address
1260 #define DCRN_NB 1024
1262 ppc_dcrn_t dcrn
[DCRN_NB
];
1263 int (*read_error
)(int dcrn
);
1264 int (*write_error
)(int dcrn
);
1267 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1271 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1273 dcr
= &dcr_env
->dcrn
[dcrn
];
1274 if (dcr
->dcr_read
== NULL
)
1276 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1281 if (dcr_env
->read_error
!= NULL
)
1282 return (*dcr_env
->read_error
)(dcrn
);
1287 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1291 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1293 dcr
= &dcr_env
->dcrn
[dcrn
];
1294 if (dcr
->dcr_write
== NULL
)
1296 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1301 if (dcr_env
->write_error
!= NULL
)
1302 return (*dcr_env
->write_error
)(dcrn
);
1307 int ppc_dcr_register (CPUPPCState
*env
, int dcrn
, void *opaque
,
1308 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1313 dcr_env
= env
->dcr_env
;
1314 if (dcr_env
== NULL
)
1316 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1318 dcr
= &dcr_env
->dcrn
[dcrn
];
1319 if (dcr
->opaque
!= NULL
||
1320 dcr
->dcr_read
!= NULL
||
1321 dcr
->dcr_write
!= NULL
)
1323 dcr
->opaque
= opaque
;
1324 dcr
->dcr_read
= dcr_read
;
1325 dcr
->dcr_write
= dcr_write
;
1330 int ppc_dcr_init (CPUPPCState
*env
, int (*read_error
)(int dcrn
),
1331 int (*write_error
)(int dcrn
))
1335 dcr_env
= g_malloc0(sizeof(ppc_dcr_t
));
1336 dcr_env
->read_error
= read_error
;
1337 dcr_env
->write_error
= write_error
;
1338 env
->dcr_env
= dcr_env
;
1343 /*****************************************************************************/
1345 void PPC_debug_write (void *opaque
, uint32_t addr
, uint32_t val
)
1357 printf("Set loglevel to %04" PRIx32
"\n", val
);
1358 qemu_set_log(val
| 0x100);