blob: 5da3515f5f2561977c4c646cab4aefe30367a647 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb94dc892015-03-05 12:25:25 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassb94dc892015-03-05 12:25:25 -07005 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_PCI
8
Simon Glassb94dc892015-03-05 12:25:25 -07009#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glassb94dc892015-03-05 12:25:25 -070015#include <pci.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glassc5f053b2015-11-29 13:18:03 -070017#include <asm/io.h>
Simon Glassb94dc892015-03-05 12:25:25 -070018#include <dm/device-internal.h>
Simon Glass89d83232017-05-18 20:09:51 -060019#include <dm/lists.h>
Simon Glassbe706102020-12-16 21:20:18 -070020#include <dm/uclass-internal.h>
Bin Mengc0820a42015-08-20 06:40:23 -070021#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
Simon Glassef8a2dd2019-08-24 14:19:05 -060022#include <asm/fsp/fsp_support.h>
Bin Mengc0820a42015-08-20 06:40:23 -070023#endif
Simon Glass8807a562021-06-27 17:50:57 -060024#include <dt-bindings/pci/pci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060025#include <linux/delay.h>
Simon Glass37a3f94b2015-11-29 13:17:49 -070026#include "pci_internal.h"
Simon Glassb94dc892015-03-05 12:25:25 -070027
28DECLARE_GLOBAL_DATA_PTR;
29
Simon Glass2e4e4432016-01-18 20:19:14 -070030int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass7d07e592015-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 Glassc7298e72016-02-11 13:23:26 -070038 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass7d07e592015-08-31 18:55:35 -060039 if (ret)
40 return ret;
Simon Glass7d07e592015-08-31 18:55:35 -060041 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
42 }
43
44 return ret;
45}
46
Simon Glass6256d672015-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 Glassc92aac12020-01-27 08:49:38 -070055pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glassc9118d42015-07-06 16:47:46 -060056{
Simon Glassb75b15b2020-12-03 16:55:23 -070057 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassc9118d42015-07-06 16:47:46 -060058 struct udevice *bus = dev->parent;
59
Simon Glass1c6449c2019-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 Glassaad29ae2020-12-03 16:55:21 -070066 * of_to_plat() method of @dev. Accessing the PCI bus in that
Simon Glass1c6449c2019-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 Glass75e534b2020-12-16 21:20:07 -070073 return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
Simon Glassc9118d42015-07-06 16:47:46 -060074}
75
Simon Glassb94dc892015-03-05 12:25:25 -070076/**
77 * pci_get_bus_max() - returns the bus number of the last active bus
78 *
79 * @return last bus number, or -1 if no active buses
80 */
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 Glass75e534b2020-12-16 21:20:07 -070089 if (dev_seq(bus) > ret)
90 ret = dev_seq(bus);
Simon Glassb94dc892015-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 Meng5bc3f8a2015-10-01 00:36:01 -0700100 return pci_get_bus_max();
Simon Glassb94dc892015-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 Vasutb4535792018-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 Glass2a311e82020-01-27 08:49:37 -0700136int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassb94dc892015-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 Glassb75b15b2020-12-03 16:55:23 -0700144 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700145
Simon Glass71fa5b42020-12-03 16:55:18 -0700146 pplat = dev_get_parent_plat(dev);
Simon Glassb94dc892015-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 Glass84283d52015-11-29 13:17:48 -0700156int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassb94dc892015-03-05 12:25:25 -0700157{
158 struct udevice *bus;
159 int ret;
160
Simon Glass7d07e592015-08-31 18:55:35 -0600161 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-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 Glass3f7dc6e2021-06-27 17:50:56 -0600168 const struct pci_device_id *ids)
Simon Glassb94dc892015-03-05 12:25:25 -0700169{
Simon Glassb75b15b2020-12-03 16:55:23 -0700170 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700171 int i;
172
Simon Glass71fa5b42020-12-03 16:55:18 -0700173 pplat = dev_get_parent_plat(dev);
Simon Glassb94dc892015-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 Glass3f7dc6e2021-06-27 17:50:56 -0600185int pci_bus_find_devices(struct udevice *bus, const struct pci_device_id *ids,
Simon Glassb94dc892015-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 Glass3f7dc6e2021-06-27 17:50:56 -0600205int pci_find_device_id(const struct pci_device_id *ids, int index,
Simon Glassb94dc892015-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 Glass70e0c582015-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 Glassb75b15b2020-12-03 16:55:23 -0700226 struct pci_child_plat *pplat;
Simon Glass70e0c582015-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 Glass71fa5b42020-12-03 16:55:18 -0700232 pplat = dev_get_parent_plat(dev);
Simon Glass70e0c582015-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 Glassb639d512015-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 Glassb75b15b2020-12-03 16:55:23 -0700269 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassb639d512015-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 Glassb94dc892015-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 Glass9cec2df2016-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 Oltean278a5b52021-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 Glassb94dc892015-03-05 12:25:25 -0700309{
310 struct udevice *bus;
311 int ret;
312
Simon Glass7d07e592015-08-31 18:55:35 -0600313 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700314 if (ret)
315 return ret;
316
Bin Meng0a721522015-07-19 00:20:04 +0800317 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700318}
319
Simon Glass94ef2422015-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 Meng05bedb12015-09-11 03:24:34 -0700325 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600326 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700327 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
328 size);
Simon Glass94ef2422015-08-10 07:05:03 -0600329}
330
Simon Glassb94dc892015-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 Glass94ef2422015-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 Glassc92aac12020-01-27 08:49:38 -0700361int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassb94dc892015-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 Oltean69c5f8a2021-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 Glassb94dc892015-03-05 12:25:25 -0700374{
375 struct udevice *bus;
376 int ret;
377
Simon Glass7d07e592015-08-31 18:55:35 -0600378 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700379 if (ret)
380 return ret;
381
Bin Meng0a721522015-07-19 00:20:04 +0800382 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700383}
384
Simon Glassc92aac12020-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 Glass94ef2422015-08-10 07:05:03 -0600387{
Simon Glassc92aac12020-01-27 08:49:38 -0700388 const struct udevice *bus;
Simon Glass94ef2422015-08-10 07:05:03 -0600389
Bin Meng05bedb12015-09-11 03:24:34 -0700390 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600391 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700392 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass94ef2422015-08-10 07:05:03 -0600393 size);
394}
395
Simon Glassb94dc892015-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 Glassc92aac12020-01-27 08:49:38 -0700435int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass94ef2422015-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 Glassc92aac12020-01-27 08:49:38 -0700448int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass94ef2422015-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 Glassc92aac12020-01-27 08:49:38 -0700461int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass94ef2422015-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 Glass9cec2df2016-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 Menga0705782015-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 Glass75e534b2020-12-16 21:20:07 -0700521 while (dev_seq(parent) != 0) {
Bin Menga0705782015-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 Glassb94dc892015-03-05 12:25:25 -0700529int pci_auto_config_devices(struct udevice *bus)
530{
Simon Glass95588622020-12-22 19:30:28 -0700531 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700532 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700533 unsigned int sub_bus;
534 struct udevice *dev;
535 int ret;
536
Simon Glass75e534b2020-12-16 21:20:07 -0700537 sub_bus = dev_seq(bus);
Simon Glassb94dc892015-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 Glassb94dc892015-03-05 12:25:25 -0700543 unsigned int max_bus;
Simon Glassb072d522015-09-08 17:52:47 -0600544 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700545
Simon Glassb94dc892015-03-05 12:25:25 -0700546 debug("%s: device %s\n", __func__, dev->name);
Simon Glassf1d50f72020-12-19 10:40:13 -0700547 if (dev_has_ofnode(dev) &&
Suneel Garapatif8c86282020-05-04 21:25:25 -0700548 dev_read_bool(dev, "pci,no-autoconfig"))
Simon Glassf3005fb2020-04-08 16:57:26 -0600549 continue;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700550 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600551 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -0700552 return log_msg_ret("auto", ret);
Simon Glassb072d522015-09-08 17:52:47 -0600553 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700554 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700555
Masami Hiramatsu7ccdc672021-06-04 18:43:34 +0900556 if (dev_get_parent(dev) == bus)
557 continue;
558
Simon Glass71fa5b42020-12-03 16:55:18 -0700559 pplat = dev_get_parent_plat(dev);
Bin Menga0705782015-10-01 00:36:02 -0700560 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
561 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700562 }
563 debug("%s: done\n", __func__);
564
Simon Glassbe706102020-12-16 21:20:18 -0700565 return log_msg_ret("sub", sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700566}
567
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300568int pci_generic_mmap_write_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700569 const struct udevice *bus,
570 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
571 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300572 pci_dev_t bdf,
573 uint offset,
574 ulong value,
575 enum pci_size_t size)
576{
577 void *address;
578
579 if (addr_f(bus, bdf, offset, &address) < 0)
580 return 0;
581
582 switch (size) {
583 case PCI_SIZE_8:
584 writeb(value, address);
585 return 0;
586 case PCI_SIZE_16:
587 writew(value, address);
588 return 0;
589 case PCI_SIZE_32:
590 writel(value, address);
591 return 0;
592 default:
593 return -EINVAL;
594 }
595}
596
597int pci_generic_mmap_read_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700598 const struct udevice *bus,
599 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
600 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300601 pci_dev_t bdf,
602 uint offset,
603 ulong *valuep,
604 enum pci_size_t size)
605{
606 void *address;
607
608 if (addr_f(bus, bdf, offset, &address) < 0) {
609 *valuep = pci_get_ff(size);
610 return 0;
611 }
612
613 switch (size) {
614 case PCI_SIZE_8:
615 *valuep = readb(address);
616 return 0;
617 case PCI_SIZE_16:
618 *valuep = readw(address);
619 return 0;
620 case PCI_SIZE_32:
621 *valuep = readl(address);
622 return 0;
623 default:
624 return -EINVAL;
625 }
626}
627
Simon Glass37a3f94b2015-11-29 13:17:49 -0700628int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700629{
Pali RohĂĄr4fb65992021-10-07 14:50:58 +0200630 u8 header_type;
Simon Glassb94dc892015-03-05 12:25:25 -0700631 int sub_bus;
632 int ret;
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700633 int ea_pos;
634 u8 reg;
Simon Glassb94dc892015-03-05 12:25:25 -0700635
636 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700637
Pali RohĂĄr4fb65992021-10-07 14:50:58 +0200638 dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);
639 header_type &= 0x7f;
640 if (header_type != PCI_HEADER_TYPE_BRIDGE) {
641 debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n",
642 __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type);
643 return log_msg_ret("probe", -EINVAL);
644 }
645
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700646 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
647 if (ea_pos) {
648 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
649 &reg);
650 sub_bus = reg;
651 } else {
652 sub_bus = pci_get_bus_max() + 1;
653 }
Simon Glassb94dc892015-03-05 12:25:25 -0700654 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700655 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700656
657 ret = device_probe(bus);
658 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600659 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700660 ret);
Simon Glassbe706102020-12-16 21:20:18 -0700661 return log_msg_ret("probe", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700662 }
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700663
Masami Hiramatsuff022452021-04-16 14:53:46 -0700664 if (!ea_pos)
665 sub_bus = pci_get_bus_max();
666
Simon Glass37a3f94b2015-11-29 13:17:49 -0700667 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700668
669 return sub_bus;
670}
671
Simon Glass318d71c2015-07-06 16:47:44 -0600672/**
673 * pci_match_one_device - Tell if a PCI device structure has a matching
674 * PCI device id structure
675 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800676 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600677 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800678 * Returns true if the finding pci_device_id structure matched or false if
679 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600680 */
681static bool pci_match_one_id(const struct pci_device_id *id,
682 const struct pci_device_id *find)
683{
684 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
685 (id->device == PCI_ANY_ID || id->device == find->device) &&
686 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
687 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
688 !((id->class ^ find->class) & id->class_mask))
689 return true;
690
691 return false;
692}
693
694/**
Simon Glass8807a562021-06-27 17:50:57 -0600695 * pci_need_device_pre_reloc() - Check if a device should be bound
696 *
697 * This checks a list of vendor/device-ID values indicating devices that should
698 * be bound before relocation.
699 *
700 * @bus: Bus to check
701 * @vendor: Vendor ID to check
702 * @device: Device ID to check
703 * @return true if the vendor/device is in the list, false if not
704 */
705static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor,
706 uint device)
707{
708 u32 vendev;
709 int index;
710
711 for (index = 0;
712 !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index,
713 &vendev);
714 index++) {
715 if (vendev == PCI_VENDEV(vendor, device))
716 return true;
717 }
718
719 return false;
720}
721
722/**
Simon Glass318d71c2015-07-06 16:47:44 -0600723 * pci_find_and_bind_driver() - Find and bind the right PCI driver
724 *
725 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600726 *
727 * @parent: Parent bus
728 * @find_id: Specification of the driver to find
729 * @bdf: Bus/device/function addreess - see PCI_BDF()
730 * @devp: Returns a pointer to the device created
731 * @return 0 if OK, -EPERM if the device is not needed before relocation and
732 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600733 */
734static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600735 struct pci_device_id *find_id,
736 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600737{
738 struct pci_driver_entry *start, *entry;
Marek Vasutb4535792018-10-10 21:27:06 +0200739 ofnode node = ofnode_null();
Simon Glass318d71c2015-07-06 16:47:44 -0600740 const char *drv;
741 int n_ents;
742 int ret;
743 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700744 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600745
746 *devp = NULL;
747
748 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
749 find_id->vendor, find_id->device);
Marek Vasutb4535792018-10-10 21:27:06 +0200750
751 /* Determine optional OF node */
Suneel Garapaticb7093d2019-10-19 16:02:48 -0700752 if (ofnode_valid(dev_ofnode(parent)))
753 pci_dev_find_ofnode(parent, bdf, &node);
Marek Vasutb4535792018-10-10 21:27:06 +0200754
Michael Walle2e21f372019-12-01 17:45:18 +0100755 if (ofnode_valid(node) && !ofnode_is_available(node)) {
756 debug("%s: Ignoring disabled device\n", __func__);
Simon Glassbe706102020-12-16 21:20:18 -0700757 return log_msg_ret("dis", -EPERM);
Michael Walle2e21f372019-12-01 17:45:18 +0100758 }
759
Simon Glass318d71c2015-07-06 16:47:44 -0600760 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
761 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
762 for (entry = start; entry != start + n_ents; entry++) {
763 const struct pci_device_id *id;
764 struct udevice *dev;
765 const struct driver *drv;
766
767 for (id = entry->match;
768 id->vendor || id->subvendor || id->class_mask;
769 id++) {
770 if (!pci_match_one_id(id, find_id))
771 continue;
772
773 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700774
775 /*
776 * In the pre-relocation phase, we only bind devices
777 * whose driver has the DM_FLAG_PRE_RELOC set, to save
778 * precious memory space as on some platforms as that
779 * space is pretty limited (ie: using Cache As RAM).
780 */
781 if (!(gd->flags & GD_FLG_RELOC) &&
782 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassbe706102020-12-16 21:20:18 -0700783 return log_msg_ret("pre", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700784
Simon Glass318d71c2015-07-06 16:47:44 -0600785 /*
786 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700787 * plat (instead of NULL) and allow its bind()
Simon Glass318d71c2015-07-06 16:47:44 -0600788 * method to return -ENOENT if it doesn't support this
789 * device. That way we could continue the search to
790 * find another driver. For now this doesn't seem
791 * necesssary, so just bind the first match.
792 */
Simon Glass884870f2020-11-28 17:50:01 -0700793 ret = device_bind(parent, drv, drv->name, NULL, node,
794 &dev);
Simon Glass318d71c2015-07-06 16:47:44 -0600795 if (ret)
796 goto error;
797 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700798 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600799 *devp = dev;
800 return 0;
801 }
802 }
803
Bin Meng984c0dc2015-08-20 06:40:17 -0700804 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
805 /*
806 * In the pre-relocation phase, we only bind bridge devices to save
807 * precious memory space as on some platforms as that space is pretty
808 * limited (ie: using Cache As RAM).
809 */
Simon Glass8807a562021-06-27 17:50:57 -0600810 if (!(gd->flags & GD_FLG_RELOC) && !bridge &&
811 !pci_need_device_pre_reloc(parent, find_id->vendor,
812 find_id->device))
Simon Glassbe706102020-12-16 21:20:18 -0700813 return log_msg_ret("notbr", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700814
Simon Glass318d71c2015-07-06 16:47:44 -0600815 /* Bind a generic driver so that the device can be used */
Simon Glass75e534b2020-12-16 21:20:07 -0700816 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
Bin Meng0a721522015-07-19 00:20:04 +0800817 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600818 str = strdup(name);
819 if (!str)
820 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700821 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
822
Marek Vasutb4535792018-10-10 21:27:06 +0200823 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glass318d71c2015-07-06 16:47:44 -0600824 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600825 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200826 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600827 return ret;
828 }
829 debug("%s: No match found: bound generic driver instead\n", __func__);
830
831 return 0;
832
833error:
834 debug("%s: No match found: error %d\n", __func__, ret);
835 return ret;
836}
837
Tim Harvey4c57bf72021-04-16 14:53:47 -0700838__weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
839{
840}
841
Simon Glassb94dc892015-03-05 12:25:25 -0700842int pci_bind_bus_devices(struct udevice *bus)
843{
844 ulong vendor, device;
845 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800846 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700847 bool found_multi;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700848 int ari_off;
Simon Glassb94dc892015-03-05 12:25:25 -0700849 int ret;
850
851 found_multi = false;
Simon Glass75e534b2020-12-16 21:20:07 -0700852 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
Bin Meng0a721522015-07-19 00:20:04 +0800853 PCI_MAX_PCI_FUNCTIONS - 1);
Simon Glass75e534b2020-12-16 21:20:07 -0700854 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800855 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700856 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700857 struct udevice *dev;
858 ulong class;
859
Bin Meng20bdc1e2018-08-03 01:14:37 -0700860 if (!PCI_FUNC(bdf))
861 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800862 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700863 continue;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800864
Simon Glassb94dc892015-03-05 12:25:25 -0700865 /* Check only the first access, we don't expect problems */
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800866 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
867 PCI_SIZE_16);
Pali RohĂĄra8a520d2021-09-07 18:07:08 +0200868 if (ret || vendor == 0xffff || vendor == 0x0000)
Simon Glassb94dc892015-03-05 12:25:25 -0700869 continue;
870
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800871 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
872 &header_type, PCI_SIZE_8);
873
Bin Meng0a721522015-07-19 00:20:04 +0800874 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700875 found_multi = header_type & 0x80;
876
Simon Glass25916d62019-09-25 08:56:12 -0600877 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -0700878 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Bin Meng0a721522015-07-19 00:20:04 +0800879 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700880 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800881 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600882 PCI_SIZE_32);
883 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700884
885 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800886 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass25916d62019-09-25 08:56:12 -0600887 debug(": find ret=%d\n", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700888
Simon Glass413ebdb2015-11-29 13:18:09 -0700889 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700890 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600891 struct pci_device_id find_id;
892 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700893
Simon Glass318d71c2015-07-06 16:47:44 -0600894 memset(&find_id, '\0', sizeof(find_id));
895 find_id.vendor = vendor;
896 find_id.device = device;
897 find_id.class = class;
898 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800899 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600900 PCI_SUBSYSTEM_VENDOR_ID,
901 &val, PCI_SIZE_32);
902 find_id.subvendor = val & 0xffff;
903 find_id.subdevice = val >> 16;
904 }
Bin Meng0a721522015-07-19 00:20:04 +0800905 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600906 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700907 }
Simon Glassc45abf12015-09-08 17:52:49 -0600908 if (ret == -EPERM)
909 continue;
910 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700911 return ret;
912
913 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -0700914 pplat = dev_get_parent_plat(dev);
Simon Glassc45abf12015-09-08 17:52:49 -0600915 pplat->devfn = PCI_MASK_BUS(bdf);
916 pplat->vendor = vendor;
917 pplat->device = device;
918 pplat->class = class;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700919
920 if (IS_ENABLED(CONFIG_PCI_ARID)) {
921 ari_off = dm_pci_find_ext_capability(dev,
922 PCI_EXT_CAP_ID_ARI);
923 if (ari_off) {
924 u16 ari_cap;
925
926 /*
927 * Read Next Function number in ARI Cap
928 * Register
929 */
930 dm_pci_read_config16(dev, ari_off + 4,
931 &ari_cap);
932 /*
933 * Update next scan on this function number,
934 * subtract 1 in BDF to satisfy loop increment.
935 */
936 if (ari_cap & 0xff00) {
937 bdf = PCI_BDF(PCI_BUS(bdf),
938 PCI_DEV(ari_cap),
939 PCI_FUNC(ari_cap));
940 bdf = bdf - 0x100;
941 }
942 }
943 }
Tim Harvey4c57bf72021-04-16 14:53:47 -0700944
945 board_pci_fixup_dev(bus, dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700946 }
947
948 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -0700949}
950
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700951static void decode_regions(struct pci_controller *hose, ofnode parent_node,
952 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700953{
954 int pci_addr_cells, addr_cells, size_cells;
955 int cells_per_record;
Stefan Roesebbc88462020-08-12 11:55:46 +0200956 struct bd_info *bd;
Simon Glassb94dc892015-03-05 12:25:25 -0700957 const u32 *prop;
Stefan Roese950864f2020-07-23 16:34:10 +0200958 int max_regions;
Simon Glassb94dc892015-03-05 12:25:25 -0700959 int len;
960 int i;
961
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900962 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700963 if (!prop) {
964 debug("%s: Cannot decode regions\n", __func__);
965 return;
966 }
967
Simon Glass4191dc12017-06-12 06:21:31 -0600968 pci_addr_cells = ofnode_read_simple_addr_cells(node);
969 addr_cells = ofnode_read_simple_addr_cells(parent_node);
970 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700971
972 /* PCI addresses are always 3-cells */
973 len /= sizeof(u32);
974 cells_per_record = pci_addr_cells + addr_cells + size_cells;
975 hose->region_count = 0;
976 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
977 cells_per_record);
Stefan Roese950864f2020-07-23 16:34:10 +0200978
979 /* Dynamically allocate the regions array */
980 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
981 hose->regions = (struct pci_region *)
982 calloc(1, max_regions * sizeof(struct pci_region));
983
984 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassb94dc892015-03-05 12:25:25 -0700985 u64 pci_addr, addr, size;
986 int space_code;
987 u32 flags;
988 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -0700989 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -0700990
991 if (len < cells_per_record)
992 break;
993 flags = fdt32_to_cpu(prop[0]);
994 space_code = (flags >> 24) & 3;
995 pci_addr = fdtdec_get_number(prop + 1, 2);
996 prop += pci_addr_cells;
997 addr = fdtdec_get_number(prop, addr_cells);
998 prop += addr_cells;
999 size = fdtdec_get_number(prop, size_cells);
1000 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001001 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
1002 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -07001003 if (space_code & 2) {
1004 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
1005 PCI_REGION_MEM;
1006 } else if (space_code & 1) {
1007 type = PCI_REGION_IO;
1008 } else {
1009 continue;
1010 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +03001011
1012 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
1013 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
1014 debug(" - beyond the 32-bit boundary, ignoring\n");
1015 continue;
1016 }
1017
Simon Glass7efc9ba2015-11-19 20:26:58 -07001018 pos = -1;
Suneel Garapati3ac3aec2019-10-19 17:10:20 -07001019 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
1020 for (i = 0; i < hose->region_count; i++) {
1021 if (hose->regions[i].flags == type)
1022 pos = i;
1023 }
Simon Glass7efc9ba2015-11-19 20:26:58 -07001024 }
Suneel Garapati3ac3aec2019-10-19 17:10:20 -07001025
Simon Glass7efc9ba2015-11-19 20:26:58 -07001026 if (pos == -1)
1027 pos = hose->region_count++;
1028 debug(" - type=%d, pos=%d\n", type, pos);
1029 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -07001030 }
1031
1032 /* Add a region for our local memory */
Stefan Roesebbc88462020-08-12 11:55:46 +02001033 bd = gd->bd;
Bin Mengae0bdde2018-03-27 00:46:05 -07001034 if (!bd)
Christian Gmeiner5f4e0942018-06-10 06:25:05 -07001035 return;
Bin Mengae0bdde2018-03-27 00:46:05 -07001036
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001037 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1038 if (bd->bi_dram[i].size) {
Daniel Schwierzeckf59925e2021-07-15 20:53:56 +02001039 phys_addr_t start = bd->bi_dram[i].start;
1040
1041 if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY))
1042 start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start);
1043
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001044 pci_set_region(hose->regions + hose->region_count++,
Daniel Schwierzeckf59925e2021-07-15 20:53:56 +02001045 start, start, bd->bi_dram[i].size,
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001046 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1047 }
1048 }
Simon Glassb94dc892015-03-05 12:25:25 -07001049
Christian Gmeiner5f4e0942018-06-10 06:25:05 -07001050 return;
Simon Glassb94dc892015-03-05 12:25:25 -07001051}
1052
1053static int pci_uclass_pre_probe(struct udevice *bus)
1054{
1055 struct pci_controller *hose;
Simon Glassbe706102020-12-16 21:20:18 -07001056 struct uclass *uc;
1057 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001058
Simon Glass75e534b2020-12-16 21:20:07 -07001059 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -07001060 bus->parent->name);
Simon Glass95588622020-12-22 19:30:28 -07001061 hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001062
Simon Glassbe706102020-12-16 21:20:18 -07001063 /*
1064 * Set the sequence number, if device_bind() doesn't. We want control
1065 * of this so that numbers are allocated as devices are probed. That
1066 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1067 * higher than their parents)
1068 */
1069 if (dev_seq(bus) == -1) {
1070 ret = uclass_get(UCLASS_PCI, &uc);
1071 if (ret)
1072 return ret;
Simon Glass5e349922020-12-19 10:40:09 -07001073 bus->seq_ = uclass_find_next_free_seq(uc);
Simon Glassbe706102020-12-16 21:20:18 -07001074 }
1075
Simon Glassb94dc892015-03-05 12:25:25 -07001076 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +01001077 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -07001078 hose->ctlr = bus;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -07001079 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001080 } else {
1081 struct pci_controller *parent_hose;
1082
1083 parent_hose = dev_get_uclass_priv(bus->parent);
1084 hose->ctlr = parent_hose->bus;
1085 }
Simon Glassbe706102020-12-16 21:20:18 -07001086
Simon Glassb94dc892015-03-05 12:25:25 -07001087 hose->bus = bus;
Simon Glass75e534b2020-12-16 21:20:07 -07001088 hose->first_busno = dev_seq(bus);
1089 hose->last_busno = dev_seq(bus);
Simon Glassf1d50f72020-12-19 10:40:13 -07001090 if (dev_has_ofnode(bus)) {
Suneel Garapatif8c86282020-05-04 21:25:25 -07001091 hose->skip_auto_config_until_reloc =
1092 dev_read_bool(bus,
1093 "u-boot,skip-auto-config-until-reloc");
1094 }
Simon Glassb94dc892015-03-05 12:25:25 -07001095
1096 return 0;
1097}
1098
1099static int pci_uclass_post_probe(struct udevice *bus)
1100{
Simon Glass68e35a72019-12-06 21:41:37 -07001101 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001102 int ret;
1103
Simon Glass75e534b2020-12-16 21:20:07 -07001104 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001105 ret = pci_bind_bus_devices(bus);
1106 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001107 return log_msg_ret("bind", ret);
Simon Glassb94dc892015-03-05 12:25:25 -07001108
Simon Glassbd165e72020-04-26 09:12:56 -06001109 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass68e35a72019-12-06 21:41:37 -07001110 (!hose->skip_auto_config_until_reloc ||
1111 (gd->flags & GD_FLG_RELOC))) {
1112 ret = pci_auto_config_devices(bus);
1113 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -07001114 return log_msg_ret("cfg", ret);
Simon Glass68e35a72019-12-06 21:41:37 -07001115 }
Simon Glassb94dc892015-03-05 12:25:25 -07001116
Bin Mengc0820a42015-08-20 06:40:23 -07001117#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1118 /*
1119 * Per Intel FSP specification, we should call FSP notify API to
1120 * inform FSP that PCI enumeration has been done so that FSP will
1121 * do any necessary initialization as required by the chipset's
1122 * BIOS Writer's Guide (BWG).
1123 *
1124 * Unfortunately we have to put this call here as with driver model,
1125 * the enumeration is all done on a lazy basis as needed, so until
1126 * something is touched on PCI it won't happen.
1127 *
1128 * Note we only call this 1) after U-Boot is relocated, and 2)
1129 * root bus has finished probing.
1130 */
Simon Glass75e534b2020-12-16 21:20:07 -07001131 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
Bin Mengc0820a42015-08-20 06:40:23 -07001132 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -06001133 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001134 return log_msg_ret("fsp", ret);
Simon Glassb072d522015-09-08 17:52:47 -06001135 }
Bin Mengc0820a42015-08-20 06:40:23 -07001136#endif
1137
Simon Glassb072d522015-09-08 17:52:47 -06001138 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001139}
1140
1141static int pci_uclass_child_post_bind(struct udevice *dev)
1142{
Simon Glassb75b15b2020-12-03 16:55:23 -07001143 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -07001144
Simon Glassf1d50f72020-12-19 10:40:13 -07001145 if (!dev_has_ofnode(dev))
Simon Glassb94dc892015-03-05 12:25:25 -07001146 return 0;
1147
Simon Glass71fa5b42020-12-03 16:55:18 -07001148 pplat = dev_get_parent_plat(dev);
Bin Meng00d808e2018-08-03 01:14:36 -07001149
1150 /* Extract vendor id and device id if available */
1151 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1152
1153 /* Extract the devfn from fdt_pci_addr */
Stefan Roesea74eb552019-01-25 11:52:42 +01001154 pplat->devfn = pci_get_devfn(dev);
Simon Glassb94dc892015-03-05 12:25:25 -07001155
1156 return 0;
1157}
1158
Simon Glass2a311e82020-01-27 08:49:37 -07001159static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng0a721522015-07-19 00:20:04 +08001160 uint offset, ulong *valuep,
1161 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001162{
Simon Glass95588622020-12-22 19:30:28 -07001163 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001164
1165 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1166}
1167
Bin Meng0a721522015-07-19 00:20:04 +08001168static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1169 uint offset, ulong value,
1170 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001171{
Simon Glass95588622020-12-22 19:30:28 -07001172 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001173
1174 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1175}
1176
Simon Glass04c8b6a2015-08-10 07:05:04 -06001177static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1178{
1179 struct udevice *dev;
1180 int ret = 0;
1181
1182 /*
1183 * Scan through all the PCI controllers. On x86 there will only be one
1184 * but that is not necessarily true on other hardware.
1185 */
1186 do {
1187 device_find_first_child(bus, &dev);
1188 if (dev) {
1189 *devp = dev;
1190 return 0;
1191 }
1192 ret = uclass_next_device(&bus);
1193 if (ret)
1194 return ret;
1195 } while (bus);
1196
1197 return 0;
1198}
1199
1200int pci_find_next_device(struct udevice **devp)
1201{
1202 struct udevice *child = *devp;
1203 struct udevice *bus = child->parent;
1204 int ret;
1205
1206 /* First try all the siblings */
1207 *devp = NULL;
1208 while (child) {
1209 device_find_next_child(&child);
1210 if (child) {
1211 *devp = child;
1212 return 0;
1213 }
1214 }
1215
1216 /* We ran out of siblings. Try the next bus */
1217 ret = uclass_next_device(&bus);
1218 if (ret)
1219 return ret;
1220
1221 return bus ? skip_to_next_device(bus, devp) : 0;
1222}
1223
1224int pci_find_first_device(struct udevice **devp)
1225{
1226 struct udevice *bus;
1227 int ret;
1228
1229 *devp = NULL;
1230 ret = uclass_first_device(UCLASS_PCI, &bus);
1231 if (ret)
1232 return ret;
1233
1234 return skip_to_next_device(bus, devp);
1235}
1236
Simon Glass27a733f2015-11-19 20:26:59 -07001237ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1238{
1239 switch (size) {
1240 case PCI_SIZE_8:
1241 return (value >> ((offset & 3) * 8)) & 0xff;
1242 case PCI_SIZE_16:
1243 return (value >> ((offset & 2) * 8)) & 0xffff;
1244 default:
1245 return value;
1246 }
1247}
1248
1249ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1250 enum pci_size_t size)
1251{
1252 uint off_mask;
1253 uint val_mask, shift;
1254 ulong ldata, mask;
1255
1256 switch (size) {
1257 case PCI_SIZE_8:
1258 off_mask = 3;
1259 val_mask = 0xff;
1260 break;
1261 case PCI_SIZE_16:
1262 off_mask = 2;
1263 val_mask = 0xffff;
1264 break;
1265 default:
1266 return value;
1267 }
1268 shift = (offset & off_mask) * 8;
1269 ldata = (value & val_mask) << shift;
1270 mask = val_mask << shift;
1271 value = (old & ~mask) | ldata;
1272
1273 return value;
1274}
1275
Rayagonda Kokatanurcdc7ed32020-05-12 13:29:49 +05301276int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1277{
1278 int pci_addr_cells, addr_cells, size_cells;
1279 int cells_per_record;
1280 const u32 *prop;
1281 int len;
1282 int i = 0;
1283
1284 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1285 if (!prop) {
1286 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1287 dev->name);
1288 return -EINVAL;
1289 }
1290
1291 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1292 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1293 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1294
1295 /* PCI addresses are always 3-cells */
1296 len /= sizeof(u32);
1297 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1298 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1299 cells_per_record);
1300
1301 while (len) {
1302 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1303 prop += pci_addr_cells;
1304 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1305 prop += addr_cells;
1306 memp->size = fdtdec_get_number(prop, size_cells);
1307 prop += size_cells;
1308
1309 if (i == index)
1310 return 0;
1311 i++;
1312 len -= cells_per_record;
1313 }
1314
1315 return -EINVAL;
1316}
1317
Simon Glassdcdc0122015-11-19 20:27:01 -07001318int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1319 struct pci_region **memp, struct pci_region **prefp)
1320{
1321 struct udevice *bus = pci_get_controller(dev);
1322 struct pci_controller *hose = dev_get_uclass_priv(bus);
1323 int i;
1324
1325 *iop = NULL;
1326 *memp = NULL;
1327 *prefp = NULL;
1328 for (i = 0; i < hose->region_count; i++) {
1329 switch (hose->regions[i].flags) {
1330 case PCI_REGION_IO:
1331 if (!*iop || (*iop)->size < hose->regions[i].size)
1332 *iop = hose->regions + i;
1333 break;
1334 case PCI_REGION_MEM:
1335 if (!*memp || (*memp)->size < hose->regions[i].size)
1336 *memp = hose->regions + i;
1337 break;
1338 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1339 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1340 *prefp = hose->regions + i;
1341 break;
1342 }
1343 }
1344
1345 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1346}
1347
Simon Glassc92aac12020-01-27 08:49:38 -07001348u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glass3452cb12015-11-29 13:17:53 -07001349{
1350 u32 addr;
1351 int bar;
1352
1353 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1354 dm_pci_read_config32(dev, bar, &addr);
Simon Glass71fafd12020-04-09 10:27:36 -06001355
1356 /*
1357 * If we get an invalid address, return this so that comparisons with
1358 * FDT_ADDR_T_NONE work correctly
1359 */
1360 if (addr == 0xffffffff)
1361 return addr;
1362 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glass3452cb12015-11-29 13:17:53 -07001363 return addr & PCI_BASE_ADDRESS_IO_MASK;
1364 else
1365 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1366}
1367
Simon Glasse2b6b562016-01-18 20:19:15 -07001368void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1369{
1370 int bar;
1371
1372 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1373 dm_pci_write_config32(dev, bar, addr);
1374}
1375
Simon Glassc5f053b2015-11-29 13:18:03 -07001376static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1377 pci_addr_t bus_addr, unsigned long flags,
1378 unsigned long skip_mask, phys_addr_t *pa)
1379{
1380 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1381 struct pci_region *res;
1382 int i;
1383
Christian Gmeiner7241f802018-06-10 06:25:06 -07001384 if (hose->region_count == 0) {
1385 *pa = bus_addr;
1386 return 0;
1387 }
1388
Simon Glassc5f053b2015-11-29 13:18:03 -07001389 for (i = 0; i < hose->region_count; i++) {
1390 res = &hose->regions[i];
1391
1392 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1393 continue;
1394
1395 if (res->flags & skip_mask)
1396 continue;
1397
1398 if (bus_addr >= res->bus_start &&
1399 (bus_addr - res->bus_start) < res->size) {
1400 *pa = (bus_addr - res->bus_start + res->phys_start);
1401 return 0;
1402 }
1403 }
1404
1405 return 1;
1406}
1407
1408phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1409 unsigned long flags)
1410{
1411 phys_addr_t phys_addr = 0;
1412 struct udevice *ctlr;
1413 int ret;
1414
1415 /* The root controller has the region information */
1416 ctlr = pci_get_controller(dev);
1417
1418 /*
1419 * if PCI_REGION_MEM is set we do a two pass search with preference
1420 * on matches that don't have PCI_REGION_SYS_MEMORY set
1421 */
1422 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1423 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1424 flags, PCI_REGION_SYS_MEMORY,
1425 &phys_addr);
1426 if (!ret)
1427 return phys_addr;
1428 }
1429
1430 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1431
1432 if (ret)
1433 puts("pci_hose_bus_to_phys: invalid physical address\n");
1434
1435 return phys_addr;
1436}
1437
Vladimir Oltean86fa8b02021-09-17 15:11:27 +03001438static int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1439 unsigned long flags, unsigned long skip_mask,
1440 pci_addr_t *ba)
Simon Glassc5f053b2015-11-29 13:18:03 -07001441{
1442 struct pci_region *res;
1443 struct udevice *ctlr;
1444 pci_addr_t bus_addr;
1445 int i;
1446 struct pci_controller *hose;
1447
1448 /* The root controller has the region information */
1449 ctlr = pci_get_controller(dev);
1450 hose = dev_get_uclass_priv(ctlr);
1451
Christian Gmeiner7241f802018-06-10 06:25:06 -07001452 if (hose->region_count == 0) {
1453 *ba = phys_addr;
1454 return 0;
1455 }
1456
Simon Glassc5f053b2015-11-29 13:18:03 -07001457 for (i = 0; i < hose->region_count; i++) {
1458 res = &hose->regions[i];
1459
1460 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1461 continue;
1462
1463 if (res->flags & skip_mask)
1464 continue;
1465
1466 bus_addr = phys_addr - res->phys_start + res->bus_start;
1467
1468 if (bus_addr >= res->bus_start &&
1469 (bus_addr - res->bus_start) < res->size) {
1470 *ba = bus_addr;
1471 return 0;
1472 }
1473 }
1474
1475 return 1;
1476}
1477
1478pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1479 unsigned long flags)
1480{
1481 pci_addr_t bus_addr = 0;
1482 int ret;
1483
1484 /*
1485 * if PCI_REGION_MEM is set we do a two pass search with preference
1486 * on matches that don't have PCI_REGION_SYS_MEMORY set
1487 */
1488 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1489 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1490 PCI_REGION_SYS_MEMORY, &bus_addr);
1491 if (!ret)
1492 return bus_addr;
1493 }
1494
1495 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1496
1497 if (ret)
1498 puts("pci_hose_phys_to_bus: invalid physical address\n");
1499
1500 return bus_addr;
1501}
1502
Suneel Garapati5858ba82019-10-19 16:34:16 -07001503static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
Simon Glassb75b15b2020-12-03 16:55:23 -07001504 struct pci_child_plat *pdata)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001505{
1506 phys_addr_t addr = 0;
1507
1508 /*
1509 * In the case of a Virtual Function device using BAR
1510 * base and size, add offset for VFn BAR(1, 2, 3...n)
1511 */
1512 if (pdata->is_virtfn) {
1513 size_t sz;
1514 u32 ea_entry;
1515
1516 /* MaxOffset, 1st DW */
1517 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1518 sz = ea_entry & PCI_EA_FIELD_MASK;
1519 /* Fill up lower 2 bits */
1520 sz |= (~PCI_EA_FIELD_MASK);
1521
1522 if (ea_entry & PCI_EA_IS_64) {
1523 /* MaxOffset 2nd DW */
1524 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1525 sz |= ((u64)ea_entry) << 32;
1526 }
1527
1528 addr = (pdata->virtid - 1) * (sz + 1);
1529 }
1530
1531 return addr;
1532}
1533
Alex Marginean1c934a62019-06-07 11:24:23 +03001534static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
Simon Glassb75b15b2020-12-03 16:55:23 -07001535 int ea_off, struct pci_child_plat *pdata)
Alex Marginean1c934a62019-06-07 11:24:23 +03001536{
1537 int ea_cnt, i, entry_size;
1538 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1539 u32 ea_entry;
1540 phys_addr_t addr;
1541
Suneel Garapati5858ba82019-10-19 16:34:16 -07001542 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1543 /*
1544 * In the case of a Virtual Function device, device is
1545 * Physical function, so pdata will point to required VF
1546 * specific data.
1547 */
1548 if (pdata->is_virtfn)
1549 bar_id += PCI_EA_BEI_VF_BAR0;
1550 }
1551
Alex Marginean1c934a62019-06-07 11:24:23 +03001552 /* EA capability structure header */
1553 dm_pci_read_config32(dev, ea_off, &ea_entry);
1554 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1555 ea_off += PCI_EA_FIRST_ENT;
1556
1557 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1558 /* Entry header */
1559 dm_pci_read_config32(dev, ea_off, &ea_entry);
1560 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1561
1562 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1563 continue;
1564
1565 /* Base address, 1st DW */
1566 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1567 addr = ea_entry & PCI_EA_FIELD_MASK;
1568 if (ea_entry & PCI_EA_IS_64) {
1569 /* Base address, 2nd DW, skip over 4B MaxOffset */
1570 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1571 addr |= ((u64)ea_entry) << 32;
1572 }
1573
Suneel Garapati5858ba82019-10-19 16:34:16 -07001574 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1575 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1576
Alex Marginean1c934a62019-06-07 11:24:23 +03001577 /* size ignored for now */
Suneel Garapati47f19622019-10-19 16:44:35 -07001578 return map_physmem(addr, 0, flags);
Alex Marginean1c934a62019-06-07 11:24:23 +03001579 }
1580
1581 return 0;
1582}
1583
Simon Glassc5f053b2015-11-29 13:18:03 -07001584void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1585{
Simon Glassb75b15b2020-12-03 16:55:23 -07001586 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
Suneel Garapati5858ba82019-10-19 16:34:16 -07001587 struct udevice *udev = dev;
Simon Glassc5f053b2015-11-29 13:18:03 -07001588 pci_addr_t pci_bus_addr;
1589 u32 bar_response;
Alex Marginean1c934a62019-06-07 11:24:23 +03001590 int ea_off;
1591
Suneel Garapati5858ba82019-10-19 16:34:16 -07001592 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1593 /*
1594 * In case of Virtual Function devices, use PF udevice
1595 * as EA capability is defined in Physical Function
1596 */
1597 if (pdata->is_virtfn)
1598 udev = pdata->pfdev;
1599 }
1600
Alex Marginean1c934a62019-06-07 11:24:23 +03001601 /*
1602 * if the function supports Enhanced Allocation use that instead of
1603 * BARs
Suneel Garapati5858ba82019-10-19 16:34:16 -07001604 * Incase of virtual functions, pdata will help read VF BEI
1605 * and EA entry size.
Alex Marginean1c934a62019-06-07 11:24:23 +03001606 */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001607 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
Alex Marginean1c934a62019-06-07 11:24:23 +03001608 if (ea_off)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001609 return dm_pci_map_ea_bar(udev, bar, flags, ea_off, pdata);
Simon Glassc5f053b2015-11-29 13:18:03 -07001610
1611 /* read BAR address */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001612 dm_pci_read_config32(udev, bar, &bar_response);
Simon Glassc5f053b2015-11-29 13:18:03 -07001613 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1614
1615 /*
1616 * Pass "0" as the length argument to pci_bus_to_virt. The arg
Suneel Garapati47f19622019-10-19 16:44:35 -07001617 * isn't actually used on any platform because U-Boot assumes a static
Simon Glassc5f053b2015-11-29 13:18:03 -07001618 * linear mapping. In the future, this could read the BAR size
1619 * and pass that as the size if needed.
1620 */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001621 return dm_pci_bus_to_virt(udev, pci_bus_addr, flags, 0, MAP_NOCACHE);
Simon Glassc5f053b2015-11-29 13:18:03 -07001622}
1623
Bin Meng631f3482018-10-15 02:21:21 -07001624static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001625{
Bin Menga7366f02018-08-03 01:14:52 -07001626 int ttl = PCI_FIND_CAP_TTL;
1627 u8 id;
1628 u16 ent;
Bin Menga7366f02018-08-03 01:14:52 -07001629
1630 dm_pci_read_config8(dev, pos, &pos);
Bin Meng631f3482018-10-15 02:21:21 -07001631
Bin Menga7366f02018-08-03 01:14:52 -07001632 while (ttl--) {
1633 if (pos < PCI_STD_HEADER_SIZEOF)
1634 break;
1635 pos &= ~3;
1636 dm_pci_read_config16(dev, pos, &ent);
1637
1638 id = ent & 0xff;
1639 if (id == 0xff)
1640 break;
1641 if (id == cap)
1642 return pos;
1643 pos = (ent >> 8);
1644 }
1645
1646 return 0;
1647}
1648
Bin Meng631f3482018-10-15 02:21:21 -07001649int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1650{
1651 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1652 cap);
1653}
1654
1655int dm_pci_find_capability(struct udevice *dev, int cap)
1656{
1657 u16 status;
1658 u8 header_type;
1659 u8 pos;
1660
1661 dm_pci_read_config16(dev, PCI_STATUS, &status);
1662 if (!(status & PCI_STATUS_CAP_LIST))
1663 return 0;
1664
1665 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1666 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1667 pos = PCI_CB_CAPABILITY_LIST;
1668 else
1669 pos = PCI_CAPABILITY_LIST;
1670
1671 return _dm_pci_find_next_capability(dev, pos, cap);
1672}
1673
1674int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001675{
1676 u32 header;
1677 int ttl;
1678 int pos = PCI_CFG_SPACE_SIZE;
1679
1680 /* minimum 8 bytes per capability */
1681 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1682
Bin Meng631f3482018-10-15 02:21:21 -07001683 if (start)
1684 pos = start;
1685
Bin Menga7366f02018-08-03 01:14:52 -07001686 dm_pci_read_config32(dev, pos, &header);
1687 /*
1688 * If we have no capabilities, this is indicated by cap ID,
1689 * cap version and next pointer all being 0.
1690 */
1691 if (header == 0)
1692 return 0;
1693
1694 while (ttl--) {
1695 if (PCI_EXT_CAP_ID(header) == cap)
1696 return pos;
1697
1698 pos = PCI_EXT_CAP_NEXT(header);
1699 if (pos < PCI_CFG_SPACE_SIZE)
1700 break;
1701
1702 dm_pci_read_config32(dev, pos, &header);
1703 }
1704
1705 return 0;
1706}
1707
Bin Meng631f3482018-10-15 02:21:21 -07001708int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1709{
1710 return dm_pci_find_next_ext_capability(dev, 0, cap);
1711}
1712
Alex Marginean09467d32019-06-07 11:24:25 +03001713int dm_pci_flr(struct udevice *dev)
1714{
1715 int pcie_off;
1716 u32 cap;
1717
1718 /* look for PCI Express Capability */
1719 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1720 if (!pcie_off)
1721 return -ENOENT;
1722
1723 /* check FLR capability */
1724 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1725 if (!(cap & PCI_EXP_DEVCAP_FLR))
1726 return -ENOENT;
1727
1728 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1729 PCI_EXP_DEVCTL_BCR_FLR);
1730
1731 /* wait 100ms, per PCI spec */
1732 mdelay(100);
1733
1734 return 0;
1735}
1736
Suneel Garapati13822f72019-10-19 16:07:20 -07001737#if defined(CONFIG_PCI_SRIOV)
1738int pci_sriov_init(struct udevice *pdev, int vf_en)
1739{
1740 u16 vendor, device;
1741 struct udevice *bus;
1742 struct udevice *dev;
1743 pci_dev_t bdf;
1744 u16 ctrl;
1745 u16 num_vfs;
1746 u16 total_vf;
1747 u16 vf_offset;
1748 u16 vf_stride;
1749 int vf, ret;
1750 int pos;
1751
1752 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1753 if (!pos) {
1754 debug("Error: SRIOV capability not found\n");
1755 return -ENOENT;
1756 }
1757
1758 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1759
1760 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1761 if (vf_en > total_vf)
1762 vf_en = total_vf;
1763 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1764
1765 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1766 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1767
1768 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1769 if (num_vfs > vf_en)
1770 num_vfs = vf_en;
1771
1772 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1773 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1774
1775 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1776 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1777
1778 bdf = dm_pci_get_bdf(pdev);
1779
1780 pci_get_bus(PCI_BUS(bdf), &bus);
1781
1782 if (!bus)
1783 return -ENODEV;
1784
1785 bdf += PCI_BDF(0, 0, vf_offset);
1786
1787 for (vf = 0; vf < num_vfs; vf++) {
Simon Glassb75b15b2020-12-03 16:55:23 -07001788 struct pci_child_plat *pplat;
Suneel Garapati13822f72019-10-19 16:07:20 -07001789 ulong class;
1790
1791 pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1792 &class, PCI_SIZE_16);
1793
1794 debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -07001795 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Suneel Garapati13822f72019-10-19 16:07:20 -07001796
1797 /* Find this device in the device tree */
1798 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1799
1800 if (ret == -ENODEV) {
1801 struct pci_device_id find_id;
1802
1803 memset(&find_id, '\0', sizeof(find_id));
1804 find_id.vendor = vendor;
1805 find_id.device = device;
1806 find_id.class = class;
1807
1808 ret = pci_find_and_bind_driver(bus, &find_id,
1809 bdf, &dev);
1810
1811 if (ret)
1812 return ret;
1813 }
1814
1815 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -07001816 pplat = dev_get_parent_plat(dev);
Suneel Garapati13822f72019-10-19 16:07:20 -07001817 pplat->devfn = PCI_MASK_BUS(bdf);
1818 pplat->vendor = vendor;
1819 pplat->device = device;
1820 pplat->class = class;
1821 pplat->is_virtfn = true;
1822 pplat->pfdev = pdev;
1823 pplat->virtid = vf * vf_stride + vf_offset;
1824
1825 debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
Simon Glass75e534b2020-12-16 21:20:07 -07001826 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
Suneel Garapati13822f72019-10-19 16:07:20 -07001827 PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1828 bdf += PCI_BDF(0, 0, vf_stride);
1829 }
1830
1831 return 0;
1832}
1833
1834int pci_sriov_get_totalvfs(struct udevice *pdev)
1835{
1836 u16 total_vf;
1837 int pos;
1838
1839 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1840 if (!pos) {
1841 debug("Error: SRIOV capability not found\n");
1842 return -ENOENT;
1843 }
1844
1845 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1846
1847 return total_vf;
1848}
1849#endif /* SRIOV */
1850
Simon Glassb94dc892015-03-05 12:25:25 -07001851UCLASS_DRIVER(pci) = {
1852 .id = UCLASS_PCI,
1853 .name = "pci",
Simon Glassbe706102020-12-16 21:20:18 -07001854 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
Simon Glass18230342016-07-05 17:10:10 -06001855 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001856 .pre_probe = pci_uclass_pre_probe,
1857 .post_probe = pci_uclass_post_probe,
1858 .child_post_bind = pci_uclass_child_post_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001859 .per_device_auto = sizeof(struct pci_controller),
Simon Glassb75b15b2020-12-03 16:55:23 -07001860 .per_child_plat_auto = sizeof(struct pci_child_plat),
Simon Glassb94dc892015-03-05 12:25:25 -07001861};
1862
1863static const struct dm_pci_ops pci_bridge_ops = {
1864 .read_config = pci_bridge_read_config,
1865 .write_config = pci_bridge_write_config,
1866};
1867
1868static const struct udevice_id pci_bridge_ids[] = {
1869 { .compatible = "pci-bridge" },
1870 { }
1871};
1872
1873U_BOOT_DRIVER(pci_bridge_drv) = {
1874 .name = "pci_bridge_drv",
1875 .id = UCLASS_PCI,
1876 .of_match = pci_bridge_ids,
1877 .ops = &pci_bridge_ops,
1878};
1879
1880UCLASS_DRIVER(pci_generic) = {
1881 .id = UCLASS_PCI_GENERIC,
1882 .name = "pci_generic",
1883};
1884
1885static const struct udevice_id pci_generic_ids[] = {
1886 { .compatible = "pci-generic" },
1887 { }
1888};
1889
1890U_BOOT_DRIVER(pci_generic_drv) = {
1891 .name = "pci_generic_drv",
1892 .id = UCLASS_PCI_GENERIC,
1893 .of_match = pci_generic_ids,
1894};
Stephen Warren04eb2692016-01-26 11:10:11 -07001895
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001896int pci_init(void)
Stephen Warren04eb2692016-01-26 11:10:11 -07001897{
1898 struct udevice *bus;
1899
1900 /*
1901 * Enumerate all known controller devices. Enumeration has the side-
1902 * effect of probing them, so PCIe devices will be enumerated too.
1903 */
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001904 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warren04eb2692016-01-26 11:10:11 -07001905 bus;
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001906 uclass_next_device_check(&bus)) {
Stephen Warren04eb2692016-01-26 11:10:11 -07001907 ;
1908 }
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001909
1910 return 0;
Stephen Warren04eb2692016-01-26 11:10:11 -07001911}