]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/rtc/mc146818rtc.c
linux-user, mips64: add syscall table generation support
[thirdparty/qemu.git] / hw / rtc / mc146818rtc.c
CommitLineData
80cabfad
FB
1/*
2 * QEMU MC146818 RTC emulation
5fafdf24 3 *
80cabfad 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
80cabfad
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
e688df6b 24
b6a0aa05 25#include "qemu/osdep.h"
a8d25326 26#include "qemu-common.h"
f348b6d1 27#include "qemu/cutils.h"
0b8fa32f 28#include "qemu/module.h"
f348b6d1 29#include "qemu/bcd.h"
64552b6b 30#include "hw/irq.h"
a27bd6c7 31#include "hw/qdev-properties.h"
1de7afc9 32#include "qemu/timer.h"
9c17d615 33#include "sysemu/sysemu.h"
1dfb1b2d 34#include "sysemu/replay.h"
71e8a915 35#include "sysemu/reset.h"
54d31236 36#include "sysemu/runstate.h"
bcdb9064 37#include "hw/rtc/mc146818rtc.h"
7ffcb73d 38#include "hw/rtc/mc146818rtc_regs.h"
d6454270 39#include "migration/vmstate.h"
e688df6b 40#include "qapi/error.h"
b0227cdb 41#include "qapi/qapi-events-misc-target.h"
7b1b5d19 42#include "qapi/visitor.h"
f98167ea 43#include "exec/address-spaces.h"
673652a7 44#include "hw/rtc/mc146818rtc_regs.h"
80cabfad 45
d362e757 46#ifdef TARGET_I386
3c13c4be 47#include "qapi/qapi-commands-misc-target.h"
0d09e41a 48#include "hw/i386/apic.h"
d362e757
JK
49#endif
50
80cabfad 51//#define DEBUG_CMOS
aa6f63ff 52//#define DEBUG_COALESCED
80cabfad 53
ec51e364
IY
54#ifdef DEBUG_CMOS
55# define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
56#else
57# define CMOS_DPRINTF(format, ...) do { } while (0)
58#endif
59
aa6f63ff
BS
60#ifdef DEBUG_COALESCED
61# define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
62#else
63# define DPRINTF_C(format, ...) do { } while (0)
64#endif
65
00cf5774
PB
66#define SEC_PER_MIN 60
67#define MIN_PER_HOUR 60
68#define SEC_PER_HOUR 3600
69#define HOUR_PER_DAY 24
70#define SEC_PER_DAY 86400
56038ef6 71
dd17765b 72#define RTC_REINJECT_ON_ACK_COUNT 20
e46deaba 73#define RTC_CLOCK_RATE 32768
13566fe3 74#define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
ba32edab 75
dff38e7b 76static void rtc_set_time(RTCState *s);
56038ef6 77static void rtc_update_time(RTCState *s);
e2826cf4 78static void rtc_set_cmos(RTCState *s, const struct tm *tm);
56038ef6 79static inline int rtc_from_bcd(RTCState *s, int a);
00cf5774 80static uint64_t get_next_alarm(RTCState *s);
56038ef6 81
41a9b8b2
YZ
82static inline bool rtc_running(RTCState *s)
83{
84 return (!(s->cmos_data[RTC_REG_B] & REG_B_SET) &&
85 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20);
86}
87
56038ef6
YZ
88static uint64_t get_guest_rtc_ns(RTCState *s)
89{
884f17c2 90 uint64_t guest_clock = qemu_clock_get_ns(rtc_clock);
56038ef6 91
9be38598 92 return s->base_rtc * NANOSECONDS_PER_SECOND +
73bcb24d 93 guest_clock - s->last_update + s->offset;
56038ef6 94}
dff38e7b 95
93b66569
AL
96static void rtc_coalesced_timer_update(RTCState *s)
97{
98 if (s->irq_coalesced == 0) {
bc72ad67 99 timer_del(s->coalesced_timer);
93b66569
AL
100 } else {
101 /* divide each RTC interval to 2 - 8 smaller intervals */
7cd9681b 102 int c = MIN(s->irq_coalesced, 7) + 1;
884f17c2 103 int64_t next_clock = qemu_clock_get_ns(rtc_clock) +
bd618eab 104 periodic_clock_to_ns(s->period / c);
bc72ad67 105 timer_mod(s->coalesced_timer, next_clock);
93b66569
AL
106 }
107}
108
e0c8b950
XG
109static QLIST_HEAD(, RTCState) rtc_devices =
110 QLIST_HEAD_INITIALIZER(rtc_devices);
111
388ad5d2 112#ifdef TARGET_I386
e0c8b950
XG
113void qmp_rtc_reset_reinjection(Error **errp)
114{
115 RTCState *s;
116
117 QLIST_FOREACH(s, &rtc_devices, link) {
118 s->irq_coalesced = 0;
119 }
120}
121
122static bool rtc_policy_slew_deliver_irq(RTCState *s)
123{
124 apic_reset_irq_delivered();
125 qemu_irq_raise(s->irq);
126 return apic_get_irq_delivered();
127}
128
93b66569
AL
129static void rtc_coalesced_timer(void *opaque)
130{
131 RTCState *s = opaque;
132
133 if (s->irq_coalesced != 0) {
93b66569 134 s->cmos_data[RTC_REG_C] |= 0xc0;
aa6f63ff 135 DPRINTF_C("cmos: injecting from timer\n");
e0c8b950 136 if (rtc_policy_slew_deliver_irq(s)) {
93b66569 137 s->irq_coalesced--;
aa6f63ff
BS
138 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
139 s->irq_coalesced);
93b66569
AL
140 }
141 }
142
143 rtc_coalesced_timer_update(s);
144}
e0c8b950
XG
145#else
146static bool rtc_policy_slew_deliver_irq(RTCState *s)
147{
148 assert(0);
149 return false;
150}
93b66569
AL
151#endif
152
369b4135 153static uint32_t rtc_periodic_clock_ticks(RTCState *s)
dff38e7b 154{
369b4135
TY
155 int period_code;
156
157 if (!(s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
158 return 0;
159 }
dff38e7b
FB
160
161 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
369b4135 162
bd618eab 163 return periodic_period_to_clock(period_code);
369b4135
TY
164}
165
166/*
167 * handle periodic timer. @old_period indicates the periodic timer update
168 * is just due to period adjustment.
169 */
170static void
7a3e29b1 171periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period, bool period_change)
369b4135
TY
172{
173 uint32_t period;
174 int64_t cur_clock, next_irq_clock, lost_clock = 0;
175
176 period = rtc_periodic_clock_ticks(s);
7a3e29b1
PB
177 s->period = period;
178
b429de73
MT
179 if (!period) {
180 s->irq_coalesced = 0;
181 timer_del(s->periodic_timer);
182 return;
183 }
73bcb24d 184
b429de73
MT
185 /* compute 32 khz clock */
186 cur_clock =
187 muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
188
189 /*
190 * if the periodic timer's update is due to period re-configuration,
191 * we should count the clock since last interrupt.
192 */
7a3e29b1 193 if (old_period && period_change) {
b429de73
MT
194 int64_t last_periodic_clock, next_periodic_clock;
195
196 next_periodic_clock = muldiv64(s->next_periodic_time,
197 RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
198 last_periodic_clock = next_periodic_clock - old_period;
199 lost_clock = cur_clock - last_periodic_clock;
200 assert(lost_clock >= 0);
3ae32adf 201 }
369b4135 202
3ae32adf
PB
203 /*
204 * s->irq_coalesced can change for two reasons:
205 *
206 * a) if one or more periodic timer interrupts have been lost,
207 * lost_clock will be more that a period.
208 *
209 * b) when the period may be reconfigured, we expect the OS to
210 * treat delayed tick as the new period. So, when switching
211 * from a shorter to a longer period, scale down the missing,
212 * because the OS will treat past delayed ticks as longer
213 * (leftovers are put back into lost_clock). When switching
214 * to a shorter period, scale up the missing ticks since the
215 * OS handler will treat past delayed ticks as shorter.
216 */
217 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
218 uint32_t old_irq_coalesced = s->irq_coalesced;
219
3ae32adf
PB
220 lost_clock += old_irq_coalesced * old_period;
221 s->irq_coalesced = lost_clock / s->period;
222 lost_clock %= s->period;
223 if (old_irq_coalesced != s->irq_coalesced ||
224 old_period != s->period) {
225 DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
226 "period scaled from %d to %d\n", old_irq_coalesced,
227 s->irq_coalesced, old_period, s->period);
228 rtc_coalesced_timer_update(s);
229 }
230 } else {
369b4135 231 /*
3ae32adf
PB
232 * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
233 * is not used, we should make the time progress anyway.
369b4135 234 */
3ae32adf 235 lost_clock = MIN(lost_clock, period);
b429de73 236 }
369b4135 237
b429de73 238 assert(lost_clock >= 0 && lost_clock <= period);
369b4135 239
b429de73
MT
240 next_irq_clock = cur_clock + period - lost_clock;
241 s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
242 timer_mod(s->periodic_timer, s->next_periodic_time);
dff38e7b
FB
243}
244
245static void rtc_periodic_timer(void *opaque)
246{
247 RTCState *s = opaque;
248
7a3e29b1 249 periodic_timer_update(s, s->next_periodic_time, s->period, false);
663447d4 250 s->cmos_data[RTC_REG_C] |= REG_C_PF;
100d9891 251 if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
663447d4 252 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
104059da 253 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
ba32edab 254 if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
e0c8b950
XG
255 s->irq_reinject_on_ack_count = 0;
256 if (!rtc_policy_slew_deliver_irq(s)) {
93b66569
AL
257 s->irq_coalesced++;
258 rtc_coalesced_timer_update(s);
aa6f63ff
BS
259 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
260 s->irq_coalesced);
93b66569
AL
261 }
262 } else
e0c8b950 263 qemu_irq_raise(s->irq);
100d9891 264 }
dff38e7b 265}
80cabfad 266
56038ef6
YZ
267/* handle update-ended timer */
268static void check_update_timer(RTCState *s)
269{
270 uint64_t next_update_time;
271 uint64_t guest_nsec;
00cf5774 272 int next_alarm_sec;
56038ef6 273
41a9b8b2
YZ
274 /* From the data sheet: "Holding the dividers in reset prevents
275 * interrupts from operating, while setting the SET bit allows"
6a51d83a 276 * them to occur.
56038ef6 277 */
41a9b8b2 278 if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) {
33f21e4f 279 assert((s->cmos_data[RTC_REG_A] & REG_A_UIP) == 0);
bc72ad67 280 timer_del(s->update_timer);
41a9b8b2
YZ
281 return;
282 }
56038ef6 283
13566fe3 284 guest_nsec = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
884f17c2 285 next_update_time = qemu_clock_get_ns(rtc_clock)
13566fe3 286 + NANOSECONDS_PER_SECOND - guest_nsec;
00cf5774
PB
287
288 /* Compute time of next alarm. One second is already accounted
289 * for in next_update_time.
290 */
291 next_alarm_sec = get_next_alarm(s);
13566fe3
SH
292 s->next_alarm_time = next_update_time +
293 (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND;
00cf5774 294
33f21e4f
PB
295 /* If update_in_progress latched the UIP bit, we must keep the timer
296 * programmed to the next second, so that UIP is cleared. Otherwise,
297 * if UF is already set, we might be able to optimize.
298 */
299 if (!(s->cmos_data[RTC_REG_A] & REG_A_UIP) &&
300 (s->cmos_data[RTC_REG_C] & REG_C_UF)) {
6a51d83a
PB
301 /* If AF cannot change (i.e. either it is set already, or
302 * SET=1 and then the time is not updated), nothing to do.
303 */
304 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) ||
305 (s->cmos_data[RTC_REG_C] & REG_C_AF)) {
306 timer_del(s->update_timer);
307 return;
308 }
309
00cf5774
PB
310 /* UF is set, but AF is clear. Program the timer to target
311 * the alarm time. */
312 next_update_time = s->next_alarm_time;
313 }
e93379b0 314 if (next_update_time != timer_expire_time_ns(s->update_timer)) {
bc72ad67 315 timer_mod(s->update_timer, next_update_time);
56038ef6
YZ
316 }
317}
318
319static inline uint8_t convert_hour(RTCState *s, uint8_t hour)
320{
321 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
322 hour %= 12;
323 if (s->cmos_data[RTC_HOURS] & 0x80) {
324 hour += 12;
325 }
326 }
327 return hour;
328}
329
00cf5774 330static uint64_t get_next_alarm(RTCState *s)
56038ef6 331{
00cf5774
PB
332 int32_t alarm_sec, alarm_min, alarm_hour, cur_hour, cur_min, cur_sec;
333 int32_t hour, min, sec;
334
335 rtc_update_time(s);
56038ef6
YZ
336
337 alarm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]);
338 alarm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]);
339 alarm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]);
00cf5774 340 alarm_hour = alarm_hour == -1 ? -1 : convert_hour(s, alarm_hour);
56038ef6
YZ
341
342 cur_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
343 cur_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
344 cur_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS]);
345 cur_hour = convert_hour(s, cur_hour);
346
00cf5774
PB
347 if (alarm_hour == -1) {
348 alarm_hour = cur_hour;
349 if (alarm_min == -1) {
350 alarm_min = cur_min;
351 if (alarm_sec == -1) {
352 alarm_sec = cur_sec + 1;
353 } else if (cur_sec > alarm_sec) {
354 alarm_min++;
355 }
356 } else if (cur_min == alarm_min) {
357 if (alarm_sec == -1) {
358 alarm_sec = cur_sec + 1;
359 } else {
360 if (cur_sec > alarm_sec) {
361 alarm_hour++;
362 }
363 }
364 if (alarm_sec == SEC_PER_MIN) {
365 /* wrap to next hour, minutes is not in don't care mode */
366 alarm_sec = 0;
367 alarm_hour++;
368 }
369 } else if (cur_min > alarm_min) {
370 alarm_hour++;
371 }
372 } else if (cur_hour == alarm_hour) {
373 if (alarm_min == -1) {
374 alarm_min = cur_min;
375 if (alarm_sec == -1) {
376 alarm_sec = cur_sec + 1;
377 } else if (cur_sec > alarm_sec) {
378 alarm_min++;
379 }
380
381 if (alarm_sec == SEC_PER_MIN) {
382 alarm_sec = 0;
383 alarm_min++;
384 }
385 /* wrap to next day, hour is not in don't care mode */
386 alarm_min %= MIN_PER_HOUR;
387 } else if (cur_min == alarm_min) {
388 if (alarm_sec == -1) {
389 alarm_sec = cur_sec + 1;
390 }
391 /* wrap to next day, hours+minutes not in don't care mode */
392 alarm_sec %= SEC_PER_MIN;
393 }
56038ef6 394 }
56038ef6 395
00cf5774
PB
396 /* values that are still don't care fire at the next min/sec */
397 if (alarm_min == -1) {
398 alarm_min = 0;
399 }
400 if (alarm_sec == -1) {
401 alarm_sec = 0;
402 }
403
404 /* keep values in range */
405 if (alarm_sec == SEC_PER_MIN) {
406 alarm_sec = 0;
407 alarm_min++;
408 }
409 if (alarm_min == MIN_PER_HOUR) {
410 alarm_min = 0;
411 alarm_hour++;
412 }
413 alarm_hour %= HOUR_PER_DAY;
414
415 hour = alarm_hour - cur_hour;
416 min = hour * MIN_PER_HOUR + alarm_min - cur_min;
417 sec = min * SEC_PER_MIN + alarm_sec - cur_sec;
418 return sec <= 0 ? sec + SEC_PER_DAY : sec;
56038ef6
YZ
419}
420
421static void rtc_update_timer(void *opaque)
422{
423 RTCState *s = opaque;
424 int32_t irqs = REG_C_UF;
425 int32_t new_irqs;
426
41a9b8b2
YZ
427 assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60);
428
56038ef6
YZ
429 /* UIP might have been latched, update time and clear it. */
430 rtc_update_time(s);
431 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
432
884f17c2 433 if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) {
56038ef6
YZ
434 irqs |= REG_C_AF;
435 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
fb064112 436 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC, NULL);
56038ef6
YZ
437 }
438 }
00cf5774 439
56038ef6
YZ
440 new_irqs = irqs & ~s->cmos_data[RTC_REG_C];
441 s->cmos_data[RTC_REG_C] |= irqs;
442 if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
443 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
444 qemu_irq_raise(s->irq);
445 }
446 check_update_timer(s);
447}
448
0da8c842
AG
449static void cmos_ioport_write(void *opaque, hwaddr addr,
450 uint64_t data, unsigned size)
80cabfad 451{
b41a2cd1 452 RTCState *s = opaque;
369b4135 453 uint32_t old_period;
9a6e2dcf 454 bool update_periodic_timer;
80cabfad
FB
455
456 if ((addr & 1) == 0) {
457 s->cmos_index = data & 0x7f;
458 } else {
c5539cb4 459 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64 "\n",
ec51e364 460 s->cmos_index, data);
dff38e7b 461 switch(s->cmos_index) {
80cabfad
FB
462 case RTC_SECONDS_ALARM:
463 case RTC_MINUTES_ALARM:
464 case RTC_HOURS_ALARM:
80cabfad 465 s->cmos_data[s->cmos_index] = data;
56038ef6 466 check_update_timer(s);
80cabfad 467 break;
7cd9681b 468 case RTC_IBM_PS2_CENTURY_BYTE:
e67edb94
PB
469 s->cmos_index = RTC_CENTURY;
470 /* fall through */
471 case RTC_CENTURY:
80cabfad
FB
472 case RTC_SECONDS:
473 case RTC_MINUTES:
474 case RTC_HOURS:
475 case RTC_DAY_OF_WEEK:
476 case RTC_DAY_OF_MONTH:
477 case RTC_MONTH:
478 case RTC_YEAR:
479 s->cmos_data[s->cmos_index] = data;
dff38e7b 480 /* if in set mode, do not update the time */
41a9b8b2 481 if (rtc_running(s)) {
dff38e7b 482 rtc_set_time(s);
56038ef6 483 check_update_timer(s);
dff38e7b 484 }
80cabfad
FB
485 break;
486 case RTC_REG_A:
9a6e2dcf 487 update_periodic_timer = (s->cmos_data[RTC_REG_A] ^ data) & 0x0f;
369b4135 488 old_period = rtc_periodic_clock_ticks(s);
9a6e2dcf 489
41a9b8b2
YZ
490 if ((data & 0x60) == 0x60) {
491 if (rtc_running(s)) {
492 rtc_update_time(s);
493 }
494 /* What happens to UIP when divider reset is enabled is
495 * unclear from the datasheet. Shouldn't matter much
496 * though.
497 */
498 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
499 } else if (((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) &&
500 (data & 0x70) <= 0x20) {
501 /* when the divider reset is removed, the first update cycle
502 * begins one-half second later*/
503 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
504 s->offset = 500000000;
505 rtc_set_time(s);
506 }
507 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
508 }
dff38e7b
FB
509 /* UIP bit is read only */
510 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
511 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
9a6e2dcf
XG
512
513 if (update_periodic_timer) {
369b4135 514 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
7a3e29b1 515 old_period, true);
9a6e2dcf
XG
516 }
517
56038ef6 518 check_update_timer(s);
dff38e7b 519 break;
80cabfad 520 case RTC_REG_B:
9a6e2dcf
XG
521 update_periodic_timer = (s->cmos_data[RTC_REG_B] ^ data)
522 & REG_B_PIE;
369b4135 523 old_period = rtc_periodic_clock_ticks(s);
9a6e2dcf 524
dff38e7b 525 if (data & REG_B_SET) {
56038ef6 526 /* update cmos to when the rtc was stopping */
41a9b8b2 527 if (rtc_running(s)) {
56038ef6
YZ
528 rtc_update_time(s);
529 }
dff38e7b
FB
530 /* set mode: reset UIP mode */
531 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
532 data &= ~REG_B_UIE;
533 } else {
534 /* if disabling set mode, update the time */
41a9b8b2
YZ
535 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) &&
536 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20) {
13566fe3 537 s->offset = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
dff38e7b
FB
538 rtc_set_time(s);
539 }
540 }
9324cc50
YZ
541 /* if an interrupt flag is already set when the interrupt
542 * becomes enabled, raise an interrupt immediately. */
543 if (data & s->cmos_data[RTC_REG_C] & REG_C_MASK) {
544 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
545 qemu_irq_raise(s->irq);
546 } else {
547 s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF;
548 qemu_irq_lower(s->irq);
549 }
bedc572e 550 s->cmos_data[RTC_REG_B] = data;
9a6e2dcf
XG
551
552 if (update_periodic_timer) {
369b4135 553 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
7a3e29b1 554 old_period, true);
9a6e2dcf
XG
555 }
556
56038ef6 557 check_update_timer(s);
80cabfad
FB
558 break;
559 case RTC_REG_C:
560 case RTC_REG_D:
561 /* cannot write to them */
562 break;
563 default:
564 s->cmos_data[s->cmos_index] = data;
565 break;
566 }
567 }
568}
569
abd0c6bd 570static inline int rtc_to_bcd(RTCState *s, int a)
80cabfad 571{
6f1bf24d 572 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
dff38e7b
FB
573 return a;
574 } else {
575 return ((a / 10) << 4) | (a % 10);
576 }
80cabfad
FB
577}
578
abd0c6bd 579static inline int rtc_from_bcd(RTCState *s, int a)
80cabfad 580{
00cf5774
PB
581 if ((a & 0xc0) == 0xc0) {
582 return -1;
583 }
6f1bf24d 584 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
dff38e7b
FB
585 return a;
586 } else {
587 return ((a >> 4) * 10) + (a & 0x0f);
588 }
589}
590
e2826cf4 591static void rtc_get_time(RTCState *s, struct tm *tm)
dff38e7b 592{
abd0c6bd
PB
593 tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
594 tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
595 tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
3b89eb43
PB
596 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
597 tm->tm_hour %= 12;
598 if (s->cmos_data[RTC_HOURS] & 0x80) {
599 tm->tm_hour += 12;
600 }
43f493af 601 }
abd0c6bd
PB
602 tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
603 tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
604 tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
b8994faf
PB
605 tm->tm_year =
606 rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year +
607 rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900;
e2826cf4
PB
608}
609
610static void rtc_set_time(RTCState *s)
611{
612 struct tm tm;
80cd3478 613
e2826cf4 614 rtc_get_time(s, &tm);
e2826cf4 615 s->base_rtc = mktimegm(&tm);
884f17c2 616 s->last_update = qemu_clock_get_ns(rtc_clock);
56038ef6 617
3ab72385 618 qapi_event_send_rtc_change(qemu_timedate_diff(&tm));
43f493af
FB
619}
620
e2826cf4 621static void rtc_set_cmos(RTCState *s, const struct tm *tm)
43f493af 622{
42fc73a1 623 int year;
dff38e7b 624
abd0c6bd
PB
625 s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
626 s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
c29cd656 627 if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
43f493af 628 /* 24 hour format */
abd0c6bd 629 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
43f493af
FB
630 } else {
631 /* 12 hour format */
3b89eb43
PB
632 int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
633 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
43f493af
FB
634 if (tm->tm_hour >= 12)
635 s->cmos_data[RTC_HOURS] |= 0x80;
636 }
abd0c6bd
PB
637 s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
638 s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
639 s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
b8994faf
PB
640 year = tm->tm_year + 1900 - s->base_year;
641 s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100);
642 s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100);
43f493af
FB
643}
644
56038ef6 645static void rtc_update_time(RTCState *s)
43f493af 646{
56038ef6
YZ
647 struct tm ret;
648 time_t guest_sec;
649 int64_t guest_nsec;
650
651 guest_nsec = get_guest_rtc_ns(s);
13566fe3 652 guest_sec = guest_nsec / NANOSECONDS_PER_SECOND;
56038ef6 653 gmtime_r(&guest_sec, &ret);
02c6ccc6
AH
654
655 /* Is SET flag of Register B disabled? */
656 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) {
657 rtc_set_cmos(s, &ret);
658 }
43f493af
FB
659}
660
56038ef6 661static int update_in_progress(RTCState *s)
43f493af 662{
56038ef6 663 int64_t guest_nsec;
3b46e624 664
41a9b8b2 665 if (!rtc_running(s)) {
56038ef6 666 return 0;
dff38e7b 667 }
e93379b0
AB
668 if (timer_pending(s->update_timer)) {
669 int64_t next_update_time = timer_expire_time_ns(s->update_timer);
56038ef6 670 /* Latch UIP until the timer expires. */
884f17c2
AB
671 if (qemu_clock_get_ns(rtc_clock) >=
672 (next_update_time - UIP_HOLD_LENGTH)) {
56038ef6
YZ
673 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
674 return 1;
dff38e7b
FB
675 }
676 }
677
56038ef6
YZ
678 guest_nsec = get_guest_rtc_ns(s);
679 /* UIP bit will be set at last 244us of every second. */
13566fe3
SH
680 if ((guest_nsec % NANOSECONDS_PER_SECOND) >=
681 (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) {
56038ef6 682 return 1;
dff38e7b 683 }
56038ef6 684 return 0;
80cabfad
FB
685}
686
0da8c842
AG
687static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
688 unsigned size)
80cabfad 689{
b41a2cd1 690 RTCState *s = opaque;
80cabfad
FB
691 int ret;
692 if ((addr & 1) == 0) {
693 return 0xff;
694 } else {
695 switch(s->cmos_index) {
7cd9681b 696 case RTC_IBM_PS2_CENTURY_BYTE:
e67edb94
PB
697 s->cmos_index = RTC_CENTURY;
698 /* fall through */
699 case RTC_CENTURY:
80cabfad
FB
700 case RTC_SECONDS:
701 case RTC_MINUTES:
702 case RTC_HOURS:
703 case RTC_DAY_OF_WEEK:
704 case RTC_DAY_OF_MONTH:
705 case RTC_MONTH:
706 case RTC_YEAR:
56038ef6
YZ
707 /* if not in set mode, calibrate cmos before
708 * reading*/
41a9b8b2 709 if (rtc_running(s)) {
56038ef6
YZ
710 rtc_update_time(s);
711 }
80cabfad
FB
712 ret = s->cmos_data[s->cmos_index];
713 break;
714 case RTC_REG_A:
33f21e4f 715 ret = s->cmos_data[s->cmos_index];
56038ef6 716 if (update_in_progress(s)) {
33f21e4f 717 ret |= REG_A_UIP;
56038ef6 718 }
80cabfad
FB
719 break;
720 case RTC_REG_C:
721 ret = s->cmos_data[s->cmos_index];
d537cf6c 722 qemu_irq_lower(s->irq);
fbc15e27 723 s->cmos_data[RTC_REG_C] = 0x00;
56038ef6
YZ
724 if (ret & (REG_C_UF | REG_C_AF)) {
725 check_update_timer(s);
726 }
e0c8b950 727
ba32edab 728 if(s->irq_coalesced &&
fbc15e27 729 (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
ba32edab
GN
730 s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
731 s->irq_reinject_on_ack_count++;
fbc15e27 732 s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
aa6f63ff 733 DPRINTF_C("cmos: injecting on ack\n");
e0c8b950 734 if (rtc_policy_slew_deliver_irq(s)) {
ba32edab 735 s->irq_coalesced--;
aa6f63ff
BS
736 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
737 s->irq_coalesced);
738 }
ba32edab 739 }
80cabfad
FB
740 break;
741 default:
742 ret = s->cmos_data[s->cmos_index];
743 break;
744 }
ec51e364
IY
745 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
746 s->cmos_index, ret);
80cabfad
FB
747 return ret;
748 }
749}
750
1d914fa0 751void rtc_set_memory(ISADevice *dev, int addr, int val)
dff38e7b 752{
0e41271e 753 RTCState *s = MC146818_RTC(dev);
dff38e7b
FB
754 if (addr >= 0 && addr <= 127)
755 s->cmos_data[addr] = val;
756}
757
b8b7456d
IM
758int rtc_get_memory(ISADevice *dev, int addr)
759{
760 RTCState *s = MC146818_RTC(dev);
761 assert(addr >= 0 && addr <= 127);
762 return s->cmos_data[addr];
763}
764
1d914fa0 765static void rtc_set_date_from_host(ISADevice *dev)
ea55ffb3 766{
0e41271e 767 RTCState *s = MC146818_RTC(dev);
f6503059 768 struct tm tm;
ea55ffb3 769
f6503059 770 qemu_get_timedate(&tm, 0);
56038ef6
YZ
771
772 s->base_rtc = mktimegm(&tm);
884f17c2 773 s->last_update = qemu_clock_get_ns(rtc_clock);
56038ef6
YZ
774 s->offset = 0;
775
776 /* set the CMOS date */
e2826cf4 777 rtc_set_cmos(s, &tm);
ea55ffb3
TS
778}
779
44b1ff31 780static int rtc_pre_save(void *opaque)
3cf294ee
JB
781{
782 RTCState *s = opaque;
783
784 rtc_update_time(s);
44b1ff31
DDAG
785
786 return 0;
3cf294ee
JB
787}
788
6b075b8a 789static int rtc_post_load(void *opaque, int version_id)
80cabfad 790{
dff38e7b
FB
791 RTCState *s = opaque;
792
3cf294ee 793 if (version_id <= 2 || rtc_clock == QEMU_CLOCK_REALTIME) {
56038ef6
YZ
794 rtc_set_time(s);
795 s->offset = 0;
796 check_update_timer(s);
797 }
7a3e29b1 798 s->period = rtc_periodic_clock_ticks(s);
56038ef6 799
1dfb1b2d
PD
800 /* The periodic timer is deterministic in record/replay mode,
801 * so there is no need to update it after loading the vmstate.
802 * Reading RTC here would misalign record and replay.
803 */
804 if (replay_mode == REPLAY_MODE_NONE) {
805 uint64_t now = qemu_clock_get_ns(rtc_clock);
806 if (now < s->next_periodic_time ||
807 now > (s->next_periodic_time + get_max_clock_jump())) {
7a3e29b1 808 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock), s->period, false);
1dfb1b2d 809 }
ae46e239
PD
810 }
811
048c74c4 812 if (version_id >= 2) {
104059da 813 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
048c74c4
JQ
814 rtc_coalesced_timer_update(s);
815 }
048c74c4 816 }
73822ec8
AL
817 return 0;
818}
73822ec8 819
5cd8cada
JQ
820static bool rtc_irq_reinject_on_ack_count_needed(void *opaque)
821{
822 RTCState *s = (RTCState *)opaque;
823 return s->irq_reinject_on_ack_count != 0;
824}
825
0b102153 826static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count = {
bb426311 827 .name = "mc146818rtc/irq_reinject_on_ack_count",
0b102153
PD
828 .version_id = 1,
829 .minimum_version_id = 1,
5cd8cada 830 .needed = rtc_irq_reinject_on_ack_count_needed,
0b102153
PD
831 .fields = (VMStateField[]) {
832 VMSTATE_UINT16(irq_reinject_on_ack_count, RTCState),
833 VMSTATE_END_OF_LIST()
834 }
835};
836
6b075b8a
JQ
837static const VMStateDescription vmstate_rtc = {
838 .name = "mc146818rtc",
56038ef6 839 .version_id = 3,
6b075b8a 840 .minimum_version_id = 1,
3cf294ee 841 .pre_save = rtc_pre_save,
6b075b8a 842 .post_load = rtc_post_load,
d49805ae 843 .fields = (VMStateField[]) {
6b075b8a
JQ
844 VMSTATE_BUFFER(cmos_data, RTCState),
845 VMSTATE_UINT8(cmos_index, RTCState),
89166459 846 VMSTATE_UNUSED(7*4),
e720677e 847 VMSTATE_TIMER_PTR(periodic_timer, RTCState),
6b075b8a 848 VMSTATE_INT64(next_periodic_time, RTCState),
56038ef6 849 VMSTATE_UNUSED(3*8),
6b075b8a
JQ
850 VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
851 VMSTATE_UINT32_V(period, RTCState, 2),
56038ef6
YZ
852 VMSTATE_UINT64_V(base_rtc, RTCState, 3),
853 VMSTATE_UINT64_V(last_update, RTCState, 3),
854 VMSTATE_INT64_V(offset, RTCState, 3),
e720677e 855 VMSTATE_TIMER_PTR_V(update_timer, RTCState, 3),
00cf5774 856 VMSTATE_UINT64_V(next_alarm_time, RTCState, 3),
6b075b8a 857 VMSTATE_END_OF_LIST()
0b102153 858 },
5cd8cada
JQ
859 .subsections = (const VMStateDescription*[]) {
860 &vmstate_rtc_irq_reinject_on_ack_count,
861 NULL
6b075b8a
JQ
862 }
863};
864
da98c8eb
GH
865/* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
866 BIOS will read it and start S3 resume at POST Entry */
867static void rtc_notify_suspend(Notifier *notifier, void *data)
868{
869 RTCState *s = container_of(notifier, RTCState, suspend_notifier);
0e41271e 870 rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE);
da98c8eb
GH
871}
872
eeb7c03c
GN
873static void rtc_reset(void *opaque)
874{
875 RTCState *s = opaque;
876
72716184
AL
877 s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
878 s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
56038ef6 879 check_update_timer(s);
eeb7c03c 880
72716184 881 qemu_irq_lower(s->irq);
eeb7c03c 882
104059da 883 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
433acf0d 884 s->irq_coalesced = 0;
7cd9681b 885 s->irq_reinject_on_ack_count = 0;
433acf0d 886 }
eeb7c03c
GN
887}
888
b2c5009b 889static const MemoryRegionOps cmos_ops = {
0da8c842
AG
890 .read = cmos_ioport_read,
891 .write = cmos_ioport_write,
892 .impl = {
893 .min_access_size = 1,
894 .max_access_size = 1,
895 },
896 .endianness = DEVICE_LITTLE_ENDIAN,
b2c5009b
RH
897};
898
8e099d14 899static void rtc_get_date(Object *obj, struct tm *current_tm, Error **errp)
18297050 900{
0e41271e 901 RTCState *s = MC146818_RTC(obj);
18297050 902
56038ef6 903 rtc_update_time(s);
8e099d14 904 rtc_get_time(s, current_tm);
18297050
AL
905}
906
db895a1e 907static void rtc_realizefn(DeviceState *dev, Error **errp)
dff38e7b 908{
db895a1e 909 ISADevice *isadev = ISA_DEVICE(dev);
0e41271e 910 RTCState *s = MC146818_RTC(dev);
32e0c826 911 int base = 0x70;
80cabfad 912
80cabfad
FB
913 s->cmos_data[RTC_REG_A] = 0x26;
914 s->cmos_data[RTC_REG_B] = 0x02;
915 s->cmos_data[RTC_REG_C] = 0x00;
916 s->cmos_data[RTC_REG_D] = 0x80;
917
b8994faf
PB
918 /* This is for historical reasons. The default base year qdev property
919 * was set to 2000 for most machine types before the century byte was
920 * implemented.
921 *
922 * This if statement means that the century byte will be always 0
923 * (at least until 2079...) for base_year = 1980, but will be set
924 * correctly for base_year = 2000.
925 */
926 if (s->base_year == 2000) {
927 s->base_year = 0;
928 }
929
db895a1e 930 rtc_set_date_from_host(isadev);
ea55ffb3 931
433acf0d 932 switch (s->lost_tick_policy) {
4aa70a0e 933#ifdef TARGET_I386
104059da 934 case LOST_TICK_POLICY_SLEW:
6875204c 935 s->coalesced_timer =
884f17c2 936 timer_new_ns(rtc_clock, rtc_coalesced_timer, s);
433acf0d 937 break;
4aa70a0e 938#endif
104059da 939 case LOST_TICK_POLICY_DISCARD:
433acf0d
JK
940 break;
941 default:
db895a1e
AF
942 error_setg(errp, "Invalid lost tick policy.");
943 return;
433acf0d 944 }
433acf0d 945
884f17c2
AB
946 s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s);
947 s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s);
56038ef6 948 check_update_timer(s);
dff38e7b 949
da98c8eb
GH
950 s->suspend_notifier.notify = rtc_notify_suspend;
951 qemu_register_suspend_notifier(&s->suspend_notifier);
952
853dca12 953 memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2);
db895a1e 954 isa_register_ioport(isadev, &s->io, base);
dff38e7b 955
f98167ea
PH
956 /* register rtc 0x70 port for coalesced_pio */
957 memory_region_set_flush_coalesced(&s->io);
958 memory_region_init_io(&s->coalesced_io, OBJECT(s), &cmos_ops,
959 s, "rtc-index", 1);
960 memory_region_add_subregion(&s->io, 0, &s->coalesced_io);
961 memory_region_add_coalescing(&s->coalesced_io, 0, 1);
962
db895a1e 963 qdev_set_legacy_instance_id(dev, base, 3);
a08d4367 964 qemu_register_reset(rtc_reset, s);
18297050 965
8e099d14 966 object_property_add_tm(OBJECT(s), "date", rtc_get_date, NULL);
654a36d8 967
3638439d 968 qdev_init_gpio_out(dev, &s->irq, 1);
df84f17d 969 QLIST_INSERT_HEAD(&rtc_devices, s, link);
32e0c826
GH
970}
971
6c646a11 972ISADevice *mc146818_rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
32e0c826 973{
0e41271e
AF
974 DeviceState *dev;
975 ISADevice *isadev;
eeb7c03c 976
0e41271e
AF
977 isadev = isa_create(bus, TYPE_MC146818_RTC);
978 dev = DEVICE(isadev);
0e41271e
AF
979 qdev_prop_set_int32(dev, "base_year", base_year);
980 qdev_init_nofail(dev);
7d932dfd 981 if (intercept_irq) {
3638439d 982 qdev_connect_gpio_out(dev, 0, intercept_irq);
7d932dfd 983 } else {
3638439d 984 isa_connect_gpio_out(isadev, 0, RTC_ISA_IRQ);
7d932dfd 985 }
f2ae8abf 986
df84f17d 987 object_property_add_alias(qdev_get_machine(), "rtc-time", OBJECT(isadev),
29551fdc
TH
988 "date", NULL);
989
0e41271e 990 return isadev;
80cabfad
FB
991}
992
39bffca2
AL
993static Property mc146818rtc_properties[] = {
994 DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
995 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
104059da 996 lost_tick_policy, LOST_TICK_POLICY_DISCARD),
39bffca2
AL
997 DEFINE_PROP_END_OF_LIST(),
998};
999
bf7bb91e 1000static void rtc_resetdev(DeviceState *d)
1001{
1002 RTCState *s = MC146818_RTC(d);
1003
1004 /* Reason: VM do suspend self will set 0xfe
1005 * Reset any values other than 0xfe(Guest suspend case) */
1006 if (s->cmos_data[0x0f] != 0xfe) {
1007 s->cmos_data[0x0f] = 0x00;
1008 }
1009}
1010
8f04ee08
AL
1011static void rtc_class_initfn(ObjectClass *klass, void *data)
1012{
39bffca2 1013 DeviceClass *dc = DEVICE_CLASS(klass);
db895a1e
AF
1014
1015 dc->realize = rtc_realizefn;
bf7bb91e 1016 dc->reset = rtc_resetdev;
39bffca2 1017 dc->vmsd = &vmstate_rtc;
4f67d30b 1018 device_class_set_props(dc, mc146818rtc_properties);
8f04ee08
AL
1019}
1020
8c43a6f0 1021static const TypeInfo mc146818rtc_info = {
0e41271e 1022 .name = TYPE_MC146818_RTC,
39bffca2
AL
1023 .parent = TYPE_ISA_DEVICE,
1024 .instance_size = sizeof(RTCState),
1025 .class_init = rtc_class_initfn,
32e0c826
GH
1026};
1027
83f7d43a 1028static void mc146818rtc_register_types(void)
100d9891 1029{
39bffca2 1030 type_register_static(&mc146818rtc_info);
100d9891 1031}
83f7d43a
AF
1032
1033type_init(mc146818rtc_register_types)