1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
10 #include <test/test.h>
13 /* Test that sandbox PCI works correctly */
14 static int dm_test_pci_base(struct unit_test_state *uts)
18 ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
22 DM_TEST(dm_test_pci_base, UTF_SCAN_PDATA | UTF_SCAN_FDT);
24 /* Test that sandbox PCI bus numbering and device works correctly */
25 static int dm_test_pci_busdev(struct unit_test_state *uts)
31 /* Test bus#0 and its devices */
32 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
34 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
36 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
37 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
38 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
40 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
41 ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
43 /* Test bus#1 and its devices */
44 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
46 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
48 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
49 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
50 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
52 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
53 ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
57 DM_TEST(dm_test_pci_busdev, UTF_SCAN_PDATA | UTF_SCAN_FDT);
59 /* Test that we can use the swapcase device correctly */
60 static int dm_test_pci_swapcase(struct unit_test_state *uts)
63 ulong io_addr, mem_addr;
66 /* Check that asking for the device 0 automatically fires up PCI */
67 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
70 io_addr = dm_pci_read_bar32(swap, 0);
72 ut_asserteq(2, inb(io_addr));
75 * Now test memory mapping - note we must unmap and remap to cause
76 * the swapcase emulation to see our data and response.
78 mem_addr = dm_pci_read_bar32(swap, 1);
79 ptr = map_sysmem(mem_addr, 20);
80 strcpy(ptr, "This is a TesT");
83 ptr = map_sysmem(mem_addr, 20);
84 ut_asserteq_str("tHIS IS A tESt", ptr);
87 /* Check that asking for the device 1 automatically fires up PCI */
88 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
91 io_addr = dm_pci_read_bar32(swap, 0);
93 ut_asserteq(2, inb(io_addr));
96 * Now test memory mapping - note we must unmap and remap to cause
97 * the swapcase emulation to see our data and response.
99 mem_addr = dm_pci_read_bar32(swap, 1);
100 ptr = map_sysmem(mem_addr, 20);
101 strcpy(ptr, "This is a TesT");
104 ptr = map_sysmem(mem_addr, 20);
105 ut_asserteq_str("tHIS IS A tESt", ptr);
110 DM_TEST(dm_test_pci_swapcase, UTF_SCAN_PDATA | UTF_SCAN_FDT);
112 /* Test that we can dynamically bind the device driver correctly */
113 static int dm_test_pci_drvdata(struct unit_test_state *uts)
115 struct udevice *bus, *swap;
117 /* Check that asking for the device automatically fires up PCI */
118 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
120 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
121 ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
122 ut_assertok(dev_has_ofnode(swap));
123 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
124 ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
125 ut_assertok(dev_has_ofnode(swap));
126 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x10, 0), &swap));
127 ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
128 ut_assertok(!dev_has_ofnode(swap));
132 DM_TEST(dm_test_pci_drvdata, UTF_SCAN_PDATA | UTF_SCAN_FDT);
134 /* Test that devices on PCI bus#2 can be accessed correctly */
135 static int dm_test_pci_mixed(struct unit_test_state *uts)
137 /* PCI bus#2 has both statically and dynamic declared devices */
138 struct udevice *bus, *swap;
140 ulong io_addr, mem_addr;
143 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 2, &bus));
145 /* Test the dynamic device */
146 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x08, 0), &swap));
148 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
149 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
152 io_addr = dm_pci_read_bar32(swap, 0);
154 ut_asserteq(2, inb(io_addr));
157 * Now test memory mapping - note we must unmap and remap to cause
158 * the swapcase emulation to see our data and response.
160 mem_addr = dm_pci_read_bar32(swap, 1);
161 ptr = map_sysmem(mem_addr, 30);
162 strcpy(ptr, "This is a TesT oN dYNAMIc");
165 ptr = map_sysmem(mem_addr, 30);
166 ut_asserteq_str("tHIS IS A tESt On DynamiC", ptr);
169 /* Test the static device */
170 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x1f, 0), &swap));
172 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
173 ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
176 io_addr = dm_pci_read_bar32(swap, 0);
178 ut_asserteq(2, inb(io_addr));
181 * Now test memory mapping - note we must unmap and remap to cause
182 * the swapcase emulation to see our data and response.
184 mem_addr = dm_pci_read_bar32(swap, 1);
185 ptr = map_sysmem(mem_addr, 30);
186 strcpy(ptr, "This is a TesT oN sTATIc");
189 ptr = map_sysmem(mem_addr, 30);
190 ut_asserteq_str("tHIS IS A tESt On StatiC", ptr);
195 DM_TEST(dm_test_pci_mixed, UTF_SCAN_PDATA | UTF_SCAN_FDT);
197 /* Test looking up PCI capability and extended capability */
198 static int dm_test_pci_cap(struct unit_test_state *uts)
200 struct udevice *bus, *swap;
203 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
204 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
206 /* look up PCI_CAP_ID_EXP */
207 cap = dm_pci_find_capability(swap, PCI_CAP_ID_EXP);
208 ut_asserteq(PCI_CAP_ID_EXP_OFFSET, cap);
210 /* look up PCI_CAP_ID_PCIX */
211 cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX);
214 /* look up PCI_CAP_ID_MSIX starting from PCI_CAP_ID_PM_OFFSET */
215 cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_PM_OFFSET,
217 ut_asserteq(PCI_CAP_ID_MSIX_OFFSET, cap);
219 /* look up PCI_CAP_ID_VNDR starting from PCI_CAP_ID_EXP_OFFSET */
220 cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_EXP_OFFSET,
224 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
225 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
227 /* look up PCI_EXT_CAP_ID_DSN */
228 cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_DSN);
229 ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
231 /* look up PCI_EXT_CAP_ID_SRIOV */
232 cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV);
235 /* look up PCI_EXT_CAP_ID_DSN starting from PCI_EXT_CAP_ID_ERR_OFFSET */
236 cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_ERR_OFFSET,
238 ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
240 /* look up PCI_EXT_CAP_ID_RCRB starting from PCI_EXT_CAP_ID_VC_OFFSET */
241 cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_VC_OFFSET,
242 PCI_EXT_CAP_ID_RCRB);
247 DM_TEST(dm_test_pci_cap, UTF_SCAN_PDATA | UTF_SCAN_FDT);
249 /* Test looking up BARs in EA capability structure */
250 static int dm_test_pci_ea(struct unit_test_state *uts)
252 struct udevice *bus, *swap;
257 * use emulated device mapping function, we're not using real physical
258 * addresses in this test
260 sandbox_set_enable_pci_map(true);
262 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
263 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x01, 0), &swap));
265 /* look up PCI_CAP_ID_EA */
266 cap = dm_pci_find_capability(swap, PCI_CAP_ID_EA);
267 ut_asserteq(PCI_CAP_ID_EA_OFFSET, cap);
269 /* test swap case in BAR 1 */
270 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE, 0);
271 ut_assertnonnull(bar);
272 *(int *)bar = 2; /* swap upper/lower */
274 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0, 0, PCI_REGION_TYPE, 0);
275 ut_assertnonnull(bar);
276 strcpy(bar, "ea TEST");
278 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0, 0, PCI_REGION_TYPE, 0);
279 ut_assertnonnull(bar);
280 ut_asserteq_str("EA test", bar);
282 /* test magic values in BARs2, 4; BAR 3 is n/a */
283 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_2, 0, 0, PCI_REGION_TYPE, 0);
284 ut_assertnonnull(bar);
285 ut_asserteq(PCI_EA_BAR2_MAGIC, *(u32 *)bar);
287 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_3, 0, 0, PCI_REGION_TYPE, 0);
290 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_4, 0, 0, PCI_REGION_TYPE, 0);
291 ut_assertnonnull(bar);
292 ut_asserteq(PCI_EA_BAR4_MAGIC, *(u32 *)bar);
296 DM_TEST(dm_test_pci_ea, UTF_SCAN_PDATA | UTF_SCAN_FDT);
298 /* Test the dev_read_addr_pci() function */
299 static int dm_test_pci_addr_flat(struct unit_test_state *uts)
301 struct udevice *swap1f, *swap1;
302 ulong io_addr, mem_addr;
305 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f));
306 io_addr = dm_pci_read_bar32(swap1f, 0);
307 ut_asserteq(io_addr, dev_read_addr_pci(swap1f, &size));
308 ut_asserteq(0, size);
311 * This device has both I/O and MEM spaces but the MEM space appears
314 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1));
315 mem_addr = dm_pci_read_bar32(swap1, 1);
316 ut_asserteq(mem_addr, dev_read_addr_pci(swap1, &size));
317 ut_asserteq(0, size);
321 DM_TEST(dm_test_pci_addr_flat, UTF_SCAN_PDATA | UTF_SCAN_FDT |
325 * Test the dev_read_addr_pci() function with livetree. That function is
326 * not currently fully implemented, in that it fails to return the BAR address.
327 * Once that is implemented this test can be removed and dm_test_pci_addr_flat()
328 * can be used for both flattree and livetree by removing the UTF_FLAT_TREE
331 static int dm_test_pci_addr_live(struct unit_test_state *uts)
333 struct udevice *swap1f, *swap1;
336 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f));
337 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1f, &size));
338 ut_asserteq(0, size);
340 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1));
341 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1, &size));
342 ut_asserteq(0, size);
346 DM_TEST(dm_test_pci_addr_live, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_LIVE_TREE);
348 /* Test device_is_on_pci_bus() */
349 static int dm_test_pci_on_bus(struct unit_test_state *uts)
353 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &dev));
354 ut_asserteq(true, device_is_on_pci_bus(dev));
355 ut_asserteq(false, device_is_on_pci_bus(dev_get_parent(dev)));
356 ut_asserteq(true, device_is_on_pci_bus(dev));
360 DM_TEST(dm_test_pci_on_bus, UTF_SCAN_PDATA | UTF_SCAN_FDT);
363 * Test support for multiple memory regions enabled via
364 * CONFIG_PCI_REGION_MULTI_ENTRY. When this feature is not enabled,
365 * only the last region of one type is stored. In this test-case,
366 * we have 2 memory regions, the first at 0x3000.0000 and the 2nd
367 * at 0x3100.0000. A correct test results now in BAR1 located at
370 static int dm_test_pci_region_multi(struct unit_test_state *uts)
375 /* Test memory BAR1 on bus#1 */
376 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev));
377 mem_addr = dm_pci_read_bar32(dev, 1);
378 ut_asserteq(mem_addr, 0x30000000);
382 DM_TEST(dm_test_pci_region_multi, UTF_SCAN_PDATA | UTF_SCAN_FDT);
385 * Test the translation of PCI bus addresses to physical addresses using the
388 static int dm_test_pci_bus_to_phys(struct unit_test_state *uts)
390 unsigned long mask = PCI_REGION_TYPE;
391 unsigned long flags = PCI_REGION_MEM;
393 phys_addr_t phys_addr;
395 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev));
397 /* Before any of the ranges. */
398 phys_addr = dm_pci_bus_to_phys(dev, 0x20000000, 0x400, mask, flags);
399 ut_asserteq(0, phys_addr);
401 /* Identity range: whole, start, mid, end */
402 phys_addr = dm_pci_bus_to_phys(dev, 0x2ffff000, 0x2000, mask, flags);
403 ut_asserteq(0, phys_addr);
404 phys_addr = dm_pci_bus_to_phys(dev, 0x30000000, 0x2000, mask, flags);
405 ut_asserteq(0x30000000, phys_addr);
406 phys_addr = dm_pci_bus_to_phys(dev, 0x30000000, 0x1000, mask, flags);
407 ut_asserteq(0x30000000, phys_addr);
408 phys_addr = dm_pci_bus_to_phys(dev, 0x30000abc, 0x12, mask, flags);
409 ut_asserteq(0x30000abc, phys_addr);
410 phys_addr = dm_pci_bus_to_phys(dev, 0x30000800, 0x1800, mask, flags);
411 ut_asserteq(0x30000800, phys_addr);
412 phys_addr = dm_pci_bus_to_phys(dev, 0x30008000, 0x1801, mask, flags);
413 ut_asserteq(0, phys_addr);
415 /* Translated range: whole, start, mid, end */
416 phys_addr = dm_pci_bus_to_phys(dev, 0x30fff000, 0x2000, mask, flags);
417 ut_asserteq(0, phys_addr);
418 phys_addr = dm_pci_bus_to_phys(dev, 0x31000000, 0x2000, mask, flags);
419 ut_asserteq(0x3e000000, phys_addr);
420 phys_addr = dm_pci_bus_to_phys(dev, 0x31000000, 0x1000, mask, flags);
421 ut_asserteq(0x3e000000, phys_addr);
422 phys_addr = dm_pci_bus_to_phys(dev, 0x31000abc, 0x12, mask, flags);
423 ut_asserteq(0x3e000abc, phys_addr);
424 phys_addr = dm_pci_bus_to_phys(dev, 0x31000800, 0x1800, mask, flags);
425 ut_asserteq(0x3e000800, phys_addr);
426 phys_addr = dm_pci_bus_to_phys(dev, 0x31008000, 0x1801, mask, flags);
427 ut_asserteq(0, phys_addr);
429 /* Beyond all of the ranges. */
430 phys_addr = dm_pci_bus_to_phys(dev, 0x32000000, 0x400, mask, flags);
431 ut_asserteq(0, phys_addr);
435 DM_TEST(dm_test_pci_bus_to_phys, UTF_SCAN_PDATA | UTF_SCAN_FDT);
438 * Test the translation of physical addresses to PCI bus addresses using the
441 static int dm_test_pci_phys_to_bus(struct unit_test_state *uts)
443 unsigned long mask = PCI_REGION_TYPE;
444 unsigned long flags = PCI_REGION_MEM;
448 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev));
450 /* Before any of the ranges. */
451 pci_addr = dm_pci_phys_to_bus(dev, 0x20000000, 0x400, mask, flags);
452 ut_asserteq(0, pci_addr);
454 /* Identity range: partial overlap, whole, start, mid, end */
455 pci_addr = dm_pci_phys_to_bus(dev, 0x2ffff000, 0x2000, mask, flags);
456 ut_asserteq(0, pci_addr);
457 pci_addr = dm_pci_phys_to_bus(dev, 0x30000000, 0x2000, mask, flags);
458 ut_asserteq(0x30000000, pci_addr);
459 pci_addr = dm_pci_phys_to_bus(dev, 0x30000000, 0x1000, mask, flags);
460 ut_asserteq(0x30000000, pci_addr);
461 pci_addr = dm_pci_phys_to_bus(dev, 0x30000abc, 0x12, mask, flags);
462 ut_asserteq(0x30000abc, pci_addr);
463 pci_addr = dm_pci_phys_to_bus(dev, 0x30000800, 0x1800, mask, flags);
464 ut_asserteq(0x30000800, pci_addr);
465 pci_addr = dm_pci_phys_to_bus(dev, 0x30008000, 0x1801, mask, flags);
466 ut_asserteq(0, pci_addr);
468 /* Translated range: partial overlap, whole, start, mid, end */
469 pci_addr = dm_pci_phys_to_bus(dev, 0x3dfff000, 0x2000, mask, flags);
470 ut_asserteq(0, pci_addr);
471 pci_addr = dm_pci_phys_to_bus(dev, 0x3e000000, 0x2000, mask, flags);
472 ut_asserteq(0x31000000, pci_addr);
473 pci_addr = dm_pci_phys_to_bus(dev, 0x3e000000, 0x1000, mask, flags);
474 ut_asserteq(0x31000000, pci_addr);
475 pci_addr = dm_pci_phys_to_bus(dev, 0x3e000abc, 0x12, mask, flags);
476 ut_asserteq(0x31000abc, pci_addr);
477 pci_addr = dm_pci_phys_to_bus(dev, 0x3e000800, 0x1800, mask, flags);
478 ut_asserteq(0x31000800, pci_addr);
479 pci_addr = dm_pci_phys_to_bus(dev, 0x3e008000, 0x1801, mask, flags);
480 ut_asserteq(0, pci_addr);
482 /* Beyond all of the ranges. */
483 pci_addr = dm_pci_phys_to_bus(dev, 0x3f000000, 0x400, mask, flags);
484 ut_asserteq(0, pci_addr);
488 DM_TEST(dm_test_pci_phys_to_bus, UTF_SCAN_PDATA | UTF_SCAN_FDT);