xref: /openbmc/qemu/hw/ppc/ppc_booke.c (revision 6b829602e2f10f301ff8508f3a6850a0e913142c)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * QEMU PowerPC Booke hardware System Emulator
353018216SPaolo Bonzini  *
453018216SPaolo Bonzini  * Copyright (c) 2011 AdaCore
553018216SPaolo Bonzini  *
653018216SPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
753018216SPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
853018216SPaolo Bonzini  * in the Software without restriction, including without limitation the rights
953018216SPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1053018216SPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
1153018216SPaolo Bonzini  * furnished to do so, subject to the following conditions:
1253018216SPaolo Bonzini  *
1353018216SPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
1453018216SPaolo Bonzini  * all copies or substantial portions of the Software.
1553018216SPaolo Bonzini  *
1653018216SPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1753018216SPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1853018216SPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1953018216SPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2053018216SPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2153018216SPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2253018216SPaolo Bonzini  * THE SOFTWARE.
2353018216SPaolo Bonzini  */
2471e8a915SMarkus Armbruster 
250d75590dSPeter Maydell #include "qemu/osdep.h"
264771d756SPaolo Bonzini #include "cpu.h"
270d09e41aSPaolo Bonzini #include "hw/ppc/ppc.h"
2853018216SPaolo Bonzini #include "qemu/timer.h"
2971e8a915SMarkus Armbruster #include "sysemu/reset.h"
3054d31236SMarkus Armbruster #include "sysemu/runstate.h"
3153018216SPaolo Bonzini #include "hw/loader.h"
3231f2cb8fSBharat Bhushan #include "kvm_ppc.h"
3353018216SPaolo Bonzini 
booke_set_tlb(ppcemb_tlb_t * tlb,target_ulong va,hwaddr pa,target_ulong size)34*afff8800SBALATON Zoltan void booke_set_tlb(ppcemb_tlb_t *tlb, target_ulong va, hwaddr pa,
35*afff8800SBALATON Zoltan                    target_ulong size)
36*afff8800SBALATON Zoltan {
37*afff8800SBALATON Zoltan     tlb->attr = 0;
38*afff8800SBALATON Zoltan     tlb->prot = PAGE_RWX << 4 | PAGE_VALID;
39*afff8800SBALATON Zoltan     tlb->size = size;
40*afff8800SBALATON Zoltan     tlb->EPN = va & TARGET_PAGE_MASK;
41*afff8800SBALATON Zoltan     tlb->RPN = pa & TARGET_PAGE_MASK;
42*afff8800SBALATON Zoltan     tlb->PID = 0;
43*afff8800SBALATON Zoltan }
4453018216SPaolo Bonzini 
4553018216SPaolo Bonzini /* Timer Control Register */
4653018216SPaolo Bonzini 
4753018216SPaolo Bonzini #define TCR_WP_SHIFT  30        /* Watchdog Timer Period */
48a1f7f97bSPeter Maydell #define TCR_WP_MASK   (0x3U << TCR_WP_SHIFT)
4953018216SPaolo Bonzini #define TCR_WRC_SHIFT 28        /* Watchdog Timer Reset Control */
50a1f7f97bSPeter Maydell #define TCR_WRC_MASK  (0x3U << TCR_WRC_SHIFT)
51a1f7f97bSPeter Maydell #define TCR_WIE       (1U << 27) /* Watchdog Timer Interrupt Enable */
52a1f7f97bSPeter Maydell #define TCR_DIE       (1U << 26) /* Decrementer Interrupt Enable */
5353018216SPaolo Bonzini #define TCR_FP_SHIFT  24        /* Fixed-Interval Timer Period */
54a1f7f97bSPeter Maydell #define TCR_FP_MASK   (0x3U << TCR_FP_SHIFT)
55a1f7f97bSPeter Maydell #define TCR_FIE       (1U << 23) /* Fixed-Interval Timer Interrupt Enable */
56a1f7f97bSPeter Maydell #define TCR_ARE       (1U << 22) /* Auto-Reload Enable */
5753018216SPaolo Bonzini 
5853018216SPaolo Bonzini /* Timer Control Register (e500 specific fields) */
5953018216SPaolo Bonzini 
6053018216SPaolo Bonzini #define TCR_E500_FPEXT_SHIFT 13 /* Fixed-Interval Timer Period Extension */
6153018216SPaolo Bonzini #define TCR_E500_FPEXT_MASK  (0xf << TCR_E500_FPEXT_SHIFT)
6253018216SPaolo Bonzini #define TCR_E500_WPEXT_SHIFT 17 /* Watchdog Timer Period Extension */
6353018216SPaolo Bonzini #define TCR_E500_WPEXT_MASK  (0xf << TCR_E500_WPEXT_SHIFT)
6453018216SPaolo Bonzini 
6553018216SPaolo Bonzini /* Timer Status Register  */
6653018216SPaolo Bonzini 
67a1f7f97bSPeter Maydell #define TSR_FIS       (1U << 26) /* Fixed-Interval Timer Interrupt Status */
68a1f7f97bSPeter Maydell #define TSR_DIS       (1U << 27) /* Decrementer Interrupt Status */
6953018216SPaolo Bonzini #define TSR_WRS_SHIFT 28        /* Watchdog Timer Reset Status */
70a1f7f97bSPeter Maydell #define TSR_WRS_MASK  (0x3U << TSR_WRS_SHIFT)
71a1f7f97bSPeter Maydell #define TSR_WIS       (1U << 30) /* Watchdog Timer Interrupt Status */
72a1f7f97bSPeter Maydell #define TSR_ENW       (1U << 31) /* Enable Next Watchdog Timer */
7353018216SPaolo Bonzini 
7453018216SPaolo Bonzini typedef struct booke_timer_t booke_timer_t;
7553018216SPaolo Bonzini struct booke_timer_t {
7653018216SPaolo Bonzini 
7753018216SPaolo Bonzini     uint64_t fit_next;
781246b259SStefan Weil     QEMUTimer *fit_timer;
7953018216SPaolo Bonzini 
8053018216SPaolo Bonzini     uint64_t wdt_next;
811246b259SStefan Weil     QEMUTimer *wdt_timer;
8253018216SPaolo Bonzini 
8353018216SPaolo Bonzini     uint32_t flags;
8453018216SPaolo Bonzini };
8553018216SPaolo Bonzini 
booke_update_irq(PowerPCCPU * cpu)8653018216SPaolo Bonzini static void booke_update_irq(PowerPCCPU *cpu)
8753018216SPaolo Bonzini {
8853018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
8953018216SPaolo Bonzini 
9053018216SPaolo Bonzini     ppc_set_irq(cpu, PPC_INTERRUPT_DECR,
9153018216SPaolo Bonzini                 (env->spr[SPR_BOOKE_TSR] & TSR_DIS
9253018216SPaolo Bonzini                  && env->spr[SPR_BOOKE_TCR] & TCR_DIE));
9353018216SPaolo Bonzini 
9453018216SPaolo Bonzini     ppc_set_irq(cpu, PPC_INTERRUPT_WDT,
9553018216SPaolo Bonzini                 (env->spr[SPR_BOOKE_TSR] & TSR_WIS
9653018216SPaolo Bonzini                  && env->spr[SPR_BOOKE_TCR] & TCR_WIE));
9753018216SPaolo Bonzini 
9853018216SPaolo Bonzini     ppc_set_irq(cpu, PPC_INTERRUPT_FIT,
9953018216SPaolo Bonzini                 (env->spr[SPR_BOOKE_TSR] & TSR_FIS
10053018216SPaolo Bonzini                  && env->spr[SPR_BOOKE_TCR] & TCR_FIE));
10153018216SPaolo Bonzini }
10253018216SPaolo Bonzini 
10353018216SPaolo Bonzini /* Return the location of the bit of time base at which the FIT will raise an
10453018216SPaolo Bonzini    interrupt */
booke_get_fit_target(CPUPPCState * env,ppc_tb_t * tb_env)10553018216SPaolo Bonzini static uint8_t booke_get_fit_target(CPUPPCState *env, ppc_tb_t *tb_env)
10653018216SPaolo Bonzini {
10753018216SPaolo Bonzini     uint8_t fp = (env->spr[SPR_BOOKE_TCR] & TCR_FP_MASK) >> TCR_FP_SHIFT;
10853018216SPaolo Bonzini 
10953018216SPaolo Bonzini     if (tb_env->flags & PPC_TIMER_E500) {
11053018216SPaolo Bonzini         /* e500 Fixed-interval timer period extension */
11153018216SPaolo Bonzini         uint32_t fpext = (env->spr[SPR_BOOKE_TCR] & TCR_E500_FPEXT_MASK)
11253018216SPaolo Bonzini             >> TCR_E500_FPEXT_SHIFT;
11353018216SPaolo Bonzini         fp = 63 - (fp | fpext << 2);
11453018216SPaolo Bonzini     } else {
11553018216SPaolo Bonzini         fp = env->fit_period[fp];
11653018216SPaolo Bonzini     }
11753018216SPaolo Bonzini 
11853018216SPaolo Bonzini     return fp;
11953018216SPaolo Bonzini }
12053018216SPaolo Bonzini 
12153018216SPaolo Bonzini /* Return the location of the bit of time base at which the WDT will raise an
12253018216SPaolo Bonzini    interrupt */
booke_get_wdt_target(CPUPPCState * env,ppc_tb_t * tb_env)12353018216SPaolo Bonzini static uint8_t booke_get_wdt_target(CPUPPCState *env, ppc_tb_t *tb_env)
12453018216SPaolo Bonzini {
12553018216SPaolo Bonzini     uint8_t wp = (env->spr[SPR_BOOKE_TCR] & TCR_WP_MASK) >> TCR_WP_SHIFT;
12653018216SPaolo Bonzini 
12753018216SPaolo Bonzini     if (tb_env->flags & PPC_TIMER_E500) {
12853018216SPaolo Bonzini         /* e500 Watchdog timer period extension */
12953018216SPaolo Bonzini         uint32_t wpext = (env->spr[SPR_BOOKE_TCR] & TCR_E500_WPEXT_MASK)
13053018216SPaolo Bonzini             >> TCR_E500_WPEXT_SHIFT;
13153018216SPaolo Bonzini         wp = 63 - (wp | wpext << 2);
13253018216SPaolo Bonzini     } else {
13353018216SPaolo Bonzini         wp = env->wdt_period[wp];
13453018216SPaolo Bonzini     }
13553018216SPaolo Bonzini 
13653018216SPaolo Bonzini     return wp;
13753018216SPaolo Bonzini }
13853018216SPaolo Bonzini 
booke_update_fixed_timer(CPUPPCState * env,uint8_t target_bit,uint64_t * next,QEMUTimer * timer,int tsr_bit)13953018216SPaolo Bonzini static void booke_update_fixed_timer(CPUPPCState         *env,
14053018216SPaolo Bonzini                                      uint8_t           target_bit,
14153018216SPaolo Bonzini                                      uint64_t          *next,
142455df3f3SAlexander Graf                                      QEMUTimer         *timer,
143455df3f3SAlexander Graf                                      int               tsr_bit)
14453018216SPaolo Bonzini {
14553018216SPaolo Bonzini     ppc_tb_t *tb_env = env->tb_env;
146ab8131afSBharat Bhushan     uint64_t delta_tick, ticks = 0;
14753018216SPaolo Bonzini     uint64_t tb;
148ab8131afSBharat Bhushan     uint64_t period;
14953018216SPaolo Bonzini     uint64_t now;
15053018216SPaolo Bonzini 
151455df3f3SAlexander Graf     if (!(env->spr[SPR_BOOKE_TSR] & tsr_bit)) {
152455df3f3SAlexander Graf         /*
153455df3f3SAlexander Graf          * Don't arm the timer again when the guest has the current
154455df3f3SAlexander Graf          * interrupt still pending. Wait for it to ack it.
155455df3f3SAlexander Graf          */
156455df3f3SAlexander Graf         return;
157455df3f3SAlexander Graf     }
158455df3f3SAlexander Graf 
159bc72ad67SAlex Bligh     now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
16053018216SPaolo Bonzini     tb  = cpu_ppc_get_tb(tb_env, now, tb_env->tb_offset);
161ab8131afSBharat Bhushan     period = 1ULL << target_bit;
162ab8131afSBharat Bhushan     delta_tick = period - (tb & (period - 1));
16353018216SPaolo Bonzini 
164ab8131afSBharat Bhushan     /* the timer triggers only when the selected bit toggles from 0 to 1 */
165ab8131afSBharat Bhushan     if (tb & period) {
166ab8131afSBharat Bhushan         ticks = period;
167ab8131afSBharat Bhushan     }
16853018216SPaolo Bonzini 
169ab8131afSBharat Bhushan     if (ticks + delta_tick < ticks) {
170ab8131afSBharat Bhushan         /* Overflow, so assume the biggest number we can express. */
171ab8131afSBharat Bhushan         ticks = UINT64_MAX;
172ab8131afSBharat Bhushan     } else {
173ab8131afSBharat Bhushan         ticks += delta_tick;
174ab8131afSBharat Bhushan     }
175ab8131afSBharat Bhushan 
17673bcb24dSRutuja Shah     *next = now + muldiv64(ticks, NANOSECONDS_PER_SECOND, tb_env->tb_freq);
177ab8131afSBharat Bhushan     if ((*next < now) || (*next > INT64_MAX)) {
178ab8131afSBharat Bhushan         /* Overflow, so assume the biggest number the qemu timer supports. */
179ab8131afSBharat Bhushan         *next = INT64_MAX;
180ab8131afSBharat Bhushan     }
18153018216SPaolo Bonzini 
18253018216SPaolo Bonzini     /* XXX: If expire time is now. We can't run the callback because we don't
18353018216SPaolo Bonzini      * have access to it. So we just set the timer one nanosecond later.
18453018216SPaolo Bonzini      */
18553018216SPaolo Bonzini 
18653018216SPaolo Bonzini     if (*next == now) {
18753018216SPaolo Bonzini         (*next)++;
18884dc96e1SAlexander Graf     } else {
18984dc96e1SAlexander Graf         /*
19084dc96e1SAlexander Graf          * There's no point to fake any granularity that's more fine grained
19184dc96e1SAlexander Graf          * than milliseconds. Anything beyond that just overloads the system.
19284dc96e1SAlexander Graf          */
19384dc96e1SAlexander Graf         *next = MAX(*next, now + SCALE_MS);
19453018216SPaolo Bonzini     }
19553018216SPaolo Bonzini 
196455df3f3SAlexander Graf     /* Fire the next timer */
197bc72ad67SAlex Bligh     timer_mod(timer, *next);
19853018216SPaolo Bonzini }
19953018216SPaolo Bonzini 
booke_decr_cb(void * opaque)20053018216SPaolo Bonzini static void booke_decr_cb(void *opaque)
20153018216SPaolo Bonzini {
20253018216SPaolo Bonzini     PowerPCCPU *cpu = opaque;
20353018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
20453018216SPaolo Bonzini 
20553018216SPaolo Bonzini     env->spr[SPR_BOOKE_TSR] |= TSR_DIS;
20653018216SPaolo Bonzini     booke_update_irq(cpu);
20753018216SPaolo Bonzini 
20853018216SPaolo Bonzini     if (env->spr[SPR_BOOKE_TCR] & TCR_ARE) {
2090dfe952dSRoman Kapl         /* Do not reload 0, it is already there. It would just trigger
2100dfe952dSRoman Kapl          * the timer again and lead to infinite loop */
2110dfe952dSRoman Kapl         if (env->spr[SPR_BOOKE_DECAR] != 0) {
21253018216SPaolo Bonzini             /* Auto Reload */
21353018216SPaolo Bonzini             cpu_ppc_store_decr(env, env->spr[SPR_BOOKE_DECAR]);
21453018216SPaolo Bonzini         }
21553018216SPaolo Bonzini     }
2160dfe952dSRoman Kapl }
21753018216SPaolo Bonzini 
booke_fit_cb(void * opaque)21853018216SPaolo Bonzini static void booke_fit_cb(void *opaque)
21953018216SPaolo Bonzini {
22053018216SPaolo Bonzini     PowerPCCPU *cpu = opaque;
22153018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
22253018216SPaolo Bonzini     ppc_tb_t *tb_env;
22353018216SPaolo Bonzini     booke_timer_t *booke_timer;
22453018216SPaolo Bonzini 
22553018216SPaolo Bonzini     tb_env = env->tb_env;
22653018216SPaolo Bonzini     booke_timer = tb_env->opaque;
22753018216SPaolo Bonzini     env->spr[SPR_BOOKE_TSR] |= TSR_FIS;
22853018216SPaolo Bonzini 
22953018216SPaolo Bonzini     booke_update_irq(cpu);
23053018216SPaolo Bonzini 
23153018216SPaolo Bonzini     booke_update_fixed_timer(env,
23253018216SPaolo Bonzini                              booke_get_fit_target(env, tb_env),
23353018216SPaolo Bonzini                              &booke_timer->fit_next,
234455df3f3SAlexander Graf                              booke_timer->fit_timer,
235455df3f3SAlexander Graf                              TSR_FIS);
23653018216SPaolo Bonzini }
23753018216SPaolo Bonzini 
booke_wdt_cb(void * opaque)23853018216SPaolo Bonzini static void booke_wdt_cb(void *opaque)
23953018216SPaolo Bonzini {
24053018216SPaolo Bonzini     PowerPCCPU *cpu = opaque;
24153018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
24253018216SPaolo Bonzini     ppc_tb_t *tb_env;
24353018216SPaolo Bonzini     booke_timer_t *booke_timer;
24453018216SPaolo Bonzini 
24553018216SPaolo Bonzini     tb_env = env->tb_env;
24653018216SPaolo Bonzini     booke_timer = tb_env->opaque;
24753018216SPaolo Bonzini 
24853018216SPaolo Bonzini     /* TODO: There's lots of complicated stuff to do here */
24953018216SPaolo Bonzini 
25053018216SPaolo Bonzini     booke_update_irq(cpu);
25153018216SPaolo Bonzini 
25253018216SPaolo Bonzini     booke_update_fixed_timer(env,
25353018216SPaolo Bonzini                              booke_get_wdt_target(env, tb_env),
25453018216SPaolo Bonzini                              &booke_timer->wdt_next,
255455df3f3SAlexander Graf                              booke_timer->wdt_timer,
256455df3f3SAlexander Graf                              TSR_WIS);
25753018216SPaolo Bonzini }
25853018216SPaolo Bonzini 
store_booke_tsr(CPUPPCState * env,target_ulong val)25953018216SPaolo Bonzini void store_booke_tsr(CPUPPCState *env, target_ulong val)
26053018216SPaolo Bonzini {
261db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
262455df3f3SAlexander Graf     ppc_tb_t *tb_env = env->tb_env;
263455df3f3SAlexander Graf     booke_timer_t *booke_timer = tb_env->opaque;
26453018216SPaolo Bonzini 
26553018216SPaolo Bonzini     env->spr[SPR_BOOKE_TSR] &= ~val;
26631f2cb8fSBharat Bhushan     kvmppc_clear_tsr_bits(cpu, val);
267455df3f3SAlexander Graf 
268455df3f3SAlexander Graf     if (val & TSR_FIS) {
269455df3f3SAlexander Graf         booke_update_fixed_timer(env,
270455df3f3SAlexander Graf                                  booke_get_fit_target(env, tb_env),
271455df3f3SAlexander Graf                                  &booke_timer->fit_next,
272455df3f3SAlexander Graf                                  booke_timer->fit_timer,
273455df3f3SAlexander Graf                                  TSR_FIS);
274455df3f3SAlexander Graf     }
275455df3f3SAlexander Graf 
276455df3f3SAlexander Graf     if (val & TSR_WIS) {
277455df3f3SAlexander Graf         booke_update_fixed_timer(env,
278455df3f3SAlexander Graf                                  booke_get_wdt_target(env, tb_env),
279455df3f3SAlexander Graf                                  &booke_timer->wdt_next,
280455df3f3SAlexander Graf                                  booke_timer->wdt_timer,
281455df3f3SAlexander Graf                                  TSR_WIS);
282455df3f3SAlexander Graf     }
283455df3f3SAlexander Graf 
28453018216SPaolo Bonzini     booke_update_irq(cpu);
28553018216SPaolo Bonzini }
28653018216SPaolo Bonzini 
store_booke_tcr(CPUPPCState * env,target_ulong val)28753018216SPaolo Bonzini void store_booke_tcr(CPUPPCState *env, target_ulong val)
28853018216SPaolo Bonzini {
289db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
29053018216SPaolo Bonzini     ppc_tb_t *tb_env = env->tb_env;
29153018216SPaolo Bonzini     booke_timer_t *booke_timer = tb_env->opaque;
29253018216SPaolo Bonzini 
29353018216SPaolo Bonzini     env->spr[SPR_BOOKE_TCR] = val;
29431f2cb8fSBharat Bhushan     kvmppc_set_tcr(cpu);
29553018216SPaolo Bonzini 
29653018216SPaolo Bonzini     booke_update_irq(cpu);
29753018216SPaolo Bonzini 
29853018216SPaolo Bonzini     booke_update_fixed_timer(env,
29953018216SPaolo Bonzini                              booke_get_fit_target(env, tb_env),
30053018216SPaolo Bonzini                              &booke_timer->fit_next,
301455df3f3SAlexander Graf                              booke_timer->fit_timer,
302455df3f3SAlexander Graf                              TSR_FIS);
30353018216SPaolo Bonzini 
30453018216SPaolo Bonzini     booke_update_fixed_timer(env,
30553018216SPaolo Bonzini                              booke_get_wdt_target(env, tb_env),
30653018216SPaolo Bonzini                              &booke_timer->wdt_next,
307455df3f3SAlexander Graf                              booke_timer->wdt_timer,
308455df3f3SAlexander Graf                              TSR_WIS);
30953018216SPaolo Bonzini }
31053018216SPaolo Bonzini 
ppc_booke_timer_reset_handle(void * opaque)31153018216SPaolo Bonzini static void ppc_booke_timer_reset_handle(void *opaque)
31253018216SPaolo Bonzini {
31353018216SPaolo Bonzini     PowerPCCPU *cpu = opaque;
31453018216SPaolo Bonzini     CPUPPCState *env = &cpu->env;
31553018216SPaolo Bonzini 
31631f2cb8fSBharat Bhushan     store_booke_tcr(env, 0);
31731f2cb8fSBharat Bhushan     store_booke_tsr(env, -1);
31831f2cb8fSBharat Bhushan }
31953018216SPaolo Bonzini 
32031f2cb8fSBharat Bhushan /*
32131f2cb8fSBharat Bhushan  * This function will be called whenever the CPU state changes.
32231f2cb8fSBharat Bhushan  * CPU states are defined "typedef enum RunState".
32331f2cb8fSBharat Bhushan  * Regarding timer, When CPU state changes to running after debug halt
32431f2cb8fSBharat Bhushan  * or similar cases which takes time then in between final watchdog
32531f2cb8fSBharat Bhushan  * expiry happenes. This will cause exit to QEMU and configured watchdog
32631f2cb8fSBharat Bhushan  * action will be taken. To avoid this we always clear the watchdog state when
32731f2cb8fSBharat Bhushan  * state changes to running.
32831f2cb8fSBharat Bhushan  */
cpu_state_change_handler(void * opaque,bool running,RunState state)329538f0497SPhilippe Mathieu-Daudé static void cpu_state_change_handler(void *opaque, bool running, RunState state)
33031f2cb8fSBharat Bhushan {
33131f2cb8fSBharat Bhushan     PowerPCCPU *cpu = opaque;
33231f2cb8fSBharat Bhushan     CPUPPCState *env = &cpu->env;
33331f2cb8fSBharat Bhushan 
33431f2cb8fSBharat Bhushan     if (!running) {
33531f2cb8fSBharat Bhushan         return;
33631f2cb8fSBharat Bhushan     }
33731f2cb8fSBharat Bhushan 
33831f2cb8fSBharat Bhushan     /*
33931f2cb8fSBharat Bhushan      * Clear watchdog interrupt condition by clearing TSR.
34031f2cb8fSBharat Bhushan      */
34131f2cb8fSBharat Bhushan     store_booke_tsr(env, TSR_ENW | TSR_WIS | TSR_WRS_MASK);
34253018216SPaolo Bonzini }
34353018216SPaolo Bonzini 
ppc_booke_timers_init(PowerPCCPU * cpu,uint32_t freq,uint32_t flags)34453018216SPaolo Bonzini void ppc_booke_timers_init(PowerPCCPU *cpu, uint32_t freq, uint32_t flags)
34553018216SPaolo Bonzini {
34653018216SPaolo Bonzini     ppc_tb_t *tb_env;
34753018216SPaolo Bonzini     booke_timer_t *booke_timer;
34831f2cb8fSBharat Bhushan     int ret = 0;
34953018216SPaolo Bonzini 
350b21e2380SMarkus Armbruster     tb_env      = g_new0(ppc_tb_t, 1);
351b21e2380SMarkus Armbruster     booke_timer = g_new0(booke_timer_t, 1);
35253018216SPaolo Bonzini 
35353018216SPaolo Bonzini     cpu->env.tb_env = tb_env;
35453018216SPaolo Bonzini     tb_env->flags = flags | PPC_TIMER_BOOKE | PPC_DECR_ZERO_TRIGGERED;
35553018216SPaolo Bonzini 
35653018216SPaolo Bonzini     tb_env->tb_freq    = freq;
35753018216SPaolo Bonzini     tb_env->decr_freq  = freq;
35853018216SPaolo Bonzini     tb_env->opaque     = booke_timer;
359bc72ad67SAlex Bligh     tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &booke_decr_cb, cpu);
36053018216SPaolo Bonzini 
36153018216SPaolo Bonzini     booke_timer->fit_timer =
362bc72ad67SAlex Bligh         timer_new_ns(QEMU_CLOCK_VIRTUAL, &booke_fit_cb, cpu);
36353018216SPaolo Bonzini     booke_timer->wdt_timer =
364bc72ad67SAlex Bligh         timer_new_ns(QEMU_CLOCK_VIRTUAL, &booke_wdt_cb, cpu);
36553018216SPaolo Bonzini 
36631f2cb8fSBharat Bhushan     ret = kvmppc_booke_watchdog_enable(cpu);
36731f2cb8fSBharat Bhushan 
36831f2cb8fSBharat Bhushan     if (ret) {
36931f2cb8fSBharat Bhushan         /* TODO: Start the QEMU emulated watchdog if not running on KVM.
37031f2cb8fSBharat Bhushan          * Also start the QEMU emulated watchdog if KVM does not support
37131f2cb8fSBharat Bhushan          * emulated watchdog or somehow it is not enabled (supported but
37231f2cb8fSBharat Bhushan          * not enabled is though some bug and requires debugging :)).
37331f2cb8fSBharat Bhushan          */
37431f2cb8fSBharat Bhushan     }
37531f2cb8fSBharat Bhushan 
37631f2cb8fSBharat Bhushan     qemu_add_vm_change_state_handler(cpu_state_change_handler, cpu);
37731f2cb8fSBharat Bhushan 
37853018216SPaolo Bonzini     qemu_register_reset(ppc_booke_timer_reset_handle, cpu);
37953018216SPaolo Bonzini }
380