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