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"
26 #include "qemu/timer.h"
27 #include "sysemu/sysemu.h"
28 #include "hw/timer/mc146818rtc.h"
29 #include "qapi/visitor.h"
30 #include "qapi-event.h"
31 #include "qmp-commands.h"
34 #include "hw/i386/apic.h"
38 //#define DEBUG_COALESCED
41 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
43 # define CMOS_DPRINTF(format, ...) do { } while (0)
46 #ifdef DEBUG_COALESCED
47 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
49 # define DPRINTF_C(format, ...) do { } while (0)
52 #define SEC_PER_MIN 60
53 #define MIN_PER_HOUR 60
54 #define SEC_PER_HOUR 3600
55 #define HOUR_PER_DAY 24
56 #define SEC_PER_DAY 86400
58 #define RTC_REINJECT_ON_ACK_COUNT 20
59 #define RTC_CLOCK_RATE 32768
60 #define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
62 #define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC)
64 typedef struct RTCState
{
68 uint8_t cmos_data
[128];
77 QEMUTimer
*periodic_timer
;
78 int64_t next_periodic_time
;
79 /* update-ended timer */
80 QEMUTimer
*update_timer
;
81 uint64_t next_alarm_time
;
82 uint16_t irq_reinject_on_ack_count
;
83 uint32_t irq_coalesced
;
85 QEMUTimer
*coalesced_timer
;
86 Notifier clock_reset_notifier
;
87 LostTickPolicy lost_tick_policy
;
88 Notifier suspend_notifier
;
89 QLIST_ENTRY(RTCState
) link
;
92 static void rtc_set_time(RTCState
*s
);
93 static void rtc_update_time(RTCState
*s
);
94 static void rtc_set_cmos(RTCState
*s
, const struct tm
*tm
);
95 static inline int rtc_from_bcd(RTCState
*s
, int a
);
96 static uint64_t get_next_alarm(RTCState
*s
);
98 static inline bool rtc_running(RTCState
*s
)
100 return (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
101 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20);
104 static uint64_t get_guest_rtc_ns(RTCState
*s
)
107 uint64_t guest_clock
= qemu_clock_get_ns(rtc_clock
);
109 guest_rtc
= s
->base_rtc
* NANOSECONDS_PER_SECOND
110 + 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 muldiv64(s
->period
/ c
, get_ticks_per_sec(), RTC_CLOCK_RATE
);
124 timer_mod(s
->coalesced_timer
, next_clock
);
128 static void rtc_coalesced_timer(void *opaque
)
130 RTCState
*s
= opaque
;
132 if (s
->irq_coalesced
!= 0) {
133 apic_reset_irq_delivered();
134 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
135 DPRINTF_C("cmos: injecting from timer\n");
136 qemu_irq_raise(s
->irq
);
137 if (apic_get_irq_delivered()) {
139 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
144 rtc_coalesced_timer_update(s
);
148 /* handle periodic timer */
149 static void periodic_timer_update(RTCState
*s
, int64_t current_time
)
151 int period_code
, period
;
152 int64_t cur_clock
, next_irq_clock
;
154 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
156 && (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
)) {
157 if (period_code
<= 2)
159 /* period in 32 Khz cycles */
160 period
= 1 << (period_code
- 1);
162 if (period
!= s
->period
) {
163 s
->irq_coalesced
= (s
->irq_coalesced
* s
->period
) / period
;
164 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s
->irq_coalesced
);
168 /* compute 32 khz clock */
169 cur_clock
= muldiv64(current_time
, RTC_CLOCK_RATE
, get_ticks_per_sec());
170 next_irq_clock
= (cur_clock
& ~(period
- 1)) + period
;
171 s
->next_periodic_time
=
172 muldiv64(next_irq_clock
, get_ticks_per_sec(), RTC_CLOCK_RATE
) + 1;
173 timer_mod(s
->periodic_timer
, s
->next_periodic_time
);
176 s
->irq_coalesced
= 0;
178 timer_del(s
->periodic_timer
);
182 static void rtc_periodic_timer(void *opaque
)
184 RTCState
*s
= opaque
;
186 periodic_timer_update(s
, s
->next_periodic_time
);
187 s
->cmos_data
[RTC_REG_C
] |= REG_C_PF
;
188 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
189 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
191 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
192 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
193 s
->irq_reinject_on_ack_count
= 0;
194 apic_reset_irq_delivered();
195 qemu_irq_raise(s
->irq
);
196 if (!apic_get_irq_delivered()) {
198 rtc_coalesced_timer_update(s
);
199 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
204 qemu_irq_raise(s
->irq
);
208 /* handle update-ended timer */
209 static void check_update_timer(RTCState
*s
)
211 uint64_t next_update_time
;
215 /* From the data sheet: "Holding the dividers in reset prevents
216 * interrupts from operating, while setting the SET bit allows"
217 * them to occur. However, it will prevent an alarm interrupt
218 * from occurring, because the time of day is not updated.
220 if ((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) {
221 timer_del(s
->update_timer
);
224 if ((s
->cmos_data
[RTC_REG_C
] & REG_C_UF
) &&
225 (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
226 timer_del(s
->update_timer
);
229 if ((s
->cmos_data
[RTC_REG_C
] & REG_C_UF
) &&
230 (s
->cmos_data
[RTC_REG_C
] & REG_C_AF
)) {
231 timer_del(s
->update_timer
);
235 guest_nsec
= get_guest_rtc_ns(s
) % NANOSECONDS_PER_SECOND
;
236 /* if UF is clear, reprogram to next second */
237 next_update_time
= qemu_clock_get_ns(rtc_clock
)
238 + NANOSECONDS_PER_SECOND
- guest_nsec
;
240 /* Compute time of next alarm. One second is already accounted
241 * for in next_update_time.
243 next_alarm_sec
= get_next_alarm(s
);
244 s
->next_alarm_time
= next_update_time
+
245 (next_alarm_sec
- 1) * NANOSECONDS_PER_SECOND
;
247 if (s
->cmos_data
[RTC_REG_C
] & REG_C_UF
) {
248 /* UF is set, but AF is clear. Program the timer to target
250 next_update_time
= s
->next_alarm_time
;
252 if (next_update_time
!= timer_expire_time_ns(s
->update_timer
)) {
253 timer_mod(s
->update_timer
, next_update_time
);
257 static inline uint8_t convert_hour(RTCState
*s
, uint8_t hour
)
259 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
261 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
268 static uint64_t get_next_alarm(RTCState
*s
)
270 int32_t alarm_sec
, alarm_min
, alarm_hour
, cur_hour
, cur_min
, cur_sec
;
271 int32_t hour
, min
, sec
;
275 alarm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS_ALARM
]);
276 alarm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES_ALARM
]);
277 alarm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS_ALARM
]);
278 alarm_hour
= alarm_hour
== -1 ? -1 : convert_hour(s
, alarm_hour
);
280 cur_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
281 cur_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
282 cur_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
]);
283 cur_hour
= convert_hour(s
, cur_hour
);
285 if (alarm_hour
== -1) {
286 alarm_hour
= cur_hour
;
287 if (alarm_min
== -1) {
289 if (alarm_sec
== -1) {
290 alarm_sec
= cur_sec
+ 1;
291 } else if (cur_sec
> alarm_sec
) {
294 } else if (cur_min
== alarm_min
) {
295 if (alarm_sec
== -1) {
296 alarm_sec
= cur_sec
+ 1;
298 if (cur_sec
> alarm_sec
) {
302 if (alarm_sec
== SEC_PER_MIN
) {
303 /* wrap to next hour, minutes is not in don't care mode */
307 } else if (cur_min
> alarm_min
) {
310 } else if (cur_hour
== alarm_hour
) {
311 if (alarm_min
== -1) {
313 if (alarm_sec
== -1) {
314 alarm_sec
= cur_sec
+ 1;
315 } else if (cur_sec
> alarm_sec
) {
319 if (alarm_sec
== SEC_PER_MIN
) {
323 /* wrap to next day, hour is not in don't care mode */
324 alarm_min
%= MIN_PER_HOUR
;
325 } else if (cur_min
== alarm_min
) {
326 if (alarm_sec
== -1) {
327 alarm_sec
= cur_sec
+ 1;
329 /* wrap to next day, hours+minutes not in don't care mode */
330 alarm_sec
%= SEC_PER_MIN
;
334 /* values that are still don't care fire at the next min/sec */
335 if (alarm_min
== -1) {
338 if (alarm_sec
== -1) {
342 /* keep values in range */
343 if (alarm_sec
== SEC_PER_MIN
) {
347 if (alarm_min
== MIN_PER_HOUR
) {
351 alarm_hour
%= HOUR_PER_DAY
;
353 hour
= alarm_hour
- cur_hour
;
354 min
= hour
* MIN_PER_HOUR
+ alarm_min
- cur_min
;
355 sec
= min
* SEC_PER_MIN
+ alarm_sec
- cur_sec
;
356 return sec
<= 0 ? sec
+ SEC_PER_DAY
: sec
;
359 static void rtc_update_timer(void *opaque
)
361 RTCState
*s
= opaque
;
362 int32_t irqs
= REG_C_UF
;
365 assert((s
->cmos_data
[RTC_REG_A
] & 0x60) != 0x60);
367 /* UIP might have been latched, update time and clear it. */
369 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
371 if (qemu_clock_get_ns(rtc_clock
) >= s
->next_alarm_time
) {
373 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
374 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC
);
378 new_irqs
= irqs
& ~s
->cmos_data
[RTC_REG_C
];
379 s
->cmos_data
[RTC_REG_C
] |= irqs
;
380 if ((new_irqs
& s
->cmos_data
[RTC_REG_B
]) != 0) {
381 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
382 qemu_irq_raise(s
->irq
);
384 check_update_timer(s
);
387 static void cmos_ioport_write(void *opaque
, hwaddr addr
,
388 uint64_t data
, unsigned size
)
390 RTCState
*s
= opaque
;
392 if ((addr
& 1) == 0) {
393 s
->cmos_index
= data
& 0x7f;
395 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64
"\n",
396 s
->cmos_index
, data
);
397 switch(s
->cmos_index
) {
398 case RTC_SECONDS_ALARM
:
399 case RTC_MINUTES_ALARM
:
400 case RTC_HOURS_ALARM
:
401 s
->cmos_data
[s
->cmos_index
] = data
;
402 check_update_timer(s
);
404 case RTC_IBM_PS2_CENTURY_BYTE
:
405 s
->cmos_index
= RTC_CENTURY
;
411 case RTC_DAY_OF_WEEK
:
412 case RTC_DAY_OF_MONTH
:
415 s
->cmos_data
[s
->cmos_index
] = data
;
416 /* if in set mode, do not update the time */
417 if (rtc_running(s
)) {
419 check_update_timer(s
);
423 if ((data
& 0x60) == 0x60) {
424 if (rtc_running(s
)) {
427 /* What happens to UIP when divider reset is enabled is
428 * unclear from the datasheet. Shouldn't matter much
431 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
432 } else if (((s
->cmos_data
[RTC_REG_A
] & 0x60) == 0x60) &&
433 (data
& 0x70) <= 0x20) {
434 /* when the divider reset is removed, the first update cycle
435 * begins one-half second later*/
436 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
437 s
->offset
= 500000000;
440 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
442 /* UIP bit is read only */
443 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
444 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
445 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
));
446 check_update_timer(s
);
449 if (data
& REG_B_SET
) {
450 /* update cmos to when the rtc was stopping */
451 if (rtc_running(s
)) {
454 /* set mode: reset UIP mode */
455 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
458 /* if disabling set mode, update the time */
459 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) &&
460 (s
->cmos_data
[RTC_REG_A
] & 0x70) <= 0x20) {
461 s
->offset
= get_guest_rtc_ns(s
) % NANOSECONDS_PER_SECOND
;
465 /* if an interrupt flag is already set when the interrupt
466 * becomes enabled, raise an interrupt immediately. */
467 if (data
& s
->cmos_data
[RTC_REG_C
] & REG_C_MASK
) {
468 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
469 qemu_irq_raise(s
->irq
);
471 s
->cmos_data
[RTC_REG_C
] &= ~REG_C_IRQF
;
472 qemu_irq_lower(s
->irq
);
474 s
->cmos_data
[RTC_REG_B
] = data
;
475 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
));
476 check_update_timer(s
);
480 /* cannot write to them */
483 s
->cmos_data
[s
->cmos_index
] = data
;
489 static inline int rtc_to_bcd(RTCState
*s
, int a
)
491 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
494 return ((a
/ 10) << 4) | (a
% 10);
498 static inline int rtc_from_bcd(RTCState
*s
, int a
)
500 if ((a
& 0xc0) == 0xc0) {
503 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
506 return ((a
>> 4) * 10) + (a
& 0x0f);
510 static void rtc_get_time(RTCState
*s
, struct tm
*tm
)
512 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
513 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
514 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
515 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
517 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
521 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
522 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
523 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
525 rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
+
526 rtc_from_bcd(s
, s
->cmos_data
[RTC_CENTURY
]) * 100 - 1900;
529 static QLIST_HEAD(, RTCState
) rtc_devices
=
530 QLIST_HEAD_INITIALIZER(rtc_devices
);
533 void qmp_rtc_reset_reinjection(Error
**errp
)
537 QLIST_FOREACH(s
, &rtc_devices
, link
) {
538 s
->irq_coalesced
= 0;
543 static void rtc_set_time(RTCState
*s
)
547 rtc_get_time(s
, &tm
);
548 s
->base_rtc
= mktimegm(&tm
);
549 s
->last_update
= qemu_clock_get_ns(rtc_clock
);
551 qapi_event_send_rtc_change(qemu_timedate_diff(&tm
), &error_abort
);
554 static void rtc_set_cmos(RTCState
*s
, const struct tm
*tm
)
558 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
559 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
560 if (s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) {
562 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
565 int h
= (tm
->tm_hour
% 12) ? tm
->tm_hour
% 12 : 12;
566 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, h
);
567 if (tm
->tm_hour
>= 12)
568 s
->cmos_data
[RTC_HOURS
] |= 0x80;
570 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
571 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
572 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
573 year
= tm
->tm_year
+ 1900 - s
->base_year
;
574 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
% 100);
575 s
->cmos_data
[RTC_CENTURY
] = rtc_to_bcd(s
, year
/ 100);
578 static void rtc_update_time(RTCState
*s
)
584 guest_nsec
= get_guest_rtc_ns(s
);
585 guest_sec
= guest_nsec
/ NANOSECONDS_PER_SECOND
;
586 gmtime_r(&guest_sec
, &ret
);
588 /* Is SET flag of Register B disabled? */
589 if ((s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) == 0) {
590 rtc_set_cmos(s
, &ret
);
594 static int update_in_progress(RTCState
*s
)
598 if (!rtc_running(s
)) {
601 if (timer_pending(s
->update_timer
)) {
602 int64_t next_update_time
= timer_expire_time_ns(s
->update_timer
);
603 /* Latch UIP until the timer expires. */
604 if (qemu_clock_get_ns(rtc_clock
) >=
605 (next_update_time
- UIP_HOLD_LENGTH
)) {
606 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
611 guest_nsec
= get_guest_rtc_ns(s
);
612 /* UIP bit will be set at last 244us of every second. */
613 if ((guest_nsec
% NANOSECONDS_PER_SECOND
) >=
614 (NANOSECONDS_PER_SECOND
- UIP_HOLD_LENGTH
)) {
620 static uint64_t cmos_ioport_read(void *opaque
, hwaddr addr
,
623 RTCState
*s
= opaque
;
625 if ((addr
& 1) == 0) {
628 switch(s
->cmos_index
) {
629 case RTC_IBM_PS2_CENTURY_BYTE
:
630 s
->cmos_index
= RTC_CENTURY
;
636 case RTC_DAY_OF_WEEK
:
637 case RTC_DAY_OF_MONTH
:
640 /* if not in set mode, calibrate cmos before
642 if (rtc_running(s
)) {
645 ret
= s
->cmos_data
[s
->cmos_index
];
648 if (update_in_progress(s
)) {
649 s
->cmos_data
[s
->cmos_index
] |= REG_A_UIP
;
651 s
->cmos_data
[s
->cmos_index
] &= ~REG_A_UIP
;
653 ret
= s
->cmos_data
[s
->cmos_index
];
656 ret
= s
->cmos_data
[s
->cmos_index
];
657 qemu_irq_lower(s
->irq
);
658 s
->cmos_data
[RTC_REG_C
] = 0x00;
659 if (ret
& (REG_C_UF
| REG_C_AF
)) {
660 check_update_timer(s
);
663 if(s
->irq_coalesced
&&
664 (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) &&
665 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
666 s
->irq_reinject_on_ack_count
++;
667 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
| REG_C_PF
;
668 apic_reset_irq_delivered();
669 DPRINTF_C("cmos: injecting on ack\n");
670 qemu_irq_raise(s
->irq
);
671 if (apic_get_irq_delivered()) {
673 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
680 ret
= s
->cmos_data
[s
->cmos_index
];
683 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
689 void rtc_set_memory(ISADevice
*dev
, int addr
, int val
)
691 RTCState
*s
= MC146818_RTC(dev
);
692 if (addr
>= 0 && addr
<= 127)
693 s
->cmos_data
[addr
] = val
;
696 int rtc_get_memory(ISADevice
*dev
, int addr
)
698 RTCState
*s
= MC146818_RTC(dev
);
699 assert(addr
>= 0 && addr
<= 127);
700 return s
->cmos_data
[addr
];
703 static void rtc_set_date_from_host(ISADevice
*dev
)
705 RTCState
*s
= MC146818_RTC(dev
);
708 qemu_get_timedate(&tm
, 0);
710 s
->base_rtc
= mktimegm(&tm
);
711 s
->last_update
= qemu_clock_get_ns(rtc_clock
);
714 /* set the CMOS date */
715 rtc_set_cmos(s
, &tm
);
718 static int rtc_post_load(void *opaque
, int version_id
)
720 RTCState
*s
= opaque
;
722 if (version_id
<= 2) {
725 check_update_timer(s
);
728 uint64_t now
= qemu_clock_get_ns(rtc_clock
);
729 if (now
< s
->next_periodic_time
||
730 now
> (s
->next_periodic_time
+ get_max_clock_jump())) {
731 periodic_timer_update(s
, qemu_clock_get_ns(rtc_clock
));
735 if (version_id
>= 2) {
736 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
737 rtc_coalesced_timer_update(s
);
744 static bool rtc_irq_reinject_on_ack_count_needed(void *opaque
)
746 RTCState
*s
= (RTCState
*)opaque
;
747 return s
->irq_reinject_on_ack_count
!= 0;
750 static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count
= {
751 .name
= "mc146818rtc/irq_reinject_on_ack_count",
753 .minimum_version_id
= 1,
754 .needed
= rtc_irq_reinject_on_ack_count_needed
,
755 .fields
= (VMStateField
[]) {
756 VMSTATE_UINT16(irq_reinject_on_ack_count
, RTCState
),
757 VMSTATE_END_OF_LIST()
761 static const VMStateDescription vmstate_rtc
= {
762 .name
= "mc146818rtc",
764 .minimum_version_id
= 1,
765 .post_load
= rtc_post_load
,
766 .fields
= (VMStateField
[]) {
767 VMSTATE_BUFFER(cmos_data
, RTCState
),
768 VMSTATE_UINT8(cmos_index
, RTCState
),
770 VMSTATE_TIMER_PTR(periodic_timer
, RTCState
),
771 VMSTATE_INT64(next_periodic_time
, RTCState
),
773 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
774 VMSTATE_UINT32_V(period
, RTCState
, 2),
775 VMSTATE_UINT64_V(base_rtc
, RTCState
, 3),
776 VMSTATE_UINT64_V(last_update
, RTCState
, 3),
777 VMSTATE_INT64_V(offset
, RTCState
, 3),
778 VMSTATE_TIMER_PTR_V(update_timer
, RTCState
, 3),
779 VMSTATE_UINT64_V(next_alarm_time
, RTCState
, 3),
780 VMSTATE_END_OF_LIST()
782 .subsections
= (const VMStateDescription
*[]) {
783 &vmstate_rtc_irq_reinject_on_ack_count
,
788 static void rtc_notify_clock_reset(Notifier
*notifier
, void *data
)
790 RTCState
*s
= container_of(notifier
, RTCState
, clock_reset_notifier
);
791 int64_t now
= *(int64_t *)data
;
793 rtc_set_date_from_host(ISA_DEVICE(s
));
794 periodic_timer_update(s
, now
);
795 check_update_timer(s
);
797 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
798 rtc_coalesced_timer_update(s
);
803 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
804 BIOS will read it and start S3 resume at POST Entry */
805 static void rtc_notify_suspend(Notifier
*notifier
, void *data
)
807 RTCState
*s
= container_of(notifier
, RTCState
, suspend_notifier
);
808 rtc_set_memory(ISA_DEVICE(s
), 0xF, 0xFE);
811 static void rtc_reset(void *opaque
)
813 RTCState
*s
= opaque
;
815 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
816 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
817 check_update_timer(s
);
819 qemu_irq_lower(s
->irq
);
822 if (s
->lost_tick_policy
== LOST_TICK_POLICY_SLEW
) {
823 s
->irq_coalesced
= 0;
824 s
->irq_reinject_on_ack_count
= 0;
829 static const MemoryRegionOps cmos_ops
= {
830 .read
= cmos_ioport_read
,
831 .write
= cmos_ioport_write
,
833 .min_access_size
= 1,
834 .max_access_size
= 1,
836 .endianness
= DEVICE_LITTLE_ENDIAN
,
839 static void rtc_get_date(Object
*obj
, struct tm
*current_tm
, Error
**errp
)
841 RTCState
*s
= MC146818_RTC(obj
);
844 rtc_get_time(s
, current_tm
);
847 static void rtc_realizefn(DeviceState
*dev
, Error
**errp
)
849 ISADevice
*isadev
= ISA_DEVICE(dev
);
850 RTCState
*s
= MC146818_RTC(dev
);
853 s
->cmos_data
[RTC_REG_A
] = 0x26;
854 s
->cmos_data
[RTC_REG_B
] = 0x02;
855 s
->cmos_data
[RTC_REG_C
] = 0x00;
856 s
->cmos_data
[RTC_REG_D
] = 0x80;
858 /* This is for historical reasons. The default base year qdev property
859 * was set to 2000 for most machine types before the century byte was
862 * This if statement means that the century byte will be always 0
863 * (at least until 2079...) for base_year = 1980, but will be set
864 * correctly for base_year = 2000.
866 if (s
->base_year
== 2000) {
870 rtc_set_date_from_host(isadev
);
873 switch (s
->lost_tick_policy
) {
874 case LOST_TICK_POLICY_SLEW
:
876 timer_new_ns(rtc_clock
, rtc_coalesced_timer
, s
);
878 case LOST_TICK_POLICY_DISCARD
:
881 error_setg(errp
, "Invalid lost tick policy.");
886 s
->periodic_timer
= timer_new_ns(rtc_clock
, rtc_periodic_timer
, s
);
887 s
->update_timer
= timer_new_ns(rtc_clock
, rtc_update_timer
, s
);
888 check_update_timer(s
);
890 s
->clock_reset_notifier
.notify
= rtc_notify_clock_reset
;
891 qemu_clock_register_reset_notifier(rtc_clock
,
892 &s
->clock_reset_notifier
);
894 s
->suspend_notifier
.notify
= rtc_notify_suspend
;
895 qemu_register_suspend_notifier(&s
->suspend_notifier
);
897 memory_region_init_io(&s
->io
, OBJECT(s
), &cmos_ops
, s
, "rtc", 2);
898 isa_register_ioport(isadev
, &s
->io
, base
);
900 qdev_set_legacy_instance_id(dev
, base
, 3);
901 qemu_register_reset(rtc_reset
, s
);
903 object_property_add_tm(OBJECT(s
), "date", rtc_get_date
, NULL
);
905 object_property_add_alias(qdev_get_machine(), "rtc-time",
906 OBJECT(s
), "date", NULL
);
909 ISADevice
*rtc_init(ISABus
*bus
, int base_year
, qemu_irq intercept_irq
)
915 isadev
= isa_create(bus
, TYPE_MC146818_RTC
);
916 dev
= DEVICE(isadev
);
917 s
= MC146818_RTC(isadev
);
918 qdev_prop_set_int32(dev
, "base_year", base_year
);
919 qdev_init_nofail(dev
);
921 s
->irq
= intercept_irq
;
923 isa_init_irq(isadev
, &s
->irq
, RTC_ISA_IRQ
);
925 QLIST_INSERT_HEAD(&rtc_devices
, s
, link
);
930 static Property mc146818rtc_properties
[] = {
931 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
932 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState
,
933 lost_tick_policy
, LOST_TICK_POLICY_DISCARD
),
934 DEFINE_PROP_END_OF_LIST(),
937 static void rtc_class_initfn(ObjectClass
*klass
, void *data
)
939 DeviceClass
*dc
= DEVICE_CLASS(klass
);
941 dc
->realize
= rtc_realizefn
;
942 dc
->vmsd
= &vmstate_rtc
;
943 dc
->props
= mc146818rtc_properties
;
944 /* Reason: needs to be wired up by rtc_init() */
945 dc
->cannot_instantiate_with_device_add_yet
= true;
948 static void rtc_finalize(Object
*obj
)
950 object_property_del(qdev_get_machine(), "rtc", NULL
);
953 static const TypeInfo mc146818rtc_info
= {
954 .name
= TYPE_MC146818_RTC
,
955 .parent
= TYPE_ISA_DEVICE
,
956 .instance_size
= sizeof(RTCState
),
957 .class_init
= rtc_class_initfn
,
958 .instance_finalize
= rtc_finalize
,
961 static void mc146818rtc_register_types(void)
963 type_register_static(&mc146818rtc_info
);
966 type_init(mc146818rtc_register_types
)