2 * QEMU generic PowerPC hardware System Emulator
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/ppc/ppc.h"
26 #include "hw/ppc/ppc_e500.h"
27 #include "qemu/timer.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/cpus.h"
30 #include "hw/timer/m48t59.h"
32 #include "qemu/error-report.h"
33 #include "hw/loader.h"
34 #include "sysemu/kvm.h"
38 //#define PPC_DEBUG_IRQ
39 //#define PPC_DEBUG_TB
42 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
44 # define LOG_IRQ(...) do { } while (0)
49 # define LOG_TB(...) qemu_log(__VA_ARGS__)
51 # define LOG_TB(...) do { } while (0)
54 #define NSEC_PER_SEC 1000000000LL
56 static void cpu_ppc_tb_stop (CPUPPCState
*env
);
57 static void cpu_ppc_tb_start (CPUPPCState
*env
);
59 void ppc_set_irq(PowerPCCPU
*cpu
, int n_IRQ
, int level
)
61 CPUState
*cs
= CPU(cpu
);
62 CPUPPCState
*env
= &cpu
->env
;
63 unsigned int old_pending
= env
->pending_interrupts
;
66 env
->pending_interrupts
|= 1 << n_IRQ
;
67 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
69 env
->pending_interrupts
&= ~(1 << n_IRQ
);
70 if (env
->pending_interrupts
== 0) {
71 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
75 if (old_pending
!= env
->pending_interrupts
) {
77 kvmppc_set_interrupt(cpu
, n_IRQ
, level
);
81 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
82 "req %08x\n", __func__
, env
, n_IRQ
, level
,
83 env
->pending_interrupts
, CPU(cpu
)->interrupt_request
);
86 /* PowerPC 6xx / 7xx internal IRQ controller */
87 static void ppc6xx_set_irq(void *opaque
, int pin
, int level
)
89 PowerPCCPU
*cpu
= opaque
;
90 CPUPPCState
*env
= &cpu
->env
;
93 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
95 cur_level
= (env
->irq_input_state
>> pin
) & 1;
96 /* Don't generate spurious events */
97 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
98 CPUState
*cs
= CPU(cpu
);
101 case PPC6xx_INPUT_TBEN
:
102 /* Level sensitive - active high */
103 LOG_IRQ("%s: %s the time base\n",
104 __func__
, level
? "start" : "stop");
106 cpu_ppc_tb_start(env
);
108 cpu_ppc_tb_stop(env
);
110 case PPC6xx_INPUT_INT
:
111 /* Level sensitive - active high */
112 LOG_IRQ("%s: set the external IRQ state to %d\n",
114 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
116 case PPC6xx_INPUT_SMI
:
117 /* Level sensitive - active high */
118 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
120 ppc_set_irq(cpu
, PPC_INTERRUPT_SMI
, level
);
122 case PPC6xx_INPUT_MCP
:
123 /* Negative edge sensitive */
124 /* XXX: TODO: actual reaction may depends on HID0 status
125 * 603/604/740/750: check HID0[EMCP]
127 if (cur_level
== 1 && level
== 0) {
128 LOG_IRQ("%s: raise machine check state\n",
130 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
133 case PPC6xx_INPUT_CKSTP_IN
:
134 /* Level sensitive - active low */
135 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
136 /* XXX: Note that the only way to restart the CPU is to reset it */
138 LOG_IRQ("%s: stop the CPU\n", __func__
);
142 case PPC6xx_INPUT_HRESET
:
143 /* Level sensitive - active low */
145 LOG_IRQ("%s: reset the CPU\n", __func__
);
146 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
149 case PPC6xx_INPUT_SRESET
:
150 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
152 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
155 /* Unknown pin - do nothing */
156 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
160 env
->irq_input_state
|= 1 << pin
;
162 env
->irq_input_state
&= ~(1 << pin
);
166 void ppc6xx_irq_init(CPUPPCState
*env
)
168 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
170 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, cpu
,
174 #if defined(TARGET_PPC64)
175 /* PowerPC 970 internal IRQ controller */
176 static void ppc970_set_irq(void *opaque
, int pin
, int level
)
178 PowerPCCPU
*cpu
= opaque
;
179 CPUPPCState
*env
= &cpu
->env
;
182 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
184 cur_level
= (env
->irq_input_state
>> pin
) & 1;
185 /* Don't generate spurious events */
186 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
187 CPUState
*cs
= CPU(cpu
);
190 case PPC970_INPUT_INT
:
191 /* Level sensitive - active high */
192 LOG_IRQ("%s: set the external IRQ state to %d\n",
194 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
196 case PPC970_INPUT_THINT
:
197 /* Level sensitive - active high */
198 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__
,
200 ppc_set_irq(cpu
, PPC_INTERRUPT_THERM
, level
);
202 case PPC970_INPUT_MCP
:
203 /* Negative edge sensitive */
204 /* XXX: TODO: actual reaction may depends on HID0 status
205 * 603/604/740/750: check HID0[EMCP]
207 if (cur_level
== 1 && level
== 0) {
208 LOG_IRQ("%s: raise machine check state\n",
210 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
213 case PPC970_INPUT_CKSTP
:
214 /* Level sensitive - active low */
215 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
217 LOG_IRQ("%s: stop the CPU\n", __func__
);
220 LOG_IRQ("%s: restart the CPU\n", __func__
);
225 case PPC970_INPUT_HRESET
:
226 /* Level sensitive - active low */
228 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
231 case PPC970_INPUT_SRESET
:
232 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
234 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
236 case PPC970_INPUT_TBEN
:
237 LOG_IRQ("%s: set the TBEN state to %d\n", __func__
,
242 /* Unknown pin - do nothing */
243 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
247 env
->irq_input_state
|= 1 << pin
;
249 env
->irq_input_state
&= ~(1 << pin
);
253 void ppc970_irq_init(CPUPPCState
*env
)
255 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
257 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, cpu
,
261 /* POWER7 internal IRQ controller */
262 static void power7_set_irq(void *opaque
, int pin
, int level
)
264 PowerPCCPU
*cpu
= opaque
;
265 CPUPPCState
*env
= &cpu
->env
;
267 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
271 case POWER7_INPUT_INT
:
272 /* Level sensitive - active high */
273 LOG_IRQ("%s: set the external IRQ state to %d\n",
275 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
278 /* Unknown pin - do nothing */
279 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
283 env
->irq_input_state
|= 1 << pin
;
285 env
->irq_input_state
&= ~(1 << pin
);
289 void ppcPOWER7_irq_init(CPUPPCState
*env
)
291 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
293 env
->irq_inputs
= (void **)qemu_allocate_irqs(&power7_set_irq
, cpu
,
296 #endif /* defined(TARGET_PPC64) */
298 /* PowerPC 40x internal IRQ controller */
299 static void ppc40x_set_irq(void *opaque
, int pin
, int level
)
301 PowerPCCPU
*cpu
= opaque
;
302 CPUPPCState
*env
= &cpu
->env
;
305 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
307 cur_level
= (env
->irq_input_state
>> pin
) & 1;
308 /* Don't generate spurious events */
309 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
310 CPUState
*cs
= CPU(cpu
);
313 case PPC40x_INPUT_RESET_SYS
:
315 LOG_IRQ("%s: reset the PowerPC system\n",
317 ppc40x_system_reset(cpu
);
320 case PPC40x_INPUT_RESET_CHIP
:
322 LOG_IRQ("%s: reset the PowerPC chip\n", __func__
);
323 ppc40x_chip_reset(cpu
);
326 case PPC40x_INPUT_RESET_CORE
:
327 /* XXX: TODO: update DBSR[MRR] */
329 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
330 ppc40x_core_reset(cpu
);
333 case PPC40x_INPUT_CINT
:
334 /* Level sensitive - active high */
335 LOG_IRQ("%s: set the critical IRQ state to %d\n",
337 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
339 case PPC40x_INPUT_INT
:
340 /* Level sensitive - active high */
341 LOG_IRQ("%s: set the external IRQ state to %d\n",
343 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
345 case PPC40x_INPUT_HALT
:
346 /* Level sensitive - active low */
348 LOG_IRQ("%s: stop the CPU\n", __func__
);
351 LOG_IRQ("%s: restart the CPU\n", __func__
);
356 case PPC40x_INPUT_DEBUG
:
357 /* Level sensitive - active high */
358 LOG_IRQ("%s: set the debug pin state to %d\n",
360 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
363 /* Unknown pin - do nothing */
364 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
368 env
->irq_input_state
|= 1 << pin
;
370 env
->irq_input_state
&= ~(1 << pin
);
374 void ppc40x_irq_init(CPUPPCState
*env
)
376 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
378 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc40x_set_irq
,
379 cpu
, PPC40x_INPUT_NB
);
382 /* PowerPC E500 internal IRQ controller */
383 static void ppce500_set_irq(void *opaque
, int pin
, int level
)
385 PowerPCCPU
*cpu
= opaque
;
386 CPUPPCState
*env
= &cpu
->env
;
389 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
391 cur_level
= (env
->irq_input_state
>> pin
) & 1;
392 /* Don't generate spurious events */
393 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
395 case PPCE500_INPUT_MCK
:
397 LOG_IRQ("%s: reset the PowerPC system\n",
399 qemu_system_reset_request();
402 case PPCE500_INPUT_RESET_CORE
:
404 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
405 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, level
);
408 case PPCE500_INPUT_CINT
:
409 /* Level sensitive - active high */
410 LOG_IRQ("%s: set the critical IRQ state to %d\n",
412 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
414 case PPCE500_INPUT_INT
:
415 /* Level sensitive - active high */
416 LOG_IRQ("%s: set the core IRQ state to %d\n",
418 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
420 case PPCE500_INPUT_DEBUG
:
421 /* Level sensitive - active high */
422 LOG_IRQ("%s: set the debug pin state to %d\n",
424 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
427 /* Unknown pin - do nothing */
428 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
432 env
->irq_input_state
|= 1 << pin
;
434 env
->irq_input_state
&= ~(1 << pin
);
438 void ppce500_irq_init(CPUPPCState
*env
)
440 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
442 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppce500_set_irq
,
443 cpu
, PPCE500_INPUT_NB
);
446 /* Enable or Disable the E500 EPR capability */
447 void ppce500_set_mpic_proxy(bool enabled
)
452 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
454 cpu
->env
.mpic_proxy
= enabled
;
456 kvmppc_set_mpic_proxy(cpu
, enabled
);
461 /*****************************************************************************/
462 /* PowerPC time base and decrementer emulation */
464 uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
, int64_t tb_offset
)
466 /* TB time in tb periods */
467 return muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec()) + tb_offset
;
470 uint64_t cpu_ppc_load_tbl (CPUPPCState
*env
)
472 ppc_tb_t
*tb_env
= env
->tb_env
;
476 return env
->spr
[SPR_TBL
];
479 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
480 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
485 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState
*env
)
487 ppc_tb_t
*tb_env
= env
->tb_env
;
490 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
491 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
496 uint32_t cpu_ppc_load_tbu (CPUPPCState
*env
)
499 return env
->spr
[SPR_TBU
];
502 return _cpu_ppc_load_tbu(env
);
505 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
506 int64_t *tb_offsetp
, uint64_t value
)
508 *tb_offsetp
= value
- muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec());
509 LOG_TB("%s: tb %016" PRIx64
" offset %08" PRIx64
"\n",
510 __func__
, value
, *tb_offsetp
);
513 void cpu_ppc_store_tbl (CPUPPCState
*env
, uint32_t value
)
515 ppc_tb_t
*tb_env
= env
->tb_env
;
518 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
519 tb
&= 0xFFFFFFFF00000000ULL
;
520 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
521 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
524 static inline void _cpu_ppc_store_tbu(CPUPPCState
*env
, uint32_t value
)
526 ppc_tb_t
*tb_env
= env
->tb_env
;
529 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
530 tb
&= 0x00000000FFFFFFFFULL
;
531 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
532 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
535 void cpu_ppc_store_tbu (CPUPPCState
*env
, uint32_t value
)
537 _cpu_ppc_store_tbu(env
, value
);
540 uint64_t cpu_ppc_load_atbl (CPUPPCState
*env
)
542 ppc_tb_t
*tb_env
= env
->tb_env
;
545 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
546 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
551 uint32_t cpu_ppc_load_atbu (CPUPPCState
*env
)
553 ppc_tb_t
*tb_env
= env
->tb_env
;
556 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
557 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
562 void cpu_ppc_store_atbl (CPUPPCState
*env
, uint32_t value
)
564 ppc_tb_t
*tb_env
= env
->tb_env
;
567 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
568 tb
&= 0xFFFFFFFF00000000ULL
;
569 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
570 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
573 void cpu_ppc_store_atbu (CPUPPCState
*env
, uint32_t value
)
575 ppc_tb_t
*tb_env
= env
->tb_env
;
578 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
579 tb
&= 0x00000000FFFFFFFFULL
;
580 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
581 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
584 static void cpu_ppc_tb_stop (CPUPPCState
*env
)
586 ppc_tb_t
*tb_env
= env
->tb_env
;
587 uint64_t tb
, atb
, vmclk
;
589 /* If the time base is already frozen, do nothing */
590 if (tb_env
->tb_freq
!= 0) {
591 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
592 /* Get the time base */
593 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
594 /* Get the alternate time base */
595 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
596 /* Store the time base value (ie compute the current offset) */
597 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
598 /* Store the alternate time base value (compute the current offset) */
599 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
600 /* Set the time base frequency to zero */
602 /* Now, the time bases are frozen to tb_offset / atb_offset value */
606 static void cpu_ppc_tb_start (CPUPPCState
*env
)
608 ppc_tb_t
*tb_env
= env
->tb_env
;
609 uint64_t tb
, atb
, vmclk
;
611 /* If the time base is not frozen, do nothing */
612 if (tb_env
->tb_freq
== 0) {
613 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
614 /* Get the time base from tb_offset */
615 tb
= tb_env
->tb_offset
;
616 /* Get the alternate time base from atb_offset */
617 atb
= tb_env
->atb_offset
;
618 /* Restore the tb frequency from the decrementer frequency */
619 tb_env
->tb_freq
= tb_env
->decr_freq
;
620 /* Store the time base value */
621 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
622 /* Store the alternate time base value */
623 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
627 bool ppc_decr_clear_on_delivery(CPUPPCState
*env
)
629 ppc_tb_t
*tb_env
= env
->tb_env
;
630 int flags
= PPC_DECR_UNDERFLOW_TRIGGERED
| PPC_DECR_UNDERFLOW_LEVEL
;
631 return ((tb_env
->flags
& flags
) == PPC_DECR_UNDERFLOW_TRIGGERED
);
634 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState
*env
, uint64_t next
)
636 ppc_tb_t
*tb_env
= env
->tb_env
;
640 diff
= next
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
642 decr
= muldiv64(diff
, tb_env
->decr_freq
, get_ticks_per_sec());
643 } else if (tb_env
->flags
& PPC_TIMER_BOOKE
) {
646 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, get_ticks_per_sec());
648 LOG_TB("%s: %08" PRIx32
"\n", __func__
, decr
);
653 uint32_t cpu_ppc_load_decr (CPUPPCState
*env
)
655 ppc_tb_t
*tb_env
= env
->tb_env
;
658 return env
->spr
[SPR_DECR
];
661 return _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
664 uint32_t cpu_ppc_load_hdecr (CPUPPCState
*env
)
666 ppc_tb_t
*tb_env
= env
->tb_env
;
668 return _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
671 uint64_t cpu_ppc_load_purr (CPUPPCState
*env
)
673 ppc_tb_t
*tb_env
= env
->tb_env
;
676 diff
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - tb_env
->purr_start
;
678 return tb_env
->purr_load
+ muldiv64(diff
, tb_env
->tb_freq
, get_ticks_per_sec());
681 /* When decrementer expires,
682 * all we need to do is generate or queue a CPU exception
684 static inline void cpu_ppc_decr_excp(PowerPCCPU
*cpu
)
687 LOG_TB("raise decrementer exception\n");
688 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 1);
691 static inline void cpu_ppc_decr_lower(PowerPCCPU
*cpu
)
693 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 0);
696 static inline void cpu_ppc_hdecr_excp(PowerPCCPU
*cpu
)
699 LOG_TB("raise decrementer exception\n");
700 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 1);
703 static inline void cpu_ppc_hdecr_lower(PowerPCCPU
*cpu
)
705 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 0);
708 static void __cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint64_t *nextp
,
710 void (*raise_excp
)(void *),
711 void (*lower_excp
)(PowerPCCPU
*),
712 uint32_t decr
, uint32_t value
)
714 CPUPPCState
*env
= &cpu
->env
;
715 ppc_tb_t
*tb_env
= env
->tb_env
;
718 LOG_TB("%s: %08" PRIx32
" => %08" PRIx32
"\n", __func__
,
722 /* KVM handles decrementer exceptions, we don't need our own timer */
727 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
730 * If we get a really small DEC value, we can assume that by the time we
731 * handled it we should inject an interrupt already.
733 * On MSB level based DEC implementations the MSB always means the interrupt
734 * is pending, so raise it on those.
736 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
737 * an edge interrupt, so raise it here too.
740 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
) && (value
& 0x80000000)) ||
741 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_TRIGGERED
) && (value
& 0x80000000)
742 && !(decr
& 0x80000000))) {
747 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
748 if (!(value
& 0x80000000) && (tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
)) {
752 /* Calculate the next timer event */
753 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
754 next
= now
+ muldiv64(value
, get_ticks_per_sec(), tb_env
->decr_freq
);
758 timer_mod(timer
, next
);
761 static inline void _cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint32_t decr
,
764 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
766 __cpu_ppc_store_decr(cpu
, &tb_env
->decr_next
, tb_env
->decr_timer
,
767 tb_env
->decr_timer
->cb
, &cpu_ppc_decr_lower
, decr
,
771 void cpu_ppc_store_decr (CPUPPCState
*env
, uint32_t value
)
773 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
775 _cpu_ppc_store_decr(cpu
, cpu_ppc_load_decr(env
), value
);
778 static void cpu_ppc_decr_cb(void *opaque
)
780 PowerPCCPU
*cpu
= opaque
;
782 cpu_ppc_decr_excp(cpu
);
785 static inline void _cpu_ppc_store_hdecr(PowerPCCPU
*cpu
, uint32_t hdecr
,
788 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
790 if (tb_env
->hdecr_timer
!= NULL
) {
791 __cpu_ppc_store_decr(cpu
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
792 tb_env
->hdecr_timer
->cb
, &cpu_ppc_hdecr_lower
,
797 void cpu_ppc_store_hdecr (CPUPPCState
*env
, uint32_t value
)
799 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
801 _cpu_ppc_store_hdecr(cpu
, cpu_ppc_load_hdecr(env
), value
);
804 static void cpu_ppc_hdecr_cb(void *opaque
)
806 PowerPCCPU
*cpu
= opaque
;
808 cpu_ppc_hdecr_excp(cpu
);
811 static void cpu_ppc_store_purr(PowerPCCPU
*cpu
, uint64_t value
)
813 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
815 tb_env
->purr_load
= value
;
816 tb_env
->purr_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
819 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
821 CPUPPCState
*env
= opaque
;
822 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
823 ppc_tb_t
*tb_env
= env
->tb_env
;
825 tb_env
->tb_freq
= freq
;
826 tb_env
->decr_freq
= freq
;
827 /* There is a bug in Linux 2.4 kernels:
828 * if a decrementer exception is pending when it enables msr_ee at startup,
829 * it's not ready to handle it...
831 _cpu_ppc_store_decr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF);
832 _cpu_ppc_store_hdecr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF);
833 cpu_ppc_store_purr(cpu
, 0x0000000000000000ULL
);
836 static void timebase_pre_save(void *opaque
)
838 PPCTimebase
*tb
= opaque
;
839 uint64_t ticks
= cpu_get_real_ticks();
840 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
842 if (!first_ppc_cpu
->env
.tb_env
) {
843 error_report("No timebase object");
847 tb
->time_of_the_day_ns
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
849 * tb_offset is only expected to be changed by migration so
850 * there is no need to update it from KVM here
852 tb
->guest_timebase
= ticks
+ first_ppc_cpu
->env
.tb_env
->tb_offset
;
855 static int timebase_post_load(void *opaque
, int version_id
)
857 PPCTimebase
*tb_remote
= opaque
;
859 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
860 int64_t tb_off_adj
, tb_off
, ns_diff
;
861 int64_t migration_duration_ns
, migration_duration_tb
, guest_tb
, host_ns
;
864 if (!first_ppc_cpu
->env
.tb_env
) {
865 error_report("No timebase object");
869 freq
= first_ppc_cpu
->env
.tb_env
->tb_freq
;
871 * Calculate timebase on the destination side of migration.
872 * The destination timebase must be not less than the source timebase.
873 * We try to adjust timebase by downtime if host clocks are not
874 * too much out of sync (1 second for now).
876 host_ns
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
877 ns_diff
= MAX(0, host_ns
- tb_remote
->time_of_the_day_ns
);
878 migration_duration_ns
= MIN(NSEC_PER_SEC
, ns_diff
);
879 migration_duration_tb
= muldiv64(migration_duration_ns
, freq
, NSEC_PER_SEC
);
880 guest_tb
= tb_remote
->guest_timebase
+ MIN(0, migration_duration_tb
);
882 tb_off_adj
= guest_tb
- cpu_get_real_ticks();
884 tb_off
= first_ppc_cpu
->env
.tb_env
->tb_offset
;
885 trace_ppc_tb_adjust(tb_off
, tb_off_adj
, tb_off_adj
- tb_off
,
886 (tb_off_adj
- tb_off
) / freq
);
888 /* Set new offset to all CPUs */
890 PowerPCCPU
*pcpu
= POWERPC_CPU(cpu
);
891 pcpu
->env
.tb_env
->tb_offset
= tb_off_adj
;
897 const VMStateDescription vmstate_ppc_timebase
= {
900 .minimum_version_id
= 1,
901 .minimum_version_id_old
= 1,
902 .pre_save
= timebase_pre_save
,
903 .post_load
= timebase_post_load
,
904 .fields
= (VMStateField
[]) {
905 VMSTATE_UINT64(guest_timebase
, PPCTimebase
),
906 VMSTATE_INT64(time_of_the_day_ns
, PPCTimebase
),
907 VMSTATE_END_OF_LIST()
911 /* Set up (once) timebase frequency (in Hz) */
912 clk_setup_cb
cpu_ppc_tb_init (CPUPPCState
*env
, uint32_t freq
)
914 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
917 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
918 env
->tb_env
= tb_env
;
919 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
920 if (env
->insns_flags
& PPC_SEGMENT_64B
) {
921 /* All Book3S 64bit CPUs implement level based DEC logic */
922 tb_env
->flags
|= PPC_DECR_UNDERFLOW_LEVEL
;
924 /* Create new timer */
925 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_decr_cb
, cpu
);
927 /* XXX: find a suitable condition to enable the hypervisor decrementer
929 tb_env
->hdecr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_hdecr_cb
,
932 tb_env
->hdecr_timer
= NULL
;
934 cpu_ppc_set_tb_clk(env
, freq
);
936 return &cpu_ppc_set_tb_clk
;
939 /* Specific helpers for POWER & PowerPC 601 RTC */
941 static clk_setup_cb
cpu_ppc601_rtc_init (CPUPPCState
*env
)
943 return cpu_ppc_tb_init(env
, 7812500);
947 void cpu_ppc601_store_rtcu (CPUPPCState
*env
, uint32_t value
)
949 _cpu_ppc_store_tbu(env
, value
);
952 uint32_t cpu_ppc601_load_rtcu (CPUPPCState
*env
)
954 return _cpu_ppc_load_tbu(env
);
957 void cpu_ppc601_store_rtcl (CPUPPCState
*env
, uint32_t value
)
959 cpu_ppc_store_tbl(env
, value
& 0x3FFFFF80);
962 uint32_t cpu_ppc601_load_rtcl (CPUPPCState
*env
)
964 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
967 /*****************************************************************************/
968 /* PowerPC 40x timers */
971 typedef struct ppc40x_timer_t ppc40x_timer_t
;
972 struct ppc40x_timer_t
{
973 uint64_t pit_reload
; /* PIT auto-reload value */
974 uint64_t fit_next
; /* Tick for next FIT interrupt */
975 QEMUTimer
*fit_timer
;
976 uint64_t wdt_next
; /* Tick for next WDT interrupt */
977 QEMUTimer
*wdt_timer
;
979 /* 405 have the PIT, 440 have a DECR. */
980 unsigned int decr_excp
;
983 /* Fixed interval timer */
984 static void cpu_4xx_fit_cb (void *opaque
)
989 ppc40x_timer_t
*ppc40x_timer
;
993 cpu
= ppc_env_get_cpu(env
);
994 tb_env
= env
->tb_env
;
995 ppc40x_timer
= tb_env
->opaque
;
996 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
997 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
1011 /* Cannot occur, but makes gcc happy */
1014 next
= now
+ muldiv64(next
, get_ticks_per_sec(), tb_env
->tb_freq
);
1017 timer_mod(ppc40x_timer
->fit_timer
, next
);
1018 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
1019 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1) {
1020 ppc_set_irq(cpu
, PPC_INTERRUPT_FIT
, 1);
1022 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
1023 (int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
1024 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1027 /* Programmable interval timer */
1028 static void start_stop_pit (CPUPPCState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
1030 ppc40x_timer_t
*ppc40x_timer
;
1033 ppc40x_timer
= tb_env
->opaque
;
1034 if (ppc40x_timer
->pit_reload
<= 1 ||
1035 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
1036 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
1038 LOG_TB("%s: stop PIT\n", __func__
);
1039 timer_del(tb_env
->decr_timer
);
1041 LOG_TB("%s: start PIT %016" PRIx64
"\n",
1042 __func__
, ppc40x_timer
->pit_reload
);
1043 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1044 next
= now
+ muldiv64(ppc40x_timer
->pit_reload
,
1045 get_ticks_per_sec(), tb_env
->decr_freq
);
1047 next
+= tb_env
->decr_next
- now
;
1050 timer_mod(tb_env
->decr_timer
, next
);
1051 tb_env
->decr_next
= next
;
1055 static void cpu_4xx_pit_cb (void *opaque
)
1060 ppc40x_timer_t
*ppc40x_timer
;
1063 cpu
= ppc_env_get_cpu(env
);
1064 tb_env
= env
->tb_env
;
1065 ppc40x_timer
= tb_env
->opaque
;
1066 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
1067 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) {
1068 ppc_set_irq(cpu
, ppc40x_timer
->decr_excp
, 1);
1070 start_stop_pit(env
, tb_env
, 1);
1071 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
" "
1072 "%016" PRIx64
"\n", __func__
,
1073 (int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
1074 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
1075 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
1076 ppc40x_timer
->pit_reload
);
1079 /* Watchdog timer */
1080 static void cpu_4xx_wdt_cb (void *opaque
)
1085 ppc40x_timer_t
*ppc40x_timer
;
1089 cpu
= ppc_env_get_cpu(env
);
1090 tb_env
= env
->tb_env
;
1091 ppc40x_timer
= tb_env
->opaque
;
1092 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1093 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
1107 /* Cannot occur, but makes gcc happy */
1110 next
= now
+ muldiv64(next
, get_ticks_per_sec(), tb_env
->decr_freq
);
1113 LOG_TB("%s: TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
1114 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1115 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
1118 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1119 ppc40x_timer
->wdt_next
= next
;
1120 env
->spr
[SPR_40x_TSR
] |= 1U << 31;
1123 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1124 ppc40x_timer
->wdt_next
= next
;
1125 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
1126 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1) {
1127 ppc_set_irq(cpu
, PPC_INTERRUPT_WDT
, 1);
1131 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
1132 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
1133 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
1137 case 0x1: /* Core reset */
1138 ppc40x_core_reset(cpu
);
1140 case 0x2: /* Chip reset */
1141 ppc40x_chip_reset(cpu
);
1143 case 0x3: /* System reset */
1144 ppc40x_system_reset(cpu
);
1150 void store_40x_pit (CPUPPCState
*env
, target_ulong val
)
1153 ppc40x_timer_t
*ppc40x_timer
;
1155 tb_env
= env
->tb_env
;
1156 ppc40x_timer
= tb_env
->opaque
;
1157 LOG_TB("%s val" TARGET_FMT_lx
"\n", __func__
, val
);
1158 ppc40x_timer
->pit_reload
= val
;
1159 start_stop_pit(env
, tb_env
, 0);
1162 target_ulong
load_40x_pit (CPUPPCState
*env
)
1164 return cpu_ppc_load_decr(env
);
1167 static void ppc_40x_set_tb_clk (void *opaque
, uint32_t freq
)
1169 CPUPPCState
*env
= opaque
;
1170 ppc_tb_t
*tb_env
= env
->tb_env
;
1172 LOG_TB("%s set new frequency to %" PRIu32
"\n", __func__
,
1174 tb_env
->tb_freq
= freq
;
1175 tb_env
->decr_freq
= freq
;
1176 /* XXX: we should also update all timers */
1179 clk_setup_cb
ppc_40x_timers_init (CPUPPCState
*env
, uint32_t freq
,
1180 unsigned int decr_excp
)
1183 ppc40x_timer_t
*ppc40x_timer
;
1185 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
1186 env
->tb_env
= tb_env
;
1187 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1188 ppc40x_timer
= g_malloc0(sizeof(ppc40x_timer_t
));
1189 tb_env
->tb_freq
= freq
;
1190 tb_env
->decr_freq
= freq
;
1191 tb_env
->opaque
= ppc40x_timer
;
1192 LOG_TB("%s freq %" PRIu32
"\n", __func__
, freq
);
1193 if (ppc40x_timer
!= NULL
) {
1194 /* We use decr timer for PIT */
1195 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_pit_cb
, env
);
1196 ppc40x_timer
->fit_timer
=
1197 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_fit_cb
, env
);
1198 ppc40x_timer
->wdt_timer
=
1199 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_wdt_cb
, env
);
1200 ppc40x_timer
->decr_excp
= decr_excp
;
1203 return &ppc_40x_set_tb_clk
;
1206 /*****************************************************************************/
1207 /* Embedded PowerPC Device Control Registers */
1208 typedef struct ppc_dcrn_t ppc_dcrn_t
;
1210 dcr_read_cb dcr_read
;
1211 dcr_write_cb dcr_write
;
1215 /* XXX: on 460, DCR addresses are 32 bits wide,
1216 * using DCRIPR to get the 22 upper bits of the DCR address
1218 #define DCRN_NB 1024
1220 ppc_dcrn_t dcrn
[DCRN_NB
];
1221 int (*read_error
)(int dcrn
);
1222 int (*write_error
)(int dcrn
);
1225 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1229 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1231 dcr
= &dcr_env
->dcrn
[dcrn
];
1232 if (dcr
->dcr_read
== NULL
)
1234 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1239 if (dcr_env
->read_error
!= NULL
)
1240 return (*dcr_env
->read_error
)(dcrn
);
1245 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1249 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1251 dcr
= &dcr_env
->dcrn
[dcrn
];
1252 if (dcr
->dcr_write
== NULL
)
1254 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1259 if (dcr_env
->write_error
!= NULL
)
1260 return (*dcr_env
->write_error
)(dcrn
);
1265 int ppc_dcr_register (CPUPPCState
*env
, int dcrn
, void *opaque
,
1266 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1271 dcr_env
= env
->dcr_env
;
1272 if (dcr_env
== NULL
)
1274 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1276 dcr
= &dcr_env
->dcrn
[dcrn
];
1277 if (dcr
->opaque
!= NULL
||
1278 dcr
->dcr_read
!= NULL
||
1279 dcr
->dcr_write
!= NULL
)
1281 dcr
->opaque
= opaque
;
1282 dcr
->dcr_read
= dcr_read
;
1283 dcr
->dcr_write
= dcr_write
;
1288 int ppc_dcr_init (CPUPPCState
*env
, int (*read_error
)(int dcrn
),
1289 int (*write_error
)(int dcrn
))
1293 dcr_env
= g_malloc0(sizeof(ppc_dcr_t
));
1294 dcr_env
->read_error
= read_error
;
1295 dcr_env
->write_error
= write_error
;
1296 env
->dcr_env
= dcr_env
;
1301 /*****************************************************************************/
1303 void PPC_debug_write (void *opaque
, uint32_t addr
, uint32_t val
)
1315 printf("Set loglevel to %04" PRIx32
"\n", val
);
1316 qemu_set_log(val
| 0x100);
1321 /* CPU device-tree ID helpers */
1322 int ppc_get_vcpu_dt_id(PowerPCCPU
*cpu
)
1324 return cpu
->cpu_dt_id
;
1327 PowerPCCPU
*ppc_get_vcpu_by_dt_id(int cpu_dt_id
)
1332 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1334 if (cpu
->cpu_dt_id
== cpu_dt_id
) {