iotests.py: Allow concurrent qemu instances
[qemu/ar7.git] / hw / timer / mc146818rtc.c
blobda209d02f08489033a094b083da5edaefae30292
1 /*
2 * QEMU MC146818 RTC emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "qemu/cutils.h"
26 #include "qemu/bcd.h"
27 #include "hw/hw.h"
28 #include "qemu/timer.h"
29 #include "sysemu/sysemu.h"
30 #include "hw/timer/mc146818rtc.h"
31 #include "qapi/visitor.h"
32 #include "qapi-event.h"
33 #include "qmp-commands.h"
35 #ifdef TARGET_I386
36 #include "hw/i386/apic.h"
37 #endif
39 //#define DEBUG_CMOS
40 //#define DEBUG_COALESCED
42 #ifdef DEBUG_CMOS
43 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
44 #else
45 # define CMOS_DPRINTF(format, ...) do { } while (0)
46 #endif
48 #ifdef DEBUG_COALESCED
49 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
50 #else
51 # define DPRINTF_C(format, ...) do { } while (0)
52 #endif
54 #define SEC_PER_MIN 60
55 #define MIN_PER_HOUR 60
56 #define SEC_PER_HOUR 3600
57 #define HOUR_PER_DAY 24
58 #define SEC_PER_DAY 86400
60 #define RTC_REINJECT_ON_ACK_COUNT 20
61 #define RTC_CLOCK_RATE 32768
62 #define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
64 #define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC)
66 typedef struct RTCState {
67 ISADevice parent_obj;
69 MemoryRegion io;
70 uint8_t cmos_data[128];
71 uint8_t cmos_index;
72 int32_t base_year;
73 uint64_t base_rtc;
74 uint64_t last_update;
75 int64_t offset;
76 qemu_irq irq;
77 int it_shift;
78 /* periodic timer */
79 QEMUTimer *periodic_timer;
80 int64_t next_periodic_time;
81 /* update-ended timer */
82 QEMUTimer *update_timer;
83 uint64_t next_alarm_time;
84 uint16_t irq_reinject_on_ack_count;
85 uint32_t irq_coalesced;
86 uint32_t period;
87 QEMUTimer *coalesced_timer;
88 Notifier clock_reset_notifier;
89 LostTickPolicy lost_tick_policy;
90 Notifier suspend_notifier;
91 QLIST_ENTRY(RTCState) link;
92 } RTCState;
94 static void rtc_set_time(RTCState *s);
95 static void rtc_update_time(RTCState *s);
96 static void rtc_set_cmos(RTCState *s, const struct tm *tm);
97 static inline int rtc_from_bcd(RTCState *s, int a);
98 static uint64_t get_next_alarm(RTCState *s);
100 static inline bool rtc_running(RTCState *s)
102 return (!(s->cmos_data[RTC_REG_B] & REG_B_SET) &&
103 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20);
106 static uint64_t get_guest_rtc_ns(RTCState *s)
108 uint64_t guest_clock = qemu_clock_get_ns(rtc_clock);
110 return s->base_rtc * NANOSECONDS_PER_SECOND +
111 guest_clock - s->last_update + s->offset;
114 #ifdef TARGET_I386
115 static void rtc_coalesced_timer_update(RTCState *s)
117 if (s->irq_coalesced == 0) {
118 timer_del(s->coalesced_timer);
119 } else {
120 /* divide each RTC interval to 2 - 8 smaller intervals */
121 int c = MIN(s->irq_coalesced, 7) + 1;
122 int64_t next_clock = qemu_clock_get_ns(rtc_clock) +
123 muldiv64(s->period / c, NANOSECONDS_PER_SECOND, RTC_CLOCK_RATE);
124 timer_mod(s->coalesced_timer, next_clock);
128 static void rtc_coalesced_timer(void *opaque)
130 RTCState *s = opaque;
132 if (s->irq_coalesced != 0) {
133 apic_reset_irq_delivered();
134 s->cmos_data[RTC_REG_C] |= 0xc0;
135 DPRINTF_C("cmos: injecting from timer\n");
136 qemu_irq_raise(s->irq);
137 if (apic_get_irq_delivered()) {
138 s->irq_coalesced--;
139 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
140 s->irq_coalesced);
144 rtc_coalesced_timer_update(s);
146 #endif
148 /* handle periodic timer */
149 static void periodic_timer_update(RTCState *s, int64_t current_time)
151 int period_code, period;
152 int64_t cur_clock, next_irq_clock;
154 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
155 if (period_code != 0
156 && (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
157 if (period_code <= 2)
158 period_code += 7;
159 /* period in 32 Khz cycles */
160 period = 1 << (period_code - 1);
161 #ifdef TARGET_I386
162 if (period != s->period) {
163 s->irq_coalesced = (s->irq_coalesced * s->period) / period;
164 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced);
166 s->period = period;
167 #endif
168 /* compute 32 khz clock */
169 cur_clock =
170 muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
172 next_irq_clock = (cur_clock & ~(period - 1)) + period;
173 s->next_periodic_time = muldiv64(next_irq_clock, NANOSECONDS_PER_SECOND,
174 RTC_CLOCK_RATE) + 1;
175 timer_mod(s->periodic_timer, s->next_periodic_time);
176 } else {
177 #ifdef TARGET_I386
178 s->irq_coalesced = 0;
179 #endif
180 timer_del(s->periodic_timer);
184 static void rtc_periodic_timer(void *opaque)
186 RTCState *s = opaque;
188 periodic_timer_update(s, s->next_periodic_time);
189 s->cmos_data[RTC_REG_C] |= REG_C_PF;
190 if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
191 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
192 #ifdef TARGET_I386
193 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
194 if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
195 s->irq_reinject_on_ack_count = 0;
196 apic_reset_irq_delivered();
197 qemu_irq_raise(s->irq);
198 if (!apic_get_irq_delivered()) {
199 s->irq_coalesced++;
200 rtc_coalesced_timer_update(s);
201 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
202 s->irq_coalesced);
204 } else
205 #endif
206 qemu_irq_raise(s->irq);
210 /* handle update-ended timer */
211 static void check_update_timer(RTCState *s)
213 uint64_t next_update_time;
214 uint64_t guest_nsec;
215 int next_alarm_sec;
217 /* From the data sheet: "Holding the dividers in reset prevents
218 * interrupts from operating, while setting the SET bit allows"
219 * them to occur. However, it will prevent an alarm interrupt
220 * from occurring, because the time of day is not updated.
222 if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) {
223 timer_del(s->update_timer);
224 return;
226 if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
227 (s->cmos_data[RTC_REG_B] & REG_B_SET)) {
228 timer_del(s->update_timer);
229 return;
231 if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
232 (s->cmos_data[RTC_REG_C] & REG_C_AF)) {
233 timer_del(s->update_timer);
234 return;
237 guest_nsec = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
238 /* if UF is clear, reprogram to next second */
239 next_update_time = qemu_clock_get_ns(rtc_clock)
240 + NANOSECONDS_PER_SECOND - guest_nsec;
242 /* Compute time of next alarm. One second is already accounted
243 * for in next_update_time.
245 next_alarm_sec = get_next_alarm(s);
246 s->next_alarm_time = next_update_time +
247 (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND;
249 if (s->cmos_data[RTC_REG_C] & REG_C_UF) {
250 /* UF is set, but AF is clear. Program the timer to target
251 * the alarm time. */
252 next_update_time = s->next_alarm_time;
254 if (next_update_time != timer_expire_time_ns(s->update_timer)) {
255 timer_mod(s->update_timer, next_update_time);
259 static inline uint8_t convert_hour(RTCState *s, uint8_t hour)
261 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
262 hour %= 12;
263 if (s->cmos_data[RTC_HOURS] & 0x80) {
264 hour += 12;
267 return hour;
270 static uint64_t get_next_alarm(RTCState *s)
272 int32_t alarm_sec, alarm_min, alarm_hour, cur_hour, cur_min, cur_sec;
273 int32_t hour, min, sec;
275 rtc_update_time(s);
277 alarm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]);
278 alarm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]);
279 alarm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]);
280 alarm_hour = alarm_hour == -1 ? -1 : convert_hour(s, alarm_hour);
282 cur_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
283 cur_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
284 cur_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS]);
285 cur_hour = convert_hour(s, cur_hour);
287 if (alarm_hour == -1) {
288 alarm_hour = cur_hour;
289 if (alarm_min == -1) {
290 alarm_min = cur_min;
291 if (alarm_sec == -1) {
292 alarm_sec = cur_sec + 1;
293 } else if (cur_sec > alarm_sec) {
294 alarm_min++;
296 } else if (cur_min == alarm_min) {
297 if (alarm_sec == -1) {
298 alarm_sec = cur_sec + 1;
299 } else {
300 if (cur_sec > alarm_sec) {
301 alarm_hour++;
304 if (alarm_sec == SEC_PER_MIN) {
305 /* wrap to next hour, minutes is not in don't care mode */
306 alarm_sec = 0;
307 alarm_hour++;
309 } else if (cur_min > alarm_min) {
310 alarm_hour++;
312 } else if (cur_hour == alarm_hour) {
313 if (alarm_min == -1) {
314 alarm_min = cur_min;
315 if (alarm_sec == -1) {
316 alarm_sec = cur_sec + 1;
317 } else if (cur_sec > alarm_sec) {
318 alarm_min++;
321 if (alarm_sec == SEC_PER_MIN) {
322 alarm_sec = 0;
323 alarm_min++;
325 /* wrap to next day, hour is not in don't care mode */
326 alarm_min %= MIN_PER_HOUR;
327 } else if (cur_min == alarm_min) {
328 if (alarm_sec == -1) {
329 alarm_sec = cur_sec + 1;
331 /* wrap to next day, hours+minutes not in don't care mode */
332 alarm_sec %= SEC_PER_MIN;
336 /* values that are still don't care fire at the next min/sec */
337 if (alarm_min == -1) {
338 alarm_min = 0;
340 if (alarm_sec == -1) {
341 alarm_sec = 0;
344 /* keep values in range */
345 if (alarm_sec == SEC_PER_MIN) {
346 alarm_sec = 0;
347 alarm_min++;
349 if (alarm_min == MIN_PER_HOUR) {
350 alarm_min = 0;
351 alarm_hour++;
353 alarm_hour %= HOUR_PER_DAY;
355 hour = alarm_hour - cur_hour;
356 min = hour * MIN_PER_HOUR + alarm_min - cur_min;
357 sec = min * SEC_PER_MIN + alarm_sec - cur_sec;
358 return sec <= 0 ? sec + SEC_PER_DAY : sec;
361 static void rtc_update_timer(void *opaque)
363 RTCState *s = opaque;
364 int32_t irqs = REG_C_UF;
365 int32_t new_irqs;
367 assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60);
369 /* UIP might have been latched, update time and clear it. */
370 rtc_update_time(s);
371 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
373 if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) {
374 irqs |= REG_C_AF;
375 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
376 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC);
380 new_irqs = irqs & ~s->cmos_data[RTC_REG_C];
381 s->cmos_data[RTC_REG_C] |= irqs;
382 if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
383 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
384 qemu_irq_raise(s->irq);
386 check_update_timer(s);
389 static void cmos_ioport_write(void *opaque, hwaddr addr,
390 uint64_t data, unsigned size)
392 RTCState *s = opaque;
394 if ((addr & 1) == 0) {
395 s->cmos_index = data & 0x7f;
396 } else {
397 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64 "\n",
398 s->cmos_index, data);
399 switch(s->cmos_index) {
400 case RTC_SECONDS_ALARM:
401 case RTC_MINUTES_ALARM:
402 case RTC_HOURS_ALARM:
403 s->cmos_data[s->cmos_index] = data;
404 check_update_timer(s);
405 break;
406 case RTC_IBM_PS2_CENTURY_BYTE:
407 s->cmos_index = RTC_CENTURY;
408 /* fall through */
409 case RTC_CENTURY:
410 case RTC_SECONDS:
411 case RTC_MINUTES:
412 case RTC_HOURS:
413 case RTC_DAY_OF_WEEK:
414 case RTC_DAY_OF_MONTH:
415 case RTC_MONTH:
416 case RTC_YEAR:
417 s->cmos_data[s->cmos_index] = data;
418 /* if in set mode, do not update the time */
419 if (rtc_running(s)) {
420 rtc_set_time(s);
421 check_update_timer(s);
423 break;
424 case RTC_REG_A:
425 if ((data & 0x60) == 0x60) {
426 if (rtc_running(s)) {
427 rtc_update_time(s);
429 /* What happens to UIP when divider reset is enabled is
430 * unclear from the datasheet. Shouldn't matter much
431 * though.
433 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
434 } else if (((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) &&
435 (data & 0x70) <= 0x20) {
436 /* when the divider reset is removed, the first update cycle
437 * begins one-half second later*/
438 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
439 s->offset = 500000000;
440 rtc_set_time(s);
442 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
444 /* UIP bit is read only */
445 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
446 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
447 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock));
448 check_update_timer(s);
449 break;
450 case RTC_REG_B:
451 if (data & REG_B_SET) {
452 /* update cmos to when the rtc was stopping */
453 if (rtc_running(s)) {
454 rtc_update_time(s);
456 /* set mode: reset UIP mode */
457 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
458 data &= ~REG_B_UIE;
459 } else {
460 /* if disabling set mode, update the time */
461 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) &&
462 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20) {
463 s->offset = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
464 rtc_set_time(s);
467 /* if an interrupt flag is already set when the interrupt
468 * becomes enabled, raise an interrupt immediately. */
469 if (data & s->cmos_data[RTC_REG_C] & REG_C_MASK) {
470 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
471 qemu_irq_raise(s->irq);
472 } else {
473 s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF;
474 qemu_irq_lower(s->irq);
476 s->cmos_data[RTC_REG_B] = data;
477 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock));
478 check_update_timer(s);
479 break;
480 case RTC_REG_C:
481 case RTC_REG_D:
482 /* cannot write to them */
483 break;
484 default:
485 s->cmos_data[s->cmos_index] = data;
486 break;
491 static inline int rtc_to_bcd(RTCState *s, int a)
493 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
494 return a;
495 } else {
496 return ((a / 10) << 4) | (a % 10);
500 static inline int rtc_from_bcd(RTCState *s, int a)
502 if ((a & 0xc0) == 0xc0) {
503 return -1;
505 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
506 return a;
507 } else {
508 return ((a >> 4) * 10) + (a & 0x0f);
512 static void rtc_get_time(RTCState *s, struct tm *tm)
514 tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
515 tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
516 tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
517 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
518 tm->tm_hour %= 12;
519 if (s->cmos_data[RTC_HOURS] & 0x80) {
520 tm->tm_hour += 12;
523 tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
524 tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
525 tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
526 tm->tm_year =
527 rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year +
528 rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900;
531 static QLIST_HEAD(, RTCState) rtc_devices =
532 QLIST_HEAD_INITIALIZER(rtc_devices);
534 #ifdef TARGET_I386
535 void qmp_rtc_reset_reinjection(Error **errp)
537 RTCState *s;
539 QLIST_FOREACH(s, &rtc_devices, link) {
540 s->irq_coalesced = 0;
543 #endif
545 static void rtc_set_time(RTCState *s)
547 struct tm tm;
549 rtc_get_time(s, &tm);
550 s->base_rtc = mktimegm(&tm);
551 s->last_update = qemu_clock_get_ns(rtc_clock);
553 qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
556 static void rtc_set_cmos(RTCState *s, const struct tm *tm)
558 int year;
560 s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
561 s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
562 if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
563 /* 24 hour format */
564 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
565 } else {
566 /* 12 hour format */
567 int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
568 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
569 if (tm->tm_hour >= 12)
570 s->cmos_data[RTC_HOURS] |= 0x80;
572 s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
573 s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
574 s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
575 year = tm->tm_year + 1900 - s->base_year;
576 s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100);
577 s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100);
580 static void rtc_update_time(RTCState *s)
582 struct tm ret;
583 time_t guest_sec;
584 int64_t guest_nsec;
586 guest_nsec = get_guest_rtc_ns(s);
587 guest_sec = guest_nsec / NANOSECONDS_PER_SECOND;
588 gmtime_r(&guest_sec, &ret);
590 /* Is SET flag of Register B disabled? */
591 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) {
592 rtc_set_cmos(s, &ret);
596 static int update_in_progress(RTCState *s)
598 int64_t guest_nsec;
600 if (!rtc_running(s)) {
601 return 0;
603 if (timer_pending(s->update_timer)) {
604 int64_t next_update_time = timer_expire_time_ns(s->update_timer);
605 /* Latch UIP until the timer expires. */
606 if (qemu_clock_get_ns(rtc_clock) >=
607 (next_update_time - UIP_HOLD_LENGTH)) {
608 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
609 return 1;
613 guest_nsec = get_guest_rtc_ns(s);
614 /* UIP bit will be set at last 244us of every second. */
615 if ((guest_nsec % NANOSECONDS_PER_SECOND) >=
616 (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) {
617 return 1;
619 return 0;
622 static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
623 unsigned size)
625 RTCState *s = opaque;
626 int ret;
627 if ((addr & 1) == 0) {
628 return 0xff;
629 } else {
630 switch(s->cmos_index) {
631 case RTC_IBM_PS2_CENTURY_BYTE:
632 s->cmos_index = RTC_CENTURY;
633 /* fall through */
634 case RTC_CENTURY:
635 case RTC_SECONDS:
636 case RTC_MINUTES:
637 case RTC_HOURS:
638 case RTC_DAY_OF_WEEK:
639 case RTC_DAY_OF_MONTH:
640 case RTC_MONTH:
641 case RTC_YEAR:
642 /* if not in set mode, calibrate cmos before
643 * reading*/
644 if (rtc_running(s)) {
645 rtc_update_time(s);
647 ret = s->cmos_data[s->cmos_index];
648 break;
649 case RTC_REG_A:
650 if (update_in_progress(s)) {
651 s->cmos_data[s->cmos_index] |= REG_A_UIP;
652 } else {
653 s->cmos_data[s->cmos_index] &= ~REG_A_UIP;
655 ret = s->cmos_data[s->cmos_index];
656 break;
657 case RTC_REG_C:
658 ret = s->cmos_data[s->cmos_index];
659 qemu_irq_lower(s->irq);
660 s->cmos_data[RTC_REG_C] = 0x00;
661 if (ret & (REG_C_UF | REG_C_AF)) {
662 check_update_timer(s);
664 #ifdef TARGET_I386
665 if(s->irq_coalesced &&
666 (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
667 s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
668 s->irq_reinject_on_ack_count++;
669 s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
670 apic_reset_irq_delivered();
671 DPRINTF_C("cmos: injecting on ack\n");
672 qemu_irq_raise(s->irq);
673 if (apic_get_irq_delivered()) {
674 s->irq_coalesced--;
675 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
676 s->irq_coalesced);
679 #endif
680 break;
681 default:
682 ret = s->cmos_data[s->cmos_index];
683 break;
685 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
686 s->cmos_index, ret);
687 return ret;
691 void rtc_set_memory(ISADevice *dev, int addr, int val)
693 RTCState *s = MC146818_RTC(dev);
694 if (addr >= 0 && addr <= 127)
695 s->cmos_data[addr] = val;
698 int rtc_get_memory(ISADevice *dev, int addr)
700 RTCState *s = MC146818_RTC(dev);
701 assert(addr >= 0 && addr <= 127);
702 return s->cmos_data[addr];
705 static void rtc_set_date_from_host(ISADevice *dev)
707 RTCState *s = MC146818_RTC(dev);
708 struct tm tm;
710 qemu_get_timedate(&tm, 0);
712 s->base_rtc = mktimegm(&tm);
713 s->last_update = qemu_clock_get_ns(rtc_clock);
714 s->offset = 0;
716 /* set the CMOS date */
717 rtc_set_cmos(s, &tm);
720 static void rtc_pre_save(void *opaque)
722 RTCState *s = opaque;
724 rtc_update_time(s);
727 static int rtc_post_load(void *opaque, int version_id)
729 RTCState *s = opaque;
731 if (version_id <= 2 || rtc_clock == QEMU_CLOCK_REALTIME) {
732 rtc_set_time(s);
733 s->offset = 0;
734 check_update_timer(s);
737 uint64_t now = qemu_clock_get_ns(rtc_clock);
738 if (now < s->next_periodic_time ||
739 now > (s->next_periodic_time + get_max_clock_jump())) {
740 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock));
743 #ifdef TARGET_I386
744 if (version_id >= 2) {
745 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
746 rtc_coalesced_timer_update(s);
749 #endif
750 return 0;
753 static bool rtc_irq_reinject_on_ack_count_needed(void *opaque)
755 RTCState *s = (RTCState *)opaque;
756 return s->irq_reinject_on_ack_count != 0;
759 static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count = {
760 .name = "mc146818rtc/irq_reinject_on_ack_count",
761 .version_id = 1,
762 .minimum_version_id = 1,
763 .needed = rtc_irq_reinject_on_ack_count_needed,
764 .fields = (VMStateField[]) {
765 VMSTATE_UINT16(irq_reinject_on_ack_count, RTCState),
766 VMSTATE_END_OF_LIST()
770 static const VMStateDescription vmstate_rtc = {
771 .name = "mc146818rtc",
772 .version_id = 3,
773 .minimum_version_id = 1,
774 .pre_save = rtc_pre_save,
775 .post_load = rtc_post_load,
776 .fields = (VMStateField[]) {
777 VMSTATE_BUFFER(cmos_data, RTCState),
778 VMSTATE_UINT8(cmos_index, RTCState),
779 VMSTATE_UNUSED(7*4),
780 VMSTATE_TIMER_PTR(periodic_timer, RTCState),
781 VMSTATE_INT64(next_periodic_time, RTCState),
782 VMSTATE_UNUSED(3*8),
783 VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
784 VMSTATE_UINT32_V(period, RTCState, 2),
785 VMSTATE_UINT64_V(base_rtc, RTCState, 3),
786 VMSTATE_UINT64_V(last_update, RTCState, 3),
787 VMSTATE_INT64_V(offset, RTCState, 3),
788 VMSTATE_TIMER_PTR_V(update_timer, RTCState, 3),
789 VMSTATE_UINT64_V(next_alarm_time, RTCState, 3),
790 VMSTATE_END_OF_LIST()
792 .subsections = (const VMStateDescription*[]) {
793 &vmstate_rtc_irq_reinject_on_ack_count,
794 NULL
798 static void rtc_notify_clock_reset(Notifier *notifier, void *data)
800 RTCState *s = container_of(notifier, RTCState, clock_reset_notifier);
801 int64_t now = *(int64_t *)data;
803 rtc_set_date_from_host(ISA_DEVICE(s));
804 periodic_timer_update(s, now);
805 check_update_timer(s);
806 #ifdef TARGET_I386
807 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
808 rtc_coalesced_timer_update(s);
810 #endif
813 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
814 BIOS will read it and start S3 resume at POST Entry */
815 static void rtc_notify_suspend(Notifier *notifier, void *data)
817 RTCState *s = container_of(notifier, RTCState, suspend_notifier);
818 rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE);
821 static void rtc_reset(void *opaque)
823 RTCState *s = opaque;
825 s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
826 s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
827 check_update_timer(s);
829 qemu_irq_lower(s->irq);
831 #ifdef TARGET_I386
832 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
833 s->irq_coalesced = 0;
834 s->irq_reinject_on_ack_count = 0;
836 #endif
839 static const MemoryRegionOps cmos_ops = {
840 .read = cmos_ioport_read,
841 .write = cmos_ioport_write,
842 .impl = {
843 .min_access_size = 1,
844 .max_access_size = 1,
846 .endianness = DEVICE_LITTLE_ENDIAN,
849 static void rtc_get_date(Object *obj, struct tm *current_tm, Error **errp)
851 RTCState *s = MC146818_RTC(obj);
853 rtc_update_time(s);
854 rtc_get_time(s, current_tm);
857 static void rtc_realizefn(DeviceState *dev, Error **errp)
859 ISADevice *isadev = ISA_DEVICE(dev);
860 RTCState *s = MC146818_RTC(dev);
861 int base = 0x70;
863 s->cmos_data[RTC_REG_A] = 0x26;
864 s->cmos_data[RTC_REG_B] = 0x02;
865 s->cmos_data[RTC_REG_C] = 0x00;
866 s->cmos_data[RTC_REG_D] = 0x80;
868 /* This is for historical reasons. The default base year qdev property
869 * was set to 2000 for most machine types before the century byte was
870 * implemented.
872 * This if statement means that the century byte will be always 0
873 * (at least until 2079...) for base_year = 1980, but will be set
874 * correctly for base_year = 2000.
876 if (s->base_year == 2000) {
877 s->base_year = 0;
880 rtc_set_date_from_host(isadev);
882 #ifdef TARGET_I386
883 switch (s->lost_tick_policy) {
884 case LOST_TICK_POLICY_SLEW:
885 s->coalesced_timer =
886 timer_new_ns(rtc_clock, rtc_coalesced_timer, s);
887 break;
888 case LOST_TICK_POLICY_DISCARD:
889 break;
890 default:
891 error_setg(errp, "Invalid lost tick policy.");
892 return;
894 #endif
896 s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s);
897 s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s);
898 check_update_timer(s);
900 s->clock_reset_notifier.notify = rtc_notify_clock_reset;
901 qemu_clock_register_reset_notifier(rtc_clock,
902 &s->clock_reset_notifier);
904 s->suspend_notifier.notify = rtc_notify_suspend;
905 qemu_register_suspend_notifier(&s->suspend_notifier);
907 memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2);
908 isa_register_ioport(isadev, &s->io, base);
910 qdev_set_legacy_instance_id(dev, base, 3);
911 qemu_register_reset(rtc_reset, s);
913 object_property_add_tm(OBJECT(s), "date", rtc_get_date, NULL);
915 object_property_add_alias(qdev_get_machine(), "rtc-time",
916 OBJECT(s), "date", NULL);
918 qdev_init_gpio_out(dev, &s->irq, 1);
921 ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
923 DeviceState *dev;
924 ISADevice *isadev;
925 RTCState *s;
927 isadev = isa_create(bus, TYPE_MC146818_RTC);
928 dev = DEVICE(isadev);
929 s = MC146818_RTC(isadev);
930 qdev_prop_set_int32(dev, "base_year", base_year);
931 qdev_init_nofail(dev);
932 if (intercept_irq) {
933 qdev_connect_gpio_out(dev, 0, intercept_irq);
934 } else {
935 isa_connect_gpio_out(isadev, 0, RTC_ISA_IRQ);
937 QLIST_INSERT_HEAD(&rtc_devices, s, link);
939 return isadev;
942 static Property mc146818rtc_properties[] = {
943 DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
944 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
945 lost_tick_policy, LOST_TICK_POLICY_DISCARD),
946 DEFINE_PROP_END_OF_LIST(),
949 static void rtc_class_initfn(ObjectClass *klass, void *data)
951 DeviceClass *dc = DEVICE_CLASS(klass);
953 dc->realize = rtc_realizefn;
954 dc->vmsd = &vmstate_rtc;
955 dc->props = mc146818rtc_properties;
956 /* Reason: needs to be wired up by rtc_init() */
957 dc->cannot_instantiate_with_device_add_yet = true;
960 static void rtc_finalize(Object *obj)
962 object_property_del(qdev_get_machine(), "rtc", NULL);
965 static const TypeInfo mc146818rtc_info = {
966 .name = TYPE_MC146818_RTC,
967 .parent = TYPE_ISA_DEVICE,
968 .instance_size = sizeof(RTCState),
969 .class_init = rtc_class_initfn,
970 .instance_finalize = rtc_finalize,
973 static void mc146818rtc_register_types(void)
975 type_register_static(&mc146818rtc_info);
978 type_init(mc146818rtc_register_types)