blob: 3e59c2a288a2371fc6b754dbb1b24af2c165ac89 [file] [log] [blame]
Peter Maydellb9dc07d2011-12-05 15:47:49 +00001/*
2 * Private peripheral timer/watchdog blocks for ARM 11MPCore and A9MP
3 *
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Copyright (c) 2011 Linaro Limited
6 * Written by Paul Brook, Peter Maydell
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
Andreas Färbereb110bd2013-06-30 20:30:27 +020022#include "hw/timer/arm_mptimer.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/timer.h"
Andreas Färberde6db412013-06-16 17:10:28 +020024#include "qom/cpu.h"
Peter Maydellb9dc07d2011-12-05 15:47:49 +000025
26/* This device implements the per-cpu private timer and watchdog block
27 * which is used in both the ARM11MPCore and Cortex-A9MP.
28 */
29
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000030static inline int get_current_cpu(ARMMPTimerState *s)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000031{
Andreas Färber4917cf42013-05-27 05:17:50 +020032 if (current_cpu->cpu_index >= s->num_cpu) {
Peter Maydellb9dc07d2011-12-05 15:47:49 +000033 hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
Andreas Färber4917cf42013-05-27 05:17:50 +020034 s->num_cpu, current_cpu->cpu_index);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000035 }
Andreas Färber4917cf42013-05-27 05:17:50 +020036 return current_cpu->cpu_index;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000037}
38
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000039static inline void timerblock_update_irq(TimerBlock *tb)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000040{
Dmitry Osipenko257621a2015-07-06 04:27:12 +030041 qemu_set_irq(tb->irq, tb->status && (tb->control & 4));
Peter Maydellb9dc07d2011-12-05 15:47:49 +000042}
43
44/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000045static inline uint32_t timerblock_scale(TimerBlock *tb)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000046{
47 return (((tb->control >> 8) & 0xff) + 1) * 10;
48}
49
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000050static void timerblock_reload(TimerBlock *tb, int restart)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000051{
52 if (tb->count == 0) {
53 return;
54 }
55 if (restart) {
Alex Blighbc72ad62013-08-21 16:03:08 +010056 tb->tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000057 }
58 tb->tick += (int64_t)tb->count * timerblock_scale(tb);
Alex Blighbc72ad62013-08-21 16:03:08 +010059 timer_mod(tb->timer, tb->tick);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000060}
61
62static void timerblock_tick(void *opaque)
63{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000064 TimerBlock *tb = (TimerBlock *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000065 tb->status = 1;
66 if (tb->control & 2) {
67 tb->count = tb->load;
68 timerblock_reload(tb, 0);
69 } else {
70 tb->count = 0;
71 }
72 timerblock_update_irq(tb);
73}
74
Avi Kivitya8170e52012-10-23 12:30:10 +020075static uint64_t timerblock_read(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +000076 unsigned size)
77{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000078 TimerBlock *tb = (TimerBlock *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000079 int64_t val;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000080 switch (addr) {
81 case 0: /* Load */
82 return tb->load;
83 case 4: /* Counter. */
84 if (((tb->control & 1) == 0) || (tb->count == 0)) {
85 return 0;
86 }
87 /* Slow and ugly, but hopefully won't happen too often. */
Alex Blighbc72ad62013-08-21 16:03:08 +010088 val = tb->tick - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000089 val /= timerblock_scale(tb);
90 if (val < 0) {
91 val = 0;
92 }
93 return val;
94 case 8: /* Control. */
95 return tb->control;
96 case 12: /* Interrupt status. */
97 return tb->status;
98 default:
99 return 0;
100 }
101}
102
Avi Kivitya8170e52012-10-23 12:30:10 +0200103static void timerblock_write(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000104 uint64_t value, unsigned size)
105{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000106 TimerBlock *tb = (TimerBlock *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000107 int64_t old;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000108 switch (addr) {
109 case 0: /* Load */
110 tb->load = value;
111 /* Fall through. */
112 case 4: /* Counter. */
113 if ((tb->control & 1) && tb->count) {
114 /* Cancel the previous timer. */
Alex Blighbc72ad62013-08-21 16:03:08 +0100115 timer_del(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000116 }
117 tb->count = value;
118 if (tb->control & 1) {
119 timerblock_reload(tb, 1);
120 }
121 break;
122 case 8: /* Control. */
123 old = tb->control;
124 tb->control = value;
Dmitry Osipenko8a523402015-07-06 01:47:47 +0300125 if (value & 1) {
126 if ((old & 1) && (tb->count != 0)) {
127 /* Do nothing if timer is ticking right now. */
128 break;
129 }
130 if (tb->control & 2) {
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000131 tb->count = tb->load;
132 }
133 timerblock_reload(tb, 1);
Dmitry Osipenko8a523402015-07-06 01:47:47 +0300134 } else if (old & 1) {
135 /* Shutdown the timer. */
136 timer_del(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000137 }
138 break;
139 case 12: /* Interrupt status. */
140 tb->status &= ~value;
141 timerblock_update_irq(tb);
142 break;
143 }
144}
145
146/* Wrapper functions to implement the "read timer/watchdog for
147 * the current CPU" memory regions.
148 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200149static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000150 unsigned size)
151{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000152 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000153 int id = get_current_cpu(s);
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000154 return timerblock_read(&s->timerblock[id], addr, size);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000155}
156
Avi Kivitya8170e52012-10-23 12:30:10 +0200157static void arm_thistimer_write(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000158 uint64_t value, unsigned size)
159{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000160 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000161 int id = get_current_cpu(s);
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000162 timerblock_write(&s->timerblock[id], addr, value, size);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000163}
164
165static const MemoryRegionOps arm_thistimer_ops = {
166 .read = arm_thistimer_read,
167 .write = arm_thistimer_write,
168 .valid = {
169 .min_access_size = 4,
170 .max_access_size = 4,
171 },
172 .endianness = DEVICE_NATIVE_ENDIAN,
173};
174
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000175static const MemoryRegionOps timerblock_ops = {
176 .read = timerblock_read,
177 .write = timerblock_write,
178 .valid = {
179 .min_access_size = 4,
180 .max_access_size = 4,
181 },
182 .endianness = DEVICE_NATIVE_ENDIAN,
183};
184
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000185static void timerblock_reset(TimerBlock *tb)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000186{
187 tb->count = 0;
188 tb->load = 0;
189 tb->control = 0;
190 tb->status = 0;
191 tb->tick = 0;
Peter Maydellbdac1c12012-04-20 15:38:52 +0000192 if (tb->timer) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100193 timer_del(tb->timer);
Peter Maydellbdac1c12012-04-20 15:38:52 +0000194 }
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000195}
196
197static void arm_mptimer_reset(DeviceState *dev)
198{
Andreas Färber68653fd2013-06-30 19:37:10 +0200199 ARMMPTimerState *s = ARM_MPTIMER(dev);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000200 int i;
Andreas Färber68653fd2013-06-30 19:37:10 +0200201
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000202 for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
203 timerblock_reset(&s->timerblock[i]);
204 }
205}
206
Andreas Färber0aadb492013-06-30 19:42:55 +0200207static void arm_mptimer_init(Object *obj)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000208{
Andreas Färber0aadb492013-06-30 19:42:55 +0200209 ARMMPTimerState *s = ARM_MPTIMER(obj);
210
211 memory_region_init_io(&s->iomem, obj, &arm_thistimer_ops, s,
212 "arm_mptimer_timer", 0x20);
213 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
214}
215
216static void arm_mptimer_realize(DeviceState *dev, Error **errp)
217{
218 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Andreas Färber68653fd2013-06-30 19:37:10 +0200219 ARMMPTimerState *s = ARM_MPTIMER(dev);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000220 int i;
Andreas Färber68653fd2013-06-30 19:37:10 +0200221
Andreas Färbereb110bd2013-06-30 20:30:27 +0200222 if (s->num_cpu < 1 || s->num_cpu > ARM_MPTIMER_MAX_CPUS) {
223 hw_error("%s: num-cpu must be between 1 and %d\n",
224 __func__, ARM_MPTIMER_MAX_CPUS);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000225 }
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000226 /* We implement one timer block per CPU, and expose multiple MMIO regions:
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000227 * * region 0 is "timer for this core"
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000228 * * region 1 is "timer for core 0"
229 * * region 2 is "timer for core 1"
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000230 * and so on.
231 * The outgoing interrupt lines are
232 * * timer for core 0
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000233 * * timer for core 1
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000234 * and so on.
235 */
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000236 for (i = 0; i < s->num_cpu; i++) {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000237 TimerBlock *tb = &s->timerblock[i];
Alex Blighbc72ad62013-08-21 16:03:08 +0100238 tb->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, timerblock_tick, tb);
Andreas Färber0aadb492013-06-30 19:42:55 +0200239 sysbus_init_irq(sbd, &tb->irq);
Paolo Bonzini853dca12013-06-06 21:25:08 -0400240 memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000241 "arm_mptimer_timerblock", 0x20);
Andreas Färber0aadb492013-06-30 19:42:55 +0200242 sysbus_init_mmio(sbd, &tb->iomem);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000243 }
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000244}
245
246static const VMStateDescription vmstate_timerblock = {
247 .name = "arm_mptimer_timerblock",
Peter Maydell28092a22013-04-05 16:17:58 +0100248 .version_id = 2,
249 .minimum_version_id = 2,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000250 .fields = (VMStateField[]) {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000251 VMSTATE_UINT32(count, TimerBlock),
252 VMSTATE_UINT32(load, TimerBlock),
253 VMSTATE_UINT32(control, TimerBlock),
254 VMSTATE_UINT32(status, TimerBlock),
255 VMSTATE_INT64(tick, TimerBlock),
Paolo Bonzinie7206772015-01-08 10:18:59 +0100256 VMSTATE_TIMER_PTR(timer, TimerBlock),
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000257 VMSTATE_END_OF_LIST()
258 }
259};
260
261static const VMStateDescription vmstate_arm_mptimer = {
262 .name = "arm_mptimer",
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000263 .version_id = 2,
264 .minimum_version_id = 2,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000265 .fields = (VMStateField[]) {
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000266 VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
267 2, vmstate_timerblock, TimerBlock),
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000268 VMSTATE_END_OF_LIST()
269 }
270};
271
Anthony Liguori39bffca2011-12-07 21:34:16 -0600272static Property arm_mptimer_properties[] = {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000273 DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600274 DEFINE_PROP_END_OF_LIST()
275};
276
Anthony Liguori999e12b2012-01-24 13:12:29 -0600277static void arm_mptimer_class_init(ObjectClass *klass, void *data)
278{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600279 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600280
Andreas Färber0aadb492013-06-30 19:42:55 +0200281 dc->realize = arm_mptimer_realize;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600282 dc->vmsd = &vmstate_arm_mptimer;
283 dc->reset = arm_mptimer_reset;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600284 dc->props = arm_mptimer_properties;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600285}
286
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100287static const TypeInfo arm_mptimer_info = {
Andreas Färber68653fd2013-06-30 19:37:10 +0200288 .name = TYPE_ARM_MPTIMER,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600289 .parent = TYPE_SYS_BUS_DEVICE,
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000290 .instance_size = sizeof(ARMMPTimerState),
Andreas Färber0aadb492013-06-30 19:42:55 +0200291 .instance_init = arm_mptimer_init,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600292 .class_init = arm_mptimer_class_init,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000293};
294
Andreas Färber83f7d432012-02-09 15:20:55 +0100295static void arm_mptimer_register_types(void)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000296{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600297 type_register_static(&arm_mptimer_info);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000298}
299
Andreas Färber83f7d432012-02-09 15:20:55 +0100300type_init(arm_mptimer_register_types)