]> git.proxmox.com Git - mirror_qemu.git/blob - tests/m48t59-test.c
virtio: add AioContext-specific function for host notifiers
[mirror_qemu.git] / tests / m48t59-test.c
1 /*
2 * QTest testcase for the M48T59 and M48T08 real-time clocks
3 *
4 * Based on MC146818 RTC test:
5 * Copyright IBM, Corp. 2012
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 *
13 */
14
15 #include "qemu/osdep.h"
16 #include <glib.h>
17
18 #include "libqtest.h"
19
20 #define RTC_SECONDS 0x9
21 #define RTC_MINUTES 0xa
22 #define RTC_HOURS 0xb
23
24 #define RTC_DAY_OF_WEEK 0xc
25 #define RTC_DAY_OF_MONTH 0xd
26 #define RTC_MONTH 0xe
27 #define RTC_YEAR 0xf
28
29 static uint32_t base;
30 static uint16_t reg_base = 0x1ff0; /* 0x7f0 for m48t02 */
31 static int base_year;
32 static bool use_mmio;
33
34 static uint8_t cmos_read_mmio(uint8_t reg)
35 {
36 return readb(base + (uint32_t)reg_base + (uint32_t)reg);
37 }
38
39 static void cmos_write_mmio(uint8_t reg, uint8_t val)
40 {
41 uint8_t data = val;
42
43 writeb(base + (uint32_t)reg_base + (uint32_t)reg, data);
44 }
45
46 static uint8_t cmos_read_ioio(uint8_t reg)
47 {
48 outw(base + 0, reg_base + (uint16_t)reg);
49 return inb(base + 3);
50 }
51
52 static void cmos_write_ioio(uint8_t reg, uint8_t val)
53 {
54 outw(base + 0, reg_base + (uint16_t)reg);
55 outb(base + 3, val);
56 }
57
58 static uint8_t cmos_read(uint8_t reg)
59 {
60 if (use_mmio) {
61 return cmos_read_mmio(reg);
62 } else {
63 return cmos_read_ioio(reg);
64 }
65 }
66
67 static void cmos_write(uint8_t reg, uint8_t val)
68 {
69 if (use_mmio) {
70 cmos_write_mmio(reg, val);
71 } else {
72 cmos_write_ioio(reg, val);
73 }
74 }
75
76 static int bcd2dec(int value)
77 {
78 return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
79 }
80
81 static int tm_cmp(struct tm *lhs, struct tm *rhs)
82 {
83 time_t a, b;
84 struct tm d1, d2;
85
86 memcpy(&d1, lhs, sizeof(d1));
87 memcpy(&d2, rhs, sizeof(d2));
88
89 a = mktime(&d1);
90 b = mktime(&d2);
91
92 if (a < b) {
93 return -1;
94 } else if (a > b) {
95 return 1;
96 }
97
98 return 0;
99 }
100
101 #if 0
102 static void print_tm(struct tm *tm)
103 {
104 printf("%04d-%02d-%02d %02d:%02d:%02d %+02ld\n",
105 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
106 tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
107 }
108 #endif
109
110 static void cmos_get_date_time(struct tm *date)
111 {
112 int sec, min, hour, mday, mon, year;
113 time_t ts;
114 struct tm dummy;
115
116 sec = cmos_read(RTC_SECONDS);
117 min = cmos_read(RTC_MINUTES);
118 hour = cmos_read(RTC_HOURS);
119 mday = cmos_read(RTC_DAY_OF_MONTH);
120 mon = cmos_read(RTC_MONTH);
121 year = cmos_read(RTC_YEAR);
122
123 sec = bcd2dec(sec);
124 min = bcd2dec(min);
125 hour = bcd2dec(hour);
126 mday = bcd2dec(mday);
127 mon = bcd2dec(mon);
128 year = bcd2dec(year);
129
130 ts = time(NULL);
131 localtime_r(&ts, &dummy);
132
133 date->tm_isdst = dummy.tm_isdst;
134 date->tm_sec = sec;
135 date->tm_min = min;
136 date->tm_hour = hour;
137 date->tm_mday = mday;
138 date->tm_mon = mon - 1;
139 date->tm_year = base_year + year - 1900;
140 #ifndef __sun__
141 date->tm_gmtoff = 0;
142 #endif
143
144 ts = mktime(date);
145 }
146
147 static void check_time(int wiggle)
148 {
149 struct tm start, date[4], end;
150 struct tm *datep;
151 time_t ts;
152
153 /*
154 * This check assumes a few things. First, we cannot guarantee that we get
155 * a consistent reading from the wall clock because we may hit an edge of
156 * the clock while reading. To work around this, we read four clock readings
157 * such that at least two of them should match. We need to assume that one
158 * reading is corrupt so we need four readings to ensure that we have at
159 * least two consecutive identical readings
160 *
161 * It's also possible that we'll cross an edge reading the host clock so
162 * simply check to make sure that the clock reading is within the period of
163 * when we expect it to be.
164 */
165
166 ts = time(NULL);
167 gmtime_r(&ts, &start);
168
169 cmos_get_date_time(&date[0]);
170 cmos_get_date_time(&date[1]);
171 cmos_get_date_time(&date[2]);
172 cmos_get_date_time(&date[3]);
173
174 ts = time(NULL);
175 gmtime_r(&ts, &end);
176
177 if (tm_cmp(&date[0], &date[1]) == 0) {
178 datep = &date[0];
179 } else if (tm_cmp(&date[1], &date[2]) == 0) {
180 datep = &date[1];
181 } else if (tm_cmp(&date[2], &date[3]) == 0) {
182 datep = &date[2];
183 } else {
184 g_assert_not_reached();
185 }
186
187 if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
188 long t, s;
189
190 start.tm_isdst = datep->tm_isdst;
191
192 t = (long)mktime(datep);
193 s = (long)mktime(&start);
194 if (t < s) {
195 g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
196 } else {
197 g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
198 }
199
200 g_assert_cmpint(ABS(t - s), <=, wiggle);
201 }
202 }
203
204 static int wiggle = 2;
205
206 static void bcd_check_time(void)
207 {
208 if (strcmp(qtest_get_arch(), "sparc64") == 0) {
209 base = 0x74;
210 base_year = 1900;
211 use_mmio = false;
212 } else if (strcmp(qtest_get_arch(), "sparc") == 0) {
213 base = 0x71200000;
214 base_year = 1968;
215 use_mmio = true;
216 } else { /* PPC: need to map macio in PCI */
217 g_assert_not_reached();
218 }
219 check_time(wiggle);
220 }
221
222 /* success if no crash or abort */
223 static void fuzz_registers(void)
224 {
225 unsigned int i;
226
227 for (i = 0; i < 1000; i++) {
228 uint8_t reg, val;
229
230 reg = (uint8_t)g_test_rand_int_range(0, 16);
231 val = (uint8_t)g_test_rand_int_range(0, 256);
232
233 if (reg == 7) {
234 /* watchdog setup register, may trigger system reset, skip */
235 continue;
236 }
237
238 cmos_write(reg, val);
239 cmos_read(reg);
240 }
241 }
242
243 int main(int argc, char **argv)
244 {
245 QTestState *s = NULL;
246 int ret;
247
248 g_test_init(&argc, &argv, NULL);
249
250 s = qtest_start("-rtc clock=vm");
251
252 qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
253 qtest_add_func("/rtc/fuzz-registers", fuzz_registers);
254 ret = g_test_run();
255
256 if (s) {
257 qtest_quit(s);
258 }
259
260 return ret;
261 }