]>
Commit | Line | Data |
---|---|---|
80cabfad FB |
1 | /* |
2 | * QEMU MC146818 RTC emulation | |
5fafdf24 | 3 | * |
80cabfad | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
80cabfad FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
b6a0aa05 | 24 | #include "qemu/osdep.h" |
83c9f4ca | 25 | #include "hw/hw.h" |
1de7afc9 | 26 | #include "qemu/timer.h" |
9c17d615 | 27 | #include "sysemu/sysemu.h" |
0d09e41a | 28 | #include "hw/timer/mc146818rtc.h" |
7b1b5d19 | 29 | #include "qapi/visitor.h" |
e010ad8f | 30 | #include "qapi-event.h" |
f2ae8abf | 31 | #include "qmp-commands.h" |
80cabfad | 32 | |
d362e757 | 33 | #ifdef TARGET_I386 |
0d09e41a | 34 | #include "hw/i386/apic.h" |
d362e757 JK |
35 | #endif |
36 | ||
80cabfad | 37 | //#define DEBUG_CMOS |
aa6f63ff | 38 | //#define DEBUG_COALESCED |
80cabfad | 39 | |
ec51e364 IY |
40 | #ifdef DEBUG_CMOS |
41 | # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) | |
42 | #else | |
43 | # define CMOS_DPRINTF(format, ...) do { } while (0) | |
44 | #endif | |
45 | ||
aa6f63ff BS |
46 | #ifdef DEBUG_COALESCED |
47 | # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__) | |
48 | #else | |
49 | # define DPRINTF_C(format, ...) do { } while (0) | |
50 | #endif | |
51 | ||
00cf5774 PB |
52 | #define SEC_PER_MIN 60 |
53 | #define MIN_PER_HOUR 60 | |
54 | #define SEC_PER_HOUR 3600 | |
55 | #define HOUR_PER_DAY 24 | |
56 | #define SEC_PER_DAY 86400 | |
56038ef6 | 57 | |
dd17765b | 58 | #define RTC_REINJECT_ON_ACK_COUNT 20 |
e46deaba | 59 | #define RTC_CLOCK_RATE 32768 |
13566fe3 | 60 | #define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768) |
ba32edab | 61 | |
0e41271e AF |
62 | #define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC) |
63 | ||
1d914fa0 | 64 | typedef struct RTCState { |
0e41271e AF |
65 | ISADevice parent_obj; |
66 | ||
b2c5009b | 67 | MemoryRegion io; |
dff38e7b FB |
68 | uint8_t cmos_data[128]; |
69 | uint8_t cmos_index; | |
32e0c826 | 70 | int32_t base_year; |
56038ef6 YZ |
71 | uint64_t base_rtc; |
72 | uint64_t last_update; | |
73 | int64_t offset; | |
d537cf6c | 74 | qemu_irq irq; |
18c6e2ff | 75 | int it_shift; |
dff38e7b FB |
76 | /* periodic timer */ |
77 | QEMUTimer *periodic_timer; | |
78 | int64_t next_periodic_time; | |
56038ef6 YZ |
79 | /* update-ended timer */ |
80 | QEMUTimer *update_timer; | |
00cf5774 | 81 | uint64_t next_alarm_time; |
ba32edab | 82 | uint16_t irq_reinject_on_ack_count; |
73822ec8 AL |
83 | uint32_t irq_coalesced; |
84 | uint32_t period; | |
93b66569 | 85 | QEMUTimer *coalesced_timer; |
17604dac | 86 | Notifier clock_reset_notifier; |
433acf0d | 87 | LostTickPolicy lost_tick_policy; |
da98c8eb | 88 | Notifier suspend_notifier; |
f2ae8abf | 89 | QLIST_ENTRY(RTCState) link; |
1d914fa0 | 90 | } RTCState; |
dff38e7b FB |
91 | |
92 | static void rtc_set_time(RTCState *s); | |
56038ef6 | 93 | static void rtc_update_time(RTCState *s); |
e2826cf4 | 94 | static void rtc_set_cmos(RTCState *s, const struct tm *tm); |
56038ef6 | 95 | static inline int rtc_from_bcd(RTCState *s, int a); |
00cf5774 | 96 | static uint64_t get_next_alarm(RTCState *s); |
56038ef6 | 97 | |
41a9b8b2 YZ |
98 | static inline bool rtc_running(RTCState *s) |
99 | { | |
100 | return (!(s->cmos_data[RTC_REG_B] & REG_B_SET) && | |
101 | (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20); | |
102 | } | |
103 | ||
56038ef6 YZ |
104 | static uint64_t get_guest_rtc_ns(RTCState *s) |
105 | { | |
106 | uint64_t guest_rtc; | |
884f17c2 | 107 | uint64_t guest_clock = qemu_clock_get_ns(rtc_clock); |
56038ef6 | 108 | |
13566fe3 | 109 | guest_rtc = s->base_rtc * NANOSECONDS_PER_SECOND |
56038ef6 YZ |
110 | + guest_clock - s->last_update + s->offset; |
111 | return guest_rtc; | |
112 | } | |
dff38e7b | 113 | |
93b66569 AL |
114 | #ifdef TARGET_I386 |
115 | static void rtc_coalesced_timer_update(RTCState *s) | |
116 | { | |
117 | if (s->irq_coalesced == 0) { | |
bc72ad67 | 118 | timer_del(s->coalesced_timer); |
93b66569 AL |
119 | } else { |
120 | /* divide each RTC interval to 2 - 8 smaller intervals */ | |
121 | int c = MIN(s->irq_coalesced, 7) + 1; | |
884f17c2 | 122 | int64_t next_clock = qemu_clock_get_ns(rtc_clock) + |
e46deaba | 123 | muldiv64(s->period / c, get_ticks_per_sec(), RTC_CLOCK_RATE); |
bc72ad67 | 124 | timer_mod(s->coalesced_timer, next_clock); |
93b66569 AL |
125 | } |
126 | } | |
127 | ||
128 | static void rtc_coalesced_timer(void *opaque) | |
129 | { | |
130 | RTCState *s = opaque; | |
131 | ||
132 | if (s->irq_coalesced != 0) { | |
133 | apic_reset_irq_delivered(); | |
134 | s->cmos_data[RTC_REG_C] |= 0xc0; | |
aa6f63ff | 135 | DPRINTF_C("cmos: injecting from timer\n"); |
7d932dfd | 136 | qemu_irq_raise(s->irq); |
93b66569 AL |
137 | if (apic_get_irq_delivered()) { |
138 | s->irq_coalesced--; | |
aa6f63ff BS |
139 | DPRINTF_C("cmos: coalesced irqs decreased to %d\n", |
140 | s->irq_coalesced); | |
93b66569 AL |
141 | } |
142 | } | |
143 | ||
144 | rtc_coalesced_timer_update(s); | |
145 | } | |
146 | #endif | |
147 | ||
56038ef6 | 148 | /* handle periodic timer */ |
c4c18e24 | 149 | static void periodic_timer_update(RTCState *s, int64_t current_time) |
dff38e7b FB |
150 | { |
151 | int period_code, period; | |
152 | int64_t cur_clock, next_irq_clock; | |
153 | ||
154 | period_code = s->cmos_data[RTC_REG_A] & 0x0f; | |
100d9891 | 155 | if (period_code != 0 |
c2d30667 | 156 | && (s->cmos_data[RTC_REG_B] & REG_B_PIE)) { |
dff38e7b FB |
157 | if (period_code <= 2) |
158 | period_code += 7; | |
159 | /* period in 32 Khz cycles */ | |
160 | period = 1 << (period_code - 1); | |
73822ec8 | 161 | #ifdef TARGET_I386 |
aa6f63ff | 162 | if (period != s->period) { |
73822ec8 | 163 | s->irq_coalesced = (s->irq_coalesced * s->period) / period; |
aa6f63ff BS |
164 | DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced); |
165 | } | |
73822ec8 AL |
166 | s->period = period; |
167 | #endif | |
dff38e7b | 168 | /* compute 32 khz clock */ |
e46deaba | 169 | cur_clock = muldiv64(current_time, RTC_CLOCK_RATE, get_ticks_per_sec()); |
dff38e7b | 170 | next_irq_clock = (cur_clock & ~(period - 1)) + period; |
6875204c | 171 | s->next_periodic_time = |
e46deaba | 172 | muldiv64(next_irq_clock, get_ticks_per_sec(), RTC_CLOCK_RATE) + 1; |
bc72ad67 | 173 | timer_mod(s->periodic_timer, s->next_periodic_time); |
dff38e7b | 174 | } else { |
73822ec8 AL |
175 | #ifdef TARGET_I386 |
176 | s->irq_coalesced = 0; | |
177 | #endif | |
bc72ad67 | 178 | timer_del(s->periodic_timer); |
dff38e7b FB |
179 | } |
180 | } | |
181 | ||
182 | static void rtc_periodic_timer(void *opaque) | |
183 | { | |
184 | RTCState *s = opaque; | |
185 | ||
c4c18e24 | 186 | periodic_timer_update(s, s->next_periodic_time); |
663447d4 | 187 | s->cmos_data[RTC_REG_C] |= REG_C_PF; |
100d9891 | 188 | if (s->cmos_data[RTC_REG_B] & REG_B_PIE) { |
663447d4 | 189 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; |
93b66569 | 190 | #ifdef TARGET_I386 |
104059da | 191 | if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) { |
ba32edab GN |
192 | if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT) |
193 | s->irq_reinject_on_ack_count = 0; | |
93b66569 | 194 | apic_reset_irq_delivered(); |
7d932dfd | 195 | qemu_irq_raise(s->irq); |
93b66569 AL |
196 | if (!apic_get_irq_delivered()) { |
197 | s->irq_coalesced++; | |
198 | rtc_coalesced_timer_update(s); | |
aa6f63ff BS |
199 | DPRINTF_C("cmos: coalesced irqs increased to %d\n", |
200 | s->irq_coalesced); | |
93b66569 AL |
201 | } |
202 | } else | |
203 | #endif | |
7d932dfd | 204 | qemu_irq_raise(s->irq); |
100d9891 | 205 | } |
dff38e7b | 206 | } |
80cabfad | 207 | |
56038ef6 YZ |
208 | /* handle update-ended timer */ |
209 | static void check_update_timer(RTCState *s) | |
210 | { | |
211 | uint64_t next_update_time; | |
212 | uint64_t guest_nsec; | |
00cf5774 | 213 | int next_alarm_sec; |
56038ef6 | 214 | |
41a9b8b2 YZ |
215 | /* From the data sheet: "Holding the dividers in reset prevents |
216 | * interrupts from operating, while setting the SET bit allows" | |
217 | * them to occur. However, it will prevent an alarm interrupt | |
218 | * from occurring, because the time of day is not updated. | |
56038ef6 | 219 | */ |
41a9b8b2 | 220 | if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) { |
bc72ad67 | 221 | timer_del(s->update_timer); |
41a9b8b2 YZ |
222 | return; |
223 | } | |
56038ef6 YZ |
224 | if ((s->cmos_data[RTC_REG_C] & REG_C_UF) && |
225 | (s->cmos_data[RTC_REG_B] & REG_B_SET)) { | |
bc72ad67 | 226 | timer_del(s->update_timer); |
56038ef6 YZ |
227 | return; |
228 | } | |
229 | if ((s->cmos_data[RTC_REG_C] & REG_C_UF) && | |
230 | (s->cmos_data[RTC_REG_C] & REG_C_AF)) { | |
bc72ad67 | 231 | timer_del(s->update_timer); |
56038ef6 YZ |
232 | return; |
233 | } | |
234 | ||
13566fe3 | 235 | guest_nsec = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND; |
00cf5774 | 236 | /* if UF is clear, reprogram to next second */ |
884f17c2 | 237 | next_update_time = qemu_clock_get_ns(rtc_clock) |
13566fe3 | 238 | + NANOSECONDS_PER_SECOND - guest_nsec; |
00cf5774 PB |
239 | |
240 | /* Compute time of next alarm. One second is already accounted | |
241 | * for in next_update_time. | |
242 | */ | |
243 | next_alarm_sec = get_next_alarm(s); | |
13566fe3 SH |
244 | s->next_alarm_time = next_update_time + |
245 | (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND; | |
00cf5774 PB |
246 | |
247 | if (s->cmos_data[RTC_REG_C] & REG_C_UF) { | |
248 | /* UF is set, but AF is clear. Program the timer to target | |
249 | * the alarm time. */ | |
250 | next_update_time = s->next_alarm_time; | |
251 | } | |
e93379b0 | 252 | if (next_update_time != timer_expire_time_ns(s->update_timer)) { |
bc72ad67 | 253 | timer_mod(s->update_timer, next_update_time); |
56038ef6 YZ |
254 | } |
255 | } | |
256 | ||
257 | static inline uint8_t convert_hour(RTCState *s, uint8_t hour) | |
258 | { | |
259 | if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) { | |
260 | hour %= 12; | |
261 | if (s->cmos_data[RTC_HOURS] & 0x80) { | |
262 | hour += 12; | |
263 | } | |
264 | } | |
265 | return hour; | |
266 | } | |
267 | ||
00cf5774 | 268 | static uint64_t get_next_alarm(RTCState *s) |
56038ef6 | 269 | { |
00cf5774 PB |
270 | int32_t alarm_sec, alarm_min, alarm_hour, cur_hour, cur_min, cur_sec; |
271 | int32_t hour, min, sec; | |
272 | ||
273 | rtc_update_time(s); | |
56038ef6 YZ |
274 | |
275 | alarm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]); | |
276 | alarm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]); | |
277 | alarm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]); | |
00cf5774 | 278 | alarm_hour = alarm_hour == -1 ? -1 : convert_hour(s, alarm_hour); |
56038ef6 YZ |
279 | |
280 | cur_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]); | |
281 | cur_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]); | |
282 | cur_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS]); | |
283 | cur_hour = convert_hour(s, cur_hour); | |
284 | ||
00cf5774 PB |
285 | if (alarm_hour == -1) { |
286 | alarm_hour = cur_hour; | |
287 | if (alarm_min == -1) { | |
288 | alarm_min = cur_min; | |
289 | if (alarm_sec == -1) { | |
290 | alarm_sec = cur_sec + 1; | |
291 | } else if (cur_sec > alarm_sec) { | |
292 | alarm_min++; | |
293 | } | |
294 | } else if (cur_min == alarm_min) { | |
295 | if (alarm_sec == -1) { | |
296 | alarm_sec = cur_sec + 1; | |
297 | } else { | |
298 | if (cur_sec > alarm_sec) { | |
299 | alarm_hour++; | |
300 | } | |
301 | } | |
302 | if (alarm_sec == SEC_PER_MIN) { | |
303 | /* wrap to next hour, minutes is not in don't care mode */ | |
304 | alarm_sec = 0; | |
305 | alarm_hour++; | |
306 | } | |
307 | } else if (cur_min > alarm_min) { | |
308 | alarm_hour++; | |
309 | } | |
310 | } else if (cur_hour == alarm_hour) { | |
311 | if (alarm_min == -1) { | |
312 | alarm_min = cur_min; | |
313 | if (alarm_sec == -1) { | |
314 | alarm_sec = cur_sec + 1; | |
315 | } else if (cur_sec > alarm_sec) { | |
316 | alarm_min++; | |
317 | } | |
318 | ||
319 | if (alarm_sec == SEC_PER_MIN) { | |
320 | alarm_sec = 0; | |
321 | alarm_min++; | |
322 | } | |
323 | /* wrap to next day, hour is not in don't care mode */ | |
324 | alarm_min %= MIN_PER_HOUR; | |
325 | } else if (cur_min == alarm_min) { | |
326 | if (alarm_sec == -1) { | |
327 | alarm_sec = cur_sec + 1; | |
328 | } | |
329 | /* wrap to next day, hours+minutes not in don't care mode */ | |
330 | alarm_sec %= SEC_PER_MIN; | |
331 | } | |
56038ef6 | 332 | } |
56038ef6 | 333 | |
00cf5774 PB |
334 | /* values that are still don't care fire at the next min/sec */ |
335 | if (alarm_min == -1) { | |
336 | alarm_min = 0; | |
337 | } | |
338 | if (alarm_sec == -1) { | |
339 | alarm_sec = 0; | |
340 | } | |
341 | ||
342 | /* keep values in range */ | |
343 | if (alarm_sec == SEC_PER_MIN) { | |
344 | alarm_sec = 0; | |
345 | alarm_min++; | |
346 | } | |
347 | if (alarm_min == MIN_PER_HOUR) { | |
348 | alarm_min = 0; | |
349 | alarm_hour++; | |
350 | } | |
351 | alarm_hour %= HOUR_PER_DAY; | |
352 | ||
353 | hour = alarm_hour - cur_hour; | |
354 | min = hour * MIN_PER_HOUR + alarm_min - cur_min; | |
355 | sec = min * SEC_PER_MIN + alarm_sec - cur_sec; | |
356 | return sec <= 0 ? sec + SEC_PER_DAY : sec; | |
56038ef6 YZ |
357 | } |
358 | ||
359 | static void rtc_update_timer(void *opaque) | |
360 | { | |
361 | RTCState *s = opaque; | |
362 | int32_t irqs = REG_C_UF; | |
363 | int32_t new_irqs; | |
364 | ||
41a9b8b2 YZ |
365 | assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60); |
366 | ||
56038ef6 YZ |
367 | /* UIP might have been latched, update time and clear it. */ |
368 | rtc_update_time(s); | |
369 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
370 | ||
884f17c2 | 371 | if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) { |
56038ef6 YZ |
372 | irqs |= REG_C_AF; |
373 | if (s->cmos_data[RTC_REG_B] & REG_B_AIE) { | |
374 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC); | |
375 | } | |
376 | } | |
00cf5774 | 377 | |
56038ef6 YZ |
378 | new_irqs = irqs & ~s->cmos_data[RTC_REG_C]; |
379 | s->cmos_data[RTC_REG_C] |= irqs; | |
380 | if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) { | |
381 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; | |
382 | qemu_irq_raise(s->irq); | |
383 | } | |
384 | check_update_timer(s); | |
385 | } | |
386 | ||
0da8c842 AG |
387 | static void cmos_ioport_write(void *opaque, hwaddr addr, |
388 | uint64_t data, unsigned size) | |
80cabfad | 389 | { |
b41a2cd1 | 390 | RTCState *s = opaque; |
80cabfad FB |
391 | |
392 | if ((addr & 1) == 0) { | |
393 | s->cmos_index = data & 0x7f; | |
394 | } else { | |
c5539cb4 | 395 | CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64 "\n", |
ec51e364 | 396 | s->cmos_index, data); |
dff38e7b | 397 | switch(s->cmos_index) { |
80cabfad FB |
398 | case RTC_SECONDS_ALARM: |
399 | case RTC_MINUTES_ALARM: | |
400 | case RTC_HOURS_ALARM: | |
80cabfad | 401 | s->cmos_data[s->cmos_index] = data; |
56038ef6 | 402 | check_update_timer(s); |
80cabfad | 403 | break; |
e67edb94 PB |
404 | case RTC_IBM_PS2_CENTURY_BYTE: |
405 | s->cmos_index = RTC_CENTURY; | |
406 | /* fall through */ | |
407 | case RTC_CENTURY: | |
80cabfad FB |
408 | case RTC_SECONDS: |
409 | case RTC_MINUTES: | |
410 | case RTC_HOURS: | |
411 | case RTC_DAY_OF_WEEK: | |
412 | case RTC_DAY_OF_MONTH: | |
413 | case RTC_MONTH: | |
414 | case RTC_YEAR: | |
415 | s->cmos_data[s->cmos_index] = data; | |
dff38e7b | 416 | /* if in set mode, do not update the time */ |
41a9b8b2 | 417 | if (rtc_running(s)) { |
dff38e7b | 418 | rtc_set_time(s); |
56038ef6 | 419 | check_update_timer(s); |
dff38e7b | 420 | } |
80cabfad FB |
421 | break; |
422 | case RTC_REG_A: | |
41a9b8b2 YZ |
423 | if ((data & 0x60) == 0x60) { |
424 | if (rtc_running(s)) { | |
425 | rtc_update_time(s); | |
426 | } | |
427 | /* What happens to UIP when divider reset is enabled is | |
428 | * unclear from the datasheet. Shouldn't matter much | |
429 | * though. | |
430 | */ | |
431 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
432 | } else if (((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) && | |
433 | (data & 0x70) <= 0x20) { | |
434 | /* when the divider reset is removed, the first update cycle | |
435 | * begins one-half second later*/ | |
436 | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) { | |
437 | s->offset = 500000000; | |
438 | rtc_set_time(s); | |
439 | } | |
440 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
441 | } | |
dff38e7b FB |
442 | /* UIP bit is read only */ |
443 | s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) | | |
444 | (s->cmos_data[RTC_REG_A] & REG_A_UIP); | |
884f17c2 | 445 | periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); |
56038ef6 | 446 | check_update_timer(s); |
dff38e7b | 447 | break; |
80cabfad | 448 | case RTC_REG_B: |
dff38e7b | 449 | if (data & REG_B_SET) { |
56038ef6 | 450 | /* update cmos to when the rtc was stopping */ |
41a9b8b2 | 451 | if (rtc_running(s)) { |
56038ef6 YZ |
452 | rtc_update_time(s); |
453 | } | |
dff38e7b FB |
454 | /* set mode: reset UIP mode */ |
455 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
456 | data &= ~REG_B_UIE; | |
457 | } else { | |
458 | /* if disabling set mode, update the time */ | |
41a9b8b2 YZ |
459 | if ((s->cmos_data[RTC_REG_B] & REG_B_SET) && |
460 | (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20) { | |
13566fe3 | 461 | s->offset = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND; |
dff38e7b FB |
462 | rtc_set_time(s); |
463 | } | |
464 | } | |
9324cc50 YZ |
465 | /* if an interrupt flag is already set when the interrupt |
466 | * becomes enabled, raise an interrupt immediately. */ | |
467 | if (data & s->cmos_data[RTC_REG_C] & REG_C_MASK) { | |
468 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; | |
469 | qemu_irq_raise(s->irq); | |
470 | } else { | |
471 | s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF; | |
472 | qemu_irq_lower(s->irq); | |
473 | } | |
bedc572e | 474 | s->cmos_data[RTC_REG_B] = data; |
884f17c2 | 475 | periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); |
56038ef6 | 476 | check_update_timer(s); |
80cabfad FB |
477 | break; |
478 | case RTC_REG_C: | |
479 | case RTC_REG_D: | |
480 | /* cannot write to them */ | |
481 | break; | |
482 | default: | |
483 | s->cmos_data[s->cmos_index] = data; | |
484 | break; | |
485 | } | |
486 | } | |
487 | } | |
488 | ||
abd0c6bd | 489 | static inline int rtc_to_bcd(RTCState *s, int a) |
80cabfad | 490 | { |
6f1bf24d | 491 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) { |
dff38e7b FB |
492 | return a; |
493 | } else { | |
494 | return ((a / 10) << 4) | (a % 10); | |
495 | } | |
80cabfad FB |
496 | } |
497 | ||
abd0c6bd | 498 | static inline int rtc_from_bcd(RTCState *s, int a) |
80cabfad | 499 | { |
00cf5774 PB |
500 | if ((a & 0xc0) == 0xc0) { |
501 | return -1; | |
502 | } | |
6f1bf24d | 503 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) { |
dff38e7b FB |
504 | return a; |
505 | } else { | |
506 | return ((a >> 4) * 10) + (a & 0x0f); | |
507 | } | |
508 | } | |
509 | ||
e2826cf4 | 510 | static void rtc_get_time(RTCState *s, struct tm *tm) |
dff38e7b | 511 | { |
abd0c6bd PB |
512 | tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]); |
513 | tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]); | |
514 | tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f); | |
3b89eb43 PB |
515 | if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) { |
516 | tm->tm_hour %= 12; | |
517 | if (s->cmos_data[RTC_HOURS] & 0x80) { | |
518 | tm->tm_hour += 12; | |
519 | } | |
43f493af | 520 | } |
abd0c6bd PB |
521 | tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1; |
522 | tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]); | |
523 | tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1; | |
b8994faf PB |
524 | tm->tm_year = |
525 | rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year + | |
526 | rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900; | |
e2826cf4 PB |
527 | } |
528 | ||
f2ae8abf MT |
529 | static QLIST_HEAD(, RTCState) rtc_devices = |
530 | QLIST_HEAD_INITIALIZER(rtc_devices); | |
531 | ||
532 | #ifdef TARGET_I386 | |
533 | void qmp_rtc_reset_reinjection(Error **errp) | |
534 | { | |
535 | RTCState *s; | |
536 | ||
537 | QLIST_FOREACH(s, &rtc_devices, link) { | |
538 | s->irq_coalesced = 0; | |
539 | } | |
540 | } | |
541 | #endif | |
542 | ||
e2826cf4 PB |
543 | static void rtc_set_time(RTCState *s) |
544 | { | |
545 | struct tm tm; | |
80cd3478 | 546 | |
e2826cf4 | 547 | rtc_get_time(s, &tm); |
e2826cf4 | 548 | s->base_rtc = mktimegm(&tm); |
884f17c2 | 549 | s->last_update = qemu_clock_get_ns(rtc_clock); |
56038ef6 | 550 | |
e010ad8f | 551 | qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort); |
43f493af FB |
552 | } |
553 | ||
e2826cf4 | 554 | static void rtc_set_cmos(RTCState *s, const struct tm *tm) |
43f493af | 555 | { |
42fc73a1 | 556 | int year; |
dff38e7b | 557 | |
abd0c6bd PB |
558 | s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec); |
559 | s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min); | |
c29cd656 | 560 | if (s->cmos_data[RTC_REG_B] & REG_B_24H) { |
43f493af | 561 | /* 24 hour format */ |
abd0c6bd | 562 | s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour); |
43f493af FB |
563 | } else { |
564 | /* 12 hour format */ | |
3b89eb43 PB |
565 | int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12; |
566 | s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h); | |
43f493af FB |
567 | if (tm->tm_hour >= 12) |
568 | s->cmos_data[RTC_HOURS] |= 0x80; | |
569 | } | |
abd0c6bd PB |
570 | s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1); |
571 | s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday); | |
572 | s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1); | |
b8994faf PB |
573 | year = tm->tm_year + 1900 - s->base_year; |
574 | s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100); | |
575 | s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100); | |
43f493af FB |
576 | } |
577 | ||
56038ef6 | 578 | static void rtc_update_time(RTCState *s) |
43f493af | 579 | { |
56038ef6 YZ |
580 | struct tm ret; |
581 | time_t guest_sec; | |
582 | int64_t guest_nsec; | |
583 | ||
584 | guest_nsec = get_guest_rtc_ns(s); | |
13566fe3 | 585 | guest_sec = guest_nsec / NANOSECONDS_PER_SECOND; |
56038ef6 | 586 | gmtime_r(&guest_sec, &ret); |
02c6ccc6 AH |
587 | |
588 | /* Is SET flag of Register B disabled? */ | |
589 | if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) { | |
590 | rtc_set_cmos(s, &ret); | |
591 | } | |
43f493af FB |
592 | } |
593 | ||
56038ef6 | 594 | static int update_in_progress(RTCState *s) |
43f493af | 595 | { |
56038ef6 | 596 | int64_t guest_nsec; |
3b46e624 | 597 | |
41a9b8b2 | 598 | if (!rtc_running(s)) { |
56038ef6 | 599 | return 0; |
dff38e7b | 600 | } |
e93379b0 AB |
601 | if (timer_pending(s->update_timer)) { |
602 | int64_t next_update_time = timer_expire_time_ns(s->update_timer); | |
56038ef6 | 603 | /* Latch UIP until the timer expires. */ |
884f17c2 AB |
604 | if (qemu_clock_get_ns(rtc_clock) >= |
605 | (next_update_time - UIP_HOLD_LENGTH)) { | |
56038ef6 YZ |
606 | s->cmos_data[RTC_REG_A] |= REG_A_UIP; |
607 | return 1; | |
dff38e7b FB |
608 | } |
609 | } | |
610 | ||
56038ef6 YZ |
611 | guest_nsec = get_guest_rtc_ns(s); |
612 | /* UIP bit will be set at last 244us of every second. */ | |
13566fe3 SH |
613 | if ((guest_nsec % NANOSECONDS_PER_SECOND) >= |
614 | (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) { | |
56038ef6 | 615 | return 1; |
dff38e7b | 616 | } |
56038ef6 | 617 | return 0; |
80cabfad FB |
618 | } |
619 | ||
0da8c842 AG |
620 | static uint64_t cmos_ioport_read(void *opaque, hwaddr addr, |
621 | unsigned size) | |
80cabfad | 622 | { |
b41a2cd1 | 623 | RTCState *s = opaque; |
80cabfad FB |
624 | int ret; |
625 | if ((addr & 1) == 0) { | |
626 | return 0xff; | |
627 | } else { | |
628 | switch(s->cmos_index) { | |
e67edb94 PB |
629 | case RTC_IBM_PS2_CENTURY_BYTE: |
630 | s->cmos_index = RTC_CENTURY; | |
631 | /* fall through */ | |
632 | case RTC_CENTURY: | |
80cabfad FB |
633 | case RTC_SECONDS: |
634 | case RTC_MINUTES: | |
635 | case RTC_HOURS: | |
636 | case RTC_DAY_OF_WEEK: | |
637 | case RTC_DAY_OF_MONTH: | |
638 | case RTC_MONTH: | |
639 | case RTC_YEAR: | |
56038ef6 YZ |
640 | /* if not in set mode, calibrate cmos before |
641 | * reading*/ | |
41a9b8b2 | 642 | if (rtc_running(s)) { |
56038ef6 YZ |
643 | rtc_update_time(s); |
644 | } | |
80cabfad FB |
645 | ret = s->cmos_data[s->cmos_index]; |
646 | break; | |
647 | case RTC_REG_A: | |
56038ef6 YZ |
648 | if (update_in_progress(s)) { |
649 | s->cmos_data[s->cmos_index] |= REG_A_UIP; | |
650 | } else { | |
651 | s->cmos_data[s->cmos_index] &= ~REG_A_UIP; | |
652 | } | |
80cabfad | 653 | ret = s->cmos_data[s->cmos_index]; |
80cabfad FB |
654 | break; |
655 | case RTC_REG_C: | |
656 | ret = s->cmos_data[s->cmos_index]; | |
d537cf6c | 657 | qemu_irq_lower(s->irq); |
fbc15e27 | 658 | s->cmos_data[RTC_REG_C] = 0x00; |
56038ef6 YZ |
659 | if (ret & (REG_C_UF | REG_C_AF)) { |
660 | check_update_timer(s); | |
661 | } | |
ba32edab GN |
662 | #ifdef TARGET_I386 |
663 | if(s->irq_coalesced && | |
fbc15e27 | 664 | (s->cmos_data[RTC_REG_B] & REG_B_PIE) && |
ba32edab GN |
665 | s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) { |
666 | s->irq_reinject_on_ack_count++; | |
fbc15e27 | 667 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF; |
ba32edab | 668 | apic_reset_irq_delivered(); |
aa6f63ff | 669 | DPRINTF_C("cmos: injecting on ack\n"); |
ba32edab | 670 | qemu_irq_raise(s->irq); |
aa6f63ff | 671 | if (apic_get_irq_delivered()) { |
ba32edab | 672 | s->irq_coalesced--; |
aa6f63ff BS |
673 | DPRINTF_C("cmos: coalesced irqs decreased to %d\n", |
674 | s->irq_coalesced); | |
675 | } | |
ba32edab GN |
676 | } |
677 | #endif | |
80cabfad FB |
678 | break; |
679 | default: | |
680 | ret = s->cmos_data[s->cmos_index]; | |
681 | break; | |
682 | } | |
ec51e364 IY |
683 | CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n", |
684 | s->cmos_index, ret); | |
80cabfad FB |
685 | return ret; |
686 | } | |
687 | } | |
688 | ||
1d914fa0 | 689 | void rtc_set_memory(ISADevice *dev, int addr, int val) |
dff38e7b | 690 | { |
0e41271e | 691 | RTCState *s = MC146818_RTC(dev); |
dff38e7b FB |
692 | if (addr >= 0 && addr <= 127) |
693 | s->cmos_data[addr] = val; | |
694 | } | |
695 | ||
b8b7456d IM |
696 | int rtc_get_memory(ISADevice *dev, int addr) |
697 | { | |
698 | RTCState *s = MC146818_RTC(dev); | |
699 | assert(addr >= 0 && addr <= 127); | |
700 | return s->cmos_data[addr]; | |
701 | } | |
702 | ||
1d914fa0 | 703 | static void rtc_set_date_from_host(ISADevice *dev) |
ea55ffb3 | 704 | { |
0e41271e | 705 | RTCState *s = MC146818_RTC(dev); |
f6503059 | 706 | struct tm tm; |
ea55ffb3 | 707 | |
f6503059 | 708 | qemu_get_timedate(&tm, 0); |
56038ef6 YZ |
709 | |
710 | s->base_rtc = mktimegm(&tm); | |
884f17c2 | 711 | s->last_update = qemu_clock_get_ns(rtc_clock); |
56038ef6 YZ |
712 | s->offset = 0; |
713 | ||
714 | /* set the CMOS date */ | |
e2826cf4 | 715 | rtc_set_cmos(s, &tm); |
ea55ffb3 TS |
716 | } |
717 | ||
6b075b8a | 718 | static int rtc_post_load(void *opaque, int version_id) |
80cabfad | 719 | { |
dff38e7b FB |
720 | RTCState *s = opaque; |
721 | ||
56038ef6 YZ |
722 | if (version_id <= 2) { |
723 | rtc_set_time(s); | |
724 | s->offset = 0; | |
725 | check_update_timer(s); | |
726 | } | |
727 | ||
ae46e239 PD |
728 | uint64_t now = qemu_clock_get_ns(rtc_clock); |
729 | if (now < s->next_periodic_time || | |
730 | now > (s->next_periodic_time + get_max_clock_jump())) { | |
731 | periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); | |
732 | } | |
733 | ||
56038ef6 | 734 | #ifdef TARGET_I386 |
048c74c4 | 735 | if (version_id >= 2) { |
104059da | 736 | if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) { |
048c74c4 JQ |
737 | rtc_coalesced_timer_update(s); |
738 | } | |
048c74c4 | 739 | } |
6b075b8a | 740 | #endif |
73822ec8 AL |
741 | return 0; |
742 | } | |
73822ec8 | 743 | |
5cd8cada JQ |
744 | static bool rtc_irq_reinject_on_ack_count_needed(void *opaque) |
745 | { | |
746 | RTCState *s = (RTCState *)opaque; | |
747 | return s->irq_reinject_on_ack_count != 0; | |
748 | } | |
749 | ||
0b102153 | 750 | static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count = { |
bb426311 | 751 | .name = "mc146818rtc/irq_reinject_on_ack_count", |
0b102153 PD |
752 | .version_id = 1, |
753 | .minimum_version_id = 1, | |
5cd8cada | 754 | .needed = rtc_irq_reinject_on_ack_count_needed, |
0b102153 PD |
755 | .fields = (VMStateField[]) { |
756 | VMSTATE_UINT16(irq_reinject_on_ack_count, RTCState), | |
757 | VMSTATE_END_OF_LIST() | |
758 | } | |
759 | }; | |
760 | ||
6b075b8a JQ |
761 | static const VMStateDescription vmstate_rtc = { |
762 | .name = "mc146818rtc", | |
56038ef6 | 763 | .version_id = 3, |
6b075b8a | 764 | .minimum_version_id = 1, |
6b075b8a | 765 | .post_load = rtc_post_load, |
d49805ae | 766 | .fields = (VMStateField[]) { |
6b075b8a JQ |
767 | VMSTATE_BUFFER(cmos_data, RTCState), |
768 | VMSTATE_UINT8(cmos_index, RTCState), | |
89166459 | 769 | VMSTATE_UNUSED(7*4), |
e720677e | 770 | VMSTATE_TIMER_PTR(periodic_timer, RTCState), |
6b075b8a | 771 | VMSTATE_INT64(next_periodic_time, RTCState), |
56038ef6 | 772 | VMSTATE_UNUSED(3*8), |
6b075b8a JQ |
773 | VMSTATE_UINT32_V(irq_coalesced, RTCState, 2), |
774 | VMSTATE_UINT32_V(period, RTCState, 2), | |
56038ef6 YZ |
775 | VMSTATE_UINT64_V(base_rtc, RTCState, 3), |
776 | VMSTATE_UINT64_V(last_update, RTCState, 3), | |
777 | VMSTATE_INT64_V(offset, RTCState, 3), | |
e720677e | 778 | VMSTATE_TIMER_PTR_V(update_timer, RTCState, 3), |
00cf5774 | 779 | VMSTATE_UINT64_V(next_alarm_time, RTCState, 3), |
6b075b8a | 780 | VMSTATE_END_OF_LIST() |
0b102153 | 781 | }, |
5cd8cada JQ |
782 | .subsections = (const VMStateDescription*[]) { |
783 | &vmstate_rtc_irq_reinject_on_ack_count, | |
784 | NULL | |
6b075b8a JQ |
785 | } |
786 | }; | |
787 | ||
17604dac JK |
788 | static void rtc_notify_clock_reset(Notifier *notifier, void *data) |
789 | { | |
790 | RTCState *s = container_of(notifier, RTCState, clock_reset_notifier); | |
791 | int64_t now = *(int64_t *)data; | |
792 | ||
0e41271e | 793 | rtc_set_date_from_host(ISA_DEVICE(s)); |
c4c18e24 | 794 | periodic_timer_update(s, now); |
56038ef6 | 795 | check_update_timer(s); |
17604dac | 796 | #ifdef TARGET_I386 |
104059da | 797 | if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) { |
17604dac JK |
798 | rtc_coalesced_timer_update(s); |
799 | } | |
800 | #endif | |
801 | } | |
802 | ||
da98c8eb GH |
803 | /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE) |
804 | BIOS will read it and start S3 resume at POST Entry */ | |
805 | static void rtc_notify_suspend(Notifier *notifier, void *data) | |
806 | { | |
807 | RTCState *s = container_of(notifier, RTCState, suspend_notifier); | |
0e41271e | 808 | rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE); |
da98c8eb GH |
809 | } |
810 | ||
eeb7c03c GN |
811 | static void rtc_reset(void *opaque) |
812 | { | |
813 | RTCState *s = opaque; | |
814 | ||
72716184 AL |
815 | s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE); |
816 | s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF); | |
56038ef6 | 817 | check_update_timer(s); |
eeb7c03c | 818 | |
72716184 | 819 | qemu_irq_lower(s->irq); |
eeb7c03c GN |
820 | |
821 | #ifdef TARGET_I386 | |
104059da | 822 | if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) { |
433acf0d | 823 | s->irq_coalesced = 0; |
172dbc52 | 824 | s->irq_reinject_on_ack_count = 0; |
433acf0d | 825 | } |
eeb7c03c GN |
826 | #endif |
827 | } | |
828 | ||
b2c5009b | 829 | static const MemoryRegionOps cmos_ops = { |
0da8c842 AG |
830 | .read = cmos_ioport_read, |
831 | .write = cmos_ioport_write, | |
832 | .impl = { | |
833 | .min_access_size = 1, | |
834 | .max_access_size = 1, | |
835 | }, | |
836 | .endianness = DEVICE_LITTLE_ENDIAN, | |
b2c5009b RH |
837 | }; |
838 | ||
8e099d14 | 839 | static void rtc_get_date(Object *obj, struct tm *current_tm, Error **errp) |
18297050 | 840 | { |
0e41271e | 841 | RTCState *s = MC146818_RTC(obj); |
18297050 | 842 | |
56038ef6 | 843 | rtc_update_time(s); |
8e099d14 | 844 | rtc_get_time(s, current_tm); |
18297050 AL |
845 | } |
846 | ||
db895a1e | 847 | static void rtc_realizefn(DeviceState *dev, Error **errp) |
dff38e7b | 848 | { |
db895a1e | 849 | ISADevice *isadev = ISA_DEVICE(dev); |
0e41271e | 850 | RTCState *s = MC146818_RTC(dev); |
32e0c826 | 851 | int base = 0x70; |
80cabfad | 852 | |
80cabfad FB |
853 | s->cmos_data[RTC_REG_A] = 0x26; |
854 | s->cmos_data[RTC_REG_B] = 0x02; | |
855 | s->cmos_data[RTC_REG_C] = 0x00; | |
856 | s->cmos_data[RTC_REG_D] = 0x80; | |
857 | ||
b8994faf PB |
858 | /* This is for historical reasons. The default base year qdev property |
859 | * was set to 2000 for most machine types before the century byte was | |
860 | * implemented. | |
861 | * | |
862 | * This if statement means that the century byte will be always 0 | |
863 | * (at least until 2079...) for base_year = 1980, but will be set | |
864 | * correctly for base_year = 2000. | |
865 | */ | |
866 | if (s->base_year == 2000) { | |
867 | s->base_year = 0; | |
868 | } | |
869 | ||
db895a1e | 870 | rtc_set_date_from_host(isadev); |
ea55ffb3 | 871 | |
93b66569 | 872 | #ifdef TARGET_I386 |
433acf0d | 873 | switch (s->lost_tick_policy) { |
104059da | 874 | case LOST_TICK_POLICY_SLEW: |
6875204c | 875 | s->coalesced_timer = |
884f17c2 | 876 | timer_new_ns(rtc_clock, rtc_coalesced_timer, s); |
433acf0d | 877 | break; |
104059da | 878 | case LOST_TICK_POLICY_DISCARD: |
433acf0d JK |
879 | break; |
880 | default: | |
db895a1e AF |
881 | error_setg(errp, "Invalid lost tick policy."); |
882 | return; | |
433acf0d | 883 | } |
93b66569 | 884 | #endif |
433acf0d | 885 | |
884f17c2 AB |
886 | s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s); |
887 | s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s); | |
56038ef6 | 888 | check_update_timer(s); |
dff38e7b | 889 | |
17604dac | 890 | s->clock_reset_notifier.notify = rtc_notify_clock_reset; |
13c0cbae | 891 | qemu_clock_register_reset_notifier(rtc_clock, |
884f17c2 | 892 | &s->clock_reset_notifier); |
17604dac | 893 | |
da98c8eb GH |
894 | s->suspend_notifier.notify = rtc_notify_suspend; |
895 | qemu_register_suspend_notifier(&s->suspend_notifier); | |
896 | ||
853dca12 | 897 | memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2); |
db895a1e | 898 | isa_register_ioport(isadev, &s->io, base); |
dff38e7b | 899 | |
db895a1e | 900 | qdev_set_legacy_instance_id(dev, base, 3); |
a08d4367 | 901 | qemu_register_reset(rtc_reset, s); |
18297050 | 902 | |
8e099d14 | 903 | object_property_add_tm(OBJECT(s), "date", rtc_get_date, NULL); |
654a36d8 MT |
904 | |
905 | object_property_add_alias(qdev_get_machine(), "rtc-time", | |
906 | OBJECT(s), "date", NULL); | |
32e0c826 GH |
907 | } |
908 | ||
48a18b3c | 909 | ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq) |
32e0c826 | 910 | { |
0e41271e AF |
911 | DeviceState *dev; |
912 | ISADevice *isadev; | |
7d932dfd | 913 | RTCState *s; |
eeb7c03c | 914 | |
0e41271e AF |
915 | isadev = isa_create(bus, TYPE_MC146818_RTC); |
916 | dev = DEVICE(isadev); | |
917 | s = MC146818_RTC(isadev); | |
918 | qdev_prop_set_int32(dev, "base_year", base_year); | |
919 | qdev_init_nofail(dev); | |
7d932dfd JK |
920 | if (intercept_irq) { |
921 | s->irq = intercept_irq; | |
922 | } else { | |
0e41271e | 923 | isa_init_irq(isadev, &s->irq, RTC_ISA_IRQ); |
7d932dfd | 924 | } |
f2ae8abf MT |
925 | QLIST_INSERT_HEAD(&rtc_devices, s, link); |
926 | ||
0e41271e | 927 | return isadev; |
80cabfad FB |
928 | } |
929 | ||
39bffca2 AL |
930 | static Property mc146818rtc_properties[] = { |
931 | DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980), | |
932 | DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState, | |
104059da | 933 | lost_tick_policy, LOST_TICK_POLICY_DISCARD), |
39bffca2 AL |
934 | DEFINE_PROP_END_OF_LIST(), |
935 | }; | |
936 | ||
8f04ee08 AL |
937 | static void rtc_class_initfn(ObjectClass *klass, void *data) |
938 | { | |
39bffca2 | 939 | DeviceClass *dc = DEVICE_CLASS(klass); |
db895a1e AF |
940 | |
941 | dc->realize = rtc_realizefn; | |
39bffca2 AL |
942 | dc->vmsd = &vmstate_rtc; |
943 | dc->props = mc146818rtc_properties; | |
f3b17640 MA |
944 | /* Reason: needs to be wired up by rtc_init() */ |
945 | dc->cannot_instantiate_with_device_add_yet = true; | |
8f04ee08 AL |
946 | } |
947 | ||
654a36d8 MT |
948 | static void rtc_finalize(Object *obj) |
949 | { | |
950 | object_property_del(qdev_get_machine(), "rtc", NULL); | |
951 | } | |
952 | ||
8c43a6f0 | 953 | static const TypeInfo mc146818rtc_info = { |
0e41271e | 954 | .name = TYPE_MC146818_RTC, |
39bffca2 AL |
955 | .parent = TYPE_ISA_DEVICE, |
956 | .instance_size = sizeof(RTCState), | |
957 | .class_init = rtc_class_initfn, | |
654a36d8 | 958 | .instance_finalize = rtc_finalize, |
32e0c826 GH |
959 | }; |
960 | ||
83f7d43a | 961 | static void mc146818rtc_register_types(void) |
100d9891 | 962 | { |
39bffca2 | 963 | type_register_static(&mc146818rtc_info); |
100d9891 | 964 | } |
83f7d43a AF |
965 | |
966 | type_init(mc146818rtc_register_types) |