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