VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevRTC.cpp@ 29085

Last change on this file since 29085 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 35.2 KB
Line 
1/* $Id: DevRTC.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * Motorola MC146818 RTC/CMOS Device with PIIX4 extensions.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * --------------------------------------------------------------------
17 *
18 * This code is based on:
19 *
20 * QEMU MC146818 RTC emulation
21 *
22 * Copyright (c) 2003-2004 Fabrice Bellard
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
37 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40 * THE SOFTWARE.
41 */
42
43/*******************************************************************************
44* Header Files *
45*******************************************************************************/
46#define LOG_GROUP LOG_GROUP_DEV_RTC
47#include <VBox/pdmdev.h>
48#include <VBox/log.h>
49#include <iprt/asm.h>
50#include <iprt/assert.h>
51#include <iprt/string.h>
52
53#ifdef IN_RING3
54# include <iprt/alloc.h>
55# include <iprt/uuid.h>
56#endif /* IN_RING3 */
57
58#include "../Builtins.h"
59
60struct RTCState;
61typedef struct RTCState RTCState;
62
63#define RTC_CRC_START 0x10
64#define RTC_CRC_LAST 0x2d
65#define RTC_CRC_HIGH 0x2e
66#define RTC_CRC_LOW 0x2f
67
68
69/*******************************************************************************
70* Internal Functions *
71*******************************************************************************/
72#ifndef VBOX_DEVICE_STRUCT_TESTCASE
73RT_C_DECLS_BEGIN
74PDMBOTHCBDECL(int) rtcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
75PDMBOTHCBDECL(int) rtcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
76PDMBOTHCBDECL(void) rtcTimerPeriodic(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser);
77PDMBOTHCBDECL(void) rtcTimerSecond(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser);
78PDMBOTHCBDECL(void) rtcTimerSecond2(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser);
79RT_C_DECLS_END
80#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
81
82
83/*******************************************************************************
84* Defined Constants And Macros *
85*******************************************************************************/
86/*#define DEBUG_CMOS*/
87
88#define RTC_SECONDS 0
89#define RTC_SECONDS_ALARM 1
90#define RTC_MINUTES 2
91#define RTC_MINUTES_ALARM 3
92#define RTC_HOURS 4
93#define RTC_HOURS_ALARM 5
94#define RTC_ALARM_DONT_CARE 0xC0
95
96#define RTC_DAY_OF_WEEK 6
97#define RTC_DAY_OF_MONTH 7
98#define RTC_MONTH 8
99#define RTC_YEAR 9
100
101#define RTC_REG_A 10
102#define RTC_REG_B 11
103#define RTC_REG_C 12
104#define RTC_REG_D 13
105
106#define REG_A_UIP 0x80
107
108#define REG_B_SET 0x80
109#define REG_B_PIE 0x40
110#define REG_B_AIE 0x20
111#define REG_B_UIE 0x10
112
113
114/** The saved state version. */
115#define RTC_SAVED_STATE_VERSION 4
116/** The saved state version used by VirtualBox pre-3.2.
117 * This does not include the second 128-byte bank. */
118#define RTC_SAVED_STATE_VERSION_VBOX_32PRE 3
119/** The saved state version used by VirtualBox 3.1 and earlier.
120 * This does not include disabled by HPET state. */
121#define RTC_SAVED_STATE_VERSION_VBOX_31 2
122/** The saved state version used by VirtualBox 3.0 and earlier.
123 * This does not include the configuration. */
124#define RTC_SAVED_STATE_VERSION_VBOX_30 1
125
126
127/*******************************************************************************
128* Structures and Typedefs *
129*******************************************************************************/
130/** @todo Replace struct my_tm with RTTIME. */
131struct my_tm
132{
133 int32_t tm_sec;
134 int32_t tm_min;
135 int32_t tm_hour;
136 int32_t tm_mday;
137 int32_t tm_mon;
138 int32_t tm_year;
139 int32_t tm_wday;
140 int32_t tm_yday;
141};
142
143
144struct RTCState {
145 uint8_t cmos_data[256];
146 uint8_t cmos_index[2];
147 uint8_t Alignment0[6];
148 struct my_tm current_tm;
149 /** The configured IRQ. */
150 int32_t irq;
151 /** The configured I/O port base. */
152 RTIOPORT IOPortBase;
153 /** Use UTC or local time initially. */
154 bool fUTC;
155 /** Disabled by HPET legacy mode. */
156 bool fDisabledByHpet;
157 /* periodic timer */
158 int64_t next_periodic_time;
159 /* second update */
160 int64_t next_second_time;
161
162 /** Pointer to the device instance - R3 Ptr. */
163 PPDMDEVINSR3 pDevInsR3;
164 /** The periodic timer (rtcTimerPeriodic) - R3 Ptr. */
165 PTMTIMERR3 pPeriodicTimerR3;
166 /** The second timer (rtcTimerSecond) - R3 Ptr. */
167 PTMTIMERR3 pSecondTimerR3;
168 /** The second second timer (rtcTimerSecond2) - R3 Ptr. */
169 PTMTIMERR3 pSecondTimer2R3;
170
171 /** Pointer to the device instance - R0 Ptr. */
172 PPDMDEVINSR0 pDevInsR0;
173 /** The periodic timer (rtcTimerPeriodic) - R0 Ptr. */
174 PTMTIMERR0 pPeriodicTimerR0;
175 /** The second timer (rtcTimerSecond) - R0 Ptr. */
176 PTMTIMERR0 pSecondTimerR0;
177 /** The second second timer (rtcTimerSecond2) - R0 Ptr. */
178 PTMTIMERR0 pSecondTimer2R0;
179
180 /** Pointer to the device instance - RC Ptr. */
181 PPDMDEVINSRC pDevInsRC;
182 /** The periodic timer (rtcTimerPeriodic) - RC Ptr. */
183 PTMTIMERRC pPeriodicTimerRC;
184 /** The second timer (rtcTimerSecond) - RC Ptr. */
185 PTMTIMERRC pSecondTimerRC;
186 /** The second second timer (rtcTimerSecond2) - RC Ptr. */
187 PTMTIMERRC pSecondTimer2RC;
188
189 /** The RTC registration structure. */
190 PDMRTCREG RtcReg;
191 /** The RTC device helpers. */
192 R3PTRTYPE(PCPDMRTCHLP) pRtcHlpR3;
193 /** Number of release log entries. Used to prevent flooding. */
194 uint32_t cRelLogEntries;
195 /** The current/previous timer period. Used to prevent flooding changes. */
196 int32_t CurPeriod;
197
198 /** HPET legacy mode notification interface. */
199 PDMIHPETLEGACYNOTIFY IHpetLegacyNotify;
200};
201
202#ifndef VBOX_DEVICE_STRUCT_TESTCASE
203static void rtc_set_time(RTCState *s);
204static void rtc_copy_date(RTCState *s);
205
206static void rtc_timer_update(RTCState *s, int64_t current_time)
207{
208 int period_code, period;
209 uint64_t cur_clock, next_irq_clock;
210 uint32_t freq;
211
212 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
213 if (period_code != 0 &&
214 (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
215 if (period_code <= 2)
216 period_code += 7;
217 /* period in 32 kHz cycles */
218 period = 1 << (period_code - 1);
219 /* compute 32 kHz clock */
220 freq = TMTimerGetFreq(s->CTX_SUFF(pPeriodicTimer));
221
222 cur_clock = ASMMultU64ByU32DivByU32(current_time, 32768, freq);
223 next_irq_clock = (cur_clock & ~(uint64_t)(period - 1)) + period;
224 s->next_periodic_time = ASMMultU64ByU32DivByU32(next_irq_clock, freq, 32768) + 1;
225 TMTimerSet(s->CTX_SUFF(pPeriodicTimer), s->next_periodic_time);
226
227 if (period != s->CurPeriod)
228 {
229 if (s->cRelLogEntries++ < 64)
230 LogRel(("RTC: period=%#x (%d) %u Hz\n", period, period, _32K / period));
231 s->CurPeriod = period;
232 }
233 } else {
234 if (TMTimerIsActive(s->CTX_SUFF(pPeriodicTimer)) && s->cRelLogEntries++ < 64)
235 LogRel(("RTC: stopped the periodic timer\n"));
236 TMTimerStop(s->CTX_SUFF(pPeriodicTimer));
237 }
238}
239
240static void rtc_raise_irq(RTCState* pThis, uint32_t iLevel)
241{
242 if (!pThis->fDisabledByHpet)
243 PDMDevHlpISASetIrq(pThis->CTX_SUFF(pDevIns), pThis->irq, iLevel);
244}
245
246static void rtc_periodic_timer(void *opaque)
247{
248 RTCState *s = (RTCState*)opaque;
249
250 rtc_timer_update(s, s->next_periodic_time);
251 s->cmos_data[RTC_REG_C] |= 0xc0;
252
253 rtc_raise_irq(s, 1);
254}
255
256static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
257{
258 RTCState *s = (RTCState*)opaque;
259 uint32_t bank;
260
261 bank = (addr >> 1) & 1;
262 if ((addr & 1) == 0) {
263 s->cmos_index[bank] = (data & 0x7f) + (bank * 128);
264 } else {
265 Log(("CMOS: Write bank %d idx %#04x: %#04x (old %#04x)\n", bank,
266 s->cmos_index[bank], data, s->cmos_data[s->cmos_index[bank]]));
267 switch(s->cmos_index[bank]) {
268 case RTC_SECONDS_ALARM:
269 case RTC_MINUTES_ALARM:
270 case RTC_HOURS_ALARM:
271 s->cmos_data[s->cmos_index[0]] = data;
272 break;
273 case RTC_SECONDS:
274 case RTC_MINUTES:
275 case RTC_HOURS:
276 case RTC_DAY_OF_WEEK:
277 case RTC_DAY_OF_MONTH:
278 case RTC_MONTH:
279 case RTC_YEAR:
280 s->cmos_data[s->cmos_index[0]] = data;
281 /* if in set mode, do not update the time */
282 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
283 rtc_set_time(s);
284 }
285 break;
286 case RTC_REG_A:
287 /* UIP bit is read only */
288 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
289 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
290 rtc_timer_update(s, TMTimerGet(s->CTX_SUFF(pPeriodicTimer)));
291 break;
292 case RTC_REG_B:
293 if (data & REG_B_SET) {
294 /* set mode: reset UIP mode */
295 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
296#if 0 /* This is probably wrong as it breaks changing the time/date in OS/2. */
297 data &= ~REG_B_UIE;
298#endif
299 } else {
300 /* if disabling set mode, update the time */
301 if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
302 rtc_set_time(s);
303 }
304 }
305 s->cmos_data[RTC_REG_B] = data;
306 rtc_timer_update(s, TMTimerGet(s->CTX_SUFF(pPeriodicTimer)));
307 break;
308 case RTC_REG_C:
309 case RTC_REG_D:
310 /* cannot write to them */
311 break;
312 default:
313 s->cmos_data[s->cmos_index[bank]] = data;
314 break;
315 }
316 }
317}
318
319static inline int to_bcd(RTCState *s, int a)
320{
321 if (s->cmos_data[RTC_REG_B] & 0x04) {
322 return a;
323 } else {
324 return ((a / 10) << 4) | (a % 10);
325 }
326}
327
328static inline int from_bcd(RTCState *s, int a)
329{
330 if (s->cmos_data[RTC_REG_B] & 0x04) {
331 return a;
332 } else {
333 return ((a >> 4) * 10) + (a & 0x0f);
334 }
335}
336
337static void rtc_set_time(RTCState *s)
338{
339 struct my_tm *tm = &s->current_tm;
340
341 tm->tm_sec = from_bcd(s, s->cmos_data[RTC_SECONDS]);
342 tm->tm_min = from_bcd(s, s->cmos_data[RTC_MINUTES]);
343 tm->tm_hour = from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
344 if (!(s->cmos_data[RTC_REG_B] & 0x02) &&
345 (s->cmos_data[RTC_HOURS] & 0x80)) {
346 tm->tm_hour += 12;
347 }
348 tm->tm_wday = from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]);
349 tm->tm_mday = from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
350 tm->tm_mon = from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
351 tm->tm_year = from_bcd(s, s->cmos_data[RTC_YEAR]) + 100;
352}
353
354static void rtc_copy_date(RTCState *s)
355{
356 const struct my_tm *tm = &s->current_tm;
357
358 s->cmos_data[RTC_SECONDS] = to_bcd(s, tm->tm_sec);
359 s->cmos_data[RTC_MINUTES] = to_bcd(s, tm->tm_min);
360 if (s->cmos_data[RTC_REG_B] & 0x02) {
361 /* 24 hour format */
362 s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour);
363 } else {
364 /* 12 hour format */
365 s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour % 12);
366 if (tm->tm_hour >= 12)
367 s->cmos_data[RTC_HOURS] |= 0x80;
368 }
369 s->cmos_data[RTC_DAY_OF_WEEK] = to_bcd(s, tm->tm_wday);
370 s->cmos_data[RTC_DAY_OF_MONTH] = to_bcd(s, tm->tm_mday);
371 s->cmos_data[RTC_MONTH] = to_bcd(s, tm->tm_mon + 1);
372 s->cmos_data[RTC_YEAR] = to_bcd(s, tm->tm_year % 100);
373}
374
375/* month is between 0 and 11. */
376static int get_days_in_month(int month, int year)
377{
378 static const int days_tab[12] = {
379 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
380 };
381 int d;
382 if ((unsigned )month >= 12)
383 return 31;
384 d = days_tab[month];
385 if (month == 1) {
386 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
387 d++;
388 }
389 return d;
390}
391
392/* update 'tm' to the next second */
393static void rtc_next_second(struct my_tm *tm)
394{
395 int days_in_month;
396
397 tm->tm_sec++;
398 if ((unsigned)tm->tm_sec >= 60) {
399 tm->tm_sec = 0;
400 tm->tm_min++;
401 if ((unsigned)tm->tm_min >= 60) {
402 tm->tm_min = 0;
403 tm->tm_hour++;
404 if ((unsigned)tm->tm_hour >= 24) {
405 tm->tm_hour = 0;
406 /* next day */
407 tm->tm_wday++;
408 if ((unsigned)tm->tm_wday >= 7)
409 tm->tm_wday = 0;
410 days_in_month = get_days_in_month(tm->tm_mon,
411 tm->tm_year + 1900);
412 tm->tm_mday++;
413 if (tm->tm_mday < 1) {
414 tm->tm_mday = 1;
415 } else if (tm->tm_mday > days_in_month) {
416 tm->tm_mday = 1;
417 tm->tm_mon++;
418 if (tm->tm_mon >= 12) {
419 tm->tm_mon = 0;
420 tm->tm_year++;
421 }
422 }
423 }
424 }
425 }
426}
427
428
429static void rtc_update_second(void *opaque)
430{
431 RTCState *s = (RTCState*)opaque;
432
433 /* if the oscillator is not in normal operation, we do not update */
434 if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) {
435 s->next_second_time += TMTimerGetFreq(s->CTX_SUFF(pSecondTimer));
436 TMTimerSet(s->CTX_SUFF(pSecondTimer), s->next_second_time);
437 } else {
438 rtc_next_second(&s->current_tm);
439
440 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
441 /* update in progress bit */
442 Log2(("RTC: UIP %x -> 1\n", !!(s->cmos_data[RTC_REG_A] & REG_A_UIP)));
443 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
444 }
445
446 /* 244140 ns = 8 / 32768 seconds */
447 uint64_t delay = TMTimerFromNano(s->CTX_SUFF(pSecondTimer2), 244140);
448 TMTimerSet(s->CTX_SUFF(pSecondTimer2), s->next_second_time + delay);
449 }
450}
451
452static void rtc_update_second2(void *opaque)
453{
454 RTCState *s = (RTCState*)opaque;
455
456 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
457 rtc_copy_date(s);
458 }
459
460 /* check alarm */
461 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
462 if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 ||
463 from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]) == s->current_tm.tm_sec) &&
464 ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 ||
465 from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]) == s->current_tm.tm_min) &&
466 ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 ||
467 from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]) == s->current_tm.tm_hour)) {
468
469 s->cmos_data[RTC_REG_C] |= 0xa0;
470 rtc_raise_irq(s, 1);
471 }
472 }
473
474 /* update ended interrupt */
475 if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
476 s->cmos_data[RTC_REG_C] |= 0x90;
477 rtc_raise_irq(s, 1);
478 }
479
480 /* clear update in progress bit */
481 Log2(("RTC: UIP %x -> 0\n", !!(s->cmos_data[RTC_REG_A] & REG_A_UIP)));
482 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
483
484 s->next_second_time += TMTimerGetFreq(s->CTX_SUFF(pSecondTimer));
485 TMTimerSet(s->CTX_SUFF(pSecondTimer), s->next_second_time);
486}
487
488static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
489{
490 RTCState *s = (RTCState*)opaque;
491 int ret;
492 unsigned bank;
493
494 bank = (addr >> 1) & 1;
495 if ((addr & 1) == 0) {
496 return 0xff;
497 } else {
498 switch(s->cmos_index[bank]) {
499 case RTC_SECONDS:
500 case RTC_MINUTES:
501 case RTC_HOURS:
502 case RTC_DAY_OF_WEEK:
503 case RTC_DAY_OF_MONTH:
504 case RTC_MONTH:
505 case RTC_YEAR:
506 ret = s->cmos_data[s->cmos_index[0]];
507 break;
508 case RTC_REG_A:
509 ret = s->cmos_data[s->cmos_index[0]];
510 break;
511 case RTC_REG_C:
512 ret = s->cmos_data[s->cmos_index[0]];
513 rtc_raise_irq(s, 0);
514 s->cmos_data[RTC_REG_C] = 0x00;
515 break;
516 default:
517 ret = s->cmos_data[s->cmos_index[bank]];
518 break;
519 }
520 Log(("CMOS: Read bank %d idx %#04x: %#04x\n", bank, s->cmos_index[bank], ret));
521 return ret;
522 }
523}
524
525#ifdef IN_RING3
526static void rtc_set_memory(RTCState *s, int addr, int val)
527{
528 if (addr >= 0 && addr <= 127)
529 s->cmos_data[addr] = val;
530}
531
532static void rtc_set_date(RTCState *s, const struct my_tm *tm)
533{
534 s->current_tm = *tm;
535 rtc_copy_date(s);
536}
537
538#endif /* IN_RING3 */
539
540/* -=-=-=-=-=- wrappers / stuff -=-=-=-=-=- */
541
542/**
543 * Port I/O Handler for IN operations.
544 *
545 * @returns VBox status code.
546 *
547 * @param pDevIns The device instance.
548 * @param pvUser User argument - ignored.
549 * @param uPort Port number used for the IN operation.
550 * @param pu32 Where to store the result.
551 * @param cb Number of bytes read.
552 */
553PDMBOTHCBDECL(int) rtcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
554{
555 NOREF(pvUser);
556 if (cb == 1)
557 {
558 *pu32 = cmos_ioport_read(PDMINS_2_DATA(pDevIns, RTCState *), Port);
559 return VINF_SUCCESS;
560 }
561 return VERR_IOM_IOPORT_UNUSED;
562}
563
564
565/**
566 * Port I/O Handler for OUT operations.
567 *
568 * @returns VBox status code.
569 *
570 * @param pDevIns The device instance.
571 * @param pvUser User argument - ignored.
572 * @param uPort Port number used for the IN operation.
573 * @param u32 The value to output.
574 * @param cb The value size in bytes.
575 */
576PDMBOTHCBDECL(int) rtcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
577{
578 NOREF(pvUser);
579 if (cb == 1)
580 cmos_ioport_write(PDMINS_2_DATA(pDevIns, RTCState *), Port, u32);
581 return VINF_SUCCESS;
582}
583
584
585/**
586 * Device timer callback function, periodic.
587 *
588 * @param pDevIns Device instance of the device which registered the timer.
589 * @param pTimer The timer handle.
590 * @param pvUser Pointer to the RTC state.
591 */
592PDMBOTHCBDECL(void) rtcTimerPeriodic(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
593{
594 rtc_periodic_timer((RTCState *)pvUser);
595}
596
597
598/**
599 * Device timer callback function, second.
600 *
601 * @param pDevIns Device instance of the device which registered the timer.
602 * @param pTimer The timer handle.
603 * @param pvUser Pointer to the RTC state.
604 */
605PDMBOTHCBDECL(void) rtcTimerSecond(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
606{
607 rtc_update_second((RTCState *)pvUser);
608}
609
610
611/**
612 * Device timer callback function, second2.
613 *
614 * @param pDevIns Device instance of the device which registered the timer.
615 * @param pTimer The timer handle.
616 * @param pvUser Pointer to the RTC state.
617 */
618PDMBOTHCBDECL(void) rtcTimerSecond2(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
619{
620 rtc_update_second2((RTCState *)pvUser);
621}
622
623#ifdef IN_RING3
624
625/**
626 * @copydoc FNSSMDEVLIVEEXEC
627 */
628static DECLCALLBACK(int) rtcLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
629{
630 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
631
632 SSMR3PutU8( pSSM, pThis->irq);
633 SSMR3PutIOPort(pSSM, pThis->IOPortBase);
634 SSMR3PutBool( pSSM, pThis->fUTC);
635
636 return VINF_SSM_DONT_CALL_AGAIN;
637}
638
639
640/**
641 * @copydoc FNSSMDEVSAVEEXEC
642 */
643static DECLCALLBACK(int) rtcSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
644{
645 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
646
647 /* The config. */
648 rtcLiveExec(pDevIns, pSSM, SSM_PASS_FINAL);
649
650 /* The state. */
651 SSMR3PutMem(pSSM, pThis->cmos_data, 128);
652 SSMR3PutU8(pSSM, pThis->cmos_index[0]);
653
654 SSMR3PutS32(pSSM, pThis->current_tm.tm_sec);
655 SSMR3PutS32(pSSM, pThis->current_tm.tm_min);
656 SSMR3PutS32(pSSM, pThis->current_tm.tm_hour);
657 SSMR3PutS32(pSSM, pThis->current_tm.tm_wday);
658 SSMR3PutS32(pSSM, pThis->current_tm.tm_mday);
659 SSMR3PutS32(pSSM, pThis->current_tm.tm_mon);
660 SSMR3PutS32(pSSM, pThis->current_tm.tm_year);
661
662 TMR3TimerSave(pThis->CTX_SUFF(pPeriodicTimer), pSSM);
663
664 SSMR3PutS64(pSSM, pThis->next_periodic_time);
665
666 SSMR3PutS64(pSSM, pThis->next_second_time);
667 TMR3TimerSave(pThis->CTX_SUFF(pSecondTimer), pSSM);
668 TMR3TimerSave(pThis->CTX_SUFF(pSecondTimer2), pSSM);
669
670 SSMR3PutBool(pSSM, pThis->fDisabledByHpet);
671
672 SSMR3PutMem(pSSM, &pThis->cmos_data[128], 128);
673 return SSMR3PutU8(pSSM, pThis->cmos_index[1]);
674}
675
676
677/**
678 * @copydoc FNSSMDEVLOADEXEC
679 */
680static DECLCALLBACK(int) rtcLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
681{
682 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
683 int rc;
684
685 if ( uVersion != RTC_SAVED_STATE_VERSION
686 && uVersion != RTC_SAVED_STATE_VERSION_VBOX_32PRE
687 && uVersion != RTC_SAVED_STATE_VERSION_VBOX_31
688 && uVersion != RTC_SAVED_STATE_VERSION_VBOX_30)
689 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
690
691 /* The config. */
692 if (uVersion > RTC_SAVED_STATE_VERSION_VBOX_30)
693 {
694 uint8_t u8Irq;
695 rc = SSMR3GetU8(pSSM, &u8Irq); AssertRCReturn(rc, rc);
696 if (u8Irq != pThis->irq)
697 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - u8Irq: saved=%#x config=%#x"), u8Irq, pThis->irq);
698
699 RTIOPORT IOPortBase;
700 rc = SSMR3GetIOPort(pSSM, &IOPortBase); AssertRCReturn(rc, rc);
701 if (IOPortBase != pThis->IOPortBase)
702 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - IOPortBase: saved=%RTiop config=%RTiop"), IOPortBase, pThis->IOPortBase);
703
704 bool fUTC;
705 rc = SSMR3GetBool(pSSM, &fUTC); AssertRCReturn(rc, rc);
706 if (fUTC != pThis->fUTC)
707 LogRel(("RTC: Config mismatch - fUTC: saved=%RTbool config=%RTbool\n", fUTC, pThis->fUTC));
708 }
709
710 if (uPass != SSM_PASS_FINAL)
711 return VINF_SUCCESS;
712
713 /* The state. */
714 SSMR3GetMem(pSSM, pThis->cmos_data, 128);
715 SSMR3GetU8(pSSM, &pThis->cmos_index[0]);
716
717 SSMR3GetS32(pSSM, &pThis->current_tm.tm_sec);
718 SSMR3GetS32(pSSM, &pThis->current_tm.tm_min);
719 SSMR3GetS32(pSSM, &pThis->current_tm.tm_hour);
720 SSMR3GetS32(pSSM, &pThis->current_tm.tm_wday);
721 SSMR3GetS32(pSSM, &pThis->current_tm.tm_mday);
722 SSMR3GetS32(pSSM, &pThis->current_tm.tm_mon);
723 SSMR3GetS32(pSSM, &pThis->current_tm.tm_year);
724
725 TMR3TimerLoad(pThis->CTX_SUFF(pPeriodicTimer), pSSM);
726
727 SSMR3GetS64(pSSM, &pThis->next_periodic_time);
728
729 SSMR3GetS64(pSSM, &pThis->next_second_time);
730 TMR3TimerLoad(pThis->CTX_SUFF(pSecondTimer), pSSM);
731 TMR3TimerLoad(pThis->CTX_SUFF(pSecondTimer2), pSSM);
732
733 if (uVersion > RTC_SAVED_STATE_VERSION_VBOX_31)
734 SSMR3GetBool(pSSM, &pThis->fDisabledByHpet);
735
736 if (uVersion > RTC_SAVED_STATE_VERSION_VBOX_32PRE)
737 {
738 /* Second CMOS bank. */
739 SSMR3GetMem(pSSM, &pThis->cmos_data[128], 128);
740 SSMR3GetU8(pSSM, &pThis->cmos_index[1]);
741 }
742
743 int period_code = pThis->cmos_data[RTC_REG_A] & 0x0f;
744 if ( period_code != 0
745 && (pThis->cmos_data[RTC_REG_B] & REG_B_PIE)) {
746 if (period_code <= 2)
747 period_code += 7;
748 int period = 1 << (period_code - 1);
749 LogRel(("RTC: period=%#x (%d) %u Hz (restore)\n", period, period, _32K / period));
750 pThis->CurPeriod = period;
751 } else {
752 LogRel(("RTC: stopped the periodic timer (restore)\n"));
753 pThis->CurPeriod = 0;
754 }
755 pThis->cRelLogEntries = 0;
756
757 return VINF_SUCCESS;
758}
759
760
761/* -=-=-=-=-=- PDM Interface provided by the RTC device -=-=-=-=-=- */
762
763/**
764 * Calculate and update the standard CMOS checksum.
765 *
766 * @param pThis Pointer to the RTC state data.
767 */
768static void rtcCalcCRC(RTCState *pThis)
769{
770 uint16_t u16;
771 unsigned i;
772
773 for (i = RTC_CRC_START, u16 = 0; i <= RTC_CRC_LAST; i++)
774 u16 += pThis->cmos_data[i];
775 pThis->cmos_data[RTC_CRC_LOW] = u16 & 0xff;
776 pThis->cmos_data[RTC_CRC_HIGH] = (u16 >> 8) & 0xff;
777}
778
779
780/**
781 * Write to a CMOS register and update the checksum if necessary.
782 *
783 * @returns VBox status code.
784 * @param pDevIns Device instance of the RTC.
785 * @param iReg The CMOS register index; bit 8 determines bank.
786 * @param u8Value The CMOS register value.
787 */
788static DECLCALLBACK(int) rtcCMOSWrite(PPDMDEVINS pDevIns, unsigned iReg, uint8_t u8Value)
789{
790 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
791 if (iReg < RT_ELEMENTS(pThis->cmos_data))
792 {
793 pThis->cmos_data[iReg] = u8Value;
794
795 /* does it require checksum update? */
796 if ( iReg >= RTC_CRC_START
797 && iReg <= RTC_CRC_LAST)
798 rtcCalcCRC(pThis);
799
800 return VINF_SUCCESS;
801 }
802 AssertMsgFailed(("iReg=%d\n", iReg));
803 return VERR_INVALID_PARAMETER;
804}
805
806
807/**
808 * Read a CMOS register.
809 *
810 * @returns VBox status code.
811 * @param pDevIns Device instance of the RTC.
812 * @param iReg The CMOS register index; bit 8 determines bank.
813 * @param pu8Value Where to store the CMOS register value.
814 */
815static DECLCALLBACK(int) rtcCMOSRead(PPDMDEVINS pDevIns, unsigned iReg, uint8_t *pu8Value)
816{
817 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
818 if (iReg < RT_ELEMENTS(pThis->cmos_data))
819 {
820 *pu8Value = pThis->cmos_data[iReg];
821 return VINF_SUCCESS;
822 }
823 AssertMsgFailed(("iReg=%d\n", iReg));
824 return VERR_INVALID_PARAMETER;
825}
826
827
828/* -=-=-=-=-=- based on bits from pc.c -=-=-=-=-=- */
829
830/** @copydoc FNPDMDEVINITCOMPLETE */
831static DECLCALLBACK(int) rtcInitComplete(PPDMDEVINS pDevIns)
832{
833 /** @todo this should be (re)done at power on if we didn't load a state... */
834 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
835
836 /*
837 * Set the CMOS date/time.
838 */
839 RTTIMESPEC Now;
840 PDMDevHlpTMUtcNow(pDevIns, &Now);
841 RTTIME Time;
842 if (pThis->fUTC)
843 RTTimeExplode(&Time, &Now);
844 else
845 RTTimeLocalExplode(&Time, &Now);
846
847 struct my_tm Tm;
848 memset(&Tm, 0, sizeof(Tm));
849 Tm.tm_year = Time.i32Year - 1900;
850 Tm.tm_mon = Time.u8Month - 1;
851 Tm.tm_mday = Time.u8MonthDay;
852 Tm.tm_wday = (Time.u8WeekDay + 1 + 7) % 7; /* 0 = monday -> sunday */
853 Tm.tm_yday = Time.u16YearDay - 1;
854 Tm.tm_hour = Time.u8Hour;
855 Tm.tm_min = Time.u8Minute;
856 Tm.tm_sec = Time.u8Second;
857
858 rtc_set_date(pThis, &Tm);
859
860 int iYear = to_bcd(pThis, (Tm.tm_year / 100) + 19); /* tm_year is 1900 based */
861 rtc_set_memory(pThis, 0x32, iYear); /* 32h - Century Byte (BCD value for the century */
862 rtc_set_memory(pThis, 0x37, iYear); /* 37h - (IBM PS/2) Date Century Byte */
863
864 /*
865 * Recalculate the checksum just in case.
866 */
867 rtcCalcCRC(pThis);
868
869 Log(("CMOS bank 0: \n%16.128Rhxd\n", &pThis->cmos_data[0]));
870 Log(("CMOS bank 1: \n%16.128Rhxd\n", &pThis->cmos_data[128]));
871 return VINF_SUCCESS;
872}
873
874
875/* -=-=-=-=-=- real code -=-=-=-=-=- */
876
877/**
878 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
879 */
880static DECLCALLBACK(void *) rtcQueryInterface(PPDMIBASE pInterface, const char *pszIID)
881{
882 PPDMDEVINS pDevIns = RT_FROM_MEMBER(pInterface, PDMDEVINS, IBase);
883 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
884 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDevIns->IBase);
885 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHPETLEGACYNOTIFY, &pThis->IHpetLegacyNotify);
886 return NULL;
887}
888
889
890/**
891 * @interface_method_impl{PDMIHPETLEGACYNOTIFY,pfnModeChanged}
892 */
893static DECLCALLBACK(void) rtcHpetLegacyNotify_ModeChanged(PPDMIHPETLEGACYNOTIFY pInterface, bool fActivated)
894{
895 RTCState *pThis = RT_FROM_MEMBER(pInterface, RTCState, IHpetLegacyNotify);
896 pThis->fDisabledByHpet = fActivated;
897}
898
899
900/**
901 * @copydoc
902 */
903static DECLCALLBACK(void) rtcRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
904{
905 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
906
907 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
908 pThis->pPeriodicTimerRC = TMTimerRCPtr(pThis->pPeriodicTimerR3);
909 pThis->pSecondTimerRC = TMTimerRCPtr(pThis->pSecondTimerR3);
910 pThis->pSecondTimer2RC = TMTimerRCPtr(pThis->pSecondTimer2R3);
911}
912
913
914/**
915 * @interface_method_impl{PDMDEVREG,pfnConstruct}
916 */
917static DECLCALLBACK(int) rtcConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
918{
919 RTCState *pThis = PDMINS_2_DATA(pDevIns, RTCState *);
920 int rc;
921 Assert(iInstance == 0);
922
923 /*
924 * Validate configuration.
925 */
926 if (!CFGMR3AreValuesValid(pCfg,
927 "Irq\0"
928 "Base\0"
929 "UseUTC\0"
930 "GCEnabled\0"
931 "R0Enabled\0"))
932 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
933
934 /*
935 * Init the data.
936 */
937 uint8_t u8Irq;
938 rc = CFGMR3QueryU8Def(pCfg, "Irq", &u8Irq, 8);
939 if (RT_FAILURE(rc))
940 return PDMDEV_SET_ERROR(pDevIns, rc,
941 N_("Configuration error: Querying \"Irq\" as a uint8_t failed"));
942 pThis->irq = u8Irq;
943
944 rc = CFGMR3QueryPortDef(pCfg, "Base", &pThis->IOPortBase, 0x70);
945 if (RT_FAILURE(rc))
946 return PDMDEV_SET_ERROR(pDevIns, rc,
947 N_("Configuration error: Querying \"Base\" as a RTIOPORT failed"));
948
949 rc = CFGMR3QueryBoolDef(pCfg, "UseUTC", &pThis->fUTC, false);
950 if (RT_FAILURE(rc))
951 return PDMDEV_SET_ERROR(pDevIns, rc,
952 N_("Configuration error: Querying \"UseUTC\" as a bool failed"));
953
954 bool fGCEnabled;
955 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &fGCEnabled, true);
956 if (RT_FAILURE(rc))
957 return PDMDEV_SET_ERROR(pDevIns, rc,
958 N_("Configuration error: failed to read GCEnabled as boolean"));
959
960 bool fR0Enabled;
961 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &fR0Enabled, true);
962 if (RT_FAILURE(rc))
963 return PDMDEV_SET_ERROR(pDevIns, rc,
964 N_("Configuration error: failed to read R0Enabled as boolean"));
965
966 Log(("RTC: Irq=%#x Base=%#x fGCEnabled=%RTbool fR0Enabled=%RTbool\n",
967 u8Irq, pThis->IOPortBase, fGCEnabled, fR0Enabled));
968
969
970 pThis->pDevInsR3 = pDevIns;
971 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
972 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
973 pThis->cmos_data[RTC_REG_A] = 0x26;
974 pThis->cmos_data[RTC_REG_B] = 0x02;
975 pThis->cmos_data[RTC_REG_C] = 0x00;
976 pThis->cmos_data[RTC_REG_D] = 0x80;
977 pThis->RtcReg.u32Version = PDM_RTCREG_VERSION;
978 pThis->RtcReg.pfnRead = rtcCMOSRead;
979 pThis->RtcReg.pfnWrite = rtcCMOSWrite;
980 pThis->fDisabledByHpet = false;
981
982 /* IBase */
983 pDevIns->IBase.pfnQueryInterface = rtcQueryInterface;
984 /* IHpetLegacyNotify */
985 pThis->IHpetLegacyNotify.pfnModeChanged = rtcHpetLegacyNotify_ModeChanged;
986
987 /*
988 * Create timers, arm them, register I/O Ports and save state.
989 */
990 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, rtcTimerPeriodic, pThis,
991 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "MC146818 RTC/CMOS - Periodic",
992 &pThis->pPeriodicTimerR3);
993 if (RT_FAILURE(rc))
994 return rc;
995 pThis->pPeriodicTimerR0 = TMTimerR0Ptr(pThis->pPeriodicTimerR3);
996 pThis->pPeriodicTimerRC = TMTimerRCPtr(pThis->pPeriodicTimerR3);
997
998 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, rtcTimerSecond, pThis,
999 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "MC146818 RTC/CMOS - Second",
1000 &pThis->pSecondTimerR3);
1001 if (RT_FAILURE(rc))
1002 return rc;
1003 pThis->pSecondTimerR0 = TMTimerR0Ptr(pThis->pSecondTimerR3);
1004 pThis->pSecondTimerRC = TMTimerRCPtr(pThis->pSecondTimerR3);
1005
1006 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL_SYNC, rtcTimerSecond2, pThis,
1007 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "MC146818 RTC/CMOS - Second2",
1008 &pThis->pSecondTimer2R3);
1009 if (RT_FAILURE(rc))
1010 return rc;
1011 pThis->pSecondTimer2R0 = TMTimerR0Ptr(pThis->pSecondTimer2R3);
1012 pThis->pSecondTimer2RC = TMTimerRCPtr(pThis->pSecondTimer2R3);
1013 pThis->next_second_time = TMTimerGet(pThis->CTX_SUFF(pSecondTimer2))
1014 + (TMTimerGetFreq(pThis->CTX_SUFF(pSecondTimer2)) * 99) / 100;
1015 rc = TMTimerSet(pThis->CTX_SUFF(pSecondTimer2), pThis->next_second_time);
1016 if (RT_FAILURE(rc))
1017 return rc;
1018
1019 rc = PDMDevHlpIOPortRegister(pDevIns, pThis->IOPortBase, 4, NULL,
1020 rtcIOPortWrite, rtcIOPortRead, NULL, NULL, "MC146818 RTC/CMOS");
1021 if (RT_FAILURE(rc))
1022 return rc;
1023 if (fGCEnabled)
1024 {
1025 rc = PDMDevHlpIOPortRegisterRC(pDevIns, pThis->IOPortBase, 4, 0,
1026 "rtcIOPortWrite", "rtcIOPortRead", NULL, NULL, "MC146818 RTC/CMOS");
1027 if (RT_FAILURE(rc))
1028 return rc;
1029 }
1030 if (fR0Enabled)
1031 {
1032 rc = PDMDevHlpIOPortRegisterR0(pDevIns, pThis->IOPortBase, 4, 0,
1033 "rtcIOPortWrite", "rtcIOPortRead", NULL, NULL, "MC146818 RTC/CMOS");
1034 if (RT_FAILURE(rc))
1035 return rc;
1036 }
1037
1038 rc = PDMDevHlpSSMRegister3(pDevIns, RTC_SAVED_STATE_VERSION, sizeof(*pThis), rtcLiveExec, rtcSaveExec, rtcLoadExec);
1039 if (RT_FAILURE(rc))
1040 return rc;
1041
1042 /*
1043 * Register ourselves as the RTC/CMOS with PDM.
1044 */
1045 rc = PDMDevHlpRTCRegister(pDevIns, &pThis->RtcReg, &pThis->pRtcHlpR3);
1046 if (RT_FAILURE(rc))
1047 return rc;
1048
1049 return VINF_SUCCESS;
1050}
1051
1052
1053/**
1054 * The device registration structure.
1055 */
1056const PDMDEVREG g_DeviceMC146818 =
1057{
1058 /* u32Version */
1059 PDM_DEVREG_VERSION,
1060 /* szName */
1061 "mc146818",
1062 /* szRCMod */
1063 "VBoxDDGC.gc",
1064 /* szR0Mod */
1065 "VBoxDDR0.r0",
1066 /* pszDescription */
1067 "Motorola MC146818 RTC/CMOS Device.",
1068 /* fFlags */
1069 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
1070 /* fClass */
1071 PDM_DEVREG_CLASS_RTC,
1072 /* cMaxInstances */
1073 1,
1074 /* cbInstance */
1075 sizeof(RTCState),
1076 /* pfnConstruct */
1077 rtcConstruct,
1078 /* pfnDestruct */
1079 NULL,
1080 /* pfnRelocate */
1081 rtcRelocate,
1082 /* pfnIOCtl */
1083 NULL,
1084 /* pfnPowerOn */
1085 NULL,
1086 /* pfnReset */
1087 NULL,
1088 /* pfnSuspend */
1089 NULL,
1090 /* pfnResume */
1091 NULL,
1092 /* pfnAttach */
1093 NULL,
1094 /* pfnDetach */
1095 NULL,
1096 /* pfnQueryInterface */
1097 NULL,
1098 /* pfnInitComplete */
1099 rtcInitComplete,
1100 /* pfnPowerOff */
1101 NULL,
1102 /* pfnSoftReset */
1103 NULL,
1104 /* u32VersionEnd */
1105 PDM_DEVREG_VERSION
1106};
1107
1108#endif /* IN_RING3 */
1109#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
1110
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette