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
25 #include "hw/ppc/ppc.h"
26 #include "hw/ppc/ppc_e500.h"
27 #include "qemu/timer.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/cpus.h"
30 #include "hw/timer/m48t59.h"
32 #include "hw/loader.h"
33 #include "sysemu/kvm.h"
36 //#define PPC_DEBUG_IRQ
37 //#define PPC_DEBUG_TB
40 # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
42 # define LOG_IRQ(...) do { } while (0)
47 # define LOG_TB(...) qemu_log(__VA_ARGS__)
49 # define LOG_TB(...) do { } while (0)
52 static void cpu_ppc_tb_stop (CPUPPCState
*env
);
53 static void cpu_ppc_tb_start (CPUPPCState
*env
);
55 void ppc_set_irq(PowerPCCPU
*cpu
, int n_IRQ
, int level
)
57 CPUState
*cs
= CPU(cpu
);
58 CPUPPCState
*env
= &cpu
->env
;
59 unsigned int old_pending
= env
->pending_interrupts
;
62 env
->pending_interrupts
|= 1 << n_IRQ
;
63 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
65 env
->pending_interrupts
&= ~(1 << n_IRQ
);
66 if (env
->pending_interrupts
== 0) {
67 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
71 if (old_pending
!= env
->pending_interrupts
) {
73 kvmppc_set_interrupt(cpu
, n_IRQ
, level
);
77 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
78 "req %08x\n", __func__
, env
, n_IRQ
, level
,
79 env
->pending_interrupts
, CPU(cpu
)->interrupt_request
);
82 /* PowerPC 6xx / 7xx internal IRQ controller */
83 static void ppc6xx_set_irq(void *opaque
, int pin
, int level
)
85 PowerPCCPU
*cpu
= opaque
;
86 CPUPPCState
*env
= &cpu
->env
;
89 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
91 cur_level
= (env
->irq_input_state
>> pin
) & 1;
92 /* Don't generate spurious events */
93 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
94 CPUState
*cs
= CPU(cpu
);
97 case PPC6xx_INPUT_TBEN
:
98 /* Level sensitive - active high */
99 LOG_IRQ("%s: %s the time base\n",
100 __func__
, level
? "start" : "stop");
102 cpu_ppc_tb_start(env
);
104 cpu_ppc_tb_stop(env
);
106 case PPC6xx_INPUT_INT
:
107 /* Level sensitive - active high */
108 LOG_IRQ("%s: set the external IRQ state to %d\n",
110 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
112 case PPC6xx_INPUT_SMI
:
113 /* Level sensitive - active high */
114 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
116 ppc_set_irq(cpu
, PPC_INTERRUPT_SMI
, level
);
118 case PPC6xx_INPUT_MCP
:
119 /* Negative edge sensitive */
120 /* XXX: TODO: actual reaction may depends on HID0 status
121 * 603/604/740/750: check HID0[EMCP]
123 if (cur_level
== 1 && level
== 0) {
124 LOG_IRQ("%s: raise machine check state\n",
126 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
129 case PPC6xx_INPUT_CKSTP_IN
:
130 /* Level sensitive - active low */
131 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
132 /* XXX: Note that the only way to restart the CPU is to reset it */
134 LOG_IRQ("%s: stop the CPU\n", __func__
);
138 case PPC6xx_INPUT_HRESET
:
139 /* Level sensitive - active low */
141 LOG_IRQ("%s: reset the CPU\n", __func__
);
142 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
145 case PPC6xx_INPUT_SRESET
:
146 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
148 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
151 /* Unknown pin - do nothing */
152 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
156 env
->irq_input_state
|= 1 << pin
;
158 env
->irq_input_state
&= ~(1 << pin
);
162 void ppc6xx_irq_init(CPUPPCState
*env
)
164 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
166 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc6xx_set_irq
, cpu
,
170 #if defined(TARGET_PPC64)
171 /* PowerPC 970 internal IRQ controller */
172 static void ppc970_set_irq(void *opaque
, int pin
, int level
)
174 PowerPCCPU
*cpu
= opaque
;
175 CPUPPCState
*env
= &cpu
->env
;
178 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
180 cur_level
= (env
->irq_input_state
>> pin
) & 1;
181 /* Don't generate spurious events */
182 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
183 CPUState
*cs
= CPU(cpu
);
186 case PPC970_INPUT_INT
:
187 /* Level sensitive - active high */
188 LOG_IRQ("%s: set the external IRQ state to %d\n",
190 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
192 case PPC970_INPUT_THINT
:
193 /* Level sensitive - active high */
194 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__
,
196 ppc_set_irq(cpu
, PPC_INTERRUPT_THERM
, level
);
198 case PPC970_INPUT_MCP
:
199 /* Negative edge sensitive */
200 /* XXX: TODO: actual reaction may depends on HID0 status
201 * 603/604/740/750: check HID0[EMCP]
203 if (cur_level
== 1 && level
== 0) {
204 LOG_IRQ("%s: raise machine check state\n",
206 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, 1);
209 case PPC970_INPUT_CKSTP
:
210 /* Level sensitive - active low */
211 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
213 LOG_IRQ("%s: stop the CPU\n", __func__
);
216 LOG_IRQ("%s: restart the CPU\n", __func__
);
221 case PPC970_INPUT_HRESET
:
222 /* Level sensitive - active low */
224 cpu_interrupt(cs
, CPU_INTERRUPT_RESET
);
227 case PPC970_INPUT_SRESET
:
228 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
230 ppc_set_irq(cpu
, PPC_INTERRUPT_RESET
, level
);
232 case PPC970_INPUT_TBEN
:
233 LOG_IRQ("%s: set the TBEN state to %d\n", __func__
,
238 /* Unknown pin - do nothing */
239 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
243 env
->irq_input_state
|= 1 << pin
;
245 env
->irq_input_state
&= ~(1 << pin
);
249 void ppc970_irq_init(CPUPPCState
*env
)
251 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
253 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc970_set_irq
, cpu
,
257 /* POWER7 internal IRQ controller */
258 static void power7_set_irq(void *opaque
, int pin
, int level
)
260 PowerPCCPU
*cpu
= opaque
;
261 CPUPPCState
*env
= &cpu
->env
;
263 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
267 case POWER7_INPUT_INT
:
268 /* Level sensitive - active high */
269 LOG_IRQ("%s: set the external IRQ state to %d\n",
271 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
274 /* Unknown pin - do nothing */
275 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
279 env
->irq_input_state
|= 1 << pin
;
281 env
->irq_input_state
&= ~(1 << pin
);
285 void ppcPOWER7_irq_init(CPUPPCState
*env
)
287 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
289 env
->irq_inputs
= (void **)qemu_allocate_irqs(&power7_set_irq
, cpu
,
292 #endif /* defined(TARGET_PPC64) */
294 /* PowerPC 40x internal IRQ controller */
295 static void ppc40x_set_irq(void *opaque
, int pin
, int level
)
297 PowerPCCPU
*cpu
= opaque
;
298 CPUPPCState
*env
= &cpu
->env
;
301 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
303 cur_level
= (env
->irq_input_state
>> pin
) & 1;
304 /* Don't generate spurious events */
305 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
306 CPUState
*cs
= CPU(cpu
);
309 case PPC40x_INPUT_RESET_SYS
:
311 LOG_IRQ("%s: reset the PowerPC system\n",
313 ppc40x_system_reset(cpu
);
316 case PPC40x_INPUT_RESET_CHIP
:
318 LOG_IRQ("%s: reset the PowerPC chip\n", __func__
);
319 ppc40x_chip_reset(cpu
);
322 case PPC40x_INPUT_RESET_CORE
:
323 /* XXX: TODO: update DBSR[MRR] */
325 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
326 ppc40x_core_reset(cpu
);
329 case PPC40x_INPUT_CINT
:
330 /* Level sensitive - active high */
331 LOG_IRQ("%s: set the critical IRQ state to %d\n",
333 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
335 case PPC40x_INPUT_INT
:
336 /* Level sensitive - active high */
337 LOG_IRQ("%s: set the external IRQ state to %d\n",
339 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
341 case PPC40x_INPUT_HALT
:
342 /* Level sensitive - active low */
344 LOG_IRQ("%s: stop the CPU\n", __func__
);
347 LOG_IRQ("%s: restart the CPU\n", __func__
);
352 case PPC40x_INPUT_DEBUG
:
353 /* Level sensitive - active high */
354 LOG_IRQ("%s: set the debug pin state to %d\n",
356 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
359 /* Unknown pin - do nothing */
360 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
364 env
->irq_input_state
|= 1 << pin
;
366 env
->irq_input_state
&= ~(1 << pin
);
370 void ppc40x_irq_init(CPUPPCState
*env
)
372 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
374 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppc40x_set_irq
,
375 cpu
, PPC40x_INPUT_NB
);
378 /* PowerPC E500 internal IRQ controller */
379 static void ppce500_set_irq(void *opaque
, int pin
, int level
)
381 PowerPCCPU
*cpu
= opaque
;
382 CPUPPCState
*env
= &cpu
->env
;
385 LOG_IRQ("%s: env %p pin %d level %d\n", __func__
,
387 cur_level
= (env
->irq_input_state
>> pin
) & 1;
388 /* Don't generate spurious events */
389 if ((cur_level
== 1 && level
== 0) || (cur_level
== 0 && level
!= 0)) {
391 case PPCE500_INPUT_MCK
:
393 LOG_IRQ("%s: reset the PowerPC system\n",
395 qemu_system_reset_request();
398 case PPCE500_INPUT_RESET_CORE
:
400 LOG_IRQ("%s: reset the PowerPC core\n", __func__
);
401 ppc_set_irq(cpu
, PPC_INTERRUPT_MCK
, level
);
404 case PPCE500_INPUT_CINT
:
405 /* Level sensitive - active high */
406 LOG_IRQ("%s: set the critical IRQ state to %d\n",
408 ppc_set_irq(cpu
, PPC_INTERRUPT_CEXT
, level
);
410 case PPCE500_INPUT_INT
:
411 /* Level sensitive - active high */
412 LOG_IRQ("%s: set the core IRQ state to %d\n",
414 ppc_set_irq(cpu
, PPC_INTERRUPT_EXT
, level
);
416 case PPCE500_INPUT_DEBUG
:
417 /* Level sensitive - active high */
418 LOG_IRQ("%s: set the debug pin state to %d\n",
420 ppc_set_irq(cpu
, PPC_INTERRUPT_DEBUG
, level
);
423 /* Unknown pin - do nothing */
424 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__
, pin
);
428 env
->irq_input_state
|= 1 << pin
;
430 env
->irq_input_state
&= ~(1 << pin
);
434 void ppce500_irq_init(CPUPPCState
*env
)
436 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
438 env
->irq_inputs
= (void **)qemu_allocate_irqs(&ppce500_set_irq
,
439 cpu
, PPCE500_INPUT_NB
);
442 /* Enable or Disable the E500 EPR capability */
443 void ppce500_set_mpic_proxy(bool enabled
)
448 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
450 cpu
->env
.mpic_proxy
= enabled
;
452 kvmppc_set_mpic_proxy(cpu
, enabled
);
457 /*****************************************************************************/
458 /* PowerPC time base and decrementer emulation */
460 uint64_t cpu_ppc_get_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
, int64_t tb_offset
)
462 /* TB time in tb periods */
463 return muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec()) + tb_offset
;
466 uint64_t cpu_ppc_load_tbl (CPUPPCState
*env
)
468 ppc_tb_t
*tb_env
= env
->tb_env
;
472 return env
->spr
[SPR_TBL
];
475 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
476 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
481 static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState
*env
)
483 ppc_tb_t
*tb_env
= env
->tb_env
;
486 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
487 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
492 uint32_t cpu_ppc_load_tbu (CPUPPCState
*env
)
495 return env
->spr
[SPR_TBU
];
498 return _cpu_ppc_load_tbu(env
);
501 static inline void cpu_ppc_store_tb(ppc_tb_t
*tb_env
, uint64_t vmclk
,
502 int64_t *tb_offsetp
, uint64_t value
)
504 *tb_offsetp
= value
- muldiv64(vmclk
, tb_env
->tb_freq
, get_ticks_per_sec());
505 LOG_TB("%s: tb %016" PRIx64
" offset %08" PRIx64
"\n",
506 __func__
, value
, *tb_offsetp
);
509 void cpu_ppc_store_tbl (CPUPPCState
*env
, uint32_t value
)
511 ppc_tb_t
*tb_env
= env
->tb_env
;
514 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
515 tb
&= 0xFFFFFFFF00000000ULL
;
516 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
517 &tb_env
->tb_offset
, tb
| (uint64_t)value
);
520 static inline void _cpu_ppc_store_tbu(CPUPPCState
*env
, uint32_t value
)
522 ppc_tb_t
*tb_env
= env
->tb_env
;
525 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->tb_offset
);
526 tb
&= 0x00000000FFFFFFFFULL
;
527 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
528 &tb_env
->tb_offset
, ((uint64_t)value
<< 32) | tb
);
531 void cpu_ppc_store_tbu (CPUPPCState
*env
, uint32_t value
)
533 _cpu_ppc_store_tbu(env
, value
);
536 uint64_t cpu_ppc_load_atbl (CPUPPCState
*env
)
538 ppc_tb_t
*tb_env
= env
->tb_env
;
541 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
542 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
547 uint32_t cpu_ppc_load_atbu (CPUPPCState
*env
)
549 ppc_tb_t
*tb_env
= env
->tb_env
;
552 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
553 LOG_TB("%s: tb %016" PRIx64
"\n", __func__
, tb
);
558 void cpu_ppc_store_atbl (CPUPPCState
*env
, uint32_t value
)
560 ppc_tb_t
*tb_env
= env
->tb_env
;
563 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
564 tb
&= 0xFFFFFFFF00000000ULL
;
565 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
566 &tb_env
->atb_offset
, tb
| (uint64_t)value
);
569 void cpu_ppc_store_atbu (CPUPPCState
*env
, uint32_t value
)
571 ppc_tb_t
*tb_env
= env
->tb_env
;
574 tb
= cpu_ppc_get_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
), tb_env
->atb_offset
);
575 tb
&= 0x00000000FFFFFFFFULL
;
576 cpu_ppc_store_tb(tb_env
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
),
577 &tb_env
->atb_offset
, ((uint64_t)value
<< 32) | tb
);
580 static void cpu_ppc_tb_stop (CPUPPCState
*env
)
582 ppc_tb_t
*tb_env
= env
->tb_env
;
583 uint64_t tb
, atb
, vmclk
;
585 /* If the time base is already frozen, do nothing */
586 if (tb_env
->tb_freq
!= 0) {
587 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
588 /* Get the time base */
589 tb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->tb_offset
);
590 /* Get the alternate time base */
591 atb
= cpu_ppc_get_tb(tb_env
, vmclk
, tb_env
->atb_offset
);
592 /* Store the time base value (ie compute the current offset) */
593 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
594 /* Store the alternate time base value (compute the current offset) */
595 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
596 /* Set the time base frequency to zero */
598 /* Now, the time bases are frozen to tb_offset / atb_offset value */
602 static void cpu_ppc_tb_start (CPUPPCState
*env
)
604 ppc_tb_t
*tb_env
= env
->tb_env
;
605 uint64_t tb
, atb
, vmclk
;
607 /* If the time base is not frozen, do nothing */
608 if (tb_env
->tb_freq
== 0) {
609 vmclk
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
610 /* Get the time base from tb_offset */
611 tb
= tb_env
->tb_offset
;
612 /* Get the alternate time base from atb_offset */
613 atb
= tb_env
->atb_offset
;
614 /* Restore the tb frequency from the decrementer frequency */
615 tb_env
->tb_freq
= tb_env
->decr_freq
;
616 /* Store the time base value */
617 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->tb_offset
, tb
);
618 /* Store the alternate time base value */
619 cpu_ppc_store_tb(tb_env
, vmclk
, &tb_env
->atb_offset
, atb
);
623 static inline uint32_t _cpu_ppc_load_decr(CPUPPCState
*env
, uint64_t next
)
625 ppc_tb_t
*tb_env
= env
->tb_env
;
629 diff
= next
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
631 decr
= muldiv64(diff
, tb_env
->decr_freq
, get_ticks_per_sec());
632 } else if (tb_env
->flags
& PPC_TIMER_BOOKE
) {
635 decr
= -muldiv64(-diff
, tb_env
->decr_freq
, get_ticks_per_sec());
637 LOG_TB("%s: %08" PRIx32
"\n", __func__
, decr
);
642 uint32_t cpu_ppc_load_decr (CPUPPCState
*env
)
644 ppc_tb_t
*tb_env
= env
->tb_env
;
647 return env
->spr
[SPR_DECR
];
650 return _cpu_ppc_load_decr(env
, tb_env
->decr_next
);
653 uint32_t cpu_ppc_load_hdecr (CPUPPCState
*env
)
655 ppc_tb_t
*tb_env
= env
->tb_env
;
657 return _cpu_ppc_load_decr(env
, tb_env
->hdecr_next
);
660 uint64_t cpu_ppc_load_purr (CPUPPCState
*env
)
662 ppc_tb_t
*tb_env
= env
->tb_env
;
665 diff
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - tb_env
->purr_start
;
667 return tb_env
->purr_load
+ muldiv64(diff
, tb_env
->tb_freq
, get_ticks_per_sec());
670 /* When decrementer expires,
671 * all we need to do is generate or queue a CPU exception
673 static inline void cpu_ppc_decr_excp(PowerPCCPU
*cpu
)
676 LOG_TB("raise decrementer exception\n");
677 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
, 1);
680 static inline void cpu_ppc_hdecr_excp(PowerPCCPU
*cpu
)
683 LOG_TB("raise decrementer exception\n");
684 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 1);
687 static void __cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint64_t *nextp
,
689 void (*raise_excp
)(PowerPCCPU
*),
690 uint32_t decr
, uint32_t value
,
693 CPUPPCState
*env
= &cpu
->env
;
694 ppc_tb_t
*tb_env
= env
->tb_env
;
697 LOG_TB("%s: %08" PRIx32
" => %08" PRIx32
"\n", __func__
,
701 /* KVM handles decrementer exceptions, we don't need our own timer */
705 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
706 next
= now
+ muldiv64(value
, get_ticks_per_sec(), tb_env
->decr_freq
);
708 next
+= *nextp
- now
;
715 timer_mod(timer
, next
);
717 /* If we set a negative value and the decrementer was positive, raise an
720 if ((tb_env
->flags
& PPC_DECR_UNDERFLOW_TRIGGERED
)
721 && (value
& 0x80000000)
722 && !(decr
& 0x80000000)) {
727 static inline void _cpu_ppc_store_decr(PowerPCCPU
*cpu
, uint32_t decr
,
728 uint32_t value
, int is_excp
)
730 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
732 __cpu_ppc_store_decr(cpu
, &tb_env
->decr_next
, tb_env
->decr_timer
,
733 &cpu_ppc_decr_excp
, decr
, value
, is_excp
);
736 void cpu_ppc_store_decr (CPUPPCState
*env
, uint32_t value
)
738 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
740 _cpu_ppc_store_decr(cpu
, cpu_ppc_load_decr(env
), value
, 0);
743 static void cpu_ppc_decr_cb(void *opaque
)
745 PowerPCCPU
*cpu
= opaque
;
747 _cpu_ppc_store_decr(cpu
, 0x00000000, 0xFFFFFFFF, 1);
750 static inline void _cpu_ppc_store_hdecr(PowerPCCPU
*cpu
, uint32_t hdecr
,
751 uint32_t value
, int is_excp
)
753 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
755 if (tb_env
->hdecr_timer
!= NULL
) {
756 __cpu_ppc_store_decr(cpu
, &tb_env
->hdecr_next
, tb_env
->hdecr_timer
,
757 &cpu_ppc_hdecr_excp
, hdecr
, value
, is_excp
);
761 void cpu_ppc_store_hdecr (CPUPPCState
*env
, uint32_t value
)
763 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
765 _cpu_ppc_store_hdecr(cpu
, cpu_ppc_load_hdecr(env
), value
, 0);
768 static void cpu_ppc_hdecr_cb(void *opaque
)
770 PowerPCCPU
*cpu
= opaque
;
772 _cpu_ppc_store_hdecr(cpu
, 0x00000000, 0xFFFFFFFF, 1);
775 static void cpu_ppc_store_purr(PowerPCCPU
*cpu
, uint64_t value
)
777 ppc_tb_t
*tb_env
= cpu
->env
.tb_env
;
779 tb_env
->purr_load
= value
;
780 tb_env
->purr_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
783 static void cpu_ppc_set_tb_clk (void *opaque
, uint32_t freq
)
785 CPUPPCState
*env
= opaque
;
786 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
787 ppc_tb_t
*tb_env
= env
->tb_env
;
789 tb_env
->tb_freq
= freq
;
790 tb_env
->decr_freq
= freq
;
791 /* There is a bug in Linux 2.4 kernels:
792 * if a decrementer exception is pending when it enables msr_ee at startup,
793 * it's not ready to handle it...
795 _cpu_ppc_store_decr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
796 _cpu_ppc_store_hdecr(cpu
, 0xFFFFFFFF, 0xFFFFFFFF, 0);
797 cpu_ppc_store_purr(cpu
, 0x0000000000000000ULL
);
800 /* Set up (once) timebase frequency (in Hz) */
801 clk_setup_cb
cpu_ppc_tb_init (CPUPPCState
*env
, uint32_t freq
)
803 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
806 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
807 env
->tb_env
= tb_env
;
808 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
809 /* Create new timer */
810 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_decr_cb
, cpu
);
812 /* XXX: find a suitable condition to enable the hypervisor decrementer
814 tb_env
->hdecr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_ppc_hdecr_cb
,
817 tb_env
->hdecr_timer
= NULL
;
819 cpu_ppc_set_tb_clk(env
, freq
);
821 return &cpu_ppc_set_tb_clk
;
824 /* Specific helpers for POWER & PowerPC 601 RTC */
826 static clk_setup_cb
cpu_ppc601_rtc_init (CPUPPCState
*env
)
828 return cpu_ppc_tb_init(env
, 7812500);
832 void cpu_ppc601_store_rtcu (CPUPPCState
*env
, uint32_t value
)
834 _cpu_ppc_store_tbu(env
, value
);
837 uint32_t cpu_ppc601_load_rtcu (CPUPPCState
*env
)
839 return _cpu_ppc_load_tbu(env
);
842 void cpu_ppc601_store_rtcl (CPUPPCState
*env
, uint32_t value
)
844 cpu_ppc_store_tbl(env
, value
& 0x3FFFFF80);
847 uint32_t cpu_ppc601_load_rtcl (CPUPPCState
*env
)
849 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
852 /*****************************************************************************/
853 /* PowerPC 40x timers */
856 typedef struct ppc40x_timer_t ppc40x_timer_t
;
857 struct ppc40x_timer_t
{
858 uint64_t pit_reload
; /* PIT auto-reload value */
859 uint64_t fit_next
; /* Tick for next FIT interrupt */
860 QEMUTimer
*fit_timer
;
861 uint64_t wdt_next
; /* Tick for next WDT interrupt */
862 QEMUTimer
*wdt_timer
;
864 /* 405 have the PIT, 440 have a DECR. */
865 unsigned int decr_excp
;
868 /* Fixed interval timer */
869 static void cpu_4xx_fit_cb (void *opaque
)
874 ppc40x_timer_t
*ppc40x_timer
;
878 cpu
= ppc_env_get_cpu(env
);
879 tb_env
= env
->tb_env
;
880 ppc40x_timer
= tb_env
->opaque
;
881 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
882 switch ((env
->spr
[SPR_40x_TCR
] >> 24) & 0x3) {
896 /* Cannot occur, but makes gcc happy */
899 next
= now
+ muldiv64(next
, get_ticks_per_sec(), tb_env
->tb_freq
);
902 timer_mod(ppc40x_timer
->fit_timer
, next
);
903 env
->spr
[SPR_40x_TSR
] |= 1 << 26;
904 if ((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1) {
905 ppc_set_irq(cpu
, PPC_INTERRUPT_FIT
, 1);
907 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
908 (int)((env
->spr
[SPR_40x_TCR
] >> 23) & 0x1),
909 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
912 /* Programmable interval timer */
913 static void start_stop_pit (CPUPPCState
*env
, ppc_tb_t
*tb_env
, int is_excp
)
915 ppc40x_timer_t
*ppc40x_timer
;
918 ppc40x_timer
= tb_env
->opaque
;
919 if (ppc40x_timer
->pit_reload
<= 1 ||
920 !((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) ||
921 (is_excp
&& !((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1))) {
923 LOG_TB("%s: stop PIT\n", __func__
);
924 timer_del(tb_env
->decr_timer
);
926 LOG_TB("%s: start PIT %016" PRIx64
"\n",
927 __func__
, ppc40x_timer
->pit_reload
);
928 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
929 next
= now
+ muldiv64(ppc40x_timer
->pit_reload
,
930 get_ticks_per_sec(), tb_env
->decr_freq
);
932 next
+= tb_env
->decr_next
- now
;
935 timer_mod(tb_env
->decr_timer
, next
);
936 tb_env
->decr_next
= next
;
940 static void cpu_4xx_pit_cb (void *opaque
)
945 ppc40x_timer_t
*ppc40x_timer
;
948 cpu
= ppc_env_get_cpu(env
);
949 tb_env
= env
->tb_env
;
950 ppc40x_timer
= tb_env
->opaque
;
951 env
->spr
[SPR_40x_TSR
] |= 1 << 27;
952 if ((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1) {
953 ppc_set_irq(cpu
, ppc40x_timer
->decr_excp
, 1);
955 start_stop_pit(env
, tb_env
, 1);
956 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
" "
957 "%016" PRIx64
"\n", __func__
,
958 (int)((env
->spr
[SPR_40x_TCR
] >> 22) & 0x1),
959 (int)((env
->spr
[SPR_40x_TCR
] >> 26) & 0x1),
960 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
],
961 ppc40x_timer
->pit_reload
);
965 static void cpu_4xx_wdt_cb (void *opaque
)
970 ppc40x_timer_t
*ppc40x_timer
;
974 cpu
= ppc_env_get_cpu(env
);
975 tb_env
= env
->tb_env
;
976 ppc40x_timer
= tb_env
->opaque
;
977 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
978 switch ((env
->spr
[SPR_40x_TCR
] >> 30) & 0x3) {
992 /* Cannot occur, but makes gcc happy */
995 next
= now
+ muldiv64(next
, get_ticks_per_sec(), tb_env
->decr_freq
);
998 LOG_TB("%s: TCR " TARGET_FMT_lx
" TSR " TARGET_FMT_lx
"\n", __func__
,
999 env
->spr
[SPR_40x_TCR
], env
->spr
[SPR_40x_TSR
]);
1000 switch ((env
->spr
[SPR_40x_TSR
] >> 30) & 0x3) {
1003 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1004 ppc40x_timer
->wdt_next
= next
;
1005 env
->spr
[SPR_40x_TSR
] |= 1 << 31;
1008 timer_mod(ppc40x_timer
->wdt_timer
, next
);
1009 ppc40x_timer
->wdt_next
= next
;
1010 env
->spr
[SPR_40x_TSR
] |= 1 << 30;
1011 if ((env
->spr
[SPR_40x_TCR
] >> 27) & 0x1) {
1012 ppc_set_irq(cpu
, PPC_INTERRUPT_WDT
, 1);
1016 env
->spr
[SPR_40x_TSR
] &= ~0x30000000;
1017 env
->spr
[SPR_40x_TSR
] |= env
->spr
[SPR_40x_TCR
] & 0x30000000;
1018 switch ((env
->spr
[SPR_40x_TCR
] >> 28) & 0x3) {
1022 case 0x1: /* Core reset */
1023 ppc40x_core_reset(cpu
);
1025 case 0x2: /* Chip reset */
1026 ppc40x_chip_reset(cpu
);
1028 case 0x3: /* System reset */
1029 ppc40x_system_reset(cpu
);
1035 void store_40x_pit (CPUPPCState
*env
, target_ulong val
)
1038 ppc40x_timer_t
*ppc40x_timer
;
1040 tb_env
= env
->tb_env
;
1041 ppc40x_timer
= tb_env
->opaque
;
1042 LOG_TB("%s val" TARGET_FMT_lx
"\n", __func__
, val
);
1043 ppc40x_timer
->pit_reload
= val
;
1044 start_stop_pit(env
, tb_env
, 0);
1047 target_ulong
load_40x_pit (CPUPPCState
*env
)
1049 return cpu_ppc_load_decr(env
);
1052 static void ppc_40x_set_tb_clk (void *opaque
, uint32_t freq
)
1054 CPUPPCState
*env
= opaque
;
1055 ppc_tb_t
*tb_env
= env
->tb_env
;
1057 LOG_TB("%s set new frequency to %" PRIu32
"\n", __func__
,
1059 tb_env
->tb_freq
= freq
;
1060 tb_env
->decr_freq
= freq
;
1061 /* XXX: we should also update all timers */
1064 clk_setup_cb
ppc_40x_timers_init (CPUPPCState
*env
, uint32_t freq
,
1065 unsigned int decr_excp
)
1068 ppc40x_timer_t
*ppc40x_timer
;
1070 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
1071 env
->tb_env
= tb_env
;
1072 tb_env
->flags
= PPC_DECR_UNDERFLOW_TRIGGERED
;
1073 ppc40x_timer
= g_malloc0(sizeof(ppc40x_timer_t
));
1074 tb_env
->tb_freq
= freq
;
1075 tb_env
->decr_freq
= freq
;
1076 tb_env
->opaque
= ppc40x_timer
;
1077 LOG_TB("%s freq %" PRIu32
"\n", __func__
, freq
);
1078 if (ppc40x_timer
!= NULL
) {
1079 /* We use decr timer for PIT */
1080 tb_env
->decr_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_pit_cb
, env
);
1081 ppc40x_timer
->fit_timer
=
1082 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_fit_cb
, env
);
1083 ppc40x_timer
->wdt_timer
=
1084 timer_new_ns(QEMU_CLOCK_VIRTUAL
, &cpu_4xx_wdt_cb
, env
);
1085 ppc40x_timer
->decr_excp
= decr_excp
;
1088 return &ppc_40x_set_tb_clk
;
1091 /*****************************************************************************/
1092 /* Embedded PowerPC Device Control Registers */
1093 typedef struct ppc_dcrn_t ppc_dcrn_t
;
1095 dcr_read_cb dcr_read
;
1096 dcr_write_cb dcr_write
;
1100 /* XXX: on 460, DCR addresses are 32 bits wide,
1101 * using DCRIPR to get the 22 upper bits of the DCR address
1103 #define DCRN_NB 1024
1105 ppc_dcrn_t dcrn
[DCRN_NB
];
1106 int (*read_error
)(int dcrn
);
1107 int (*write_error
)(int dcrn
);
1110 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1114 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1116 dcr
= &dcr_env
->dcrn
[dcrn
];
1117 if (dcr
->dcr_read
== NULL
)
1119 *valp
= (*dcr
->dcr_read
)(dcr
->opaque
, dcrn
);
1124 if (dcr_env
->read_error
!= NULL
)
1125 return (*dcr_env
->read_error
)(dcrn
);
1130 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1134 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1136 dcr
= &dcr_env
->dcrn
[dcrn
];
1137 if (dcr
->dcr_write
== NULL
)
1139 (*dcr
->dcr_write
)(dcr
->opaque
, dcrn
, val
);
1144 if (dcr_env
->write_error
!= NULL
)
1145 return (*dcr_env
->write_error
)(dcrn
);
1150 int ppc_dcr_register (CPUPPCState
*env
, int dcrn
, void *opaque
,
1151 dcr_read_cb dcr_read
, dcr_write_cb dcr_write
)
1156 dcr_env
= env
->dcr_env
;
1157 if (dcr_env
== NULL
)
1159 if (dcrn
< 0 || dcrn
>= DCRN_NB
)
1161 dcr
= &dcr_env
->dcrn
[dcrn
];
1162 if (dcr
->opaque
!= NULL
||
1163 dcr
->dcr_read
!= NULL
||
1164 dcr
->dcr_write
!= NULL
)
1166 dcr
->opaque
= opaque
;
1167 dcr
->dcr_read
= dcr_read
;
1168 dcr
->dcr_write
= dcr_write
;
1173 int ppc_dcr_init (CPUPPCState
*env
, int (*read_error
)(int dcrn
),
1174 int (*write_error
)(int dcrn
))
1178 dcr_env
= g_malloc0(sizeof(ppc_dcr_t
));
1179 dcr_env
->read_error
= read_error
;
1180 dcr_env
->write_error
= write_error
;
1181 env
->dcr_env
= dcr_env
;
1186 /*****************************************************************************/
1188 void PPC_debug_write (void *opaque
, uint32_t addr
, uint32_t val
)
1200 printf("Set loglevel to %04" PRIx32
"\n", val
);
1201 qemu_set_log(val
| 0x100);
1206 /*****************************************************************************/
1208 static inline uint32_t nvram_read (nvram_t
*nvram
, uint32_t addr
)
1210 return (*nvram
->read_fn
)(nvram
->opaque
, addr
);
1213 static inline void nvram_write (nvram_t
*nvram
, uint32_t addr
, uint32_t val
)
1215 (*nvram
->write_fn
)(nvram
->opaque
, addr
, val
);
1218 static void NVRAM_set_byte(nvram_t
*nvram
, uint32_t addr
, uint8_t value
)
1220 nvram_write(nvram
, addr
, value
);
1223 static uint8_t NVRAM_get_byte(nvram_t
*nvram
, uint32_t addr
)
1225 return nvram_read(nvram
, addr
);
1228 static void NVRAM_set_word(nvram_t
*nvram
, uint32_t addr
, uint16_t value
)
1230 nvram_write(nvram
, addr
, value
>> 8);
1231 nvram_write(nvram
, addr
+ 1, value
& 0xFF);
1234 static uint16_t NVRAM_get_word(nvram_t
*nvram
, uint32_t addr
)
1238 tmp
= nvram_read(nvram
, addr
) << 8;
1239 tmp
|= nvram_read(nvram
, addr
+ 1);
1244 static void NVRAM_set_lword(nvram_t
*nvram
, uint32_t addr
, uint32_t value
)
1246 nvram_write(nvram
, addr
, value
>> 24);
1247 nvram_write(nvram
, addr
+ 1, (value
>> 16) & 0xFF);
1248 nvram_write(nvram
, addr
+ 2, (value
>> 8) & 0xFF);
1249 nvram_write(nvram
, addr
+ 3, value
& 0xFF);
1252 uint32_t NVRAM_get_lword (nvram_t
*nvram
, uint32_t addr
)
1256 tmp
= nvram_read(nvram
, addr
) << 24;
1257 tmp
|= nvram_read(nvram
, addr
+ 1) << 16;
1258 tmp
|= nvram_read(nvram
, addr
+ 2) << 8;
1259 tmp
|= nvram_read(nvram
, addr
+ 3);
1264 static void NVRAM_set_string(nvram_t
*nvram
, uint32_t addr
, const char *str
,
1269 for (i
= 0; i
< max
&& str
[i
] != '\0'; i
++) {
1270 nvram_write(nvram
, addr
+ i
, str
[i
]);
1272 nvram_write(nvram
, addr
+ i
, str
[i
]);
1273 nvram_write(nvram
, addr
+ max
- 1, '\0');
1276 int NVRAM_get_string (nvram_t
*nvram
, uint8_t *dst
, uint16_t addr
, int max
)
1280 memset(dst
, 0, max
);
1281 for (i
= 0; i
< max
; i
++) {
1282 dst
[i
] = NVRAM_get_byte(nvram
, addr
+ i
);
1290 static uint16_t NVRAM_crc_update (uint16_t prev
, uint16_t value
)
1293 uint16_t pd
, pd1
, pd2
;
1298 pd2
= ((pd
>> 4) & 0x000F) ^ pd1
;
1299 tmp
^= (pd1
<< 3) | (pd1
<< 8);
1300 tmp
^= pd2
| (pd2
<< 7) | (pd2
<< 12);
1305 static uint16_t NVRAM_compute_crc (nvram_t
*nvram
, uint32_t start
, uint32_t count
)
1308 uint16_t crc
= 0xFFFF;
1313 for (i
= 0; i
!= count
; i
++) {
1314 crc
= NVRAM_crc_update(crc
, NVRAM_get_word(nvram
, start
+ i
));
1317 crc
= NVRAM_crc_update(crc
, NVRAM_get_byte(nvram
, start
+ i
) << 8);
1323 #define CMDLINE_ADDR 0x017ff000
1325 int PPC_NVRAM_set_params (nvram_t
*nvram
, uint16_t NVRAM_size
,
1327 uint32_t RAM_size
, int boot_device
,
1328 uint32_t kernel_image
, uint32_t kernel_size
,
1329 const char *cmdline
,
1330 uint32_t initrd_image
, uint32_t initrd_size
,
1331 uint32_t NVRAM_image
,
1332 int width
, int height
, int depth
)
1336 /* Set parameters for Open Hack'Ware BIOS */
1337 NVRAM_set_string(nvram
, 0x00, "QEMU_BIOS", 16);
1338 NVRAM_set_lword(nvram
, 0x10, 0x00000002); /* structure v2 */
1339 NVRAM_set_word(nvram
, 0x14, NVRAM_size
);
1340 NVRAM_set_string(nvram
, 0x20, arch
, 16);
1341 NVRAM_set_lword(nvram
, 0x30, RAM_size
);
1342 NVRAM_set_byte(nvram
, 0x34, boot_device
);
1343 NVRAM_set_lword(nvram
, 0x38, kernel_image
);
1344 NVRAM_set_lword(nvram
, 0x3C, kernel_size
);
1346 /* XXX: put the cmdline in NVRAM too ? */
1347 pstrcpy_targphys("cmdline", CMDLINE_ADDR
, RAM_size
- CMDLINE_ADDR
, cmdline
);
1348 NVRAM_set_lword(nvram
, 0x40, CMDLINE_ADDR
);
1349 NVRAM_set_lword(nvram
, 0x44, strlen(cmdline
));
1351 NVRAM_set_lword(nvram
, 0x40, 0);
1352 NVRAM_set_lword(nvram
, 0x44, 0);
1354 NVRAM_set_lword(nvram
, 0x48, initrd_image
);
1355 NVRAM_set_lword(nvram
, 0x4C, initrd_size
);
1356 NVRAM_set_lword(nvram
, 0x50, NVRAM_image
);
1358 NVRAM_set_word(nvram
, 0x54, width
);
1359 NVRAM_set_word(nvram
, 0x56, height
);
1360 NVRAM_set_word(nvram
, 0x58, depth
);
1361 crc
= NVRAM_compute_crc(nvram
, 0x00, 0xF8);
1362 NVRAM_set_word(nvram
, 0xFC, crc
);
1367 /* CPU device-tree ID helpers */
1368 int ppc_get_vcpu_dt_id(PowerPCCPU
*cpu
)
1370 return cpu
->cpu_dt_id
;
1373 PowerPCCPU
*ppc_get_vcpu_by_dt_id(int cpu_dt_id
)
1378 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1380 if (cpu
->cpu_dt_id
== cpu_dt_id
) {