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