blob: 2c85e78a13652c6f2a7b8f804c530f31c24d4bd5 [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 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010079 * Return: last bus number, or -1 if no active buses
Simon Glassb94dc892015-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 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 }
Pali RohĂĄr8b79e082022-01-17 16:38:37 +0100563 if (hose->last_busno < sub_bus)
564 hose->last_busno = sub_bus;
Simon Glassb94dc892015-03-05 12:25:25 -0700565 debug("%s: done\n", __func__);
566
Simon Glassbe706102020-12-16 21:20:18 -0700567 return log_msg_ret("sub", sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700568}
569
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300570int pci_generic_mmap_write_config(
Simon Glass2a311e82020-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 Tynkkynen8cce4cf2017-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 Glass2a311e82020-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 Tynkkynen8cce4cf2017-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 Glass37a3f94b2015-11-29 13:17:49 -0700630int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700631{
Pali RohĂĄr4fb65992021-10-07 14:50:58 +0200632 u8 header_type;
Simon Glassb94dc892015-03-05 12:25:25 -0700633 int sub_bus;
634 int ret;
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700635 int ea_pos;
636 u8 reg;
Simon Glassb94dc892015-03-05 12:25:25 -0700637
638 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700639
Pali RohĂĄr4fb65992021-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
Andrew Scull71e7e1a2022-04-21 16:11:16 +0000648 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
649 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
650 else
651 ea_pos = 0;
652
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700653 if (ea_pos) {
654 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
655 &reg);
656 sub_bus = reg;
657 } else {
658 sub_bus = pci_get_bus_max() + 1;
659 }
Simon Glassb94dc892015-03-05 12:25:25 -0700660 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700661 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700662
663 ret = device_probe(bus);
664 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600665 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700666 ret);
Simon Glassbe706102020-12-16 21:20:18 -0700667 return log_msg_ret("probe", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700668 }
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700669
Masami Hiramatsuff022452021-04-16 14:53:46 -0700670 if (!ea_pos)
671 sub_bus = pci_get_bus_max();
672
Simon Glass37a3f94b2015-11-29 13:17:49 -0700673 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700674
675 return sub_bus;
676}
677
Simon Glass318d71c2015-07-06 16:47:44 -0600678/**
679 * pci_match_one_device - Tell if a PCI device structure has a matching
680 * PCI device id structure
681 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800682 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600683 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800684 * Returns true if the finding pci_device_id structure matched or false if
685 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600686 */
687static bool pci_match_one_id(const struct pci_device_id *id,
688 const struct pci_device_id *find)
689{
690 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
691 (id->device == PCI_ANY_ID || id->device == find->device) &&
692 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
693 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
694 !((id->class ^ find->class) & id->class_mask))
695 return true;
696
697 return false;
698}
699
700/**
Simon Glass8807a562021-06-27 17:50:57 -0600701 * pci_need_device_pre_reloc() - Check if a device should be bound
702 *
703 * This checks a list of vendor/device-ID values indicating devices that should
704 * be bound before relocation.
705 *
706 * @bus: Bus to check
707 * @vendor: Vendor ID to check
708 * @device: Device ID to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100709 * Return: true if the vendor/device is in the list, false if not
Simon Glass8807a562021-06-27 17:50:57 -0600710 */
711static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor,
712 uint device)
713{
714 u32 vendev;
715 int index;
716
717 for (index = 0;
718 !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index,
719 &vendev);
720 index++) {
721 if (vendev == PCI_VENDEV(vendor, device))
722 return true;
723 }
724
725 return false;
726}
727
728/**
Simon Glass318d71c2015-07-06 16:47:44 -0600729 * pci_find_and_bind_driver() - Find and bind the right PCI driver
730 *
731 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600732 *
733 * @parent: Parent bus
734 * @find_id: Specification of the driver to find
735 * @bdf: Bus/device/function addreess - see PCI_BDF()
736 * @devp: Returns a pointer to the device created
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100737 * Return: 0 if OK, -EPERM if the device is not needed before relocation and
Simon Glassc45abf12015-09-08 17:52:49 -0600738 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600739 */
740static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600741 struct pci_device_id *find_id,
742 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600743{
744 struct pci_driver_entry *start, *entry;
Marek Vasutb4535792018-10-10 21:27:06 +0200745 ofnode node = ofnode_null();
Simon Glass318d71c2015-07-06 16:47:44 -0600746 const char *drv;
747 int n_ents;
748 int ret;
749 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700750 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600751
752 *devp = NULL;
753
754 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
755 find_id->vendor, find_id->device);
Marek Vasutb4535792018-10-10 21:27:06 +0200756
757 /* Determine optional OF node */
Suneel Garapaticb7093d2019-10-19 16:02:48 -0700758 if (ofnode_valid(dev_ofnode(parent)))
759 pci_dev_find_ofnode(parent, bdf, &node);
Marek Vasutb4535792018-10-10 21:27:06 +0200760
Michael Walle2e21f372019-12-01 17:45:18 +0100761 if (ofnode_valid(node) && !ofnode_is_available(node)) {
762 debug("%s: Ignoring disabled device\n", __func__);
Simon Glassbe706102020-12-16 21:20:18 -0700763 return log_msg_ret("dis", -EPERM);
Michael Walle2e21f372019-12-01 17:45:18 +0100764 }
765
Simon Glass318d71c2015-07-06 16:47:44 -0600766 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
767 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
768 for (entry = start; entry != start + n_ents; entry++) {
769 const struct pci_device_id *id;
770 struct udevice *dev;
771 const struct driver *drv;
772
773 for (id = entry->match;
774 id->vendor || id->subvendor || id->class_mask;
775 id++) {
776 if (!pci_match_one_id(id, find_id))
777 continue;
778
779 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700780
781 /*
782 * In the pre-relocation phase, we only bind devices
783 * whose driver has the DM_FLAG_PRE_RELOC set, to save
784 * precious memory space as on some platforms as that
785 * space is pretty limited (ie: using Cache As RAM).
786 */
787 if (!(gd->flags & GD_FLG_RELOC) &&
788 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassbe706102020-12-16 21:20:18 -0700789 return log_msg_ret("pre", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700790
Simon Glass318d71c2015-07-06 16:47:44 -0600791 /*
792 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700793 * plat (instead of NULL) and allow its bind()
Simon Glass318d71c2015-07-06 16:47:44 -0600794 * method to return -ENOENT if it doesn't support this
795 * device. That way we could continue the search to
796 * find another driver. For now this doesn't seem
797 * necesssary, so just bind the first match.
798 */
Simon Glass884870f2020-11-28 17:50:01 -0700799 ret = device_bind(parent, drv, drv->name, NULL, node,
800 &dev);
Simon Glass318d71c2015-07-06 16:47:44 -0600801 if (ret)
802 goto error;
803 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700804 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600805 *devp = dev;
806 return 0;
807 }
808 }
809
Bin Meng984c0dc2015-08-20 06:40:17 -0700810 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
811 /*
812 * In the pre-relocation phase, we only bind bridge devices to save
813 * precious memory space as on some platforms as that space is pretty
814 * limited (ie: using Cache As RAM).
815 */
Simon Glass8807a562021-06-27 17:50:57 -0600816 if (!(gd->flags & GD_FLG_RELOC) && !bridge &&
817 !pci_need_device_pre_reloc(parent, find_id->vendor,
818 find_id->device))
Simon Glassbe706102020-12-16 21:20:18 -0700819 return log_msg_ret("notbr", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700820
Simon Glass318d71c2015-07-06 16:47:44 -0600821 /* Bind a generic driver so that the device can be used */
Simon Glass75e534b2020-12-16 21:20:07 -0700822 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
Bin Meng0a721522015-07-19 00:20:04 +0800823 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600824 str = strdup(name);
825 if (!str)
826 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700827 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
828
Marek Vasutb4535792018-10-10 21:27:06 +0200829 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glass318d71c2015-07-06 16:47:44 -0600830 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600831 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200832 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600833 return ret;
834 }
835 debug("%s: No match found: bound generic driver instead\n", __func__);
836
837 return 0;
838
839error:
840 debug("%s: No match found: error %d\n", __func__, ret);
841 return ret;
842}
843
Tim Harvey4c57bf72021-04-16 14:53:47 -0700844__weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
845{
846}
847
Simon Glassb94dc892015-03-05 12:25:25 -0700848int pci_bind_bus_devices(struct udevice *bus)
849{
850 ulong vendor, device;
851 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800852 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700853 bool found_multi;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700854 int ari_off;
Simon Glassb94dc892015-03-05 12:25:25 -0700855 int ret;
856
857 found_multi = false;
Simon Glass75e534b2020-12-16 21:20:07 -0700858 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
Bin Meng0a721522015-07-19 00:20:04 +0800859 PCI_MAX_PCI_FUNCTIONS - 1);
Simon Glass75e534b2020-12-16 21:20:07 -0700860 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800861 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700862 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700863 struct udevice *dev;
864 ulong class;
865
Bin Meng20bdc1e2018-08-03 01:14:37 -0700866 if (!PCI_FUNC(bdf))
867 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800868 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700869 continue;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800870
Simon Glassb94dc892015-03-05 12:25:25 -0700871 /* Check only the first access, we don't expect problems */
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800872 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
873 PCI_SIZE_16);
Pali RohĂĄra8a520d2021-09-07 18:07:08 +0200874 if (ret || vendor == 0xffff || vendor == 0x0000)
Simon Glassb94dc892015-03-05 12:25:25 -0700875 continue;
876
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800877 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
878 &header_type, PCI_SIZE_8);
879
Bin Meng0a721522015-07-19 00:20:04 +0800880 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700881 found_multi = header_type & 0x80;
882
Simon Glass25916d62019-09-25 08:56:12 -0600883 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -0700884 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Bin Meng0a721522015-07-19 00:20:04 +0800885 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700886 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800887 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600888 PCI_SIZE_32);
889 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700890
891 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800892 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass25916d62019-09-25 08:56:12 -0600893 debug(": find ret=%d\n", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700894
Simon Glass413ebdb2015-11-29 13:18:09 -0700895 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700896 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600897 struct pci_device_id find_id;
898 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700899
Simon Glass318d71c2015-07-06 16:47:44 -0600900 memset(&find_id, '\0', sizeof(find_id));
901 find_id.vendor = vendor;
902 find_id.device = device;
903 find_id.class = class;
904 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800905 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600906 PCI_SUBSYSTEM_VENDOR_ID,
907 &val, PCI_SIZE_32);
908 find_id.subvendor = val & 0xffff;
909 find_id.subdevice = val >> 16;
910 }
Bin Meng0a721522015-07-19 00:20:04 +0800911 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600912 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700913 }
Simon Glassc45abf12015-09-08 17:52:49 -0600914 if (ret == -EPERM)
915 continue;
916 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700917 return ret;
918
919 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -0700920 pplat = dev_get_parent_plat(dev);
Simon Glassc45abf12015-09-08 17:52:49 -0600921 pplat->devfn = PCI_MASK_BUS(bdf);
922 pplat->vendor = vendor;
923 pplat->device = device;
924 pplat->class = class;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700925
926 if (IS_ENABLED(CONFIG_PCI_ARID)) {
927 ari_off = dm_pci_find_ext_capability(dev,
928 PCI_EXT_CAP_ID_ARI);
929 if (ari_off) {
930 u16 ari_cap;
931
932 /*
933 * Read Next Function number in ARI Cap
934 * Register
935 */
936 dm_pci_read_config16(dev, ari_off + 4,
937 &ari_cap);
938 /*
939 * Update next scan on this function number,
940 * subtract 1 in BDF to satisfy loop increment.
941 */
942 if (ari_cap & 0xff00) {
943 bdf = PCI_BDF(PCI_BUS(bdf),
944 PCI_DEV(ari_cap),
945 PCI_FUNC(ari_cap));
946 bdf = bdf - 0x100;
947 }
948 }
949 }
Tim Harvey4c57bf72021-04-16 14:53:47 -0700950
951 board_pci_fixup_dev(bus, dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700952 }
953
954 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -0700955}
956
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +0100957static int decode_regions(struct pci_controller *hose, ofnode parent_node,
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700958 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700959{
960 int pci_addr_cells, addr_cells, size_cells;
961 int cells_per_record;
Stefan Roesebbc88462020-08-12 11:55:46 +0200962 struct bd_info *bd;
Simon Glassb94dc892015-03-05 12:25:25 -0700963 const u32 *prop;
Stefan Roese950864f2020-07-23 16:34:10 +0200964 int max_regions;
Simon Glassb94dc892015-03-05 12:25:25 -0700965 int len;
966 int i;
967
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900968 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700969 if (!prop) {
970 debug("%s: Cannot decode regions\n", __func__);
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +0100971 return -EINVAL;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700972 }
973
Simon Glass4191dc12017-06-12 06:21:31 -0600974 pci_addr_cells = ofnode_read_simple_addr_cells(node);
975 addr_cells = ofnode_read_simple_addr_cells(parent_node);
976 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700977
978 /* PCI addresses are always 3-cells */
979 len /= sizeof(u32);
980 cells_per_record = pci_addr_cells + addr_cells + size_cells;
981 hose->region_count = 0;
982 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
983 cells_per_record);
Stefan Roese950864f2020-07-23 16:34:10 +0200984
985 /* Dynamically allocate the regions array */
986 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
987 hose->regions = (struct pci_region *)
988 calloc(1, max_regions * sizeof(struct pci_region));
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +0100989 if (!hose->regions)
990 return -ENOMEM;
Stefan Roese950864f2020-07-23 16:34:10 +0200991
992 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassb94dc892015-03-05 12:25:25 -0700993 u64 pci_addr, addr, size;
994 int space_code;
995 u32 flags;
996 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -0700997 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -0700998
999 if (len < cells_per_record)
1000 break;
1001 flags = fdt32_to_cpu(prop[0]);
1002 space_code = (flags >> 24) & 3;
1003 pci_addr = fdtdec_get_number(prop + 1, 2);
1004 prop += pci_addr_cells;
1005 addr = fdtdec_get_number(prop, addr_cells);
1006 prop += addr_cells;
1007 size = fdtdec_get_number(prop, size_cells);
1008 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001009 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
1010 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -07001011 if (space_code & 2) {
1012 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
1013 PCI_REGION_MEM;
1014 } else if (space_code & 1) {
1015 type = PCI_REGION_IO;
1016 } else {
1017 continue;
1018 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +03001019
1020 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
1021 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
Andrew Scullcb06f0e2022-04-21 16:11:07 +00001022 debug(" - pci_addr beyond the 32-bit boundary, ignoring\n");
1023 continue;
1024 }
1025
1026 if (!IS_ENABLED(CONFIG_PHYS_64BIT) && upper_32_bits(addr)) {
1027 debug(" - addr beyond the 32-bit boundary, ignoring\n");
1028 continue;
1029 }
1030
1031 if (~((pci_addr_t)0) - pci_addr < size) {
1032 debug(" - PCI range exceeds max address, ignoring\n");
1033 continue;
1034 }
1035
1036 if (~((phys_addr_t)0) - addr < size) {
1037 debug(" - phys range exceeds max address, ignoring\n");
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +03001038 continue;
1039 }
1040
Simon Glass7efc9ba2015-11-19 20:26:58 -07001041 pos = -1;
Suneel Garapati3ac3aec2019-10-19 17:10:20 -07001042 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
1043 for (i = 0; i < hose->region_count; i++) {
1044 if (hose->regions[i].flags == type)
1045 pos = i;
1046 }
Simon Glass7efc9ba2015-11-19 20:26:58 -07001047 }
Suneel Garapati3ac3aec2019-10-19 17:10:20 -07001048
Simon Glass7efc9ba2015-11-19 20:26:58 -07001049 if (pos == -1)
1050 pos = hose->region_count++;
1051 debug(" - type=%d, pos=%d\n", type, pos);
1052 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -07001053 }
1054
1055 /* Add a region for our local memory */
Stefan Roesebbc88462020-08-12 11:55:46 +02001056 bd = gd->bd;
Bin Mengae0bdde2018-03-27 00:46:05 -07001057 if (!bd)
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001058 return 0;
Bin Mengae0bdde2018-03-27 00:46:05 -07001059
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001060 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1061 if (bd->bi_dram[i].size) {
Daniel Schwierzeckf59925e2021-07-15 20:53:56 +02001062 phys_addr_t start = bd->bi_dram[i].start;
1063
1064 if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY))
1065 start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start);
1066
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001067 pci_set_region(hose->regions + hose->region_count++,
Daniel Schwierzeckf59925e2021-07-15 20:53:56 +02001068 start, start, bd->bi_dram[i].size,
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001069 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1070 }
1071 }
Simon Glassb94dc892015-03-05 12:25:25 -07001072
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001073 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001074}
1075
1076static int pci_uclass_pre_probe(struct udevice *bus)
1077{
1078 struct pci_controller *hose;
Simon Glassbe706102020-12-16 21:20:18 -07001079 struct uclass *uc;
1080 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001081
Simon Glass75e534b2020-12-16 21:20:07 -07001082 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -07001083 bus->parent->name);
Simon Glass95588622020-12-22 19:30:28 -07001084 hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001085
Simon Glassbe706102020-12-16 21:20:18 -07001086 /*
1087 * Set the sequence number, if device_bind() doesn't. We want control
1088 * of this so that numbers are allocated as devices are probed. That
1089 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1090 * higher than their parents)
1091 */
1092 if (dev_seq(bus) == -1) {
1093 ret = uclass_get(UCLASS_PCI, &uc);
1094 if (ret)
1095 return ret;
Simon Glass5e349922020-12-19 10:40:09 -07001096 bus->seq_ = uclass_find_next_free_seq(uc);
Simon Glassbe706102020-12-16 21:20:18 -07001097 }
1098
Simon Glassb94dc892015-03-05 12:25:25 -07001099 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +01001100 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -07001101 hose->ctlr = bus;
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001102 ret = decode_regions(hose, dev_ofnode(bus->parent),
1103 dev_ofnode(bus));
1104 if (ret)
1105 return ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001106 } else {
1107 struct pci_controller *parent_hose;
1108
1109 parent_hose = dev_get_uclass_priv(bus->parent);
1110 hose->ctlr = parent_hose->bus;
1111 }
Simon Glassbe706102020-12-16 21:20:18 -07001112
Simon Glassb94dc892015-03-05 12:25:25 -07001113 hose->bus = bus;
Simon Glass75e534b2020-12-16 21:20:07 -07001114 hose->first_busno = dev_seq(bus);
1115 hose->last_busno = dev_seq(bus);
Simon Glassf1d50f72020-12-19 10:40:13 -07001116 if (dev_has_ofnode(bus)) {
Suneel Garapatif8c86282020-05-04 21:25:25 -07001117 hose->skip_auto_config_until_reloc =
1118 dev_read_bool(bus,
1119 "u-boot,skip-auto-config-until-reloc");
1120 }
Simon Glassb94dc892015-03-05 12:25:25 -07001121
1122 return 0;
1123}
1124
1125static int pci_uclass_post_probe(struct udevice *bus)
1126{
Simon Glass68e35a72019-12-06 21:41:37 -07001127 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001128 int ret;
1129
Simon Glass75e534b2020-12-16 21:20:07 -07001130 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001131 ret = pci_bind_bus_devices(bus);
1132 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001133 return log_msg_ret("bind", ret);
Simon Glassb94dc892015-03-05 12:25:25 -07001134
Simon Glassbd165e72020-04-26 09:12:56 -06001135 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass68e35a72019-12-06 21:41:37 -07001136 (!hose->skip_auto_config_until_reloc ||
1137 (gd->flags & GD_FLG_RELOC))) {
1138 ret = pci_auto_config_devices(bus);
1139 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -07001140 return log_msg_ret("cfg", ret);
Simon Glass68e35a72019-12-06 21:41:37 -07001141 }
Simon Glassb94dc892015-03-05 12:25:25 -07001142
Bin Mengc0820a42015-08-20 06:40:23 -07001143#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1144 /*
1145 * Per Intel FSP specification, we should call FSP notify API to
1146 * inform FSP that PCI enumeration has been done so that FSP will
1147 * do any necessary initialization as required by the chipset's
1148 * BIOS Writer's Guide (BWG).
1149 *
1150 * Unfortunately we have to put this call here as with driver model,
1151 * the enumeration is all done on a lazy basis as needed, so until
1152 * something is touched on PCI it won't happen.
1153 *
1154 * Note we only call this 1) after U-Boot is relocated, and 2)
1155 * root bus has finished probing.
1156 */
Simon Glass75e534b2020-12-16 21:20:07 -07001157 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
Bin Mengc0820a42015-08-20 06:40:23 -07001158 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -06001159 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001160 return log_msg_ret("fsp", ret);
Simon Glassb072d522015-09-08 17:52:47 -06001161 }
Bin Mengc0820a42015-08-20 06:40:23 -07001162#endif
1163
Simon Glassb072d522015-09-08 17:52:47 -06001164 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001165}
1166
1167static int pci_uclass_child_post_bind(struct udevice *dev)
1168{
Simon Glassb75b15b2020-12-03 16:55:23 -07001169 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -07001170
Simon Glassf1d50f72020-12-19 10:40:13 -07001171 if (!dev_has_ofnode(dev))
Simon Glassb94dc892015-03-05 12:25:25 -07001172 return 0;
1173
Simon Glass71fa5b42020-12-03 16:55:18 -07001174 pplat = dev_get_parent_plat(dev);
Bin Meng00d808e2018-08-03 01:14:36 -07001175
1176 /* Extract vendor id and device id if available */
1177 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1178
1179 /* Extract the devfn from fdt_pci_addr */
Stefan Roesea74eb552019-01-25 11:52:42 +01001180 pplat->devfn = pci_get_devfn(dev);
Simon Glassb94dc892015-03-05 12:25:25 -07001181
1182 return 0;
1183}
1184
Simon Glass2a311e82020-01-27 08:49:37 -07001185static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng0a721522015-07-19 00:20:04 +08001186 uint offset, ulong *valuep,
1187 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001188{
Simon Glass95588622020-12-22 19:30:28 -07001189 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001190
1191 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1192}
1193
Bin Meng0a721522015-07-19 00:20:04 +08001194static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1195 uint offset, ulong value,
1196 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001197{
Simon Glass95588622020-12-22 19:30:28 -07001198 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001199
1200 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1201}
1202
Simon Glass04c8b6a2015-08-10 07:05:04 -06001203static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1204{
1205 struct udevice *dev;
1206 int ret = 0;
1207
1208 /*
1209 * Scan through all the PCI controllers. On x86 there will only be one
1210 * but that is not necessarily true on other hardware.
1211 */
1212 do {
1213 device_find_first_child(bus, &dev);
1214 if (dev) {
1215 *devp = dev;
1216 return 0;
1217 }
1218 ret = uclass_next_device(&bus);
1219 if (ret)
1220 return ret;
1221 } while (bus);
1222
1223 return 0;
1224}
1225
1226int pci_find_next_device(struct udevice **devp)
1227{
1228 struct udevice *child = *devp;
1229 struct udevice *bus = child->parent;
1230 int ret;
1231
1232 /* First try all the siblings */
1233 *devp = NULL;
1234 while (child) {
1235 device_find_next_child(&child);
1236 if (child) {
1237 *devp = child;
1238 return 0;
1239 }
1240 }
1241
1242 /* We ran out of siblings. Try the next bus */
1243 ret = uclass_next_device(&bus);
1244 if (ret)
1245 return ret;
1246
1247 return bus ? skip_to_next_device(bus, devp) : 0;
1248}
1249
1250int pci_find_first_device(struct udevice **devp)
1251{
1252 struct udevice *bus;
1253 int ret;
1254
1255 *devp = NULL;
1256 ret = uclass_first_device(UCLASS_PCI, &bus);
1257 if (ret)
1258 return ret;
1259
1260 return skip_to_next_device(bus, devp);
1261}
1262
Simon Glass27a733f2015-11-19 20:26:59 -07001263ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1264{
1265 switch (size) {
1266 case PCI_SIZE_8:
1267 return (value >> ((offset & 3) * 8)) & 0xff;
1268 case PCI_SIZE_16:
1269 return (value >> ((offset & 2) * 8)) & 0xffff;
1270 default:
1271 return value;
1272 }
1273}
1274
1275ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1276 enum pci_size_t size)
1277{
1278 uint off_mask;
1279 uint val_mask, shift;
1280 ulong ldata, mask;
1281
1282 switch (size) {
1283 case PCI_SIZE_8:
1284 off_mask = 3;
1285 val_mask = 0xff;
1286 break;
1287 case PCI_SIZE_16:
1288 off_mask = 2;
1289 val_mask = 0xffff;
1290 break;
1291 default:
1292 return value;
1293 }
1294 shift = (offset & off_mask) * 8;
1295 ldata = (value & val_mask) << shift;
1296 mask = val_mask << shift;
1297 value = (old & ~mask) | ldata;
1298
1299 return value;
1300}
1301
Rayagonda Kokatanurcdc7ed32020-05-12 13:29:49 +05301302int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1303{
1304 int pci_addr_cells, addr_cells, size_cells;
1305 int cells_per_record;
1306 const u32 *prop;
1307 int len;
1308 int i = 0;
1309
1310 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1311 if (!prop) {
1312 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1313 dev->name);
1314 return -EINVAL;
1315 }
1316
1317 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1318 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1319 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1320
1321 /* PCI addresses are always 3-cells */
1322 len /= sizeof(u32);
1323 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1324 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1325 cells_per_record);
1326
1327 while (len) {
1328 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1329 prop += pci_addr_cells;
1330 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1331 prop += addr_cells;
1332 memp->size = fdtdec_get_number(prop, size_cells);
1333 prop += size_cells;
1334
1335 if (i == index)
1336 return 0;
1337 i++;
1338 len -= cells_per_record;
1339 }
1340
1341 return -EINVAL;
1342}
1343
Simon Glassdcdc0122015-11-19 20:27:01 -07001344int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1345 struct pci_region **memp, struct pci_region **prefp)
1346{
1347 struct udevice *bus = pci_get_controller(dev);
1348 struct pci_controller *hose = dev_get_uclass_priv(bus);
1349 int i;
1350
1351 *iop = NULL;
1352 *memp = NULL;
1353 *prefp = NULL;
1354 for (i = 0; i < hose->region_count; i++) {
1355 switch (hose->regions[i].flags) {
1356 case PCI_REGION_IO:
1357 if (!*iop || (*iop)->size < hose->regions[i].size)
1358 *iop = hose->regions + i;
1359 break;
1360 case PCI_REGION_MEM:
1361 if (!*memp || (*memp)->size < hose->regions[i].size)
1362 *memp = hose->regions + i;
1363 break;
1364 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1365 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1366 *prefp = hose->regions + i;
1367 break;
1368 }
1369 }
1370
1371 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1372}
1373
Simon Glassc92aac12020-01-27 08:49:38 -07001374u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glass3452cb12015-11-29 13:17:53 -07001375{
1376 u32 addr;
1377 int bar;
1378
1379 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1380 dm_pci_read_config32(dev, bar, &addr);
Simon Glass71fafd12020-04-09 10:27:36 -06001381
1382 /*
1383 * If we get an invalid address, return this so that comparisons with
1384 * FDT_ADDR_T_NONE work correctly
1385 */
1386 if (addr == 0xffffffff)
1387 return addr;
1388 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glass3452cb12015-11-29 13:17:53 -07001389 return addr & PCI_BASE_ADDRESS_IO_MASK;
1390 else
1391 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1392}
1393
Simon Glasse2b6b562016-01-18 20:19:15 -07001394void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1395{
1396 int bar;
1397
1398 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1399 dm_pci_write_config32(dev, bar, addr);
1400}
1401
Andrew Scull994b60d2022-04-21 16:11:11 +00001402phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1403 size_t len, unsigned long mask,
1404 unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001405{
Andrew Scull994b60d2022-04-21 16:11:11 +00001406 struct udevice *ctlr;
1407 struct pci_controller *hose;
Simon Glassc5f053b2015-11-29 13:18:03 -07001408 struct pci_region *res;
Andrew Scull3bf61522022-04-21 16:11:08 +00001409 pci_addr_t offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001410 int i;
1411
Andrew Scull994b60d2022-04-21 16:11:11 +00001412 /* The root controller has the region information */
1413 ctlr = pci_get_controller(dev);
1414 hose = dev_get_uclass_priv(ctlr);
1415
1416 if (hose->region_count == 0)
1417 return bus_addr;
Christian Gmeiner7241f802018-06-10 06:25:06 -07001418
Simon Glassc5f053b2015-11-29 13:18:03 -07001419 for (i = 0; i < hose->region_count; i++) {
1420 res = &hose->regions[i];
1421
Andrew Scull994b60d2022-04-21 16:11:11 +00001422 if ((res->flags & mask) != flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001423 continue;
1424
Andrew Scull3bf61522022-04-21 16:11:08 +00001425 if (bus_addr < res->bus_start)
1426 continue;
1427
1428 offset = bus_addr - res->bus_start;
1429 if (offset >= res->size)
1430 continue;
1431
1432 if (len > res->size - offset)
1433 continue;
1434
Andrew Scull994b60d2022-04-21 16:11:11 +00001435 return res->phys_start + offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001436 }
1437
Andrew Scull994b60d2022-04-21 16:11:11 +00001438 puts("pci_hose_bus_to_phys: invalid physical address\n");
1439 return 0;
Simon Glassc5f053b2015-11-29 13:18:03 -07001440}
1441
Andrew Scull994b60d2022-04-21 16:11:11 +00001442pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1443 size_t len, unsigned long mask,
1444 unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001445{
Simon Glassc5f053b2015-11-29 13:18:03 -07001446 struct udevice *ctlr;
Andrew Scull994b60d2022-04-21 16:11:11 +00001447 struct pci_controller *hose;
Simon Glassc5f053b2015-11-29 13:18:03 -07001448 struct pci_region *res;
Andrew Scull3bf61522022-04-21 16:11:08 +00001449 phys_addr_t offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001450 int i;
Simon Glassc5f053b2015-11-29 13:18:03 -07001451
1452 /* The root controller has the region information */
1453 ctlr = pci_get_controller(dev);
1454 hose = dev_get_uclass_priv(ctlr);
1455
Andrew Scull994b60d2022-04-21 16:11:11 +00001456 if (hose->region_count == 0)
1457 return phys_addr;
Christian Gmeiner7241f802018-06-10 06:25:06 -07001458
Simon Glassc5f053b2015-11-29 13:18:03 -07001459 for (i = 0; i < hose->region_count; i++) {
1460 res = &hose->regions[i];
1461
Andrew Scull994b60d2022-04-21 16:11:11 +00001462 if ((res->flags & mask) != flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001463 continue;
1464
Andrew Scull3bf61522022-04-21 16:11:08 +00001465 if (phys_addr < res->phys_start)
1466 continue;
1467
1468 offset = phys_addr - res->phys_start;
1469 if (offset >= res->size)
1470 continue;
Simon Glassc5f053b2015-11-29 13:18:03 -07001471
Andrew Scull3bf61522022-04-21 16:11:08 +00001472 if (len > res->size - offset)
1473 continue;
1474
Andrew Scull994b60d2022-04-21 16:11:11 +00001475 return res->bus_start + offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001476 }
1477
Andrew Scull994b60d2022-04-21 16:11:11 +00001478 puts("pci_hose_phys_to_bus: invalid physical address\n");
1479 return 0;
Simon Glassc5f053b2015-11-29 13:18:03 -07001480}
1481
Suneel Garapati5858ba82019-10-19 16:34:16 -07001482static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
Simon Glassb75b15b2020-12-03 16:55:23 -07001483 struct pci_child_plat *pdata)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001484{
1485 phys_addr_t addr = 0;
1486
1487 /*
1488 * In the case of a Virtual Function device using BAR
1489 * base and size, add offset for VFn BAR(1, 2, 3...n)
1490 */
1491 if (pdata->is_virtfn) {
1492 size_t sz;
1493 u32 ea_entry;
1494
1495 /* MaxOffset, 1st DW */
1496 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1497 sz = ea_entry & PCI_EA_FIELD_MASK;
1498 /* Fill up lower 2 bits */
1499 sz |= (~PCI_EA_FIELD_MASK);
1500
1501 if (ea_entry & PCI_EA_IS_64) {
1502 /* MaxOffset 2nd DW */
1503 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1504 sz |= ((u64)ea_entry) << 32;
1505 }
1506
1507 addr = (pdata->virtid - 1) * (sz + 1);
1508 }
1509
1510 return addr;
1511}
1512
Andrew Scull58c61022022-04-21 16:11:10 +00001513static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, size_t offset,
1514 size_t len, int ea_off,
Andrew Scull30d338d2022-04-21 16:11:06 +00001515 struct pci_child_plat *pdata)
Alex Marginean1c934a62019-06-07 11:24:23 +03001516{
1517 int ea_cnt, i, entry_size;
1518 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1519 u32 ea_entry;
1520 phys_addr_t addr;
1521
Suneel Garapati5858ba82019-10-19 16:34:16 -07001522 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1523 /*
1524 * In the case of a Virtual Function device, device is
1525 * Physical function, so pdata will point to required VF
1526 * specific data.
1527 */
1528 if (pdata->is_virtfn)
1529 bar_id += PCI_EA_BEI_VF_BAR0;
1530 }
1531
Alex Marginean1c934a62019-06-07 11:24:23 +03001532 /* EA capability structure header */
1533 dm_pci_read_config32(dev, ea_off, &ea_entry);
1534 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1535 ea_off += PCI_EA_FIRST_ENT;
1536
1537 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1538 /* Entry header */
1539 dm_pci_read_config32(dev, ea_off, &ea_entry);
1540 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1541
1542 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1543 continue;
1544
1545 /* Base address, 1st DW */
1546 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1547 addr = ea_entry & PCI_EA_FIELD_MASK;
1548 if (ea_entry & PCI_EA_IS_64) {
1549 /* Base address, 2nd DW, skip over 4B MaxOffset */
1550 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1551 addr |= ((u64)ea_entry) << 32;
1552 }
1553
Suneel Garapati5858ba82019-10-19 16:34:16 -07001554 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1555 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1556
Andrew Scull58c61022022-04-21 16:11:10 +00001557 if (~((phys_addr_t)0) - addr < offset)
1558 return NULL;
1559
Alex Marginean1c934a62019-06-07 11:24:23 +03001560 /* size ignored for now */
Andrew Scull58c61022022-04-21 16:11:10 +00001561 return map_physmem(addr + offset, len, MAP_NOCACHE);
Alex Marginean1c934a62019-06-07 11:24:23 +03001562 }
1563
1564 return 0;
1565}
1566
Andrew Scull58c61022022-04-21 16:11:10 +00001567void *dm_pci_map_bar(struct udevice *dev, int bar, size_t offset, size_t len,
Andrew Scull6520c822022-04-21 16:11:13 +00001568 unsigned long mask, unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001569{
Simon Glassb75b15b2020-12-03 16:55:23 -07001570 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
Suneel Garapati5858ba82019-10-19 16:34:16 -07001571 struct udevice *udev = dev;
Simon Glassc5f053b2015-11-29 13:18:03 -07001572 pci_addr_t pci_bus_addr;
1573 u32 bar_response;
Alex Marginean1c934a62019-06-07 11:24:23 +03001574 int ea_off;
1575
Suneel Garapati5858ba82019-10-19 16:34:16 -07001576 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1577 /*
1578 * In case of Virtual Function devices, use PF udevice
1579 * as EA capability is defined in Physical Function
1580 */
1581 if (pdata->is_virtfn)
1582 udev = pdata->pfdev;
1583 }
1584
Alex Marginean1c934a62019-06-07 11:24:23 +03001585 /*
1586 * if the function supports Enhanced Allocation use that instead of
1587 * BARs
Suneel Garapati5858ba82019-10-19 16:34:16 -07001588 * Incase of virtual functions, pdata will help read VF BEI
1589 * and EA entry size.
Alex Marginean1c934a62019-06-07 11:24:23 +03001590 */
Andrew Scull71e7e1a2022-04-21 16:11:16 +00001591 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
1592 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
1593 else
1594 ea_off = 0;
1595
Alex Marginean1c934a62019-06-07 11:24:23 +03001596 if (ea_off)
Andrew Scull58c61022022-04-21 16:11:10 +00001597 return dm_pci_map_ea_bar(udev, bar, offset, len, ea_off, pdata);
Simon Glassc5f053b2015-11-29 13:18:03 -07001598
1599 /* read BAR address */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001600 dm_pci_read_config32(udev, bar, &bar_response);
Simon Glassc5f053b2015-11-29 13:18:03 -07001601 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1602
Andrew Scull58c61022022-04-21 16:11:10 +00001603 if (~((pci_addr_t)0) - pci_bus_addr < offset)
1604 return NULL;
1605
Simon Glassc5f053b2015-11-29 13:18:03 -07001606 /*
Andrew Scull58c61022022-04-21 16:11:10 +00001607 * Forward the length argument to dm_pci_bus_to_virt. The length will
1608 * be used to check that the entire address range has been declared as
1609 * a PCI range, but a better check would be to probe for the size of
1610 * the bar and prevent overflow more locally.
Simon Glassc5f053b2015-11-29 13:18:03 -07001611 */
Andrew Scull6520c822022-04-21 16:11:13 +00001612 return dm_pci_bus_to_virt(udev, pci_bus_addr + offset, len, mask, flags,
1613 MAP_NOCACHE);
Simon Glassc5f053b2015-11-29 13:18:03 -07001614}
1615
Bin Meng631f3482018-10-15 02:21:21 -07001616static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001617{
Bin Menga7366f02018-08-03 01:14:52 -07001618 int ttl = PCI_FIND_CAP_TTL;
1619 u8 id;
1620 u16 ent;
Bin Menga7366f02018-08-03 01:14:52 -07001621
1622 dm_pci_read_config8(dev, pos, &pos);
Bin Meng631f3482018-10-15 02:21:21 -07001623
Bin Menga7366f02018-08-03 01:14:52 -07001624 while (ttl--) {
1625 if (pos < PCI_STD_HEADER_SIZEOF)
1626 break;
1627 pos &= ~3;
1628 dm_pci_read_config16(dev, pos, &ent);
1629
1630 id = ent & 0xff;
1631 if (id == 0xff)
1632 break;
1633 if (id == cap)
1634 return pos;
1635 pos = (ent >> 8);
1636 }
1637
1638 return 0;
1639}
1640
Bin Meng631f3482018-10-15 02:21:21 -07001641int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1642{
1643 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1644 cap);
1645}
1646
1647int dm_pci_find_capability(struct udevice *dev, int cap)
1648{
1649 u16 status;
1650 u8 header_type;
1651 u8 pos;
1652
1653 dm_pci_read_config16(dev, PCI_STATUS, &status);
1654 if (!(status & PCI_STATUS_CAP_LIST))
1655 return 0;
1656
1657 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1658 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1659 pos = PCI_CB_CAPABILITY_LIST;
1660 else
1661 pos = PCI_CAPABILITY_LIST;
1662
1663 return _dm_pci_find_next_capability(dev, pos, cap);
1664}
1665
1666int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001667{
1668 u32 header;
1669 int ttl;
1670 int pos = PCI_CFG_SPACE_SIZE;
1671
1672 /* minimum 8 bytes per capability */
1673 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1674
Bin Meng631f3482018-10-15 02:21:21 -07001675 if (start)
1676 pos = start;
1677
Bin Menga7366f02018-08-03 01:14:52 -07001678 dm_pci_read_config32(dev, pos, &header);
1679 /*
1680 * If we have no capabilities, this is indicated by cap ID,
1681 * cap version and next pointer all being 0.
1682 */
1683 if (header == 0)
1684 return 0;
1685
1686 while (ttl--) {
1687 if (PCI_EXT_CAP_ID(header) == cap)
1688 return pos;
1689
1690 pos = PCI_EXT_CAP_NEXT(header);
1691 if (pos < PCI_CFG_SPACE_SIZE)
1692 break;
1693
1694 dm_pci_read_config32(dev, pos, &header);
1695 }
1696
1697 return 0;
1698}
1699
Bin Meng631f3482018-10-15 02:21:21 -07001700int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1701{
1702 return dm_pci_find_next_ext_capability(dev, 0, cap);
1703}
1704
Alex Marginean09467d32019-06-07 11:24:25 +03001705int dm_pci_flr(struct udevice *dev)
1706{
1707 int pcie_off;
1708 u32 cap;
1709
1710 /* look for PCI Express Capability */
1711 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1712 if (!pcie_off)
1713 return -ENOENT;
1714
1715 /* check FLR capability */
1716 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1717 if (!(cap & PCI_EXP_DEVCAP_FLR))
1718 return -ENOENT;
1719
1720 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1721 PCI_EXP_DEVCTL_BCR_FLR);
1722
1723 /* wait 100ms, per PCI spec */
1724 mdelay(100);
1725
1726 return 0;
1727}
1728
Suneel Garapati13822f72019-10-19 16:07:20 -07001729#if defined(CONFIG_PCI_SRIOV)
1730int pci_sriov_init(struct udevice *pdev, int vf_en)
1731{
1732 u16 vendor, device;
1733 struct udevice *bus;
1734 struct udevice *dev;
1735 pci_dev_t bdf;
1736 u16 ctrl;
1737 u16 num_vfs;
1738 u16 total_vf;
1739 u16 vf_offset;
1740 u16 vf_stride;
1741 int vf, ret;
1742 int pos;
1743
1744 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1745 if (!pos) {
1746 debug("Error: SRIOV capability not found\n");
1747 return -ENOENT;
1748 }
1749
1750 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1751
1752 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1753 if (vf_en > total_vf)
1754 vf_en = total_vf;
1755 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1756
1757 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1758 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1759
1760 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1761 if (num_vfs > vf_en)
1762 num_vfs = vf_en;
1763
1764 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1765 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1766
1767 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1768 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1769
1770 bdf = dm_pci_get_bdf(pdev);
1771
1772 pci_get_bus(PCI_BUS(bdf), &bus);
1773
1774 if (!bus)
1775 return -ENODEV;
1776
1777 bdf += PCI_BDF(0, 0, vf_offset);
1778
1779 for (vf = 0; vf < num_vfs; vf++) {
Simon Glassb75b15b2020-12-03 16:55:23 -07001780 struct pci_child_plat *pplat;
Suneel Garapati13822f72019-10-19 16:07:20 -07001781 ulong class;
1782
1783 pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1784 &class, PCI_SIZE_16);
1785
1786 debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -07001787 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Suneel Garapati13822f72019-10-19 16:07:20 -07001788
1789 /* Find this device in the device tree */
1790 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1791
1792 if (ret == -ENODEV) {
1793 struct pci_device_id find_id;
1794
1795 memset(&find_id, '\0', sizeof(find_id));
1796 find_id.vendor = vendor;
1797 find_id.device = device;
1798 find_id.class = class;
1799
1800 ret = pci_find_and_bind_driver(bus, &find_id,
1801 bdf, &dev);
1802
1803 if (ret)
1804 return ret;
1805 }
1806
1807 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -07001808 pplat = dev_get_parent_plat(dev);
Suneel Garapati13822f72019-10-19 16:07:20 -07001809 pplat->devfn = PCI_MASK_BUS(bdf);
1810 pplat->vendor = vendor;
1811 pplat->device = device;
1812 pplat->class = class;
1813 pplat->is_virtfn = true;
1814 pplat->pfdev = pdev;
1815 pplat->virtid = vf * vf_stride + vf_offset;
1816
1817 debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
Simon Glass75e534b2020-12-16 21:20:07 -07001818 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
Suneel Garapati13822f72019-10-19 16:07:20 -07001819 PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1820 bdf += PCI_BDF(0, 0, vf_stride);
1821 }
1822
1823 return 0;
1824}
1825
1826int pci_sriov_get_totalvfs(struct udevice *pdev)
1827{
1828 u16 total_vf;
1829 int pos;
1830
1831 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1832 if (!pos) {
1833 debug("Error: SRIOV capability not found\n");
1834 return -ENOENT;
1835 }
1836
1837 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1838
1839 return total_vf;
1840}
1841#endif /* SRIOV */
1842
Simon Glassb94dc892015-03-05 12:25:25 -07001843UCLASS_DRIVER(pci) = {
1844 .id = UCLASS_PCI,
1845 .name = "pci",
Simon Glassbe706102020-12-16 21:20:18 -07001846 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
Simon Glass18230342016-07-05 17:10:10 -06001847 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001848 .pre_probe = pci_uclass_pre_probe,
1849 .post_probe = pci_uclass_post_probe,
1850 .child_post_bind = pci_uclass_child_post_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001851 .per_device_auto = sizeof(struct pci_controller),
Simon Glassb75b15b2020-12-03 16:55:23 -07001852 .per_child_plat_auto = sizeof(struct pci_child_plat),
Simon Glassb94dc892015-03-05 12:25:25 -07001853};
1854
1855static const struct dm_pci_ops pci_bridge_ops = {
1856 .read_config = pci_bridge_read_config,
1857 .write_config = pci_bridge_write_config,
1858};
1859
1860static const struct udevice_id pci_bridge_ids[] = {
1861 { .compatible = "pci-bridge" },
1862 { }
1863};
1864
1865U_BOOT_DRIVER(pci_bridge_drv) = {
1866 .name = "pci_bridge_drv",
1867 .id = UCLASS_PCI,
1868 .of_match = pci_bridge_ids,
1869 .ops = &pci_bridge_ops,
1870};
1871
1872UCLASS_DRIVER(pci_generic) = {
1873 .id = UCLASS_PCI_GENERIC,
1874 .name = "pci_generic",
1875};
1876
1877static const struct udevice_id pci_generic_ids[] = {
1878 { .compatible = "pci-generic" },
1879 { }
1880};
1881
1882U_BOOT_DRIVER(pci_generic_drv) = {
1883 .name = "pci_generic_drv",
1884 .id = UCLASS_PCI_GENERIC,
1885 .of_match = pci_generic_ids,
1886};
Stephen Warren04eb2692016-01-26 11:10:11 -07001887
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001888int pci_init(void)
Stephen Warren04eb2692016-01-26 11:10:11 -07001889{
1890 struct udevice *bus;
1891
1892 /*
1893 * Enumerate all known controller devices. Enumeration has the side-
1894 * effect of probing them, so PCIe devices will be enumerated too.
1895 */
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001896 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warren04eb2692016-01-26 11:10:11 -07001897 bus;
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001898 uclass_next_device_check(&bus)) {
Stephen Warren04eb2692016-01-26 11:10:11 -07001899 ;
1900 }
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001901
1902 return 0;
Stephen Warren04eb2692016-01-26 11:10:11 -07001903}