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
24 #include "qemu/osdep.h"
25 #include "qemu/cutils.h"
28 #include "qemu/timer.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/replay.h"
31 #include "hw/timer/mc146818rtc.h"
32 #include "qapi/visitor.h"
33 #include "qapi-event.h"
34 #include "qmp-commands.h"
37 #include "hw/i386/apic.h"
41 //#define DEBUG_COALESCED
44 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
46 # define CMOS_DPRINTF(format, ...) do { } while (0)
49 #ifdef DEBUG_COALESCED
50 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
52 # define DPRINTF_C(format, ...) do { } while (0)
55 #define SEC_PER_MIN 60
56 #define MIN_PER_HOUR 60
57 #define SEC_PER_HOUR 3600
58 #define HOUR_PER_DAY 24
59 #define SEC_PER_DAY 86400
61 #define RTC_REINJECT_ON_ACK_COUNT 20
62 #define RTC_CLOCK_RATE 32768
63 #define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
65 #define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC)
67 typedef struct RTCState
{
71 uint8_t cmos_data
[128];
80 QEMUTimer
*periodic_timer
;
81 int64_t next_periodic_time
;
82 /* update-ended timer */
83 QEMUTimer
*update_timer
;
84 uint64_t next_alarm_time
;
85 uint16_t irq_reinject_on_ack_count
;
86 uint32_t irq_coalesced
;
88 QEMUTimer
*coalesced_timer
;
89 Notifier clock_reset_notifier
;
90 LostTickPolicy lost_tick_policy
;
91 Notifier suspend_notifier
;
92 QLIST_ENTRY(RTCState
) link
;
95 static void rtc_set_time(RTCState
*s
);
96 static void rtc_update_time(RTCState
*s
);
97 static void rtc_set_cmos(RTCState
*s
, const struct tm
*tm
);
98 static inline int rtc_from_bcd(RTCState
*s
, int a
);
99 static uint64_t get_next_alarm(RTCState
*s
);
101 static inline bool rtc_running(RTCState
*s
)
103 return (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
104 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20);
107 static uint64_t get_guest_rtc_ns(RTCState
*s
)
109 uint64_t guest_clock
= qemu_clock_get_ns(rtc_clock
);
111 return s
->base_rtc
* NANOSECONDS_PER_SECOND
+
112 guest_clock
- s
->last_update
+ s
->offset
;
115 static void rtc_coalesced_timer_update(RTCState
*s
)
117 if (s
->irq_coalesced
== 0) {
118 timer_del(s
->coalesced_timer
);
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 periodic_clock_to_ns(s
->period
/ c
);
124 timer_mod(s
->coalesced_timer
, next_clock
);
128 static QLIST_HEAD(, RTCState
) rtc_devices
=
129 QLIST_HEAD_INITIALIZER(rtc_devices
);
132 void qmp_rtc_reset_reinjection(Error
**errp
)
136 QLIST_FOREACH(s
, &rtc_devices
, link
) {
137 s
->irq_coalesced
= 0;
141 static bool rtc_policy_slew_deliver_irq(RTCState
*s
)
143 apic_reset_irq_delivered();
144 qemu_irq_raise(s
->irq
);
145 return apic_get_irq_delivered();
148 static void rtc_coalesced_timer(void *opaque
)
150 RTCState
*s
= opaque
;
152 if (s
->irq_coalesced
!= 0) {
153 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
154 DPRINTF_C("cmos: injecting from timer\n");
155 if (rtc_policy_slew_deliver_irq(s
)) {
157 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
162 rtc_coalesced_timer_update(s
);
165 static bool rtc_policy_slew_deliver_irq(RTCState
*s
)
172 static uint32_t rtc_periodic_clock_ticks(RTCState
*s
)
176 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
)) {
180 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
182 return periodic_period_to_clock(period_code
);
186 * handle periodic timer. @old_period indicates the periodic timer update
187 * is just due to period adjustment.
190 periodic_timer_update(RTCState
*s
, int64_t current_time
, uint32_t old_period
)
193 int64_t cur_clock
, next_irq_clock
, lost_clock
= 0;
195 period
= rtc_periodic_clock_ticks(s
);
198 /* compute 32 khz clock */
200 muldiv64(current_time
, RTC_CLOCK_RATE
, NANOSECONDS_PER_SECOND
);
203 * if the periodic timer's update is due to period re-configuration,
204 * we should count the clock since last interrupt.
207 int64_t last_periodic_clock
, next_periodic_clock
;
209 next_periodic_clock
= muldiv64(s
->next_periodic_time
,
210 RTC_CLOCK_RATE
, NANOSECONDS_PER_SECOND
);
211 last_periodic_clock
= next_periodic_clock
- old_period
;
212 lost_clock
= cur_clock
- last_periodic_clock
;
213 assert(lost_clock
>= 0);
217 * s->irq_coalesced can change for two reasons:
219 * a) if one or more periodic timer interrupts have been lost,
220 * lost_clock will be more that a period.
222 * b) when the period may be reconfigured, we expect the OS to
223 * treat delayed tick as the new period. So, when switching
224 * from a shorter to a longer period, scale down the missing,
225 * because the OS will treat past delayed ticks as longer
226 * (leftovers are put back into lost_clock). When switching
227 * to a shorter period, scale up the missing ticks since the
228 * OS handler will treat past delayed ticks as shorter.
230 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
231 uint32_t old_irq_coalesced
= s
->irq_coalesced
;
234 lost_clock
+= old_irq_coalesced
* old_period
;
235 s
->irq_coalesced
= lost_clock
/ s
->period
;
236 lost_clock
%= s
->period
;
237 if (old_irq_coalesced
!= s
->irq_coalesced
||
238 old_period
!= s
->period
) {
239 DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
240 "period scaled from %d to %d\n", old_irq_coalesced
,
241 s
->irq_coalesced
, old_period
, s
->period
);
242 rtc_coalesced_timer_update(s
);
246 * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
247 * is not used, we should make the time progress anyway.
249 lost_clock
= MIN(lost_clock
, period
);
252 assert(lost_clock
>= 0 && lost_clock
<= period
);
254 next_irq_clock
= cur_clock
+ period
- lost_clock
;
255 s
->next_periodic_time
= periodic_clock_to_ns(next_irq_clock
) + 1;
256 timer_mod(s
->periodic_timer
, s
->next_periodic_time
);
258 s
->irq_coalesced
= 0;
259 timer_del(s
->periodic_timer
);
263 static void rtc_periodic_timer(void *opaque
)
265 RTCState
*s
= opaque
;
267 periodic_timer_update(s
, s
->next_periodic_time
, 0);
268 s
->cmos_data
[RTC_REG_C
] |= REG_C_PF
;
269 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
270 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
271 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
272 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
273 s
->irq_reinject_on_ack_count
= 0;
274 if (!rtc_policy_slew_deliver_irq(s
)) {
276 rtc_coalesced_timer_update(s
);
277 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
281 qemu_irq_raise(s
->irq
);
285 /* handle update-ended timer */
286 static void check_update_timer(RTCState
*s
)
288 uint64_t next_update_time
;
292 /* From the data sheet: "Holding the dividers in reset prevents
293 * interrupts from operating, while setting the SET bit allows"
296 if ((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) {
297 timer_del(s
->update_timer
);
301 guest_nsec
= get_guest_rtc_ns(s
) % NANOSECONDS_PER_SECOND
;
302 next_update_time
= qemu_clock_get_ns(rtc_clock
)
303 + NANOSECONDS_PER_SECOND
- guest_nsec
;
305 /* Compute time of next alarm. One second is already accounted
306 * for in next_update_time.
308 next_alarm_sec
= get_next_alarm(s
);
309 s
->next_alarm_time
= next_update_time
+
310 (next_alarm_sec
- 1) * NANOSECONDS_PER_SECOND
;
312 /* If UF is already set, we might be able to optimize. */
313 if (s
->cmos_data
[RTC_REG_C
] & REG_C_UF
) {
314 /* If AF cannot change (i.e. either it is set already, or
315 * SET=1 and then the time is not updated), nothing to do.
317 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) ||
318 (s
->cmos_data
[RTC_REG_C
] & REG_C_AF
)) {
319 timer_del(s
->update_timer
);
323 /* UF is set, but AF is clear. Program the timer to target
325 next_update_time
= s
->next_alarm_time
;
327 if (next_update_time
!= timer_expire_time_ns(s
->update_timer
)) {
328 timer_mod(s
->update_timer
, next_update_time
);
332 static inline uint8_t convert_hour(RTCState
*s
, uint8_t hour
)
334 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
336 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
343 static uint64_t get_next_alarm(RTCState
*s
)
345 int32_t alarm_sec
, alarm_min
, alarm_hour
, cur_hour
, cur_min
, cur_sec
;
346 int32_t hour
, min
, sec
;
350 alarm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS_ALARM
]);
351 alarm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES_ALARM
]);
352 alarm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS_ALARM
]);
353 alarm_hour
= alarm_hour
== -1 ? -1 : convert_hour(s
, alarm_hour
);
355 cur_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
356 cur_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
357 cur_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
]);
358 cur_hour
= convert_hour(s
, cur_hour
);
360 if (alarm_hour
== -1) {
361 alarm_hour
= cur_hour
;
362 if (alarm_min
== -1) {
364 if (alarm_sec
== -1) {
365 alarm_sec
= cur_sec
+ 1;
366 } else if (cur_sec
> alarm_sec
) {
369 } else if (cur_min
== alarm_min
) {
370 if (alarm_sec
== -1) {
371 alarm_sec
= cur_sec
+ 1;
373 if (cur_sec
> alarm_sec
) {
377 if (alarm_sec
== SEC_PER_MIN
) {
378 /* wrap to next hour, minutes is not in don't care mode */
382 } else if (cur_min
> alarm_min
) {
385 } else if (cur_hour
== alarm_hour
) {
386 if (alarm_min
== -1) {
388 if (alarm_sec
== -1) {
389 alarm_sec
= cur_sec
+ 1;
390 } else if (cur_sec
> alarm_sec
) {
394 if (alarm_sec
== SEC_PER_MIN
) {
398 /* wrap to next day, hour is not in don't care mode */
399 alarm_min
%= MIN_PER_HOUR
;
400 } else if (cur_min
== alarm_min
) {
401 if (alarm_sec
== -1) {
402 alarm_sec
= cur_sec
+ 1;
404 /* wrap to next day, hours+minutes not in don't care mode */
405 alarm_sec
%= SEC_PER_MIN
;
409 /* values that are still don't care fire at the next min/sec */
410 if (alarm_min
== -1) {
413 if (alarm_sec
== -1) {
417 /* keep values in range */
418 if (alarm_sec
== SEC_PER_MIN
) {
422 if (alarm_min
== MIN_PER_HOUR
) {
426 alarm_hour
%= HOUR_PER_DAY
;
428 hour
= alarm_hour
- cur_hour
;
429 min
= hour
* MIN_PER_HOUR
+ alarm_min
- cur_min
;
430 sec
= min
* SEC_PER_MIN
+ alarm_sec
- cur_sec
;
431 return sec
<= 0 ? sec
+ SEC_PER_DAY
: sec
;
434 static void rtc_update_timer(void *opaque
)
436 RTCState
*s
= opaque
;
437 int32_t irqs
= REG_C_UF
;
440 assert((s
->cmos_data
[RTC_REG_A
] & 0x60) != 0x60);
442 /* UIP might have been latched, update time and clear it. */
444 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
446 if (qemu_clock_get_ns(rtc_clock
) >= s
->next_alarm_time
) {
448 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
449 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC
);
453 new_irqs
= irqs
& ~s
->cmos_data
[RTC_REG_C
];
454 s
->cmos_data
[RTC_REG_C
] |= irqs
;
455 if ((new_irqs
& s
->cmos_data
[RTC_REG_B
]) != 0) {
456 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
457 qemu_irq_raise(s
->irq
);
459 check_update_timer(s
);
462 static void cmos_ioport_write(void *opaque
, hwaddr addr
,
463 uint64_t data
, unsigned size
)
465 RTCState
*s
= opaque
;
467 bool update_periodic_timer
;
469 if ((addr
& 1) == 0) {
470 s
->cmos_index
= data
& 0x7f;
472 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64
"\n",
473 s
->cmos_index
, data
);
474 switch(s
->cmos_index
) {
475 case RTC_SECONDS_ALARM
:
476 case RTC_MINUTES_ALARM
:
477 case RTC_HOURS_ALARM
:
478 s
->cmos_data
[s
->cmos_index
] = data
;
479 check_update_timer(s
);
481 case RTC_IBM_PS2_CENTURY_BYTE
:
482 s
->cmos_index
= RTC_CENTURY
;
488 case RTC_DAY_OF_WEEK
:
489 case RTC_DAY_OF_MONTH
:
492 s
->cmos_data
[s
->cmos_index
] = data
;
493 /* if in set mode, do not update the time */
494 if (rtc_running(s
)) {
496 check_update_timer(s
);
500 update_periodic_timer
= (s
->cmos_data
[RTC_REG_A
] ^ data
) & 0x0f;
501 old_period
= rtc_periodic_clock_ticks(s
);
503 if ((data
& 0x60) == 0x60) {
504 if (rtc_running(s
)) {
507 /* What happens to UIP when divider reset is enabled is
508 * unclear from the datasheet. Shouldn't matter much
511 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
512 } else if (((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) &&
513 (data
& 0x70) <= 0x20) {
514 /* when the divider reset is removed, the first update cycle
515 * begins one-half second later*/
516 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
517 s
->offset
= 500000000;
520 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
522 /* UIP bit is read only */
523 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
524 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
526 if (update_periodic_timer
) {
527 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
),
531 check_update_timer(s
);
534 update_periodic_timer
= (s
->cmos_data
[RTC_REG_B
] ^ data
)
536 old_period
= rtc_periodic_clock_ticks(s
);
538 if (data
& REG_B_SET
) {
539 /* update cmos to when the rtc was stopping */
540 if (rtc_running(s
)) {
543 /* set mode: reset UIP mode */
544 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
547 /* if disabling set mode, update the time */
548 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
549 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20) {
550 s
->offset
= get_guest_rtc_ns(s
) % NANOSECONDS_PER_SECOND
;
554 /* if an interrupt flag is already set when the interrupt
555 * becomes enabled, raise an interrupt immediately. */
556 if (data
& s
->cmos_data
[RTC_REG_C
] & REG_C_MASK
) {
557 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
558 qemu_irq_raise(s
->irq
);
560 s
->cmos_data
[RTC_REG_C
] &= ~REG_C_IRQF
;
561 qemu_irq_lower(s
->irq
);
563 s
->cmos_data
[RTC_REG_B
] = data
;
565 if (update_periodic_timer
) {
566 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
),
570 check_update_timer(s
);
574 /* cannot write to them */
577 s
->cmos_data
[s
->cmos_index
] = data
;
583 static inline int rtc_to_bcd(RTCState
*s
, int a
)
585 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
588 return ((a
/ 10) << 4) | (a
% 10);
592 static inline int rtc_from_bcd(RTCState
*s
, int a
)
594 if ((a
& 0xc0) == 0xc0) {
597 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
600 return ((a
>> 4) * 10) + (a
& 0x0f);
604 static void rtc_get_time(RTCState
*s
, struct tm
*tm
)
606 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
607 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
608 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
609 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
611 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
615 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
616 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
617 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
619 rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
+
620 rtc_from_bcd(s
, s
->cmos_data
[RTC_CENTURY
]) * 100 - 1900;
623 static void rtc_set_time(RTCState
*s
)
627 rtc_get_time(s
, &tm
);
628 s
->base_rtc
= mktimegm(&tm
);
629 s
->last_update
= qemu_clock_get_ns(rtc_clock
);
631 qapi_event_send_rtc_change(qemu_timedate_diff(&tm
), &error_abort
);
634 static void rtc_set_cmos(RTCState
*s
, const struct tm
*tm
)
638 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
639 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
640 if (s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) {
642 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
645 int h
= (tm
->tm_hour
% 12) ? tm
->tm_hour
% 12 : 12;
646 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, h
);
647 if (tm
->tm_hour
>= 12)
648 s
->cmos_data
[RTC_HOURS
] |= 0x80;
650 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
651 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
652 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
653 year
= tm
->tm_year
+ 1900 - s
->base_year
;
654 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
% 100);
655 s
->cmos_data
[RTC_CENTURY
] = rtc_to_bcd(s
, year
/ 100);
658 static void rtc_update_time(RTCState
*s
)
664 guest_nsec
= get_guest_rtc_ns(s
);
665 guest_sec
= guest_nsec
/ NANOSECONDS_PER_SECOND
;
666 gmtime_r(&guest_sec
, &ret
);
668 /* Is SET flag of Register B disabled? */
669 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) == 0) {
670 rtc_set_cmos(s
, &ret
);
674 static int update_in_progress(RTCState
*s
)
678 if (!rtc_running(s
)) {
681 if (timer_pending(s
->update_timer
)) {
682 int64_t next_update_time
= timer_expire_time_ns(s
->update_timer
);
683 /* Latch UIP until the timer expires. */
684 if (qemu_clock_get_ns(rtc_clock
) >=
685 (next_update_time
- UIP_HOLD_LENGTH
)) {
686 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
691 guest_nsec
= get_guest_rtc_ns(s
);
692 /* UIP bit will be set at last 244us of every second. */
693 if ((guest_nsec
% NANOSECONDS_PER_SECOND
) >=
694 (NANOSECONDS_PER_SECOND
- UIP_HOLD_LENGTH
)) {
700 static uint64_t cmos_ioport_read(void *opaque
, hwaddr addr
,
703 RTCState
*s
= opaque
;
705 if ((addr
& 1) == 0) {
708 switch(s
->cmos_index
) {
709 case RTC_IBM_PS2_CENTURY_BYTE
:
710 s
->cmos_index
= RTC_CENTURY
;
716 case RTC_DAY_OF_WEEK
:
717 case RTC_DAY_OF_MONTH
:
720 /* if not in set mode, calibrate cmos before
722 if (rtc_running(s
)) {
725 ret
= s
->cmos_data
[s
->cmos_index
];
728 if (update_in_progress(s
)) {
729 s
->cmos_data
[s
->cmos_index
] |= REG_A_UIP
;
731 s
->cmos_data
[s
->cmos_index
] &= ~REG_A_UIP
;
733 ret
= s
->cmos_data
[s
->cmos_index
];
736 ret
= s
->cmos_data
[s
->cmos_index
];
737 qemu_irq_lower(s
->irq
);
738 s
->cmos_data
[RTC_REG_C
] = 0x00;
739 if (ret
& (REG_C_UF
| REG_C_AF
)) {
740 check_update_timer(s
);
743 if(s
->irq_coalesced
&&
744 (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) &&
745 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
746 s
->irq_reinject_on_ack_count
++;
747 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
| REG_C_PF
;
748 DPRINTF_C("cmos: injecting on ack\n");
749 if (rtc_policy_slew_deliver_irq(s
)) {
751 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
757 ret
= s
->cmos_data
[s
->cmos_index
];
760 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
766 void rtc_set_memory(ISADevice
*dev
, int addr
, int val
)
768 RTCState
*s
= MC146818_RTC(dev
);
769 if (addr
>= 0 && addr
<= 127)
770 s
->cmos_data
[addr
] = val
;
773 int rtc_get_memory(ISADevice
*dev
, int addr
)
775 RTCState
*s
= MC146818_RTC(dev
);
776 assert(addr
>= 0 && addr
<= 127);
777 return s
->cmos_data
[addr
];
780 static void rtc_set_date_from_host(ISADevice
*dev
)
782 RTCState
*s
= MC146818_RTC(dev
);
785 qemu_get_timedate(&tm
, 0);
787 s
->base_rtc
= mktimegm(&tm
);
788 s
->last_update
= qemu_clock_get_ns(rtc_clock
);
791 /* set the CMOS date */
792 rtc_set_cmos(s
, &tm
);
795 static void rtc_pre_save(void *opaque
)
797 RTCState
*s
= opaque
;
802 static int rtc_post_load(void *opaque
, int version_id
)
804 RTCState
*s
= opaque
;
806 if (version_id
<= 2 || rtc_clock
== QEMU_CLOCK_REALTIME
) {
809 check_update_timer(s
);
812 /* The periodic timer is deterministic in record/replay mode,
813 * so there is no need to update it after loading the vmstate.
814 * Reading RTC here would misalign record and replay.
816 if (replay_mode
== REPLAY_MODE_NONE
) {
817 uint64_t now
= qemu_clock_get_ns(rtc_clock
);
818 if (now
< s
->next_periodic_time
||
819 now
> (s
->next_periodic_time
+ get_max_clock_jump())) {
820 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
), 0);
824 if (version_id
>= 2) {
825 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
826 rtc_coalesced_timer_update(s
);
832 static bool rtc_irq_reinject_on_ack_count_needed(void *opaque
)
834 RTCState
*s
= (RTCState
*)opaque
;
835 return s
->irq_reinject_on_ack_count
!= 0;
838 static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count
= {
839 .name
= "mc146818rtc/irq_reinject_on_ack_count",
841 .minimum_version_id
= 1,
842 .needed
= rtc_irq_reinject_on_ack_count_needed
,
843 .fields
= (VMStateField
[]) {
844 VMSTATE_UINT16(irq_reinject_on_ack_count
, RTCState
),
845 VMSTATE_END_OF_LIST()
849 static const VMStateDescription vmstate_rtc
= {
850 .name
= "mc146818rtc",
852 .minimum_version_id
= 1,
853 .pre_save
= rtc_pre_save
,
854 .post_load
= rtc_post_load
,
855 .fields
= (VMStateField
[]) {
856 VMSTATE_BUFFER(cmos_data
, RTCState
),
857 VMSTATE_UINT8(cmos_index
, RTCState
),
859 VMSTATE_TIMER_PTR(periodic_timer
, RTCState
),
860 VMSTATE_INT64(next_periodic_time
, RTCState
),
862 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
863 VMSTATE_UINT32_V(period
, RTCState
, 2),
864 VMSTATE_UINT64_V(base_rtc
, RTCState
, 3),
865 VMSTATE_UINT64_V(last_update
, RTCState
, 3),
866 VMSTATE_INT64_V(offset
, RTCState
, 3),
867 VMSTATE_TIMER_PTR_V(update_timer
, RTCState
, 3),
868 VMSTATE_UINT64_V(next_alarm_time
, RTCState
, 3),
869 VMSTATE_END_OF_LIST()
871 .subsections
= (const VMStateDescription
*[]) {
872 &vmstate_rtc_irq_reinject_on_ack_count
,
877 static void rtc_notify_clock_reset(Notifier
*notifier
, void *data
)
879 RTCState
*s
= container_of(notifier
, RTCState
, clock_reset_notifier
);
880 int64_t now
= *(int64_t *)data
;
882 rtc_set_date_from_host(ISA_DEVICE(s
));
883 periodic_timer_update(s
, now
, 0);
884 check_update_timer(s
);
886 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
887 rtc_coalesced_timer_update(s
);
891 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
892 BIOS will read it and start S3 resume at POST Entry */
893 static void rtc_notify_suspend(Notifier
*notifier
, void *data
)
895 RTCState
*s
= container_of(notifier
, RTCState
, suspend_notifier
);
896 rtc_set_memory(ISA_DEVICE(s
), 0xF, 0xFE);
899 static void rtc_reset(void *opaque
)
901 RTCState
*s
= opaque
;
903 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
904 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
905 check_update_timer(s
);
907 qemu_irq_lower(s
->irq
);
909 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
910 s
->irq_coalesced
= 0;
911 s
->irq_reinject_on_ack_count
= 0;
915 static const MemoryRegionOps cmos_ops
= {
916 .read
= cmos_ioport_read
,
917 .write
= cmos_ioport_write
,
919 .min_access_size
= 1,
920 .max_access_size
= 1,
922 .endianness
= DEVICE_LITTLE_ENDIAN
,
925 static void rtc_get_date(Object
*obj
, struct tm
*current_tm
, Error
**errp
)
927 RTCState
*s
= MC146818_RTC(obj
);
930 rtc_get_time(s
, current_tm
);
933 static void rtc_realizefn(DeviceState
*dev
, Error
**errp
)
935 ISADevice
*isadev
= ISA_DEVICE(dev
);
936 RTCState
*s
= MC146818_RTC(dev
);
939 s
->cmos_data
[RTC_REG_A
] = 0x26;
940 s
->cmos_data
[RTC_REG_B
] = 0x02;
941 s
->cmos_data
[RTC_REG_C
] = 0x00;
942 s
->cmos_data
[RTC_REG_D
] = 0x80;
944 /* This is for historical reasons. The default base year qdev property
945 * was set to 2000 for most machine types before the century byte was
948 * This if statement means that the century byte will be always 0
949 * (at least until 2079...) for base_year = 1980, but will be set
950 * correctly for base_year = 2000.
952 if (s
->base_year
== 2000) {
956 rtc_set_date_from_host(isadev
);
958 switch (s
->lost_tick_policy
) {
960 case LOST_TICK_POLICY_SLEW
:
962 timer_new_ns(rtc_clock
, rtc_coalesced_timer
, s
);
965 case LOST_TICK_POLICY_DISCARD
:
968 error_setg(errp
, "Invalid lost tick policy.");
972 s
->periodic_timer
= timer_new_ns(rtc_clock
, rtc_periodic_timer
, s
);
973 s
->update_timer
= timer_new_ns(rtc_clock
, rtc_update_timer
, s
);
974 check_update_timer(s
);
976 s
->clock_reset_notifier
.notify
= rtc_notify_clock_reset
;
977 qemu_clock_register_reset_notifier(rtc_clock
,
978 &s
->clock_reset_notifier
);
980 s
->suspend_notifier
.notify
= rtc_notify_suspend
;
981 qemu_register_suspend_notifier(&s
->suspend_notifier
);
983 memory_region_init_io(&s
->io
, OBJECT(s
), &cmos_ops
, s
, "rtc", 2);
984 isa_register_ioport(isadev
, &s
->io
, base
);
986 qdev_set_legacy_instance_id(dev
, base
, 3);
987 qemu_register_reset(rtc_reset
, s
);
989 object_property_add_tm(OBJECT(s
), "date", rtc_get_date
, NULL
);
991 object_property_add_alias(qdev_get_machine(), "rtc-time",
992 OBJECT(s
), "date", NULL
);
994 qdev_init_gpio_out(dev
, &s
->irq
, 1);
997 ISADevice
*rtc_init(ISABus
*bus
, int base_year
, qemu_irq intercept_irq
)
1003 isadev
= isa_create(bus
, TYPE_MC146818_RTC
);
1004 dev
= DEVICE(isadev
);
1005 s
= MC146818_RTC(isadev
);
1006 qdev_prop_set_int32(dev
, "base_year", base_year
);
1007 qdev_init_nofail(dev
);
1008 if (intercept_irq
) {
1009 qdev_connect_gpio_out(dev
, 0, intercept_irq
);
1011 isa_connect_gpio_out(isadev
, 0, RTC_ISA_IRQ
);
1013 QLIST_INSERT_HEAD(&rtc_devices
, s
, link
);
1018 static Property mc146818rtc_properties
[] = {
1019 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
1020 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState
,
1021 lost_tick_policy
, LOST_TICK_POLICY_DISCARD
),
1022 DEFINE_PROP_END_OF_LIST(),
1025 static void rtc_resetdev(DeviceState
*d
)
1027 RTCState
*s
= MC146818_RTC(d
);
1029 /* Reason: VM do suspend self will set 0xfe
1030 * Reset any values other than 0xfe(Guest suspend case) */
1031 if (s
->cmos_data
[0x0f] != 0xfe) {
1032 s
->cmos_data
[0x0f] = 0x00;
1036 static void rtc_class_initfn(ObjectClass
*klass
, void *data
)
1038 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1040 dc
->realize
= rtc_realizefn
;
1041 dc
->reset
= rtc_resetdev
;
1042 dc
->vmsd
= &vmstate_rtc
;
1043 dc
->props
= mc146818rtc_properties
;
1044 /* Reason: needs to be wired up by rtc_init() */
1045 dc
->user_creatable
= false;
1048 static void rtc_finalize(Object
*obj
)
1050 object_property_del(qdev_get_machine(), "rtc", NULL
);
1053 static const TypeInfo mc146818rtc_info
= {
1054 .name
= TYPE_MC146818_RTC
,
1055 .parent
= TYPE_ISA_DEVICE
,
1056 .instance_size
= sizeof(RTCState
),
1057 .class_init
= rtc_class_initfn
,
1058 .instance_finalize
= rtc_finalize
,
1061 static void mc146818rtc_register_types(void)
1063 type_register_static(&mc146818rtc_info
);
1066 type_init(mc146818rtc_register_types
)