net: introduce qemu_find_net_clients_except()
[qemu.git] / hw / ppc_booke.c
blob25a4e91b6966fd9a5ad12bbc7a41711e66bec15c
1 /*
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
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "ppc.h"
26 #include "qemu/timer.h"
27 #include "sysemu/sysemu.h"
28 #include "nvram.h"
29 #include "qemu/log.h"
30 #include "loader.h"
33 /* Timer Control Register */
35 #define TCR_WP_SHIFT 30 /* Watchdog Timer Period */
36 #define TCR_WP_MASK (0x3 << TCR_WP_SHIFT)
37 #define TCR_WRC_SHIFT 28 /* Watchdog Timer Reset Control */
38 #define TCR_WRC_MASK (0x3 << TCR_WRC_SHIFT)
39 #define TCR_WIE (1 << 27) /* Watchdog Timer Interrupt Enable */
40 #define TCR_DIE (1 << 26) /* Decrementer Interrupt Enable */
41 #define TCR_FP_SHIFT 24 /* Fixed-Interval Timer Period */
42 #define TCR_FP_MASK (0x3 << TCR_FP_SHIFT)
43 #define TCR_FIE (1 << 23) /* Fixed-Interval Timer Interrupt Enable */
44 #define TCR_ARE (1 << 22) /* Auto-Reload Enable */
46 /* Timer Control Register (e500 specific fields) */
48 #define TCR_E500_FPEXT_SHIFT 13 /* Fixed-Interval Timer Period Extension */
49 #define TCR_E500_FPEXT_MASK (0xf << TCR_E500_FPEXT_SHIFT)
50 #define TCR_E500_WPEXT_SHIFT 17 /* Watchdog Timer Period Extension */
51 #define TCR_E500_WPEXT_MASK (0xf << TCR_E500_WPEXT_SHIFT)
53 /* Timer Status Register */
55 #define TSR_FIS (1 << 26) /* Fixed-Interval Timer Interrupt Status */
56 #define TSR_DIS (1 << 27) /* Decrementer Interrupt Status */
57 #define TSR_WRS_SHIFT 28 /* Watchdog Timer Reset Status */
58 #define TSR_WRS_MASK (0x3 << TSR_WRS_SHIFT)
59 #define TSR_WIS (1 << 30) /* Watchdog Timer Interrupt Status */
60 #define TSR_ENW (1 << 31) /* Enable Next Watchdog Timer */
62 typedef struct booke_timer_t booke_timer_t;
63 struct booke_timer_t {
65 uint64_t fit_next;
66 struct QEMUTimer *fit_timer;
68 uint64_t wdt_next;
69 struct QEMUTimer *wdt_timer;
71 uint32_t flags;
74 static void booke_update_irq(PowerPCCPU *cpu)
76 CPUPPCState *env = &cpu->env;
78 ppc_set_irq(cpu, PPC_INTERRUPT_DECR,
79 (env->spr[SPR_BOOKE_TSR] & TSR_DIS
80 && env->spr[SPR_BOOKE_TCR] & TCR_DIE));
82 ppc_set_irq(cpu, PPC_INTERRUPT_WDT,
83 (env->spr[SPR_BOOKE_TSR] & TSR_WIS
84 && env->spr[SPR_BOOKE_TCR] & TCR_WIE));
86 ppc_set_irq(cpu, PPC_INTERRUPT_FIT,
87 (env->spr[SPR_BOOKE_TSR] & TSR_FIS
88 && env->spr[SPR_BOOKE_TCR] & TCR_FIE));
91 /* Return the location of the bit of time base at which the FIT will raise an
92 interrupt */
93 static uint8_t booke_get_fit_target(CPUPPCState *env, ppc_tb_t *tb_env)
95 uint8_t fp = (env->spr[SPR_BOOKE_TCR] & TCR_FP_MASK) >> TCR_FP_SHIFT;
97 if (tb_env->flags & PPC_TIMER_E500) {
98 /* e500 Fixed-interval timer period extension */
99 uint32_t fpext = (env->spr[SPR_BOOKE_TCR] & TCR_E500_FPEXT_MASK)
100 >> TCR_E500_FPEXT_SHIFT;
101 fp = 63 - (fp | fpext << 2);
102 } else {
103 fp = env->fit_period[fp];
106 return fp;
109 /* Return the location of the bit of time base at which the WDT will raise an
110 interrupt */
111 static uint8_t booke_get_wdt_target(CPUPPCState *env, ppc_tb_t *tb_env)
113 uint8_t wp = (env->spr[SPR_BOOKE_TCR] & TCR_WP_MASK) >> TCR_WP_SHIFT;
115 if (tb_env->flags & PPC_TIMER_E500) {
116 /* e500 Watchdog timer period extension */
117 uint32_t wpext = (env->spr[SPR_BOOKE_TCR] & TCR_E500_WPEXT_MASK)
118 >> TCR_E500_WPEXT_SHIFT;
119 wp = 63 - (wp | wpext << 2);
120 } else {
121 wp = env->wdt_period[wp];
124 return wp;
127 static void booke_update_fixed_timer(CPUPPCState *env,
128 uint8_t target_bit,
129 uint64_t *next,
130 struct QEMUTimer *timer)
132 ppc_tb_t *tb_env = env->tb_env;
133 uint64_t lapse;
134 uint64_t tb;
135 uint64_t period = 1 << (target_bit + 1);
136 uint64_t now;
138 now = qemu_get_clock_ns(vm_clock);
139 tb = cpu_ppc_get_tb(tb_env, now, tb_env->tb_offset);
141 lapse = period - ((tb - (1 << target_bit)) & (period - 1));
143 *next = now + muldiv64(lapse, get_ticks_per_sec(), tb_env->tb_freq);
145 /* XXX: If expire time is now. We can't run the callback because we don't
146 * have access to it. So we just set the timer one nanosecond later.
149 if (*next == now) {
150 (*next)++;
153 qemu_mod_timer(timer, *next);
156 static void booke_decr_cb(void *opaque)
158 PowerPCCPU *cpu = opaque;
159 CPUPPCState *env = &cpu->env;
161 env->spr[SPR_BOOKE_TSR] |= TSR_DIS;
162 booke_update_irq(cpu);
164 if (env->spr[SPR_BOOKE_TCR] & TCR_ARE) {
165 /* Auto Reload */
166 cpu_ppc_store_decr(env, env->spr[SPR_BOOKE_DECAR]);
170 static void booke_fit_cb(void *opaque)
172 PowerPCCPU *cpu = opaque;
173 CPUPPCState *env = &cpu->env;
174 ppc_tb_t *tb_env;
175 booke_timer_t *booke_timer;
177 tb_env = env->tb_env;
178 booke_timer = tb_env->opaque;
179 env->spr[SPR_BOOKE_TSR] |= TSR_FIS;
181 booke_update_irq(cpu);
183 booke_update_fixed_timer(env,
184 booke_get_fit_target(env, tb_env),
185 &booke_timer->fit_next,
186 booke_timer->fit_timer);
189 static void booke_wdt_cb(void *opaque)
191 PowerPCCPU *cpu = opaque;
192 CPUPPCState *env = &cpu->env;
193 ppc_tb_t *tb_env;
194 booke_timer_t *booke_timer;
196 tb_env = env->tb_env;
197 booke_timer = tb_env->opaque;
199 /* TODO: There's lots of complicated stuff to do here */
201 booke_update_irq(cpu);
203 booke_update_fixed_timer(env,
204 booke_get_wdt_target(env, tb_env),
205 &booke_timer->wdt_next,
206 booke_timer->wdt_timer);
209 void store_booke_tsr(CPUPPCState *env, target_ulong val)
211 PowerPCCPU *cpu = ppc_env_get_cpu(env);
213 env->spr[SPR_BOOKE_TSR] &= ~val;
214 booke_update_irq(cpu);
217 void store_booke_tcr(CPUPPCState *env, target_ulong val)
219 PowerPCCPU *cpu = ppc_env_get_cpu(env);
220 ppc_tb_t *tb_env = env->tb_env;
221 booke_timer_t *booke_timer = tb_env->opaque;
223 tb_env = env->tb_env;
224 env->spr[SPR_BOOKE_TCR] = val;
226 booke_update_irq(cpu);
228 booke_update_fixed_timer(env,
229 booke_get_fit_target(env, tb_env),
230 &booke_timer->fit_next,
231 booke_timer->fit_timer);
233 booke_update_fixed_timer(env,
234 booke_get_wdt_target(env, tb_env),
235 &booke_timer->wdt_next,
236 booke_timer->wdt_timer);
240 static void ppc_booke_timer_reset_handle(void *opaque)
242 PowerPCCPU *cpu = opaque;
243 CPUPPCState *env = &cpu->env;
245 env->spr[SPR_BOOKE_TSR] = 0;
246 env->spr[SPR_BOOKE_TCR] = 0;
248 booke_update_irq(cpu);
251 void ppc_booke_timers_init(PowerPCCPU *cpu, uint32_t freq, uint32_t flags)
253 ppc_tb_t *tb_env;
254 booke_timer_t *booke_timer;
256 tb_env = g_malloc0(sizeof(ppc_tb_t));
257 booke_timer = g_malloc0(sizeof(booke_timer_t));
259 cpu->env.tb_env = tb_env;
260 tb_env->flags = flags | PPC_TIMER_BOOKE | PPC_DECR_ZERO_TRIGGERED;
262 tb_env->tb_freq = freq;
263 tb_env->decr_freq = freq;
264 tb_env->opaque = booke_timer;
265 tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &booke_decr_cb, cpu);
267 booke_timer->fit_timer =
268 qemu_new_timer_ns(vm_clock, &booke_fit_cb, cpu);
269 booke_timer->wdt_timer =
270 qemu_new_timer_ns(vm_clock, &booke_wdt_cb, cpu);
272 qemu_register_reset(ppc_booke_timer_reset_handle, cpu);