blob: 632c1a63cfce99c9e657268197ea7a8f9cfc894d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb94dc892015-03-05 12:25:25 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassb94dc892015-03-05 12:25:25 -07005 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_PCI
8
Simon Glassb94dc892015-03-05 12:25:25 -07009#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glassb94dc892015-03-05 12:25:25 -070015#include <pci.h>
Simon Glass797b8e82023-07-15 21:38:55 -060016#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glassc5f053b2015-11-29 13:18:03 -070018#include <asm/io.h>
Simon Glassb94dc892015-03-05 12:25:25 -070019#include <dm/device-internal.h>
Simon Glass89d83232017-05-18 20:09:51 -060020#include <dm/lists.h>
Simon Glassbe706102020-12-16 21:20:18 -070021#include <dm/uclass-internal.h>
Bin Mengc0820a42015-08-20 06:40:23 -070022#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
Simon Glassef8a2dd2019-08-24 14:19:05 -060023#include <asm/fsp/fsp_support.h>
Bin Mengc0820a42015-08-20 06:40:23 -070024#endif
Simon Glass8807a562021-06-27 17:50:57 -060025#include <dt-bindings/pci/pci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060026#include <linux/delay.h>
Simon Glass37a3f94b2015-11-29 13:17:49 -070027#include "pci_internal.h"
Simon Glassb94dc892015-03-05 12:25:25 -070028
29DECLARE_GLOBAL_DATA_PTR;
30
Simon Glass2e4e4432016-01-18 20:19:14 -070031int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass7d07e592015-08-31 18:55:35 -060032{
33 int ret;
34
35 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
36
37 /* Since buses may not be numbered yet try a little harder with bus 0 */
38 if (ret == -ENODEV) {
Simon Glassc7298e72016-02-11 13:23:26 -070039 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass7d07e592015-08-31 18:55:35 -060040 if (ret)
41 return ret;
Simon Glass7d07e592015-08-31 18:55:35 -060042 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
43 }
44
45 return ret;
46}
47
Simon Glass6256d672015-11-19 20:27:00 -070048struct udevice *pci_get_controller(struct udevice *dev)
49{
50 while (device_is_on_pci_bus(dev))
51 dev = dev->parent;
52
53 return dev;
54}
55
Simon Glassc92aac12020-01-27 08:49:38 -070056pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glassc9118d42015-07-06 16:47:46 -060057{
Simon Glassb75b15b2020-12-03 16:55:23 -070058 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassc9118d42015-07-06 16:47:46 -060059 struct udevice *bus = dev->parent;
60
Simon Glass1c6449c2019-12-29 21:19:14 -070061 /*
62 * This error indicates that @dev is a device on an unprobed PCI bus.
63 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
64 * will produce a bad BDF>
65 *
66 * A common cause of this problem is that this function is called in the
Simon Glassaad29ae2020-12-03 16:55:21 -070067 * of_to_plat() method of @dev. Accessing the PCI bus in that
Simon Glass1c6449c2019-12-29 21:19:14 -070068 * method is not allowed, since it has not yet been probed. To fix this,
69 * move that access to the probe() method of @dev instead.
70 */
71 if (!device_active(bus))
72 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
73 bus->name);
Simon Glass75e534b2020-12-16 21:20:07 -070074 return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
Simon Glassc9118d42015-07-06 16:47:46 -060075}
76
Simon Glassb94dc892015-03-05 12:25:25 -070077/**
78 * pci_get_bus_max() - returns the bus number of the last active bus
79 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010080 * Return: last bus number, or -1 if no active buses
Simon Glassb94dc892015-03-05 12:25:25 -070081 */
82static int pci_get_bus_max(void)
83{
84 struct udevice *bus;
85 struct uclass *uc;
86 int ret = -1;
87
88 ret = uclass_get(UCLASS_PCI, &uc);
89 uclass_foreach_dev(bus, uc) {
Simon Glass75e534b2020-12-16 21:20:07 -070090 if (dev_seq(bus) > ret)
91 ret = dev_seq(bus);
Simon Glassb94dc892015-03-05 12:25:25 -070092 }
93
94 debug("%s: ret=%d\n", __func__, ret);
95
96 return ret;
97}
98
99int pci_last_busno(void)
100{
Bin Meng5bc3f8a2015-10-01 00:36:01 -0700101 return pci_get_bus_max();
Simon Glassb94dc892015-03-05 12:25:25 -0700102}
103
104int pci_get_ff(enum pci_size_t size)
105{
106 switch (size) {
107 case PCI_SIZE_8:
108 return 0xff;
109 case PCI_SIZE_16:
110 return 0xffff;
111 default:
112 return 0xffffffff;
113 }
114}
115
Marek Vasutb4535792018-10-10 21:27:06 +0200116static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
117 ofnode *rnode)
118{
119 struct fdt_pci_addr addr;
120 ofnode node;
121 int ret;
122
123 dev_for_each_subnode(node, bus) {
124 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
125 &addr);
126 if (ret)
127 continue;
128
129 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
130 continue;
131
132 *rnode = node;
133 break;
134 }
135};
136
Simon Glass2a311e82020-01-27 08:49:37 -0700137int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassb94dc892015-03-05 12:25:25 -0700138 struct udevice **devp)
139{
140 struct udevice *dev;
141
142 for (device_find_first_child(bus, &dev);
143 dev;
144 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700145 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700146
Simon Glass71fa5b42020-12-03 16:55:18 -0700147 pplat = dev_get_parent_plat(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700148 if (pplat && pplat->devfn == find_devfn) {
149 *devp = dev;
150 return 0;
151 }
152 }
153
154 return -ENODEV;
155}
156
Simon Glass84283d52015-11-29 13:17:48 -0700157int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassb94dc892015-03-05 12:25:25 -0700158{
159 struct udevice *bus;
160 int ret;
161
Simon Glass7d07e592015-08-31 18:55:35 -0600162 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700163 if (ret)
164 return ret;
165 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
166}
167
168static int pci_device_matches_ids(struct udevice *dev,
Simon Glass3f7dc6e2021-06-27 17:50:56 -0600169 const struct pci_device_id *ids)
Simon Glassb94dc892015-03-05 12:25:25 -0700170{
Simon Glassb75b15b2020-12-03 16:55:23 -0700171 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700172 int i;
173
Simon Glass71fa5b42020-12-03 16:55:18 -0700174 pplat = dev_get_parent_plat(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700175 if (!pplat)
176 return -EINVAL;
177 for (i = 0; ids[i].vendor != 0; i++) {
178 if (pplat->vendor == ids[i].vendor &&
179 pplat->device == ids[i].device)
180 return i;
181 }
182
183 return -EINVAL;
184}
185
Simon Glass3f7dc6e2021-06-27 17:50:56 -0600186int pci_bus_find_devices(struct udevice *bus, const struct pci_device_id *ids,
Simon Glassb94dc892015-03-05 12:25:25 -0700187 int *indexp, struct udevice **devp)
188{
189 struct udevice *dev;
190
191 /* Scan all devices on this bus */
192 for (device_find_first_child(bus, &dev);
193 dev;
194 device_find_next_child(&dev)) {
195 if (pci_device_matches_ids(dev, ids) >= 0) {
196 if ((*indexp)-- <= 0) {
197 *devp = dev;
198 return 0;
199 }
200 }
201 }
202
203 return -ENODEV;
204}
205
Simon Glass3f7dc6e2021-06-27 17:50:56 -0600206int pci_find_device_id(const struct pci_device_id *ids, int index,
Simon Glassb94dc892015-03-05 12:25:25 -0700207 struct udevice **devp)
208{
209 struct udevice *bus;
210
211 /* Scan all known buses */
212 for (uclass_first_device(UCLASS_PCI, &bus);
213 bus;
214 uclass_next_device(&bus)) {
215 if (!pci_bus_find_devices(bus, ids, &index, devp))
216 return 0;
217 }
218 *devp = NULL;
219
220 return -ENODEV;
221}
222
Simon Glass70e0c582015-11-29 13:17:50 -0700223static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
224 unsigned int device, int *indexp,
225 struct udevice **devp)
226{
Simon Glassb75b15b2020-12-03 16:55:23 -0700227 struct pci_child_plat *pplat;
Simon Glass70e0c582015-11-29 13:17:50 -0700228 struct udevice *dev;
229
230 for (device_find_first_child(bus, &dev);
231 dev;
232 device_find_next_child(&dev)) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700233 pplat = dev_get_parent_plat(dev);
Simon Glass70e0c582015-11-29 13:17:50 -0700234 if (pplat->vendor == vendor && pplat->device == device) {
235 if (!(*indexp)--) {
236 *devp = dev;
237 return 0;
238 }
239 }
240 }
241
242 return -ENODEV;
243}
244
245int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
246 struct udevice **devp)
247{
248 struct udevice *bus;
249
250 /* Scan all known buses */
251 for (uclass_first_device(UCLASS_PCI, &bus);
252 bus;
253 uclass_next_device(&bus)) {
254 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
255 return device_probe(*devp);
256 }
257 *devp = NULL;
258
259 return -ENODEV;
260}
261
Simon Glassb639d512015-11-29 13:17:52 -0700262int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
263{
264 struct udevice *dev;
265
266 /* Scan all known buses */
267 for (pci_find_first_device(&dev);
268 dev;
269 pci_find_next_device(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700270 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassb639d512015-11-29 13:17:52 -0700271
272 if (pplat->class == find_class && !index--) {
273 *devp = dev;
274 return device_probe(*devp);
275 }
276 }
277 *devp = NULL;
278
279 return -ENODEV;
280}
281
Simon Glassb94dc892015-03-05 12:25:25 -0700282int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
283 unsigned long value, enum pci_size_t size)
284{
285 struct dm_pci_ops *ops;
286
287 ops = pci_get_ops(bus);
288 if (!ops->write_config)
289 return -ENOSYS;
Pali RohĂĄrbce69632022-07-03 12:48:06 +0200290 if (offset < 0 || offset >= 4096)
291 return -EINVAL;
Simon Glassb94dc892015-03-05 12:25:25 -0700292 return ops->write_config(bus, bdf, offset, value, size);
293}
294
Simon Glass9cec2df2016-03-06 19:27:52 -0700295int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
296 u32 clr, u32 set)
297{
298 ulong val;
299 int ret;
300
301 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
302 if (ret)
303 return ret;
304 val &= ~clr;
305 val |= set;
306
307 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
308}
309
Vladimir Oltean278a5b52021-09-17 15:11:25 +0300310static int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
311 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -0700312{
313 struct udevice *bus;
314 int ret;
315
Simon Glass7d07e592015-08-31 18:55:35 -0600316 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700317 if (ret)
318 return ret;
319
Bin Meng0a721522015-07-19 00:20:04 +0800320 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700321}
322
Simon Glass94ef2422015-08-10 07:05:03 -0600323int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
324 enum pci_size_t size)
325{
326 struct udevice *bus;
327
Bin Meng05bedb12015-09-11 03:24:34 -0700328 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600329 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700330 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
331 size);
Simon Glass94ef2422015-08-10 07:05:03 -0600332}
333
Simon Glassb94dc892015-03-05 12:25:25 -0700334int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
335{
336 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
337}
338
339int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
340{
341 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
342}
343
344int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
345{
346 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
347}
348
Simon Glass94ef2422015-08-10 07:05:03 -0600349int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
350{
351 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
352}
353
354int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
355{
356 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
357}
358
359int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
360{
361 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
362}
363
Simon Glassc92aac12020-01-27 08:49:38 -0700364int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassb94dc892015-03-05 12:25:25 -0700365 unsigned long *valuep, enum pci_size_t size)
366{
367 struct dm_pci_ops *ops;
368
369 ops = pci_get_ops(bus);
Pali RohĂĄrbce69632022-07-03 12:48:06 +0200370 if (!ops->read_config) {
371 *valuep = pci_conv_32_to_size(~0, offset, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700372 return -ENOSYS;
Pali RohĂĄrbce69632022-07-03 12:48:06 +0200373 }
374 if (offset < 0 || offset >= 4096) {
375 *valuep = pci_conv_32_to_size(0, offset, size);
376 return -EINVAL;
377 }
Simon Glassb94dc892015-03-05 12:25:25 -0700378 return ops->read_config(bus, bdf, offset, valuep, size);
379}
380
Vladimir Oltean69c5f8a2021-09-17 15:11:26 +0300381static int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
382 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -0700383{
384 struct udevice *bus;
385 int ret;
386
Simon Glass7d07e592015-08-31 18:55:35 -0600387 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700388 if (ret)
389 return ret;
390
Bin Meng0a721522015-07-19 00:20:04 +0800391 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700392}
393
Simon Glassc92aac12020-01-27 08:49:38 -0700394int dm_pci_read_config(const struct udevice *dev, int offset,
395 unsigned long *valuep, enum pci_size_t size)
Simon Glass94ef2422015-08-10 07:05:03 -0600396{
Simon Glassc92aac12020-01-27 08:49:38 -0700397 const struct udevice *bus;
Simon Glass94ef2422015-08-10 07:05:03 -0600398
Bin Meng05bedb12015-09-11 03:24:34 -0700399 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600400 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700401 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass94ef2422015-08-10 07:05:03 -0600402 size);
403}
404
Simon Glassb94dc892015-03-05 12:25:25 -0700405int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
406{
407 unsigned long value;
408 int ret;
409
410 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
411 if (ret)
412 return ret;
413 *valuep = value;
414
415 return 0;
416}
417
418int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
419{
420 unsigned long value;
421 int ret;
422
423 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
424 if (ret)
425 return ret;
426 *valuep = value;
427
428 return 0;
429}
430
431int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
432{
433 unsigned long value;
434 int ret;
435
436 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
437 if (ret)
438 return ret;
439 *valuep = value;
440
441 return 0;
442}
443
Simon Glassc92aac12020-01-27 08:49:38 -0700444int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600445{
446 unsigned long value;
447 int ret;
448
449 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
450 if (ret)
451 return ret;
452 *valuep = value;
453
454 return 0;
455}
456
Simon Glassc92aac12020-01-27 08:49:38 -0700457int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600458{
459 unsigned long value;
460 int ret;
461
462 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
463 if (ret)
464 return ret;
465 *valuep = value;
466
467 return 0;
468}
469
Simon Glassc92aac12020-01-27 08:49:38 -0700470int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600471{
472 unsigned long value;
473 int ret;
474
475 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
476 if (ret)
477 return ret;
478 *valuep = value;
479
480 return 0;
481}
482
Simon Glass9cec2df2016-03-06 19:27:52 -0700483int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
484{
485 u8 val;
486 int ret;
487
488 ret = dm_pci_read_config8(dev, offset, &val);
489 if (ret)
490 return ret;
491 val &= ~clr;
492 val |= set;
493
494 return dm_pci_write_config8(dev, offset, val);
495}
496
497int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
498{
499 u16 val;
500 int ret;
501
502 ret = dm_pci_read_config16(dev, offset, &val);
503 if (ret)
504 return ret;
505 val &= ~clr;
506 val |= set;
507
508 return dm_pci_write_config16(dev, offset, val);
509}
510
511int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
512{
513 u32 val;
514 int ret;
515
516 ret = dm_pci_read_config32(dev, offset, &val);
517 if (ret)
518 return ret;
519 val &= ~clr;
520 val |= set;
521
522 return dm_pci_write_config32(dev, offset, val);
523}
524
Bin Menga0705782015-10-01 00:36:02 -0700525static void set_vga_bridge_bits(struct udevice *dev)
526{
527 struct udevice *parent = dev->parent;
528 u16 bc;
529
Simon Glass75e534b2020-12-16 21:20:07 -0700530 while (dev_seq(parent) != 0) {
Bin Menga0705782015-10-01 00:36:02 -0700531 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
532 bc |= PCI_BRIDGE_CTL_VGA;
533 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
534 parent = parent->parent;
535 }
536}
537
Simon Glassb94dc892015-03-05 12:25:25 -0700538int pci_auto_config_devices(struct udevice *bus)
539{
Simon Glass95588622020-12-22 19:30:28 -0700540 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700541 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700542 unsigned int sub_bus;
543 struct udevice *dev;
544 int ret;
545
Simon Glass75e534b2020-12-16 21:20:07 -0700546 sub_bus = dev_seq(bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700547 debug("%s: start\n", __func__);
548 pciauto_config_init(hose);
549 for (ret = device_find_first_child(bus, &dev);
550 !ret && dev;
551 ret = device_find_next_child(&dev)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700552 unsigned int max_bus;
Simon Glassb072d522015-09-08 17:52:47 -0600553 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700554
Simon Glassb94dc892015-03-05 12:25:25 -0700555 debug("%s: device %s\n", __func__, dev->name);
Simon Glassf1d50f72020-12-19 10:40:13 -0700556 if (dev_has_ofnode(dev) &&
Suneel Garapatif8c86282020-05-04 21:25:25 -0700557 dev_read_bool(dev, "pci,no-autoconfig"))
Simon Glassf3005fb2020-04-08 16:57:26 -0600558 continue;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700559 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600560 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -0700561 return log_msg_ret("auto", ret);
Simon Glassb072d522015-09-08 17:52:47 -0600562 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700563 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700564
Masami Hiramatsu7ccdc672021-06-04 18:43:34 +0900565 if (dev_get_parent(dev) == bus)
566 continue;
567
Simon Glass71fa5b42020-12-03 16:55:18 -0700568 pplat = dev_get_parent_plat(dev);
Bin Menga0705782015-10-01 00:36:02 -0700569 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
570 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700571 }
Pali RohĂĄr8b79e082022-01-17 16:38:37 +0100572 if (hose->last_busno < sub_bus)
573 hose->last_busno = sub_bus;
Simon Glassb94dc892015-03-05 12:25:25 -0700574 debug("%s: done\n", __func__);
575
Simon Glassbe706102020-12-16 21:20:18 -0700576 return log_msg_ret("sub", sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700577}
578
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300579int pci_generic_mmap_write_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700580 const struct udevice *bus,
581 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
582 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300583 pci_dev_t bdf,
584 uint offset,
585 ulong value,
586 enum pci_size_t size)
587{
588 void *address;
589
590 if (addr_f(bus, bdf, offset, &address) < 0)
591 return 0;
592
593 switch (size) {
594 case PCI_SIZE_8:
595 writeb(value, address);
596 return 0;
597 case PCI_SIZE_16:
598 writew(value, address);
599 return 0;
600 case PCI_SIZE_32:
601 writel(value, address);
602 return 0;
603 default:
604 return -EINVAL;
605 }
606}
607
608int pci_generic_mmap_read_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700609 const struct udevice *bus,
610 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
611 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300612 pci_dev_t bdf,
613 uint offset,
614 ulong *valuep,
615 enum pci_size_t size)
616{
617 void *address;
618
619 if (addr_f(bus, bdf, offset, &address) < 0) {
620 *valuep = pci_get_ff(size);
621 return 0;
622 }
623
624 switch (size) {
625 case PCI_SIZE_8:
626 *valuep = readb(address);
627 return 0;
628 case PCI_SIZE_16:
629 *valuep = readw(address);
630 return 0;
631 case PCI_SIZE_32:
632 *valuep = readl(address);
633 return 0;
634 default:
635 return -EINVAL;
636 }
637}
638
Simon Glass37a3f94b2015-11-29 13:17:49 -0700639int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700640{
Pali RohĂĄr4fb65992021-10-07 14:50:58 +0200641 u8 header_type;
Simon Glassb94dc892015-03-05 12:25:25 -0700642 int sub_bus;
643 int ret;
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700644 int ea_pos;
645 u8 reg;
Simon Glassb94dc892015-03-05 12:25:25 -0700646
647 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700648
Pali RohĂĄr4fb65992021-10-07 14:50:58 +0200649 dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);
650 header_type &= 0x7f;
651 if (header_type != PCI_HEADER_TYPE_BRIDGE) {
652 debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n",
653 __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type);
654 return log_msg_ret("probe", -EINVAL);
655 }
656
Andrew Scull71e7e1a2022-04-21 16:11:16 +0000657 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
658 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
659 else
660 ea_pos = 0;
661
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700662 if (ea_pos) {
663 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
664 &reg);
665 sub_bus = reg;
666 } else {
667 sub_bus = pci_get_bus_max() + 1;
668 }
Simon Glassb94dc892015-03-05 12:25:25 -0700669 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700670 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700671
672 ret = device_probe(bus);
673 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600674 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700675 ret);
Simon Glassbe706102020-12-16 21:20:18 -0700676 return log_msg_ret("probe", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700677 }
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700678
Masami Hiramatsuff022452021-04-16 14:53:46 -0700679 if (!ea_pos)
680 sub_bus = pci_get_bus_max();
681
Simon Glass37a3f94b2015-11-29 13:17:49 -0700682 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700683
684 return sub_bus;
685}
686
Simon Glass318d71c2015-07-06 16:47:44 -0600687/**
688 * pci_match_one_device - Tell if a PCI device structure has a matching
689 * PCI device id structure
690 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800691 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600692 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800693 * Returns true if the finding pci_device_id structure matched or false if
694 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600695 */
696static bool pci_match_one_id(const struct pci_device_id *id,
697 const struct pci_device_id *find)
698{
699 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
700 (id->device == PCI_ANY_ID || id->device == find->device) &&
701 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
702 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
703 !((id->class ^ find->class) & id->class_mask))
704 return true;
705
706 return false;
707}
708
709/**
Simon Glass8807a562021-06-27 17:50:57 -0600710 * pci_need_device_pre_reloc() - Check if a device should be bound
711 *
712 * This checks a list of vendor/device-ID values indicating devices that should
713 * be bound before relocation.
714 *
715 * @bus: Bus to check
716 * @vendor: Vendor ID to check
717 * @device: Device ID to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100718 * Return: true if the vendor/device is in the list, false if not
Simon Glass8807a562021-06-27 17:50:57 -0600719 */
720static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor,
721 uint device)
722{
723 u32 vendev;
724 int index;
725
Simon Glass797b8e82023-07-15 21:38:55 -0600726 if (spl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(PCI_PNP))
727 return true;
728
Simon Glass8807a562021-06-27 17:50:57 -0600729 for (index = 0;
730 !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index,
731 &vendev);
732 index++) {
733 if (vendev == PCI_VENDEV(vendor, device))
734 return true;
735 }
736
737 return false;
738}
739
740/**
Simon Glass318d71c2015-07-06 16:47:44 -0600741 * pci_find_and_bind_driver() - Find and bind the right PCI driver
742 *
743 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600744 *
745 * @parent: Parent bus
746 * @find_id: Specification of the driver to find
747 * @bdf: Bus/device/function addreess - see PCI_BDF()
748 * @devp: Returns a pointer to the device created
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100749 * Return: 0 if OK, -EPERM if the device is not needed before relocation and
Simon Glassc45abf12015-09-08 17:52:49 -0600750 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600751 */
752static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600753 struct pci_device_id *find_id,
754 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600755{
756 struct pci_driver_entry *start, *entry;
Marek Vasutb4535792018-10-10 21:27:06 +0200757 ofnode node = ofnode_null();
Simon Glass318d71c2015-07-06 16:47:44 -0600758 const char *drv;
759 int n_ents;
760 int ret;
761 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700762 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600763
764 *devp = NULL;
765
766 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
767 find_id->vendor, find_id->device);
Marek Vasutb4535792018-10-10 21:27:06 +0200768
769 /* Determine optional OF node */
Suneel Garapaticb7093d2019-10-19 16:02:48 -0700770 if (ofnode_valid(dev_ofnode(parent)))
771 pci_dev_find_ofnode(parent, bdf, &node);
Marek Vasutb4535792018-10-10 21:27:06 +0200772
Simon Glass2e4938b2022-09-06 20:27:17 -0600773 if (ofnode_valid(node) && !ofnode_is_enabled(node)) {
Michael Walle2e21f372019-12-01 17:45:18 +0100774 debug("%s: Ignoring disabled device\n", __func__);
Simon Glassbe706102020-12-16 21:20:18 -0700775 return log_msg_ret("dis", -EPERM);
Michael Walle2e21f372019-12-01 17:45:18 +0100776 }
777
Simon Glass318d71c2015-07-06 16:47:44 -0600778 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
779 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
780 for (entry = start; entry != start + n_ents; entry++) {
781 const struct pci_device_id *id;
782 struct udevice *dev;
783 const struct driver *drv;
784
785 for (id = entry->match;
786 id->vendor || id->subvendor || id->class_mask;
787 id++) {
788 if (!pci_match_one_id(id, find_id))
789 continue;
790
791 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700792
793 /*
794 * In the pre-relocation phase, we only bind devices
795 * whose driver has the DM_FLAG_PRE_RELOC set, to save
796 * precious memory space as on some platforms as that
797 * space is pretty limited (ie: using Cache As RAM).
798 */
799 if (!(gd->flags & GD_FLG_RELOC) &&
Simon Glass797b8e82023-07-15 21:38:55 -0600800 !(drv->flags & DM_FLAG_PRE_RELOC) &&
801 (!CONFIG_IS_ENABLED(PCI_PNP) ||
802 spl_phase() != PHASE_SPL))
Simon Glassbe706102020-12-16 21:20:18 -0700803 return log_msg_ret("pre", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700804
Simon Glass318d71c2015-07-06 16:47:44 -0600805 /*
806 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700807 * plat (instead of NULL) and allow its bind()
Simon Glass318d71c2015-07-06 16:47:44 -0600808 * method to return -ENOENT if it doesn't support this
809 * device. That way we could continue the search to
810 * find another driver. For now this doesn't seem
811 * necesssary, so just bind the first match.
812 */
Simon Glass884870f2020-11-28 17:50:01 -0700813 ret = device_bind(parent, drv, drv->name, NULL, node,
814 &dev);
Simon Glass318d71c2015-07-06 16:47:44 -0600815 if (ret)
816 goto error;
817 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700818 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600819 *devp = dev;
820 return 0;
821 }
822 }
823
Bin Meng984c0dc2015-08-20 06:40:17 -0700824 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
825 /*
826 * In the pre-relocation phase, we only bind bridge devices to save
827 * precious memory space as on some platforms as that space is pretty
828 * limited (ie: using Cache As RAM).
829 */
Simon Glass8807a562021-06-27 17:50:57 -0600830 if (!(gd->flags & GD_FLG_RELOC) && !bridge &&
831 !pci_need_device_pre_reloc(parent, find_id->vendor,
832 find_id->device))
Simon Glassbe706102020-12-16 21:20:18 -0700833 return log_msg_ret("notbr", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700834
Simon Glass318d71c2015-07-06 16:47:44 -0600835 /* Bind a generic driver so that the device can be used */
Simon Glass75e534b2020-12-16 21:20:07 -0700836 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
Bin Meng0a721522015-07-19 00:20:04 +0800837 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600838 str = strdup(name);
839 if (!str)
840 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700841 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
842
Marek Vasutb4535792018-10-10 21:27:06 +0200843 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glass318d71c2015-07-06 16:47:44 -0600844 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600845 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200846 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600847 return ret;
848 }
849 debug("%s: No match found: bound generic driver instead\n", __func__);
850
851 return 0;
852
853error:
854 debug("%s: No match found: error %d\n", __func__, ret);
855 return ret;
856}
857
Tim Harvey4c57bf72021-04-16 14:53:47 -0700858__weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
859{
860}
861
Simon Glassb94dc892015-03-05 12:25:25 -0700862int pci_bind_bus_devices(struct udevice *bus)
863{
864 ulong vendor, device;
865 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800866 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700867 bool found_multi;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700868 int ari_off;
Simon Glassb94dc892015-03-05 12:25:25 -0700869 int ret;
870
871 found_multi = false;
Simon Glass75e534b2020-12-16 21:20:07 -0700872 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
Bin Meng0a721522015-07-19 00:20:04 +0800873 PCI_MAX_PCI_FUNCTIONS - 1);
Simon Glass75e534b2020-12-16 21:20:07 -0700874 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800875 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700876 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700877 struct udevice *dev;
878 ulong class;
879
Bin Meng20bdc1e2018-08-03 01:14:37 -0700880 if (!PCI_FUNC(bdf))
881 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800882 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700883 continue;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800884
Simon Glassb94dc892015-03-05 12:25:25 -0700885 /* Check only the first access, we don't expect problems */
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800886 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
887 PCI_SIZE_16);
Pali RohĂĄra8a520d2021-09-07 18:07:08 +0200888 if (ret || vendor == 0xffff || vendor == 0x0000)
Simon Glassb94dc892015-03-05 12:25:25 -0700889 continue;
890
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800891 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
892 &header_type, PCI_SIZE_8);
893
Bin Meng0a721522015-07-19 00:20:04 +0800894 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700895 found_multi = header_type & 0x80;
896
Simon Glass25916d62019-09-25 08:56:12 -0600897 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -0700898 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Bin Meng0a721522015-07-19 00:20:04 +0800899 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700900 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800901 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600902 PCI_SIZE_32);
903 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700904
905 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800906 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass25916d62019-09-25 08:56:12 -0600907 debug(": find ret=%d\n", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700908
Simon Glass413ebdb2015-11-29 13:18:09 -0700909 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700910 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600911 struct pci_device_id find_id;
912 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700913
Simon Glass318d71c2015-07-06 16:47:44 -0600914 memset(&find_id, '\0', sizeof(find_id));
915 find_id.vendor = vendor;
916 find_id.device = device;
917 find_id.class = class;
918 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800919 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600920 PCI_SUBSYSTEM_VENDOR_ID,
921 &val, PCI_SIZE_32);
922 find_id.subvendor = val & 0xffff;
923 find_id.subdevice = val >> 16;
924 }
Bin Meng0a721522015-07-19 00:20:04 +0800925 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600926 &dev);
Simon Glass797b8e82023-07-15 21:38:55 -0600927 } else {
928 debug("device: %s\n", dev->name);
Simon Glassb94dc892015-03-05 12:25:25 -0700929 }
Simon Glassc45abf12015-09-08 17:52:49 -0600930 if (ret == -EPERM)
931 continue;
932 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700933 return ret;
934
935 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -0700936 pplat = dev_get_parent_plat(dev);
Simon Glassc45abf12015-09-08 17:52:49 -0600937 pplat->devfn = PCI_MASK_BUS(bdf);
938 pplat->vendor = vendor;
939 pplat->device = device;
940 pplat->class = class;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700941
942 if (IS_ENABLED(CONFIG_PCI_ARID)) {
943 ari_off = dm_pci_find_ext_capability(dev,
944 PCI_EXT_CAP_ID_ARI);
945 if (ari_off) {
946 u16 ari_cap;
947
948 /*
949 * Read Next Function number in ARI Cap
950 * Register
951 */
952 dm_pci_read_config16(dev, ari_off + 4,
953 &ari_cap);
954 /*
955 * Update next scan on this function number,
956 * subtract 1 in BDF to satisfy loop increment.
957 */
958 if (ari_cap & 0xff00) {
959 bdf = PCI_BDF(PCI_BUS(bdf),
960 PCI_DEV(ari_cap),
961 PCI_FUNC(ari_cap));
962 bdf = bdf - 0x100;
963 }
964 }
965 }
Tim Harvey4c57bf72021-04-16 14:53:47 -0700966
967 board_pci_fixup_dev(bus, dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700968 }
969
970 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -0700971}
972
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +0100973static int decode_regions(struct pci_controller *hose, ofnode parent_node,
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700974 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700975{
976 int pci_addr_cells, addr_cells, size_cells;
977 int cells_per_record;
Stefan Roesebbc88462020-08-12 11:55:46 +0200978 struct bd_info *bd;
Simon Glassb94dc892015-03-05 12:25:25 -0700979 const u32 *prop;
Stefan Roese950864f2020-07-23 16:34:10 +0200980 int max_regions;
Simon Glassb94dc892015-03-05 12:25:25 -0700981 int len;
982 int i;
983
Simon Glassa5f9a612023-05-04 16:55:01 -0600984 /* handle booting from coreboot, etc. */
985 if (!ll_boot_init())
986 return 0;
987
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900988 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700989 if (!prop) {
990 debug("%s: Cannot decode regions\n", __func__);
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +0100991 return -EINVAL;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700992 }
993
Simon Glass4191dc12017-06-12 06:21:31 -0600994 pci_addr_cells = ofnode_read_simple_addr_cells(node);
995 addr_cells = ofnode_read_simple_addr_cells(parent_node);
996 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700997
998 /* PCI addresses are always 3-cells */
999 len /= sizeof(u32);
1000 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1001 hose->region_count = 0;
1002 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1003 cells_per_record);
Stefan Roese950864f2020-07-23 16:34:10 +02001004
1005 /* Dynamically allocate the regions array */
1006 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
1007 hose->regions = (struct pci_region *)
1008 calloc(1, max_regions * sizeof(struct pci_region));
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001009 if (!hose->regions)
1010 return -ENOMEM;
Stefan Roese950864f2020-07-23 16:34:10 +02001011
1012 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassb94dc892015-03-05 12:25:25 -07001013 u64 pci_addr, addr, size;
1014 int space_code;
1015 u32 flags;
1016 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -07001017 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -07001018
1019 if (len < cells_per_record)
1020 break;
1021 flags = fdt32_to_cpu(prop[0]);
1022 space_code = (flags >> 24) & 3;
1023 pci_addr = fdtdec_get_number(prop + 1, 2);
1024 prop += pci_addr_cells;
1025 addr = fdtdec_get_number(prop, addr_cells);
1026 prop += addr_cells;
1027 size = fdtdec_get_number(prop, size_cells);
1028 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001029 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
1030 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -07001031 if (space_code & 2) {
1032 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
1033 PCI_REGION_MEM;
1034 } else if (space_code & 1) {
1035 type = PCI_REGION_IO;
1036 } else {
1037 continue;
1038 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +03001039
1040 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
1041 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
Andrew Scullcb06f0e2022-04-21 16:11:07 +00001042 debug(" - pci_addr beyond the 32-bit boundary, ignoring\n");
1043 continue;
1044 }
1045
1046 if (!IS_ENABLED(CONFIG_PHYS_64BIT) && upper_32_bits(addr)) {
1047 debug(" - addr beyond the 32-bit boundary, ignoring\n");
1048 continue;
1049 }
1050
1051 if (~((pci_addr_t)0) - pci_addr < size) {
1052 debug(" - PCI range exceeds max address, ignoring\n");
1053 continue;
1054 }
1055
1056 if (~((phys_addr_t)0) - addr < size) {
1057 debug(" - phys range exceeds max address, ignoring\n");
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +03001058 continue;
1059 }
1060
Simon Glass7efc9ba2015-11-19 20:26:58 -07001061 pos = -1;
Suneel Garapati3ac3aec2019-10-19 17:10:20 -07001062 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
1063 for (i = 0; i < hose->region_count; i++) {
1064 if (hose->regions[i].flags == type)
1065 pos = i;
1066 }
Simon Glass7efc9ba2015-11-19 20:26:58 -07001067 }
Suneel Garapati3ac3aec2019-10-19 17:10:20 -07001068
Simon Glass7efc9ba2015-11-19 20:26:58 -07001069 if (pos == -1)
1070 pos = hose->region_count++;
1071 debug(" - type=%d, pos=%d\n", type, pos);
1072 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -07001073 }
1074
1075 /* Add a region for our local memory */
Stefan Roesebbc88462020-08-12 11:55:46 +02001076 bd = gd->bd;
Bin Mengae0bdde2018-03-27 00:46:05 -07001077 if (!bd)
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001078 return 0;
Bin Mengae0bdde2018-03-27 00:46:05 -07001079
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001080 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1081 if (bd->bi_dram[i].size) {
Daniel Schwierzeckf59925e2021-07-15 20:53:56 +02001082 phys_addr_t start = bd->bi_dram[i].start;
1083
1084 if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY))
1085 start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start);
1086
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001087 pci_set_region(hose->regions + hose->region_count++,
Daniel Schwierzeckf59925e2021-07-15 20:53:56 +02001088 start, start, bd->bi_dram[i].size,
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001089 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1090 }
1091 }
Simon Glassb94dc892015-03-05 12:25:25 -07001092
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001093 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001094}
1095
1096static int pci_uclass_pre_probe(struct udevice *bus)
1097{
1098 struct pci_controller *hose;
Simon Glassbe706102020-12-16 21:20:18 -07001099 struct uclass *uc;
1100 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001101
Simon Glass75e534b2020-12-16 21:20:07 -07001102 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -07001103 bus->parent->name);
Simon Glass95588622020-12-22 19:30:28 -07001104 hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001105
Simon Glassbe706102020-12-16 21:20:18 -07001106 /*
1107 * Set the sequence number, if device_bind() doesn't. We want control
1108 * of this so that numbers are allocated as devices are probed. That
1109 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1110 * higher than their parents)
1111 */
1112 if (dev_seq(bus) == -1) {
1113 ret = uclass_get(UCLASS_PCI, &uc);
1114 if (ret)
1115 return ret;
Simon Glass5e349922020-12-19 10:40:09 -07001116 bus->seq_ = uclass_find_next_free_seq(uc);
Simon Glassbe706102020-12-16 21:20:18 -07001117 }
1118
Simon Glassb94dc892015-03-05 12:25:25 -07001119 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +01001120 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -07001121 hose->ctlr = bus;
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001122 ret = decode_regions(hose, dev_ofnode(bus->parent),
1123 dev_ofnode(bus));
1124 if (ret)
1125 return ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001126 } else {
1127 struct pci_controller *parent_hose;
1128
1129 parent_hose = dev_get_uclass_priv(bus->parent);
1130 hose->ctlr = parent_hose->bus;
1131 }
Simon Glassbe706102020-12-16 21:20:18 -07001132
Simon Glassb94dc892015-03-05 12:25:25 -07001133 hose->bus = bus;
Simon Glass75e534b2020-12-16 21:20:07 -07001134 hose->first_busno = dev_seq(bus);
1135 hose->last_busno = dev_seq(bus);
Simon Glassf1d50f72020-12-19 10:40:13 -07001136 if (dev_has_ofnode(bus)) {
Suneel Garapatif8c86282020-05-04 21:25:25 -07001137 hose->skip_auto_config_until_reloc =
1138 dev_read_bool(bus,
1139 "u-boot,skip-auto-config-until-reloc");
1140 }
Simon Glassb94dc892015-03-05 12:25:25 -07001141
1142 return 0;
1143}
1144
1145static int pci_uclass_post_probe(struct udevice *bus)
1146{
Simon Glass68e35a72019-12-06 21:41:37 -07001147 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001148 int ret;
1149
Simon Glass75e534b2020-12-16 21:20:07 -07001150 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001151 ret = pci_bind_bus_devices(bus);
1152 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001153 return log_msg_ret("bind", ret);
Simon Glassb94dc892015-03-05 12:25:25 -07001154
Simon Glassbd165e72020-04-26 09:12:56 -06001155 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass68e35a72019-12-06 21:41:37 -07001156 (!hose->skip_auto_config_until_reloc ||
1157 (gd->flags & GD_FLG_RELOC))) {
1158 ret = pci_auto_config_devices(bus);
1159 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -07001160 return log_msg_ret("cfg", ret);
Simon Glass68e35a72019-12-06 21:41:37 -07001161 }
Simon Glassb94dc892015-03-05 12:25:25 -07001162
Bin Mengc0820a42015-08-20 06:40:23 -07001163#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1164 /*
1165 * Per Intel FSP specification, we should call FSP notify API to
1166 * inform FSP that PCI enumeration has been done so that FSP will
1167 * do any necessary initialization as required by the chipset's
1168 * BIOS Writer's Guide (BWG).
1169 *
1170 * Unfortunately we have to put this call here as with driver model,
1171 * the enumeration is all done on a lazy basis as needed, so until
1172 * something is touched on PCI it won't happen.
1173 *
1174 * Note we only call this 1) after U-Boot is relocated, and 2)
1175 * root bus has finished probing.
1176 */
Simon Glass75e534b2020-12-16 21:20:07 -07001177 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
Bin Mengc0820a42015-08-20 06:40:23 -07001178 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -06001179 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001180 return log_msg_ret("fsp", ret);
Simon Glassb072d522015-09-08 17:52:47 -06001181 }
Bin Mengc0820a42015-08-20 06:40:23 -07001182#endif
1183
Simon Glassb072d522015-09-08 17:52:47 -06001184 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001185}
1186
1187static int pci_uclass_child_post_bind(struct udevice *dev)
1188{
Simon Glassb75b15b2020-12-03 16:55:23 -07001189 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -07001190
Simon Glassf1d50f72020-12-19 10:40:13 -07001191 if (!dev_has_ofnode(dev))
Simon Glassb94dc892015-03-05 12:25:25 -07001192 return 0;
1193
Simon Glass71fa5b42020-12-03 16:55:18 -07001194 pplat = dev_get_parent_plat(dev);
Bin Meng00d808e2018-08-03 01:14:36 -07001195
1196 /* Extract vendor id and device id if available */
1197 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1198
1199 /* Extract the devfn from fdt_pci_addr */
Stefan Roesea74eb552019-01-25 11:52:42 +01001200 pplat->devfn = pci_get_devfn(dev);
Simon Glassb94dc892015-03-05 12:25:25 -07001201
1202 return 0;
1203}
1204
Simon Glass2a311e82020-01-27 08:49:37 -07001205static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng0a721522015-07-19 00:20:04 +08001206 uint offset, ulong *valuep,
1207 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001208{
Simon Glass95588622020-12-22 19:30:28 -07001209 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001210
1211 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1212}
1213
Bin Meng0a721522015-07-19 00:20:04 +08001214static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1215 uint offset, ulong value,
1216 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001217{
Simon Glass95588622020-12-22 19:30:28 -07001218 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001219
1220 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1221}
1222
Simon Glass04c8b6a2015-08-10 07:05:04 -06001223static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1224{
1225 struct udevice *dev;
Simon Glass04c8b6a2015-08-10 07:05:04 -06001226
1227 /*
1228 * Scan through all the PCI controllers. On x86 there will only be one
1229 * but that is not necessarily true on other hardware.
1230 */
Michal Suchanekfae28a32022-10-12 21:57:52 +02001231 while (bus) {
Simon Glass04c8b6a2015-08-10 07:05:04 -06001232 device_find_first_child(bus, &dev);
1233 if (dev) {
1234 *devp = dev;
1235 return 0;
1236 }
Michal Suchanek91c96fe2022-10-12 21:58:08 +02001237 uclass_next_device(&bus);
Michal Suchanekfae28a32022-10-12 21:57:52 +02001238 }
Simon Glass04c8b6a2015-08-10 07:05:04 -06001239
1240 return 0;
1241}
1242
1243int pci_find_next_device(struct udevice **devp)
1244{
1245 struct udevice *child = *devp;
1246 struct udevice *bus = child->parent;
Simon Glass04c8b6a2015-08-10 07:05:04 -06001247
1248 /* First try all the siblings */
1249 *devp = NULL;
1250 while (child) {
1251 device_find_next_child(&child);
1252 if (child) {
1253 *devp = child;
1254 return 0;
1255 }
1256 }
1257
1258 /* We ran out of siblings. Try the next bus */
Michal Suchanek91c96fe2022-10-12 21:58:08 +02001259 uclass_next_device(&bus);
Simon Glass04c8b6a2015-08-10 07:05:04 -06001260
1261 return bus ? skip_to_next_device(bus, devp) : 0;
1262}
1263
1264int pci_find_first_device(struct udevice **devp)
1265{
1266 struct udevice *bus;
Simon Glass04c8b6a2015-08-10 07:05:04 -06001267
1268 *devp = NULL;
Michal Suchanek91c96fe2022-10-12 21:58:08 +02001269 uclass_first_device(UCLASS_PCI, &bus);
Simon Glass04c8b6a2015-08-10 07:05:04 -06001270
1271 return skip_to_next_device(bus, devp);
1272}
1273
Simon Glass27a733f2015-11-19 20:26:59 -07001274ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1275{
1276 switch (size) {
1277 case PCI_SIZE_8:
1278 return (value >> ((offset & 3) * 8)) & 0xff;
1279 case PCI_SIZE_16:
1280 return (value >> ((offset & 2) * 8)) & 0xffff;
1281 default:
1282 return value;
1283 }
1284}
1285
1286ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1287 enum pci_size_t size)
1288{
1289 uint off_mask;
1290 uint val_mask, shift;
1291 ulong ldata, mask;
1292
1293 switch (size) {
1294 case PCI_SIZE_8:
1295 off_mask = 3;
1296 val_mask = 0xff;
1297 break;
1298 case PCI_SIZE_16:
1299 off_mask = 2;
1300 val_mask = 0xffff;
1301 break;
1302 default:
1303 return value;
1304 }
1305 shift = (offset & off_mask) * 8;
1306 ldata = (value & val_mask) << shift;
1307 mask = val_mask << shift;
1308 value = (old & ~mask) | ldata;
1309
1310 return value;
1311}
1312
Rayagonda Kokatanurcdc7ed32020-05-12 13:29:49 +05301313int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1314{
1315 int pci_addr_cells, addr_cells, size_cells;
1316 int cells_per_record;
1317 const u32 *prop;
1318 int len;
1319 int i = 0;
1320
1321 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1322 if (!prop) {
1323 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1324 dev->name);
1325 return -EINVAL;
1326 }
1327
1328 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1329 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1330 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1331
1332 /* PCI addresses are always 3-cells */
1333 len /= sizeof(u32);
1334 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1335 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1336 cells_per_record);
1337
1338 while (len) {
1339 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1340 prop += pci_addr_cells;
1341 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1342 prop += addr_cells;
1343 memp->size = fdtdec_get_number(prop, size_cells);
1344 prop += size_cells;
1345
1346 if (i == index)
1347 return 0;
1348 i++;
1349 len -= cells_per_record;
1350 }
1351
1352 return -EINVAL;
1353}
1354
Simon Glassdcdc0122015-11-19 20:27:01 -07001355int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1356 struct pci_region **memp, struct pci_region **prefp)
1357{
1358 struct udevice *bus = pci_get_controller(dev);
1359 struct pci_controller *hose = dev_get_uclass_priv(bus);
1360 int i;
1361
1362 *iop = NULL;
1363 *memp = NULL;
1364 *prefp = NULL;
1365 for (i = 0; i < hose->region_count; i++) {
1366 switch (hose->regions[i].flags) {
1367 case PCI_REGION_IO:
1368 if (!*iop || (*iop)->size < hose->regions[i].size)
1369 *iop = hose->regions + i;
1370 break;
1371 case PCI_REGION_MEM:
1372 if (!*memp || (*memp)->size < hose->regions[i].size)
1373 *memp = hose->regions + i;
1374 break;
1375 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1376 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1377 *prefp = hose->regions + i;
1378 break;
1379 }
1380 }
1381
1382 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1383}
1384
Simon Glassc92aac12020-01-27 08:49:38 -07001385u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glass3452cb12015-11-29 13:17:53 -07001386{
1387 u32 addr;
1388 int bar;
1389
1390 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1391 dm_pci_read_config32(dev, bar, &addr);
Simon Glass71fafd12020-04-09 10:27:36 -06001392
1393 /*
1394 * If we get an invalid address, return this so that comparisons with
1395 * FDT_ADDR_T_NONE work correctly
1396 */
1397 if (addr == 0xffffffff)
1398 return addr;
1399 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glass3452cb12015-11-29 13:17:53 -07001400 return addr & PCI_BASE_ADDRESS_IO_MASK;
1401 else
1402 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1403}
1404
Simon Glasse2b6b562016-01-18 20:19:15 -07001405void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1406{
1407 int bar;
1408
1409 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1410 dm_pci_write_config32(dev, bar, addr);
1411}
1412
Andrew Scull994b60d2022-04-21 16:11:11 +00001413phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1414 size_t len, unsigned long mask,
1415 unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001416{
Andrew Scull994b60d2022-04-21 16:11:11 +00001417 struct udevice *ctlr;
1418 struct pci_controller *hose;
Simon Glassc5f053b2015-11-29 13:18:03 -07001419 struct pci_region *res;
Andrew Scull3bf61522022-04-21 16:11:08 +00001420 pci_addr_t offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001421 int i;
1422
Andrew Scull994b60d2022-04-21 16:11:11 +00001423 /* The root controller has the region information */
1424 ctlr = pci_get_controller(dev);
1425 hose = dev_get_uclass_priv(ctlr);
1426
1427 if (hose->region_count == 0)
1428 return bus_addr;
Christian Gmeiner7241f802018-06-10 06:25:06 -07001429
Simon Glassc5f053b2015-11-29 13:18:03 -07001430 for (i = 0; i < hose->region_count; i++) {
1431 res = &hose->regions[i];
1432
Andrew Scull994b60d2022-04-21 16:11:11 +00001433 if ((res->flags & mask) != flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001434 continue;
1435
Andrew Scull3bf61522022-04-21 16:11:08 +00001436 if (bus_addr < res->bus_start)
1437 continue;
1438
1439 offset = bus_addr - res->bus_start;
1440 if (offset >= res->size)
1441 continue;
1442
1443 if (len > res->size - offset)
1444 continue;
1445
Andrew Scull994b60d2022-04-21 16:11:11 +00001446 return res->phys_start + offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001447 }
1448
Andrew Scull994b60d2022-04-21 16:11:11 +00001449 puts("pci_hose_bus_to_phys: invalid physical address\n");
1450 return 0;
Simon Glassc5f053b2015-11-29 13:18:03 -07001451}
1452
Andrew Scull994b60d2022-04-21 16:11:11 +00001453pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1454 size_t len, unsigned long mask,
1455 unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001456{
Simon Glassc5f053b2015-11-29 13:18:03 -07001457 struct udevice *ctlr;
Andrew Scull994b60d2022-04-21 16:11:11 +00001458 struct pci_controller *hose;
Simon Glassc5f053b2015-11-29 13:18:03 -07001459 struct pci_region *res;
Andrew Scull3bf61522022-04-21 16:11:08 +00001460 phys_addr_t offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001461 int i;
Simon Glassc5f053b2015-11-29 13:18:03 -07001462
1463 /* The root controller has the region information */
1464 ctlr = pci_get_controller(dev);
1465 hose = dev_get_uclass_priv(ctlr);
1466
Andrew Scull994b60d2022-04-21 16:11:11 +00001467 if (hose->region_count == 0)
1468 return phys_addr;
Christian Gmeiner7241f802018-06-10 06:25:06 -07001469
Simon Glassc5f053b2015-11-29 13:18:03 -07001470 for (i = 0; i < hose->region_count; i++) {
1471 res = &hose->regions[i];
1472
Andrew Scull994b60d2022-04-21 16:11:11 +00001473 if ((res->flags & mask) != flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001474 continue;
1475
Andrew Scull3bf61522022-04-21 16:11:08 +00001476 if (phys_addr < res->phys_start)
1477 continue;
1478
1479 offset = phys_addr - res->phys_start;
1480 if (offset >= res->size)
1481 continue;
Simon Glassc5f053b2015-11-29 13:18:03 -07001482
Andrew Scull3bf61522022-04-21 16:11:08 +00001483 if (len > res->size - offset)
1484 continue;
1485
Andrew Scull994b60d2022-04-21 16:11:11 +00001486 return res->bus_start + offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001487 }
1488
Andrew Scull994b60d2022-04-21 16:11:11 +00001489 puts("pci_hose_phys_to_bus: invalid physical address\n");
1490 return 0;
Simon Glassc5f053b2015-11-29 13:18:03 -07001491}
1492
Suneel Garapati5858ba82019-10-19 16:34:16 -07001493static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
Simon Glassb75b15b2020-12-03 16:55:23 -07001494 struct pci_child_plat *pdata)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001495{
1496 phys_addr_t addr = 0;
1497
1498 /*
1499 * In the case of a Virtual Function device using BAR
1500 * base and size, add offset for VFn BAR(1, 2, 3...n)
1501 */
1502 if (pdata->is_virtfn) {
1503 size_t sz;
1504 u32 ea_entry;
1505
1506 /* MaxOffset, 1st DW */
1507 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1508 sz = ea_entry & PCI_EA_FIELD_MASK;
1509 /* Fill up lower 2 bits */
1510 sz |= (~PCI_EA_FIELD_MASK);
1511
1512 if (ea_entry & PCI_EA_IS_64) {
1513 /* MaxOffset 2nd DW */
1514 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1515 sz |= ((u64)ea_entry) << 32;
1516 }
1517
1518 addr = (pdata->virtid - 1) * (sz + 1);
1519 }
1520
1521 return addr;
1522}
1523
Andrew Scull58c61022022-04-21 16:11:10 +00001524static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, size_t offset,
1525 size_t len, int ea_off,
Andrew Scull30d338d2022-04-21 16:11:06 +00001526 struct pci_child_plat *pdata)
Alex Marginean1c934a62019-06-07 11:24:23 +03001527{
1528 int ea_cnt, i, entry_size;
1529 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1530 u32 ea_entry;
1531 phys_addr_t addr;
1532
Suneel Garapati5858ba82019-10-19 16:34:16 -07001533 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1534 /*
1535 * In the case of a Virtual Function device, device is
1536 * Physical function, so pdata will point to required VF
1537 * specific data.
1538 */
1539 if (pdata->is_virtfn)
1540 bar_id += PCI_EA_BEI_VF_BAR0;
1541 }
1542
Alex Marginean1c934a62019-06-07 11:24:23 +03001543 /* EA capability structure header */
1544 dm_pci_read_config32(dev, ea_off, &ea_entry);
1545 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1546 ea_off += PCI_EA_FIRST_ENT;
1547
1548 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1549 /* Entry header */
1550 dm_pci_read_config32(dev, ea_off, &ea_entry);
1551 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1552
1553 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1554 continue;
1555
1556 /* Base address, 1st DW */
1557 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1558 addr = ea_entry & PCI_EA_FIELD_MASK;
1559 if (ea_entry & PCI_EA_IS_64) {
1560 /* Base address, 2nd DW, skip over 4B MaxOffset */
1561 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1562 addr |= ((u64)ea_entry) << 32;
1563 }
1564
Suneel Garapati5858ba82019-10-19 16:34:16 -07001565 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1566 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1567
Andrew Scull58c61022022-04-21 16:11:10 +00001568 if (~((phys_addr_t)0) - addr < offset)
1569 return NULL;
1570
Alex Marginean1c934a62019-06-07 11:24:23 +03001571 /* size ignored for now */
Andrew Scull58c61022022-04-21 16:11:10 +00001572 return map_physmem(addr + offset, len, MAP_NOCACHE);
Alex Marginean1c934a62019-06-07 11:24:23 +03001573 }
1574
1575 return 0;
1576}
1577
Andrew Scull58c61022022-04-21 16:11:10 +00001578void *dm_pci_map_bar(struct udevice *dev, int bar, size_t offset, size_t len,
Andrew Scull6520c822022-04-21 16:11:13 +00001579 unsigned long mask, unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001580{
Simon Glassb75b15b2020-12-03 16:55:23 -07001581 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
Suneel Garapati5858ba82019-10-19 16:34:16 -07001582 struct udevice *udev = dev;
Simon Glassc5f053b2015-11-29 13:18:03 -07001583 pci_addr_t pci_bus_addr;
1584 u32 bar_response;
Alex Marginean1c934a62019-06-07 11:24:23 +03001585 int ea_off;
1586
Suneel Garapati5858ba82019-10-19 16:34:16 -07001587 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1588 /*
1589 * In case of Virtual Function devices, use PF udevice
1590 * as EA capability is defined in Physical Function
1591 */
1592 if (pdata->is_virtfn)
1593 udev = pdata->pfdev;
1594 }
1595
Alex Marginean1c934a62019-06-07 11:24:23 +03001596 /*
1597 * if the function supports Enhanced Allocation use that instead of
1598 * BARs
Suneel Garapati5858ba82019-10-19 16:34:16 -07001599 * Incase of virtual functions, pdata will help read VF BEI
1600 * and EA entry size.
Alex Marginean1c934a62019-06-07 11:24:23 +03001601 */
Andrew Scull71e7e1a2022-04-21 16:11:16 +00001602 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
1603 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
1604 else
1605 ea_off = 0;
1606
Alex Marginean1c934a62019-06-07 11:24:23 +03001607 if (ea_off)
Andrew Scull58c61022022-04-21 16:11:10 +00001608 return dm_pci_map_ea_bar(udev, bar, offset, len, ea_off, pdata);
Simon Glassc5f053b2015-11-29 13:18:03 -07001609
1610 /* read BAR address */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001611 dm_pci_read_config32(udev, bar, &bar_response);
Simon Glassc5f053b2015-11-29 13:18:03 -07001612 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1613
Andrew Scull58c61022022-04-21 16:11:10 +00001614 if (~((pci_addr_t)0) - pci_bus_addr < offset)
1615 return NULL;
1616
Simon Glassc5f053b2015-11-29 13:18:03 -07001617 /*
Andrew Scull58c61022022-04-21 16:11:10 +00001618 * Forward the length argument to dm_pci_bus_to_virt. The length will
1619 * be used to check that the entire address range has been declared as
1620 * a PCI range, but a better check would be to probe for the size of
1621 * the bar and prevent overflow more locally.
Simon Glassc5f053b2015-11-29 13:18:03 -07001622 */
Andrew Scull6520c822022-04-21 16:11:13 +00001623 return dm_pci_bus_to_virt(udev, pci_bus_addr + offset, len, mask, flags,
1624 MAP_NOCACHE);
Simon Glassc5f053b2015-11-29 13:18:03 -07001625}
1626
Bin Meng631f3482018-10-15 02:21:21 -07001627static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001628{
Bin Menga7366f02018-08-03 01:14:52 -07001629 int ttl = PCI_FIND_CAP_TTL;
1630 u8 id;
1631 u16 ent;
Bin Menga7366f02018-08-03 01:14:52 -07001632
1633 dm_pci_read_config8(dev, pos, &pos);
Bin Meng631f3482018-10-15 02:21:21 -07001634
Bin Menga7366f02018-08-03 01:14:52 -07001635 while (ttl--) {
1636 if (pos < PCI_STD_HEADER_SIZEOF)
1637 break;
1638 pos &= ~3;
1639 dm_pci_read_config16(dev, pos, &ent);
1640
1641 id = ent & 0xff;
1642 if (id == 0xff)
1643 break;
1644 if (id == cap)
1645 return pos;
1646 pos = (ent >> 8);
1647 }
1648
1649 return 0;
1650}
1651
Bin Meng631f3482018-10-15 02:21:21 -07001652int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1653{
1654 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1655 cap);
1656}
1657
1658int dm_pci_find_capability(struct udevice *dev, int cap)
1659{
1660 u16 status;
1661 u8 header_type;
1662 u8 pos;
1663
1664 dm_pci_read_config16(dev, PCI_STATUS, &status);
1665 if (!(status & PCI_STATUS_CAP_LIST))
1666 return 0;
1667
1668 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1669 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1670 pos = PCI_CB_CAPABILITY_LIST;
1671 else
1672 pos = PCI_CAPABILITY_LIST;
1673
1674 return _dm_pci_find_next_capability(dev, pos, cap);
1675}
1676
1677int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001678{
1679 u32 header;
1680 int ttl;
1681 int pos = PCI_CFG_SPACE_SIZE;
1682
1683 /* minimum 8 bytes per capability */
1684 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1685
Bin Meng631f3482018-10-15 02:21:21 -07001686 if (start)
1687 pos = start;
1688
Bin Menga7366f02018-08-03 01:14:52 -07001689 dm_pci_read_config32(dev, pos, &header);
1690 /*
1691 * If we have no capabilities, this is indicated by cap ID,
1692 * cap version and next pointer all being 0.
1693 */
1694 if (header == 0)
1695 return 0;
1696
1697 while (ttl--) {
1698 if (PCI_EXT_CAP_ID(header) == cap)
1699 return pos;
1700
1701 pos = PCI_EXT_CAP_NEXT(header);
1702 if (pos < PCI_CFG_SPACE_SIZE)
1703 break;
1704
1705 dm_pci_read_config32(dev, pos, &header);
1706 }
1707
1708 return 0;
1709}
1710
Bin Meng631f3482018-10-15 02:21:21 -07001711int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1712{
1713 return dm_pci_find_next_ext_capability(dev, 0, cap);
1714}
1715
Alex Marginean09467d32019-06-07 11:24:25 +03001716int dm_pci_flr(struct udevice *dev)
1717{
1718 int pcie_off;
1719 u32 cap;
1720
1721 /* look for PCI Express Capability */
1722 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1723 if (!pcie_off)
1724 return -ENOENT;
1725
1726 /* check FLR capability */
1727 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1728 if (!(cap & PCI_EXP_DEVCAP_FLR))
1729 return -ENOENT;
1730
1731 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1732 PCI_EXP_DEVCTL_BCR_FLR);
1733
1734 /* wait 100ms, per PCI spec */
1735 mdelay(100);
1736
1737 return 0;
1738}
1739
Suneel Garapati13822f72019-10-19 16:07:20 -07001740#if defined(CONFIG_PCI_SRIOV)
1741int pci_sriov_init(struct udevice *pdev, int vf_en)
1742{
1743 u16 vendor, device;
1744 struct udevice *bus;
1745 struct udevice *dev;
1746 pci_dev_t bdf;
1747 u16 ctrl;
1748 u16 num_vfs;
1749 u16 total_vf;
1750 u16 vf_offset;
1751 u16 vf_stride;
1752 int vf, ret;
1753 int pos;
1754
1755 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1756 if (!pos) {
1757 debug("Error: SRIOV capability not found\n");
1758 return -ENOENT;
1759 }
1760
1761 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1762
1763 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1764 if (vf_en > total_vf)
1765 vf_en = total_vf;
1766 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1767
1768 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1769 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1770
1771 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1772 if (num_vfs > vf_en)
1773 num_vfs = vf_en;
1774
1775 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1776 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1777
1778 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1779 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1780
1781 bdf = dm_pci_get_bdf(pdev);
1782
Michal Suchanek4cd455e2022-09-25 13:08:16 +02001783 ret = pci_get_bus(PCI_BUS(bdf), &bus);
1784 if (ret)
1785 return ret;
Suneel Garapati13822f72019-10-19 16:07:20 -07001786
1787 bdf += PCI_BDF(0, 0, vf_offset);
1788
1789 for (vf = 0; vf < num_vfs; vf++) {
Simon Glassb75b15b2020-12-03 16:55:23 -07001790 struct pci_child_plat *pplat;
Suneel Garapati13822f72019-10-19 16:07:20 -07001791 ulong class;
1792
1793 pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1794 &class, PCI_SIZE_16);
1795
1796 debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -07001797 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Suneel Garapati13822f72019-10-19 16:07:20 -07001798
1799 /* Find this device in the device tree */
1800 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1801
1802 if (ret == -ENODEV) {
1803 struct pci_device_id find_id;
1804
1805 memset(&find_id, '\0', sizeof(find_id));
1806 find_id.vendor = vendor;
1807 find_id.device = device;
1808 find_id.class = class;
1809
1810 ret = pci_find_and_bind_driver(bus, &find_id,
1811 bdf, &dev);
1812
1813 if (ret)
1814 return ret;
1815 }
1816
1817 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -07001818 pplat = dev_get_parent_plat(dev);
Suneel Garapati13822f72019-10-19 16:07:20 -07001819 pplat->devfn = PCI_MASK_BUS(bdf);
1820 pplat->vendor = vendor;
1821 pplat->device = device;
1822 pplat->class = class;
1823 pplat->is_virtfn = true;
1824 pplat->pfdev = pdev;
1825 pplat->virtid = vf * vf_stride + vf_offset;
1826
1827 debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
Simon Glass75e534b2020-12-16 21:20:07 -07001828 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
Suneel Garapati13822f72019-10-19 16:07:20 -07001829 PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1830 bdf += PCI_BDF(0, 0, vf_stride);
1831 }
1832
1833 return 0;
1834}
1835
1836int pci_sriov_get_totalvfs(struct udevice *pdev)
1837{
1838 u16 total_vf;
1839 int pos;
1840
1841 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1842 if (!pos) {
1843 debug("Error: SRIOV capability not found\n");
1844 return -ENOENT;
1845 }
1846
1847 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1848
1849 return total_vf;
1850}
1851#endif /* SRIOV */
1852
Simon Glassb94dc892015-03-05 12:25:25 -07001853UCLASS_DRIVER(pci) = {
1854 .id = UCLASS_PCI,
1855 .name = "pci",
Simon Glassbe706102020-12-16 21:20:18 -07001856 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
Simon Glass18230342016-07-05 17:10:10 -06001857 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001858 .pre_probe = pci_uclass_pre_probe,
1859 .post_probe = pci_uclass_post_probe,
1860 .child_post_bind = pci_uclass_child_post_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001861 .per_device_auto = sizeof(struct pci_controller),
Simon Glassb75b15b2020-12-03 16:55:23 -07001862 .per_child_plat_auto = sizeof(struct pci_child_plat),
Simon Glassb94dc892015-03-05 12:25:25 -07001863};
1864
1865static const struct dm_pci_ops pci_bridge_ops = {
1866 .read_config = pci_bridge_read_config,
1867 .write_config = pci_bridge_write_config,
1868};
1869
1870static const struct udevice_id pci_bridge_ids[] = {
1871 { .compatible = "pci-bridge" },
1872 { }
1873};
1874
1875U_BOOT_DRIVER(pci_bridge_drv) = {
1876 .name = "pci_bridge_drv",
1877 .id = UCLASS_PCI,
1878 .of_match = pci_bridge_ids,
1879 .ops = &pci_bridge_ops,
1880};
1881
1882UCLASS_DRIVER(pci_generic) = {
1883 .id = UCLASS_PCI_GENERIC,
1884 .name = "pci_generic",
1885};
1886
1887static const struct udevice_id pci_generic_ids[] = {
1888 { .compatible = "pci-generic" },
1889 { }
1890};
1891
1892U_BOOT_DRIVER(pci_generic_drv) = {
1893 .name = "pci_generic_drv",
1894 .id = UCLASS_PCI_GENERIC,
1895 .of_match = pci_generic_ids,
1896};
Stephen Warren04eb2692016-01-26 11:10:11 -07001897
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001898int pci_init(void)
Stephen Warren04eb2692016-01-26 11:10:11 -07001899{
1900 struct udevice *bus;
1901
1902 /*
1903 * Enumerate all known controller devices. Enumeration has the side-
1904 * effect of probing them, so PCIe devices will be enumerated too.
1905 */
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001906 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warren04eb2692016-01-26 11:10:11 -07001907 bus;
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001908 uclass_next_device_check(&bus)) {
Stephen Warren04eb2692016-01-26 11:10:11 -07001909 ;
1910 }
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001911
1912 return 0;
Stephen Warren04eb2692016-01-26 11:10:11 -07001913}