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
27 //#define PPC_DEBUG_IRQ
28 //#define PPC_DEBUG_TB
33 void ppc_set_irq (CPUState
*env
, int n_IRQ
, int level
)
36 env
->pending_interrupts
|= 1 << n_IRQ
;
37 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
39 env
->pending_interrupts
&= ~(1 << n_IRQ
);
40 if (env
->pending_interrupts
== 0)
41 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
43 #if defined(PPC_DEBUG_IRQ)
44 if (loglevel
& CPU_LOG_INT
) {
45 fprintf(logfile
, "%s: %p n_IRQ %d level %d => pending %08x req %08x\n",
46 __func__
, env
, n_IRQ
, level
,
47 env
->pending_interrupts
, env
->interrupt_request
);
52 /* PowerPC 6xx / 7xx internal IRQ controller */
53 static void ppc6xx_set_irq (void *opaque
, int pin
, int level
)
55 CPUState
*env
= opaque
;
58 #if defined(PPC_DEBUG_IRQ)
59 if (loglevel
& CPU_LOG_INT
) {
60 fprintf(logfile
, "%s: env %p pin %d level %d\n", __func__
,
64 cur_level
= (env
->irq_input_state
>> pin
) & 1;
65 /* Don't generate spurious events */
66 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
68 case PPC6xx_INPUT_INT
:
69 /* Level sensitive - active high */
70 #if defined(PPC_DEBUG_IRQ)
71 if (loglevel
& CPU_LOG_INT
) {
72 fprintf(logfile
, "%s: set the external IRQ state to %d\n",
76 ppc_set_irq(env
, PPC_INTERRUPT_EXT
, level
);
78 case PPC6xx_INPUT_SMI
:
79 /* Level sensitive - active high */
80 #if defined(PPC_DEBUG_IRQ)
81 if (loglevel
& CPU_LOG_INT
) {
82 fprintf(logfile
, "%s: set the SMI IRQ state to %d\n",
86 ppc_set_irq(env
, PPC_INTERRUPT_SMI
, level
);
88 case PPC6xx_INPUT_MCP
:
89 /* Negative edge sensitive */
90 /* XXX: TODO: actual reaction may depends on HID0 status
91 * 603/604/740/750: check HID0[EMCP]
93 if (cur_level
== 1 && level
== 0) {
94 #if defined(PPC_DEBUG_IRQ)
95 if (loglevel
& CPU_LOG_INT
) {
96 fprintf(logfile
, "%s: raise machine check state\n",
100 ppc_set_irq(env
, PPC_INTERRUPT_MCK
, 1);
103 case PPC6xx_INPUT_CKSTP_IN
:
104 /* Level sensitive - active low */
105 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
107 #if defined(PPC_DEBUG_IRQ)
108 if (loglevel
& CPU_LOG_INT
) {
109 fprintf(logfile
, "%s: stop the CPU\n", __func__
);
114 #if defined(PPC_DEBUG_IRQ)
115 if (loglevel
& CPU_LOG_INT
) {
116 fprintf(logfile
, "%s: restart the CPU\n", __func__
);
122 case PPC6xx_INPUT_HRESET
:
123 /* Level sensitive - active low */
126 #if defined(PPC_DEBUG_IRQ)
127 if (loglevel
& CPU_LOG_INT
) {
128 fprintf(logfile
, "%s: reset the CPU\n", __func__
);
135 case PPC6xx_INPUT_SRESET
:
136 #if defined(PPC_DEBUG_IRQ)
137 if (loglevel
& CPU_LOG_INT
) {
138 fprintf(logfile
, "%s: set the RESET IRQ state to %d\n",
142 ppc_set_irq(env
, PPC_INTERRUPT_RESET
, level
);
145 /* Unknown pin - do nothing */
146 #if defined(PPC_DEBUG_IRQ)
147 if (loglevel
& CPU_LOG_INT
) {
148 fprintf(logfile
, "%s: unknown IRQ pin %d\n", __func__
, pin
);
154 env
->irq_input_state
|= 1 << pin
;
156 env
->irq_input_state
&= ~(1 << pin
);
160 void ppc6xx_irq_init (CPUState
*env
)
162 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, env
, 6);
165 /* PowerPC 970 internal IRQ controller */
166 static void ppc970_set_irq (void *opaque
, int pin
, int level
)
168 CPUState
*env
= opaque
;
171 #if defined(PPC_DEBUG_IRQ)
172 if (loglevel
& CPU_LOG_INT
) {
173 fprintf(logfile
, "%s: env %p pin %d level %d\n", __func__
,
177 cur_level
= (env
->irq_input_state
>> pin
) & 1;
178 /* Don't generate spurious events */
179 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
181 case PPC970_INPUT_INT
:
182 /* Level sensitive - active high */
183 #if defined(PPC_DEBUG_IRQ)
184 if (loglevel
& CPU_LOG_INT
) {
185 fprintf(logfile
, "%s: set the external IRQ state to %d\n",
189 ppc_set_irq(env
, PPC_INTERRUPT_EXT
, level
);
191 case PPC970_INPUT_THINT
:
192 /* Level sensitive - active high */
193 #if defined(PPC_DEBUG_IRQ)
194 if (loglevel
& CPU_LOG_INT
) {
195 fprintf(logfile
, "%s: set the SMI IRQ state to %d\n", __func__
,
199 ppc_set_irq(env
, PPC_INTERRUPT_THERM
, level
);
201 case PPC970_INPUT_MCP
:
202 /* Negative edge sensitive */
203 /* XXX: TODO: actual reaction may depends on HID0 status
204 * 603/604/740/750: check HID0[EMCP]
206 if (cur_level
== 1 && level
== 0) {
207 #if defined(PPC_DEBUG_IRQ)
208 if (loglevel
& CPU_LOG_INT
) {
209 fprintf(logfile
, "%s: raise machine check state\n",
213 ppc_set_irq(env
, PPC_INTERRUPT_MCK
, 1);
216 case PPC970_INPUT_CKSTP
:
217 /* Level sensitive - active low */
218 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
220 #if defined(PPC_DEBUG_IRQ)
221 if (loglevel
& CPU_LOG_INT
) {
222 fprintf(logfile
, "%s: stop the CPU\n", __func__
);
227 #if defined(PPC_DEBUG_IRQ)
228 if (loglevel
& CPU_LOG_INT
) {
229 fprintf(logfile
, "%s: restart the CPU\n", __func__
);
235 case PPC970_INPUT_HRESET
:
236 /* Level sensitive - active low */
239 #if defined(PPC_DEBUG_IRQ)
240 if (loglevel
& CPU_LOG_INT
) {
241 fprintf(logfile
, "%s: reset the CPU\n", __func__
);
248 case PPC970_INPUT_SRESET
:
249 #if defined(PPC_DEBUG_IRQ)
250 if (loglevel
& CPU_LOG_INT
) {
251 fprintf(logfile
, "%s: set the RESET IRQ state to %d\n",
255 ppc_set_irq(env
, PPC_INTERRUPT_RESET
, level
);
257 case PPC970_INPUT_TBEN
:
258 #if defined(PPC_DEBUG_IRQ)
259 if (loglevel
& CPU_LOG_INT
) {
260 fprintf(logfile
, "%s: set the TBEN state to %d\n", __func__
,
267 /* Unknown pin - do nothing */
268 #if defined(PPC_DEBUG_IRQ)
269 if (loglevel
& CPU_LOG_INT
) {
270 fprintf(logfile
, "%s: unknown IRQ pin %d\n", __func__
, pin
);
276 env
->irq_input_state
|= 1 << pin
;
278 env
->irq_input_state
&= ~(1 << pin
);
282 void ppc970_irq_init (CPUState
*env
)
284 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, env
, 7);
287 /* PowerPC 405 internal IRQ controller */
288 static void ppc405_set_irq (void *opaque
, int pin
, int level
)
290 CPUState
*env
= opaque
;
293 #if defined(PPC_DEBUG_IRQ)
294 if (loglevel
& CPU_LOG_INT
) {
295 fprintf(logfile
, "%s: env %p pin %d level %d\n", __func__
,
299 cur_level
= (env
->irq_input_state
>> pin
) & 1;
300 /* Don't generate spurious events */
301 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
303 case PPC405_INPUT_RESET_SYS
:
305 #if defined(PPC_DEBUG_IRQ)
306 if (loglevel
& CPU_LOG_INT
) {
307 fprintf(logfile
, "%s: reset the PowerPC system\n",
311 ppc40x_system_reset(env
);
314 case PPC405_INPUT_RESET_CHIP
:
316 #if defined(PPC_DEBUG_IRQ)
317 if (loglevel
& CPU_LOG_INT
) {
318 fprintf(logfile
, "%s: reset the PowerPC chip\n", __func__
);
321 ppc40x_chip_reset(env
);
325 case PPC405_INPUT_RESET_CORE
:
326 /* XXX: TODO: update DBSR[MRR] */
328 #if defined(PPC_DEBUG_IRQ)
329 if (loglevel
& CPU_LOG_INT
) {
330 fprintf(logfile
, "%s: reset the PowerPC core\n", __func__
);
333 ppc40x_core_reset(env
);
336 case PPC405_INPUT_CINT
:
337 /* Level sensitive - active high */
338 #if defined(PPC_DEBUG_IRQ)
339 if (loglevel
& CPU_LOG_INT
) {
340 fprintf(logfile
, "%s: set the critical IRQ state to %d\n",
345 ppc_set_irq(env
, PPC_INTERRUPT_RESET
, level
);
347 case PPC405_INPUT_INT
:
348 /* Level sensitive - active high */
349 #if defined(PPC_DEBUG_IRQ)
350 if (loglevel
& CPU_LOG_INT
) {
351 fprintf(logfile
, "%s: set the external IRQ state to %d\n",
355 ppc_set_irq(env
, PPC_INTERRUPT_EXT
, level
);
357 case PPC405_INPUT_HALT
:
358 /* Level sensitive - active low */
360 #if defined(PPC_DEBUG_IRQ)
361 if (loglevel
& CPU_LOG_INT
) {
362 fprintf(logfile
, "%s: stop the CPU\n", __func__
);
367 #if defined(PPC_DEBUG_IRQ)
368 if (loglevel
& CPU_LOG_INT
) {
369 fprintf(logfile
, "%s: restart the CPU\n", __func__
);
375 case PPC405_INPUT_DEBUG
:
376 /* Level sensitive - active high */
377 #if defined(PPC_DEBUG_IRQ)
378 if (loglevel
& CPU_LOG_INT
) {
379 fprintf(logfile
, "%s: set the debug pin state to %d\n",
383 ppc_set_irq(env
, PPC_INTERRUPT_DEBUG
, level
);
386 /* Unknown pin - do nothing */
387 #if defined(PPC_DEBUG_IRQ)
388 if (loglevel
& CPU_LOG_INT
) {
389 fprintf(logfile
, "%s: unknown IRQ pin %d\n", __func__
, pin
);
395 env
->irq_input_state
|= 1 << pin
;
397 env
->irq_input_state
&= ~(1 << pin
);
401 void ppc405_irq_init (CPUState
*env
)
403 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc405_set_irq
, env
, 7);
406 /*****************************************************************************/
407 /* PowerPC time base and decrementer emulation */
409 /* Time base management */
410 int64_t tb_offset
; /* Compensation */
411 uint32_t tb_freq
; /* TB frequency */
412 /* Decrementer management */
413 uint64_t decr_next
; /* Tick for next decr interrupt */
414 struct QEMUTimer
*decr_timer
;
415 #if defined(TARGET_PPC64H)
416 /* Hypervisor decrementer management */
417 uint64_t hdecr_next
; /* Tick for next hdecr interrupt */
418 struct QEMUTimer
*hdecr_timer
;
425 static inline uint64_t cpu_ppc_get_tb (ppc_tb_t
*tb_env
)
427 /* TB time in tb periods */
428 return muldiv64(qemu_get_clock(vm_clock
) + tb_env
->tb_offset
,
429 tb_env
->tb_freq
, ticks_per_sec
);
432 uint32_t cpu_ppc_load_tbl (CPUState
*env
)
434 ppc_tb_t
*tb_env
= env
->tb_env
;
437 tb
= cpu_ppc_get_tb(tb_env
);
440 static int last_time
;
443 if (last_time
!= now
) {
446 fprintf(logfile
, "%s: tb=0x%016lx %d %08lx\n",
447 __func__
, tb
, now
, tb_env
->tb_offset
);
453 return tb
& 0xFFFFFFFF;
456 uint32_t cpu_ppc_load_tbu (CPUState
*env
)
458 ppc_tb_t
*tb_env
= env
->tb_env
;
461 tb
= cpu_ppc_get_tb(tb_env
);
462 #if defined(PPC_DEBUG_TB)
464 fprintf(logfile
, "%s: tb=0x%016lx\n", __func__
, tb
);
471 static void cpu_ppc_store_tb (ppc_tb_t
*tb_env
, uint64_t value
)
473 tb_env
->tb_offset
= muldiv64(value
, ticks_per_sec
, tb_env
->tb_freq
)
474 - qemu_get_clock(vm_clock
);
477 fprintf(logfile
, "%s: tb=0x%016lx offset=%08lx\n", __func__
, value
,
483 void cpu_ppc_store_tbu (CPUState
*env
, uint32_t value
)
485 ppc_tb_t
*tb_env
= env
->tb_env
;
487 cpu_ppc_store_tb(tb_env
,
488 ((uint64_t)value
<< 32) | cpu_ppc_load_tbl(env
));
491 void cpu_ppc_store_tbl (CPUState
*env
, uint32_t value
)
493 ppc_tb_t
*tb_env
= env
->tb_env
;
495 cpu_ppc_store_tb(tb_env
,
496 ((uint64_t)cpu_ppc_load_tbu(env
) << 32) | value
);
499 static inline uint32_t _cpu_ppc_load_decr (CPUState
*env
, uint64_t *next
)
501 ppc_tb_t
*tb_env
= env
->tb_env
;
505 diff
= tb_env
->decr_next
- qemu_get_clock(vm_clock
);
507 decr
= muldiv64(diff
, tb_env
->tb_freq
, ticks_per_sec
);
509 decr
= -muldiv64(-diff
, tb_env
->tb_freq
, ticks_per_sec
);
510 #if defined(PPC_DEBUG_TB)
512 fprintf(logfile
, "%s: 0x%08x\n", __func__
, decr
);
519 uint32_t cpu_ppc_load_decr (CPUState
*env
)
521 ppc_tb_t
*tb_env
= env
->tb_env
;
523 return _cpu_ppc_load_decr(env
, &tb_env
->decr_next
);
526 #if defined(TARGET_PPC64H)
527 uint32_t cpu_ppc_load_hdecr (CPUState
*env
)
529 ppc_tb_t
*tb_env
= env
->tb_env
;
531 return _cpu_ppc_load_decr(env
, &tb_env
->hdecr_next
);
534 uint64_t cpu_ppc_load_purr (CPUState
*env
)
536 ppc_tb_t
*tb_env
= env
->tb_env
;
539 diff
= qemu_get_clock(vm_clock
) - tb_env
->purr_start
;
541 return tb_env
->purr_load
+ muldiv64(diff
, tb_env
->tb_freq
, ticks_per_sec
);
543 #endif /* defined(TARGET_PPC64H) */
545 /* When decrementer expires,
546 * all we need to do is generate or queue a CPU exception
548 static inline void cpu_ppc_decr_excp (CPUState
*env
)
553 fprintf(logfile
, "raise decrementer exception\n");
556 ppc_set_irq(env
, PPC_INTERRUPT_DECR
, 1);
559 static inline void cpu_ppc_hdecr_excp (CPUState
*env
)
564 fprintf(logfile
, "raise decrementer exception\n");
567 ppc_set_irq(env
, PPC_INTERRUPT_HDECR
, 1);
570 static void __cpu_ppc_store_decr (CPUState
*env
, uint64_t *nextp
,
571 struct QEMUTimer
*timer
,
572 void (*raise_excp
)(CPUState
*),
573 uint32_t decr
, uint32_t value
,
576 ppc_tb_t
*tb_env
= env
->tb_env
;
581 fprintf(logfile
, "%s: 0x%08x => 0x%08x\n", __func__
, decr
, value
);
584 now
= qemu_get_clock(vm_clock
);
585 next
= now
+ muldiv64(value
, ticks_per_sec
, tb_env
->tb_freq
);
587 next
+= *nextp
- now
;
592 qemu_mod_timer(timer
, next
);
593 /* If we set a negative value and the decrementer was positive,
594 * raise an exception.
596 if ((value
& 0x80000000) && !(decr
& 0x80000000))
601 static inline void _cpu_ppc_store_decr (CPUState
*env
, uint32_t decr
,
602 uint32_t value
, int is_excp
)
604 ppc_tb_t
*tb_env
= env
->tb_env
;
606 __cpu_ppc_store_decr(env
, &tb_env
->decr_next
, tb_env
->decr_timer
,
607 &cpu_ppc_decr_excp
, decr
, value
, is_excp
);
610 void cpu_ppc_store_decr (CPUState
*env
, uint32_t value
)
612 _cpu_ppc_store_decr(env
, cpu_ppc_load_decr(env
), value
, 0);
615 static void cpu_ppc_decr_cb (void *opaque
)
617 _cpu_ppc_store_decr(opaque
, 0x00000000, 0xFFFFFFFF, 1);
620 #if defined(TARGET_PPC64H)
621 static inline void _cpu_ppc_store_hdecr (CPUState
*env
, uint32_t hdecr
,
622 uint32_t value
, int is_excp
)
624 ppc_tb_t
*tb_env
= env
->tb_env
;
626 __cpu_ppc_store_decr(env
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
627 &cpu_ppc_hdecr_excp
, hdecr
, value
, is_excp
);
630 void cpu_ppc_store_hdecr (CPUState
*env
, uint32_t value
)
632 _cpu_ppc_store_hdecr(env
, cpu_ppc_load_hdecr(env
), value
, 0);
635 static void cpu_ppc_hdecr_cb (void *opaque
)
637 _cpu_ppc_store_hdecr(opaque
, 0x00000000, 0xFFFFFFFF, 1);
640 void cpu_ppc_store_purr (CPUState
*env
, uint64_t value
)
642 ppc_tb_t
*tb_env
= env
->tb_env
;
644 tb_env
->purr_load
= value
;
645 tb_env
->purr_start
= qemu_get_clock(vm_clock
);
647 #endif /* defined(TARGET_PPC64H) */
649 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
651 CPUState
*env
= opaque
;
652 ppc_tb_t
*tb_env
= env
->tb_env
;
654 tb_env
->tb_freq
= freq
;
655 /* There is a bug in Linux 2.4 kernels:
656 * if a decrementer exception is pending when it enables msr_ee at startup,
657 * it's not ready to handle it...
659 _cpu_ppc_store_decr(env
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
660 #if defined(TARGET_PPC64H)
661 _cpu_ppc_store_hdecr(env
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
662 cpu_ppc_store_purr(env
, 0x0000000000000000ULL
);
663 #endif /* defined(TARGET_PPC64H) */
666 /* Set up (once) timebase frequency (in Hz) */
667 clk_setup_cb
cpu_ppc_tb_init (CPUState
*env
, uint32_t freq
)
671 tb_env
= qemu_mallocz(sizeof(ppc_tb_t
));
674 env
->tb_env
= tb_env
;
675 /* Create new timer */
676 tb_env
->decr_timer
= qemu_new_timer(vm_clock
, &cpu_ppc_decr_cb
, env
);
677 #if defined(TARGET_PPC64H)
678 tb_env
->hdecr_timer
= qemu_new_timer(vm_clock
, &cpu_ppc_hdecr_cb
, env
);
679 #endif /* defined(TARGET_PPC64H) */
680 cpu_ppc_set_tb_clk(env
, freq
);
682 return &cpu_ppc_set_tb_clk
;
685 /* Specific helpers for POWER & PowerPC 601 RTC */
686 clk_setup_cb
cpu_ppc601_rtc_init (CPUState
*env
)
688 return cpu_ppc_tb_init(env
, 7812500);
691 void cpu_ppc601_store_rtcu (CPUState
*env
, uint32_t value
)
692 __attribute__ (( alias ("cpu_ppc_store_tbu") ));
694 uint32_t cpu_ppc601_load_rtcu (CPUState
*env
)
695 __attribute__ (( alias ("cpu_ppc_load_tbu") ));
697 void cpu_ppc601_store_rtcl (CPUState
*env
, uint32_t value
)
699 cpu_ppc_store_tbl(env
, value
& 0x3FFFFF80);
702 uint32_t cpu_ppc601_load_rtcl (CPUState
*env
)
704 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
707 /*****************************************************************************/
708 /* Embedded PowerPC timers */
711 typedef struct ppcemb_timer_t ppcemb_timer_t
;
712 struct ppcemb_timer_t
{
713 uint64_t pit_reload
; /* PIT auto-reload value */
714 uint64_t fit_next
; /* Tick for next FIT interrupt */
715 struct QEMUTimer
*fit_timer
;
716 uint64_t wdt_next
; /* Tick for next WDT interrupt */
717 struct QEMUTimer
*wdt_timer
;
720 /* Fixed interval timer */
721 static void cpu_4xx_fit_cb (void *opaque
)
725 ppcemb_timer_t
*ppcemb_timer
;
729 tb_env
= env
->tb_env
;
730 ppcemb_timer
= tb_env
->opaque
;
731 now
= qemu_get_clock(vm_clock
);
732 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
746 /* Cannot occur, but makes gcc happy */
749 next
= now
+ muldiv64(next
, ticks_per_sec
, tb_env
->tb_freq
);
752 qemu_mod_timer(ppcemb_timer
->fit_timer
, next
);
753 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
754 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1)
755 ppc_set_irq(env
, PPC_INTERRUPT_FIT
, 1);
758 fprintf(logfile
, "%s: ir %d TCR " ADDRX
" TSR " ADDRX
"\n", __func__
,
759 (int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
760 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
765 /* Programmable interval timer */
766 static void start_stop_pit (CPUState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
768 ppcemb_timer_t
*ppcemb_timer
;
771 ppcemb_timer
= tb_env
->opaque
;
772 if (ppcemb_timer
->pit_reload
<= 1 ||
773 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
774 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
778 fprintf(logfile
, "%s: stop PIT\n", __func__
);
781 qemu_del_timer(tb_env
->decr_timer
);
785 fprintf(logfile
, "%s: start PIT 0x" REGX
"\n",
786 __func__
, ppcemb_timer
->pit_reload
);
789 now
= qemu_get_clock(vm_clock
);
790 next
= now
+ muldiv64(ppcemb_timer
->pit_reload
,
791 ticks_per_sec
, tb_env
->tb_freq
);
793 next
+= tb_env
->decr_next
- now
;
796 qemu_mod_timer(tb_env
->decr_timer
, next
);
797 tb_env
->decr_next
= next
;
801 static void cpu_4xx_pit_cb (void *opaque
)
805 ppcemb_timer_t
*ppcemb_timer
;
808 tb_env
= env
->tb_env
;
809 ppcemb_timer
= tb_env
->opaque
;
810 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
811 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1)
812 ppc_set_irq(env
, PPC_INTERRUPT_PIT
, 1);
813 start_stop_pit(env
, tb_env
, 1);
816 fprintf(logfile
, "%s: ar %d ir %d TCR " ADDRX
" TSR " ADDRX
" "
817 "%016" PRIx64
"\n", __func__
,
818 (int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
819 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
820 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
821 ppcemb_timer
->pit_reload
);
827 static void cpu_4xx_wdt_cb (void *opaque
)
831 ppcemb_timer_t
*ppcemb_timer
;
835 tb_env
= env
->tb_env
;
836 ppcemb_timer
= tb_env
->opaque
;
837 now
= qemu_get_clock(vm_clock
);
838 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
852 /* Cannot occur, but makes gcc happy */
855 next
= now
+ muldiv64(next
, ticks_per_sec
, tb_env
->tb_freq
);
860 fprintf(logfile
, "%s: TCR " ADDRX
" TSR " ADDRX
"\n", __func__
,
861 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
864 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
867 qemu_mod_timer(ppcemb_timer
->wdt_timer
, next
);
868 ppcemb_timer
->wdt_next
= next
;
869 env
->spr
[SPR_40x_TSR
] |= 1 << 31;
872 qemu_mod_timer(ppcemb_timer
->wdt_timer
, next
);
873 ppcemb_timer
->wdt_next
= next
;
874 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
875 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1)
876 ppc_set_irq(env
, PPC_INTERRUPT_WDT
, 1);
879 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
880 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
881 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
885 case 0x1: /* Core reset */
886 ppc40x_core_reset(env
);
888 case 0x2: /* Chip reset */
889 ppc40x_chip_reset(env
);
891 case 0x3: /* System reset */
892 ppc40x_system_reset(env
);
898 void store_40x_pit (CPUState
*env
, target_ulong val
)
901 ppcemb_timer_t
*ppcemb_timer
;
903 tb_env
= env
->tb_env
;
904 ppcemb_timer
= tb_env
->opaque
;
907 fprintf(logfile
, "%s %p %p\n", __func__
, tb_env
, ppcemb_timer
);
910 ppcemb_timer
->pit_reload
= val
;
911 start_stop_pit(env
, tb_env
, 0);
914 target_ulong
load_40x_pit (CPUState
*env
)
916 return cpu_ppc_load_decr(env
);
919 void store_booke_tsr (CPUState
*env
, target_ulong val
)
923 fprintf(logfile
, "%s: val=" ADDRX
"\n", __func__
, val
);
926 env
->spr
[SPR_40x_TSR
] &= ~(val
& 0xFC000000);
927 if (val
& 0x80000000)
928 ppc_set_irq(env
, PPC_INTERRUPT_PIT
, 0);
931 void store_booke_tcr (CPUState
*env
, target_ulong val
)
935 tb_env
= env
->tb_env
;
938 fprintf(logfile
, "%s: val=" ADDRX
"\n", __func__
, val
);
941 env
->spr
[SPR_40x_TCR
] = val
& 0xFFC00000;
942 start_stop_pit(env
, tb_env
, 1);
946 static void ppc_emb_set_tb_clk (void *opaque
, uint32_t freq
)
948 CPUState
*env
= opaque
;
949 ppc_tb_t
*tb_env
= env
->tb_env
;
953 fprintf(logfile
, "%s set new frequency to %u\n", __func__
, freq
);
956 tb_env
->tb_freq
= freq
;
957 /* XXX: we should also update all timers */
960 clk_setup_cb
ppc_emb_timers_init (CPUState
*env
, uint32_t freq
)
963 ppcemb_timer_t
*ppcemb_timer
;
965 tb_env
= qemu_mallocz(sizeof(ppc_tb_t
));
966 if (tb_env
== NULL
) {
969 env
->tb_env
= tb_env
;
970 ppcemb_timer
= qemu_mallocz(sizeof(ppcemb_timer_t
));
971 tb_env
->tb_freq
= freq
;
972 tb_env
->opaque
= ppcemb_timer
;
975 fprintf(logfile
, "%s %p %p %p\n", __func__
, tb_env
, ppcemb_timer
,
976 &ppc_emb_set_tb_clk
);
979 if (ppcemb_timer
!= NULL
) {
980 /* We use decr timer for PIT */
981 tb_env
->decr_timer
= qemu_new_timer(vm_clock
, &cpu_4xx_pit_cb
, env
);
982 ppcemb_timer
->fit_timer
=
983 qemu_new_timer(vm_clock
, &cpu_4xx_fit_cb
, env
);
984 ppcemb_timer
->wdt_timer
=
985 qemu_new_timer(vm_clock
, &cpu_4xx_wdt_cb
, env
);
988 return &ppc_emb_set_tb_clk
;
991 /*****************************************************************************/
992 /* Embedded PowerPC Device Control Registers */
993 typedef struct ppc_dcrn_t ppc_dcrn_t
;
995 dcr_read_cb dcr_read
;
996 dcr_write_cb dcr_write
;
1000 /* XXX: on 460, DCR addresses are 32 bits wide,
1001 * using DCRIPR to get the 22 upper bits of the DCR address
1003 #define DCRN_NB 1024
1005 ppc_dcrn_t dcrn
[DCRN_NB
];
1006 int (*read_error
)(int dcrn
);
1007 int (*write_error
)(int dcrn
);
1010 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, target_ulong
*valp
)
1014 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1016 dcr
= &dcr_env
->dcrn
[dcrn
];
1017 if (dcr
->dcr_read
== NULL
)
1019 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1024 if (dcr_env
->read_error
!= NULL
)
1025 return (*dcr_env
->read_error
)(dcrn
);
1030 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, target_ulong val
)
1034 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1036 dcr
= &dcr_env
->dcrn
[dcrn
];
1037 if (dcr
->dcr_write
== NULL
)
1039 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1044 if (dcr_env
->write_error
!= NULL
)
1045 return (*dcr_env
->write_error
)(dcrn
);
1050 int ppc_dcr_register (CPUState
*env
, int dcrn
, void *opaque
,
1051 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1056 dcr_env
= env
->dcr_env
;
1057 if (dcr_env
== NULL
)
1059 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1061 dcr
= &dcr_env
->dcrn
[dcrn
];
1062 if (dcr
->opaque
!= NULL
||
1063 dcr
->dcr_read
!= NULL
||
1064 dcr
->dcr_write
!= NULL
)
1066 dcr
->opaque
= opaque
;
1067 dcr
->dcr_read
= dcr_read
;
1068 dcr
->dcr_write
= dcr_write
;
1073 int ppc_dcr_init (CPUState
*env
, int (*read_error
)(int dcrn
),
1074 int (*write_error
)(int dcrn
))
1078 dcr_env
= qemu_mallocz(sizeof(ppc_dcr_t
));
1079 if (dcr_env
== NULL
)
1081 dcr_env
->read_error
= read_error
;
1082 dcr_env
->write_error
= write_error
;
1083 env
->dcr_env
= dcr_env
;
1090 /*****************************************************************************/
1091 /* Handle system reset (for now, just stop emulation) */
1092 void cpu_ppc_reset (CPUState
*env
)
1094 printf("Reset asked... Stop emulation\n");
1099 /*****************************************************************************/
1101 void PPC_debug_write (void *opaque
, uint32_t addr
, uint32_t val
)
1113 printf("Set loglevel to %04x\n", val
);
1114 cpu_set_log(val
| 0x100);
1119 /*****************************************************************************/
1121 void NVRAM_set_byte (m48t59_t
*nvram
, uint32_t addr
, uint8_t value
)
1123 m48t59_write(nvram
, addr
, value
);
1126 uint8_t NVRAM_get_byte (m48t59_t
*nvram
, uint32_t addr
)
1128 return m48t59_read(nvram
, addr
);
1131 void NVRAM_set_word (m48t59_t
*nvram
, uint32_t addr
, uint16_t value
)
1133 m48t59_write(nvram
, addr
, value
>> 8);
1134 m48t59_write(nvram
, addr
+ 1, value
& 0xFF);
1137 uint16_t NVRAM_get_word (m48t59_t
*nvram
, uint32_t addr
)
1141 tmp
= m48t59_read(nvram
, addr
) << 8;
1142 tmp
|= m48t59_read(nvram
, addr
+ 1);
1146 void NVRAM_set_lword (m48t59_t
*nvram
, uint32_t addr
, uint32_t value
)
1148 m48t59_write(nvram
, addr
, value
>> 24);
1149 m48t59_write(nvram
, addr
+ 1, (value
>> 16) & 0xFF);
1150 m48t59_write(nvram
, addr
+ 2, (value
>> 8) & 0xFF);
1151 m48t59_write(nvram
, addr
+ 3, value
& 0xFF);
1154 uint32_t NVRAM_get_lword (m48t59_t
*nvram
, uint32_t addr
)
1158 tmp
= m48t59_read(nvram
, addr
) << 24;
1159 tmp
|= m48t59_read(nvram
, addr
+ 1) << 16;
1160 tmp
|= m48t59_read(nvram
, addr
+ 2) << 8;
1161 tmp
|= m48t59_read(nvram
, addr
+ 3);
1166 void NVRAM_set_string (m48t59_t
*nvram
, uint32_t addr
,
1167 const unsigned char *str
, uint32_t max
)
1171 for (i
= 0; i
< max
&& str
[i
] != '\0'; i
++) {
1172 m48t59_write(nvram
, addr
+ i
, str
[i
]);
1174 m48t59_write(nvram
, addr
+ max
- 1, '\0');
1177 int NVRAM_get_string (m48t59_t
*nvram
, uint8_t *dst
, uint16_t addr
, int max
)
1181 memset(dst
, 0, max
);
1182 for (i
= 0; i
< max
; i
++) {
1183 dst
[i
] = NVRAM_get_byte(nvram
, addr
+ i
);
1191 static uint16_t NVRAM_crc_update (uint16_t prev
, uint16_t value
)
1194 uint16_t pd
, pd1
, pd2
;
1199 pd2
= ((pd
>> 4) & 0x000F) ^ pd1
;
1200 tmp
^= (pd1
<< 3) | (pd1
<< 8);
1201 tmp
^= pd2
| (pd2
<< 7) | (pd2
<< 12);
1206 uint16_t NVRAM_compute_crc (m48t59_t
*nvram
, uint32_t start
, uint32_t count
)
1209 uint16_t crc
= 0xFFFF;
1214 for (i
= 0; i
!= count
; i
++) {
1215 crc
= NVRAM_crc_update(crc
, NVRAM_get_word(nvram
, start
+ i
));
1218 crc
= NVRAM_crc_update(crc
, NVRAM_get_byte(nvram
, start
+ i
) << 8);
1224 #define CMDLINE_ADDR 0x017ff000
1226 int PPC_NVRAM_set_params (m48t59_t
*nvram
, uint16_t NVRAM_size
,
1227 const unsigned char *arch
,
1228 uint32_t RAM_size
, int boot_device
,
1229 uint32_t kernel_image
, uint32_t kernel_size
,
1230 const char *cmdline
,
1231 uint32_t initrd_image
, uint32_t initrd_size
,
1232 uint32_t NVRAM_image
,
1233 int width
, int height
, int depth
)
1237 /* Set parameters for Open Hack'Ware BIOS */
1238 NVRAM_set_string(nvram
, 0x00, "QEMU_BIOS", 16);
1239 NVRAM_set_lword(nvram
, 0x10, 0x00000002); /* structure v2 */
1240 NVRAM_set_word(nvram
, 0x14, NVRAM_size
);
1241 NVRAM_set_string(nvram
, 0x20, arch
, 16);
1242 NVRAM_set_lword(nvram
, 0x30, RAM_size
);
1243 NVRAM_set_byte(nvram
, 0x34, boot_device
);
1244 NVRAM_set_lword(nvram
, 0x38, kernel_image
);
1245 NVRAM_set_lword(nvram
, 0x3C, kernel_size
);
1247 /* XXX: put the cmdline in NVRAM too ? */
1248 strcpy(phys_ram_base
+ CMDLINE_ADDR
, cmdline
);
1249 NVRAM_set_lword(nvram
, 0x40, CMDLINE_ADDR
);
1250 NVRAM_set_lword(nvram
, 0x44, strlen(cmdline
));
1252 NVRAM_set_lword(nvram
, 0x40, 0);
1253 NVRAM_set_lword(nvram
, 0x44, 0);
1255 NVRAM_set_lword(nvram
, 0x48, initrd_image
);
1256 NVRAM_set_lword(nvram
, 0x4C, initrd_size
);
1257 NVRAM_set_lword(nvram
, 0x50, NVRAM_image
);
1259 NVRAM_set_word(nvram
, 0x54, width
);
1260 NVRAM_set_word(nvram
, 0x56, height
);
1261 NVRAM_set_word(nvram
, 0x58, depth
);
1262 crc
= NVRAM_compute_crc(nvram
, 0x00, 0xF8);
1263 NVRAM_set_word(nvram
, 0xFC, crc
);