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 "qemu-common.h"
16 #include "hw/rtc/pl031.h"
17 #include "migration/vmstate.h"
19 #include "hw/qdev-properties.h"
20 #include "hw/sysbus.h"
21 #include "qemu/timer.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/cutils.h"
25 #include "qemu/module.h"
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 */
37 static const unsigned char pl031_id
[] = {
38 0x31, 0x10, 0x14, 0x00, /* Device ID */
39 0x0d, 0xf0, 0x05, 0xb1 /* Cell ID */
42 static void pl031_update(PL031State
*s
)
44 uint32_t flags
= s
->is
& s
->im
;
46 trace_pl031_irq_state(flags
);
47 qemu_set_irq(s
->irq
, flags
);
50 static void pl031_interrupt(void * opaque
)
52 PL031State
*s
= (PL031State
*)opaque
;
55 trace_pl031_alarm_raised();
59 static uint32_t pl031_get_count(PL031State
*s
)
61 int64_t now
= qemu_clock_get_ns(rtc_clock
);
62 return s
->tick_offset
+ now
/ NANOSECONDS_PER_SECOND
;
65 static void pl031_set_alarm(PL031State
*s
)
69 /* The timer wraps around. This subtraction also wraps in the same way,
70 and gives correct results when alarm < now_ticks. */
71 ticks
= s
->mr
- pl031_get_count(s
);
72 trace_pl031_set_alarm(ticks
);
77 int64_t now
= qemu_clock_get_ns(rtc_clock
);
78 timer_mod(s
->timer
, now
+ (int64_t)ticks
* NANOSECONDS_PER_SECOND
);
82 static uint64_t pl031_read(void *opaque
, hwaddr offset
,
85 PL031State
*s
= (PL031State
*)opaque
;
90 r
= pl031_get_count(s
);
105 /* RTC is permanently enabled. */
111 case 0xfe0 ... 0xfff:
112 r
= pl031_id
[(offset
- 0xfe0) >> 2];
115 qemu_log_mask(LOG_GUEST_ERROR
,
116 "pl031: read of write-only register at offset 0x%x\n",
121 qemu_log_mask(LOG_GUEST_ERROR
,
122 "pl031_read: Bad offset 0x%x\n", (int)offset
);
127 trace_pl031_read(offset
, r
);
131 static void pl031_write(void * opaque
, hwaddr offset
,
132 uint64_t value
, unsigned size
)
134 PL031State
*s
= (PL031State
*)opaque
;
136 trace_pl031_write(offset
, value
);
140 s
->tick_offset
+= value
- pl031_get_count(s
);
156 /* Written value is ignored. */
162 qemu_log_mask(LOG_GUEST_ERROR
,
163 "pl031: write to read-only register at offset 0x%x\n",
168 qemu_log_mask(LOG_GUEST_ERROR
,
169 "pl031_write: Bad offset 0x%x\n", (int)offset
);
174 static const MemoryRegionOps pl031_ops
= {
176 .write
= pl031_write
,
177 .endianness
= DEVICE_NATIVE_ENDIAN
,
180 static void pl031_init(Object
*obj
)
182 PL031State
*s
= PL031(obj
);
183 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
186 memory_region_init_io(&s
->iomem
, obj
, &pl031_ops
, s
, "pl031", 0x1000);
187 sysbus_init_mmio(dev
, &s
->iomem
);
189 sysbus_init_irq(dev
, &s
->irq
);
190 qemu_get_timedate(&tm
, 0);
191 s
->tick_offset
= mktimegm(&tm
) -
192 qemu_clock_get_ns(rtc_clock
) / NANOSECONDS_PER_SECOND
;
194 s
->timer
= timer_new_ns(rtc_clock
, pl031_interrupt
, s
);
197 static void pl031_finalize(Object
*obj
)
199 PL031State
*s
= PL031(obj
);
201 timer_free(s
->timer
);
204 static int pl031_pre_save(void *opaque
)
206 PL031State
*s
= opaque
;
209 * The PL031 device model code uses the tick_offset field, which is
210 * the offset between what the guest RTC should read and what the
211 * QEMU rtc_clock reads:
212 * guest_rtc = rtc_clock + tick_offset
214 * tick_offset = guest_rtc - rtc_clock
216 * We want to migrate this offset, which sounds straightforward.
217 * Unfortunately older versions of QEMU migrated a conversion of this
218 * offset into an offset from the vm_clock. (This was in turn an
219 * attempt to be compatible with even older QEMU versions, but it
220 * has incorrect behaviour if the rtc_clock is not the same as the
221 * vm_clock.) So we put the actual tick_offset into a migration
222 * subsection, and the backwards-compatible time-relative-to-vm_clock
223 * in the main migration state.
225 * Calculate base time relative to QEMU_CLOCK_VIRTUAL:
227 int64_t delta
= qemu_clock_get_ns(rtc_clock
) - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
228 s
->tick_offset_vmstate
= s
->tick_offset
+ delta
/ NANOSECONDS_PER_SECOND
;
233 static int pl031_pre_load(void *opaque
)
235 PL031State
*s
= opaque
;
237 s
->tick_offset_migrated
= false;
241 static int pl031_post_load(void *opaque
, int version_id
)
243 PL031State
*s
= opaque
;
246 * If we got the tick_offset subsection, then we can just use
247 * the value in that. Otherwise the source is an older QEMU and
248 * has given us the offset from the vm_clock; convert it back to
249 * an offset from the rtc_clock. This will cause time to incorrectly
250 * go backwards compared to the host RTC, but this is unavoidable.
253 if (!s
->tick_offset_migrated
) {
254 int64_t delta
= qemu_clock_get_ns(rtc_clock
) -
255 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
256 s
->tick_offset
= s
->tick_offset_vmstate
-
257 delta
/ NANOSECONDS_PER_SECOND
;
263 static int pl031_tick_offset_post_load(void *opaque
, int version_id
)
265 PL031State
*s
= opaque
;
267 s
->tick_offset_migrated
= true;
271 static bool pl031_tick_offset_needed(void *opaque
)
273 PL031State
*s
= opaque
;
275 return s
->migrate_tick_offset
;
278 static const VMStateDescription vmstate_pl031_tick_offset
= {
279 .name
= "pl031/tick-offset",
281 .minimum_version_id
= 1,
282 .needed
= pl031_tick_offset_needed
,
283 .post_load
= pl031_tick_offset_post_load
,
284 .fields
= (VMStateField
[]) {
285 VMSTATE_UINT32(tick_offset
, PL031State
),
286 VMSTATE_END_OF_LIST()
290 static const VMStateDescription vmstate_pl031
= {
293 .minimum_version_id
= 1,
294 .pre_save
= pl031_pre_save
,
295 .pre_load
= pl031_pre_load
,
296 .post_load
= pl031_post_load
,
297 .fields
= (VMStateField
[]) {
298 VMSTATE_UINT32(tick_offset_vmstate
, PL031State
),
299 VMSTATE_UINT32(mr
, PL031State
),
300 VMSTATE_UINT32(lr
, PL031State
),
301 VMSTATE_UINT32(cr
, PL031State
),
302 VMSTATE_UINT32(im
, PL031State
),
303 VMSTATE_UINT32(is
, PL031State
),
304 VMSTATE_END_OF_LIST()
306 .subsections
= (const VMStateDescription
*[]) {
307 &vmstate_pl031_tick_offset
,
312 static Property pl031_properties
[] = {
314 * True to correctly migrate the tick offset of the RTC. False to
315 * obtain backward migration compatibility with older QEMU versions,
316 * at the expense of the guest RTC going backwards compared with the
317 * host RTC when the VM is saved/restored if using -rtc host.
318 * (Even if set to 'true' older QEMU can migrate forward to newer QEMU;
319 * 'false' also permits newer QEMU to migrate to older QEMU.)
321 DEFINE_PROP_BOOL("migrate-tick-offset",
322 PL031State
, migrate_tick_offset
, true),
323 DEFINE_PROP_END_OF_LIST()
326 static void pl031_class_init(ObjectClass
*klass
, void *data
)
328 DeviceClass
*dc
= DEVICE_CLASS(klass
);
330 dc
->vmsd
= &vmstate_pl031
;
331 device_class_set_props(dc
, pl031_properties
);
334 static const TypeInfo pl031_info
= {
336 .parent
= TYPE_SYS_BUS_DEVICE
,
337 .instance_size
= sizeof(PL031State
),
338 .instance_init
= pl031_init
,
339 .instance_finalize
= pl031_finalize
,
340 .class_init
= pl031_class_init
,
343 static void pl031_register_types(void)
345 type_register_static(&pl031_info
);
348 type_init(pl031_register_types
)