2 * QEMU PowerPC Booke hardware System Emulator
4 * Copyright (c) 2011 AdaCore
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 "qemu/timer.h"
27 #include "sysemu/sysemu.h"
28 #include "hw/timer/m48t59.h"
30 #include "hw/loader.h"
34 /* Timer Control Register */
36 #define TCR_WP_SHIFT 30 /* Watchdog Timer Period */
37 #define TCR_WP_MASK (0x3 << TCR_WP_SHIFT)
38 #define TCR_WRC_SHIFT 28 /* Watchdog Timer Reset Control */
39 #define TCR_WRC_MASK (0x3 << TCR_WRC_SHIFT)
40 #define TCR_WIE (1 << 27) /* Watchdog Timer Interrupt Enable */
41 #define TCR_DIE (1 << 26) /* Decrementer Interrupt Enable */
42 #define TCR_FP_SHIFT 24 /* Fixed-Interval Timer Period */
43 #define TCR_FP_MASK (0x3 << TCR_FP_SHIFT)
44 #define TCR_FIE (1 << 23) /* Fixed-Interval Timer Interrupt Enable */
45 #define TCR_ARE (1 << 22) /* Auto-Reload Enable */
47 /* Timer Control Register (e500 specific fields) */
49 #define TCR_E500_FPEXT_SHIFT 13 /* Fixed-Interval Timer Period Extension */
50 #define TCR_E500_FPEXT_MASK (0xf << TCR_E500_FPEXT_SHIFT)
51 #define TCR_E500_WPEXT_SHIFT 17 /* Watchdog Timer Period Extension */
52 #define TCR_E500_WPEXT_MASK (0xf << TCR_E500_WPEXT_SHIFT)
54 /* Timer Status Register */
56 #define TSR_FIS (1 << 26) /* Fixed-Interval Timer Interrupt Status */
57 #define TSR_DIS (1 << 27) /* Decrementer Interrupt Status */
58 #define TSR_WRS_SHIFT 28 /* Watchdog Timer Reset Status */
59 #define TSR_WRS_MASK (0x3 << TSR_WRS_SHIFT)
60 #define TSR_WIS (1 << 30) /* Watchdog Timer Interrupt Status */
61 #define TSR_ENW (1 << 31) /* Enable Next Watchdog Timer */
63 typedef struct booke_timer_t booke_timer_t
;
64 struct booke_timer_t
{
67 struct QEMUTimer
*fit_timer
;
70 struct QEMUTimer
*wdt_timer
;
75 static void booke_update_irq(PowerPCCPU
*cpu
)
77 CPUPPCState
*env
= &cpu
->env
;
79 ppc_set_irq(cpu
, PPC_INTERRUPT_DECR
,
80 (env
->spr
[SPR_BOOKE_TSR
] & TSR_DIS
81 && env
->spr
[SPR_BOOKE_TCR
] & TCR_DIE
));
83 ppc_set_irq(cpu
, PPC_INTERRUPT_WDT
,
84 (env
->spr
[SPR_BOOKE_TSR
] & TSR_WIS
85 && env
->spr
[SPR_BOOKE_TCR
] & TCR_WIE
));
87 ppc_set_irq(cpu
, PPC_INTERRUPT_FIT
,
88 (env
->spr
[SPR_BOOKE_TSR
] & TSR_FIS
89 && env
->spr
[SPR_BOOKE_TCR
] & TCR_FIE
));
92 /* Return the location of the bit of time base at which the FIT will raise an
94 static uint8_t booke_get_fit_target(CPUPPCState
*env
, ppc_tb_t
*tb_env
)
96 uint8_t fp
= (env
->spr
[SPR_BOOKE_TCR
] & TCR_FP_MASK
) >> TCR_FP_SHIFT
;
98 if (tb_env
->flags
& PPC_TIMER_E500
) {
99 /* e500 Fixed-interval timer period extension */
100 uint32_t fpext
= (env
->spr
[SPR_BOOKE_TCR
] & TCR_E500_FPEXT_MASK
)
101 >> TCR_E500_FPEXT_SHIFT
;
102 fp
= 63 - (fp
| fpext
<< 2);
104 fp
= env
->fit_period
[fp
];
110 /* Return the location of the bit of time base at which the WDT will raise an
112 static uint8_t booke_get_wdt_target(CPUPPCState
*env
, ppc_tb_t
*tb_env
)
114 uint8_t wp
= (env
->spr
[SPR_BOOKE_TCR
] & TCR_WP_MASK
) >> TCR_WP_SHIFT
;
116 if (tb_env
->flags
& PPC_TIMER_E500
) {
117 /* e500 Watchdog timer period extension */
118 uint32_t wpext
= (env
->spr
[SPR_BOOKE_TCR
] & TCR_E500_WPEXT_MASK
)
119 >> TCR_E500_WPEXT_SHIFT
;
120 wp
= 63 - (wp
| wpext
<< 2);
122 wp
= env
->wdt_period
[wp
];
128 static void booke_update_fixed_timer(CPUPPCState
*env
,
131 struct QEMUTimer
*timer
)
133 ppc_tb_t
*tb_env
= env
->tb_env
;
136 uint64_t period
= 1 << (target_bit
+ 1);
139 now
= qemu_get_clock_ns(vm_clock
);
140 tb
= cpu_ppc_get_tb(tb_env
, now
, tb_env
->tb_offset
);
142 lapse
= period
- ((tb
- (1 << target_bit
)) & (period
- 1));
144 *next
= now
+ muldiv64(lapse
, get_ticks_per_sec(), tb_env
->tb_freq
);
146 /* XXX: If expire time is now. We can't run the callback because we don't
147 * have access to it. So we just set the timer one nanosecond later.
154 qemu_mod_timer(timer
, *next
);
157 static void booke_decr_cb(void *opaque
)
159 PowerPCCPU
*cpu
= opaque
;
160 CPUPPCState
*env
= &cpu
->env
;
162 env
->spr
[SPR_BOOKE_TSR
] |= TSR_DIS
;
163 booke_update_irq(cpu
);
165 if (env
->spr
[SPR_BOOKE_TCR
] & TCR_ARE
) {
167 cpu_ppc_store_decr(env
, env
->spr
[SPR_BOOKE_DECAR
]);
171 static void booke_fit_cb(void *opaque
)
173 PowerPCCPU
*cpu
= opaque
;
174 CPUPPCState
*env
= &cpu
->env
;
176 booke_timer_t
*booke_timer
;
178 tb_env
= env
->tb_env
;
179 booke_timer
= tb_env
->opaque
;
180 env
->spr
[SPR_BOOKE_TSR
] |= TSR_FIS
;
182 booke_update_irq(cpu
);
184 booke_update_fixed_timer(env
,
185 booke_get_fit_target(env
, tb_env
),
186 &booke_timer
->fit_next
,
187 booke_timer
->fit_timer
);
190 static void booke_wdt_cb(void *opaque
)
192 PowerPCCPU
*cpu
= opaque
;
193 CPUPPCState
*env
= &cpu
->env
;
195 booke_timer_t
*booke_timer
;
197 tb_env
= env
->tb_env
;
198 booke_timer
= tb_env
->opaque
;
200 /* TODO: There's lots of complicated stuff to do here */
202 booke_update_irq(cpu
);
204 booke_update_fixed_timer(env
,
205 booke_get_wdt_target(env
, tb_env
),
206 &booke_timer
->wdt_next
,
207 booke_timer
->wdt_timer
);
210 void store_booke_tsr(CPUPPCState
*env
, target_ulong val
)
212 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
214 env
->spr
[SPR_BOOKE_TSR
] &= ~val
;
215 kvmppc_clear_tsr_bits(cpu
, val
);
216 booke_update_irq(cpu
);
219 void store_booke_tcr(CPUPPCState
*env
, target_ulong val
)
221 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
222 ppc_tb_t
*tb_env
= env
->tb_env
;
223 booke_timer_t
*booke_timer
= tb_env
->opaque
;
225 tb_env
= env
->tb_env
;
226 env
->spr
[SPR_BOOKE_TCR
] = val
;
229 booke_update_irq(cpu
);
231 booke_update_fixed_timer(env
,
232 booke_get_fit_target(env
, tb_env
),
233 &booke_timer
->fit_next
,
234 booke_timer
->fit_timer
);
236 booke_update_fixed_timer(env
,
237 booke_get_wdt_target(env
, tb_env
),
238 &booke_timer
->wdt_next
,
239 booke_timer
->wdt_timer
);
242 static void ppc_booke_timer_reset_handle(void *opaque
)
244 PowerPCCPU
*cpu
= opaque
;
245 CPUPPCState
*env
= &cpu
->env
;
247 store_booke_tcr(env
, 0);
248 store_booke_tsr(env
, -1);
252 * This function will be called whenever the CPU state changes.
253 * CPU states are defined "typedef enum RunState".
254 * Regarding timer, When CPU state changes to running after debug halt
255 * or similar cases which takes time then in between final watchdog
256 * expiry happenes. This will cause exit to QEMU and configured watchdog
257 * action will be taken. To avoid this we always clear the watchdog state when
258 * state changes to running.
260 static void cpu_state_change_handler(void *opaque
, int running
, RunState state
)
262 PowerPCCPU
*cpu
= opaque
;
263 CPUPPCState
*env
= &cpu
->env
;
270 * Clear watchdog interrupt condition by clearing TSR.
272 store_booke_tsr(env
, TSR_ENW
| TSR_WIS
| TSR_WRS_MASK
);
275 void ppc_booke_timers_init(PowerPCCPU
*cpu
, uint32_t freq
, uint32_t flags
)
278 booke_timer_t
*booke_timer
;
281 tb_env
= g_malloc0(sizeof(ppc_tb_t
));
282 booke_timer
= g_malloc0(sizeof(booke_timer_t
));
284 cpu
->env
.tb_env
= tb_env
;
285 tb_env
->flags
= flags
| PPC_TIMER_BOOKE
| PPC_DECR_ZERO_TRIGGERED
;
287 tb_env
->tb_freq
= freq
;
288 tb_env
->decr_freq
= freq
;
289 tb_env
->opaque
= booke_timer
;
290 tb_env
->decr_timer
= qemu_new_timer_ns(vm_clock
, &booke_decr_cb
, cpu
);
292 booke_timer
->fit_timer
=
293 qemu_new_timer_ns(vm_clock
, &booke_fit_cb
, cpu
);
294 booke_timer
->wdt_timer
=
295 qemu_new_timer_ns(vm_clock
, &booke_wdt_cb
, cpu
);
297 ret
= kvmppc_booke_watchdog_enable(cpu
);
300 /* TODO: Start the QEMU emulated watchdog if not running on KVM.
301 * Also start the QEMU emulated watchdog if KVM does not support
302 * emulated watchdog or somehow it is not enabled (supported but
303 * not enabled is though some bug and requires debugging :)).
307 qemu_add_vm_change_state_handler(cpu_state_change_handler
, cpu
);
309 qemu_register_reset(ppc_booke_timer_reset_handle
, cpu
);