1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "qemu/osdep.h"
20#include "hw/sysbus.h"
21#include "qemu/timer.h"
22
23#ifdef CADENCE_TTC_ERR_DEBUG
24#define DB_PRINT(...) do { \
25 fprintf(stderr, ": %s: ", __func__); \
26 fprintf(stderr, ## __VA_ARGS__); \
27 } while (0);
28#else
29 #define DB_PRINT(...)
30#endif
31
32#define COUNTER_INTR_IV 0x00000001
33#define COUNTER_INTR_M1 0x00000002
34#define COUNTER_INTR_M2 0x00000004
35#define COUNTER_INTR_M3 0x00000008
36#define COUNTER_INTR_OV 0x00000010
37#define COUNTER_INTR_EV 0x00000020
38
39#define COUNTER_CTRL_DIS 0x00000001
40#define COUNTER_CTRL_INT 0x00000002
41#define COUNTER_CTRL_DEC 0x00000004
42#define COUNTER_CTRL_MATCH 0x00000008
43#define COUNTER_CTRL_RST 0x00000010
44
45#define CLOCK_CTRL_PS_EN 0x00000001
46#define CLOCK_CTRL_PS_V 0x0000001e
47
48typedef struct CadenceTTCState CadenceTTCState;
49
50typedef struct {
51 QEMUTimer *timer;
52 int freq;
53
54 uint32_t reg_clock;
55 uint32_t reg_count;
56 uint64_t reg_value;
57 uint32_t reg_interval;
58 uint32_t reg_match[3];
59 uint32_t reg_intr;
60 uint32_t reg_intr_en;
61 uint32_t reg_event_ctrl;
62 uint32_t reg_event;
63
64 uint64_t cpu_time;
65 unsigned int cpu_time_valid;
66
67 CadenceTTCState *container;
68
69 qemu_irq irq;
70} CadenceTimerState;
71
72#define TYPE_CADENCE_TTC "cadence_ttc"
73#define CADENCE_TTC(obj) \
74 OBJECT_CHECK(CadenceTTCState, (obj), TYPE_CADENCE_TTC)
75
76struct CadenceTTCState {
77 SysBusDevice parent_obj;
78
79 MemoryRegion iomem;
80
81 uint8_t bit_width;
82 CadenceTimerState timer[3];
83};
84
85static void cadence_timer_update(CadenceTimerState *s)
86{
87 qemu_set_irq(s->irq, !!(s->reg_intr & s->reg_intr_en));
88}
89
90static CadenceTimerState *cadence_timer_from_addr(void *opaque,
91 hwaddr offset)
92{
93 unsigned int index;
94 CadenceTTCState *s = (CadenceTTCState *)opaque;
95
96 index = (offset >> 2) % 3;
97
98 return &s->timer[index];
99}
100
101static uint64_t cadence_timer_get_ns(CadenceTimerState *s, uint64_t timer_steps)
102{
103 uint64_t r = timer_steps * 1000000000ULL;
104 if (s->reg_clock & CLOCK_CTRL_PS_EN) {
105 r >>= 16 - (((s->reg_clock & CLOCK_CTRL_PS_V) >> 1) + 1);
106 } else {
107 r >>= 16;
108 }
109 r /= (uint64_t)s->freq;
110
111 r = MAX(r, 1);
112 return r;
113}
114
115static uint64_t cadence_timer_get_steps(CadenceTimerState *s, uint64_t ns)
116{
117 uint64_t to_divide = 1000000000ULL;
118
119 uint64_t r = ns;
120
121
122 while (r >= 8 * NANOSECONDS_PER_SECOND && to_divide > 1) {
123 r /= 1000;
124 to_divide /= 1000;
125 }
126 r <<= 16;
127
128 while (r >= 8 * NANOSECONDS_PER_SECOND && to_divide > 1) {
129 r /= 1000;
130 to_divide /= 1000;
131 }
132 r *= (uint64_t)s->freq;
133 if (s->reg_clock & CLOCK_CTRL_PS_EN) {
134 r /= 1 << (((s->reg_clock & CLOCK_CTRL_PS_V) >> 1) + 1);
135 }
136
137 r /= to_divide;
138 return r;
139}
140
141
142
143static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
144{
145 if (a < b) {
146 return x > a && x <= b;
147 }
148 return x < a && x >= b;
149}
150
151static void cadence_timer_run(CadenceTimerState *s)
152{
153 int i;
154 int64_t event_interval, next_value;
155
156 assert(s->cpu_time_valid);
157
158 if (s->reg_count & COUNTER_CTRL_DIS) {
159 s->cpu_time_valid = 0;
160 return;
161 }
162
163 {
164 int64_t interval = (s->reg_count & COUNTER_CTRL_INT) ?
165 (int64_t)s->reg_interval + 1 :
166 1ull << s->container->bit_width;
167 interval <<= 16;
168 next_value = (s->reg_count & COUNTER_CTRL_DEC) ? -1ULL : interval;
169 for (i = 0; i < 3; ++i) {
170 int64_t cand = (uint64_t)s->reg_match[i] << 16;
171 if (is_between(cand, (uint64_t)s->reg_value, next_value)) {
172 next_value = cand;
173 }
174 }
175 }
176 DB_PRINT("next timer event value: %16llx\n",
177 (unsigned long long)next_value);
178
179 event_interval = next_value - (int64_t)s->reg_value;
180 event_interval = (event_interval < 0) ? -event_interval : event_interval;
181
182 timer_mod(s->timer, s->cpu_time +
183 cadence_timer_get_ns(s, event_interval));
184}
185
186static void cadence_timer_sync(CadenceTimerState *s)
187{
188 int i;
189 int64_t r, x;
190 int64_t interval = (s->reg_count & COUNTER_CTRL_INT) ?
191 (int64_t)s->reg_interval + 1 :
192 1ull << s->container->bit_width;
193 interval <<= 16;
194 uint64_t old_time = s->cpu_time;
195
196 s->cpu_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
197 DB_PRINT("cpu time: %lld ns\n", (long long)old_time);
198
199 if (!s->cpu_time_valid || old_time == s->cpu_time) {
200 s->cpu_time_valid = 1;
201 return;
202 }
203
204 r = (int64_t)cadence_timer_get_steps(s, s->cpu_time - old_time);
205 x = (int64_t)s->reg_value + ((s->reg_count & COUNTER_CTRL_DEC) ? -r : r);
206
207 for (i = 0; i < 3; ++i) {
208 int64_t m = (int64_t)s->reg_match[i] << 16;
209 if (m > interval) {
210 continue;
211 }
212
213
214 if (is_between(m, s->reg_value, x) ||
215 is_between(m + interval, s->reg_value, x) ||
216 is_between(m - interval, s->reg_value, x)) {
217 s->reg_intr |= (2 << i);
218 }
219 }
220 if ((x < 0) || (x >= interval)) {
221 s->reg_intr |= (s->reg_count & COUNTER_CTRL_INT) ?
222 COUNTER_INTR_IV : COUNTER_INTR_OV;
223 }
224 while (x < 0) {
225 x += interval;
226 }
227 s->reg_value = x % interval;
228
229 cadence_timer_update(s);
230}
231
232static void cadence_timer_tick(void *opaque)
233{
234 CadenceTimerState *s = opaque;
235
236 DB_PRINT("\n");
237 cadence_timer_sync(s);
238 cadence_timer_run(s);
239}
240
241static uint32_t cadence_ttc_read_imp(void *opaque, hwaddr offset)
242{
243 CadenceTimerState *s = cadence_timer_from_addr(opaque, offset);
244 uint32_t value;
245
246 cadence_timer_sync(s);
247 cadence_timer_run(s);
248
249 switch (offset) {
250 case 0x00:
251 case 0x04:
252 case 0x08:
253 return s->reg_clock;
254
255 case 0x0c:
256 case 0x10:
257 case 0x14:
258 return s->reg_count;
259
260 case 0x18:
261 case 0x1c:
262 case 0x20:
263 return (uint32_t)(s->reg_value >> 16);
264
265 case 0x24:
266 case 0x28:
267 case 0x2c:
268 return s->reg_interval;
269
270 case 0x30:
271 case 0x34:
272 case 0x38:
273 return s->reg_match[0];
274
275 case 0x3c:
276 case 0x40:
277 case 0x44:
278 return s->reg_match[1];
279
280 case 0x48:
281 case 0x4c:
282 case 0x50:
283 return s->reg_match[2];
284
285 case 0x54:
286 case 0x58:
287 case 0x5c:
288
289 value = s->reg_intr;
290 s->reg_intr = 0;
291 cadence_timer_update(s);
292 return value;
293
294 case 0x60:
295 case 0x64:
296 case 0x68:
297 return s->reg_intr_en;
298
299 case 0x6c:
300 case 0x70:
301 case 0x74:
302 return s->reg_event_ctrl;
303
304 case 0x78:
305 case 0x7c:
306 case 0x80:
307 return s->reg_event;
308
309 default:
310 return 0;
311 }
312}
313
314static uint64_t cadence_ttc_read(void *opaque, hwaddr offset,
315 unsigned size)
316{
317 uint32_t ret = cadence_ttc_read_imp(opaque, offset);
318
319 DB_PRINT("addr: %08x data: %08x\n", (unsigned)offset, (unsigned)ret);
320 return ret;
321}
322
323static void cadence_ttc_write(void *opaque, hwaddr offset,
324 uint64_t value, unsigned size)
325{
326 CadenceTimerState *s = cadence_timer_from_addr(opaque, offset);
327 uint64_t mask = (1ull << s->container->bit_width) - 1;
328
329 DB_PRINT("addr: %08x data %08x\n", (unsigned)offset, (unsigned)value);
330
331 cadence_timer_sync(s);
332
333 switch (offset) {
334 case 0x00:
335 case 0x04:
336 case 0x08:
337 s->reg_clock = value & 0x3F;
338 break;
339
340 case 0x0c:
341 case 0x10:
342 case 0x14:
343 if (value & COUNTER_CTRL_RST) {
344 s->reg_value = 0;
345 }
346 s->reg_count = value & 0x3f & ~COUNTER_CTRL_RST;
347 break;
348
349 case 0x24:
350 case 0x28:
351 case 0x2c:
352 s->reg_interval = value & mask;
353 break;
354
355 case 0x30:
356 case 0x34:
357 case 0x38:
358 s->reg_match[0] = value & mask;
359 break;
360
361 case 0x3c:
362 case 0x40:
363 case 0x44:
364 s->reg_match[1] = value & mask;
365 break;
366
367 case 0x48:
368 case 0x4c:
369 case 0x50:
370 s->reg_match[2] = value & mask;
371 break;
372
373 case 0x54:
374 case 0x58:
375 case 0x5c:
376 break;
377
378 case 0x60:
379 case 0x64:
380 case 0x68:
381 s->reg_intr_en = value & 0x3f;
382 break;
383
384 case 0x6c:
385 case 0x70:
386 case 0x74:
387 s->reg_event_ctrl = value & 0x07;
388 break;
389
390 default:
391 return;
392 }
393
394 cadence_timer_run(s);
395 cadence_timer_update(s);
396}
397
398static const MemoryRegionOps cadence_ttc_ops = {
399 .read = cadence_ttc_read,
400 .write = cadence_ttc_write,
401 .endianness = DEVICE_NATIVE_ENDIAN,
402};
403
404static void cadence_timer_reset(CadenceTimerState *s)
405{
406 s->reg_count = 0x21;
407}
408
409static void cadence_timer_init(uint32_t freq, CadenceTimerState *s)
410{
411 memset(s, 0, sizeof(CadenceTimerState));
412 s->freq = freq;
413
414 cadence_timer_reset(s);
415
416 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, cadence_timer_tick, s);
417}
418
419static void cadence_ttc_init(Object *obj)
420{
421 CadenceTTCState *s = CADENCE_TTC(obj);
422 int i;
423
424 for (i = 0; i < 3; ++i) {
425 cadence_timer_init(133000000, &s->timer[i]);
426 s->timer[i].container = s;
427 sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->timer[i].irq);
428 }
429
430 memory_region_init_io(&s->iomem, obj, &cadence_ttc_ops, s,
431 "timer", 0x1000);
432 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
433}
434
435static int cadence_timer_pre_save(void *opaque)
436{
437 cadence_timer_sync((CadenceTimerState *)opaque);
438
439 return 0;
440}
441
442static int cadence_timer_post_load(void *opaque, int version_id)
443{
444 CadenceTimerState *s = opaque;
445
446 s->cpu_time_valid = 0;
447 cadence_timer_sync(s);
448 cadence_timer_run(s);
449 cadence_timer_update(s);
450 return 0;
451}
452
453static const VMStateDescription vmstate_cadence_timer = {
454 .name = "cadence_timer",
455 .version_id = 1,
456 .minimum_version_id = 1,
457 .pre_save = cadence_timer_pre_save,
458 .post_load = cadence_timer_post_load,
459 .fields = (VMStateField[]) {
460 VMSTATE_UINT32(reg_clock, CadenceTimerState),
461 VMSTATE_UINT32(reg_count, CadenceTimerState),
462 VMSTATE_UINT64(reg_value, CadenceTimerState),
463 VMSTATE_UINT32(reg_interval, CadenceTimerState),
464 VMSTATE_UINT32_ARRAY(reg_match, CadenceTimerState, 3),
465 VMSTATE_UINT32(reg_intr, CadenceTimerState),
466 VMSTATE_UINT32(reg_intr_en, CadenceTimerState),
467 VMSTATE_UINT32(reg_event_ctrl, CadenceTimerState),
468 VMSTATE_UINT32(reg_event, CadenceTimerState),
469 VMSTATE_END_OF_LIST()
470 }
471};
472
473static const VMStateDescription vmstate_cadence_ttc = {
474 .name = "cadence_TTC",
475 .version_id = 1,
476 .minimum_version_id = 1,
477 .fields = (VMStateField[]) {
478 VMSTATE_STRUCT_ARRAY(timer, CadenceTTCState, 3, 0,
479 vmstate_cadence_timer,
480 CadenceTimerState),
481 VMSTATE_END_OF_LIST()
482 }
483};
484
485static Property cadence_ttc_props[] = {
486 DEFINE_PROP_UINT8("width", CadenceTTCState, bit_width, 16),
487 DEFINE_PROP_END_OF_LIST(),
488};
489
490static void cadence_ttc_class_init(ObjectClass *klass, void *data)
491{
492 DeviceClass *dc = DEVICE_CLASS(klass);
493
494 dc->vmsd = &vmstate_cadence_ttc;
495 dc->props = cadence_ttc_props;
496}
497
498static const TypeInfo cadence_ttc_info = {
499 .name = TYPE_CADENCE_TTC,
500 .parent = TYPE_SYS_BUS_DEVICE,
501 .instance_size = sizeof(CadenceTTCState),
502 .instance_init = cadence_ttc_init,
503 .class_init = cadence_ttc_class_init,
504};
505
506static void cadence_ttc_register_types(void)
507{
508 type_register_static(&cadence_ttc_info);
509}
510
511type_init(cadence_ttc_register_types)
512