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 static 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 #if defined(TARGET_PPC64)
166 /* PowerPC 970 internal IRQ controller */
167 static void ppc970_set_irq (void *opaque, int pin, int level)
169 CPUState *env = opaque;
172 #if defined(PPC_DEBUG_IRQ)
173 if (loglevel & CPU_LOG_INT) {
174 fprintf(logfile, "%s: env %p pin %d level %d\n", __func__,
178 cur_level = (env->irq_input_state >> pin) & 1;
179 /* Don't generate spurious events */
180 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
182 case PPC970_INPUT_INT:
183 /* Level sensitive - active high */
184 #if defined(PPC_DEBUG_IRQ)
185 if (loglevel & CPU_LOG_INT) {
186 fprintf(logfile, "%s: set the external IRQ state to %d\n",
190 ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
192 case PPC970_INPUT_THINT:
193 /* Level sensitive - active high */
194 #if defined(PPC_DEBUG_IRQ)
195 if (loglevel & CPU_LOG_INT) {
196 fprintf(logfile, "%s: set the SMI IRQ state to %d\n", __func__,
200 ppc_set_irq(env, PPC_INTERRUPT_THERM, level);
202 case PPC970_INPUT_MCP:
203 /* Negative edge sensitive */
204 /* XXX: TODO: actual reaction may depends on HID0 status
205 * 603/604/740/750: check HID0[EMCP]
207 if (cur_level == 1 && level == 0) {
208 #if defined(PPC_DEBUG_IRQ)
209 if (loglevel & CPU_LOG_INT) {
210 fprintf(logfile, "%s: raise machine check state\n",
214 ppc_set_irq(env, PPC_INTERRUPT_MCK, 1);
217 case PPC970_INPUT_CKSTP:
218 /* Level sensitive - active low */
219 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
221 #if defined(PPC_DEBUG_IRQ)
222 if (loglevel & CPU_LOG_INT) {
223 fprintf(logfile, "%s: stop the CPU\n", __func__);
228 #if defined(PPC_DEBUG_IRQ)
229 if (loglevel & CPU_LOG_INT) {
230 fprintf(logfile, "%s: restart the CPU\n", __func__);
236 case PPC970_INPUT_HRESET:
237 /* Level sensitive - active low */
240 #if defined(PPC_DEBUG_IRQ)
241 if (loglevel & CPU_LOG_INT) {
242 fprintf(logfile, "%s: reset the CPU\n", __func__);
249 case PPC970_INPUT_SRESET:
250 #if defined(PPC_DEBUG_IRQ)
251 if (loglevel & CPU_LOG_INT) {
252 fprintf(logfile, "%s: set the RESET IRQ state to %d\n",
256 ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
258 case PPC970_INPUT_TBEN:
259 #if defined(PPC_DEBUG_IRQ)
260 if (loglevel & CPU_LOG_INT) {
261 fprintf(logfile, "%s: set the TBEN state to %d\n", __func__,
268 /* Unknown pin - do nothing */
269 #if defined(PPC_DEBUG_IRQ)
270 if (loglevel & CPU_LOG_INT) {
271 fprintf(logfile, "%s: unknown IRQ pin %d\n", __func__, pin);
277 env->irq_input_state |= 1 << pin;
279 env->irq_input_state &= ~(1 << pin);
283 void ppc970_irq_init (CPUState *env)
285 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, env, 7);
287 #endif /* defined(TARGET_PPC64) */
289 /* PowerPC 40x internal IRQ controller */
290 static void ppc40x_set_irq (void *opaque, int pin, int level)
292 CPUState *env = opaque;
295 #if defined(PPC_DEBUG_IRQ)
296 if (loglevel & CPU_LOG_INT) {
297 fprintf(logfile, "%s: env %p pin %d level %d\n", __func__,
301 cur_level = (env->irq_input_state >> pin) & 1;
302 /* Don't generate spurious events */
303 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
305 case PPC40x_INPUT_RESET_SYS:
307 #if defined(PPC_DEBUG_IRQ)
308 if (loglevel & CPU_LOG_INT) {
309 fprintf(logfile, "%s: reset the PowerPC system\n",
313 ppc40x_system_reset(env);
316 case PPC40x_INPUT_RESET_CHIP:
318 #if defined(PPC_DEBUG_IRQ)
319 if (loglevel & CPU_LOG_INT) {
320 fprintf(logfile, "%s: reset the PowerPC chip\n", __func__);
323 ppc40x_chip_reset(env);
326 case PPC40x_INPUT_RESET_CORE:
327 /* XXX: TODO: update DBSR[MRR] */
329 #if defined(PPC_DEBUG_IRQ)
330 if (loglevel & CPU_LOG_INT) {
331 fprintf(logfile, "%s: reset the PowerPC core\n", __func__);
334 ppc40x_core_reset(env);
337 case PPC40x_INPUT_CINT:
338 /* Level sensitive - active high */
339 #if defined(PPC_DEBUG_IRQ)
340 if (loglevel & CPU_LOG_INT) {
341 fprintf(logfile, "%s: set the critical IRQ state to %d\n",
345 ppc_set_irq(env, PPC_INTERRUPT_CEXT, level);
347 case PPC40x_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 PPC40x_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 PPC40x_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 ppc40x_irq_init (CPUState *env)
403 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq,
404 env, PPC40x_INPUT_NB);
407 /*****************************************************************************/
408 /* PowerPC time base and decrementer emulation */
410 /* Time base management */
411 int64_t tb_offset; /* Compensation */
412 int64_t atb_offset; /* Compensation */
413 uint32_t tb_freq; /* TB frequency */
414 /* Decrementer management */
415 uint64_t decr_next; /* Tick for next decr interrupt */
416 struct QEMUTimer *decr_timer;
417 #if defined(TARGET_PPC64H)
418 /* Hypervisor decrementer management */
419 uint64_t hdecr_next; /* Tick for next hdecr interrupt */
420 struct QEMUTimer *hdecr_timer;
427 static inline uint64_t cpu_ppc_get_tb (ppc_tb_t *tb_env, int64_t tb_offset)
429 /* TB time in tb periods */
430 return muldiv64(qemu_get_clock(vm_clock) + tb_env->tb_offset,
431 tb_env->tb_freq, ticks_per_sec);
434 uint32_t cpu_ppc_load_tbl (CPUState *env)
436 ppc_tb_t *tb_env = env->tb_env;
439 tb = cpu_ppc_get_tb(tb_env, tb_env->tb_offset);
440 #if defined(PPC_DEBUG_TB)
442 fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb);
446 return tb & 0xFFFFFFFF;
449 static inline uint32_t _cpu_ppc_load_tbu (CPUState *env)
451 ppc_tb_t *tb_env = env->tb_env;
454 tb = cpu_ppc_get_tb(tb_env, tb_env->tb_offset);
455 #if defined(PPC_DEBUG_TB)
457 fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb);
464 uint32_t cpu_ppc_load_tbu (CPUState *env)
466 return _cpu_ppc_load_tbu(env);
469 static inline void cpu_ppc_store_tb (ppc_tb_t *tb_env, int64_t *tb_offsetp,
472 *tb_offsetp = muldiv64(value, ticks_per_sec, tb_env->tb_freq)
473 - qemu_get_clock(vm_clock);
476 fprintf(logfile, "%s: tb=0x%016lx offset=%08lx\n", __func__, value,
482 void cpu_ppc_store_tbl (CPUState *env, uint32_t value)
484 ppc_tb_t *tb_env = env->tb_env;
487 tb = cpu_ppc_get_tb(tb_env, tb_env->tb_offset);
488 tb &= 0xFFFFFFFF00000000ULL;
489 cpu_ppc_store_tb(tb_env, &tb_env->tb_offset, tb | (uint64_t)value);
492 static inline void _cpu_ppc_store_tbu (CPUState *env, uint32_t value)
494 ppc_tb_t *tb_env = env->tb_env;
497 tb = cpu_ppc_get_tb(tb_env, tb_env->tb_offset);
498 tb &= 0x00000000FFFFFFFFULL;
499 cpu_ppc_store_tb(tb_env, &tb_env->tb_offset,
500 ((uint64_t)value << 32) | tb);
503 void cpu_ppc_store_tbu (CPUState *env, uint32_t value)
505 _cpu_ppc_store_tbu(env, value);
508 uint32_t cpu_ppc_load_atbl (CPUState *env)
510 ppc_tb_t *tb_env = env->tb_env;
513 tb = cpu_ppc_get_tb(tb_env, tb_env->atb_offset);
514 #if defined(PPC_DEBUG_TB)
516 fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb);
520 return tb & 0xFFFFFFFF;
523 uint32_t cpu_ppc_load_atbu (CPUState *env)
525 ppc_tb_t *tb_env = env->tb_env;
528 tb = cpu_ppc_get_tb(tb_env, tb_env->atb_offset);
529 #if defined(PPC_DEBUG_TB)
531 fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb);
538 void cpu_ppc_store_atbl (CPUState *env, uint32_t value)
540 ppc_tb_t *tb_env = env->tb_env;
543 tb = cpu_ppc_get_tb(tb_env, tb_env->atb_offset);
544 tb &= 0xFFFFFFFF00000000ULL;
545 cpu_ppc_store_tb(tb_env, &tb_env->atb_offset, tb | (uint64_t)value);
548 void cpu_ppc_store_atbu (CPUState *env, uint32_t value)
550 ppc_tb_t *tb_env = env->tb_env;
553 tb = cpu_ppc_get_tb(tb_env, tb_env->atb_offset);
554 tb &= 0x00000000FFFFFFFFULL;
555 cpu_ppc_store_tb(tb_env, &tb_env->atb_offset,
556 ((uint64_t)value << 32) | tb);
559 static inline uint32_t _cpu_ppc_load_decr (CPUState *env, uint64_t *next)
561 ppc_tb_t *tb_env = env->tb_env;
565 diff = tb_env->decr_next - qemu_get_clock(vm_clock);
567 decr = muldiv64(diff, tb_env->tb_freq, ticks_per_sec);
569 decr = -muldiv64(-diff, tb_env->tb_freq, ticks_per_sec);
570 #if defined(PPC_DEBUG_TB)
572 fprintf(logfile, "%s: 0x%08x\n", __func__, decr);
579 uint32_t cpu_ppc_load_decr (CPUState *env)
581 ppc_tb_t *tb_env = env->tb_env;
583 return _cpu_ppc_load_decr(env, &tb_env->decr_next);
586 #if defined(TARGET_PPC64H)
587 uint32_t cpu_ppc_load_hdecr (CPUState *env)
589 ppc_tb_t *tb_env = env->tb_env;
591 return _cpu_ppc_load_decr(env, &tb_env->hdecr_next);
594 uint64_t cpu_ppc_load_purr (CPUState *env)
596 ppc_tb_t *tb_env = env->tb_env;
599 diff = qemu_get_clock(vm_clock) - tb_env->purr_start;
601 return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, ticks_per_sec);
603 #endif /* defined(TARGET_PPC64H) */
605 /* When decrementer expires,
606 * all we need to do is generate or queue a CPU exception
608 static inline void cpu_ppc_decr_excp (CPUState *env)
613 fprintf(logfile, "raise decrementer exception\n");
616 ppc_set_irq(env, PPC_INTERRUPT_DECR, 1);
619 static inline void cpu_ppc_hdecr_excp (CPUState *env)
624 fprintf(logfile, "raise decrementer exception\n");
627 ppc_set_irq(env, PPC_INTERRUPT_HDECR, 1);
630 static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,
631 struct QEMUTimer *timer,
632 void (*raise_excp)(CPUState *),
633 uint32_t decr, uint32_t value,
636 ppc_tb_t *tb_env = env->tb_env;
641 fprintf(logfile, "%s: 0x%08x => 0x%08x\n", __func__, decr, value);
644 now = qemu_get_clock(vm_clock);
645 next = now + muldiv64(value, ticks_per_sec, tb_env->tb_freq);
647 next += *nextp - now;
652 qemu_mod_timer(timer, next);
653 /* If we set a negative value and the decrementer was positive,
654 * raise an exception.
656 if ((value & 0x80000000) && !(decr & 0x80000000))
661 static inline void _cpu_ppc_store_decr (CPUState *env, uint32_t decr,
662 uint32_t value, int is_excp)
664 ppc_tb_t *tb_env = env->tb_env;
666 __cpu_ppc_store_decr(env, &tb_env->decr_next, tb_env->decr_timer,
667 &cpu_ppc_decr_excp, decr, value, is_excp);
670 void cpu_ppc_store_decr (CPUState *env, uint32_t value)
672 _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0);
675 static void cpu_ppc_decr_cb (void *opaque)
677 _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1);
680 #if defined(TARGET_PPC64H)
681 static inline void _cpu_ppc_store_hdecr (CPUState *env, uint32_t hdecr,
682 uint32_t value, int is_excp)
684 ppc_tb_t *tb_env = env->tb_env;
686 __cpu_ppc_store_decr(env, &tb_env->hdecr_next, tb_env->hdecr_timer,
687 &cpu_ppc_hdecr_excp, hdecr, value, is_excp);
690 void cpu_ppc_store_hdecr (CPUState *env, uint32_t value)
692 _cpu_ppc_store_hdecr(env, cpu_ppc_load_hdecr(env), value, 0);
695 static void cpu_ppc_hdecr_cb (void *opaque)
697 _cpu_ppc_store_hdecr(opaque, 0x00000000, 0xFFFFFFFF, 1);
700 void cpu_ppc_store_purr (CPUState *env, uint64_t value)
702 ppc_tb_t *tb_env = env->tb_env;
704 tb_env->purr_load = value;
705 tb_env->purr_start = qemu_get_clock(vm_clock);
707 #endif /* defined(TARGET_PPC64H) */
709 static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
711 CPUState *env = opaque;
712 ppc_tb_t *tb_env = env->tb_env;
714 tb_env->tb_freq = freq;
715 /* There is a bug in Linux 2.4 kernels:
716 * if a decrementer exception is pending when it enables msr_ee at startup,
717 * it's not ready to handle it...
719 _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
720 #if defined(TARGET_PPC64H)
721 _cpu_ppc_store_hdecr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
722 cpu_ppc_store_purr(env, 0x0000000000000000ULL);
723 #endif /* defined(TARGET_PPC64H) */
726 /* Set up (once) timebase frequency (in Hz) */
727 clk_setup_cb cpu_ppc_tb_init (CPUState *env, uint32_t freq)
731 tb_env = qemu_mallocz(sizeof(ppc_tb_t));
734 env->tb_env = tb_env;
735 /* Create new timer */
736 tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_ppc_decr_cb, env);
737 #if defined(TARGET_PPC64H)
738 tb_env->hdecr_timer = qemu_new_timer(vm_clock, &cpu_ppc_hdecr_cb, env);
739 #endif /* defined(TARGET_PPC64H) */
740 cpu_ppc_set_tb_clk(env, freq);
742 return &cpu_ppc_set_tb_clk;
745 /* Specific helpers for POWER & PowerPC 601 RTC */
746 clk_setup_cb cpu_ppc601_rtc_init (CPUState *env)
748 return cpu_ppc_tb_init(env, 7812500);
751 void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value)
753 _cpu_ppc_store_tbu(env, value);
756 uint32_t cpu_ppc601_load_rtcu (CPUState *env)
758 return _cpu_ppc_load_tbu(env);
761 void cpu_ppc601_store_rtcl (CPUState *env, uint32_t value)
763 cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
766 uint32_t cpu_ppc601_load_rtcl (CPUState *env)
768 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
771 /*****************************************************************************/
772 /* Embedded PowerPC timers */
775 typedef struct ppcemb_timer_t ppcemb_timer_t;
776 struct ppcemb_timer_t {
777 uint64_t pit_reload; /* PIT auto-reload value */
778 uint64_t fit_next; /* Tick for next FIT interrupt */
779 struct QEMUTimer *fit_timer;
780 uint64_t wdt_next; /* Tick for next WDT interrupt */
781 struct QEMUTimer *wdt_timer;
784 /* Fixed interval timer */
785 static void cpu_4xx_fit_cb (void *opaque)
789 ppcemb_timer_t *ppcemb_timer;
793 tb_env = env->tb_env;
794 ppcemb_timer = tb_env->opaque;
795 now = qemu_get_clock(vm_clock);
796 switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
810 /* Cannot occur, but makes gcc happy */
813 next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq);
816 qemu_mod_timer(ppcemb_timer->fit_timer, next);
817 env->spr[SPR_40x_TSR] |= 1 << 26;
818 if ((env->spr[SPR_40x_TCR] >> 23) & 0x1)
819 ppc_set_irq(env, PPC_INTERRUPT_FIT, 1);
822 fprintf(logfile, "%s: ir %d TCR " ADDRX " TSR " ADDRX "\n", __func__,
823 (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
824 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
829 /* Programmable interval timer */
830 static void start_stop_pit (CPUState *env, ppc_tb_t *tb_env, int is_excp)
832 ppcemb_timer_t *ppcemb_timer;
835 ppcemb_timer = tb_env->opaque;
836 if (ppcemb_timer->pit_reload <= 1 ||
837 !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
838 (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
842 fprintf(logfile, "%s: stop PIT\n", __func__);
845 qemu_del_timer(tb_env->decr_timer);
849 fprintf(logfile, "%s: start PIT 0x" REGX "\n",
850 __func__, ppcemb_timer->pit_reload);
853 now = qemu_get_clock(vm_clock);
854 next = now + muldiv64(ppcemb_timer->pit_reload,
855 ticks_per_sec, tb_env->tb_freq);
857 next += tb_env->decr_next - now;
860 qemu_mod_timer(tb_env->decr_timer, next);
861 tb_env->decr_next = next;
865 static void cpu_4xx_pit_cb (void *opaque)
869 ppcemb_timer_t *ppcemb_timer;
872 tb_env = env->tb_env;
873 ppcemb_timer = tb_env->opaque;
874 env->spr[SPR_40x_TSR] |= 1 << 27;
875 if ((env->spr[SPR_40x_TCR] >> 26) & 0x1)
876 ppc_set_irq(env, PPC_INTERRUPT_PIT, 1);
877 start_stop_pit(env, tb_env, 1);
880 fprintf(logfile, "%s: ar %d ir %d TCR " ADDRX " TSR " ADDRX " "
881 "%016" PRIx64 "\n", __func__,
882 (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
883 (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
884 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
885 ppcemb_timer->pit_reload);
891 static void cpu_4xx_wdt_cb (void *opaque)
895 ppcemb_timer_t *ppcemb_timer;
899 tb_env = env->tb_env;
900 ppcemb_timer = tb_env->opaque;
901 now = qemu_get_clock(vm_clock);
902 switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
916 /* Cannot occur, but makes gcc happy */
919 next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq);
924 fprintf(logfile, "%s: TCR " ADDRX " TSR " ADDRX "\n", __func__,
925 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
928 switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
931 qemu_mod_timer(ppcemb_timer->wdt_timer, next);
932 ppcemb_timer->wdt_next = next;
933 env->spr[SPR_40x_TSR] |= 1 << 31;
936 qemu_mod_timer(ppcemb_timer->wdt_timer, next);
937 ppcemb_timer->wdt_next = next;
938 env->spr[SPR_40x_TSR] |= 1 << 30;
939 if ((env->spr[SPR_40x_TCR] >> 27) & 0x1)
940 ppc_set_irq(env, PPC_INTERRUPT_WDT, 1);
943 env->spr[SPR_40x_TSR] &= ~0x30000000;
944 env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
945 switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
949 case 0x1: /* Core reset */
950 ppc40x_core_reset(env);
952 case 0x2: /* Chip reset */
953 ppc40x_chip_reset(env);
955 case 0x3: /* System reset */
956 ppc40x_system_reset(env);
962 void store_40x_pit (CPUState *env, target_ulong val)
965 ppcemb_timer_t *ppcemb_timer;
967 tb_env = env->tb_env;
968 ppcemb_timer = tb_env->opaque;
971 fprintf(logfile, "%s %p %p\n", __func__, tb_env, ppcemb_timer);
974 ppcemb_timer->pit_reload = val;
975 start_stop_pit(env, tb_env, 0);
978 target_ulong load_40x_pit (CPUState *env)
980 return cpu_ppc_load_decr(env);
983 void store_booke_tsr (CPUState *env, target_ulong val)
987 fprintf(logfile, "%s: val=" ADDRX "\n", __func__, val);
990 env->spr[SPR_40x_TSR] &= ~(val & 0xFC000000);
991 if (val & 0x80000000)
992 ppc_set_irq(env, PPC_INTERRUPT_PIT, 0);
995 void store_booke_tcr (CPUState *env, target_ulong val)
999 tb_env = env->tb_env;
1001 if (loglevel != 0) {
1002 fprintf(logfile, "%s: val=" ADDRX "\n", __func__, val);
1005 env->spr[SPR_40x_TCR] = val & 0xFFC00000;
1006 start_stop_pit(env, tb_env, 1);
1007 cpu_4xx_wdt_cb(env);
1010 static void ppc_emb_set_tb_clk (void *opaque, uint32_t freq)
1012 CPUState *env = opaque;
1013 ppc_tb_t *tb_env = env->tb_env;
1016 if (loglevel != 0) {
1017 fprintf(logfile, "%s set new frequency to %u\n", __func__, freq);
1020 tb_env->tb_freq = freq;
1021 /* XXX: we should also update all timers */
1024 clk_setup_cb ppc_emb_timers_init (CPUState *env, uint32_t freq)
1027 ppcemb_timer_t *ppcemb_timer;
1029 tb_env = qemu_mallocz(sizeof(ppc_tb_t));
1030 if (tb_env == NULL) {
1033 env->tb_env = tb_env;
1034 ppcemb_timer = qemu_mallocz(sizeof(ppcemb_timer_t));
1035 tb_env->tb_freq = freq;
1036 tb_env->opaque = ppcemb_timer;
1038 if (loglevel != 0) {
1039 fprintf(logfile, "%s %p %p %p\n", __func__, tb_env, ppcemb_timer,
1040 &ppc_emb_set_tb_clk);
1043 if (ppcemb_timer != NULL) {
1044 /* We use decr timer for PIT */
1045 tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_4xx_pit_cb, env);
1046 ppcemb_timer->fit_timer =
1047 qemu_new_timer(vm_clock, &cpu_4xx_fit_cb, env);
1048 ppcemb_timer->wdt_timer =
1049 qemu_new_timer(vm_clock, &cpu_4xx_wdt_cb, env);
1052 return &ppc_emb_set_tb_clk;
1055 /*****************************************************************************/
1056 /* Embedded PowerPC Device Control Registers */
1057 typedef struct ppc_dcrn_t ppc_dcrn_t;
1059 dcr_read_cb dcr_read;
1060 dcr_write_cb dcr_write;
1064 /* XXX: on 460, DCR addresses are 32 bits wide,
1065 * using DCRIPR to get the 22 upper bits of the DCR address
1067 #define DCRN_NB 1024
1069 ppc_dcrn_t dcrn[DCRN_NB];
1070 int (*read_error)(int dcrn);
1071 int (*write_error)(int dcrn);
1074 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
1078 if (dcrn < 0 || dcrn >= DCRN_NB)
1080 dcr = &dcr_env->dcrn[dcrn];
1081 if (dcr->dcr_read == NULL)
1083 *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
1088 if (dcr_env->read_error != NULL)
1089 return (*dcr_env->read_error)(dcrn);
1094 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
1098 if (dcrn < 0 || dcrn >= DCRN_NB)
1100 dcr = &dcr_env->dcrn[dcrn];
1101 if (dcr->dcr_write == NULL)
1103 (*dcr->dcr_write)(dcr->opaque, dcrn, val);
1108 if (dcr_env->write_error != NULL)
1109 return (*dcr_env->write_error)(dcrn);
1114 int ppc_dcr_register (CPUState *env, int dcrn, void *opaque,
1115 dcr_read_cb dcr_read, dcr_write_cb dcr_write)
1120 dcr_env = env->dcr_env;
1121 if (dcr_env == NULL)
1123 if (dcrn < 0 || dcrn >= DCRN_NB)
1125 dcr = &dcr_env->dcrn[dcrn];
1126 if (dcr->opaque != NULL ||
1127 dcr->dcr_read != NULL ||
1128 dcr->dcr_write != NULL)
1130 dcr->opaque = opaque;
1131 dcr->dcr_read = dcr_read;
1132 dcr->dcr_write = dcr_write;
1137 int ppc_dcr_init (CPUState *env, int (*read_error)(int dcrn),
1138 int (*write_error)(int dcrn))
1142 dcr_env = qemu_mallocz(sizeof(ppc_dcr_t));
1143 if (dcr_env == NULL)
1145 dcr_env->read_error = read_error;
1146 dcr_env->write_error = write_error;
1147 env->dcr_env = dcr_env;
1154 /*****************************************************************************/
1155 /* Handle system reset (for now, just stop emulation) */
1156 void cpu_ppc_reset (CPUState *env)
1158 printf("Reset asked... Stop emulation\n");
1163 /*****************************************************************************/
1165 void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
1177 printf("Set loglevel to %04x\n", val);
1178 cpu_set_log(val | 0x100);
1183 /*****************************************************************************/
1185 void NVRAM_set_byte (m48t59_t *nvram, uint32_t addr, uint8_t value)
1187 m48t59_write(nvram, addr, value);
1190 uint8_t NVRAM_get_byte (m48t59_t *nvram, uint32_t addr)
1192 return m48t59_read(nvram, addr);
1195 void NVRAM_set_word (m48t59_t *nvram, uint32_t addr, uint16_t value)
1197 m48t59_write(nvram, addr, value >> 8);
1198 m48t59_write(nvram, addr + 1, value & 0xFF);
1201 uint16_t NVRAM_get_word (m48t59_t *nvram, uint32_t addr)
1205 tmp = m48t59_read(nvram, addr) << 8;
1206 tmp |= m48t59_read(nvram, addr + 1);
1210 void NVRAM_set_lword (m48t59_t *nvram, uint32_t addr, uint32_t value)
1212 m48t59_write(nvram, addr, value >> 24);
1213 m48t59_write(nvram, addr + 1, (value >> 16) & 0xFF);
1214 m48t59_write(nvram, addr + 2, (value >> 8) & 0xFF);
1215 m48t59_write(nvram, addr + 3, value & 0xFF);
1218 uint32_t NVRAM_get_lword (m48t59_t *nvram, uint32_t addr)
1222 tmp = m48t59_read(nvram, addr) << 24;
1223 tmp |= m48t59_read(nvram, addr + 1) << 16;
1224 tmp |= m48t59_read(nvram, addr + 2) << 8;
1225 tmp |= m48t59_read(nvram, addr + 3);
1230 void NVRAM_set_string (m48t59_t *nvram, uint32_t addr,
1231 const unsigned char *str, uint32_t max)
1235 for (i = 0; i < max && str[i] != '\0'; i++) {
1236 m48t59_write(nvram, addr + i, str[i]);
1238 m48t59_write(nvram, addr + max - 1, '\0');
1241 int NVRAM_get_string (m48t59_t *nvram, uint8_t *dst, uint16_t addr, int max)
1245 memset(dst, 0, max);
1246 for (i = 0; i < max; i++) {
1247 dst[i] = NVRAM_get_byte(nvram, addr + i);
1255 static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
1258 uint16_t pd, pd1, pd2;
1263 pd2 = ((pd >> 4) & 0x000F) ^ pd1;
1264 tmp ^= (pd1 << 3) | (pd1 << 8);
1265 tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
1270 uint16_t NVRAM_compute_crc (m48t59_t *nvram, uint32_t start, uint32_t count)
1273 uint16_t crc = 0xFFFF;
1278 for (i = 0; i != count; i++) {
1279 crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
1282 crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
1288 #define CMDLINE_ADDR 0x017ff000
1290 int PPC_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
1291 const unsigned char *arch,
1292 uint32_t RAM_size, int boot_device,
1293 uint32_t kernel_image, uint32_t kernel_size,
1294 const char *cmdline,
1295 uint32_t initrd_image, uint32_t initrd_size,
1296 uint32_t NVRAM_image,
1297 int width, int height, int depth)
1301 /* Set parameters for Open Hack'Ware BIOS */
1302 NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
1303 NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
1304 NVRAM_set_word(nvram, 0x14, NVRAM_size);
1305 NVRAM_set_string(nvram, 0x20, arch, 16);
1306 NVRAM_set_lword(nvram, 0x30, RAM_size);
1307 NVRAM_set_byte(nvram, 0x34, boot_device);
1308 NVRAM_set_lword(nvram, 0x38, kernel_image);
1309 NVRAM_set_lword(nvram, 0x3C, kernel_size);
1311 /* XXX: put the cmdline in NVRAM too ? */
1312 strcpy(phys_ram_base + CMDLINE_ADDR, cmdline);
1313 NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
1314 NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
1316 NVRAM_set_lword(nvram, 0x40, 0);
1317 NVRAM_set_lword(nvram, 0x44, 0);
1319 NVRAM_set_lword(nvram, 0x48, initrd_image);
1320 NVRAM_set_lword(nvram, 0x4C, initrd_size);
1321 NVRAM_set_lword(nvram, 0x50, NVRAM_image);
1323 NVRAM_set_word(nvram, 0x54, width);
1324 NVRAM_set_word(nvram, 0x56, height);
1325 NVRAM_set_word(nvram, 0x58, depth);
1326 crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
1327 NVRAM_set_word(nvram, 0xFC, crc);