]> Git Repo - qemu.git/blame - hw/timer/pl031.c
hw/timer: Compile devices not target-dependent as common object
[qemu.git] / hw / timer / pl031.c
CommitLineData
7e1543c2
PB
1/*
2 * ARM AMBA PrimeCell PL031 RTC
3 *
4 * Copyright (c) 2007 CodeSourcery
5 *
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
6b620ca3
PB
10 * Contributions after 2012-01-13 are licensed under the terms of the
11 * GNU GPL, version 2 or (at your option) any later version.
7e1543c2
PB
12 */
13
8ef94f0b 14#include "qemu/osdep.h"
a8d25326 15#include "qemu-common.h"
b0de99f3 16#include "hw/timer/pl031.h"
d6454270 17#include "migration/vmstate.h"
64552b6b 18#include "hw/irq.h"
a27bd6c7 19#include "hw/qdev-properties.h"
83c9f4ca 20#include "hw/sysbus.h"
1de7afc9 21#include "qemu/timer.h"
9c17d615 22#include "sysemu/sysemu.h"
f348b6d1 23#include "qemu/cutils.h"
03dd024f 24#include "qemu/log.h"
0b8fa32f 25#include "qemu/module.h"
dd849ef2 26#include "trace.h"
7e1543c2
PB
27
28#define RTC_DR 0x00 /* Data read register */
29#define RTC_MR 0x04 /* Match register */
30#define RTC_LR 0x08 /* Data load register */
31#define RTC_CR 0x0c /* Control register */
32#define RTC_IMSC 0x10 /* Interrupt mask and set register */
33#define RTC_RIS 0x14 /* Raw interrupt status register */
34#define RTC_MIS 0x18 /* Masked interrupt status register */
35#define RTC_ICR 0x1c /* Interrupt clear register */
36
7e1543c2
PB
37static const unsigned char pl031_id[] = {
38 0x31, 0x10, 0x14, 0x00, /* Device ID */
39 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
40};
41
b91f0dfd 42static void pl031_update(PL031State *s)
7e1543c2 43{
dd849ef2
PM
44 uint32_t flags = s->is & s->im;
45
46 trace_pl031_irq_state(flags);
47 qemu_set_irq(s->irq, flags);
7e1543c2
PB
48}
49
50static void pl031_interrupt(void * opaque)
51{
b91f0dfd 52 PL031State *s = (PL031State *)opaque;
7e1543c2 53
13a16f1d 54 s->is = 1;
dd849ef2 55 trace_pl031_alarm_raised();
7e1543c2
PB
56 pl031_update(s);
57}
58
b91f0dfd 59static uint32_t pl031_get_count(PL031State *s)
7e1543c2 60{
884f17c2 61 int64_t now = qemu_clock_get_ns(rtc_clock);
73bcb24d 62 return s->tick_offset + now / NANOSECONDS_PER_SECOND;
7e1543c2
PB
63}
64
b91f0dfd 65static void pl031_set_alarm(PL031State *s)
7e1543c2 66{
7e1543c2
PB
67 uint32_t ticks;
68
7e1543c2
PB
69 /* The timer wraps around. This subtraction also wraps in the same way,
70 and gives correct results when alarm < now_ticks. */
b0f26631 71 ticks = s->mr - pl031_get_count(s);
dd849ef2 72 trace_pl031_set_alarm(ticks);
7e1543c2 73 if (ticks == 0) {
bc72ad67 74 timer_del(s->timer);
7e1543c2
PB
75 pl031_interrupt(s);
76 } else {
884f17c2 77 int64_t now = qemu_clock_get_ns(rtc_clock);
73bcb24d 78 timer_mod(s->timer, now + (int64_t)ticks * NANOSECONDS_PER_SECOND);
7e1543c2
PB
79 }
80}
81
a8170e5e 82static uint64_t pl031_read(void *opaque, hwaddr offset,
9edbe481 83 unsigned size)
7e1543c2 84{
b91f0dfd 85 PL031State *s = (PL031State *)opaque;
dd849ef2 86 uint64_t r;
7e1543c2
PB
87
88 switch (offset) {
89 case RTC_DR:
dd849ef2
PM
90 r = pl031_get_count(s);
91 break;
7e1543c2 92 case RTC_MR:
dd849ef2
PM
93 r = s->mr;
94 break;
7e1543c2 95 case RTC_IMSC:
dd849ef2
PM
96 r = s->im;
97 break;
7e1543c2 98 case RTC_RIS:
dd849ef2
PM
99 r = s->is;
100 break;
7e1543c2 101 case RTC_LR:
dd849ef2
PM
102 r = s->lr;
103 break;
7e1543c2
PB
104 case RTC_CR:
105 /* RTC is permanently enabled. */
dd849ef2
PM
106 r = 1;
107 break;
7e1543c2 108 case RTC_MIS:
dd849ef2
PM
109 r = s->is & s->im;
110 break;
111 case 0xfe0 ... 0xfff:
112 r = pl031_id[(offset - 0xfe0) >> 2];
113 break;
7e1543c2 114 case RTC_ICR:
a5089c05
PM
115 qemu_log_mask(LOG_GUEST_ERROR,
116 "pl031: read of write-only register at offset 0x%x\n",
117 (int)offset);
dd849ef2 118 r = 0;
7e1543c2
PB
119 break;
120 default:
a5089c05
PM
121 qemu_log_mask(LOG_GUEST_ERROR,
122 "pl031_read: Bad offset 0x%x\n", (int)offset);
dd849ef2 123 r = 0;
7e1543c2
PB
124 break;
125 }
126
dd849ef2
PM
127 trace_pl031_read(offset, r);
128 return r;
7e1543c2
PB
129}
130
a8170e5e 131static void pl031_write(void * opaque, hwaddr offset,
9edbe481 132 uint64_t value, unsigned size)
7e1543c2 133{
b91f0dfd 134 PL031State *s = (PL031State *)opaque;
7e1543c2 135
dd849ef2 136 trace_pl031_write(offset, value);
7e1543c2
PB
137
138 switch (offset) {
139 case RTC_LR:
140 s->tick_offset += value - pl031_get_count(s);
141 pl031_set_alarm(s);
142 break;
143 case RTC_MR:
144 s->mr = value;
145 pl031_set_alarm(s);
146 break;
147 case RTC_IMSC:
148 s->im = value & 1;
7e1543c2
PB
149 pl031_update(s);
150 break;
151 case RTC_ICR:
ff2712ba 152 /* The PL031 documentation (DDI0224B) states that the interrupt is
7e1543c2
PB
153 cleared when bit 0 of the written value is set. However the
154 arm926e documentation (DDI0287B) states that the interrupt is
155 cleared when any value is written. */
7e1543c2
PB
156 s->is = 0;
157 pl031_update(s);
158 break;
159 case RTC_CR:
160 /* Written value is ignored. */
161 break;
162
163 case RTC_DR:
164 case RTC_MIS:
165 case RTC_RIS:
a5089c05
PM
166 qemu_log_mask(LOG_GUEST_ERROR,
167 "pl031: write to read-only register at offset 0x%x\n",
168 (int)offset);
7e1543c2
PB
169 break;
170
171 default:
a5089c05
PM
172 qemu_log_mask(LOG_GUEST_ERROR,
173 "pl031_write: Bad offset 0x%x\n", (int)offset);
7e1543c2
PB
174 break;
175 }
176}
177
9edbe481
AK
178static const MemoryRegionOps pl031_ops = {
179 .read = pl031_read,
180 .write = pl031_write,
181 .endianness = DEVICE_NATIVE_ENDIAN,
7e1543c2
PB
182};
183
81dcc494 184static void pl031_init(Object *obj)
7e1543c2 185{
81dcc494
XZ
186 PL031State *s = PL031(obj);
187 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
f6503059 188 struct tm tm;
7e1543c2 189
81dcc494 190 memory_region_init_io(&s->iomem, obj, &pl031_ops, s, "pl031", 0x1000);
750ecd44 191 sysbus_init_mmio(dev, &s->iomem);
7e1543c2 192
a63bdb31 193 sysbus_init_irq(dev, &s->irq);
f6503059 194 qemu_get_timedate(&tm, 0);
884f17c2 195 s->tick_offset = mktimegm(&tm) -
73bcb24d 196 qemu_clock_get_ns(rtc_clock) / NANOSECONDS_PER_SECOND;
7e1543c2 197
884f17c2 198 s->timer = timer_new_ns(rtc_clock, pl031_interrupt, s);
7e1543c2 199}
a63bdb31 200
44b1ff31 201static int pl031_pre_save(void *opaque)
b0f26631 202{
b91f0dfd 203 PL031State *s = opaque;
b0f26631 204
032cfe6a
PM
205 /*
206 * The PL031 device model code uses the tick_offset field, which is
207 * the offset between what the guest RTC should read and what the
208 * QEMU rtc_clock reads:
209 * guest_rtc = rtc_clock + tick_offset
210 * and so
211 * tick_offset = guest_rtc - rtc_clock
212 *
213 * We want to migrate this offset, which sounds straightforward.
214 * Unfortunately older versions of QEMU migrated a conversion of this
215 * offset into an offset from the vm_clock. (This was in turn an
216 * attempt to be compatible with even older QEMU versions, but it
217 * has incorrect behaviour if the rtc_clock is not the same as the
218 * vm_clock.) So we put the actual tick_offset into a migration
219 * subsection, and the backwards-compatible time-relative-to-vm_clock
220 * in the main migration state.
221 *
222 * Calculate base time relative to QEMU_CLOCK_VIRTUAL:
223 */
bc72ad67 224 int64_t delta = qemu_clock_get_ns(rtc_clock) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
73bcb24d 225 s->tick_offset_vmstate = s->tick_offset + delta / NANOSECONDS_PER_SECOND;
44b1ff31
DDAG
226
227 return 0;
b0f26631
PB
228}
229
032cfe6a
PM
230static int pl031_pre_load(void *opaque)
231{
232 PL031State *s = opaque;
233
234 s->tick_offset_migrated = false;
235 return 0;
236}
237
ac204b8f
PB
238static int pl031_post_load(void *opaque, int version_id)
239{
b91f0dfd 240 PL031State *s = opaque;
ac204b8f 241
032cfe6a
PM
242 /*
243 * If we got the tick_offset subsection, then we can just use
244 * the value in that. Otherwise the source is an older QEMU and
245 * has given us the offset from the vm_clock; convert it back to
246 * an offset from the rtc_clock. This will cause time to incorrectly
247 * go backwards compared to the host RTC, but this is unavoidable.
248 */
249
250 if (!s->tick_offset_migrated) {
251 int64_t delta = qemu_clock_get_ns(rtc_clock) -
252 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
253 s->tick_offset = s->tick_offset_vmstate -
254 delta / NANOSECONDS_PER_SECOND;
255 }
ac204b8f
PB
256 pl031_set_alarm(s);
257 return 0;
258}
259
032cfe6a
PM
260static int pl031_tick_offset_post_load(void *opaque, int version_id)
261{
262 PL031State *s = opaque;
263
264 s->tick_offset_migrated = true;
265 return 0;
266}
267
268static bool pl031_tick_offset_needed(void *opaque)
269{
270 PL031State *s = opaque;
271
272 return s->migrate_tick_offset;
273}
274
275static const VMStateDescription vmstate_pl031_tick_offset = {
276 .name = "pl031/tick-offset",
277 .version_id = 1,
278 .minimum_version_id = 1,
279 .needed = pl031_tick_offset_needed,
280 .post_load = pl031_tick_offset_post_load,
281 .fields = (VMStateField[]) {
282 VMSTATE_UINT32(tick_offset, PL031State),
283 VMSTATE_END_OF_LIST()
284 }
285};
286
ac204b8f
PB
287static const VMStateDescription vmstate_pl031 = {
288 .name = "pl031",
289 .version_id = 1,
290 .minimum_version_id = 1,
b0f26631 291 .pre_save = pl031_pre_save,
032cfe6a 292 .pre_load = pl031_pre_load,
ac204b8f
PB
293 .post_load = pl031_post_load,
294 .fields = (VMStateField[]) {
b91f0dfd
AF
295 VMSTATE_UINT32(tick_offset_vmstate, PL031State),
296 VMSTATE_UINT32(mr, PL031State),
297 VMSTATE_UINT32(lr, PL031State),
298 VMSTATE_UINT32(cr, PL031State),
299 VMSTATE_UINT32(im, PL031State),
300 VMSTATE_UINT32(is, PL031State),
ac204b8f 301 VMSTATE_END_OF_LIST()
032cfe6a
PM
302 },
303 .subsections = (const VMStateDescription*[]) {
304 &vmstate_pl031_tick_offset,
305 NULL
ac204b8f
PB
306 }
307};
308
032cfe6a
PM
309static Property pl031_properties[] = {
310 /*
311 * True to correctly migrate the tick offset of the RTC. False to
312 * obtain backward migration compatibility with older QEMU versions,
313 * at the expense of the guest RTC going backwards compared with the
314 * host RTC when the VM is saved/restored if using -rtc host.
315 * (Even if set to 'true' older QEMU can migrate forward to newer QEMU;
316 * 'false' also permits newer QEMU to migrate to older QEMU.)
317 */
318 DEFINE_PROP_BOOL("migrate-tick-offset",
319 PL031State, migrate_tick_offset, true),
320 DEFINE_PROP_END_OF_LIST()
321};
322
999e12bb
AL
323static void pl031_class_init(ObjectClass *klass, void *data)
324{
39bffca2 325 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 326
39bffca2 327 dc->vmsd = &vmstate_pl031;
032cfe6a 328 dc->props = pl031_properties;
999e12bb
AL
329}
330
8c43a6f0 331static const TypeInfo pl031_info = {
d3b80049 332 .name = TYPE_PL031,
39bffca2 333 .parent = TYPE_SYS_BUS_DEVICE,
b91f0dfd 334 .instance_size = sizeof(PL031State),
81dcc494 335 .instance_init = pl031_init,
39bffca2 336 .class_init = pl031_class_init,
0dc5595c
PM
337};
338
83f7d43a 339static void pl031_register_types(void)
a63bdb31 340{
39bffca2 341 type_register_static(&pl031_info);
a63bdb31
PB
342}
343
83f7d43a 344type_init(pl031_register_types)
This page took 0.990916 seconds and 4 git commands to generate.