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 "qemu/osdep.h"
27 #include "hw/ppc/ppc.h"
28 #include "hw/ppc/ppc_e500.h"
29 #include "qemu/timer.h"
30 #include "sysemu/cpus.h"
32 #include "qemu/main-loop.h"
33 #include "qemu/error-report.h"
34 #include "sysemu/kvm.h"
35 #include "sysemu/runstate.h"
37 #include "migration/vmstate.h"
40 static void cpu_ppc_tb_stop (CPUPPCState
*env
);
41 static void cpu_ppc_tb_start (CPUPPCState
*env
);
43 void ppc_set_irq(PowerPCCPU
*cpu
, int n_IRQ
, int level
)
45 CPUState
*cs
= CPU(cpu
);
46 CPUPPCState
*env
= &cpu
->env
;
47 unsigned int old_pending
;
50 /* We may already have the BQL if coming from the reset path */
51 if (!qemu_mutex_iothread_locked()) {
53 qemu_mutex_lock_iothread();
56 old_pending
= env
->pending_interrupts
;
59 env
->pending_interrupts
|= 1 << n_IRQ
;
60 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
62 env
->pending_interrupts
&= ~(1 << n_IRQ
);
63 if (env
->pending_interrupts
== 0) {
64 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
68 if (old_pending
!= env
->pending_interrupts
) {
69 kvmppc_set_interrupt(cpu
, n_IRQ
, level
);
73 trace_ppc_irq_set_exit(env
, n_IRQ
, level
, env
->pending_interrupts
,
74 CPU(cpu
)->interrupt_request
);
77 qemu_mutex_unlock_iothread();
81 /* PowerPC 6xx / 7xx internal IRQ controller */
82 static void ppc6xx_set_irq(void *opaque
, int pin
, int level
)
84 PowerPCCPU
*cpu
= opaque
;
85 CPUPPCState
*env
= &cpu
->env
;
88 trace_ppc_irq_set(env
, pin
, level
);
90 cur_level
= (env
->irq_input_state
>> pin
) & 1;
91 /* Don't generate spurious events */
92 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
93 CPUState
*cs
= CPU(cpu
);
96 case PPC6xx_INPUT_TBEN
:
97 /* Level sensitive - active high */
98 trace_ppc_irq_set_state("time base", level
);
100 cpu_ppc_tb_start(env
);
102 cpu_ppc_tb_stop(env
);
105 case PPC6xx_INPUT_INT
:
106 /* Level sensitive - active high */
107 trace_ppc_irq_set_state("external IRQ", level
);
108 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
110 case PPC6xx_INPUT_SMI
:
111 /* Level sensitive - active high */
112 trace_ppc_irq_set_state("SMI IRQ", level
);
113 ppc_set_irq(cpu
, PPC_INTERRUPT_SMI
, level
);
115 case PPC6xx_INPUT_MCP
:
116 /* Negative edge sensitive */
117 /* XXX: TODO: actual reaction may depends on HID0 status
118 * 603/604/740/750: check HID0[EMCP]
120 if (cur_level
== 1 && level
== 0) {
121 trace_ppc_irq_set_state("machine check", 1);
122 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
125 case PPC6xx_INPUT_CKSTP_IN
:
126 /* Level sensitive - active low */
127 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
128 /* XXX: Note that the only way to restart the CPU is to reset it */
130 trace_ppc_irq_cpu("stop");
134 case PPC6xx_INPUT_HRESET
:
135 /* Level sensitive - active low */
137 trace_ppc_irq_reset("CPU");
138 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
141 case PPC6xx_INPUT_SRESET
:
142 trace_ppc_irq_set_state("RESET IRQ", level
);
143 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
146 g_assert_not_reached();
149 env
->irq_input_state
|= 1 << pin
;
151 env
->irq_input_state
&= ~(1 << pin
);
155 void ppc6xx_irq_init(PowerPCCPU
*cpu
)
157 qdev_init_gpio_in(DEVICE(cpu
), ppc6xx_set_irq
, PPC6xx_INPUT_NB
);
160 #if defined(TARGET_PPC64)
161 /* PowerPC 970 internal IRQ controller */
162 static void ppc970_set_irq(void *opaque
, int pin
, int level
)
164 PowerPCCPU
*cpu
= opaque
;
165 CPUPPCState
*env
= &cpu
->env
;
168 trace_ppc_irq_set(env
, pin
, level
);
170 cur_level
= (env
->irq_input_state
>> pin
) & 1;
171 /* Don't generate spurious events */
172 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
173 CPUState
*cs
= CPU(cpu
);
176 case PPC970_INPUT_INT
:
177 /* Level sensitive - active high */
178 trace_ppc_irq_set_state("external IRQ", level
);
179 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
181 case PPC970_INPUT_THINT
:
182 /* Level sensitive - active high */
183 trace_ppc_irq_set_state("SMI IRQ", level
);
184 ppc_set_irq(cpu
, PPC_INTERRUPT_THERM
, level
);
186 case PPC970_INPUT_MCP
:
187 /* Negative edge sensitive */
188 /* XXX: TODO: actual reaction may depends on HID0 status
189 * 603/604/740/750: check HID0[EMCP]
191 if (cur_level
== 1 && level
== 0) {
192 trace_ppc_irq_set_state("machine check", 1);
193 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
196 case PPC970_INPUT_CKSTP
:
197 /* Level sensitive - active low */
198 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
200 trace_ppc_irq_cpu("stop");
203 trace_ppc_irq_cpu("restart");
208 case PPC970_INPUT_HRESET
:
209 /* Level sensitive - active low */
211 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
214 case PPC970_INPUT_SRESET
:
215 trace_ppc_irq_set_state("RESET IRQ", level
);
216 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
218 case PPC970_INPUT_TBEN
:
219 trace_ppc_irq_set_state("TBEN IRQ", level
);
223 g_assert_not_reached();
226 env
->irq_input_state
|= 1 << pin
;
228 env
->irq_input_state
&= ~(1 << pin
);
232 void ppc970_irq_init(PowerPCCPU
*cpu
)
234 qdev_init_gpio_in(DEVICE(cpu
), ppc970_set_irq
, PPC970_INPUT_NB
);
237 /* POWER7 internal IRQ controller */
238 static void power7_set_irq(void *opaque
, int pin
, int level
)
240 PowerPCCPU
*cpu
= opaque
;
242 trace_ppc_irq_set(&cpu
->env
, pin
, level
);
245 case POWER7_INPUT_INT
:
246 /* Level sensitive - active high */
247 trace_ppc_irq_set_state("external IRQ", level
);
248 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
251 g_assert_not_reached();
255 void ppcPOWER7_irq_init(PowerPCCPU
*cpu
)
257 qdev_init_gpio_in(DEVICE(cpu
), power7_set_irq
, POWER7_INPUT_NB
);
260 /* POWER9 internal IRQ controller */
261 static void power9_set_irq(void *opaque
, int pin
, int level
)
263 PowerPCCPU
*cpu
= opaque
;
265 trace_ppc_irq_set(&cpu
->env
, pin
, level
);
268 case POWER9_INPUT_INT
:
269 /* Level sensitive - active high */
270 trace_ppc_irq_set_state("external IRQ", level
);
271 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
273 case POWER9_INPUT_HINT
:
274 /* Level sensitive - active high */
275 trace_ppc_irq_set_state("HV external IRQ", level
);
276 ppc_set_irq(cpu
, PPC_INTERRUPT_HVIRT
, level
);
279 g_assert_not_reached();
284 void ppcPOWER9_irq_init(PowerPCCPU
*cpu
)
286 qdev_init_gpio_in(DEVICE(cpu
), power9_set_irq
, POWER9_INPUT_NB
);
288 #endif /* defined(TARGET_PPC64) */
290 void ppc40x_core_reset(PowerPCCPU
*cpu
)
292 CPUPPCState
*env
= &cpu
->env
;
295 qemu_log_mask(CPU_LOG_RESET
, "Reset PowerPC core\n");
296 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
297 dbsr
= env
->spr
[SPR_40x_DBSR
];
300 env
->spr
[SPR_40x_DBSR
] = dbsr
;
303 void ppc40x_chip_reset(PowerPCCPU
*cpu
)
305 CPUPPCState
*env
= &cpu
->env
;
308 qemu_log_mask(CPU_LOG_RESET
, "Reset PowerPC chip\n");
309 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
310 /* XXX: TODO reset all internal peripherals */
311 dbsr
= env
->spr
[SPR_40x_DBSR
];
314 env
->spr
[SPR_40x_DBSR
] = dbsr
;
317 void ppc40x_system_reset(PowerPCCPU
*cpu
)
319 qemu_log_mask(CPU_LOG_RESET
, "Reset PowerPC system\n");
320 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
323 void store_40x_dbcr0(CPUPPCState
*env
, uint32_t val
)
325 PowerPCCPU
*cpu
= env_archcpu(env
);
327 qemu_mutex_lock_iothread();
329 switch ((val
>> 28) & 0x3) {
335 ppc40x_core_reset(cpu
);
339 ppc40x_chip_reset(cpu
);
343 ppc40x_system_reset(cpu
);
347 qemu_mutex_unlock_iothread();
350 /* PowerPC 40x internal IRQ controller */
351 static void ppc40x_set_irq(void *opaque
, int pin
, int level
)
353 PowerPCCPU
*cpu
= opaque
;
354 CPUPPCState
*env
= &cpu
->env
;
357 trace_ppc_irq_set(env
, pin
, level
);
359 cur_level
= (env
->irq_input_state
>> pin
) & 1;
360 /* Don't generate spurious events */
361 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
362 CPUState
*cs
= CPU(cpu
);
365 case PPC40x_INPUT_RESET_SYS
:
367 trace_ppc_irq_reset("system");
368 ppc40x_system_reset(cpu
);
371 case PPC40x_INPUT_RESET_CHIP
:
373 trace_ppc_irq_reset("chip");
374 ppc40x_chip_reset(cpu
);
377 case PPC40x_INPUT_RESET_CORE
:
378 /* XXX: TODO: update DBSR[MRR] */
380 trace_ppc_irq_reset("core");
381 ppc40x_core_reset(cpu
);
384 case PPC40x_INPUT_CINT
:
385 /* Level sensitive - active high */
386 trace_ppc_irq_set_state("critical IRQ", level
);
387 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
389 case PPC40x_INPUT_INT
:
390 /* Level sensitive - active high */
391 trace_ppc_irq_set_state("external IRQ", level
);
392 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
394 case PPC40x_INPUT_HALT
:
395 /* Level sensitive - active low */
397 trace_ppc_irq_cpu("stop");
400 trace_ppc_irq_cpu("restart");
405 case PPC40x_INPUT_DEBUG
:
406 /* Level sensitive - active high */
407 trace_ppc_irq_set_state("debug pin", level
);
408 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
411 g_assert_not_reached();
414 env
->irq_input_state
|= 1 << pin
;
416 env
->irq_input_state
&= ~(1 << pin
);
420 void ppc40x_irq_init(PowerPCCPU
*cpu
)
422 qdev_init_gpio_in(DEVICE(cpu
), ppc40x_set_irq
, PPC40x_INPUT_NB
);
425 /* PowerPC E500 internal IRQ controller */
426 static void ppce500_set_irq(void *opaque
, int pin
, int level
)
428 PowerPCCPU
*cpu
= opaque
;
429 CPUPPCState
*env
= &cpu
->env
;
432 trace_ppc_irq_set(env
, pin
, level
);
434 cur_level
= (env
->irq_input_state
>> pin
) & 1;
435 /* Don't generate spurious events */
436 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
438 case PPCE500_INPUT_MCK
:
440 trace_ppc_irq_reset("system");
441 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
444 case PPCE500_INPUT_RESET_CORE
:
446 trace_ppc_irq_reset("core");
447 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, level
);
450 case PPCE500_INPUT_CINT
:
451 /* Level sensitive - active high */
452 trace_ppc_irq_set_state("critical IRQ", level
);
453 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
455 case PPCE500_INPUT_INT
:
456 /* Level sensitive - active high */
457 trace_ppc_irq_set_state("core IRQ", level
);
458 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
460 case PPCE500_INPUT_DEBUG
:
461 /* Level sensitive - active high */
462 trace_ppc_irq_set_state("debug pin", level
);
463 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
466 g_assert_not_reached();
469 env
->irq_input_state
|= 1 << pin
;
471 env
->irq_input_state
&= ~(1 << pin
);
475 void ppce500_irq_init(PowerPCCPU
*cpu
)
477 qdev_init_gpio_in(DEVICE(cpu
), ppce500_set_irq
, PPCE500_INPUT_NB
);
480 /* Enable or Disable the E500 EPR capability */
481 void ppce500_set_mpic_proxy(bool enabled
)
486 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
488 cpu
->env
.mpic_proxy
= enabled
;
490 kvmppc_set_mpic_proxy(cpu
, enabled
);
495 /*****************************************************************************/
496 /* PowerPC time base and decrementer emulation */
498 uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
, int64_t tb_offset
)
500 /* TB time in tb periods */
501 return muldiv64(vmclk
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
) + tb_offset
;
504 uint64_t cpu_ppc_load_tbl (CPUPPCState
*env
)
506 ppc_tb_t
*tb_env
= env
->tb_env
;
510 return env
->spr
[SPR_TBL
];
513 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
514 trace_ppc_tb_load(tb
);
519 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState
*env
)
521 ppc_tb_t
*tb_env
= env
->tb_env
;
524 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
525 trace_ppc_tb_load(tb
);
530 uint32_t cpu_ppc_load_tbu (CPUPPCState
*env
)
533 return env
->spr
[SPR_TBU
];
536 return _cpu_ppc_load_tbu(env
);
539 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
540 int64_t *tb_offsetp
, uint64_t value
)
542 *tb_offsetp
= value
-
543 muldiv64(vmclk
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
);
545 trace_ppc_tb_store(value
, *tb_offsetp
);
548 void cpu_ppc_store_tbl (CPUPPCState
*env
, uint32_t value
)
550 ppc_tb_t
*tb_env
= env
->tb_env
;
553 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
554 tb
&= 0xFFFFFFFF00000000ULL
;
555 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
556 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
559 static inline void _cpu_ppc_store_tbu(CPUPPCState
*env
, uint32_t value
)
561 ppc_tb_t
*tb_env
= env
->tb_env
;
564 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
565 tb
&= 0x00000000FFFFFFFFULL
;
566 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
567 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
570 void cpu_ppc_store_tbu (CPUPPCState
*env
, uint32_t value
)
572 _cpu_ppc_store_tbu(env
, value
);
575 uint64_t cpu_ppc_load_atbl (CPUPPCState
*env
)
577 ppc_tb_t
*tb_env
= env
->tb_env
;
580 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
581 trace_ppc_tb_load(tb
);
586 uint32_t cpu_ppc_load_atbu (CPUPPCState
*env
)
588 ppc_tb_t
*tb_env
= env
->tb_env
;
591 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
592 trace_ppc_tb_load(tb
);
597 void cpu_ppc_store_atbl (CPUPPCState
*env
, uint32_t value
)
599 ppc_tb_t
*tb_env
= env
->tb_env
;
602 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
603 tb
&= 0xFFFFFFFF00000000ULL
;
604 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
605 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
608 void cpu_ppc_store_atbu (CPUPPCState
*env
, uint32_t value
)
610 ppc_tb_t
*tb_env
= env
->tb_env
;
613 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
614 tb
&= 0x00000000FFFFFFFFULL
;
615 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
616 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
619 uint64_t cpu_ppc_load_vtb(CPUPPCState
*env
)
621 ppc_tb_t
*tb_env
= env
->tb_env
;
623 return cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
627 void cpu_ppc_store_vtb(CPUPPCState
*env
, uint64_t value
)
629 ppc_tb_t
*tb_env
= env
->tb_env
;
631 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
632 &tb_env
->vtb_offset
, value
);
635 void cpu_ppc_store_tbu40(CPUPPCState
*env
, uint64_t value
)
637 ppc_tb_t
*tb_env
= env
->tb_env
;
640 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
643 tb
|= (value
& ~0xFFFFFFUL
);
644 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
645 &tb_env
->tb_offset
, tb
);
648 static void cpu_ppc_tb_stop (CPUPPCState
*env
)
650 ppc_tb_t
*tb_env
= env
->tb_env
;
651 uint64_t tb
, atb
, vmclk
;
653 /* If the time base is already frozen, do nothing */
654 if (tb_env
->tb_freq
!= 0) {
655 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
656 /* Get the time base */
657 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
658 /* Get the alternate time base */
659 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
660 /* Store the time base value (ie compute the current offset) */
661 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
662 /* Store the alternate time base value (compute the current offset) */
663 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
664 /* Set the time base frequency to zero */
666 /* Now, the time bases are frozen to tb_offset / atb_offset value */
670 static void cpu_ppc_tb_start (CPUPPCState
*env
)
672 ppc_tb_t
*tb_env
= env
->tb_env
;
673 uint64_t tb
, atb
, vmclk
;
675 /* If the time base is not frozen, do nothing */
676 if (tb_env
->tb_freq
== 0) {
677 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
678 /* Get the time base from tb_offset */
679 tb
= tb_env
->tb_offset
;
680 /* Get the alternate time base from atb_offset */
681 atb
= tb_env
->atb_offset
;
682 /* Restore the tb frequency from the decrementer frequency */
683 tb_env
->tb_freq
= tb_env
->decr_freq
;
684 /* Store the time base value */
685 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
686 /* Store the alternate time base value */
687 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
691 bool ppc_decr_clear_on_delivery(CPUPPCState
*env
)
693 ppc_tb_t
*tb_env
= env
->tb_env
;
694 int flags
= PPC_DECR_UNDERFLOW_TRIGGERED
| PPC_DECR_UNDERFLOW_LEVEL
;
695 return ((tb_env
->flags
& flags
) == PPC_DECR_UNDERFLOW_TRIGGERED
);
698 static inline int64_t _cpu_ppc_load_decr(CPUPPCState
*env
, uint64_t next
)
700 ppc_tb_t
*tb_env
= env
->tb_env
;
703 diff
= next
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
705 decr
= muldiv64(diff
, tb_env
->decr_freq
, NANOSECONDS_PER_SECOND
);
706 } else if (tb_env
->flags
& PPC_TIMER_BOOKE
) {
709 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, NANOSECONDS_PER_SECOND
);
711 trace_ppc_decr_load(decr
);
716 target_ulong
cpu_ppc_load_decr(CPUPPCState
*env
)
718 ppc_tb_t
*tb_env
= env
->tb_env
;
722 return env
->spr
[SPR_DECR
];
725 decr
= _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
728 * If large decrementer is enabled then the decrementer is signed extened
729 * to 64 bits, otherwise it is a 32 bit value.
731 if (env
->spr
[SPR_LPCR
] & LPCR_LD
) {
734 return (uint32_t) decr
;
737 target_ulong
cpu_ppc_load_hdecr(CPUPPCState
*env
)
739 PowerPCCPU
*cpu
= env_archcpu(env
);
740 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
741 ppc_tb_t
*tb_env
= env
->tb_env
;
744 hdecr
= _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
747 * If we have a large decrementer (POWER9 or later) then hdecr is sign
748 * extended to 64 bits, otherwise it is 32 bits.
750 if (pcc
->lrg_decr_bits
> 32) {
753 return (uint32_t) hdecr
;
756 uint64_t cpu_ppc_load_purr (CPUPPCState
*env
)
758 ppc_tb_t
*tb_env
= env
->tb_env
;
760 return cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
761 tb_env
->purr_offset
);
764 /* When decrementer expires,
765 * all we need to do is generate or queue a CPU exception
767 static inline void cpu_ppc_decr_excp(PowerPCCPU
*cpu
)
770 trace_ppc_decr_excp("raise");
771 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 1);
774 static inline void cpu_ppc_decr_lower(PowerPCCPU
*cpu
)
776 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 0);
779 static inline void cpu_ppc_hdecr_excp(PowerPCCPU
*cpu
)
781 CPUPPCState
*env
= &cpu
->env
;
784 trace_ppc_decr_excp("raise HV");
786 /* The architecture specifies that we don't deliver HDEC
787 * interrupts in a PM state. Not only they don't cause a
788 * wakeup but they also get effectively discarded.
790 if (!env
->resume_as_sreset
) {
791 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 1);
795 static inline void cpu_ppc_hdecr_lower(PowerPCCPU
*cpu
)
797 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 0);
800 static void __cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint64_t *nextp
,
802 void (*raise_excp
)(void *),
803 void (*lower_excp
)(PowerPCCPU
*),
804 target_ulong decr
, target_ulong value
,
807 CPUPPCState
*env
= &cpu
->env
;
808 ppc_tb_t
*tb_env
= env
->tb_env
;
810 int64_t signed_value
;
813 /* Truncate value to decr_width and sign extend for simplicity */
814 signed_value
= sextract64(value
, 0, nr_bits
);
815 signed_decr
= sextract64(decr
, 0, nr_bits
);
817 trace_ppc_decr_store(nr_bits
, decr
, value
);
820 /* KVM handles decrementer exceptions, we don't need our own timer */
825 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
828 * If we get a really small DEC value, we can assume that by the time we
829 * handled it we should inject an interrupt already.
831 * On MSB level based DEC implementations the MSB always means the interrupt
832 * is pending, so raise it on those.
834 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
835 * an edge interrupt, so raise it here too.
838 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
) && signed_value
< 0) ||
839 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_TRIGGERED
) && signed_value
< 0
840 && signed_decr
>= 0)) {
845 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
846 if (signed_value
>= 0 && (tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
)) {
850 /* Calculate the next timer event */
851 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
852 next
= now
+ muldiv64(value
, NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
856 timer_mod(timer
, next
);
859 static inline void _cpu_ppc_store_decr(PowerPCCPU
*cpu
, target_ulong decr
,
860 target_ulong value
, int nr_bits
)
862 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
864 __cpu_ppc_store_decr(cpu
, &tb_env
->decr_next
, tb_env
->decr_timer
,
865 tb_env
->decr_timer
->cb
, &cpu_ppc_decr_lower
, decr
,
869 void cpu_ppc_store_decr(CPUPPCState
*env
, target_ulong value
)
871 PowerPCCPU
*cpu
= env_archcpu(env
);
872 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
875 if (env
->spr
[SPR_LPCR
] & LPCR_LD
) {
876 nr_bits
= pcc
->lrg_decr_bits
;
879 _cpu_ppc_store_decr(cpu
, cpu_ppc_load_decr(env
), value
, nr_bits
);
882 static void cpu_ppc_decr_cb(void *opaque
)
884 PowerPCCPU
*cpu
= opaque
;
886 cpu_ppc_decr_excp(cpu
);
889 static inline void _cpu_ppc_store_hdecr(PowerPCCPU
*cpu
, target_ulong hdecr
,
890 target_ulong value
, int nr_bits
)
892 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
894 if (tb_env
->hdecr_timer
!= NULL
) {
895 __cpu_ppc_store_decr(cpu
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
896 tb_env
->hdecr_timer
->cb
, &cpu_ppc_hdecr_lower
,
897 hdecr
, value
, nr_bits
);
901 void cpu_ppc_store_hdecr(CPUPPCState
*env
, target_ulong value
)
903 PowerPCCPU
*cpu
= env_archcpu(env
);
904 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
906 _cpu_ppc_store_hdecr(cpu
, cpu_ppc_load_hdecr(env
), value
,
910 static void cpu_ppc_hdecr_cb(void *opaque
)
912 PowerPCCPU
*cpu
= opaque
;
914 cpu_ppc_hdecr_excp(cpu
);
917 void cpu_ppc_store_purr(CPUPPCState
*env
, uint64_t value
)
919 ppc_tb_t
*tb_env
= env
->tb_env
;
921 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
922 &tb_env
->purr_offset
, value
);
925 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
927 CPUPPCState
*env
= opaque
;
928 PowerPCCPU
*cpu
= env_archcpu(env
);
929 ppc_tb_t
*tb_env
= env
->tb_env
;
931 tb_env
->tb_freq
= freq
;
932 tb_env
->decr_freq
= freq
;
933 /* There is a bug in Linux 2.4 kernels:
934 * if a decrementer exception is pending when it enables msr_ee at startup,
935 * it's not ready to handle it...
937 _cpu_ppc_store_decr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF, 32);
938 _cpu_ppc_store_hdecr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF, 32);
939 cpu_ppc_store_purr(env
, 0x0000000000000000ULL
);
942 static void timebase_save(PPCTimebase
*tb
)
944 uint64_t ticks
= cpu_get_host_ticks();
945 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
947 if (!first_ppc_cpu
->env
.tb_env
) {
948 error_report("No timebase object");
952 /* not used anymore, we keep it for compatibility */
953 tb
->time_of_the_day_ns
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
955 * tb_offset is only expected to be changed by QEMU so
956 * there is no need to update it from KVM here
958 tb
->guest_timebase
= ticks
+ first_ppc_cpu
->env
.tb_env
->tb_offset
;
960 tb
->runstate_paused
=
961 runstate_check(RUN_STATE_PAUSED
) || runstate_check(RUN_STATE_SAVE_VM
);
964 static void timebase_load(PPCTimebase
*tb
)
967 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
968 int64_t tb_off_adj
, tb_off
;
971 if (!first_ppc_cpu
->env
.tb_env
) {
972 error_report("No timebase object");
976 freq
= first_ppc_cpu
->env
.tb_env
->tb_freq
;
978 tb_off_adj
= tb
->guest_timebase
- cpu_get_host_ticks();
980 tb_off
= first_ppc_cpu
->env
.tb_env
->tb_offset
;
981 trace_ppc_tb_adjust(tb_off
, tb_off_adj
, tb_off_adj
- tb_off
,
982 (tb_off_adj
- tb_off
) / freq
);
984 /* Set new offset to all CPUs */
986 PowerPCCPU
*pcpu
= POWERPC_CPU(cpu
);
987 pcpu
->env
.tb_env
->tb_offset
= tb_off_adj
;
988 kvmppc_set_reg_tb_offset(pcpu
, pcpu
->env
.tb_env
->tb_offset
);
992 void cpu_ppc_clock_vm_state_change(void *opaque
, bool running
,
995 PPCTimebase
*tb
= opaque
;
1005 * When migrating a running guest, read the clock just
1006 * before migration, so that the guest clock counts
1007 * during the events between:
1013 * This reduces clock difference on migration from 5s
1014 * to 0.1s (when max_downtime == 5s), because sending the
1015 * final pages of memory (which happens between vm_stop()
1016 * and pre_save()) takes max_downtime.
1018 static int timebase_pre_save(void *opaque
)
1020 PPCTimebase
*tb
= opaque
;
1022 /* guest_timebase won't be overridden in case of paused guest or savevm */
1023 if (!tb
->runstate_paused
) {
1030 const VMStateDescription vmstate_ppc_timebase
= {
1033 .minimum_version_id
= 1,
1034 .pre_save
= timebase_pre_save
,
1035 .fields
= (VMStateField
[]) {
1036 VMSTATE_UINT64(guest_timebase
, PPCTimebase
),
1037 VMSTATE_INT64(time_of_the_day_ns
, PPCTimebase
),
1038 VMSTATE_END_OF_LIST()
1042 /* Set up (once) timebase frequency (in Hz) */
1043 clk_setup_cb
cpu_ppc_tb_init (CPUPPCState
*env
, uint32_t freq
)
1045 PowerPCCPU
*cpu
= env_archcpu(env
);
1048 tb_env
= g_new0(ppc_tb_t
, 1);
1049 env
->tb_env
= tb_env
;
1050 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1051 if (is_book3s_arch2x(env
)) {
1052 /* All Book3S 64bit CPUs implement level based DEC logic */
1053 tb_env
->flags
|= PPC_DECR_UNDERFLOW_LEVEL
;
1055 /* Create new timer */
1056 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_decr_cb
, cpu
);
1057 if (env
->has_hv_mode
&& !cpu
->vhyp
) {
1058 tb_env
->hdecr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_hdecr_cb
,
1061 tb_env
->hdecr_timer
= NULL
;
1063 cpu_ppc_set_tb_clk(env
, freq
);
1065 return &cpu_ppc_set_tb_clk
;
1068 void cpu_ppc_tb_free(CPUPPCState
*env
)
1070 timer_free(env
->tb_env
->decr_timer
);
1071 timer_free(env
->tb_env
->hdecr_timer
);
1072 g_free(env
->tb_env
);
1075 /* cpu_ppc_hdecr_init may be used if the timer is not used by HDEC emulation */
1076 void cpu_ppc_hdecr_init(CPUPPCState
*env
)
1078 PowerPCCPU
*cpu
= env_archcpu(env
);
1080 assert(env
->tb_env
->hdecr_timer
== NULL
);
1082 env
->tb_env
->hdecr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
1083 &cpu_ppc_hdecr_cb
, cpu
);
1086 void cpu_ppc_hdecr_exit(CPUPPCState
*env
)
1088 PowerPCCPU
*cpu
= env_archcpu(env
);
1090 timer_free(env
->tb_env
->hdecr_timer
);
1091 env
->tb_env
->hdecr_timer
= NULL
;
1093 cpu_ppc_hdecr_lower(cpu
);
1096 /*****************************************************************************/
1097 /* PowerPC 40x timers */
1099 /* PIT, FIT & WDT */
1100 typedef struct ppc40x_timer_t ppc40x_timer_t
;
1101 struct ppc40x_timer_t
{
1102 uint64_t pit_reload
; /* PIT auto-reload value */
1103 uint64_t fit_next
; /* Tick for next FIT interrupt */
1104 QEMUTimer
*fit_timer
;
1105 uint64_t wdt_next
; /* Tick for next WDT interrupt */
1106 QEMUTimer
*wdt_timer
;
1108 /* 405 have the PIT, 440 have a DECR. */
1109 unsigned int decr_excp
;
1112 /* Fixed interval timer */
1113 static void cpu_4xx_fit_cb (void *opaque
)
1115 PowerPCCPU
*cpu
= opaque
;
1116 CPUPPCState
*env
= &cpu
->env
;
1118 ppc40x_timer_t
*ppc40x_timer
;
1121 tb_env
= env
->tb_env
;
1122 ppc40x_timer
= tb_env
->opaque
;
1123 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1124 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
1138 /* Cannot occur, but makes gcc happy */
1141 next
= now
+ muldiv64(next
, NANOSECONDS_PER_SECOND
, tb_env
->tb_freq
);
1144 timer_mod(ppc40x_timer
->fit_timer
, next
);
1145 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
1146 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1) {
1147 ppc_set_irq(cpu
, PPC_INTERRUPT_FIT
, 1);
1149 trace_ppc4xx_fit((int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
1150 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1153 /* Programmable interval timer */
1154 static void start_stop_pit (CPUPPCState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
1156 ppc40x_timer_t
*ppc40x_timer
;
1159 ppc40x_timer
= tb_env
->opaque
;
1160 if (ppc40x_timer
->pit_reload
<= 1 ||
1161 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
1162 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
1164 trace_ppc4xx_pit_stop();
1165 timer_del(tb_env
->decr_timer
);
1167 trace_ppc4xx_pit_start(ppc40x_timer
->pit_reload
);
1168 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1169 next
= now
+ muldiv64(ppc40x_timer
->pit_reload
,
1170 NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
1172 next
+= tb_env
->decr_next
- now
;
1175 timer_mod(tb_env
->decr_timer
, next
);
1176 tb_env
->decr_next
= next
;
1180 static void cpu_4xx_pit_cb (void *opaque
)
1182 PowerPCCPU
*cpu
= opaque
;
1183 CPUPPCState
*env
= &cpu
->env
;
1185 ppc40x_timer_t
*ppc40x_timer
;
1187 tb_env
= env
->tb_env
;
1188 ppc40x_timer
= tb_env
->opaque
;
1189 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
1190 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) {
1191 ppc_set_irq(cpu
, ppc40x_timer
->decr_excp
, 1);
1193 start_stop_pit(env
, tb_env
, 1);
1194 trace_ppc4xx_pit((int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
1195 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
1196 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
1197 ppc40x_timer
->pit_reload
);
1200 /* Watchdog timer */
1201 static void cpu_4xx_wdt_cb (void *opaque
)
1203 PowerPCCPU
*cpu
= opaque
;
1204 CPUPPCState
*env
= &cpu
->env
;
1206 ppc40x_timer_t
*ppc40x_timer
;
1209 tb_env
= env
->tb_env
;
1210 ppc40x_timer
= tb_env
->opaque
;
1211 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1212 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
1226 /* Cannot occur, but makes gcc happy */
1229 next
= now
+ muldiv64(next
, NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
1232 trace_ppc4xx_wdt(env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1233 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
1236 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1237 ppc40x_timer
->wdt_next
= next
;
1238 env
->spr
[SPR_40x_TSR
] |= 1U << 31;
1241 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1242 ppc40x_timer
->wdt_next
= next
;
1243 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
1244 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1) {
1245 ppc_set_irq(cpu
, PPC_INTERRUPT_WDT
, 1);
1249 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
1250 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
1251 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
1255 case 0x1: /* Core reset */
1256 ppc40x_core_reset(cpu
);
1258 case 0x2: /* Chip reset */
1259 ppc40x_chip_reset(cpu
);
1261 case 0x3: /* System reset */
1262 ppc40x_system_reset(cpu
);
1268 void store_40x_pit (CPUPPCState
*env
, target_ulong val
)
1271 ppc40x_timer_t
*ppc40x_timer
;
1273 tb_env
= env
->tb_env
;
1274 ppc40x_timer
= tb_env
->opaque
;
1275 trace_ppc40x_store_pit(val
);
1276 ppc40x_timer
->pit_reload
= val
;
1277 start_stop_pit(env
, tb_env
, 0);
1280 target_ulong
load_40x_pit (CPUPPCState
*env
)
1282 return cpu_ppc_load_decr(env
);
1285 void store_40x_tsr(CPUPPCState
*env
, target_ulong val
)
1287 PowerPCCPU
*cpu
= env_archcpu(env
);
1289 trace_ppc40x_store_tcr(val
);
1291 env
->spr
[SPR_40x_TSR
] &= ~(val
& 0xFC000000);
1292 if (val
& 0x80000000) {
1293 ppc_set_irq(cpu
, PPC_INTERRUPT_PIT
, 0);
1297 void store_40x_tcr(CPUPPCState
*env
, target_ulong val
)
1299 PowerPCCPU
*cpu
= env_archcpu(env
);
1302 trace_ppc40x_store_tsr(val
);
1304 tb_env
= env
->tb_env
;
1305 env
->spr
[SPR_40x_TCR
] = val
& 0xFFC00000;
1306 start_stop_pit(env
, tb_env
, 1);
1307 cpu_4xx_wdt_cb(cpu
);
1310 static void ppc_40x_set_tb_clk (void *opaque
, uint32_t freq
)
1312 CPUPPCState
*env
= opaque
;
1313 ppc_tb_t
*tb_env
= env
->tb_env
;
1315 trace_ppc40x_set_tb_clk(freq
);
1316 tb_env
->tb_freq
= freq
;
1317 tb_env
->decr_freq
= freq
;
1318 /* XXX: we should also update all timers */
1321 clk_setup_cb
ppc_40x_timers_init (CPUPPCState
*env
, uint32_t freq
,
1322 unsigned int decr_excp
)
1325 ppc40x_timer_t
*ppc40x_timer
;
1326 PowerPCCPU
*cpu
= env_archcpu(env
);
1328 trace_ppc40x_timers_init(freq
);
1330 tb_env
= g_new0(ppc_tb_t
, 1);
1331 ppc40x_timer
= g_new0(ppc40x_timer_t
, 1);
1333 env
->tb_env
= tb_env
;
1334 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1335 tb_env
->tb_freq
= freq
;
1336 tb_env
->decr_freq
= freq
;
1337 tb_env
->opaque
= ppc40x_timer
;
1339 /* We use decr timer for PIT */
1340 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_pit_cb
, cpu
);
1341 ppc40x_timer
->fit_timer
=
1342 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_fit_cb
, cpu
);
1343 ppc40x_timer
->wdt_timer
=
1344 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_wdt_cb
, cpu
);
1345 ppc40x_timer
->decr_excp
= decr_excp
;
1347 return &ppc_40x_set_tb_clk
;
1350 /*****************************************************************************/
1351 /* Embedded PowerPC Device Control Registers */
1352 typedef struct ppc_dcrn_t ppc_dcrn_t
;
1354 dcr_read_cb dcr_read
;
1355 dcr_write_cb dcr_write
;
1359 /* XXX: on 460, DCR addresses are 32 bits wide,
1360 * using DCRIPR to get the 22 upper bits of the DCR address
1362 #define DCRN_NB 1024
1364 ppc_dcrn_t dcrn
[DCRN_NB
];
1365 int (*read_error
)(int dcrn
);
1366 int (*write_error
)(int dcrn
);
1369 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1373 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1375 dcr
= &dcr_env
->dcrn
[dcrn
];
1376 if (dcr
->dcr_read
== NULL
)
1378 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1379 trace_ppc_dcr_read(dcrn
, *valp
);
1384 if (dcr_env
->read_error
!= NULL
)
1385 return (*dcr_env
->read_error
)(dcrn
);
1390 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1394 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1396 dcr
= &dcr_env
->dcrn
[dcrn
];
1397 if (dcr
->dcr_write
== NULL
)
1399 trace_ppc_dcr_write(dcrn
, val
);
1400 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1405 if (dcr_env
->write_error
!= NULL
)
1406 return (*dcr_env
->write_error
)(dcrn
);
1411 int ppc_dcr_register (CPUPPCState
*env
, int dcrn
, void *opaque
,
1412 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1417 dcr_env
= env
->dcr_env
;
1418 if (dcr_env
== NULL
)
1420 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1422 dcr
= &dcr_env
->dcrn
[dcrn
];
1423 if (dcr
->opaque
!= NULL
||
1424 dcr
->dcr_read
!= NULL
||
1425 dcr
->dcr_write
!= NULL
)
1427 dcr
->opaque
= opaque
;
1428 dcr
->dcr_read
= dcr_read
;
1429 dcr
->dcr_write
= dcr_write
;
1434 int ppc_dcr_init (CPUPPCState
*env
, int (*read_error
)(int dcrn
),
1435 int (*write_error
)(int dcrn
))
1439 dcr_env
= g_new0(ppc_dcr_t
, 1);
1440 dcr_env
->read_error
= read_error
;
1441 dcr_env
->write_error
= write_error
;
1442 env
->dcr_env
= dcr_env
;
1447 /*****************************************************************************/
1449 int ppc_cpu_pir(PowerPCCPU
*cpu
)
1451 CPUPPCState
*env
= &cpu
->env
;
1452 return env
->spr_cb
[SPR_PIR
].default_value
;
1455 PowerPCCPU
*ppc_get_vcpu_by_pir(int pir
)
1460 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1462 if (ppc_cpu_pir(cpu
) == pir
) {
1470 void ppc_irq_reset(PowerPCCPU
*cpu
)
1472 CPUPPCState
*env
= &cpu
->env
;
1474 env
->irq_input_state
= 0;
1475 kvmppc_set_interrupt(cpu
, PPC_INTERRUPT_EXT
, 0);