blob: bca4cee0e4ee1e4be61a8694cce73913de0c39ec [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"
Markus Armbruster650d1032019-08-12 07:23:48 +020023#include "hw/hw.h"
Markus Armbruster64552b62019-08-12 07:23:42 +020024#include "hw/irq.h"
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010025#include "hw/ptimer.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020026#include "hw/qdev-properties.h"
Andreas Färbereb110bd2013-06-30 20:30:27 +020027#include "hw/timer/arm_mptimer.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020028#include "migration/vmstate.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010029#include "qapi/error.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020030#include "qemu/module.h"
Markus Armbruster2e5b09f2019-07-09 17:20:52 +020031#include "hw/core/cpu.h"
Peter Maydellb9dc07d2011-12-05 15:47:49 +000032
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010033#define PTIMER_POLICY \
34 (PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD | \
35 PTIMER_POLICY_CONTINUOUS_TRIGGER | \
36 PTIMER_POLICY_NO_IMMEDIATE_TRIGGER | \
37 PTIMER_POLICY_NO_IMMEDIATE_RELOAD | \
38 PTIMER_POLICY_NO_COUNTER_ROUND_DOWN)
39
Peter Maydellb9dc07d2011-12-05 15:47:49 +000040/* This device implements the per-cpu private timer and watchdog block
41 * which is used in both the ARM11MPCore and Cortex-A9MP.
42 */
43
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000044static inline int get_current_cpu(ARMMPTimerState *s)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000045{
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010046 int cpu_id = current_cpu ? current_cpu->cpu_index : 0;
47
48 if (cpu_id >= s->num_cpu) {
Peter Maydellb9dc07d2011-12-05 15:47:49 +000049 hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010050 s->num_cpu, cpu_id);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000051 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010052
53 return cpu_id;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000054}
55
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000056static inline void timerblock_update_irq(TimerBlock *tb)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000057{
Dmitry Osipenko257621a2015-07-06 04:27:12 +030058 qemu_set_irq(tb->irq, tb->status && (tb->control & 4));
Peter Maydellb9dc07d2011-12-05 15:47:49 +000059}
60
61/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010062static inline uint32_t timerblock_scale(uint32_t control)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000063{
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010064 return (((control >> 8) & 0xff) + 1) * 10;
Peter Maydellb9dc07d2011-12-05 15:47:49 +000065}
66
Peter Maydell581b0882019-10-08 18:17:26 +010067/* Must be called within a ptimer transaction block */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010068static inline void timerblock_set_count(struct ptimer_state *timer,
69 uint32_t control, uint64_t *count)
Peter Maydellb9dc07d2011-12-05 15:47:49 +000070{
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010071 /* PTimer would trigger interrupt for periodic timer when counter set
72 * to 0, MPtimer under certain condition only.
73 */
74 if ((control & 3) == 3 && (control & 0xff00) == 0 && *count == 0) {
75 *count = ptimer_get_limit(timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000076 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010077 ptimer_set_count(timer, *count);
78}
79
Peter Maydell581b0882019-10-08 18:17:26 +010080/* Must be called within a ptimer transaction block */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010081static inline void timerblock_run(struct ptimer_state *timer,
82 uint32_t control, uint32_t load)
83{
84 if ((control & 1) && ((control & 0xff00) || load != 0)) {
85 ptimer_run(timer, !(control & 2));
Peter Maydellb9dc07d2011-12-05 15:47:49 +000086 }
Peter Maydellb9dc07d2011-12-05 15:47:49 +000087}
88
89static void timerblock_tick(void *opaque)
90{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +000091 TimerBlock *tb = (TimerBlock *)opaque;
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010092 /* Periodic timer with load = 0 and prescaler != 0 would re-trigger
93 * IRQ after one period, otherwise it either stops or wraps around.
94 */
95 if ((tb->control & 2) && (tb->control & 0xff00) == 0 &&
96 ptimer_get_limit(tb->timer) == 0) {
97 ptimer_stop(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +000098 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +010099 tb->status = 1;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000100 timerblock_update_irq(tb);
101}
102
Avi Kivitya8170e52012-10-23 12:30:10 +0200103static uint64_t timerblock_read(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000104 unsigned size)
105{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000106 TimerBlock *tb = (TimerBlock *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000107 switch (addr) {
108 case 0: /* Load */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100109 return ptimer_get_limit(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000110 case 4: /* Counter. */
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100111 return ptimer_get_count(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000112 case 8: /* Control. */
113 return tb->control;
114 case 12: /* Interrupt status. */
115 return tb->status;
116 default:
117 return 0;
118 }
119}
120
Avi Kivitya8170e52012-10-23 12:30:10 +0200121static void timerblock_write(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000122 uint64_t value, unsigned size)
123{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000124 TimerBlock *tb = (TimerBlock *)opaque;
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100125 uint32_t control = tb->control;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000126 switch (addr) {
127 case 0: /* Load */
Peter Maydell581b0882019-10-08 18:17:26 +0100128 ptimer_transaction_begin(tb->timer);
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100129 /* Setting load to 0 stops the timer without doing the tick if
130 * prescaler = 0.
131 */
132 if ((control & 1) && (control & 0xff00) == 0 && value == 0) {
133 ptimer_stop(tb->timer);
134 }
135 ptimer_set_limit(tb->timer, value, 1);
136 timerblock_run(tb->timer, control, value);
Peter Maydell581b0882019-10-08 18:17:26 +0100137 ptimer_transaction_commit(tb->timer);
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100138 break;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000139 case 4: /* Counter. */
Peter Maydell581b0882019-10-08 18:17:26 +0100140 ptimer_transaction_begin(tb->timer);
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100141 /* Setting counter to 0 stops the one-shot timer, or periodic with
142 * load = 0, without doing the tick if prescaler = 0.
143 */
144 if ((control & 1) && (control & 0xff00) == 0 && value == 0 &&
145 (!(control & 2) || ptimer_get_limit(tb->timer) == 0)) {
146 ptimer_stop(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000147 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100148 timerblock_set_count(tb->timer, control, &value);
149 timerblock_run(tb->timer, control, value);
Peter Maydell581b0882019-10-08 18:17:26 +0100150 ptimer_transaction_commit(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000151 break;
152 case 8: /* Control. */
Peter Maydell581b0882019-10-08 18:17:26 +0100153 ptimer_transaction_begin(tb->timer);
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100154 if ((control & 3) != (value & 3)) {
155 ptimer_stop(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000156 }
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100157 if ((control & 0xff00) != (value & 0xff00)) {
158 ptimer_set_period(tb->timer, timerblock_scale(value));
159 }
160 if (value & 1) {
161 uint64_t count = ptimer_get_count(tb->timer);
162 /* Re-load periodic timer counter if needed. */
163 if ((value & 2) && count == 0) {
164 timerblock_set_count(tb->timer, value, &count);
165 }
166 timerblock_run(tb->timer, value, count);
167 }
168 tb->control = value;
Peter Maydell581b0882019-10-08 18:17:26 +0100169 ptimer_transaction_commit(tb->timer);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000170 break;
171 case 12: /* Interrupt status. */
172 tb->status &= ~value;
173 timerblock_update_irq(tb);
174 break;
175 }
176}
177
178/* Wrapper functions to implement the "read timer/watchdog for
179 * the current CPU" memory regions.
180 */
Avi Kivitya8170e52012-10-23 12:30:10 +0200181static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000182 unsigned size)
183{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000184 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000185 int id = get_current_cpu(s);
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000186 return timerblock_read(&s->timerblock[id], addr, size);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000187}
188
Avi Kivitya8170e52012-10-23 12:30:10 +0200189static void arm_thistimer_write(void *opaque, hwaddr addr,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000190 uint64_t value, unsigned size)
191{
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000192 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000193 int id = get_current_cpu(s);
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000194 timerblock_write(&s->timerblock[id], addr, value, size);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000195}
196
197static const MemoryRegionOps arm_thistimer_ops = {
198 .read = arm_thistimer_read,
199 .write = arm_thistimer_write,
200 .valid = {
201 .min_access_size = 4,
202 .max_access_size = 4,
203 },
204 .endianness = DEVICE_NATIVE_ENDIAN,
205};
206
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000207static const MemoryRegionOps timerblock_ops = {
208 .read = timerblock_read,
209 .write = timerblock_write,
210 .valid = {
211 .min_access_size = 4,
212 .max_access_size = 4,
213 },
214 .endianness = DEVICE_NATIVE_ENDIAN,
215};
216
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000217static void timerblock_reset(TimerBlock *tb)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000218{
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000219 tb->control = 0;
220 tb->status = 0;
Peter Maydellbdac1c12012-04-20 15:38:52 +0000221 if (tb->timer) {
Peter Maydell581b0882019-10-08 18:17:26 +0100222 ptimer_transaction_begin(tb->timer);
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100223 ptimer_stop(tb->timer);
224 ptimer_set_limit(tb->timer, 0, 1);
225 ptimer_set_period(tb->timer, timerblock_scale(0));
Peter Maydell581b0882019-10-08 18:17:26 +0100226 ptimer_transaction_commit(tb->timer);
Peter Maydellbdac1c12012-04-20 15:38:52 +0000227 }
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000228}
229
230static void arm_mptimer_reset(DeviceState *dev)
231{
Andreas Färber68653fd2013-06-30 19:37:10 +0200232 ARMMPTimerState *s = ARM_MPTIMER(dev);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000233 int i;
Andreas Färber68653fd2013-06-30 19:37:10 +0200234
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000235 for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
236 timerblock_reset(&s->timerblock[i]);
237 }
238}
239
Peter Maydella1f9a902019-10-22 16:50:35 +0100240static void arm_mptimer_init(Object *obj)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000241{
Andreas Färber0aadb492013-06-30 19:42:55 +0200242 ARMMPTimerState *s = ARM_MPTIMER(obj);
243
244 memory_region_init_io(&s->iomem, obj, &arm_thistimer_ops, s,
245 "arm_mptimer_timer", 0x20);
246 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
247}
248
249static void arm_mptimer_realize(DeviceState *dev, Error **errp)
250{
251 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Andreas Färber68653fd2013-06-30 19:37:10 +0200252 ARMMPTimerState *s = ARM_MPTIMER(dev);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000253 int i;
Andreas Färber68653fd2013-06-30 19:37:10 +0200254
Andreas Färbereb110bd2013-06-30 20:30:27 +0200255 if (s->num_cpu < 1 || s->num_cpu > ARM_MPTIMER_MAX_CPUS) {
Markus Armbrusterb097e482015-12-17 17:35:11 +0100256 error_setg(errp, "num-cpu must be between 1 and %d",
257 ARM_MPTIMER_MAX_CPUS);
258 return;
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000259 }
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000260 /* We implement one timer block per CPU, and expose multiple MMIO regions:
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000261 * * region 0 is "timer for this core"
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000262 * * region 1 is "timer for core 0"
263 * * region 2 is "timer for core 1"
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000264 * and so on.
265 * The outgoing interrupt lines are
266 * * timer for core 0
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000267 * * timer for core 1
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000268 * and so on.
269 */
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000270 for (i = 0; i < s->num_cpu; i++) {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000271 TimerBlock *tb = &s->timerblock[i];
Peter Maydell581b0882019-10-08 18:17:26 +0100272 tb->timer = ptimer_init(timerblock_tick, tb, PTIMER_POLICY);
Andreas Färber0aadb492013-06-30 19:42:55 +0200273 sysbus_init_irq(sbd, &tb->irq);
Paolo Bonzini853dca12013-06-06 21:25:08 -0400274 memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000275 "arm_mptimer_timerblock", 0x20);
Andreas Färber0aadb492013-06-30 19:42:55 +0200276 sysbus_init_mmio(sbd, &tb->iomem);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000277 }
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000278}
279
280static const VMStateDescription vmstate_timerblock = {
281 .name = "arm_mptimer_timerblock",
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100282 .version_id = 3,
283 .minimum_version_id = 3,
Richard Hendersonba324b32023-12-21 14:16:37 +1100284 .fields = (const VMStateField[]) {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000285 VMSTATE_UINT32(control, TimerBlock),
286 VMSTATE_UINT32(status, TimerBlock),
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100287 VMSTATE_PTIMER(timer, TimerBlock),
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000288 VMSTATE_END_OF_LIST()
289 }
290};
291
292static const VMStateDescription vmstate_arm_mptimer = {
293 .name = "arm_mptimer",
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100294 .version_id = 3,
295 .minimum_version_id = 3,
Richard Hendersonba324b32023-12-21 14:16:37 +1100296 .fields = (const VMStateField[]) {
Peter Crosthwaitecde45772013-02-28 18:23:13 +0000297 VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
Dmitry Osipenko226fb5a2016-10-24 16:26:53 +0100298 3, vmstate_timerblock, TimerBlock),
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000299 VMSTATE_END_OF_LIST()
300 }
301};
302
Anthony Liguori39bffca2011-12-07 21:34:16 -0600303static Property arm_mptimer_properties[] = {
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000304 DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600305 DEFINE_PROP_END_OF_LIST()
306};
307
Anthony Liguori999e12b2012-01-24 13:12:29 -0600308static void arm_mptimer_class_init(ObjectClass *klass, void *data)
309{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600310 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600311
Andreas Färber0aadb492013-06-30 19:42:55 +0200312 dc->realize = arm_mptimer_realize;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600313 dc->vmsd = &vmstate_arm_mptimer;
314 dc->reset = arm_mptimer_reset;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400315 device_class_set_props(dc, arm_mptimer_properties);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600316}
317
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100318static const TypeInfo arm_mptimer_info = {
Andreas Färber68653fd2013-06-30 19:37:10 +0200319 .name = TYPE_ARM_MPTIMER,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600320 .parent = TYPE_SYS_BUS_DEVICE,
Peter Crosthwaitec6205dd2013-02-28 18:23:13 +0000321 .instance_size = sizeof(ARMMPTimerState),
Peter Maydella1f9a902019-10-22 16:50:35 +0100322 .instance_init = arm_mptimer_init,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600323 .class_init = arm_mptimer_class_init,
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000324};
325
Andreas Färber83f7d432012-02-09 15:20:55 +0100326static void arm_mptimer_register_types(void)
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000327{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600328 type_register_static(&arm_mptimer_info);
Peter Maydellb9dc07d2011-12-05 15:47:49 +0000329}
330
Andreas Färber83f7d432012-02-09 15:20:55 +0100331type_init(arm_mptimer_register_types)