]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/timer/slavio_timer.c
Include qemu/module.h where needed, drop it from qemu-common.h
[thirdparty/qemu.git] / hw / timer / slavio_timer.c
CommitLineData
e80cfcfc
FB
1/*
2 * QEMU Sparc SLAVIO timer controller emulation
3 *
66321a11 4 * Copyright (c) 2003-2005 Fabrice Bellard
5fafdf24 5 *
e80cfcfc
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
c70c59ee 24
282bc81e 25#include "qemu/osdep.h"
1de7afc9 26#include "qemu/timer.h"
83c9f4ca
PB
27#include "hw/ptimer.h"
28#include "hw/sysbus.h"
97bf4851 29#include "trace.h"
6a1751b7 30#include "qemu/main-loop.h"
0b8fa32f 31#include "qemu/module.h"
66321a11 32
e80cfcfc
FB
33/*
34 * Registers of hardware timer in sun4m.
35 *
36 * This is the timer/counter part of chip STP2001 (Slave I/O), also
37 * produced as NCR89C105. See
38 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
5fafdf24 39 *
e80cfcfc
FB
40 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
41 * are zero. Bit 31 is 1 when count has been reached.
42 *
ba3c64fb
FB
43 * Per-CPU timers interrupt local CPU, system timer uses normal
44 * interrupt routing.
45 *
e80cfcfc
FB
46 */
47
81732d19
BS
48#define MAX_CPUS 16
49
7204ff9c 50typedef struct CPUTimerState {
d7edfd27 51 qemu_irq irq;
8d05ea8a
BS
52 ptimer_state *timer;
53 uint32_t count, counthigh, reached;
f90074f4 54 /* processor only */
ead4cf04 55 uint32_t run;
f90074f4 56 uint64_t limit;
7204ff9c
BS
57} CPUTimerState;
58
c275471e
AF
59#define TYPE_SLAVIO_TIMER "slavio_timer"
60#define SLAVIO_TIMER(obj) \
61 OBJECT_CHECK(SLAVIO_TIMERState, (obj), TYPE_SLAVIO_TIMER)
62
7204ff9c 63typedef struct SLAVIO_TIMERState {
c275471e
AF
64 SysBusDevice parent_obj;
65
7204ff9c 66 uint32_t num_cpus;
7204ff9c 67 uint32_t cputimer_mode;
f90074f4 68 CPUTimerState cputimer[MAX_CPUS + 1];
e80cfcfc
FB
69} SLAVIO_TIMERState;
70
7204ff9c 71typedef struct TimerContext {
a3d12d07 72 MemoryRegion iomem;
7204ff9c
BS
73 SLAVIO_TIMERState *s;
74 unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
75} TimerContext;
76
115646b6 77#define SYS_TIMER_SIZE 0x14
81732d19 78#define CPU_TIMER_SIZE 0x10
e80cfcfc 79
d2c38b24
BS
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 // 500ns
68fb89a2
BS
92#define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
93#define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
d2c38b24 94
7204ff9c 95static int slavio_timer_is_user(TimerContext *tc)
115646b6 96{
7204ff9c
BS
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)));
115646b6
BS
101}
102
e80cfcfc 103// Update count, set irq, update expire_time
8d05ea8a 104// Convert from ptimer countdown units
7204ff9c 105static void slavio_timer_get_out(CPUTimerState *t)
e80cfcfc 106{
bd7e2875 107 uint64_t count, limit;
e80cfcfc 108
7204ff9c 109 if (t->limit == 0) { /* free-run system or processor counter */
bd7e2875 110 limit = TIMER_MAX_COUNT32;
7204ff9c
BS
111 } else {
112 limit = t->limit;
113 }
9ebec28b
BS
114 count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));
115
97bf4851 116 trace_slavio_timer_get_out(t->limit, t->counthigh, t->count);
7204ff9c
BS
117 t->count = count & TIMER_COUNT_MASK32;
118 t->counthigh = count >> 32;
e80cfcfc
FB
119}
120
121// timer callback
122static void slavio_timer_irq(void *opaque)
123{
7204ff9c
BS
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);
97bf4851 129 trace_slavio_timer_irq(t->counthigh, t->count);
68fb89a2
BS
130 /* if limit is 0 (free-run), there will be no match */
131 if (t->limit != 0) {
132 t->reached = TIMER_REACHED;
133 }
452efba6
BS
134 /* there is no interrupt if user timer or free-run */
135 if (!slavio_timer_is_user(tc) && t->limit != 0) {
7204ff9c
BS
136 qemu_irq_raise(t->irq);
137 }
e80cfcfc
FB
138}
139
a8170e5e 140static uint64_t slavio_timer_mem_readl(void *opaque, hwaddr addr,
a3d12d07 141 unsigned size)
e80cfcfc 142{
7204ff9c
BS
143 TimerContext *tc = opaque;
144 SLAVIO_TIMERState *s = tc->s;
8d05ea8a 145 uint32_t saddr, ret;
7204ff9c
BS
146 unsigned int timer_index = tc->timer_index;
147 CPUTimerState *t = &s->cputimer[timer_index];
e80cfcfc 148
e64d7d59 149 saddr = addr >> 2;
e80cfcfc 150 switch (saddr) {
d2c38b24 151 case TIMER_LIMIT:
f930d07e
BS
152 // read limit (system counter mode) or read most signifying
153 // part of counter (user mode)
7204ff9c 154 if (slavio_timer_is_user(tc)) {
115646b6 155 // read user timer MSW
7204ff9c
BS
156 slavio_timer_get_out(t);
157 ret = t->counthigh | t->reached;
115646b6
BS
158 } else {
159 // read limit
f930d07e 160 // clear irq
7204ff9c
BS
161 qemu_irq_lower(t->irq);
162 t->reached = 0;
163 ret = t->limit & TIMER_LIMIT_MASK32;
f930d07e 164 }
8d05ea8a 165 break;
d2c38b24 166 case TIMER_COUNTER:
f930d07e
BS
167 // read counter and reached bit (system mode) or read lsbits
168 // of counter (user mode)
7204ff9c
BS
169 slavio_timer_get_out(t);
170 if (slavio_timer_is_user(tc)) { // read user timer LSW
171 ret = t->count & TIMER_MAX_COUNT64;
172 } else { // read limit
173 ret = (t->count & TIMER_MAX_COUNT32) |
174 t->reached;
175 }
8d05ea8a 176 break;
d2c38b24 177 case TIMER_STATUS:
115646b6 178 // only available in processor counter/timer
f930d07e 179 // read start/stop status
7204ff9c 180 if (timer_index > 0) {
ead4cf04 181 ret = t->run;
7204ff9c
BS
182 } else {
183 ret = 0;
184 }
8d05ea8a 185 break;
d2c38b24 186 case TIMER_MODE:
115646b6 187 // only available in system counter
f930d07e 188 // read user/system mode
7204ff9c 189 ret = s->cputimer_mode;
8d05ea8a 190 break;
e80cfcfc 191 default:
97bf4851 192 trace_slavio_timer_mem_readl_invalid(addr);
8d05ea8a
BS
193 ret = 0;
194 break;
e80cfcfc 195 }
97bf4851 196 trace_slavio_timer_mem_readl(addr, ret);
8d05ea8a 197 return ret;
e80cfcfc
FB
198}
199
a8170e5e 200static void slavio_timer_mem_writel(void *opaque, hwaddr addr,
a3d12d07 201 uint64_t val, unsigned size)
e80cfcfc 202{
7204ff9c
BS
203 TimerContext *tc = opaque;
204 SLAVIO_TIMERState *s = tc->s;
e80cfcfc 205 uint32_t saddr;
7204ff9c
BS
206 unsigned int timer_index = tc->timer_index;
207 CPUTimerState *t = &s->cputimer[timer_index];
e80cfcfc 208
97bf4851 209 trace_slavio_timer_mem_writel(addr, val);
e64d7d59 210 saddr = addr >> 2;
e80cfcfc 211 switch (saddr) {
d2c38b24 212 case TIMER_LIMIT:
7204ff9c 213 if (slavio_timer_is_user(tc)) {
e1cb9502
BS
214 uint64_t count;
215
115646b6 216 // set user counter MSW, reset counter
7204ff9c
BS
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;
97bf4851 221 trace_slavio_timer_mem_writel_limit(timer_index, count);
9ebec28b 222 ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
115646b6
BS
223 } else {
224 // set limit, reset counter
7204ff9c
BS
225 qemu_irq_lower(t->irq);
226 t->limit = val & TIMER_MAX_COUNT32;
227 if (t->timer) {
228 if (t->limit == 0) { /* free-run */
229 ptimer_set_limit(t->timer,
77f193da 230 LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
7204ff9c
BS
231 } else {
232 ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
233 }
85e3023e 234 }
81732d19 235 }
115646b6 236 break;
d2c38b24 237 case TIMER_COUNTER:
7204ff9c 238 if (slavio_timer_is_user(tc)) {
e1cb9502
BS
239 uint64_t count;
240
115646b6 241 // set user counter LSW, reset counter
7204ff9c
BS
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;
97bf4851 246 trace_slavio_timer_mem_writel_limit(timer_index, count);
9ebec28b 247 ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
97bf4851
BS
248 } else {
249 trace_slavio_timer_mem_writel_counter_invalid();
250 }
115646b6 251 break;
d2c38b24 252 case TIMER_COUNTER_NORST:
f930d07e 253 // set limit without resetting counter
7204ff9c 254 t->limit = val & TIMER_MAX_COUNT32;
9ebec28b
BS
255 if (t->limit == 0) { /* free-run */
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);
85e3023e 259 }
f930d07e 260 break;
d2c38b24 261 case TIMER_STATUS:
7204ff9c 262 if (slavio_timer_is_user(tc)) {
115646b6 263 // start/stop user counter
ead4cf04 264 if (val & 1) {
97bf4851 265 trace_slavio_timer_mem_writel_status_start(timer_index);
9ebec28b 266 ptimer_run(t->timer, 0);
ead4cf04 267 } else {
97bf4851 268 trace_slavio_timer_mem_writel_status_stop(timer_index);
9ebec28b 269 ptimer_stop(t->timer);
f930d07e
BS
270 }
271 }
ead4cf04 272 t->run = val & 1;
f930d07e 273 break;
d2c38b24 274 case TIMER_MODE:
7204ff9c 275 if (timer_index == 0) {
81732d19
BS
276 unsigned int i;
277
7204ff9c 278 for (i = 0; i < s->num_cpus; i++) {
67e42751 279 unsigned int processor = 1 << i;
7204ff9c 280 CPUTimerState *curr_timer = &s->cputimer[i + 1];
67e42751
BS
281
282 // check for a change in timer mode for this processor
7204ff9c 283 if ((val & processor) != (s->cputimer_mode & processor)) {
67e42751 284 if (val & processor) { // counter -> user timer
7204ff9c 285 qemu_irq_lower(curr_timer->irq);
67e42751 286 // counters are always running
ead4cf04
MCA
287 if (!curr_timer->run) {
288 ptimer_stop(curr_timer->timer);
289 }
67e42751 290 // user timer limit is always the same
7204ff9c
BS
291 curr_timer->limit = TIMER_MAX_COUNT64;
292 ptimer_set_limit(curr_timer->timer,
293 LIMIT_TO_PERIODS(curr_timer->limit),
77f193da 294 1);
67e42751
BS
295 // set this processors user timer bit in config
296 // register
7204ff9c 297 s->cputimer_mode |= processor;
97bf4851 298 trace_slavio_timer_mem_writel_mode_user(timer_index);
67e42751 299 } else { // user timer -> counter
67e42751 300 // start the counter
7204ff9c 301 ptimer_run(curr_timer->timer, 0);
67e42751
BS
302 // clear this processors user timer bit in config
303 // register
7204ff9c 304 s->cputimer_mode &= ~processor;
97bf4851 305 trace_slavio_timer_mem_writel_mode_counter(timer_index);
67e42751 306 }
115646b6 307 }
81732d19 308 }
7204ff9c 309 } else {
97bf4851 310 trace_slavio_timer_mem_writel_mode_invalid();
7204ff9c 311 }
f930d07e 312 break;
e80cfcfc 313 default:
97bf4851 314 trace_slavio_timer_mem_writel_invalid(addr);
f930d07e 315 break;
e80cfcfc
FB
316 }
317}
318
a3d12d07
BC
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 },
e80cfcfc
FB
327};
328
f4b19cd0
BS
329static const VMStateDescription vmstate_timer = {
330 .name ="timer",
331 .version_id = 3,
332 .minimum_version_id = 3,
35d08458 333 .fields = (VMStateField[]) {
f4b19cd0
BS
334 VMSTATE_UINT64(limit, CPUTimerState),
335 VMSTATE_UINT32(count, CPUTimerState),
336 VMSTATE_UINT32(counthigh, CPUTimerState),
337 VMSTATE_UINT32(reached, CPUTimerState),
ead4cf04 338 VMSTATE_UINT32(run , CPUTimerState),
f4b19cd0
BS
339 VMSTATE_PTIMER(timer, CPUTimerState),
340 VMSTATE_END_OF_LIST()
7204ff9c 341 }
f4b19cd0 342};
e80cfcfc 343
f4b19cd0
BS
344static const VMStateDescription vmstate_slavio_timer = {
345 .name ="slavio_timer",
346 .version_id = 3,
347 .minimum_version_id = 3,
35d08458 348 .fields = (VMStateField[]) {
f4b19cd0
BS
349 VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
350 vmstate_timer, CPUTimerState),
351 VMSTATE_END_OF_LIST()
7204ff9c 352 }
f4b19cd0 353};
e80cfcfc 354
0e0bfeea 355static void slavio_timer_reset(DeviceState *d)
e80cfcfc 356{
c275471e 357 SLAVIO_TIMERState *s = SLAVIO_TIMER(d);
7204ff9c
BS
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;
5933e8a9 366 if (i <= s->num_cpus) {
7204ff9c
BS
367 ptimer_set_limit(curr_timer->timer,
368 LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
369 ptimer_run(curr_timer->timer, 0);
ead4cf04 370 curr_timer->run = 1;
7204ff9c 371 }
85e3023e 372 }
7204ff9c 373 s->cputimer_mode = 0;
e80cfcfc
FB
374}
375
4410b94c 376static void slavio_timer_init(Object *obj)
c70c59ee 377{
4410b94c
XZ
378 SLAVIO_TIMERState *s = SLAVIO_TIMER(obj);
379 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
8d05ea8a 380 QEMUBH *bh;
7204ff9c
BS
381 unsigned int i;
382 TimerContext *tc;
e80cfcfc 383
7204ff9c 384 for (i = 0; i <= MAX_CPUS; i++) {
a3d12d07
BC
385 uint64_t size;
386 char timer_name[20];
387
7267c094 388 tc = g_malloc0(sizeof(TimerContext));
7204ff9c
BS
389 tc->s = s;
390 tc->timer_index = i;
c70c59ee 391
7204ff9c 392 bh = qemu_bh_new(slavio_timer_irq, tc);
e7ea81c3 393 s->cputimer[i].timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
7204ff9c 394 ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
e80cfcfc 395
a3d12d07
BC
396 size = i == 0 ? SYS_TIMER_SIZE : CPU_TIMER_SIZE;
397 snprintf(timer_name, sizeof(timer_name), "timer-%i", i);
4410b94c 398 memory_region_init_io(&tc->iomem, obj, &slavio_timer_mem_ops, tc,
a3d12d07 399 timer_name, size);
750ecd44 400 sysbus_init_mmio(dev, &tc->iomem);
7204ff9c
BS
401
402 sysbus_init_irq(dev, &s->cputimer[i].irq);
c70c59ee 403 }
81732d19
BS
404}
405
999e12bb
AL
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{
39bffca2 413 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 414
39bffca2
AL
415 dc->reset = slavio_timer_reset;
416 dc->vmsd = &vmstate_slavio_timer;
417 dc->props = slavio_timer_properties;
999e12bb
AL
418}
419
8c43a6f0 420static const TypeInfo slavio_timer_info = {
c275471e 421 .name = TYPE_SLAVIO_TIMER,
39bffca2
AL
422 .parent = TYPE_SYS_BUS_DEVICE,
423 .instance_size = sizeof(SLAVIO_TIMERState),
4410b94c 424 .instance_init = slavio_timer_init,
39bffca2 425 .class_init = slavio_timer_class_init,
c70c59ee
BS
426};
427
83f7d43a 428static void slavio_timer_register_types(void)
c70c59ee 429{
39bffca2 430 type_register_static(&slavio_timer_info);
c70c59ee
BS
431}
432
83f7d43a 433type_init(slavio_timer_register_types)