]> git.proxmox.com Git - mirror_qemu.git/blame - hw/timer/arm_mptimer.c
qga: Make qemu-ga compile statically for Windows
[mirror_qemu.git] / hw / timer / arm_mptimer.c
CommitLineData
b9dc07d4
PM
1/*
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
8ef94f0b 22#include "qemu/osdep.h"
226fb5aa 23#include "hw/ptimer.h"
eb110bd8 24#include "hw/timer/arm_mptimer.h"
da34e65c 25#include "qapi/error.h"
226fb5aa 26#include "qemu/main-loop.h"
de6db419 27#include "qom/cpu.h"
b9dc07d4 28
226fb5aa
DO
29#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
b9dc07d4
PM
36/* This device implements the per-cpu private timer and watchdog block
37 * which is used in both the ARM11MPCore and Cortex-A9MP.
38 */
39
c6205ddf 40static inline int get_current_cpu(ARMMPTimerState *s)
b9dc07d4 41{
226fb5aa
DO
42 int cpu_id = current_cpu ? current_cpu->cpu_index : 0;
43
44 if (cpu_id >= s->num_cpu) {
b9dc07d4 45 hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
226fb5aa 46 s->num_cpu, cpu_id);
b9dc07d4 47 }
226fb5aa
DO
48
49 return cpu_id;
b9dc07d4
PM
50}
51
c6205ddf 52static inline void timerblock_update_irq(TimerBlock *tb)
b9dc07d4 53{
257621a9 54 qemu_set_irq(tb->irq, tb->status && (tb->control & 4));
b9dc07d4
PM
55}
56
57/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
226fb5aa 58static inline uint32_t timerblock_scale(uint32_t control)
b9dc07d4 59{
226fb5aa 60 return (((control >> 8) & 0xff) + 1) * 10;
b9dc07d4
PM
61}
62
226fb5aa
DO
63static inline void timerblock_set_count(struct ptimer_state *timer,
64 uint32_t control, uint64_t *count)
b9dc07d4 65{
226fb5aa
DO
66 /* 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);
b9dc07d4 71 }
226fb5aa
DO
72 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));
b9dc07d4 80 }
b9dc07d4
PM
81}
82
83static void timerblock_tick(void *opaque)
84{
c6205ddf 85 TimerBlock *tb = (TimerBlock *)opaque;
226fb5aa
DO
86 /* 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);
b9dc07d4 92 }
226fb5aa 93 tb->status = 1;
b9dc07d4
PM
94 timerblock_update_irq(tb);
95}
96
a8170e5e 97static uint64_t timerblock_read(void *opaque, hwaddr addr,
b9dc07d4
PM
98 unsigned size)
99{
c6205ddf 100 TimerBlock *tb = (TimerBlock *)opaque;
b9dc07d4
PM
101 switch (addr) {
102 case 0: /* Load */
226fb5aa 103 return ptimer_get_limit(tb->timer);
b9dc07d4 104 case 4: /* Counter. */
226fb5aa 105 return ptimer_get_count(tb->timer);
b9dc07d4
PM
106 case 8: /* Control. */
107 return tb->control;
108 case 12: /* Interrupt status. */
109 return tb->status;
110 default:
111 return 0;
112 }
113}
114
a8170e5e 115static void timerblock_write(void *opaque, hwaddr addr,
b9dc07d4
PM
116 uint64_t value, unsigned size)
117{
c6205ddf 118 TimerBlock *tb = (TimerBlock *)opaque;
226fb5aa 119 uint32_t control = tb->control;
b9dc07d4
PM
120 switch (addr) {
121 case 0: /* Load */
226fb5aa
DO
122 /* 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);
b9dc07d4 127 }
226fb5aa
DO
128 ptimer_set_limit(tb->timer, value, 1);
129 timerblock_run(tb->timer, control, value);
130 break;
131 case 4: /* Counter. */
132 /* 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);
b9dc07d4 138 }
226fb5aa
DO
139 timerblock_set_count(tb->timer, control, &value);
140 timerblock_run(tb->timer, control, value);
b9dc07d4
PM
141 break;
142 case 8: /* Control. */
226fb5aa
DO
143 if ((control & 3) != (value & 3)) {
144 ptimer_stop(tb->timer);
145 }
146 if ((control & 0xff00) != (value & 0xff00)) {
147 ptimer_set_period(tb->timer, timerblock_scale(value));
148 }
8a52340c 149 if (value & 1) {
226fb5aa
DO
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);
8a52340c 154 }
226fb5aa 155 timerblock_run(tb->timer, value, count);
b9dc07d4 156 }
226fb5aa 157 tb->control = value;
b9dc07d4
PM
158 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 */
a8170e5e 169static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
b9dc07d4
PM
170 unsigned size)
171{
c6205ddf 172 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
b9dc07d4 173 int id = get_current_cpu(s);
cde4577f 174 return timerblock_read(&s->timerblock[id], addr, size);
b9dc07d4
PM
175}
176
a8170e5e 177static void arm_thistimer_write(void *opaque, hwaddr addr,
b9dc07d4
PM
178 uint64_t value, unsigned size)
179{
c6205ddf 180 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
b9dc07d4 181 int id = get_current_cpu(s);
cde4577f 182 timerblock_write(&s->timerblock[id], addr, value, size);
b9dc07d4
PM
183}
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
b9dc07d4
PM
195static 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
c6205ddf 205static void timerblock_reset(TimerBlock *tb)
b9dc07d4 206{
b9dc07d4
PM
207 tb->control = 0;
208 tb->status = 0;
bdac1c1e 209 if (tb->timer) {
226fb5aa
DO
210 ptimer_stop(tb->timer);
211 ptimer_set_limit(tb->timer, 0, 1);
212 ptimer_set_period(tb->timer, timerblock_scale(0));
bdac1c1e 213 }
b9dc07d4
PM
214}
215
216static void arm_mptimer_reset(DeviceState *dev)
217{
68653fd6 218 ARMMPTimerState *s = ARM_MPTIMER(dev);
b9dc07d4 219 int i;
68653fd6 220
b9dc07d4
PM
221 for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
222 timerblock_reset(&s->timerblock[i]);
223 }
224}
225
0aadb490 226static void arm_mptimer_init(Object *obj)
b9dc07d4 227{
0aadb490
AF
228 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);
68653fd6 238 ARMMPTimerState *s = ARM_MPTIMER(dev);
b9dc07d4 239 int i;
68653fd6 240
eb110bd8 241 if (s->num_cpu < 1 || s->num_cpu > ARM_MPTIMER_MAX_CPUS) {
b097e481
MA
242 error_setg(errp, "num-cpu must be between 1 and %d",
243 ARM_MPTIMER_MAX_CPUS);
244 return;
b9dc07d4 245 }
cde4577f 246 /* We implement one timer block per CPU, and expose multiple MMIO regions:
b9dc07d4 247 * * region 0 is "timer for this core"
cde4577f
PC
248 * * region 1 is "timer for core 0"
249 * * region 2 is "timer for core 1"
b9dc07d4
PM
250 * and so on.
251 * The outgoing interrupt lines are
252 * * timer for core 0
b9dc07d4 253 * * timer for core 1
b9dc07d4
PM
254 * and so on.
255 */
cde4577f 256 for (i = 0; i < s->num_cpu; i++) {
c6205ddf 257 TimerBlock *tb = &s->timerblock[i];
226fb5aa
DO
258 QEMUBH *bh = qemu_bh_new(timerblock_tick, tb);
259 tb->timer = ptimer_init(bh, PTIMER_POLICY);
0aadb490 260 sysbus_init_irq(sbd, &tb->irq);
853dca12 261 memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb,
b9dc07d4 262 "arm_mptimer_timerblock", 0x20);
0aadb490 263 sysbus_init_mmio(sbd, &tb->iomem);
b9dc07d4 264 }
b9dc07d4
PM
265}
266
267static const VMStateDescription vmstate_timerblock = {
268 .name = "arm_mptimer_timerblock",
226fb5aa
DO
269 .version_id = 3,
270 .minimum_version_id = 3,
b9dc07d4 271 .fields = (VMStateField[]) {
c6205ddf
PC
272 VMSTATE_UINT32(control, TimerBlock),
273 VMSTATE_UINT32(status, TimerBlock),
226fb5aa 274 VMSTATE_PTIMER(timer, TimerBlock),
b9dc07d4
PM
275 VMSTATE_END_OF_LIST()
276 }
277};
278
279static const VMStateDescription vmstate_arm_mptimer = {
280 .name = "arm_mptimer",
226fb5aa
DO
281 .version_id = 3,
282 .minimum_version_id = 3,
b9dc07d4 283 .fields = (VMStateField[]) {
cde4577f 284 VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
226fb5aa 285 3, vmstate_timerblock, TimerBlock),
b9dc07d4
PM
286 VMSTATE_END_OF_LIST()
287 }
288};
289
39bffca2 290static Property arm_mptimer_properties[] = {
c6205ddf 291 DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
39bffca2
AL
292 DEFINE_PROP_END_OF_LIST()
293};
294
999e12bb
AL
295static void arm_mptimer_class_init(ObjectClass *klass, void *data)
296{
39bffca2 297 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 298
0aadb490 299 dc->realize = arm_mptimer_realize;
39bffca2
AL
300 dc->vmsd = &vmstate_arm_mptimer;
301 dc->reset = arm_mptimer_reset;
39bffca2 302 dc->props = arm_mptimer_properties;
999e12bb
AL
303}
304
8c43a6f0 305static const TypeInfo arm_mptimer_info = {
68653fd6 306 .name = TYPE_ARM_MPTIMER,
39bffca2 307 .parent = TYPE_SYS_BUS_DEVICE,
c6205ddf 308 .instance_size = sizeof(ARMMPTimerState),
0aadb490 309 .instance_init = arm_mptimer_init,
39bffca2 310 .class_init = arm_mptimer_class_init,
b9dc07d4
PM
311};
312
83f7d43a 313static void arm_mptimer_register_types(void)
b9dc07d4 314{
39bffca2 315 type_register_static(&arm_mptimer_info);
b9dc07d4
PM
316}
317
83f7d43a 318type_init(arm_mptimer_register_types)