1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
11 #include <test/test.h>
14 /* Test that sandbox PCI works correctly */
15 static int dm_test_pci_base(struct unit_test_state *uts)
19 ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus));
23 DM_TEST(dm_test_pci_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
25 /* Test that sandbox PCI bus numbering and device works correctly */
26 static int dm_test_pci_busdev(struct unit_test_state *uts)
32 /* Test bus#0 and its devices */
33 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
35 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
37 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
38 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
39 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
41 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
42 ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
44 /* Test bus#1 and its devices */
45 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
47 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
49 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
50 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
51 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
53 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
54 ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
58 DM_TEST(dm_test_pci_busdev, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
60 /* Test that we can use the swapcase device correctly */
61 static int dm_test_pci_swapcase(struct unit_test_state *uts)
64 ulong io_addr, mem_addr;
67 /* Check that asking for the device 0 automatically fires up PCI */
68 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
71 io_addr = dm_pci_read_bar32(swap, 0);
73 ut_asserteq(2, inb(io_addr));
76 * Now test memory mapping - note we must unmap and remap to cause
77 * the swapcase emulation to see our data and response.
79 mem_addr = dm_pci_read_bar32(swap, 1);
80 ptr = map_sysmem(mem_addr, 20);
81 strcpy(ptr, "This is a TesT");
84 ptr = map_sysmem(mem_addr, 20);
85 ut_asserteq_str("tHIS IS A tESt", ptr);
88 /* Check that asking for the device 1 automatically fires up PCI */
89 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
92 io_addr = dm_pci_read_bar32(swap, 0);
94 ut_asserteq(2, inb(io_addr));
97 * Now test memory mapping - note we must unmap and remap to cause
98 * the swapcase emulation to see our data and response.
100 mem_addr = dm_pci_read_bar32(swap, 1);
101 ptr = map_sysmem(mem_addr, 20);
102 strcpy(ptr, "This is a TesT");
105 ptr = map_sysmem(mem_addr, 20);
106 ut_asserteq_str("tHIS IS A tESt", ptr);
111 DM_TEST(dm_test_pci_swapcase, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
113 /* Test that we can dynamically bind the device driver correctly */
114 static int dm_test_pci_drvdata(struct unit_test_state *uts)
116 struct udevice *bus, *swap;
118 /* Check that asking for the device automatically fires up PCI */
119 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
121 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
122 ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
123 ut_assertok(dev_has_ofnode(swap));
124 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
125 ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
126 ut_assertok(dev_has_ofnode(swap));
127 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x10, 0), &swap));
128 ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
129 ut_assertok(!dev_has_ofnode(swap));
133 DM_TEST(dm_test_pci_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
135 /* Test that devices on PCI bus#2 can be accessed correctly */
136 static int dm_test_pci_mixed(struct unit_test_state *uts)
138 /* PCI bus#2 has both statically and dynamic declared devices */
139 struct udevice *bus, *swap;
141 ulong io_addr, mem_addr;
144 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 2, &bus));
146 /* Test the dynamic device */
147 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x08, 0), &swap));
149 ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
150 ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
153 io_addr = dm_pci_read_bar32(swap, 0);
155 ut_asserteq(2, inb(io_addr));
158 * Now test memory mapping - note we must unmap and remap to cause
159 * the swapcase emulation to see our data and response.
161 mem_addr = dm_pci_read_bar32(swap, 1);
162 ptr = map_sysmem(mem_addr, 30);
163 strcpy(ptr, "This is a TesT oN dYNAMIc");
166 ptr = map_sysmem(mem_addr, 30);
167 ut_asserteq_str("tHIS IS A tESt On DynamiC", ptr);
170 /* Test the static device */
171 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x1f, 0), &swap));
173 ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
174 ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device);
177 io_addr = dm_pci_read_bar32(swap, 0);
179 ut_asserteq(2, inb(io_addr));
182 * Now test memory mapping - note we must unmap and remap to cause
183 * the swapcase emulation to see our data and response.
185 mem_addr = dm_pci_read_bar32(swap, 1);
186 ptr = map_sysmem(mem_addr, 30);
187 strcpy(ptr, "This is a TesT oN sTATIc");
190 ptr = map_sysmem(mem_addr, 30);
191 ut_asserteq_str("tHIS IS A tESt On StatiC", ptr);
196 DM_TEST(dm_test_pci_mixed, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
198 /* Test looking up PCI capability and extended capability */
199 static int dm_test_pci_cap(struct unit_test_state *uts)
201 struct udevice *bus, *swap;
204 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
205 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
207 /* look up PCI_CAP_ID_EXP */
208 cap = dm_pci_find_capability(swap, PCI_CAP_ID_EXP);
209 ut_asserteq(PCI_CAP_ID_EXP_OFFSET, cap);
211 /* look up PCI_CAP_ID_PCIX */
212 cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX);
215 /* look up PCI_CAP_ID_MSIX starting from PCI_CAP_ID_PM_OFFSET */
216 cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_PM_OFFSET,
218 ut_asserteq(PCI_CAP_ID_MSIX_OFFSET, cap);
220 /* look up PCI_CAP_ID_VNDR starting from PCI_CAP_ID_EXP_OFFSET */
221 cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_EXP_OFFSET,
225 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
226 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
228 /* look up PCI_EXT_CAP_ID_DSN */
229 cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_DSN);
230 ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
232 /* look up PCI_EXT_CAP_ID_SRIOV */
233 cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV);
236 /* look up PCI_EXT_CAP_ID_DSN starting from PCI_EXT_CAP_ID_ERR_OFFSET */
237 cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_ERR_OFFSET,
239 ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
241 /* look up PCI_EXT_CAP_ID_RCRB starting from PCI_EXT_CAP_ID_VC_OFFSET */
242 cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_VC_OFFSET,
243 PCI_EXT_CAP_ID_RCRB);
248 DM_TEST(dm_test_pci_cap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
250 /* Test looking up BARs in EA capability structure */
251 static int dm_test_pci_ea(struct unit_test_state *uts)
253 struct udevice *bus, *swap;
258 * use emulated device mapping function, we're not using real physical
259 * addresses in this test
261 sandbox_set_enable_pci_map(true);
263 ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
264 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x01, 0), &swap));
266 /* look up PCI_CAP_ID_EA */
267 cap = dm_pci_find_capability(swap, PCI_CAP_ID_EA);
268 ut_asserteq(PCI_CAP_ID_EA_OFFSET, cap);
270 /* test swap case in BAR 1 */
271 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_0, 0);
272 ut_assertnonnull(bar);
273 *(int *)bar = 2; /* swap upper/lower */
275 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0);
276 ut_assertnonnull(bar);
277 strcpy(bar, "ea TEST");
279 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0);
280 ut_assertnonnull(bar);
281 ut_asserteq_str("EA test", bar);
283 /* test magic values in BARs2, 4; BAR 3 is n/a */
284 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_2, 0);
285 ut_assertnonnull(bar);
286 ut_asserteq(PCI_EA_BAR2_MAGIC, *(u32 *)bar);
288 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_3, 0);
291 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_4, 0);
292 ut_assertnonnull(bar);
293 ut_asserteq(PCI_EA_BAR4_MAGIC, *(u32 *)bar);
297 DM_TEST(dm_test_pci_ea, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
299 /* Test the dev_read_addr_pci() function */
300 static int dm_test_pci_addr_flat(struct unit_test_state *uts)
302 struct udevice *swap1f, *swap1;
303 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));
310 * This device has both I/O and MEM spaces but the MEM space appears
313 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1));
314 mem_addr = dm_pci_read_bar32(swap1, 1);
315 ut_asserteq(mem_addr, dev_read_addr_pci(swap1));
319 DM_TEST(dm_test_pci_addr_flat, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT |
323 * Test the dev_read_addr_pci() function with livetree. That function is
324 * not currently fully implemented, in that it fails to return the BAR address.
325 * Once that is implemented this test can be removed and dm_test_pci_addr_flat()
326 * can be used for both flattree and livetree by removing the UT_TESTF_FLAT_TREE
329 static int dm_test_pci_addr_live(struct unit_test_state *uts)
331 struct udevice *swap1f, *swap1;
333 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f));
334 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1f));
336 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1));
337 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1));
341 DM_TEST(dm_test_pci_addr_live, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT |
344 /* Test device_is_on_pci_bus() */
345 static int dm_test_pci_on_bus(struct unit_test_state *uts)
349 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &dev));
350 ut_asserteq(true, device_is_on_pci_bus(dev));
351 ut_asserteq(false, device_is_on_pci_bus(dev_get_parent(dev)));
352 ut_asserteq(true, device_is_on_pci_bus(dev));
356 DM_TEST(dm_test_pci_on_bus, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
359 * Test support for multiple memory regions enabled via
360 * CONFIG_PCI_REGION_MULTI_ENTRY. When this feature is not enabled,
361 * only the last region of one type is stored. In this test-case,
362 * we have 2 memory regions, the first at 0x3000.0000 and the 2nd
363 * at 0x3100.0000. A correct test results now in BAR1 located at
366 static int dm_test_pci_region_multi(struct unit_test_state *uts)
371 /* Test memory BAR1 on bus#1 */
372 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev));
373 mem_addr = dm_pci_read_bar32(dev, 1);
374 ut_asserteq(mem_addr, 0x30000000);
378 DM_TEST(dm_test_pci_region_multi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);