2 * qtest I440FX test case
4 * Copyright IBM, Corp. 2012-2013
5 * Copyright Red Hat, Inc. 2013
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Laszlo Ersek <lersek@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
15 #include "libqos/pci.h"
16 #include "libqos/pci-pc.h"
19 #include "hw/pci/pci_regs.h"
31 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
33 typedef struct TestData
38 typedef struct FirmwareTestFixture
{
39 /* decides whether we're testing -bios or -pflash */
41 } FirmwareTestFixture
;
43 static QPCIBus
*test_start_get_bus(const TestData
*s
)
47 cmdline
= g_strdup_printf("-smp %d", s
->num_cpus
);
50 return qpci_init_pc();
53 static void test_i440fx_defaults(gconstpointer opaque
)
55 const TestData
*s
= opaque
;
60 bus
= test_start_get_bus(s
);
61 dev
= qpci_device_find(bus
, QPCI_DEVFN(0, 0));
62 g_assert(dev
!= NULL
);
65 g_assert_cmpint(qpci_config_readw(dev
, PCI_VENDOR_ID
), ==, 0x8086);
67 g_assert_cmpint(qpci_config_readw(dev
, PCI_DEVICE_ID
), ==, 0x1237);
70 g_assert_cmpint(qpci_config_readw(dev
, PCI_COMMAND
), ==, 0x0006);
72 g_assert_cmpint(qpci_config_readw(dev
, PCI_STATUS
), ==, 0x0280);
75 g_assert_cmpint(qpci_config_readb(dev
, PCI_CLASS_PROG
), ==, 0x00);
76 g_assert_cmpint(qpci_config_readw(dev
, PCI_CLASS_DEVICE
), ==, 0x0600);
78 g_assert_cmpint(qpci_config_readb(dev
, PCI_LATENCY_TIMER
), ==, 0x00);
80 g_assert_cmpint(qpci_config_readb(dev
, PCI_HEADER_TYPE
), ==, 0x00);
82 g_assert_cmpint(qpci_config_readb(dev
, PCI_BIST
), ==, 0x00);
85 value
= qpci_config_readw(dev
, 0x50); /* PMCCFG */
86 if (s
->num_cpus
== 1) { /* WPE */
87 g_assert(!(value
& (1 << 15)));
89 g_assert((value
& (1 << 15)));
92 g_assert(!(value
& (1 << 6))); /* EPTE */
95 g_assert_cmpint(qpci_config_readb(dev
, 0x52), ==, 0x00); /* DETURBO */
98 g_assert_cmpint(qpci_config_readb(dev
, 0x53), ==, 0x80); /* DBC */
101 g_assert_cmpint(qpci_config_readb(dev
, 0x54), ==, 0x00); /* AXC */
103 g_assert_cmpint(qpci_config_readw(dev
, 0x55), ==, 0x0000); /* DRT */
106 g_assert_cmpint(qpci_config_readb(dev
, 0x57), ==, 0x01); /* DRAMC */
108 g_assert_cmpint(qpci_config_readb(dev
, 0x58), ==, 0x10); /* DRAMT */
111 g_assert_cmpint(qpci_config_readb(dev
, 0x59), ==, 0x00); /* PAM0 */
112 g_assert_cmpint(qpci_config_readb(dev
, 0x5A), ==, 0x00); /* PAM1 */
113 g_assert_cmpint(qpci_config_readb(dev
, 0x5B), ==, 0x00); /* PAM2 */
114 g_assert_cmpint(qpci_config_readb(dev
, 0x5C), ==, 0x00); /* PAM3 */
115 g_assert_cmpint(qpci_config_readb(dev
, 0x5D), ==, 0x00); /* PAM4 */
116 g_assert_cmpint(qpci_config_readb(dev
, 0x5E), ==, 0x00); /* PAM5 */
117 g_assert_cmpint(qpci_config_readb(dev
, 0x5F), ==, 0x00); /* PAM6 */
120 g_assert_cmpint(qpci_config_readb(dev
, 0x60), ==, 0x01); /* DRB0 */
121 g_assert_cmpint(qpci_config_readb(dev
, 0x61), ==, 0x01); /* DRB1 */
122 g_assert_cmpint(qpci_config_readb(dev
, 0x62), ==, 0x01); /* DRB2 */
123 g_assert_cmpint(qpci_config_readb(dev
, 0x63), ==, 0x01); /* DRB3 */
124 g_assert_cmpint(qpci_config_readb(dev
, 0x64), ==, 0x01); /* DRB4 */
125 g_assert_cmpint(qpci_config_readb(dev
, 0x65), ==, 0x01); /* DRB5 */
126 g_assert_cmpint(qpci_config_readb(dev
, 0x66), ==, 0x01); /* DRB6 */
127 g_assert_cmpint(qpci_config_readb(dev
, 0x67), ==, 0x01); /* DRB7 */
130 g_assert_cmpint(qpci_config_readb(dev
, 0x68), ==, 0x00); /* FDHC */
132 g_assert_cmpint(qpci_config_readb(dev
, 0x70), ==, 0x00); /* MTT */
135 g_assert_cmpint(qpci_config_readb(dev
, 0x71), ==, 0x10); /* CLT */
138 g_assert_cmpint(qpci_config_readb(dev
, 0x72), ==, 0x02); /* SMRAM */
140 g_assert_cmpint(qpci_config_readb(dev
, 0x90), ==, 0x00); /* ERRCMD */
142 g_assert_cmpint(qpci_config_readb(dev
, 0x91), ==, 0x00); /* ERRSTS */
144 g_assert_cmpint(qpci_config_readb(dev
, 0x93), ==, 0x00); /* TRC */
152 static void pam_set(QPCIDevice
*dev
, int index
, int flags
)
154 int regno
= 0x59 + (index
/ 2);
157 reg
= qpci_config_readb(dev
, regno
);
159 reg
= (reg
& 0x0F) | (flags
<< 4);
161 reg
= (reg
& 0xF0) | flags
;
163 qpci_config_writeb(dev
, regno
, reg
);
166 static gboolean
verify_area(uint32_t start
, uint32_t end
, uint8_t value
)
168 uint32_t size
= end
- start
+ 1;
173 data
= g_malloc0(size
);
174 memread(start
, data
, size
);
176 g_test_message("verify_area: data[0] = 0x%x", data
[0]);
178 for (i
= 0; i
< size
; i
++) {
179 if (data
[i
] != value
) {
190 static void write_area(uint32_t start
, uint32_t end
, uint8_t value
)
192 uint32_t size
= end
- start
+ 1;
195 data
= g_malloc0(size
);
196 memset(data
, value
, size
);
197 memwrite(start
, data
, size
);
202 static void test_i440fx_pam(gconstpointer opaque
)
204 const TestData
*s
= opaque
;
212 { 0, 0 }, /* Reserved */
213 { 0xF0000, 0xFFFFF }, /* BIOS Area */
214 { 0xC0000, 0xC3FFF }, /* Option ROM */
215 { 0xC4000, 0xC7FFF }, /* Option ROM */
216 { 0xC8000, 0xCBFFF }, /* Option ROM */
217 { 0xCC000, 0xCFFFF }, /* Option ROM */
218 { 0xD0000, 0xD3FFF }, /* Option ROM */
219 { 0xD4000, 0xD7FFF }, /* Option ROM */
220 { 0xD8000, 0xDBFFF }, /* Option ROM */
221 { 0xDC000, 0xDFFFF }, /* Option ROM */
222 { 0xE0000, 0xE3FFF }, /* BIOS Extension */
223 { 0xE4000, 0xE7FFF }, /* BIOS Extension */
224 { 0xE8000, 0xEBFFF }, /* BIOS Extension */
225 { 0xEC000, 0xEFFFF }, /* BIOS Extension */
228 bus
= test_start_get_bus(s
);
229 dev
= qpci_device_find(bus
, QPCI_DEVFN(0, 0));
230 g_assert(dev
!= NULL
);
232 for (i
= 0; i
< ARRAY_SIZE(pam_area
); i
++) {
233 if (pam_area
[i
].start
== pam_area
[i
].end
) {
237 g_test_message("Checking area 0x%05x..0x%05x",
238 pam_area
[i
].start
, pam_area
[i
].end
);
239 /* Switch to RE for the area */
240 pam_set(dev
, i
, PAM_RE
);
241 /* Verify the RAM is all zeros */
242 g_assert(verify_area(pam_area
[i
].start
, pam_area
[i
].end
, 0));
244 /* Switch to WE for the area */
245 pam_set(dev
, i
, PAM_RE
| PAM_WE
);
246 /* Write out a non-zero mask to the full area */
247 write_area(pam_area
[i
].start
, pam_area
[i
].end
, 0x42);
250 /* QEMU only supports a limited form of PAM */
252 /* Switch to !RE for the area */
253 pam_set(dev
, i
, PAM_WE
);
254 /* Verify the area is not our mask */
255 g_assert(!verify_area(pam_area
[i
].start
, pam_area
[i
].end
, 0x42));
258 /* Verify the area is our new mask */
259 g_assert(verify_area(pam_area
[i
].start
, pam_area
[i
].end
, 0x42));
261 /* Write out a new mask */
262 write_area(pam_area
[i
].start
, pam_area
[i
].end
, 0x82);
265 /* QEMU only supports a limited form of PAM */
267 /* Verify the area is not our mask */
268 g_assert(!verify_area(pam_area
[i
].start
, pam_area
[i
].end
, 0x82));
270 /* Switch to RE for the area */
271 pam_set(dev
, i
, PAM_RE
| PAM_WE
);
273 /* Verify the area is our new mask */
274 g_assert(verify_area(pam_area
[i
].start
, pam_area
[i
].end
, 0x82));
279 /* Verify the area is not our new mask */
280 g_assert(!verify_area(pam_area
[i
].start
, pam_area
[i
].end
, 0x82));
285 #define BLOB_SIZE ((size_t)65536)
286 #define ISA_BIOS_MAXSZ ((size_t)(128 * 1024))
288 /* Create a blob file, and return its absolute pathname as a dynamically
290 * The file is closed before the function returns.
291 * In case of error, NULL is returned. The function prints the error message.
293 static char *create_blob_file(void)
297 GError
*error
= NULL
;
300 fd
= g_file_open_tmp("blob_XXXXXX", &pathname
, &error
);
302 fprintf(stderr
, "unable to create blob file: %s\n", error
->message
);
305 if (ftruncate(fd
, BLOB_SIZE
) == -1) {
306 fprintf(stderr
, "ftruncate(\"%s\", %zu): %s\n", pathname
,
307 BLOB_SIZE
, strerror(errno
));
311 buf
= mmap(NULL
, BLOB_SIZE
, PROT_WRITE
, MAP_SHARED
, fd
, 0);
312 if (buf
== MAP_FAILED
) {
313 fprintf(stderr
, "mmap(\"%s\", %zu): %s\n", pathname
, BLOB_SIZE
,
318 for (i
= 0; i
< BLOB_SIZE
; ++i
) {
319 ((uint8_t *)buf
)[i
] = i
;
321 munmap(buf
, BLOB_SIZE
);
332 return ret
== -1 ? NULL
: pathname
;
335 static void test_i440fx_firmware(FirmwareTestFixture
*fixture
,
336 gconstpointer user_data
)
338 char *fw_pathname
, *cmdline
;
340 size_t i
, isa_bios_size
;
342 fw_pathname
= create_blob_file();
343 g_assert(fw_pathname
!= NULL
);
345 /* Better hope the user didn't put metacharacters in TMPDIR and co. */
346 cmdline
= g_strdup_printf("-S %s %s",
347 fixture
->is_bios
? "-bios" : "-pflash",
349 g_test_message("qemu cmdline: %s", cmdline
);
350 qtest_start(cmdline
);
353 /* Qemu has loaded the firmware (because qtest_start() only returns after
354 * the QMP handshake completes). We must unlink the firmware blob right
355 * here, because any assertion firing below would leak it in the
356 * filesystem. This is also the reason why we recreate the blob every time
357 * this function is invoked.
363 buf
= g_malloc0(BLOB_SIZE
);
364 memread(0x100000000ULL
- BLOB_SIZE
, buf
, BLOB_SIZE
);
365 for (i
= 0; i
< BLOB_SIZE
; ++i
) {
366 g_assert_cmphex(buf
[i
], ==, (uint8_t)i
);
369 /* check in ISA space too */
370 memset(buf
, 0, BLOB_SIZE
);
371 isa_bios_size
= ISA_BIOS_MAXSZ
< BLOB_SIZE
? ISA_BIOS_MAXSZ
: BLOB_SIZE
;
372 memread(0x100000 - isa_bios_size
, buf
, isa_bios_size
);
373 for (i
= 0; i
< isa_bios_size
; ++i
) {
374 g_assert_cmphex(buf
[i
], ==,
375 (uint8_t)((BLOB_SIZE
- isa_bios_size
) + i
));
382 static void add_firmware_test(const char *testpath
,
383 void (*setup_fixture
)(FirmwareTestFixture
*f
,
384 gconstpointer test_data
))
386 g_test_add(testpath
, FirmwareTestFixture
, NULL
, setup_fixture
,
387 test_i440fx_firmware
, NULL
);
390 static void request_bios(FirmwareTestFixture
*fixture
,
391 gconstpointer user_data
)
393 fixture
->is_bios
= true;
396 static void request_pflash(FirmwareTestFixture
*fixture
,
397 gconstpointer user_data
)
399 fixture
->is_bios
= false;
402 int main(int argc
, char **argv
)
407 g_test_init(&argc
, &argv
, NULL
);
411 g_test_add_data_func("/i440fx/defaults", &data
, test_i440fx_defaults
);
412 g_test_add_data_func("/i440fx/pam", &data
, test_i440fx_pam
);
413 add_firmware_test("/i440fx/firmware/bios", request_bios
);
414 add_firmware_test("/i440fx/firmware/pflash", request_pflash
);