1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "hw/sparc/sun4m.h"
27#include "qemu/timer.h"
28#include "hw/ptimer.h"
29#include "hw/sysbus.h"
30#include "trace.h"
31#include "qemu/main-loop.h"
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#define MAX_CPUS 16
49
50typedef struct CPUTimerState {
51 qemu_irq irq;
52 ptimer_state *timer;
53 uint32_t count, counthigh, reached;
54
55 uint32_t run;
56 uint64_t limit;
57} CPUTimerState;
58
59#define TYPE_SLAVIO_TIMER "slavio_timer"
60#define SLAVIO_TIMER(obj) \
61 OBJECT_CHECK(SLAVIO_TIMERState, (obj), TYPE_SLAVIO_TIMER)
62
63typedef struct SLAVIO_TIMERState {
64 SysBusDevice parent_obj;
65
66 uint32_t num_cpus;
67 uint32_t cputimer_mode;
68 CPUTimerState cputimer[MAX_CPUS + 1];
69} SLAVIO_TIMERState;
70
71typedef struct TimerContext {
72 MemoryRegion iomem;
73 SLAVIO_TIMERState *s;
74 unsigned int timer_index;
75} TimerContext;
76
77#define SYS_TIMER_SIZE 0x14
78#define CPU_TIMER_SIZE 0x10
79
80#define TIMER_LIMIT 0
81#define TIMER_COUNTER 1
82#define TIMER_COUNTER_NORST 2
83#define TIMER_STATUS 3
84#define TIMER_MODE 4
85
86#define TIMER_COUNT_MASK32 0xfffffe00
87#define TIMER_LIMIT_MASK32 0x7fffffff
88#define TIMER_MAX_COUNT64 0x7ffffffffffffe00ULL
89#define TIMER_MAX_COUNT32 0x7ffffe00ULL
90#define TIMER_REACHED 0x80000000
91#define TIMER_PERIOD 500ULL
92#define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
93#define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
94
95static int slavio_timer_is_user(TimerContext *tc)
96{
97 SLAVIO_TIMERState *s = tc->s;
98 unsigned int timer_index = tc->timer_index;
99
100 return timer_index != 0 && (s->cputimer_mode & (1 << (timer_index - 1)));
101}
102
103
104
105static void slavio_timer_get_out(CPUTimerState *t)
106{
107 uint64_t count, limit;
108
109 if (t->limit == 0) {
110 limit = TIMER_MAX_COUNT32;
111 } else {
112 limit = t->limit;
113 }
114 count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));
115
116 trace_slavio_timer_get_out(t->limit, t->counthigh, t->count);
117 t->count = count & TIMER_COUNT_MASK32;
118 t->counthigh = count >> 32;
119}
120
121
122static void slavio_timer_irq(void *opaque)
123{
124 TimerContext *tc = opaque;
125 SLAVIO_TIMERState *s = tc->s;
126 CPUTimerState *t = &s->cputimer[tc->timer_index];
127
128 slavio_timer_get_out(t);
129 trace_slavio_timer_irq(t->counthigh, t->count);
130
131 if (t->limit != 0) {
132 t->reached = TIMER_REACHED;
133 }
134
135 if (!slavio_timer_is_user(tc) && t->limit != 0) {
136 qemu_irq_raise(t->irq);
137 }
138}
139
140static uint64_t slavio_timer_mem_readl(void *opaque, hwaddr addr,
141 unsigned size)
142{
143 TimerContext *tc = opaque;
144 SLAVIO_TIMERState *s = tc->s;
145 uint32_t saddr, ret;
146 unsigned int timer_index = tc->timer_index;
147 CPUTimerState *t = &s->cputimer[timer_index];
148
149 saddr = addr >> 2;
150 switch (saddr) {
151 case TIMER_LIMIT:
152
153
154 if (slavio_timer_is_user(tc)) {
155
156 slavio_timer_get_out(t);
157 ret = t->counthigh | t->reached;
158 } else {
159
160
161 qemu_irq_lower(t->irq);
162 t->reached = 0;
163 ret = t->limit & TIMER_LIMIT_MASK32;
164 }
165 break;
166 case TIMER_COUNTER:
167
168
169 slavio_timer_get_out(t);
170 if (slavio_timer_is_user(tc)) {
171 ret = t->count & TIMER_MAX_COUNT64;
172 } else {
173 ret = (t->count & TIMER_MAX_COUNT32) |
174 t->reached;
175 }
176 break;
177 case TIMER_STATUS:
178
179
180 if (timer_index > 0) {
181 ret = t->run;
182 } else {
183 ret = 0;
184 }
185 break;
186 case TIMER_MODE:
187
188
189 ret = s->cputimer_mode;
190 break;
191 default:
192 trace_slavio_timer_mem_readl_invalid(addr);
193 ret = 0;
194 break;
195 }
196 trace_slavio_timer_mem_readl(addr, ret);
197 return ret;
198}
199
200static void slavio_timer_mem_writel(void *opaque, hwaddr addr,
201 uint64_t val, unsigned size)
202{
203 TimerContext *tc = opaque;
204 SLAVIO_TIMERState *s = tc->s;
205 uint32_t saddr;
206 unsigned int timer_index = tc->timer_index;
207 CPUTimerState *t = &s->cputimer[timer_index];
208
209 trace_slavio_timer_mem_writel(addr, val);
210 saddr = addr >> 2;
211 switch (saddr) {
212 case TIMER_LIMIT:
213 if (slavio_timer_is_user(tc)) {
214 uint64_t count;
215
216
217 t->limit = TIMER_MAX_COUNT64;
218 t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
219 t->reached = 0;
220 count = ((uint64_t)t->counthigh << 32) | t->count;
221 trace_slavio_timer_mem_writel_limit(timer_index, count);
222 ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
223 } else {
224
225 qemu_irq_lower(t->irq);
226 t->limit = val & TIMER_MAX_COUNT32;
227 if (t->timer) {
228 if (t->limit == 0) {
229 ptimer_set_limit(t->timer,
230 LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
231 } else {
232 ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
233 }
234 }
235 }
236 break;
237 case TIMER_COUNTER:
238 if (slavio_timer_is_user(tc)) {
239 uint64_t count;
240
241
242 t->limit = TIMER_MAX_COUNT64;
243 t->count = val & TIMER_MAX_COUNT64;
244 t->reached = 0;
245 count = ((uint64_t)t->counthigh) << 32 | t->count;
246 trace_slavio_timer_mem_writel_limit(timer_index, count);
247 ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
248 } else {
249 trace_slavio_timer_mem_writel_counter_invalid();
250 }
251 break;
252 case TIMER_COUNTER_NORST:
253
254 t->limit = val & TIMER_MAX_COUNT32;
255 if (t->limit == 0) {
256 ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
257 } else {
258 ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 0);
259 }
260 break;
261 case TIMER_STATUS:
262 if (slavio_timer_is_user(tc)) {
263
264 if (val & 1) {
265 trace_slavio_timer_mem_writel_status_start(timer_index);
266 ptimer_run(t->timer, 0);
267 } else {
268 trace_slavio_timer_mem_writel_status_stop(timer_index);
269 ptimer_stop(t->timer);
270 }
271 }
272 t->run = val & 1;
273 break;
274 case TIMER_MODE:
275 if (timer_index == 0) {
276 unsigned int i;
277
278 for (i = 0; i < s->num_cpus; i++) {
279 unsigned int processor = 1 << i;
280 CPUTimerState *curr_timer = &s->cputimer[i + 1];
281
282
283 if ((val & processor) != (s->cputimer_mode & processor)) {
284 if (val & processor) {
285 qemu_irq_lower(curr_timer->irq);
286
287 if (!curr_timer->run) {
288 ptimer_stop(curr_timer->timer);
289 }
290
291 curr_timer->limit = TIMER_MAX_COUNT64;
292 ptimer_set_limit(curr_timer->timer,
293 LIMIT_TO_PERIODS(curr_timer->limit),
294 1);
295
296
297 s->cputimer_mode |= processor;
298 trace_slavio_timer_mem_writel_mode_user(timer_index);
299 } else {
300
301 ptimer_run(curr_timer->timer, 0);
302
303
304 s->cputimer_mode &= ~processor;
305 trace_slavio_timer_mem_writel_mode_counter(timer_index);
306 }
307 }
308 }
309 } else {
310 trace_slavio_timer_mem_writel_mode_invalid();
311 }
312 break;
313 default:
314 trace_slavio_timer_mem_writel_invalid(addr);
315 break;
316 }
317}
318
319static const MemoryRegionOps slavio_timer_mem_ops = {
320 .read = slavio_timer_mem_readl,
321 .write = slavio_timer_mem_writel,
322 .endianness = DEVICE_NATIVE_ENDIAN,
323 .valid = {
324 .min_access_size = 4,
325 .max_access_size = 4,
326 },
327};
328
329static const VMStateDescription vmstate_timer = {
330 .name ="timer",
331 .version_id = 3,
332 .minimum_version_id = 3,
333 .fields = (VMStateField[]) {
334 VMSTATE_UINT64(limit, CPUTimerState),
335 VMSTATE_UINT32(count, CPUTimerState),
336 VMSTATE_UINT32(counthigh, CPUTimerState),
337 VMSTATE_UINT32(reached, CPUTimerState),
338 VMSTATE_UINT32(run , CPUTimerState),
339 VMSTATE_PTIMER(timer, CPUTimerState),
340 VMSTATE_END_OF_LIST()
341 }
342};
343
344static const VMStateDescription vmstate_slavio_timer = {
345 .name ="slavio_timer",
346 .version_id = 3,
347 .minimum_version_id = 3,
348 .fields = (VMStateField[]) {
349 VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
350 vmstate_timer, CPUTimerState),
351 VMSTATE_END_OF_LIST()
352 }
353};
354
355static void slavio_timer_reset(DeviceState *d)
356{
357 SLAVIO_TIMERState *s = SLAVIO_TIMER(d);
358 unsigned int i;
359 CPUTimerState *curr_timer;
360
361 for (i = 0; i <= MAX_CPUS; i++) {
362 curr_timer = &s->cputimer[i];
363 curr_timer->limit = 0;
364 curr_timer->count = 0;
365 curr_timer->reached = 0;
366 if (i <= s->num_cpus) {
367 ptimer_set_limit(curr_timer->timer,
368 LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
369 ptimer_run(curr_timer->timer, 0);
370 curr_timer->run = 1;
371 }
372 }
373 s->cputimer_mode = 0;
374}
375
376static void slavio_timer_init(Object *obj)
377{
378 SLAVIO_TIMERState *s = SLAVIO_TIMER(obj);
379 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
380 QEMUBH *bh;
381 unsigned int i;
382 TimerContext *tc;
383
384 for (i = 0; i <= MAX_CPUS; i++) {
385 uint64_t size;
386 char timer_name[20];
387
388 tc = g_malloc0(sizeof(TimerContext));
389 tc->s = s;
390 tc->timer_index = i;
391
392 bh = qemu_bh_new(slavio_timer_irq, tc);
393 s->cputimer[i].timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
394 ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
395
396 size = i == 0 ? SYS_TIMER_SIZE : CPU_TIMER_SIZE;
397 snprintf(timer_name, sizeof(timer_name), "timer-%i", i);
398 memory_region_init_io(&tc->iomem, obj, &slavio_timer_mem_ops, tc,
399 timer_name, size);
400 sysbus_init_mmio(dev, &tc->iomem);
401
402 sysbus_init_irq(dev, &s->cputimer[i].irq);
403 }
404}
405
406static Property slavio_timer_properties[] = {
407 DEFINE_PROP_UINT32("num_cpus", SLAVIO_TIMERState, num_cpus, 0),
408 DEFINE_PROP_END_OF_LIST(),
409};
410
411static void slavio_timer_class_init(ObjectClass *klass, void *data)
412{
413 DeviceClass *dc = DEVICE_CLASS(klass);
414
415 dc->reset = slavio_timer_reset;
416 dc->vmsd = &vmstate_slavio_timer;
417 dc->props = slavio_timer_properties;
418}
419
420static const TypeInfo slavio_timer_info = {
421 .name = TYPE_SLAVIO_TIMER,
422 .parent = TYPE_SYS_BUS_DEVICE,
423 .instance_size = sizeof(SLAVIO_TIMERState),
424 .instance_init = slavio_timer_init,
425 .class_init = slavio_timer_class_init,
426};
427
428static void slavio_timer_register_types(void)
429{
430 type_register_static(&slavio_timer_info);
431}
432
433type_init(slavio_timer_register_types)
434