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"
27 #include "hw/ppc/ppc.h"
28 #include "hw/ppc/ppc_e500.h"
29 #include "qemu/timer.h"
30 #include "sysemu/sysemu.h"
31 #include "sysemu/cpus.h"
33 #include "qemu/error-report.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 static void cpu_ppc_tb_stop (CPUPPCState
*env
);
55 static void cpu_ppc_tb_start (CPUPPCState
*env
);
57 void ppc_set_irq(PowerPCCPU
*cpu
, int n_IRQ
, int level
)
59 CPUState
*cs
= CPU(cpu
);
60 CPUPPCState
*env
= &cpu
->env
;
61 unsigned int old_pending
;
64 /* We may already have the BQL if coming from the reset path */
65 if (!qemu_mutex_iothread_locked()) {
67 qemu_mutex_lock_iothread();
70 old_pending
= env
->pending_interrupts
;
73 env
->pending_interrupts
|= 1 << n_IRQ
;
74 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
76 env
->pending_interrupts
&= ~(1 << n_IRQ
);
77 if (env
->pending_interrupts
== 0) {
78 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
82 if (old_pending
!= env
->pending_interrupts
) {
83 kvmppc_set_interrupt(cpu
, n_IRQ
, level
);
87 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
88 "req %08x\n", __func__
, env
, n_IRQ
, level
,
89 env
->pending_interrupts
, CPU(cpu
)->interrupt_request
);
92 qemu_mutex_unlock_iothread();
96 /* PowerPC 6xx / 7xx internal IRQ controller */
97 static void ppc6xx_set_irq(void *opaque
, int pin
, int level
)
99 PowerPCCPU
*cpu
= opaque
;
100 CPUPPCState
*env
= &cpu
->env
;
103 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
105 cur_level
= (env
->irq_input_state
>> pin
) & 1;
106 /* Don't generate spurious events */
107 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
108 CPUState
*cs
= CPU(cpu
);
111 case PPC6xx_INPUT_TBEN
:
112 /* Level sensitive - active high */
113 LOG_IRQ("%s: %s the time base\n",
114 __func__
, level
? "start" : "stop");
116 cpu_ppc_tb_start(env
);
118 cpu_ppc_tb_stop(env
);
120 case PPC6xx_INPUT_INT
:
121 /* Level sensitive - active high */
122 LOG_IRQ("%s: set the external IRQ state to %d\n",
124 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
126 case PPC6xx_INPUT_SMI
:
127 /* Level sensitive - active high */
128 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
130 ppc_set_irq(cpu
, PPC_INTERRUPT_SMI
, level
);
132 case PPC6xx_INPUT_MCP
:
133 /* Negative edge sensitive */
134 /* XXX: TODO: actual reaction may depends on HID0 status
135 * 603/604/740/750: check HID0[EMCP]
137 if (cur_level
== 1 && level
== 0) {
138 LOG_IRQ("%s: raise machine check state\n",
140 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
143 case PPC6xx_INPUT_CKSTP_IN
:
144 /* Level sensitive - active low */
145 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
146 /* XXX: Note that the only way to restart the CPU is to reset it */
148 LOG_IRQ("%s: stop the CPU\n", __func__
);
152 case PPC6xx_INPUT_HRESET
:
153 /* Level sensitive - active low */
155 LOG_IRQ("%s: reset the CPU\n", __func__
);
156 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
159 case PPC6xx_INPUT_SRESET
:
160 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
162 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
165 /* Unknown pin - do nothing */
166 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
170 env
->irq_input_state
|= 1 << pin
;
172 env
->irq_input_state
&= ~(1 << pin
);
176 void ppc6xx_irq_init(PowerPCCPU
*cpu
)
178 CPUPPCState
*env
= &cpu
->env
;
180 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, cpu
,
184 #if defined(TARGET_PPC64)
185 /* PowerPC 970 internal IRQ controller */
186 static void ppc970_set_irq(void *opaque
, int pin
, int level
)
188 PowerPCCPU
*cpu
= opaque
;
189 CPUPPCState
*env
= &cpu
->env
;
192 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
194 cur_level
= (env
->irq_input_state
>> pin
) & 1;
195 /* Don't generate spurious events */
196 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
197 CPUState
*cs
= CPU(cpu
);
200 case PPC970_INPUT_INT
:
201 /* Level sensitive - active high */
202 LOG_IRQ("%s: set the external IRQ state to %d\n",
204 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
206 case PPC970_INPUT_THINT
:
207 /* Level sensitive - active high */
208 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__
,
210 ppc_set_irq(cpu
, PPC_INTERRUPT_THERM
, level
);
212 case PPC970_INPUT_MCP
:
213 /* Negative edge sensitive */
214 /* XXX: TODO: actual reaction may depends on HID0 status
215 * 603/604/740/750: check HID0[EMCP]
217 if (cur_level
== 1 && level
== 0) {
218 LOG_IRQ("%s: raise machine check state\n",
220 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
223 case PPC970_INPUT_CKSTP
:
224 /* Level sensitive - active low */
225 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
227 LOG_IRQ("%s: stop the CPU\n", __func__
);
230 LOG_IRQ("%s: restart the CPU\n", __func__
);
235 case PPC970_INPUT_HRESET
:
236 /* Level sensitive - active low */
238 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
241 case PPC970_INPUT_SRESET
:
242 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
244 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
246 case PPC970_INPUT_TBEN
:
247 LOG_IRQ("%s: set the TBEN state to %d\n", __func__
,
252 /* Unknown pin - do nothing */
253 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
257 env
->irq_input_state
|= 1 << pin
;
259 env
->irq_input_state
&= ~(1 << pin
);
263 void ppc970_irq_init(PowerPCCPU
*cpu
)
265 CPUPPCState
*env
= &cpu
->env
;
267 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, cpu
,
271 /* POWER7 internal IRQ controller */
272 static void power7_set_irq(void *opaque
, int pin
, int level
)
274 PowerPCCPU
*cpu
= opaque
;
275 CPUPPCState
*env
= &cpu
->env
;
277 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
281 case POWER7_INPUT_INT
:
282 /* Level sensitive - active high */
283 LOG_IRQ("%s: set the external IRQ state to %d\n",
285 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
288 /* Unknown pin - do nothing */
289 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
293 env
->irq_input_state
|= 1 << pin
;
295 env
->irq_input_state
&= ~(1 << pin
);
299 void ppcPOWER7_irq_init(PowerPCCPU
*cpu
)
301 CPUPPCState
*env
= &cpu
->env
;
303 env
->irq_inputs
= (void **)qemu_allocate_irqs(&power7_set_irq
, cpu
,
307 /* POWER9 internal IRQ controller */
308 static void power9_set_irq(void *opaque
, int pin
, int level
)
310 PowerPCCPU
*cpu
= opaque
;
311 CPUPPCState
*env
= &cpu
->env
;
313 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
317 case POWER9_INPUT_INT
:
318 /* Level sensitive - active high */
319 LOG_IRQ("%s: set the external IRQ state to %d\n",
321 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
323 case POWER9_INPUT_HINT
:
324 /* Level sensitive - active high */
325 LOG_IRQ("%s: set the external IRQ state to %d\n",
327 ppc_set_irq(cpu
, PPC_INTERRUPT_HVIRT
, level
);
330 /* Unknown pin - do nothing */
331 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
335 env
->irq_input_state
|= 1 << pin
;
337 env
->irq_input_state
&= ~(1 << pin
);
341 void ppcPOWER9_irq_init(PowerPCCPU
*cpu
)
343 CPUPPCState
*env
= &cpu
->env
;
345 env
->irq_inputs
= (void **)qemu_allocate_irqs(&power9_set_irq
, cpu
,
348 #endif /* defined(TARGET_PPC64) */
350 void ppc40x_core_reset(PowerPCCPU
*cpu
)
352 CPUPPCState
*env
= &cpu
->env
;
355 qemu_log_mask(CPU_LOG_RESET
, "Reset PowerPC core\n");
356 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
357 dbsr
= env
->spr
[SPR_40x_DBSR
];
360 env
->spr
[SPR_40x_DBSR
] = dbsr
;
363 void ppc40x_chip_reset(PowerPCCPU
*cpu
)
365 CPUPPCState
*env
= &cpu
->env
;
368 qemu_log_mask(CPU_LOG_RESET
, "Reset PowerPC chip\n");
369 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_RESET
);
370 /* XXX: TODO reset all internal peripherals */
371 dbsr
= env
->spr
[SPR_40x_DBSR
];
374 env
->spr
[SPR_40x_DBSR
] = dbsr
;
377 void ppc40x_system_reset(PowerPCCPU
*cpu
)
379 qemu_log_mask(CPU_LOG_RESET
, "Reset PowerPC system\n");
380 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
383 void store_40x_dbcr0(CPUPPCState
*env
, uint32_t val
)
385 PowerPCCPU
*cpu
= env_archcpu(env
);
387 switch ((val
>> 28) & 0x3) {
393 ppc40x_core_reset(cpu
);
397 ppc40x_chip_reset(cpu
);
401 ppc40x_system_reset(cpu
);
406 /* PowerPC 40x internal IRQ controller */
407 static void ppc40x_set_irq(void *opaque
, int pin
, int level
)
409 PowerPCCPU
*cpu
= opaque
;
410 CPUPPCState
*env
= &cpu
->env
;
413 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
415 cur_level
= (env
->irq_input_state
>> pin
) & 1;
416 /* Don't generate spurious events */
417 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
418 CPUState
*cs
= CPU(cpu
);
421 case PPC40x_INPUT_RESET_SYS
:
423 LOG_IRQ("%s: reset the PowerPC system\n",
425 ppc40x_system_reset(cpu
);
428 case PPC40x_INPUT_RESET_CHIP
:
430 LOG_IRQ("%s: reset the PowerPC chip\n", __func__
);
431 ppc40x_chip_reset(cpu
);
434 case PPC40x_INPUT_RESET_CORE
:
435 /* XXX: TODO: update DBSR[MRR] */
437 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
438 ppc40x_core_reset(cpu
);
441 case PPC40x_INPUT_CINT
:
442 /* Level sensitive - active high */
443 LOG_IRQ("%s: set the critical IRQ state to %d\n",
445 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
447 case PPC40x_INPUT_INT
:
448 /* Level sensitive - active high */
449 LOG_IRQ("%s: set the external IRQ state to %d\n",
451 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
453 case PPC40x_INPUT_HALT
:
454 /* Level sensitive - active low */
456 LOG_IRQ("%s: stop the CPU\n", __func__
);
459 LOG_IRQ("%s: restart the CPU\n", __func__
);
464 case PPC40x_INPUT_DEBUG
:
465 /* Level sensitive - active high */
466 LOG_IRQ("%s: set the debug pin state to %d\n",
468 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
471 /* Unknown pin - do nothing */
472 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
476 env
->irq_input_state
|= 1 << pin
;
478 env
->irq_input_state
&= ~(1 << pin
);
482 void ppc40x_irq_init(PowerPCCPU
*cpu
)
484 CPUPPCState
*env
= &cpu
->env
;
486 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc40x_set_irq
,
487 cpu
, PPC40x_INPUT_NB
);
490 /* PowerPC E500 internal IRQ controller */
491 static void ppce500_set_irq(void *opaque
, int pin
, int level
)
493 PowerPCCPU
*cpu
= opaque
;
494 CPUPPCState
*env
= &cpu
->env
;
497 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
499 cur_level
= (env
->irq_input_state
>> pin
) & 1;
500 /* Don't generate spurious events */
501 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
503 case PPCE500_INPUT_MCK
:
505 LOG_IRQ("%s: reset the PowerPC system\n",
507 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
510 case PPCE500_INPUT_RESET_CORE
:
512 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
513 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, level
);
516 case PPCE500_INPUT_CINT
:
517 /* Level sensitive - active high */
518 LOG_IRQ("%s: set the critical IRQ state to %d\n",
520 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
522 case PPCE500_INPUT_INT
:
523 /* Level sensitive - active high */
524 LOG_IRQ("%s: set the core IRQ state to %d\n",
526 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
528 case PPCE500_INPUT_DEBUG
:
529 /* Level sensitive - active high */
530 LOG_IRQ("%s: set the debug pin state to %d\n",
532 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
535 /* Unknown pin - do nothing */
536 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
540 env
->irq_input_state
|= 1 << pin
;
542 env
->irq_input_state
&= ~(1 << pin
);
546 void ppce500_irq_init(PowerPCCPU
*cpu
)
548 CPUPPCState
*env
= &cpu
->env
;
550 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppce500_set_irq
,
551 cpu
, PPCE500_INPUT_NB
);
554 /* Enable or Disable the E500 EPR capability */
555 void ppce500_set_mpic_proxy(bool enabled
)
560 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
562 cpu
->env
.mpic_proxy
= enabled
;
564 kvmppc_set_mpic_proxy(cpu
, enabled
);
569 /*****************************************************************************/
570 /* PowerPC time base and decrementer emulation */
572 uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
, int64_t tb_offset
)
574 /* TB time in tb periods */
575 return muldiv64(vmclk
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
) + tb_offset
;
578 uint64_t cpu_ppc_load_tbl (CPUPPCState
*env
)
580 ppc_tb_t
*tb_env
= env
->tb_env
;
584 return env
->spr
[SPR_TBL
];
587 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
588 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
593 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState
*env
)
595 ppc_tb_t
*tb_env
= env
->tb_env
;
598 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
599 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
604 uint32_t cpu_ppc_load_tbu (CPUPPCState
*env
)
607 return env
->spr
[SPR_TBU
];
610 return _cpu_ppc_load_tbu(env
);
613 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
614 int64_t *tb_offsetp
, uint64_t value
)
616 *tb_offsetp
= value
-
617 muldiv64(vmclk
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
);
619 LOG_TB("%s: tb %016" PRIx64
" offset %08" PRIx64
"\n",
620 __func__
, value
, *tb_offsetp
);
623 void cpu_ppc_store_tbl (CPUPPCState
*env
, uint32_t value
)
625 ppc_tb_t
*tb_env
= env
->tb_env
;
628 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
629 tb
&= 0xFFFFFFFF00000000ULL
;
630 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
631 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
634 static inline void _cpu_ppc_store_tbu(CPUPPCState
*env
, uint32_t value
)
636 ppc_tb_t
*tb_env
= env
->tb_env
;
639 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
640 tb
&= 0x00000000FFFFFFFFULL
;
641 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
642 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
645 void cpu_ppc_store_tbu (CPUPPCState
*env
, uint32_t value
)
647 _cpu_ppc_store_tbu(env
, value
);
650 uint64_t cpu_ppc_load_atbl (CPUPPCState
*env
)
652 ppc_tb_t
*tb_env
= env
->tb_env
;
655 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
656 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
661 uint32_t cpu_ppc_load_atbu (CPUPPCState
*env
)
663 ppc_tb_t
*tb_env
= env
->tb_env
;
666 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
667 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
672 void cpu_ppc_store_atbl (CPUPPCState
*env
, uint32_t value
)
674 ppc_tb_t
*tb_env
= env
->tb_env
;
677 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
678 tb
&= 0xFFFFFFFF00000000ULL
;
679 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
680 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
683 void cpu_ppc_store_atbu (CPUPPCState
*env
, uint32_t value
)
685 ppc_tb_t
*tb_env
= env
->tb_env
;
688 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
689 tb
&= 0x00000000FFFFFFFFULL
;
690 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
691 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
694 static void cpu_ppc_tb_stop (CPUPPCState
*env
)
696 ppc_tb_t
*tb_env
= env
->tb_env
;
697 uint64_t tb
, atb
, vmclk
;
699 /* If the time base is already frozen, do nothing */
700 if (tb_env
->tb_freq
!= 0) {
701 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
702 /* Get the time base */
703 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
704 /* Get the alternate time base */
705 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
706 /* Store the time base value (ie compute the current offset) */
707 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
708 /* Store the alternate time base value (compute the current offset) */
709 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
710 /* Set the time base frequency to zero */
712 /* Now, the time bases are frozen to tb_offset / atb_offset value */
716 static void cpu_ppc_tb_start (CPUPPCState
*env
)
718 ppc_tb_t
*tb_env
= env
->tb_env
;
719 uint64_t tb
, atb
, vmclk
;
721 /* If the time base is not frozen, do nothing */
722 if (tb_env
->tb_freq
== 0) {
723 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
724 /* Get the time base from tb_offset */
725 tb
= tb_env
->tb_offset
;
726 /* Get the alternate time base from atb_offset */
727 atb
= tb_env
->atb_offset
;
728 /* Restore the tb frequency from the decrementer frequency */
729 tb_env
->tb_freq
= tb_env
->decr_freq
;
730 /* Store the time base value */
731 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
732 /* Store the alternate time base value */
733 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
737 bool ppc_decr_clear_on_delivery(CPUPPCState
*env
)
739 ppc_tb_t
*tb_env
= env
->tb_env
;
740 int flags
= PPC_DECR_UNDERFLOW_TRIGGERED
| PPC_DECR_UNDERFLOW_LEVEL
;
741 return ((tb_env
->flags
& flags
) == PPC_DECR_UNDERFLOW_TRIGGERED
);
744 static inline int64_t _cpu_ppc_load_decr(CPUPPCState
*env
, uint64_t next
)
746 ppc_tb_t
*tb_env
= env
->tb_env
;
749 diff
= next
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
751 decr
= muldiv64(diff
, tb_env
->decr_freq
, NANOSECONDS_PER_SECOND
);
752 } else if (tb_env
->flags
& PPC_TIMER_BOOKE
) {
755 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, NANOSECONDS_PER_SECOND
);
757 LOG_TB("%s: %016" PRIx64
"\n", __func__
, decr
);
762 target_ulong
cpu_ppc_load_decr(CPUPPCState
*env
)
764 ppc_tb_t
*tb_env
= env
->tb_env
;
768 return env
->spr
[SPR_DECR
];
771 decr
= _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
774 * If large decrementer is enabled then the decrementer is signed extened
775 * to 64 bits, otherwise it is a 32 bit value.
777 if (env
->spr
[SPR_LPCR
] & LPCR_LD
) {
780 return (uint32_t) decr
;
783 target_ulong
cpu_ppc_load_hdecr(CPUPPCState
*env
)
785 PowerPCCPU
*cpu
= env_archcpu(env
);
786 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
787 ppc_tb_t
*tb_env
= env
->tb_env
;
790 hdecr
= _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
793 * If we have a large decrementer (POWER9 or later) then hdecr is sign
794 * extended to 64 bits, otherwise it is 32 bits.
796 if (pcc
->lrg_decr_bits
> 32) {
799 return (uint32_t) hdecr
;
802 uint64_t cpu_ppc_load_purr (CPUPPCState
*env
)
804 ppc_tb_t
*tb_env
= env
->tb_env
;
807 diff
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - tb_env
->purr_start
;
809 return tb_env
->purr_load
+
810 muldiv64(diff
, tb_env
->tb_freq
, NANOSECONDS_PER_SECOND
);
813 /* When decrementer expires,
814 * all we need to do is generate or queue a CPU exception
816 static inline void cpu_ppc_decr_excp(PowerPCCPU
*cpu
)
819 LOG_TB("raise decrementer exception\n");
820 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 1);
823 static inline void cpu_ppc_decr_lower(PowerPCCPU
*cpu
)
825 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 0);
828 static inline void cpu_ppc_hdecr_excp(PowerPCCPU
*cpu
)
830 CPUPPCState
*env
= &cpu
->env
;
833 LOG_TB("raise hv decrementer exception\n");
835 /* The architecture specifies that we don't deliver HDEC
836 * interrupts in a PM state. Not only they don't cause a
837 * wakeup but they also get effectively discarded.
839 if (!env
->resume_as_sreset
) {
840 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 1);
844 static inline void cpu_ppc_hdecr_lower(PowerPCCPU
*cpu
)
846 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 0);
849 static void __cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint64_t *nextp
,
851 void (*raise_excp
)(void *),
852 void (*lower_excp
)(PowerPCCPU
*),
853 target_ulong decr
, target_ulong value
,
856 CPUPPCState
*env
= &cpu
->env
;
857 ppc_tb_t
*tb_env
= env
->tb_env
;
861 /* Truncate value to decr_width and sign extend for simplicity */
862 value
&= ((1ULL << nr_bits
) - 1);
863 negative
= !!(value
& (1ULL << (nr_bits
- 1)));
865 value
|= (0xFFFFFFFFULL
<< nr_bits
);
868 LOG_TB("%s: " TARGET_FMT_lx
" => " TARGET_FMT_lx
"\n", __func__
,
872 /* KVM handles decrementer exceptions, we don't need our own timer */
877 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
880 * If we get a really small DEC value, we can assume that by the time we
881 * handled it we should inject an interrupt already.
883 * On MSB level based DEC implementations the MSB always means the interrupt
884 * is pending, so raise it on those.
886 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
887 * an edge interrupt, so raise it here too.
890 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
) && negative
) ||
891 ((tb_env
->flags
& PPC_DECR_UNDERFLOW_TRIGGERED
) && negative
892 && !(decr
& (1ULL << (nr_bits
- 1))))) {
897 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
898 if (!negative
&& (tb_env
->flags
& PPC_DECR_UNDERFLOW_LEVEL
)) {
902 /* Calculate the next timer event */
903 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
904 next
= now
+ muldiv64(value
, NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
908 timer_mod(timer
, next
);
911 static inline void _cpu_ppc_store_decr(PowerPCCPU
*cpu
, target_ulong decr
,
912 target_ulong value
, int nr_bits
)
914 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
916 __cpu_ppc_store_decr(cpu
, &tb_env
->decr_next
, tb_env
->decr_timer
,
917 tb_env
->decr_timer
->cb
, &cpu_ppc_decr_lower
, decr
,
921 void cpu_ppc_store_decr(CPUPPCState
*env
, target_ulong value
)
923 PowerPCCPU
*cpu
= env_archcpu(env
);
924 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
927 if (env
->spr
[SPR_LPCR
] & LPCR_LD
) {
928 nr_bits
= pcc
->lrg_decr_bits
;
931 _cpu_ppc_store_decr(cpu
, cpu_ppc_load_decr(env
), value
, nr_bits
);
934 static void cpu_ppc_decr_cb(void *opaque
)
936 PowerPCCPU
*cpu
= opaque
;
938 cpu_ppc_decr_excp(cpu
);
941 static inline void _cpu_ppc_store_hdecr(PowerPCCPU
*cpu
, target_ulong hdecr
,
942 target_ulong value
, int nr_bits
)
944 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
946 if (tb_env
->hdecr_timer
!= NULL
) {
947 __cpu_ppc_store_decr(cpu
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
948 tb_env
->hdecr_timer
->cb
, &cpu_ppc_hdecr_lower
,
949 hdecr
, value
, nr_bits
);
953 void cpu_ppc_store_hdecr(CPUPPCState
*env
, target_ulong value
)
955 PowerPCCPU
*cpu
= env_archcpu(env
);
956 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
958 _cpu_ppc_store_hdecr(cpu
, cpu_ppc_load_hdecr(env
), value
,
962 static void cpu_ppc_hdecr_cb(void *opaque
)
964 PowerPCCPU
*cpu
= opaque
;
966 cpu_ppc_hdecr_excp(cpu
);
969 static void cpu_ppc_store_purr(PowerPCCPU
*cpu
, uint64_t value
)
971 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
973 tb_env
->purr_load
= value
;
974 tb_env
->purr_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
977 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
979 CPUPPCState
*env
= opaque
;
980 PowerPCCPU
*cpu
= env_archcpu(env
);
981 ppc_tb_t
*tb_env
= env
->tb_env
;
983 tb_env
->tb_freq
= freq
;
984 tb_env
->decr_freq
= freq
;
985 /* There is a bug in Linux 2.4 kernels:
986 * if a decrementer exception is pending when it enables msr_ee at startup,
987 * it's not ready to handle it...
989 _cpu_ppc_store_decr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF, 32);
990 _cpu_ppc_store_hdecr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF, 32);
991 cpu_ppc_store_purr(cpu
, 0x0000000000000000ULL
);
994 static void timebase_save(PPCTimebase
*tb
)
996 uint64_t ticks
= cpu_get_host_ticks();
997 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
999 if (!first_ppc_cpu
->env
.tb_env
) {
1000 error_report("No timebase object");
1004 /* not used anymore, we keep it for compatibility */
1005 tb
->time_of_the_day_ns
= qemu_clock_get_ns(QEMU_CLOCK_HOST
);
1007 * tb_offset is only expected to be changed by QEMU so
1008 * there is no need to update it from KVM here
1010 tb
->guest_timebase
= ticks
+ first_ppc_cpu
->env
.tb_env
->tb_offset
;
1013 static void timebase_load(PPCTimebase
*tb
)
1016 PowerPCCPU
*first_ppc_cpu
= POWERPC_CPU(first_cpu
);
1017 int64_t tb_off_adj
, tb_off
;
1020 if (!first_ppc_cpu
->env
.tb_env
) {
1021 error_report("No timebase object");
1025 freq
= first_ppc_cpu
->env
.tb_env
->tb_freq
;
1027 tb_off_adj
= tb
->guest_timebase
- cpu_get_host_ticks();
1029 tb_off
= first_ppc_cpu
->env
.tb_env
->tb_offset
;
1030 trace_ppc_tb_adjust(tb_off
, tb_off_adj
, tb_off_adj
- tb_off
,
1031 (tb_off_adj
- tb_off
) / freq
);
1033 /* Set new offset to all CPUs */
1035 PowerPCCPU
*pcpu
= POWERPC_CPU(cpu
);
1036 pcpu
->env
.tb_env
->tb_offset
= tb_off_adj
;
1037 kvmppc_set_reg_tb_offset(pcpu
, pcpu
->env
.tb_env
->tb_offset
);
1041 void cpu_ppc_clock_vm_state_change(void *opaque
, int running
,
1044 PPCTimebase
*tb
= opaque
;
1054 * When migrating, read the clock just before migration,
1055 * so that the guest clock counts during the events
1062 * This reduces clock difference on migration from 5s
1063 * to 0.1s (when max_downtime == 5s), because sending the
1064 * final pages of memory (which happens between vm_stop()
1065 * and pre_save()) takes max_downtime.
1067 static int timebase_pre_save(void *opaque
)
1069 PPCTimebase
*tb
= opaque
;
1076 const VMStateDescription vmstate_ppc_timebase
= {
1079 .minimum_version_id
= 1,
1080 .minimum_version_id_old
= 1,
1081 .pre_save
= timebase_pre_save
,
1082 .fields
= (VMStateField
[]) {
1083 VMSTATE_UINT64(guest_timebase
, PPCTimebase
),
1084 VMSTATE_INT64(time_of_the_day_ns
, PPCTimebase
),
1085 VMSTATE_END_OF_LIST()
1089 /* Set up (once) timebase frequency (in Hz) */
1090 clk_setup_cb
cpu_ppc_tb_init (CPUPPCState
*env
, uint32_t freq
)
1092 PowerPCCPU
*cpu
= env_archcpu(env
);
1095 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
1096 env
->tb_env
= tb_env
;
1097 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1098 if (is_book3s_arch2x(env
)) {
1099 /* All Book3S 64bit CPUs implement level based DEC logic */
1100 tb_env
->flags
|= PPC_DECR_UNDERFLOW_LEVEL
;
1102 /* Create new timer */
1103 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_decr_cb
, cpu
);
1104 if (env
->has_hv_mode
) {
1105 tb_env
->hdecr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_hdecr_cb
,
1108 tb_env
->hdecr_timer
= NULL
;
1110 cpu_ppc_set_tb_clk(env
, freq
);
1112 return &cpu_ppc_set_tb_clk
;
1115 /* Specific helpers for POWER & PowerPC 601 RTC */
1116 void cpu_ppc601_store_rtcu (CPUPPCState
*env
, uint32_t value
)
1118 _cpu_ppc_store_tbu(env
, value
);
1121 uint32_t cpu_ppc601_load_rtcu (CPUPPCState
*env
)
1123 return _cpu_ppc_load_tbu(env
);
1126 void cpu_ppc601_store_rtcl (CPUPPCState
*env
, uint32_t value
)
1128 cpu_ppc_store_tbl(env
, value
& 0x3FFFFF80);
1131 uint32_t cpu_ppc601_load_rtcl (CPUPPCState
*env
)
1133 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
1136 /*****************************************************************************/
1137 /* PowerPC 40x timers */
1139 /* PIT, FIT & WDT */
1140 typedef struct ppc40x_timer_t ppc40x_timer_t
;
1141 struct ppc40x_timer_t
{
1142 uint64_t pit_reload
; /* PIT auto-reload value */
1143 uint64_t fit_next
; /* Tick for next FIT interrupt */
1144 QEMUTimer
*fit_timer
;
1145 uint64_t wdt_next
; /* Tick for next WDT interrupt */
1146 QEMUTimer
*wdt_timer
;
1148 /* 405 have the PIT, 440 have a DECR. */
1149 unsigned int decr_excp
;
1152 /* Fixed interval timer */
1153 static void cpu_4xx_fit_cb (void *opaque
)
1158 ppc40x_timer_t
*ppc40x_timer
;
1162 cpu
= env_archcpu(env
);
1163 tb_env
= env
->tb_env
;
1164 ppc40x_timer
= tb_env
->opaque
;
1165 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1166 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
1180 /* Cannot occur, but makes gcc happy */
1183 next
= now
+ muldiv64(next
, NANOSECONDS_PER_SECOND
, tb_env
->tb_freq
);
1186 timer_mod(ppc40x_timer
->fit_timer
, next
);
1187 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
1188 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1) {
1189 ppc_set_irq(cpu
, PPC_INTERRUPT_FIT
, 1);
1191 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
1192 (int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
1193 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1196 /* Programmable interval timer */
1197 static void start_stop_pit (CPUPPCState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
1199 ppc40x_timer_t
*ppc40x_timer
;
1202 ppc40x_timer
= tb_env
->opaque
;
1203 if (ppc40x_timer
->pit_reload
<= 1 ||
1204 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
1205 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
1207 LOG_TB("%s: stop PIT\n", __func__
);
1208 timer_del(tb_env
->decr_timer
);
1210 LOG_TB("%s: start PIT %016" PRIx64
"\n",
1211 __func__
, ppc40x_timer
->pit_reload
);
1212 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1213 next
= now
+ muldiv64(ppc40x_timer
->pit_reload
,
1214 NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
1216 next
+= tb_env
->decr_next
- now
;
1219 timer_mod(tb_env
->decr_timer
, next
);
1220 tb_env
->decr_next
= next
;
1224 static void cpu_4xx_pit_cb (void *opaque
)
1229 ppc40x_timer_t
*ppc40x_timer
;
1232 cpu
= env_archcpu(env
);
1233 tb_env
= env
->tb_env
;
1234 ppc40x_timer
= tb_env
->opaque
;
1235 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
1236 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) {
1237 ppc_set_irq(cpu
, ppc40x_timer
->decr_excp
, 1);
1239 start_stop_pit(env
, tb_env
, 1);
1240 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
" "
1241 "%016" PRIx64
"\n", __func__
,
1242 (int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
1243 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
1244 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
1245 ppc40x_timer
->pit_reload
);
1248 /* Watchdog timer */
1249 static void cpu_4xx_wdt_cb (void *opaque
)
1254 ppc40x_timer_t
*ppc40x_timer
;
1258 cpu
= env_archcpu(env
);
1259 tb_env
= env
->tb_env
;
1260 ppc40x_timer
= tb_env
->opaque
;
1261 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1262 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
1276 /* Cannot occur, but makes gcc happy */
1279 next
= now
+ muldiv64(next
, NANOSECONDS_PER_SECOND
, tb_env
->decr_freq
);
1282 LOG_TB("%s: TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
1283 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1284 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
1287 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1288 ppc40x_timer
->wdt_next
= next
;
1289 env
->spr
[SPR_40x_TSR
] |= 1U << 31;
1292 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1293 ppc40x_timer
->wdt_next
= next
;
1294 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
1295 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1) {
1296 ppc_set_irq(cpu
, PPC_INTERRUPT_WDT
, 1);
1300 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
1301 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
1302 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
1306 case 0x1: /* Core reset */
1307 ppc40x_core_reset(cpu
);
1309 case 0x2: /* Chip reset */
1310 ppc40x_chip_reset(cpu
);
1312 case 0x3: /* System reset */
1313 ppc40x_system_reset(cpu
);
1319 void store_40x_pit (CPUPPCState
*env
, target_ulong val
)
1322 ppc40x_timer_t
*ppc40x_timer
;
1324 tb_env
= env
->tb_env
;
1325 ppc40x_timer
= tb_env
->opaque
;
1326 LOG_TB("%s val" TARGET_FMT_lx
"\n", __func__
, val
);
1327 ppc40x_timer
->pit_reload
= val
;
1328 start_stop_pit(env
, tb_env
, 0);
1331 target_ulong
load_40x_pit (CPUPPCState
*env
)
1333 return cpu_ppc_load_decr(env
);
1336 static void ppc_40x_set_tb_clk (void *opaque
, uint32_t freq
)
1338 CPUPPCState
*env
= opaque
;
1339 ppc_tb_t
*tb_env
= env
->tb_env
;
1341 LOG_TB("%s set new frequency to %" PRIu32
"\n", __func__
,
1343 tb_env
->tb_freq
= freq
;
1344 tb_env
->decr_freq
= freq
;
1345 /* XXX: we should also update all timers */
1348 clk_setup_cb
ppc_40x_timers_init (CPUPPCState
*env
, uint32_t freq
,
1349 unsigned int decr_excp
)
1352 ppc40x_timer_t
*ppc40x_timer
;
1354 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
1355 env
->tb_env
= tb_env
;
1356 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1357 ppc40x_timer
= g_malloc0(sizeof(ppc40x_timer_t
));
1358 tb_env
->tb_freq
= freq
;
1359 tb_env
->decr_freq
= freq
;
1360 tb_env
->opaque
= ppc40x_timer
;
1361 LOG_TB("%s freq %" PRIu32
"\n", __func__
, freq
);
1362 if (ppc40x_timer
!= NULL
) {
1363 /* We use decr timer for PIT */
1364 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_pit_cb
, env
);
1365 ppc40x_timer
->fit_timer
=
1366 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_fit_cb
, env
);
1367 ppc40x_timer
->wdt_timer
=
1368 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_wdt_cb
, env
);
1369 ppc40x_timer
->decr_excp
= decr_excp
;
1372 return &ppc_40x_set_tb_clk
;
1375 /*****************************************************************************/
1376 /* Embedded PowerPC Device Control Registers */
1377 typedef struct ppc_dcrn_t ppc_dcrn_t
;
1379 dcr_read_cb dcr_read
;
1380 dcr_write_cb dcr_write
;
1384 /* XXX: on 460, DCR addresses are 32 bits wide,
1385 * using DCRIPR to get the 22 upper bits of the DCR address
1387 #define DCRN_NB 1024
1389 ppc_dcrn_t dcrn
[DCRN_NB
];
1390 int (*read_error
)(int dcrn
);
1391 int (*write_error
)(int dcrn
);
1394 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1398 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1400 dcr
= &dcr_env
->dcrn
[dcrn
];
1401 if (dcr
->dcr_read
== NULL
)
1403 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1408 if (dcr_env
->read_error
!= NULL
)
1409 return (*dcr_env
->read_error
)(dcrn
);
1414 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1418 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1420 dcr
= &dcr_env
->dcrn
[dcrn
];
1421 if (dcr
->dcr_write
== NULL
)
1423 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1428 if (dcr_env
->write_error
!= NULL
)
1429 return (*dcr_env
->write_error
)(dcrn
);
1434 int ppc_dcr_register (CPUPPCState
*env
, int dcrn
, void *opaque
,
1435 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1440 dcr_env
= env
->dcr_env
;
1441 if (dcr_env
== NULL
)
1443 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1445 dcr
= &dcr_env
->dcrn
[dcrn
];
1446 if (dcr
->opaque
!= NULL
||
1447 dcr
->dcr_read
!= NULL
||
1448 dcr
->dcr_write
!= NULL
)
1450 dcr
->opaque
= opaque
;
1451 dcr
->dcr_read
= dcr_read
;
1452 dcr
->dcr_write
= dcr_write
;
1457 int ppc_dcr_init (CPUPPCState
*env
, int (*read_error
)(int dcrn
),
1458 int (*write_error
)(int dcrn
))
1462 dcr_env
= g_malloc0(sizeof(ppc_dcr_t
));
1463 dcr_env
->read_error
= read_error
;
1464 dcr_env
->write_error
= write_error
;
1465 env
->dcr_env
= dcr_env
;
1470 /*****************************************************************************/
1472 void PPC_debug_write (void *opaque
, uint32_t addr
, uint32_t val
)
1484 printf("Set loglevel to %04" PRIx32
"\n", val
);
1485 qemu_set_log(val
| 0x100);
1490 PowerPCCPU
*ppc_get_vcpu_by_pir(int pir
)
1495 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1496 CPUPPCState
*env
= &cpu
->env
;
1498 if (env
->spr_cb
[SPR_PIR
].default_value
== pir
) {