1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
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, DM_TESTF_SCAN_PDATA | DM_TESTF_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, DM_TESTF_SCAN_PDATA | DM_TESTF_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, DM_TESTF_SCAN_PDATA | DM_TESTF_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_of_valid(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_of_valid(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_of_valid(swap));
132 DM_TEST(dm_test_pci_drvdata, DM_TESTF_SCAN_PDATA | DM_TESTF_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, DM_TESTF_SCAN_PDATA | DM_TESTF_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, DM_TESTF_SCAN_PDATA | DM_TESTF_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);
271 ut_assertnonnull(bar);
272 *(int *)bar = 2; /* swap upper/lower */
274 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0);
275 ut_assertnonnull(bar);
276 strcpy(bar, "ea TEST");
278 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 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);
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);
290 bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_4, 0);
291 ut_assertnonnull(bar);
292 ut_asserteq(PCI_EA_BAR4_MAGIC, *(u32 *)bar);
296 DM_TEST(dm_test_pci_ea, DM_TESTF_SCAN_PDATA | DM_TESTF_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;
304 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f));
305 io_addr = dm_pci_read_bar32(swap1f, 0);
306 ut_asserteq(io_addr, dev_read_addr_pci(swap1f));
309 * This device has both I/O and MEM spaces but the MEM space appears
312 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1));
313 mem_addr = dm_pci_read_bar32(swap1, 1);
314 ut_asserteq(mem_addr, dev_read_addr_pci(swap1));
318 DM_TEST(dm_test_pci_addr_flat, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT |
322 * Test the dev_read_addr_pci() function with livetree. That function is
323 * not currently fully implemented, in that it fails to return the BAR address.
324 * Once that is implemented this test can be removed and dm_test_pci_addr_flat()
325 * can be used for both flattree and livetree by removing the DM_TESTF_FLAT_TREE
328 static int dm_test_pci_addr_live(struct unit_test_state *uts)
330 struct udevice *swap1f, *swap1;
332 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f));
333 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1f));
335 ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1));
336 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1));
340 DM_TEST(dm_test_pci_addr_live, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT |