blob: 16a6a699f922e9599f1b55ecb95929a458ea7660 [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;
Pali RohĂĄrbce69632022-07-03 12:48:06 +0200289 if (offset < 0 || offset >= 4096)
290 return -EINVAL;
Simon Glassb94dc892015-03-05 12:25:25 -0700291 return ops->write_config(bus, bdf, offset, value, size);
292}
293
Simon Glass9cec2df2016-03-06 19:27:52 -0700294int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
295 u32 clr, u32 set)
296{
297 ulong val;
298 int ret;
299
300 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
301 if (ret)
302 return ret;
303 val &= ~clr;
304 val |= set;
305
306 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
307}
308
Vladimir Oltean278a5b52021-09-17 15:11:25 +0300309static int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
310 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -0700311{
312 struct udevice *bus;
313 int ret;
314
Simon Glass7d07e592015-08-31 18:55:35 -0600315 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700316 if (ret)
317 return ret;
318
Bin Meng0a721522015-07-19 00:20:04 +0800319 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700320}
321
Simon Glass94ef2422015-08-10 07:05:03 -0600322int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
323 enum pci_size_t size)
324{
325 struct udevice *bus;
326
Bin Meng05bedb12015-09-11 03:24:34 -0700327 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600328 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700329 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
330 size);
Simon Glass94ef2422015-08-10 07:05:03 -0600331}
332
Simon Glassb94dc892015-03-05 12:25:25 -0700333int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
334{
335 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
336}
337
338int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
339{
340 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
341}
342
343int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
344{
345 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
346}
347
Simon Glass94ef2422015-08-10 07:05:03 -0600348int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
349{
350 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
351}
352
353int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
354{
355 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
356}
357
358int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
359{
360 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
361}
362
Simon Glassc92aac12020-01-27 08:49:38 -0700363int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassb94dc892015-03-05 12:25:25 -0700364 unsigned long *valuep, enum pci_size_t size)
365{
366 struct dm_pci_ops *ops;
367
368 ops = pci_get_ops(bus);
Pali RohĂĄrbce69632022-07-03 12:48:06 +0200369 if (!ops->read_config) {
370 *valuep = pci_conv_32_to_size(~0, offset, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700371 return -ENOSYS;
Pali RohĂĄrbce69632022-07-03 12:48:06 +0200372 }
373 if (offset < 0 || offset >= 4096) {
374 *valuep = pci_conv_32_to_size(0, offset, size);
375 return -EINVAL;
376 }
Simon Glassb94dc892015-03-05 12:25:25 -0700377 return ops->read_config(bus, bdf, offset, valuep, size);
378}
379
Vladimir Oltean69c5f8a2021-09-17 15:11:26 +0300380static int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
381 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -0700382{
383 struct udevice *bus;
384 int ret;
385
Simon Glass7d07e592015-08-31 18:55:35 -0600386 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700387 if (ret)
388 return ret;
389
Bin Meng0a721522015-07-19 00:20:04 +0800390 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700391}
392
Simon Glassc92aac12020-01-27 08:49:38 -0700393int dm_pci_read_config(const struct udevice *dev, int offset,
394 unsigned long *valuep, enum pci_size_t size)
Simon Glass94ef2422015-08-10 07:05:03 -0600395{
Simon Glassc92aac12020-01-27 08:49:38 -0700396 const struct udevice *bus;
Simon Glass94ef2422015-08-10 07:05:03 -0600397
Bin Meng05bedb12015-09-11 03:24:34 -0700398 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600399 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700400 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass94ef2422015-08-10 07:05:03 -0600401 size);
402}
403
Simon Glassb94dc892015-03-05 12:25:25 -0700404int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
405{
406 unsigned long value;
407 int ret;
408
409 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
410 if (ret)
411 return ret;
412 *valuep = value;
413
414 return 0;
415}
416
417int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
418{
419 unsigned long value;
420 int ret;
421
422 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
423 if (ret)
424 return ret;
425 *valuep = value;
426
427 return 0;
428}
429
430int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
431{
432 unsigned long value;
433 int ret;
434
435 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
436 if (ret)
437 return ret;
438 *valuep = value;
439
440 return 0;
441}
442
Simon Glassc92aac12020-01-27 08:49:38 -0700443int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600444{
445 unsigned long value;
446 int ret;
447
448 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
449 if (ret)
450 return ret;
451 *valuep = value;
452
453 return 0;
454}
455
Simon Glassc92aac12020-01-27 08:49:38 -0700456int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600457{
458 unsigned long value;
459 int ret;
460
461 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
462 if (ret)
463 return ret;
464 *valuep = value;
465
466 return 0;
467}
468
Simon Glassc92aac12020-01-27 08:49:38 -0700469int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600470{
471 unsigned long value;
472 int ret;
473
474 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
475 if (ret)
476 return ret;
477 *valuep = value;
478
479 return 0;
480}
481
Simon Glass9cec2df2016-03-06 19:27:52 -0700482int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
483{
484 u8 val;
485 int ret;
486
487 ret = dm_pci_read_config8(dev, offset, &val);
488 if (ret)
489 return ret;
490 val &= ~clr;
491 val |= set;
492
493 return dm_pci_write_config8(dev, offset, val);
494}
495
496int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
497{
498 u16 val;
499 int ret;
500
501 ret = dm_pci_read_config16(dev, offset, &val);
502 if (ret)
503 return ret;
504 val &= ~clr;
505 val |= set;
506
507 return dm_pci_write_config16(dev, offset, val);
508}
509
510int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
511{
512 u32 val;
513 int ret;
514
515 ret = dm_pci_read_config32(dev, offset, &val);
516 if (ret)
517 return ret;
518 val &= ~clr;
519 val |= set;
520
521 return dm_pci_write_config32(dev, offset, val);
522}
523
Bin Menga0705782015-10-01 00:36:02 -0700524static void set_vga_bridge_bits(struct udevice *dev)
525{
526 struct udevice *parent = dev->parent;
527 u16 bc;
528
Simon Glass75e534b2020-12-16 21:20:07 -0700529 while (dev_seq(parent) != 0) {
Bin Menga0705782015-10-01 00:36:02 -0700530 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
531 bc |= PCI_BRIDGE_CTL_VGA;
532 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
533 parent = parent->parent;
534 }
535}
536
Simon Glassb94dc892015-03-05 12:25:25 -0700537int pci_auto_config_devices(struct udevice *bus)
538{
Simon Glass95588622020-12-22 19:30:28 -0700539 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700540 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700541 unsigned int sub_bus;
542 struct udevice *dev;
543 int ret;
544
Simon Glass75e534b2020-12-16 21:20:07 -0700545 sub_bus = dev_seq(bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700546 debug("%s: start\n", __func__);
547 pciauto_config_init(hose);
548 for (ret = device_find_first_child(bus, &dev);
549 !ret && dev;
550 ret = device_find_next_child(&dev)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700551 unsigned int max_bus;
Simon Glassb072d522015-09-08 17:52:47 -0600552 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700553
Simon Glassb94dc892015-03-05 12:25:25 -0700554 debug("%s: device %s\n", __func__, dev->name);
Simon Glassf1d50f72020-12-19 10:40:13 -0700555 if (dev_has_ofnode(dev) &&
Suneel Garapatif8c86282020-05-04 21:25:25 -0700556 dev_read_bool(dev, "pci,no-autoconfig"))
Simon Glassf3005fb2020-04-08 16:57:26 -0600557 continue;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700558 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600559 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -0700560 return log_msg_ret("auto", ret);
Simon Glassb072d522015-09-08 17:52:47 -0600561 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700562 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700563
Masami Hiramatsu7ccdc672021-06-04 18:43:34 +0900564 if (dev_get_parent(dev) == bus)
565 continue;
566
Simon Glass71fa5b42020-12-03 16:55:18 -0700567 pplat = dev_get_parent_plat(dev);
Bin Menga0705782015-10-01 00:36:02 -0700568 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
569 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700570 }
Pali RohĂĄr8b79e082022-01-17 16:38:37 +0100571 if (hose->last_busno < sub_bus)
572 hose->last_busno = sub_bus;
Simon Glassb94dc892015-03-05 12:25:25 -0700573 debug("%s: done\n", __func__);
574
Simon Glassbe706102020-12-16 21:20:18 -0700575 return log_msg_ret("sub", sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700576}
577
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300578int pci_generic_mmap_write_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700579 const struct udevice *bus,
580 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
581 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300582 pci_dev_t bdf,
583 uint offset,
584 ulong value,
585 enum pci_size_t size)
586{
587 void *address;
588
589 if (addr_f(bus, bdf, offset, &address) < 0)
590 return 0;
591
592 switch (size) {
593 case PCI_SIZE_8:
594 writeb(value, address);
595 return 0;
596 case PCI_SIZE_16:
597 writew(value, address);
598 return 0;
599 case PCI_SIZE_32:
600 writel(value, address);
601 return 0;
602 default:
603 return -EINVAL;
604 }
605}
606
607int pci_generic_mmap_read_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700608 const struct udevice *bus,
609 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
610 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300611 pci_dev_t bdf,
612 uint offset,
613 ulong *valuep,
614 enum pci_size_t size)
615{
616 void *address;
617
618 if (addr_f(bus, bdf, offset, &address) < 0) {
619 *valuep = pci_get_ff(size);
620 return 0;
621 }
622
623 switch (size) {
624 case PCI_SIZE_8:
625 *valuep = readb(address);
626 return 0;
627 case PCI_SIZE_16:
628 *valuep = readw(address);
629 return 0;
630 case PCI_SIZE_32:
631 *valuep = readl(address);
632 return 0;
633 default:
634 return -EINVAL;
635 }
636}
637
Simon Glass37a3f94b2015-11-29 13:17:49 -0700638int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700639{
Pali RohĂĄr4fb65992021-10-07 14:50:58 +0200640 u8 header_type;
Simon Glassb94dc892015-03-05 12:25:25 -0700641 int sub_bus;
642 int ret;
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700643 int ea_pos;
644 u8 reg;
Simon Glassb94dc892015-03-05 12:25:25 -0700645
646 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700647
Pali RohĂĄr4fb65992021-10-07 14:50:58 +0200648 dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);
649 header_type &= 0x7f;
650 if (header_type != PCI_HEADER_TYPE_BRIDGE) {
651 debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n",
652 __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type);
653 return log_msg_ret("probe", -EINVAL);
654 }
655
Andrew Scull71e7e1a2022-04-21 16:11:16 +0000656 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
657 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
658 else
659 ea_pos = 0;
660
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700661 if (ea_pos) {
662 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
663 &reg);
664 sub_bus = reg;
665 } else {
666 sub_bus = pci_get_bus_max() + 1;
667 }
Simon Glassb94dc892015-03-05 12:25:25 -0700668 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700669 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700670
671 ret = device_probe(bus);
672 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600673 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700674 ret);
Simon Glassbe706102020-12-16 21:20:18 -0700675 return log_msg_ret("probe", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700676 }
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700677
Masami Hiramatsuff022452021-04-16 14:53:46 -0700678 if (!ea_pos)
679 sub_bus = pci_get_bus_max();
680
Simon Glass37a3f94b2015-11-29 13:17:49 -0700681 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700682
683 return sub_bus;
684}
685
Simon Glass318d71c2015-07-06 16:47:44 -0600686/**
687 * pci_match_one_device - Tell if a PCI device structure has a matching
688 * PCI device id structure
689 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800690 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600691 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800692 * Returns true if the finding pci_device_id structure matched or false if
693 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600694 */
695static bool pci_match_one_id(const struct pci_device_id *id,
696 const struct pci_device_id *find)
697{
698 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
699 (id->device == PCI_ANY_ID || id->device == find->device) &&
700 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
701 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
702 !((id->class ^ find->class) & id->class_mask))
703 return true;
704
705 return false;
706}
707
708/**
Simon Glass8807a562021-06-27 17:50:57 -0600709 * pci_need_device_pre_reloc() - Check if a device should be bound
710 *
711 * This checks a list of vendor/device-ID values indicating devices that should
712 * be bound before relocation.
713 *
714 * @bus: Bus to check
715 * @vendor: Vendor ID to check
716 * @device: Device ID to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100717 * Return: true if the vendor/device is in the list, false if not
Simon Glass8807a562021-06-27 17:50:57 -0600718 */
719static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor,
720 uint device)
721{
722 u32 vendev;
723 int index;
724
725 for (index = 0;
726 !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index,
727 &vendev);
728 index++) {
729 if (vendev == PCI_VENDEV(vendor, device))
730 return true;
731 }
732
733 return false;
734}
735
736/**
Simon Glass318d71c2015-07-06 16:47:44 -0600737 * pci_find_and_bind_driver() - Find and bind the right PCI driver
738 *
739 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600740 *
741 * @parent: Parent bus
742 * @find_id: Specification of the driver to find
743 * @bdf: Bus/device/function addreess - see PCI_BDF()
744 * @devp: Returns a pointer to the device created
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100745 * Return: 0 if OK, -EPERM if the device is not needed before relocation and
Simon Glassc45abf12015-09-08 17:52:49 -0600746 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600747 */
748static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600749 struct pci_device_id *find_id,
750 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600751{
752 struct pci_driver_entry *start, *entry;
Marek Vasutb4535792018-10-10 21:27:06 +0200753 ofnode node = ofnode_null();
Simon Glass318d71c2015-07-06 16:47:44 -0600754 const char *drv;
755 int n_ents;
756 int ret;
757 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700758 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600759
760 *devp = NULL;
761
762 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
763 find_id->vendor, find_id->device);
Marek Vasutb4535792018-10-10 21:27:06 +0200764
765 /* Determine optional OF node */
Suneel Garapaticb7093d2019-10-19 16:02:48 -0700766 if (ofnode_valid(dev_ofnode(parent)))
767 pci_dev_find_ofnode(parent, bdf, &node);
Marek Vasutb4535792018-10-10 21:27:06 +0200768
Michael Walle2e21f372019-12-01 17:45:18 +0100769 if (ofnode_valid(node) && !ofnode_is_available(node)) {
770 debug("%s: Ignoring disabled device\n", __func__);
Simon Glassbe706102020-12-16 21:20:18 -0700771 return log_msg_ret("dis", -EPERM);
Michael Walle2e21f372019-12-01 17:45:18 +0100772 }
773
Simon Glass318d71c2015-07-06 16:47:44 -0600774 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
775 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
776 for (entry = start; entry != start + n_ents; entry++) {
777 const struct pci_device_id *id;
778 struct udevice *dev;
779 const struct driver *drv;
780
781 for (id = entry->match;
782 id->vendor || id->subvendor || id->class_mask;
783 id++) {
784 if (!pci_match_one_id(id, find_id))
785 continue;
786
787 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700788
789 /*
790 * In the pre-relocation phase, we only bind devices
791 * whose driver has the DM_FLAG_PRE_RELOC set, to save
792 * precious memory space as on some platforms as that
793 * space is pretty limited (ie: using Cache As RAM).
794 */
795 if (!(gd->flags & GD_FLG_RELOC) &&
796 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassbe706102020-12-16 21:20:18 -0700797 return log_msg_ret("pre", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700798
Simon Glass318d71c2015-07-06 16:47:44 -0600799 /*
800 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700801 * plat (instead of NULL) and allow its bind()
Simon Glass318d71c2015-07-06 16:47:44 -0600802 * method to return -ENOENT if it doesn't support this
803 * device. That way we could continue the search to
804 * find another driver. For now this doesn't seem
805 * necesssary, so just bind the first match.
806 */
Simon Glass884870f2020-11-28 17:50:01 -0700807 ret = device_bind(parent, drv, drv->name, NULL, node,
808 &dev);
Simon Glass318d71c2015-07-06 16:47:44 -0600809 if (ret)
810 goto error;
811 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700812 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600813 *devp = dev;
814 return 0;
815 }
816 }
817
Bin Meng984c0dc2015-08-20 06:40:17 -0700818 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
819 /*
820 * In the pre-relocation phase, we only bind bridge devices to save
821 * precious memory space as on some platforms as that space is pretty
822 * limited (ie: using Cache As RAM).
823 */
Simon Glass8807a562021-06-27 17:50:57 -0600824 if (!(gd->flags & GD_FLG_RELOC) && !bridge &&
825 !pci_need_device_pre_reloc(parent, find_id->vendor,
826 find_id->device))
Simon Glassbe706102020-12-16 21:20:18 -0700827 return log_msg_ret("notbr", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700828
Simon Glass318d71c2015-07-06 16:47:44 -0600829 /* Bind a generic driver so that the device can be used */
Simon Glass75e534b2020-12-16 21:20:07 -0700830 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
Bin Meng0a721522015-07-19 00:20:04 +0800831 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600832 str = strdup(name);
833 if (!str)
834 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700835 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
836
Marek Vasutb4535792018-10-10 21:27:06 +0200837 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glass318d71c2015-07-06 16:47:44 -0600838 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600839 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200840 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600841 return ret;
842 }
843 debug("%s: No match found: bound generic driver instead\n", __func__);
844
845 return 0;
846
847error:
848 debug("%s: No match found: error %d\n", __func__, ret);
849 return ret;
850}
851
Tim Harvey4c57bf72021-04-16 14:53:47 -0700852__weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
853{
854}
855
Simon Glassb94dc892015-03-05 12:25:25 -0700856int pci_bind_bus_devices(struct udevice *bus)
857{
858 ulong vendor, device;
859 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800860 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700861 bool found_multi;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700862 int ari_off;
Simon Glassb94dc892015-03-05 12:25:25 -0700863 int ret;
864
865 found_multi = false;
Simon Glass75e534b2020-12-16 21:20:07 -0700866 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
Bin Meng0a721522015-07-19 00:20:04 +0800867 PCI_MAX_PCI_FUNCTIONS - 1);
Simon Glass75e534b2020-12-16 21:20:07 -0700868 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800869 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700870 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700871 struct udevice *dev;
872 ulong class;
873
Bin Meng20bdc1e2018-08-03 01:14:37 -0700874 if (!PCI_FUNC(bdf))
875 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800876 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700877 continue;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800878
Simon Glassb94dc892015-03-05 12:25:25 -0700879 /* Check only the first access, we don't expect problems */
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800880 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
881 PCI_SIZE_16);
Pali RohĂĄra8a520d2021-09-07 18:07:08 +0200882 if (ret || vendor == 0xffff || vendor == 0x0000)
Simon Glassb94dc892015-03-05 12:25:25 -0700883 continue;
884
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800885 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
886 &header_type, PCI_SIZE_8);
887
Bin Meng0a721522015-07-19 00:20:04 +0800888 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700889 found_multi = header_type & 0x80;
890
Simon Glass25916d62019-09-25 08:56:12 -0600891 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -0700892 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Bin Meng0a721522015-07-19 00:20:04 +0800893 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700894 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800895 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600896 PCI_SIZE_32);
897 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700898
899 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800900 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass25916d62019-09-25 08:56:12 -0600901 debug(": find ret=%d\n", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700902
Simon Glass413ebdb2015-11-29 13:18:09 -0700903 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700904 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600905 struct pci_device_id find_id;
906 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700907
Simon Glass318d71c2015-07-06 16:47:44 -0600908 memset(&find_id, '\0', sizeof(find_id));
909 find_id.vendor = vendor;
910 find_id.device = device;
911 find_id.class = class;
912 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800913 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600914 PCI_SUBSYSTEM_VENDOR_ID,
915 &val, PCI_SIZE_32);
916 find_id.subvendor = val & 0xffff;
917 find_id.subdevice = val >> 16;
918 }
Bin Meng0a721522015-07-19 00:20:04 +0800919 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600920 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700921 }
Simon Glassc45abf12015-09-08 17:52:49 -0600922 if (ret == -EPERM)
923 continue;
924 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700925 return ret;
926
927 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -0700928 pplat = dev_get_parent_plat(dev);
Simon Glassc45abf12015-09-08 17:52:49 -0600929 pplat->devfn = PCI_MASK_BUS(bdf);
930 pplat->vendor = vendor;
931 pplat->device = device;
932 pplat->class = class;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700933
934 if (IS_ENABLED(CONFIG_PCI_ARID)) {
935 ari_off = dm_pci_find_ext_capability(dev,
936 PCI_EXT_CAP_ID_ARI);
937 if (ari_off) {
938 u16 ari_cap;
939
940 /*
941 * Read Next Function number in ARI Cap
942 * Register
943 */
944 dm_pci_read_config16(dev, ari_off + 4,
945 &ari_cap);
946 /*
947 * Update next scan on this function number,
948 * subtract 1 in BDF to satisfy loop increment.
949 */
950 if (ari_cap & 0xff00) {
951 bdf = PCI_BDF(PCI_BUS(bdf),
952 PCI_DEV(ari_cap),
953 PCI_FUNC(ari_cap));
954 bdf = bdf - 0x100;
955 }
956 }
957 }
Tim Harvey4c57bf72021-04-16 14:53:47 -0700958
959 board_pci_fixup_dev(bus, dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700960 }
961
962 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -0700963}
964
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +0100965static int decode_regions(struct pci_controller *hose, ofnode parent_node,
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700966 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700967{
968 int pci_addr_cells, addr_cells, size_cells;
969 int cells_per_record;
Stefan Roesebbc88462020-08-12 11:55:46 +0200970 struct bd_info *bd;
Simon Glassb94dc892015-03-05 12:25:25 -0700971 const u32 *prop;
Stefan Roese950864f2020-07-23 16:34:10 +0200972 int max_regions;
Simon Glassb94dc892015-03-05 12:25:25 -0700973 int len;
974 int i;
975
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900976 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700977 if (!prop) {
978 debug("%s: Cannot decode regions\n", __func__);
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +0100979 return -EINVAL;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700980 }
981
Simon Glass4191dc12017-06-12 06:21:31 -0600982 pci_addr_cells = ofnode_read_simple_addr_cells(node);
983 addr_cells = ofnode_read_simple_addr_cells(parent_node);
984 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700985
986 /* PCI addresses are always 3-cells */
987 len /= sizeof(u32);
988 cells_per_record = pci_addr_cells + addr_cells + size_cells;
989 hose->region_count = 0;
990 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
991 cells_per_record);
Stefan Roese950864f2020-07-23 16:34:10 +0200992
993 /* Dynamically allocate the regions array */
994 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
995 hose->regions = (struct pci_region *)
996 calloc(1, max_regions * sizeof(struct pci_region));
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +0100997 if (!hose->regions)
998 return -ENOMEM;
Stefan Roese950864f2020-07-23 16:34:10 +0200999
1000 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassb94dc892015-03-05 12:25:25 -07001001 u64 pci_addr, addr, size;
1002 int space_code;
1003 u32 flags;
1004 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -07001005 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -07001006
1007 if (len < cells_per_record)
1008 break;
1009 flags = fdt32_to_cpu(prop[0]);
1010 space_code = (flags >> 24) & 3;
1011 pci_addr = fdtdec_get_number(prop + 1, 2);
1012 prop += pci_addr_cells;
1013 addr = fdtdec_get_number(prop, addr_cells);
1014 prop += addr_cells;
1015 size = fdtdec_get_number(prop, size_cells);
1016 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001017 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
1018 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -07001019 if (space_code & 2) {
1020 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
1021 PCI_REGION_MEM;
1022 } else if (space_code & 1) {
1023 type = PCI_REGION_IO;
1024 } else {
1025 continue;
1026 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +03001027
1028 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
1029 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
Andrew Scullcb06f0e2022-04-21 16:11:07 +00001030 debug(" - pci_addr beyond the 32-bit boundary, ignoring\n");
1031 continue;
1032 }
1033
1034 if (!IS_ENABLED(CONFIG_PHYS_64BIT) && upper_32_bits(addr)) {
1035 debug(" - addr beyond the 32-bit boundary, ignoring\n");
1036 continue;
1037 }
1038
1039 if (~((pci_addr_t)0) - pci_addr < size) {
1040 debug(" - PCI range exceeds max address, ignoring\n");
1041 continue;
1042 }
1043
1044 if (~((phys_addr_t)0) - addr < size) {
1045 debug(" - phys range exceeds max address, ignoring\n");
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +03001046 continue;
1047 }
1048
Simon Glass7efc9ba2015-11-19 20:26:58 -07001049 pos = -1;
Suneel Garapati3ac3aec2019-10-19 17:10:20 -07001050 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
1051 for (i = 0; i < hose->region_count; i++) {
1052 if (hose->regions[i].flags == type)
1053 pos = i;
1054 }
Simon Glass7efc9ba2015-11-19 20:26:58 -07001055 }
Suneel Garapati3ac3aec2019-10-19 17:10:20 -07001056
Simon Glass7efc9ba2015-11-19 20:26:58 -07001057 if (pos == -1)
1058 pos = hose->region_count++;
1059 debug(" - type=%d, pos=%d\n", type, pos);
1060 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -07001061 }
1062
1063 /* Add a region for our local memory */
Stefan Roesebbc88462020-08-12 11:55:46 +02001064 bd = gd->bd;
Bin Mengae0bdde2018-03-27 00:46:05 -07001065 if (!bd)
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001066 return 0;
Bin Mengae0bdde2018-03-27 00:46:05 -07001067
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001068 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1069 if (bd->bi_dram[i].size) {
Daniel Schwierzeckf59925e2021-07-15 20:53:56 +02001070 phys_addr_t start = bd->bi_dram[i].start;
1071
1072 if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY))
1073 start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start);
1074
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001075 pci_set_region(hose->regions + hose->region_count++,
Daniel Schwierzeckf59925e2021-07-15 20:53:56 +02001076 start, start, bd->bi_dram[i].size,
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001077 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1078 }
1079 }
Simon Glassb94dc892015-03-05 12:25:25 -07001080
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001081 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001082}
1083
1084static int pci_uclass_pre_probe(struct udevice *bus)
1085{
1086 struct pci_controller *hose;
Simon Glassbe706102020-12-16 21:20:18 -07001087 struct uclass *uc;
1088 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001089
Simon Glass75e534b2020-12-16 21:20:07 -07001090 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -07001091 bus->parent->name);
Simon Glass95588622020-12-22 19:30:28 -07001092 hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001093
Simon Glassbe706102020-12-16 21:20:18 -07001094 /*
1095 * Set the sequence number, if device_bind() doesn't. We want control
1096 * of this so that numbers are allocated as devices are probed. That
1097 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1098 * higher than their parents)
1099 */
1100 if (dev_seq(bus) == -1) {
1101 ret = uclass_get(UCLASS_PCI, &uc);
1102 if (ret)
1103 return ret;
Simon Glass5e349922020-12-19 10:40:09 -07001104 bus->seq_ = uclass_find_next_free_seq(uc);
Simon Glassbe706102020-12-16 21:20:18 -07001105 }
1106
Simon Glassb94dc892015-03-05 12:25:25 -07001107 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +01001108 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -07001109 hose->ctlr = bus;
Pierre-Clément Tosic84d8302022-05-19 17:48:30 +01001110 ret = decode_regions(hose, dev_ofnode(bus->parent),
1111 dev_ofnode(bus));
1112 if (ret)
1113 return ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001114 } else {
1115 struct pci_controller *parent_hose;
1116
1117 parent_hose = dev_get_uclass_priv(bus->parent);
1118 hose->ctlr = parent_hose->bus;
1119 }
Simon Glassbe706102020-12-16 21:20:18 -07001120
Simon Glassb94dc892015-03-05 12:25:25 -07001121 hose->bus = bus;
Simon Glass75e534b2020-12-16 21:20:07 -07001122 hose->first_busno = dev_seq(bus);
1123 hose->last_busno = dev_seq(bus);
Simon Glassf1d50f72020-12-19 10:40:13 -07001124 if (dev_has_ofnode(bus)) {
Suneel Garapatif8c86282020-05-04 21:25:25 -07001125 hose->skip_auto_config_until_reloc =
1126 dev_read_bool(bus,
1127 "u-boot,skip-auto-config-until-reloc");
1128 }
Simon Glassb94dc892015-03-05 12:25:25 -07001129
1130 return 0;
1131}
1132
1133static int pci_uclass_post_probe(struct udevice *bus)
1134{
Simon Glass68e35a72019-12-06 21:41:37 -07001135 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001136 int ret;
1137
Simon Glass75e534b2020-12-16 21:20:07 -07001138 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001139 ret = pci_bind_bus_devices(bus);
1140 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001141 return log_msg_ret("bind", ret);
Simon Glassb94dc892015-03-05 12:25:25 -07001142
Simon Glassbd165e72020-04-26 09:12:56 -06001143 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass68e35a72019-12-06 21:41:37 -07001144 (!hose->skip_auto_config_until_reloc ||
1145 (gd->flags & GD_FLG_RELOC))) {
1146 ret = pci_auto_config_devices(bus);
1147 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -07001148 return log_msg_ret("cfg", ret);
Simon Glass68e35a72019-12-06 21:41:37 -07001149 }
Simon Glassb94dc892015-03-05 12:25:25 -07001150
Bin Mengc0820a42015-08-20 06:40:23 -07001151#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1152 /*
1153 * Per Intel FSP specification, we should call FSP notify API to
1154 * inform FSP that PCI enumeration has been done so that FSP will
1155 * do any necessary initialization as required by the chipset's
1156 * BIOS Writer's Guide (BWG).
1157 *
1158 * Unfortunately we have to put this call here as with driver model,
1159 * the enumeration is all done on a lazy basis as needed, so until
1160 * something is touched on PCI it won't happen.
1161 *
1162 * Note we only call this 1) after U-Boot is relocated, and 2)
1163 * root bus has finished probing.
1164 */
Simon Glass75e534b2020-12-16 21:20:07 -07001165 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
Bin Mengc0820a42015-08-20 06:40:23 -07001166 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -06001167 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001168 return log_msg_ret("fsp", ret);
Simon Glassb072d522015-09-08 17:52:47 -06001169 }
Bin Mengc0820a42015-08-20 06:40:23 -07001170#endif
1171
Simon Glassb072d522015-09-08 17:52:47 -06001172 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001173}
1174
1175static int pci_uclass_child_post_bind(struct udevice *dev)
1176{
Simon Glassb75b15b2020-12-03 16:55:23 -07001177 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -07001178
Simon Glassf1d50f72020-12-19 10:40:13 -07001179 if (!dev_has_ofnode(dev))
Simon Glassb94dc892015-03-05 12:25:25 -07001180 return 0;
1181
Simon Glass71fa5b42020-12-03 16:55:18 -07001182 pplat = dev_get_parent_plat(dev);
Bin Meng00d808e2018-08-03 01:14:36 -07001183
1184 /* Extract vendor id and device id if available */
1185 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1186
1187 /* Extract the devfn from fdt_pci_addr */
Stefan Roesea74eb552019-01-25 11:52:42 +01001188 pplat->devfn = pci_get_devfn(dev);
Simon Glassb94dc892015-03-05 12:25:25 -07001189
1190 return 0;
1191}
1192
Simon Glass2a311e82020-01-27 08:49:37 -07001193static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng0a721522015-07-19 00:20:04 +08001194 uint offset, ulong *valuep,
1195 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001196{
Simon Glass95588622020-12-22 19:30:28 -07001197 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001198
1199 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1200}
1201
Bin Meng0a721522015-07-19 00:20:04 +08001202static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1203 uint offset, ulong value,
1204 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001205{
Simon Glass95588622020-12-22 19:30:28 -07001206 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001207
1208 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1209}
1210
Simon Glass04c8b6a2015-08-10 07:05:04 -06001211static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1212{
1213 struct udevice *dev;
1214 int ret = 0;
1215
1216 /*
1217 * Scan through all the PCI controllers. On x86 there will only be one
1218 * but that is not necessarily true on other hardware.
1219 */
1220 do {
1221 device_find_first_child(bus, &dev);
1222 if (dev) {
1223 *devp = dev;
1224 return 0;
1225 }
1226 ret = uclass_next_device(&bus);
1227 if (ret)
1228 return ret;
1229 } while (bus);
1230
1231 return 0;
1232}
1233
1234int pci_find_next_device(struct udevice **devp)
1235{
1236 struct udevice *child = *devp;
1237 struct udevice *bus = child->parent;
1238 int ret;
1239
1240 /* First try all the siblings */
1241 *devp = NULL;
1242 while (child) {
1243 device_find_next_child(&child);
1244 if (child) {
1245 *devp = child;
1246 return 0;
1247 }
1248 }
1249
1250 /* We ran out of siblings. Try the next bus */
1251 ret = uclass_next_device(&bus);
1252 if (ret)
1253 return ret;
1254
1255 return bus ? skip_to_next_device(bus, devp) : 0;
1256}
1257
1258int pci_find_first_device(struct udevice **devp)
1259{
1260 struct udevice *bus;
1261 int ret;
1262
1263 *devp = NULL;
1264 ret = uclass_first_device(UCLASS_PCI, &bus);
1265 if (ret)
1266 return ret;
1267
1268 return skip_to_next_device(bus, devp);
1269}
1270
Simon Glass27a733f2015-11-19 20:26:59 -07001271ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1272{
1273 switch (size) {
1274 case PCI_SIZE_8:
1275 return (value >> ((offset & 3) * 8)) & 0xff;
1276 case PCI_SIZE_16:
1277 return (value >> ((offset & 2) * 8)) & 0xffff;
1278 default:
1279 return value;
1280 }
1281}
1282
1283ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1284 enum pci_size_t size)
1285{
1286 uint off_mask;
1287 uint val_mask, shift;
1288 ulong ldata, mask;
1289
1290 switch (size) {
1291 case PCI_SIZE_8:
1292 off_mask = 3;
1293 val_mask = 0xff;
1294 break;
1295 case PCI_SIZE_16:
1296 off_mask = 2;
1297 val_mask = 0xffff;
1298 break;
1299 default:
1300 return value;
1301 }
1302 shift = (offset & off_mask) * 8;
1303 ldata = (value & val_mask) << shift;
1304 mask = val_mask << shift;
1305 value = (old & ~mask) | ldata;
1306
1307 return value;
1308}
1309
Rayagonda Kokatanurcdc7ed32020-05-12 13:29:49 +05301310int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1311{
1312 int pci_addr_cells, addr_cells, size_cells;
1313 int cells_per_record;
1314 const u32 *prop;
1315 int len;
1316 int i = 0;
1317
1318 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1319 if (!prop) {
1320 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1321 dev->name);
1322 return -EINVAL;
1323 }
1324
1325 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1326 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1327 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1328
1329 /* PCI addresses are always 3-cells */
1330 len /= sizeof(u32);
1331 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1332 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1333 cells_per_record);
1334
1335 while (len) {
1336 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1337 prop += pci_addr_cells;
1338 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1339 prop += addr_cells;
1340 memp->size = fdtdec_get_number(prop, size_cells);
1341 prop += size_cells;
1342
1343 if (i == index)
1344 return 0;
1345 i++;
1346 len -= cells_per_record;
1347 }
1348
1349 return -EINVAL;
1350}
1351
Simon Glassdcdc0122015-11-19 20:27:01 -07001352int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1353 struct pci_region **memp, struct pci_region **prefp)
1354{
1355 struct udevice *bus = pci_get_controller(dev);
1356 struct pci_controller *hose = dev_get_uclass_priv(bus);
1357 int i;
1358
1359 *iop = NULL;
1360 *memp = NULL;
1361 *prefp = NULL;
1362 for (i = 0; i < hose->region_count; i++) {
1363 switch (hose->regions[i].flags) {
1364 case PCI_REGION_IO:
1365 if (!*iop || (*iop)->size < hose->regions[i].size)
1366 *iop = hose->regions + i;
1367 break;
1368 case PCI_REGION_MEM:
1369 if (!*memp || (*memp)->size < hose->regions[i].size)
1370 *memp = hose->regions + i;
1371 break;
1372 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1373 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1374 *prefp = hose->regions + i;
1375 break;
1376 }
1377 }
1378
1379 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1380}
1381
Simon Glassc92aac12020-01-27 08:49:38 -07001382u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glass3452cb12015-11-29 13:17:53 -07001383{
1384 u32 addr;
1385 int bar;
1386
1387 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1388 dm_pci_read_config32(dev, bar, &addr);
Simon Glass71fafd12020-04-09 10:27:36 -06001389
1390 /*
1391 * If we get an invalid address, return this so that comparisons with
1392 * FDT_ADDR_T_NONE work correctly
1393 */
1394 if (addr == 0xffffffff)
1395 return addr;
1396 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glass3452cb12015-11-29 13:17:53 -07001397 return addr & PCI_BASE_ADDRESS_IO_MASK;
1398 else
1399 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1400}
1401
Simon Glasse2b6b562016-01-18 20:19:15 -07001402void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1403{
1404 int bar;
1405
1406 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1407 dm_pci_write_config32(dev, bar, addr);
1408}
1409
Andrew Scull994b60d2022-04-21 16:11:11 +00001410phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1411 size_t len, unsigned long mask,
1412 unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001413{
Andrew Scull994b60d2022-04-21 16:11:11 +00001414 struct udevice *ctlr;
1415 struct pci_controller *hose;
Simon Glassc5f053b2015-11-29 13:18:03 -07001416 struct pci_region *res;
Andrew Scull3bf61522022-04-21 16:11:08 +00001417 pci_addr_t offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001418 int i;
1419
Andrew Scull994b60d2022-04-21 16:11:11 +00001420 /* The root controller has the region information */
1421 ctlr = pci_get_controller(dev);
1422 hose = dev_get_uclass_priv(ctlr);
1423
1424 if (hose->region_count == 0)
1425 return bus_addr;
Christian Gmeiner7241f802018-06-10 06:25:06 -07001426
Simon Glassc5f053b2015-11-29 13:18:03 -07001427 for (i = 0; i < hose->region_count; i++) {
1428 res = &hose->regions[i];
1429
Andrew Scull994b60d2022-04-21 16:11:11 +00001430 if ((res->flags & mask) != flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001431 continue;
1432
Andrew Scull3bf61522022-04-21 16:11:08 +00001433 if (bus_addr < res->bus_start)
1434 continue;
1435
1436 offset = bus_addr - res->bus_start;
1437 if (offset >= res->size)
1438 continue;
1439
1440 if (len > res->size - offset)
1441 continue;
1442
Andrew Scull994b60d2022-04-21 16:11:11 +00001443 return res->phys_start + offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001444 }
1445
Andrew Scull994b60d2022-04-21 16:11:11 +00001446 puts("pci_hose_bus_to_phys: invalid physical address\n");
1447 return 0;
Simon Glassc5f053b2015-11-29 13:18:03 -07001448}
1449
Andrew Scull994b60d2022-04-21 16:11:11 +00001450pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1451 size_t len, unsigned long mask,
1452 unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001453{
Simon Glassc5f053b2015-11-29 13:18:03 -07001454 struct udevice *ctlr;
Andrew Scull994b60d2022-04-21 16:11:11 +00001455 struct pci_controller *hose;
Simon Glassc5f053b2015-11-29 13:18:03 -07001456 struct pci_region *res;
Andrew Scull3bf61522022-04-21 16:11:08 +00001457 phys_addr_t offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001458 int i;
Simon Glassc5f053b2015-11-29 13:18:03 -07001459
1460 /* The root controller has the region information */
1461 ctlr = pci_get_controller(dev);
1462 hose = dev_get_uclass_priv(ctlr);
1463
Andrew Scull994b60d2022-04-21 16:11:11 +00001464 if (hose->region_count == 0)
1465 return phys_addr;
Christian Gmeiner7241f802018-06-10 06:25:06 -07001466
Simon Glassc5f053b2015-11-29 13:18:03 -07001467 for (i = 0; i < hose->region_count; i++) {
1468 res = &hose->regions[i];
1469
Andrew Scull994b60d2022-04-21 16:11:11 +00001470 if ((res->flags & mask) != flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001471 continue;
1472
Andrew Scull3bf61522022-04-21 16:11:08 +00001473 if (phys_addr < res->phys_start)
1474 continue;
1475
1476 offset = phys_addr - res->phys_start;
1477 if (offset >= res->size)
1478 continue;
Simon Glassc5f053b2015-11-29 13:18:03 -07001479
Andrew Scull3bf61522022-04-21 16:11:08 +00001480 if (len > res->size - offset)
1481 continue;
1482
Andrew Scull994b60d2022-04-21 16:11:11 +00001483 return res->bus_start + offset;
Simon Glassc5f053b2015-11-29 13:18:03 -07001484 }
1485
Andrew Scull994b60d2022-04-21 16:11:11 +00001486 puts("pci_hose_phys_to_bus: invalid physical address\n");
1487 return 0;
Simon Glassc5f053b2015-11-29 13:18:03 -07001488}
1489
Suneel Garapati5858ba82019-10-19 16:34:16 -07001490static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
Simon Glassb75b15b2020-12-03 16:55:23 -07001491 struct pci_child_plat *pdata)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001492{
1493 phys_addr_t addr = 0;
1494
1495 /*
1496 * In the case of a Virtual Function device using BAR
1497 * base and size, add offset for VFn BAR(1, 2, 3...n)
1498 */
1499 if (pdata->is_virtfn) {
1500 size_t sz;
1501 u32 ea_entry;
1502
1503 /* MaxOffset, 1st DW */
1504 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1505 sz = ea_entry & PCI_EA_FIELD_MASK;
1506 /* Fill up lower 2 bits */
1507 sz |= (~PCI_EA_FIELD_MASK);
1508
1509 if (ea_entry & PCI_EA_IS_64) {
1510 /* MaxOffset 2nd DW */
1511 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1512 sz |= ((u64)ea_entry) << 32;
1513 }
1514
1515 addr = (pdata->virtid - 1) * (sz + 1);
1516 }
1517
1518 return addr;
1519}
1520
Andrew Scull58c61022022-04-21 16:11:10 +00001521static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, size_t offset,
1522 size_t len, int ea_off,
Andrew Scull30d338d2022-04-21 16:11:06 +00001523 struct pci_child_plat *pdata)
Alex Marginean1c934a62019-06-07 11:24:23 +03001524{
1525 int ea_cnt, i, entry_size;
1526 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1527 u32 ea_entry;
1528 phys_addr_t addr;
1529
Suneel Garapati5858ba82019-10-19 16:34:16 -07001530 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1531 /*
1532 * In the case of a Virtual Function device, device is
1533 * Physical function, so pdata will point to required VF
1534 * specific data.
1535 */
1536 if (pdata->is_virtfn)
1537 bar_id += PCI_EA_BEI_VF_BAR0;
1538 }
1539
Alex Marginean1c934a62019-06-07 11:24:23 +03001540 /* EA capability structure header */
1541 dm_pci_read_config32(dev, ea_off, &ea_entry);
1542 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1543 ea_off += PCI_EA_FIRST_ENT;
1544
1545 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1546 /* Entry header */
1547 dm_pci_read_config32(dev, ea_off, &ea_entry);
1548 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1549
1550 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1551 continue;
1552
1553 /* Base address, 1st DW */
1554 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1555 addr = ea_entry & PCI_EA_FIELD_MASK;
1556 if (ea_entry & PCI_EA_IS_64) {
1557 /* Base address, 2nd DW, skip over 4B MaxOffset */
1558 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1559 addr |= ((u64)ea_entry) << 32;
1560 }
1561
Suneel Garapati5858ba82019-10-19 16:34:16 -07001562 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1563 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1564
Andrew Scull58c61022022-04-21 16:11:10 +00001565 if (~((phys_addr_t)0) - addr < offset)
1566 return NULL;
1567
Alex Marginean1c934a62019-06-07 11:24:23 +03001568 /* size ignored for now */
Andrew Scull58c61022022-04-21 16:11:10 +00001569 return map_physmem(addr + offset, len, MAP_NOCACHE);
Alex Marginean1c934a62019-06-07 11:24:23 +03001570 }
1571
1572 return 0;
1573}
1574
Andrew Scull58c61022022-04-21 16:11:10 +00001575void *dm_pci_map_bar(struct udevice *dev, int bar, size_t offset, size_t len,
Andrew Scull6520c822022-04-21 16:11:13 +00001576 unsigned long mask, unsigned long flags)
Simon Glassc5f053b2015-11-29 13:18:03 -07001577{
Simon Glassb75b15b2020-12-03 16:55:23 -07001578 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
Suneel Garapati5858ba82019-10-19 16:34:16 -07001579 struct udevice *udev = dev;
Simon Glassc5f053b2015-11-29 13:18:03 -07001580 pci_addr_t pci_bus_addr;
1581 u32 bar_response;
Alex Marginean1c934a62019-06-07 11:24:23 +03001582 int ea_off;
1583
Suneel Garapati5858ba82019-10-19 16:34:16 -07001584 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1585 /*
1586 * In case of Virtual Function devices, use PF udevice
1587 * as EA capability is defined in Physical Function
1588 */
1589 if (pdata->is_virtfn)
1590 udev = pdata->pfdev;
1591 }
1592
Alex Marginean1c934a62019-06-07 11:24:23 +03001593 /*
1594 * if the function supports Enhanced Allocation use that instead of
1595 * BARs
Suneel Garapati5858ba82019-10-19 16:34:16 -07001596 * Incase of virtual functions, pdata will help read VF BEI
1597 * and EA entry size.
Alex Marginean1c934a62019-06-07 11:24:23 +03001598 */
Andrew Scull71e7e1a2022-04-21 16:11:16 +00001599 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
1600 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
1601 else
1602 ea_off = 0;
1603
Alex Marginean1c934a62019-06-07 11:24:23 +03001604 if (ea_off)
Andrew Scull58c61022022-04-21 16:11:10 +00001605 return dm_pci_map_ea_bar(udev, bar, offset, len, ea_off, pdata);
Simon Glassc5f053b2015-11-29 13:18:03 -07001606
1607 /* read BAR address */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001608 dm_pci_read_config32(udev, bar, &bar_response);
Simon Glassc5f053b2015-11-29 13:18:03 -07001609 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1610
Andrew Scull58c61022022-04-21 16:11:10 +00001611 if (~((pci_addr_t)0) - pci_bus_addr < offset)
1612 return NULL;
1613
Simon Glassc5f053b2015-11-29 13:18:03 -07001614 /*
Andrew Scull58c61022022-04-21 16:11:10 +00001615 * Forward the length argument to dm_pci_bus_to_virt. The length will
1616 * be used to check that the entire address range has been declared as
1617 * a PCI range, but a better check would be to probe for the size of
1618 * the bar and prevent overflow more locally.
Simon Glassc5f053b2015-11-29 13:18:03 -07001619 */
Andrew Scull6520c822022-04-21 16:11:13 +00001620 return dm_pci_bus_to_virt(udev, pci_bus_addr + offset, len, mask, flags,
1621 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}