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 "qemu/osdep.h"
20 #include "libqos/pci.h"
21 #include "libqos/pci-pc.h"
22 #include "hw/pci/pci_regs.h"
26 typedef struct TestData
31 typedef struct FirmwareTestFixture {
32 /* decides whether we're testing -bios or -pflash */
34 } FirmwareTestFixture;
36 static QPCIBus *test_start_get_bus(const TestData *s)
40 cmdline = g_strdup_printf("-smp %d", s->num_cpus);
43 return qpci_init_pc();
46 static void test_i440fx_defaults(gconstpointer opaque)
48 const TestData *s = opaque;
53 bus = test_start_get_bus(s);
54 dev = qpci_device_find(bus, QPCI_DEVFN(0, 0));
55 g_assert(dev != NULL);
58 g_assert_cmpint(qpci_config_readw(dev, PCI_VENDOR_ID), ==, 0x8086);
60 g_assert_cmpint(qpci_config_readw(dev, PCI_DEVICE_ID), ==, 0x1237);
63 g_assert_cmpint(qpci_config_readw(dev, PCI_COMMAND), ==, 0x0006);
65 g_assert_cmpint(qpci_config_readw(dev, PCI_STATUS), ==, 0x0280);
68 g_assert_cmpint(qpci_config_readb(dev, PCI_CLASS_PROG), ==, 0x00);
69 g_assert_cmpint(qpci_config_readw(dev, PCI_CLASS_DEVICE), ==, 0x0600);
71 g_assert_cmpint(qpci_config_readb(dev, PCI_LATENCY_TIMER), ==, 0x00);
73 g_assert_cmpint(qpci_config_readb(dev, PCI_HEADER_TYPE), ==, 0x00);
75 g_assert_cmpint(qpci_config_readb(dev, PCI_BIST), ==, 0x00);
78 value = qpci_config_readw(dev, 0x50); /* PMCCFG */
79 if (s->num_cpus == 1) { /* WPE */
80 g_assert(!(value & (1 << 15)));
82 g_assert((value & (1 << 15)));
85 g_assert(!(value & (1 << 6))); /* EPTE */
88 g_assert_cmpint(qpci_config_readb(dev, 0x52), ==, 0x00); /* DETURBO */
91 g_assert_cmpint(qpci_config_readb(dev, 0x53), ==, 0x80); /* DBC */
94 g_assert_cmpint(qpci_config_readb(dev, 0x54), ==, 0x00); /* AXC */
96 g_assert_cmpint(qpci_config_readw(dev, 0x55), ==, 0x0000); /* DRT */
99 g_assert_cmpint(qpci_config_readb(dev, 0x57), ==, 0x01); /* DRAMC */
101 g_assert_cmpint(qpci_config_readb(dev, 0x58), ==, 0x10); /* DRAMT */
104 g_assert_cmpint(qpci_config_readb(dev, 0x59), ==, 0x00); /* PAM0 */
105 g_assert_cmpint(qpci_config_readb(dev, 0x5A), ==, 0x00); /* PAM1 */
106 g_assert_cmpint(qpci_config_readb(dev, 0x5B), ==, 0x00); /* PAM2 */
107 g_assert_cmpint(qpci_config_readb(dev, 0x5C), ==, 0x00); /* PAM3 */
108 g_assert_cmpint(qpci_config_readb(dev, 0x5D), ==, 0x00); /* PAM4 */
109 g_assert_cmpint(qpci_config_readb(dev, 0x5E), ==, 0x00); /* PAM5 */
110 g_assert_cmpint(qpci_config_readb(dev, 0x5F), ==, 0x00); /* PAM6 */
113 g_assert_cmpint(qpci_config_readb(dev, 0x60), ==, 0x01); /* DRB0 */
114 g_assert_cmpint(qpci_config_readb(dev, 0x61), ==, 0x01); /* DRB1 */
115 g_assert_cmpint(qpci_config_readb(dev, 0x62), ==, 0x01); /* DRB2 */
116 g_assert_cmpint(qpci_config_readb(dev, 0x63), ==, 0x01); /* DRB3 */
117 g_assert_cmpint(qpci_config_readb(dev, 0x64), ==, 0x01); /* DRB4 */
118 g_assert_cmpint(qpci_config_readb(dev, 0x65), ==, 0x01); /* DRB5 */
119 g_assert_cmpint(qpci_config_readb(dev, 0x66), ==, 0x01); /* DRB6 */
120 g_assert_cmpint(qpci_config_readb(dev, 0x67), ==, 0x01); /* DRB7 */
123 g_assert_cmpint(qpci_config_readb(dev, 0x68), ==, 0x00); /* FDHC */
125 g_assert_cmpint(qpci_config_readb(dev, 0x70), ==, 0x00); /* MTT */
128 g_assert_cmpint(qpci_config_readb(dev, 0x71), ==, 0x10); /* CLT */
131 g_assert_cmpint(qpci_config_readb(dev, 0x72), ==, 0x02); /* SMRAM */
133 g_assert_cmpint(qpci_config_readb(dev, 0x90), ==, 0x00); /* ERRCMD */
135 g_assert_cmpint(qpci_config_readb(dev, 0x91), ==, 0x00); /* ERRSTS */
137 g_assert_cmpint(qpci_config_readb(dev, 0x93), ==, 0x00); /* TRC */
145 static void pam_set(QPCIDevice *dev, int index, int flags)
147 int regno = 0x59 + (index / 2);
150 reg = qpci_config_readb(dev, regno);
152 reg = (reg & 0x0F) | (flags << 4);
154 reg = (reg & 0xF0) | flags;
156 qpci_config_writeb(dev, regno, reg);
159 static gboolean verify_area(uint32_t start, uint32_t end, uint8_t value)
161 uint32_t size = end - start + 1;
166 data = g_malloc0(size);
167 memread(start, data, size);
169 g_test_message("verify_area: data[0] = 0x%x", data[0]);
171 for (i = 0; i < size; i++) {
172 if (data[i] != value) {
183 static void write_area(uint32_t start, uint32_t end, uint8_t value)
185 uint32_t size = end - start + 1;
188 data = g_malloc(size);
189 memset(data, value, size);
190 memwrite(start, data, size);
195 static void test_i440fx_pam(gconstpointer opaque)
197 const TestData *s = opaque;
205 { 0, 0 }, /* Reserved */
206 { 0xF0000, 0xFFFFF }, /* BIOS Area */
207 { 0xC0000, 0xC3FFF }, /* Option ROM */
208 { 0xC4000, 0xC7FFF }, /* Option ROM */
209 { 0xC8000, 0xCBFFF }, /* Option ROM */
210 { 0xCC000, 0xCFFFF }, /* Option ROM */
211 { 0xD0000, 0xD3FFF }, /* Option ROM */
212 { 0xD4000, 0xD7FFF }, /* Option ROM */
213 { 0xD8000, 0xDBFFF }, /* Option ROM */
214 { 0xDC000, 0xDFFFF }, /* Option ROM */
215 { 0xE0000, 0xE3FFF }, /* BIOS Extension */
216 { 0xE4000, 0xE7FFF }, /* BIOS Extension */
217 { 0xE8000, 0xEBFFF }, /* BIOS Extension */
218 { 0xEC000, 0xEFFFF }, /* BIOS Extension */
221 bus = test_start_get_bus(s);
222 dev = qpci_device_find(bus, QPCI_DEVFN(0, 0));
223 g_assert(dev != NULL);
225 for (i = 0; i < ARRAY_SIZE(pam_area); i++) {
226 if (pam_area[i].start == pam_area[i].end) {
230 g_test_message("Checking area 0x%05x..0x%05x",
231 pam_area[i].start, pam_area[i].end);
232 /* Switch to RE for the area */
233 pam_set(dev, i, PAM_RE);
234 /* Verify the RAM is all zeros */
235 g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0));
237 /* Switch to WE for the area */
238 pam_set(dev, i, PAM_RE | PAM_WE);
239 /* Write out a non-zero mask to the full area */
240 write_area(pam_area[i].start, pam_area[i].end, 0x42);
243 /* QEMU only supports a limited form of PAM */
245 /* Switch to !RE for the area */
246 pam_set(dev, i, PAM_WE);
247 /* Verify the area is not our mask */
248 g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x42));
251 /* Verify the area is our new mask */
252 g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0x42));
254 /* Write out a new mask */
255 write_area(pam_area[i].start, pam_area[i].end, 0x82);
258 /* QEMU only supports a limited form of PAM */
260 /* Verify the area is not our mask */
261 g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x82));
263 /* Switch to RE for the area */
264 pam_set(dev, i, PAM_RE | PAM_WE);
266 /* Verify the area is our new mask */
267 g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0x82));
272 /* Verify the area is not our new mask */
273 g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x82));
278 #define BLOB_SIZE ((size_t)65536)
279 #define ISA_BIOS_MAXSZ ((size_t)(128 * 1024))
281 /* Create a blob file, and return its absolute pathname as a dynamically
283 * The file is closed before the function returns.
284 * In case of error, NULL is returned. The function prints the error message.
286 static char *create_blob_file(void)
290 GError *error = NULL;
293 fd = g_file_open_tmp("blob_XXXXXX", &pathname, &error);
295 fprintf(stderr, "unable to create blob file: %s\n", error->message);
298 if (ftruncate(fd, BLOB_SIZE) == -1) {
299 fprintf(stderr, "ftruncate(\"%s\", %zu): %s\n", pathname,
300 BLOB_SIZE, strerror(errno));
304 buf = mmap(NULL, BLOB_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
305 if (buf == MAP_FAILED) {
306 fprintf(stderr, "mmap(\"%s\", %zu): %s\n", pathname, BLOB_SIZE,
311 for (i = 0; i < BLOB_SIZE; ++i) {
312 ((uint8_t *)buf)[i] = i;
314 munmap(buf, BLOB_SIZE);
325 return ret == -1 ? NULL : pathname;
328 static void test_i440fx_firmware(FirmwareTestFixture *fixture,
329 gconstpointer user_data)
331 char *fw_pathname, *cmdline;
333 size_t i, isa_bios_size;
335 fw_pathname = create_blob_file();
336 g_assert(fw_pathname != NULL);
338 /* Better hope the user didn't put metacharacters in TMPDIR and co. */
339 cmdline = g_strdup_printf("-S %s%s", fixture->is_bios
341 : "-drive if=pflash,format=raw,file=",
343 g_test_message("qemu cmdline: %s", cmdline);
344 qtest_start(cmdline);
347 /* QEMU has loaded the firmware (because qtest_start() only returns after
348 * the QMP handshake completes). We must unlink the firmware blob right
349 * here, because any assertion firing below would leak it in the
350 * filesystem. This is also the reason why we recreate the blob every time
351 * this function is invoked.
357 buf = g_malloc0(BLOB_SIZE);
358 memread(0x100000000ULL - BLOB_SIZE, buf, BLOB_SIZE);
359 for (i = 0; i < BLOB_SIZE; ++i) {
360 g_assert_cmphex(buf[i], ==, (uint8_t)i);
363 /* check in ISA space too */
364 memset(buf, 0, BLOB_SIZE);
365 isa_bios_size = ISA_BIOS_MAXSZ < BLOB_SIZE ? ISA_BIOS_MAXSZ : BLOB_SIZE;
366 memread(0x100000 - isa_bios_size, buf, isa_bios_size);
367 for (i = 0; i < isa_bios_size; ++i) {
368 g_assert_cmphex(buf[i], ==,
369 (uint8_t)((BLOB_SIZE - isa_bios_size) + i));
376 static void add_firmware_test(const char *testpath,
377 void (*setup_fixture)(FirmwareTestFixture *f,
378 gconstpointer test_data))
380 qtest_add(testpath, FirmwareTestFixture, NULL, setup_fixture,
381 test_i440fx_firmware, NULL);
384 static void request_bios(FirmwareTestFixture *fixture,
385 gconstpointer user_data)
387 fixture->is_bios = true;
390 static void request_pflash(FirmwareTestFixture *fixture,
391 gconstpointer user_data)
393 fixture->is_bios = false;
396 int main(int argc, char **argv)
401 g_test_init(&argc, &argv, NULL);
405 qtest_add_data_func("i440fx/defaults", &data, test_i440fx_defaults);
406 qtest_add_data_func("i440fx/pam", &data, test_i440fx_pam);
407 add_firmware_test("i440fx/firmware/bios", request_bios);
408 add_firmware_test("i440fx/firmware/pflash", request_pflash);