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/timer.h"
26 #include "sysemu/sysemu.h"
27 #include "hw/timer/mc146818rtc.h"
28 #include "qapi/visitor.h"
31 #include "hw/i386/apic.h"
35 //#define DEBUG_COALESCED
38 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
40 # define CMOS_DPRINTF(format, ...) do { } while (0)
43 #ifdef DEBUG_COALESCED
44 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
46 # define DPRINTF_C(format, ...) do { } while (0)
49 #define NSEC_PER_SEC 1000000000LL
50 #define SEC_PER_MIN 60
51 #define MIN_PER_HOUR 60
52 #define SEC_PER_HOUR 3600
53 #define HOUR_PER_DAY 24
54 #define SEC_PER_DAY 86400
56 #define RTC_REINJECT_ON_ACK_COUNT 20
57 #define RTC_CLOCK_RATE 32768
58 #define UIP_HOLD_LENGTH (8 * NSEC_PER_SEC / 32768)
60 #define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC)
62 typedef struct RTCState
{
66 uint8_t cmos_data
[128];
76 QEMUTimer
*periodic_timer
;
77 int64_t next_periodic_time
;
78 /* update-ended timer */
79 QEMUTimer
*update_timer
;
80 uint64_t next_alarm_time
;
81 uint16_t irq_reinject_on_ack_count
;
82 uint32_t irq_coalesced
;
84 QEMUTimer
*coalesced_timer
;
85 Notifier clock_reset_notifier
;
86 LostTickPolicy lost_tick_policy
;
87 Notifier suspend_notifier
;
90 static void rtc_set_time(RTCState
*s
);
91 static void rtc_update_time(RTCState
*s
);
92 static void rtc_set_cmos(RTCState
*s
, const struct tm
*tm
);
93 static inline int rtc_from_bcd(RTCState
*s
, int a
);
94 static uint64_t get_next_alarm(RTCState
*s
);
96 static inline bool rtc_running(RTCState
*s
)
98 return (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
99 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20);
102 static uint64_t get_guest_rtc_ns(RTCState
*s
)
105 uint64_t guest_clock
= qemu_get_clock_ns(rtc_clock
);
107 guest_rtc
= s
->base_rtc
* NSEC_PER_SEC
108 + guest_clock
- s
->last_update
+ s
->offset
;
113 static void rtc_coalesced_timer_update(RTCState
*s
)
115 if (s
->irq_coalesced
== 0) {
116 qemu_del_timer(s
->coalesced_timer
);
118 /* divide each RTC interval to 2 - 8 smaller intervals */
119 int c
= MIN(s
->irq_coalesced
, 7) + 1;
120 int64_t next_clock
= qemu_get_clock_ns(rtc_clock
) +
121 muldiv64(s
->period
/ c
, get_ticks_per_sec(), RTC_CLOCK_RATE
);
122 qemu_mod_timer(s
->coalesced_timer
, next_clock
);
126 static void rtc_coalesced_timer(void *opaque
)
128 RTCState
*s
= opaque
;
130 if (s
->irq_coalesced
!= 0) {
131 apic_reset_irq_delivered();
132 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
133 DPRINTF_C("cmos: injecting from timer\n");
134 qemu_irq_raise(s
->irq
);
135 if (apic_get_irq_delivered()) {
137 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
142 rtc_coalesced_timer_update(s
);
146 /* handle periodic timer */
147 static void periodic_timer_update(RTCState
*s
, int64_t current_time
)
149 int period_code
, period
;
150 int64_t cur_clock
, next_irq_clock
;
152 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
154 && ((s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
)
155 || ((s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) && s
->sqw_irq
))) {
156 if (period_code
<= 2)
158 /* period in 32 Khz cycles */
159 period
= 1 << (period_code
- 1);
161 if (period
!= s
->period
) {
162 s
->irq_coalesced
= (s
->irq_coalesced
* s
->period
) / period
;
163 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s
->irq_coalesced
);
167 /* compute 32 khz clock */
168 cur_clock
= muldiv64(current_time
, RTC_CLOCK_RATE
, get_ticks_per_sec());
169 next_irq_clock
= (cur_clock
& ~(period
- 1)) + period
;
170 s
->next_periodic_time
=
171 muldiv64(next_irq_clock
, get_ticks_per_sec(), RTC_CLOCK_RATE
) + 1;
172 qemu_mod_timer(s
->periodic_timer
, s
->next_periodic_time
);
175 s
->irq_coalesced
= 0;
177 qemu_del_timer(s
->periodic_timer
);
181 static void rtc_periodic_timer(void *opaque
)
183 RTCState
*s
= opaque
;
185 periodic_timer_update(s
, s
->next_periodic_time
);
186 s
->cmos_data
[RTC_REG_C
] |= REG_C_PF
;
187 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
188 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
190 if (s
->lost_tick_policy
== LOST_TICK_SLEW
) {
191 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
192 s
->irq_reinject_on_ack_count
= 0;
193 apic_reset_irq_delivered();
194 qemu_irq_raise(s
->irq
);
195 if (!apic_get_irq_delivered()) {
197 rtc_coalesced_timer_update(s
);
198 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
203 qemu_irq_raise(s
->irq
);
205 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) {
206 /* Not square wave at all but we don't want 2048Hz interrupts!
207 Must be seen as a pulse. */
208 qemu_irq_raise(s
->sqw_irq
);
212 /* handle update-ended timer */
213 static void check_update_timer(RTCState
*s
)
215 uint64_t next_update_time
;
219 /* From the data sheet: "Holding the dividers in reset prevents
220 * interrupts from operating, while setting the SET bit allows"
221 * them to occur. However, it will prevent an alarm interrupt
222 * from occurring, because the time of day is not updated.
224 if ((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) {
225 qemu_del_timer(s
->update_timer
);
228 if ((s
->cmos_data
[RTC_REG_C
] & REG_C_UF
) &&
229 (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
230 qemu_del_timer(s
->update_timer
);
233 if ((s
->cmos_data
[RTC_REG_C
] & REG_C_UF
) &&
234 (s
->cmos_data
[RTC_REG_C
] & REG_C_AF
)) {
235 qemu_del_timer(s
->update_timer
);
239 guest_nsec
= get_guest_rtc_ns(s
) % NSEC_PER_SEC
;
240 /* if UF is clear, reprogram to next second */
241 next_update_time
= qemu_get_clock_ns(rtc_clock
)
242 + NSEC_PER_SEC
- guest_nsec
;
244 /* Compute time of next alarm. One second is already accounted
245 * for in next_update_time.
247 next_alarm_sec
= get_next_alarm(s
);
248 s
->next_alarm_time
= next_update_time
+ (next_alarm_sec
- 1) * NSEC_PER_SEC
;
250 if (s
->cmos_data
[RTC_REG_C
] & REG_C_UF
) {
251 /* UF is set, but AF is clear. Program the timer to target
253 next_update_time
= s
->next_alarm_time
;
255 if (next_update_time
!= qemu_timer_expire_time_ns(s
->update_timer
)) {
256 qemu_mod_timer(s
->update_timer
, next_update_time
);
260 static inline uint8_t convert_hour(RTCState
*s
, uint8_t hour
)
262 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
264 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
271 static uint64_t get_next_alarm(RTCState
*s
)
273 int32_t alarm_sec
, alarm_min
, alarm_hour
, cur_hour
, cur_min
, cur_sec
;
274 int32_t hour
, min
, sec
;
278 alarm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS_ALARM
]);
279 alarm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES_ALARM
]);
280 alarm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS_ALARM
]);
281 alarm_hour
= alarm_hour
== -1 ? -1 : convert_hour(s
, alarm_hour
);
283 cur_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
284 cur_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
285 cur_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
]);
286 cur_hour
= convert_hour(s
, cur_hour
);
288 if (alarm_hour
== -1) {
289 alarm_hour
= cur_hour
;
290 if (alarm_min
== -1) {
292 if (alarm_sec
== -1) {
293 alarm_sec
= cur_sec
+ 1;
294 } else if (cur_sec
> alarm_sec
) {
297 } else if (cur_min
== alarm_min
) {
298 if (alarm_sec
== -1) {
299 alarm_sec
= cur_sec
+ 1;
301 if (cur_sec
> alarm_sec
) {
305 if (alarm_sec
== SEC_PER_MIN
) {
306 /* wrap to next hour, minutes is not in don't care mode */
310 } else if (cur_min
> alarm_min
) {
313 } else if (cur_hour
== alarm_hour
) {
314 if (alarm_min
== -1) {
316 if (alarm_sec
== -1) {
317 alarm_sec
= cur_sec
+ 1;
318 } else if (cur_sec
> alarm_sec
) {
322 if (alarm_sec
== SEC_PER_MIN
) {
326 /* wrap to next day, hour is not in don't care mode */
327 alarm_min
%= MIN_PER_HOUR
;
328 } else if (cur_min
== alarm_min
) {
329 if (alarm_sec
== -1) {
330 alarm_sec
= cur_sec
+ 1;
332 /* wrap to next day, hours+minutes not in don't care mode */
333 alarm_sec
%= SEC_PER_MIN
;
337 /* values that are still don't care fire at the next min/sec */
338 if (alarm_min
== -1) {
341 if (alarm_sec
== -1) {
345 /* keep values in range */
346 if (alarm_sec
== SEC_PER_MIN
) {
350 if (alarm_min
== MIN_PER_HOUR
) {
354 alarm_hour
%= HOUR_PER_DAY
;
356 hour
= alarm_hour
- cur_hour
;
357 min
= hour
* MIN_PER_HOUR
+ alarm_min
- cur_min
;
358 sec
= min
* SEC_PER_MIN
+ alarm_sec
- cur_sec
;
359 return sec
<= 0 ? sec
+ SEC_PER_DAY
: sec
;
362 static void rtc_update_timer(void *opaque
)
364 RTCState
*s
= opaque
;
365 int32_t irqs
= REG_C_UF
;
368 assert((s
->cmos_data
[RTC_REG_A
] & 0x60) != 0x60);
370 /* UIP might have been latched, update time and clear it. */
372 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
374 if (qemu_get_clock_ns(rtc_clock
) >= s
->next_alarm_time
) {
376 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
377 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC
);
381 new_irqs
= irqs
& ~s
->cmos_data
[RTC_REG_C
];
382 s
->cmos_data
[RTC_REG_C
] |= irqs
;
383 if ((new_irqs
& s
->cmos_data
[RTC_REG_B
]) != 0) {
384 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
385 qemu_irq_raise(s
->irq
);
387 check_update_timer(s
);
390 static void cmos_ioport_write(void *opaque
, hwaddr addr
,
391 uint64_t data
, unsigned size
)
393 RTCState
*s
= opaque
;
395 if ((addr
& 1) == 0) {
396 s
->cmos_index
= data
& 0x7f;
398 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
399 s
->cmos_index
, data
);
400 switch(s
->cmos_index
) {
401 case RTC_SECONDS_ALARM
:
402 case RTC_MINUTES_ALARM
:
403 case RTC_HOURS_ALARM
:
404 s
->cmos_data
[s
->cmos_index
] = data
;
405 check_update_timer(s
);
407 case RTC_IBM_PS2_CENTURY_BYTE
:
408 s
->cmos_index
= RTC_CENTURY
;
414 case RTC_DAY_OF_WEEK
:
415 case RTC_DAY_OF_MONTH
:
418 s
->cmos_data
[s
->cmos_index
] = data
;
419 /* if in set mode, do not update the time */
420 if (rtc_running(s
)) {
422 check_update_timer(s
);
426 if ((data
& 0x60) == 0x60) {
427 if (rtc_running(s
)) {
430 /* What happens to UIP when divider reset is enabled is
431 * unclear from the datasheet. Shouldn't matter much
434 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
435 } else if (((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) &&
436 (data
& 0x70) <= 0x20) {
437 /* when the divider reset is removed, the first update cycle
438 * begins one-half second later*/
439 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
440 s
->offset
= 500000000;
443 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
445 /* UIP bit is read only */
446 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
447 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
448 periodic_timer_update(s
, qemu_get_clock_ns(rtc_clock
));
449 check_update_timer(s
);
452 if (data
& REG_B_SET
) {
453 /* update cmos to when the rtc was stopping */
454 if (rtc_running(s
)) {
457 /* set mode: reset UIP mode */
458 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
461 /* if disabling set mode, update the time */
462 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
463 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20) {
464 s
->offset
= get_guest_rtc_ns(s
) % NSEC_PER_SEC
;
468 /* if an interrupt flag is already set when the interrupt
469 * becomes enabled, raise an interrupt immediately. */
470 if (data
& s
->cmos_data
[RTC_REG_C
] & REG_C_MASK
) {
471 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
472 qemu_irq_raise(s
->irq
);
474 s
->cmos_data
[RTC_REG_C
] &= ~REG_C_IRQF
;
475 qemu_irq_lower(s
->irq
);
477 s
->cmos_data
[RTC_REG_B
] = data
;
478 periodic_timer_update(s
, qemu_get_clock_ns(rtc_clock
));
479 check_update_timer(s
);
483 /* cannot write to them */
486 s
->cmos_data
[s
->cmos_index
] = data
;
492 static inline int rtc_to_bcd(RTCState
*s
, int a
)
494 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
497 return ((a
/ 10) << 4) | (a
% 10);
501 static inline int rtc_from_bcd(RTCState
*s
, int a
)
503 if ((a
& 0xc0) == 0xc0) {
506 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
509 return ((a
>> 4) * 10) + (a
& 0x0f);
513 static void rtc_get_time(RTCState
*s
, struct tm
*tm
)
515 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
516 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
517 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
518 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
520 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
524 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
525 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
526 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
528 rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
+
529 rtc_from_bcd(s
, s
->cmos_data
[RTC_CENTURY
]) * 100 - 1900;
532 static void rtc_set_time(RTCState
*s
)
536 rtc_get_time(s
, &tm
);
537 s
->base_rtc
= mktimegm(&tm
);
538 s
->last_update
= qemu_get_clock_ns(rtc_clock
);
540 rtc_change_mon_event(&tm
);
543 static void rtc_set_cmos(RTCState
*s
, const struct tm
*tm
)
547 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
548 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
549 if (s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) {
551 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
554 int h
= (tm
->tm_hour
% 12) ? tm
->tm_hour
% 12 : 12;
555 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, h
);
556 if (tm
->tm_hour
>= 12)
557 s
->cmos_data
[RTC_HOURS
] |= 0x80;
559 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
560 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
561 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
562 year
= tm
->tm_year
+ 1900 - s
->base_year
;
563 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
% 100);
564 s
->cmos_data
[RTC_CENTURY
] = rtc_to_bcd(s
, year
/ 100);
567 static void rtc_update_time(RTCState
*s
)
573 guest_nsec
= get_guest_rtc_ns(s
);
574 guest_sec
= guest_nsec
/ NSEC_PER_SEC
;
575 gmtime_r(&guest_sec
, &ret
);
577 /* Is SET flag of Register B disabled? */
578 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) == 0) {
579 rtc_set_cmos(s
, &ret
);
583 static int update_in_progress(RTCState
*s
)
587 if (!rtc_running(s
)) {
590 if (qemu_timer_pending(s
->update_timer
)) {
591 int64_t next_update_time
= qemu_timer_expire_time_ns(s
->update_timer
);
592 /* Latch UIP until the timer expires. */
593 if (qemu_get_clock_ns(rtc_clock
) >= (next_update_time
- UIP_HOLD_LENGTH
)) {
594 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
599 guest_nsec
= get_guest_rtc_ns(s
);
600 /* UIP bit will be set at last 244us of every second. */
601 if ((guest_nsec
% NSEC_PER_SEC
) >= (NSEC_PER_SEC
- UIP_HOLD_LENGTH
)) {
607 static uint64_t cmos_ioport_read(void *opaque
, hwaddr addr
,
610 RTCState
*s
= opaque
;
612 if ((addr
& 1) == 0) {
615 switch(s
->cmos_index
) {
616 case RTC_IBM_PS2_CENTURY_BYTE
:
617 s
->cmos_index
= RTC_CENTURY
;
623 case RTC_DAY_OF_WEEK
:
624 case RTC_DAY_OF_MONTH
:
627 /* if not in set mode, calibrate cmos before
629 if (rtc_running(s
)) {
632 ret
= s
->cmos_data
[s
->cmos_index
];
635 if (update_in_progress(s
)) {
636 s
->cmos_data
[s
->cmos_index
] |= REG_A_UIP
;
638 s
->cmos_data
[s
->cmos_index
] &= ~REG_A_UIP
;
640 ret
= s
->cmos_data
[s
->cmos_index
];
643 ret
= s
->cmos_data
[s
->cmos_index
];
644 qemu_irq_lower(s
->irq
);
645 s
->cmos_data
[RTC_REG_C
] = 0x00;
646 if (ret
& (REG_C_UF
| REG_C_AF
)) {
647 check_update_timer(s
);
650 if(s
->irq_coalesced
&&
651 (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) &&
652 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
653 s
->irq_reinject_on_ack_count
++;
654 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
| REG_C_PF
;
655 apic_reset_irq_delivered();
656 DPRINTF_C("cmos: injecting on ack\n");
657 qemu_irq_raise(s
->irq
);
658 if (apic_get_irq_delivered()) {
660 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
667 ret
= s
->cmos_data
[s
->cmos_index
];
670 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
676 void rtc_set_memory(ISADevice
*dev
, int addr
, int val
)
678 RTCState
*s
= MC146818_RTC(dev
);
679 if (addr
>= 0 && addr
<= 127)
680 s
->cmos_data
[addr
] = val
;
683 int rtc_get_memory(ISADevice
*dev
, int addr
)
685 RTCState
*s
= MC146818_RTC(dev
);
686 assert(addr
>= 0 && addr
<= 127);
687 return s
->cmos_data
[addr
];
690 static void rtc_set_date_from_host(ISADevice
*dev
)
692 RTCState
*s
= MC146818_RTC(dev
);
695 qemu_get_timedate(&tm
, 0);
697 s
->base_rtc
= mktimegm(&tm
);
698 s
->last_update
= qemu_get_clock_ns(rtc_clock
);
701 /* set the CMOS date */
702 rtc_set_cmos(s
, &tm
);
705 static int rtc_post_load(void *opaque
, int version_id
)
707 RTCState
*s
= opaque
;
709 if (version_id
<= 2) {
712 check_update_timer(s
);
716 if (version_id
>= 2) {
717 if (s
->lost_tick_policy
== LOST_TICK_SLEW
) {
718 rtc_coalesced_timer_update(s
);
725 static const VMStateDescription vmstate_rtc
= {
726 .name
= "mc146818rtc",
728 .minimum_version_id
= 1,
729 .minimum_version_id_old
= 1,
730 .post_load
= rtc_post_load
,
731 .fields
= (VMStateField
[]) {
732 VMSTATE_BUFFER(cmos_data
, RTCState
),
733 VMSTATE_UINT8(cmos_index
, RTCState
),
735 VMSTATE_TIMER(periodic_timer
, RTCState
),
736 VMSTATE_INT64(next_periodic_time
, RTCState
),
738 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
739 VMSTATE_UINT32_V(period
, RTCState
, 2),
740 VMSTATE_UINT64_V(base_rtc
, RTCState
, 3),
741 VMSTATE_UINT64_V(last_update
, RTCState
, 3),
742 VMSTATE_INT64_V(offset
, RTCState
, 3),
743 VMSTATE_TIMER_V(update_timer
, RTCState
, 3),
744 VMSTATE_UINT64_V(next_alarm_time
, RTCState
, 3),
745 VMSTATE_END_OF_LIST()
749 static void rtc_notify_clock_reset(Notifier
*notifier
, void *data
)
751 RTCState
*s
= container_of(notifier
, RTCState
, clock_reset_notifier
);
752 int64_t now
= *(int64_t *)data
;
754 rtc_set_date_from_host(ISA_DEVICE(s
));
755 periodic_timer_update(s
, now
);
756 check_update_timer(s
);
758 if (s
->lost_tick_policy
== LOST_TICK_SLEW
) {
759 rtc_coalesced_timer_update(s
);
764 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
765 BIOS will read it and start S3 resume at POST Entry */
766 static void rtc_notify_suspend(Notifier
*notifier
, void *data
)
768 RTCState
*s
= container_of(notifier
, RTCState
, suspend_notifier
);
769 rtc_set_memory(ISA_DEVICE(s
), 0xF, 0xFE);
772 static void rtc_reset(void *opaque
)
774 RTCState
*s
= opaque
;
776 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
777 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
778 check_update_timer(s
);
780 qemu_irq_lower(s
->irq
);
783 if (s
->lost_tick_policy
== LOST_TICK_SLEW
) {
784 s
->irq_coalesced
= 0;
789 static const MemoryRegionOps cmos_ops
= {
790 .read
= cmos_ioport_read
,
791 .write
= cmos_ioport_write
,
793 .min_access_size
= 1,
794 .max_access_size
= 1,
796 .endianness
= DEVICE_LITTLE_ENDIAN
,
799 static void rtc_get_date(Object
*obj
, Visitor
*v
, void *opaque
,
800 const char *name
, Error
**errp
)
802 RTCState
*s
= MC146818_RTC(obj
);
803 struct tm current_tm
;
806 rtc_get_time(s
, ¤t_tm
);
807 visit_start_struct(v
, NULL
, "struct tm", name
, 0, errp
);
808 visit_type_int32(v
, ¤t_tm
.tm_year
, "tm_year", errp
);
809 visit_type_int32(v
, ¤t_tm
.tm_mon
, "tm_mon", errp
);
810 visit_type_int32(v
, ¤t_tm
.tm_mday
, "tm_mday", errp
);
811 visit_type_int32(v
, ¤t_tm
.tm_hour
, "tm_hour", errp
);
812 visit_type_int32(v
, ¤t_tm
.tm_min
, "tm_min", errp
);
813 visit_type_int32(v
, ¤t_tm
.tm_sec
, "tm_sec", errp
);
814 visit_end_struct(v
, errp
);
817 static int rtc_initfn(ISADevice
*dev
)
819 RTCState
*s
= MC146818_RTC(dev
);
822 s
->cmos_data
[RTC_REG_A
] = 0x26;
823 s
->cmos_data
[RTC_REG_B
] = 0x02;
824 s
->cmos_data
[RTC_REG_C
] = 0x00;
825 s
->cmos_data
[RTC_REG_D
] = 0x80;
827 /* This is for historical reasons. The default base year qdev property
828 * was set to 2000 for most machine types before the century byte was
831 * This if statement means that the century byte will be always 0
832 * (at least until 2079...) for base_year = 1980, but will be set
833 * correctly for base_year = 2000.
835 if (s
->base_year
== 2000) {
839 rtc_set_date_from_host(dev
);
842 switch (s
->lost_tick_policy
) {
845 qemu_new_timer_ns(rtc_clock
, rtc_coalesced_timer
, s
);
847 case LOST_TICK_DISCARD
:
854 s
->periodic_timer
= qemu_new_timer_ns(rtc_clock
, rtc_periodic_timer
, s
);
855 s
->update_timer
= qemu_new_timer_ns(rtc_clock
, rtc_update_timer
, s
);
856 check_update_timer(s
);
858 s
->clock_reset_notifier
.notify
= rtc_notify_clock_reset
;
859 qemu_register_clock_reset_notifier(rtc_clock
, &s
->clock_reset_notifier
);
861 s
->suspend_notifier
.notify
= rtc_notify_suspend
;
862 qemu_register_suspend_notifier(&s
->suspend_notifier
);
864 memory_region_init_io(&s
->io
, &cmos_ops
, s
, "rtc", 2);
865 isa_register_ioport(dev
, &s
->io
, base
);
867 qdev_set_legacy_instance_id(&dev
->qdev
, base
, 3);
868 qemu_register_reset(rtc_reset
, s
);
870 object_property_add(OBJECT(s
), "date", "struct tm",
871 rtc_get_date
, NULL
, NULL
, s
, NULL
);
876 ISADevice
*rtc_init(ISABus
*bus
, int base_year
, qemu_irq intercept_irq
)
882 isadev
= isa_create(bus
, TYPE_MC146818_RTC
);
883 dev
= DEVICE(isadev
);
884 s
= MC146818_RTC(isadev
);
885 qdev_prop_set_int32(dev
, "base_year", base_year
);
886 qdev_init_nofail(dev
);
888 s
->irq
= intercept_irq
;
890 isa_init_irq(isadev
, &s
->irq
, RTC_ISA_IRQ
);
895 static Property mc146818rtc_properties
[] = {
896 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
897 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState
,
898 lost_tick_policy
, LOST_TICK_DISCARD
),
899 DEFINE_PROP_END_OF_LIST(),
902 static void rtc_class_initfn(ObjectClass
*klass
, void *data
)
904 DeviceClass
*dc
= DEVICE_CLASS(klass
);
905 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
906 ic
->init
= rtc_initfn
;
908 dc
->vmsd
= &vmstate_rtc
;
909 dc
->props
= mc146818rtc_properties
;
912 static const TypeInfo mc146818rtc_info
= {
913 .name
= TYPE_MC146818_RTC
,
914 .parent
= TYPE_ISA_DEVICE
,
915 .instance_size
= sizeof(RTCState
),
916 .class_init
= rtc_class_initfn
,
919 static void mc146818rtc_register_types(void)
921 type_register_static(&mc146818rtc_info
);
924 type_init(mc146818rtc_register_types
)