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.
15 #include "libqos/pci.h"
16 #include "libqos/pci-pc.h"
17 #include "hw/pci/pci_regs.h"
18 #include "hw/i386/ich9.h"
19 #include "hw/acpi/ich9.h"
20 #include "hw/acpi/tco.h"
22 #define RCBA_BASE_ADDR 0xfed1c000
23 #define PM_IO_BASE_ADDR 0xb000
26 TCO_RLD_DEFAULT
= 0x0000,
27 TCO_DAT_IN_DEFAULT
= 0x00,
28 TCO_DAT_OUT_DEFAULT
= 0x00,
29 TCO1_STS_DEFAULT
= 0x0000,
30 TCO2_STS_DEFAULT
= 0x0000,
31 TCO1_CNT_DEFAULT
= 0x0000,
32 TCO2_CNT_DEFAULT
= 0x0008,
33 TCO_MESSAGE1_DEFAULT
= 0x00,
34 TCO_MESSAGE2_DEFAULT
= 0x00,
35 TCO_WDCNT_DEFAULT
= 0x00,
36 TCO_TMR_DEFAULT
= 0x0004,
37 SW_IRQ_GEN_DEFAULT
= 0x03,
40 #define TCO_SECS_TO_TICKS(secs) (((secs) * 10) / 6)
41 #define TCO_TICKS_TO_SECS(ticks) (((ticks) * 6) / 10)
50 static void test_init(TestData
*d
)
56 s
= g_strdup_printf("-machine q35 %s %s",
57 d
->noreboot
? "" : "-global ICH9-LPC.noreboot=false",
58 !d
->args
? "" : d
->args
);
60 qtest_irq_intercept_in(qs
, "ioapic");
64 d
->dev
= qpci_device_find(bus
, QPCI_DEVFN(0x1f, 0x00));
65 g_assert(d
->dev
!= NULL
);
67 qpci_device_enable(d
->dev
);
69 /* set ACPI PM I/O space base address */
70 qpci_config_writel(d
->dev
, ICH9_LPC_PMBASE
, PM_IO_BASE_ADDR
| 0x1);
72 qpci_config_writeb(d
->dev
, ICH9_LPC_ACPI_CTRL
, 0x80);
73 /* set Root Complex BAR */
74 qpci_config_writel(d
->dev
, ICH9_LPC_RCBA
, RCBA_BASE_ADDR
| 0x1);
76 d
->tco_io_base
= (void *)((uintptr_t)PM_IO_BASE_ADDR
+ 0x60);
79 static void stop_tco(const TestData
*d
)
83 val
= qpci_io_readw(d
->dev
, d
->tco_io_base
+ TCO1_CNT
);
85 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO1_CNT
, val
);
88 static void start_tco(const TestData
*d
)
92 val
= qpci_io_readw(d
->dev
, d
->tco_io_base
+ TCO1_CNT
);
94 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO1_CNT
, val
);
97 static void load_tco(const TestData
*d
)
99 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO_RLD
, 4);
102 static void set_tco_timeout(const TestData
*d
, uint16_t ticks
)
104 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO_TMR
, ticks
);
107 static void clear_tco_status(const TestData
*d
)
109 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO1_STS
, 0x0008);
110 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO2_STS
, 0x0002);
111 qpci_io_writew(d
->dev
, d
->tco_io_base
+ TCO2_STS
, 0x0004);
114 static void reset_on_second_timeout(bool enable
)
118 val
= readl(RCBA_BASE_ADDR
+ ICH9_CC_GCS
);
120 val
&= ~ICH9_CC_GCS_NO_REBOOT
;
122 val
|= ICH9_CC_GCS_NO_REBOOT
;
124 writel(RCBA_BASE_ADDR
+ ICH9_CC_GCS
, val
);
127 static void test_tco_defaults(void)
134 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_RLD
), ==,
136 /* TCO_DAT_IN & TCO_DAT_OUT */
137 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_DAT_IN
), ==,
138 (TCO_DAT_OUT_DEFAULT
<< 8) | TCO_DAT_IN_DEFAULT
);
139 /* TCO1_STS & TCO2_STS */
140 g_assert_cmpint(qpci_io_readl(d
.dev
, d
.tco_io_base
+ TCO1_STS
), ==,
141 (TCO2_STS_DEFAULT
<< 16) | TCO1_STS_DEFAULT
);
142 /* TCO1_CNT & TCO2_CNT */
143 g_assert_cmpint(qpci_io_readl(d
.dev
, d
.tco_io_base
+ TCO1_CNT
), ==,
144 (TCO2_CNT_DEFAULT
<< 16) | TCO1_CNT_DEFAULT
);
145 /* TCO_MESSAGE1 & TCO_MESSAGE2 */
146 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_MESSAGE1
), ==,
147 (TCO_MESSAGE2_DEFAULT
<< 8) | TCO_MESSAGE1_DEFAULT
);
148 g_assert_cmpint(qpci_io_readb(d
.dev
, d
.tco_io_base
+ TCO_WDCNT
), ==,
150 g_assert_cmpint(qpci_io_readb(d
.dev
, d
.tco_io_base
+ SW_IRQ_GEN
), ==,
152 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_TMR
), ==,
157 static void test_tco_timeout(void)
160 const uint16_t ticks
= TCO_SECS_TO_TICKS(4);
169 clear_tco_status(&d
);
170 reset_on_second_timeout(false);
171 set_tco_timeout(&d
, ticks
);
174 clock_step(ticks
* TCO_TICK_NSEC
);
176 /* test first timeout */
177 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
178 ret
= val
& TCO_TIMEOUT
? 1 : 0;
181 /* test clearing timeout bit */
183 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO1_STS
, val
);
184 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
185 ret
= val
& TCO_TIMEOUT
? 1 : 0;
188 /* test second timeout */
189 clock_step(ticks
* TCO_TICK_NSEC
);
190 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
191 ret
= val
& TCO_TIMEOUT
? 1 : 0;
193 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO2_STS
);
194 ret
= val
& TCO_SECOND_TO_STS
? 1 : 0;
201 static void test_tco_max_timeout(void)
204 const uint16_t ticks
= 0xffff;
213 clear_tco_status(&d
);
214 reset_on_second_timeout(false);
215 set_tco_timeout(&d
, ticks
);
218 clock_step(((ticks
& TCO_TMR_MASK
) - 1) * TCO_TICK_NSEC
);
220 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_RLD
);
221 g_assert_cmpint(val
& TCO_RLD_MASK
, ==, 1);
222 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
223 ret
= val
& TCO_TIMEOUT
? 1 : 0;
225 clock_step(TCO_TICK_NSEC
);
226 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
227 ret
= val
& TCO_TIMEOUT
? 1 : 0;
234 static QDict
*get_watchdog_action(void)
238 g_assert(!strcmp(qdict_get_str(ev
, "event"), "WATCHDOG"));
240 data
= qdict_get_qdict(ev
, "data");
246 static void test_tco_second_timeout_pause(void)
249 const uint16_t ticks
= TCO_SECS_TO_TICKS(32);
252 td
.args
= "-watchdog-action pause";
257 clear_tco_status(&td
);
258 reset_on_second_timeout(true);
259 set_tco_timeout(&td
, TCO_SECS_TO_TICKS(16));
262 clock_step(ticks
* TCO_TICK_NSEC
* 2);
263 ad
= get_watchdog_action();
264 g_assert(!strcmp(qdict_get_str(ad
, "action"), "pause"));
271 static void test_tco_second_timeout_reset(void)
274 const uint16_t ticks
= TCO_SECS_TO_TICKS(16);
277 td
.args
= "-watchdog-action reset";
282 clear_tco_status(&td
);
283 reset_on_second_timeout(true);
284 set_tco_timeout(&td
, TCO_SECS_TO_TICKS(16));
287 clock_step(ticks
* TCO_TICK_NSEC
* 2);
288 ad
= get_watchdog_action();
289 g_assert(!strcmp(qdict_get_str(ad
, "action"), "reset"));
296 static void test_tco_second_timeout_shutdown(void)
299 const uint16_t ticks
= TCO_SECS_TO_TICKS(128);
302 td
.args
= "-watchdog-action shutdown";
307 clear_tco_status(&td
);
308 reset_on_second_timeout(true);
309 set_tco_timeout(&td
, ticks
);
312 clock_step(ticks
* TCO_TICK_NSEC
* 2);
313 ad
= get_watchdog_action();
314 g_assert(!strcmp(qdict_get_str(ad
, "action"), "shutdown"));
321 static void test_tco_second_timeout_none(void)
324 const uint16_t ticks
= TCO_SECS_TO_TICKS(256);
327 td
.args
= "-watchdog-action none";
332 clear_tco_status(&td
);
333 reset_on_second_timeout(true);
334 set_tco_timeout(&td
, ticks
);
337 clock_step(ticks
* TCO_TICK_NSEC
* 2);
338 ad
= get_watchdog_action();
339 g_assert(!strcmp(qdict_get_str(ad
, "action"), "none"));
346 static void test_tco_ticks_counter(void)
349 uint16_t ticks
= TCO_SECS_TO_TICKS(8);
357 clear_tco_status(&d
);
358 reset_on_second_timeout(false);
359 set_tco_timeout(&d
, ticks
);
364 rld
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO_RLD
) & TCO_RLD_MASK
;
365 g_assert_cmpint(rld
, ==, ticks
);
366 clock_step(TCO_TICK_NSEC
);
368 } while (!(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
) & TCO_TIMEOUT
));
374 static void test_tco1_control_bits(void)
384 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO1_CNT
, val
);
386 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO1_CNT
, val
);
387 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_CNT
), ==,
392 static void test_tco1_status_bits(void)
404 clear_tco_status(&d
);
405 reset_on_second_timeout(false);
406 set_tco_timeout(&d
, ticks
);
409 clock_step(ticks
* TCO_TICK_NSEC
);
411 qpci_io_writeb(d
.dev
, d
.tco_io_base
+ TCO_DAT_IN
, 0);
412 qpci_io_writeb(d
.dev
, d
.tco_io_base
+ TCO_DAT_OUT
, 0);
413 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
);
414 ret
= val
& (TCO_TIMEOUT
| SW_TCO_SMI
| TCO_INT_STS
) ? 1 : 0;
416 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO1_STS
, val
);
417 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO1_STS
), ==, 0);
421 static void test_tco2_status_bits(void)
433 clear_tco_status(&d
);
434 reset_on_second_timeout(true);
435 set_tco_timeout(&d
, ticks
);
438 clock_step(ticks
* TCO_TICK_NSEC
* 2);
440 val
= qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO2_STS
);
441 ret
= val
& (TCO_SECOND_TO_STS
| TCO_BOOT_STS
) ? 1 : 0;
443 qpci_io_writew(d
.dev
, d
.tco_io_base
+ TCO2_STS
, val
);
444 g_assert_cmpint(qpci_io_readw(d
.dev
, d
.tco_io_base
+ TCO2_STS
), ==, 0);
448 int main(int argc
, char **argv
)
450 g_test_init(&argc
, &argv
, NULL
);
452 qtest_add_func("tco/defaults", test_tco_defaults
);
453 qtest_add_func("tco/timeout/no_action", test_tco_timeout
);
454 qtest_add_func("tco/timeout/no_action/max", test_tco_max_timeout
);
455 qtest_add_func("tco/second_timeout/pause", test_tco_second_timeout_pause
);
456 qtest_add_func("tco/second_timeout/reset", test_tco_second_timeout_reset
);
457 qtest_add_func("tco/second_timeout/shutdown",
458 test_tco_second_timeout_shutdown
);
459 qtest_add_func("tco/second_timeout/none", test_tco_second_timeout_none
);
460 qtest_add_func("tco/counter", test_tco_ticks_counter
);
461 qtest_add_func("tco/tco1_control/bits", test_tco1_control_bits
);
462 qtest_add_func("tco/tco1_status/bits", test_tco1_status_bits
);
463 qtest_add_func("tco/tco2_status/bits", test_tco2_status_bits
);