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