2 * ARM AMBA PrimeCell PL031 RTC
4 * Copyright (c) 2007 CodeSourcery
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.
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.
14 #include "qemu/osdep.h"
15 #include "hw/rtc/pl031.h"
16 #include "migration/vmstate.h"
18 #include "hw/qdev-properties.h"
19 #include "hw/sysbus.h"
20 #include "qemu/timer.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/rtc.h"
23 #include "qemu/cutils.h"
25 #include "qemu/module.h"
27 #include "qapi/qapi-events-misc.h"
29 #define RTC_DR 0x00 /* Data read register */
30 #define RTC_MR 0x04 /* Match register */
31 #define RTC_LR 0x08 /* Data load register */
32 #define RTC_CR 0x0c /* Control register */
33 #define RTC_IMSC 0x10 /* Interrupt mask and set register */
34 #define RTC_RIS 0x14 /* Raw interrupt status register */
35 #define RTC_MIS 0x18 /* Masked interrupt status register */
36 #define RTC_ICR 0x1c /* Interrupt clear register */
38 static const unsigned char pl031_id
[] = {
39 0x31, 0x10, 0x14, 0x00, /* Device ID */
40 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
43 static void pl031_update(PL031State
*s
)
45 uint32_t flags
= s
->is
& s
->im
;
47 trace_pl031_irq_state(flags
);
48 qemu_set_irq(s
->irq
, flags
);
51 static void pl031_interrupt(void * opaque
)
53 PL031State
*s
= (PL031State
*)opaque
;
56 trace_pl031_alarm_raised();
60 static uint32_t pl031_get_count(PL031State
*s
)
62 int64_t now
= qemu_clock_get_ns(rtc_clock
);
63 return s
->tick_offset
+ now
/ NANOSECONDS_PER_SECOND
;
66 static void pl031_set_alarm(PL031State
*s
)
70 /* The timer wraps around. This subtraction also wraps in the same way,
71 and gives correct results when alarm < now_ticks. */
72 ticks
= s
->mr
- pl031_get_count(s
);
73 trace_pl031_set_alarm(ticks
);
78 int64_t now
= qemu_clock_get_ns(rtc_clock
);
79 timer_mod(s
->timer
, now
+ (int64_t)ticks
* NANOSECONDS_PER_SECOND
);
83 static uint64_t pl031_read(void *opaque
, hwaddr offset
,
86 PL031State
*s
= (PL031State
*)opaque
;
91 r
= pl031_get_count(s
);
106 /* RTC is permanently enabled. */
112 case 0xfe0 ... 0xfff:
113 r
= pl031_id
[(offset
- 0xfe0) >> 2];
116 qemu_log_mask(LOG_GUEST_ERROR
,
117 "pl031: read of write-only register at offset 0x%x\n",
122 qemu_log_mask(LOG_GUEST_ERROR
,
123 "pl031_read: Bad offset 0x%x\n", (int)offset
);
128 trace_pl031_read(offset
, r
);
132 static void pl031_write(void * opaque
, hwaddr offset
,
133 uint64_t value
, unsigned size
)
135 PL031State
*s
= (PL031State
*)opaque
;
137 trace_pl031_write(offset
, value
);
141 g_autofree
const char *qom_path
= object_get_canonical_path(opaque
);
145 s
->tick_offset
+= value
- pl031_get_count(s
);
147 qemu_get_timedate(&tm
, s
->tick_offset
);
148 qapi_event_send_rtc_change(qemu_timedate_diff(&tm
), qom_path
);
166 /* Written value is ignored. */
172 qemu_log_mask(LOG_GUEST_ERROR
,
173 "pl031: write to read-only register at offset 0x%x\n",
178 qemu_log_mask(LOG_GUEST_ERROR
,
179 "pl031_write: Bad offset 0x%x\n", (int)offset
);
184 static const MemoryRegionOps pl031_ops
= {
186 .write
= pl031_write
,
187 .endianness
= DEVICE_NATIVE_ENDIAN
,
190 static void pl031_init(Object
*obj
)
192 PL031State
*s
= PL031(obj
);
193 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
196 memory_region_init_io(&s
->iomem
, obj
, &pl031_ops
, s
, "pl031", 0x1000);
197 sysbus_init_mmio(dev
, &s
->iomem
);
199 sysbus_init_irq(dev
, &s
->irq
);
200 qemu_get_timedate(&tm
, 0);
201 s
->tick_offset
= mktimegm(&tm
) -
202 qemu_clock_get_ns(rtc_clock
) / NANOSECONDS_PER_SECOND
;
204 s
->timer
= timer_new_ns(rtc_clock
, pl031_interrupt
, s
);
207 static void pl031_finalize(Object
*obj
)
209 PL031State
*s
= PL031(obj
);
211 timer_free(s
->timer
);
214 static int pl031_pre_save(void *opaque
)
216 PL031State
*s
= opaque
;
219 * The PL031 device model code uses the tick_offset field, which is
220 * the offset between what the guest RTC should read and what the
221 * QEMU rtc_clock reads:
222 * guest_rtc = rtc_clock + tick_offset
224 * tick_offset = guest_rtc - rtc_clock
226 * We want to migrate this offset, which sounds straightforward.
227 * Unfortunately older versions of QEMU migrated a conversion of this
228 * offset into an offset from the vm_clock. (This was in turn an
229 * attempt to be compatible with even older QEMU versions, but it
230 * has incorrect behaviour if the rtc_clock is not the same as the
231 * vm_clock.) So we put the actual tick_offset into a migration
232 * subsection, and the backwards-compatible time-relative-to-vm_clock
233 * in the main migration state.
235 * Calculate base time relative to QEMU_CLOCK_VIRTUAL:
237 int64_t delta
= qemu_clock_get_ns(rtc_clock
) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
238 s
->tick_offset_vmstate
= s
->tick_offset
+ delta
/ NANOSECONDS_PER_SECOND
;
243 static int pl031_pre_load(void *opaque
)
245 PL031State
*s
= opaque
;
247 s
->tick_offset_migrated
= false;
251 static int pl031_post_load(void *opaque
, int version_id
)
253 PL031State
*s
= opaque
;
256 * If we got the tick_offset subsection, then we can just use
257 * the value in that. Otherwise the source is an older QEMU and
258 * has given us the offset from the vm_clock; convert it back to
259 * an offset from the rtc_clock. This will cause time to incorrectly
260 * go backwards compared to the host RTC, but this is unavoidable.
263 if (!s
->tick_offset_migrated
) {
264 int64_t delta
= qemu_clock_get_ns(rtc_clock
) -
265 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
266 s
->tick_offset
= s
->tick_offset_vmstate
-
267 delta
/ NANOSECONDS_PER_SECOND
;
273 static int pl031_tick_offset_post_load(void *opaque
, int version_id
)
275 PL031State
*s
= opaque
;
277 s
->tick_offset_migrated
= true;
281 static bool pl031_tick_offset_needed(void *opaque
)
283 PL031State
*s
= opaque
;
285 return s
->migrate_tick_offset
;
288 static const VMStateDescription vmstate_pl031_tick_offset
= {
289 .name
= "pl031/tick-offset",
291 .minimum_version_id
= 1,
292 .needed
= pl031_tick_offset_needed
,
293 .post_load
= pl031_tick_offset_post_load
,
294 .fields
= (const VMStateField
[]) {
295 VMSTATE_UINT32(tick_offset
, PL031State
),
296 VMSTATE_END_OF_LIST()
300 static const VMStateDescription vmstate_pl031
= {
303 .minimum_version_id
= 1,
304 .pre_save
= pl031_pre_save
,
305 .pre_load
= pl031_pre_load
,
306 .post_load
= pl031_post_load
,
307 .fields
= (const VMStateField
[]) {
308 VMSTATE_UINT32(tick_offset_vmstate
, PL031State
),
309 VMSTATE_UINT32(mr
, PL031State
),
310 VMSTATE_UINT32(lr
, PL031State
),
311 VMSTATE_UINT32(cr
, PL031State
),
312 VMSTATE_UINT32(im
, PL031State
),
313 VMSTATE_UINT32(is
, PL031State
),
314 VMSTATE_END_OF_LIST()
316 .subsections
= (const VMStateDescription
* const []) {
317 &vmstate_pl031_tick_offset
,
322 static Property pl031_properties
[] = {
324 * True to correctly migrate the tick offset of the RTC. False to
325 * obtain backward migration compatibility with older QEMU versions,
326 * at the expense of the guest RTC going backwards compared with the
327 * host RTC when the VM is saved/restored if using -rtc host.
328 * (Even if set to 'true' older QEMU can migrate forward to newer QEMU;
329 * 'false' also permits newer QEMU to migrate to older QEMU.)
331 DEFINE_PROP_BOOL("migrate-tick-offset",
332 PL031State
, migrate_tick_offset
, true),
333 DEFINE_PROP_END_OF_LIST()
336 static void pl031_class_init(ObjectClass
*klass
, void *data
)
338 DeviceClass
*dc
= DEVICE_CLASS(klass
);
340 dc
->vmsd
= &vmstate_pl031
;
341 device_class_set_props(dc
, pl031_properties
);
344 static const TypeInfo pl031_info
= {
346 .parent
= TYPE_SYS_BUS_DEVICE
,
347 .instance_size
= sizeof(PL031State
),
348 .instance_init
= pl031_init
,
349 .instance_finalize
= pl031_finalize
,
350 .class_init
= pl031_class_init
,
353 static void pl031_register_types(void)
355 type_register_static(&pl031_info
);
358 type_init(pl031_register_types
)