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 "hw/loader.h"
37 #include "sysemu/kvm.h"
41 //#define PPC_DEBUG_IRQ
42 //#define PPC_DEBUG_TB
45 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
47 # define LOG_IRQ(...) do { } while (0)
52 # define LOG_TB(...) qemu_log(__VA_ARGS__)
54 # define LOG_TB(...) do { } while (0)
57 static void cpu_ppc_tb_stop (CPUPPCState
*env
);
58 static void cpu_ppc_tb_start (CPUPPCState
*env
);
60 void ppc_set_irq(PowerPCCPU
*cpu
, int n_IRQ
, int level
)
62 CPUState
*cs
= CPU(cpu
);
63 CPUPPCState
*env
= &cpu
->env
;
64 unsigned int old_pending
;
67 /* We may already have the BQL if coming from the reset path */
68 if (!qemu_mutex_iothread_locked()) {
70 qemu_mutex_lock_iothread();
73 old_pending
= env
->pending_interrupts
;
76 env
->pending_interrupts
|= 1 << n_IRQ
;
77 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
79 env
->pending_interrupts
&= ~(1 << n_IRQ
);
80 if (env
->pending_interrupts
== 0) {
81 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
85 if (old_pending
!= env
->pending_interrupts
) {
87 kvmppc_set_interrupt(cpu
, n_IRQ
, level
);
92 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
93 "req %08x\n", __func__
, env
, n_IRQ
, level
,
94 env
->pending_interrupts
, CPU(cpu
)->interrupt_request
);
97 qemu_mutex_unlock_iothread();
101 /* PowerPC 6xx / 7xx internal IRQ controller */
102 static void ppc6xx_set_irq(void *opaque
, int pin
, int level
)
104 PowerPCCPU
*cpu
= opaque
;
105 CPUPPCState
*env
= &cpu
->env
;
108 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
110 cur_level
= (env
->irq_input_state
>> pin
) & 1;
111 /* Don't generate spurious events */
112 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
113 CPUState
*cs
= CPU(cpu
);
116 case PPC6xx_INPUT_TBEN
:
117 /* Level sensitive - active high */
118 LOG_IRQ("%s: %s the time base\n",
119 __func__
, level
? "start" : "stop");
121 cpu_ppc_tb_start(env
);
123 cpu_ppc_tb_stop(env
);
125 case PPC6xx_INPUT_INT
:
126 /* Level sensitive - active high */
127 LOG_IRQ("%s: set the external IRQ state to %d\n",
129 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
131 case PPC6xx_INPUT_SMI
:
132 /* Level sensitive - active high */
133 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
135 ppc_set_irq(cpu
, PPC_INTERRUPT_SMI
, level
);
137 case PPC6xx_INPUT_MCP
:
138 /* Negative edge sensitive */
139 /* XXX: TODO: actual reaction may depends on HID0 status
140 * 603/604/740/750: check HID0[EMCP]
142 if (cur_level
== 1 && level
== 0) {
143 LOG_IRQ("%s: raise machine check state\n",
145 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
148 case PPC6xx_INPUT_CKSTP_IN
:
149 /* Level sensitive - active low */
150 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
151 /* XXX: Note that the only way to restart the CPU is to reset it */
153 LOG_IRQ("%s: stop the CPU\n", __func__
);
157 case PPC6xx_INPUT_HRESET
:
158 /* Level sensitive - active low */
160 LOG_IRQ("%s: reset the CPU\n", __func__
);
161 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
164 case PPC6xx_INPUT_SRESET
:
165 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
167 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
170 /* Unknown pin - do nothing */
171 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
175 env
->irq_input_state
|= 1 << pin
;
177 env
->irq_input_state
&= ~(1 << pin
);
181 void ppc6xx_irq_init(PowerPCCPU
*cpu
)
183 CPUPPCState
*env
= &cpu
->env
;
185 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, cpu
,
189 #if defined(TARGET_PPC64)
190 /* PowerPC 970 internal IRQ controller */
191 static void ppc970_set_irq(void *opaque
, int pin
, int level
)
193 PowerPCCPU
*cpu
= opaque
;
194 CPUPPCState
*env
= &cpu
->env
;
197 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
199 cur_level
= (env
->irq_input_state
>> pin
) & 1;
200 /* Don't generate spurious events */
201 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
202 CPUState
*cs
= CPU(cpu
);
205 case PPC970_INPUT_INT
:
206 /* Level sensitive - active high */
207 LOG_IRQ("%s: set the external IRQ state to %d\n",
209 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
211 case PPC970_INPUT_THINT
:
212 /* Level sensitive - active high */
213 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__
,
215 ppc_set_irq(cpu
, PPC_INTERRUPT_THERM
, level
);
217 case PPC970_INPUT_MCP
:
218 /* Negative edge sensitive */
219 /* XXX: TODO: actual reaction may depends on HID0 status
220 * 603/604/740/750: check HID0[EMCP]
222 if (cur_level
== 1 && level
== 0) {
223 LOG_IRQ("%s: raise machine check state\n",
225 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
228 case PPC970_INPUT_CKSTP
:
229 /* Level sensitive - active low */
230 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
232 LOG_IRQ("%s: stop the CPU\n", __func__
);
235 LOG_IRQ("%s: restart the CPU\n", __func__
);
240 case PPC970_INPUT_HRESET
:
241 /* Level sensitive - active low */
243 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
246 case PPC970_INPUT_SRESET
:
247 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
249 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
251 case PPC970_INPUT_TBEN
:
252 LOG_IRQ("%s: set the TBEN state to %d\n", __func__
,
257 /* Unknown pin - do nothing */
258 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
262 env
->irq_input_state
|= 1 << pin
;
264 env
->irq_input_state
&= ~(1 << pin
);
268 void ppc970_irq_init(PowerPCCPU
*cpu
)
270 CPUPPCState
*env
= &cpu
->env
;
272 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, cpu
,
276 /* POWER7 internal IRQ controller */
277 static void power7_set_irq(void *opaque
, int pin
, int level
)
279 PowerPCCPU
*cpu
= opaque
;
280 CPUPPCState
*env
= &cpu
->env
;
282 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
286 case POWER7_INPUT_INT
:
287 /* Level sensitive - active high */
288 LOG_IRQ("%s: set the external IRQ state to %d\n",
290 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
293 /* Unknown pin - do nothing */
294 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
298 env
->irq_input_state
|= 1 << pin
;
300 env
->irq_input_state
&= ~(1 << pin
);
304 void ppcPOWER7_irq_init(PowerPCCPU
*cpu
)
306 CPUPPCState
*env
= &cpu
->env
;
308 env
->irq_inputs
= (void **)qemu_allocate_irqs(&power7_set_irq
, cpu
,
311 #endif /* defined(TARGET_PPC64) */
313 /* PowerPC 40x internal IRQ controller */
314 static void ppc40x_set_irq(void *opaque
, int pin
, int level
)
316 PowerPCCPU
*cpu
= opaque
;
317 CPUPPCState
*env
= &cpu
->env
;
320 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
322 cur_level
= (env
->irq_input_state
>> pin
) & 1;
323 /* Don't generate spurious events */
324 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
325 CPUState
*cs
= CPU(cpu
);
328 case PPC40x_INPUT_RESET_SYS
:
330 LOG_IRQ("%s: reset the PowerPC system\n",
332 ppc40x_system_reset(cpu
);
335 case PPC40x_INPUT_RESET_CHIP
:
337 LOG_IRQ("%s: reset the PowerPC chip\n", __func__
);
338 ppc40x_chip_reset(cpu
);
341 case PPC40x_INPUT_RESET_CORE
:
342 /* XXX: TODO: update DBSR[MRR] */
344 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
345 ppc40x_core_reset(cpu
);
348 case PPC40x_INPUT_CINT
:
349 /* Level sensitive - active high */
350 LOG_IRQ("%s: set the critical IRQ state to %d\n",
352 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
354 case PPC40x_INPUT_INT
:
355 /* Level sensitive - active high */
356 LOG_IRQ("%s: set the external IRQ state to %d\n",
358 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
360 case PPC40x_INPUT_HALT
:
361 /* Level sensitive - active low */
363 LOG_IRQ("%s: stop the CPU\n", __func__
);
366 LOG_IRQ("%s: restart the CPU\n", __func__
);
371 case PPC40x_INPUT_DEBUG
:
372 /* Level sensitive - active high */
373 LOG_IRQ("%s: set the debug pin state to %d\n",
375 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
378 /* Unknown pin - do nothing */
379 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
383 env
->irq_input_state
|= 1 << pin
;
385 env
->irq_input_state
&= ~(1 << pin
);
389 void ppc40x_irq_init(PowerPCCPU
*cpu
)
391 CPUPPCState
*env
= &cpu
->env
;
393 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc40x_set_irq
,
394 cpu
, PPC40x_INPUT_NB
);
397 /* PowerPC E500 internal IRQ controller */
398 static void ppce500_set_irq(void *opaque
, int pin
, int level
)
400 PowerPCCPU
*cpu
= opaque
;
401 CPUPPCState
*env
= &cpu
->env
;
404 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
406 cur_level
= (env
->irq_input_state
>> pin
) & 1;
407 /* Don't generate spurious events */
408 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
410 case PPCE500_INPUT_MCK
:
412 LOG_IRQ("%s: reset the PowerPC system\n",
414 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
417 case PPCE500_INPUT_RESET_CORE
:
419 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
420 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, level
);
423 case PPCE500_INPUT_CINT
:
424 /* Level sensitive - active high */
425 LOG_IRQ("%s: set the critical IRQ state to %d\n",
427 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
429 case PPCE500_INPUT_INT
:
430 /* Level sensitive - active high */
431 LOG_IRQ("%s: set the core IRQ state to %d\n",
433 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
435 case PPCE500_INPUT_DEBUG
:
436 /* Level sensitive - active high */
437 LOG_IRQ("%s: set the debug pin state to %d\n",
439 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
442 /* Unknown pin - do nothing */
443 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
447 env
->irq_input_state
|= 1 << pin
;
449 env
->irq_input_state
&= ~(1 << pin
);
453 void ppce500_irq_init(PowerPCCPU
*cpu
)
455 CPUPPCState
*env
= &cpu
->env
;
457 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppce500_set_irq
,
458 cpu
, PPCE500_INPUT_NB
);
461 /* Enable or Disable the E500 EPR capability */
462 void ppce500_set_mpic_proxy(bool enabled
)
467 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
469 cpu
->env
.mpic_proxy
= enabled
;
471 kvmppc_set_mpic_proxy(cpu
, enabled
);
476 /*****************************************************************************/
477 /* PowerPC time base and decrementer emulation */
479 uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
, int64_t tb_offset
)
481 /* TB time in tb periods */
482 return muldiv64(vmclk
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
) + tb_offset
;
485 uint64_t cpu_ppc_load_tbl (CPUPPCState
*env
)
487 ppc_tb_t
*tb_env
= env
->tb_env
;
491 return env
->spr
[SPR_TBL
];
494 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
495 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
500 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState
*env
)
502 ppc_tb_t
*tb_env
= env
->tb_env
;
505 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
506 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
511 uint32_t cpu_ppc_load_tbu (CPUPPCState
*env
)
514 return env
->spr
[SPR_TBU
];
517 return _cpu_ppc_load_tbu(env
);
520 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
521 int64_t *tb_offsetp
, uint64_t value
)
523 *tb_offsetp
= value
-
524 muldiv64(vmclk
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
);
526 LOG_TB("%s: tb %016" PRIx64
" offset %08" PRIx64
"\n",
527 __func__
, value
, *tb_offsetp
);
530 void cpu_ppc_store_tbl (CPUPPCState
*env
, uint32_t value
)
532 ppc_tb_t
*tb_env
= env
->tb_env
;
535 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
536 tb
&= 0xFFFFFFFF00000000ULL
;
537 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
538 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
541 static inline void _cpu_ppc_store_tbu(CPUPPCState
*env
, uint32_t value
)
543 ppc_tb_t
*tb_env
= env
->tb_env
;
546 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
547 tb
&= 0x00000000FFFFFFFFULL
;
548 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
549 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
552 void cpu_ppc_store_tbu (CPUPPCState
*env
, uint32_t value
)
554 _cpu_ppc_store_tbu(env
, value
);
557 uint64_t cpu_ppc_load_atbl (CPUPPCState
*env
)
559 ppc_tb_t
*tb_env
= env
->tb_env
;
562 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
563 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
568 uint32_t cpu_ppc_load_atbu (CPUPPCState
*env
)
570 ppc_tb_t
*tb_env
= env
->tb_env
;
573 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
574 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
579 void cpu_ppc_store_atbl (CPUPPCState
*env
, uint32_t value
)
581 ppc_tb_t
*tb_env
= env
->tb_env
;
584 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
585 tb
&= 0xFFFFFFFF00000000ULL
;
586 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
587 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
590 void cpu_ppc_store_atbu (CPUPPCState
*env
, uint32_t value
)
592 ppc_tb_t
*tb_env
= env
->tb_env
;
595 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
596 tb
&= 0x00000000FFFFFFFFULL
;
597 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
598 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
601 static void cpu_ppc_tb_stop (CPUPPCState
*env
)
603 ppc_tb_t
*tb_env
= env
->tb_env
;
604 uint64_t tb
, atb
, vmclk
;
606 /* If the time base is already frozen, do nothing */
607 if (tb_env
->tb_freq
!= 0) {
608 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
609 /* Get the time base */
610 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
611 /* Get the alternate time base */
612 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
613 /* Store the time base value (ie compute the current offset) */
614 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
615 /* Store the alternate time base value (compute the current offset) */
616 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
617 /* Set the time base frequency to zero */
619 /* Now, the time bases are frozen to tb_offset / atb_offset value */
623 static void cpu_ppc_tb_start (CPUPPCState
*env
)
625 ppc_tb_t
*tb_env
= env
->tb_env
;
626 uint64_t tb
, atb
, vmclk
;
628 /* If the time base is not frozen, do nothing */
629 if (tb_env
->tb_freq
== 0) {
630 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
631 /* Get the time base from tb_offset */
632 tb
= tb_env
->tb_offset
;
633 /* Get the alternate time base from atb_offset */
634 atb
= tb_env
->atb_offset
;
635 /* Restore the tb frequency from the decrementer frequency */
636 tb_env
->tb_freq
= tb_env
->decr_freq
;
637 /* Store the time base value */
638 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
639 /* Store the alternate time base value */
640 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
644 bool ppc_decr_clear_on_delivery(CPUPPCState
*env
)
646 ppc_tb_t
*tb_env
= env
->tb_env
;
647 int flags
= PPC_DECR_UNDERFLOW_TRIGGERED
| PPC_DECR_UNDERFLOW_LEVEL
;
648 return ((tb_env
->flags
& flags
) == PPC_DECR_UNDERFLOW_TRIGGERED
);
651 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState
*env
, uint64_t next
)
653 ppc_tb_t
*tb_env
= env
->tb_env
;
657 diff
= next
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
659 decr
= muldiv64(diff
, tb_env
->decr_freq
, NANOSECONDS_PER_SECOND
);
660 } else if (tb_env
->flags
& PPC_TIMER_BOOKE
) {
663 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, NANOSECONDS_PER_SECOND
);
665 LOG_TB("%s: %08" PRIx32
"\n", __func__
, decr
);
670 uint32_t cpu_ppc_load_decr (CPUPPCState
*env
)
672 ppc_tb_t
*tb_env
= env
->tb_env
;
675 return env
->spr
[SPR_DECR
];
678 return _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
681 uint32_t cpu_ppc_load_hdecr (CPUPPCState
*env
)
683 ppc_tb_t
*tb_env
= env
->tb_env
;
685 return _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
688 uint64_t cpu_ppc_load_purr (CPUPPCState
*env
)
690 ppc_tb_t
*tb_env
= env
->tb_env
;
693 diff
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - tb_env
->purr_start
;
695 return tb_env
->purr_load
+
696 muldiv64(diff
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
);
699 /* When decrementer expires,
700 * all we need to do is generate or queue a CPU exception
702 static inline void cpu_ppc_decr_excp(PowerPCCPU
*cpu
)
705 LOG_TB("raise decrementer exception\n");
706 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 1);
709 static inline void cpu_ppc_decr_lower(PowerPCCPU
*cpu
)
711 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 0);
714 static inline void cpu_ppc_hdecr_excp(PowerPCCPU
*cpu
)
716 CPUPPCState
*env
= &cpu
->env
;
719 LOG_TB("raise hv decrementer exception\n");
721 /* The architecture specifies that we don't deliver HDEC
722 * interrupts in a PM state. Not only they don't cause a
723 * wakeup but they also get effectively discarded.
725 if (!env
->in_pm_state
) {
726 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 1);
730 static inline void cpu_ppc_hdecr_lower(PowerPCCPU
*cpu
)
732 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 0);
735 static void __cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint64_t *nextp
,
737 void (*raise_excp
)(void *),
738 void (*lower_excp
)(PowerPCCPU
*),
739 uint32_t decr
, uint32_t value
)
741 CPUPPCState
*env
= &cpu
->env
;
742 ppc_tb_t
*tb_env
= env
->tb_env
;
745 LOG_TB("%s: %08" PRIx32
" => %08" PRIx32
"\n", __func__
,
749 /* KVM handles decrementer exceptions, we don't need our own timer */
754 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
757 * If we get a really small DEC value, we can assume that by the time we
758 * handled it we should inject an interrupt already.
760 * On MSB level based DEC implementations the MSB always means the interrupt
761 * is pending, so raise it on those.
763 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
764 * an edge interrupt, so raise it here too.
767 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
) && (value
& 0x80000000)) ||
768 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_TRIGGERED
) && (value
& 0x80000000)
769 && !(decr
& 0x80000000))) {
774 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
775 if (!(value
& 0x80000000) && (tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
)) {
779 /* Calculate the next timer event */
780 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
781 next
= now
+ muldiv64(value
, NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
785 timer_mod(timer
, next
);
788 static inline void _cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint32_t decr
,
791 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
793 __cpu_ppc_store_decr(cpu
, &tb_env
->decr_next
, tb_env
->decr_timer
,
794 tb_env
->decr_timer
->cb
, &cpu_ppc_decr_lower
, decr
,
798 void cpu_ppc_store_decr (CPUPPCState
*env
, uint32_t value
)
800 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
802 _cpu_ppc_store_decr(cpu
, cpu_ppc_load_decr(env
), value
);
805 static void cpu_ppc_decr_cb(void *opaque
)
807 PowerPCCPU
*cpu
= opaque
;
809 cpu_ppc_decr_excp(cpu
);
812 static inline void _cpu_ppc_store_hdecr(PowerPCCPU
*cpu
, uint32_t hdecr
,
815 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
817 if (tb_env
->hdecr_timer
!= NULL
) {
818 __cpu_ppc_store_decr(cpu
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
819 tb_env
->hdecr_timer
->cb
, &cpu_ppc_hdecr_lower
,
824 void cpu_ppc_store_hdecr (CPUPPCState
*env
, uint32_t value
)
826 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
828 _cpu_ppc_store_hdecr(cpu
, cpu_ppc_load_hdecr(env
), value
);
831 static void cpu_ppc_hdecr_cb(void *opaque
)
833 PowerPCCPU
*cpu
= opaque
;
835 cpu_ppc_hdecr_excp(cpu
);
838 static void cpu_ppc_store_purr(PowerPCCPU
*cpu
, uint64_t value
)
840 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
842 tb_env
->purr_load
= value
;
843 tb_env
->purr_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
846 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
848 CPUPPCState
*env
= opaque
;
849 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
850 ppc_tb_t
*tb_env
= env
->tb_env
;
852 tb_env
->tb_freq
= freq
;
853 tb_env
->decr_freq
= freq
;
854 /* There is a bug in Linux 2.4 kernels:
855 * if a decrementer exception is pending when it enables msr_ee at startup,
856 * it's not ready to handle it...
858 _cpu_ppc_store_decr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF);
859 _cpu_ppc_store_hdecr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF);
860 cpu_ppc_store_purr(cpu
, 0x0000000000000000ULL
);
863 static void timebase_save(PPCTimebase
*tb
)
865 uint64_t ticks
= cpu_get_host_ticks();
866 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
868 if (!first_ppc_cpu
->env
.tb_env
) {
869 error_report("No timebase object");
873 /* not used anymore, we keep it for compatibility */
874 tb
->time_of_the_day_ns
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
876 * tb_offset is only expected to be changed by QEMU so
877 * there is no need to update it from KVM here
879 tb
->guest_timebase
= ticks
+ first_ppc_cpu
->env
.tb_env
->tb_offset
;
882 static void timebase_load(PPCTimebase
*tb
)
885 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
886 int64_t tb_off_adj
, tb_off
;
889 if (!first_ppc_cpu
->env
.tb_env
) {
890 error_report("No timebase object");
894 freq
= first_ppc_cpu
->env
.tb_env
->tb_freq
;
896 tb_off_adj
= tb
->guest_timebase
- cpu_get_host_ticks();
898 tb_off
= first_ppc_cpu
->env
.tb_env
->tb_offset
;
899 trace_ppc_tb_adjust(tb_off
, tb_off_adj
, tb_off_adj
- tb_off
,
900 (tb_off_adj
- tb_off
) / freq
);
902 /* Set new offset to all CPUs */
904 PowerPCCPU
*pcpu
= POWERPC_CPU(cpu
);
905 pcpu
->env
.tb_env
->tb_offset
= tb_off_adj
;
906 #if defined(CONFIG_KVM)
907 kvm_set_one_reg(cpu
, KVM_REG_PPC_TB_OFFSET
,
908 &pcpu
->env
.tb_env
->tb_offset
);
913 void cpu_ppc_clock_vm_state_change(void *opaque
, int running
,
916 PPCTimebase
*tb
= opaque
;
926 * When migrating, read the clock just before migration,
927 * so that the guest clock counts during the events
934 * This reduces clock difference on migration from 5s
935 * to 0.1s (when max_downtime == 5s), because sending the
936 * final pages of memory (which happens between vm_stop()
937 * and pre_save()) takes max_downtime.
939 static int timebase_pre_save(void *opaque
)
941 PPCTimebase
*tb
= opaque
;
948 const VMStateDescription vmstate_ppc_timebase
= {
951 .minimum_version_id
= 1,
952 .minimum_version_id_old
= 1,
953 .pre_save
= timebase_pre_save
,
954 .fields
= (VMStateField
[]) {
955 VMSTATE_UINT64(guest_timebase
, PPCTimebase
),
956 VMSTATE_INT64(time_of_the_day_ns
, PPCTimebase
),
957 VMSTATE_END_OF_LIST()
961 /* Set up (once) timebase frequency (in Hz) */
962 clk_setup_cb
cpu_ppc_tb_init (CPUPPCState
*env
, uint32_t freq
)
964 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
967 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
968 env
->tb_env
= tb_env
;
969 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
970 if (env
->insns_flags
& PPC_SEGMENT_64B
) {
971 /* All Book3S 64bit CPUs implement level based DEC logic */
972 tb_env
->flags
|= PPC_DECR_UNDERFLOW_LEVEL
;
974 /* Create new timer */
975 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_decr_cb
, cpu
);
976 if (env
->has_hv_mode
) {
977 tb_env
->hdecr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_hdecr_cb
,
980 tb_env
->hdecr_timer
= NULL
;
982 cpu_ppc_set_tb_clk(env
, freq
);
984 return &cpu_ppc_set_tb_clk
;
987 /* Specific helpers for POWER & PowerPC 601 RTC */
988 void cpu_ppc601_store_rtcu (CPUPPCState
*env
, uint32_t value
)
990 _cpu_ppc_store_tbu(env
, value
);
993 uint32_t cpu_ppc601_load_rtcu (CPUPPCState
*env
)
995 return _cpu_ppc_load_tbu(env
);
998 void cpu_ppc601_store_rtcl (CPUPPCState
*env
, uint32_t value
)
1000 cpu_ppc_store_tbl(env
, value
& 0x3FFFFF80);
1003 uint32_t cpu_ppc601_load_rtcl (CPUPPCState
*env
)
1005 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
1008 /*****************************************************************************/
1009 /* PowerPC 40x timers */
1011 /* PIT, FIT & WDT */
1012 typedef struct ppc40x_timer_t ppc40x_timer_t
;
1013 struct ppc40x_timer_t
{
1014 uint64_t pit_reload
; /* PIT auto-reload value */
1015 uint64_t fit_next
; /* Tick for next FIT interrupt */
1016 QEMUTimer
*fit_timer
;
1017 uint64_t wdt_next
; /* Tick for next WDT interrupt */
1018 QEMUTimer
*wdt_timer
;
1020 /* 405 have the PIT, 440 have a DECR. */
1021 unsigned int decr_excp
;
1024 /* Fixed interval timer */
1025 static void cpu_4xx_fit_cb (void *opaque
)
1030 ppc40x_timer_t
*ppc40x_timer
;
1034 cpu
= ppc_env_get_cpu(env
);
1035 tb_env
= env
->tb_env
;
1036 ppc40x_timer
= tb_env
->opaque
;
1037 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1038 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
1052 /* Cannot occur, but makes gcc happy */
1055 next
= now
+ muldiv64(next
, NANOSECONDS_PER_SECOND
, tb_env
->tb_freq
);
1058 timer_mod(ppc40x_timer
->fit_timer
, next
);
1059 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
1060 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1) {
1061 ppc_set_irq(cpu
, PPC_INTERRUPT_FIT
, 1);
1063 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
1064 (int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
1065 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1068 /* Programmable interval timer */
1069 static void start_stop_pit (CPUPPCState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
1071 ppc40x_timer_t
*ppc40x_timer
;
1074 ppc40x_timer
= tb_env
->opaque
;
1075 if (ppc40x_timer
->pit_reload
<= 1 ||
1076 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
1077 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
1079 LOG_TB("%s: stop PIT\n", __func__
);
1080 timer_del(tb_env
->decr_timer
);
1082 LOG_TB("%s: start PIT %016" PRIx64
"\n",
1083 __func__
, ppc40x_timer
->pit_reload
);
1084 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1085 next
= now
+ muldiv64(ppc40x_timer
->pit_reload
,
1086 NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
1088 next
+= tb_env
->decr_next
- now
;
1091 timer_mod(tb_env
->decr_timer
, next
);
1092 tb_env
->decr_next
= next
;
1096 static void cpu_4xx_pit_cb (void *opaque
)
1101 ppc40x_timer_t
*ppc40x_timer
;
1104 cpu
= ppc_env_get_cpu(env
);
1105 tb_env
= env
->tb_env
;
1106 ppc40x_timer
= tb_env
->opaque
;
1107 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
1108 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) {
1109 ppc_set_irq(cpu
, ppc40x_timer
->decr_excp
, 1);
1111 start_stop_pit(env
, tb_env
, 1);
1112 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
" "
1113 "%016" PRIx64
"\n", __func__
,
1114 (int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
1115 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
1116 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
1117 ppc40x_timer
->pit_reload
);
1120 /* Watchdog timer */
1121 static void cpu_4xx_wdt_cb (void *opaque
)
1126 ppc40x_timer_t
*ppc40x_timer
;
1130 cpu
= ppc_env_get_cpu(env
);
1131 tb_env
= env
->tb_env
;
1132 ppc40x_timer
= tb_env
->opaque
;
1133 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1134 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
1148 /* Cannot occur, but makes gcc happy */
1151 next
= now
+ muldiv64(next
, NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
1154 LOG_TB("%s: TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
1155 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1156 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
1159 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1160 ppc40x_timer
->wdt_next
= next
;
1161 env
->spr
[SPR_40x_TSR
] |= 1U << 31;
1164 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1165 ppc40x_timer
->wdt_next
= next
;
1166 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
1167 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1) {
1168 ppc_set_irq(cpu
, PPC_INTERRUPT_WDT
, 1);
1172 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
1173 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
1174 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
1178 case 0x1: /* Core reset */
1179 ppc40x_core_reset(cpu
);
1181 case 0x2: /* Chip reset */
1182 ppc40x_chip_reset(cpu
);
1184 case 0x3: /* System reset */
1185 ppc40x_system_reset(cpu
);
1191 void store_40x_pit (CPUPPCState
*env
, target_ulong val
)
1194 ppc40x_timer_t
*ppc40x_timer
;
1196 tb_env
= env
->tb_env
;
1197 ppc40x_timer
= tb_env
->opaque
;
1198 LOG_TB("%s val" TARGET_FMT_lx
"\n", __func__
, val
);
1199 ppc40x_timer
->pit_reload
= val
;
1200 start_stop_pit(env
, tb_env
, 0);
1203 target_ulong
load_40x_pit (CPUPPCState
*env
)
1205 return cpu_ppc_load_decr(env
);
1208 static void ppc_40x_set_tb_clk (void *opaque
, uint32_t freq
)
1210 CPUPPCState
*env
= opaque
;
1211 ppc_tb_t
*tb_env
= env
->tb_env
;
1213 LOG_TB("%s set new frequency to %" PRIu32
"\n", __func__
,
1215 tb_env
->tb_freq
= freq
;
1216 tb_env
->decr_freq
= freq
;
1217 /* XXX: we should also update all timers */
1220 clk_setup_cb
ppc_40x_timers_init (CPUPPCState
*env
, uint32_t freq
,
1221 unsigned int decr_excp
)
1224 ppc40x_timer_t
*ppc40x_timer
;
1226 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
1227 env
->tb_env
= tb_env
;
1228 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1229 ppc40x_timer
= g_malloc0(sizeof(ppc40x_timer_t
));
1230 tb_env
->tb_freq
= freq
;
1231 tb_env
->decr_freq
= freq
;
1232 tb_env
->opaque
= ppc40x_timer
;
1233 LOG_TB("%s freq %" PRIu32
"\n", __func__
, freq
);
1234 if (ppc40x_timer
!= NULL
) {
1235 /* We use decr timer for PIT */
1236 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_pit_cb
, env
);
1237 ppc40x_timer
->fit_timer
=
1238 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_fit_cb
, env
);
1239 ppc40x_timer
->wdt_timer
=
1240 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_wdt_cb
, env
);
1241 ppc40x_timer
->decr_excp
= decr_excp
;
1244 return &ppc_40x_set_tb_clk
;
1247 /*****************************************************************************/
1248 /* Embedded PowerPC Device Control Registers */
1249 typedef struct ppc_dcrn_t ppc_dcrn_t
;
1251 dcr_read_cb dcr_read
;
1252 dcr_write_cb dcr_write
;
1256 /* XXX: on 460, DCR addresses are 32 bits wide,
1257 * using DCRIPR to get the 22 upper bits of the DCR address
1259 #define DCRN_NB 1024
1261 ppc_dcrn_t dcrn
[DCRN_NB
];
1262 int (*read_error
)(int dcrn
);
1263 int (*write_error
)(int dcrn
);
1266 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1270 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1272 dcr
= &dcr_env
->dcrn
[dcrn
];
1273 if (dcr
->dcr_read
== NULL
)
1275 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1280 if (dcr_env
->read_error
!= NULL
)
1281 return (*dcr_env
->read_error
)(dcrn
);
1286 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1290 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1292 dcr
= &dcr_env
->dcrn
[dcrn
];
1293 if (dcr
->dcr_write
== NULL
)
1295 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1300 if (dcr_env
->write_error
!= NULL
)
1301 return (*dcr_env
->write_error
)(dcrn
);
1306 int ppc_dcr_register (CPUPPCState
*env
, int dcrn
, void *opaque
,
1307 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1312 dcr_env
= env
->dcr_env
;
1313 if (dcr_env
== NULL
)
1315 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1317 dcr
= &dcr_env
->dcrn
[dcrn
];
1318 if (dcr
->opaque
!= NULL
||
1319 dcr
->dcr_read
!= NULL
||
1320 dcr
->dcr_write
!= NULL
)
1322 dcr
->opaque
= opaque
;
1323 dcr
->dcr_read
= dcr_read
;
1324 dcr
->dcr_write
= dcr_write
;
1329 int ppc_dcr_init (CPUPPCState
*env
, int (*read_error
)(int dcrn
),
1330 int (*write_error
)(int dcrn
))
1334 dcr_env
= g_malloc0(sizeof(ppc_dcr_t
));
1335 dcr_env
->read_error
= read_error
;
1336 dcr_env
->write_error
= write_error
;
1337 env
->dcr_env
= dcr_env
;
1342 /*****************************************************************************/
1344 void PPC_debug_write (void *opaque
, uint32_t addr
, uint32_t val
)
1356 printf("Set loglevel to %04" PRIx32
"\n", val
);
1357 qemu_set_log(val
| 0x100);