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