]> git.proxmox.com Git - qemu.git/blame - hw/arm_mptimer.c
cpu: Move halted and interrupt_request fields to CPUState
[qemu.git] / hw / 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
83c9f4ca 22#include "hw/sysbus.h"
1de7afc9 23#include "qemu/timer.h"
b9dc07d4
PM
24
25/* This device implements the per-cpu private timer and watchdog block
26 * which is used in both the ARM11MPCore and Cortex-A9MP.
27 */
28
29#define MAX_CPUS 4
30
31/* State of a single timer or watchdog block */
32typedef struct {
33 uint32_t count;
34 uint32_t load;
35 uint32_t control;
36 uint32_t status;
37 int64_t tick;
38 QEMUTimer *timer;
39 qemu_irq irq;
40 MemoryRegion iomem;
c6205ddf 41} TimerBlock;
b9dc07d4
PM
42
43typedef struct {
44 SysBusDevice busdev;
45 uint32_t num_cpu;
cde4577f
PC
46 TimerBlock timerblock[MAX_CPUS];
47 MemoryRegion iomem;
c6205ddf 48} ARMMPTimerState;
b9dc07d4 49
c6205ddf 50static inline int get_current_cpu(ARMMPTimerState *s)
b9dc07d4 51{
55e5c285
AF
52 CPUState *cpu_single_cpu = ENV_GET_CPU(cpu_single_env);
53
54 if (cpu_single_cpu->cpu_index >= s->num_cpu) {
b9dc07d4 55 hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
55e5c285 56 s->num_cpu, cpu_single_cpu->cpu_index);
b9dc07d4 57 }
55e5c285 58 return cpu_single_cpu->cpu_index;
b9dc07d4
PM
59}
60
c6205ddf 61static inline void timerblock_update_irq(TimerBlock *tb)
b9dc07d4
PM
62{
63 qemu_set_irq(tb->irq, tb->status);
64}
65
66/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
c6205ddf 67static inline uint32_t timerblock_scale(TimerBlock *tb)
b9dc07d4
PM
68{
69 return (((tb->control >> 8) & 0xff) + 1) * 10;
70}
71
c6205ddf 72static void timerblock_reload(TimerBlock *tb, int restart)
b9dc07d4
PM
73{
74 if (tb->count == 0) {
75 return;
76 }
77 if (restart) {
78 tb->tick = qemu_get_clock_ns(vm_clock);
79 }
80 tb->tick += (int64_t)tb->count * timerblock_scale(tb);
81 qemu_mod_timer(tb->timer, tb->tick);
82}
83
84static void timerblock_tick(void *opaque)
85{
c6205ddf 86 TimerBlock *tb = (TimerBlock *)opaque;
b9dc07d4
PM
87 tb->status = 1;
88 if (tb->control & 2) {
89 tb->count = tb->load;
90 timerblock_reload(tb, 0);
91 } else {
92 tb->count = 0;
93 }
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 101 int64_t val;
b9dc07d4
PM
102 switch (addr) {
103 case 0: /* Load */
104 return tb->load;
105 case 4: /* Counter. */
106 if (((tb->control & 1) == 0) || (tb->count == 0)) {
107 return 0;
108 }
109 /* Slow and ugly, but hopefully won't happen too often. */
110 val = tb->tick - qemu_get_clock_ns(vm_clock);
111 val /= timerblock_scale(tb);
112 if (val < 0) {
113 val = 0;
114 }
115 return val;
116 case 8: /* Control. */
117 return tb->control;
118 case 12: /* Interrupt status. */
119 return tb->status;
120 default:
121 return 0;
122 }
123}
124
a8170e5e 125static void timerblock_write(void *opaque, hwaddr addr,
b9dc07d4
PM
126 uint64_t value, unsigned size)
127{
c6205ddf 128 TimerBlock *tb = (TimerBlock *)opaque;
b9dc07d4 129 int64_t old;
b9dc07d4
PM
130 switch (addr) {
131 case 0: /* Load */
132 tb->load = value;
133 /* Fall through. */
134 case 4: /* Counter. */
135 if ((tb->control & 1) && tb->count) {
136 /* Cancel the previous timer. */
137 qemu_del_timer(tb->timer);
138 }
139 tb->count = value;
140 if (tb->control & 1) {
141 timerblock_reload(tb, 1);
142 }
143 break;
144 case 8: /* Control. */
145 old = tb->control;
146 tb->control = value;
147 if (((old & 1) == 0) && (value & 1)) {
148 if (tb->count == 0 && (tb->control & 2)) {
149 tb->count = tb->load;
150 }
151 timerblock_reload(tb, 1);
152 }
153 break;
154 case 12: /* Interrupt status. */
155 tb->status &= ~value;
156 timerblock_update_irq(tb);
157 break;
158 }
159}
160
161/* Wrapper functions to implement the "read timer/watchdog for
162 * the current CPU" memory regions.
163 */
a8170e5e 164static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
b9dc07d4
PM
165 unsigned size)
166{
c6205ddf 167 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
b9dc07d4 168 int id = get_current_cpu(s);
cde4577f 169 return timerblock_read(&s->timerblock[id], addr, size);
b9dc07d4
PM
170}
171
a8170e5e 172static void arm_thistimer_write(void *opaque, hwaddr addr,
b9dc07d4
PM
173 uint64_t value, unsigned size)
174{
c6205ddf 175 ARMMPTimerState *s = (ARMMPTimerState *)opaque;
b9dc07d4 176 int id = get_current_cpu(s);
cde4577f 177 timerblock_write(&s->timerblock[id], addr, value, size);
b9dc07d4
PM
178}
179
180static const MemoryRegionOps arm_thistimer_ops = {
181 .read = arm_thistimer_read,
182 .write = arm_thistimer_write,
183 .valid = {
184 .min_access_size = 4,
185 .max_access_size = 4,
186 },
187 .endianness = DEVICE_NATIVE_ENDIAN,
188};
189
b9dc07d4
PM
190static const MemoryRegionOps timerblock_ops = {
191 .read = timerblock_read,
192 .write = timerblock_write,
193 .valid = {
194 .min_access_size = 4,
195 .max_access_size = 4,
196 },
197 .endianness = DEVICE_NATIVE_ENDIAN,
198};
199
c6205ddf 200static void timerblock_reset(TimerBlock *tb)
b9dc07d4
PM
201{
202 tb->count = 0;
203 tb->load = 0;
204 tb->control = 0;
205 tb->status = 0;
206 tb->tick = 0;
bdac1c1e
PM
207 if (tb->timer) {
208 qemu_del_timer(tb->timer);
209 }
b9dc07d4
PM
210}
211
212static void arm_mptimer_reset(DeviceState *dev)
213{
c6205ddf
PC
214 ARMMPTimerState *s =
215 FROM_SYSBUS(ARMMPTimerState, SYS_BUS_DEVICE(dev));
b9dc07d4 216 int i;
b9dc07d4
PM
217 for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
218 timerblock_reset(&s->timerblock[i]);
219 }
220}
221
222static int arm_mptimer_init(SysBusDevice *dev)
223{
c6205ddf 224 ARMMPTimerState *s = FROM_SYSBUS(ARMMPTimerState, dev);
b9dc07d4
PM
225 int i;
226 if (s->num_cpu < 1 || s->num_cpu > MAX_CPUS) {
227 hw_error("%s: num-cpu must be between 1 and %d\n", __func__, MAX_CPUS);
228 }
cde4577f 229 /* We implement one timer block per CPU, and expose multiple MMIO regions:
b9dc07d4 230 * * region 0 is "timer for this core"
cde4577f
PC
231 * * region 1 is "timer for core 0"
232 * * region 2 is "timer for core 1"
b9dc07d4
PM
233 * and so on.
234 * The outgoing interrupt lines are
235 * * timer for core 0
b9dc07d4 236 * * timer for core 1
b9dc07d4
PM
237 * and so on.
238 */
cde4577f 239 memory_region_init_io(&s->iomem, &arm_thistimer_ops, s,
b9dc07d4 240 "arm_mptimer_timer", 0x20);
cde4577f
PC
241 sysbus_init_mmio(dev, &s->iomem);
242 for (i = 0; i < s->num_cpu; i++) {
c6205ddf 243 TimerBlock *tb = &s->timerblock[i];
b9dc07d4
PM
244 tb->timer = qemu_new_timer_ns(vm_clock, timerblock_tick, tb);
245 sysbus_init_irq(dev, &tb->irq);
246 memory_region_init_io(&tb->iomem, &timerblock_ops, tb,
247 "arm_mptimer_timerblock", 0x20);
248 sysbus_init_mmio(dev, &tb->iomem);
249 }
250
251 return 0;
252}
253
254static const VMStateDescription vmstate_timerblock = {
255 .name = "arm_mptimer_timerblock",
256 .version_id = 1,
257 .minimum_version_id = 1,
258 .fields = (VMStateField[]) {
c6205ddf
PC
259 VMSTATE_UINT32(count, TimerBlock),
260 VMSTATE_UINT32(load, TimerBlock),
261 VMSTATE_UINT32(control, TimerBlock),
262 VMSTATE_UINT32(status, TimerBlock),
263 VMSTATE_INT64(tick, TimerBlock),
b9dc07d4
PM
264 VMSTATE_END_OF_LIST()
265 }
266};
267
268static const VMStateDescription vmstate_arm_mptimer = {
269 .name = "arm_mptimer",
cde4577f
PC
270 .version_id = 2,
271 .minimum_version_id = 2,
b9dc07d4 272 .fields = (VMStateField[]) {
cde4577f
PC
273 VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
274 2, vmstate_timerblock, TimerBlock),
b9dc07d4
PM
275 VMSTATE_END_OF_LIST()
276 }
277};
278
39bffca2 279static Property arm_mptimer_properties[] = {
c6205ddf 280 DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
39bffca2
AL
281 DEFINE_PROP_END_OF_LIST()
282};
283
999e12bb
AL
284static void arm_mptimer_class_init(ObjectClass *klass, void *data)
285{
39bffca2 286 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
287 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
288
289 sbc->init = arm_mptimer_init;
39bffca2
AL
290 dc->vmsd = &vmstate_arm_mptimer;
291 dc->reset = arm_mptimer_reset;
292 dc->no_user = 1;
293 dc->props = arm_mptimer_properties;
999e12bb
AL
294}
295
8c43a6f0 296static const TypeInfo arm_mptimer_info = {
39bffca2
AL
297 .name = "arm_mptimer",
298 .parent = TYPE_SYS_BUS_DEVICE,
c6205ddf 299 .instance_size = sizeof(ARMMPTimerState),
39bffca2 300 .class_init = arm_mptimer_class_init,
b9dc07d4
PM
301};
302
83f7d43a 303static void arm_mptimer_register_types(void)
b9dc07d4 304{
39bffca2 305 type_register_static(&arm_mptimer_info);
b9dc07d4
PM
306}
307
83f7d43a 308type_init(arm_mptimer_register_types)