2 * QEMU ICH9 TCO emulation tests
4 * Copyright (c) 2015 Paulo Alcantara <pcacjr@zytor.com>
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"
12 #include "libqos/pci.h"
13 #include "libqos/pci-pc.h"
14 #include "hw/pci/pci_regs.h"
15 #include "hw/i386/ich9.h"
16 #include "hw/acpi/ich9.h"
17 #include "hw/acpi/tco.h"
19 #define RCBA_BASE_ADDR 0xfed1c000
20 #define PM_IO_BASE_ADDR 0xb000
23 TCO_RLD_DEFAULT
= 0x0000,
24 TCO_DAT_IN_DEFAULT
= 0x00,
25 TCO_DAT_OUT_DEFAULT
= 0x00,
26 TCO1_STS_DEFAULT
= 0x0000,
27 TCO2_STS_DEFAULT
= 0x0000,
28 TCO1_CNT_DEFAULT
= 0x0000,
29 TCO2_CNT_DEFAULT
= 0x0008,
30 TCO_MESSAGE1_DEFAULT
= 0x00,
31 TCO_MESSAGE2_DEFAULT
= 0x00,
32 TCO_WDCNT_DEFAULT
= 0x00,
33 TCO_TMR_DEFAULT
= 0x0004,
34 SW_IRQ_GEN_DEFAULT
= 0x03,
37 #define TCO_SECS_TO_TICKS(secs) (((secs) * 10) / 6)
38 #define TCO_TICKS_TO_SECS(ticks) (((ticks) * 6) / 10)
47 static void test_init(TestData
*d
)
53 s
= g_strdup_printf("-machine q35 %s %s",
54 d
->noreboot
? "" : "-global ICH9-LPC.noreboot=false",
55 !d
->args
? "" : d
->args
);
57 qtest_irq_intercept_in(qs
, "ioapic");
61 d
->dev
= qpci_device_find(bus
, QPCI_DEVFN(0x1f, 0x00));
62 g_assert(d
->dev
!= NULL
);
64 qpci_device_enable(d
->dev
);
66 /* set ACPI PM I/O space base address */
67 qpci_config_writel(d
->dev
, ICH9_LPC_PMBASE
, PM_IO_BASE_ADDR
| 0x1);
69 qpci_config_writeb(d
->dev
, ICH9_LPC_ACPI_CTRL
, 0x80);
70 /* set Root Complex BAR */
71 qpci_config_writel(d
->dev
, ICH9_LPC_RCBA
, RCBA_BASE_ADDR
| 0x1);
73 d
->tco_io_base
= (void *)((uintptr_t)PM_IO_BASE_ADDR
+ 0x60);
76 static void stop_tco(const TestData
*d
)
80 val
= qpci_io_readw(d
->dev
, d
->tco_io_base
+ TCO1_CNT
);
82 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO1_CNT
, val
);
85 static void start_tco(const TestData
*d
)
89 val
= qpci_io_readw(d
->dev
, d
->tco_io_base
+ TCO1_CNT
);
91 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO1_CNT
, val
);
94 static void load_tco(const TestData
*d
)
96 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO_RLD
, 4);
99 static void set_tco_timeout(const TestData
*d
, uint16_t ticks
)
101 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO_TMR
, ticks
);
104 static void clear_tco_status(const TestData
*d
)
106 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO1_STS
, 0x0008);
107 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO2_STS
, 0x0002);
108 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO2_STS
, 0x0004);
111 static void reset_on_second_timeout(bool enable
)
115 val
= readl(RCBA_BASE_ADDR
+ ICH9_CC_GCS
);
117 val
&= ~ICH9_CC_GCS_NO_REBOOT
;
119 val
|= ICH9_CC_GCS_NO_REBOOT
;
121 writel(RCBA_BASE_ADDR
+ ICH9_CC_GCS
, val
);
124 static void test_tco_defaults(void)
131 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_RLD
), ==,
133 /* TCO_DAT_IN & TCO_DAT_OUT */
134 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_DAT_IN
), ==,
135 (TCO_DAT_OUT_DEFAULT
<< 8) | TCO_DAT_IN_DEFAULT
);
136 /* TCO1_STS & TCO2_STS */
137 g_assert_cmpint(qpci_io_readl(d
.dev
, d
.tco_io_base
+ TCO1_STS
), ==,
138 (TCO2_STS_DEFAULT
<< 16) | TCO1_STS_DEFAULT
);
139 /* TCO1_CNT & TCO2_CNT */
140 g_assert_cmpint(qpci_io_readl(d
.dev
, d
.tco_io_base
+ TCO1_CNT
), ==,
141 (TCO2_CNT_DEFAULT
<< 16) | TCO1_CNT_DEFAULT
);
142 /* TCO_MESSAGE1 & TCO_MESSAGE2 */
143 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_MESSAGE1
), ==,
144 (TCO_MESSAGE2_DEFAULT
<< 8) | TCO_MESSAGE1_DEFAULT
);
145 g_assert_cmpint(qpci_io_readb(d
.dev
, d
.tco_io_base
+ TCO_WDCNT
), ==,
147 g_assert_cmpint(qpci_io_readb(d
.dev
, d
.tco_io_base
+ SW_IRQ_GEN
), ==,
149 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_TMR
), ==,
154 static void test_tco_timeout(void)
157 const uint16_t ticks
= TCO_SECS_TO_TICKS(4);
166 clear_tco_status(&d
);
167 reset_on_second_timeout(false);
168 set_tco_timeout(&d
, ticks
);
171 clock_step(ticks
* TCO_TICK_NSEC
);
173 /* test first timeout */
174 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
175 ret
= val
& TCO_TIMEOUT
? 1 : 0;
178 /* test clearing timeout bit */
180 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO1_STS
, val
);
181 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
182 ret
= val
& TCO_TIMEOUT
? 1 : 0;
185 /* test second timeout */
186 clock_step(ticks
* TCO_TICK_NSEC
);
187 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
188 ret
= val
& TCO_TIMEOUT
? 1 : 0;
190 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO2_STS
);
191 ret
= val
& TCO_SECOND_TO_STS
? 1 : 0;
198 static void test_tco_max_timeout(void)
201 const uint16_t ticks
= 0xffff;
210 clear_tco_status(&d
);
211 reset_on_second_timeout(false);
212 set_tco_timeout(&d
, ticks
);
215 clock_step(((ticks
& TCO_TMR_MASK
) - 1) * TCO_TICK_NSEC
);
217 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_RLD
);
218 g_assert_cmpint(val
& TCO_RLD_MASK
, ==, 1);
219 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
220 ret
= val
& TCO_TIMEOUT
? 1 : 0;
222 clock_step(TCO_TICK_NSEC
);
223 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
224 ret
= val
& TCO_TIMEOUT
? 1 : 0;
231 static QDict
*get_watchdog_action(void)
235 g_assert(!strcmp(qdict_get_str(ev
, "event"), "WATCHDOG"));
237 data
= qdict_get_qdict(ev
, "data");
243 static void test_tco_second_timeout_pause(void)
246 const uint16_t ticks
= TCO_SECS_TO_TICKS(32);
249 td
.args
= "-watchdog-action pause";
254 clear_tco_status(&td
);
255 reset_on_second_timeout(true);
256 set_tco_timeout(&td
, TCO_SECS_TO_TICKS(16));
259 clock_step(ticks
* TCO_TICK_NSEC
* 2);
260 ad
= get_watchdog_action();
261 g_assert(!strcmp(qdict_get_str(ad
, "action"), "pause"));
268 static void test_tco_second_timeout_reset(void)
271 const uint16_t ticks
= TCO_SECS_TO_TICKS(16);
274 td
.args
= "-watchdog-action reset";
279 clear_tco_status(&td
);
280 reset_on_second_timeout(true);
281 set_tco_timeout(&td
, TCO_SECS_TO_TICKS(16));
284 clock_step(ticks
* TCO_TICK_NSEC
* 2);
285 ad
= get_watchdog_action();
286 g_assert(!strcmp(qdict_get_str(ad
, "action"), "reset"));
293 static void test_tco_second_timeout_shutdown(void)
296 const uint16_t ticks
= TCO_SECS_TO_TICKS(128);
299 td
.args
= "-watchdog-action shutdown";
304 clear_tco_status(&td
);
305 reset_on_second_timeout(true);
306 set_tco_timeout(&td
, ticks
);
309 clock_step(ticks
* TCO_TICK_NSEC
* 2);
310 ad
= get_watchdog_action();
311 g_assert(!strcmp(qdict_get_str(ad
, "action"), "shutdown"));
318 static void test_tco_second_timeout_none(void)
321 const uint16_t ticks
= TCO_SECS_TO_TICKS(256);
324 td
.args
= "-watchdog-action none";
329 clear_tco_status(&td
);
330 reset_on_second_timeout(true);
331 set_tco_timeout(&td
, ticks
);
334 clock_step(ticks
* TCO_TICK_NSEC
* 2);
335 ad
= get_watchdog_action();
336 g_assert(!strcmp(qdict_get_str(ad
, "action"), "none"));
343 static void test_tco_ticks_counter(void)
346 uint16_t ticks
= TCO_SECS_TO_TICKS(8);
354 clear_tco_status(&d
);
355 reset_on_second_timeout(false);
356 set_tco_timeout(&d
, ticks
);
361 rld
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_RLD
) & TCO_RLD_MASK
;
362 g_assert_cmpint(rld
, ==, ticks
);
363 clock_step(TCO_TICK_NSEC
);
365 } while (!(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
) & TCO_TIMEOUT
));
371 static void test_tco1_control_bits(void)
381 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO1_CNT
, val
);
383 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO1_CNT
, val
);
384 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_CNT
), ==,
389 static void test_tco1_status_bits(void)
401 clear_tco_status(&d
);
402 reset_on_second_timeout(false);
403 set_tco_timeout(&d
, ticks
);
406 clock_step(ticks
* TCO_TICK_NSEC
);
408 qpci_io_writeb(d
.dev
, d
.tco_io_base
+ TCO_DAT_IN
, 0);
409 qpci_io_writeb(d
.dev
, d
.tco_io_base
+ TCO_DAT_OUT
, 0);
410 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
411 ret
= val
& (TCO_TIMEOUT
| SW_TCO_SMI
| TCO_INT_STS
) ? 1 : 0;
413 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO1_STS
, val
);
414 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
), ==, 0);
418 static void test_tco2_status_bits(void)
430 clear_tco_status(&d
);
431 reset_on_second_timeout(true);
432 set_tco_timeout(&d
, ticks
);
435 clock_step(ticks
* TCO_TICK_NSEC
* 2);
437 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO2_STS
);
438 ret
= val
& (TCO_SECOND_TO_STS
| TCO_BOOT_STS
) ? 1 : 0;
440 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO2_STS
, val
);
441 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO2_STS
), ==, 0);
445 int main(int argc
, char **argv
)
447 g_test_init(&argc
, &argv
, NULL
);
449 qtest_add_func("tco/defaults", test_tco_defaults
);
450 qtest_add_func("tco/timeout/no_action", test_tco_timeout
);
451 qtest_add_func("tco/timeout/no_action/max", test_tco_max_timeout
);
452 qtest_add_func("tco/second_timeout/pause", test_tco_second_timeout_pause
);
453 qtest_add_func("tco/second_timeout/reset", test_tco_second_timeout_reset
);
454 qtest_add_func("tco/second_timeout/shutdown",
455 test_tco_second_timeout_shutdown
);
456 qtest_add_func("tco/second_timeout/none", test_tco_second_timeout_none
);
457 qtest_add_func("tco/counter", test_tco_ticks_counter
);
458 qtest_add_func("tco/tco1_control/bits", test_tco1_control_bits
);
459 qtest_add_func("tco/tco1_status/bits", test_tco1_status_bits
);
460 qtest_add_func("tco/tco2_status/bits", test_tco2_status_bits
);