xref: /openbmc/qemu/tests/qtest/tco-test.c (revision 1a6981bb)
11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * QEMU ICH9 TCO emulation tests
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2015 Paulo Alcantara <pcacjr@zytor.com>
51e8a1faeSThomas Huth  *
61e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
71e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
81e8a1faeSThomas Huth  */
91e8a1faeSThomas Huth 
101e8a1faeSThomas Huth #include "qemu/osdep.h"
111e8a1faeSThomas Huth 
12907b5105SMarc-André Lureau #include "libqtest.h"
131e8a1faeSThomas Huth #include "libqos/pci.h"
141e8a1faeSThomas Huth #include "libqos/pci-pc.h"
151e8a1faeSThomas Huth #include "qapi/qmp/qdict.h"
161e8a1faeSThomas Huth #include "hw/pci/pci_regs.h"
17*1a6981bbSBernhard Beschow #include "hw/southbridge/ich9.h"
181e8a1faeSThomas Huth #include "hw/acpi/ich9.h"
19fbae27e8SPhilippe Mathieu-Daudé #include "hw/acpi/ich9_tco.h"
201e8a1faeSThomas Huth 
211e8a1faeSThomas Huth #define RCBA_BASE_ADDR    0xfed1c000
221e8a1faeSThomas Huth #define PM_IO_BASE_ADDR   0xb000
231e8a1faeSThomas Huth 
241e8a1faeSThomas Huth enum {
251e8a1faeSThomas Huth     TCO_RLD_DEFAULT         = 0x0000,
261e8a1faeSThomas Huth     TCO_DAT_IN_DEFAULT      = 0x00,
271e8a1faeSThomas Huth     TCO_DAT_OUT_DEFAULT     = 0x00,
281e8a1faeSThomas Huth     TCO1_STS_DEFAULT        = 0x0000,
291e8a1faeSThomas Huth     TCO2_STS_DEFAULT        = 0x0000,
301e8a1faeSThomas Huth     TCO1_CNT_DEFAULT        = 0x0000,
311e8a1faeSThomas Huth     TCO2_CNT_DEFAULT        = 0x0008,
321e8a1faeSThomas Huth     TCO_MESSAGE1_DEFAULT    = 0x00,
331e8a1faeSThomas Huth     TCO_MESSAGE2_DEFAULT    = 0x00,
341e8a1faeSThomas Huth     TCO_WDCNT_DEFAULT       = 0x00,
351e8a1faeSThomas Huth     TCO_TMR_DEFAULT         = 0x0004,
361e8a1faeSThomas Huth     SW_IRQ_GEN_DEFAULT      = 0x03,
371e8a1faeSThomas Huth };
381e8a1faeSThomas Huth 
391e8a1faeSThomas Huth #define TCO_SECS_TO_TICKS(secs)     (((secs) * 10) / 6)
401e8a1faeSThomas Huth #define TCO_TICKS_TO_SECS(ticks)    (((ticks) * 6) / 10)
411e8a1faeSThomas Huth 
421e8a1faeSThomas Huth typedef struct {
431e8a1faeSThomas Huth     const char *args;
441e8a1faeSThomas Huth     bool noreboot;
451e8a1faeSThomas Huth     QPCIDevice *dev;
461e8a1faeSThomas Huth     QPCIBar tco_io_bar;
471e8a1faeSThomas Huth     QPCIBus *bus;
481e8a1faeSThomas Huth     QTestState *qts;
491e8a1faeSThomas Huth } TestData;
501e8a1faeSThomas Huth 
test_end(TestData * d)511e8a1faeSThomas Huth static void test_end(TestData *d)
521e8a1faeSThomas Huth {
531e8a1faeSThomas Huth     g_free(d->dev);
541e8a1faeSThomas Huth     qpci_free_pc(d->bus);
551e8a1faeSThomas Huth     qtest_quit(d->qts);
561e8a1faeSThomas Huth }
571e8a1faeSThomas Huth 
test_init(TestData * d)581e8a1faeSThomas Huth static void test_init(TestData *d)
591e8a1faeSThomas Huth {
601e8a1faeSThomas Huth     QTestState *qs;
611e8a1faeSThomas Huth 
621e8a1faeSThomas Huth     qs = qtest_initf("-machine q35 %s %s",
63a6b6414fSDaniel P. Berrangé                      d->noreboot ? "-global ICH9-LPC.noreboot=true" : "",
641e8a1faeSThomas Huth                      !d->args ? "" : d->args);
651e8a1faeSThomas Huth     qtest_irq_intercept_in(qs, "ioapic");
661e8a1faeSThomas Huth 
671e8a1faeSThomas Huth     d->bus = qpci_new_pc(qs, NULL);
681e8a1faeSThomas Huth     d->dev = qpci_device_find(d->bus, QPCI_DEVFN(0x1f, 0x00));
691e8a1faeSThomas Huth     g_assert(d->dev != NULL);
701e8a1faeSThomas Huth 
711e8a1faeSThomas Huth     qpci_device_enable(d->dev);
721e8a1faeSThomas Huth 
731e8a1faeSThomas Huth     /* set ACPI PM I/O space base address */
741e8a1faeSThomas Huth     qpci_config_writel(d->dev, ICH9_LPC_PMBASE, PM_IO_BASE_ADDR | 0x1);
751e8a1faeSThomas Huth     /* enable ACPI I/O */
761e8a1faeSThomas Huth     qpci_config_writeb(d->dev, ICH9_LPC_ACPI_CTRL, 0x80);
771e8a1faeSThomas Huth     /* set Root Complex BAR */
781e8a1faeSThomas Huth     qpci_config_writel(d->dev, ICH9_LPC_RCBA, RCBA_BASE_ADDR | 0x1);
791e8a1faeSThomas Huth 
801e8a1faeSThomas Huth     d->tco_io_bar = qpci_legacy_iomap(d->dev, PM_IO_BASE_ADDR + 0x60);
811e8a1faeSThomas Huth     d->qts = qs;
821e8a1faeSThomas Huth }
831e8a1faeSThomas Huth 
stop_tco(const TestData * d)841e8a1faeSThomas Huth static void stop_tco(const TestData *d)
851e8a1faeSThomas Huth {
861e8a1faeSThomas Huth     uint32_t val;
871e8a1faeSThomas Huth 
881e8a1faeSThomas Huth     val = qpci_io_readw(d->dev, d->tco_io_bar, TCO1_CNT);
891e8a1faeSThomas Huth     val |= TCO_TMR_HLT;
901e8a1faeSThomas Huth     qpci_io_writew(d->dev, d->tco_io_bar, TCO1_CNT, val);
911e8a1faeSThomas Huth }
921e8a1faeSThomas Huth 
start_tco(const TestData * d)931e8a1faeSThomas Huth static void start_tco(const TestData *d)
941e8a1faeSThomas Huth {
951e8a1faeSThomas Huth     uint32_t val;
961e8a1faeSThomas Huth 
971e8a1faeSThomas Huth     val = qpci_io_readw(d->dev, d->tco_io_bar, TCO1_CNT);
981e8a1faeSThomas Huth     val &= ~TCO_TMR_HLT;
991e8a1faeSThomas Huth     qpci_io_writew(d->dev, d->tco_io_bar, TCO1_CNT, val);
1001e8a1faeSThomas Huth }
1011e8a1faeSThomas Huth 
load_tco(const TestData * d)1021e8a1faeSThomas Huth static void load_tco(const TestData *d)
1031e8a1faeSThomas Huth {
1041e8a1faeSThomas Huth     qpci_io_writew(d->dev, d->tco_io_bar, TCO_RLD, 4);
1051e8a1faeSThomas Huth }
1061e8a1faeSThomas Huth 
set_tco_timeout(const TestData * d,uint16_t ticks)1071e8a1faeSThomas Huth static void set_tco_timeout(const TestData *d, uint16_t ticks)
1081e8a1faeSThomas Huth {
1091e8a1faeSThomas Huth     qpci_io_writew(d->dev, d->tco_io_bar, TCO_TMR, ticks);
1101e8a1faeSThomas Huth }
1111e8a1faeSThomas Huth 
clear_tco_status(const TestData * d)1121e8a1faeSThomas Huth static void clear_tco_status(const TestData *d)
1131e8a1faeSThomas Huth {
1141e8a1faeSThomas Huth     qpci_io_writew(d->dev, d->tco_io_bar, TCO1_STS, 0x0008);
1151e8a1faeSThomas Huth     qpci_io_writew(d->dev, d->tco_io_bar, TCO2_STS, 0x0002);
1161e8a1faeSThomas Huth     qpci_io_writew(d->dev, d->tco_io_bar, TCO2_STS, 0x0004);
1171e8a1faeSThomas Huth }
1181e8a1faeSThomas Huth 
reset_on_second_timeout(const TestData * td,bool enable)1191e8a1faeSThomas Huth static void reset_on_second_timeout(const TestData *td, bool enable)
1201e8a1faeSThomas Huth {
1211e8a1faeSThomas Huth     uint32_t val;
1221e8a1faeSThomas Huth 
1231e8a1faeSThomas Huth     val = qtest_readl(td->qts, RCBA_BASE_ADDR + ICH9_CC_GCS);
1241e8a1faeSThomas Huth     if (enable) {
1251e8a1faeSThomas Huth         val &= ~ICH9_CC_GCS_NO_REBOOT;
1261e8a1faeSThomas Huth     } else {
1271e8a1faeSThomas Huth         val |= ICH9_CC_GCS_NO_REBOOT;
1281e8a1faeSThomas Huth     }
1291e8a1faeSThomas Huth     qtest_writel(td->qts, RCBA_BASE_ADDR + ICH9_CC_GCS, val);
1301e8a1faeSThomas Huth }
1311e8a1faeSThomas Huth 
test_tco_defaults(void)1321e8a1faeSThomas Huth static void test_tco_defaults(void)
1331e8a1faeSThomas Huth {
1341e8a1faeSThomas Huth     TestData d;
1351e8a1faeSThomas Huth 
1361e8a1faeSThomas Huth     d.args = NULL;
1371e8a1faeSThomas Huth     d.noreboot = true;
1381e8a1faeSThomas Huth     test_init(&d);
1391e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_bar, TCO_RLD), ==,
1401e8a1faeSThomas Huth                     TCO_RLD_DEFAULT);
1411e8a1faeSThomas Huth     /* TCO_DAT_IN & TCO_DAT_OUT */
1421e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_bar, TCO_DAT_IN), ==,
1431e8a1faeSThomas Huth                     (TCO_DAT_OUT_DEFAULT << 8) | TCO_DAT_IN_DEFAULT);
1441e8a1faeSThomas Huth     /* TCO1_STS & TCO2_STS */
1451e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readl(d.dev, d.tco_io_bar, TCO1_STS), ==,
1461e8a1faeSThomas Huth                     (TCO2_STS_DEFAULT << 16) | TCO1_STS_DEFAULT);
1471e8a1faeSThomas Huth     /* TCO1_CNT & TCO2_CNT */
1481e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readl(d.dev, d.tco_io_bar, TCO1_CNT), ==,
1491e8a1faeSThomas Huth                     (TCO2_CNT_DEFAULT << 16) | TCO1_CNT_DEFAULT);
1501e8a1faeSThomas Huth     /* TCO_MESSAGE1 & TCO_MESSAGE2 */
1511e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_bar, TCO_MESSAGE1), ==,
1521e8a1faeSThomas Huth                     (TCO_MESSAGE2_DEFAULT << 8) | TCO_MESSAGE1_DEFAULT);
1531e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readb(d.dev, d.tco_io_bar, TCO_WDCNT), ==,
1541e8a1faeSThomas Huth                     TCO_WDCNT_DEFAULT);
1551e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readb(d.dev, d.tco_io_bar, SW_IRQ_GEN), ==,
1561e8a1faeSThomas Huth                     SW_IRQ_GEN_DEFAULT);
1571e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_bar, TCO_TMR), ==,
1581e8a1faeSThomas Huth                     TCO_TMR_DEFAULT);
1591e8a1faeSThomas Huth     test_end(&d);
1601e8a1faeSThomas Huth }
1611e8a1faeSThomas Huth 
test_tco_timeout(void)1621e8a1faeSThomas Huth static void test_tco_timeout(void)
1631e8a1faeSThomas Huth {
1641e8a1faeSThomas Huth     TestData d;
1651e8a1faeSThomas Huth     const uint16_t ticks = TCO_SECS_TO_TICKS(4);
1661e8a1faeSThomas Huth     uint32_t val;
1671e8a1faeSThomas Huth     int ret;
1681e8a1faeSThomas Huth 
1691e8a1faeSThomas Huth     d.args = NULL;
1701e8a1faeSThomas Huth     d.noreboot = true;
1711e8a1faeSThomas Huth     test_init(&d);
1721e8a1faeSThomas Huth 
1731e8a1faeSThomas Huth     stop_tco(&d);
1741e8a1faeSThomas Huth     clear_tco_status(&d);
1751e8a1faeSThomas Huth     reset_on_second_timeout(&d, false);
1761e8a1faeSThomas Huth     set_tco_timeout(&d, ticks);
1771e8a1faeSThomas Huth     load_tco(&d);
1781e8a1faeSThomas Huth     start_tco(&d);
1791e8a1faeSThomas Huth     qtest_clock_step(d.qts, ticks * TCO_TICK_NSEC);
1801e8a1faeSThomas Huth 
1811e8a1faeSThomas Huth     /* test first timeout */
1821e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
1831e8a1faeSThomas Huth     ret = val & TCO_TIMEOUT ? 1 : 0;
1841e8a1faeSThomas Huth     g_assert(ret == 1);
1851e8a1faeSThomas Huth 
1861e8a1faeSThomas Huth     /* test clearing timeout bit */
1871e8a1faeSThomas Huth     val |= TCO_TIMEOUT;
1881e8a1faeSThomas Huth     qpci_io_writew(d.dev, d.tco_io_bar, TCO1_STS, val);
1891e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
1901e8a1faeSThomas Huth     ret = val & TCO_TIMEOUT ? 1 : 0;
1911e8a1faeSThomas Huth     g_assert(ret == 0);
1921e8a1faeSThomas Huth 
1931e8a1faeSThomas Huth     /* test second timeout */
1941e8a1faeSThomas Huth     qtest_clock_step(d.qts, ticks * TCO_TICK_NSEC);
1951e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
1961e8a1faeSThomas Huth     ret = val & TCO_TIMEOUT ? 1 : 0;
1971e8a1faeSThomas Huth     g_assert(ret == 1);
1981e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO2_STS);
1991e8a1faeSThomas Huth     ret = val & TCO_SECOND_TO_STS ? 1 : 0;
2001e8a1faeSThomas Huth     g_assert(ret == 1);
2011e8a1faeSThomas Huth 
2021e8a1faeSThomas Huth     stop_tco(&d);
2031e8a1faeSThomas Huth     test_end(&d);
2041e8a1faeSThomas Huth }
2051e8a1faeSThomas Huth 
test_tco_max_timeout(void)2061e8a1faeSThomas Huth static void test_tco_max_timeout(void)
2071e8a1faeSThomas Huth {
2081e8a1faeSThomas Huth     TestData d;
2091e8a1faeSThomas Huth     const uint16_t ticks = 0xffff;
2101e8a1faeSThomas Huth     uint32_t val;
2111e8a1faeSThomas Huth     int ret;
2121e8a1faeSThomas Huth 
2131e8a1faeSThomas Huth     d.args = NULL;
2141e8a1faeSThomas Huth     d.noreboot = true;
2151e8a1faeSThomas Huth     test_init(&d);
2161e8a1faeSThomas Huth 
2171e8a1faeSThomas Huth     stop_tco(&d);
2181e8a1faeSThomas Huth     clear_tco_status(&d);
2191e8a1faeSThomas Huth     reset_on_second_timeout(&d, false);
2201e8a1faeSThomas Huth     set_tco_timeout(&d, ticks);
2211e8a1faeSThomas Huth     load_tco(&d);
2221e8a1faeSThomas Huth     start_tco(&d);
2231e8a1faeSThomas Huth     qtest_clock_step(d.qts, ((ticks & TCO_TMR_MASK) - 1) * TCO_TICK_NSEC);
2241e8a1faeSThomas Huth 
2251e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO_RLD);
2261e8a1faeSThomas Huth     g_assert_cmpint(val & TCO_RLD_MASK, ==, 1);
2271e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
2281e8a1faeSThomas Huth     ret = val & TCO_TIMEOUT ? 1 : 0;
2291e8a1faeSThomas Huth     g_assert(ret == 0);
2301e8a1faeSThomas Huth     qtest_clock_step(d.qts, TCO_TICK_NSEC);
2311e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
2321e8a1faeSThomas Huth     ret = val & TCO_TIMEOUT ? 1 : 0;
2331e8a1faeSThomas Huth     g_assert(ret == 1);
2341e8a1faeSThomas Huth 
2351e8a1faeSThomas Huth     stop_tco(&d);
2361e8a1faeSThomas Huth     test_end(&d);
2371e8a1faeSThomas Huth }
2381e8a1faeSThomas Huth 
get_watchdog_action(const TestData * td)2391e8a1faeSThomas Huth static QDict *get_watchdog_action(const TestData *td)
2401e8a1faeSThomas Huth {
2411e8a1faeSThomas Huth     QDict *ev = qtest_qmp_eventwait_ref(td->qts, "WATCHDOG");
2421e8a1faeSThomas Huth     QDict *data;
2431e8a1faeSThomas Huth 
2441e8a1faeSThomas Huth     data = qdict_get_qdict(ev, "data");
2451e8a1faeSThomas Huth     qobject_ref(data);
2461e8a1faeSThomas Huth     qobject_unref(ev);
2471e8a1faeSThomas Huth     return data;
2481e8a1faeSThomas Huth }
2491e8a1faeSThomas Huth 
test_tco_second_timeout_pause(void)2501e8a1faeSThomas Huth static void test_tco_second_timeout_pause(void)
2511e8a1faeSThomas Huth {
2521e8a1faeSThomas Huth     TestData td;
2531e8a1faeSThomas Huth     const uint16_t ticks = TCO_SECS_TO_TICKS(32);
2541e8a1faeSThomas Huth     QDict *ad;
2551e8a1faeSThomas Huth 
2561e8a1faeSThomas Huth     td.args = "-watchdog-action pause";
2571e8a1faeSThomas Huth     td.noreboot = false;
2581e8a1faeSThomas Huth     test_init(&td);
2591e8a1faeSThomas Huth 
2601e8a1faeSThomas Huth     stop_tco(&td);
2611e8a1faeSThomas Huth     clear_tco_status(&td);
2621e8a1faeSThomas Huth     reset_on_second_timeout(&td, true);
2631e8a1faeSThomas Huth     set_tco_timeout(&td, TCO_SECS_TO_TICKS(16));
2641e8a1faeSThomas Huth     load_tco(&td);
2651e8a1faeSThomas Huth     start_tco(&td);
2661e8a1faeSThomas Huth     qtest_clock_step(td.qts, ticks * TCO_TICK_NSEC * 2);
2671e8a1faeSThomas Huth     ad = get_watchdog_action(&td);
2681e8a1faeSThomas Huth     g_assert(!strcmp(qdict_get_str(ad, "action"), "pause"));
2691e8a1faeSThomas Huth     qobject_unref(ad);
2701e8a1faeSThomas Huth 
2711e8a1faeSThomas Huth     stop_tco(&td);
2721e8a1faeSThomas Huth     test_end(&td);
2731e8a1faeSThomas Huth }
2741e8a1faeSThomas Huth 
test_tco_second_timeout_reset(void)2751e8a1faeSThomas Huth static void test_tco_second_timeout_reset(void)
2761e8a1faeSThomas Huth {
2771e8a1faeSThomas Huth     TestData td;
2781e8a1faeSThomas Huth     const uint16_t ticks = TCO_SECS_TO_TICKS(16);
2791e8a1faeSThomas Huth     QDict *ad;
2801e8a1faeSThomas Huth 
2811e8a1faeSThomas Huth     td.args = "-watchdog-action reset";
2821e8a1faeSThomas Huth     td.noreboot = false;
2831e8a1faeSThomas Huth     test_init(&td);
2841e8a1faeSThomas Huth 
2851e8a1faeSThomas Huth     stop_tco(&td);
2861e8a1faeSThomas Huth     clear_tco_status(&td);
2871e8a1faeSThomas Huth     reset_on_second_timeout(&td, true);
2881e8a1faeSThomas Huth     set_tco_timeout(&td, TCO_SECS_TO_TICKS(16));
2891e8a1faeSThomas Huth     load_tco(&td);
2901e8a1faeSThomas Huth     start_tco(&td);
2911e8a1faeSThomas Huth     qtest_clock_step(td.qts, ticks * TCO_TICK_NSEC * 2);
2921e8a1faeSThomas Huth     ad = get_watchdog_action(&td);
2931e8a1faeSThomas Huth     g_assert(!strcmp(qdict_get_str(ad, "action"), "reset"));
2941e8a1faeSThomas Huth     qobject_unref(ad);
2951e8a1faeSThomas Huth 
2961e8a1faeSThomas Huth     stop_tco(&td);
2971e8a1faeSThomas Huth     test_end(&td);
2981e8a1faeSThomas Huth }
2991e8a1faeSThomas Huth 
test_tco_second_timeout_shutdown(void)3001e8a1faeSThomas Huth static void test_tco_second_timeout_shutdown(void)
3011e8a1faeSThomas Huth {
3021e8a1faeSThomas Huth     TestData td;
3031e8a1faeSThomas Huth     const uint16_t ticks = TCO_SECS_TO_TICKS(128);
3041e8a1faeSThomas Huth     QDict *ad;
3051e8a1faeSThomas Huth 
3061e8a1faeSThomas Huth     td.args = "-watchdog-action shutdown";
3071e8a1faeSThomas Huth     td.noreboot = false;
3081e8a1faeSThomas Huth     test_init(&td);
3091e8a1faeSThomas Huth 
3101e8a1faeSThomas Huth     stop_tco(&td);
3111e8a1faeSThomas Huth     clear_tco_status(&td);
3121e8a1faeSThomas Huth     reset_on_second_timeout(&td, true);
3131e8a1faeSThomas Huth     set_tco_timeout(&td, ticks);
3141e8a1faeSThomas Huth     load_tco(&td);
3151e8a1faeSThomas Huth     start_tco(&td);
3161e8a1faeSThomas Huth     qtest_clock_step(td.qts, ticks * TCO_TICK_NSEC * 2);
3171e8a1faeSThomas Huth     ad = get_watchdog_action(&td);
3181e8a1faeSThomas Huth     g_assert(!strcmp(qdict_get_str(ad, "action"), "shutdown"));
3191e8a1faeSThomas Huth     qobject_unref(ad);
3201e8a1faeSThomas Huth 
3211e8a1faeSThomas Huth     stop_tco(&td);
3221e8a1faeSThomas Huth     test_end(&td);
3231e8a1faeSThomas Huth }
3241e8a1faeSThomas Huth 
test_tco_second_timeout_none(void)3251e8a1faeSThomas Huth static void test_tco_second_timeout_none(void)
3261e8a1faeSThomas Huth {
3271e8a1faeSThomas Huth     TestData td;
3281e8a1faeSThomas Huth     const uint16_t ticks = TCO_SECS_TO_TICKS(256);
3291e8a1faeSThomas Huth     QDict *ad;
3301e8a1faeSThomas Huth 
3311e8a1faeSThomas Huth     td.args = "-watchdog-action none";
3321e8a1faeSThomas Huth     td.noreboot = false;
3331e8a1faeSThomas Huth     test_init(&td);
3341e8a1faeSThomas Huth 
3351e8a1faeSThomas Huth     stop_tco(&td);
3361e8a1faeSThomas Huth     clear_tco_status(&td);
3371e8a1faeSThomas Huth     reset_on_second_timeout(&td, true);
3381e8a1faeSThomas Huth     set_tco_timeout(&td, ticks);
3391e8a1faeSThomas Huth     load_tco(&td);
3401e8a1faeSThomas Huth     start_tco(&td);
3411e8a1faeSThomas Huth     qtest_clock_step(td.qts, ticks * TCO_TICK_NSEC * 2);
3421e8a1faeSThomas Huth     ad = get_watchdog_action(&td);
3431e8a1faeSThomas Huth     g_assert(!strcmp(qdict_get_str(ad, "action"), "none"));
3441e8a1faeSThomas Huth     qobject_unref(ad);
3451e8a1faeSThomas Huth 
3461e8a1faeSThomas Huth     stop_tco(&td);
3471e8a1faeSThomas Huth     test_end(&td);
3481e8a1faeSThomas Huth }
3491e8a1faeSThomas Huth 
test_tco_ticks_counter(void)3501e8a1faeSThomas Huth static void test_tco_ticks_counter(void)
3511e8a1faeSThomas Huth {
3521e8a1faeSThomas Huth     TestData d;
3531e8a1faeSThomas Huth     uint16_t ticks = TCO_SECS_TO_TICKS(8);
3541e8a1faeSThomas Huth     uint16_t rld;
3551e8a1faeSThomas Huth 
3561e8a1faeSThomas Huth     d.args = NULL;
3571e8a1faeSThomas Huth     d.noreboot = true;
3581e8a1faeSThomas Huth     test_init(&d);
3591e8a1faeSThomas Huth 
3601e8a1faeSThomas Huth     stop_tco(&d);
3611e8a1faeSThomas Huth     clear_tco_status(&d);
3621e8a1faeSThomas Huth     reset_on_second_timeout(&d, false);
3631e8a1faeSThomas Huth     set_tco_timeout(&d, ticks);
3641e8a1faeSThomas Huth     load_tco(&d);
3651e8a1faeSThomas Huth     start_tco(&d);
3661e8a1faeSThomas Huth 
3671e8a1faeSThomas Huth     do {
3681e8a1faeSThomas Huth         rld = qpci_io_readw(d.dev, d.tco_io_bar, TCO_RLD) & TCO_RLD_MASK;
3691e8a1faeSThomas Huth         g_assert_cmpint(rld, ==, ticks);
3701e8a1faeSThomas Huth         qtest_clock_step(d.qts, TCO_TICK_NSEC);
3711e8a1faeSThomas Huth         ticks--;
3721e8a1faeSThomas Huth     } while (!(qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS) & TCO_TIMEOUT));
3731e8a1faeSThomas Huth 
3741e8a1faeSThomas Huth     stop_tco(&d);
3751e8a1faeSThomas Huth     test_end(&d);
3761e8a1faeSThomas Huth }
3771e8a1faeSThomas Huth 
test_tco1_control_bits(void)3781e8a1faeSThomas Huth static void test_tco1_control_bits(void)
3791e8a1faeSThomas Huth {
3801e8a1faeSThomas Huth     TestData d;
3811e8a1faeSThomas Huth     uint16_t val;
3821e8a1faeSThomas Huth 
3831e8a1faeSThomas Huth     d.args = NULL;
3841e8a1faeSThomas Huth     d.noreboot = true;
3851e8a1faeSThomas Huth     test_init(&d);
3861e8a1faeSThomas Huth 
3871e8a1faeSThomas Huth     val = TCO_LOCK;
3881e8a1faeSThomas Huth     qpci_io_writew(d.dev, d.tco_io_bar, TCO1_CNT, val);
3891e8a1faeSThomas Huth     val &= ~TCO_LOCK;
3901e8a1faeSThomas Huth     qpci_io_writew(d.dev, d.tco_io_bar, TCO1_CNT, val);
3911e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_bar, TCO1_CNT), ==,
3921e8a1faeSThomas Huth                     TCO_LOCK);
3931e8a1faeSThomas Huth     test_end(&d);
3941e8a1faeSThomas Huth }
3951e8a1faeSThomas Huth 
test_tco1_status_bits(void)3961e8a1faeSThomas Huth static void test_tco1_status_bits(void)
3971e8a1faeSThomas Huth {
3981e8a1faeSThomas Huth     TestData d;
3991e8a1faeSThomas Huth     uint16_t ticks = 8;
4001e8a1faeSThomas Huth     uint16_t val;
4011e8a1faeSThomas Huth     int ret;
4021e8a1faeSThomas Huth 
4031e8a1faeSThomas Huth     d.args = NULL;
4041e8a1faeSThomas Huth     d.noreboot = true;
4051e8a1faeSThomas Huth     test_init(&d);
4061e8a1faeSThomas Huth 
4071e8a1faeSThomas Huth     stop_tco(&d);
4081e8a1faeSThomas Huth     clear_tco_status(&d);
4091e8a1faeSThomas Huth     reset_on_second_timeout(&d, false);
4101e8a1faeSThomas Huth     set_tco_timeout(&d, ticks);
4111e8a1faeSThomas Huth     load_tco(&d);
4121e8a1faeSThomas Huth     start_tco(&d);
4131e8a1faeSThomas Huth     qtest_clock_step(d.qts, ticks * TCO_TICK_NSEC);
4141e8a1faeSThomas Huth 
4151e8a1faeSThomas Huth     qpci_io_writeb(d.dev, d.tco_io_bar, TCO_DAT_IN, 0);
4161e8a1faeSThomas Huth     qpci_io_writeb(d.dev, d.tco_io_bar, TCO_DAT_OUT, 0);
4171e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS);
4181e8a1faeSThomas Huth     ret = val & (TCO_TIMEOUT | SW_TCO_SMI | TCO_INT_STS) ? 1 : 0;
4191e8a1faeSThomas Huth     g_assert(ret == 1);
4201e8a1faeSThomas Huth     qpci_io_writew(d.dev, d.tco_io_bar, TCO1_STS, val);
4211e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_bar, TCO1_STS), ==, 0);
4221e8a1faeSThomas Huth     test_end(&d);
4231e8a1faeSThomas Huth }
4241e8a1faeSThomas Huth 
test_tco2_status_bits(void)4251e8a1faeSThomas Huth static void test_tco2_status_bits(void)
4261e8a1faeSThomas Huth {
4271e8a1faeSThomas Huth     TestData d;
4281e8a1faeSThomas Huth     uint16_t ticks = 8;
4291e8a1faeSThomas Huth     uint16_t val;
4301e8a1faeSThomas Huth     int ret;
4311e8a1faeSThomas Huth 
4321e8a1faeSThomas Huth     d.args = NULL;
4331e8a1faeSThomas Huth     d.noreboot = true;
4341e8a1faeSThomas Huth     test_init(&d);
4351e8a1faeSThomas Huth 
4361e8a1faeSThomas Huth     stop_tco(&d);
4371e8a1faeSThomas Huth     clear_tco_status(&d);
4381e8a1faeSThomas Huth     reset_on_second_timeout(&d, true);
4391e8a1faeSThomas Huth     set_tco_timeout(&d, ticks);
4401e8a1faeSThomas Huth     load_tco(&d);
4411e8a1faeSThomas Huth     start_tco(&d);
4421e8a1faeSThomas Huth     qtest_clock_step(d.qts, ticks * TCO_TICK_NSEC * 2);
4431e8a1faeSThomas Huth 
4441e8a1faeSThomas Huth     val = qpci_io_readw(d.dev, d.tco_io_bar, TCO2_STS);
4451e8a1faeSThomas Huth     ret = val & (TCO_SECOND_TO_STS | TCO_BOOT_STS) ? 1 : 0;
4461e8a1faeSThomas Huth     g_assert(ret == 1);
4471e8a1faeSThomas Huth     qpci_io_writew(d.dev, d.tco_io_bar, TCO2_STS, val);
4481e8a1faeSThomas Huth     g_assert_cmpint(qpci_io_readw(d.dev, d.tco_io_bar, TCO2_STS), ==, 0);
4491e8a1faeSThomas Huth     test_end(&d);
4501e8a1faeSThomas Huth }
4511e8a1faeSThomas Huth 
main(int argc,char ** argv)4521e8a1faeSThomas Huth int main(int argc, char **argv)
4531e8a1faeSThomas Huth {
4541e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
4551e8a1faeSThomas Huth 
4561e8a1faeSThomas Huth     qtest_add_func("tco/defaults", test_tco_defaults);
4571e8a1faeSThomas Huth     qtest_add_func("tco/timeout/no_action", test_tco_timeout);
4581e8a1faeSThomas Huth     qtest_add_func("tco/timeout/no_action/max", test_tco_max_timeout);
4591e8a1faeSThomas Huth     qtest_add_func("tco/second_timeout/pause", test_tco_second_timeout_pause);
4601e8a1faeSThomas Huth     qtest_add_func("tco/second_timeout/reset", test_tco_second_timeout_reset);
4611e8a1faeSThomas Huth     qtest_add_func("tco/second_timeout/shutdown",
4621e8a1faeSThomas Huth                    test_tco_second_timeout_shutdown);
4631e8a1faeSThomas Huth     qtest_add_func("tco/second_timeout/none", test_tco_second_timeout_none);
4641e8a1faeSThomas Huth     qtest_add_func("tco/counter", test_tco_ticks_counter);
4651e8a1faeSThomas Huth     qtest_add_func("tco/tco1_control/bits", test_tco1_control_bits);
4661e8a1faeSThomas Huth     qtest_add_func("tco/tco1_status/bits", test_tco1_status_bits);
4671e8a1faeSThomas Huth     qtest_add_func("tco/tco2_status/bits", test_tco2_status_bits);
4681e8a1faeSThomas Huth     return g_test_run();
4691e8a1faeSThomas Huth }
470