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"
32 //#define PPC_DEBUG_IRQ
33 //#define PPC_DEBUG_TB
36 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
38 # define LOG_IRQ(...) do { } while (0)
43 # define LOG_TB(...) qemu_log(__VA_ARGS__)
45 # define LOG_TB(...) do { } while (0)
48 static void cpu_ppc_tb_stop (CPUState
*env
);
49 static void cpu_ppc_tb_start (CPUState
*env
);
51 static void ppc_set_irq (CPUState
*env
, int n_IRQ
, int level
)
54 env
->pending_interrupts
|= 1 << n_IRQ
;
55 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
57 env
->pending_interrupts
&= ~(1 << n_IRQ
);
58 if (env
->pending_interrupts
== 0)
59 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
61 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
62 "req %08x\n", __func__
, env
, n_IRQ
, level
,
63 env
->pending_interrupts
, env
->interrupt_request
);
66 /* PowerPC 6xx / 7xx internal IRQ controller */
67 static void ppc6xx_set_irq (void *opaque
, int pin
, int level
)
69 CPUState
*env
= opaque
;
72 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
74 cur_level
= (env
->irq_input_state
>> pin
) & 1;
75 /* Don't generate spurious events */
76 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
78 case PPC6xx_INPUT_TBEN
:
79 /* Level sensitive - active high */
80 LOG_IRQ("%s: %s the time base\n",
81 __func__
, level
? "start" : "stop");
83 cpu_ppc_tb_start(env
);
87 case PPC6xx_INPUT_INT
:
88 /* Level sensitive - active high */
89 LOG_IRQ("%s: set the external IRQ state to %d\n",
91 ppc_set_irq(env
, PPC_INTERRUPT_EXT
, level
);
93 case PPC6xx_INPUT_SMI
:
94 /* Level sensitive - active high */
95 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
97 ppc_set_irq(env
, PPC_INTERRUPT_SMI
, level
);
99 case PPC6xx_INPUT_MCP
:
100 /* Negative edge sensitive */
101 /* XXX: TODO: actual reaction may depends on HID0 status
102 * 603/604/740/750: check HID0[EMCP]
104 if (cur_level
== 1 && level
== 0) {
105 LOG_IRQ("%s: raise machine check state\n",
107 ppc_set_irq(env
, PPC_INTERRUPT_MCK
, 1);
110 case PPC6xx_INPUT_CKSTP_IN
:
111 /* Level sensitive - active low */
112 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
113 /* XXX: Note that the only way to restart the CPU is to reset it */
115 LOG_IRQ("%s: stop the CPU\n", __func__
);
119 case PPC6xx_INPUT_HRESET
:
120 /* Level sensitive - active low */
122 LOG_IRQ("%s: reset the CPU\n", __func__
);
123 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
128 qemu_system_reset_request();
132 case PPC6xx_INPUT_SRESET
:
133 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
135 ppc_set_irq(env
, PPC_INTERRUPT_RESET
, level
);
138 /* Unknown pin - do nothing */
139 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
143 env
->irq_input_state
|= 1 << pin
;
145 env
->irq_input_state
&= ~(1 << pin
);
149 void ppc6xx_irq_init (CPUState
*env
)
151 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, env
,
155 #if defined(TARGET_PPC64)
156 /* PowerPC 970 internal IRQ controller */
157 static void ppc970_set_irq (void *opaque
, int pin
, int level
)
159 CPUState
*env
= opaque
;
162 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
164 cur_level
= (env
->irq_input_state
>> pin
) & 1;
165 /* Don't generate spurious events */
166 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
168 case PPC970_INPUT_INT
:
169 /* Level sensitive - active high */
170 LOG_IRQ("%s: set the external IRQ state to %d\n",
172 ppc_set_irq(env
, PPC_INTERRUPT_EXT
, level
);
174 case PPC970_INPUT_THINT
:
175 /* Level sensitive - active high */
176 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__
,
178 ppc_set_irq(env
, PPC_INTERRUPT_THERM
, level
);
180 case PPC970_INPUT_MCP
:
181 /* Negative edge sensitive */
182 /* XXX: TODO: actual reaction may depends on HID0 status
183 * 603/604/740/750: check HID0[EMCP]
185 if (cur_level
== 1 && level
== 0) {
186 LOG_IRQ("%s: raise machine check state\n",
188 ppc_set_irq(env
, PPC_INTERRUPT_MCK
, 1);
191 case PPC970_INPUT_CKSTP
:
192 /* Level sensitive - active low */
193 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
195 LOG_IRQ("%s: stop the CPU\n", __func__
);
198 LOG_IRQ("%s: restart the CPU\n", __func__
);
202 case PPC970_INPUT_HRESET
:
203 /* Level sensitive - active low */
206 LOG_IRQ("%s: reset the CPU\n", __func__
);
211 case PPC970_INPUT_SRESET
:
212 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
214 ppc_set_irq(env
, PPC_INTERRUPT_RESET
, level
);
216 case PPC970_INPUT_TBEN
:
217 LOG_IRQ("%s: set the TBEN state to %d\n", __func__
,
222 /* Unknown pin - do nothing */
223 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
227 env
->irq_input_state
|= 1 << pin
;
229 env
->irq_input_state
&= ~(1 << pin
);
233 void ppc970_irq_init (CPUState
*env
)
235 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, env
,
238 #endif /* defined(TARGET_PPC64) */
240 /* PowerPC 40x internal IRQ controller */
241 static void ppc40x_set_irq (void *opaque
, int pin
, int level
)
243 CPUState
*env
= opaque
;
246 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
248 cur_level
= (env
->irq_input_state
>> pin
) & 1;
249 /* Don't generate spurious events */
250 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
252 case PPC40x_INPUT_RESET_SYS
:
254 LOG_IRQ("%s: reset the PowerPC system\n",
256 ppc40x_system_reset(env
);
259 case PPC40x_INPUT_RESET_CHIP
:
261 LOG_IRQ("%s: reset the PowerPC chip\n", __func__
);
262 ppc40x_chip_reset(env
);
265 case PPC40x_INPUT_RESET_CORE
:
266 /* XXX: TODO: update DBSR[MRR] */
268 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
269 ppc40x_core_reset(env
);
272 case PPC40x_INPUT_CINT
:
273 /* Level sensitive - active high */
274 LOG_IRQ("%s: set the critical IRQ state to %d\n",
276 ppc_set_irq(env
, PPC_INTERRUPT_CEXT
, level
);
278 case PPC40x_INPUT_INT
:
279 /* Level sensitive - active high */
280 LOG_IRQ("%s: set the external IRQ state to %d\n",
282 ppc_set_irq(env
, PPC_INTERRUPT_EXT
, level
);
284 case PPC40x_INPUT_HALT
:
285 /* Level sensitive - active low */
287 LOG_IRQ("%s: stop the CPU\n", __func__
);
290 LOG_IRQ("%s: restart the CPU\n", __func__
);
294 case PPC40x_INPUT_DEBUG
:
295 /* Level sensitive - active high */
296 LOG_IRQ("%s: set the debug pin state to %d\n",
298 ppc_set_irq(env
, PPC_INTERRUPT_DEBUG
, level
);
301 /* Unknown pin - do nothing */
302 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
306 env
->irq_input_state
|= 1 << pin
;
308 env
->irq_input_state
&= ~(1 << pin
);
312 void ppc40x_irq_init (CPUState
*env
)
314 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc40x_set_irq
,
315 env
, PPC40x_INPUT_NB
);
318 /* PowerPC E500 internal IRQ controller */
319 static void ppce500_set_irq (void *opaque
, int pin
, int level
)
321 CPUState
*env
= opaque
;
324 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
326 cur_level
= (env
->irq_input_state
>> pin
) & 1;
327 /* Don't generate spurious events */
328 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
330 case PPCE500_INPUT_MCK
:
332 LOG_IRQ("%s: reset the PowerPC system\n",
334 qemu_system_reset_request();
337 case PPCE500_INPUT_RESET_CORE
:
339 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
340 ppc_set_irq(env
, PPC_INTERRUPT_MCK
, level
);
343 case PPCE500_INPUT_CINT
:
344 /* Level sensitive - active high */
345 LOG_IRQ("%s: set the critical IRQ state to %d\n",
347 ppc_set_irq(env
, PPC_INTERRUPT_CEXT
, level
);
349 case PPCE500_INPUT_INT
:
350 /* Level sensitive - active high */
351 LOG_IRQ("%s: set the core IRQ state to %d\n",
353 ppc_set_irq(env
, PPC_INTERRUPT_EXT
, level
);
355 case PPCE500_INPUT_DEBUG
:
356 /* Level sensitive - active high */
357 LOG_IRQ("%s: set the debug pin state to %d\n",
359 ppc_set_irq(env
, PPC_INTERRUPT_DEBUG
, level
);
362 /* Unknown pin - do nothing */
363 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
367 env
->irq_input_state
|= 1 << pin
;
369 env
->irq_input_state
&= ~(1 << pin
);
373 void ppce500_irq_init (CPUState
*env
)
375 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppce500_set_irq
,
376 env
, PPCE500_INPUT_NB
);
378 /*****************************************************************************/
379 /* PowerPC time base and decrementer emulation */
381 /* Time base management */
382 int64_t tb_offset
; /* Compensation */
383 int64_t atb_offset
; /* Compensation */
384 uint32_t tb_freq
; /* TB frequency */
385 /* Decrementer management */
386 uint64_t decr_next
; /* Tick for next decr interrupt */
387 uint32_t decr_freq
; /* decrementer frequency */
388 struct QEMUTimer
*decr_timer
;
389 /* Hypervisor decrementer management */
390 uint64_t hdecr_next
; /* Tick for next hdecr interrupt */
391 struct QEMUTimer
*hdecr_timer
;
397 static inline uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
400 /* TB time in tb periods */
401 return muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec()) + tb_offset
;
404 uint64_t cpu_ppc_load_tbl (CPUState
*env
)
406 ppc_tb_t
*tb_env
= env
->tb_env
;
409 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock(vm_clock
), tb_env
->tb_offset
);
410 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
415 static inline uint32_t _cpu_ppc_load_tbu(CPUState
*env
)
417 ppc_tb_t
*tb_env
= env
->tb_env
;
420 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock(vm_clock
), tb_env
->tb_offset
);
421 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
426 uint32_t cpu_ppc_load_tbu (CPUState
*env
)
428 return _cpu_ppc_load_tbu(env
);
431 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
432 int64_t *tb_offsetp
, uint64_t value
)
434 *tb_offsetp
= value
- muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec());
435 LOG_TB("%s: tb %016" PRIx64
" offset %08" PRIx64
"\n",
436 __func__
, value
, *tb_offsetp
);
439 void cpu_ppc_store_tbl (CPUState
*env
, uint32_t value
)
441 ppc_tb_t
*tb_env
= env
->tb_env
;
444 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock(vm_clock
), tb_env
->tb_offset
);
445 tb
&= 0xFFFFFFFF00000000ULL
;
446 cpu_ppc_store_tb(tb_env
, qemu_get_clock(vm_clock
),
447 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
450 static inline void _cpu_ppc_store_tbu(CPUState
*env
, uint32_t value
)
452 ppc_tb_t
*tb_env
= env
->tb_env
;
455 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock(vm_clock
), tb_env
->tb_offset
);
456 tb
&= 0x00000000FFFFFFFFULL
;
457 cpu_ppc_store_tb(tb_env
, qemu_get_clock(vm_clock
),
458 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
461 void cpu_ppc_store_tbu (CPUState
*env
, uint32_t value
)
463 _cpu_ppc_store_tbu(env
, value
);
466 uint64_t cpu_ppc_load_atbl (CPUState
*env
)
468 ppc_tb_t
*tb_env
= env
->tb_env
;
471 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock(vm_clock
), tb_env
->atb_offset
);
472 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
477 uint32_t cpu_ppc_load_atbu (CPUState
*env
)
479 ppc_tb_t
*tb_env
= env
->tb_env
;
482 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock(vm_clock
), tb_env
->atb_offset
);
483 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
488 void cpu_ppc_store_atbl (CPUState
*env
, uint32_t value
)
490 ppc_tb_t
*tb_env
= env
->tb_env
;
493 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock(vm_clock
), tb_env
->atb_offset
);
494 tb
&= 0xFFFFFFFF00000000ULL
;
495 cpu_ppc_store_tb(tb_env
, qemu_get_clock(vm_clock
),
496 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
499 void cpu_ppc_store_atbu (CPUState
*env
, uint32_t value
)
501 ppc_tb_t
*tb_env
= env
->tb_env
;
504 tb
= cpu_ppc_get_tb(tb_env
, qemu_get_clock(vm_clock
), tb_env
->atb_offset
);
505 tb
&= 0x00000000FFFFFFFFULL
;
506 cpu_ppc_store_tb(tb_env
, qemu_get_clock(vm_clock
),
507 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
510 static void cpu_ppc_tb_stop (CPUState
*env
)
512 ppc_tb_t
*tb_env
= env
->tb_env
;
513 uint64_t tb
, atb
, vmclk
;
515 /* If the time base is already frozen, do nothing */
516 if (tb_env
->tb_freq
!= 0) {
517 vmclk
= qemu_get_clock(vm_clock
);
518 /* Get the time base */
519 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
520 /* Get the alternate time base */
521 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
522 /* Store the time base value (ie compute the current offset) */
523 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
524 /* Store the alternate time base value (compute the current offset) */
525 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
526 /* Set the time base frequency to zero */
528 /* Now, the time bases are frozen to tb_offset / atb_offset value */
532 static void cpu_ppc_tb_start (CPUState
*env
)
534 ppc_tb_t
*tb_env
= env
->tb_env
;
535 uint64_t tb
, atb
, vmclk
;
537 /* If the time base is not frozen, do nothing */
538 if (tb_env
->tb_freq
== 0) {
539 vmclk
= qemu_get_clock(vm_clock
);
540 /* Get the time base from tb_offset */
541 tb
= tb_env
->tb_offset
;
542 /* Get the alternate time base from atb_offset */
543 atb
= tb_env
->atb_offset
;
544 /* Restore the tb frequency from the decrementer frequency */
545 tb_env
->tb_freq
= tb_env
->decr_freq
;
546 /* Store the time base value */
547 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
548 /* Store the alternate time base value */
549 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
553 static inline uint32_t _cpu_ppc_load_decr(CPUState
*env
, uint64_t next
)
555 ppc_tb_t
*tb_env
= env
->tb_env
;
559 diff
= next
- qemu_get_clock(vm_clock
);
561 decr
= muldiv64(diff
, tb_env
->decr_freq
, get_ticks_per_sec());
563 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, get_ticks_per_sec());
564 LOG_TB("%s: %08" PRIx32
"\n", __func__
, decr
);
569 uint32_t cpu_ppc_load_decr (CPUState
*env
)
571 ppc_tb_t
*tb_env
= env
->tb_env
;
573 return _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
576 uint32_t cpu_ppc_load_hdecr (CPUState
*env
)
578 ppc_tb_t
*tb_env
= env
->tb_env
;
580 return _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
583 uint64_t cpu_ppc_load_purr (CPUState
*env
)
585 ppc_tb_t
*tb_env
= env
->tb_env
;
588 diff
= qemu_get_clock(vm_clock
) - tb_env
->purr_start
;
590 return tb_env
->purr_load
+ muldiv64(diff
, tb_env
->tb_freq
, get_ticks_per_sec());
593 /* When decrementer expires,
594 * all we need to do is generate or queue a CPU exception
596 static inline void cpu_ppc_decr_excp(CPUState
*env
)
599 LOG_TB("raise decrementer exception\n");
600 ppc_set_irq(env
, PPC_INTERRUPT_DECR
, 1);
603 static inline void cpu_ppc_hdecr_excp(CPUState
*env
)
606 LOG_TB("raise decrementer exception\n");
607 ppc_set_irq(env
, PPC_INTERRUPT_HDECR
, 1);
610 static void __cpu_ppc_store_decr (CPUState
*env
, uint64_t *nextp
,
611 struct QEMUTimer
*timer
,
612 void (*raise_excp
)(CPUState
*),
613 uint32_t decr
, uint32_t value
,
616 ppc_tb_t
*tb_env
= env
->tb_env
;
619 LOG_TB("%s: %08" PRIx32
" => %08" PRIx32
"\n", __func__
,
621 now
= qemu_get_clock(vm_clock
);
622 next
= now
+ muldiv64(value
, get_ticks_per_sec(), tb_env
->decr_freq
);
624 next
+= *nextp
- now
;
629 qemu_mod_timer(timer
, next
);
630 /* If we set a negative value and the decrementer was positive,
631 * raise an exception.
633 if ((value
& 0x80000000) && !(decr
& 0x80000000))
637 static inline void _cpu_ppc_store_decr(CPUState
*env
, uint32_t decr
,
638 uint32_t value
, int is_excp
)
640 ppc_tb_t
*tb_env
= env
->tb_env
;
642 __cpu_ppc_store_decr(env
, &tb_env
->decr_next
, tb_env
->decr_timer
,
643 &cpu_ppc_decr_excp
, decr
, value
, is_excp
);
646 void cpu_ppc_store_decr (CPUState
*env
, uint32_t value
)
648 _cpu_ppc_store_decr(env
, cpu_ppc_load_decr(env
), value
, 0);
651 static void cpu_ppc_decr_cb (void *opaque
)
653 _cpu_ppc_store_decr(opaque
, 0x00000000, 0xFFFFFFFF, 1);
656 static inline void _cpu_ppc_store_hdecr(CPUState
*env
, uint32_t hdecr
,
657 uint32_t value
, int is_excp
)
659 ppc_tb_t
*tb_env
= env
->tb_env
;
661 if (tb_env
->hdecr_timer
!= NULL
) {
662 __cpu_ppc_store_decr(env
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
663 &cpu_ppc_hdecr_excp
, hdecr
, value
, is_excp
);
667 void cpu_ppc_store_hdecr (CPUState
*env
, uint32_t value
)
669 _cpu_ppc_store_hdecr(env
, cpu_ppc_load_hdecr(env
), value
, 0);
672 static void cpu_ppc_hdecr_cb (void *opaque
)
674 _cpu_ppc_store_hdecr(opaque
, 0x00000000, 0xFFFFFFFF, 1);
677 void cpu_ppc_store_purr (CPUState
*env
, uint64_t value
)
679 ppc_tb_t
*tb_env
= env
->tb_env
;
681 tb_env
->purr_load
= value
;
682 tb_env
->purr_start
= qemu_get_clock(vm_clock
);
685 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
687 CPUState
*env
= opaque
;
688 ppc_tb_t
*tb_env
= env
->tb_env
;
690 tb_env
->tb_freq
= freq
;
691 tb_env
->decr_freq
= freq
;
692 /* There is a bug in Linux 2.4 kernels:
693 * if a decrementer exception is pending when it enables msr_ee at startup,
694 * it's not ready to handle it...
696 _cpu_ppc_store_decr(env
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
697 _cpu_ppc_store_hdecr(env
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
698 cpu_ppc_store_purr(env
, 0x0000000000000000ULL
);
701 /* Set up (once) timebase frequency (in Hz) */
702 clk_setup_cb
cpu_ppc_tb_init (CPUState
*env
, uint32_t freq
)
706 tb_env
= qemu_mallocz(sizeof(ppc_tb_t
));
707 env
->tb_env
= tb_env
;
708 /* Create new timer */
709 tb_env
->decr_timer
= qemu_new_timer(vm_clock
, &cpu_ppc_decr_cb
, env
);
711 /* XXX: find a suitable condition to enable the hypervisor decrementer
713 tb_env
->hdecr_timer
= qemu_new_timer(vm_clock
, &cpu_ppc_hdecr_cb
, env
);
715 tb_env
->hdecr_timer
= NULL
;
717 cpu_ppc_set_tb_clk(env
, freq
);
719 return &cpu_ppc_set_tb_clk
;
722 /* Specific helpers for POWER & PowerPC 601 RTC */
724 static clk_setup_cb
cpu_ppc601_rtc_init (CPUState
*env
)
726 return cpu_ppc_tb_init(env
, 7812500);
730 void cpu_ppc601_store_rtcu (CPUState
*env
, uint32_t value
)
732 _cpu_ppc_store_tbu(env
, value
);
735 uint32_t cpu_ppc601_load_rtcu (CPUState
*env
)
737 return _cpu_ppc_load_tbu(env
);
740 void cpu_ppc601_store_rtcl (CPUState
*env
, uint32_t value
)
742 cpu_ppc_store_tbl(env
, value
& 0x3FFFFF80);
745 uint32_t cpu_ppc601_load_rtcl (CPUState
*env
)
747 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
750 /*****************************************************************************/
751 /* Embedded PowerPC timers */
754 typedef struct ppcemb_timer_t ppcemb_timer_t
;
755 struct ppcemb_timer_t
{
756 uint64_t pit_reload
; /* PIT auto-reload value */
757 uint64_t fit_next
; /* Tick for next FIT interrupt */
758 struct QEMUTimer
*fit_timer
;
759 uint64_t wdt_next
; /* Tick for next WDT interrupt */
760 struct QEMUTimer
*wdt_timer
;
763 /* Fixed interval timer */
764 static void cpu_4xx_fit_cb (void *opaque
)
768 ppcemb_timer_t
*ppcemb_timer
;
772 tb_env
= env
->tb_env
;
773 ppcemb_timer
= tb_env
->opaque
;
774 now
= qemu_get_clock(vm_clock
);
775 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
789 /* Cannot occur, but makes gcc happy */
792 next
= now
+ muldiv64(next
, get_ticks_per_sec(), tb_env
->tb_freq
);
795 qemu_mod_timer(ppcemb_timer
->fit_timer
, next
);
796 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
797 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1)
798 ppc_set_irq(env
, PPC_INTERRUPT_FIT
, 1);
799 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
800 (int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
801 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
804 /* Programmable interval timer */
805 static void start_stop_pit (CPUState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
807 ppcemb_timer_t
*ppcemb_timer
;
810 ppcemb_timer
= tb_env
->opaque
;
811 if (ppcemb_timer
->pit_reload
<= 1 ||
812 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
813 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
815 LOG_TB("%s: stop PIT\n", __func__
);
816 qemu_del_timer(tb_env
->decr_timer
);
818 LOG_TB("%s: start PIT %016" PRIx64
"\n",
819 __func__
, ppcemb_timer
->pit_reload
);
820 now
= qemu_get_clock(vm_clock
);
821 next
= now
+ muldiv64(ppcemb_timer
->pit_reload
,
822 get_ticks_per_sec(), tb_env
->decr_freq
);
824 next
+= tb_env
->decr_next
- now
;
827 qemu_mod_timer(tb_env
->decr_timer
, next
);
828 tb_env
->decr_next
= next
;
832 static void cpu_4xx_pit_cb (void *opaque
)
836 ppcemb_timer_t
*ppcemb_timer
;
839 tb_env
= env
->tb_env
;
840 ppcemb_timer
= tb_env
->opaque
;
841 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
842 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1)
843 ppc_set_irq(env
, PPC_INTERRUPT_PIT
, 1);
844 start_stop_pit(env
, tb_env
, 1);
845 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
" "
846 "%016" PRIx64
"\n", __func__
,
847 (int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
848 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
849 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
850 ppcemb_timer
->pit_reload
);
854 static void cpu_4xx_wdt_cb (void *opaque
)
858 ppcemb_timer_t
*ppcemb_timer
;
862 tb_env
= env
->tb_env
;
863 ppcemb_timer
= tb_env
->opaque
;
864 now
= qemu_get_clock(vm_clock
);
865 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
879 /* Cannot occur, but makes gcc happy */
882 next
= now
+ muldiv64(next
, get_ticks_per_sec(), tb_env
->decr_freq
);
885 LOG_TB("%s: TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
886 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
887 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
890 qemu_mod_timer(ppcemb_timer
->wdt_timer
, next
);
891 ppcemb_timer
->wdt_next
= next
;
892 env
->spr
[SPR_40x_TSR
] |= 1 << 31;
895 qemu_mod_timer(ppcemb_timer
->wdt_timer
, next
);
896 ppcemb_timer
->wdt_next
= next
;
897 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
898 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1)
899 ppc_set_irq(env
, PPC_INTERRUPT_WDT
, 1);
902 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
903 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
904 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
908 case 0x1: /* Core reset */
909 ppc40x_core_reset(env
);
911 case 0x2: /* Chip reset */
912 ppc40x_chip_reset(env
);
914 case 0x3: /* System reset */
915 ppc40x_system_reset(env
);
921 void store_40x_pit (CPUState
*env
, target_ulong val
)
924 ppcemb_timer_t
*ppcemb_timer
;
926 tb_env
= env
->tb_env
;
927 ppcemb_timer
= tb_env
->opaque
;
928 LOG_TB("%s val" TARGET_FMT_lx
"\n", __func__
, val
);
929 ppcemb_timer
->pit_reload
= val
;
930 start_stop_pit(env
, tb_env
, 0);
933 target_ulong
load_40x_pit (CPUState
*env
)
935 return cpu_ppc_load_decr(env
);
938 void store_booke_tsr (CPUState
*env
, target_ulong val
)
940 LOG_TB("%s: val " TARGET_FMT_lx
"\n", __func__
, val
);
941 env
->spr
[SPR_40x_TSR
] &= ~(val
& 0xFC000000);
942 if (val
& 0x80000000)
943 ppc_set_irq(env
, PPC_INTERRUPT_PIT
, 0);
946 void store_booke_tcr (CPUState
*env
, target_ulong val
)
950 tb_env
= env
->tb_env
;
951 LOG_TB("%s: val " TARGET_FMT_lx
"\n", __func__
, val
);
952 env
->spr
[SPR_40x_TCR
] = val
& 0xFFC00000;
953 start_stop_pit(env
, tb_env
, 1);
957 static void ppc_emb_set_tb_clk (void *opaque
, uint32_t freq
)
959 CPUState
*env
= opaque
;
960 ppc_tb_t
*tb_env
= env
->tb_env
;
962 LOG_TB("%s set new frequency to %" PRIu32
"\n", __func__
,
964 tb_env
->tb_freq
= freq
;
965 tb_env
->decr_freq
= freq
;
966 /* XXX: we should also update all timers */
969 clk_setup_cb
ppc_emb_timers_init (CPUState
*env
, uint32_t freq
)
972 ppcemb_timer_t
*ppcemb_timer
;
974 tb_env
= qemu_mallocz(sizeof(ppc_tb_t
));
975 env
->tb_env
= tb_env
;
976 ppcemb_timer
= qemu_mallocz(sizeof(ppcemb_timer_t
));
977 tb_env
->tb_freq
= freq
;
978 tb_env
->decr_freq
= freq
;
979 tb_env
->opaque
= ppcemb_timer
;
980 LOG_TB("%s freq %" PRIu32
"\n", __func__
, freq
);
981 if (ppcemb_timer
!= NULL
) {
982 /* We use decr timer for PIT */
983 tb_env
->decr_timer
= qemu_new_timer(vm_clock
, &cpu_4xx_pit_cb
, env
);
984 ppcemb_timer
->fit_timer
=
985 qemu_new_timer(vm_clock
, &cpu_4xx_fit_cb
, env
);
986 ppcemb_timer
->wdt_timer
=
987 qemu_new_timer(vm_clock
, &cpu_4xx_wdt_cb
, env
);
990 return &ppc_emb_set_tb_clk
;
993 /*****************************************************************************/
994 /* Embedded PowerPC Device Control Registers */
995 typedef struct ppc_dcrn_t ppc_dcrn_t
;
997 dcr_read_cb dcr_read
;
998 dcr_write_cb dcr_write
;
1002 /* XXX: on 460, DCR addresses are 32 bits wide,
1003 * using DCRIPR to get the 22 upper bits of the DCR address
1005 #define DCRN_NB 1024
1007 ppc_dcrn_t dcrn
[DCRN_NB
];
1008 int (*read_error
)(int dcrn
);
1009 int (*write_error
)(int dcrn
);
1012 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1016 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1018 dcr
= &dcr_env
->dcrn
[dcrn
];
1019 if (dcr
->dcr_read
== NULL
)
1021 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1026 if (dcr_env
->read_error
!= NULL
)
1027 return (*dcr_env
->read_error
)(dcrn
);
1032 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1036 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1038 dcr
= &dcr_env
->dcrn
[dcrn
];
1039 if (dcr
->dcr_write
== NULL
)
1041 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1046 if (dcr_env
->write_error
!= NULL
)
1047 return (*dcr_env
->write_error
)(dcrn
);
1052 int ppc_dcr_register (CPUState
*env
, int dcrn
, void *opaque
,
1053 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1058 dcr_env
= env
->dcr_env
;
1059 if (dcr_env
== NULL
)
1061 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1063 dcr
= &dcr_env
->dcrn
[dcrn
];
1064 if (dcr
->opaque
!= NULL
||
1065 dcr
->dcr_read
!= NULL
||
1066 dcr
->dcr_write
!= NULL
)
1068 dcr
->opaque
= opaque
;
1069 dcr
->dcr_read
= dcr_read
;
1070 dcr
->dcr_write
= dcr_write
;
1075 int ppc_dcr_init (CPUState
*env
, int (*read_error
)(int dcrn
),
1076 int (*write_error
)(int dcrn
))
1080 dcr_env
= qemu_mallocz(sizeof(ppc_dcr_t
));
1081 dcr_env
->read_error
= read_error
;
1082 dcr_env
->write_error
= write_error
;
1083 env
->dcr_env
= dcr_env
;
1089 /*****************************************************************************/
1090 /* Handle system reset (for now, just stop emulation) */
1091 void cpu_reset(CPUState
*env
)
1093 printf("Reset asked... Stop emulation\n");
1098 /*****************************************************************************/
1100 void PPC_debug_write (void *opaque
, uint32_t addr
, uint32_t val
)
1112 printf("Set loglevel to %04" PRIx32
"\n", val
);
1113 cpu_set_log(val
| 0x100);
1118 /*****************************************************************************/
1120 static inline uint32_t nvram_read (nvram_t
*nvram
, uint32_t addr
)
1122 return (*nvram
->read_fn
)(nvram
->opaque
, addr
);;
1125 static inline void nvram_write (nvram_t
*nvram
, uint32_t addr
, uint32_t val
)
1127 (*nvram
->write_fn
)(nvram
->opaque
, addr
, val
);
1130 void NVRAM_set_byte (nvram_t
*nvram
, uint32_t addr
, uint8_t value
)
1132 nvram_write(nvram
, addr
, value
);
1135 uint8_t NVRAM_get_byte (nvram_t
*nvram
, uint32_t addr
)
1137 return nvram_read(nvram
, addr
);
1140 void NVRAM_set_word (nvram_t
*nvram
, uint32_t addr
, uint16_t value
)
1142 nvram_write(nvram
, addr
, value
>> 8);
1143 nvram_write(nvram
, addr
+ 1, value
& 0xFF);
1146 uint16_t NVRAM_get_word (nvram_t
*nvram
, uint32_t addr
)
1150 tmp
= nvram_read(nvram
, addr
) << 8;
1151 tmp
|= nvram_read(nvram
, addr
+ 1);
1156 void NVRAM_set_lword (nvram_t
*nvram
, uint32_t addr
, uint32_t value
)
1158 nvram_write(nvram
, addr
, value
>> 24);
1159 nvram_write(nvram
, addr
+ 1, (value
>> 16) & 0xFF);
1160 nvram_write(nvram
, addr
+ 2, (value
>> 8) & 0xFF);
1161 nvram_write(nvram
, addr
+ 3, value
& 0xFF);
1164 uint32_t NVRAM_get_lword (nvram_t
*nvram
, uint32_t addr
)
1168 tmp
= nvram_read(nvram
, addr
) << 24;
1169 tmp
|= nvram_read(nvram
, addr
+ 1) << 16;
1170 tmp
|= nvram_read(nvram
, addr
+ 2) << 8;
1171 tmp
|= nvram_read(nvram
, addr
+ 3);
1176 void NVRAM_set_string (nvram_t
*nvram
, uint32_t addr
,
1177 const char *str
, uint32_t max
)
1181 for (i
= 0; i
< max
&& str
[i
] != '\0'; i
++) {
1182 nvram_write(nvram
, addr
+ i
, str
[i
]);
1184 nvram_write(nvram
, addr
+ i
, str
[i
]);
1185 nvram_write(nvram
, addr
+ max
- 1, '\0');
1188 int NVRAM_get_string (nvram_t
*nvram
, uint8_t *dst
, uint16_t addr
, int max
)
1192 memset(dst
, 0, max
);
1193 for (i
= 0; i
< max
; i
++) {
1194 dst
[i
] = NVRAM_get_byte(nvram
, addr
+ i
);
1202 static uint16_t NVRAM_crc_update (uint16_t prev
, uint16_t value
)
1205 uint16_t pd
, pd1
, pd2
;
1210 pd2
= ((pd
>> 4) & 0x000F) ^ pd1
;
1211 tmp
^= (pd1
<< 3) | (pd1
<< 8);
1212 tmp
^= pd2
| (pd2
<< 7) | (pd2
<< 12);
1217 static uint16_t NVRAM_compute_crc (nvram_t
*nvram
, uint32_t start
, uint32_t count
)
1220 uint16_t crc
= 0xFFFF;
1225 for (i
= 0; i
!= count
; i
++) {
1226 crc
= NVRAM_crc_update(crc
, NVRAM_get_word(nvram
, start
+ i
));
1229 crc
= NVRAM_crc_update(crc
, NVRAM_get_byte(nvram
, start
+ i
) << 8);
1235 #define CMDLINE_ADDR 0x017ff000
1237 int PPC_NVRAM_set_params (nvram_t
*nvram
, uint16_t NVRAM_size
,
1239 uint32_t RAM_size
, int boot_device
,
1240 uint32_t kernel_image
, uint32_t kernel_size
,
1241 const char *cmdline
,
1242 uint32_t initrd_image
, uint32_t initrd_size
,
1243 uint32_t NVRAM_image
,
1244 int width
, int height
, int depth
)
1248 /* Set parameters for Open Hack'Ware BIOS */
1249 NVRAM_set_string(nvram
, 0x00, "QEMU_BIOS", 16);
1250 NVRAM_set_lword(nvram
, 0x10, 0x00000002); /* structure v2 */
1251 NVRAM_set_word(nvram
, 0x14, NVRAM_size
);
1252 NVRAM_set_string(nvram
, 0x20, arch
, 16);
1253 NVRAM_set_lword(nvram
, 0x30, RAM_size
);
1254 NVRAM_set_byte(nvram
, 0x34, boot_device
);
1255 NVRAM_set_lword(nvram
, 0x38, kernel_image
);
1256 NVRAM_set_lword(nvram
, 0x3C, kernel_size
);
1258 /* XXX: put the cmdline in NVRAM too ? */
1259 pstrcpy_targphys("cmdline", CMDLINE_ADDR
, RAM_size
- CMDLINE_ADDR
, cmdline
);
1260 NVRAM_set_lword(nvram
, 0x40, CMDLINE_ADDR
);
1261 NVRAM_set_lword(nvram
, 0x44, strlen(cmdline
));
1263 NVRAM_set_lword(nvram
, 0x40, 0);
1264 NVRAM_set_lword(nvram
, 0x44, 0);
1266 NVRAM_set_lword(nvram
, 0x48, initrd_image
);
1267 NVRAM_set_lword(nvram
, 0x4C, initrd_size
);
1268 NVRAM_set_lword(nvram
, 0x50, NVRAM_image
);
1270 NVRAM_set_word(nvram
, 0x54, width
);
1271 NVRAM_set_word(nvram
, 0x56, height
);
1272 NVRAM_set_word(nvram
, 0x58, depth
);
1273 crc
= NVRAM_compute_crc(nvram
, 0x00, 0xF8);
1274 NVRAM_set_word(nvram
, 0xFC, crc
);