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"
27 #include "qemu/module.h"
29 #include "hw/acpi/acpi_aml_interface.h"
30 #include "hw/intc/kvm_irqcount.h"
32 #include "hw/qdev-properties.h"
33 #include "hw/qdev-properties-system.h"
34 #include "qemu/timer.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/replay.h"
37 #include "sysemu/reset.h"
38 #include "sysemu/runstate.h"
39 #include "sysemu/rtc.h"
40 #include "hw/rtc/mc146818rtc.h"
41 #include "hw/rtc/mc146818rtc_regs.h"
42 #include "migration/vmstate.h"
43 #include "qapi/error.h"
44 #include "qapi/qapi-events-misc.h"
45 #include "qapi/visitor.h"
48 //#define DEBUG_COALESCED
51 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
53 # define CMOS_DPRINTF(format, ...) do { } while (0)
56 #ifdef DEBUG_COALESCED
57 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
59 # define DPRINTF_C(format, ...) do { } while (0)
62 #define SEC_PER_MIN 60
63 #define MIN_PER_HOUR 60
64 #define SEC_PER_HOUR 3600
65 #define HOUR_PER_DAY 24
66 #define SEC_PER_DAY 86400
68 #define RTC_REINJECT_ON_ACK_COUNT 20
69 #define RTC_CLOCK_RATE 32768
70 #define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
72 #define RTC_ISA_BASE 0x70
74 static void rtc_set_time(MC146818RtcState
*s
);
75 static void rtc_update_time(MC146818RtcState
*s
);
76 static void rtc_set_cmos(MC146818RtcState
*s
, const struct tm
*tm
);
77 static inline int rtc_from_bcd(MC146818RtcState
*s
, int a
);
78 static uint64_t get_next_alarm(MC146818RtcState
*s
);
80 static inline bool rtc_running(MC146818RtcState
*s
)
82 return (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
83 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20);
86 static uint64_t get_guest_rtc_ns(MC146818RtcState
*s
)
88 uint64_t guest_clock
= qemu_clock_get_ns(rtc_clock
);
90 return s
->base_rtc
* NANOSECONDS_PER_SECOND
+
91 guest_clock
- s
->last_update
+ s
->offset
;
94 static void rtc_coalesced_timer_update(MC146818RtcState
*s
)
96 if (s
->irq_coalesced
== 0) {
97 timer_del(s
->coalesced_timer
);
99 /* divide each RTC interval to 2 - 8 smaller intervals */
100 int c
= MIN(s
->irq_coalesced
, 7) + 1;
101 int64_t next_clock
= qemu_clock_get_ns(rtc_clock
) +
102 periodic_clock_to_ns(s
->period
/ c
);
103 timer_mod(s
->coalesced_timer
, next_clock
);
107 void rtc_reset_reinjection(MC146818RtcState
*rtc
)
109 rtc
->irq_coalesced
= 0;
112 static bool rtc_policy_slew_deliver_irq(MC146818RtcState
*s
)
114 kvm_reset_irq_delivered();
115 qemu_irq_raise(s
->irq
);
116 return kvm_get_irq_delivered();
119 static void rtc_coalesced_timer(void *opaque
)
121 MC146818RtcState
*s
= opaque
;
123 if (s
->irq_coalesced
!= 0) {
124 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
125 DPRINTF_C("cmos: injecting from timer\n");
126 if (rtc_policy_slew_deliver_irq(s
)) {
128 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
133 rtc_coalesced_timer_update(s
);
136 static uint32_t rtc_periodic_clock_ticks(MC146818RtcState
*s
)
140 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
)) {
144 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
146 return periodic_period_to_clock(period_code
);
150 * handle periodic timer. @old_period indicates the periodic timer update
151 * is just due to period adjustment.
153 static void periodic_timer_update(MC146818RtcState
*s
, int64_t current_time
,
154 uint32_t old_period
, bool period_change
)
157 int64_t cur_clock
, next_irq_clock
, lost_clock
= 0;
159 period
= rtc_periodic_clock_ticks(s
);
163 s
->irq_coalesced
= 0;
164 timer_del(s
->periodic_timer
);
168 /* compute 32 khz clock */
170 muldiv64(current_time
, RTC_CLOCK_RATE
, NANOSECONDS_PER_SECOND
);
173 * if the periodic timer's update is due to period re-configuration,
174 * we should count the clock since last interrupt.
176 if (old_period
&& period_change
) {
177 int64_t last_periodic_clock
, next_periodic_clock
;
179 next_periodic_clock
= muldiv64(s
->next_periodic_time
,
180 RTC_CLOCK_RATE
, NANOSECONDS_PER_SECOND
);
181 last_periodic_clock
= next_periodic_clock
- old_period
;
182 lost_clock
= cur_clock
- last_periodic_clock
;
183 assert(lost_clock
>= 0);
187 * s->irq_coalesced can change for two reasons:
189 * a) if one or more periodic timer interrupts have been lost,
190 * lost_clock will be more that a period.
192 * b) when the period may be reconfigured, we expect the OS to
193 * treat delayed tick as the new period. So, when switching
194 * from a shorter to a longer period, scale down the missing,
195 * because the OS will treat past delayed ticks as longer
196 * (leftovers are put back into lost_clock). When switching
197 * to a shorter period, scale up the missing ticks since the
198 * OS handler will treat past delayed ticks as shorter.
200 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
201 uint32_t old_irq_coalesced
= s
->irq_coalesced
;
203 lost_clock
+= old_irq_coalesced
* old_period
;
204 s
->irq_coalesced
= lost_clock
/ s
->period
;
205 lost_clock
%= s
->period
;
206 if (old_irq_coalesced
!= s
->irq_coalesced
||
207 old_period
!= s
->period
) {
208 DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
209 "period scaled from %d to %d\n", old_irq_coalesced
,
210 s
->irq_coalesced
, old_period
, s
->period
);
211 rtc_coalesced_timer_update(s
);
215 * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
216 * is not used, we should make the time progress anyway.
218 lost_clock
= MIN(lost_clock
, period
);
221 assert(lost_clock
>= 0 && lost_clock
<= period
);
223 next_irq_clock
= cur_clock
+ period
- lost_clock
;
224 s
->next_periodic_time
= periodic_clock_to_ns(next_irq_clock
) + 1;
225 timer_mod(s
->periodic_timer
, s
->next_periodic_time
);
228 static void rtc_periodic_timer(void *opaque
)
230 MC146818RtcState
*s
= opaque
;
232 periodic_timer_update(s
, s
->next_periodic_time
, s
->period
, false);
233 s
->cmos_data
[RTC_REG_C
] |= REG_C_PF
;
234 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
235 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
236 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
237 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
238 s
->irq_reinject_on_ack_count
= 0;
239 if (!rtc_policy_slew_deliver_irq(s
)) {
241 rtc_coalesced_timer_update(s
);
242 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
246 qemu_irq_raise(s
->irq
);
250 /* handle update-ended timer */
251 static void check_update_timer(MC146818RtcState
*s
)
253 uint64_t next_update_time
;
257 /* From the data sheet: "Holding the dividers in reset prevents
258 * interrupts from operating, while setting the SET bit allows"
261 if ((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) {
262 assert((s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
) == 0);
263 timer_del(s
->update_timer
);
267 guest_nsec
= get_guest_rtc_ns(s
) % NANOSECONDS_PER_SECOND
;
268 next_update_time
= qemu_clock_get_ns(rtc_clock
)
269 + NANOSECONDS_PER_SECOND
- guest_nsec
;
271 /* Compute time of next alarm. One second is already accounted
272 * for in next_update_time.
274 next_alarm_sec
= get_next_alarm(s
);
275 s
->next_alarm_time
= next_update_time
+
276 (next_alarm_sec
- 1) * NANOSECONDS_PER_SECOND
;
278 /* If update_in_progress latched the UIP bit, we must keep the timer
279 * programmed to the next second, so that UIP is cleared. Otherwise,
280 * if UF is already set, we might be able to optimize.
282 if (!(s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
) &&
283 (s
->cmos_data
[RTC_REG_C
] & REG_C_UF
)) {
284 /* If AF cannot change (i.e. either it is set already, or
285 * SET=1 and then the time is not updated), nothing to do.
287 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) ||
288 (s
->cmos_data
[RTC_REG_C
] & REG_C_AF
)) {
289 timer_del(s
->update_timer
);
293 /* UF is set, but AF is clear. Program the timer to target
295 next_update_time
= s
->next_alarm_time
;
297 if (next_update_time
!= timer_expire_time_ns(s
->update_timer
)) {
298 timer_mod(s
->update_timer
, next_update_time
);
302 static inline uint8_t convert_hour(MC146818RtcState
*s
, uint8_t hour
)
304 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
306 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
313 static uint64_t get_next_alarm(MC146818RtcState
*s
)
315 int32_t alarm_sec
, alarm_min
, alarm_hour
, cur_hour
, cur_min
, cur_sec
;
316 int32_t hour
, min
, sec
;
320 alarm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS_ALARM
]);
321 alarm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES_ALARM
]);
322 alarm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS_ALARM
]);
323 alarm_hour
= alarm_hour
== -1 ? -1 : convert_hour(s
, alarm_hour
);
325 cur_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
326 cur_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
327 cur_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
]);
328 cur_hour
= convert_hour(s
, cur_hour
);
330 if (alarm_hour
== -1) {
331 alarm_hour
= cur_hour
;
332 if (alarm_min
== -1) {
334 if (alarm_sec
== -1) {
335 alarm_sec
= cur_sec
+ 1;
336 } else if (cur_sec
> alarm_sec
) {
339 } else if (cur_min
== alarm_min
) {
340 if (alarm_sec
== -1) {
341 alarm_sec
= cur_sec
+ 1;
343 if (cur_sec
> alarm_sec
) {
347 if (alarm_sec
== SEC_PER_MIN
) {
348 /* wrap to next hour, minutes is not in don't care mode */
352 } else if (cur_min
> alarm_min
) {
355 } else if (cur_hour
== alarm_hour
) {
356 if (alarm_min
== -1) {
358 if (alarm_sec
== -1) {
359 alarm_sec
= cur_sec
+ 1;
360 } else if (cur_sec
> alarm_sec
) {
364 if (alarm_sec
== SEC_PER_MIN
) {
368 /* wrap to next day, hour is not in don't care mode */
369 alarm_min
%= MIN_PER_HOUR
;
370 } else if (cur_min
== alarm_min
) {
371 if (alarm_sec
== -1) {
372 alarm_sec
= cur_sec
+ 1;
374 /* wrap to next day, hours+minutes not in don't care mode */
375 alarm_sec
%= SEC_PER_MIN
;
379 /* values that are still don't care fire at the next min/sec */
380 if (alarm_min
== -1) {
383 if (alarm_sec
== -1) {
387 /* keep values in range */
388 if (alarm_sec
== SEC_PER_MIN
) {
392 if (alarm_min
== MIN_PER_HOUR
) {
396 alarm_hour
%= HOUR_PER_DAY
;
398 hour
= alarm_hour
- cur_hour
;
399 min
= hour
* MIN_PER_HOUR
+ alarm_min
- cur_min
;
400 sec
= min
* SEC_PER_MIN
+ alarm_sec
- cur_sec
;
401 return sec
<= 0 ? sec
+ SEC_PER_DAY
: sec
;
404 static void rtc_update_timer(void *opaque
)
406 MC146818RtcState
*s
= opaque
;
407 int32_t irqs
= REG_C_UF
;
410 assert((s
->cmos_data
[RTC_REG_A
] & 0x60) != 0x60);
412 /* UIP might have been latched, update time and clear it. */
414 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
416 if (qemu_clock_get_ns(rtc_clock
) >= s
->next_alarm_time
) {
418 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
419 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC
, NULL
);
423 new_irqs
= irqs
& ~s
->cmos_data
[RTC_REG_C
];
424 s
->cmos_data
[RTC_REG_C
] |= irqs
;
425 if ((new_irqs
& s
->cmos_data
[RTC_REG_B
]) != 0) {
426 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
427 qemu_irq_raise(s
->irq
);
429 check_update_timer(s
);
432 static void cmos_ioport_write(void *opaque
, hwaddr addr
,
433 uint64_t data
, unsigned size
)
435 MC146818RtcState
*s
= opaque
;
437 bool update_periodic_timer
;
439 if ((addr
& 1) == 0) {
440 s
->cmos_index
= data
& 0x7f;
442 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64
"\n",
443 s
->cmos_index
, data
);
444 switch(s
->cmos_index
) {
445 case RTC_SECONDS_ALARM
:
446 case RTC_MINUTES_ALARM
:
447 case RTC_HOURS_ALARM
:
448 s
->cmos_data
[s
->cmos_index
] = data
;
449 check_update_timer(s
);
451 case RTC_IBM_PS2_CENTURY_BYTE
:
452 s
->cmos_index
= RTC_CENTURY
;
458 case RTC_DAY_OF_WEEK
:
459 case RTC_DAY_OF_MONTH
:
462 s
->cmos_data
[s
->cmos_index
] = data
;
463 /* if in set mode, do not update the time */
464 if (rtc_running(s
)) {
466 check_update_timer(s
);
470 update_periodic_timer
= (s
->cmos_data
[RTC_REG_A
] ^ data
) & 0x0f;
471 old_period
= rtc_periodic_clock_ticks(s
);
473 if ((data
& 0x60) == 0x60) {
474 if (rtc_running(s
)) {
477 /* What happens to UIP when divider reset is enabled is
478 * unclear from the datasheet. Shouldn't matter much
481 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
482 } else if (((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) &&
483 (data
& 0x70) <= 0x20) {
484 /* when the divider reset is removed, the first update cycle
485 * begins one-half second later*/
486 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
487 s
->offset
= 500000000;
490 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
492 /* UIP bit is read only */
493 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
494 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
496 if (update_periodic_timer
) {
497 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
),
501 check_update_timer(s
);
504 update_periodic_timer
= (s
->cmos_data
[RTC_REG_B
] ^ data
)
506 old_period
= rtc_periodic_clock_ticks(s
);
508 if (data
& REG_B_SET
) {
509 /* update cmos to when the rtc was stopping */
510 if (rtc_running(s
)) {
513 /* set mode: reset UIP mode */
514 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
517 /* if disabling set mode, update the time */
518 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
519 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20) {
520 s
->offset
= get_guest_rtc_ns(s
) % NANOSECONDS_PER_SECOND
;
524 /* if an interrupt flag is already set when the interrupt
525 * becomes enabled, raise an interrupt immediately. */
526 if (data
& s
->cmos_data
[RTC_REG_C
] & REG_C_MASK
) {
527 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
528 qemu_irq_raise(s
->irq
);
530 s
->cmos_data
[RTC_REG_C
] &= ~REG_C_IRQF
;
531 qemu_irq_lower(s
->irq
);
533 s
->cmos_data
[RTC_REG_B
] = data
;
535 if (update_periodic_timer
) {
536 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
),
540 check_update_timer(s
);
544 /* cannot write to them */
547 s
->cmos_data
[s
->cmos_index
] = data
;
553 static inline int rtc_to_bcd(MC146818RtcState
*s
, int a
)
555 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
558 return ((a
/ 10) << 4) | (a
% 10);
562 static inline int rtc_from_bcd(MC146818RtcState
*s
, int a
)
564 if ((a
& 0xc0) == 0xc0) {
567 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
570 return ((a
>> 4) * 10) + (a
& 0x0f);
574 static void rtc_get_time(MC146818RtcState
*s
, struct tm
*tm
)
576 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
577 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
578 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
579 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
581 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
585 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
586 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
587 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
589 rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
+
590 rtc_from_bcd(s
, s
->cmos_data
[RTC_CENTURY
]) * 100 - 1900;
593 static void rtc_set_time(MC146818RtcState
*s
)
596 g_autofree
const char *qom_path
= object_get_canonical_path(OBJECT(s
));
598 rtc_get_time(s
, &tm
);
599 s
->base_rtc
= mktimegm(&tm
);
600 s
->last_update
= qemu_clock_get_ns(rtc_clock
);
602 qapi_event_send_rtc_change(qemu_timedate_diff(&tm
), qom_path
);
605 static void rtc_set_cmos(MC146818RtcState
*s
, const struct tm
*tm
)
609 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
610 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
611 if (s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) {
613 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
616 int h
= (tm
->tm_hour
% 12) ? tm
->tm_hour
% 12 : 12;
617 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, h
);
618 if (tm
->tm_hour
>= 12)
619 s
->cmos_data
[RTC_HOURS
] |= 0x80;
621 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
622 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
623 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
624 year
= tm
->tm_year
+ 1900 - s
->base_year
;
625 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
% 100);
626 s
->cmos_data
[RTC_CENTURY
] = rtc_to_bcd(s
, year
/ 100);
629 static void rtc_update_time(MC146818RtcState
*s
)
635 guest_nsec
= get_guest_rtc_ns(s
);
636 guest_sec
= guest_nsec
/ NANOSECONDS_PER_SECOND
;
637 gmtime_r(&guest_sec
, &ret
);
639 /* Is SET flag of Register B disabled? */
640 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) == 0) {
641 rtc_set_cmos(s
, &ret
);
645 static int update_in_progress(MC146818RtcState
*s
)
649 if (!rtc_running(s
)) {
652 if (timer_pending(s
->update_timer
)) {
653 int64_t next_update_time
= timer_expire_time_ns(s
->update_timer
);
654 /* Latch UIP until the timer expires. */
655 if (qemu_clock_get_ns(rtc_clock
) >=
656 (next_update_time
- UIP_HOLD_LENGTH
)) {
657 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
662 guest_nsec
= get_guest_rtc_ns(s
);
663 /* UIP bit will be set at last 244us of every second. */
664 if ((guest_nsec
% NANOSECONDS_PER_SECOND
) >=
665 (NANOSECONDS_PER_SECOND
- UIP_HOLD_LENGTH
)) {
671 static uint64_t cmos_ioport_read(void *opaque
, hwaddr addr
,
674 MC146818RtcState
*s
= opaque
;
676 if ((addr
& 1) == 0) {
679 switch(s
->cmos_index
) {
680 case RTC_IBM_PS2_CENTURY_BYTE
:
681 s
->cmos_index
= RTC_CENTURY
;
687 case RTC_DAY_OF_WEEK
:
688 case RTC_DAY_OF_MONTH
:
691 /* if not in set mode, calibrate cmos before
693 if (rtc_running(s
)) {
696 ret
= s
->cmos_data
[s
->cmos_index
];
699 ret
= s
->cmos_data
[s
->cmos_index
];
700 if (update_in_progress(s
)) {
705 ret
= s
->cmos_data
[s
->cmos_index
];
706 qemu_irq_lower(s
->irq
);
707 s
->cmos_data
[RTC_REG_C
] = 0x00;
708 if (ret
& (REG_C_UF
| REG_C_AF
)) {
709 check_update_timer(s
);
712 if(s
->irq_coalesced
&&
713 (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) &&
714 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
715 s
->irq_reinject_on_ack_count
++;
716 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
| REG_C_PF
;
717 DPRINTF_C("cmos: injecting on ack\n");
718 if (rtc_policy_slew_deliver_irq(s
)) {
720 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
726 ret
= s
->cmos_data
[s
->cmos_index
];
729 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
735 void mc146818rtc_set_cmos_data(MC146818RtcState
*s
, int addr
, int val
)
737 if (addr
>= 0 && addr
<= 127)
738 s
->cmos_data
[addr
] = val
;
741 int mc146818rtc_get_cmos_data(MC146818RtcState
*s
, int addr
)
743 assert(addr
>= 0 && addr
<= 127);
744 return s
->cmos_data
[addr
];
747 static void rtc_set_date_from_host(ISADevice
*dev
)
749 MC146818RtcState
*s
= MC146818_RTC(dev
);
752 qemu_get_timedate(&tm
, 0);
754 s
->base_rtc
= mktimegm(&tm
);
755 s
->last_update
= qemu_clock_get_ns(rtc_clock
);
758 /* set the CMOS date */
759 rtc_set_cmos(s
, &tm
);
762 static int rtc_pre_save(void *opaque
)
764 MC146818RtcState
*s
= opaque
;
771 static int rtc_post_load(void *opaque
, int version_id
)
773 MC146818RtcState
*s
= opaque
;
775 if (version_id
<= 2 || rtc_clock
== QEMU_CLOCK_REALTIME
) {
778 check_update_timer(s
);
780 s
->period
= rtc_periodic_clock_ticks(s
);
782 /* The periodic timer is deterministic in record/replay mode,
783 * so there is no need to update it after loading the vmstate.
784 * Reading RTC here would misalign record and replay.
786 if (replay_mode
== REPLAY_MODE_NONE
) {
787 uint64_t now
= qemu_clock_get_ns(rtc_clock
);
788 if (now
< s
->next_periodic_time
||
789 now
> (s
->next_periodic_time
+ get_max_clock_jump())) {
790 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
), s
->period
, false);
794 if (version_id
>= 2) {
795 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
796 rtc_coalesced_timer_update(s
);
802 static bool rtc_irq_reinject_on_ack_count_needed(void *opaque
)
804 MC146818RtcState
*s
= (MC146818RtcState
*)opaque
;
805 return s
->irq_reinject_on_ack_count
!= 0;
808 static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count
= {
809 .name
= "mc146818rtc/irq_reinject_on_ack_count",
811 .minimum_version_id
= 1,
812 .needed
= rtc_irq_reinject_on_ack_count_needed
,
813 .fields
= (const VMStateField
[]) {
814 VMSTATE_UINT16(irq_reinject_on_ack_count
, MC146818RtcState
),
815 VMSTATE_END_OF_LIST()
819 static const VMStateDescription vmstate_rtc
= {
820 .name
= "mc146818rtc",
822 .minimum_version_id
= 1,
823 .pre_save
= rtc_pre_save
,
824 .post_load
= rtc_post_load
,
825 .fields
= (const VMStateField
[]) {
826 VMSTATE_BUFFER(cmos_data
, MC146818RtcState
),
827 VMSTATE_UINT8(cmos_index
, MC146818RtcState
),
829 VMSTATE_TIMER_PTR(periodic_timer
, MC146818RtcState
),
830 VMSTATE_INT64(next_periodic_time
, MC146818RtcState
),
832 VMSTATE_UINT32_V(irq_coalesced
, MC146818RtcState
, 2),
833 VMSTATE_UINT32_V(period
, MC146818RtcState
, 2),
834 VMSTATE_UINT64_V(base_rtc
, MC146818RtcState
, 3),
835 VMSTATE_UINT64_V(last_update
, MC146818RtcState
, 3),
836 VMSTATE_INT64_V(offset
, MC146818RtcState
, 3),
837 VMSTATE_TIMER_PTR_V(update_timer
, MC146818RtcState
, 3),
838 VMSTATE_UINT64_V(next_alarm_time
, MC146818RtcState
, 3),
839 VMSTATE_END_OF_LIST()
841 .subsections
= (const VMStateDescription
* const []) {
842 &vmstate_rtc_irq_reinject_on_ack_count
,
847 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
848 BIOS will read it and start S3 resume at POST Entry */
849 static void rtc_notify_suspend(Notifier
*notifier
, void *data
)
851 MC146818RtcState
*s
= container_of(notifier
, MC146818RtcState
,
853 mc146818rtc_set_cmos_data(s
, 0xF, 0xFE);
856 static const MemoryRegionOps cmos_ops
= {
857 .read
= cmos_ioport_read
,
858 .write
= cmos_ioport_write
,
860 .min_access_size
= 1,
861 .max_access_size
= 1,
863 .endianness
= DEVICE_LITTLE_ENDIAN
,
866 static void rtc_get_date(Object
*obj
, struct tm
*current_tm
, Error
**errp
)
868 MC146818RtcState
*s
= MC146818_RTC(obj
);
871 rtc_get_time(s
, current_tm
);
874 static void rtc_realizefn(DeviceState
*dev
, Error
**errp
)
876 ISADevice
*isadev
= ISA_DEVICE(dev
);
877 MC146818RtcState
*s
= MC146818_RTC(dev
);
879 s
->cmos_data
[RTC_REG_A
] = 0x26;
880 s
->cmos_data
[RTC_REG_B
] = 0x02;
881 s
->cmos_data
[RTC_REG_C
] = 0x00;
882 s
->cmos_data
[RTC_REG_D
] = 0x80;
884 /* This is for historical reasons. The default base year qdev property
885 * was set to 2000 for most machine types before the century byte was
888 * This if statement means that the century byte will be always 0
889 * (at least until 2079...) for base_year = 1980, but will be set
890 * correctly for base_year = 2000.
892 if (s
->base_year
== 2000) {
896 if (s
->isairq
>= ISA_NUM_IRQS
) {
897 error_setg(errp
, "Maximum value for \"irq\" is: %u", ISA_NUM_IRQS
- 1);
901 rtc_set_date_from_host(isadev
);
903 switch (s
->lost_tick_policy
) {
904 case LOST_TICK_POLICY_SLEW
:
906 timer_new_ns(rtc_clock
, rtc_coalesced_timer
, s
);
908 case LOST_TICK_POLICY_DISCARD
:
911 error_setg(errp
, "Invalid lost tick policy.");
915 s
->periodic_timer
= timer_new_ns(rtc_clock
, rtc_periodic_timer
, s
);
916 s
->update_timer
= timer_new_ns(rtc_clock
, rtc_update_timer
, s
);
917 check_update_timer(s
);
919 s
->suspend_notifier
.notify
= rtc_notify_suspend
;
920 qemu_register_suspend_notifier(&s
->suspend_notifier
);
922 memory_region_init_io(&s
->io
, OBJECT(s
), &cmos_ops
, s
, "rtc", 2);
923 isa_register_ioport(isadev
, &s
->io
, s
->io_base
);
925 /* register rtc 0x70 port for coalesced_pio */
926 memory_region_set_flush_coalesced(&s
->io
);
927 memory_region_init_io(&s
->coalesced_io
, OBJECT(s
), &cmos_ops
,
929 memory_region_add_subregion(&s
->io
, 0, &s
->coalesced_io
);
930 memory_region_add_coalescing(&s
->coalesced_io
, 0, 1);
932 qdev_set_legacy_instance_id(dev
, s
->io_base
, 3);
934 object_property_add_tm(OBJECT(s
), "date", rtc_get_date
);
936 qdev_init_gpio_out(dev
, &s
->irq
, 1);
939 MC146818RtcState
*mc146818_rtc_init(ISABus
*bus
, int base_year
,
940 qemu_irq intercept_irq
)
946 isadev
= isa_new(TYPE_MC146818_RTC
);
947 dev
= DEVICE(isadev
);
948 s
= MC146818_RTC(isadev
);
949 qdev_prop_set_int32(dev
, "base_year", base_year
);
950 isa_realize_and_unref(isadev
, bus
, &error_fatal
);
952 qdev_connect_gpio_out(dev
, 0, intercept_irq
);
954 isa_connect_gpio_out(isadev
, 0, s
->isairq
);
957 object_property_add_alias(qdev_get_machine(), "rtc-time", OBJECT(isadev
),
963 static Property mc146818rtc_properties
[] = {
964 DEFINE_PROP_INT32("base_year", MC146818RtcState
, base_year
, 1980),
965 DEFINE_PROP_UINT16("iobase", MC146818RtcState
, io_base
, RTC_ISA_BASE
),
966 DEFINE_PROP_UINT8("irq", MC146818RtcState
, isairq
, RTC_ISA_IRQ
),
967 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", MC146818RtcState
,
968 lost_tick_policy
, LOST_TICK_POLICY_DISCARD
),
969 DEFINE_PROP_END_OF_LIST(),
972 static void rtc_reset_enter(Object
*obj
, ResetType type
)
974 MC146818RtcState
*s
= MC146818_RTC(obj
);
976 /* Reason: VM do suspend self will set 0xfe
977 * Reset any values other than 0xfe(Guest suspend case) */
978 if (s
->cmos_data
[0x0f] != 0xfe) {
979 s
->cmos_data
[0x0f] = 0x00;
982 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
983 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
984 check_update_timer(s
);
987 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
988 s
->irq_coalesced
= 0;
989 s
->irq_reinject_on_ack_count
= 0;
993 static void rtc_reset_hold(Object
*obj
, ResetType type
)
995 MC146818RtcState
*s
= MC146818_RTC(obj
);
997 qemu_irq_lower(s
->irq
);
1000 static void rtc_build_aml(AcpiDevAmlIf
*adev
, Aml
*scope
)
1002 MC146818RtcState
*s
= MC146818_RTC(adev
);
1007 * Reserving 8 io ports here, following what physical hardware
1008 * does, even though qemu only responds to the first two ports.
1010 crs
= aml_resource_template();
1011 aml_append(crs
, aml_io(AML_DECODE16
, s
->io_base
, s
->io_base
,
1013 aml_append(crs
, aml_irq_no_flags(s
->isairq
));
1015 dev
= aml_device("RTC");
1016 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0B00")));
1017 aml_append(dev
, aml_name_decl("_CRS", crs
));
1019 aml_append(scope
, dev
);
1022 static void rtc_class_initfn(ObjectClass
*klass
, void *data
)
1024 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1025 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
1026 AcpiDevAmlIfClass
*adevc
= ACPI_DEV_AML_IF_CLASS(klass
);
1028 dc
->realize
= rtc_realizefn
;
1029 dc
->vmsd
= &vmstate_rtc
;
1030 rc
->phases
.enter
= rtc_reset_enter
;
1031 rc
->phases
.hold
= rtc_reset_hold
;
1032 adevc
->build_dev_aml
= rtc_build_aml
;
1033 device_class_set_props(dc
, mc146818rtc_properties
);
1034 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1037 static const TypeInfo mc146818rtc_info
= {
1038 .name
= TYPE_MC146818_RTC
,
1039 .parent
= TYPE_ISA_DEVICE
,
1040 .instance_size
= sizeof(MC146818RtcState
),
1041 .class_init
= rtc_class_initfn
,
1042 .interfaces
= (InterfaceInfo
[]) {
1043 { TYPE_ACPI_DEV_AML_IF
},
1048 static void mc146818rtc_register_types(void)
1050 type_register_static(&mc146818rtc_info
);
1053 type_init(mc146818rtc_register_types
)