blob: 33dda00002e4a7e357bd8eee22c91ea04faa2c52 [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 Glassf5cbb5c2021-06-27 17:50:57 -060024#include <dt-bindings/pci/pci.h>
Simon Glassc05ed002020-05-10 11:40:11 -060025#include <linux/delay.h>
Simon Glass5e23b8b2015-11-29 13:17:49 -070026#include "pci_internal.h"
Simon Glassff3e0772015-03-05 12:25:25 -070027
28DECLARE_GLOBAL_DATA_PTR;
29
Simon Glassa6eb93b2016-01-18 20:19:14 -070030int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass983c6ba22015-08-31 18:55:35 -060031{
32 int ret;
33
34 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
35
36 /* Since buses may not be numbered yet try a little harder with bus 0 */
37 if (ret == -ENODEV) {
Simon Glass3f603cb2016-02-11 13:23:26 -070038 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass983c6ba22015-08-31 18:55:35 -060039 if (ret)
40 return ret;
Simon Glass983c6ba22015-08-31 18:55:35 -060041 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
42 }
43
44 return ret;
45}
46
Simon Glass9f60fb02015-11-19 20:27:00 -070047struct udevice *pci_get_controller(struct udevice *dev)
48{
49 while (device_is_on_pci_bus(dev))
50 dev = dev->parent;
51
52 return dev;
53}
54
Simon Glass194fca92020-01-27 08:49:38 -070055pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glass4b515e42015-07-06 16:47:46 -060056{
Simon Glass8a8d24b2020-12-03 16:55:23 -070057 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glass4b515e42015-07-06 16:47:46 -060058 struct udevice *bus = dev->parent;
59
Simon Glass48862872019-12-29 21:19:14 -070060 /*
61 * This error indicates that @dev is a device on an unprobed PCI bus.
62 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
63 * will produce a bad BDF>
64 *
65 * A common cause of this problem is that this function is called in the
Simon Glassd1998a92020-12-03 16:55:21 -070066 * of_to_plat() method of @dev. Accessing the PCI bus in that
Simon Glass48862872019-12-29 21:19:14 -070067 * method is not allowed, since it has not yet been probed. To fix this,
68 * move that access to the probe() method of @dev instead.
69 */
70 if (!device_active(bus))
71 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
72 bus->name);
Simon Glass8b85dfc2020-12-16 21:20:07 -070073 return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
Simon Glass4b515e42015-07-06 16:47:46 -060074}
75
Simon Glassff3e0772015-03-05 12:25:25 -070076/**
77 * pci_get_bus_max() - returns the bus number of the last active bus
78 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010079 * Return: last bus number, or -1 if no active buses
Simon Glassff3e0772015-03-05 12:25:25 -070080 */
81static int pci_get_bus_max(void)
82{
83 struct udevice *bus;
84 struct uclass *uc;
85 int ret = -1;
86
87 ret = uclass_get(UCLASS_PCI, &uc);
88 uclass_foreach_dev(bus, uc) {
Simon Glass8b85dfc2020-12-16 21:20:07 -070089 if (dev_seq(bus) > ret)
90 ret = dev_seq(bus);
Simon Glassff3e0772015-03-05 12:25:25 -070091 }
92
93 debug("%s: ret=%d\n", __func__, ret);
94
95 return ret;
96}
97
98int pci_last_busno(void)
99{
Bin Meng069155c2015-10-01 00:36:01 -0700100 return pci_get_bus_max();
Simon Glassff3e0772015-03-05 12:25:25 -0700101}
102
103int pci_get_ff(enum pci_size_t size)
104{
105 switch (size) {
106 case PCI_SIZE_8:
107 return 0xff;
108 case PCI_SIZE_16:
109 return 0xffff;
110 default:
111 return 0xffffffff;
112 }
113}
114
Marek Vasut02e4d382018-10-10 21:27:06 +0200115static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
116 ofnode *rnode)
117{
118 struct fdt_pci_addr addr;
119 ofnode node;
120 int ret;
121
122 dev_for_each_subnode(node, bus) {
123 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
124 &addr);
125 if (ret)
126 continue;
127
128 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
129 continue;
130
131 *rnode = node;
132 break;
133 }
134};
135
Simon Glassc4e72c42020-01-27 08:49:37 -0700136int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassff3e0772015-03-05 12:25:25 -0700137 struct udevice **devp)
138{
139 struct udevice *dev;
140
141 for (device_find_first_child(bus, &dev);
142 dev;
143 device_find_next_child(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700144 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700145
Simon Glasscaa4daa2020-12-03 16:55:18 -0700146 pplat = dev_get_parent_plat(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700147 if (pplat && pplat->devfn == find_devfn) {
148 *devp = dev;
149 return 0;
150 }
151 }
152
153 return -ENODEV;
154}
155
Simon Glassf3f1fae2015-11-29 13:17:48 -0700156int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassff3e0772015-03-05 12:25:25 -0700157{
158 struct udevice *bus;
159 int ret;
160
Simon Glass983c6ba22015-08-31 18:55:35 -0600161 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700162 if (ret)
163 return ret;
164 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
165}
166
167static int pci_device_matches_ids(struct udevice *dev,
Simon Glasse58f3a72021-06-27 17:50:56 -0600168 const struct pci_device_id *ids)
Simon Glassff3e0772015-03-05 12:25:25 -0700169{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700170 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700171 int i;
172
Simon Glasscaa4daa2020-12-03 16:55:18 -0700173 pplat = dev_get_parent_plat(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700174 if (!pplat)
175 return -EINVAL;
176 for (i = 0; ids[i].vendor != 0; i++) {
177 if (pplat->vendor == ids[i].vendor &&
178 pplat->device == ids[i].device)
179 return i;
180 }
181
182 return -EINVAL;
183}
184
Simon Glasse58f3a72021-06-27 17:50:56 -0600185int pci_bus_find_devices(struct udevice *bus, const struct pci_device_id *ids,
Simon Glassff3e0772015-03-05 12:25:25 -0700186 int *indexp, struct udevice **devp)
187{
188 struct udevice *dev;
189
190 /* Scan all devices on this bus */
191 for (device_find_first_child(bus, &dev);
192 dev;
193 device_find_next_child(&dev)) {
194 if (pci_device_matches_ids(dev, ids) >= 0) {
195 if ((*indexp)-- <= 0) {
196 *devp = dev;
197 return 0;
198 }
199 }
200 }
201
202 return -ENODEV;
203}
204
Simon Glasse58f3a72021-06-27 17:50:56 -0600205int pci_find_device_id(const struct pci_device_id *ids, int index,
Simon Glassff3e0772015-03-05 12:25:25 -0700206 struct udevice **devp)
207{
208 struct udevice *bus;
209
210 /* Scan all known buses */
211 for (uclass_first_device(UCLASS_PCI, &bus);
212 bus;
213 uclass_next_device(&bus)) {
214 if (!pci_bus_find_devices(bus, ids, &index, devp))
215 return 0;
216 }
217 *devp = NULL;
218
219 return -ENODEV;
220}
221
Simon Glass5c0bf642015-11-29 13:17:50 -0700222static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
223 unsigned int device, int *indexp,
224 struct udevice **devp)
225{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700226 struct pci_child_plat *pplat;
Simon Glass5c0bf642015-11-29 13:17:50 -0700227 struct udevice *dev;
228
229 for (device_find_first_child(bus, &dev);
230 dev;
231 device_find_next_child(&dev)) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700232 pplat = dev_get_parent_plat(dev);
Simon Glass5c0bf642015-11-29 13:17:50 -0700233 if (pplat->vendor == vendor && pplat->device == device) {
234 if (!(*indexp)--) {
235 *devp = dev;
236 return 0;
237 }
238 }
239 }
240
241 return -ENODEV;
242}
243
244int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
245 struct udevice **devp)
246{
247 struct udevice *bus;
248
249 /* Scan all known buses */
250 for (uclass_first_device(UCLASS_PCI, &bus);
251 bus;
252 uclass_next_device(&bus)) {
253 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
254 return device_probe(*devp);
255 }
256 *devp = NULL;
257
258 return -ENODEV;
259}
260
Simon Glassa0eb8352015-11-29 13:17:52 -0700261int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
262{
263 struct udevice *dev;
264
265 /* Scan all known buses */
266 for (pci_find_first_device(&dev);
267 dev;
268 pci_find_next_device(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700269 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassa0eb8352015-11-29 13:17:52 -0700270
271 if (pplat->class == find_class && !index--) {
272 *devp = dev;
273 return device_probe(*devp);
274 }
275 }
276 *devp = NULL;
277
278 return -ENODEV;
279}
280
Simon Glassff3e0772015-03-05 12:25:25 -0700281int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
282 unsigned long value, enum pci_size_t size)
283{
284 struct dm_pci_ops *ops;
285
286 ops = pci_get_ops(bus);
287 if (!ops->write_config)
288 return -ENOSYS;
289 return ops->write_config(bus, bdf, offset, value, size);
290}
291
Simon Glass319dba12016-03-06 19:27:52 -0700292int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
293 u32 clr, u32 set)
294{
295 ulong val;
296 int ret;
297
298 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
299 if (ret)
300 return ret;
301 val &= ~clr;
302 val |= set;
303
304 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
305}
306
Vladimir Olteanf98aa782021-09-17 15:11:25 +0300307static int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
308 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -0700309{
310 struct udevice *bus;
311 int ret;
312
Simon Glass983c6ba22015-08-31 18:55:35 -0600313 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700314 if (ret)
315 return ret;
316
Bin Meng4d8615c2015-07-19 00:20:04 +0800317 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700318}
319
Simon Glass66afb4e2015-08-10 07:05:03 -0600320int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
321 enum pci_size_t size)
322{
323 struct udevice *bus;
324
Bin Meng1e0f2262015-09-11 03:24:34 -0700325 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600326 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700327 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
328 size);
Simon Glass66afb4e2015-08-10 07:05:03 -0600329}
330
Simon Glassff3e0772015-03-05 12:25:25 -0700331int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
332{
333 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
334}
335
336int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
337{
338 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
339}
340
341int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
342{
343 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
344}
345
Simon Glass66afb4e2015-08-10 07:05:03 -0600346int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
347{
348 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
349}
350
351int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
352{
353 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
354}
355
356int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
357{
358 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
359}
360
Simon Glass194fca92020-01-27 08:49:38 -0700361int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassff3e0772015-03-05 12:25:25 -0700362 unsigned long *valuep, enum pci_size_t size)
363{
364 struct dm_pci_ops *ops;
365
366 ops = pci_get_ops(bus);
367 if (!ops->read_config)
368 return -ENOSYS;
369 return ops->read_config(bus, bdf, offset, valuep, size);
370}
371
Vladimir Oltean1512ac12021-09-17 15:11:26 +0300372static int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
373 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -0700374{
375 struct udevice *bus;
376 int ret;
377
Simon Glass983c6ba22015-08-31 18:55:35 -0600378 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700379 if (ret)
380 return ret;
381
Bin Meng4d8615c2015-07-19 00:20:04 +0800382 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700383}
384
Simon Glass194fca92020-01-27 08:49:38 -0700385int dm_pci_read_config(const struct udevice *dev, int offset,
386 unsigned long *valuep, enum pci_size_t size)
Simon Glass66afb4e2015-08-10 07:05:03 -0600387{
Simon Glass194fca92020-01-27 08:49:38 -0700388 const struct udevice *bus;
Simon Glass66afb4e2015-08-10 07:05:03 -0600389
Bin Meng1e0f2262015-09-11 03:24:34 -0700390 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600391 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700392 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass66afb4e2015-08-10 07:05:03 -0600393 size);
394}
395
Simon Glassff3e0772015-03-05 12:25:25 -0700396int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
397{
398 unsigned long value;
399 int ret;
400
401 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
402 if (ret)
403 return ret;
404 *valuep = value;
405
406 return 0;
407}
408
409int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
410{
411 unsigned long value;
412 int ret;
413
414 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
415 if (ret)
416 return ret;
417 *valuep = value;
418
419 return 0;
420}
421
422int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
423{
424 unsigned long value;
425 int ret;
426
427 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
428 if (ret)
429 return ret;
430 *valuep = value;
431
432 return 0;
433}
434
Simon Glass194fca92020-01-27 08:49:38 -0700435int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600436{
437 unsigned long value;
438 int ret;
439
440 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
441 if (ret)
442 return ret;
443 *valuep = value;
444
445 return 0;
446}
447
Simon Glass194fca92020-01-27 08:49:38 -0700448int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600449{
450 unsigned long value;
451 int ret;
452
453 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
454 if (ret)
455 return ret;
456 *valuep = value;
457
458 return 0;
459}
460
Simon Glass194fca92020-01-27 08:49:38 -0700461int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600462{
463 unsigned long value;
464 int ret;
465
466 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
467 if (ret)
468 return ret;
469 *valuep = value;
470
471 return 0;
472}
473
Simon Glass319dba12016-03-06 19:27:52 -0700474int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
475{
476 u8 val;
477 int ret;
478
479 ret = dm_pci_read_config8(dev, offset, &val);
480 if (ret)
481 return ret;
482 val &= ~clr;
483 val |= set;
484
485 return dm_pci_write_config8(dev, offset, val);
486}
487
488int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
489{
490 u16 val;
491 int ret;
492
493 ret = dm_pci_read_config16(dev, offset, &val);
494 if (ret)
495 return ret;
496 val &= ~clr;
497 val |= set;
498
499 return dm_pci_write_config16(dev, offset, val);
500}
501
502int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
503{
504 u32 val;
505 int ret;
506
507 ret = dm_pci_read_config32(dev, offset, &val);
508 if (ret)
509 return ret;
510 val &= ~clr;
511 val |= set;
512
513 return dm_pci_write_config32(dev, offset, val);
514}
515
Bin Mengbbbcb522015-10-01 00:36:02 -0700516static void set_vga_bridge_bits(struct udevice *dev)
517{
518 struct udevice *parent = dev->parent;
519 u16 bc;
520
Simon Glass8b85dfc2020-12-16 21:20:07 -0700521 while (dev_seq(parent) != 0) {
Bin Mengbbbcb522015-10-01 00:36:02 -0700522 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
523 bc |= PCI_BRIDGE_CTL_VGA;
524 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
525 parent = parent->parent;
526 }
527}
528
Simon Glassff3e0772015-03-05 12:25:25 -0700529int pci_auto_config_devices(struct udevice *bus)
530{
Simon Glass0fd3d912020-12-22 19:30:28 -0700531 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700532 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700533 unsigned int sub_bus;
534 struct udevice *dev;
535 int ret;
536
Simon Glass8b85dfc2020-12-16 21:20:07 -0700537 sub_bus = dev_seq(bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700538 debug("%s: start\n", __func__);
539 pciauto_config_init(hose);
540 for (ret = device_find_first_child(bus, &dev);
541 !ret && dev;
542 ret = device_find_next_child(&dev)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700543 unsigned int max_bus;
Simon Glass4d214552015-09-08 17:52:47 -0600544 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700545
Simon Glassff3e0772015-03-05 12:25:25 -0700546 debug("%s: device %s\n", __func__, dev->name);
Simon Glass7d14ee42020-12-19 10:40:13 -0700547 if (dev_has_ofnode(dev) &&
Suneel Garapatif0c36922020-05-04 21:25:25 -0700548 dev_read_bool(dev, "pci,no-autoconfig"))
Simon Glassd8c7fb52020-04-08 16:57:26 -0600549 continue;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700550 ret = dm_pciauto_config_device(dev);
Simon Glass4d214552015-09-08 17:52:47 -0600551 if (ret < 0)
Simon Glass42f36632020-12-16 21:20:18 -0700552 return log_msg_ret("auto", ret);
Simon Glass4d214552015-09-08 17:52:47 -0600553 max_bus = ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700554 sub_bus = max(sub_bus, max_bus);
Bin Mengbbbcb522015-10-01 00:36:02 -0700555
Masami Hiramatsu2f7dddc2021-06-04 18:43:34 +0900556 if (dev_get_parent(dev) == bus)
557 continue;
558
Simon Glasscaa4daa2020-12-03 16:55:18 -0700559 pplat = dev_get_parent_plat(dev);
Bin Mengbbbcb522015-10-01 00:36:02 -0700560 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
561 set_vga_bridge_bits(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700562 }
Pali RohĂĄr8c303bc2022-01-17 16:38:37 +0100563 if (hose->last_busno < sub_bus)
564 hose->last_busno = sub_bus;
Simon Glassff3e0772015-03-05 12:25:25 -0700565 debug("%s: done\n", __func__);
566
Simon Glass42f36632020-12-16 21:20:18 -0700567 return log_msg_ret("sub", sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700568}
569
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300570int pci_generic_mmap_write_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700571 const struct udevice *bus,
572 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
573 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300574 pci_dev_t bdf,
575 uint offset,
576 ulong value,
577 enum pci_size_t size)
578{
579 void *address;
580
581 if (addr_f(bus, bdf, offset, &address) < 0)
582 return 0;
583
584 switch (size) {
585 case PCI_SIZE_8:
586 writeb(value, address);
587 return 0;
588 case PCI_SIZE_16:
589 writew(value, address);
590 return 0;
591 case PCI_SIZE_32:
592 writel(value, address);
593 return 0;
594 default:
595 return -EINVAL;
596 }
597}
598
599int pci_generic_mmap_read_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700600 const struct udevice *bus,
601 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
602 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300603 pci_dev_t bdf,
604 uint offset,
605 ulong *valuep,
606 enum pci_size_t size)
607{
608 void *address;
609
610 if (addr_f(bus, bdf, offset, &address) < 0) {
611 *valuep = pci_get_ff(size);
612 return 0;
613 }
614
615 switch (size) {
616 case PCI_SIZE_8:
617 *valuep = readb(address);
618 return 0;
619 case PCI_SIZE_16:
620 *valuep = readw(address);
621 return 0;
622 case PCI_SIZE_32:
623 *valuep = readl(address);
624 return 0;
625 default:
626 return -EINVAL;
627 }
628}
629
Simon Glass5e23b8b2015-11-29 13:17:49 -0700630int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassff3e0772015-03-05 12:25:25 -0700631{
Pali RohĂĄr63ae80d2021-10-07 14:50:58 +0200632 u8 header_type;
Simon Glassff3e0772015-03-05 12:25:25 -0700633 int sub_bus;
634 int ret;
Suneel Garapati636cc172019-10-19 15:52:32 -0700635 int ea_pos;
636 u8 reg;
Simon Glassff3e0772015-03-05 12:25:25 -0700637
638 debug("%s\n", __func__);
Simon Glassff3e0772015-03-05 12:25:25 -0700639
Pali RohĂĄr63ae80d2021-10-07 14:50:58 +0200640 dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);
641 header_type &= 0x7f;
642 if (header_type != PCI_HEADER_TYPE_BRIDGE) {
643 debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n",
644 __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type);
645 return log_msg_ret("probe", -EINVAL);
646 }
647
Suneel Garapati636cc172019-10-19 15:52:32 -0700648 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
649 if (ea_pos) {
650 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
651 &reg);
652 sub_bus = reg;
653 } else {
654 sub_bus = pci_get_bus_max() + 1;
655 }
Simon Glassff3e0772015-03-05 12:25:25 -0700656 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700657 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700658
659 ret = device_probe(bus);
660 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600661 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -0700662 ret);
Simon Glass42f36632020-12-16 21:20:18 -0700663 return log_msg_ret("probe", ret);
Simon Glassff3e0772015-03-05 12:25:25 -0700664 }
Suneel Garapati636cc172019-10-19 15:52:32 -0700665
Masami Hiramatsu19e1b8d2021-04-16 14:53:46 -0700666 if (!ea_pos)
667 sub_bus = pci_get_bus_max();
668
Simon Glass5e23b8b2015-11-29 13:17:49 -0700669 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700670
671 return sub_bus;
672}
673
Simon Glassaba92962015-07-06 16:47:44 -0600674/**
675 * pci_match_one_device - Tell if a PCI device structure has a matching
676 * PCI device id structure
677 * @id: single PCI device id structure to match
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800678 * @find: the PCI device id structure to match against
Simon Glassaba92962015-07-06 16:47:44 -0600679 *
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800680 * Returns true if the finding pci_device_id structure matched or false if
681 * there is no match.
Simon Glassaba92962015-07-06 16:47:44 -0600682 */
683static bool pci_match_one_id(const struct pci_device_id *id,
684 const struct pci_device_id *find)
685{
686 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
687 (id->device == PCI_ANY_ID || id->device == find->device) &&
688 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
689 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
690 !((id->class ^ find->class) & id->class_mask))
691 return true;
692
693 return false;
694}
695
696/**
Simon Glassf5cbb5c2021-06-27 17:50:57 -0600697 * pci_need_device_pre_reloc() - Check if a device should be bound
698 *
699 * This checks a list of vendor/device-ID values indicating devices that should
700 * be bound before relocation.
701 *
702 * @bus: Bus to check
703 * @vendor: Vendor ID to check
704 * @device: Device ID to check
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100705 * Return: true if the vendor/device is in the list, false if not
Simon Glassf5cbb5c2021-06-27 17:50:57 -0600706 */
707static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor,
708 uint device)
709{
710 u32 vendev;
711 int index;
712
713 for (index = 0;
714 !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index,
715 &vendev);
716 index++) {
717 if (vendev == PCI_VENDEV(vendor, device))
718 return true;
719 }
720
721 return false;
722}
723
724/**
Simon Glassaba92962015-07-06 16:47:44 -0600725 * pci_find_and_bind_driver() - Find and bind the right PCI driver
726 *
727 * This only looks at certain fields in the descriptor.
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600728 *
729 * @parent: Parent bus
730 * @find_id: Specification of the driver to find
731 * @bdf: Bus/device/function addreess - see PCI_BDF()
732 * @devp: Returns a pointer to the device created
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100733 * Return: 0 if OK, -EPERM if the device is not needed before relocation and
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600734 * therefore was not created, other -ve value on error
Simon Glassaba92962015-07-06 16:47:44 -0600735 */
736static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600737 struct pci_device_id *find_id,
738 pci_dev_t bdf, struct udevice **devp)
Simon Glassaba92962015-07-06 16:47:44 -0600739{
740 struct pci_driver_entry *start, *entry;
Marek Vasut02e4d382018-10-10 21:27:06 +0200741 ofnode node = ofnode_null();
Simon Glassaba92962015-07-06 16:47:44 -0600742 const char *drv;
743 int n_ents;
744 int ret;
745 char name[30], *str;
Bin Meng08fc7b82015-08-20 06:40:17 -0700746 bool bridge;
Simon Glassaba92962015-07-06 16:47:44 -0600747
748 *devp = NULL;
749
750 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
751 find_id->vendor, find_id->device);
Marek Vasut02e4d382018-10-10 21:27:06 +0200752
753 /* Determine optional OF node */
Suneel Garapatibc301402019-10-19 16:02:48 -0700754 if (ofnode_valid(dev_ofnode(parent)))
755 pci_dev_find_ofnode(parent, bdf, &node);
Marek Vasut02e4d382018-10-10 21:27:06 +0200756
Michael Wallea6cd5972019-12-01 17:45:18 +0100757 if (ofnode_valid(node) && !ofnode_is_available(node)) {
758 debug("%s: Ignoring disabled device\n", __func__);
Simon Glass42f36632020-12-16 21:20:18 -0700759 return log_msg_ret("dis", -EPERM);
Michael Wallea6cd5972019-12-01 17:45:18 +0100760 }
761
Simon Glassaba92962015-07-06 16:47:44 -0600762 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
763 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
764 for (entry = start; entry != start + n_ents; entry++) {
765 const struct pci_device_id *id;
766 struct udevice *dev;
767 const struct driver *drv;
768
769 for (id = entry->match;
770 id->vendor || id->subvendor || id->class_mask;
771 id++) {
772 if (!pci_match_one_id(id, find_id))
773 continue;
774
775 drv = entry->driver;
Bin Meng08fc7b82015-08-20 06:40:17 -0700776
777 /*
778 * In the pre-relocation phase, we only bind devices
779 * whose driver has the DM_FLAG_PRE_RELOC set, to save
780 * precious memory space as on some platforms as that
781 * space is pretty limited (ie: using Cache As RAM).
782 */
783 if (!(gd->flags & GD_FLG_RELOC) &&
784 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glass42f36632020-12-16 21:20:18 -0700785 return log_msg_ret("pre", -EPERM);
Bin Meng08fc7b82015-08-20 06:40:17 -0700786
Simon Glassaba92962015-07-06 16:47:44 -0600787 /*
788 * We could pass the descriptor to the driver as
Simon Glasscaa4daa2020-12-03 16:55:18 -0700789 * plat (instead of NULL) and allow its bind()
Simon Glassaba92962015-07-06 16:47:44 -0600790 * method to return -ENOENT if it doesn't support this
791 * device. That way we could continue the search to
792 * find another driver. For now this doesn't seem
793 * necesssary, so just bind the first match.
794 */
Simon Glass734206d2020-11-28 17:50:01 -0700795 ret = device_bind(parent, drv, drv->name, NULL, node,
796 &dev);
Simon Glassaba92962015-07-06 16:47:44 -0600797 if (ret)
798 goto error;
799 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menged698aa2018-08-03 01:14:44 -0700800 dev->driver_data = id->driver_data;
Simon Glassaba92962015-07-06 16:47:44 -0600801 *devp = dev;
802 return 0;
803 }
804 }
805
Bin Meng08fc7b82015-08-20 06:40:17 -0700806 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
807 /*
808 * In the pre-relocation phase, we only bind bridge devices to save
809 * precious memory space as on some platforms as that space is pretty
810 * limited (ie: using Cache As RAM).
811 */
Simon Glassf5cbb5c2021-06-27 17:50:57 -0600812 if (!(gd->flags & GD_FLG_RELOC) && !bridge &&
813 !pci_need_device_pre_reloc(parent, find_id->vendor,
814 find_id->device))
Simon Glass42f36632020-12-16 21:20:18 -0700815 return log_msg_ret("notbr", -EPERM);
Bin Meng08fc7b82015-08-20 06:40:17 -0700816
Simon Glassaba92962015-07-06 16:47:44 -0600817 /* Bind a generic driver so that the device can be used */
Simon Glass8b85dfc2020-12-16 21:20:07 -0700818 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
Bin Meng4d8615c2015-07-19 00:20:04 +0800819 PCI_FUNC(bdf));
Simon Glassaba92962015-07-06 16:47:44 -0600820 str = strdup(name);
821 if (!str)
822 return -ENOMEM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700823 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
824
Marek Vasut02e4d382018-10-10 21:27:06 +0200825 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glassaba92962015-07-06 16:47:44 -0600826 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600827 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dec42640c2017-05-08 20:40:16 +0200828 free(str);
Simon Glassaba92962015-07-06 16:47:44 -0600829 return ret;
830 }
831 debug("%s: No match found: bound generic driver instead\n", __func__);
832
833 return 0;
834
835error:
836 debug("%s: No match found: error %d\n", __func__, ret);
837 return ret;
838}
839
Tim Harveycecd0132021-04-16 14:53:47 -0700840__weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
841{
842}
843
Simon Glassff3e0772015-03-05 12:25:25 -0700844int pci_bind_bus_devices(struct udevice *bus)
845{
846 ulong vendor, device;
847 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800848 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700849 bool found_multi;
Suneel Garapatia3fac3f2019-10-23 18:40:36 -0700850 int ari_off;
Simon Glassff3e0772015-03-05 12:25:25 -0700851 int ret;
852
853 found_multi = false;
Simon Glass8b85dfc2020-12-16 21:20:07 -0700854 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
Bin Meng4d8615c2015-07-19 00:20:04 +0800855 PCI_MAX_PCI_FUNCTIONS - 1);
Simon Glass8b85dfc2020-12-16 21:20:07 -0700856 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
Bin Meng4d8615c2015-07-19 00:20:04 +0800857 bdf += PCI_BDF(0, 0, 1)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700858 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700859 struct udevice *dev;
860 ulong class;
861
Bin Meng64e45f72018-08-03 01:14:37 -0700862 if (!PCI_FUNC(bdf))
863 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800864 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700865 continue;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800866
Simon Glassff3e0772015-03-05 12:25:25 -0700867 /* Check only the first access, we don't expect problems */
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800868 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
869 PCI_SIZE_16);
Pali RohĂĄr2348e722021-09-07 18:07:08 +0200870 if (ret || vendor == 0xffff || vendor == 0x0000)
Simon Glassff3e0772015-03-05 12:25:25 -0700871 continue;
872
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800873 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
874 &header_type, PCI_SIZE_8);
875
Bin Meng4d8615c2015-07-19 00:20:04 +0800876 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-03-05 12:25:25 -0700877 found_multi = header_type & 0x80;
878
Simon Glass09115692019-09-25 08:56:12 -0600879 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Simon Glass8b85dfc2020-12-16 21:20:07 -0700880 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Bin Meng4d8615c2015-07-19 00:20:04 +0800881 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassff3e0772015-03-05 12:25:25 -0700882 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800883 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600884 PCI_SIZE_32);
885 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700886
887 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800888 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass09115692019-09-25 08:56:12 -0600889 debug(": find ret=%d\n", ret);
Simon Glassff3e0772015-03-05 12:25:25 -0700890
Simon Glass8bd42522015-11-29 13:18:09 -0700891 /* If nothing in the device tree, bind a device */
Simon Glassff3e0772015-03-05 12:25:25 -0700892 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600893 struct pci_device_id find_id;
894 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700895
Simon Glassaba92962015-07-06 16:47:44 -0600896 memset(&find_id, '\0', sizeof(find_id));
897 find_id.vendor = vendor;
898 find_id.device = device;
899 find_id.class = class;
900 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng4d8615c2015-07-19 00:20:04 +0800901 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600902 PCI_SUBSYSTEM_VENDOR_ID,
903 &val, PCI_SIZE_32);
904 find_id.subvendor = val & 0xffff;
905 find_id.subdevice = val >> 16;
906 }
Bin Meng4d8615c2015-07-19 00:20:04 +0800907 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600908 &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700909 }
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600910 if (ret == -EPERM)
911 continue;
912 else if (ret)
Simon Glassff3e0772015-03-05 12:25:25 -0700913 return ret;
914
915 /* Update the platform data */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700916 pplat = dev_get_parent_plat(dev);
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600917 pplat->devfn = PCI_MASK_BUS(bdf);
918 pplat->vendor = vendor;
919 pplat->device = device;
920 pplat->class = class;
Suneel Garapatia3fac3f2019-10-23 18:40:36 -0700921
922 if (IS_ENABLED(CONFIG_PCI_ARID)) {
923 ari_off = dm_pci_find_ext_capability(dev,
924 PCI_EXT_CAP_ID_ARI);
925 if (ari_off) {
926 u16 ari_cap;
927
928 /*
929 * Read Next Function number in ARI Cap
930 * Register
931 */
932 dm_pci_read_config16(dev, ari_off + 4,
933 &ari_cap);
934 /*
935 * Update next scan on this function number,
936 * subtract 1 in BDF to satisfy loop increment.
937 */
938 if (ari_cap & 0xff00) {
939 bdf = PCI_BDF(PCI_BUS(bdf),
940 PCI_DEV(ari_cap),
941 PCI_FUNC(ari_cap));
942 bdf = bdf - 0x100;
943 }
944 }
945 }
Tim Harveycecd0132021-04-16 14:53:47 -0700946
947 board_pci_fixup_dev(bus, dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700948 }
949
950 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -0700951}
952
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700953static void decode_regions(struct pci_controller *hose, ofnode parent_node,
954 ofnode node)
Simon Glassff3e0772015-03-05 12:25:25 -0700955{
956 int pci_addr_cells, addr_cells, size_cells;
957 int cells_per_record;
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200958 struct bd_info *bd;
Simon Glassff3e0772015-03-05 12:25:25 -0700959 const u32 *prop;
Stefan Roesee0024742020-07-23 16:34:10 +0200960 int max_regions;
Simon Glassff3e0772015-03-05 12:25:25 -0700961 int len;
962 int i;
963
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900964 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700965 if (!prop) {
966 debug("%s: Cannot decode regions\n", __func__);
967 return;
968 }
969
Simon Glass878d68c2017-06-12 06:21:31 -0600970 pci_addr_cells = ofnode_read_simple_addr_cells(node);
971 addr_cells = ofnode_read_simple_addr_cells(parent_node);
972 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassff3e0772015-03-05 12:25:25 -0700973
974 /* PCI addresses are always 3-cells */
975 len /= sizeof(u32);
976 cells_per_record = pci_addr_cells + addr_cells + size_cells;
977 hose->region_count = 0;
978 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
979 cells_per_record);
Stefan Roesee0024742020-07-23 16:34:10 +0200980
981 /* Dynamically allocate the regions array */
982 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
983 hose->regions = (struct pci_region *)
984 calloc(1, max_regions * sizeof(struct pci_region));
985
986 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassff3e0772015-03-05 12:25:25 -0700987 u64 pci_addr, addr, size;
988 int space_code;
989 u32 flags;
990 int type;
Simon Glass9526d832015-11-19 20:26:58 -0700991 int pos;
Simon Glassff3e0772015-03-05 12:25:25 -0700992
993 if (len < cells_per_record)
994 break;
995 flags = fdt32_to_cpu(prop[0]);
996 space_code = (flags >> 24) & 3;
997 pci_addr = fdtdec_get_number(prop + 1, 2);
998 prop += pci_addr_cells;
999 addr = fdtdec_get_number(prop, addr_cells);
1000 prop += addr_cells;
1001 size = fdtdec_get_number(prop, size_cells);
1002 prop += size_cells;
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001003 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
1004 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassff3e0772015-03-05 12:25:25 -07001005 if (space_code & 2) {
1006 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
1007 PCI_REGION_MEM;
1008 } else if (space_code & 1) {
1009 type = PCI_REGION_IO;
1010 } else {
1011 continue;
1012 }
Tuomas Tynkkynen52ba9072018-05-14 18:47:50 +03001013
1014 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
1015 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
1016 debug(" - beyond the 32-bit boundary, ignoring\n");
1017 continue;
1018 }
1019
Simon Glass9526d832015-11-19 20:26:58 -07001020 pos = -1;
Suneel Garapati4cf56ec2019-10-19 17:10:20 -07001021 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
1022 for (i = 0; i < hose->region_count; i++) {
1023 if (hose->regions[i].flags == type)
1024 pos = i;
1025 }
Simon Glass9526d832015-11-19 20:26:58 -07001026 }
Suneel Garapati4cf56ec2019-10-19 17:10:20 -07001027
Simon Glass9526d832015-11-19 20:26:58 -07001028 if (pos == -1)
1029 pos = hose->region_count++;
1030 debug(" - type=%d, pos=%d\n", type, pos);
1031 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassff3e0772015-03-05 12:25:25 -07001032 }
1033
1034 /* Add a region for our local memory */
Stefan Roesedfaf6a52020-08-12 11:55:46 +02001035 bd = gd->bd;
Bin Meng1eaf7802018-03-27 00:46:05 -07001036 if (!bd)
Christian Gmeinerf2825f62018-06-10 06:25:05 -07001037 return;
Bin Meng1eaf7802018-03-27 00:46:05 -07001038
Bernhard Messerklinger664758c2018-02-15 08:59:53 +01001039 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1040 if (bd->bi_dram[i].size) {
Daniel Schwierzecka45343a2021-07-15 20:53:56 +02001041 phys_addr_t start = bd->bi_dram[i].start;
1042
1043 if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY))
1044 start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start);
1045
Bernhard Messerklinger664758c2018-02-15 08:59:53 +01001046 pci_set_region(hose->regions + hose->region_count++,
Daniel Schwierzecka45343a2021-07-15 20:53:56 +02001047 start, start, bd->bi_dram[i].size,
Bernhard Messerklinger664758c2018-02-15 08:59:53 +01001048 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1049 }
1050 }
Simon Glassff3e0772015-03-05 12:25:25 -07001051
Christian Gmeinerf2825f62018-06-10 06:25:05 -07001052 return;
Simon Glassff3e0772015-03-05 12:25:25 -07001053}
1054
1055static int pci_uclass_pre_probe(struct udevice *bus)
1056{
1057 struct pci_controller *hose;
Simon Glass42f36632020-12-16 21:20:18 -07001058 struct uclass *uc;
1059 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -07001060
Simon Glass8b85dfc2020-12-16 21:20:07 -07001061 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -07001062 bus->parent->name);
Simon Glass0fd3d912020-12-22 19:30:28 -07001063 hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001064
Simon Glass42f36632020-12-16 21:20:18 -07001065 /*
1066 * Set the sequence number, if device_bind() doesn't. We want control
1067 * of this so that numbers are allocated as devices are probed. That
1068 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1069 * higher than their parents)
1070 */
1071 if (dev_seq(bus) == -1) {
1072 ret = uclass_get(UCLASS_PCI, &uc);
1073 if (ret)
1074 return ret;
Simon Glass24621392020-12-19 10:40:09 -07001075 bus->seq_ = uclass_find_next_free_seq(uc);
Simon Glass42f36632020-12-16 21:20:18 -07001076 }
1077
Simon Glassff3e0772015-03-05 12:25:25 -07001078 /* For bridges, use the top-level PCI controller */
Paul Burton65f62b12016-09-08 07:47:32 +01001079 if (!device_is_on_pci_bus(bus)) {
Simon Glassff3e0772015-03-05 12:25:25 -07001080 hose->ctlr = bus;
Christian Gmeinerf2825f62018-06-10 06:25:05 -07001081 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassff3e0772015-03-05 12:25:25 -07001082 } else {
1083 struct pci_controller *parent_hose;
1084
1085 parent_hose = dev_get_uclass_priv(bus->parent);
1086 hose->ctlr = parent_hose->bus;
1087 }
Simon Glass42f36632020-12-16 21:20:18 -07001088
Simon Glassff3e0772015-03-05 12:25:25 -07001089 hose->bus = bus;
Simon Glass8b85dfc2020-12-16 21:20:07 -07001090 hose->first_busno = dev_seq(bus);
1091 hose->last_busno = dev_seq(bus);
Simon Glass7d14ee42020-12-19 10:40:13 -07001092 if (dev_has_ofnode(bus)) {
Suneel Garapatif0c36922020-05-04 21:25:25 -07001093 hose->skip_auto_config_until_reloc =
1094 dev_read_bool(bus,
1095 "u-boot,skip-auto-config-until-reloc");
1096 }
Simon Glassff3e0772015-03-05 12:25:25 -07001097
1098 return 0;
1099}
1100
1101static int pci_uclass_post_probe(struct udevice *bus)
1102{
Simon Glass2206ac22019-12-06 21:41:37 -07001103 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001104 int ret;
1105
Simon Glass8b85dfc2020-12-16 21:20:07 -07001106 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
Simon Glassff3e0772015-03-05 12:25:25 -07001107 ret = pci_bind_bus_devices(bus);
1108 if (ret)
Simon Glass42f36632020-12-16 21:20:18 -07001109 return log_msg_ret("bind", ret);
Simon Glassff3e0772015-03-05 12:25:25 -07001110
Simon Glassf1f44382020-04-26 09:12:56 -06001111 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass2206ac22019-12-06 21:41:37 -07001112 (!hose->skip_auto_config_until_reloc ||
1113 (gd->flags & GD_FLG_RELOC))) {
1114 ret = pci_auto_config_devices(bus);
1115 if (ret < 0)
Simon Glass42f36632020-12-16 21:20:18 -07001116 return log_msg_ret("cfg", ret);
Simon Glass2206ac22019-12-06 21:41:37 -07001117 }
Simon Glassff3e0772015-03-05 12:25:25 -07001118
Bin Meng348b7442015-08-20 06:40:23 -07001119#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1120 /*
1121 * Per Intel FSP specification, we should call FSP notify API to
1122 * inform FSP that PCI enumeration has been done so that FSP will
1123 * do any necessary initialization as required by the chipset's
1124 * BIOS Writer's Guide (BWG).
1125 *
1126 * Unfortunately we have to put this call here as with driver model,
1127 * the enumeration is all done on a lazy basis as needed, so until
1128 * something is touched on PCI it won't happen.
1129 *
1130 * Note we only call this 1) after U-Boot is relocated, and 2)
1131 * root bus has finished probing.
1132 */
Simon Glass8b85dfc2020-12-16 21:20:07 -07001133 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
Bin Meng348b7442015-08-20 06:40:23 -07001134 ret = fsp_init_phase_pci();
Simon Glass4d214552015-09-08 17:52:47 -06001135 if (ret)
Simon Glass42f36632020-12-16 21:20:18 -07001136 return log_msg_ret("fsp", ret);
Simon Glass4d214552015-09-08 17:52:47 -06001137 }
Bin Meng348b7442015-08-20 06:40:23 -07001138#endif
1139
Simon Glass4d214552015-09-08 17:52:47 -06001140 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -07001141}
1142
1143static int pci_uclass_child_post_bind(struct udevice *dev)
1144{
Simon Glass8a8d24b2020-12-03 16:55:23 -07001145 struct pci_child_plat *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -07001146
Simon Glass7d14ee42020-12-19 10:40:13 -07001147 if (!dev_has_ofnode(dev))
Simon Glassff3e0772015-03-05 12:25:25 -07001148 return 0;
1149
Simon Glasscaa4daa2020-12-03 16:55:18 -07001150 pplat = dev_get_parent_plat(dev);
Bin Meng1f6b08b2018-08-03 01:14:36 -07001151
1152 /* Extract vendor id and device id if available */
1153 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1154
1155 /* Extract the devfn from fdt_pci_addr */
Stefan Roeseb5214202019-01-25 11:52:42 +01001156 pplat->devfn = pci_get_devfn(dev);
Simon Glassff3e0772015-03-05 12:25:25 -07001157
1158 return 0;
1159}
1160
Simon Glassc4e72c42020-01-27 08:49:37 -07001161static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng4d8615c2015-07-19 00:20:04 +08001162 uint offset, ulong *valuep,
1163 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001164{
Simon Glass0fd3d912020-12-22 19:30:28 -07001165 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001166
1167 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1168}
1169
Bin Meng4d8615c2015-07-19 00:20:04 +08001170static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1171 uint offset, ulong value,
1172 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001173{
Simon Glass0fd3d912020-12-22 19:30:28 -07001174 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001175
1176 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1177}
1178
Simon Glass76c3fbc2015-08-10 07:05:04 -06001179static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1180{
1181 struct udevice *dev;
1182 int ret = 0;
1183
1184 /*
1185 * Scan through all the PCI controllers. On x86 there will only be one
1186 * but that is not necessarily true on other hardware.
1187 */
1188 do {
1189 device_find_first_child(bus, &dev);
1190 if (dev) {
1191 *devp = dev;
1192 return 0;
1193 }
1194 ret = uclass_next_device(&bus);
1195 if (ret)
1196 return ret;
1197 } while (bus);
1198
1199 return 0;
1200}
1201
1202int pci_find_next_device(struct udevice **devp)
1203{
1204 struct udevice *child = *devp;
1205 struct udevice *bus = child->parent;
1206 int ret;
1207
1208 /* First try all the siblings */
1209 *devp = NULL;
1210 while (child) {
1211 device_find_next_child(&child);
1212 if (child) {
1213 *devp = child;
1214 return 0;
1215 }
1216 }
1217
1218 /* We ran out of siblings. Try the next bus */
1219 ret = uclass_next_device(&bus);
1220 if (ret)
1221 return ret;
1222
1223 return bus ? skip_to_next_device(bus, devp) : 0;
1224}
1225
1226int pci_find_first_device(struct udevice **devp)
1227{
1228 struct udevice *bus;
1229 int ret;
1230
1231 *devp = NULL;
1232 ret = uclass_first_device(UCLASS_PCI, &bus);
1233 if (ret)
1234 return ret;
1235
1236 return skip_to_next_device(bus, devp);
1237}
1238
Simon Glass9289db62015-11-19 20:26:59 -07001239ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1240{
1241 switch (size) {
1242 case PCI_SIZE_8:
1243 return (value >> ((offset & 3) * 8)) & 0xff;
1244 case PCI_SIZE_16:
1245 return (value >> ((offset & 2) * 8)) & 0xffff;
1246 default:
1247 return value;
1248 }
1249}
1250
1251ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1252 enum pci_size_t size)
1253{
1254 uint off_mask;
1255 uint val_mask, shift;
1256 ulong ldata, mask;
1257
1258 switch (size) {
1259 case PCI_SIZE_8:
1260 off_mask = 3;
1261 val_mask = 0xff;
1262 break;
1263 case PCI_SIZE_16:
1264 off_mask = 2;
1265 val_mask = 0xffff;
1266 break;
1267 default:
1268 return value;
1269 }
1270 shift = (offset & off_mask) * 8;
1271 ldata = (value & val_mask) << shift;
1272 mask = val_mask << shift;
1273 value = (old & ~mask) | ldata;
1274
1275 return value;
1276}
1277
Rayagonda Kokatanur143eb5b2020-05-12 13:29:49 +05301278int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1279{
1280 int pci_addr_cells, addr_cells, size_cells;
1281 int cells_per_record;
1282 const u32 *prop;
1283 int len;
1284 int i = 0;
1285
1286 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1287 if (!prop) {
1288 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1289 dev->name);
1290 return -EINVAL;
1291 }
1292
1293 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1294 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1295 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1296
1297 /* PCI addresses are always 3-cells */
1298 len /= sizeof(u32);
1299 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1300 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1301 cells_per_record);
1302
1303 while (len) {
1304 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1305 prop += pci_addr_cells;
1306 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1307 prop += addr_cells;
1308 memp->size = fdtdec_get_number(prop, size_cells);
1309 prop += size_cells;
1310
1311 if (i == index)
1312 return 0;
1313 i++;
1314 len -= cells_per_record;
1315 }
1316
1317 return -EINVAL;
1318}
1319
Simon Glassf9260332015-11-19 20:27:01 -07001320int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1321 struct pci_region **memp, struct pci_region **prefp)
1322{
1323 struct udevice *bus = pci_get_controller(dev);
1324 struct pci_controller *hose = dev_get_uclass_priv(bus);
1325 int i;
1326
1327 *iop = NULL;
1328 *memp = NULL;
1329 *prefp = NULL;
1330 for (i = 0; i < hose->region_count; i++) {
1331 switch (hose->regions[i].flags) {
1332 case PCI_REGION_IO:
1333 if (!*iop || (*iop)->size < hose->regions[i].size)
1334 *iop = hose->regions + i;
1335 break;
1336 case PCI_REGION_MEM:
1337 if (!*memp || (*memp)->size < hose->regions[i].size)
1338 *memp = hose->regions + i;
1339 break;
1340 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1341 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1342 *prefp = hose->regions + i;
1343 break;
1344 }
1345 }
1346
1347 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1348}
1349
Simon Glass194fca92020-01-27 08:49:38 -07001350u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glassbab17cf2015-11-29 13:17:53 -07001351{
1352 u32 addr;
1353 int bar;
1354
1355 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1356 dm_pci_read_config32(dev, bar, &addr);
Simon Glass9ece4b02020-04-09 10:27:36 -06001357
1358 /*
1359 * If we get an invalid address, return this so that comparisons with
1360 * FDT_ADDR_T_NONE work correctly
1361 */
1362 if (addr == 0xffffffff)
1363 return addr;
1364 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glassbab17cf2015-11-29 13:17:53 -07001365 return addr & PCI_BASE_ADDRESS_IO_MASK;
1366 else
1367 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1368}
1369
Simon Glass9d731c82016-01-18 20:19:15 -07001370void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1371{
1372 int bar;
1373
1374 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1375 dm_pci_write_config32(dev, bar, addr);
1376}
1377
Simon Glass21d1fe72015-11-29 13:18:03 -07001378static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1379 pci_addr_t bus_addr, unsigned long flags,
1380 unsigned long skip_mask, phys_addr_t *pa)
1381{
1382 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1383 struct pci_region *res;
1384 int i;
1385
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001386 if (hose->region_count == 0) {
1387 *pa = bus_addr;
1388 return 0;
1389 }
1390
Simon Glass21d1fe72015-11-29 13:18:03 -07001391 for (i = 0; i < hose->region_count; i++) {
1392 res = &hose->regions[i];
1393
1394 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1395 continue;
1396
1397 if (res->flags & skip_mask)
1398 continue;
1399
1400 if (bus_addr >= res->bus_start &&
1401 (bus_addr - res->bus_start) < res->size) {
1402 *pa = (bus_addr - res->bus_start + res->phys_start);
1403 return 0;
1404 }
1405 }
1406
1407 return 1;
1408}
1409
1410phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1411 unsigned long flags)
1412{
1413 phys_addr_t phys_addr = 0;
1414 struct udevice *ctlr;
1415 int ret;
1416
1417 /* The root controller has the region information */
1418 ctlr = pci_get_controller(dev);
1419
1420 /*
1421 * if PCI_REGION_MEM is set we do a two pass search with preference
1422 * on matches that don't have PCI_REGION_SYS_MEMORY set
1423 */
1424 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1425 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1426 flags, PCI_REGION_SYS_MEMORY,
1427 &phys_addr);
1428 if (!ret)
1429 return phys_addr;
1430 }
1431
1432 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1433
1434 if (ret)
1435 puts("pci_hose_bus_to_phys: invalid physical address\n");
1436
1437 return phys_addr;
1438}
1439
Vladimir Oltean8101a402021-09-17 15:11:27 +03001440static int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1441 unsigned long flags, unsigned long skip_mask,
1442 pci_addr_t *ba)
Simon Glass21d1fe72015-11-29 13:18:03 -07001443{
1444 struct pci_region *res;
1445 struct udevice *ctlr;
1446 pci_addr_t bus_addr;
1447 int i;
1448 struct pci_controller *hose;
1449
1450 /* The root controller has the region information */
1451 ctlr = pci_get_controller(dev);
1452 hose = dev_get_uclass_priv(ctlr);
1453
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001454 if (hose->region_count == 0) {
1455 *ba = phys_addr;
1456 return 0;
1457 }
1458
Simon Glass21d1fe72015-11-29 13:18:03 -07001459 for (i = 0; i < hose->region_count; i++) {
1460 res = &hose->regions[i];
1461
1462 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1463 continue;
1464
1465 if (res->flags & skip_mask)
1466 continue;
1467
1468 bus_addr = phys_addr - res->phys_start + res->bus_start;
1469
1470 if (bus_addr >= res->bus_start &&
1471 (bus_addr - res->bus_start) < res->size) {
1472 *ba = bus_addr;
1473 return 0;
1474 }
1475 }
1476
1477 return 1;
1478}
1479
1480pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1481 unsigned long flags)
1482{
1483 pci_addr_t bus_addr = 0;
1484 int ret;
1485
1486 /*
1487 * if PCI_REGION_MEM is set we do a two pass search with preference
1488 * on matches that don't have PCI_REGION_SYS_MEMORY set
1489 */
1490 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1491 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1492 PCI_REGION_SYS_MEMORY, &bus_addr);
1493 if (!ret)
1494 return bus_addr;
1495 }
1496
1497 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1498
1499 if (ret)
1500 puts("pci_hose_phys_to_bus: invalid physical address\n");
1501
1502 return bus_addr;
1503}
1504
Suneel Garapati51eeae92019-10-19 16:34:16 -07001505static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
Simon Glass8a8d24b2020-12-03 16:55:23 -07001506 struct pci_child_plat *pdata)
Suneel Garapati51eeae92019-10-19 16:34:16 -07001507{
1508 phys_addr_t addr = 0;
1509
1510 /*
1511 * In the case of a Virtual Function device using BAR
1512 * base and size, add offset for VFn BAR(1, 2, 3...n)
1513 */
1514 if (pdata->is_virtfn) {
1515 size_t sz;
1516 u32 ea_entry;
1517
1518 /* MaxOffset, 1st DW */
1519 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1520 sz = ea_entry & PCI_EA_FIELD_MASK;
1521 /* Fill up lower 2 bits */
1522 sz |= (~PCI_EA_FIELD_MASK);
1523
1524 if (ea_entry & PCI_EA_IS_64) {
1525 /* MaxOffset 2nd DW */
1526 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1527 sz |= ((u64)ea_entry) << 32;
1528 }
1529
1530 addr = (pdata->virtid - 1) * (sz + 1);
1531 }
1532
1533 return addr;
1534}
1535
Alex Marginean0b143d82019-06-07 11:24:23 +03001536static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
Simon Glass8a8d24b2020-12-03 16:55:23 -07001537 int ea_off, struct pci_child_plat *pdata)
Alex Marginean0b143d82019-06-07 11:24:23 +03001538{
1539 int ea_cnt, i, entry_size;
1540 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1541 u32 ea_entry;
1542 phys_addr_t addr;
1543
Suneel Garapati51eeae92019-10-19 16:34:16 -07001544 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1545 /*
1546 * In the case of a Virtual Function device, device is
1547 * Physical function, so pdata will point to required VF
1548 * specific data.
1549 */
1550 if (pdata->is_virtfn)
1551 bar_id += PCI_EA_BEI_VF_BAR0;
1552 }
1553
Alex Marginean0b143d82019-06-07 11:24:23 +03001554 /* EA capability structure header */
1555 dm_pci_read_config32(dev, ea_off, &ea_entry);
1556 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1557 ea_off += PCI_EA_FIRST_ENT;
1558
1559 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1560 /* Entry header */
1561 dm_pci_read_config32(dev, ea_off, &ea_entry);
1562 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1563
1564 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1565 continue;
1566
1567 /* Base address, 1st DW */
1568 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1569 addr = ea_entry & PCI_EA_FIELD_MASK;
1570 if (ea_entry & PCI_EA_IS_64) {
1571 /* Base address, 2nd DW, skip over 4B MaxOffset */
1572 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1573 addr |= ((u64)ea_entry) << 32;
1574 }
1575
Suneel Garapati51eeae92019-10-19 16:34:16 -07001576 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1577 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1578
Alex Marginean0b143d82019-06-07 11:24:23 +03001579 /* size ignored for now */
Suneel Garapatib3699a12019-10-19 16:44:35 -07001580 return map_physmem(addr, 0, flags);
Alex Marginean0b143d82019-06-07 11:24:23 +03001581 }
1582
1583 return 0;
1584}
1585
Simon Glass21d1fe72015-11-29 13:18:03 -07001586void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1587{
Simon Glass8a8d24b2020-12-03 16:55:23 -07001588 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
Suneel Garapati51eeae92019-10-19 16:34:16 -07001589 struct udevice *udev = dev;
Simon Glass21d1fe72015-11-29 13:18:03 -07001590 pci_addr_t pci_bus_addr;
1591 u32 bar_response;
Alex Marginean0b143d82019-06-07 11:24:23 +03001592 int ea_off;
1593
Suneel Garapati51eeae92019-10-19 16:34:16 -07001594 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1595 /*
1596 * In case of Virtual Function devices, use PF udevice
1597 * as EA capability is defined in Physical Function
1598 */
1599 if (pdata->is_virtfn)
1600 udev = pdata->pfdev;
1601 }
1602
Alex Marginean0b143d82019-06-07 11:24:23 +03001603 /*
1604 * if the function supports Enhanced Allocation use that instead of
1605 * BARs
Suneel Garapati51eeae92019-10-19 16:34:16 -07001606 * Incase of virtual functions, pdata will help read VF BEI
1607 * and EA entry size.
Alex Marginean0b143d82019-06-07 11:24:23 +03001608 */
Suneel Garapati51eeae92019-10-19 16:34:16 -07001609 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
Alex Marginean0b143d82019-06-07 11:24:23 +03001610 if (ea_off)
Suneel Garapati51eeae92019-10-19 16:34:16 -07001611 return dm_pci_map_ea_bar(udev, bar, flags, ea_off, pdata);
Simon Glass21d1fe72015-11-29 13:18:03 -07001612
1613 /* read BAR address */
Suneel Garapati51eeae92019-10-19 16:34:16 -07001614 dm_pci_read_config32(udev, bar, &bar_response);
Simon Glass21d1fe72015-11-29 13:18:03 -07001615 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1616
1617 /*
1618 * Pass "0" as the length argument to pci_bus_to_virt. The arg
Suneel Garapatib3699a12019-10-19 16:44:35 -07001619 * isn't actually used on any platform because U-Boot assumes a static
Simon Glass21d1fe72015-11-29 13:18:03 -07001620 * linear mapping. In the future, this could read the BAR size
1621 * and pass that as the size if needed.
1622 */
Suneel Garapati51eeae92019-10-19 16:34:16 -07001623 return dm_pci_bus_to_virt(udev, pci_bus_addr, flags, 0, MAP_NOCACHE);
Simon Glass21d1fe72015-11-29 13:18:03 -07001624}
1625
Bin Menga8c5f8d2018-10-15 02:21:21 -07001626static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001627{
Bin Mengdac01fd2018-08-03 01:14:52 -07001628 int ttl = PCI_FIND_CAP_TTL;
1629 u8 id;
1630 u16 ent;
Bin Mengdac01fd2018-08-03 01:14:52 -07001631
1632 dm_pci_read_config8(dev, pos, &pos);
Bin Menga8c5f8d2018-10-15 02:21:21 -07001633
Bin Mengdac01fd2018-08-03 01:14:52 -07001634 while (ttl--) {
1635 if (pos < PCI_STD_HEADER_SIZEOF)
1636 break;
1637 pos &= ~3;
1638 dm_pci_read_config16(dev, pos, &ent);
1639
1640 id = ent & 0xff;
1641 if (id == 0xff)
1642 break;
1643 if (id == cap)
1644 return pos;
1645 pos = (ent >> 8);
1646 }
1647
1648 return 0;
1649}
1650
Bin Menga8c5f8d2018-10-15 02:21:21 -07001651int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1652{
1653 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1654 cap);
1655}
1656
1657int dm_pci_find_capability(struct udevice *dev, int cap)
1658{
1659 u16 status;
1660 u8 header_type;
1661 u8 pos;
1662
1663 dm_pci_read_config16(dev, PCI_STATUS, &status);
1664 if (!(status & PCI_STATUS_CAP_LIST))
1665 return 0;
1666
1667 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1668 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1669 pos = PCI_CB_CAPABILITY_LIST;
1670 else
1671 pos = PCI_CAPABILITY_LIST;
1672
1673 return _dm_pci_find_next_capability(dev, pos, cap);
1674}
1675
1676int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001677{
1678 u32 header;
1679 int ttl;
1680 int pos = PCI_CFG_SPACE_SIZE;
1681
1682 /* minimum 8 bytes per capability */
1683 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1684
Bin Menga8c5f8d2018-10-15 02:21:21 -07001685 if (start)
1686 pos = start;
1687
Bin Mengdac01fd2018-08-03 01:14:52 -07001688 dm_pci_read_config32(dev, pos, &header);
1689 /*
1690 * If we have no capabilities, this is indicated by cap ID,
1691 * cap version and next pointer all being 0.
1692 */
1693 if (header == 0)
1694 return 0;
1695
1696 while (ttl--) {
1697 if (PCI_EXT_CAP_ID(header) == cap)
1698 return pos;
1699
1700 pos = PCI_EXT_CAP_NEXT(header);
1701 if (pos < PCI_CFG_SPACE_SIZE)
1702 break;
1703
1704 dm_pci_read_config32(dev, pos, &header);
1705 }
1706
1707 return 0;
1708}
1709
Bin Menga8c5f8d2018-10-15 02:21:21 -07001710int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1711{
1712 return dm_pci_find_next_ext_capability(dev, 0, cap);
1713}
1714
Alex Margineanb8e1f822019-06-07 11:24:25 +03001715int dm_pci_flr(struct udevice *dev)
1716{
1717 int pcie_off;
1718 u32 cap;
1719
1720 /* look for PCI Express Capability */
1721 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1722 if (!pcie_off)
1723 return -ENOENT;
1724
1725 /* check FLR capability */
1726 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1727 if (!(cap & PCI_EXP_DEVCAP_FLR))
1728 return -ENOENT;
1729
1730 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1731 PCI_EXP_DEVCTL_BCR_FLR);
1732
1733 /* wait 100ms, per PCI spec */
1734 mdelay(100);
1735
1736 return 0;
1737}
1738
Suneel Garapatib8852dc2019-10-19 16:07:20 -07001739#if defined(CONFIG_PCI_SRIOV)
1740int pci_sriov_init(struct udevice *pdev, int vf_en)
1741{
1742 u16 vendor, device;
1743 struct udevice *bus;
1744 struct udevice *dev;
1745 pci_dev_t bdf;
1746 u16 ctrl;
1747 u16 num_vfs;
1748 u16 total_vf;
1749 u16 vf_offset;
1750 u16 vf_stride;
1751 int vf, ret;
1752 int pos;
1753
1754 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1755 if (!pos) {
1756 debug("Error: SRIOV capability not found\n");
1757 return -ENOENT;
1758 }
1759
1760 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1761
1762 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1763 if (vf_en > total_vf)
1764 vf_en = total_vf;
1765 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1766
1767 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1768 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1769
1770 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1771 if (num_vfs > vf_en)
1772 num_vfs = vf_en;
1773
1774 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1775 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1776
1777 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1778 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1779
1780 bdf = dm_pci_get_bdf(pdev);
1781
1782 pci_get_bus(PCI_BUS(bdf), &bus);
1783
1784 if (!bus)
1785 return -ENODEV;
1786
1787 bdf += PCI_BDF(0, 0, vf_offset);
1788
1789 for (vf = 0; vf < num_vfs; vf++) {
Simon Glass8a8d24b2020-12-03 16:55:23 -07001790 struct pci_child_plat *pplat;
Suneel Garapatib8852dc2019-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 Glass8b85dfc2020-12-16 21:20:07 -07001797 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Suneel Garapatib8852dc2019-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 Glasscaa4daa2020-12-03 16:55:18 -07001818 pplat = dev_get_parent_plat(dev);
Suneel Garapatib8852dc2019-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 Glass8b85dfc2020-12-16 21:20:07 -07001828 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
Suneel Garapatib8852dc2019-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 Glassff3e0772015-03-05 12:25:25 -07001853UCLASS_DRIVER(pci) = {
1854 .id = UCLASS_PCI,
1855 .name = "pci",
Simon Glass42f36632020-12-16 21:20:18 -07001856 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
Simon Glass91195482016-07-05 17:10:10 -06001857 .post_bind = dm_scan_fdt_dev,
Simon Glassff3e0772015-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 Glass41575d82020-12-03 16:55:17 -07001861 .per_device_auto = sizeof(struct pci_controller),
Simon Glass8a8d24b2020-12-03 16:55:23 -07001862 .per_child_plat_auto = sizeof(struct pci_child_plat),
Simon Glassff3e0772015-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 Warrene578b922016-01-26 11:10:11 -07001897
Ovidiu Panaitb9f6d0f2020-11-28 10:43:12 +02001898int pci_init(void)
Stephen Warrene578b922016-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Ășn60ee6092019-05-21 12:04:31 +02001906 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warrene578b922016-01-26 11:10:11 -07001907 bus;
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001908 uclass_next_device_check(&bus)) {
Stephen Warrene578b922016-01-26 11:10:11 -07001909 ;
1910 }
Ovidiu Panaitb9f6d0f2020-11-28 10:43:12 +02001911
1912 return 0;
Stephen Warrene578b922016-01-26 11:10:11 -07001913}