blob: daf6c487977467f0b300d13383a3c100829283e4 [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
Peter Maydell8ef94f02016-01-26 18:17:05 +000022#include "qemu/osdep.h"
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010023#include "hw/ptimer.h"
Andreas Färbereb110bd2013-06-30 20:30:27 +020024#include "hw/timer/arm_mptimer.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010026#include "qemu/main-loop.h"
Andreas Färberde6db412013-06-16 17:10:28 +020027#include "qom/cpu.h"
Peter Maydellb9dc07d2011-12-05 15:47:49 +000028
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010029#define PTIMER_POLICY \
30 (PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD | \
31 PTIMER_POLICY_CONTINUOUS_TRIGGER | \
32 PTIMER_POLICY_NO_IMMEDIATE_TRIGGER | \
33 PTIMER_POLICY_NO_IMMEDIATE_RELOAD | \
34 PTIMER_POLICY_NO_COUNTER_ROUND_DOWN)
35
Peter Maydellb9dc07d2011-12-05 15:47:49 +000036/* This device implements the per-cpu private timer and watchdog block
37 * which is used in both the ARM11MPCore and Cortex-A9MP.
38 */
39
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000040static inline int get_current_cpu(ARMMPTimerState *s)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000041{
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010042 int cpu_id = current_cpu ? current_cpu->cpu_index : 0;
43
44 if (cpu_id >= s->num_cpu) {
Peter Maydellb9dc07d2011-12-05 15:47:49 +000045 hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010046 s->num_cpu, cpu_id);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000047 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010048
49 return cpu_id;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000050}
51
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000052static inline void timerblock_update_irq(TimerBlock *tb)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000053{
Dmitry Osipenko257621a2015-07-06 04:27:12 +030054 qemu_set_irq(tb->irq, tb->status && (tb->control & 4));
Peter Maydellb9dc07d2011-12-05 15:47:49 +000055}
56
57/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010058static inline uint32_t timerblock_scale(uint32_t control)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000059{
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010060 return (((control >> 8) & 0xff) + 1) * 10;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000061}
62
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010063static inline void timerblock_set_count(struct ptimer_state *timer,
64 uint32_t control, uint64_t *count)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000065{
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010066 /* PTimer would trigger interrupt for periodic timer when counter set
67 * to 0, MPtimer under certain condition only.
68 */
69 if ((control & 3) == 3 && (control & 0xff00) == 0 && *count == 0) {
70 *count = ptimer_get_limit(timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000071 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010072 ptimer_set_count(timer, *count);
73}
74
75static inline void timerblock_run(struct ptimer_state *timer,
76 uint32_t control, uint32_t load)
77{
78 if ((control & 1) && ((control & 0xff00) || load != 0)) {
79 ptimer_run(timer, !(control & 2));
Peter Maydellb9dc07d2011-12-05 15:47:49 +000080 }
Peter Maydellb9dc07d2011-12-05 15:47:49 +000081}
82
83static void timerblock_tick(void *opaque)
84{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000085 TimerBlock *tb = (TimerBlock *)opaque;
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010086 /* Periodic timer with load = 0 and prescaler != 0 would re-trigger
87 * IRQ after one period, otherwise it either stops or wraps around.
88 */
89 if ((tb->control & 2) && (tb->control & 0xff00) == 0 &&
90 ptimer_get_limit(tb->timer) == 0) {
91 ptimer_stop(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000092 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010093 tb->status = 1;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000094 timerblock_update_irq(tb);
95}
96
Avi Kivitya8170e52012-10-23 12:30:10 +020097static uint64_t timerblock_read(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +000098 unsigned size)
99{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000100 TimerBlock *tb = (TimerBlock *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000101 switch (addr) {
102 case 0: /* Load */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100103 return ptimer_get_limit(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000104 case 4: /* Counter. */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100105 return ptimer_get_count(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000106 case 8: /* Control. */
107 return tb->control;
108 case 12: /* Interrupt status. */
109 return tb->status;
110 default:
111 return 0;
112 }
113}
114
Avi Kivitya8170e52012-10-23 12:30:10 +0200115static void timerblock_write(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000116 uint64_t value, unsigned size)
117{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000118 TimerBlock *tb = (TimerBlock *)opaque;
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100119 uint32_t control = tb->control;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000120 switch (addr) {
121 case 0: /* Load */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100122 /* Setting load to 0 stops the timer without doing the tick if
123 * prescaler = 0.
124 */
125 if ((control & 1) && (control & 0xff00) == 0 && value == 0) {
126 ptimer_stop(tb->timer);
127 }
128 ptimer_set_limit(tb->timer, value, 1);
129 timerblock_run(tb->timer, control, value);
130 break;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000131 case 4: /* Counter. */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100132 /* Setting counter to 0 stops the one-shot timer, or periodic with
133 * load = 0, without doing the tick if prescaler = 0.
134 */
135 if ((control & 1) && (control & 0xff00) == 0 && value == 0 &&
136 (!(control & 2) || ptimer_get_limit(tb->timer) == 0)) {
137 ptimer_stop(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000138 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100139 timerblock_set_count(tb->timer, control, &value);
140 timerblock_run(tb->timer, control, value);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000141 break;
142 case 8: /* Control. */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100143 if ((control & 3) != (value & 3)) {
144 ptimer_stop(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000145 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100146 if ((control & 0xff00) != (value & 0xff00)) {
147 ptimer_set_period(tb->timer, timerblock_scale(value));
148 }
149 if (value & 1) {
150 uint64_t count = ptimer_get_count(tb->timer);
151 /* Re-load periodic timer counter if needed. */
152 if ((value & 2) && count == 0) {
153 timerblock_set_count(tb->timer, value, &count);
154 }
155 timerblock_run(tb->timer, value, count);
156 }
157 tb->control = value;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000158 break;
159 case 12: /* Interrupt status. */
160 tb->status &= ~value;
161 timerblock_update_irq(tb);
162 break;
163 }
164}
165
166/* Wrapper functions to implement the "read timer/watchdog for
167 * the current CPU" memory regions.
168 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200169static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000170 unsigned size)
171{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000172 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000173 int id = get_current_cpu(s);
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000174 return timerblock_read(&s->timerblock[id], addr, size);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000175}
176
Avi Kivitya8170e52012-10-23 12:30:10 +0200177static void arm_thistimer_write(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000178 uint64_t value, unsigned size)
179{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000180 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000181 int id = get_current_cpu(s);
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000182 timerblock_write(&s->timerblock[id], addr, value, size);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000183}
184
185static const MemoryRegionOps arm_thistimer_ops = {
186 .read = arm_thistimer_read,
187 .write = arm_thistimer_write,
188 .valid = {
189 .min_access_size = 4,
190 .max_access_size = 4,
191 },
192 .endianness = DEVICE_NATIVE_ENDIAN,
193};
194
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000195static const MemoryRegionOps timerblock_ops = {
196 .read = timerblock_read,
197 .write = timerblock_write,
198 .valid = {
199 .min_access_size = 4,
200 .max_access_size = 4,
201 },
202 .endianness = DEVICE_NATIVE_ENDIAN,
203};
204
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000205static void timerblock_reset(TimerBlock *tb)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000206{
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000207 tb->control = 0;
208 tb->status = 0;
Peter Maydellbdac1c12012-04-20 15:38:52 +0000209 if (tb->timer) {
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100210 ptimer_stop(tb->timer);
211 ptimer_set_limit(tb->timer, 0, 1);
212 ptimer_set_period(tb->timer, timerblock_scale(0));
Peter Maydellbdac1c12012-04-20 15:38:52 +0000213 }
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000214}
215
216static void arm_mptimer_reset(DeviceState *dev)
217{
Andreas Färber68653fd2013-06-30 19:37:10 +0200218 ARMMPTimerState *s = ARM_MPTIMER(dev);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000219 int i;
Andreas Färber68653fd2013-06-30 19:37:10 +0200220
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000221 for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
222 timerblock_reset(&s->timerblock[i]);
223 }
224}
225
Andreas Färber0aadb492013-06-30 19:42:55 +0200226static void arm_mptimer_init(Object *obj)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000227{
Andreas Färber0aadb492013-06-30 19:42:55 +0200228 ARMMPTimerState *s = ARM_MPTIMER(obj);
229
230 memory_region_init_io(&s->iomem, obj, &arm_thistimer_ops, s,
231 "arm_mptimer_timer", 0x20);
232 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
233}
234
235static void arm_mptimer_realize(DeviceState *dev, Error **errp)
236{
237 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Andreas Färber68653fd2013-06-30 19:37:10 +0200238 ARMMPTimerState *s = ARM_MPTIMER(dev);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000239 int i;
Andreas Färber68653fd2013-06-30 19:37:10 +0200240
Andreas Färbereb110bd2013-06-30 20:30:27 +0200241 if (s->num_cpu < 1 || s->num_cpu > ARM_MPTIMER_MAX_CPUS) {
Markus Armbrusterb097e482015-12-17 17:35:11 +0100242 error_setg(errp, "num-cpu must be between 1 and %d",
243 ARM_MPTIMER_MAX_CPUS);
244 return;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000245 }
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000246 /* We implement one timer block per CPU, and expose multiple MMIO regions:
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000247 * * region 0 is "timer for this core"
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000248 * * region 1 is "timer for core 0"
249 * * region 2 is "timer for core 1"
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000250 * and so on.
251 * The outgoing interrupt lines are
252 * * timer for core 0
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000253 * * timer for core 1
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000254 * and so on.
255 */
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000256 for (i = 0; i < s->num_cpu; i++) {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000257 TimerBlock *tb = &s->timerblock[i];
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100258 QEMUBH *bh = qemu_bh_new(timerblock_tick, tb);
259 tb->timer = ptimer_init(bh, PTIMER_POLICY);
Andreas Färber0aadb492013-06-30 19:42:55 +0200260 sysbus_init_irq(sbd, &tb->irq);
Paolo Bonzini853dca12013-06-06 21:25:08 -0400261 memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000262 "arm_mptimer_timerblock", 0x20);
Andreas Färber0aadb492013-06-30 19:42:55 +0200263 sysbus_init_mmio(sbd, &tb->iomem);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000264 }
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000265}
266
267static const VMStateDescription vmstate_timerblock = {
268 .name = "arm_mptimer_timerblock",
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100269 .version_id = 3,
270 .minimum_version_id = 3,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000271 .fields = (VMStateField[]) {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000272 VMSTATE_UINT32(control, TimerBlock),
273 VMSTATE_UINT32(status, TimerBlock),
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100274 VMSTATE_PTIMER(timer, TimerBlock),
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000275 VMSTATE_END_OF_LIST()
276 }
277};
278
279static const VMStateDescription vmstate_arm_mptimer = {
280 .name = "arm_mptimer",
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100281 .version_id = 3,
282 .minimum_version_id = 3,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000283 .fields = (VMStateField[]) {
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000284 VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100285 3, vmstate_timerblock, TimerBlock),
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000286 VMSTATE_END_OF_LIST()
287 }
288};
289
Anthony Liguori39bffca2011-12-07 21:34:16 -0600290static Property arm_mptimer_properties[] = {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000291 DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600292 DEFINE_PROP_END_OF_LIST()
293};
294
Anthony Liguori999e12b2012-01-24 13:12:29 -0600295static void arm_mptimer_class_init(ObjectClass *klass, void *data)
296{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600297 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600298
Andreas Färber0aadb492013-06-30 19:42:55 +0200299 dc->realize = arm_mptimer_realize;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600300 dc->vmsd = &vmstate_arm_mptimer;
301 dc->reset = arm_mptimer_reset;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600302 dc->props = arm_mptimer_properties;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600303}
304
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100305static const TypeInfo arm_mptimer_info = {
Andreas Färber68653fd2013-06-30 19:37:10 +0200306 .name = TYPE_ARM_MPTIMER,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600307 .parent = TYPE_SYS_BUS_DEVICE,
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000308 .instance_size = sizeof(ARMMPTimerState),
Andreas Färber0aadb492013-06-30 19:42:55 +0200309 .instance_init = arm_mptimer_init,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600310 .class_init = arm_mptimer_class_init,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000311};
312
Andreas Färber83f7d432012-02-09 15:20:55 +0100313static void arm_mptimer_register_types(void)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000314{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600315 type_register_static(&arm_mptimer_info);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000316}
317
Andreas Färber83f7d432012-02-09 15:20:55 +0100318type_init(arm_mptimer_register_types)