blob: 0bd604d061d5c38030d98fe5f5f8fae582ceff77 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassff3e0772015-03-05 12:25:25 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassff3e0772015-03-05 12:25:25 -07005 */
6
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_PCI
8
Simon Glassff3e0772015-03-05 12:25:25 -07009#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060012#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glassff3e0772015-03-05 12:25:25 -070015#include <pci.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glass21d1fe72015-11-29 13:18:03 -070017#include <asm/io.h>
Simon Glassff3e0772015-03-05 12:25:25 -070018#include <dm/device-internal.h>
Simon Glassbf501592017-05-18 20:09:51 -060019#include <dm/lists.h>
Simon Glass42f36632020-12-16 21:20:18 -070020#include <dm/uclass-internal.h>
Bin Meng348b7442015-08-20 06:40:23 -070021#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
Simon Glass07f2f582019-08-24 14:19:05 -060022#include <asm/fsp/fsp_support.h>
Bin Meng348b7442015-08-20 06:40:23 -070023#endif
Simon Glassc05ed002020-05-10 11:40:11 -060024#include <linux/delay.h>
Simon Glass5e23b8b2015-11-29 13:17:49 -070025#include "pci_internal.h"
Simon Glassff3e0772015-03-05 12:25:25 -070026
27DECLARE_GLOBAL_DATA_PTR;
28
Simon Glassa6eb93b2016-01-18 20:19:14 -070029int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass983c6ba22015-08-31 18:55:35 -060030{
31 int ret;
32
33 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
34
35 /* Since buses may not be numbered yet try a little harder with bus 0 */
36 if (ret == -ENODEV) {
Simon Glass3f603cb2016-02-11 13:23:26 -070037 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass983c6ba22015-08-31 18:55:35 -060038 if (ret)
39 return ret;
Simon Glass983c6ba22015-08-31 18:55:35 -060040 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
41 }
42
43 return ret;
44}
45
Simon Glass9f60fb02015-11-19 20:27:00 -070046struct udevice *pci_get_controller(struct udevice *dev)
47{
48 while (device_is_on_pci_bus(dev))
49 dev = dev->parent;
50
51 return dev;
52}
53
Simon Glass194fca92020-01-27 08:49:38 -070054pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glass4b515e42015-07-06 16:47:46 -060055{
Simon Glass8a8d24b2020-12-03 16:55:23 -070056 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glass4b515e42015-07-06 16:47:46 -060057 struct udevice *bus = dev->parent;
58
Simon Glass48862872019-12-29 21:19:14 -070059 /*
60 * This error indicates that @dev is a device on an unprobed PCI bus.
61 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
62 * will produce a bad BDF>
63 *
64 * A common cause of this problem is that this function is called in the
Simon Glassd1998a92020-12-03 16:55:21 -070065 * of_to_plat() method of @dev. Accessing the PCI bus in that
Simon Glass48862872019-12-29 21:19:14 -070066 * method is not allowed, since it has not yet been probed. To fix this,
67 * move that access to the probe() method of @dev instead.
68 */
69 if (!device_active(bus))
70 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
71 bus->name);
Simon Glass8b85dfc2020-12-16 21:20:07 -070072 return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
Simon Glass4b515e42015-07-06 16:47:46 -060073}
74
Simon Glassff3e0772015-03-05 12:25:25 -070075/**
76 * pci_get_bus_max() - returns the bus number of the last active bus
77 *
78 * @return last bus number, or -1 if no active buses
79 */
80static int pci_get_bus_max(void)
81{
82 struct udevice *bus;
83 struct uclass *uc;
84 int ret = -1;
85
86 ret = uclass_get(UCLASS_PCI, &uc);
87 uclass_foreach_dev(bus, uc) {
Simon Glass8b85dfc2020-12-16 21:20:07 -070088 if (dev_seq(bus) > ret)
89 ret = dev_seq(bus);
Simon Glassff3e0772015-03-05 12:25:25 -070090 }
91
92 debug("%s: ret=%d\n", __func__, ret);
93
94 return ret;
95}
96
97int pci_last_busno(void)
98{
Bin Meng069155c2015-10-01 00:36:01 -070099 return pci_get_bus_max();
Simon Glassff3e0772015-03-05 12:25:25 -0700100}
101
102int pci_get_ff(enum pci_size_t size)
103{
104 switch (size) {
105 case PCI_SIZE_8:
106 return 0xff;
107 case PCI_SIZE_16:
108 return 0xffff;
109 default:
110 return 0xffffffff;
111 }
112}
113
Marek Vasut02e4d382018-10-10 21:27:06 +0200114static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
115 ofnode *rnode)
116{
117 struct fdt_pci_addr addr;
118 ofnode node;
119 int ret;
120
121 dev_for_each_subnode(node, bus) {
122 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
123 &addr);
124 if (ret)
125 continue;
126
127 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
128 continue;
129
130 *rnode = node;
131 break;
132 }
133};
134
Simon Glassc4e72c42020-01-27 08:49:37 -0700135int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassff3e0772015-03-05 12:25:25 -0700136 struct udevice **devp)
137{
138 struct udevice *dev;
139
140 for (device_find_first_child(bus, &dev);
141 dev;
142 device_find_next_child(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700143 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700144
Simon Glasscaa4daa2020-12-03 16:55:18 -0700145 pplat = dev_get_parent_plat(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700146 if (pplat && pplat->devfn == find_devfn) {
147 *devp = dev;
148 return 0;
149 }
150 }
151
152 return -ENODEV;
153}
154
Simon Glassf3f1fae2015-11-29 13:17:48 -0700155int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassff3e0772015-03-05 12:25:25 -0700156{
157 struct udevice *bus;
158 int ret;
159
Simon Glass983c6ba22015-08-31 18:55:35 -0600160 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700161 if (ret)
162 return ret;
163 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
164}
165
166static int pci_device_matches_ids(struct udevice *dev,
167 struct pci_device_id *ids)
168{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700169 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700170 int i;
171
Simon Glasscaa4daa2020-12-03 16:55:18 -0700172 pplat = dev_get_parent_plat(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700173 if (!pplat)
174 return -EINVAL;
175 for (i = 0; ids[i].vendor != 0; i++) {
176 if (pplat->vendor == ids[i].vendor &&
177 pplat->device == ids[i].device)
178 return i;
179 }
180
181 return -EINVAL;
182}
183
184int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
185 int *indexp, struct udevice **devp)
186{
187 struct udevice *dev;
188
189 /* Scan all devices on this bus */
190 for (device_find_first_child(bus, &dev);
191 dev;
192 device_find_next_child(&dev)) {
193 if (pci_device_matches_ids(dev, ids) >= 0) {
194 if ((*indexp)-- <= 0) {
195 *devp = dev;
196 return 0;
197 }
198 }
199 }
200
201 return -ENODEV;
202}
203
204int pci_find_device_id(struct pci_device_id *ids, int index,
205 struct udevice **devp)
206{
207 struct udevice *bus;
208
209 /* Scan all known buses */
210 for (uclass_first_device(UCLASS_PCI, &bus);
211 bus;
212 uclass_next_device(&bus)) {
213 if (!pci_bus_find_devices(bus, ids, &index, devp))
214 return 0;
215 }
216 *devp = NULL;
217
218 return -ENODEV;
219}
220
Simon Glass5c0bf642015-11-29 13:17:50 -0700221static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
222 unsigned int device, int *indexp,
223 struct udevice **devp)
224{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700225 struct pci_child_plat *pplat;
Simon Glass5c0bf642015-11-29 13:17:50 -0700226 struct udevice *dev;
227
228 for (device_find_first_child(bus, &dev);
229 dev;
230 device_find_next_child(&dev)) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700231 pplat = dev_get_parent_plat(dev);
Simon Glass5c0bf642015-11-29 13:17:50 -0700232 if (pplat->vendor == vendor && pplat->device == device) {
233 if (!(*indexp)--) {
234 *devp = dev;
235 return 0;
236 }
237 }
238 }
239
240 return -ENODEV;
241}
242
243int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
244 struct udevice **devp)
245{
246 struct udevice *bus;
247
248 /* Scan all known buses */
249 for (uclass_first_device(UCLASS_PCI, &bus);
250 bus;
251 uclass_next_device(&bus)) {
252 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
253 return device_probe(*devp);
254 }
255 *devp = NULL;
256
257 return -ENODEV;
258}
259
Simon Glassa0eb8352015-11-29 13:17:52 -0700260int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
261{
262 struct udevice *dev;
263
264 /* Scan all known buses */
265 for (pci_find_first_device(&dev);
266 dev;
267 pci_find_next_device(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700268 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassa0eb8352015-11-29 13:17:52 -0700269
270 if (pplat->class == find_class && !index--) {
271 *devp = dev;
272 return device_probe(*devp);
273 }
274 }
275 *devp = NULL;
276
277 return -ENODEV;
278}
279
Simon Glassff3e0772015-03-05 12:25:25 -0700280int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
281 unsigned long value, enum pci_size_t size)
282{
283 struct dm_pci_ops *ops;
284
285 ops = pci_get_ops(bus);
286 if (!ops->write_config)
287 return -ENOSYS;
288 return ops->write_config(bus, bdf, offset, value, size);
289}
290
Simon Glass319dba12016-03-06 19:27:52 -0700291int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
292 u32 clr, u32 set)
293{
294 ulong val;
295 int ret;
296
297 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
298 if (ret)
299 return ret;
300 val &= ~clr;
301 val |= set;
302
303 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
304}
305
Simon Glassff3e0772015-03-05 12:25:25 -0700306int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
307 enum pci_size_t size)
308{
309 struct udevice *bus;
310 int ret;
311
Simon Glass983c6ba22015-08-31 18:55:35 -0600312 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700313 if (ret)
314 return ret;
315
Bin Meng4d8615c2015-07-19 00:20:04 +0800316 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700317}
318
Simon Glass66afb4e2015-08-10 07:05:03 -0600319int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
320 enum pci_size_t size)
321{
322 struct udevice *bus;
323
Bin Meng1e0f2262015-09-11 03:24:34 -0700324 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600325 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700326 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
327 size);
Simon Glass66afb4e2015-08-10 07:05:03 -0600328}
329
Simon Glassff3e0772015-03-05 12:25:25 -0700330int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
331{
332 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
333}
334
335int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
336{
337 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
338}
339
340int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
341{
342 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
343}
344
Simon Glass66afb4e2015-08-10 07:05:03 -0600345int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
346{
347 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
348}
349
350int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
351{
352 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
353}
354
355int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
356{
357 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
358}
359
Simon Glass194fca92020-01-27 08:49:38 -0700360int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassff3e0772015-03-05 12:25:25 -0700361 unsigned long *valuep, enum pci_size_t size)
362{
363 struct dm_pci_ops *ops;
364
365 ops = pci_get_ops(bus);
366 if (!ops->read_config)
367 return -ENOSYS;
368 return ops->read_config(bus, bdf, offset, valuep, size);
369}
370
371int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
372 enum pci_size_t size)
373{
374 struct udevice *bus;
375 int ret;
376
Simon Glass983c6ba22015-08-31 18:55:35 -0600377 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700378 if (ret)
379 return ret;
380
Bin Meng4d8615c2015-07-19 00:20:04 +0800381 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700382}
383
Simon Glass194fca92020-01-27 08:49:38 -0700384int dm_pci_read_config(const struct udevice *dev, int offset,
385 unsigned long *valuep, enum pci_size_t size)
Simon Glass66afb4e2015-08-10 07:05:03 -0600386{
Simon Glass194fca92020-01-27 08:49:38 -0700387 const struct udevice *bus;
Simon Glass66afb4e2015-08-10 07:05:03 -0600388
Bin Meng1e0f2262015-09-11 03:24:34 -0700389 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600390 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700391 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass66afb4e2015-08-10 07:05:03 -0600392 size);
393}
394
Simon Glassff3e0772015-03-05 12:25:25 -0700395int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
396{
397 unsigned long value;
398 int ret;
399
400 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
401 if (ret)
402 return ret;
403 *valuep = value;
404
405 return 0;
406}
407
408int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
409{
410 unsigned long value;
411 int ret;
412
413 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
414 if (ret)
415 return ret;
416 *valuep = value;
417
418 return 0;
419}
420
421int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
422{
423 unsigned long value;
424 int ret;
425
426 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
427 if (ret)
428 return ret;
429 *valuep = value;
430
431 return 0;
432}
433
Simon Glass194fca92020-01-27 08:49:38 -0700434int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600435{
436 unsigned long value;
437 int ret;
438
439 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
440 if (ret)
441 return ret;
442 *valuep = value;
443
444 return 0;
445}
446
Simon Glass194fca92020-01-27 08:49:38 -0700447int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600448{
449 unsigned long value;
450 int ret;
451
452 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
453 if (ret)
454 return ret;
455 *valuep = value;
456
457 return 0;
458}
459
Simon Glass194fca92020-01-27 08:49:38 -0700460int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600461{
462 unsigned long value;
463 int ret;
464
465 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
466 if (ret)
467 return ret;
468 *valuep = value;
469
470 return 0;
471}
472
Simon Glass319dba12016-03-06 19:27:52 -0700473int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
474{
475 u8 val;
476 int ret;
477
478 ret = dm_pci_read_config8(dev, offset, &val);
479 if (ret)
480 return ret;
481 val &= ~clr;
482 val |= set;
483
484 return dm_pci_write_config8(dev, offset, val);
485}
486
487int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
488{
489 u16 val;
490 int ret;
491
492 ret = dm_pci_read_config16(dev, offset, &val);
493 if (ret)
494 return ret;
495 val &= ~clr;
496 val |= set;
497
498 return dm_pci_write_config16(dev, offset, val);
499}
500
501int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
502{
503 u32 val;
504 int ret;
505
506 ret = dm_pci_read_config32(dev, offset, &val);
507 if (ret)
508 return ret;
509 val &= ~clr;
510 val |= set;
511
512 return dm_pci_write_config32(dev, offset, val);
513}
514
Bin Mengbbbcb522015-10-01 00:36:02 -0700515static void set_vga_bridge_bits(struct udevice *dev)
516{
517 struct udevice *parent = dev->parent;
518 u16 bc;
519
Simon Glass8b85dfc2020-12-16 21:20:07 -0700520 while (dev_seq(parent) != 0) {
Bin Mengbbbcb522015-10-01 00:36:02 -0700521 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
522 bc |= PCI_BRIDGE_CTL_VGA;
523 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
524 parent = parent->parent;
525 }
526}
527
Simon Glassff3e0772015-03-05 12:25:25 -0700528int pci_auto_config_devices(struct udevice *bus)
529{
Simon Glass0fd3d912020-12-22 19:30:28 -0700530 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700531 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700532 unsigned int sub_bus;
533 struct udevice *dev;
534 int ret;
535
Simon Glass8b85dfc2020-12-16 21:20:07 -0700536 sub_bus = dev_seq(bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700537 debug("%s: start\n", __func__);
538 pciauto_config_init(hose);
539 for (ret = device_find_first_child(bus, &dev);
540 !ret && dev;
541 ret = device_find_next_child(&dev)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700542 unsigned int max_bus;
Simon Glass4d214552015-09-08 17:52:47 -0600543 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700544
Simon Glassff3e0772015-03-05 12:25:25 -0700545 debug("%s: device %s\n", __func__, dev->name);
Simon Glass7d14ee42020-12-19 10:40:13 -0700546 if (dev_has_ofnode(dev) &&
Suneel Garapatif0c36922020-05-04 21:25:25 -0700547 dev_read_bool(dev, "pci,no-autoconfig"))
Simon Glassd8c7fb52020-04-08 16:57:26 -0600548 continue;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700549 ret = dm_pciauto_config_device(dev);
Simon Glass4d214552015-09-08 17:52:47 -0600550 if (ret < 0)
Simon Glass42f36632020-12-16 21:20:18 -0700551 return log_msg_ret("auto", ret);
Simon Glass4d214552015-09-08 17:52:47 -0600552 max_bus = ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700553 sub_bus = max(sub_bus, max_bus);
Bin Mengbbbcb522015-10-01 00:36:02 -0700554
Simon Glasscaa4daa2020-12-03 16:55:18 -0700555 pplat = dev_get_parent_plat(dev);
Bin Mengbbbcb522015-10-01 00:36:02 -0700556 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
557 set_vga_bridge_bits(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700558 }
559 debug("%s: done\n", __func__);
560
Simon Glass42f36632020-12-16 21:20:18 -0700561 return log_msg_ret("sub", sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700562}
563
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300564int pci_generic_mmap_write_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700565 const struct udevice *bus,
566 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
567 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300568 pci_dev_t bdf,
569 uint offset,
570 ulong value,
571 enum pci_size_t size)
572{
573 void *address;
574
575 if (addr_f(bus, bdf, offset, &address) < 0)
576 return 0;
577
578 switch (size) {
579 case PCI_SIZE_8:
580 writeb(value, address);
581 return 0;
582 case PCI_SIZE_16:
583 writew(value, address);
584 return 0;
585 case PCI_SIZE_32:
586 writel(value, address);
587 return 0;
588 default:
589 return -EINVAL;
590 }
591}
592
593int pci_generic_mmap_read_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700594 const struct udevice *bus,
595 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
596 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300597 pci_dev_t bdf,
598 uint offset,
599 ulong *valuep,
600 enum pci_size_t size)
601{
602 void *address;
603
604 if (addr_f(bus, bdf, offset, &address) < 0) {
605 *valuep = pci_get_ff(size);
606 return 0;
607 }
608
609 switch (size) {
610 case PCI_SIZE_8:
611 *valuep = readb(address);
612 return 0;
613 case PCI_SIZE_16:
614 *valuep = readw(address);
615 return 0;
616 case PCI_SIZE_32:
617 *valuep = readl(address);
618 return 0;
619 default:
620 return -EINVAL;
621 }
622}
623
Simon Glass5e23b8b2015-11-29 13:17:49 -0700624int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassff3e0772015-03-05 12:25:25 -0700625{
Simon Glassff3e0772015-03-05 12:25:25 -0700626 int sub_bus;
627 int ret;
Suneel Garapati636cc172019-10-19 15:52:32 -0700628 int ea_pos;
629 u8 reg;
Simon Glassff3e0772015-03-05 12:25:25 -0700630
631 debug("%s\n", __func__);
Simon Glassff3e0772015-03-05 12:25:25 -0700632
Suneel Garapati636cc172019-10-19 15:52:32 -0700633 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
634 if (ea_pos) {
635 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
636 &reg);
637 sub_bus = reg;
638 } else {
639 sub_bus = pci_get_bus_max() + 1;
640 }
Simon Glassff3e0772015-03-05 12:25:25 -0700641 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700642 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700643
644 ret = device_probe(bus);
645 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600646 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -0700647 ret);
Simon Glass42f36632020-12-16 21:20:18 -0700648 return log_msg_ret("probe", ret);
Simon Glassff3e0772015-03-05 12:25:25 -0700649 }
Suneel Garapati636cc172019-10-19 15:52:32 -0700650
Masami Hiramatsu19e1b8d2021-04-16 14:53:46 -0700651 if (!ea_pos)
652 sub_bus = pci_get_bus_max();
653
Simon Glass5e23b8b2015-11-29 13:17:49 -0700654 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700655
656 return sub_bus;
657}
658
Simon Glassaba92962015-07-06 16:47:44 -0600659/**
660 * pci_match_one_device - Tell if a PCI device structure has a matching
661 * PCI device id structure
662 * @id: single PCI device id structure to match
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800663 * @find: the PCI device id structure to match against
Simon Glassaba92962015-07-06 16:47:44 -0600664 *
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800665 * Returns true if the finding pci_device_id structure matched or false if
666 * there is no match.
Simon Glassaba92962015-07-06 16:47:44 -0600667 */
668static bool pci_match_one_id(const struct pci_device_id *id,
669 const struct pci_device_id *find)
670{
671 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
672 (id->device == PCI_ANY_ID || id->device == find->device) &&
673 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
674 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
675 !((id->class ^ find->class) & id->class_mask))
676 return true;
677
678 return false;
679}
680
681/**
682 * pci_find_and_bind_driver() - Find and bind the right PCI driver
683 *
684 * This only looks at certain fields in the descriptor.
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600685 *
686 * @parent: Parent bus
687 * @find_id: Specification of the driver to find
688 * @bdf: Bus/device/function addreess - see PCI_BDF()
689 * @devp: Returns a pointer to the device created
690 * @return 0 if OK, -EPERM if the device is not needed before relocation and
691 * therefore was not created, other -ve value on error
Simon Glassaba92962015-07-06 16:47:44 -0600692 */
693static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600694 struct pci_device_id *find_id,
695 pci_dev_t bdf, struct udevice **devp)
Simon Glassaba92962015-07-06 16:47:44 -0600696{
697 struct pci_driver_entry *start, *entry;
Marek Vasut02e4d382018-10-10 21:27:06 +0200698 ofnode node = ofnode_null();
Simon Glassaba92962015-07-06 16:47:44 -0600699 const char *drv;
700 int n_ents;
701 int ret;
702 char name[30], *str;
Bin Meng08fc7b82015-08-20 06:40:17 -0700703 bool bridge;
Simon Glassaba92962015-07-06 16:47:44 -0600704
705 *devp = NULL;
706
707 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
708 find_id->vendor, find_id->device);
Marek Vasut02e4d382018-10-10 21:27:06 +0200709
710 /* Determine optional OF node */
Suneel Garapatibc301402019-10-19 16:02:48 -0700711 if (ofnode_valid(dev_ofnode(parent)))
712 pci_dev_find_ofnode(parent, bdf, &node);
Marek Vasut02e4d382018-10-10 21:27:06 +0200713
Michael Wallea6cd5972019-12-01 17:45:18 +0100714 if (ofnode_valid(node) && !ofnode_is_available(node)) {
715 debug("%s: Ignoring disabled device\n", __func__);
Simon Glass42f36632020-12-16 21:20:18 -0700716 return log_msg_ret("dis", -EPERM);
Michael Wallea6cd5972019-12-01 17:45:18 +0100717 }
718
Simon Glassaba92962015-07-06 16:47:44 -0600719 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
720 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
721 for (entry = start; entry != start + n_ents; entry++) {
722 const struct pci_device_id *id;
723 struct udevice *dev;
724 const struct driver *drv;
725
726 for (id = entry->match;
727 id->vendor || id->subvendor || id->class_mask;
728 id++) {
729 if (!pci_match_one_id(id, find_id))
730 continue;
731
732 drv = entry->driver;
Bin Meng08fc7b82015-08-20 06:40:17 -0700733
734 /*
735 * In the pre-relocation phase, we only bind devices
736 * whose driver has the DM_FLAG_PRE_RELOC set, to save
737 * precious memory space as on some platforms as that
738 * space is pretty limited (ie: using Cache As RAM).
739 */
740 if (!(gd->flags & GD_FLG_RELOC) &&
741 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glass42f36632020-12-16 21:20:18 -0700742 return log_msg_ret("pre", -EPERM);
Bin Meng08fc7b82015-08-20 06:40:17 -0700743
Simon Glassaba92962015-07-06 16:47:44 -0600744 /*
745 * We could pass the descriptor to the driver as
Simon Glasscaa4daa2020-12-03 16:55:18 -0700746 * plat (instead of NULL) and allow its bind()
Simon Glassaba92962015-07-06 16:47:44 -0600747 * method to return -ENOENT if it doesn't support this
748 * device. That way we could continue the search to
749 * find another driver. For now this doesn't seem
750 * necesssary, so just bind the first match.
751 */
Simon Glass734206d2020-11-28 17:50:01 -0700752 ret = device_bind(parent, drv, drv->name, NULL, node,
753 &dev);
Simon Glassaba92962015-07-06 16:47:44 -0600754 if (ret)
755 goto error;
756 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menged698aa2018-08-03 01:14:44 -0700757 dev->driver_data = id->driver_data;
Simon Glassaba92962015-07-06 16:47:44 -0600758 *devp = dev;
759 return 0;
760 }
761 }
762
Bin Meng08fc7b82015-08-20 06:40:17 -0700763 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
764 /*
765 * In the pre-relocation phase, we only bind bridge devices to save
766 * precious memory space as on some platforms as that space is pretty
767 * limited (ie: using Cache As RAM).
768 */
769 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glass42f36632020-12-16 21:20:18 -0700770 return log_msg_ret("notbr", -EPERM);
Bin Meng08fc7b82015-08-20 06:40:17 -0700771
Simon Glassaba92962015-07-06 16:47:44 -0600772 /* Bind a generic driver so that the device can be used */
Simon Glass8b85dfc2020-12-16 21:20:07 -0700773 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
Bin Meng4d8615c2015-07-19 00:20:04 +0800774 PCI_FUNC(bdf));
Simon Glassaba92962015-07-06 16:47:44 -0600775 str = strdup(name);
776 if (!str)
777 return -ENOMEM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700778 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
779
Marek Vasut02e4d382018-10-10 21:27:06 +0200780 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glassaba92962015-07-06 16:47:44 -0600781 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600782 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dec42640c2017-05-08 20:40:16 +0200783 free(str);
Simon Glassaba92962015-07-06 16:47:44 -0600784 return ret;
785 }
786 debug("%s: No match found: bound generic driver instead\n", __func__);
787
788 return 0;
789
790error:
791 debug("%s: No match found: error %d\n", __func__, ret);
792 return ret;
793}
794
Tim Harveycecd0132021-04-16 14:53:47 -0700795__weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
796{
797}
798
Simon Glassff3e0772015-03-05 12:25:25 -0700799int pci_bind_bus_devices(struct udevice *bus)
800{
801 ulong vendor, device;
802 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800803 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700804 bool found_multi;
Suneel Garapatia3fac3f2019-10-23 18:40:36 -0700805 int ari_off;
Simon Glassff3e0772015-03-05 12:25:25 -0700806 int ret;
807
808 found_multi = false;
Simon Glass8b85dfc2020-12-16 21:20:07 -0700809 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
Bin Meng4d8615c2015-07-19 00:20:04 +0800810 PCI_MAX_PCI_FUNCTIONS - 1);
Simon Glass8b85dfc2020-12-16 21:20:07 -0700811 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
Bin Meng4d8615c2015-07-19 00:20:04 +0800812 bdf += PCI_BDF(0, 0, 1)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700813 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700814 struct udevice *dev;
815 ulong class;
816
Bin Meng64e45f72018-08-03 01:14:37 -0700817 if (!PCI_FUNC(bdf))
818 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800819 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700820 continue;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800821
Simon Glassff3e0772015-03-05 12:25:25 -0700822 /* Check only the first access, we don't expect problems */
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800823 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
824 PCI_SIZE_16);
Simon Glassff3e0772015-03-05 12:25:25 -0700825 if (ret)
826 goto error;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800827
Simon Glassff3e0772015-03-05 12:25:25 -0700828 if (vendor == 0xffff || vendor == 0x0000)
829 continue;
830
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800831 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
832 &header_type, PCI_SIZE_8);
833
Bin Meng4d8615c2015-07-19 00:20:04 +0800834 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-03-05 12:25:25 -0700835 found_multi = header_type & 0x80;
836
Simon Glass09115692019-09-25 08:56:12 -0600837 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Simon Glass8b85dfc2020-12-16 21:20:07 -0700838 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Bin Meng4d8615c2015-07-19 00:20:04 +0800839 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassff3e0772015-03-05 12:25:25 -0700840 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800841 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600842 PCI_SIZE_32);
843 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700844
845 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800846 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass09115692019-09-25 08:56:12 -0600847 debug(": find ret=%d\n", ret);
Simon Glassff3e0772015-03-05 12:25:25 -0700848
Simon Glass8bd42522015-11-29 13:18:09 -0700849 /* If nothing in the device tree, bind a device */
Simon Glassff3e0772015-03-05 12:25:25 -0700850 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600851 struct pci_device_id find_id;
852 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700853
Simon Glassaba92962015-07-06 16:47:44 -0600854 memset(&find_id, '\0', sizeof(find_id));
855 find_id.vendor = vendor;
856 find_id.device = device;
857 find_id.class = class;
858 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng4d8615c2015-07-19 00:20:04 +0800859 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600860 PCI_SUBSYSTEM_VENDOR_ID,
861 &val, PCI_SIZE_32);
862 find_id.subvendor = val & 0xffff;
863 find_id.subdevice = val >> 16;
864 }
Bin Meng4d8615c2015-07-19 00:20:04 +0800865 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600866 &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700867 }
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600868 if (ret == -EPERM)
869 continue;
870 else if (ret)
Simon Glassff3e0772015-03-05 12:25:25 -0700871 return ret;
872
873 /* Update the platform data */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700874 pplat = dev_get_parent_plat(dev);
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600875 pplat->devfn = PCI_MASK_BUS(bdf);
876 pplat->vendor = vendor;
877 pplat->device = device;
878 pplat->class = class;
Suneel Garapatia3fac3f2019-10-23 18:40:36 -0700879
880 if (IS_ENABLED(CONFIG_PCI_ARID)) {
881 ari_off = dm_pci_find_ext_capability(dev,
882 PCI_EXT_CAP_ID_ARI);
883 if (ari_off) {
884 u16 ari_cap;
885
886 /*
887 * Read Next Function number in ARI Cap
888 * Register
889 */
890 dm_pci_read_config16(dev, ari_off + 4,
891 &ari_cap);
892 /*
893 * Update next scan on this function number,
894 * subtract 1 in BDF to satisfy loop increment.
895 */
896 if (ari_cap & 0xff00) {
897 bdf = PCI_BDF(PCI_BUS(bdf),
898 PCI_DEV(ari_cap),
899 PCI_FUNC(ari_cap));
900 bdf = bdf - 0x100;
901 }
902 }
903 }
Tim Harveycecd0132021-04-16 14:53:47 -0700904
905 board_pci_fixup_dev(bus, dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700906 }
907
908 return 0;
909error:
910 printf("Cannot read bus configuration: %d\n", ret);
911
912 return ret;
913}
914
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700915static void decode_regions(struct pci_controller *hose, ofnode parent_node,
916 ofnode node)
Simon Glassff3e0772015-03-05 12:25:25 -0700917{
918 int pci_addr_cells, addr_cells, size_cells;
919 int cells_per_record;
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200920 struct bd_info *bd;
Simon Glassff3e0772015-03-05 12:25:25 -0700921 const u32 *prop;
Stefan Roesee0024742020-07-23 16:34:10 +0200922 int max_regions;
Simon Glassff3e0772015-03-05 12:25:25 -0700923 int len;
924 int i;
925
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900926 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700927 if (!prop) {
928 debug("%s: Cannot decode regions\n", __func__);
929 return;
930 }
931
Simon Glass878d68c2017-06-12 06:21:31 -0600932 pci_addr_cells = ofnode_read_simple_addr_cells(node);
933 addr_cells = ofnode_read_simple_addr_cells(parent_node);
934 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassff3e0772015-03-05 12:25:25 -0700935
936 /* PCI addresses are always 3-cells */
937 len /= sizeof(u32);
938 cells_per_record = pci_addr_cells + addr_cells + size_cells;
939 hose->region_count = 0;
940 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
941 cells_per_record);
Stefan Roesee0024742020-07-23 16:34:10 +0200942
943 /* Dynamically allocate the regions array */
944 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
945 hose->regions = (struct pci_region *)
946 calloc(1, max_regions * sizeof(struct pci_region));
947
948 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassff3e0772015-03-05 12:25:25 -0700949 u64 pci_addr, addr, size;
950 int space_code;
951 u32 flags;
952 int type;
Simon Glass9526d832015-11-19 20:26:58 -0700953 int pos;
Simon Glassff3e0772015-03-05 12:25:25 -0700954
955 if (len < cells_per_record)
956 break;
957 flags = fdt32_to_cpu(prop[0]);
958 space_code = (flags >> 24) & 3;
959 pci_addr = fdtdec_get_number(prop + 1, 2);
960 prop += pci_addr_cells;
961 addr = fdtdec_get_number(prop, addr_cells);
962 prop += addr_cells;
963 size = fdtdec_get_number(prop, size_cells);
964 prop += size_cells;
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900965 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
966 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassff3e0772015-03-05 12:25:25 -0700967 if (space_code & 2) {
968 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
969 PCI_REGION_MEM;
970 } else if (space_code & 1) {
971 type = PCI_REGION_IO;
972 } else {
973 continue;
974 }
Tuomas Tynkkynen52ba9072018-05-14 18:47:50 +0300975
976 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
977 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
978 debug(" - beyond the 32-bit boundary, ignoring\n");
979 continue;
980 }
981
Simon Glass9526d832015-11-19 20:26:58 -0700982 pos = -1;
Suneel Garapati4cf56ec2019-10-19 17:10:20 -0700983 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
984 for (i = 0; i < hose->region_count; i++) {
985 if (hose->regions[i].flags == type)
986 pos = i;
987 }
Simon Glass9526d832015-11-19 20:26:58 -0700988 }
Suneel Garapati4cf56ec2019-10-19 17:10:20 -0700989
Simon Glass9526d832015-11-19 20:26:58 -0700990 if (pos == -1)
991 pos = hose->region_count++;
992 debug(" - type=%d, pos=%d\n", type, pos);
993 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassff3e0772015-03-05 12:25:25 -0700994 }
995
996 /* Add a region for our local memory */
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200997 bd = gd->bd;
Bin Meng1eaf7802018-03-27 00:46:05 -0700998 if (!bd)
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700999 return;
Bin Meng1eaf7802018-03-27 00:46:05 -07001000
Bernhard Messerklinger664758c2018-02-15 08:59:53 +01001001 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1002 if (bd->bi_dram[i].size) {
1003 pci_set_region(hose->regions + hose->region_count++,
1004 bd->bi_dram[i].start,
1005 bd->bi_dram[i].start,
1006 bd->bi_dram[i].size,
1007 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1008 }
1009 }
Simon Glassff3e0772015-03-05 12:25:25 -07001010
Christian Gmeinerf2825f62018-06-10 06:25:05 -07001011 return;
Simon Glassff3e0772015-03-05 12:25:25 -07001012}
1013
1014static int pci_uclass_pre_probe(struct udevice *bus)
1015{
1016 struct pci_controller *hose;
Simon Glass42f36632020-12-16 21:20:18 -07001017 struct uclass *uc;
1018 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -07001019
Simon Glass8b85dfc2020-12-16 21:20:07 -07001020 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -07001021 bus->parent->name);
Simon Glass0fd3d912020-12-22 19:30:28 -07001022 hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001023
Simon Glass42f36632020-12-16 21:20:18 -07001024 /*
1025 * Set the sequence number, if device_bind() doesn't. We want control
1026 * of this so that numbers are allocated as devices are probed. That
1027 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1028 * higher than their parents)
1029 */
1030 if (dev_seq(bus) == -1) {
1031 ret = uclass_get(UCLASS_PCI, &uc);
1032 if (ret)
1033 return ret;
Simon Glass24621392020-12-19 10:40:09 -07001034 bus->seq_ = uclass_find_next_free_seq(uc);
Simon Glass42f36632020-12-16 21:20:18 -07001035 }
1036
Simon Glassff3e0772015-03-05 12:25:25 -07001037 /* For bridges, use the top-level PCI controller */
Paul Burton65f62b12016-09-08 07:47:32 +01001038 if (!device_is_on_pci_bus(bus)) {
Simon Glassff3e0772015-03-05 12:25:25 -07001039 hose->ctlr = bus;
Christian Gmeinerf2825f62018-06-10 06:25:05 -07001040 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassff3e0772015-03-05 12:25:25 -07001041 } else {
1042 struct pci_controller *parent_hose;
1043
1044 parent_hose = dev_get_uclass_priv(bus->parent);
1045 hose->ctlr = parent_hose->bus;
1046 }
Simon Glass42f36632020-12-16 21:20:18 -07001047
Simon Glassff3e0772015-03-05 12:25:25 -07001048 hose->bus = bus;
Simon Glass8b85dfc2020-12-16 21:20:07 -07001049 hose->first_busno = dev_seq(bus);
1050 hose->last_busno = dev_seq(bus);
Simon Glass7d14ee42020-12-19 10:40:13 -07001051 if (dev_has_ofnode(bus)) {
Suneel Garapatif0c36922020-05-04 21:25:25 -07001052 hose->skip_auto_config_until_reloc =
1053 dev_read_bool(bus,
1054 "u-boot,skip-auto-config-until-reloc");
1055 }
Simon Glassff3e0772015-03-05 12:25:25 -07001056
1057 return 0;
1058}
1059
1060static int pci_uclass_post_probe(struct udevice *bus)
1061{
Simon Glass2206ac22019-12-06 21:41:37 -07001062 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001063 int ret;
1064
Simon Glass8b85dfc2020-12-16 21:20:07 -07001065 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
Simon Glassff3e0772015-03-05 12:25:25 -07001066 ret = pci_bind_bus_devices(bus);
1067 if (ret)
Simon Glass42f36632020-12-16 21:20:18 -07001068 return log_msg_ret("bind", ret);
Simon Glassff3e0772015-03-05 12:25:25 -07001069
Simon Glassf1f44382020-04-26 09:12:56 -06001070 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass2206ac22019-12-06 21:41:37 -07001071 (!hose->skip_auto_config_until_reloc ||
1072 (gd->flags & GD_FLG_RELOC))) {
1073 ret = pci_auto_config_devices(bus);
1074 if (ret < 0)
Simon Glass42f36632020-12-16 21:20:18 -07001075 return log_msg_ret("cfg", ret);
Simon Glass2206ac22019-12-06 21:41:37 -07001076 }
Simon Glassff3e0772015-03-05 12:25:25 -07001077
Bin Meng348b7442015-08-20 06:40:23 -07001078#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1079 /*
1080 * Per Intel FSP specification, we should call FSP notify API to
1081 * inform FSP that PCI enumeration has been done so that FSP will
1082 * do any necessary initialization as required by the chipset's
1083 * BIOS Writer's Guide (BWG).
1084 *
1085 * Unfortunately we have to put this call here as with driver model,
1086 * the enumeration is all done on a lazy basis as needed, so until
1087 * something is touched on PCI it won't happen.
1088 *
1089 * Note we only call this 1) after U-Boot is relocated, and 2)
1090 * root bus has finished probing.
1091 */
Simon Glass8b85dfc2020-12-16 21:20:07 -07001092 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
Bin Meng348b7442015-08-20 06:40:23 -07001093 ret = fsp_init_phase_pci();
Simon Glass4d214552015-09-08 17:52:47 -06001094 if (ret)
Simon Glass42f36632020-12-16 21:20:18 -07001095 return log_msg_ret("fsp", ret);
Simon Glass4d214552015-09-08 17:52:47 -06001096 }
Bin Meng348b7442015-08-20 06:40:23 -07001097#endif
1098
Simon Glass4d214552015-09-08 17:52:47 -06001099 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -07001100}
1101
1102static int pci_uclass_child_post_bind(struct udevice *dev)
1103{
Simon Glass8a8d24b2020-12-03 16:55:23 -07001104 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -07001105
Simon Glass7d14ee42020-12-19 10:40:13 -07001106 if (!dev_has_ofnode(dev))
Simon Glassff3e0772015-03-05 12:25:25 -07001107 return 0;
1108
Simon Glasscaa4daa2020-12-03 16:55:18 -07001109 pplat = dev_get_parent_plat(dev);
Bin Meng1f6b08b2018-08-03 01:14:36 -07001110
1111 /* Extract vendor id and device id if available */
1112 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1113
1114 /* Extract the devfn from fdt_pci_addr */
Stefan Roeseb5214202019-01-25 11:52:42 +01001115 pplat->devfn = pci_get_devfn(dev);
Simon Glassff3e0772015-03-05 12:25:25 -07001116
1117 return 0;
1118}
1119
Simon Glassc4e72c42020-01-27 08:49:37 -07001120static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng4d8615c2015-07-19 00:20:04 +08001121 uint offset, ulong *valuep,
1122 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001123{
Simon Glass0fd3d912020-12-22 19:30:28 -07001124 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001125
1126 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1127}
1128
Bin Meng4d8615c2015-07-19 00:20:04 +08001129static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1130 uint offset, ulong value,
1131 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001132{
Simon Glass0fd3d912020-12-22 19:30:28 -07001133 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001134
1135 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1136}
1137
Simon Glass76c3fbc2015-08-10 07:05:04 -06001138static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1139{
1140 struct udevice *dev;
1141 int ret = 0;
1142
1143 /*
1144 * Scan through all the PCI controllers. On x86 there will only be one
1145 * but that is not necessarily true on other hardware.
1146 */
1147 do {
1148 device_find_first_child(bus, &dev);
1149 if (dev) {
1150 *devp = dev;
1151 return 0;
1152 }
1153 ret = uclass_next_device(&bus);
1154 if (ret)
1155 return ret;
1156 } while (bus);
1157
1158 return 0;
1159}
1160
1161int pci_find_next_device(struct udevice **devp)
1162{
1163 struct udevice *child = *devp;
1164 struct udevice *bus = child->parent;
1165 int ret;
1166
1167 /* First try all the siblings */
1168 *devp = NULL;
1169 while (child) {
1170 device_find_next_child(&child);
1171 if (child) {
1172 *devp = child;
1173 return 0;
1174 }
1175 }
1176
1177 /* We ran out of siblings. Try the next bus */
1178 ret = uclass_next_device(&bus);
1179 if (ret)
1180 return ret;
1181
1182 return bus ? skip_to_next_device(bus, devp) : 0;
1183}
1184
1185int pci_find_first_device(struct udevice **devp)
1186{
1187 struct udevice *bus;
1188 int ret;
1189
1190 *devp = NULL;
1191 ret = uclass_first_device(UCLASS_PCI, &bus);
1192 if (ret)
1193 return ret;
1194
1195 return skip_to_next_device(bus, devp);
1196}
1197
Simon Glass9289db62015-11-19 20:26:59 -07001198ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1199{
1200 switch (size) {
1201 case PCI_SIZE_8:
1202 return (value >> ((offset & 3) * 8)) & 0xff;
1203 case PCI_SIZE_16:
1204 return (value >> ((offset & 2) * 8)) & 0xffff;
1205 default:
1206 return value;
1207 }
1208}
1209
1210ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1211 enum pci_size_t size)
1212{
1213 uint off_mask;
1214 uint val_mask, shift;
1215 ulong ldata, mask;
1216
1217 switch (size) {
1218 case PCI_SIZE_8:
1219 off_mask = 3;
1220 val_mask = 0xff;
1221 break;
1222 case PCI_SIZE_16:
1223 off_mask = 2;
1224 val_mask = 0xffff;
1225 break;
1226 default:
1227 return value;
1228 }
1229 shift = (offset & off_mask) * 8;
1230 ldata = (value & val_mask) << shift;
1231 mask = val_mask << shift;
1232 value = (old & ~mask) | ldata;
1233
1234 return value;
1235}
1236
Rayagonda Kokatanur143eb5b2020-05-12 13:29:49 +05301237int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1238{
1239 int pci_addr_cells, addr_cells, size_cells;
1240 int cells_per_record;
1241 const u32 *prop;
1242 int len;
1243 int i = 0;
1244
1245 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1246 if (!prop) {
1247 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1248 dev->name);
1249 return -EINVAL;
1250 }
1251
1252 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1253 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1254 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1255
1256 /* PCI addresses are always 3-cells */
1257 len /= sizeof(u32);
1258 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1259 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1260 cells_per_record);
1261
1262 while (len) {
1263 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1264 prop += pci_addr_cells;
1265 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1266 prop += addr_cells;
1267 memp->size = fdtdec_get_number(prop, size_cells);
1268 prop += size_cells;
1269
1270 if (i == index)
1271 return 0;
1272 i++;
1273 len -= cells_per_record;
1274 }
1275
1276 return -EINVAL;
1277}
1278
Simon Glassf9260332015-11-19 20:27:01 -07001279int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1280 struct pci_region **memp, struct pci_region **prefp)
1281{
1282 struct udevice *bus = pci_get_controller(dev);
1283 struct pci_controller *hose = dev_get_uclass_priv(bus);
1284 int i;
1285
1286 *iop = NULL;
1287 *memp = NULL;
1288 *prefp = NULL;
1289 for (i = 0; i < hose->region_count; i++) {
1290 switch (hose->regions[i].flags) {
1291 case PCI_REGION_IO:
1292 if (!*iop || (*iop)->size < hose->regions[i].size)
1293 *iop = hose->regions + i;
1294 break;
1295 case PCI_REGION_MEM:
1296 if (!*memp || (*memp)->size < hose->regions[i].size)
1297 *memp = hose->regions + i;
1298 break;
1299 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1300 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1301 *prefp = hose->regions + i;
1302 break;
1303 }
1304 }
1305
1306 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1307}
1308
Simon Glass194fca92020-01-27 08:49:38 -07001309u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glassbab17cf2015-11-29 13:17:53 -07001310{
1311 u32 addr;
1312 int bar;
1313
1314 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1315 dm_pci_read_config32(dev, bar, &addr);
Simon Glass9ece4b02020-04-09 10:27:36 -06001316
1317 /*
1318 * If we get an invalid address, return this so that comparisons with
1319 * FDT_ADDR_T_NONE work correctly
1320 */
1321 if (addr == 0xffffffff)
1322 return addr;
1323 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glassbab17cf2015-11-29 13:17:53 -07001324 return addr & PCI_BASE_ADDRESS_IO_MASK;
1325 else
1326 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1327}
1328
Simon Glass9d731c82016-01-18 20:19:15 -07001329void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1330{
1331 int bar;
1332
1333 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1334 dm_pci_write_config32(dev, bar, addr);
1335}
1336
Simon Glass21d1fe72015-11-29 13:18:03 -07001337static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1338 pci_addr_t bus_addr, unsigned long flags,
1339 unsigned long skip_mask, phys_addr_t *pa)
1340{
1341 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1342 struct pci_region *res;
1343 int i;
1344
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001345 if (hose->region_count == 0) {
1346 *pa = bus_addr;
1347 return 0;
1348 }
1349
Simon Glass21d1fe72015-11-29 13:18:03 -07001350 for (i = 0; i < hose->region_count; i++) {
1351 res = &hose->regions[i];
1352
1353 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1354 continue;
1355
1356 if (res->flags & skip_mask)
1357 continue;
1358
1359 if (bus_addr >= res->bus_start &&
1360 (bus_addr - res->bus_start) < res->size) {
1361 *pa = (bus_addr - res->bus_start + res->phys_start);
1362 return 0;
1363 }
1364 }
1365
1366 return 1;
1367}
1368
1369phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1370 unsigned long flags)
1371{
1372 phys_addr_t phys_addr = 0;
1373 struct udevice *ctlr;
1374 int ret;
1375
1376 /* The root controller has the region information */
1377 ctlr = pci_get_controller(dev);
1378
1379 /*
1380 * if PCI_REGION_MEM is set we do a two pass search with preference
1381 * on matches that don't have PCI_REGION_SYS_MEMORY set
1382 */
1383 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1384 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1385 flags, PCI_REGION_SYS_MEMORY,
1386 &phys_addr);
1387 if (!ret)
1388 return phys_addr;
1389 }
1390
1391 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1392
1393 if (ret)
1394 puts("pci_hose_bus_to_phys: invalid physical address\n");
1395
1396 return phys_addr;
1397}
1398
1399int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1400 unsigned long flags, unsigned long skip_mask,
1401 pci_addr_t *ba)
1402{
1403 struct pci_region *res;
1404 struct udevice *ctlr;
1405 pci_addr_t bus_addr;
1406 int i;
1407 struct pci_controller *hose;
1408
1409 /* The root controller has the region information */
1410 ctlr = pci_get_controller(dev);
1411 hose = dev_get_uclass_priv(ctlr);
1412
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001413 if (hose->region_count == 0) {
1414 *ba = phys_addr;
1415 return 0;
1416 }
1417
Simon Glass21d1fe72015-11-29 13:18:03 -07001418 for (i = 0; i < hose->region_count; i++) {
1419 res = &hose->regions[i];
1420
1421 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1422 continue;
1423
1424 if (res->flags & skip_mask)
1425 continue;
1426
1427 bus_addr = phys_addr - res->phys_start + res->bus_start;
1428
1429 if (bus_addr >= res->bus_start &&
1430 (bus_addr - res->bus_start) < res->size) {
1431 *ba = bus_addr;
1432 return 0;
1433 }
1434 }
1435
1436 return 1;
1437}
1438
1439pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1440 unsigned long flags)
1441{
1442 pci_addr_t bus_addr = 0;
1443 int ret;
1444
1445 /*
1446 * if PCI_REGION_MEM is set we do a two pass search with preference
1447 * on matches that don't have PCI_REGION_SYS_MEMORY set
1448 */
1449 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1450 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1451 PCI_REGION_SYS_MEMORY, &bus_addr);
1452 if (!ret)
1453 return bus_addr;
1454 }
1455
1456 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1457
1458 if (ret)
1459 puts("pci_hose_phys_to_bus: invalid physical address\n");
1460
1461 return bus_addr;
1462}
1463
Suneel Garapati51eeae92019-10-19 16:34:16 -07001464static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
Simon Glass8a8d24b2020-12-03 16:55:23 -07001465 struct pci_child_plat *pdata)
Suneel Garapati51eeae92019-10-19 16:34:16 -07001466{
1467 phys_addr_t addr = 0;
1468
1469 /*
1470 * In the case of a Virtual Function device using BAR
1471 * base and size, add offset for VFn BAR(1, 2, 3...n)
1472 */
1473 if (pdata->is_virtfn) {
1474 size_t sz;
1475 u32 ea_entry;
1476
1477 /* MaxOffset, 1st DW */
1478 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1479 sz = ea_entry & PCI_EA_FIELD_MASK;
1480 /* Fill up lower 2 bits */
1481 sz |= (~PCI_EA_FIELD_MASK);
1482
1483 if (ea_entry & PCI_EA_IS_64) {
1484 /* MaxOffset 2nd DW */
1485 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1486 sz |= ((u64)ea_entry) << 32;
1487 }
1488
1489 addr = (pdata->virtid - 1) * (sz + 1);
1490 }
1491
1492 return addr;
1493}
1494
Alex Marginean0b143d82019-06-07 11:24:23 +03001495static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
Simon Glass8a8d24b2020-12-03 16:55:23 -07001496 int ea_off, struct pci_child_plat *pdata)
Alex Marginean0b143d82019-06-07 11:24:23 +03001497{
1498 int ea_cnt, i, entry_size;
1499 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1500 u32 ea_entry;
1501 phys_addr_t addr;
1502
Suneel Garapati51eeae92019-10-19 16:34:16 -07001503 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1504 /*
1505 * In the case of a Virtual Function device, device is
1506 * Physical function, so pdata will point to required VF
1507 * specific data.
1508 */
1509 if (pdata->is_virtfn)
1510 bar_id += PCI_EA_BEI_VF_BAR0;
1511 }
1512
Alex Marginean0b143d82019-06-07 11:24:23 +03001513 /* EA capability structure header */
1514 dm_pci_read_config32(dev, ea_off, &ea_entry);
1515 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1516 ea_off += PCI_EA_FIRST_ENT;
1517
1518 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1519 /* Entry header */
1520 dm_pci_read_config32(dev, ea_off, &ea_entry);
1521 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1522
1523 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1524 continue;
1525
1526 /* Base address, 1st DW */
1527 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1528 addr = ea_entry & PCI_EA_FIELD_MASK;
1529 if (ea_entry & PCI_EA_IS_64) {
1530 /* Base address, 2nd DW, skip over 4B MaxOffset */
1531 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1532 addr |= ((u64)ea_entry) << 32;
1533 }
1534
Suneel Garapati51eeae92019-10-19 16:34:16 -07001535 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1536 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1537
Alex Marginean0b143d82019-06-07 11:24:23 +03001538 /* size ignored for now */
Suneel Garapatib3699a12019-10-19 16:44:35 -07001539 return map_physmem(addr, 0, flags);
Alex Marginean0b143d82019-06-07 11:24:23 +03001540 }
1541
1542 return 0;
1543}
1544
Simon Glass21d1fe72015-11-29 13:18:03 -07001545void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1546{
Simon Glass8a8d24b2020-12-03 16:55:23 -07001547 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
Suneel Garapati51eeae92019-10-19 16:34:16 -07001548 struct udevice *udev = dev;
Simon Glass21d1fe72015-11-29 13:18:03 -07001549 pci_addr_t pci_bus_addr;
1550 u32 bar_response;
Alex Marginean0b143d82019-06-07 11:24:23 +03001551 int ea_off;
1552
Suneel Garapati51eeae92019-10-19 16:34:16 -07001553 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1554 /*
1555 * In case of Virtual Function devices, use PF udevice
1556 * as EA capability is defined in Physical Function
1557 */
1558 if (pdata->is_virtfn)
1559 udev = pdata->pfdev;
1560 }
1561
Alex Marginean0b143d82019-06-07 11:24:23 +03001562 /*
1563 * if the function supports Enhanced Allocation use that instead of
1564 * BARs
Suneel Garapati51eeae92019-10-19 16:34:16 -07001565 * Incase of virtual functions, pdata will help read VF BEI
1566 * and EA entry size.
Alex Marginean0b143d82019-06-07 11:24:23 +03001567 */
Suneel Garapati51eeae92019-10-19 16:34:16 -07001568 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
Alex Marginean0b143d82019-06-07 11:24:23 +03001569 if (ea_off)
Suneel Garapati51eeae92019-10-19 16:34:16 -07001570 return dm_pci_map_ea_bar(udev, bar, flags, ea_off, pdata);
Simon Glass21d1fe72015-11-29 13:18:03 -07001571
1572 /* read BAR address */
Suneel Garapati51eeae92019-10-19 16:34:16 -07001573 dm_pci_read_config32(udev, bar, &bar_response);
Simon Glass21d1fe72015-11-29 13:18:03 -07001574 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1575
1576 /*
1577 * Pass "0" as the length argument to pci_bus_to_virt. The arg
Suneel Garapatib3699a12019-10-19 16:44:35 -07001578 * isn't actually used on any platform because U-Boot assumes a static
Simon Glass21d1fe72015-11-29 13:18:03 -07001579 * linear mapping. In the future, this could read the BAR size
1580 * and pass that as the size if needed.
1581 */
Suneel Garapati51eeae92019-10-19 16:34:16 -07001582 return dm_pci_bus_to_virt(udev, pci_bus_addr, flags, 0, MAP_NOCACHE);
Simon Glass21d1fe72015-11-29 13:18:03 -07001583}
1584
Bin Menga8c5f8d2018-10-15 02:21:21 -07001585static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001586{
Bin Mengdac01fd2018-08-03 01:14:52 -07001587 int ttl = PCI_FIND_CAP_TTL;
1588 u8 id;
1589 u16 ent;
Bin Mengdac01fd2018-08-03 01:14:52 -07001590
1591 dm_pci_read_config8(dev, pos, &pos);
Bin Menga8c5f8d2018-10-15 02:21:21 -07001592
Bin Mengdac01fd2018-08-03 01:14:52 -07001593 while (ttl--) {
1594 if (pos < PCI_STD_HEADER_SIZEOF)
1595 break;
1596 pos &= ~3;
1597 dm_pci_read_config16(dev, pos, &ent);
1598
1599 id = ent & 0xff;
1600 if (id == 0xff)
1601 break;
1602 if (id == cap)
1603 return pos;
1604 pos = (ent >> 8);
1605 }
1606
1607 return 0;
1608}
1609
Bin Menga8c5f8d2018-10-15 02:21:21 -07001610int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1611{
1612 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1613 cap);
1614}
1615
1616int dm_pci_find_capability(struct udevice *dev, int cap)
1617{
1618 u16 status;
1619 u8 header_type;
1620 u8 pos;
1621
1622 dm_pci_read_config16(dev, PCI_STATUS, &status);
1623 if (!(status & PCI_STATUS_CAP_LIST))
1624 return 0;
1625
1626 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1627 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1628 pos = PCI_CB_CAPABILITY_LIST;
1629 else
1630 pos = PCI_CAPABILITY_LIST;
1631
1632 return _dm_pci_find_next_capability(dev, pos, cap);
1633}
1634
1635int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001636{
1637 u32 header;
1638 int ttl;
1639 int pos = PCI_CFG_SPACE_SIZE;
1640
1641 /* minimum 8 bytes per capability */
1642 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1643
Bin Menga8c5f8d2018-10-15 02:21:21 -07001644 if (start)
1645 pos = start;
1646
Bin Mengdac01fd2018-08-03 01:14:52 -07001647 dm_pci_read_config32(dev, pos, &header);
1648 /*
1649 * If we have no capabilities, this is indicated by cap ID,
1650 * cap version and next pointer all being 0.
1651 */
1652 if (header == 0)
1653 return 0;
1654
1655 while (ttl--) {
1656 if (PCI_EXT_CAP_ID(header) == cap)
1657 return pos;
1658
1659 pos = PCI_EXT_CAP_NEXT(header);
1660 if (pos < PCI_CFG_SPACE_SIZE)
1661 break;
1662
1663 dm_pci_read_config32(dev, pos, &header);
1664 }
1665
1666 return 0;
1667}
1668
Bin Menga8c5f8d2018-10-15 02:21:21 -07001669int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1670{
1671 return dm_pci_find_next_ext_capability(dev, 0, cap);
1672}
1673
Alex Margineanb8e1f822019-06-07 11:24:25 +03001674int dm_pci_flr(struct udevice *dev)
1675{
1676 int pcie_off;
1677 u32 cap;
1678
1679 /* look for PCI Express Capability */
1680 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1681 if (!pcie_off)
1682 return -ENOENT;
1683
1684 /* check FLR capability */
1685 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1686 if (!(cap & PCI_EXP_DEVCAP_FLR))
1687 return -ENOENT;
1688
1689 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1690 PCI_EXP_DEVCTL_BCR_FLR);
1691
1692 /* wait 100ms, per PCI spec */
1693 mdelay(100);
1694
1695 return 0;
1696}
1697
Suneel Garapatib8852dc2019-10-19 16:07:20 -07001698#if defined(CONFIG_PCI_SRIOV)
1699int pci_sriov_init(struct udevice *pdev, int vf_en)
1700{
1701 u16 vendor, device;
1702 struct udevice *bus;
1703 struct udevice *dev;
1704 pci_dev_t bdf;
1705 u16 ctrl;
1706 u16 num_vfs;
1707 u16 total_vf;
1708 u16 vf_offset;
1709 u16 vf_stride;
1710 int vf, ret;
1711 int pos;
1712
1713 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1714 if (!pos) {
1715 debug("Error: SRIOV capability not found\n");
1716 return -ENOENT;
1717 }
1718
1719 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1720
1721 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1722 if (vf_en > total_vf)
1723 vf_en = total_vf;
1724 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1725
1726 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1727 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1728
1729 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1730 if (num_vfs > vf_en)
1731 num_vfs = vf_en;
1732
1733 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1734 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1735
1736 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1737 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1738
1739 bdf = dm_pci_get_bdf(pdev);
1740
1741 pci_get_bus(PCI_BUS(bdf), &bus);
1742
1743 if (!bus)
1744 return -ENODEV;
1745
1746 bdf += PCI_BDF(0, 0, vf_offset);
1747
1748 for (vf = 0; vf < num_vfs; vf++) {
Simon Glass8a8d24b2020-12-03 16:55:23 -07001749 struct pci_child_plat *pplat;
Suneel Garapatib8852dc2019-10-19 16:07:20 -07001750 ulong class;
1751
1752 pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1753 &class, PCI_SIZE_16);
1754
1755 debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
Simon Glass8b85dfc2020-12-16 21:20:07 -07001756 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Suneel Garapatib8852dc2019-10-19 16:07:20 -07001757
1758 /* Find this device in the device tree */
1759 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1760
1761 if (ret == -ENODEV) {
1762 struct pci_device_id find_id;
1763
1764 memset(&find_id, '\0', sizeof(find_id));
1765 find_id.vendor = vendor;
1766 find_id.device = device;
1767 find_id.class = class;
1768
1769 ret = pci_find_and_bind_driver(bus, &find_id,
1770 bdf, &dev);
1771
1772 if (ret)
1773 return ret;
1774 }
1775
1776 /* Update the platform data */
Simon Glasscaa4daa2020-12-03 16:55:18 -07001777 pplat = dev_get_parent_plat(dev);
Suneel Garapatib8852dc2019-10-19 16:07:20 -07001778 pplat->devfn = PCI_MASK_BUS(bdf);
1779 pplat->vendor = vendor;
1780 pplat->device = device;
1781 pplat->class = class;
1782 pplat->is_virtfn = true;
1783 pplat->pfdev = pdev;
1784 pplat->virtid = vf * vf_stride + vf_offset;
1785
1786 debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
Simon Glass8b85dfc2020-12-16 21:20:07 -07001787 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
Suneel Garapatib8852dc2019-10-19 16:07:20 -07001788 PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1789 bdf += PCI_BDF(0, 0, vf_stride);
1790 }
1791
1792 return 0;
1793}
1794
1795int pci_sriov_get_totalvfs(struct udevice *pdev)
1796{
1797 u16 total_vf;
1798 int pos;
1799
1800 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1801 if (!pos) {
1802 debug("Error: SRIOV capability not found\n");
1803 return -ENOENT;
1804 }
1805
1806 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1807
1808 return total_vf;
1809}
1810#endif /* SRIOV */
1811
Simon Glassff3e0772015-03-05 12:25:25 -07001812UCLASS_DRIVER(pci) = {
1813 .id = UCLASS_PCI,
1814 .name = "pci",
Simon Glass42f36632020-12-16 21:20:18 -07001815 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
Simon Glass91195482016-07-05 17:10:10 -06001816 .post_bind = dm_scan_fdt_dev,
Simon Glassff3e0772015-03-05 12:25:25 -07001817 .pre_probe = pci_uclass_pre_probe,
1818 .post_probe = pci_uclass_post_probe,
1819 .child_post_bind = pci_uclass_child_post_bind,
Simon Glass41575d82020-12-03 16:55:17 -07001820 .per_device_auto = sizeof(struct pci_controller),
Simon Glass8a8d24b2020-12-03 16:55:23 -07001821 .per_child_plat_auto = sizeof(struct pci_child_plat),
Simon Glassff3e0772015-03-05 12:25:25 -07001822};
1823
1824static const struct dm_pci_ops pci_bridge_ops = {
1825 .read_config = pci_bridge_read_config,
1826 .write_config = pci_bridge_write_config,
1827};
1828
1829static const struct udevice_id pci_bridge_ids[] = {
1830 { .compatible = "pci-bridge" },
1831 { }
1832};
1833
1834U_BOOT_DRIVER(pci_bridge_drv) = {
1835 .name = "pci_bridge_drv",
1836 .id = UCLASS_PCI,
1837 .of_match = pci_bridge_ids,
1838 .ops = &pci_bridge_ops,
1839};
1840
1841UCLASS_DRIVER(pci_generic) = {
1842 .id = UCLASS_PCI_GENERIC,
1843 .name = "pci_generic",
1844};
1845
1846static const struct udevice_id pci_generic_ids[] = {
1847 { .compatible = "pci-generic" },
1848 { }
1849};
1850
1851U_BOOT_DRIVER(pci_generic_drv) = {
1852 .name = "pci_generic_drv",
1853 .id = UCLASS_PCI_GENERIC,
1854 .of_match = pci_generic_ids,
1855};
Stephen Warrene578b922016-01-26 11:10:11 -07001856
Ovidiu Panaitb9f6d0f2020-11-28 10:43:12 +02001857int pci_init(void)
Stephen Warrene578b922016-01-26 11:10:11 -07001858{
1859 struct udevice *bus;
1860
1861 /*
1862 * Enumerate all known controller devices. Enumeration has the side-
1863 * effect of probing them, so PCIe devices will be enumerated too.
1864 */
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001865 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warrene578b922016-01-26 11:10:11 -07001866 bus;
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001867 uclass_next_device_check(&bus)) {
Stephen Warrene578b922016-01-26 11:10:11 -07001868 ;
1869 }
Ovidiu Panaitb9f6d0f2020-11-28 10:43:12 +02001870
1871 return 0;
Stephen Warrene578b922016-01-26 11:10:11 -07001872}