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