2 * QEMU ICH9 TCO emulation tests
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.
9 #include "qemu/osdep.h"
13 #include "libqos/pci.h"
14 #include "libqos/pci-pc.h"
15 #include "hw/pci/pci_regs.h"
16 #include "hw/i386/ich9.h"
17 #include "hw/acpi/ich9.h"
18 #include "hw/acpi/tco.h"
20 #define RCBA_BASE_ADDR 0xfed1c000
21 #define PM_IO_BASE_ADDR 0xb000
24 TCO_RLD_DEFAULT = 0x0000,
25 TCO_DAT_IN_DEFAULT = 0x00,
26 TCO_DAT_OUT_DEFAULT = 0x00,
27 TCO1_STS_DEFAULT = 0x0000,
28 TCO2_STS_DEFAULT = 0x0000,
29 TCO1_CNT_DEFAULT = 0x0000,
30 TCO2_CNT_DEFAULT = 0x0008,
31 TCO_MESSAGE1_DEFAULT = 0x00,
32 TCO_MESSAGE2_DEFAULT = 0x00,
33 TCO_WDCNT_DEFAULT = 0x00,
34 TCO_TMR_DEFAULT = 0x0004,
35 SW_IRQ_GEN_DEFAULT = 0x03,
38 #define TCO_SECS_TO_TICKS(secs) (((secs) * 10) / 6)
39 #define TCO_TICKS_TO_SECS(ticks) (((ticks) * 6) / 10)
48 static void test_init(TestData *d)
54 s = g_strdup_printf("-machine q35 %s %s",
55 d->noreboot ? "" : "-global ICH9-LPC.noreboot=false",
56 !d->args ? "" : d->args);
58 qtest_irq_intercept_in(qs, "ioapic");
62 d->dev = qpci_device_find(bus, QPCI_DEVFN(0x1f, 0x00));
63 g_assert(d->dev != NULL);
65 qpci_device_enable(d->dev);
67 /* set ACPI PM I/O space base address */
68 qpci_config_writel(d->dev, ICH9_LPC_PMBASE, PM_IO_BASE_ADDR | 0x1);
70 qpci_config_writeb(d->dev, ICH9_LPC_ACPI_CTRL, 0x80);
71 /* set Root Complex BAR */
72 qpci_config_writel(d->dev, ICH9_LPC_RCBA, RCBA_BASE_ADDR | 0x1);
74 d->tco_io_base = (void *)((uintptr_t)PM_IO_BASE_ADDR + 0x60);
77 static void stop_tco(const TestData *d)
81 val = qpci_io_readw(d->dev, d->tco_io_base + TCO1_CNT);
83 qpci_io_writew(d->dev, d->tco_io_base + TCO1_CNT, val);
86 static void start_tco(const TestData *d)
90 val = qpci_io_readw(d->dev, d->tco_io_base + TCO1_CNT);
92 qpci_io_writew(d->dev, d->tco_io_base + TCO1_CNT, val);
95 static void load_tco(const TestData *d)
97 qpci_io_writew(d->dev, d->tco_io_base + TCO_RLD, 4);
100 static void set_tco_timeout(const TestData *d, uint16_t ticks)
102 qpci_io_writew(d->dev, d->tco_io_base + TCO_TMR, ticks);
105 static void clear_tco_status(const TestData *d)
107 qpci_io_writew(d->dev, d->tco_io_base + TCO1_STS, 0x0008);
108 qpci_io_writew(d->dev, d->tco_io_base + TCO2_STS, 0x0002);
109 qpci_io_writew(d->dev, d->tco_io_base + TCO2_STS, 0x0004);
112 static void reset_on_second_timeout(bool enable)
116 val = readl(RCBA_BASE_ADDR + ICH9_CC_GCS);
118 val &= ~ICH9_CC_GCS_NO_REBOOT;
120 val |= ICH9_CC_GCS_NO_REBOOT;
122 writel(RCBA_BASE_ADDR + ICH9_CC_GCS, val);
125 static void test_tco_defaults(void)
132 g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_base + TCO_RLD), ==,
134 /* TCO_DAT_IN & TCO_DAT_OUT */
135 g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_base + TCO_DAT_IN), ==,
136 (TCO_DAT_OUT_DEFAULT << 8) | TCO_DAT_IN_DEFAULT);
137 /* TCO1_STS & TCO2_STS */
138 g_assert_cmpint(qpci_io_readl(d.dev, d.tco_io_base + TCO1_STS), ==,
139 (TCO2_STS_DEFAULT << 16) | TCO1_STS_DEFAULT);
140 /* TCO1_CNT & TCO2_CNT */
141 g_assert_cmpint(qpci_io_readl(d.dev, d.tco_io_base + TCO1_CNT), ==,
142 (TCO2_CNT_DEFAULT << 16) | TCO1_CNT_DEFAULT);
143 /* TCO_MESSAGE1 & TCO_MESSAGE2 */
144 g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_base + TCO_MESSAGE1), ==,
145 (TCO_MESSAGE2_DEFAULT << 8) | TCO_MESSAGE1_DEFAULT);
146 g_assert_cmpint(qpci_io_readb(d.dev, d.tco_io_base + TCO_WDCNT), ==,
148 g_assert_cmpint(qpci_io_readb(d.dev, d.tco_io_base + SW_IRQ_GEN), ==,
150 g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_base + TCO_TMR), ==,
155 static void test_tco_timeout(void)
158 const uint16_t ticks = TCO_SECS_TO_TICKS(4);
167 clear_tco_status(&d);
168 reset_on_second_timeout(false);
169 set_tco_timeout(&d, ticks);
172 clock_step(ticks * TCO_TICK_NSEC);
174 /* test first timeout */
175 val = qpci_io_readw(d.dev, d.tco_io_base + TCO1_STS);
176 ret = val & TCO_TIMEOUT ? 1 : 0;
179 /* test clearing timeout bit */
181 qpci_io_writew(d.dev, d.tco_io_base + TCO1_STS, val);
182 val = qpci_io_readw(d.dev, d.tco_io_base + TCO1_STS);
183 ret = val & TCO_TIMEOUT ? 1 : 0;
186 /* test second timeout */
187 clock_step(ticks * TCO_TICK_NSEC);
188 val = qpci_io_readw(d.dev, d.tco_io_base + TCO1_STS);
189 ret = val & TCO_TIMEOUT ? 1 : 0;
191 val = qpci_io_readw(d.dev, d.tco_io_base + TCO2_STS);
192 ret = val & TCO_SECOND_TO_STS ? 1 : 0;
199 static void test_tco_max_timeout(void)
202 const uint16_t ticks = 0xffff;
211 clear_tco_status(&d);
212 reset_on_second_timeout(false);
213 set_tco_timeout(&d, ticks);
216 clock_step(((ticks & TCO_TMR_MASK) - 1) * TCO_TICK_NSEC);
218 val = qpci_io_readw(d.dev, d.tco_io_base + TCO_RLD);
219 g_assert_cmpint(val & TCO_RLD_MASK, ==, 1);
220 val = qpci_io_readw(d.dev, d.tco_io_base + TCO1_STS);
221 ret = val & TCO_TIMEOUT ? 1 : 0;
223 clock_step(TCO_TICK_NSEC);
224 val = qpci_io_readw(d.dev, d.tco_io_base + TCO1_STS);
225 ret = val & TCO_TIMEOUT ? 1 : 0;
232 static QDict *get_watchdog_action(void)
236 g_assert(!strcmp(qdict_get_str(ev, "event"), "WATCHDOG"));
238 data = qdict_get_qdict(ev, "data");
244 static void test_tco_second_timeout_pause(void)
247 const uint16_t ticks = TCO_SECS_TO_TICKS(32);
250 td.args = "-watchdog-action pause";
255 clear_tco_status(&td);
256 reset_on_second_timeout(true);
257 set_tco_timeout(&td, TCO_SECS_TO_TICKS(16));
260 clock_step(ticks * TCO_TICK_NSEC * 2);
261 ad = get_watchdog_action();
262 g_assert(!strcmp(qdict_get_str(ad, "action"), "pause"));
269 static void test_tco_second_timeout_reset(void)
272 const uint16_t ticks = TCO_SECS_TO_TICKS(16);
275 td.args = "-watchdog-action reset";
280 clear_tco_status(&td);
281 reset_on_second_timeout(true);
282 set_tco_timeout(&td, TCO_SECS_TO_TICKS(16));
285 clock_step(ticks * TCO_TICK_NSEC * 2);
286 ad = get_watchdog_action();
287 g_assert(!strcmp(qdict_get_str(ad, "action"), "reset"));
294 static void test_tco_second_timeout_shutdown(void)
297 const uint16_t ticks = TCO_SECS_TO_TICKS(128);
300 td.args = "-watchdog-action shutdown";
305 clear_tco_status(&td);
306 reset_on_second_timeout(true);
307 set_tco_timeout(&td, ticks);
310 clock_step(ticks * TCO_TICK_NSEC * 2);
311 ad = get_watchdog_action();
312 g_assert(!strcmp(qdict_get_str(ad, "action"), "shutdown"));
319 static void test_tco_second_timeout_none(void)
322 const uint16_t ticks = TCO_SECS_TO_TICKS(256);
325 td.args = "-watchdog-action none";
330 clear_tco_status(&td);
331 reset_on_second_timeout(true);
332 set_tco_timeout(&td, ticks);
335 clock_step(ticks * TCO_TICK_NSEC * 2);
336 ad = get_watchdog_action();
337 g_assert(!strcmp(qdict_get_str(ad, "action"), "none"));
344 static void test_tco_ticks_counter(void)
347 uint16_t ticks = TCO_SECS_TO_TICKS(8);
355 clear_tco_status(&d);
356 reset_on_second_timeout(false);
357 set_tco_timeout(&d, ticks);
362 rld = qpci_io_readw(d.dev, d.tco_io_base + TCO_RLD) & TCO_RLD_MASK;
363 g_assert_cmpint(rld, ==, ticks);
364 clock_step(TCO_TICK_NSEC);
366 } while (!(qpci_io_readw(d.dev, d.tco_io_base + TCO1_STS) & TCO_TIMEOUT));
372 static void test_tco1_control_bits(void)
382 qpci_io_writew(d.dev, d.tco_io_base + TCO1_CNT, val);
384 qpci_io_writew(d.dev, d.tco_io_base + TCO1_CNT, val);
385 g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_base + TCO1_CNT), ==,
390 static void test_tco1_status_bits(void)
402 clear_tco_status(&d);
403 reset_on_second_timeout(false);
404 set_tco_timeout(&d, ticks);
407 clock_step(ticks * TCO_TICK_NSEC);
409 qpci_io_writeb(d.dev, d.tco_io_base + TCO_DAT_IN, 0);
410 qpci_io_writeb(d.dev, d.tco_io_base + TCO_DAT_OUT, 0);
411 val = qpci_io_readw(d.dev, d.tco_io_base + TCO1_STS);
412 ret = val & (TCO_TIMEOUT | SW_TCO_SMI | TCO_INT_STS) ? 1 : 0;
414 qpci_io_writew(d.dev, d.tco_io_base + TCO1_STS, val);
415 g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_base + TCO1_STS), ==, 0);
419 static void test_tco2_status_bits(void)
431 clear_tco_status(&d);
432 reset_on_second_timeout(true);
433 set_tco_timeout(&d, ticks);
436 clock_step(ticks * TCO_TICK_NSEC * 2);
438 val = qpci_io_readw(d.dev, d.tco_io_base + TCO2_STS);
439 ret = val & (TCO_SECOND_TO_STS | TCO_BOOT_STS) ? 1 : 0;
441 qpci_io_writew(d.dev, d.tco_io_base + TCO2_STS, val);
442 g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_base + TCO2_STS), ==, 0);
446 int main(int argc, char **argv)
448 g_test_init(&argc, &argv, NULL);
450 qtest_add_func("tco/defaults", test_tco_defaults);
451 qtest_add_func("tco/timeout/no_action", test_tco_timeout);
452 qtest_add_func("tco/timeout/no_action/max", test_tco_max_timeout);
453 qtest_add_func("tco/second_timeout/pause", test_tco_second_timeout_pause);
454 qtest_add_func("tco/second_timeout/reset", test_tco_second_timeout_reset);
455 qtest_add_func("tco/second_timeout/shutdown",
456 test_tco_second_timeout_shutdown);
457 qtest_add_func("tco/second_timeout/none", test_tco_second_timeout_none);
458 qtest_add_func("tco/counter", test_tco_ticks_counter);
459 qtest_add_func("tco/tco1_control/bits", test_tco1_control_bits);
460 qtest_add_func("tco/tco1_status/bits", test_tco1_status_bits);
461 qtest_add_func("tco/tco2_status/bits", test_tco2_status_bits);