Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <asm/io.h> |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 8 | #include <asm/test.h> |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 9 | #include <dm/test.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 10 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 11 | #include <test/ut.h> |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 12 | |
| 13 | /* Test that sandbox PCI works correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | static int dm_test_pci_base(struct unit_test_state *uts) |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 15 | { |
| 16 | struct udevice *bus; |
| 17 | |
| 18 | ut_assertok(uclass_get_device(UCLASS_PCI, 0, &bus)); |
| 19 | |
| 20 | return 0; |
| 21 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 22 | DM_TEST(dm_test_pci_base, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 23 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 24 | /* Test that sandbox PCI bus numbering and device works correctly */ |
| 25 | static int dm_test_pci_busdev(struct unit_test_state *uts) |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 26 | { |
| 27 | struct udevice *bus; |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 28 | struct udevice *swap; |
| 29 | u16 vendor, device; |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 30 | |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 31 | /* Test bus#0 and its devices */ |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 32 | ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus)); |
| 33 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 34 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap)); |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 35 | vendor = 0; |
| 36 | ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor)); |
| 37 | ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor); |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 38 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap)); |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 39 | device = 0; |
| 40 | ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device)); |
Simon Glass | 21c8f1a | 2019-09-25 08:56:01 -0600 | [diff] [blame] | 41 | ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device); |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 42 | |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 43 | /* Test bus#1 and its devices */ |
| 44 | ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus)); |
| 45 | |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 46 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap)); |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 47 | vendor = 0; |
| 48 | ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor)); |
| 49 | ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor); |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 50 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap)); |
Bin Meng | 325eed9 | 2018-08-03 01:14:42 -0700 | [diff] [blame] | 51 | device = 0; |
| 52 | ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device)); |
Simon Glass | 21c8f1a | 2019-09-25 08:56:01 -0600 | [diff] [blame] | 53 | ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device); |
Bin Meng | 408e590 | 2018-08-03 01:14:41 -0700 | [diff] [blame] | 54 | |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 55 | return 0; |
| 56 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 57 | DM_TEST(dm_test_pci_busdev, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | a814941 | 2015-05-10 21:08:06 -0600 | [diff] [blame] | 58 | |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 59 | /* Test that we can use the swapcase device correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 60 | static int dm_test_pci_swapcase(struct unit_test_state *uts) |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 61 | { |
Bin Meng | 1445042 | 2018-08-03 01:14:38 -0700 | [diff] [blame] | 62 | struct udevice *swap; |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 63 | ulong io_addr, mem_addr; |
| 64 | char *ptr; |
| 65 | |
Bin Meng | cbf071b | 2018-08-03 01:14:39 -0700 | [diff] [blame] | 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)); |
| 68 | |
| 69 | /* First test I/O */ |
| 70 | io_addr = dm_pci_read_bar32(swap, 0); |
| 71 | outb(2, io_addr); |
| 72 | ut_asserteq(2, inb(io_addr)); |
| 73 | |
| 74 | /* |
| 75 | * Now test memory mapping - note we must unmap and remap to cause |
| 76 | * the swapcase emulation to see our data and response. |
| 77 | */ |
| 78 | mem_addr = dm_pci_read_bar32(swap, 1); |
| 79 | ptr = map_sysmem(mem_addr, 20); |
| 80 | strcpy(ptr, "This is a TesT"); |
| 81 | unmap_sysmem(ptr); |
| 82 | |
| 83 | ptr = map_sysmem(mem_addr, 20); |
| 84 | ut_asserteq_str("tHIS IS A tESt", ptr); |
| 85 | unmap_sysmem(ptr); |
| 86 | |
| 87 | /* Check that asking for the device 1 automatically fires up PCI */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 88 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap)); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 89 | |
| 90 | /* First test I/O */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 91 | io_addr = dm_pci_read_bar32(swap, 0); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 92 | outb(2, io_addr); |
| 93 | ut_asserteq(2, inb(io_addr)); |
| 94 | |
| 95 | /* |
| 96 | * Now test memory mapping - note we must unmap and remap to cause |
| 97 | * the swapcase emulation to see our data and response. |
| 98 | */ |
Simon Glass | 0120d46 | 2015-11-29 13:18:02 -0700 | [diff] [blame] | 99 | mem_addr = dm_pci_read_bar32(swap, 1); |
Simon Glass | 3a6eae6 | 2015-03-05 12:25:34 -0700 | [diff] [blame] | 100 | ptr = map_sysmem(mem_addr, 20); |
| 101 | strcpy(ptr, "This is a TesT"); |
| 102 | unmap_sysmem(ptr); |
| 103 | |
| 104 | ptr = map_sysmem(mem_addr, 20); |
| 105 | ut_asserteq_str("tHIS IS A tESt", ptr); |
| 106 | unmap_sysmem(ptr); |
| 107 | |
| 108 | return 0; |
| 109 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 110 | DM_TEST(dm_test_pci_swapcase, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Bin Meng | 4e08073 | 2018-08-03 01:14:48 -0700 | [diff] [blame] | 111 | |
| 112 | /* Test that we can dynamically bind the device driver correctly */ |
| 113 | static int dm_test_pci_drvdata(struct unit_test_state *uts) |
| 114 | { |
| 115 | struct udevice *bus, *swap; |
| 116 | |
| 117 | /* Check that asking for the device automatically fires up PCI */ |
| 118 | ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus)); |
| 119 | |
| 120 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap)); |
| 121 | ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data); |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 122 | ut_assertok(dev_has_ofnode(swap)); |
Bin Meng | 4e08073 | 2018-08-03 01:14:48 -0700 | [diff] [blame] | 123 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap)); |
| 124 | ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data); |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 125 | ut_assertok(dev_has_ofnode(swap)); |
Marek Vasut | c17d162 | 2018-10-10 21:27:09 +0200 | [diff] [blame] | 126 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x10, 0), &swap)); |
| 127 | ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data); |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 128 | ut_assertok(!dev_has_ofnode(swap)); |
Bin Meng | 4e08073 | 2018-08-03 01:14:48 -0700 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 132 | DM_TEST(dm_test_pci_drvdata, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Bin Meng | 510dddb | 2018-08-03 01:14:50 -0700 | [diff] [blame] | 133 | |
| 134 | /* Test that devices on PCI bus#2 can be accessed correctly */ |
| 135 | static int dm_test_pci_mixed(struct unit_test_state *uts) |
| 136 | { |
| 137 | /* PCI bus#2 has both statically and dynamic declared devices */ |
| 138 | struct udevice *bus, *swap; |
| 139 | u16 vendor, device; |
| 140 | ulong io_addr, mem_addr; |
| 141 | char *ptr; |
| 142 | |
| 143 | ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 2, &bus)); |
| 144 | |
| 145 | /* Test the dynamic device */ |
| 146 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x08, 0), &swap)); |
| 147 | vendor = 0; |
| 148 | ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor)); |
| 149 | ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor); |
| 150 | |
| 151 | /* First test I/O */ |
| 152 | io_addr = dm_pci_read_bar32(swap, 0); |
| 153 | outb(2, io_addr); |
| 154 | ut_asserteq(2, inb(io_addr)); |
| 155 | |
| 156 | /* |
| 157 | * Now test memory mapping - note we must unmap and remap to cause |
| 158 | * the swapcase emulation to see our data and response. |
| 159 | */ |
| 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"); |
| 163 | unmap_sysmem(ptr); |
| 164 | |
| 165 | ptr = map_sysmem(mem_addr, 30); |
| 166 | ut_asserteq_str("tHIS IS A tESt On DynamiC", ptr); |
| 167 | unmap_sysmem(ptr); |
| 168 | |
| 169 | /* Test the static device */ |
| 170 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(2, 0x1f, 0), &swap)); |
| 171 | device = 0; |
| 172 | ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device)); |
Simon Glass | 21c8f1a | 2019-09-25 08:56:01 -0600 | [diff] [blame] | 173 | ut_asserteq(SANDBOX_PCI_SWAP_CASE_EMUL_ID, device); |
Bin Meng | 510dddb | 2018-08-03 01:14:50 -0700 | [diff] [blame] | 174 | |
| 175 | /* First test I/O */ |
| 176 | io_addr = dm_pci_read_bar32(swap, 0); |
| 177 | outb(2, io_addr); |
| 178 | ut_asserteq(2, inb(io_addr)); |
| 179 | |
| 180 | /* |
| 181 | * Now test memory mapping - note we must unmap and remap to cause |
| 182 | * the swapcase emulation to see our data and response. |
| 183 | */ |
| 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"); |
| 187 | unmap_sysmem(ptr); |
| 188 | |
| 189 | ptr = map_sysmem(mem_addr, 30); |
| 190 | ut_asserteq_str("tHIS IS A tESt On StatiC", ptr); |
| 191 | unmap_sysmem(ptr); |
| 192 | |
| 193 | return 0; |
| 194 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 195 | DM_TEST(dm_test_pci_mixed, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Bin Meng | d74d312 | 2018-08-03 01:14:53 -0700 | [diff] [blame] | 196 | |
| 197 | /* Test looking up PCI capability and extended capability */ |
| 198 | static int dm_test_pci_cap(struct unit_test_state *uts) |
| 199 | { |
| 200 | struct udevice *bus, *swap; |
| 201 | int cap; |
| 202 | |
| 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)); |
| 205 | |
| 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); |
| 209 | |
| 210 | /* look up PCI_CAP_ID_PCIX */ |
| 211 | cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX); |
| 212 | ut_asserteq(0, cap); |
| 213 | |
Bin Meng | b59b369 | 2018-10-15 02:21:22 -0700 | [diff] [blame] | 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, |
| 216 | PCI_CAP_ID_MSIX); |
| 217 | ut_asserteq(PCI_CAP_ID_MSIX_OFFSET, cap); |
| 218 | |
| 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, |
| 221 | PCI_CAP_ID_VNDR); |
| 222 | ut_asserteq(0, cap); |
| 223 | |
Bin Meng | d74d312 | 2018-08-03 01:14:53 -0700 | [diff] [blame] | 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)); |
| 226 | |
| 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); |
| 230 | |
| 231 | /* look up PCI_EXT_CAP_ID_SRIOV */ |
| 232 | cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV); |
| 233 | ut_asserteq(0, cap); |
| 234 | |
Bin Meng | b59b369 | 2018-10-15 02:21:22 -0700 | [diff] [blame] | 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, |
| 237 | PCI_EXT_CAP_ID_DSN); |
| 238 | ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap); |
| 239 | |
| 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); |
| 243 | ut_asserteq(0, cap); |
| 244 | |
Bin Meng | d74d312 | 2018-08-03 01:14:53 -0700 | [diff] [blame] | 245 | return 0; |
| 246 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 247 | DM_TEST(dm_test_pci_cap, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Alex Marginean | f127443 | 2019-06-07 11:24:24 +0300 | [diff] [blame] | 248 | |
| 249 | /* Test looking up BARs in EA capability structure */ |
| 250 | static int dm_test_pci_ea(struct unit_test_state *uts) |
| 251 | { |
| 252 | struct udevice *bus, *swap; |
| 253 | void *bar; |
| 254 | int cap; |
| 255 | |
| 256 | /* |
| 257 | * use emulated device mapping function, we're not using real physical |
| 258 | * addresses in this test |
| 259 | */ |
| 260 | sandbox_set_enable_pci_map(true); |
| 261 | |
| 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)); |
| 264 | |
| 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); |
| 268 | |
| 269 | /* test swap case in BAR 1 */ |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 270 | bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE, 0); |
Alex Marginean | f127443 | 2019-06-07 11:24:24 +0300 | [diff] [blame] | 271 | ut_assertnonnull(bar); |
| 272 | *(int *)bar = 2; /* swap upper/lower */ |
| 273 | |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 274 | bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0, 0, PCI_REGION_TYPE, 0); |
Alex Marginean | f127443 | 2019-06-07 11:24:24 +0300 | [diff] [blame] | 275 | ut_assertnonnull(bar); |
| 276 | strcpy(bar, "ea TEST"); |
| 277 | unmap_sysmem(bar); |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 278 | bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_1, 0, 0, PCI_REGION_TYPE, 0); |
Alex Marginean | f127443 | 2019-06-07 11:24:24 +0300 | [diff] [blame] | 279 | ut_assertnonnull(bar); |
| 280 | ut_asserteq_str("EA test", bar); |
| 281 | |
| 282 | /* test magic values in BARs2, 4; BAR 3 is n/a */ |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 283 | bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_2, 0, 0, PCI_REGION_TYPE, 0); |
Alex Marginean | f127443 | 2019-06-07 11:24:24 +0300 | [diff] [blame] | 284 | ut_assertnonnull(bar); |
| 285 | ut_asserteq(PCI_EA_BAR2_MAGIC, *(u32 *)bar); |
| 286 | |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 287 | bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_3, 0, 0, PCI_REGION_TYPE, 0); |
Alex Marginean | f127443 | 2019-06-07 11:24:24 +0300 | [diff] [blame] | 288 | ut_assertnull(bar); |
| 289 | |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 290 | bar = dm_pci_map_bar(swap, PCI_BASE_ADDRESS_4, 0, 0, PCI_REGION_TYPE, 0); |
Alex Marginean | f127443 | 2019-06-07 11:24:24 +0300 | [diff] [blame] | 291 | ut_assertnonnull(bar); |
| 292 | ut_asserteq(PCI_EA_BAR4_MAGIC, *(u32 *)bar); |
| 293 | |
| 294 | return 0; |
| 295 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 296 | DM_TEST(dm_test_pci_ea, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 297 | |
| 298 | /* Test the dev_read_addr_pci() function */ |
| 299 | static int dm_test_pci_addr_flat(struct unit_test_state *uts) |
| 300 | { |
| 301 | struct udevice *swap1f, *swap1; |
| 302 | ulong io_addr, mem_addr; |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 303 | fdt_addr_t size; |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 304 | |
| 305 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f)); |
| 306 | io_addr = dm_pci_read_bar32(swap1f, 0); |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 307 | ut_asserteq(io_addr, dev_read_addr_pci(swap1f, &size)); |
| 308 | ut_asserteq(0, size); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * This device has both I/O and MEM spaces but the MEM space appears |
| 312 | * first |
| 313 | */ |
| 314 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1)); |
| 315 | mem_addr = dm_pci_read_bar32(swap1, 1); |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 316 | ut_asserteq(mem_addr, dev_read_addr_pci(swap1, &size)); |
| 317 | ut_asserteq(0, size); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 318 | |
| 319 | return 0; |
| 320 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 321 | DM_TEST(dm_test_pci_addr_flat, UTF_SCAN_PDATA | UTF_SCAN_FDT | |
| 322 | UTF_FLAT_TREE); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 323 | |
| 324 | /* |
| 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() |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 328 | * can be used for both flattree and livetree by removing the UTF_FLAT_TREE |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 329 | * flag above. |
| 330 | */ |
| 331 | static int dm_test_pci_addr_live(struct unit_test_state *uts) |
| 332 | { |
| 333 | struct udevice *swap1f, *swap1; |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 334 | fdt_size_t size; |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 335 | |
| 336 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap1f)); |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 337 | ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1f, &size)); |
| 338 | ut_asserteq(0, size); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 339 | |
| 340 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1, 0), &swap1)); |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 341 | ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr_pci(swap1, &size)); |
| 342 | ut_asserteq(0, size); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 343 | |
| 344 | return 0; |
| 345 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 346 | DM_TEST(dm_test_pci_addr_live, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_LIVE_TREE); |
Simon Glass | 6ad24f6 | 2020-07-07 13:12:10 -0600 | [diff] [blame] | 347 | |
| 348 | /* Test device_is_on_pci_bus() */ |
| 349 | static int dm_test_pci_on_bus(struct unit_test_state *uts) |
| 350 | { |
| 351 | struct udevice *dev; |
| 352 | |
| 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)); |
| 357 | |
| 358 | return 0; |
| 359 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 360 | DM_TEST(dm_test_pci_on_bus, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Suneel Garapati | 3ac3aec | 2019-10-19 17:10:20 -0700 | [diff] [blame] | 361 | |
| 362 | /* |
| 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 |
| 368 | * 0x3000.0000. |
| 369 | */ |
| 370 | static int dm_test_pci_region_multi(struct unit_test_state *uts) |
| 371 | { |
| 372 | struct udevice *dev; |
| 373 | ulong mem_addr; |
| 374 | |
| 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); |
| 379 | |
| 380 | return 0; |
| 381 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 382 | DM_TEST(dm_test_pci_region_multi, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 383 | |
| 384 | /* |
| 385 | * Test the translation of PCI bus addresses to physical addresses using the |
| 386 | * ranges from bus#1. |
| 387 | */ |
| 388 | static int dm_test_pci_bus_to_phys(struct unit_test_state *uts) |
| 389 | { |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 390 | unsigned long mask = PCI_REGION_TYPE; |
| 391 | unsigned long flags = PCI_REGION_MEM; |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 392 | struct udevice *dev; |
| 393 | phys_addr_t phys_addr; |
| 394 | |
| 395 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev)); |
| 396 | |
| 397 | /* Before any of the ranges. */ |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 398 | phys_addr = dm_pci_bus_to_phys(dev, 0x20000000, 0x400, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 399 | ut_asserteq(0, phys_addr); |
| 400 | |
| 401 | /* Identity range: whole, start, mid, end */ |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 402 | phys_addr = dm_pci_bus_to_phys(dev, 0x2ffff000, 0x2000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 403 | ut_asserteq(0, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 404 | phys_addr = dm_pci_bus_to_phys(dev, 0x30000000, 0x2000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 405 | ut_asserteq(0x30000000, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 406 | phys_addr = dm_pci_bus_to_phys(dev, 0x30000000, 0x1000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 407 | ut_asserteq(0x30000000, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 408 | phys_addr = dm_pci_bus_to_phys(dev, 0x30000abc, 0x12, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 409 | ut_asserteq(0x30000abc, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 410 | phys_addr = dm_pci_bus_to_phys(dev, 0x30000800, 0x1800, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 411 | ut_asserteq(0x30000800, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 412 | phys_addr = dm_pci_bus_to_phys(dev, 0x30008000, 0x1801, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 413 | ut_asserteq(0, phys_addr); |
| 414 | |
| 415 | /* Translated range: whole, start, mid, end */ |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 416 | phys_addr = dm_pci_bus_to_phys(dev, 0x30fff000, 0x2000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 417 | ut_asserteq(0, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 418 | phys_addr = dm_pci_bus_to_phys(dev, 0x31000000, 0x2000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 419 | ut_asserteq(0x3e000000, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 420 | phys_addr = dm_pci_bus_to_phys(dev, 0x31000000, 0x1000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 421 | ut_asserteq(0x3e000000, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 422 | phys_addr = dm_pci_bus_to_phys(dev, 0x31000abc, 0x12, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 423 | ut_asserteq(0x3e000abc, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 424 | phys_addr = dm_pci_bus_to_phys(dev, 0x31000800, 0x1800, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 425 | ut_asserteq(0x3e000800, phys_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 426 | phys_addr = dm_pci_bus_to_phys(dev, 0x31008000, 0x1801, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 427 | ut_asserteq(0, phys_addr); |
| 428 | |
| 429 | /* Beyond all of the ranges. */ |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 430 | phys_addr = dm_pci_bus_to_phys(dev, 0x32000000, 0x400, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 431 | ut_asserteq(0, phys_addr); |
| 432 | |
| 433 | return 0; |
| 434 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 435 | DM_TEST(dm_test_pci_bus_to_phys, UTF_SCAN_PDATA | UTF_SCAN_FDT); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Test the translation of physical addresses to PCI bus addresses using the |
| 439 | * ranges from bus#1. |
| 440 | */ |
| 441 | static int dm_test_pci_phys_to_bus(struct unit_test_state *uts) |
| 442 | { |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 443 | unsigned long mask = PCI_REGION_TYPE; |
| 444 | unsigned long flags = PCI_REGION_MEM; |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 445 | struct udevice *dev; |
| 446 | pci_addr_t pci_addr; |
| 447 | |
| 448 | ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &dev)); |
| 449 | |
| 450 | /* Before any of the ranges. */ |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 451 | pci_addr = dm_pci_phys_to_bus(dev, 0x20000000, 0x400, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 452 | ut_asserteq(0, pci_addr); |
| 453 | |
| 454 | /* Identity range: partial overlap, whole, start, mid, end */ |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 455 | pci_addr = dm_pci_phys_to_bus(dev, 0x2ffff000, 0x2000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 456 | ut_asserteq(0, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 457 | pci_addr = dm_pci_phys_to_bus(dev, 0x30000000, 0x2000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 458 | ut_asserteq(0x30000000, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 459 | pci_addr = dm_pci_phys_to_bus(dev, 0x30000000, 0x1000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 460 | ut_asserteq(0x30000000, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 461 | pci_addr = dm_pci_phys_to_bus(dev, 0x30000abc, 0x12, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 462 | ut_asserteq(0x30000abc, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 463 | pci_addr = dm_pci_phys_to_bus(dev, 0x30000800, 0x1800, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 464 | ut_asserteq(0x30000800, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 465 | pci_addr = dm_pci_phys_to_bus(dev, 0x30008000, 0x1801, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 466 | ut_asserteq(0, pci_addr); |
| 467 | |
| 468 | /* Translated range: partial overlap, whole, start, mid, end */ |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 469 | pci_addr = dm_pci_phys_to_bus(dev, 0x3dfff000, 0x2000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 470 | ut_asserteq(0, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 471 | pci_addr = dm_pci_phys_to_bus(dev, 0x3e000000, 0x2000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 472 | ut_asserteq(0x31000000, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 473 | pci_addr = dm_pci_phys_to_bus(dev, 0x3e000000, 0x1000, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 474 | ut_asserteq(0x31000000, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 475 | pci_addr = dm_pci_phys_to_bus(dev, 0x3e000abc, 0x12, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 476 | ut_asserteq(0x31000abc, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 477 | pci_addr = dm_pci_phys_to_bus(dev, 0x3e000800, 0x1800, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 478 | ut_asserteq(0x31000800, pci_addr); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 479 | pci_addr = dm_pci_phys_to_bus(dev, 0x3e008000, 0x1801, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 480 | ut_asserteq(0, pci_addr); |
| 481 | |
| 482 | /* Beyond all of the ranges. */ |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 483 | pci_addr = dm_pci_phys_to_bus(dev, 0x3f000000, 0x400, mask, flags); |
Andrew Scull | c7456a4 | 2022-04-21 16:11:09 +0000 | [diff] [blame] | 484 | ut_asserteq(0, pci_addr); |
| 485 | |
| 486 | return 0; |
| 487 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 488 | DM_TEST(dm_test_pci_phys_to_bus, UTF_SCAN_PDATA | UTF_SCAN_FDT); |