timer: rename NSEC_PER_SEC due to Mac OS X header clash
[qemu.git] / tests / wdt_ib700-test.c
1 /*
2 * QTest testcase for the IB700 watchdog
3 *
4 * Copyright (c) 2014 Red Hat, Inc.
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9
10 #include <glib.h>
11 #include <string.h>
12 #include "libqtest.h"
13 #include "qemu/osdep.h"
14 #include "qemu/timer.h"
15
16 static void qmp_check_no_event(void)
17 {
18 QDict *resp = qmp("{'execute':'query-status'}");
19 g_assert(qdict_haskey(resp, "return"));
20 QDECREF(resp);
21 }
22
23 static QDict *qmp_get_event(const char *name)
24 {
25 QDict *event = qmp("");
26 QDict *data;
27 g_assert(qdict_haskey(event, "event"));
28 g_assert(!strcmp(qdict_get_str(event, "event"), name));
29
30 if (qdict_haskey(event, "data")) {
31 data = qdict_get_qdict(event, "data");
32 QINCREF(data);
33 } else {
34 data = NULL;
35 }
36
37 QDECREF(event);
38 return data;
39 }
40
41 static QDict *ib700_program_and_wait(QTestState *s)
42 {
43 clock_step(NANOSECONDS_PER_SECOND * 40);
44 qmp_check_no_event();
45
46 /* 2 second limit */
47 outb(0x443, 14);
48
49 /* Ping */
50 clock_step(NANOSECONDS_PER_SECOND);
51 qmp_check_no_event();
52 outb(0x443, 14);
53
54 /* Disable */
55 clock_step(NANOSECONDS_PER_SECOND);
56 qmp_check_no_event();
57 outb(0x441, 1);
58 clock_step(3 * NANOSECONDS_PER_SECOND);
59 qmp_check_no_event();
60
61 /* Enable and let it fire */
62 outb(0x443, 13);
63 clock_step(3 * NANOSECONDS_PER_SECOND);
64 qmp_check_no_event();
65 clock_step(2 * NANOSECONDS_PER_SECOND);
66 return qmp_get_event("WATCHDOG");
67 }
68
69
70 static void ib700_pause(void)
71 {
72 QDict *d;
73 QTestState *s = qtest_start("-watchdog-action pause -device ib700");
74 qtest_irq_intercept_in(s, "ioapic");
75 d = ib700_program_and_wait(s);
76 g_assert(!strcmp(qdict_get_str(d, "action"), "pause"));
77 QDECREF(d);
78 d = qmp_get_event("STOP");
79 QDECREF(d);
80 qtest_end();
81 }
82
83 static void ib700_reset(void)
84 {
85 QDict *d;
86 QTestState *s = qtest_start("-watchdog-action reset -device ib700");
87 qtest_irq_intercept_in(s, "ioapic");
88 d = ib700_program_and_wait(s);
89 g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
90 QDECREF(d);
91 d = qmp_get_event("RESET");
92 QDECREF(d);
93 qtest_end();
94 }
95
96 static void ib700_shutdown(void)
97 {
98 QDict *d;
99 QTestState *s = qtest_start("-watchdog-action reset -no-reboot -device ib700");
100 qtest_irq_intercept_in(s, "ioapic");
101 d = ib700_program_and_wait(s);
102 g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
103 QDECREF(d);
104 d = qmp_get_event("SHUTDOWN");
105 QDECREF(d);
106 qtest_end();
107 }
108
109 static void ib700_none(void)
110 {
111 QDict *d;
112 QTestState *s = qtest_start("-watchdog-action none -device ib700");
113 qtest_irq_intercept_in(s, "ioapic");
114 d = ib700_program_and_wait(s);
115 g_assert(!strcmp(qdict_get_str(d, "action"), "none"));
116 QDECREF(d);
117 qtest_end();
118 }
119
120 int main(int argc, char **argv)
121 {
122 int ret;
123
124 g_test_init(&argc, &argv, NULL);
125 qtest_add_func("/wdt_ib700/pause", ib700_pause);
126 qtest_add_func("/wdt_ib700/reset", ib700_reset);
127 qtest_add_func("/wdt_ib700/shutdown", ib700_shutdown);
128 qtest_add_func("/wdt_ib700/none", ib700_none);
129
130 ret = g_test_run();
131
132 return ret;
133 }