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
26 #include "qemu/timer.h"
27 #include "sysemu/sysemu.h"
31 #include "sysemu/kvm.h"
34 //#define PPC_DEBUG_IRQ
35 //#define PPC_DEBUG_TB
38 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
40 # define LOG_IRQ(...) do { } while (0)
45 # define LOG_TB(...) qemu_log(__VA_ARGS__)
47 # define LOG_TB(...) do { } while (0)
50 static void cpu_ppc_tb_stop (CPUPPCState
*env
);
51 static void cpu_ppc_tb_start (CPUPPCState
*env
);
53 void ppc_set_irq(PowerPCCPU
*cpu
, int n_IRQ
, int level
)
55 CPUPPCState
*env
= &cpu
->env
;
56 unsigned int old_pending
= env
->pending_interrupts
;
59 env
->pending_interrupts
|= 1 << n_IRQ
;
60 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
62 env
->pending_interrupts
&= ~(1 << n_IRQ
);
63 if (env
->pending_interrupts
== 0)
64 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
67 if (old_pending
!= env
->pending_interrupts
) {
69 kvmppc_set_interrupt(cpu
, n_IRQ
, level
);
73 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
74 "req %08x\n", __func__
, env
, n_IRQ
, level
,
75 env
->pending_interrupts
, env
->interrupt_request
);
78 /* PowerPC 6xx / 7xx internal IRQ controller */
79 static void ppc6xx_set_irq(void *opaque
, int pin
, int level
)
81 PowerPCCPU
*cpu
= opaque
;
82 CPUPPCState
*env
= &cpu
->env
;
85 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
87 cur_level
= (env
->irq_input_state
>> pin
) & 1;
88 /* Don't generate spurious events */
89 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
91 case PPC6xx_INPUT_TBEN
:
92 /* Level sensitive - active high */
93 LOG_IRQ("%s: %s the time base\n",
94 __func__
, level
? "start" : "stop");
96 cpu_ppc_tb_start(env
);
100 case PPC6xx_INPUT_INT
:
101 /* Level sensitive - active high */
102 LOG_IRQ("%s: set the external IRQ state to %d\n",
104 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
106 case PPC6xx_INPUT_SMI
:
107 /* Level sensitive - active high */
108 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
110 ppc_set_irq(cpu
, PPC_INTERRUPT_SMI
, level
);
112 case PPC6xx_INPUT_MCP
:
113 /* Negative edge sensitive */
114 /* XXX: TODO: actual reaction may depends on HID0 status
115 * 603/604/740/750: check HID0[EMCP]
117 if (cur_level
== 1 && level
== 0) {
118 LOG_IRQ("%s: raise machine check state\n",
120 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
123 case PPC6xx_INPUT_CKSTP_IN
:
124 /* Level sensitive - active low */
125 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
126 /* XXX: Note that the only way to restart the CPU is to reset it */
128 LOG_IRQ("%s: stop the CPU\n", __func__
);
132 case PPC6xx_INPUT_HRESET
:
133 /* Level sensitive - active low */
135 LOG_IRQ("%s: reset the CPU\n", __func__
);
136 cpu_interrupt(env
, CPU_INTERRUPT_RESET
);
139 case PPC6xx_INPUT_SRESET
:
140 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
142 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
145 /* Unknown pin - do nothing */
146 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
150 env
->irq_input_state
|= 1 << pin
;
152 env
->irq_input_state
&= ~(1 << pin
);
156 void ppc6xx_irq_init(CPUPPCState
*env
)
158 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
160 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, cpu
,
164 #if defined(TARGET_PPC64)
165 /* PowerPC 970 internal IRQ controller */
166 static void ppc970_set_irq(void *opaque
, int pin
, int level
)
168 PowerPCCPU
*cpu
= opaque
;
169 CPUPPCState
*env
= &cpu
->env
;
172 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
174 cur_level
= (env
->irq_input_state
>> pin
) & 1;
175 /* Don't generate spurious events */
176 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
178 case PPC970_INPUT_INT
:
179 /* Level sensitive - active high */
180 LOG_IRQ("%s: set the external IRQ state to %d\n",
182 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
184 case PPC970_INPUT_THINT
:
185 /* Level sensitive - active high */
186 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__
,
188 ppc_set_irq(cpu
, PPC_INTERRUPT_THERM
, level
);
190 case PPC970_INPUT_MCP
:
191 /* Negative edge sensitive */
192 /* XXX: TODO: actual reaction may depends on HID0 status
193 * 603/604/740/750: check HID0[EMCP]
195 if (cur_level
== 1 && level
== 0) {
196 LOG_IRQ("%s: raise machine check state\n",
198 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
201 case PPC970_INPUT_CKSTP
:
202 /* Level sensitive - active low */
203 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
205 LOG_IRQ("%s: stop the CPU\n", __func__
);
208 LOG_IRQ("%s: restart the CPU\n", __func__
);
210 qemu_cpu_kick(CPU(cpu
));
213 case PPC970_INPUT_HRESET
:
214 /* Level sensitive - active low */
216 cpu_interrupt(env
, CPU_INTERRUPT_RESET
);
219 case PPC970_INPUT_SRESET
:
220 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
222 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
224 case PPC970_INPUT_TBEN
:
225 LOG_IRQ("%s: set the TBEN state to %d\n", __func__
,
230 /* Unknown pin - do nothing */
231 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
235 env
->irq_input_state
|= 1 << pin
;
237 env
->irq_input_state
&= ~(1 << pin
);
241 void ppc970_irq_init(CPUPPCState
*env
)
243 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
245 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, cpu
,
249 /* POWER7 internal IRQ controller */
250 static void power7_set_irq(void *opaque
, int pin
, int level
)
252 PowerPCCPU
*cpu
= opaque
;
253 CPUPPCState
*env
= &cpu
->env
;
255 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
259 case POWER7_INPUT_INT
:
260 /* Level sensitive - active high */
261 LOG_IRQ("%s: set the external IRQ state to %d\n",
263 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
266 /* Unknown pin - do nothing */
267 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
271 env
->irq_input_state
|= 1 << pin
;
273 env
->irq_input_state
&= ~(1 << pin
);
277 void ppcPOWER7_irq_init(CPUPPCState
*env
)
279 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
281 env
->irq_inputs
= (void **)qemu_allocate_irqs(&power7_set_irq
, cpu
,
284 #endif /* defined(TARGET_PPC64) */
286 /* PowerPC 40x internal IRQ controller */
287 static void ppc40x_set_irq(void *opaque
, int pin
, int level
)
289 PowerPCCPU
*cpu
= opaque
;
290 CPUPPCState
*env
= &cpu
->env
;
293 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
295 cur_level
= (env
->irq_input_state
>> pin
) & 1;
296 /* Don't generate spurious events */
297 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
299 case PPC40x_INPUT_RESET_SYS
:
301 LOG_IRQ("%s: reset the PowerPC system\n",
303 ppc40x_system_reset(cpu
);
306 case PPC40x_INPUT_RESET_CHIP
:
308 LOG_IRQ("%s: reset the PowerPC chip\n", __func__
);
309 ppc40x_chip_reset(cpu
);
312 case PPC40x_INPUT_RESET_CORE
:
313 /* XXX: TODO: update DBSR[MRR] */
315 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
316 ppc40x_core_reset(cpu
);
319 case PPC40x_INPUT_CINT
:
320 /* Level sensitive - active high */
321 LOG_IRQ("%s: set the critical IRQ state to %d\n",
323 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
325 case PPC40x_INPUT_INT
:
326 /* Level sensitive - active high */
327 LOG_IRQ("%s: set the external IRQ state to %d\n",
329 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
331 case PPC40x_INPUT_HALT
:
332 /* Level sensitive - active low */
334 LOG_IRQ("%s: stop the CPU\n", __func__
);
337 LOG_IRQ("%s: restart the CPU\n", __func__
);
339 qemu_cpu_kick(CPU(cpu
));
342 case PPC40x_INPUT_DEBUG
:
343 /* Level sensitive - active high */
344 LOG_IRQ("%s: set the debug pin state to %d\n",
346 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
349 /* Unknown pin - do nothing */
350 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
354 env
->irq_input_state
|= 1 << pin
;
356 env
->irq_input_state
&= ~(1 << pin
);
360 void ppc40x_irq_init(CPUPPCState
*env
)
362 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
364 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc40x_set_irq
,
365 cpu
, PPC40x_INPUT_NB
);
368 /* PowerPC E500 internal IRQ controller */
369 static void ppce500_set_irq(void *opaque
, int pin
, int level
)
371 PowerPCCPU
*cpu
= opaque
;
372 CPUPPCState
*env
= &cpu
->env
;
375 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
377 cur_level
= (env
->irq_input_state
>> pin
) & 1;
378 /* Don't generate spurious events */
379 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
381 case PPCE500_INPUT_MCK
:
383 LOG_IRQ("%s: reset the PowerPC system\n",
385 qemu_system_reset_request();
388 case PPCE500_INPUT_RESET_CORE
:
390 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
391 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, level
);
394 case PPCE500_INPUT_CINT
:
395 /* Level sensitive - active high */
396 LOG_IRQ("%s: set the critical IRQ state to %d\n",
398 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
400 case PPCE500_INPUT_INT
:
401 /* Level sensitive - active high */
402 LOG_IRQ("%s: set the core IRQ state to %d\n",
404 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
406 case PPCE500_INPUT_DEBUG
:
407 /* Level sensitive - active high */
408 LOG_IRQ("%s: set the debug pin state to %d\n",
410 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
413 /* Unknown pin - do nothing */
414 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
418 env
->irq_input_state
|= 1 << pin
;
420 env
->irq_input_state
&= ~(1 << pin
);
424 void ppce500_irq_init(CPUPPCState
*env
)
426 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
428 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppce500_set_irq
,
429 cpu
, PPCE500_INPUT_NB
);
432 /* Enable or Disable the E500 EPR capability */
433 void ppce500_set_mpic_proxy(bool enabled
)
437 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
438 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
439 CPUState
*cs
= CPU(cpu
);
441 env
->mpic_proxy
= enabled
;
443 kvmppc_set_mpic_proxy(POWERPC_CPU(cs
), enabled
);
448 /*****************************************************************************/
449 /* PowerPC time base and decrementer emulation */
451 uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
, int64_t tb_offset
)
453 /* TB time in tb periods */
454 return muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec()) + tb_offset
;
457 uint64_t cpu_ppc_load_tbl (CPUPPCState
*env
)
459 ppc_tb_t
*tb_env
= env
->tb_env
;
463 return env
->spr
[SPR_TBL
];
466 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock_ns(vm_clock
), tb_env
->tb_offset
);
467 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
472 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState
*env
)
474 ppc_tb_t
*tb_env
= env
->tb_env
;
477 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock_ns(vm_clock
), tb_env
->tb_offset
);
478 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
483 uint32_t cpu_ppc_load_tbu (CPUPPCState
*env
)
486 return env
->spr
[SPR_TBU
];
489 return _cpu_ppc_load_tbu(env
);
492 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
493 int64_t *tb_offsetp
, uint64_t value
)
495 *tb_offsetp
= value
- muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec());
496 LOG_TB("%s: tb %016" PRIx64
" offset %08" PRIx64
"\n",
497 __func__
, value
, *tb_offsetp
);
500 void cpu_ppc_store_tbl (CPUPPCState
*env
, uint32_t value
)
502 ppc_tb_t
*tb_env
= env
->tb_env
;
505 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock_ns(vm_clock
), tb_env
->tb_offset
);
506 tb
&= 0xFFFFFFFF00000000ULL
;
507 cpu_ppc_store_tb(tb_env
, qemu_get_clock_ns(vm_clock
),
508 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
511 static inline void _cpu_ppc_store_tbu(CPUPPCState
*env
, uint32_t value
)
513 ppc_tb_t
*tb_env
= env
->tb_env
;
516 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock_ns(vm_clock
), tb_env
->tb_offset
);
517 tb
&= 0x00000000FFFFFFFFULL
;
518 cpu_ppc_store_tb(tb_env
, qemu_get_clock_ns(vm_clock
),
519 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
522 void cpu_ppc_store_tbu (CPUPPCState
*env
, uint32_t value
)
524 _cpu_ppc_store_tbu(env
, value
);
527 uint64_t cpu_ppc_load_atbl (CPUPPCState
*env
)
529 ppc_tb_t
*tb_env
= env
->tb_env
;
532 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock_ns(vm_clock
), tb_env
->atb_offset
);
533 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
538 uint32_t cpu_ppc_load_atbu (CPUPPCState
*env
)
540 ppc_tb_t
*tb_env
= env
->tb_env
;
543 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock_ns(vm_clock
), tb_env
->atb_offset
);
544 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
549 void cpu_ppc_store_atbl (CPUPPCState
*env
, uint32_t value
)
551 ppc_tb_t
*tb_env
= env
->tb_env
;
554 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock_ns(vm_clock
), tb_env
->atb_offset
);
555 tb
&= 0xFFFFFFFF00000000ULL
;
556 cpu_ppc_store_tb(tb_env
, qemu_get_clock_ns(vm_clock
),
557 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
560 void cpu_ppc_store_atbu (CPUPPCState
*env
, uint32_t value
)
562 ppc_tb_t
*tb_env
= env
->tb_env
;
565 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock_ns(vm_clock
), tb_env
->atb_offset
);
566 tb
&= 0x00000000FFFFFFFFULL
;
567 cpu_ppc_store_tb(tb_env
, qemu_get_clock_ns(vm_clock
),
568 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
571 static void cpu_ppc_tb_stop (CPUPPCState
*env
)
573 ppc_tb_t
*tb_env
= env
->tb_env
;
574 uint64_t tb
, atb
, vmclk
;
576 /* If the time base is already frozen, do nothing */
577 if (tb_env
->tb_freq
!= 0) {
578 vmclk
= qemu_get_clock_ns(vm_clock
);
579 /* Get the time base */
580 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
581 /* Get the alternate time base */
582 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
583 /* Store the time base value (ie compute the current offset) */
584 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
585 /* Store the alternate time base value (compute the current offset) */
586 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
587 /* Set the time base frequency to zero */
589 /* Now, the time bases are frozen to tb_offset / atb_offset value */
593 static void cpu_ppc_tb_start (CPUPPCState
*env
)
595 ppc_tb_t
*tb_env
= env
->tb_env
;
596 uint64_t tb
, atb
, vmclk
;
598 /* If the time base is not frozen, do nothing */
599 if (tb_env
->tb_freq
== 0) {
600 vmclk
= qemu_get_clock_ns(vm_clock
);
601 /* Get the time base from tb_offset */
602 tb
= tb_env
->tb_offset
;
603 /* Get the alternate time base from atb_offset */
604 atb
= tb_env
->atb_offset
;
605 /* Restore the tb frequency from the decrementer frequency */
606 tb_env
->tb_freq
= tb_env
->decr_freq
;
607 /* Store the time base value */
608 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
609 /* Store the alternate time base value */
610 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
614 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState
*env
, uint64_t next
)
616 ppc_tb_t
*tb_env
= env
->tb_env
;
620 diff
= next
- qemu_get_clock_ns(vm_clock
);
622 decr
= muldiv64(diff
, tb_env
->decr_freq
, get_ticks_per_sec());
623 } else if (tb_env
->flags
& PPC_TIMER_BOOKE
) {
626 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, get_ticks_per_sec());
628 LOG_TB("%s: %08" PRIx32
"\n", __func__
, decr
);
633 uint32_t cpu_ppc_load_decr (CPUPPCState
*env
)
635 ppc_tb_t
*tb_env
= env
->tb_env
;
638 return env
->spr
[SPR_DECR
];
641 return _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
644 uint32_t cpu_ppc_load_hdecr (CPUPPCState
*env
)
646 ppc_tb_t
*tb_env
= env
->tb_env
;
648 return _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
651 uint64_t cpu_ppc_load_purr (CPUPPCState
*env
)
653 ppc_tb_t
*tb_env
= env
->tb_env
;
656 diff
= qemu_get_clock_ns(vm_clock
) - tb_env
->purr_start
;
658 return tb_env
->purr_load
+ muldiv64(diff
, tb_env
->tb_freq
, get_ticks_per_sec());
661 /* When decrementer expires,
662 * all we need to do is generate or queue a CPU exception
664 static inline void cpu_ppc_decr_excp(PowerPCCPU
*cpu
)
667 LOG_TB("raise decrementer exception\n");
668 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 1);
671 static inline void cpu_ppc_hdecr_excp(PowerPCCPU
*cpu
)
674 LOG_TB("raise decrementer exception\n");
675 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 1);
678 static void __cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint64_t *nextp
,
679 struct QEMUTimer
*timer
,
680 void (*raise_excp
)(PowerPCCPU
*),
681 uint32_t decr
, uint32_t value
,
684 CPUPPCState
*env
= &cpu
->env
;
685 ppc_tb_t
*tb_env
= env
->tb_env
;
688 LOG_TB("%s: %08" PRIx32
" => %08" PRIx32
"\n", __func__
,
692 /* KVM handles decrementer exceptions, we don't need our own timer */
696 now
= qemu_get_clock_ns(vm_clock
);
697 next
= now
+ muldiv64(value
, get_ticks_per_sec(), tb_env
->decr_freq
);
699 next
+= *nextp
- now
;
706 qemu_mod_timer(timer
, next
);
708 /* If we set a negative value and the decrementer was positive, raise an
711 if ((tb_env
->flags
& PPC_DECR_UNDERFLOW_TRIGGERED
)
712 && (value
& 0x80000000)
713 && !(decr
& 0x80000000)) {
718 static inline void _cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint32_t decr
,
719 uint32_t value
, int is_excp
)
721 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
723 __cpu_ppc_store_decr(cpu
, &tb_env
->decr_next
, tb_env
->decr_timer
,
724 &cpu_ppc_decr_excp
, decr
, value
, is_excp
);
727 void cpu_ppc_store_decr (CPUPPCState
*env
, uint32_t value
)
729 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
731 _cpu_ppc_store_decr(cpu
, cpu_ppc_load_decr(env
), value
, 0);
734 static void cpu_ppc_decr_cb(void *opaque
)
736 PowerPCCPU
*cpu
= opaque
;
738 _cpu_ppc_store_decr(cpu
, 0x00000000, 0xFFFFFFFF, 1);
741 static inline void _cpu_ppc_store_hdecr(PowerPCCPU
*cpu
, uint32_t hdecr
,
742 uint32_t value
, int is_excp
)
744 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
746 if (tb_env
->hdecr_timer
!= NULL
) {
747 __cpu_ppc_store_decr(cpu
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
748 &cpu_ppc_hdecr_excp
, hdecr
, value
, is_excp
);
752 void cpu_ppc_store_hdecr (CPUPPCState
*env
, uint32_t value
)
754 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
756 _cpu_ppc_store_hdecr(cpu
, cpu_ppc_load_hdecr(env
), value
, 0);
759 static void cpu_ppc_hdecr_cb(void *opaque
)
761 PowerPCCPU
*cpu
= opaque
;
763 _cpu_ppc_store_hdecr(cpu
, 0x00000000, 0xFFFFFFFF, 1);
766 static void cpu_ppc_store_purr(PowerPCCPU
*cpu
, uint64_t value
)
768 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
770 tb_env
->purr_load
= value
;
771 tb_env
->purr_start
= qemu_get_clock_ns(vm_clock
);
774 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
776 CPUPPCState
*env
= opaque
;
777 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
778 ppc_tb_t
*tb_env
= env
->tb_env
;
780 tb_env
->tb_freq
= freq
;
781 tb_env
->decr_freq
= freq
;
782 /* There is a bug in Linux 2.4 kernels:
783 * if a decrementer exception is pending when it enables msr_ee at startup,
784 * it's not ready to handle it...
786 _cpu_ppc_store_decr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
787 _cpu_ppc_store_hdecr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
788 cpu_ppc_store_purr(cpu
, 0x0000000000000000ULL
);
791 /* Set up (once) timebase frequency (in Hz) */
792 clk_setup_cb
cpu_ppc_tb_init (CPUPPCState
*env
, uint32_t freq
)
794 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
797 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
798 env
->tb_env
= tb_env
;
799 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
800 /* Create new timer */
801 tb_env
->decr_timer
= qemu_new_timer_ns(vm_clock
, &cpu_ppc_decr_cb
, cpu
);
803 /* XXX: find a suitable condition to enable the hypervisor decrementer
805 tb_env
->hdecr_timer
= qemu_new_timer_ns(vm_clock
, &cpu_ppc_hdecr_cb
,
808 tb_env
->hdecr_timer
= NULL
;
810 cpu_ppc_set_tb_clk(env
, freq
);
812 return &cpu_ppc_set_tb_clk
;
815 /* Specific helpers for POWER & PowerPC 601 RTC */
817 static clk_setup_cb
cpu_ppc601_rtc_init (CPUPPCState
*env
)
819 return cpu_ppc_tb_init(env
, 7812500);
823 void cpu_ppc601_store_rtcu (CPUPPCState
*env
, uint32_t value
)
825 _cpu_ppc_store_tbu(env
, value
);
828 uint32_t cpu_ppc601_load_rtcu (CPUPPCState
*env
)
830 return _cpu_ppc_load_tbu(env
);
833 void cpu_ppc601_store_rtcl (CPUPPCState
*env
, uint32_t value
)
835 cpu_ppc_store_tbl(env
, value
& 0x3FFFFF80);
838 uint32_t cpu_ppc601_load_rtcl (CPUPPCState
*env
)
840 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
843 /*****************************************************************************/
844 /* PowerPC 40x timers */
847 typedef struct ppc40x_timer_t ppc40x_timer_t
;
848 struct ppc40x_timer_t
{
849 uint64_t pit_reload
; /* PIT auto-reload value */
850 uint64_t fit_next
; /* Tick for next FIT interrupt */
851 struct QEMUTimer
*fit_timer
;
852 uint64_t wdt_next
; /* Tick for next WDT interrupt */
853 struct QEMUTimer
*wdt_timer
;
855 /* 405 have the PIT, 440 have a DECR. */
856 unsigned int decr_excp
;
859 /* Fixed interval timer */
860 static void cpu_4xx_fit_cb (void *opaque
)
865 ppc40x_timer_t
*ppc40x_timer
;
869 cpu
= ppc_env_get_cpu(env
);
870 tb_env
= env
->tb_env
;
871 ppc40x_timer
= tb_env
->opaque
;
872 now
= qemu_get_clock_ns(vm_clock
);
873 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
887 /* Cannot occur, but makes gcc happy */
890 next
= now
+ muldiv64(next
, get_ticks_per_sec(), tb_env
->tb_freq
);
893 qemu_mod_timer(ppc40x_timer
->fit_timer
, next
);
894 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
895 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1) {
896 ppc_set_irq(cpu
, PPC_INTERRUPT_FIT
, 1);
898 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
899 (int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
900 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
903 /* Programmable interval timer */
904 static void start_stop_pit (CPUPPCState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
906 ppc40x_timer_t
*ppc40x_timer
;
909 ppc40x_timer
= tb_env
->opaque
;
910 if (ppc40x_timer
->pit_reload
<= 1 ||
911 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
912 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
914 LOG_TB("%s: stop PIT\n", __func__
);
915 qemu_del_timer(tb_env
->decr_timer
);
917 LOG_TB("%s: start PIT %016" PRIx64
"\n",
918 __func__
, ppc40x_timer
->pit_reload
);
919 now
= qemu_get_clock_ns(vm_clock
);
920 next
= now
+ muldiv64(ppc40x_timer
->pit_reload
,
921 get_ticks_per_sec(), tb_env
->decr_freq
);
923 next
+= tb_env
->decr_next
- now
;
926 qemu_mod_timer(tb_env
->decr_timer
, next
);
927 tb_env
->decr_next
= next
;
931 static void cpu_4xx_pit_cb (void *opaque
)
936 ppc40x_timer_t
*ppc40x_timer
;
939 cpu
= ppc_env_get_cpu(env
);
940 tb_env
= env
->tb_env
;
941 ppc40x_timer
= tb_env
->opaque
;
942 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
943 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) {
944 ppc_set_irq(cpu
, ppc40x_timer
->decr_excp
, 1);
946 start_stop_pit(env
, tb_env
, 1);
947 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
" "
948 "%016" PRIx64
"\n", __func__
,
949 (int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
950 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
951 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
952 ppc40x_timer
->pit_reload
);
956 static void cpu_4xx_wdt_cb (void *opaque
)
961 ppc40x_timer_t
*ppc40x_timer
;
965 cpu
= ppc_env_get_cpu(env
);
966 tb_env
= env
->tb_env
;
967 ppc40x_timer
= tb_env
->opaque
;
968 now
= qemu_get_clock_ns(vm_clock
);
969 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
983 /* Cannot occur, but makes gcc happy */
986 next
= now
+ muldiv64(next
, get_ticks_per_sec(), tb_env
->decr_freq
);
989 LOG_TB("%s: TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
990 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
991 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
994 qemu_mod_timer(ppc40x_timer
->wdt_timer
, next
);
995 ppc40x_timer
->wdt_next
= next
;
996 env
->spr
[SPR_40x_TSR
] |= 1 << 31;
999 qemu_mod_timer(ppc40x_timer
->wdt_timer
, next
);
1000 ppc40x_timer
->wdt_next
= next
;
1001 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
1002 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1) {
1003 ppc_set_irq(cpu
, PPC_INTERRUPT_WDT
, 1);
1007 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
1008 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
1009 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
1013 case 0x1: /* Core reset */
1014 ppc40x_core_reset(cpu
);
1016 case 0x2: /* Chip reset */
1017 ppc40x_chip_reset(cpu
);
1019 case 0x3: /* System reset */
1020 ppc40x_system_reset(cpu
);
1026 void store_40x_pit (CPUPPCState
*env
, target_ulong val
)
1029 ppc40x_timer_t
*ppc40x_timer
;
1031 tb_env
= env
->tb_env
;
1032 ppc40x_timer
= tb_env
->opaque
;
1033 LOG_TB("%s val" TARGET_FMT_lx
"\n", __func__
, val
);
1034 ppc40x_timer
->pit_reload
= val
;
1035 start_stop_pit(env
, tb_env
, 0);
1038 target_ulong
load_40x_pit (CPUPPCState
*env
)
1040 return cpu_ppc_load_decr(env
);
1043 static void ppc_40x_set_tb_clk (void *opaque
, uint32_t freq
)
1045 CPUPPCState
*env
= opaque
;
1046 ppc_tb_t
*tb_env
= env
->tb_env
;
1048 LOG_TB("%s set new frequency to %" PRIu32
"\n", __func__
,
1050 tb_env
->tb_freq
= freq
;
1051 tb_env
->decr_freq
= freq
;
1052 /* XXX: we should also update all timers */
1055 clk_setup_cb
ppc_40x_timers_init (CPUPPCState
*env
, uint32_t freq
,
1056 unsigned int decr_excp
)
1059 ppc40x_timer_t
*ppc40x_timer
;
1061 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
1062 env
->tb_env
= tb_env
;
1063 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1064 ppc40x_timer
= g_malloc0(sizeof(ppc40x_timer_t
));
1065 tb_env
->tb_freq
= freq
;
1066 tb_env
->decr_freq
= freq
;
1067 tb_env
->opaque
= ppc40x_timer
;
1068 LOG_TB("%s freq %" PRIu32
"\n", __func__
, freq
);
1069 if (ppc40x_timer
!= NULL
) {
1070 /* We use decr timer for PIT */
1071 tb_env
->decr_timer
= qemu_new_timer_ns(vm_clock
, &cpu_4xx_pit_cb
, env
);
1072 ppc40x_timer
->fit_timer
=
1073 qemu_new_timer_ns(vm_clock
, &cpu_4xx_fit_cb
, env
);
1074 ppc40x_timer
->wdt_timer
=
1075 qemu_new_timer_ns(vm_clock
, &cpu_4xx_wdt_cb
, env
);
1076 ppc40x_timer
->decr_excp
= decr_excp
;
1079 return &ppc_40x_set_tb_clk
;
1082 /*****************************************************************************/
1083 /* Embedded PowerPC Device Control Registers */
1084 typedef struct ppc_dcrn_t ppc_dcrn_t
;
1086 dcr_read_cb dcr_read
;
1087 dcr_write_cb dcr_write
;
1091 /* XXX: on 460, DCR addresses are 32 bits wide,
1092 * using DCRIPR to get the 22 upper bits of the DCR address
1094 #define DCRN_NB 1024
1096 ppc_dcrn_t dcrn
[DCRN_NB
];
1097 int (*read_error
)(int dcrn
);
1098 int (*write_error
)(int dcrn
);
1101 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1105 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1107 dcr
= &dcr_env
->dcrn
[dcrn
];
1108 if (dcr
->dcr_read
== NULL
)
1110 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1115 if (dcr_env
->read_error
!= NULL
)
1116 return (*dcr_env
->read_error
)(dcrn
);
1121 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1125 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1127 dcr
= &dcr_env
->dcrn
[dcrn
];
1128 if (dcr
->dcr_write
== NULL
)
1130 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1135 if (dcr_env
->write_error
!= NULL
)
1136 return (*dcr_env
->write_error
)(dcrn
);
1141 int ppc_dcr_register (CPUPPCState
*env
, int dcrn
, void *opaque
,
1142 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1147 dcr_env
= env
->dcr_env
;
1148 if (dcr_env
== NULL
)
1150 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1152 dcr
= &dcr_env
->dcrn
[dcrn
];
1153 if (dcr
->opaque
!= NULL
||
1154 dcr
->dcr_read
!= NULL
||
1155 dcr
->dcr_write
!= NULL
)
1157 dcr
->opaque
= opaque
;
1158 dcr
->dcr_read
= dcr_read
;
1159 dcr
->dcr_write
= dcr_write
;
1164 int ppc_dcr_init (CPUPPCState
*env
, int (*read_error
)(int dcrn
),
1165 int (*write_error
)(int dcrn
))
1169 dcr_env
= g_malloc0(sizeof(ppc_dcr_t
));
1170 dcr_env
->read_error
= read_error
;
1171 dcr_env
->write_error
= write_error
;
1172 env
->dcr_env
= dcr_env
;
1177 /*****************************************************************************/
1179 void PPC_debug_write (void *opaque
, uint32_t addr
, uint32_t val
)
1191 printf("Set loglevel to %04" PRIx32
"\n", val
);
1192 qemu_set_log(val
| 0x100);
1197 /*****************************************************************************/
1199 static inline uint32_t nvram_read (nvram_t
*nvram
, uint32_t addr
)
1201 return (*nvram
->read_fn
)(nvram
->opaque
, addr
);
1204 static inline void nvram_write (nvram_t
*nvram
, uint32_t addr
, uint32_t val
)
1206 (*nvram
->write_fn
)(nvram
->opaque
, addr
, val
);
1209 static void NVRAM_set_byte(nvram_t
*nvram
, uint32_t addr
, uint8_t value
)
1211 nvram_write(nvram
, addr
, value
);
1214 static uint8_t NVRAM_get_byte(nvram_t
*nvram
, uint32_t addr
)
1216 return nvram_read(nvram
, addr
);
1219 static void NVRAM_set_word(nvram_t
*nvram
, uint32_t addr
, uint16_t value
)
1221 nvram_write(nvram
, addr
, value
>> 8);
1222 nvram_write(nvram
, addr
+ 1, value
& 0xFF);
1225 static uint16_t NVRAM_get_word(nvram_t
*nvram
, uint32_t addr
)
1229 tmp
= nvram_read(nvram
, addr
) << 8;
1230 tmp
|= nvram_read(nvram
, addr
+ 1);
1235 static void NVRAM_set_lword(nvram_t
*nvram
, uint32_t addr
, uint32_t value
)
1237 nvram_write(nvram
, addr
, value
>> 24);
1238 nvram_write(nvram
, addr
+ 1, (value
>> 16) & 0xFF);
1239 nvram_write(nvram
, addr
+ 2, (value
>> 8) & 0xFF);
1240 nvram_write(nvram
, addr
+ 3, value
& 0xFF);
1243 uint32_t NVRAM_get_lword (nvram_t
*nvram
, uint32_t addr
)
1247 tmp
= nvram_read(nvram
, addr
) << 24;
1248 tmp
|= nvram_read(nvram
, addr
+ 1) << 16;
1249 tmp
|= nvram_read(nvram
, addr
+ 2) << 8;
1250 tmp
|= nvram_read(nvram
, addr
+ 3);
1255 static void NVRAM_set_string(nvram_t
*nvram
, uint32_t addr
, const char *str
,
1260 for (i
= 0; i
< max
&& str
[i
] != '\0'; i
++) {
1261 nvram_write(nvram
, addr
+ i
, str
[i
]);
1263 nvram_write(nvram
, addr
+ i
, str
[i
]);
1264 nvram_write(nvram
, addr
+ max
- 1, '\0');
1267 int NVRAM_get_string (nvram_t
*nvram
, uint8_t *dst
, uint16_t addr
, int max
)
1271 memset(dst
, 0, max
);
1272 for (i
= 0; i
< max
; i
++) {
1273 dst
[i
] = NVRAM_get_byte(nvram
, addr
+ i
);
1281 static uint16_t NVRAM_crc_update (uint16_t prev
, uint16_t value
)
1284 uint16_t pd
, pd1
, pd2
;
1289 pd2
= ((pd
>> 4) & 0x000F) ^ pd1
;
1290 tmp
^= (pd1
<< 3) | (pd1
<< 8);
1291 tmp
^= pd2
| (pd2
<< 7) | (pd2
<< 12);
1296 static uint16_t NVRAM_compute_crc (nvram_t
*nvram
, uint32_t start
, uint32_t count
)
1299 uint16_t crc
= 0xFFFF;
1304 for (i
= 0; i
!= count
; i
++) {
1305 crc
= NVRAM_crc_update(crc
, NVRAM_get_word(nvram
, start
+ i
));
1308 crc
= NVRAM_crc_update(crc
, NVRAM_get_byte(nvram
, start
+ i
) << 8);
1314 #define CMDLINE_ADDR 0x017ff000
1316 int PPC_NVRAM_set_params (nvram_t
*nvram
, uint16_t NVRAM_size
,
1318 uint32_t RAM_size
, int boot_device
,
1319 uint32_t kernel_image
, uint32_t kernel_size
,
1320 const char *cmdline
,
1321 uint32_t initrd_image
, uint32_t initrd_size
,
1322 uint32_t NVRAM_image
,
1323 int width
, int height
, int depth
)
1327 /* Set parameters for Open Hack'Ware BIOS */
1328 NVRAM_set_string(nvram
, 0x00, "QEMU_BIOS", 16);
1329 NVRAM_set_lword(nvram
, 0x10, 0x00000002); /* structure v2 */
1330 NVRAM_set_word(nvram
, 0x14, NVRAM_size
);
1331 NVRAM_set_string(nvram
, 0x20, arch
, 16);
1332 NVRAM_set_lword(nvram
, 0x30, RAM_size
);
1333 NVRAM_set_byte(nvram
, 0x34, boot_device
);
1334 NVRAM_set_lword(nvram
, 0x38, kernel_image
);
1335 NVRAM_set_lword(nvram
, 0x3C, kernel_size
);
1337 /* XXX: put the cmdline in NVRAM too ? */
1338 pstrcpy_targphys("cmdline", CMDLINE_ADDR
, RAM_size
- CMDLINE_ADDR
, cmdline
);
1339 NVRAM_set_lword(nvram
, 0x40, CMDLINE_ADDR
);
1340 NVRAM_set_lword(nvram
, 0x44, strlen(cmdline
));
1342 NVRAM_set_lword(nvram
, 0x40, 0);
1343 NVRAM_set_lword(nvram
, 0x44, 0);
1345 NVRAM_set_lword(nvram
, 0x48, initrd_image
);
1346 NVRAM_set_lword(nvram
, 0x4C, initrd_size
);
1347 NVRAM_set_lword(nvram
, 0x50, NVRAM_image
);
1349 NVRAM_set_word(nvram
, 0x54, width
);
1350 NVRAM_set_word(nvram
, 0x56, height
);
1351 NVRAM_set_word(nvram
, 0x58, depth
);
1352 crc
= NVRAM_compute_crc(nvram
, 0x00, 0xF8);
1353 NVRAM_set_word(nvram
, 0xFC, crc
);