ui: fix reporting of VNC auth in query-vnc-servers
[qemu/ar7.git] / hw / timer / mc146818rtc.c
blob41654502508f6420369ae188f4172fef5d0de3fc
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu/cutils.h"
26 #include "qemu/bcd.h"
27 #include "hw/hw.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"
36 #ifdef TARGET_I386
37 #include "hw/i386/apic.h"
38 #endif
40 //#define DEBUG_CMOS
41 //#define DEBUG_COALESCED
43 #ifdef DEBUG_CMOS
44 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
45 #else
46 # define CMOS_DPRINTF(format, ...) do { } while (0)
47 #endif
49 #ifdef DEBUG_COALESCED
50 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
51 #else
52 # define DPRINTF_C(format, ...) do { } while (0)
53 #endif
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 {
68 ISADevice parent_obj;
70 MemoryRegion io;
71 uint8_t cmos_data[128];
72 uint8_t cmos_index;
73 int32_t base_year;
74 uint64_t base_rtc;
75 uint64_t last_update;
76 int64_t offset;
77 qemu_irq irq;
78 int it_shift;
79 /* periodic timer */
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;
87 uint32_t period;
88 QEMUTimer *coalesced_timer;
89 Notifier clock_reset_notifier;
90 LostTickPolicy lost_tick_policy;
91 Notifier suspend_notifier;
92 QLIST_ENTRY(RTCState) link;
93 } RTCState;
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 #ifdef TARGET_I386
116 static void rtc_coalesced_timer_update(RTCState *s)
118 if (s->irq_coalesced == 0) {
119 timer_del(s->coalesced_timer);
120 } else {
121 /* divide each RTC interval to 2 - 8 smaller intervals */
122 int c = MIN(s->irq_coalesced, 7) + 1;
123 int64_t next_clock = qemu_clock_get_ns(rtc_clock) +
124 muldiv64(s->period / c, NANOSECONDS_PER_SECOND, RTC_CLOCK_RATE);
125 timer_mod(s->coalesced_timer, next_clock);
129 static void rtc_coalesced_timer(void *opaque)
131 RTCState *s = opaque;
133 if (s->irq_coalesced != 0) {
134 apic_reset_irq_delivered();
135 s->cmos_data[RTC_REG_C] |= 0xc0;
136 DPRINTF_C("cmos: injecting from timer\n");
137 qemu_irq_raise(s->irq);
138 if (apic_get_irq_delivered()) {
139 s->irq_coalesced--;
140 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
141 s->irq_coalesced);
145 rtc_coalesced_timer_update(s);
147 #endif
149 /* handle periodic timer */
150 static void periodic_timer_update(RTCState *s, int64_t current_time)
152 int period_code, period;
153 int64_t cur_clock, next_irq_clock;
155 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
156 if (period_code != 0
157 && (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
158 if (period_code <= 2)
159 period_code += 7;
160 /* period in 32 Khz cycles */
161 period = 1 << (period_code - 1);
162 #ifdef TARGET_I386
163 if (period != s->period) {
164 s->irq_coalesced = (s->irq_coalesced * s->period) / period;
165 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced);
167 s->period = period;
168 #endif
169 /* compute 32 khz clock */
170 cur_clock =
171 muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
173 next_irq_clock = (cur_clock & ~(period - 1)) + period;
174 s->next_periodic_time = muldiv64(next_irq_clock, NANOSECONDS_PER_SECOND,
175 RTC_CLOCK_RATE) + 1;
176 timer_mod(s->periodic_timer, s->next_periodic_time);
177 } else {
178 #ifdef TARGET_I386
179 s->irq_coalesced = 0;
180 #endif
181 timer_del(s->periodic_timer);
185 static void rtc_periodic_timer(void *opaque)
187 RTCState *s = opaque;
189 periodic_timer_update(s, s->next_periodic_time);
190 s->cmos_data[RTC_REG_C] |= REG_C_PF;
191 if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
192 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
193 #ifdef TARGET_I386
194 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
195 if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
196 s->irq_reinject_on_ack_count = 0;
197 apic_reset_irq_delivered();
198 qemu_irq_raise(s->irq);
199 if (!apic_get_irq_delivered()) {
200 s->irq_coalesced++;
201 rtc_coalesced_timer_update(s);
202 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
203 s->irq_coalesced);
205 } else
206 #endif
207 qemu_irq_raise(s->irq);
211 /* handle update-ended timer */
212 static void check_update_timer(RTCState *s)
214 uint64_t next_update_time;
215 uint64_t guest_nsec;
216 int next_alarm_sec;
218 /* From the data sheet: "Holding the dividers in reset prevents
219 * interrupts from operating, while setting the SET bit allows"
220 * them to occur. However, it will prevent an alarm interrupt
221 * from occurring, because the time of day is not updated.
223 if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) {
224 timer_del(s->update_timer);
225 return;
227 if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
228 (s->cmos_data[RTC_REG_B] & REG_B_SET)) {
229 timer_del(s->update_timer);
230 return;
232 if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
233 (s->cmos_data[RTC_REG_C] & REG_C_AF)) {
234 timer_del(s->update_timer);
235 return;
238 guest_nsec = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
239 /* if UF is clear, reprogram to next second */
240 next_update_time = qemu_clock_get_ns(rtc_clock)
241 + NANOSECONDS_PER_SECOND - guest_nsec;
243 /* Compute time of next alarm. One second is already accounted
244 * for in next_update_time.
246 next_alarm_sec = get_next_alarm(s);
247 s->next_alarm_time = next_update_time +
248 (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND;
250 if (s->cmos_data[RTC_REG_C] & REG_C_UF) {
251 /* UF is set, but AF is clear. Program the timer to target
252 * the alarm time. */
253 next_update_time = s->next_alarm_time;
255 if (next_update_time != timer_expire_time_ns(s->update_timer)) {
256 timer_mod(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)) {
263 hour %= 12;
264 if (s->cmos_data[RTC_HOURS] & 0x80) {
265 hour += 12;
268 return hour;
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;
276 rtc_update_time(s);
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) {
291 alarm_min = cur_min;
292 if (alarm_sec == -1) {
293 alarm_sec = cur_sec + 1;
294 } else if (cur_sec > alarm_sec) {
295 alarm_min++;
297 } else if (cur_min == alarm_min) {
298 if (alarm_sec == -1) {
299 alarm_sec = cur_sec + 1;
300 } else {
301 if (cur_sec > alarm_sec) {
302 alarm_hour++;
305 if (alarm_sec == SEC_PER_MIN) {
306 /* wrap to next hour, minutes is not in don't care mode */
307 alarm_sec = 0;
308 alarm_hour++;
310 } else if (cur_min > alarm_min) {
311 alarm_hour++;
313 } else if (cur_hour == alarm_hour) {
314 if (alarm_min == -1) {
315 alarm_min = cur_min;
316 if (alarm_sec == -1) {
317 alarm_sec = cur_sec + 1;
318 } else if (cur_sec > alarm_sec) {
319 alarm_min++;
322 if (alarm_sec == SEC_PER_MIN) {
323 alarm_sec = 0;
324 alarm_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) {
339 alarm_min = 0;
341 if (alarm_sec == -1) {
342 alarm_sec = 0;
345 /* keep values in range */
346 if (alarm_sec == SEC_PER_MIN) {
347 alarm_sec = 0;
348 alarm_min++;
350 if (alarm_min == MIN_PER_HOUR) {
351 alarm_min = 0;
352 alarm_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;
366 int32_t new_irqs;
368 assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60);
370 /* UIP might have been latched, update time and clear it. */
371 rtc_update_time(s);
372 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
374 if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) {
375 irqs |= REG_C_AF;
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;
397 } else {
398 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64 "\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);
406 break;
407 case RTC_IBM_PS2_CENTURY_BYTE:
408 s->cmos_index = RTC_CENTURY;
409 /* fall through */
410 case RTC_CENTURY:
411 case RTC_SECONDS:
412 case RTC_MINUTES:
413 case RTC_HOURS:
414 case RTC_DAY_OF_WEEK:
415 case RTC_DAY_OF_MONTH:
416 case RTC_MONTH:
417 case RTC_YEAR:
418 s->cmos_data[s->cmos_index] = data;
419 /* if in set mode, do not update the time */
420 if (rtc_running(s)) {
421 rtc_set_time(s);
422 check_update_timer(s);
424 break;
425 case RTC_REG_A:
426 if ((data & 0x60) == 0x60) {
427 if (rtc_running(s)) {
428 rtc_update_time(s);
430 /* What happens to UIP when divider reset is enabled is
431 * unclear from the datasheet. Shouldn't matter much
432 * though.
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;
441 rtc_set_time(s);
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_clock_get_ns(rtc_clock));
449 check_update_timer(s);
450 break;
451 case RTC_REG_B:
452 if (data & REG_B_SET) {
453 /* update cmos to when the rtc was stopping */
454 if (rtc_running(s)) {
455 rtc_update_time(s);
457 /* set mode: reset UIP mode */
458 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
459 data &= ~REG_B_UIE;
460 } else {
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) % NANOSECONDS_PER_SECOND;
465 rtc_set_time(s);
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);
473 } else {
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_clock_get_ns(rtc_clock));
479 check_update_timer(s);
480 break;
481 case RTC_REG_C:
482 case RTC_REG_D:
483 /* cannot write to them */
484 break;
485 default:
486 s->cmos_data[s->cmos_index] = data;
487 break;
492 static inline int rtc_to_bcd(RTCState *s, int a)
494 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
495 return a;
496 } else {
497 return ((a / 10) << 4) | (a % 10);
501 static inline int rtc_from_bcd(RTCState *s, int a)
503 if ((a & 0xc0) == 0xc0) {
504 return -1;
506 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
507 return a;
508 } else {
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)) {
519 tm->tm_hour %= 12;
520 if (s->cmos_data[RTC_HOURS] & 0x80) {
521 tm->tm_hour += 12;
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;
527 tm->tm_year =
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 QLIST_HEAD(, RTCState) rtc_devices =
533 QLIST_HEAD_INITIALIZER(rtc_devices);
535 #ifdef TARGET_I386
536 void qmp_rtc_reset_reinjection(Error **errp)
538 RTCState *s;
540 QLIST_FOREACH(s, &rtc_devices, link) {
541 s->irq_coalesced = 0;
544 #endif
546 static void rtc_set_time(RTCState *s)
548 struct tm tm;
550 rtc_get_time(s, &tm);
551 s->base_rtc = mktimegm(&tm);
552 s->last_update = qemu_clock_get_ns(rtc_clock);
554 qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
557 static void rtc_set_cmos(RTCState *s, const struct tm *tm)
559 int year;
561 s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
562 s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
563 if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
564 /* 24 hour format */
565 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
566 } else {
567 /* 12 hour format */
568 int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
569 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
570 if (tm->tm_hour >= 12)
571 s->cmos_data[RTC_HOURS] |= 0x80;
573 s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
574 s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
575 s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
576 year = tm->tm_year + 1900 - s->base_year;
577 s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100);
578 s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100);
581 static void rtc_update_time(RTCState *s)
583 struct tm ret;
584 time_t guest_sec;
585 int64_t guest_nsec;
587 guest_nsec = get_guest_rtc_ns(s);
588 guest_sec = guest_nsec / NANOSECONDS_PER_SECOND;
589 gmtime_r(&guest_sec, &ret);
591 /* Is SET flag of Register B disabled? */
592 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) {
593 rtc_set_cmos(s, &ret);
597 static int update_in_progress(RTCState *s)
599 int64_t guest_nsec;
601 if (!rtc_running(s)) {
602 return 0;
604 if (timer_pending(s->update_timer)) {
605 int64_t next_update_time = timer_expire_time_ns(s->update_timer);
606 /* Latch UIP until the timer expires. */
607 if (qemu_clock_get_ns(rtc_clock) >=
608 (next_update_time - UIP_HOLD_LENGTH)) {
609 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
610 return 1;
614 guest_nsec = get_guest_rtc_ns(s);
615 /* UIP bit will be set at last 244us of every second. */
616 if ((guest_nsec % NANOSECONDS_PER_SECOND) >=
617 (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) {
618 return 1;
620 return 0;
623 static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
624 unsigned size)
626 RTCState *s = opaque;
627 int ret;
628 if ((addr & 1) == 0) {
629 return 0xff;
630 } else {
631 switch(s->cmos_index) {
632 case RTC_IBM_PS2_CENTURY_BYTE:
633 s->cmos_index = RTC_CENTURY;
634 /* fall through */
635 case RTC_CENTURY:
636 case RTC_SECONDS:
637 case RTC_MINUTES:
638 case RTC_HOURS:
639 case RTC_DAY_OF_WEEK:
640 case RTC_DAY_OF_MONTH:
641 case RTC_MONTH:
642 case RTC_YEAR:
643 /* if not in set mode, calibrate cmos before
644 * reading*/
645 if (rtc_running(s)) {
646 rtc_update_time(s);
648 ret = s->cmos_data[s->cmos_index];
649 break;
650 case RTC_REG_A:
651 if (update_in_progress(s)) {
652 s->cmos_data[s->cmos_index] |= REG_A_UIP;
653 } else {
654 s->cmos_data[s->cmos_index] &= ~REG_A_UIP;
656 ret = s->cmos_data[s->cmos_index];
657 break;
658 case RTC_REG_C:
659 ret = s->cmos_data[s->cmos_index];
660 qemu_irq_lower(s->irq);
661 s->cmos_data[RTC_REG_C] = 0x00;
662 if (ret & (REG_C_UF | REG_C_AF)) {
663 check_update_timer(s);
665 #ifdef TARGET_I386
666 if(s->irq_coalesced &&
667 (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
668 s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
669 s->irq_reinject_on_ack_count++;
670 s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
671 apic_reset_irq_delivered();
672 DPRINTF_C("cmos: injecting on ack\n");
673 qemu_irq_raise(s->irq);
674 if (apic_get_irq_delivered()) {
675 s->irq_coalesced--;
676 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
677 s->irq_coalesced);
680 #endif
681 break;
682 default:
683 ret = s->cmos_data[s->cmos_index];
684 break;
686 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
687 s->cmos_index, ret);
688 return ret;
692 void rtc_set_memory(ISADevice *dev, int addr, int val)
694 RTCState *s = MC146818_RTC(dev);
695 if (addr >= 0 && addr <= 127)
696 s->cmos_data[addr] = val;
699 int rtc_get_memory(ISADevice *dev, int addr)
701 RTCState *s = MC146818_RTC(dev);
702 assert(addr >= 0 && addr <= 127);
703 return s->cmos_data[addr];
706 static void rtc_set_date_from_host(ISADevice *dev)
708 RTCState *s = MC146818_RTC(dev);
709 struct tm tm;
711 qemu_get_timedate(&tm, 0);
713 s->base_rtc = mktimegm(&tm);
714 s->last_update = qemu_clock_get_ns(rtc_clock);
715 s->offset = 0;
717 /* set the CMOS date */
718 rtc_set_cmos(s, &tm);
721 static void rtc_pre_save(void *opaque)
723 RTCState *s = opaque;
725 rtc_update_time(s);
728 static int rtc_post_load(void *opaque, int version_id)
730 RTCState *s = opaque;
732 if (version_id <= 2 || rtc_clock == QEMU_CLOCK_REALTIME) {
733 rtc_set_time(s);
734 s->offset = 0;
735 check_update_timer(s);
738 /* The periodic timer is deterministic in record/replay mode,
739 * so there is no need to update it after loading the vmstate.
740 * Reading RTC here would misalign record and replay.
742 if (replay_mode == REPLAY_MODE_NONE) {
743 uint64_t now = qemu_clock_get_ns(rtc_clock);
744 if (now < s->next_periodic_time ||
745 now > (s->next_periodic_time + get_max_clock_jump())) {
746 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock));
750 #ifdef TARGET_I386
751 if (version_id >= 2) {
752 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
753 rtc_coalesced_timer_update(s);
756 #endif
757 return 0;
760 static bool rtc_irq_reinject_on_ack_count_needed(void *opaque)
762 RTCState *s = (RTCState *)opaque;
763 return s->irq_reinject_on_ack_count != 0;
766 static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count = {
767 .name = "mc146818rtc/irq_reinject_on_ack_count",
768 .version_id = 1,
769 .minimum_version_id = 1,
770 .needed = rtc_irq_reinject_on_ack_count_needed,
771 .fields = (VMStateField[]) {
772 VMSTATE_UINT16(irq_reinject_on_ack_count, RTCState),
773 VMSTATE_END_OF_LIST()
777 static const VMStateDescription vmstate_rtc = {
778 .name = "mc146818rtc",
779 .version_id = 3,
780 .minimum_version_id = 1,
781 .pre_save = rtc_pre_save,
782 .post_load = rtc_post_load,
783 .fields = (VMStateField[]) {
784 VMSTATE_BUFFER(cmos_data, RTCState),
785 VMSTATE_UINT8(cmos_index, RTCState),
786 VMSTATE_UNUSED(7*4),
787 VMSTATE_TIMER_PTR(periodic_timer, RTCState),
788 VMSTATE_INT64(next_periodic_time, RTCState),
789 VMSTATE_UNUSED(3*8),
790 VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
791 VMSTATE_UINT32_V(period, RTCState, 2),
792 VMSTATE_UINT64_V(base_rtc, RTCState, 3),
793 VMSTATE_UINT64_V(last_update, RTCState, 3),
794 VMSTATE_INT64_V(offset, RTCState, 3),
795 VMSTATE_TIMER_PTR_V(update_timer, RTCState, 3),
796 VMSTATE_UINT64_V(next_alarm_time, RTCState, 3),
797 VMSTATE_END_OF_LIST()
799 .subsections = (const VMStateDescription*[]) {
800 &vmstate_rtc_irq_reinject_on_ack_count,
801 NULL
805 static void rtc_notify_clock_reset(Notifier *notifier, void *data)
807 RTCState *s = container_of(notifier, RTCState, clock_reset_notifier);
808 int64_t now = *(int64_t *)data;
810 rtc_set_date_from_host(ISA_DEVICE(s));
811 periodic_timer_update(s, now);
812 check_update_timer(s);
813 #ifdef TARGET_I386
814 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
815 rtc_coalesced_timer_update(s);
817 #endif
820 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
821 BIOS will read it and start S3 resume at POST Entry */
822 static void rtc_notify_suspend(Notifier *notifier, void *data)
824 RTCState *s = container_of(notifier, RTCState, suspend_notifier);
825 rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE);
828 static void rtc_reset(void *opaque)
830 RTCState *s = opaque;
832 s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
833 s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
834 check_update_timer(s);
836 qemu_irq_lower(s->irq);
838 #ifdef TARGET_I386
839 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
840 s->irq_coalesced = 0;
841 s->irq_reinject_on_ack_count = 0;
843 #endif
846 static const MemoryRegionOps cmos_ops = {
847 .read = cmos_ioport_read,
848 .write = cmos_ioport_write,
849 .impl = {
850 .min_access_size = 1,
851 .max_access_size = 1,
853 .endianness = DEVICE_LITTLE_ENDIAN,
856 static void rtc_get_date(Object *obj, struct tm *current_tm, Error **errp)
858 RTCState *s = MC146818_RTC(obj);
860 rtc_update_time(s);
861 rtc_get_time(s, current_tm);
864 static void rtc_realizefn(DeviceState *dev, Error **errp)
866 ISADevice *isadev = ISA_DEVICE(dev);
867 RTCState *s = MC146818_RTC(dev);
868 int base = 0x70;
870 s->cmos_data[RTC_REG_A] = 0x26;
871 s->cmos_data[RTC_REG_B] = 0x02;
872 s->cmos_data[RTC_REG_C] = 0x00;
873 s->cmos_data[RTC_REG_D] = 0x80;
875 /* This is for historical reasons. The default base year qdev property
876 * was set to 2000 for most machine types before the century byte was
877 * implemented.
879 * This if statement means that the century byte will be always 0
880 * (at least until 2079...) for base_year = 1980, but will be set
881 * correctly for base_year = 2000.
883 if (s->base_year == 2000) {
884 s->base_year = 0;
887 rtc_set_date_from_host(isadev);
889 #ifdef TARGET_I386
890 switch (s->lost_tick_policy) {
891 case LOST_TICK_POLICY_SLEW:
892 s->coalesced_timer =
893 timer_new_ns(rtc_clock, rtc_coalesced_timer, s);
894 break;
895 case LOST_TICK_POLICY_DISCARD:
896 break;
897 default:
898 error_setg(errp, "Invalid lost tick policy.");
899 return;
901 #endif
903 s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s);
904 s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s);
905 check_update_timer(s);
907 s->clock_reset_notifier.notify = rtc_notify_clock_reset;
908 qemu_clock_register_reset_notifier(rtc_clock,
909 &s->clock_reset_notifier);
911 s->suspend_notifier.notify = rtc_notify_suspend;
912 qemu_register_suspend_notifier(&s->suspend_notifier);
914 memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2);
915 isa_register_ioport(isadev, &s->io, base);
917 qdev_set_legacy_instance_id(dev, base, 3);
918 qemu_register_reset(rtc_reset, s);
920 object_property_add_tm(OBJECT(s), "date", rtc_get_date, NULL);
922 object_property_add_alias(qdev_get_machine(), "rtc-time",
923 OBJECT(s), "date", NULL);
925 qdev_init_gpio_out(dev, &s->irq, 1);
928 ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
930 DeviceState *dev;
931 ISADevice *isadev;
932 RTCState *s;
934 isadev = isa_create(bus, TYPE_MC146818_RTC);
935 dev = DEVICE(isadev);
936 s = MC146818_RTC(isadev);
937 qdev_prop_set_int32(dev, "base_year", base_year);
938 qdev_init_nofail(dev);
939 if (intercept_irq) {
940 qdev_connect_gpio_out(dev, 0, intercept_irq);
941 } else {
942 isa_connect_gpio_out(isadev, 0, RTC_ISA_IRQ);
944 QLIST_INSERT_HEAD(&rtc_devices, s, link);
946 return isadev;
949 static Property mc146818rtc_properties[] = {
950 DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
951 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
952 lost_tick_policy, LOST_TICK_POLICY_DISCARD),
953 DEFINE_PROP_END_OF_LIST(),
956 static void rtc_resetdev(DeviceState *d)
958 RTCState *s = MC146818_RTC(d);
960 /* Reason: VM do suspend self will set 0xfe
961 * Reset any values other than 0xfe(Guest suspend case) */
962 if (s->cmos_data[0x0f] != 0xfe) {
963 s->cmos_data[0x0f] = 0x00;
967 static void rtc_class_initfn(ObjectClass *klass, void *data)
969 DeviceClass *dc = DEVICE_CLASS(klass);
971 dc->realize = rtc_realizefn;
972 dc->reset = rtc_resetdev;
973 dc->vmsd = &vmstate_rtc;
974 dc->props = mc146818rtc_properties;
975 /* Reason: needs to be wired up by rtc_init() */
976 dc->cannot_instantiate_with_device_add_yet = true;
979 static void rtc_finalize(Object *obj)
981 object_property_del(qdev_get_machine(), "rtc", NULL);
984 static const TypeInfo mc146818rtc_info = {
985 .name = TYPE_MC146818_RTC,
986 .parent = TYPE_ISA_DEVICE,
987 .instance_size = sizeof(RTCState),
988 .class_init = rtc_class_initfn,
989 .instance_finalize = rtc_finalize,
992 static void mc146818rtc_register_types(void)
994 type_register_static(&mc146818rtc_info);
997 type_init(mc146818rtc_register_types)