Re: [Qemu-devel] [PATCH 01/14] tests: more thorough tests of ds1338

2018-07-17 Thread Peter Maydell
On 5 July 2018 at 19:19, Michael Davidsaver  wrote:
> Test current time and set+get round trip.
>
> Separate current time test from set/get tests
> to avoid test needing to impose order, or to
> have a magic handshaketo reset RTC to current time.
>
> Signed-off-by: Michael Davidsaver 
> ---
>  tests/Makefile.include  |   4 ++
>  tests/ds-rtc-common.h   |  67 +
>  tests/ds-rtc-current-test.c |  59 ++
>  tests/ds-rtc-set-test.c | 117 
> 
>  4 files changed, 247 insertions(+)
>  create mode 100644 tests/ds-rtc-common.h
>  create mode 100644 tests/ds-rtc-current-test.c
>  create mode 100644 tests/ds-rtc-set-test.c
>
> diff --git a/tests/Makefile.include b/tests/Makefile.include
> index 8859e88ffb..6ce5a9ff4d 100644
> --- a/tests/Makefile.include
> +++ b/tests/Makefile.include
> @@ -378,6 +378,8 @@ check-qtest-sparc64-y += tests/boot-serial-test$(EXESUF)
>  check-qtest-arm-y = tests/tmp105-test$(EXESUF)
>  check-qtest-arm-y += tests/pca9552-test$(EXESUF)
>  check-qtest-arm-y += tests/ds1338-test$(EXESUF)
> +check-qtest-arm-y += tests/ds-rtc-current-test$(EXESUF)
> +check-qtest-arm-y += tests/ds-rtc-set-test$(EXESUF)
>  check-qtest-arm-y += tests/m25p80-test$(EXESUF)
>  gcov-files-arm-y += hw/misc/tmp105.c
>  check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
> @@ -787,6 +789,8 @@ tests/pxe-test$(EXESUF): tests/pxe-test.o 
> tests/boot-sector.o $(libqos-obj-y)
>  tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
>  tests/pca9552-test$(EXESUF): tests/pca9552-test.o $(libqos-omap-obj-y)
>  tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y)
> +tests/ds-rtc-current-test$(EXESUF): tests/ds-rtc-current-test.o 
> $(libqos-imx-obj-y)
> +tests/ds-rtc-set-test$(EXESUF): tests/ds-rtc-set-test.o $(libqos-imx-obj-y)
>  tests/m25p80-test$(EXESUF): tests/m25p80-test.o
>  tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
>  tests/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y)
> diff --git a/tests/ds-rtc-common.h b/tests/ds-rtc-common.h
> new file mode 100644
> index 00..c8e6c2bc5b
> --- /dev/null
> +++ b/tests/ds-rtc-common.h
> @@ -0,0 +1,67 @@
> +/* Common code for testing of Dallas/Maxim I2C bus RTC devices
> + *
> + * Copyright (c) 2018 Michael Davidsaver
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2.  See
> + * the LICENSE file in the top-level directory.
> + */
> +#ifndef DSRTCCOMMON_H
> +#define DSRTCCOMMON_H
> +
> +#include "qemu/osdep.h"

Headers should never include osdep.h.


> +#include "qemu/cutils.h"
> +#include "libqos/i2c.h"
> +
> +#define IMX25_I2C_0_BASE 0x43F8
> +#define DS1338_ADDR 0x68
> +
> +static I2CAdapter *i2c;
> +static uint8_t addr;
> +static bool use_century;
> +
> +/* input buffer must have at least 7 elements */
> +static inline time_t rtc_parse(const uint8_t *buf)
> +{
> +struct tm parts;
> +
> +parts.tm_sec = from_bcd(buf[0]);
> +parts.tm_min = from_bcd(buf[1]);
> +if (buf[2] & 0x40) {
> +/* 12 hour */
> +/* HOUR register is 1-12. */
> +parts.tm_hour = from_bcd(buf[2] & 0x1f);
> +g_assert_cmpuint(parts.tm_hour, >=, 1);
> +g_assert_cmpuint(parts.tm_hour, <=, 12);
> +parts.tm_hour %= 12u; /* wrap 12 -> 0 */
> +if (buf[2] & 0x20) {
> +parts.tm_hour += 12u;
> +}
> +} else {
> +/* 24 hour */
> +parts.tm_hour = from_bcd(buf[2] & 0x3f);
> +}
> +parts.tm_wday = from_bcd(buf[3]);
> +parts.tm_mday = from_bcd(buf[4]);
> +parts.tm_mon =  from_bcd((buf[5] & 0x1f) - 1u);
> +parts.tm_year = from_bcd(buf[6]);
> +if (!use_century || (buf[5] & 0x80)) {
> +parts.tm_year += 100u;
> +}
> +
> +return mktimegm(&parts);
> +}
> +
> +static time_t rtc_gettime(void)
> +{
> +uint8_t buf[7];
> +
> +/* zero address pointer */
> +buf[0] = 0;
> +i2c_send(i2c, addr, buf, 1);
> +/* read back current time registers */
> +i2c_recv(i2c, addr, buf, 7);
> +
> +return rtc_parse(buf);
> +}
> +
> +#endif /* DSRTCCOMMON_H */
> diff --git a/tests/ds-rtc-current-test.c b/tests/ds-rtc-current-test.c
> new file mode 100644
> index 00..6acbbed9a6
> --- /dev/null
> +++ b/tests/ds-rtc-current-test.c
> @@ -0,0 +1,59 @@
> +/* Testing of Dallas/Maxim I2C bus RTC devices
> + *
> + * Copyright (c) 2018 Michael Davidsaver
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2.  See
> + * the LICENSE file in the top-level directory.
> + */
> +#include 

This is included for you by osdep.h.

> +
> +#include "qemu/osdep.h"
> +#include "qemu/bcd.h"
> +#include "qemu/cutils.h"
> +#include "libqtest.h"
> +#include "libqos/libqos.h"
> +#include "libqos/i2c.h"
> +
> +#include "ds-rtc-common.h"
> +
> +/* read back and compare with current system time */
> +static
> +void test_rtc_current(void)
> +{
> +time_t expected, actual;
> +/* relax test to l

[Qemu-devel] [PATCH 01/14] tests: more thorough tests of ds1338

2018-07-05 Thread Michael Davidsaver
Test current time and set+get round trip.

Separate current time test from set/get tests
to avoid test needing to impose order, or to
have a magic handshaketo reset RTC to current time.

Signed-off-by: Michael Davidsaver 
---
 tests/Makefile.include  |   4 ++
 tests/ds-rtc-common.h   |  67 +
 tests/ds-rtc-current-test.c |  59 ++
 tests/ds-rtc-set-test.c | 117 
 4 files changed, 247 insertions(+)
 create mode 100644 tests/ds-rtc-common.h
 create mode 100644 tests/ds-rtc-current-test.c
 create mode 100644 tests/ds-rtc-set-test.c

diff --git a/tests/Makefile.include b/tests/Makefile.include
index 8859e88ffb..6ce5a9ff4d 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -378,6 +378,8 @@ check-qtest-sparc64-y += tests/boot-serial-test$(EXESUF)
 check-qtest-arm-y = tests/tmp105-test$(EXESUF)
 check-qtest-arm-y += tests/pca9552-test$(EXESUF)
 check-qtest-arm-y += tests/ds1338-test$(EXESUF)
+check-qtest-arm-y += tests/ds-rtc-current-test$(EXESUF)
+check-qtest-arm-y += tests/ds-rtc-set-test$(EXESUF)
 check-qtest-arm-y += tests/m25p80-test$(EXESUF)
 gcov-files-arm-y += hw/misc/tmp105.c
 check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
@@ -787,6 +789,8 @@ tests/pxe-test$(EXESUF): tests/pxe-test.o 
tests/boot-sector.o $(libqos-obj-y)
 tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
 tests/pca9552-test$(EXESUF): tests/pca9552-test.o $(libqos-omap-obj-y)
 tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y)
+tests/ds-rtc-current-test$(EXESUF): tests/ds-rtc-current-test.o 
$(libqos-imx-obj-y)
+tests/ds-rtc-set-test$(EXESUF): tests/ds-rtc-set-test.o $(libqos-imx-obj-y)
 tests/m25p80-test$(EXESUF): tests/m25p80-test.o
 tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
 tests/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y)
diff --git a/tests/ds-rtc-common.h b/tests/ds-rtc-common.h
new file mode 100644
index 00..c8e6c2bc5b
--- /dev/null
+++ b/tests/ds-rtc-common.h
@@ -0,0 +1,67 @@
+/* Common code for testing of Dallas/Maxim I2C bus RTC devices
+ *
+ * Copyright (c) 2018 Michael Davidsaver
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the LICENSE file in the top-level directory.
+ */
+#ifndef DSRTCCOMMON_H
+#define DSRTCCOMMON_H
+
+#include "qemu/osdep.h"
+#include "qemu/cutils.h"
+#include "libqos/i2c.h"
+
+#define IMX25_I2C_0_BASE 0x43F8
+#define DS1338_ADDR 0x68
+
+static I2CAdapter *i2c;
+static uint8_t addr;
+static bool use_century;
+
+/* input buffer must have at least 7 elements */
+static inline time_t rtc_parse(const uint8_t *buf)
+{
+struct tm parts;
+
+parts.tm_sec = from_bcd(buf[0]);
+parts.tm_min = from_bcd(buf[1]);
+if (buf[2] & 0x40) {
+/* 12 hour */
+/* HOUR register is 1-12. */
+parts.tm_hour = from_bcd(buf[2] & 0x1f);
+g_assert_cmpuint(parts.tm_hour, >=, 1);
+g_assert_cmpuint(parts.tm_hour, <=, 12);
+parts.tm_hour %= 12u; /* wrap 12 -> 0 */
+if (buf[2] & 0x20) {
+parts.tm_hour += 12u;
+}
+} else {
+/* 24 hour */
+parts.tm_hour = from_bcd(buf[2] & 0x3f);
+}
+parts.tm_wday = from_bcd(buf[3]);
+parts.tm_mday = from_bcd(buf[4]);
+parts.tm_mon =  from_bcd((buf[5] & 0x1f) - 1u);
+parts.tm_year = from_bcd(buf[6]);
+if (!use_century || (buf[5] & 0x80)) {
+parts.tm_year += 100u;
+}
+
+return mktimegm(&parts);
+}
+
+static time_t rtc_gettime(void)
+{
+uint8_t buf[7];
+
+/* zero address pointer */
+buf[0] = 0;
+i2c_send(i2c, addr, buf, 1);
+/* read back current time registers */
+i2c_recv(i2c, addr, buf, 7);
+
+return rtc_parse(buf);
+}
+
+#endif /* DSRTCCOMMON_H */
diff --git a/tests/ds-rtc-current-test.c b/tests/ds-rtc-current-test.c
new file mode 100644
index 00..6acbbed9a6
--- /dev/null
+++ b/tests/ds-rtc-current-test.c
@@ -0,0 +1,59 @@
+/* Testing of Dallas/Maxim I2C bus RTC devices
+ *
+ * Copyright (c) 2018 Michael Davidsaver
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the LICENSE file in the top-level directory.
+ */
+#include 
+
+#include "qemu/osdep.h"
+#include "qemu/bcd.h"
+#include "qemu/cutils.h"
+#include "libqtest.h"
+#include "libqos/libqos.h"
+#include "libqos/i2c.h"
+
+#include "ds-rtc-common.h"
+
+/* read back and compare with current system time */
+static
+void test_rtc_current(void)
+{
+time_t expected, actual;
+/* relax test to limit false positives when host may be overloaded.
+ * Allow larger delta when running "-m quick"
+ */
+time_t max_delta = g_test_slow() ? 1 : 30;
+
+actual = time(NULL);
+/* new second may start here */
+expected = rtc_gettime();
+g_assert_cmpuint(expected, <=, actual + max_delta);
+g_assert_cmpuint(expected, >=, actual);
+}
+
+int main(int argc, char *argv[])
+{
+int ret;
+con

Re: [Qemu-devel] [PATCH 01/14] tests: more thorough tests of ds1338

2018-03-26 Thread Michael Davidsaver
On 03/26/2018 02:18 AM, Paolo Bonzini wrote:
> On 24/03/2018 20:24, Michael Davidsaver wrote:
>> Test current time and set+get round trip.
>> Separate current time test from set/get
>> tests to avoid test order issues.
> 
> What are these issues?  You can start a different QEMU for each test.

Quite right, and this is now what I do.  The previous iteration was
clever about avoiding this for ... no particular reason.



Re: [Qemu-devel] [PATCH 01/14] tests: more thorough tests of ds1338

2018-03-26 Thread Paolo Bonzini
On 24/03/2018 20:24, Michael Davidsaver wrote:
> Test current time and set+get round trip.
> Separate current time test from set/get
> tests to avoid test order issues.

What are these issues?  You can start a different QEMU for each test.

Paolo

> Signed-off-by: Michael Davidsaver 




[Qemu-devel] [PATCH 01/14] tests: more thorough tests of ds1338

2018-03-24 Thread Michael Davidsaver
Test current time and set+get round trip.
Separate current time test from set/get
tests to avoid test order issues.

Signed-off-by: Michael Davidsaver 
---
 tests/Makefile.include  |   4 ++
 tests/ds-rtc-common.h   |  67 +
 tests/ds-rtc-current-test.c |  59 ++
 tests/ds-rtc-set-test.c | 117 
 4 files changed, 247 insertions(+)
 create mode 100644 tests/ds-rtc-common.h
 create mode 100644 tests/ds-rtc-current-test.c
 create mode 100644 tests/ds-rtc-set-test.c

diff --git a/tests/Makefile.include b/tests/Makefile.include
index eb218a9539..d256095c88 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -372,6 +372,8 @@ check-qtest-sparc64-y += tests/boot-serial-test$(EXESUF)
 
 check-qtest-arm-y = tests/tmp105-test$(EXESUF)
 check-qtest-arm-y += tests/ds1338-test$(EXESUF)
+check-qtest-arm-y += tests/ds-rtc-current-test$(EXESUF)
+check-qtest-arm-y += tests/ds-rtc-set-test$(EXESUF)
 check-qtest-arm-y += tests/m25p80-test$(EXESUF)
 gcov-files-arm-y += hw/misc/tmp105.c
 check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
@@ -771,6 +773,8 @@ tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \
 tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y)
 tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
 tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y)
+tests/ds-rtc-current-test$(EXESUF): tests/ds-rtc-current-test.o 
$(libqos-imx-obj-y)
+tests/ds-rtc-set-test$(EXESUF): tests/ds-rtc-set-test.o $(libqos-imx-obj-y)
 tests/m25p80-test$(EXESUF): tests/m25p80-test.o
 tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
 tests/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y)
diff --git a/tests/ds-rtc-common.h b/tests/ds-rtc-common.h
new file mode 100644
index 00..c8e6c2bc5b
--- /dev/null
+++ b/tests/ds-rtc-common.h
@@ -0,0 +1,67 @@
+/* Common code for testing of Dallas/Maxim I2C bus RTC devices
+ *
+ * Copyright (c) 2018 Michael Davidsaver
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the LICENSE file in the top-level directory.
+ */
+#ifndef DSRTCCOMMON_H
+#define DSRTCCOMMON_H
+
+#include "qemu/osdep.h"
+#include "qemu/cutils.h"
+#include "libqos/i2c.h"
+
+#define IMX25_I2C_0_BASE 0x43F8
+#define DS1338_ADDR 0x68
+
+static I2CAdapter *i2c;
+static uint8_t addr;
+static bool use_century;
+
+/* input buffer must have at least 7 elements */
+static inline time_t rtc_parse(const uint8_t *buf)
+{
+struct tm parts;
+
+parts.tm_sec = from_bcd(buf[0]);
+parts.tm_min = from_bcd(buf[1]);
+if (buf[2] & 0x40) {
+/* 12 hour */
+/* HOUR register is 1-12. */
+parts.tm_hour = from_bcd(buf[2] & 0x1f);
+g_assert_cmpuint(parts.tm_hour, >=, 1);
+g_assert_cmpuint(parts.tm_hour, <=, 12);
+parts.tm_hour %= 12u; /* wrap 12 -> 0 */
+if (buf[2] & 0x20) {
+parts.tm_hour += 12u;
+}
+} else {
+/* 24 hour */
+parts.tm_hour = from_bcd(buf[2] & 0x3f);
+}
+parts.tm_wday = from_bcd(buf[3]);
+parts.tm_mday = from_bcd(buf[4]);
+parts.tm_mon =  from_bcd((buf[5] & 0x1f) - 1u);
+parts.tm_year = from_bcd(buf[6]);
+if (!use_century || (buf[5] & 0x80)) {
+parts.tm_year += 100u;
+}
+
+return mktimegm(&parts);
+}
+
+static time_t rtc_gettime(void)
+{
+uint8_t buf[7];
+
+/* zero address pointer */
+buf[0] = 0;
+i2c_send(i2c, addr, buf, 1);
+/* read back current time registers */
+i2c_recv(i2c, addr, buf, 7);
+
+return rtc_parse(buf);
+}
+
+#endif /* DSRTCCOMMON_H */
diff --git a/tests/ds-rtc-current-test.c b/tests/ds-rtc-current-test.c
new file mode 100644
index 00..6acbbed9a6
--- /dev/null
+++ b/tests/ds-rtc-current-test.c
@@ -0,0 +1,59 @@
+/* Testing of Dallas/Maxim I2C bus RTC devices
+ *
+ * Copyright (c) 2018 Michael Davidsaver
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the LICENSE file in the top-level directory.
+ */
+#include 
+
+#include "qemu/osdep.h"
+#include "qemu/bcd.h"
+#include "qemu/cutils.h"
+#include "libqtest.h"
+#include "libqos/libqos.h"
+#include "libqos/i2c.h"
+
+#include "ds-rtc-common.h"
+
+/* read back and compare with current system time */
+static
+void test_rtc_current(void)
+{
+time_t expected, actual;
+/* relax test to limit false positives when host may be overloaded.
+ * Allow larger delta when running "-m quick"
+ */
+time_t max_delta = g_test_slow() ? 1 : 30;
+
+actual = time(NULL);
+/* new second may start here */
+expected = rtc_gettime();
+g_assert_cmpuint(expected, <=, actual + max_delta);
+g_assert_cmpuint(expected, >=, actual);
+}
+
+int main(int argc, char *argv[])
+{
+int ret;
+const char *arch = qtest_get_arch();
+QTestState *s = NULL;
+
+g_test_init(&argc, &argv, NULL);
+
+if (strcmp(arch, "arm"