Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_PCI |
| 8 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 11 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 14 | #include <pci.h> |
Simon Glass | 797b8e8 | 2023-07-15 21:38:55 -0600 | [diff] [blame] | 15 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 17 | #include <asm/io.h> |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
Simon Glass | 89d8323 | 2017-05-18 20:09:51 -0600 | [diff] [blame] | 19 | #include <dm/lists.h> |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 20 | #include <dm/uclass-internal.h> |
Bin Meng | c0820a4 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 21 | #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) |
Simon Glass | ef8a2dd | 2019-08-24 14:19:05 -0600 | [diff] [blame] | 22 | #include <asm/fsp/fsp_support.h> |
Bin Meng | c0820a4 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 23 | #endif |
Simon Glass | 8807a56 | 2021-06-27 17:50:57 -0600 | [diff] [blame] | 24 | #include <dt-bindings/pci/pci.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 25 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 26 | #include <linux/printk.h> |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 27 | #include "pci_internal.h" |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 28 | |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Simon Glass | 2e4e443 | 2016-01-18 20:19:14 -0700 | [diff] [blame] | 31 | int pci_get_bus(int busnum, struct udevice **busp) |
Simon Glass | 7d07e59 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 32 | { |
| 33 | int ret; |
| 34 | |
| 35 | ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp); |
| 36 | |
| 37 | /* Since buses may not be numbered yet try a little harder with bus 0 */ |
| 38 | if (ret == -ENODEV) { |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 39 | ret = uclass_first_device_err(UCLASS_PCI, busp); |
Simon Glass | 7d07e59 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 40 | if (ret) |
| 41 | return ret; |
Simon Glass | 7d07e59 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 42 | ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp); |
| 43 | } |
| 44 | |
| 45 | return ret; |
| 46 | } |
| 47 | |
Simon Glass | 6256d67 | 2015-11-19 20:27:00 -0700 | [diff] [blame] | 48 | struct udevice *pci_get_controller(struct udevice *dev) |
| 49 | { |
| 50 | while (device_is_on_pci_bus(dev)) |
| 51 | dev = dev->parent; |
| 52 | |
| 53 | return dev; |
| 54 | } |
| 55 | |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 56 | pci_dev_t dm_pci_get_bdf(const struct udevice *dev) |
Simon Glass | c9118d4 | 2015-07-06 16:47:46 -0600 | [diff] [blame] | 57 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 58 | struct pci_child_plat *pplat = dev_get_parent_plat(dev); |
Simon Glass | c9118d4 | 2015-07-06 16:47:46 -0600 | [diff] [blame] | 59 | struct udevice *bus = dev->parent; |
| 60 | |
Simon Glass | 1c6449c | 2019-12-29 21:19:14 -0700 | [diff] [blame] | 61 | /* |
| 62 | * This error indicates that @dev is a device on an unprobed PCI bus. |
| 63 | * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below |
| 64 | * will produce a bad BDF> |
| 65 | * |
| 66 | * A common cause of this problem is that this function is called in the |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 67 | * of_to_plat() method of @dev. Accessing the PCI bus in that |
Simon Glass | 1c6449c | 2019-12-29 21:19:14 -0700 | [diff] [blame] | 68 | * method is not allowed, since it has not yet been probed. To fix this, |
| 69 | * move that access to the probe() method of @dev instead. |
| 70 | */ |
| 71 | if (!device_active(bus)) |
| 72 | log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name, |
| 73 | bus->name); |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 74 | return PCI_ADD_BUS(dev_seq(bus), pplat->devfn); |
Simon Glass | c9118d4 | 2015-07-06 16:47:46 -0600 | [diff] [blame] | 75 | } |
| 76 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 77 | /** |
| 78 | * pci_get_bus_max() - returns the bus number of the last active bus |
| 79 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 80 | * Return: last bus number, or -1 if no active buses |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 81 | */ |
| 82 | static int pci_get_bus_max(void) |
| 83 | { |
| 84 | struct udevice *bus; |
| 85 | struct uclass *uc; |
| 86 | int ret = -1; |
| 87 | |
| 88 | ret = uclass_get(UCLASS_PCI, &uc); |
| 89 | uclass_foreach_dev(bus, uc) { |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 90 | if (dev_seq(bus) > ret) |
| 91 | ret = dev_seq(bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | debug("%s: ret=%d\n", __func__, ret); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | int pci_last_busno(void) |
| 100 | { |
Bin Meng | 5bc3f8a | 2015-10-01 00:36:01 -0700 | [diff] [blame] | 101 | return pci_get_bus_max(); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int pci_get_ff(enum pci_size_t size) |
| 105 | { |
| 106 | switch (size) { |
| 107 | case PCI_SIZE_8: |
| 108 | return 0xff; |
| 109 | case PCI_SIZE_16: |
| 110 | return 0xffff; |
| 111 | default: |
| 112 | return 0xffffffff; |
| 113 | } |
| 114 | } |
| 115 | |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 116 | static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf, |
| 117 | ofnode *rnode) |
| 118 | { |
| 119 | struct fdt_pci_addr addr; |
| 120 | ofnode node; |
| 121 | int ret; |
| 122 | |
| 123 | dev_for_each_subnode(node, bus) { |
| 124 | ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg", |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 125 | &addr, NULL); |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 126 | if (ret) |
| 127 | continue; |
| 128 | |
| 129 | if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf)) |
| 130 | continue; |
| 131 | |
| 132 | *rnode = node; |
| 133 | break; |
| 134 | } |
| 135 | }; |
| 136 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 137 | int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 138 | struct udevice **devp) |
| 139 | { |
| 140 | struct udevice *dev; |
| 141 | |
| 142 | for (device_find_first_child(bus, &dev); |
| 143 | dev; |
| 144 | device_find_next_child(&dev)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 145 | struct pci_child_plat *pplat; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 146 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 147 | pplat = dev_get_parent_plat(dev); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 148 | if (pplat && pplat->devfn == find_devfn) { |
| 149 | *devp = dev; |
| 150 | return 0; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return -ENODEV; |
| 155 | } |
| 156 | |
Simon Glass | 84283d5 | 2015-11-29 13:17:48 -0700 | [diff] [blame] | 157 | int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 158 | { |
| 159 | struct udevice *bus; |
| 160 | int ret; |
| 161 | |
Simon Glass | 7d07e59 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 162 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 163 | if (ret) |
| 164 | return ret; |
| 165 | return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp); |
| 166 | } |
| 167 | |
| 168 | static int pci_device_matches_ids(struct udevice *dev, |
Simon Glass | 3f7dc6e | 2021-06-27 17:50:56 -0600 | [diff] [blame] | 169 | const struct pci_device_id *ids) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 170 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 171 | struct pci_child_plat *pplat; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 172 | int i; |
| 173 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 174 | pplat = dev_get_parent_plat(dev); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 175 | if (!pplat) |
| 176 | return -EINVAL; |
| 177 | for (i = 0; ids[i].vendor != 0; i++) { |
| 178 | if (pplat->vendor == ids[i].vendor && |
| 179 | pplat->device == ids[i].device) |
| 180 | return i; |
| 181 | } |
| 182 | |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
Simon Glass | 3f7dc6e | 2021-06-27 17:50:56 -0600 | [diff] [blame] | 186 | int pci_bus_find_devices(struct udevice *bus, const struct pci_device_id *ids, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 187 | int *indexp, struct udevice **devp) |
| 188 | { |
| 189 | struct udevice *dev; |
| 190 | |
| 191 | /* Scan all devices on this bus */ |
| 192 | for (device_find_first_child(bus, &dev); |
| 193 | dev; |
| 194 | device_find_next_child(&dev)) { |
| 195 | if (pci_device_matches_ids(dev, ids) >= 0) { |
| 196 | if ((*indexp)-- <= 0) { |
| 197 | *devp = dev; |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return -ENODEV; |
| 204 | } |
| 205 | |
Simon Glass | 3f7dc6e | 2021-06-27 17:50:56 -0600 | [diff] [blame] | 206 | int pci_find_device_id(const struct pci_device_id *ids, int index, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 207 | struct udevice **devp) |
| 208 | { |
| 209 | struct udevice *bus; |
| 210 | |
| 211 | /* Scan all known buses */ |
| 212 | for (uclass_first_device(UCLASS_PCI, &bus); |
| 213 | bus; |
| 214 | uclass_next_device(&bus)) { |
| 215 | if (!pci_bus_find_devices(bus, ids, &index, devp)) |
| 216 | return 0; |
| 217 | } |
| 218 | *devp = NULL; |
| 219 | |
| 220 | return -ENODEV; |
| 221 | } |
| 222 | |
Simon Glass | 70e0c58 | 2015-11-29 13:17:50 -0700 | [diff] [blame] | 223 | static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor, |
| 224 | unsigned int device, int *indexp, |
| 225 | struct udevice **devp) |
| 226 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 227 | struct pci_child_plat *pplat; |
Simon Glass | 70e0c58 | 2015-11-29 13:17:50 -0700 | [diff] [blame] | 228 | struct udevice *dev; |
| 229 | |
| 230 | for (device_find_first_child(bus, &dev); |
| 231 | dev; |
| 232 | device_find_next_child(&dev)) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 233 | pplat = dev_get_parent_plat(dev); |
Simon Glass | 70e0c58 | 2015-11-29 13:17:50 -0700 | [diff] [blame] | 234 | if (pplat->vendor == vendor && pplat->device == device) { |
| 235 | if (!(*indexp)--) { |
| 236 | *devp = dev; |
| 237 | return 0; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return -ENODEV; |
| 243 | } |
| 244 | |
| 245 | int dm_pci_find_device(unsigned int vendor, unsigned int device, int index, |
| 246 | struct udevice **devp) |
| 247 | { |
| 248 | struct udevice *bus; |
| 249 | |
| 250 | /* Scan all known buses */ |
| 251 | for (uclass_first_device(UCLASS_PCI, &bus); |
| 252 | bus; |
| 253 | uclass_next_device(&bus)) { |
| 254 | if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp)) |
| 255 | return device_probe(*devp); |
| 256 | } |
| 257 | *devp = NULL; |
| 258 | |
| 259 | return -ENODEV; |
| 260 | } |
| 261 | |
Simon Glass | b639d51 | 2015-11-29 13:17:52 -0700 | [diff] [blame] | 262 | int dm_pci_find_class(uint find_class, int index, struct udevice **devp) |
| 263 | { |
| 264 | struct udevice *dev; |
| 265 | |
| 266 | /* Scan all known buses */ |
| 267 | for (pci_find_first_device(&dev); |
| 268 | dev; |
| 269 | pci_find_next_device(&dev)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 270 | struct pci_child_plat *pplat = dev_get_parent_plat(dev); |
Simon Glass | b639d51 | 2015-11-29 13:17:52 -0700 | [diff] [blame] | 271 | |
| 272 | if (pplat->class == find_class && !index--) { |
| 273 | *devp = dev; |
| 274 | return device_probe(*devp); |
| 275 | } |
| 276 | } |
| 277 | *devp = NULL; |
| 278 | |
| 279 | return -ENODEV; |
| 280 | } |
| 281 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 282 | int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset, |
| 283 | unsigned long value, enum pci_size_t size) |
| 284 | { |
| 285 | struct dm_pci_ops *ops; |
| 286 | |
| 287 | ops = pci_get_ops(bus); |
| 288 | if (!ops->write_config) |
| 289 | return -ENOSYS; |
Pali RohĂĄr | bce6963 | 2022-07-03 12:48:06 +0200 | [diff] [blame] | 290 | if (offset < 0 || offset >= 4096) |
| 291 | return -EINVAL; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 292 | return ops->write_config(bus, bdf, offset, value, size); |
| 293 | } |
| 294 | |
Simon Glass | 9cec2df | 2016-03-06 19:27:52 -0700 | [diff] [blame] | 295 | int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset, |
| 296 | u32 clr, u32 set) |
| 297 | { |
| 298 | ulong val; |
| 299 | int ret; |
| 300 | |
| 301 | ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32); |
| 302 | if (ret) |
| 303 | return ret; |
| 304 | val &= ~clr; |
| 305 | val |= set; |
| 306 | |
| 307 | return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32); |
| 308 | } |
| 309 | |
Vladimir Oltean | 278a5b5 | 2021-09-17 15:11:25 +0300 | [diff] [blame] | 310 | static int pci_write_config(pci_dev_t bdf, int offset, unsigned long value, |
| 311 | enum pci_size_t size) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 312 | { |
| 313 | struct udevice *bus; |
| 314 | int ret; |
| 315 | |
Simon Glass | 7d07e59 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 316 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 317 | if (ret) |
| 318 | return ret; |
| 319 | |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 320 | return pci_bus_write_config(bus, bdf, offset, value, size); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 323 | int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value, |
| 324 | enum pci_size_t size) |
| 325 | { |
| 326 | struct udevice *bus; |
| 327 | |
Bin Meng | 05bedb1 | 2015-09-11 03:24:34 -0700 | [diff] [blame] | 328 | for (bus = dev; device_is_on_pci_bus(bus);) |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 329 | bus = bus->parent; |
Simon Glass | eaa1489 | 2015-11-29 13:17:47 -0700 | [diff] [blame] | 330 | return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value, |
| 331 | size); |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 332 | } |
| 333 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 334 | int pci_write_config32(pci_dev_t bdf, int offset, u32 value) |
| 335 | { |
| 336 | return pci_write_config(bdf, offset, value, PCI_SIZE_32); |
| 337 | } |
| 338 | |
| 339 | int pci_write_config16(pci_dev_t bdf, int offset, u16 value) |
| 340 | { |
| 341 | return pci_write_config(bdf, offset, value, PCI_SIZE_16); |
| 342 | } |
| 343 | |
| 344 | int pci_write_config8(pci_dev_t bdf, int offset, u8 value) |
| 345 | { |
| 346 | return pci_write_config(bdf, offset, value, PCI_SIZE_8); |
| 347 | } |
| 348 | |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 349 | int dm_pci_write_config8(struct udevice *dev, int offset, u8 value) |
| 350 | { |
| 351 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_8); |
| 352 | } |
| 353 | |
| 354 | int dm_pci_write_config16(struct udevice *dev, int offset, u16 value) |
| 355 | { |
| 356 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_16); |
| 357 | } |
| 358 | |
| 359 | int dm_pci_write_config32(struct udevice *dev, int offset, u32 value) |
| 360 | { |
| 361 | return dm_pci_write_config(dev, offset, value, PCI_SIZE_32); |
| 362 | } |
| 363 | |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 364 | int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 365 | unsigned long *valuep, enum pci_size_t size) |
| 366 | { |
| 367 | struct dm_pci_ops *ops; |
| 368 | |
| 369 | ops = pci_get_ops(bus); |
Pali RohĂĄr | bce6963 | 2022-07-03 12:48:06 +0200 | [diff] [blame] | 370 | if (!ops->read_config) { |
| 371 | *valuep = pci_conv_32_to_size(~0, offset, size); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 372 | return -ENOSYS; |
Pali RohĂĄr | bce6963 | 2022-07-03 12:48:06 +0200 | [diff] [blame] | 373 | } |
| 374 | if (offset < 0 || offset >= 4096) { |
| 375 | *valuep = pci_conv_32_to_size(0, offset, size); |
| 376 | return -EINVAL; |
| 377 | } |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 378 | return ops->read_config(bus, bdf, offset, valuep, size); |
| 379 | } |
| 380 | |
Vladimir Oltean | 69c5f8a | 2021-09-17 15:11:26 +0300 | [diff] [blame] | 381 | static int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep, |
| 382 | enum pci_size_t size) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 383 | { |
| 384 | struct udevice *bus; |
| 385 | int ret; |
| 386 | |
Simon Glass | 7d07e59 | 2015-08-31 18:55:35 -0600 | [diff] [blame] | 387 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 388 | if (ret) |
| 389 | return ret; |
| 390 | |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 391 | return pci_bus_read_config(bus, bdf, offset, valuep, size); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 394 | int dm_pci_read_config(const struct udevice *dev, int offset, |
| 395 | unsigned long *valuep, enum pci_size_t size) |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 396 | { |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 397 | const struct udevice *bus; |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 398 | |
Bin Meng | 05bedb1 | 2015-09-11 03:24:34 -0700 | [diff] [blame] | 399 | for (bus = dev; device_is_on_pci_bus(bus);) |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 400 | bus = bus->parent; |
Simon Glass | eaa1489 | 2015-11-29 13:17:47 -0700 | [diff] [blame] | 401 | return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep, |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 402 | size); |
| 403 | } |
| 404 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 405 | int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep) |
| 406 | { |
| 407 | unsigned long value; |
| 408 | int ret; |
| 409 | |
| 410 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32); |
| 411 | if (ret) |
| 412 | return ret; |
| 413 | *valuep = value; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep) |
| 419 | { |
| 420 | unsigned long value; |
| 421 | int ret; |
| 422 | |
| 423 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16); |
| 424 | if (ret) |
| 425 | return ret; |
| 426 | *valuep = value; |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep) |
| 432 | { |
| 433 | unsigned long value; |
| 434 | int ret; |
| 435 | |
| 436 | ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8); |
| 437 | if (ret) |
| 438 | return ret; |
| 439 | *valuep = value; |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 444 | int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep) |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 445 | { |
| 446 | unsigned long value; |
| 447 | int ret; |
| 448 | |
| 449 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8); |
| 450 | if (ret) |
| 451 | return ret; |
| 452 | *valuep = value; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 457 | int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep) |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 458 | { |
| 459 | unsigned long value; |
| 460 | int ret; |
| 461 | |
| 462 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16); |
| 463 | if (ret) |
| 464 | return ret; |
| 465 | *valuep = value; |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 470 | int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep) |
Simon Glass | 94ef242 | 2015-08-10 07:05:03 -0600 | [diff] [blame] | 471 | { |
| 472 | unsigned long value; |
| 473 | int ret; |
| 474 | |
| 475 | ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32); |
| 476 | if (ret) |
| 477 | return ret; |
| 478 | *valuep = value; |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
Simon Glass | 9cec2df | 2016-03-06 19:27:52 -0700 | [diff] [blame] | 483 | int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set) |
| 484 | { |
| 485 | u8 val; |
| 486 | int ret; |
| 487 | |
| 488 | ret = dm_pci_read_config8(dev, offset, &val); |
| 489 | if (ret) |
| 490 | return ret; |
| 491 | val &= ~clr; |
| 492 | val |= set; |
| 493 | |
| 494 | return dm_pci_write_config8(dev, offset, val); |
| 495 | } |
| 496 | |
| 497 | int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set) |
| 498 | { |
| 499 | u16 val; |
| 500 | int ret; |
| 501 | |
| 502 | ret = dm_pci_read_config16(dev, offset, &val); |
| 503 | if (ret) |
| 504 | return ret; |
| 505 | val &= ~clr; |
| 506 | val |= set; |
| 507 | |
| 508 | return dm_pci_write_config16(dev, offset, val); |
| 509 | } |
| 510 | |
| 511 | int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set) |
| 512 | { |
| 513 | u32 val; |
| 514 | int ret; |
| 515 | |
| 516 | ret = dm_pci_read_config32(dev, offset, &val); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | val &= ~clr; |
| 520 | val |= set; |
| 521 | |
| 522 | return dm_pci_write_config32(dev, offset, val); |
| 523 | } |
| 524 | |
Bin Meng | a070578 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 525 | static void set_vga_bridge_bits(struct udevice *dev) |
| 526 | { |
| 527 | struct udevice *parent = dev->parent; |
| 528 | u16 bc; |
| 529 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 530 | while (dev_seq(parent) != 0) { |
Bin Meng | a070578 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 531 | dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc); |
| 532 | bc |= PCI_BRIDGE_CTL_VGA; |
| 533 | dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc); |
| 534 | parent = parent->parent; |
| 535 | } |
| 536 | } |
| 537 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 538 | int pci_auto_config_devices(struct udevice *bus) |
| 539 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 540 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 541 | struct pci_child_plat *pplat; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 542 | unsigned int sub_bus; |
| 543 | struct udevice *dev; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 544 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 545 | sub_bus = dev_seq(bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 546 | debug("%s: start\n", __func__); |
| 547 | pciauto_config_init(hose); |
Marek Vasut | 8d3339a | 2023-07-16 17:53:24 +0200 | [diff] [blame] | 548 | for (device_find_first_child(bus, &dev); |
| 549 | dev; |
| 550 | device_find_next_child(&dev)) { |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 551 | unsigned int max_bus; |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 552 | int ret; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 553 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 554 | debug("%s: device %s\n", __func__, dev->name); |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 555 | if (dev_has_ofnode(dev) && |
Suneel Garapati | f8c8628 | 2020-05-04 21:25:25 -0700 | [diff] [blame] | 556 | dev_read_bool(dev, "pci,no-autoconfig")) |
Simon Glass | f3005fb | 2020-04-08 16:57:26 -0600 | [diff] [blame] | 557 | continue; |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 558 | ret = dm_pciauto_config_device(dev); |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 559 | if (ret < 0) |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 560 | return log_msg_ret("auto", ret); |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 561 | max_bus = ret; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 562 | sub_bus = max(sub_bus, max_bus); |
Bin Meng | a070578 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 563 | |
Masami Hiramatsu | 7ccdc67 | 2021-06-04 18:43:34 +0900 | [diff] [blame] | 564 | if (dev_get_parent(dev) == bus) |
| 565 | continue; |
| 566 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 567 | pplat = dev_get_parent_plat(dev); |
Bin Meng | a070578 | 2015-10-01 00:36:02 -0700 | [diff] [blame] | 568 | if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8)) |
| 569 | set_vga_bridge_bits(dev); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 570 | } |
Pali RohĂĄr | 8b79e08 | 2022-01-17 16:38:37 +0100 | [diff] [blame] | 571 | if (hose->last_busno < sub_bus) |
| 572 | hose->last_busno = sub_bus; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 573 | debug("%s: done\n", __func__); |
| 574 | |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 575 | return log_msg_ret("sub", sub_bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Tuomas Tynkkynen | 8cce4cf | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 578 | int pci_generic_mmap_write_config( |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 579 | const struct udevice *bus, |
| 580 | int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset, |
| 581 | void **addrp), |
Tuomas Tynkkynen | 8cce4cf | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 582 | pci_dev_t bdf, |
| 583 | uint offset, |
| 584 | ulong value, |
| 585 | enum pci_size_t size) |
| 586 | { |
| 587 | void *address; |
| 588 | |
| 589 | if (addr_f(bus, bdf, offset, &address) < 0) |
| 590 | return 0; |
| 591 | |
| 592 | switch (size) { |
| 593 | case PCI_SIZE_8: |
| 594 | writeb(value, address); |
| 595 | return 0; |
| 596 | case PCI_SIZE_16: |
| 597 | writew(value, address); |
| 598 | return 0; |
| 599 | case PCI_SIZE_32: |
| 600 | writel(value, address); |
| 601 | return 0; |
| 602 | default: |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | int pci_generic_mmap_read_config( |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 608 | const struct udevice *bus, |
| 609 | int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset, |
| 610 | void **addrp), |
Tuomas Tynkkynen | 8cce4cf | 2017-09-19 23:18:03 +0300 | [diff] [blame] | 611 | pci_dev_t bdf, |
| 612 | uint offset, |
| 613 | ulong *valuep, |
| 614 | enum pci_size_t size) |
| 615 | { |
| 616 | void *address; |
| 617 | |
| 618 | if (addr_f(bus, bdf, offset, &address) < 0) { |
| 619 | *valuep = pci_get_ff(size); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | switch (size) { |
| 624 | case PCI_SIZE_8: |
| 625 | *valuep = readb(address); |
| 626 | return 0; |
| 627 | case PCI_SIZE_16: |
| 628 | *valuep = readw(address); |
| 629 | return 0; |
| 630 | case PCI_SIZE_32: |
| 631 | *valuep = readl(address); |
| 632 | return 0; |
| 633 | default: |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | } |
| 637 | |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 638 | int dm_pci_hose_probe_bus(struct udevice *bus) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 639 | { |
Pali RohĂĄr | 4fb6599 | 2021-10-07 14:50:58 +0200 | [diff] [blame] | 640 | u8 header_type; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 641 | int sub_bus; |
| 642 | int ret; |
Suneel Garapati | 1b9c44e | 2019-10-19 15:52:32 -0700 | [diff] [blame] | 643 | int ea_pos; |
| 644 | u8 reg; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 645 | |
| 646 | debug("%s\n", __func__); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 647 | |
Pali RohĂĄr | 4fb6599 | 2021-10-07 14:50:58 +0200 | [diff] [blame] | 648 | dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type); |
| 649 | header_type &= 0x7f; |
| 650 | if (header_type != PCI_HEADER_TYPE_BRIDGE) { |
| 651 | debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n", |
| 652 | __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type); |
| 653 | return log_msg_ret("probe", -EINVAL); |
| 654 | } |
| 655 | |
Andrew Scull | 71e7e1a | 2022-04-21 16:11:16 +0000 | [diff] [blame] | 656 | if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION)) |
| 657 | ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA); |
| 658 | else |
| 659 | ea_pos = 0; |
| 660 | |
Suneel Garapati | 1b9c44e | 2019-10-19 15:52:32 -0700 | [diff] [blame] | 661 | if (ea_pos) { |
| 662 | dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8), |
| 663 | ®); |
| 664 | sub_bus = reg; |
| 665 | } else { |
| 666 | sub_bus = pci_get_bus_max() + 1; |
| 667 | } |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 668 | debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name); |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 669 | dm_pciauto_prescan_setup_bridge(bus, sub_bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 670 | |
| 671 | ret = device_probe(bus); |
| 672 | if (ret) { |
Simon Glass | 3b02d84 | 2015-09-08 17:52:48 -0600 | [diff] [blame] | 673 | debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 674 | ret); |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 675 | return log_msg_ret("probe", ret); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 676 | } |
Suneel Garapati | 1b9c44e | 2019-10-19 15:52:32 -0700 | [diff] [blame] | 677 | |
Masami Hiramatsu | ff02245 | 2021-04-16 14:53:46 -0700 | [diff] [blame] | 678 | if (!ea_pos) |
| 679 | sub_bus = pci_get_bus_max(); |
| 680 | |
Simon Glass | 37a3f94b | 2015-11-29 13:17:49 -0700 | [diff] [blame] | 681 | dm_pciauto_postscan_setup_bridge(bus, sub_bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 682 | |
| 683 | return sub_bus; |
| 684 | } |
| 685 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 686 | /** |
| 687 | * pci_match_one_device - Tell if a PCI device structure has a matching |
| 688 | * PCI device id structure |
| 689 | * @id: single PCI device id structure to match |
Hou Zhiqiang | d19d061 | 2017-03-22 16:07:24 +0800 | [diff] [blame] | 690 | * @find: the PCI device id structure to match against |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 691 | * |
Hou Zhiqiang | d19d061 | 2017-03-22 16:07:24 +0800 | [diff] [blame] | 692 | * Returns true if the finding pci_device_id structure matched or false if |
| 693 | * there is no match. |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 694 | */ |
| 695 | static bool pci_match_one_id(const struct pci_device_id *id, |
| 696 | const struct pci_device_id *find) |
| 697 | { |
| 698 | if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) && |
| 699 | (id->device == PCI_ANY_ID || id->device == find->device) && |
| 700 | (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) && |
| 701 | (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) && |
| 702 | !((id->class ^ find->class) & id->class_mask)) |
| 703 | return true; |
| 704 | |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | /** |
Simon Glass | 8807a56 | 2021-06-27 17:50:57 -0600 | [diff] [blame] | 709 | * pci_need_device_pre_reloc() - Check if a device should be bound |
| 710 | * |
| 711 | * This checks a list of vendor/device-ID values indicating devices that should |
| 712 | * be bound before relocation. |
| 713 | * |
| 714 | * @bus: Bus to check |
| 715 | * @vendor: Vendor ID to check |
| 716 | * @device: Device ID to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 717 | * Return: true if the vendor/device is in the list, false if not |
Simon Glass | 8807a56 | 2021-06-27 17:50:57 -0600 | [diff] [blame] | 718 | */ |
| 719 | static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor, |
| 720 | uint device) |
| 721 | { |
| 722 | u32 vendev; |
| 723 | int index; |
| 724 | |
Simon Glass | 797b8e8 | 2023-07-15 21:38:55 -0600 | [diff] [blame] | 725 | if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(PCI_PNP)) |
| 726 | return true; |
| 727 | |
Simon Glass | 8807a56 | 2021-06-27 17:50:57 -0600 | [diff] [blame] | 728 | for (index = 0; |
| 729 | !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index, |
| 730 | &vendev); |
| 731 | index++) { |
| 732 | if (vendev == PCI_VENDEV(vendor, device)) |
| 733 | return true; |
| 734 | } |
| 735 | |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | /** |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 740 | * pci_find_and_bind_driver() - Find and bind the right PCI driver |
| 741 | * |
| 742 | * This only looks at certain fields in the descriptor. |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 743 | * |
| 744 | * @parent: Parent bus |
| 745 | * @find_id: Specification of the driver to find |
| 746 | * @bdf: Bus/device/function addreess - see PCI_BDF() |
| 747 | * @devp: Returns a pointer to the device created |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 748 | * Return: 0 if OK, -EPERM if the device is not needed before relocation and |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 749 | * therefore was not created, other -ve value on error |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 750 | */ |
| 751 | static int pci_find_and_bind_driver(struct udevice *parent, |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 752 | struct pci_device_id *find_id, |
| 753 | pci_dev_t bdf, struct udevice **devp) |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 754 | { |
| 755 | struct pci_driver_entry *start, *entry; |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 756 | ofnode node = ofnode_null(); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 757 | const char *drv; |
| 758 | int n_ents; |
| 759 | int ret; |
| 760 | char name[30], *str; |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 761 | bool bridge; |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 762 | |
| 763 | *devp = NULL; |
| 764 | |
| 765 | debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__, |
| 766 | find_id->vendor, find_id->device); |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 767 | |
| 768 | /* Determine optional OF node */ |
Suneel Garapati | cb7093d | 2019-10-19 16:02:48 -0700 | [diff] [blame] | 769 | if (ofnode_valid(dev_ofnode(parent))) |
| 770 | pci_dev_find_ofnode(parent, bdf, &node); |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 771 | |
Simon Glass | 2e4938b | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 772 | if (ofnode_valid(node) && !ofnode_is_enabled(node)) { |
Michael Walle | 2e21f37 | 2019-12-01 17:45:18 +0100 | [diff] [blame] | 773 | debug("%s: Ignoring disabled device\n", __func__); |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 774 | return log_msg_ret("dis", -EPERM); |
Michael Walle | 2e21f37 | 2019-12-01 17:45:18 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 777 | start = ll_entry_start(struct pci_driver_entry, pci_driver_entry); |
| 778 | n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry); |
| 779 | for (entry = start; entry != start + n_ents; entry++) { |
| 780 | const struct pci_device_id *id; |
| 781 | struct udevice *dev; |
| 782 | const struct driver *drv; |
| 783 | |
| 784 | for (id = entry->match; |
| 785 | id->vendor || id->subvendor || id->class_mask; |
| 786 | id++) { |
| 787 | if (!pci_match_one_id(id, find_id)) |
| 788 | continue; |
| 789 | |
| 790 | drv = entry->driver; |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * In the pre-relocation phase, we only bind devices |
| 794 | * whose driver has the DM_FLAG_PRE_RELOC set, to save |
| 795 | * precious memory space as on some platforms as that |
| 796 | * space is pretty limited (ie: using Cache As RAM). |
| 797 | */ |
| 798 | if (!(gd->flags & GD_FLG_RELOC) && |
Simon Glass | 797b8e8 | 2023-07-15 21:38:55 -0600 | [diff] [blame] | 799 | !(drv->flags & DM_FLAG_PRE_RELOC) && |
| 800 | (!CONFIG_IS_ENABLED(PCI_PNP) || |
| 801 | spl_phase() != PHASE_SPL)) |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 802 | return log_msg_ret("pre", -EPERM); |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 803 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 804 | /* |
| 805 | * We could pass the descriptor to the driver as |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 806 | * plat (instead of NULL) and allow its bind() |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 807 | * method to return -ENOENT if it doesn't support this |
| 808 | * device. That way we could continue the search to |
| 809 | * find another driver. For now this doesn't seem |
| 810 | * necesssary, so just bind the first match. |
| 811 | */ |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 812 | ret = device_bind(parent, drv, drv->name, NULL, node, |
| 813 | &dev); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 814 | if (ret) |
| 815 | goto error; |
| 816 | debug("%s: Match found: %s\n", __func__, drv->name); |
Bin Meng | a8d2780 | 2018-08-03 01:14:44 -0700 | [diff] [blame] | 817 | dev->driver_data = id->driver_data; |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 818 | *devp = dev; |
| 819 | return 0; |
| 820 | } |
| 821 | } |
| 822 | |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 823 | bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI; |
| 824 | /* |
| 825 | * In the pre-relocation phase, we only bind bridge devices to save |
| 826 | * precious memory space as on some platforms as that space is pretty |
| 827 | * limited (ie: using Cache As RAM). |
| 828 | */ |
Simon Glass | 8807a56 | 2021-06-27 17:50:57 -0600 | [diff] [blame] | 829 | if (!(gd->flags & GD_FLG_RELOC) && !bridge && |
| 830 | !pci_need_device_pre_reloc(parent, find_id->vendor, |
| 831 | find_id->device)) |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 832 | return log_msg_ret("notbr", -EPERM); |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 833 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 834 | /* Bind a generic driver so that the device can be used */ |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 835 | sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf), |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 836 | PCI_FUNC(bdf)); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 837 | str = strdup(name); |
| 838 | if (!str) |
| 839 | return -ENOMEM; |
Bin Meng | 984c0dc | 2015-08-20 06:40:17 -0700 | [diff] [blame] | 840 | drv = bridge ? "pci_bridge_drv" : "pci_generic_drv"; |
| 841 | |
Marek Vasut | b453579 | 2018-10-10 21:27:06 +0200 | [diff] [blame] | 842 | ret = device_bind_driver_to_node(parent, drv, str, node, devp); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 843 | if (ret) { |
Simon Glass | 3b02d84 | 2015-09-08 17:52:48 -0600 | [diff] [blame] | 844 | debug("%s: Failed to bind generic driver: %d\n", __func__, ret); |
xypron.glpk@gmx.de | a89009c | 2017-05-08 20:40:16 +0200 | [diff] [blame] | 845 | free(str); |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | debug("%s: No match found: bound generic driver instead\n", __func__); |
| 849 | |
| 850 | return 0; |
| 851 | |
| 852 | error: |
| 853 | debug("%s: No match found: error %d\n", __func__, ret); |
| 854 | return ret; |
| 855 | } |
| 856 | |
Tim Harvey | 4c57bf7 | 2021-04-16 14:53:47 -0700 | [diff] [blame] | 857 | __weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev) |
| 858 | { |
| 859 | } |
| 860 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 861 | int pci_bind_bus_devices(struct udevice *bus) |
| 862 | { |
| 863 | ulong vendor, device; |
| 864 | ulong header_type; |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 865 | pci_dev_t bdf, end; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 866 | bool found_multi; |
Suneel Garapati | a99a5eb | 2019-10-23 18:40:36 -0700 | [diff] [blame] | 867 | int ari_off; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 868 | int ret; |
| 869 | |
| 870 | found_multi = false; |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 871 | end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1, |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 872 | PCI_MAX_PCI_FUNCTIONS - 1); |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 873 | for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end; |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 874 | bdf += PCI_BDF(0, 0, 1)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 875 | struct pci_child_plat *pplat; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 876 | struct udevice *dev; |
| 877 | ulong class; |
| 878 | |
Bin Meng | 20bdc1e | 2018-08-03 01:14:37 -0700 | [diff] [blame] | 879 | if (!PCI_FUNC(bdf)) |
| 880 | found_multi = false; |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 881 | if (PCI_FUNC(bdf) && !found_multi) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 882 | continue; |
Hou Zhiqiang | fb862b05 | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 883 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 884 | /* Check only the first access, we don't expect problems */ |
Hou Zhiqiang | fb862b05 | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 885 | ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor, |
| 886 | PCI_SIZE_16); |
Pali RohĂĄr | a8a520d | 2021-09-07 18:07:08 +0200 | [diff] [blame] | 887 | if (ret || vendor == 0xffff || vendor == 0x0000) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 888 | continue; |
| 889 | |
Hou Zhiqiang | fb862b05 | 2018-10-08 16:35:47 +0800 | [diff] [blame] | 890 | pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE, |
| 891 | &header_type, PCI_SIZE_8); |
| 892 | |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 893 | if (!PCI_FUNC(bdf)) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 894 | found_multi = header_type & 0x80; |
| 895 | |
Simon Glass | 25916d6 | 2019-09-25 08:56:12 -0600 | [diff] [blame] | 896 | debug("%s: bus %d/%s: found device %x, function %d", __func__, |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 897 | dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf)); |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 898 | pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 899 | PCI_SIZE_16); |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 900 | pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class, |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 901 | PCI_SIZE_32); |
| 902 | class >>= 8; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 903 | |
| 904 | /* Find this device in the device tree */ |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 905 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev); |
Simon Glass | 25916d6 | 2019-09-25 08:56:12 -0600 | [diff] [blame] | 906 | debug(": find ret=%d\n", ret); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 907 | |
Simon Glass | 413ebdb | 2015-11-29 13:18:09 -0700 | [diff] [blame] | 908 | /* If nothing in the device tree, bind a device */ |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 909 | if (ret == -ENODEV) { |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 910 | struct pci_device_id find_id; |
| 911 | ulong val; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 912 | |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 913 | memset(&find_id, '\0', sizeof(find_id)); |
| 914 | find_id.vendor = vendor; |
| 915 | find_id.device = device; |
| 916 | find_id.class = class; |
| 917 | if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) { |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 918 | pci_bus_read_config(bus, bdf, |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 919 | PCI_SUBSYSTEM_VENDOR_ID, |
| 920 | &val, PCI_SIZE_32); |
| 921 | find_id.subvendor = val & 0xffff; |
| 922 | find_id.subdevice = val >> 16; |
| 923 | } |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 924 | ret = pci_find_and_bind_driver(bus, &find_id, bdf, |
Simon Glass | 318d71c | 2015-07-06 16:47:44 -0600 | [diff] [blame] | 925 | &dev); |
Simon Glass | 797b8e8 | 2023-07-15 21:38:55 -0600 | [diff] [blame] | 926 | } else { |
| 927 | debug("device: %s\n", dev->name); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 928 | } |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 929 | if (ret == -EPERM) |
| 930 | continue; |
| 931 | else if (ret) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 932 | return ret; |
| 933 | |
| 934 | /* Update the platform data */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 935 | pplat = dev_get_parent_plat(dev); |
Simon Glass | c45abf1 | 2015-09-08 17:52:49 -0600 | [diff] [blame] | 936 | pplat->devfn = PCI_MASK_BUS(bdf); |
| 937 | pplat->vendor = vendor; |
| 938 | pplat->device = device; |
| 939 | pplat->class = class; |
Suneel Garapati | a99a5eb | 2019-10-23 18:40:36 -0700 | [diff] [blame] | 940 | |
| 941 | if (IS_ENABLED(CONFIG_PCI_ARID)) { |
| 942 | ari_off = dm_pci_find_ext_capability(dev, |
| 943 | PCI_EXT_CAP_ID_ARI); |
| 944 | if (ari_off) { |
| 945 | u16 ari_cap; |
| 946 | |
| 947 | /* |
| 948 | * Read Next Function number in ARI Cap |
| 949 | * Register |
| 950 | */ |
| 951 | dm_pci_read_config16(dev, ari_off + 4, |
| 952 | &ari_cap); |
| 953 | /* |
| 954 | * Update next scan on this function number, |
| 955 | * subtract 1 in BDF to satisfy loop increment. |
| 956 | */ |
| 957 | if (ari_cap & 0xff00) { |
| 958 | bdf = PCI_BDF(PCI_BUS(bdf), |
| 959 | PCI_DEV(ari_cap), |
| 960 | PCI_FUNC(ari_cap)); |
| 961 | bdf = bdf - 0x100; |
| 962 | } |
| 963 | } |
| 964 | } |
Tim Harvey | 4c57bf7 | 2021-04-16 14:53:47 -0700 | [diff] [blame] | 965 | |
| 966 | board_pci_fixup_dev(bus, dev); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | return 0; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Pierre-Clément Tosi | c84d830 | 2022-05-19 17:48:30 +0100 | [diff] [blame] | 972 | static int decode_regions(struct pci_controller *hose, ofnode parent_node, |
Christian Gmeiner | 5f4e094 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 973 | ofnode node) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 974 | { |
| 975 | int pci_addr_cells, addr_cells, size_cells; |
| 976 | int cells_per_record; |
Stefan Roese | bbc8846 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 977 | struct bd_info *bd; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 978 | const u32 *prop; |
Stefan Roese | 950864f | 2020-07-23 16:34:10 +0200 | [diff] [blame] | 979 | int max_regions; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 980 | int len; |
| 981 | int i; |
| 982 | |
Simon Glass | a5f9a61 | 2023-05-04 16:55:01 -0600 | [diff] [blame] | 983 | /* handle booting from coreboot, etc. */ |
| 984 | if (!ll_boot_init()) |
| 985 | return 0; |
| 986 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 987 | prop = ofnode_get_property(node, "ranges", &len); |
Christian Gmeiner | 5f4e094 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 988 | if (!prop) { |
| 989 | debug("%s: Cannot decode regions\n", __func__); |
Pierre-Clément Tosi | c84d830 | 2022-05-19 17:48:30 +0100 | [diff] [blame] | 990 | return -EINVAL; |
Christian Gmeiner | 5f4e094 | 2018-06-10 06:25:05 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 993 | pci_addr_cells = ofnode_read_simple_addr_cells(node); |
| 994 | addr_cells = ofnode_read_simple_addr_cells(parent_node); |
| 995 | size_cells = ofnode_read_simple_size_cells(node); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 996 | |
| 997 | /* PCI addresses are always 3-cells */ |
| 998 | len /= sizeof(u32); |
| 999 | cells_per_record = pci_addr_cells + addr_cells + size_cells; |
| 1000 | hose->region_count = 0; |
| 1001 | debug("%s: len=%d, cells_per_record=%d\n", __func__, len, |
| 1002 | cells_per_record); |
Stefan Roese | 950864f | 2020-07-23 16:34:10 +0200 | [diff] [blame] | 1003 | |
| 1004 | /* Dynamically allocate the regions array */ |
| 1005 | max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS; |
| 1006 | hose->regions = (struct pci_region *) |
| 1007 | calloc(1, max_regions * sizeof(struct pci_region)); |
Pierre-Clément Tosi | c84d830 | 2022-05-19 17:48:30 +0100 | [diff] [blame] | 1008 | if (!hose->regions) |
| 1009 | return -ENOMEM; |
Stefan Roese | 950864f | 2020-07-23 16:34:10 +0200 | [diff] [blame] | 1010 | |
| 1011 | for (i = 0; i < max_regions; i++, len -= cells_per_record) { |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1012 | u64 pci_addr, addr, size; |
| 1013 | int space_code; |
| 1014 | u32 flags; |
| 1015 | int type; |
Simon Glass | 7efc9ba | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 1016 | int pos; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1017 | |
| 1018 | if (len < cells_per_record) |
| 1019 | break; |
| 1020 | flags = fdt32_to_cpu(prop[0]); |
| 1021 | space_code = (flags >> 24) & 3; |
| 1022 | pci_addr = fdtdec_get_number(prop + 1, 2); |
| 1023 | prop += pci_addr_cells; |
| 1024 | addr = fdtdec_get_number(prop, addr_cells); |
| 1025 | prop += addr_cells; |
| 1026 | size = fdtdec_get_number(prop, size_cells); |
| 1027 | prop += size_cells; |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 1028 | debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n", |
| 1029 | __func__, hose->region_count, pci_addr, addr, size, space_code); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1030 | if (space_code & 2) { |
| 1031 | type = flags & (1U << 30) ? PCI_REGION_PREFETCH : |
| 1032 | PCI_REGION_MEM; |
| 1033 | } else if (space_code & 1) { |
| 1034 | type = PCI_REGION_IO; |
| 1035 | } else { |
| 1036 | continue; |
| 1037 | } |
Tuomas Tynkkynen | c307e17 | 2018-05-14 18:47:50 +0300 | [diff] [blame] | 1038 | |
| 1039 | if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) && |
| 1040 | type == PCI_REGION_MEM && upper_32_bits(pci_addr)) { |
Andrew Scull | cb06f0e | 2022-04-21 16:11:07 +0000 | [diff] [blame] | 1041 | debug(" - pci_addr beyond the 32-bit boundary, ignoring\n"); |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | if (!IS_ENABLED(CONFIG_PHYS_64BIT) && upper_32_bits(addr)) { |
| 1046 | debug(" - addr beyond the 32-bit boundary, ignoring\n"); |
| 1047 | continue; |
| 1048 | } |
| 1049 | |
| 1050 | if (~((pci_addr_t)0) - pci_addr < size) { |
| 1051 | debug(" - PCI range exceeds max address, ignoring\n"); |
| 1052 | continue; |
| 1053 | } |
| 1054 | |
| 1055 | if (~((phys_addr_t)0) - addr < size) { |
| 1056 | debug(" - phys range exceeds max address, ignoring\n"); |
Tuomas Tynkkynen | c307e17 | 2018-05-14 18:47:50 +0300 | [diff] [blame] | 1057 | continue; |
| 1058 | } |
| 1059 | |
Simon Glass | 7efc9ba | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 1060 | pos = -1; |
Suneel Garapati | 3ac3aec | 2019-10-19 17:10:20 -0700 | [diff] [blame] | 1061 | if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) { |
| 1062 | for (i = 0; i < hose->region_count; i++) { |
| 1063 | if (hose->regions[i].flags == type) |
| 1064 | pos = i; |
| 1065 | } |
Simon Glass | 7efc9ba | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 1066 | } |
Suneel Garapati | 3ac3aec | 2019-10-19 17:10:20 -0700 | [diff] [blame] | 1067 | |
Simon Glass | 7efc9ba | 2015-11-19 20:26:58 -0700 | [diff] [blame] | 1068 | if (pos == -1) |
| 1069 | pos = hose->region_count++; |
| 1070 | debug(" - type=%d, pos=%d\n", type, pos); |
| 1071 | pci_set_region(hose->regions + pos, pci_addr, addr, size, type); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /* Add a region for our local memory */ |
Stefan Roese | bbc8846 | 2020-08-12 11:55:46 +0200 | [diff] [blame] | 1075 | bd = gd->bd; |
Bin Meng | ae0bdde | 2018-03-27 00:46:05 -0700 | [diff] [blame] | 1076 | if (!bd) |
Pierre-Clément Tosi | c84d830 | 2022-05-19 17:48:30 +0100 | [diff] [blame] | 1077 | return 0; |
Bin Meng | ae0bdde | 2018-03-27 00:46:05 -0700 | [diff] [blame] | 1078 | |
Bernhard Messerklinger | 9c5df38 | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 1079 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) { |
| 1080 | if (bd->bi_dram[i].size) { |
Daniel Schwierzeck | f59925e | 2021-07-15 20:53:56 +0200 | [diff] [blame] | 1081 | phys_addr_t start = bd->bi_dram[i].start; |
| 1082 | |
| 1083 | if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY)) |
| 1084 | start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start); |
| 1085 | |
Bernhard Messerklinger | 9c5df38 | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 1086 | pci_set_region(hose->regions + hose->region_count++, |
Daniel Schwierzeck | f59925e | 2021-07-15 20:53:56 +0200 | [diff] [blame] | 1087 | start, start, bd->bi_dram[i].size, |
Bernhard Messerklinger | 9c5df38 | 2018-02-15 08:59:53 +0100 | [diff] [blame] | 1088 | PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
| 1089 | } |
| 1090 | } |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1091 | |
Pierre-Clément Tosi | c84d830 | 2022-05-19 17:48:30 +0100 | [diff] [blame] | 1092 | return 0; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static int pci_uclass_pre_probe(struct udevice *bus) |
| 1096 | { |
| 1097 | struct pci_controller *hose; |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1098 | struct uclass *uc; |
| 1099 | int ret; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1100 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1101 | debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1102 | bus->parent->name); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1103 | hose = dev_get_uclass_priv(bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1104 | |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1105 | /* |
| 1106 | * Set the sequence number, if device_bind() doesn't. We want control |
| 1107 | * of this so that numbers are allocated as devices are probed. That |
| 1108 | * ensures that sub-bus numbered is correct (sub-buses must get numbers |
| 1109 | * higher than their parents) |
| 1110 | */ |
| 1111 | if (dev_seq(bus) == -1) { |
| 1112 | ret = uclass_get(UCLASS_PCI, &uc); |
| 1113 | if (ret) |
| 1114 | return ret; |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 1115 | bus->seq_ = uclass_find_next_free_seq(uc); |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1118 | /* For bridges, use the top-level PCI controller */ |
Paul Burton | e3b106d | 2016-09-08 07:47:32 +0100 | [diff] [blame] | 1119 | if (!device_is_on_pci_bus(bus)) { |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1120 | hose->ctlr = bus; |
Pierre-Clément Tosi | c84d830 | 2022-05-19 17:48:30 +0100 | [diff] [blame] | 1121 | ret = decode_regions(hose, dev_ofnode(bus->parent), |
| 1122 | dev_ofnode(bus)); |
| 1123 | if (ret) |
| 1124 | return ret; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1125 | } else { |
| 1126 | struct pci_controller *parent_hose; |
| 1127 | |
| 1128 | parent_hose = dev_get_uclass_priv(bus->parent); |
| 1129 | hose->ctlr = parent_hose->bus; |
| 1130 | } |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1131 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1132 | hose->bus = bus; |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1133 | hose->first_busno = dev_seq(bus); |
| 1134 | hose->last_busno = dev_seq(bus); |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 1135 | if (dev_has_ofnode(bus)) { |
Suneel Garapati | f8c8628 | 2020-05-04 21:25:25 -0700 | [diff] [blame] | 1136 | hose->skip_auto_config_until_reloc = |
| 1137 | dev_read_bool(bus, |
| 1138 | "u-boot,skip-auto-config-until-reloc"); |
| 1139 | } |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1140 | |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
| 1144 | static int pci_uclass_post_probe(struct udevice *bus) |
| 1145 | { |
Simon Glass | 68e35a7 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 1146 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1147 | int ret; |
| 1148 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1149 | debug("%s: probing bus %d\n", __func__, dev_seq(bus)); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1150 | ret = pci_bind_bus_devices(bus); |
| 1151 | if (ret) |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1152 | return log_msg_ret("bind", ret); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1153 | |
Simon Glass | bd165e7 | 2020-04-26 09:12:56 -0600 | [diff] [blame] | 1154 | if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() && |
Simon Glass | 68e35a7 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 1155 | (!hose->skip_auto_config_until_reloc || |
| 1156 | (gd->flags & GD_FLG_RELOC))) { |
| 1157 | ret = pci_auto_config_devices(bus); |
| 1158 | if (ret < 0) |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1159 | return log_msg_ret("cfg", ret); |
Simon Glass | 68e35a7 | 2019-12-06 21:41:37 -0700 | [diff] [blame] | 1160 | } |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1161 | |
Bin Meng | c0820a4 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1162 | #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP) |
| 1163 | /* |
| 1164 | * Per Intel FSP specification, we should call FSP notify API to |
| 1165 | * inform FSP that PCI enumeration has been done so that FSP will |
| 1166 | * do any necessary initialization as required by the chipset's |
| 1167 | * BIOS Writer's Guide (BWG). |
| 1168 | * |
| 1169 | * Unfortunately we have to put this call here as with driver model, |
| 1170 | * the enumeration is all done on a lazy basis as needed, so until |
| 1171 | * something is touched on PCI it won't happen. |
| 1172 | * |
| 1173 | * Note we only call this 1) after U-Boot is relocated, and 2) |
| 1174 | * root bus has finished probing. |
| 1175 | */ |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1176 | if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) { |
Bin Meng | c0820a4 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1177 | ret = fsp_init_phase_pci(); |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 1178 | if (ret) |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1179 | return log_msg_ret("fsp", ret); |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 1180 | } |
Bin Meng | c0820a4 | 2015-08-20 06:40:23 -0700 | [diff] [blame] | 1181 | #endif |
| 1182 | |
Simon Glass | b072d52 | 2015-09-08 17:52:47 -0600 | [diff] [blame] | 1183 | return 0; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static int pci_uclass_child_post_bind(struct udevice *dev) |
| 1187 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1188 | struct pci_child_plat *pplat; |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1189 | |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 1190 | if (!dev_has_ofnode(dev)) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1191 | return 0; |
| 1192 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1193 | pplat = dev_get_parent_plat(dev); |
Bin Meng | 00d808e | 2018-08-03 01:14:36 -0700 | [diff] [blame] | 1194 | |
| 1195 | /* Extract vendor id and device id if available */ |
| 1196 | ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device); |
| 1197 | |
| 1198 | /* Extract the devfn from fdt_pci_addr */ |
Stefan Roese | a74eb55 | 2019-01-25 11:52:42 +0100 | [diff] [blame] | 1199 | pplat->devfn = pci_get_devfn(dev); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 1204 | static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf, |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 1205 | uint offset, ulong *valuep, |
| 1206 | enum pci_size_t size) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1207 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1208 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1209 | |
| 1210 | return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size); |
| 1211 | } |
| 1212 | |
Bin Meng | 0a72152 | 2015-07-19 00:20:04 +0800 | [diff] [blame] | 1213 | static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf, |
| 1214 | uint offset, ulong value, |
| 1215 | enum pci_size_t size) |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1216 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1217 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1218 | |
| 1219 | return pci_bus_write_config(hose->ctlr, bdf, offset, value, size); |
| 1220 | } |
| 1221 | |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1222 | static int skip_to_next_device(struct udevice *bus, struct udevice **devp) |
| 1223 | { |
| 1224 | struct udevice *dev; |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1225 | |
| 1226 | /* |
| 1227 | * Scan through all the PCI controllers. On x86 there will only be one |
| 1228 | * but that is not necessarily true on other hardware. |
| 1229 | */ |
Michal Suchanek | fae28a3 | 2022-10-12 21:57:52 +0200 | [diff] [blame] | 1230 | while (bus) { |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1231 | device_find_first_child(bus, &dev); |
| 1232 | if (dev) { |
| 1233 | *devp = dev; |
| 1234 | return 0; |
| 1235 | } |
Michal Suchanek | 91c96fe | 2022-10-12 21:58:08 +0200 | [diff] [blame] | 1236 | uclass_next_device(&bus); |
Michal Suchanek | fae28a3 | 2022-10-12 21:57:52 +0200 | [diff] [blame] | 1237 | } |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1238 | |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | int pci_find_next_device(struct udevice **devp) |
| 1243 | { |
| 1244 | struct udevice *child = *devp; |
| 1245 | struct udevice *bus = child->parent; |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1246 | |
| 1247 | /* First try all the siblings */ |
| 1248 | *devp = NULL; |
| 1249 | while (child) { |
| 1250 | device_find_next_child(&child); |
| 1251 | if (child) { |
| 1252 | *devp = child; |
| 1253 | return 0; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | /* We ran out of siblings. Try the next bus */ |
Michal Suchanek | 91c96fe | 2022-10-12 21:58:08 +0200 | [diff] [blame] | 1258 | uclass_next_device(&bus); |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1259 | |
| 1260 | return bus ? skip_to_next_device(bus, devp) : 0; |
| 1261 | } |
| 1262 | |
| 1263 | int pci_find_first_device(struct udevice **devp) |
| 1264 | { |
| 1265 | struct udevice *bus; |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1266 | |
| 1267 | *devp = NULL; |
Michal Suchanek | 91c96fe | 2022-10-12 21:58:08 +0200 | [diff] [blame] | 1268 | uclass_first_device(UCLASS_PCI, &bus); |
Simon Glass | 04c8b6a | 2015-08-10 07:05:04 -0600 | [diff] [blame] | 1269 | |
| 1270 | return skip_to_next_device(bus, devp); |
| 1271 | } |
| 1272 | |
Simon Glass | 27a733f | 2015-11-19 20:26:59 -0700 | [diff] [blame] | 1273 | ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size) |
| 1274 | { |
| 1275 | switch (size) { |
| 1276 | case PCI_SIZE_8: |
| 1277 | return (value >> ((offset & 3) * 8)) & 0xff; |
| 1278 | case PCI_SIZE_16: |
| 1279 | return (value >> ((offset & 2) * 8)) & 0xffff; |
| 1280 | default: |
| 1281 | return value; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | ulong pci_conv_size_to_32(ulong old, ulong value, uint offset, |
| 1286 | enum pci_size_t size) |
| 1287 | { |
| 1288 | uint off_mask; |
| 1289 | uint val_mask, shift; |
| 1290 | ulong ldata, mask; |
| 1291 | |
| 1292 | switch (size) { |
| 1293 | case PCI_SIZE_8: |
| 1294 | off_mask = 3; |
| 1295 | val_mask = 0xff; |
| 1296 | break; |
| 1297 | case PCI_SIZE_16: |
| 1298 | off_mask = 2; |
| 1299 | val_mask = 0xffff; |
| 1300 | break; |
| 1301 | default: |
| 1302 | return value; |
| 1303 | } |
| 1304 | shift = (offset & off_mask) * 8; |
| 1305 | ldata = (value & val_mask) << shift; |
| 1306 | mask = val_mask << shift; |
| 1307 | value = (old & ~mask) | ldata; |
| 1308 | |
| 1309 | return value; |
| 1310 | } |
| 1311 | |
Rayagonda Kokatanur | cdc7ed3 | 2020-05-12 13:29:49 +0530 | [diff] [blame] | 1312 | int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index) |
| 1313 | { |
| 1314 | int pci_addr_cells, addr_cells, size_cells; |
| 1315 | int cells_per_record; |
| 1316 | const u32 *prop; |
| 1317 | int len; |
| 1318 | int i = 0; |
| 1319 | |
| 1320 | prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len); |
| 1321 | if (!prop) { |
| 1322 | log_err("PCI: Device '%s': Cannot decode dma-ranges\n", |
| 1323 | dev->name); |
| 1324 | return -EINVAL; |
| 1325 | } |
| 1326 | |
| 1327 | pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev)); |
| 1328 | addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent)); |
| 1329 | size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev)); |
| 1330 | |
| 1331 | /* PCI addresses are always 3-cells */ |
| 1332 | len /= sizeof(u32); |
| 1333 | cells_per_record = pci_addr_cells + addr_cells + size_cells; |
| 1334 | debug("%s: len=%d, cells_per_record=%d\n", __func__, len, |
| 1335 | cells_per_record); |
| 1336 | |
| 1337 | while (len) { |
| 1338 | memp->bus_start = fdtdec_get_number(prop + 1, 2); |
| 1339 | prop += pci_addr_cells; |
| 1340 | memp->phys_start = fdtdec_get_number(prop, addr_cells); |
| 1341 | prop += addr_cells; |
| 1342 | memp->size = fdtdec_get_number(prop, size_cells); |
| 1343 | prop += size_cells; |
| 1344 | |
| 1345 | if (i == index) |
| 1346 | return 0; |
| 1347 | i++; |
| 1348 | len -= cells_per_record; |
| 1349 | } |
| 1350 | |
| 1351 | return -EINVAL; |
| 1352 | } |
| 1353 | |
Simon Glass | dcdc012 | 2015-11-19 20:27:01 -0700 | [diff] [blame] | 1354 | int pci_get_regions(struct udevice *dev, struct pci_region **iop, |
| 1355 | struct pci_region **memp, struct pci_region **prefp) |
| 1356 | { |
| 1357 | struct udevice *bus = pci_get_controller(dev); |
| 1358 | struct pci_controller *hose = dev_get_uclass_priv(bus); |
| 1359 | int i; |
| 1360 | |
| 1361 | *iop = NULL; |
| 1362 | *memp = NULL; |
| 1363 | *prefp = NULL; |
| 1364 | for (i = 0; i < hose->region_count; i++) { |
| 1365 | switch (hose->regions[i].flags) { |
| 1366 | case PCI_REGION_IO: |
| 1367 | if (!*iop || (*iop)->size < hose->regions[i].size) |
| 1368 | *iop = hose->regions + i; |
| 1369 | break; |
| 1370 | case PCI_REGION_MEM: |
| 1371 | if (!*memp || (*memp)->size < hose->regions[i].size) |
| 1372 | *memp = hose->regions + i; |
| 1373 | break; |
| 1374 | case (PCI_REGION_MEM | PCI_REGION_PREFETCH): |
| 1375 | if (!*prefp || (*prefp)->size < hose->regions[i].size) |
| 1376 | *prefp = hose->regions + i; |
| 1377 | break; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL); |
| 1382 | } |
| 1383 | |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 1384 | u32 dm_pci_read_bar32(const struct udevice *dev, int barnum) |
Simon Glass | 3452cb1 | 2015-11-29 13:17:53 -0700 | [diff] [blame] | 1385 | { |
| 1386 | u32 addr; |
| 1387 | int bar; |
| 1388 | |
| 1389 | bar = PCI_BASE_ADDRESS_0 + barnum * 4; |
| 1390 | dm_pci_read_config32(dev, bar, &addr); |
Simon Glass | 71fafd1 | 2020-04-09 10:27:36 -0600 | [diff] [blame] | 1391 | |
| 1392 | /* |
| 1393 | * If we get an invalid address, return this so that comparisons with |
| 1394 | * FDT_ADDR_T_NONE work correctly |
| 1395 | */ |
| 1396 | if (addr == 0xffffffff) |
| 1397 | return addr; |
| 1398 | else if (addr & PCI_BASE_ADDRESS_SPACE_IO) |
Simon Glass | 3452cb1 | 2015-11-29 13:17:53 -0700 | [diff] [blame] | 1399 | return addr & PCI_BASE_ADDRESS_IO_MASK; |
| 1400 | else |
| 1401 | return addr & PCI_BASE_ADDRESS_MEM_MASK; |
| 1402 | } |
| 1403 | |
Simon Glass | e2b6b56 | 2016-01-18 20:19:15 -0700 | [diff] [blame] | 1404 | void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr) |
| 1405 | { |
| 1406 | int bar; |
| 1407 | |
| 1408 | bar = PCI_BASE_ADDRESS_0 + barnum * 4; |
| 1409 | dm_pci_write_config32(dev, bar, addr); |
| 1410 | } |
| 1411 | |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1412 | phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr, |
| 1413 | size_t len, unsigned long mask, |
| 1414 | unsigned long flags) |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1415 | { |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1416 | struct udevice *ctlr; |
| 1417 | struct pci_controller *hose; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1418 | struct pci_region *res; |
Andrew Scull | 3bf6152 | 2022-04-21 16:11:08 +0000 | [diff] [blame] | 1419 | pci_addr_t offset; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1420 | int i; |
| 1421 | |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1422 | /* The root controller has the region information */ |
| 1423 | ctlr = pci_get_controller(dev); |
| 1424 | hose = dev_get_uclass_priv(ctlr); |
| 1425 | |
| 1426 | if (hose->region_count == 0) |
| 1427 | return bus_addr; |
Christian Gmeiner | 7241f80 | 2018-06-10 06:25:06 -0700 | [diff] [blame] | 1428 | |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1429 | for (i = 0; i < hose->region_count; i++) { |
| 1430 | res = &hose->regions[i]; |
| 1431 | |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1432 | if ((res->flags & mask) != flags) |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1433 | continue; |
| 1434 | |
Andrew Scull | 3bf6152 | 2022-04-21 16:11:08 +0000 | [diff] [blame] | 1435 | if (bus_addr < res->bus_start) |
| 1436 | continue; |
| 1437 | |
| 1438 | offset = bus_addr - res->bus_start; |
| 1439 | if (offset >= res->size) |
| 1440 | continue; |
| 1441 | |
| 1442 | if (len > res->size - offset) |
| 1443 | continue; |
| 1444 | |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1445 | return res->phys_start + offset; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Heinrich Schuchardt | d12961b | 2023-07-27 18:50:14 +0200 | [diff] [blame] | 1448 | puts("dm_pci_bus_to_phys: invalid physical address\n"); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1449 | return 0; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1452 | pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr, |
| 1453 | size_t len, unsigned long mask, |
| 1454 | unsigned long flags) |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1455 | { |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1456 | struct udevice *ctlr; |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1457 | struct pci_controller *hose; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1458 | struct pci_region *res; |
Andrew Scull | 3bf6152 | 2022-04-21 16:11:08 +0000 | [diff] [blame] | 1459 | phys_addr_t offset; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1460 | int i; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1461 | |
| 1462 | /* The root controller has the region information */ |
| 1463 | ctlr = pci_get_controller(dev); |
| 1464 | hose = dev_get_uclass_priv(ctlr); |
| 1465 | |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1466 | if (hose->region_count == 0) |
| 1467 | return phys_addr; |
Christian Gmeiner | 7241f80 | 2018-06-10 06:25:06 -0700 | [diff] [blame] | 1468 | |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1469 | for (i = 0; i < hose->region_count; i++) { |
| 1470 | res = &hose->regions[i]; |
| 1471 | |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1472 | if ((res->flags & mask) != flags) |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1473 | continue; |
| 1474 | |
Andrew Scull | 3bf6152 | 2022-04-21 16:11:08 +0000 | [diff] [blame] | 1475 | if (phys_addr < res->phys_start) |
| 1476 | continue; |
| 1477 | |
| 1478 | offset = phys_addr - res->phys_start; |
| 1479 | if (offset >= res->size) |
| 1480 | continue; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1481 | |
Andrew Scull | 3bf6152 | 2022-04-21 16:11:08 +0000 | [diff] [blame] | 1482 | if (len > res->size - offset) |
| 1483 | continue; |
| 1484 | |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1485 | return res->bus_start + offset; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Heinrich Schuchardt | d12961b | 2023-07-27 18:50:14 +0200 | [diff] [blame] | 1488 | puts("dm_pci_phys_to_bus: invalid physical address\n"); |
Andrew Scull | 994b60d | 2022-04-21 16:11:11 +0000 | [diff] [blame] | 1489 | return 0; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Suneel Garapati | 5858ba8 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1492 | static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1493 | struct pci_child_plat *pdata) |
Suneel Garapati | 5858ba8 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1494 | { |
| 1495 | phys_addr_t addr = 0; |
| 1496 | |
| 1497 | /* |
| 1498 | * In the case of a Virtual Function device using BAR |
| 1499 | * base and size, add offset for VFn BAR(1, 2, 3...n) |
| 1500 | */ |
| 1501 | if (pdata->is_virtfn) { |
| 1502 | size_t sz; |
| 1503 | u32 ea_entry; |
| 1504 | |
| 1505 | /* MaxOffset, 1st DW */ |
| 1506 | dm_pci_read_config32(dev, ea_off + 8, &ea_entry); |
| 1507 | sz = ea_entry & PCI_EA_FIELD_MASK; |
| 1508 | /* Fill up lower 2 bits */ |
| 1509 | sz |= (~PCI_EA_FIELD_MASK); |
| 1510 | |
| 1511 | if (ea_entry & PCI_EA_IS_64) { |
| 1512 | /* MaxOffset 2nd DW */ |
| 1513 | dm_pci_read_config32(dev, ea_off + 16, &ea_entry); |
| 1514 | sz |= ((u64)ea_entry) << 32; |
| 1515 | } |
| 1516 | |
| 1517 | addr = (pdata->virtid - 1) * (sz + 1); |
| 1518 | } |
| 1519 | |
| 1520 | return addr; |
| 1521 | } |
| 1522 | |
Andrew Scull | 58c6102 | 2022-04-21 16:11:10 +0000 | [diff] [blame] | 1523 | static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, size_t offset, |
| 1524 | size_t len, int ea_off, |
Andrew Scull | 30d338d | 2022-04-21 16:11:06 +0000 | [diff] [blame] | 1525 | struct pci_child_plat *pdata) |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1526 | { |
| 1527 | int ea_cnt, i, entry_size; |
| 1528 | int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2; |
| 1529 | u32 ea_entry; |
| 1530 | phys_addr_t addr; |
| 1531 | |
Suneel Garapati | 5858ba8 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1532 | if (IS_ENABLED(CONFIG_PCI_SRIOV)) { |
| 1533 | /* |
| 1534 | * In the case of a Virtual Function device, device is |
| 1535 | * Physical function, so pdata will point to required VF |
| 1536 | * specific data. |
| 1537 | */ |
| 1538 | if (pdata->is_virtfn) |
| 1539 | bar_id += PCI_EA_BEI_VF_BAR0; |
| 1540 | } |
| 1541 | |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1542 | /* EA capability structure header */ |
| 1543 | dm_pci_read_config32(dev, ea_off, &ea_entry); |
| 1544 | ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK; |
| 1545 | ea_off += PCI_EA_FIRST_ENT; |
| 1546 | |
| 1547 | for (i = 0; i < ea_cnt; i++, ea_off += entry_size) { |
| 1548 | /* Entry header */ |
| 1549 | dm_pci_read_config32(dev, ea_off, &ea_entry); |
| 1550 | entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2; |
| 1551 | |
| 1552 | if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id) |
| 1553 | continue; |
| 1554 | |
| 1555 | /* Base address, 1st DW */ |
| 1556 | dm_pci_read_config32(dev, ea_off + 4, &ea_entry); |
| 1557 | addr = ea_entry & PCI_EA_FIELD_MASK; |
| 1558 | if (ea_entry & PCI_EA_IS_64) { |
| 1559 | /* Base address, 2nd DW, skip over 4B MaxOffset */ |
| 1560 | dm_pci_read_config32(dev, ea_off + 12, &ea_entry); |
| 1561 | addr |= ((u64)ea_entry) << 32; |
| 1562 | } |
| 1563 | |
Suneel Garapati | 5858ba8 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1564 | if (IS_ENABLED(CONFIG_PCI_SRIOV)) |
| 1565 | addr += dm_pci_map_ea_virt(dev, ea_off, pdata); |
| 1566 | |
Andrew Scull | 58c6102 | 2022-04-21 16:11:10 +0000 | [diff] [blame] | 1567 | if (~((phys_addr_t)0) - addr < offset) |
| 1568 | return NULL; |
| 1569 | |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1570 | /* size ignored for now */ |
Andrew Scull | 58c6102 | 2022-04-21 16:11:10 +0000 | [diff] [blame] | 1571 | return map_physmem(addr + offset, len, MAP_NOCACHE); |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
Andrew Scull | 58c6102 | 2022-04-21 16:11:10 +0000 | [diff] [blame] | 1577 | void *dm_pci_map_bar(struct udevice *dev, int bar, size_t offset, size_t len, |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 1578 | unsigned long mask, unsigned long flags) |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1579 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1580 | struct pci_child_plat *pdata = dev_get_parent_plat(dev); |
Suneel Garapati | 5858ba8 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1581 | struct udevice *udev = dev; |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1582 | pci_addr_t pci_bus_addr; |
| 1583 | u32 bar_response; |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1584 | int ea_off; |
| 1585 | |
Suneel Garapati | 5858ba8 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1586 | if (IS_ENABLED(CONFIG_PCI_SRIOV)) { |
| 1587 | /* |
| 1588 | * In case of Virtual Function devices, use PF udevice |
| 1589 | * as EA capability is defined in Physical Function |
| 1590 | */ |
| 1591 | if (pdata->is_virtfn) |
| 1592 | udev = pdata->pfdev; |
| 1593 | } |
| 1594 | |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1595 | /* |
| 1596 | * if the function supports Enhanced Allocation use that instead of |
| 1597 | * BARs |
Suneel Garapati | 5858ba8 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1598 | * Incase of virtual functions, pdata will help read VF BEI |
| 1599 | * and EA entry size. |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1600 | */ |
Andrew Scull | 71e7e1a | 2022-04-21 16:11:16 +0000 | [diff] [blame] | 1601 | if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION)) |
| 1602 | ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA); |
| 1603 | else |
| 1604 | ea_off = 0; |
| 1605 | |
Alex Marginean | 1c934a6 | 2019-06-07 11:24:23 +0300 | [diff] [blame] | 1606 | if (ea_off) |
Andrew Scull | 58c6102 | 2022-04-21 16:11:10 +0000 | [diff] [blame] | 1607 | return dm_pci_map_ea_bar(udev, bar, offset, len, ea_off, pdata); |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1608 | |
| 1609 | /* read BAR address */ |
Suneel Garapati | 5858ba8 | 2019-10-19 16:34:16 -0700 | [diff] [blame] | 1610 | dm_pci_read_config32(udev, bar, &bar_response); |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1611 | pci_bus_addr = (pci_addr_t)(bar_response & ~0xf); |
| 1612 | |
Moritz Fischer | 512b021 | 2024-01-10 04:59:02 +0000 | [diff] [blame] | 1613 | /* This has a lot of baked in assumptions, but essentially tries |
| 1614 | * to mirror the behavior of BAR assignment for 64 Bit enabled |
| 1615 | * hosts and 64 bit placeable BARs in the auto assign code. |
| 1616 | */ |
| 1617 | #if defined(CONFIG_SYS_PCI_64BIT) |
| 1618 | if (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 1619 | dm_pci_read_config32(udev, bar + 4, &bar_response); |
| 1620 | pci_bus_addr |= (pci_addr_t)bar_response << 32; |
| 1621 | } |
| 1622 | #endif /* CONFIG_SYS_PCI_64BIT */ |
| 1623 | |
Andrew Scull | 58c6102 | 2022-04-21 16:11:10 +0000 | [diff] [blame] | 1624 | if (~((pci_addr_t)0) - pci_bus_addr < offset) |
| 1625 | return NULL; |
| 1626 | |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1627 | /* |
Andrew Scull | 58c6102 | 2022-04-21 16:11:10 +0000 | [diff] [blame] | 1628 | * Forward the length argument to dm_pci_bus_to_virt. The length will |
| 1629 | * be used to check that the entire address range has been declared as |
| 1630 | * a PCI range, but a better check would be to probe for the size of |
| 1631 | * the bar and prevent overflow more locally. |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1632 | */ |
Andrew Scull | 6520c82 | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 1633 | return dm_pci_bus_to_virt(udev, pci_bus_addr + offset, len, mask, flags, |
| 1634 | MAP_NOCACHE); |
Simon Glass | c5f053b | 2015-11-29 13:18:03 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1637 | static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap) |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1638 | { |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1639 | int ttl = PCI_FIND_CAP_TTL; |
| 1640 | u8 id; |
| 1641 | u16 ent; |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1642 | |
| 1643 | dm_pci_read_config8(dev, pos, &pos); |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1644 | |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1645 | while (ttl--) { |
| 1646 | if (pos < PCI_STD_HEADER_SIZEOF) |
| 1647 | break; |
| 1648 | pos &= ~3; |
| 1649 | dm_pci_read_config16(dev, pos, &ent); |
| 1650 | |
| 1651 | id = ent & 0xff; |
| 1652 | if (id == 0xff) |
| 1653 | break; |
| 1654 | if (id == cap) |
| 1655 | return pos; |
| 1656 | pos = (ent >> 8); |
| 1657 | } |
| 1658 | |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1662 | int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap) |
| 1663 | { |
| 1664 | return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT, |
| 1665 | cap); |
| 1666 | } |
| 1667 | |
| 1668 | int dm_pci_find_capability(struct udevice *dev, int cap) |
| 1669 | { |
| 1670 | u16 status; |
| 1671 | u8 header_type; |
| 1672 | u8 pos; |
| 1673 | |
| 1674 | dm_pci_read_config16(dev, PCI_STATUS, &status); |
| 1675 | if (!(status & PCI_STATUS_CAP_LIST)) |
| 1676 | return 0; |
| 1677 | |
| 1678 | dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type); |
| 1679 | if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS) |
| 1680 | pos = PCI_CB_CAPABILITY_LIST; |
| 1681 | else |
| 1682 | pos = PCI_CAPABILITY_LIST; |
| 1683 | |
| 1684 | return _dm_pci_find_next_capability(dev, pos, cap); |
| 1685 | } |
| 1686 | |
| 1687 | int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap) |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1688 | { |
| 1689 | u32 header; |
| 1690 | int ttl; |
| 1691 | int pos = PCI_CFG_SPACE_SIZE; |
| 1692 | |
| 1693 | /* minimum 8 bytes per capability */ |
| 1694 | ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; |
| 1695 | |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1696 | if (start) |
| 1697 | pos = start; |
| 1698 | |
Bin Meng | a7366f0 | 2018-08-03 01:14:52 -0700 | [diff] [blame] | 1699 | dm_pci_read_config32(dev, pos, &header); |
| 1700 | /* |
| 1701 | * If we have no capabilities, this is indicated by cap ID, |
| 1702 | * cap version and next pointer all being 0. |
| 1703 | */ |
| 1704 | if (header == 0) |
| 1705 | return 0; |
| 1706 | |
| 1707 | while (ttl--) { |
| 1708 | if (PCI_EXT_CAP_ID(header) == cap) |
| 1709 | return pos; |
| 1710 | |
| 1711 | pos = PCI_EXT_CAP_NEXT(header); |
| 1712 | if (pos < PCI_CFG_SPACE_SIZE) |
| 1713 | break; |
| 1714 | |
| 1715 | dm_pci_read_config32(dev, pos, &header); |
| 1716 | } |
| 1717 | |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
Bin Meng | 631f348 | 2018-10-15 02:21:21 -0700 | [diff] [blame] | 1721 | int dm_pci_find_ext_capability(struct udevice *dev, int cap) |
| 1722 | { |
| 1723 | return dm_pci_find_next_ext_capability(dev, 0, cap); |
| 1724 | } |
| 1725 | |
Alex Marginean | 09467d3 | 2019-06-07 11:24:25 +0300 | [diff] [blame] | 1726 | int dm_pci_flr(struct udevice *dev) |
| 1727 | { |
| 1728 | int pcie_off; |
| 1729 | u32 cap; |
| 1730 | |
| 1731 | /* look for PCI Express Capability */ |
| 1732 | pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP); |
| 1733 | if (!pcie_off) |
| 1734 | return -ENOENT; |
| 1735 | |
| 1736 | /* check FLR capability */ |
| 1737 | dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap); |
| 1738 | if (!(cap & PCI_EXP_DEVCAP_FLR)) |
| 1739 | return -ENOENT; |
| 1740 | |
| 1741 | dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0, |
| 1742 | PCI_EXP_DEVCTL_BCR_FLR); |
| 1743 | |
| 1744 | /* wait 100ms, per PCI spec */ |
| 1745 | mdelay(100); |
| 1746 | |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
Suneel Garapati | 13822f7 | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1750 | #if defined(CONFIG_PCI_SRIOV) |
| 1751 | int pci_sriov_init(struct udevice *pdev, int vf_en) |
| 1752 | { |
| 1753 | u16 vendor, device; |
| 1754 | struct udevice *bus; |
| 1755 | struct udevice *dev; |
| 1756 | pci_dev_t bdf; |
| 1757 | u16 ctrl; |
| 1758 | u16 num_vfs; |
| 1759 | u16 total_vf; |
| 1760 | u16 vf_offset; |
| 1761 | u16 vf_stride; |
| 1762 | int vf, ret; |
| 1763 | int pos; |
| 1764 | |
| 1765 | pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); |
| 1766 | if (!pos) { |
| 1767 | debug("Error: SRIOV capability not found\n"); |
| 1768 | return -ENOENT; |
| 1769 | } |
| 1770 | |
| 1771 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl); |
| 1772 | |
| 1773 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf); |
| 1774 | if (vf_en > total_vf) |
| 1775 | vf_en = total_vf; |
| 1776 | dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en); |
| 1777 | |
| 1778 | ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; |
| 1779 | dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl); |
| 1780 | |
| 1781 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs); |
| 1782 | if (num_vfs > vf_en) |
| 1783 | num_vfs = vf_en; |
| 1784 | |
| 1785 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset); |
| 1786 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride); |
| 1787 | |
| 1788 | dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor); |
| 1789 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device); |
| 1790 | |
| 1791 | bdf = dm_pci_get_bdf(pdev); |
| 1792 | |
Michal Suchanek | 4cd455e | 2022-09-25 13:08:16 +0200 | [diff] [blame] | 1793 | ret = pci_get_bus(PCI_BUS(bdf), &bus); |
| 1794 | if (ret) |
| 1795 | return ret; |
Suneel Garapati | 13822f7 | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1796 | |
| 1797 | bdf += PCI_BDF(0, 0, vf_offset); |
| 1798 | |
| 1799 | for (vf = 0; vf < num_vfs; vf++) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1800 | struct pci_child_plat *pplat; |
Suneel Garapati | 13822f7 | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1801 | ulong class; |
| 1802 | |
| 1803 | pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE, |
| 1804 | &class, PCI_SIZE_16); |
| 1805 | |
| 1806 | debug("%s: bus %d/%s: found VF %x:%x\n", __func__, |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1807 | dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf)); |
Suneel Garapati | 13822f7 | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1808 | |
| 1809 | /* Find this device in the device tree */ |
| 1810 | ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev); |
| 1811 | |
| 1812 | if (ret == -ENODEV) { |
| 1813 | struct pci_device_id find_id; |
| 1814 | |
| 1815 | memset(&find_id, '\0', sizeof(find_id)); |
| 1816 | find_id.vendor = vendor; |
| 1817 | find_id.device = device; |
| 1818 | find_id.class = class; |
| 1819 | |
| 1820 | ret = pci_find_and_bind_driver(bus, &find_id, |
| 1821 | bdf, &dev); |
| 1822 | |
| 1823 | if (ret) |
| 1824 | return ret; |
| 1825 | } |
| 1826 | |
| 1827 | /* Update the platform data */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1828 | pplat = dev_get_parent_plat(dev); |
Suneel Garapati | 13822f7 | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1829 | pplat->devfn = PCI_MASK_BUS(bdf); |
| 1830 | pplat->vendor = vendor; |
| 1831 | pplat->device = device; |
| 1832 | pplat->class = class; |
| 1833 | pplat->is_virtfn = true; |
| 1834 | pplat->pfdev = pdev; |
| 1835 | pplat->virtid = vf * vf_stride + vf_offset; |
| 1836 | |
| 1837 | debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n", |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 1838 | __func__, dev_seq(dev), dev->name, PCI_DEV(bdf), |
Suneel Garapati | 13822f7 | 2019-10-19 16:07:20 -0700 | [diff] [blame] | 1839 | PCI_FUNC(bdf), vendor, device, class, pplat->virtid); |
| 1840 | bdf += PCI_BDF(0, 0, vf_stride); |
| 1841 | } |
| 1842 | |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
| 1846 | int pci_sriov_get_totalvfs(struct udevice *pdev) |
| 1847 | { |
| 1848 | u16 total_vf; |
| 1849 | int pos; |
| 1850 | |
| 1851 | pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); |
| 1852 | if (!pos) { |
| 1853 | debug("Error: SRIOV capability not found\n"); |
| 1854 | return -ENOENT; |
| 1855 | } |
| 1856 | |
| 1857 | dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf); |
| 1858 | |
| 1859 | return total_vf; |
| 1860 | } |
| 1861 | #endif /* SRIOV */ |
| 1862 | |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1863 | UCLASS_DRIVER(pci) = { |
| 1864 | .id = UCLASS_PCI, |
| 1865 | .name = "pci", |
Simon Glass | be70610 | 2020-12-16 21:20:18 -0700 | [diff] [blame] | 1866 | .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ, |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 1867 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1868 | .pre_probe = pci_uclass_pre_probe, |
| 1869 | .post_probe = pci_uclass_post_probe, |
| 1870 | .child_post_bind = pci_uclass_child_post_bind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1871 | .per_device_auto = sizeof(struct pci_controller), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 1872 | .per_child_plat_auto = sizeof(struct pci_child_plat), |
Simon Glass | b94dc89 | 2015-03-05 12:25:25 -0700 | [diff] [blame] | 1873 | }; |
| 1874 | |
| 1875 | static const struct dm_pci_ops pci_bridge_ops = { |
| 1876 | .read_config = pci_bridge_read_config, |
| 1877 | .write_config = pci_bridge_write_config, |
| 1878 | }; |
| 1879 | |
| 1880 | static const struct udevice_id pci_bridge_ids[] = { |
| 1881 | { .compatible = "pci-bridge" }, |
| 1882 | { } |
| 1883 | }; |
| 1884 | |
| 1885 | U_BOOT_DRIVER(pci_bridge_drv) = { |
| 1886 | .name = "pci_bridge_drv", |
| 1887 | .id = UCLASS_PCI, |
| 1888 | .of_match = pci_bridge_ids, |
| 1889 | .ops = &pci_bridge_ops, |
| 1890 | }; |
| 1891 | |
| 1892 | UCLASS_DRIVER(pci_generic) = { |
| 1893 | .id = UCLASS_PCI_GENERIC, |
| 1894 | .name = "pci_generic", |
| 1895 | }; |
| 1896 | |
| 1897 | static const struct udevice_id pci_generic_ids[] = { |
| 1898 | { .compatible = "pci-generic" }, |
| 1899 | { } |
| 1900 | }; |
| 1901 | |
| 1902 | U_BOOT_DRIVER(pci_generic_drv) = { |
| 1903 | .name = "pci_generic_drv", |
| 1904 | .id = UCLASS_PCI_GENERIC, |
| 1905 | .of_match = pci_generic_ids, |
| 1906 | }; |
Stephen Warren | 04eb269 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1907 | |
Ovidiu Panait | e353edb | 2020-11-28 10:43:12 +0200 | [diff] [blame] | 1908 | int pci_init(void) |
Stephen Warren | 04eb269 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1909 | { |
| 1910 | struct udevice *bus; |
| 1911 | |
| 1912 | /* |
| 1913 | * Enumerate all known controller devices. Enumeration has the side- |
| 1914 | * effect of probing them, so PCIe devices will be enumerated too. |
| 1915 | */ |
Marek BehĂșn | 5df208d | 2019-05-21 12:04:31 +0200 | [diff] [blame] | 1916 | for (uclass_first_device_check(UCLASS_PCI, &bus); |
Stephen Warren | 04eb269 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1917 | bus; |
Marek BehĂșn | 5df208d | 2019-05-21 12:04:31 +0200 | [diff] [blame] | 1918 | uclass_next_device_check(&bus)) { |
Stephen Warren | 04eb269 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1919 | ; |
| 1920 | } |
Ovidiu Panait | e353edb | 2020-11-28 10:43:12 +0200 | [diff] [blame] | 1921 | |
| 1922 | return 0; |
Stephen Warren | 04eb269 | 2016-01-26 11:10:11 -0700 | [diff] [blame] | 1923 | } |