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"
26 #include "hw/ppc/ppc.h"
27 #include "hw/ppc/ppc_e500.h"
28 #include "qemu/timer.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/cpus.h"
31 #include "hw/timer/m48t59.h"
33 #include "qemu/error-report.h"
34 #include "hw/loader.h"
35 #include "sysemu/kvm.h"
39 //#define PPC_DEBUG_IRQ
40 //#define PPC_DEBUG_TB
43 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
45 # define LOG_IRQ(...) do { } while (0)
50 # define LOG_TB(...) qemu_log(__VA_ARGS__)
52 # define LOG_TB(...) do { } while (0)
55 static void cpu_ppc_tb_stop (CPUPPCState
*env
);
56 static void cpu_ppc_tb_start (CPUPPCState
*env
);
58 void ppc_set_irq(PowerPCCPU
*cpu
, int n_IRQ
, int level
)
60 CPUState
*cs
= CPU(cpu
);
61 CPUPPCState
*env
= &cpu
->env
;
62 unsigned int old_pending
= env
->pending_interrupts
;
65 env
->pending_interrupts
|= 1 << n_IRQ
;
66 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
68 env
->pending_interrupts
&= ~(1 << n_IRQ
);
69 if (env
->pending_interrupts
== 0) {
70 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
74 if (old_pending
!= env
->pending_interrupts
) {
76 kvmppc_set_interrupt(cpu
, n_IRQ
, level
);
80 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
81 "req %08x\n", __func__
, env
, n_IRQ
, level
,
82 env
->pending_interrupts
, CPU(cpu
)->interrupt_request
);
85 /* PowerPC 6xx / 7xx internal IRQ controller */
86 static void ppc6xx_set_irq(void *opaque
, int pin
, int level
)
88 PowerPCCPU
*cpu
= opaque
;
89 CPUPPCState
*env
= &cpu
->env
;
92 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
94 cur_level
= (env
->irq_input_state
>> pin
) & 1;
95 /* Don't generate spurious events */
96 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
97 CPUState
*cs
= CPU(cpu
);
100 case PPC6xx_INPUT_TBEN
:
101 /* Level sensitive - active high */
102 LOG_IRQ("%s: %s the time base\n",
103 __func__
, level
? "start" : "stop");
105 cpu_ppc_tb_start(env
);
107 cpu_ppc_tb_stop(env
);
109 case PPC6xx_INPUT_INT
:
110 /* Level sensitive - active high */
111 LOG_IRQ("%s: set the external IRQ state to %d\n",
113 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
115 case PPC6xx_INPUT_SMI
:
116 /* Level sensitive - active high */
117 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
119 ppc_set_irq(cpu
, PPC_INTERRUPT_SMI
, level
);
121 case PPC6xx_INPUT_MCP
:
122 /* Negative edge sensitive */
123 /* XXX: TODO: actual reaction may depends on HID0 status
124 * 603/604/740/750: check HID0[EMCP]
126 if (cur_level
== 1 && level
== 0) {
127 LOG_IRQ("%s: raise machine check state\n",
129 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
132 case PPC6xx_INPUT_CKSTP_IN
:
133 /* Level sensitive - active low */
134 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
135 /* XXX: Note that the only way to restart the CPU is to reset it */
137 LOG_IRQ("%s: stop the CPU\n", __func__
);
141 case PPC6xx_INPUT_HRESET
:
142 /* Level sensitive - active low */
144 LOG_IRQ("%s: reset the CPU\n", __func__
);
145 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
148 case PPC6xx_INPUT_SRESET
:
149 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
151 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
154 /* Unknown pin - do nothing */
155 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
159 env
->irq_input_state
|= 1 << pin
;
161 env
->irq_input_state
&= ~(1 << pin
);
165 void ppc6xx_irq_init(CPUPPCState
*env
)
167 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
169 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, cpu
,
173 #if defined(TARGET_PPC64)
174 /* PowerPC 970 internal IRQ controller */
175 static void ppc970_set_irq(void *opaque
, int pin
, int level
)
177 PowerPCCPU
*cpu
= opaque
;
178 CPUPPCState
*env
= &cpu
->env
;
181 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
183 cur_level
= (env
->irq_input_state
>> pin
) & 1;
184 /* Don't generate spurious events */
185 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
186 CPUState
*cs
= CPU(cpu
);
189 case PPC970_INPUT_INT
:
190 /* Level sensitive - active high */
191 LOG_IRQ("%s: set the external IRQ state to %d\n",
193 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
195 case PPC970_INPUT_THINT
:
196 /* Level sensitive - active high */
197 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__
,
199 ppc_set_irq(cpu
, PPC_INTERRUPT_THERM
, level
);
201 case PPC970_INPUT_MCP
:
202 /* Negative edge sensitive */
203 /* XXX: TODO: actual reaction may depends on HID0 status
204 * 603/604/740/750: check HID0[EMCP]
206 if (cur_level
== 1 && level
== 0) {
207 LOG_IRQ("%s: raise machine check state\n",
209 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
212 case PPC970_INPUT_CKSTP
:
213 /* Level sensitive - active low */
214 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
216 LOG_IRQ("%s: stop the CPU\n", __func__
);
219 LOG_IRQ("%s: restart the CPU\n", __func__
);
224 case PPC970_INPUT_HRESET
:
225 /* Level sensitive - active low */
227 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
230 case PPC970_INPUT_SRESET
:
231 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
233 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
235 case PPC970_INPUT_TBEN
:
236 LOG_IRQ("%s: set the TBEN state to %d\n", __func__
,
241 /* Unknown pin - do nothing */
242 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
246 env
->irq_input_state
|= 1 << pin
;
248 env
->irq_input_state
&= ~(1 << pin
);
252 void ppc970_irq_init(CPUPPCState
*env
)
254 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
256 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, cpu
,
260 /* POWER7 internal IRQ controller */
261 static void power7_set_irq(void *opaque
, int pin
, int level
)
263 PowerPCCPU
*cpu
= opaque
;
264 CPUPPCState
*env
= &cpu
->env
;
266 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
270 case POWER7_INPUT_INT
:
271 /* Level sensitive - active high */
272 LOG_IRQ("%s: set the external IRQ state to %d\n",
274 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
277 /* Unknown pin - do nothing */
278 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
282 env
->irq_input_state
|= 1 << pin
;
284 env
->irq_input_state
&= ~(1 << pin
);
288 void ppcPOWER7_irq_init(CPUPPCState
*env
)
290 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
292 env
->irq_inputs
= (void **)qemu_allocate_irqs(&power7_set_irq
, cpu
,
295 #endif /* defined(TARGET_PPC64) */
297 /* PowerPC 40x internal IRQ controller */
298 static void ppc40x_set_irq(void *opaque
, int pin
, int level
)
300 PowerPCCPU
*cpu
= opaque
;
301 CPUPPCState
*env
= &cpu
->env
;
304 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
306 cur_level
= (env
->irq_input_state
>> pin
) & 1;
307 /* Don't generate spurious events */
308 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
309 CPUState
*cs
= CPU(cpu
);
312 case PPC40x_INPUT_RESET_SYS
:
314 LOG_IRQ("%s: reset the PowerPC system\n",
316 ppc40x_system_reset(cpu
);
319 case PPC40x_INPUT_RESET_CHIP
:
321 LOG_IRQ("%s: reset the PowerPC chip\n", __func__
);
322 ppc40x_chip_reset(cpu
);
325 case PPC40x_INPUT_RESET_CORE
:
326 /* XXX: TODO: update DBSR[MRR] */
328 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
329 ppc40x_core_reset(cpu
);
332 case PPC40x_INPUT_CINT
:
333 /* Level sensitive - active high */
334 LOG_IRQ("%s: set the critical IRQ state to %d\n",
336 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
338 case PPC40x_INPUT_INT
:
339 /* Level sensitive - active high */
340 LOG_IRQ("%s: set the external IRQ state to %d\n",
342 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
344 case PPC40x_INPUT_HALT
:
345 /* Level sensitive - active low */
347 LOG_IRQ("%s: stop the CPU\n", __func__
);
350 LOG_IRQ("%s: restart the CPU\n", __func__
);
355 case PPC40x_INPUT_DEBUG
:
356 /* Level sensitive - active high */
357 LOG_IRQ("%s: set the debug pin state to %d\n",
359 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
362 /* Unknown pin - do nothing */
363 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
367 env
->irq_input_state
|= 1 << pin
;
369 env
->irq_input_state
&= ~(1 << pin
);
373 void ppc40x_irq_init(CPUPPCState
*env
)
375 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
377 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc40x_set_irq
,
378 cpu
, PPC40x_INPUT_NB
);
381 /* PowerPC E500 internal IRQ controller */
382 static void ppce500_set_irq(void *opaque
, int pin
, int level
)
384 PowerPCCPU
*cpu
= opaque
;
385 CPUPPCState
*env
= &cpu
->env
;
388 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
390 cur_level
= (env
->irq_input_state
>> pin
) & 1;
391 /* Don't generate spurious events */
392 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
394 case PPCE500_INPUT_MCK
:
396 LOG_IRQ("%s: reset the PowerPC system\n",
398 qemu_system_reset_request();
401 case PPCE500_INPUT_RESET_CORE
:
403 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
404 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, level
);
407 case PPCE500_INPUT_CINT
:
408 /* Level sensitive - active high */
409 LOG_IRQ("%s: set the critical IRQ state to %d\n",
411 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
413 case PPCE500_INPUT_INT
:
414 /* Level sensitive - active high */
415 LOG_IRQ("%s: set the core IRQ state to %d\n",
417 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
419 case PPCE500_INPUT_DEBUG
:
420 /* Level sensitive - active high */
421 LOG_IRQ("%s: set the debug pin state to %d\n",
423 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
426 /* Unknown pin - do nothing */
427 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
431 env
->irq_input_state
|= 1 << pin
;
433 env
->irq_input_state
&= ~(1 << pin
);
437 void ppce500_irq_init(CPUPPCState
*env
)
439 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
441 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppce500_set_irq
,
442 cpu
, PPCE500_INPUT_NB
);
445 /* Enable or Disable the E500 EPR capability */
446 void ppce500_set_mpic_proxy(bool enabled
)
451 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
453 cpu
->env
.mpic_proxy
= enabled
;
455 kvmppc_set_mpic_proxy(cpu
, enabled
);
460 /*****************************************************************************/
461 /* PowerPC time base and decrementer emulation */
463 uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
, int64_t tb_offset
)
465 /* TB time in tb periods */
466 return muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec()) + tb_offset
;
469 uint64_t cpu_ppc_load_tbl (CPUPPCState
*env
)
471 ppc_tb_t
*tb_env
= env
->tb_env
;
475 return env
->spr
[SPR_TBL
];
478 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
479 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
484 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState
*env
)
486 ppc_tb_t
*tb_env
= env
->tb_env
;
489 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
490 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
495 uint32_t cpu_ppc_load_tbu (CPUPPCState
*env
)
498 return env
->spr
[SPR_TBU
];
501 return _cpu_ppc_load_tbu(env
);
504 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
505 int64_t *tb_offsetp
, uint64_t value
)
507 *tb_offsetp
= value
- muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec());
508 LOG_TB("%s: tb %016" PRIx64
" offset %08" PRIx64
"\n",
509 __func__
, value
, *tb_offsetp
);
512 void cpu_ppc_store_tbl (CPUPPCState
*env
, uint32_t value
)
514 ppc_tb_t
*tb_env
= env
->tb_env
;
517 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
518 tb
&= 0xFFFFFFFF00000000ULL
;
519 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
520 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
523 static inline void _cpu_ppc_store_tbu(CPUPPCState
*env
, uint32_t value
)
525 ppc_tb_t
*tb_env
= env
->tb_env
;
528 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
529 tb
&= 0x00000000FFFFFFFFULL
;
530 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
531 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
534 void cpu_ppc_store_tbu (CPUPPCState
*env
, uint32_t value
)
536 _cpu_ppc_store_tbu(env
, value
);
539 uint64_t cpu_ppc_load_atbl (CPUPPCState
*env
)
541 ppc_tb_t
*tb_env
= env
->tb_env
;
544 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
545 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
550 uint32_t cpu_ppc_load_atbu (CPUPPCState
*env
)
552 ppc_tb_t
*tb_env
= env
->tb_env
;
555 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
556 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
561 void cpu_ppc_store_atbl (CPUPPCState
*env
, uint32_t value
)
563 ppc_tb_t
*tb_env
= env
->tb_env
;
566 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
567 tb
&= 0xFFFFFFFF00000000ULL
;
568 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
569 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
572 void cpu_ppc_store_atbu (CPUPPCState
*env
, uint32_t value
)
574 ppc_tb_t
*tb_env
= env
->tb_env
;
577 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
578 tb
&= 0x00000000FFFFFFFFULL
;
579 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
580 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
583 static void cpu_ppc_tb_stop (CPUPPCState
*env
)
585 ppc_tb_t
*tb_env
= env
->tb_env
;
586 uint64_t tb
, atb
, vmclk
;
588 /* If the time base is already frozen, do nothing */
589 if (tb_env
->tb_freq
!= 0) {
590 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
591 /* Get the time base */
592 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
593 /* Get the alternate time base */
594 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
595 /* Store the time base value (ie compute the current offset) */
596 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
597 /* Store the alternate time base value (compute the current offset) */
598 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
599 /* Set the time base frequency to zero */
601 /* Now, the time bases are frozen to tb_offset / atb_offset value */
605 static void cpu_ppc_tb_start (CPUPPCState
*env
)
607 ppc_tb_t
*tb_env
= env
->tb_env
;
608 uint64_t tb
, atb
, vmclk
;
610 /* If the time base is not frozen, do nothing */
611 if (tb_env
->tb_freq
== 0) {
612 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
613 /* Get the time base from tb_offset */
614 tb
= tb_env
->tb_offset
;
615 /* Get the alternate time base from atb_offset */
616 atb
= tb_env
->atb_offset
;
617 /* Restore the tb frequency from the decrementer frequency */
618 tb_env
->tb_freq
= tb_env
->decr_freq
;
619 /* Store the time base value */
620 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
621 /* Store the alternate time base value */
622 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
626 bool ppc_decr_clear_on_delivery(CPUPPCState
*env
)
628 ppc_tb_t
*tb_env
= env
->tb_env
;
629 int flags
= PPC_DECR_UNDERFLOW_TRIGGERED
| PPC_DECR_UNDERFLOW_LEVEL
;
630 return ((tb_env
->flags
& flags
) == PPC_DECR_UNDERFLOW_TRIGGERED
);
633 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState
*env
, uint64_t next
)
635 ppc_tb_t
*tb_env
= env
->tb_env
;
639 diff
= next
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
641 decr
= muldiv64(diff
, tb_env
->decr_freq
, get_ticks_per_sec());
642 } else if (tb_env
->flags
& PPC_TIMER_BOOKE
) {
645 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, get_ticks_per_sec());
647 LOG_TB("%s: %08" PRIx32
"\n", __func__
, decr
);
652 uint32_t cpu_ppc_load_decr (CPUPPCState
*env
)
654 ppc_tb_t
*tb_env
= env
->tb_env
;
657 return env
->spr
[SPR_DECR
];
660 return _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
663 uint32_t cpu_ppc_load_hdecr (CPUPPCState
*env
)
665 ppc_tb_t
*tb_env
= env
->tb_env
;
667 return _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
670 uint64_t cpu_ppc_load_purr (CPUPPCState
*env
)
672 ppc_tb_t
*tb_env
= env
->tb_env
;
675 diff
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - tb_env
->purr_start
;
677 return tb_env
->purr_load
+ muldiv64(diff
, tb_env
->tb_freq
, get_ticks_per_sec());
680 /* When decrementer expires,
681 * all we need to do is generate or queue a CPU exception
683 static inline void cpu_ppc_decr_excp(PowerPCCPU
*cpu
)
686 LOG_TB("raise decrementer exception\n");
687 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 1);
690 static inline void cpu_ppc_decr_lower(PowerPCCPU
*cpu
)
692 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 0);
695 static inline void cpu_ppc_hdecr_excp(PowerPCCPU
*cpu
)
698 LOG_TB("raise decrementer exception\n");
699 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 1);
702 static inline void cpu_ppc_hdecr_lower(PowerPCCPU
*cpu
)
704 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 0);
707 static void __cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint64_t *nextp
,
709 void (*raise_excp
)(void *),
710 void (*lower_excp
)(PowerPCCPU
*),
711 uint32_t decr
, uint32_t value
)
713 CPUPPCState
*env
= &cpu
->env
;
714 ppc_tb_t
*tb_env
= env
->tb_env
;
717 LOG_TB("%s: %08" PRIx32
" => %08" PRIx32
"\n", __func__
,
721 /* KVM handles decrementer exceptions, we don't need our own timer */
726 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
729 * If we get a really small DEC value, we can assume that by the time we
730 * handled it we should inject an interrupt already.
732 * On MSB level based DEC implementations the MSB always means the interrupt
733 * is pending, so raise it on those.
735 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
736 * an edge interrupt, so raise it here too.
739 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
) && (value
& 0x80000000)) ||
740 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_TRIGGERED
) && (value
& 0x80000000)
741 && !(decr
& 0x80000000))) {
746 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
747 if (!(value
& 0x80000000) && (tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
)) {
751 /* Calculate the next timer event */
752 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
753 next
= now
+ muldiv64(value
, get_ticks_per_sec(), tb_env
->decr_freq
);
757 timer_mod(timer
, next
);
760 static inline void _cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint32_t decr
,
763 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
765 __cpu_ppc_store_decr(cpu
, &tb_env
->decr_next
, tb_env
->decr_timer
,
766 tb_env
->decr_timer
->cb
, &cpu_ppc_decr_lower
, decr
,
770 void cpu_ppc_store_decr (CPUPPCState
*env
, uint32_t value
)
772 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
774 _cpu_ppc_store_decr(cpu
, cpu_ppc_load_decr(env
), value
);
777 static void cpu_ppc_decr_cb(void *opaque
)
779 PowerPCCPU
*cpu
= opaque
;
781 cpu_ppc_decr_excp(cpu
);
784 static inline void _cpu_ppc_store_hdecr(PowerPCCPU
*cpu
, uint32_t hdecr
,
787 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
789 if (tb_env
->hdecr_timer
!= NULL
) {
790 __cpu_ppc_store_decr(cpu
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
791 tb_env
->hdecr_timer
->cb
, &cpu_ppc_hdecr_lower
,
796 void cpu_ppc_store_hdecr (CPUPPCState
*env
, uint32_t value
)
798 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
800 _cpu_ppc_store_hdecr(cpu
, cpu_ppc_load_hdecr(env
), value
);
803 static void cpu_ppc_hdecr_cb(void *opaque
)
805 PowerPCCPU
*cpu
= opaque
;
807 cpu_ppc_hdecr_excp(cpu
);
810 static void cpu_ppc_store_purr(PowerPCCPU
*cpu
, uint64_t value
)
812 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
814 tb_env
->purr_load
= value
;
815 tb_env
->purr_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
818 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
820 CPUPPCState
*env
= opaque
;
821 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
822 ppc_tb_t
*tb_env
= env
->tb_env
;
824 tb_env
->tb_freq
= freq
;
825 tb_env
->decr_freq
= freq
;
826 /* There is a bug in Linux 2.4 kernels:
827 * if a decrementer exception is pending when it enables msr_ee at startup,
828 * it's not ready to handle it...
830 _cpu_ppc_store_decr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF);
831 _cpu_ppc_store_hdecr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF);
832 cpu_ppc_store_purr(cpu
, 0x0000000000000000ULL
);
835 static void timebase_pre_save(void *opaque
)
837 PPCTimebase
*tb
= opaque
;
838 uint64_t ticks
= cpu_get_host_ticks();
839 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
841 if (!first_ppc_cpu
->env
.tb_env
) {
842 error_report("No timebase object");
846 tb
->time_of_the_day_ns
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
848 * tb_offset is only expected to be changed by migration so
849 * there is no need to update it from KVM here
851 tb
->guest_timebase
= ticks
+ first_ppc_cpu
->env
.tb_env
->tb_offset
;
854 static int timebase_post_load(void *opaque
, int version_id
)
856 PPCTimebase
*tb_remote
= opaque
;
858 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
859 int64_t tb_off_adj
, tb_off
, ns_diff
;
860 int64_t migration_duration_ns
, migration_duration_tb
, guest_tb
, host_ns
;
863 if (!first_ppc_cpu
->env
.tb_env
) {
864 error_report("No timebase object");
868 freq
= first_ppc_cpu
->env
.tb_env
->tb_freq
;
870 * Calculate timebase on the destination side of migration.
871 * The destination timebase must be not less than the source timebase.
872 * We try to adjust timebase by downtime if host clocks are not
873 * too much out of sync (1 second for now).
875 host_ns
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
876 ns_diff
= MAX(0, host_ns
- tb_remote
->time_of_the_day_ns
);
877 migration_duration_ns
= MIN(NANOSECONDS_PER_SECOND
, ns_diff
);
878 migration_duration_tb
= muldiv64(migration_duration_ns
, freq
,
879 NANOSECONDS_PER_SECOND
);
880 guest_tb
= tb_remote
->guest_timebase
+ MIN(0, migration_duration_tb
);
882 tb_off_adj
= guest_tb
- cpu_get_host_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
) {