1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "hw.h"
25#include "ppc.h"
26#include "qemu-timer.h"
27#include "sysemu.h"
28#include "nvram.h"
29#include "qemu-log.h"
30#include "loader.h"
31
32
33
34
35#define TCR_WP_SHIFT 30
36#define TCR_WP_MASK (0x3 << TCR_WP_SHIFT)
37#define TCR_WRC_SHIFT 28
38#define TCR_WRC_MASK (0x3 << TCR_WRC_SHIFT)
39#define TCR_WIE (1 << 27)
40#define TCR_DIE (1 << 26)
41#define TCR_FP_SHIFT 24
42#define TCR_FP_MASK (0x3 << TCR_FP_SHIFT)
43#define TCR_FIE (1 << 23)
44#define TCR_ARE (1 << 22)
45
46
47
48#define TCR_E500_FPEXT_SHIFT 13
49#define TCR_E500_FPEXT_MASK (0xf << TCR_E500_FPEXT_SHIFT)
50#define TCR_E500_WPEXT_SHIFT 17
51#define TCR_E500_WPEXT_MASK (0xf << TCR_E500_WPEXT_SHIFT)
52
53
54
55#define TSR_FIS (1 << 26)
56#define TSR_DIS (1 << 27)
57#define TSR_WRS_SHIFT 28
58#define TSR_WRS_MASK (0x3 << TSR_WRS_SHIFT)
59#define TSR_WIS (1 << 30)
60#define TSR_ENW (1 << 31)
61
62typedef struct booke_timer_t booke_timer_t;
63struct booke_timer_t {
64
65 uint64_t fit_next;
66 struct QEMUTimer *fit_timer;
67
68 uint64_t wdt_next;
69 struct QEMUTimer *wdt_timer;
70
71 uint32_t flags;
72};
73
74static void booke_update_irq(CPUPPCState *env)
75{
76 ppc_set_irq(env, PPC_INTERRUPT_DECR,
77 (env->spr[SPR_BOOKE_TSR] & TSR_DIS
78 && env->spr[SPR_BOOKE_TCR] & TCR_DIE));
79
80 ppc_set_irq(env, PPC_INTERRUPT_WDT,
81 (env->spr[SPR_BOOKE_TSR] & TSR_WIS
82 && env->spr[SPR_BOOKE_TCR] & TCR_WIE));
83
84 ppc_set_irq(env, PPC_INTERRUPT_FIT,
85 (env->spr[SPR_BOOKE_TSR] & TSR_FIS
86 && env->spr[SPR_BOOKE_TCR] & TCR_FIE));
87}
88
89
90
91static uint8_t booke_get_fit_target(CPUPPCState *env, ppc_tb_t *tb_env)
92{
93 uint8_t fp = (env->spr[SPR_BOOKE_TCR] & TCR_FP_MASK) >> TCR_FP_SHIFT;
94
95 if (tb_env->flags & PPC_TIMER_E500) {
96
97 uint32_t fpext = (env->spr[SPR_BOOKE_TCR] & TCR_E500_FPEXT_MASK)
98 >> TCR_E500_FPEXT_SHIFT;
99 fp = 63 - (fp | fpext << 2);
100 } else {
101 fp = env->fit_period[fp];
102 }
103
104 return fp;
105}
106
107
108
109static uint8_t booke_get_wdt_target(CPUPPCState *env, ppc_tb_t *tb_env)
110{
111 uint8_t wp = (env->spr[SPR_BOOKE_TCR] & TCR_WP_MASK) >> TCR_WP_SHIFT;
112
113 if (tb_env->flags & PPC_TIMER_E500) {
114
115 uint32_t wpext = (env->spr[SPR_BOOKE_TCR] & TCR_E500_WPEXT_MASK)
116 >> TCR_E500_WPEXT_SHIFT;
117 wp = 63 - (wp | wpext << 2);
118 } else {
119 wp = env->wdt_period[wp];
120 }
121
122 return wp;
123}
124
125static void booke_update_fixed_timer(CPUPPCState *env,
126 uint8_t target_bit,
127 uint64_t *next,
128 struct QEMUTimer *timer)
129{
130 ppc_tb_t *tb_env = env->tb_env;
131 uint64_t lapse;
132 uint64_t tb;
133 uint64_t period = 1 << (target_bit + 1);
134 uint64_t now;
135
136 now = qemu_get_clock_ns(vm_clock);
137 tb = cpu_ppc_get_tb(tb_env, now, tb_env->tb_offset);
138
139 lapse = period - ((tb - (1 << target_bit)) & (period - 1));
140
141 *next = now + muldiv64(lapse, get_ticks_per_sec(), tb_env->tb_freq);
142
143
144
145
146
147 if (*next == now) {
148 (*next)++;
149 }
150
151 qemu_mod_timer(timer, *next);
152}
153
154static void booke_decr_cb(void *opaque)
155{
156 CPUPPCState *env = opaque;
157
158 env->spr[SPR_BOOKE_TSR] |= TSR_DIS;
159 booke_update_irq(env);
160
161 if (env->spr[SPR_BOOKE_TCR] & TCR_ARE) {
162
163 cpu_ppc_store_decr(env, env->spr[SPR_BOOKE_DECAR]);
164 }
165}
166
167static void booke_fit_cb(void *opaque)
168{
169 CPUPPCState *env;
170 ppc_tb_t *tb_env;
171 booke_timer_t *booke_timer;
172
173 env = opaque;
174 tb_env = env->tb_env;
175 booke_timer = tb_env->opaque;
176 env->spr[SPR_BOOKE_TSR] |= TSR_FIS;
177
178 booke_update_irq(env);
179
180 booke_update_fixed_timer(env,
181 booke_get_fit_target(env, tb_env),
182 &booke_timer->fit_next,
183 booke_timer->fit_timer);
184}
185
186static void booke_wdt_cb(void *opaque)
187{
188 CPUPPCState *env;
189 ppc_tb_t *tb_env;
190 booke_timer_t *booke_timer;
191
192 env = opaque;
193 tb_env = env->tb_env;
194 booke_timer = tb_env->opaque;
195
196
197
198 booke_update_irq(env);
199
200 booke_update_fixed_timer(env,
201 booke_get_wdt_target(env, tb_env),
202 &booke_timer->wdt_next,
203 booke_timer->wdt_timer);
204}
205
206void store_booke_tsr(CPUPPCState *env, target_ulong val)
207{
208 env->spr[SPR_BOOKE_TSR] &= ~val;
209 booke_update_irq(env);
210}
211
212void store_booke_tcr(CPUPPCState *env, target_ulong val)
213{
214 ppc_tb_t *tb_env = env->tb_env;
215 booke_timer_t *booke_timer = tb_env->opaque;
216
217 tb_env = env->tb_env;
218 env->spr[SPR_BOOKE_TCR] = val;
219
220 booke_update_irq(env);
221
222 booke_update_fixed_timer(env,
223 booke_get_fit_target(env, tb_env),
224 &booke_timer->fit_next,
225 booke_timer->fit_timer);
226
227 booke_update_fixed_timer(env,
228 booke_get_wdt_target(env, tb_env),
229 &booke_timer->wdt_next,
230 booke_timer->wdt_timer);
231
232}
233
234void ppc_booke_timers_init(CPUPPCState *env, uint32_t freq, uint32_t flags)
235{
236 ppc_tb_t *tb_env;
237 booke_timer_t *booke_timer;
238
239 tb_env = g_malloc0(sizeof(ppc_tb_t));
240 booke_timer = g_malloc0(sizeof(booke_timer_t));
241
242 env->tb_env = tb_env;
243 tb_env->flags = flags | PPC_TIMER_BOOKE | PPC_DECR_ZERO_TRIGGERED;
244
245 tb_env->tb_freq = freq;
246 tb_env->decr_freq = freq;
247 tb_env->opaque = booke_timer;
248 tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &booke_decr_cb, env);
249
250 booke_timer->fit_timer =
251 qemu_new_timer_ns(vm_clock, &booke_fit_cb, env);
252 booke_timer->wdt_timer =
253 qemu_new_timer_ns(vm_clock, &booke_wdt_cb, env);
254}
255