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
25 #include "qemu/osdep.h"
26 #include "qemu/cutils.h"
29 #include "qemu/timer.h"
30 #include "sysemu/sysemu.h"
31 #include "sysemu/replay.h"
32 #include "hw/timer/mc146818rtc.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-commands-target.h"
35 #include "qapi/qapi-events-target.h"
36 #include "qapi/visitor.h"
37 #include "exec/address-spaces.h"
40 #include "hw/i386/apic.h"
44 //#define DEBUG_COALESCED
47 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
49 # define CMOS_DPRINTF(format, ...) do { } while (0)
52 #ifdef DEBUG_COALESCED
53 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
55 # define DPRINTF_C(format, ...) do { } while (0)
58 #define SEC_PER_MIN 60
59 #define MIN_PER_HOUR 60
60 #define SEC_PER_HOUR 3600
61 #define HOUR_PER_DAY 24
62 #define SEC_PER_DAY 86400
64 #define RTC_REINJECT_ON_ACK_COUNT 20
65 #define RTC_CLOCK_RATE 32768
66 #define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
68 #define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC)
70 typedef struct RTCState
{
74 MemoryRegion coalesced_io
;
75 uint8_t cmos_data
[128];
84 QEMUTimer
*periodic_timer
;
85 int64_t next_periodic_time
;
86 /* update-ended timer */
87 QEMUTimer
*update_timer
;
88 uint64_t next_alarm_time
;
89 uint16_t irq_reinject_on_ack_count
;
90 uint32_t irq_coalesced
;
92 QEMUTimer
*coalesced_timer
;
93 Notifier clock_reset_notifier
;
94 LostTickPolicy lost_tick_policy
;
95 Notifier suspend_notifier
;
96 QLIST_ENTRY(RTCState
) link
;
99 static void rtc_set_time(RTCState
*s
);
100 static void rtc_update_time(RTCState
*s
);
101 static void rtc_set_cmos(RTCState
*s
, const struct tm
*tm
);
102 static inline int rtc_from_bcd(RTCState
*s
, int a
);
103 static uint64_t get_next_alarm(RTCState
*s
);
105 static inline bool rtc_running(RTCState
*s
)
107 return (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
108 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20);
111 static uint64_t get_guest_rtc_ns(RTCState
*s
)
113 uint64_t guest_clock
= qemu_clock_get_ns(rtc_clock
);
115 return s
->base_rtc
* NANOSECONDS_PER_SECOND
+
116 guest_clock
- s
->last_update
+ s
->offset
;
119 static void rtc_coalesced_timer_update(RTCState
*s
)
121 if (s
->irq_coalesced
== 0) {
122 timer_del(s
->coalesced_timer
);
124 /* divide each RTC interval to 2 - 8 smaller intervals */
125 int c
= MIN(s
->irq_coalesced
, 7) + 1;
126 int64_t next_clock
= qemu_clock_get_ns(rtc_clock
) +
127 periodic_clock_to_ns(s
->period
/ c
);
128 timer_mod(s
->coalesced_timer
, next_clock
);
132 static QLIST_HEAD(, RTCState
) rtc_devices
=
133 QLIST_HEAD_INITIALIZER(rtc_devices
);
136 void qmp_rtc_reset_reinjection(Error
**errp
)
140 QLIST_FOREACH(s
, &rtc_devices
, link
) {
141 s
->irq_coalesced
= 0;
145 static bool rtc_policy_slew_deliver_irq(RTCState
*s
)
147 apic_reset_irq_delivered();
148 qemu_irq_raise(s
->irq
);
149 return apic_get_irq_delivered();
152 static void rtc_coalesced_timer(void *opaque
)
154 RTCState
*s
= opaque
;
156 if (s
->irq_coalesced
!= 0) {
157 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
158 DPRINTF_C("cmos: injecting from timer\n");
159 if (rtc_policy_slew_deliver_irq(s
)) {
161 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
166 rtc_coalesced_timer_update(s
);
169 static bool rtc_policy_slew_deliver_irq(RTCState
*s
)
176 static uint32_t rtc_periodic_clock_ticks(RTCState
*s
)
180 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
)) {
184 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
186 return periodic_period_to_clock(period_code
);
190 * handle periodic timer. @old_period indicates the periodic timer update
191 * is just due to period adjustment.
194 periodic_timer_update(RTCState
*s
, int64_t current_time
, uint32_t old_period
)
197 int64_t cur_clock
, next_irq_clock
, lost_clock
= 0;
199 period
= rtc_periodic_clock_ticks(s
);
202 /* compute 32 khz clock */
204 muldiv64(current_time
, RTC_CLOCK_RATE
, NANOSECONDS_PER_SECOND
);
207 * if the periodic timer's update is due to period re-configuration,
208 * we should count the clock since last interrupt.
211 int64_t last_periodic_clock
, next_periodic_clock
;
213 next_periodic_clock
= muldiv64(s
->next_periodic_time
,
214 RTC_CLOCK_RATE
, NANOSECONDS_PER_SECOND
);
215 last_periodic_clock
= next_periodic_clock
- old_period
;
216 lost_clock
= cur_clock
- last_periodic_clock
;
217 assert(lost_clock
>= 0);
221 * s->irq_coalesced can change for two reasons:
223 * a) if one or more periodic timer interrupts have been lost,
224 * lost_clock will be more that a period.
226 * b) when the period may be reconfigured, we expect the OS to
227 * treat delayed tick as the new period. So, when switching
228 * from a shorter to a longer period, scale down the missing,
229 * because the OS will treat past delayed ticks as longer
230 * (leftovers are put back into lost_clock). When switching
231 * to a shorter period, scale up the missing ticks since the
232 * OS handler will treat past delayed ticks as shorter.
234 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
235 uint32_t old_irq_coalesced
= s
->irq_coalesced
;
238 lost_clock
+= old_irq_coalesced
* old_period
;
239 s
->irq_coalesced
= lost_clock
/ s
->period
;
240 lost_clock
%= s
->period
;
241 if (old_irq_coalesced
!= s
->irq_coalesced
||
242 old_period
!= s
->period
) {
243 DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
244 "period scaled from %d to %d\n", old_irq_coalesced
,
245 s
->irq_coalesced
, old_period
, s
->period
);
246 rtc_coalesced_timer_update(s
);
250 * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
251 * is not used, we should make the time progress anyway.
253 lost_clock
= MIN(lost_clock
, period
);
256 assert(lost_clock
>= 0 && lost_clock
<= period
);
258 next_irq_clock
= cur_clock
+ period
- lost_clock
;
259 s
->next_periodic_time
= periodic_clock_to_ns(next_irq_clock
) + 1;
260 timer_mod(s
->periodic_timer
, s
->next_periodic_time
);
262 s
->irq_coalesced
= 0;
263 timer_del(s
->periodic_timer
);
267 static void rtc_periodic_timer(void *opaque
)
269 RTCState
*s
= opaque
;
271 periodic_timer_update(s
, s
->next_periodic_time
, 0);
272 s
->cmos_data
[RTC_REG_C
] |= REG_C_PF
;
273 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
274 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
275 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
276 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
277 s
->irq_reinject_on_ack_count
= 0;
278 if (!rtc_policy_slew_deliver_irq(s
)) {
280 rtc_coalesced_timer_update(s
);
281 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
285 qemu_irq_raise(s
->irq
);
289 /* handle update-ended timer */
290 static void check_update_timer(RTCState
*s
)
292 uint64_t next_update_time
;
296 /* From the data sheet: "Holding the dividers in reset prevents
297 * interrupts from operating, while setting the SET bit allows"
300 if ((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) {
301 assert((s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
) == 0);
302 timer_del(s
->update_timer
);
306 guest_nsec
= get_guest_rtc_ns(s
) % NANOSECONDS_PER_SECOND
;
307 next_update_time
= qemu_clock_get_ns(rtc_clock
)
308 + NANOSECONDS_PER_SECOND
- guest_nsec
;
310 /* Compute time of next alarm. One second is already accounted
311 * for in next_update_time.
313 next_alarm_sec
= get_next_alarm(s
);
314 s
->next_alarm_time
= next_update_time
+
315 (next_alarm_sec
- 1) * NANOSECONDS_PER_SECOND
;
317 /* If update_in_progress latched the UIP bit, we must keep the timer
318 * programmed to the next second, so that UIP is cleared. Otherwise,
319 * if UF is already set, we might be able to optimize.
321 if (!(s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
) &&
322 (s
->cmos_data
[RTC_REG_C
] & REG_C_UF
)) {
323 /* If AF cannot change (i.e. either it is set already, or
324 * SET=1 and then the time is not updated), nothing to do.
326 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) ||
327 (s
->cmos_data
[RTC_REG_C
] & REG_C_AF
)) {
328 timer_del(s
->update_timer
);
332 /* UF is set, but AF is clear. Program the timer to target
334 next_update_time
= s
->next_alarm_time
;
336 if (next_update_time
!= timer_expire_time_ns(s
->update_timer
)) {
337 timer_mod(s
->update_timer
, next_update_time
);
341 static inline uint8_t convert_hour(RTCState
*s
, uint8_t hour
)
343 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
345 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
352 static uint64_t get_next_alarm(RTCState
*s
)
354 int32_t alarm_sec
, alarm_min
, alarm_hour
, cur_hour
, cur_min
, cur_sec
;
355 int32_t hour
, min
, sec
;
359 alarm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS_ALARM
]);
360 alarm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES_ALARM
]);
361 alarm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS_ALARM
]);
362 alarm_hour
= alarm_hour
== -1 ? -1 : convert_hour(s
, alarm_hour
);
364 cur_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
365 cur_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
366 cur_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
]);
367 cur_hour
= convert_hour(s
, cur_hour
);
369 if (alarm_hour
== -1) {
370 alarm_hour
= cur_hour
;
371 if (alarm_min
== -1) {
373 if (alarm_sec
== -1) {
374 alarm_sec
= cur_sec
+ 1;
375 } else if (cur_sec
> alarm_sec
) {
378 } else if (cur_min
== alarm_min
) {
379 if (alarm_sec
== -1) {
380 alarm_sec
= cur_sec
+ 1;
382 if (cur_sec
> alarm_sec
) {
386 if (alarm_sec
== SEC_PER_MIN
) {
387 /* wrap to next hour, minutes is not in don't care mode */
391 } else if (cur_min
> alarm_min
) {
394 } else if (cur_hour
== alarm_hour
) {
395 if (alarm_min
== -1) {
397 if (alarm_sec
== -1) {
398 alarm_sec
= cur_sec
+ 1;
399 } else if (cur_sec
> alarm_sec
) {
403 if (alarm_sec
== SEC_PER_MIN
) {
407 /* wrap to next day, hour is not in don't care mode */
408 alarm_min
%= MIN_PER_HOUR
;
409 } else if (cur_min
== alarm_min
) {
410 if (alarm_sec
== -1) {
411 alarm_sec
= cur_sec
+ 1;
413 /* wrap to next day, hours+minutes not in don't care mode */
414 alarm_sec
%= SEC_PER_MIN
;
418 /* values that are still don't care fire at the next min/sec */
419 if (alarm_min
== -1) {
422 if (alarm_sec
== -1) {
426 /* keep values in range */
427 if (alarm_sec
== SEC_PER_MIN
) {
431 if (alarm_min
== MIN_PER_HOUR
) {
435 alarm_hour
%= HOUR_PER_DAY
;
437 hour
= alarm_hour
- cur_hour
;
438 min
= hour
* MIN_PER_HOUR
+ alarm_min
- cur_min
;
439 sec
= min
* SEC_PER_MIN
+ alarm_sec
- cur_sec
;
440 return sec
<= 0 ? sec
+ SEC_PER_DAY
: sec
;
443 static void rtc_update_timer(void *opaque
)
445 RTCState
*s
= opaque
;
446 int32_t irqs
= REG_C_UF
;
449 assert((s
->cmos_data
[RTC_REG_A
] & 0x60) != 0x60);
451 /* UIP might have been latched, update time and clear it. */
453 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
455 if (qemu_clock_get_ns(rtc_clock
) >= s
->next_alarm_time
) {
457 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
458 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC
, NULL
);
462 new_irqs
= irqs
& ~s
->cmos_data
[RTC_REG_C
];
463 s
->cmos_data
[RTC_REG_C
] |= irqs
;
464 if ((new_irqs
& s
->cmos_data
[RTC_REG_B
]) != 0) {
465 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
466 qemu_irq_raise(s
->irq
);
468 check_update_timer(s
);
471 static void cmos_ioport_write(void *opaque
, hwaddr addr
,
472 uint64_t data
, unsigned size
)
474 RTCState
*s
= opaque
;
476 bool update_periodic_timer
;
478 if ((addr
& 1) == 0) {
479 s
->cmos_index
= data
& 0x7f;
481 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64
"\n",
482 s
->cmos_index
, data
);
483 switch(s
->cmos_index
) {
484 case RTC_SECONDS_ALARM
:
485 case RTC_MINUTES_ALARM
:
486 case RTC_HOURS_ALARM
:
487 s
->cmos_data
[s
->cmos_index
] = data
;
488 check_update_timer(s
);
490 case RTC_IBM_PS2_CENTURY_BYTE
:
491 s
->cmos_index
= RTC_CENTURY
;
497 case RTC_DAY_OF_WEEK
:
498 case RTC_DAY_OF_MONTH
:
501 s
->cmos_data
[s
->cmos_index
] = data
;
502 /* if in set mode, do not update the time */
503 if (rtc_running(s
)) {
505 check_update_timer(s
);
509 update_periodic_timer
= (s
->cmos_data
[RTC_REG_A
] ^ data
) & 0x0f;
510 old_period
= rtc_periodic_clock_ticks(s
);
512 if ((data
& 0x60) == 0x60) {
513 if (rtc_running(s
)) {
516 /* What happens to UIP when divider reset is enabled is
517 * unclear from the datasheet. Shouldn't matter much
520 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
521 } else if (((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) &&
522 (data
& 0x70) <= 0x20) {
523 /* when the divider reset is removed, the first update cycle
524 * begins one-half second later*/
525 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
526 s
->offset
= 500000000;
529 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
531 /* UIP bit is read only */
532 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
533 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
535 if (update_periodic_timer
) {
536 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
),
540 check_update_timer(s
);
543 update_periodic_timer
= (s
->cmos_data
[RTC_REG_B
] ^ data
)
545 old_period
= rtc_periodic_clock_ticks(s
);
547 if (data
& REG_B_SET
) {
548 /* update cmos to when the rtc was stopping */
549 if (rtc_running(s
)) {
552 /* set mode: reset UIP mode */
553 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
556 /* if disabling set mode, update the time */
557 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
558 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20) {
559 s
->offset
= get_guest_rtc_ns(s
) % NANOSECONDS_PER_SECOND
;
563 /* if an interrupt flag is already set when the interrupt
564 * becomes enabled, raise an interrupt immediately. */
565 if (data
& s
->cmos_data
[RTC_REG_C
] & REG_C_MASK
) {
566 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
567 qemu_irq_raise(s
->irq
);
569 s
->cmos_data
[RTC_REG_C
] &= ~REG_C_IRQF
;
570 qemu_irq_lower(s
->irq
);
572 s
->cmos_data
[RTC_REG_B
] = data
;
574 if (update_periodic_timer
) {
575 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
),
579 check_update_timer(s
);
583 /* cannot write to them */
586 s
->cmos_data
[s
->cmos_index
] = data
;
592 static inline int rtc_to_bcd(RTCState
*s
, int a
)
594 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
597 return ((a
/ 10) << 4) | (a
% 10);
601 static inline int rtc_from_bcd(RTCState
*s
, int a
)
603 if ((a
& 0xc0) == 0xc0) {
606 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
609 return ((a
>> 4) * 10) + (a
& 0x0f);
613 static void rtc_get_time(RTCState
*s
, struct tm
*tm
)
615 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
616 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
617 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
618 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
620 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
624 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
625 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
626 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
628 rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
+
629 rtc_from_bcd(s
, s
->cmos_data
[RTC_CENTURY
]) * 100 - 1900;
632 static void rtc_set_time(RTCState
*s
)
636 rtc_get_time(s
, &tm
);
637 s
->base_rtc
= mktimegm(&tm
);
638 s
->last_update
= qemu_clock_get_ns(rtc_clock
);
640 qapi_event_send_rtc_change(qemu_timedate_diff(&tm
));
643 static void rtc_set_cmos(RTCState
*s
, const struct tm
*tm
)
647 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
648 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
649 if (s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) {
651 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
654 int h
= (tm
->tm_hour
% 12) ? tm
->tm_hour
% 12 : 12;
655 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, h
);
656 if (tm
->tm_hour
>= 12)
657 s
->cmos_data
[RTC_HOURS
] |= 0x80;
659 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
660 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
661 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
662 year
= tm
->tm_year
+ 1900 - s
->base_year
;
663 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
% 100);
664 s
->cmos_data
[RTC_CENTURY
] = rtc_to_bcd(s
, year
/ 100);
667 static void rtc_update_time(RTCState
*s
)
673 guest_nsec
= get_guest_rtc_ns(s
);
674 guest_sec
= guest_nsec
/ NANOSECONDS_PER_SECOND
;
675 gmtime_r(&guest_sec
, &ret
);
677 /* Is SET flag of Register B disabled? */
678 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) == 0) {
679 rtc_set_cmos(s
, &ret
);
683 static int update_in_progress(RTCState
*s
)
687 if (!rtc_running(s
)) {
690 if (timer_pending(s
->update_timer
)) {
691 int64_t next_update_time
= timer_expire_time_ns(s
->update_timer
);
692 /* Latch UIP until the timer expires. */
693 if (qemu_clock_get_ns(rtc_clock
) >=
694 (next_update_time
- UIP_HOLD_LENGTH
)) {
695 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
700 guest_nsec
= get_guest_rtc_ns(s
);
701 /* UIP bit will be set at last 244us of every second. */
702 if ((guest_nsec
% NANOSECONDS_PER_SECOND
) >=
703 (NANOSECONDS_PER_SECOND
- UIP_HOLD_LENGTH
)) {
709 static uint64_t cmos_ioport_read(void *opaque
, hwaddr addr
,
712 RTCState
*s
= opaque
;
714 if ((addr
& 1) == 0) {
717 switch(s
->cmos_index
) {
718 case RTC_IBM_PS2_CENTURY_BYTE
:
719 s
->cmos_index
= RTC_CENTURY
;
725 case RTC_DAY_OF_WEEK
:
726 case RTC_DAY_OF_MONTH
:
729 /* if not in set mode, calibrate cmos before
731 if (rtc_running(s
)) {
734 ret
= s
->cmos_data
[s
->cmos_index
];
737 ret
= s
->cmos_data
[s
->cmos_index
];
738 if (update_in_progress(s
)) {
743 ret
= s
->cmos_data
[s
->cmos_index
];
744 qemu_irq_lower(s
->irq
);
745 s
->cmos_data
[RTC_REG_C
] = 0x00;
746 if (ret
& (REG_C_UF
| REG_C_AF
)) {
747 check_update_timer(s
);
750 if(s
->irq_coalesced
&&
751 (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) &&
752 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
753 s
->irq_reinject_on_ack_count
++;
754 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
| REG_C_PF
;
755 DPRINTF_C("cmos: injecting on ack\n");
756 if (rtc_policy_slew_deliver_irq(s
)) {
758 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
764 ret
= s
->cmos_data
[s
->cmos_index
];
767 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
773 void rtc_set_memory(ISADevice
*dev
, int addr
, int val
)
775 RTCState
*s
= MC146818_RTC(dev
);
776 if (addr
>= 0 && addr
<= 127)
777 s
->cmos_data
[addr
] = val
;
780 int rtc_get_memory(ISADevice
*dev
, int addr
)
782 RTCState
*s
= MC146818_RTC(dev
);
783 assert(addr
>= 0 && addr
<= 127);
784 return s
->cmos_data
[addr
];
787 static void rtc_set_date_from_host(ISADevice
*dev
)
789 RTCState
*s
= MC146818_RTC(dev
);
792 qemu_get_timedate(&tm
, 0);
794 s
->base_rtc
= mktimegm(&tm
);
795 s
->last_update
= qemu_clock_get_ns(rtc_clock
);
798 /* set the CMOS date */
799 rtc_set_cmos(s
, &tm
);
802 static int rtc_pre_save(void *opaque
)
804 RTCState
*s
= opaque
;
811 static int rtc_post_load(void *opaque
, int version_id
)
813 RTCState
*s
= opaque
;
815 if (version_id
<= 2 || rtc_clock
== QEMU_CLOCK_REALTIME
) {
818 check_update_timer(s
);
821 /* The periodic timer is deterministic in record/replay mode,
822 * so there is no need to update it after loading the vmstate.
823 * Reading RTC here would misalign record and replay.
825 if (replay_mode
== REPLAY_MODE_NONE
) {
826 uint64_t now
= qemu_clock_get_ns(rtc_clock
);
827 if (now
< s
->next_periodic_time
||
828 now
> (s
->next_periodic_time
+ get_max_clock_jump())) {
829 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
), 0);
833 if (version_id
>= 2) {
834 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
835 rtc_coalesced_timer_update(s
);
841 static bool rtc_irq_reinject_on_ack_count_needed(void *opaque
)
843 RTCState
*s
= (RTCState
*)opaque
;
844 return s
->irq_reinject_on_ack_count
!= 0;
847 static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count
= {
848 .name
= "mc146818rtc/irq_reinject_on_ack_count",
850 .minimum_version_id
= 1,
851 .needed
= rtc_irq_reinject_on_ack_count_needed
,
852 .fields
= (VMStateField
[]) {
853 VMSTATE_UINT16(irq_reinject_on_ack_count
, RTCState
),
854 VMSTATE_END_OF_LIST()
858 static const VMStateDescription vmstate_rtc
= {
859 .name
= "mc146818rtc",
861 .minimum_version_id
= 1,
862 .pre_save
= rtc_pre_save
,
863 .post_load
= rtc_post_load
,
864 .fields
= (VMStateField
[]) {
865 VMSTATE_BUFFER(cmos_data
, RTCState
),
866 VMSTATE_UINT8(cmos_index
, RTCState
),
868 VMSTATE_TIMER_PTR(periodic_timer
, RTCState
),
869 VMSTATE_INT64(next_periodic_time
, RTCState
),
871 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
872 VMSTATE_UINT32_V(period
, RTCState
, 2),
873 VMSTATE_UINT64_V(base_rtc
, RTCState
, 3),
874 VMSTATE_UINT64_V(last_update
, RTCState
, 3),
875 VMSTATE_INT64_V(offset
, RTCState
, 3),
876 VMSTATE_TIMER_PTR_V(update_timer
, RTCState
, 3),
877 VMSTATE_UINT64_V(next_alarm_time
, RTCState
, 3),
878 VMSTATE_END_OF_LIST()
880 .subsections
= (const VMStateDescription
*[]) {
881 &vmstate_rtc_irq_reinject_on_ack_count
,
886 static void rtc_notify_clock_reset(Notifier
*notifier
, void *data
)
888 RTCState
*s
= container_of(notifier
, RTCState
, clock_reset_notifier
);
889 int64_t now
= *(int64_t *)data
;
891 rtc_set_date_from_host(ISA_DEVICE(s
));
892 periodic_timer_update(s
, now
, 0);
893 check_update_timer(s
);
895 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
896 rtc_coalesced_timer_update(s
);
900 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
901 BIOS will read it and start S3 resume at POST Entry */
902 static void rtc_notify_suspend(Notifier
*notifier
, void *data
)
904 RTCState
*s
= container_of(notifier
, RTCState
, suspend_notifier
);
905 rtc_set_memory(ISA_DEVICE(s
), 0xF, 0xFE);
908 static void rtc_reset(void *opaque
)
910 RTCState
*s
= opaque
;
912 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
913 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
914 check_update_timer(s
);
916 qemu_irq_lower(s
->irq
);
918 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
919 s
->irq_coalesced
= 0;
920 s
->irq_reinject_on_ack_count
= 0;
924 static const MemoryRegionOps cmos_ops
= {
925 .read
= cmos_ioport_read
,
926 .write
= cmos_ioport_write
,
928 .min_access_size
= 1,
929 .max_access_size
= 1,
931 .endianness
= DEVICE_LITTLE_ENDIAN
,
934 static void rtc_get_date(Object
*obj
, struct tm
*current_tm
, Error
**errp
)
936 RTCState
*s
= MC146818_RTC(obj
);
939 rtc_get_time(s
, current_tm
);
942 static void rtc_realizefn(DeviceState
*dev
, Error
**errp
)
944 ISADevice
*isadev
= ISA_DEVICE(dev
);
945 RTCState
*s
= MC146818_RTC(dev
);
948 s
->cmos_data
[RTC_REG_A
] = 0x26;
949 s
->cmos_data
[RTC_REG_B
] = 0x02;
950 s
->cmos_data
[RTC_REG_C
] = 0x00;
951 s
->cmos_data
[RTC_REG_D
] = 0x80;
953 /* This is for historical reasons. The default base year qdev property
954 * was set to 2000 for most machine types before the century byte was
957 * This if statement means that the century byte will be always 0
958 * (at least until 2079...) for base_year = 1980, but will be set
959 * correctly for base_year = 2000.
961 if (s
->base_year
== 2000) {
965 rtc_set_date_from_host(isadev
);
967 switch (s
->lost_tick_policy
) {
969 case LOST_TICK_POLICY_SLEW
:
971 timer_new_ns(rtc_clock
, rtc_coalesced_timer
, s
);
974 case LOST_TICK_POLICY_DISCARD
:
977 error_setg(errp
, "Invalid lost tick policy.");
981 s
->periodic_timer
= timer_new_ns(rtc_clock
, rtc_periodic_timer
, s
);
982 s
->update_timer
= timer_new_ns(rtc_clock
, rtc_update_timer
, s
);
983 check_update_timer(s
);
985 s
->clock_reset_notifier
.notify
= rtc_notify_clock_reset
;
986 qemu_clock_register_reset_notifier(rtc_clock
,
987 &s
->clock_reset_notifier
);
989 s
->suspend_notifier
.notify
= rtc_notify_suspend
;
990 qemu_register_suspend_notifier(&s
->suspend_notifier
);
992 memory_region_init_io(&s
->io
, OBJECT(s
), &cmos_ops
, s
, "rtc", 2);
993 isa_register_ioport(isadev
, &s
->io
, base
);
995 /* register rtc 0x70 port for coalesced_pio */
996 memory_region_set_flush_coalesced(&s
->io
);
997 memory_region_init_io(&s
->coalesced_io
, OBJECT(s
), &cmos_ops
,
999 memory_region_add_subregion(&s
->io
, 0, &s
->coalesced_io
);
1000 memory_region_add_coalescing(&s
->coalesced_io
, 0, 1);
1002 qdev_set_legacy_instance_id(dev
, base
, 3);
1003 qemu_register_reset(rtc_reset
, s
);
1005 object_property_add_tm(OBJECT(s
), "date", rtc_get_date
, NULL
);
1007 qdev_init_gpio_out(dev
, &s
->irq
, 1);
1010 ISADevice
*mc146818_rtc_init(ISABus
*bus
, int base_year
, qemu_irq intercept_irq
)
1016 isadev
= isa_create(bus
, TYPE_MC146818_RTC
);
1017 dev
= DEVICE(isadev
);
1018 s
= MC146818_RTC(isadev
);
1019 qdev_prop_set_int32(dev
, "base_year", base_year
);
1020 qdev_init_nofail(dev
);
1021 if (intercept_irq
) {
1022 qdev_connect_gpio_out(dev
, 0, intercept_irq
);
1024 isa_connect_gpio_out(isadev
, 0, RTC_ISA_IRQ
);
1026 QLIST_INSERT_HEAD(&rtc_devices
, s
, link
);
1028 object_property_add_alias(qdev_get_machine(), "rtc-time", OBJECT(s
),
1034 static Property mc146818rtc_properties
[] = {
1035 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
1036 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState
,
1037 lost_tick_policy
, LOST_TICK_POLICY_DISCARD
),
1038 DEFINE_PROP_END_OF_LIST(),
1041 static void rtc_resetdev(DeviceState
*d
)
1043 RTCState
*s
= MC146818_RTC(d
);
1045 /* Reason: VM do suspend self will set 0xfe
1046 * Reset any values other than 0xfe(Guest suspend case) */
1047 if (s
->cmos_data
[0x0f] != 0xfe) {
1048 s
->cmos_data
[0x0f] = 0x00;
1052 static void rtc_class_initfn(ObjectClass
*klass
, void *data
)
1054 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1056 dc
->realize
= rtc_realizefn
;
1057 dc
->reset
= rtc_resetdev
;
1058 dc
->vmsd
= &vmstate_rtc
;
1059 dc
->props
= mc146818rtc_properties
;
1060 /* Reason: needs to be wired up by rtc_init() */
1061 dc
->user_creatable
= false;
1064 static const TypeInfo mc146818rtc_info
= {
1065 .name
= TYPE_MC146818_RTC
,
1066 .parent
= TYPE_ISA_DEVICE
,
1067 .instance_size
= sizeof(RTCState
),
1068 .class_init
= rtc_class_initfn
,
1071 static void mc146818rtc_register_types(void)
1073 type_register_static(&mc146818rtc_info
);
1076 type_init(mc146818rtc_register_types
)