]> git.proxmox.com Git - qemu.git/blame - hw/timer/mc146818rtc.c
rtc: remove dead SQW IRQ code
[qemu.git] / hw / timer / 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 */
83c9f4ca 24#include "hw/hw.h"
1de7afc9 25#include "qemu/timer.h"
9c17d615 26#include "sysemu/sysemu.h"
0d09e41a 27#include "hw/timer/mc146818rtc.h"
7b1b5d19 28#include "qapi/visitor.h"
80cabfad 29
d362e757 30#ifdef TARGET_I386
0d09e41a 31#include "hw/i386/apic.h"
d362e757
JK
32#endif
33
80cabfad 34//#define DEBUG_CMOS
aa6f63ff 35//#define DEBUG_COALESCED
80cabfad 36
ec51e364
IY
37#ifdef DEBUG_CMOS
38# define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
39#else
40# define CMOS_DPRINTF(format, ...) do { } while (0)
41#endif
42
aa6f63ff
BS
43#ifdef DEBUG_COALESCED
44# define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
45#else
46# define DPRINTF_C(format, ...) do { } while (0)
47#endif
48
56038ef6 49#define NSEC_PER_SEC 1000000000LL
00cf5774
PB
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
56038ef6 55
dd17765b 56#define RTC_REINJECT_ON_ACK_COUNT 20
e46deaba 57#define RTC_CLOCK_RATE 32768
56038ef6 58#define UIP_HOLD_LENGTH (8 * NSEC_PER_SEC / 32768)
ba32edab 59
0e41271e
AF
60#define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC)
61
1d914fa0 62typedef struct RTCState {
0e41271e
AF
63 ISADevice parent_obj;
64
b2c5009b 65 MemoryRegion io;
dff38e7b
FB
66 uint8_t cmos_data[128];
67 uint8_t cmos_index;
32e0c826 68 int32_t base_year;
56038ef6
YZ
69 uint64_t base_rtc;
70 uint64_t last_update;
71 int64_t offset;
d537cf6c 72 qemu_irq irq;
18c6e2ff 73 int it_shift;
dff38e7b
FB
74 /* periodic timer */
75 QEMUTimer *periodic_timer;
76 int64_t next_periodic_time;
56038ef6
YZ
77 /* update-ended timer */
78 QEMUTimer *update_timer;
00cf5774 79 uint64_t next_alarm_time;
ba32edab 80 uint16_t irq_reinject_on_ack_count;
73822ec8
AL
81 uint32_t irq_coalesced;
82 uint32_t period;
93b66569 83 QEMUTimer *coalesced_timer;
17604dac 84 Notifier clock_reset_notifier;
433acf0d 85 LostTickPolicy lost_tick_policy;
da98c8eb 86 Notifier suspend_notifier;
1d914fa0 87} RTCState;
dff38e7b
FB
88
89static void rtc_set_time(RTCState *s);
56038ef6 90static void rtc_update_time(RTCState *s);
e2826cf4 91static void rtc_set_cmos(RTCState *s, const struct tm *tm);
56038ef6 92static inline int rtc_from_bcd(RTCState *s, int a);
00cf5774 93static uint64_t get_next_alarm(RTCState *s);
56038ef6 94
41a9b8b2
YZ
95static inline bool rtc_running(RTCState *s)
96{
97 return (!(s->cmos_data[RTC_REG_B] & REG_B_SET) &&
98 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20);
99}
100
56038ef6
YZ
101static uint64_t get_guest_rtc_ns(RTCState *s)
102{
103 uint64_t guest_rtc;
884f17c2 104 uint64_t guest_clock = qemu_clock_get_ns(rtc_clock);
56038ef6
YZ
105
106 guest_rtc = s->base_rtc * NSEC_PER_SEC
107 + guest_clock - s->last_update + s->offset;
108 return guest_rtc;
109}
dff38e7b 110
93b66569
AL
111#ifdef TARGET_I386
112static void rtc_coalesced_timer_update(RTCState *s)
113{
114 if (s->irq_coalesced == 0) {
bc72ad67 115 timer_del(s->coalesced_timer);
93b66569
AL
116 } else {
117 /* divide each RTC interval to 2 - 8 smaller intervals */
118 int c = MIN(s->irq_coalesced, 7) + 1;
884f17c2 119 int64_t next_clock = qemu_clock_get_ns(rtc_clock) +
e46deaba 120 muldiv64(s->period / c, get_ticks_per_sec(), RTC_CLOCK_RATE);
bc72ad67 121 timer_mod(s->coalesced_timer, next_clock);
93b66569
AL
122 }
123}
124
125static void rtc_coalesced_timer(void *opaque)
126{
127 RTCState *s = opaque;
128
129 if (s->irq_coalesced != 0) {
130 apic_reset_irq_delivered();
131 s->cmos_data[RTC_REG_C] |= 0xc0;
aa6f63ff 132 DPRINTF_C("cmos: injecting from timer\n");
7d932dfd 133 qemu_irq_raise(s->irq);
93b66569
AL
134 if (apic_get_irq_delivered()) {
135 s->irq_coalesced--;
aa6f63ff
BS
136 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
137 s->irq_coalesced);
93b66569
AL
138 }
139 }
140
141 rtc_coalesced_timer_update(s);
142}
143#endif
144
56038ef6 145/* handle periodic timer */
c4c18e24 146static void periodic_timer_update(RTCState *s, int64_t current_time)
dff38e7b
FB
147{
148 int period_code, period;
149 int64_t cur_clock, next_irq_clock;
150
151 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
100d9891 152 if (period_code != 0
c2d30667 153 && (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
dff38e7b
FB
154 if (period_code <= 2)
155 period_code += 7;
156 /* period in 32 Khz cycles */
157 period = 1 << (period_code - 1);
73822ec8 158#ifdef TARGET_I386
aa6f63ff 159 if (period != s->period) {
73822ec8 160 s->irq_coalesced = (s->irq_coalesced * s->period) / period;
aa6f63ff
BS
161 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced);
162 }
73822ec8
AL
163 s->period = period;
164#endif
dff38e7b 165 /* compute 32 khz clock */
e46deaba 166 cur_clock = muldiv64(current_time, RTC_CLOCK_RATE, get_ticks_per_sec());
dff38e7b 167 next_irq_clock = (cur_clock & ~(period - 1)) + period;
6875204c 168 s->next_periodic_time =
e46deaba 169 muldiv64(next_irq_clock, get_ticks_per_sec(), RTC_CLOCK_RATE) + 1;
bc72ad67 170 timer_mod(s->periodic_timer, s->next_periodic_time);
dff38e7b 171 } else {
73822ec8
AL
172#ifdef TARGET_I386
173 s->irq_coalesced = 0;
174#endif
bc72ad67 175 timer_del(s->periodic_timer);
dff38e7b
FB
176 }
177}
178
179static void rtc_periodic_timer(void *opaque)
180{
181 RTCState *s = opaque;
182
c4c18e24 183 periodic_timer_update(s, s->next_periodic_time);
663447d4 184 s->cmos_data[RTC_REG_C] |= REG_C_PF;
100d9891 185 if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
663447d4 186 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
93b66569 187#ifdef TARGET_I386
433acf0d 188 if (s->lost_tick_policy == LOST_TICK_SLEW) {
ba32edab
GN
189 if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
190 s->irq_reinject_on_ack_count = 0;
93b66569 191 apic_reset_irq_delivered();
7d932dfd 192 qemu_irq_raise(s->irq);
93b66569
AL
193 if (!apic_get_irq_delivered()) {
194 s->irq_coalesced++;
195 rtc_coalesced_timer_update(s);
aa6f63ff
BS
196 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
197 s->irq_coalesced);
93b66569
AL
198 }
199 } else
200#endif
7d932dfd 201 qemu_irq_raise(s->irq);
100d9891 202 }
dff38e7b 203}
80cabfad 204
56038ef6
YZ
205/* handle update-ended timer */
206static void check_update_timer(RTCState *s)
207{
208 uint64_t next_update_time;
209 uint64_t guest_nsec;
00cf5774 210 int next_alarm_sec;
56038ef6 211
41a9b8b2
YZ
212 /* From the data sheet: "Holding the dividers in reset prevents
213 * interrupts from operating, while setting the SET bit allows"
214 * them to occur. However, it will prevent an alarm interrupt
215 * from occurring, because the time of day is not updated.
56038ef6 216 */
41a9b8b2 217 if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) {
bc72ad67 218 timer_del(s->update_timer);
41a9b8b2
YZ
219 return;
220 }
56038ef6
YZ
221 if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
222 (s->cmos_data[RTC_REG_B] & REG_B_SET)) {
bc72ad67 223 timer_del(s->update_timer);
56038ef6
YZ
224 return;
225 }
226 if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
227 (s->cmos_data[RTC_REG_C] & REG_C_AF)) {
bc72ad67 228 timer_del(s->update_timer);
56038ef6
YZ
229 return;
230 }
231
232 guest_nsec = get_guest_rtc_ns(s) % NSEC_PER_SEC;
00cf5774 233 /* if UF is clear, reprogram to next second */
884f17c2 234 next_update_time = qemu_clock_get_ns(rtc_clock)
56038ef6 235 + NSEC_PER_SEC - guest_nsec;
00cf5774
PB
236
237 /* Compute time of next alarm. One second is already accounted
238 * for in next_update_time.
239 */
240 next_alarm_sec = get_next_alarm(s);
241 s->next_alarm_time = next_update_time + (next_alarm_sec - 1) * NSEC_PER_SEC;
242
243 if (s->cmos_data[RTC_REG_C] & REG_C_UF) {
244 /* UF is set, but AF is clear. Program the timer to target
245 * the alarm time. */
246 next_update_time = s->next_alarm_time;
247 }
e93379b0 248 if (next_update_time != timer_expire_time_ns(s->update_timer)) {
bc72ad67 249 timer_mod(s->update_timer, next_update_time);
56038ef6
YZ
250 }
251}
252
253static inline uint8_t convert_hour(RTCState *s, uint8_t hour)
254{
255 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
256 hour %= 12;
257 if (s->cmos_data[RTC_HOURS] & 0x80) {
258 hour += 12;
259 }
260 }
261 return hour;
262}
263
00cf5774 264static uint64_t get_next_alarm(RTCState *s)
56038ef6 265{
00cf5774
PB
266 int32_t alarm_sec, alarm_min, alarm_hour, cur_hour, cur_min, cur_sec;
267 int32_t hour, min, sec;
268
269 rtc_update_time(s);
56038ef6
YZ
270
271 alarm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]);
272 alarm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]);
273 alarm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]);
00cf5774 274 alarm_hour = alarm_hour == -1 ? -1 : convert_hour(s, alarm_hour);
56038ef6
YZ
275
276 cur_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
277 cur_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
278 cur_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS]);
279 cur_hour = convert_hour(s, cur_hour);
280
00cf5774
PB
281 if (alarm_hour == -1) {
282 alarm_hour = cur_hour;
283 if (alarm_min == -1) {
284 alarm_min = cur_min;
285 if (alarm_sec == -1) {
286 alarm_sec = cur_sec + 1;
287 } else if (cur_sec > alarm_sec) {
288 alarm_min++;
289 }
290 } else if (cur_min == alarm_min) {
291 if (alarm_sec == -1) {
292 alarm_sec = cur_sec + 1;
293 } else {
294 if (cur_sec > alarm_sec) {
295 alarm_hour++;
296 }
297 }
298 if (alarm_sec == SEC_PER_MIN) {
299 /* wrap to next hour, minutes is not in don't care mode */
300 alarm_sec = 0;
301 alarm_hour++;
302 }
303 } else if (cur_min > alarm_min) {
304 alarm_hour++;
305 }
306 } else if (cur_hour == alarm_hour) {
307 if (alarm_min == -1) {
308 alarm_min = cur_min;
309 if (alarm_sec == -1) {
310 alarm_sec = cur_sec + 1;
311 } else if (cur_sec > alarm_sec) {
312 alarm_min++;
313 }
314
315 if (alarm_sec == SEC_PER_MIN) {
316 alarm_sec = 0;
317 alarm_min++;
318 }
319 /* wrap to next day, hour is not in don't care mode */
320 alarm_min %= MIN_PER_HOUR;
321 } else if (cur_min == alarm_min) {
322 if (alarm_sec == -1) {
323 alarm_sec = cur_sec + 1;
324 }
325 /* wrap to next day, hours+minutes not in don't care mode */
326 alarm_sec %= SEC_PER_MIN;
327 }
56038ef6 328 }
56038ef6 329
00cf5774
PB
330 /* values that are still don't care fire at the next min/sec */
331 if (alarm_min == -1) {
332 alarm_min = 0;
333 }
334 if (alarm_sec == -1) {
335 alarm_sec = 0;
336 }
337
338 /* keep values in range */
339 if (alarm_sec == SEC_PER_MIN) {
340 alarm_sec = 0;
341 alarm_min++;
342 }
343 if (alarm_min == MIN_PER_HOUR) {
344 alarm_min = 0;
345 alarm_hour++;
346 }
347 alarm_hour %= HOUR_PER_DAY;
348
349 hour = alarm_hour - cur_hour;
350 min = hour * MIN_PER_HOUR + alarm_min - cur_min;
351 sec = min * SEC_PER_MIN + alarm_sec - cur_sec;
352 return sec <= 0 ? sec + SEC_PER_DAY : sec;
56038ef6
YZ
353}
354
355static void rtc_update_timer(void *opaque)
356{
357 RTCState *s = opaque;
358 int32_t irqs = REG_C_UF;
359 int32_t new_irqs;
360
41a9b8b2
YZ
361 assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60);
362
56038ef6
YZ
363 /* UIP might have been latched, update time and clear it. */
364 rtc_update_time(s);
365 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
366
884f17c2 367 if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) {
56038ef6
YZ
368 irqs |= REG_C_AF;
369 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
370 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC);
371 }
372 }
00cf5774 373
56038ef6
YZ
374 new_irqs = irqs & ~s->cmos_data[RTC_REG_C];
375 s->cmos_data[RTC_REG_C] |= irqs;
376 if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
377 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
378 qemu_irq_raise(s->irq);
379 }
380 check_update_timer(s);
381}
382
0da8c842
AG
383static void cmos_ioport_write(void *opaque, hwaddr addr,
384 uint64_t data, unsigned size)
80cabfad 385{
b41a2cd1 386 RTCState *s = opaque;
80cabfad
FB
387
388 if ((addr & 1) == 0) {
389 s->cmos_index = data & 0x7f;
390 } else {
ec51e364
IY
391 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
392 s->cmos_index, data);
dff38e7b 393 switch(s->cmos_index) {
80cabfad
FB
394 case RTC_SECONDS_ALARM:
395 case RTC_MINUTES_ALARM:
396 case RTC_HOURS_ALARM:
80cabfad 397 s->cmos_data[s->cmos_index] = data;
56038ef6 398 check_update_timer(s);
80cabfad 399 break;
e67edb94
PB
400 case RTC_IBM_PS2_CENTURY_BYTE:
401 s->cmos_index = RTC_CENTURY;
402 /* fall through */
403 case RTC_CENTURY:
80cabfad
FB
404 case RTC_SECONDS:
405 case RTC_MINUTES:
406 case RTC_HOURS:
407 case RTC_DAY_OF_WEEK:
408 case RTC_DAY_OF_MONTH:
409 case RTC_MONTH:
410 case RTC_YEAR:
411 s->cmos_data[s->cmos_index] = data;
dff38e7b 412 /* if in set mode, do not update the time */
41a9b8b2 413 if (rtc_running(s)) {
dff38e7b 414 rtc_set_time(s);
56038ef6 415 check_update_timer(s);
dff38e7b 416 }
80cabfad
FB
417 break;
418 case RTC_REG_A:
41a9b8b2
YZ
419 if ((data & 0x60) == 0x60) {
420 if (rtc_running(s)) {
421 rtc_update_time(s);
422 }
423 /* What happens to UIP when divider reset is enabled is
424 * unclear from the datasheet. Shouldn't matter much
425 * though.
426 */
427 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
428 } else if (((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) &&
429 (data & 0x70) <= 0x20) {
430 /* when the divider reset is removed, the first update cycle
431 * begins one-half second later*/
432 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
433 s->offset = 500000000;
434 rtc_set_time(s);
435 }
436 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
437 }
dff38e7b
FB
438 /* UIP bit is read only */
439 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
440 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
884f17c2 441 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock));
56038ef6 442 check_update_timer(s);
dff38e7b 443 break;
80cabfad 444 case RTC_REG_B:
dff38e7b 445 if (data & REG_B_SET) {
56038ef6 446 /* update cmos to when the rtc was stopping */
41a9b8b2 447 if (rtc_running(s)) {
56038ef6
YZ
448 rtc_update_time(s);
449 }
dff38e7b
FB
450 /* set mode: reset UIP mode */
451 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
452 data &= ~REG_B_UIE;
453 } else {
454 /* if disabling set mode, update the time */
41a9b8b2
YZ
455 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) &&
456 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20) {
56038ef6 457 s->offset = get_guest_rtc_ns(s) % NSEC_PER_SEC;
dff38e7b
FB
458 rtc_set_time(s);
459 }
460 }
9324cc50
YZ
461 /* if an interrupt flag is already set when the interrupt
462 * becomes enabled, raise an interrupt immediately. */
463 if (data & s->cmos_data[RTC_REG_C] & REG_C_MASK) {
464 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
465 qemu_irq_raise(s->irq);
466 } else {
467 s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF;
468 qemu_irq_lower(s->irq);
469 }
bedc572e 470 s->cmos_data[RTC_REG_B] = data;
884f17c2 471 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock));
56038ef6 472 check_update_timer(s);
80cabfad
FB
473 break;
474 case RTC_REG_C:
475 case RTC_REG_D:
476 /* cannot write to them */
477 break;
478 default:
479 s->cmos_data[s->cmos_index] = data;
480 break;
481 }
482 }
483}
484
abd0c6bd 485static inline int rtc_to_bcd(RTCState *s, int a)
80cabfad 486{
6f1bf24d 487 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
dff38e7b
FB
488 return a;
489 } else {
490 return ((a / 10) << 4) | (a % 10);
491 }
80cabfad
FB
492}
493
abd0c6bd 494static inline int rtc_from_bcd(RTCState *s, int a)
80cabfad 495{
00cf5774
PB
496 if ((a & 0xc0) == 0xc0) {
497 return -1;
498 }
6f1bf24d 499 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
dff38e7b
FB
500 return a;
501 } else {
502 return ((a >> 4) * 10) + (a & 0x0f);
503 }
504}
505
e2826cf4 506static void rtc_get_time(RTCState *s, struct tm *tm)
dff38e7b 507{
abd0c6bd
PB
508 tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
509 tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
510 tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
3b89eb43
PB
511 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
512 tm->tm_hour %= 12;
513 if (s->cmos_data[RTC_HOURS] & 0x80) {
514 tm->tm_hour += 12;
515 }
43f493af 516 }
abd0c6bd
PB
517 tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
518 tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
519 tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
b8994faf
PB
520 tm->tm_year =
521 rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year +
522 rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900;
e2826cf4
PB
523}
524
525static void rtc_set_time(RTCState *s)
526{
527 struct tm tm;
80cd3478 528
e2826cf4 529 rtc_get_time(s, &tm);
e2826cf4 530 s->base_rtc = mktimegm(&tm);
884f17c2 531 s->last_update = qemu_clock_get_ns(rtc_clock);
56038ef6 532
e2826cf4 533 rtc_change_mon_event(&tm);
43f493af
FB
534}
535
e2826cf4 536static void rtc_set_cmos(RTCState *s, const struct tm *tm)
43f493af 537{
42fc73a1 538 int year;
dff38e7b 539
abd0c6bd
PB
540 s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
541 s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
c29cd656 542 if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
43f493af 543 /* 24 hour format */
abd0c6bd 544 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
43f493af
FB
545 } else {
546 /* 12 hour format */
3b89eb43
PB
547 int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
548 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
43f493af
FB
549 if (tm->tm_hour >= 12)
550 s->cmos_data[RTC_HOURS] |= 0x80;
551 }
abd0c6bd
PB
552 s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
553 s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
554 s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
b8994faf
PB
555 year = tm->tm_year + 1900 - s->base_year;
556 s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100);
557 s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100);
43f493af
FB
558}
559
56038ef6 560static void rtc_update_time(RTCState *s)
43f493af 561{
56038ef6
YZ
562 struct tm ret;
563 time_t guest_sec;
564 int64_t guest_nsec;
565
566 guest_nsec = get_guest_rtc_ns(s);
567 guest_sec = guest_nsec / NSEC_PER_SEC;
568 gmtime_r(&guest_sec, &ret);
02c6ccc6
AH
569
570 /* Is SET flag of Register B disabled? */
571 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) {
572 rtc_set_cmos(s, &ret);
573 }
43f493af
FB
574}
575
56038ef6 576static int update_in_progress(RTCState *s)
43f493af 577{
56038ef6 578 int64_t guest_nsec;
3b46e624 579
41a9b8b2 580 if (!rtc_running(s)) {
56038ef6 581 return 0;
dff38e7b 582 }
e93379b0
AB
583 if (timer_pending(s->update_timer)) {
584 int64_t next_update_time = timer_expire_time_ns(s->update_timer);
56038ef6 585 /* Latch UIP until the timer expires. */
884f17c2
AB
586 if (qemu_clock_get_ns(rtc_clock) >=
587 (next_update_time - UIP_HOLD_LENGTH)) {
56038ef6
YZ
588 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
589 return 1;
dff38e7b
FB
590 }
591 }
592
56038ef6
YZ
593 guest_nsec = get_guest_rtc_ns(s);
594 /* UIP bit will be set at last 244us of every second. */
595 if ((guest_nsec % NSEC_PER_SEC) >= (NSEC_PER_SEC - UIP_HOLD_LENGTH)) {
596 return 1;
dff38e7b 597 }
56038ef6 598 return 0;
80cabfad
FB
599}
600
0da8c842
AG
601static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
602 unsigned size)
80cabfad 603{
b41a2cd1 604 RTCState *s = opaque;
80cabfad
FB
605 int ret;
606 if ((addr & 1) == 0) {
607 return 0xff;
608 } else {
609 switch(s->cmos_index) {
e67edb94
PB
610 case RTC_IBM_PS2_CENTURY_BYTE:
611 s->cmos_index = RTC_CENTURY;
612 /* fall through */
613 case RTC_CENTURY:
80cabfad
FB
614 case RTC_SECONDS:
615 case RTC_MINUTES:
616 case RTC_HOURS:
617 case RTC_DAY_OF_WEEK:
618 case RTC_DAY_OF_MONTH:
619 case RTC_MONTH:
620 case RTC_YEAR:
56038ef6
YZ
621 /* if not in set mode, calibrate cmos before
622 * reading*/
41a9b8b2 623 if (rtc_running(s)) {
56038ef6
YZ
624 rtc_update_time(s);
625 }
80cabfad
FB
626 ret = s->cmos_data[s->cmos_index];
627 break;
628 case RTC_REG_A:
56038ef6
YZ
629 if (update_in_progress(s)) {
630 s->cmos_data[s->cmos_index] |= REG_A_UIP;
631 } else {
632 s->cmos_data[s->cmos_index] &= ~REG_A_UIP;
633 }
80cabfad 634 ret = s->cmos_data[s->cmos_index];
80cabfad
FB
635 break;
636 case RTC_REG_C:
637 ret = s->cmos_data[s->cmos_index];
d537cf6c 638 qemu_irq_lower(s->irq);
fbc15e27 639 s->cmos_data[RTC_REG_C] = 0x00;
56038ef6
YZ
640 if (ret & (REG_C_UF | REG_C_AF)) {
641 check_update_timer(s);
642 }
ba32edab
GN
643#ifdef TARGET_I386
644 if(s->irq_coalesced &&
fbc15e27 645 (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
ba32edab
GN
646 s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
647 s->irq_reinject_on_ack_count++;
fbc15e27 648 s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
ba32edab 649 apic_reset_irq_delivered();
aa6f63ff 650 DPRINTF_C("cmos: injecting on ack\n");
ba32edab 651 qemu_irq_raise(s->irq);
aa6f63ff 652 if (apic_get_irq_delivered()) {
ba32edab 653 s->irq_coalesced--;
aa6f63ff
BS
654 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
655 s->irq_coalesced);
656 }
ba32edab
GN
657 }
658#endif
80cabfad
FB
659 break;
660 default:
661 ret = s->cmos_data[s->cmos_index];
662 break;
663 }
ec51e364
IY
664 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
665 s->cmos_index, ret);
80cabfad
FB
666 return ret;
667 }
668}
669
1d914fa0 670void rtc_set_memory(ISADevice *dev, int addr, int val)
dff38e7b 671{
0e41271e 672 RTCState *s = MC146818_RTC(dev);
dff38e7b
FB
673 if (addr >= 0 && addr <= 127)
674 s->cmos_data[addr] = val;
675}
676
b8b7456d
IM
677int rtc_get_memory(ISADevice *dev, int addr)
678{
679 RTCState *s = MC146818_RTC(dev);
680 assert(addr >= 0 && addr <= 127);
681 return s->cmos_data[addr];
682}
683
1d914fa0 684static void rtc_set_date_from_host(ISADevice *dev)
ea55ffb3 685{
0e41271e 686 RTCState *s = MC146818_RTC(dev);
f6503059 687 struct tm tm;
ea55ffb3 688
f6503059 689 qemu_get_timedate(&tm, 0);
56038ef6
YZ
690
691 s->base_rtc = mktimegm(&tm);
884f17c2 692 s->last_update = qemu_clock_get_ns(rtc_clock);
56038ef6
YZ
693 s->offset = 0;
694
695 /* set the CMOS date */
e2826cf4 696 rtc_set_cmos(s, &tm);
ea55ffb3
TS
697}
698
6b075b8a 699static int rtc_post_load(void *opaque, int version_id)
80cabfad 700{
dff38e7b
FB
701 RTCState *s = opaque;
702
56038ef6
YZ
703 if (version_id <= 2) {
704 rtc_set_time(s);
705 s->offset = 0;
706 check_update_timer(s);
707 }
708
709#ifdef TARGET_I386
048c74c4 710 if (version_id >= 2) {
433acf0d 711 if (s->lost_tick_policy == LOST_TICK_SLEW) {
048c74c4
JQ
712 rtc_coalesced_timer_update(s);
713 }
048c74c4 714 }
6b075b8a 715#endif
73822ec8
AL
716 return 0;
717}
73822ec8 718
6b075b8a
JQ
719static const VMStateDescription vmstate_rtc = {
720 .name = "mc146818rtc",
56038ef6 721 .version_id = 3,
6b075b8a
JQ
722 .minimum_version_id = 1,
723 .minimum_version_id_old = 1,
724 .post_load = rtc_post_load,
725 .fields = (VMStateField []) {
726 VMSTATE_BUFFER(cmos_data, RTCState),
727 VMSTATE_UINT8(cmos_index, RTCState),
89166459 728 VMSTATE_UNUSED(7*4),
6b075b8a
JQ
729 VMSTATE_TIMER(periodic_timer, RTCState),
730 VMSTATE_INT64(next_periodic_time, RTCState),
56038ef6 731 VMSTATE_UNUSED(3*8),
6b075b8a
JQ
732 VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
733 VMSTATE_UINT32_V(period, RTCState, 2),
56038ef6
YZ
734 VMSTATE_UINT64_V(base_rtc, RTCState, 3),
735 VMSTATE_UINT64_V(last_update, RTCState, 3),
736 VMSTATE_INT64_V(offset, RTCState, 3),
737 VMSTATE_TIMER_V(update_timer, RTCState, 3),
00cf5774 738 VMSTATE_UINT64_V(next_alarm_time, RTCState, 3),
6b075b8a
JQ
739 VMSTATE_END_OF_LIST()
740 }
741};
742
17604dac
JK
743static void rtc_notify_clock_reset(Notifier *notifier, void *data)
744{
745 RTCState *s = container_of(notifier, RTCState, clock_reset_notifier);
746 int64_t now = *(int64_t *)data;
747
0e41271e 748 rtc_set_date_from_host(ISA_DEVICE(s));
c4c18e24 749 periodic_timer_update(s, now);
56038ef6 750 check_update_timer(s);
17604dac 751#ifdef TARGET_I386
433acf0d 752 if (s->lost_tick_policy == LOST_TICK_SLEW) {
17604dac
JK
753 rtc_coalesced_timer_update(s);
754 }
755#endif
756}
757
da98c8eb
GH
758/* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
759 BIOS will read it and start S3 resume at POST Entry */
760static void rtc_notify_suspend(Notifier *notifier, void *data)
761{
762 RTCState *s = container_of(notifier, RTCState, suspend_notifier);
0e41271e 763 rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE);
da98c8eb
GH
764}
765
eeb7c03c
GN
766static void rtc_reset(void *opaque)
767{
768 RTCState *s = opaque;
769
72716184
AL
770 s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
771 s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
56038ef6 772 check_update_timer(s);
eeb7c03c 773
72716184 774 qemu_irq_lower(s->irq);
eeb7c03c
GN
775
776#ifdef TARGET_I386
433acf0d
JK
777 if (s->lost_tick_policy == LOST_TICK_SLEW) {
778 s->irq_coalesced = 0;
779 }
eeb7c03c
GN
780#endif
781}
782
b2c5009b 783static const MemoryRegionOps cmos_ops = {
0da8c842
AG
784 .read = cmos_ioport_read,
785 .write = cmos_ioport_write,
786 .impl = {
787 .min_access_size = 1,
788 .max_access_size = 1,
789 },
790 .endianness = DEVICE_LITTLE_ENDIAN,
b2c5009b
RH
791};
792
57c9fafe 793static void rtc_get_date(Object *obj, Visitor *v, void *opaque,
18297050
AL
794 const char *name, Error **errp)
795{
0e41271e 796 RTCState *s = MC146818_RTC(obj);
e2826cf4 797 struct tm current_tm;
18297050 798
56038ef6 799 rtc_update_time(s);
e2826cf4 800 rtc_get_time(s, &current_tm);
18297050 801 visit_start_struct(v, NULL, "struct tm", name, 0, errp);
e2826cf4
PB
802 visit_type_int32(v, &current_tm.tm_year, "tm_year", errp);
803 visit_type_int32(v, &current_tm.tm_mon, "tm_mon", errp);
804 visit_type_int32(v, &current_tm.tm_mday, "tm_mday", errp);
805 visit_type_int32(v, &current_tm.tm_hour, "tm_hour", errp);
806 visit_type_int32(v, &current_tm.tm_min, "tm_min", errp);
807 visit_type_int32(v, &current_tm.tm_sec, "tm_sec", errp);
18297050
AL
808 visit_end_struct(v, errp);
809}
810
db895a1e 811static void rtc_realizefn(DeviceState *dev, Error **errp)
dff38e7b 812{
db895a1e 813 ISADevice *isadev = ISA_DEVICE(dev);
0e41271e 814 RTCState *s = MC146818_RTC(dev);
32e0c826 815 int base = 0x70;
80cabfad 816
80cabfad
FB
817 s->cmos_data[RTC_REG_A] = 0x26;
818 s->cmos_data[RTC_REG_B] = 0x02;
819 s->cmos_data[RTC_REG_C] = 0x00;
820 s->cmos_data[RTC_REG_D] = 0x80;
821
b8994faf
PB
822 /* This is for historical reasons. The default base year qdev property
823 * was set to 2000 for most machine types before the century byte was
824 * implemented.
825 *
826 * This if statement means that the century byte will be always 0
827 * (at least until 2079...) for base_year = 1980, but will be set
828 * correctly for base_year = 2000.
829 */
830 if (s->base_year == 2000) {
831 s->base_year = 0;
832 }
833
db895a1e 834 rtc_set_date_from_host(isadev);
ea55ffb3 835
93b66569 836#ifdef TARGET_I386
433acf0d
JK
837 switch (s->lost_tick_policy) {
838 case LOST_TICK_SLEW:
6875204c 839 s->coalesced_timer =
884f17c2 840 timer_new_ns(rtc_clock, rtc_coalesced_timer, s);
433acf0d
JK
841 break;
842 case LOST_TICK_DISCARD:
843 break;
844 default:
db895a1e
AF
845 error_setg(errp, "Invalid lost tick policy.");
846 return;
433acf0d 847 }
93b66569 848#endif
433acf0d 849
884f17c2
AB
850 s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s);
851 s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s);
56038ef6 852 check_update_timer(s);
dff38e7b 853
17604dac 854 s->clock_reset_notifier.notify = rtc_notify_clock_reset;
884f17c2
AB
855 qemu_clock_register_reset_notifier(QEMU_CLOCK_REALTIME,
856 &s->clock_reset_notifier);
17604dac 857
da98c8eb
GH
858 s->suspend_notifier.notify = rtc_notify_suspend;
859 qemu_register_suspend_notifier(&s->suspend_notifier);
860
853dca12 861 memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2);
db895a1e 862 isa_register_ioport(isadev, &s->io, base);
dff38e7b 863
db895a1e 864 qdev_set_legacy_instance_id(dev, base, 3);
a08d4367 865 qemu_register_reset(rtc_reset, s);
18297050 866
57c9fafe
AL
867 object_property_add(OBJECT(s), "date", "struct tm",
868 rtc_get_date, NULL, NULL, s, NULL);
32e0c826
GH
869}
870
48a18b3c 871ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
32e0c826 872{
0e41271e
AF
873 DeviceState *dev;
874 ISADevice *isadev;
7d932dfd 875 RTCState *s;
eeb7c03c 876
0e41271e
AF
877 isadev = isa_create(bus, TYPE_MC146818_RTC);
878 dev = DEVICE(isadev);
879 s = MC146818_RTC(isadev);
880 qdev_prop_set_int32(dev, "base_year", base_year);
881 qdev_init_nofail(dev);
7d932dfd
JK
882 if (intercept_irq) {
883 s->irq = intercept_irq;
884 } else {
0e41271e 885 isa_init_irq(isadev, &s->irq, RTC_ISA_IRQ);
7d932dfd 886 }
0e41271e 887 return isadev;
80cabfad
FB
888}
889
39bffca2
AL
890static Property mc146818rtc_properties[] = {
891 DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
892 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
893 lost_tick_policy, LOST_TICK_DISCARD),
894 DEFINE_PROP_END_OF_LIST(),
895};
896
8f04ee08
AL
897static void rtc_class_initfn(ObjectClass *klass, void *data)
898{
39bffca2 899 DeviceClass *dc = DEVICE_CLASS(klass);
db895a1e
AF
900
901 dc->realize = rtc_realizefn;
39bffca2
AL
902 dc->no_user = 1;
903 dc->vmsd = &vmstate_rtc;
904 dc->props = mc146818rtc_properties;
8f04ee08
AL
905}
906
8c43a6f0 907static const TypeInfo mc146818rtc_info = {
0e41271e 908 .name = TYPE_MC146818_RTC,
39bffca2
AL
909 .parent = TYPE_ISA_DEVICE,
910 .instance_size = sizeof(RTCState),
911 .class_init = rtc_class_initfn,
32e0c826
GH
912};
913
83f7d43a 914static void mc146818rtc_register_types(void)
100d9891 915{
39bffca2 916 type_register_static(&mc146818rtc_info);
100d9891 917}
83f7d43a
AF
918
919type_init(mc146818rtc_register_types)