blob: cb9aa8183591d3b4b725332263eb71573ea4a59b [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 Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Simon Glass37a3f94b2015-11-29 13:17:49 -070025#include "pci_internal.h"
Simon Glassb94dc892015-03-05 12:25:25 -070026
27DECLARE_GLOBAL_DATA_PTR;
28
Simon Glass2e4e4432016-01-18 20:19:14 -070029int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass7d07e592015-08-31 18:55:35 -060030{
31 int ret;
32
33 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
34
35 /* Since buses may not be numbered yet try a little harder with bus 0 */
36 if (ret == -ENODEV) {
Simon Glassc7298e72016-02-11 13:23:26 -070037 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass7d07e592015-08-31 18:55:35 -060038 if (ret)
39 return ret;
Simon Glass7d07e592015-08-31 18:55:35 -060040 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
41 }
42
43 return ret;
44}
45
Simon Glass6256d672015-11-19 20:27:00 -070046struct udevice *pci_get_controller(struct udevice *dev)
47{
48 while (device_is_on_pci_bus(dev))
49 dev = dev->parent;
50
51 return dev;
52}
53
Simon Glassc92aac12020-01-27 08:49:38 -070054pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glassc9118d42015-07-06 16:47:46 -060055{
Simon Glassb75b15b2020-12-03 16:55:23 -070056 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassc9118d42015-07-06 16:47:46 -060057 struct udevice *bus = dev->parent;
58
Simon Glass1c6449c2019-12-29 21:19:14 -070059 /*
60 * This error indicates that @dev is a device on an unprobed PCI bus.
61 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
62 * will produce a bad BDF>
63 *
64 * A common cause of this problem is that this function is called in the
Simon Glassaad29ae2020-12-03 16:55:21 -070065 * of_to_plat() method of @dev. Accessing the PCI bus in that
Simon Glass1c6449c2019-12-29 21:19:14 -070066 * method is not allowed, since it has not yet been probed. To fix this,
67 * move that access to the probe() method of @dev instead.
68 */
69 if (!device_active(bus))
70 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
71 bus->name);
Simon Glass75e534b2020-12-16 21:20:07 -070072 return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
Simon Glassc9118d42015-07-06 16:47:46 -060073}
74
Simon Glassb94dc892015-03-05 12:25:25 -070075/**
76 * pci_get_bus_max() - returns the bus number of the last active bus
77 *
78 * @return last bus number, or -1 if no active buses
79 */
80static int pci_get_bus_max(void)
81{
82 struct udevice *bus;
83 struct uclass *uc;
84 int ret = -1;
85
86 ret = uclass_get(UCLASS_PCI, &uc);
87 uclass_foreach_dev(bus, uc) {
Simon Glass75e534b2020-12-16 21:20:07 -070088 if (dev_seq(bus) > ret)
89 ret = dev_seq(bus);
Simon Glassb94dc892015-03-05 12:25:25 -070090 }
91
92 debug("%s: ret=%d\n", __func__, ret);
93
94 return ret;
95}
96
97int pci_last_busno(void)
98{
Bin Meng5bc3f8a2015-10-01 00:36:01 -070099 return pci_get_bus_max();
Simon Glassb94dc892015-03-05 12:25:25 -0700100}
101
102int pci_get_ff(enum pci_size_t size)
103{
104 switch (size) {
105 case PCI_SIZE_8:
106 return 0xff;
107 case PCI_SIZE_16:
108 return 0xffff;
109 default:
110 return 0xffffffff;
111 }
112}
113
Marek Vasutb4535792018-10-10 21:27:06 +0200114static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
115 ofnode *rnode)
116{
117 struct fdt_pci_addr addr;
118 ofnode node;
119 int ret;
120
121 dev_for_each_subnode(node, bus) {
122 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
123 &addr);
124 if (ret)
125 continue;
126
127 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
128 continue;
129
130 *rnode = node;
131 break;
132 }
133};
134
Simon Glass2a311e82020-01-27 08:49:37 -0700135int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassb94dc892015-03-05 12:25:25 -0700136 struct udevice **devp)
137{
138 struct udevice *dev;
139
140 for (device_find_first_child(bus, &dev);
141 dev;
142 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700143 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700144
Simon Glass71fa5b42020-12-03 16:55:18 -0700145 pplat = dev_get_parent_plat(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700146 if (pplat && pplat->devfn == find_devfn) {
147 *devp = dev;
148 return 0;
149 }
150 }
151
152 return -ENODEV;
153}
154
Simon Glass84283d52015-11-29 13:17:48 -0700155int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassb94dc892015-03-05 12:25:25 -0700156{
157 struct udevice *bus;
158 int ret;
159
Simon Glass7d07e592015-08-31 18:55:35 -0600160 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700161 if (ret)
162 return ret;
163 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
164}
165
166static int pci_device_matches_ids(struct udevice *dev,
167 struct pci_device_id *ids)
168{
Simon Glassb75b15b2020-12-03 16:55:23 -0700169 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700170 int i;
171
Simon Glass71fa5b42020-12-03 16:55:18 -0700172 pplat = dev_get_parent_plat(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700173 if (!pplat)
174 return -EINVAL;
175 for (i = 0; ids[i].vendor != 0; i++) {
176 if (pplat->vendor == ids[i].vendor &&
177 pplat->device == ids[i].device)
178 return i;
179 }
180
181 return -EINVAL;
182}
183
184int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
185 int *indexp, struct udevice **devp)
186{
187 struct udevice *dev;
188
189 /* Scan all devices on this bus */
190 for (device_find_first_child(bus, &dev);
191 dev;
192 device_find_next_child(&dev)) {
193 if (pci_device_matches_ids(dev, ids) >= 0) {
194 if ((*indexp)-- <= 0) {
195 *devp = dev;
196 return 0;
197 }
198 }
199 }
200
201 return -ENODEV;
202}
203
204int pci_find_device_id(struct pci_device_id *ids, int index,
205 struct udevice **devp)
206{
207 struct udevice *bus;
208
209 /* Scan all known buses */
210 for (uclass_first_device(UCLASS_PCI, &bus);
211 bus;
212 uclass_next_device(&bus)) {
213 if (!pci_bus_find_devices(bus, ids, &index, devp))
214 return 0;
215 }
216 *devp = NULL;
217
218 return -ENODEV;
219}
220
Simon Glass70e0c582015-11-29 13:17:50 -0700221static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
222 unsigned int device, int *indexp,
223 struct udevice **devp)
224{
Simon Glassb75b15b2020-12-03 16:55:23 -0700225 struct pci_child_plat *pplat;
Simon Glass70e0c582015-11-29 13:17:50 -0700226 struct udevice *dev;
227
228 for (device_find_first_child(bus, &dev);
229 dev;
230 device_find_next_child(&dev)) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700231 pplat = dev_get_parent_plat(dev);
Simon Glass70e0c582015-11-29 13:17:50 -0700232 if (pplat->vendor == vendor && pplat->device == device) {
233 if (!(*indexp)--) {
234 *devp = dev;
235 return 0;
236 }
237 }
238 }
239
240 return -ENODEV;
241}
242
243int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
244 struct udevice **devp)
245{
246 struct udevice *bus;
247
248 /* Scan all known buses */
249 for (uclass_first_device(UCLASS_PCI, &bus);
250 bus;
251 uclass_next_device(&bus)) {
252 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
253 return device_probe(*devp);
254 }
255 *devp = NULL;
256
257 return -ENODEV;
258}
259
Simon Glassb639d512015-11-29 13:17:52 -0700260int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
261{
262 struct udevice *dev;
263
264 /* Scan all known buses */
265 for (pci_find_first_device(&dev);
266 dev;
267 pci_find_next_device(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700268 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassb639d512015-11-29 13:17:52 -0700269
270 if (pplat->class == find_class && !index--) {
271 *devp = dev;
272 return device_probe(*devp);
273 }
274 }
275 *devp = NULL;
276
277 return -ENODEV;
278}
279
Simon Glassb94dc892015-03-05 12:25:25 -0700280int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
281 unsigned long value, enum pci_size_t size)
282{
283 struct dm_pci_ops *ops;
284
285 ops = pci_get_ops(bus);
286 if (!ops->write_config)
287 return -ENOSYS;
288 return ops->write_config(bus, bdf, offset, value, size);
289}
290
Simon Glass9cec2df2016-03-06 19:27:52 -0700291int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
292 u32 clr, u32 set)
293{
294 ulong val;
295 int ret;
296
297 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
298 if (ret)
299 return ret;
300 val &= ~clr;
301 val |= set;
302
303 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
304}
305
Simon Glassb94dc892015-03-05 12:25:25 -0700306int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
307 enum pci_size_t size)
308{
309 struct udevice *bus;
310 int ret;
311
Simon Glass7d07e592015-08-31 18:55:35 -0600312 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700313 if (ret)
314 return ret;
315
Bin Meng0a721522015-07-19 00:20:04 +0800316 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700317}
318
Simon Glass94ef2422015-08-10 07:05:03 -0600319int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
320 enum pci_size_t size)
321{
322 struct udevice *bus;
323
Bin Meng05bedb12015-09-11 03:24:34 -0700324 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600325 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700326 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
327 size);
Simon Glass94ef2422015-08-10 07:05:03 -0600328}
329
Simon Glassb94dc892015-03-05 12:25:25 -0700330int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
331{
332 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
333}
334
335int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
336{
337 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
338}
339
340int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
341{
342 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
343}
344
Simon Glass94ef2422015-08-10 07:05:03 -0600345int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
346{
347 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
348}
349
350int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
351{
352 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
353}
354
355int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
356{
357 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
358}
359
Simon Glassc92aac12020-01-27 08:49:38 -0700360int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassb94dc892015-03-05 12:25:25 -0700361 unsigned long *valuep, enum pci_size_t size)
362{
363 struct dm_pci_ops *ops;
364
365 ops = pci_get_ops(bus);
366 if (!ops->read_config)
367 return -ENOSYS;
368 return ops->read_config(bus, bdf, offset, valuep, size);
369}
370
371int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
372 enum pci_size_t size)
373{
374 struct udevice *bus;
375 int ret;
376
Simon Glass7d07e592015-08-31 18:55:35 -0600377 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700378 if (ret)
379 return ret;
380
Bin Meng0a721522015-07-19 00:20:04 +0800381 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700382}
383
Simon Glassc92aac12020-01-27 08:49:38 -0700384int dm_pci_read_config(const struct udevice *dev, int offset,
385 unsigned long *valuep, enum pci_size_t size)
Simon Glass94ef2422015-08-10 07:05:03 -0600386{
Simon Glassc92aac12020-01-27 08:49:38 -0700387 const struct udevice *bus;
Simon Glass94ef2422015-08-10 07:05:03 -0600388
Bin Meng05bedb12015-09-11 03:24:34 -0700389 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600390 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700391 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass94ef2422015-08-10 07:05:03 -0600392 size);
393}
394
Simon Glassb94dc892015-03-05 12:25:25 -0700395int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
396{
397 unsigned long value;
398 int ret;
399
400 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
401 if (ret)
402 return ret;
403 *valuep = value;
404
405 return 0;
406}
407
408int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
409{
410 unsigned long value;
411 int ret;
412
413 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
414 if (ret)
415 return ret;
416 *valuep = value;
417
418 return 0;
419}
420
421int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
422{
423 unsigned long value;
424 int ret;
425
426 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
427 if (ret)
428 return ret;
429 *valuep = value;
430
431 return 0;
432}
433
Simon Glassc92aac12020-01-27 08:49:38 -0700434int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600435{
436 unsigned long value;
437 int ret;
438
439 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
440 if (ret)
441 return ret;
442 *valuep = value;
443
444 return 0;
445}
446
Simon Glassc92aac12020-01-27 08:49:38 -0700447int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600448{
449 unsigned long value;
450 int ret;
451
452 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
453 if (ret)
454 return ret;
455 *valuep = value;
456
457 return 0;
458}
459
Simon Glassc92aac12020-01-27 08:49:38 -0700460int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600461{
462 unsigned long value;
463 int ret;
464
465 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
466 if (ret)
467 return ret;
468 *valuep = value;
469
470 return 0;
471}
472
Simon Glass9cec2df2016-03-06 19:27:52 -0700473int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
474{
475 u8 val;
476 int ret;
477
478 ret = dm_pci_read_config8(dev, offset, &val);
479 if (ret)
480 return ret;
481 val &= ~clr;
482 val |= set;
483
484 return dm_pci_write_config8(dev, offset, val);
485}
486
487int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
488{
489 u16 val;
490 int ret;
491
492 ret = dm_pci_read_config16(dev, offset, &val);
493 if (ret)
494 return ret;
495 val &= ~clr;
496 val |= set;
497
498 return dm_pci_write_config16(dev, offset, val);
499}
500
501int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
502{
503 u32 val;
504 int ret;
505
506 ret = dm_pci_read_config32(dev, offset, &val);
507 if (ret)
508 return ret;
509 val &= ~clr;
510 val |= set;
511
512 return dm_pci_write_config32(dev, offset, val);
513}
514
Bin Menga0705782015-10-01 00:36:02 -0700515static void set_vga_bridge_bits(struct udevice *dev)
516{
517 struct udevice *parent = dev->parent;
518 u16 bc;
519
Simon Glass75e534b2020-12-16 21:20:07 -0700520 while (dev_seq(parent) != 0) {
Bin Menga0705782015-10-01 00:36:02 -0700521 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
522 bc |= PCI_BRIDGE_CTL_VGA;
523 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
524 parent = parent->parent;
525 }
526}
527
Simon Glassb94dc892015-03-05 12:25:25 -0700528int pci_auto_config_devices(struct udevice *bus)
529{
Simon Glass95588622020-12-22 19:30:28 -0700530 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700531 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700532 unsigned int sub_bus;
533 struct udevice *dev;
534 int ret;
535
Simon Glass75e534b2020-12-16 21:20:07 -0700536 sub_bus = dev_seq(bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700537 debug("%s: start\n", __func__);
538 pciauto_config_init(hose);
539 for (ret = device_find_first_child(bus, &dev);
540 !ret && dev;
541 ret = device_find_next_child(&dev)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700542 unsigned int max_bus;
Simon Glassb072d522015-09-08 17:52:47 -0600543 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700544
Simon Glassb94dc892015-03-05 12:25:25 -0700545 debug("%s: device %s\n", __func__, dev->name);
Simon Glassf1d50f72020-12-19 10:40:13 -0700546 if (dev_has_ofnode(dev) &&
Suneel Garapatif8c86282020-05-04 21:25:25 -0700547 dev_read_bool(dev, "pci,no-autoconfig"))
Simon Glassf3005fb2020-04-08 16:57:26 -0600548 continue;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700549 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600550 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -0700551 return log_msg_ret("auto", ret);
Simon Glassb072d522015-09-08 17:52:47 -0600552 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700553 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700554
Masami Hiramatsu7ccdc672021-06-04 18:43:34 +0900555 if (dev_get_parent(dev) == bus)
556 continue;
557
Simon Glass71fa5b42020-12-03 16:55:18 -0700558 pplat = dev_get_parent_plat(dev);
Bin Menga0705782015-10-01 00:36:02 -0700559 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
560 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700561 }
562 debug("%s: done\n", __func__);
563
Simon Glassbe706102020-12-16 21:20:18 -0700564 return log_msg_ret("sub", sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700565}
566
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300567int pci_generic_mmap_write_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700568 const struct udevice *bus,
569 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
570 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300571 pci_dev_t bdf,
572 uint offset,
573 ulong value,
574 enum pci_size_t size)
575{
576 void *address;
577
578 if (addr_f(bus, bdf, offset, &address) < 0)
579 return 0;
580
581 switch (size) {
582 case PCI_SIZE_8:
583 writeb(value, address);
584 return 0;
585 case PCI_SIZE_16:
586 writew(value, address);
587 return 0;
588 case PCI_SIZE_32:
589 writel(value, address);
590 return 0;
591 default:
592 return -EINVAL;
593 }
594}
595
596int pci_generic_mmap_read_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700597 const struct udevice *bus,
598 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
599 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300600 pci_dev_t bdf,
601 uint offset,
602 ulong *valuep,
603 enum pci_size_t size)
604{
605 void *address;
606
607 if (addr_f(bus, bdf, offset, &address) < 0) {
608 *valuep = pci_get_ff(size);
609 return 0;
610 }
611
612 switch (size) {
613 case PCI_SIZE_8:
614 *valuep = readb(address);
615 return 0;
616 case PCI_SIZE_16:
617 *valuep = readw(address);
618 return 0;
619 case PCI_SIZE_32:
620 *valuep = readl(address);
621 return 0;
622 default:
623 return -EINVAL;
624 }
625}
626
Simon Glass37a3f94b2015-11-29 13:17:49 -0700627int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700628{
Simon Glassb94dc892015-03-05 12:25:25 -0700629 int sub_bus;
630 int ret;
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700631 int ea_pos;
632 u8 reg;
Simon Glassb94dc892015-03-05 12:25:25 -0700633
634 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700635
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700636 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
637 if (ea_pos) {
638 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
639 &reg);
640 sub_bus = reg;
641 } else {
642 sub_bus = pci_get_bus_max() + 1;
643 }
Simon Glassb94dc892015-03-05 12:25:25 -0700644 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700645 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700646
647 ret = device_probe(bus);
648 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600649 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700650 ret);
Simon Glassbe706102020-12-16 21:20:18 -0700651 return log_msg_ret("probe", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700652 }
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700653
Masami Hiramatsuff022452021-04-16 14:53:46 -0700654 if (!ea_pos)
655 sub_bus = pci_get_bus_max();
656
Simon Glass37a3f94b2015-11-29 13:17:49 -0700657 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700658
659 return sub_bus;
660}
661
Simon Glass318d71c2015-07-06 16:47:44 -0600662/**
663 * pci_match_one_device - Tell if a PCI device structure has a matching
664 * PCI device id structure
665 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800666 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600667 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800668 * Returns true if the finding pci_device_id structure matched or false if
669 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600670 */
671static bool pci_match_one_id(const struct pci_device_id *id,
672 const struct pci_device_id *find)
673{
674 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
675 (id->device == PCI_ANY_ID || id->device == find->device) &&
676 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
677 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
678 !((id->class ^ find->class) & id->class_mask))
679 return true;
680
681 return false;
682}
683
684/**
685 * pci_find_and_bind_driver() - Find and bind the right PCI driver
686 *
687 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600688 *
689 * @parent: Parent bus
690 * @find_id: Specification of the driver to find
691 * @bdf: Bus/device/function addreess - see PCI_BDF()
692 * @devp: Returns a pointer to the device created
693 * @return 0 if OK, -EPERM if the device is not needed before relocation and
694 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600695 */
696static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600697 struct pci_device_id *find_id,
698 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600699{
700 struct pci_driver_entry *start, *entry;
Marek Vasutb4535792018-10-10 21:27:06 +0200701 ofnode node = ofnode_null();
Simon Glass318d71c2015-07-06 16:47:44 -0600702 const char *drv;
703 int n_ents;
704 int ret;
705 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700706 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600707
708 *devp = NULL;
709
710 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
711 find_id->vendor, find_id->device);
Marek Vasutb4535792018-10-10 21:27:06 +0200712
713 /* Determine optional OF node */
Suneel Garapaticb7093d2019-10-19 16:02:48 -0700714 if (ofnode_valid(dev_ofnode(parent)))
715 pci_dev_find_ofnode(parent, bdf, &node);
Marek Vasutb4535792018-10-10 21:27:06 +0200716
Michael Walle2e21f372019-12-01 17:45:18 +0100717 if (ofnode_valid(node) && !ofnode_is_available(node)) {
718 debug("%s: Ignoring disabled device\n", __func__);
Simon Glassbe706102020-12-16 21:20:18 -0700719 return log_msg_ret("dis", -EPERM);
Michael Walle2e21f372019-12-01 17:45:18 +0100720 }
721
Simon Glass318d71c2015-07-06 16:47:44 -0600722 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
723 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
724 for (entry = start; entry != start + n_ents; entry++) {
725 const struct pci_device_id *id;
726 struct udevice *dev;
727 const struct driver *drv;
728
729 for (id = entry->match;
730 id->vendor || id->subvendor || id->class_mask;
731 id++) {
732 if (!pci_match_one_id(id, find_id))
733 continue;
734
735 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700736
737 /*
738 * In the pre-relocation phase, we only bind devices
739 * whose driver has the DM_FLAG_PRE_RELOC set, to save
740 * precious memory space as on some platforms as that
741 * space is pretty limited (ie: using Cache As RAM).
742 */
743 if (!(gd->flags & GD_FLG_RELOC) &&
744 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassbe706102020-12-16 21:20:18 -0700745 return log_msg_ret("pre", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700746
Simon Glass318d71c2015-07-06 16:47:44 -0600747 /*
748 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700749 * plat (instead of NULL) and allow its bind()
Simon Glass318d71c2015-07-06 16:47:44 -0600750 * method to return -ENOENT if it doesn't support this
751 * device. That way we could continue the search to
752 * find another driver. For now this doesn't seem
753 * necesssary, so just bind the first match.
754 */
Simon Glass884870f2020-11-28 17:50:01 -0700755 ret = device_bind(parent, drv, drv->name, NULL, node,
756 &dev);
Simon Glass318d71c2015-07-06 16:47:44 -0600757 if (ret)
758 goto error;
759 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700760 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600761 *devp = dev;
762 return 0;
763 }
764 }
765
Bin Meng984c0dc2015-08-20 06:40:17 -0700766 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
767 /*
768 * In the pre-relocation phase, we only bind bridge devices to save
769 * precious memory space as on some platforms as that space is pretty
770 * limited (ie: using Cache As RAM).
771 */
772 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glassbe706102020-12-16 21:20:18 -0700773 return log_msg_ret("notbr", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700774
Simon Glass318d71c2015-07-06 16:47:44 -0600775 /* Bind a generic driver so that the device can be used */
Simon Glass75e534b2020-12-16 21:20:07 -0700776 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
Bin Meng0a721522015-07-19 00:20:04 +0800777 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600778 str = strdup(name);
779 if (!str)
780 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700781 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
782
Marek Vasutb4535792018-10-10 21:27:06 +0200783 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glass318d71c2015-07-06 16:47:44 -0600784 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600785 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200786 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600787 return ret;
788 }
789 debug("%s: No match found: bound generic driver instead\n", __func__);
790
791 return 0;
792
793error:
794 debug("%s: No match found: error %d\n", __func__, ret);
795 return ret;
796}
797
Tim Harvey4c57bf72021-04-16 14:53:47 -0700798__weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
799{
800}
801
Simon Glassb94dc892015-03-05 12:25:25 -0700802int pci_bind_bus_devices(struct udevice *bus)
803{
804 ulong vendor, device;
805 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800806 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700807 bool found_multi;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700808 int ari_off;
Simon Glassb94dc892015-03-05 12:25:25 -0700809 int ret;
810
811 found_multi = false;
Simon Glass75e534b2020-12-16 21:20:07 -0700812 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
Bin Meng0a721522015-07-19 00:20:04 +0800813 PCI_MAX_PCI_FUNCTIONS - 1);
Simon Glass75e534b2020-12-16 21:20:07 -0700814 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800815 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700816 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700817 struct udevice *dev;
818 ulong class;
819
Bin Meng20bdc1e2018-08-03 01:14:37 -0700820 if (!PCI_FUNC(bdf))
821 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800822 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700823 continue;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800824
Simon Glassb94dc892015-03-05 12:25:25 -0700825 /* Check only the first access, we don't expect problems */
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800826 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
827 PCI_SIZE_16);
Simon Glassb94dc892015-03-05 12:25:25 -0700828 if (ret)
829 goto error;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800830
Simon Glassb94dc892015-03-05 12:25:25 -0700831 if (vendor == 0xffff || vendor == 0x0000)
832 continue;
833
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800834 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
835 &header_type, PCI_SIZE_8);
836
Bin Meng0a721522015-07-19 00:20:04 +0800837 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700838 found_multi = header_type & 0x80;
839
Simon Glass25916d62019-09-25 08:56:12 -0600840 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -0700841 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Bin Meng0a721522015-07-19 00:20:04 +0800842 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700843 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800844 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600845 PCI_SIZE_32);
846 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700847
848 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800849 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass25916d62019-09-25 08:56:12 -0600850 debug(": find ret=%d\n", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700851
Simon Glass413ebdb2015-11-29 13:18:09 -0700852 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700853 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600854 struct pci_device_id find_id;
855 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700856
Simon Glass318d71c2015-07-06 16:47:44 -0600857 memset(&find_id, '\0', sizeof(find_id));
858 find_id.vendor = vendor;
859 find_id.device = device;
860 find_id.class = class;
861 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800862 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600863 PCI_SUBSYSTEM_VENDOR_ID,
864 &val, PCI_SIZE_32);
865 find_id.subvendor = val & 0xffff;
866 find_id.subdevice = val >> 16;
867 }
Bin Meng0a721522015-07-19 00:20:04 +0800868 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600869 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700870 }
Simon Glassc45abf12015-09-08 17:52:49 -0600871 if (ret == -EPERM)
872 continue;
873 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700874 return ret;
875
876 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -0700877 pplat = dev_get_parent_plat(dev);
Simon Glassc45abf12015-09-08 17:52:49 -0600878 pplat->devfn = PCI_MASK_BUS(bdf);
879 pplat->vendor = vendor;
880 pplat->device = device;
881 pplat->class = class;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700882
883 if (IS_ENABLED(CONFIG_PCI_ARID)) {
884 ari_off = dm_pci_find_ext_capability(dev,
885 PCI_EXT_CAP_ID_ARI);
886 if (ari_off) {
887 u16 ari_cap;
888
889 /*
890 * Read Next Function number in ARI Cap
891 * Register
892 */
893 dm_pci_read_config16(dev, ari_off + 4,
894 &ari_cap);
895 /*
896 * Update next scan on this function number,
897 * subtract 1 in BDF to satisfy loop increment.
898 */
899 if (ari_cap & 0xff00) {
900 bdf = PCI_BDF(PCI_BUS(bdf),
901 PCI_DEV(ari_cap),
902 PCI_FUNC(ari_cap));
903 bdf = bdf - 0x100;
904 }
905 }
906 }
Tim Harvey4c57bf72021-04-16 14:53:47 -0700907
908 board_pci_fixup_dev(bus, dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700909 }
910
911 return 0;
912error:
913 printf("Cannot read bus configuration: %d\n", ret);
914
915 return ret;
916}
917
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700918static void decode_regions(struct pci_controller *hose, ofnode parent_node,
919 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700920{
921 int pci_addr_cells, addr_cells, size_cells;
922 int cells_per_record;
Stefan Roesebbc88462020-08-12 11:55:46 +0200923 struct bd_info *bd;
Simon Glassb94dc892015-03-05 12:25:25 -0700924 const u32 *prop;
Stefan Roese950864f2020-07-23 16:34:10 +0200925 int max_regions;
Simon Glassb94dc892015-03-05 12:25:25 -0700926 int len;
927 int i;
928
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900929 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700930 if (!prop) {
931 debug("%s: Cannot decode regions\n", __func__);
932 return;
933 }
934
Simon Glass4191dc12017-06-12 06:21:31 -0600935 pci_addr_cells = ofnode_read_simple_addr_cells(node);
936 addr_cells = ofnode_read_simple_addr_cells(parent_node);
937 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700938
939 /* PCI addresses are always 3-cells */
940 len /= sizeof(u32);
941 cells_per_record = pci_addr_cells + addr_cells + size_cells;
942 hose->region_count = 0;
943 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
944 cells_per_record);
Stefan Roese950864f2020-07-23 16:34:10 +0200945
946 /* Dynamically allocate the regions array */
947 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
948 hose->regions = (struct pci_region *)
949 calloc(1, max_regions * sizeof(struct pci_region));
950
951 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassb94dc892015-03-05 12:25:25 -0700952 u64 pci_addr, addr, size;
953 int space_code;
954 u32 flags;
955 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -0700956 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -0700957
958 if (len < cells_per_record)
959 break;
960 flags = fdt32_to_cpu(prop[0]);
961 space_code = (flags >> 24) & 3;
962 pci_addr = fdtdec_get_number(prop + 1, 2);
963 prop += pci_addr_cells;
964 addr = fdtdec_get_number(prop, addr_cells);
965 prop += addr_cells;
966 size = fdtdec_get_number(prop, size_cells);
967 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900968 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
969 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -0700970 if (space_code & 2) {
971 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
972 PCI_REGION_MEM;
973 } else if (space_code & 1) {
974 type = PCI_REGION_IO;
975 } else {
976 continue;
977 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +0300978
979 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
980 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
981 debug(" - beyond the 32-bit boundary, ignoring\n");
982 continue;
983 }
984
Simon Glass7efc9ba2015-11-19 20:26:58 -0700985 pos = -1;
Suneel Garapati3ac3aec2019-10-19 17:10:20 -0700986 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
987 for (i = 0; i < hose->region_count; i++) {
988 if (hose->regions[i].flags == type)
989 pos = i;
990 }
Simon Glass7efc9ba2015-11-19 20:26:58 -0700991 }
Suneel Garapati3ac3aec2019-10-19 17:10:20 -0700992
Simon Glass7efc9ba2015-11-19 20:26:58 -0700993 if (pos == -1)
994 pos = hose->region_count++;
995 debug(" - type=%d, pos=%d\n", type, pos);
996 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -0700997 }
998
999 /* Add a region for our local memory */
Stefan Roesebbc88462020-08-12 11:55:46 +02001000 bd = gd->bd;
Bin Mengae0bdde2018-03-27 00:46:05 -07001001 if (!bd)
Christian Gmeiner5f4e0942018-06-10 06:25:05 -07001002 return;
Bin Mengae0bdde2018-03-27 00:46:05 -07001003
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +01001004 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1005 if (bd->bi_dram[i].size) {
1006 pci_set_region(hose->regions + hose->region_count++,
1007 bd->bi_dram[i].start,
1008 bd->bi_dram[i].start,
1009 bd->bi_dram[i].size,
1010 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1011 }
1012 }
Simon Glassb94dc892015-03-05 12:25:25 -07001013
Christian Gmeiner5f4e0942018-06-10 06:25:05 -07001014 return;
Simon Glassb94dc892015-03-05 12:25:25 -07001015}
1016
1017static int pci_uclass_pre_probe(struct udevice *bus)
1018{
1019 struct pci_controller *hose;
Simon Glassbe706102020-12-16 21:20:18 -07001020 struct uclass *uc;
1021 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001022
Simon Glass75e534b2020-12-16 21:20:07 -07001023 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -07001024 bus->parent->name);
Simon Glass95588622020-12-22 19:30:28 -07001025 hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001026
Simon Glassbe706102020-12-16 21:20:18 -07001027 /*
1028 * Set the sequence number, if device_bind() doesn't. We want control
1029 * of this so that numbers are allocated as devices are probed. That
1030 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1031 * higher than their parents)
1032 */
1033 if (dev_seq(bus) == -1) {
1034 ret = uclass_get(UCLASS_PCI, &uc);
1035 if (ret)
1036 return ret;
Simon Glass5e349922020-12-19 10:40:09 -07001037 bus->seq_ = uclass_find_next_free_seq(uc);
Simon Glassbe706102020-12-16 21:20:18 -07001038 }
1039
Simon Glassb94dc892015-03-05 12:25:25 -07001040 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +01001041 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -07001042 hose->ctlr = bus;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -07001043 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001044 } else {
1045 struct pci_controller *parent_hose;
1046
1047 parent_hose = dev_get_uclass_priv(bus->parent);
1048 hose->ctlr = parent_hose->bus;
1049 }
Simon Glassbe706102020-12-16 21:20:18 -07001050
Simon Glassb94dc892015-03-05 12:25:25 -07001051 hose->bus = bus;
Simon Glass75e534b2020-12-16 21:20:07 -07001052 hose->first_busno = dev_seq(bus);
1053 hose->last_busno = dev_seq(bus);
Simon Glassf1d50f72020-12-19 10:40:13 -07001054 if (dev_has_ofnode(bus)) {
Suneel Garapatif8c86282020-05-04 21:25:25 -07001055 hose->skip_auto_config_until_reloc =
1056 dev_read_bool(bus,
1057 "u-boot,skip-auto-config-until-reloc");
1058 }
Simon Glassb94dc892015-03-05 12:25:25 -07001059
1060 return 0;
1061}
1062
1063static int pci_uclass_post_probe(struct udevice *bus)
1064{
Simon Glass68e35a72019-12-06 21:41:37 -07001065 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001066 int ret;
1067
Simon Glass75e534b2020-12-16 21:20:07 -07001068 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001069 ret = pci_bind_bus_devices(bus);
1070 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001071 return log_msg_ret("bind", ret);
Simon Glassb94dc892015-03-05 12:25:25 -07001072
Simon Glassbd165e72020-04-26 09:12:56 -06001073 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass68e35a72019-12-06 21:41:37 -07001074 (!hose->skip_auto_config_until_reloc ||
1075 (gd->flags & GD_FLG_RELOC))) {
1076 ret = pci_auto_config_devices(bus);
1077 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -07001078 return log_msg_ret("cfg", ret);
Simon Glass68e35a72019-12-06 21:41:37 -07001079 }
Simon Glassb94dc892015-03-05 12:25:25 -07001080
Bin Mengc0820a42015-08-20 06:40:23 -07001081#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1082 /*
1083 * Per Intel FSP specification, we should call FSP notify API to
1084 * inform FSP that PCI enumeration has been done so that FSP will
1085 * do any necessary initialization as required by the chipset's
1086 * BIOS Writer's Guide (BWG).
1087 *
1088 * Unfortunately we have to put this call here as with driver model,
1089 * the enumeration is all done on a lazy basis as needed, so until
1090 * something is touched on PCI it won't happen.
1091 *
1092 * Note we only call this 1) after U-Boot is relocated, and 2)
1093 * root bus has finished probing.
1094 */
Simon Glass75e534b2020-12-16 21:20:07 -07001095 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
Bin Mengc0820a42015-08-20 06:40:23 -07001096 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -06001097 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001098 return log_msg_ret("fsp", ret);
Simon Glassb072d522015-09-08 17:52:47 -06001099 }
Bin Mengc0820a42015-08-20 06:40:23 -07001100#endif
1101
Simon Glassb072d522015-09-08 17:52:47 -06001102 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001103}
1104
1105static int pci_uclass_child_post_bind(struct udevice *dev)
1106{
Simon Glassb75b15b2020-12-03 16:55:23 -07001107 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -07001108
Simon Glassf1d50f72020-12-19 10:40:13 -07001109 if (!dev_has_ofnode(dev))
Simon Glassb94dc892015-03-05 12:25:25 -07001110 return 0;
1111
Simon Glass71fa5b42020-12-03 16:55:18 -07001112 pplat = dev_get_parent_plat(dev);
Bin Meng00d808e2018-08-03 01:14:36 -07001113
1114 /* Extract vendor id and device id if available */
1115 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1116
1117 /* Extract the devfn from fdt_pci_addr */
Stefan Roesea74eb552019-01-25 11:52:42 +01001118 pplat->devfn = pci_get_devfn(dev);
Simon Glassb94dc892015-03-05 12:25:25 -07001119
1120 return 0;
1121}
1122
Simon Glass2a311e82020-01-27 08:49:37 -07001123static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng0a721522015-07-19 00:20:04 +08001124 uint offset, ulong *valuep,
1125 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001126{
Simon Glass95588622020-12-22 19:30:28 -07001127 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001128
1129 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1130}
1131
Bin Meng0a721522015-07-19 00:20:04 +08001132static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1133 uint offset, ulong value,
1134 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001135{
Simon Glass95588622020-12-22 19:30:28 -07001136 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001137
1138 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1139}
1140
Simon Glass04c8b6a2015-08-10 07:05:04 -06001141static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1142{
1143 struct udevice *dev;
1144 int ret = 0;
1145
1146 /*
1147 * Scan through all the PCI controllers. On x86 there will only be one
1148 * but that is not necessarily true on other hardware.
1149 */
1150 do {
1151 device_find_first_child(bus, &dev);
1152 if (dev) {
1153 *devp = dev;
1154 return 0;
1155 }
1156 ret = uclass_next_device(&bus);
1157 if (ret)
1158 return ret;
1159 } while (bus);
1160
1161 return 0;
1162}
1163
1164int pci_find_next_device(struct udevice **devp)
1165{
1166 struct udevice *child = *devp;
1167 struct udevice *bus = child->parent;
1168 int ret;
1169
1170 /* First try all the siblings */
1171 *devp = NULL;
1172 while (child) {
1173 device_find_next_child(&child);
1174 if (child) {
1175 *devp = child;
1176 return 0;
1177 }
1178 }
1179
1180 /* We ran out of siblings. Try the next bus */
1181 ret = uclass_next_device(&bus);
1182 if (ret)
1183 return ret;
1184
1185 return bus ? skip_to_next_device(bus, devp) : 0;
1186}
1187
1188int pci_find_first_device(struct udevice **devp)
1189{
1190 struct udevice *bus;
1191 int ret;
1192
1193 *devp = NULL;
1194 ret = uclass_first_device(UCLASS_PCI, &bus);
1195 if (ret)
1196 return ret;
1197
1198 return skip_to_next_device(bus, devp);
1199}
1200
Simon Glass27a733f2015-11-19 20:26:59 -07001201ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1202{
1203 switch (size) {
1204 case PCI_SIZE_8:
1205 return (value >> ((offset & 3) * 8)) & 0xff;
1206 case PCI_SIZE_16:
1207 return (value >> ((offset & 2) * 8)) & 0xffff;
1208 default:
1209 return value;
1210 }
1211}
1212
1213ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1214 enum pci_size_t size)
1215{
1216 uint off_mask;
1217 uint val_mask, shift;
1218 ulong ldata, mask;
1219
1220 switch (size) {
1221 case PCI_SIZE_8:
1222 off_mask = 3;
1223 val_mask = 0xff;
1224 break;
1225 case PCI_SIZE_16:
1226 off_mask = 2;
1227 val_mask = 0xffff;
1228 break;
1229 default:
1230 return value;
1231 }
1232 shift = (offset & off_mask) * 8;
1233 ldata = (value & val_mask) << shift;
1234 mask = val_mask << shift;
1235 value = (old & ~mask) | ldata;
1236
1237 return value;
1238}
1239
Rayagonda Kokatanurcdc7ed32020-05-12 13:29:49 +05301240int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1241{
1242 int pci_addr_cells, addr_cells, size_cells;
1243 int cells_per_record;
1244 const u32 *prop;
1245 int len;
1246 int i = 0;
1247
1248 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1249 if (!prop) {
1250 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1251 dev->name);
1252 return -EINVAL;
1253 }
1254
1255 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1256 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1257 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1258
1259 /* PCI addresses are always 3-cells */
1260 len /= sizeof(u32);
1261 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1262 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1263 cells_per_record);
1264
1265 while (len) {
1266 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1267 prop += pci_addr_cells;
1268 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1269 prop += addr_cells;
1270 memp->size = fdtdec_get_number(prop, size_cells);
1271 prop += size_cells;
1272
1273 if (i == index)
1274 return 0;
1275 i++;
1276 len -= cells_per_record;
1277 }
1278
1279 return -EINVAL;
1280}
1281
Simon Glassdcdc0122015-11-19 20:27:01 -07001282int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1283 struct pci_region **memp, struct pci_region **prefp)
1284{
1285 struct udevice *bus = pci_get_controller(dev);
1286 struct pci_controller *hose = dev_get_uclass_priv(bus);
1287 int i;
1288
1289 *iop = NULL;
1290 *memp = NULL;
1291 *prefp = NULL;
1292 for (i = 0; i < hose->region_count; i++) {
1293 switch (hose->regions[i].flags) {
1294 case PCI_REGION_IO:
1295 if (!*iop || (*iop)->size < hose->regions[i].size)
1296 *iop = hose->regions + i;
1297 break;
1298 case PCI_REGION_MEM:
1299 if (!*memp || (*memp)->size < hose->regions[i].size)
1300 *memp = hose->regions + i;
1301 break;
1302 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1303 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1304 *prefp = hose->regions + i;
1305 break;
1306 }
1307 }
1308
1309 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1310}
1311
Simon Glassc92aac12020-01-27 08:49:38 -07001312u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glass3452cb12015-11-29 13:17:53 -07001313{
1314 u32 addr;
1315 int bar;
1316
1317 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1318 dm_pci_read_config32(dev, bar, &addr);
Simon Glass71fafd12020-04-09 10:27:36 -06001319
1320 /*
1321 * If we get an invalid address, return this so that comparisons with
1322 * FDT_ADDR_T_NONE work correctly
1323 */
1324 if (addr == 0xffffffff)
1325 return addr;
1326 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glass3452cb12015-11-29 13:17:53 -07001327 return addr & PCI_BASE_ADDRESS_IO_MASK;
1328 else
1329 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1330}
1331
Simon Glasse2b6b562016-01-18 20:19:15 -07001332void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1333{
1334 int bar;
1335
1336 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1337 dm_pci_write_config32(dev, bar, addr);
1338}
1339
Simon Glassc5f053b2015-11-29 13:18:03 -07001340static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1341 pci_addr_t bus_addr, unsigned long flags,
1342 unsigned long skip_mask, phys_addr_t *pa)
1343{
1344 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1345 struct pci_region *res;
1346 int i;
1347
Christian Gmeiner7241f802018-06-10 06:25:06 -07001348 if (hose->region_count == 0) {
1349 *pa = bus_addr;
1350 return 0;
1351 }
1352
Simon Glassc5f053b2015-11-29 13:18:03 -07001353 for (i = 0; i < hose->region_count; i++) {
1354 res = &hose->regions[i];
1355
1356 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1357 continue;
1358
1359 if (res->flags & skip_mask)
1360 continue;
1361
1362 if (bus_addr >= res->bus_start &&
1363 (bus_addr - res->bus_start) < res->size) {
1364 *pa = (bus_addr - res->bus_start + res->phys_start);
1365 return 0;
1366 }
1367 }
1368
1369 return 1;
1370}
1371
1372phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1373 unsigned long flags)
1374{
1375 phys_addr_t phys_addr = 0;
1376 struct udevice *ctlr;
1377 int ret;
1378
1379 /* The root controller has the region information */
1380 ctlr = pci_get_controller(dev);
1381
1382 /*
1383 * if PCI_REGION_MEM is set we do a two pass search with preference
1384 * on matches that don't have PCI_REGION_SYS_MEMORY set
1385 */
1386 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1387 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1388 flags, PCI_REGION_SYS_MEMORY,
1389 &phys_addr);
1390 if (!ret)
1391 return phys_addr;
1392 }
1393
1394 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1395
1396 if (ret)
1397 puts("pci_hose_bus_to_phys: invalid physical address\n");
1398
1399 return phys_addr;
1400}
1401
1402int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1403 unsigned long flags, unsigned long skip_mask,
1404 pci_addr_t *ba)
1405{
1406 struct pci_region *res;
1407 struct udevice *ctlr;
1408 pci_addr_t bus_addr;
1409 int i;
1410 struct pci_controller *hose;
1411
1412 /* The root controller has the region information */
1413 ctlr = pci_get_controller(dev);
1414 hose = dev_get_uclass_priv(ctlr);
1415
Christian Gmeiner7241f802018-06-10 06:25:06 -07001416 if (hose->region_count == 0) {
1417 *ba = phys_addr;
1418 return 0;
1419 }
1420
Simon Glassc5f053b2015-11-29 13:18:03 -07001421 for (i = 0; i < hose->region_count; i++) {
1422 res = &hose->regions[i];
1423
1424 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1425 continue;
1426
1427 if (res->flags & skip_mask)
1428 continue;
1429
1430 bus_addr = phys_addr - res->phys_start + res->bus_start;
1431
1432 if (bus_addr >= res->bus_start &&
1433 (bus_addr - res->bus_start) < res->size) {
1434 *ba = bus_addr;
1435 return 0;
1436 }
1437 }
1438
1439 return 1;
1440}
1441
1442pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1443 unsigned long flags)
1444{
1445 pci_addr_t bus_addr = 0;
1446 int ret;
1447
1448 /*
1449 * if PCI_REGION_MEM is set we do a two pass search with preference
1450 * on matches that don't have PCI_REGION_SYS_MEMORY set
1451 */
1452 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1453 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1454 PCI_REGION_SYS_MEMORY, &bus_addr);
1455 if (!ret)
1456 return bus_addr;
1457 }
1458
1459 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1460
1461 if (ret)
1462 puts("pci_hose_phys_to_bus: invalid physical address\n");
1463
1464 return bus_addr;
1465}
1466
Suneel Garapati5858ba82019-10-19 16:34:16 -07001467static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
Simon Glassb75b15b2020-12-03 16:55:23 -07001468 struct pci_child_plat *pdata)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001469{
1470 phys_addr_t addr = 0;
1471
1472 /*
1473 * In the case of a Virtual Function device using BAR
1474 * base and size, add offset for VFn BAR(1, 2, 3...n)
1475 */
1476 if (pdata->is_virtfn) {
1477 size_t sz;
1478 u32 ea_entry;
1479
1480 /* MaxOffset, 1st DW */
1481 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1482 sz = ea_entry & PCI_EA_FIELD_MASK;
1483 /* Fill up lower 2 bits */
1484 sz |= (~PCI_EA_FIELD_MASK);
1485
1486 if (ea_entry & PCI_EA_IS_64) {
1487 /* MaxOffset 2nd DW */
1488 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1489 sz |= ((u64)ea_entry) << 32;
1490 }
1491
1492 addr = (pdata->virtid - 1) * (sz + 1);
1493 }
1494
1495 return addr;
1496}
1497
Alex Marginean1c934a62019-06-07 11:24:23 +03001498static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
Simon Glassb75b15b2020-12-03 16:55:23 -07001499 int ea_off, struct pci_child_plat *pdata)
Alex Marginean1c934a62019-06-07 11:24:23 +03001500{
1501 int ea_cnt, i, entry_size;
1502 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1503 u32 ea_entry;
1504 phys_addr_t addr;
1505
Suneel Garapati5858ba82019-10-19 16:34:16 -07001506 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1507 /*
1508 * In the case of a Virtual Function device, device is
1509 * Physical function, so pdata will point to required VF
1510 * specific data.
1511 */
1512 if (pdata->is_virtfn)
1513 bar_id += PCI_EA_BEI_VF_BAR0;
1514 }
1515
Alex Marginean1c934a62019-06-07 11:24:23 +03001516 /* EA capability structure header */
1517 dm_pci_read_config32(dev, ea_off, &ea_entry);
1518 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1519 ea_off += PCI_EA_FIRST_ENT;
1520
1521 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1522 /* Entry header */
1523 dm_pci_read_config32(dev, ea_off, &ea_entry);
1524 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1525
1526 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1527 continue;
1528
1529 /* Base address, 1st DW */
1530 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1531 addr = ea_entry & PCI_EA_FIELD_MASK;
1532 if (ea_entry & PCI_EA_IS_64) {
1533 /* Base address, 2nd DW, skip over 4B MaxOffset */
1534 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1535 addr |= ((u64)ea_entry) << 32;
1536 }
1537
Suneel Garapati5858ba82019-10-19 16:34:16 -07001538 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1539 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1540
Alex Marginean1c934a62019-06-07 11:24:23 +03001541 /* size ignored for now */
Suneel Garapati47f19622019-10-19 16:44:35 -07001542 return map_physmem(addr, 0, flags);
Alex Marginean1c934a62019-06-07 11:24:23 +03001543 }
1544
1545 return 0;
1546}
1547
Simon Glassc5f053b2015-11-29 13:18:03 -07001548void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1549{
Simon Glassb75b15b2020-12-03 16:55:23 -07001550 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
Suneel Garapati5858ba82019-10-19 16:34:16 -07001551 struct udevice *udev = dev;
Simon Glassc5f053b2015-11-29 13:18:03 -07001552 pci_addr_t pci_bus_addr;
1553 u32 bar_response;
Alex Marginean1c934a62019-06-07 11:24:23 +03001554 int ea_off;
1555
Suneel Garapati5858ba82019-10-19 16:34:16 -07001556 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1557 /*
1558 * In case of Virtual Function devices, use PF udevice
1559 * as EA capability is defined in Physical Function
1560 */
1561 if (pdata->is_virtfn)
1562 udev = pdata->pfdev;
1563 }
1564
Alex Marginean1c934a62019-06-07 11:24:23 +03001565 /*
1566 * if the function supports Enhanced Allocation use that instead of
1567 * BARs
Suneel Garapati5858ba82019-10-19 16:34:16 -07001568 * Incase of virtual functions, pdata will help read VF BEI
1569 * and EA entry size.
Alex Marginean1c934a62019-06-07 11:24:23 +03001570 */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001571 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
Alex Marginean1c934a62019-06-07 11:24:23 +03001572 if (ea_off)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001573 return dm_pci_map_ea_bar(udev, bar, flags, ea_off, pdata);
Simon Glassc5f053b2015-11-29 13:18:03 -07001574
1575 /* read BAR address */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001576 dm_pci_read_config32(udev, bar, &bar_response);
Simon Glassc5f053b2015-11-29 13:18:03 -07001577 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1578
1579 /*
1580 * Pass "0" as the length argument to pci_bus_to_virt. The arg
Suneel Garapati47f19622019-10-19 16:44:35 -07001581 * isn't actually used on any platform because U-Boot assumes a static
Simon Glassc5f053b2015-11-29 13:18:03 -07001582 * linear mapping. In the future, this could read the BAR size
1583 * and pass that as the size if needed.
1584 */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001585 return dm_pci_bus_to_virt(udev, pci_bus_addr, flags, 0, MAP_NOCACHE);
Simon Glassc5f053b2015-11-29 13:18:03 -07001586}
1587
Bin Meng631f3482018-10-15 02:21:21 -07001588static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001589{
Bin Menga7366f02018-08-03 01:14:52 -07001590 int ttl = PCI_FIND_CAP_TTL;
1591 u8 id;
1592 u16 ent;
Bin Menga7366f02018-08-03 01:14:52 -07001593
1594 dm_pci_read_config8(dev, pos, &pos);
Bin Meng631f3482018-10-15 02:21:21 -07001595
Bin Menga7366f02018-08-03 01:14:52 -07001596 while (ttl--) {
1597 if (pos < PCI_STD_HEADER_SIZEOF)
1598 break;
1599 pos &= ~3;
1600 dm_pci_read_config16(dev, pos, &ent);
1601
1602 id = ent & 0xff;
1603 if (id == 0xff)
1604 break;
1605 if (id == cap)
1606 return pos;
1607 pos = (ent >> 8);
1608 }
1609
1610 return 0;
1611}
1612
Bin Meng631f3482018-10-15 02:21:21 -07001613int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1614{
1615 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1616 cap);
1617}
1618
1619int dm_pci_find_capability(struct udevice *dev, int cap)
1620{
1621 u16 status;
1622 u8 header_type;
1623 u8 pos;
1624
1625 dm_pci_read_config16(dev, PCI_STATUS, &status);
1626 if (!(status & PCI_STATUS_CAP_LIST))
1627 return 0;
1628
1629 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1630 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1631 pos = PCI_CB_CAPABILITY_LIST;
1632 else
1633 pos = PCI_CAPABILITY_LIST;
1634
1635 return _dm_pci_find_next_capability(dev, pos, cap);
1636}
1637
1638int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001639{
1640 u32 header;
1641 int ttl;
1642 int pos = PCI_CFG_SPACE_SIZE;
1643
1644 /* minimum 8 bytes per capability */
1645 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1646
Bin Meng631f3482018-10-15 02:21:21 -07001647 if (start)
1648 pos = start;
1649
Bin Menga7366f02018-08-03 01:14:52 -07001650 dm_pci_read_config32(dev, pos, &header);
1651 /*
1652 * If we have no capabilities, this is indicated by cap ID,
1653 * cap version and next pointer all being 0.
1654 */
1655 if (header == 0)
1656 return 0;
1657
1658 while (ttl--) {
1659 if (PCI_EXT_CAP_ID(header) == cap)
1660 return pos;
1661
1662 pos = PCI_EXT_CAP_NEXT(header);
1663 if (pos < PCI_CFG_SPACE_SIZE)
1664 break;
1665
1666 dm_pci_read_config32(dev, pos, &header);
1667 }
1668
1669 return 0;
1670}
1671
Bin Meng631f3482018-10-15 02:21:21 -07001672int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1673{
1674 return dm_pci_find_next_ext_capability(dev, 0, cap);
1675}
1676
Alex Marginean09467d32019-06-07 11:24:25 +03001677int dm_pci_flr(struct udevice *dev)
1678{
1679 int pcie_off;
1680 u32 cap;
1681
1682 /* look for PCI Express Capability */
1683 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1684 if (!pcie_off)
1685 return -ENOENT;
1686
1687 /* check FLR capability */
1688 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1689 if (!(cap & PCI_EXP_DEVCAP_FLR))
1690 return -ENOENT;
1691
1692 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1693 PCI_EXP_DEVCTL_BCR_FLR);
1694
1695 /* wait 100ms, per PCI spec */
1696 mdelay(100);
1697
1698 return 0;
1699}
1700
Suneel Garapati13822f72019-10-19 16:07:20 -07001701#if defined(CONFIG_PCI_SRIOV)
1702int pci_sriov_init(struct udevice *pdev, int vf_en)
1703{
1704 u16 vendor, device;
1705 struct udevice *bus;
1706 struct udevice *dev;
1707 pci_dev_t bdf;
1708 u16 ctrl;
1709 u16 num_vfs;
1710 u16 total_vf;
1711 u16 vf_offset;
1712 u16 vf_stride;
1713 int vf, ret;
1714 int pos;
1715
1716 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1717 if (!pos) {
1718 debug("Error: SRIOV capability not found\n");
1719 return -ENOENT;
1720 }
1721
1722 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1723
1724 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1725 if (vf_en > total_vf)
1726 vf_en = total_vf;
1727 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1728
1729 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1730 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1731
1732 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1733 if (num_vfs > vf_en)
1734 num_vfs = vf_en;
1735
1736 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1737 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1738
1739 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1740 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1741
1742 bdf = dm_pci_get_bdf(pdev);
1743
1744 pci_get_bus(PCI_BUS(bdf), &bus);
1745
1746 if (!bus)
1747 return -ENODEV;
1748
1749 bdf += PCI_BDF(0, 0, vf_offset);
1750
1751 for (vf = 0; vf < num_vfs; vf++) {
Simon Glassb75b15b2020-12-03 16:55:23 -07001752 struct pci_child_plat *pplat;
Suneel Garapati13822f72019-10-19 16:07:20 -07001753 ulong class;
1754
1755 pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1756 &class, PCI_SIZE_16);
1757
1758 debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -07001759 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Suneel Garapati13822f72019-10-19 16:07:20 -07001760
1761 /* Find this device in the device tree */
1762 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1763
1764 if (ret == -ENODEV) {
1765 struct pci_device_id find_id;
1766
1767 memset(&find_id, '\0', sizeof(find_id));
1768 find_id.vendor = vendor;
1769 find_id.device = device;
1770 find_id.class = class;
1771
1772 ret = pci_find_and_bind_driver(bus, &find_id,
1773 bdf, &dev);
1774
1775 if (ret)
1776 return ret;
1777 }
1778
1779 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -07001780 pplat = dev_get_parent_plat(dev);
Suneel Garapati13822f72019-10-19 16:07:20 -07001781 pplat->devfn = PCI_MASK_BUS(bdf);
1782 pplat->vendor = vendor;
1783 pplat->device = device;
1784 pplat->class = class;
1785 pplat->is_virtfn = true;
1786 pplat->pfdev = pdev;
1787 pplat->virtid = vf * vf_stride + vf_offset;
1788
1789 debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
Simon Glass75e534b2020-12-16 21:20:07 -07001790 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
Suneel Garapati13822f72019-10-19 16:07:20 -07001791 PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1792 bdf += PCI_BDF(0, 0, vf_stride);
1793 }
1794
1795 return 0;
1796}
1797
1798int pci_sriov_get_totalvfs(struct udevice *pdev)
1799{
1800 u16 total_vf;
1801 int pos;
1802
1803 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1804 if (!pos) {
1805 debug("Error: SRIOV capability not found\n");
1806 return -ENOENT;
1807 }
1808
1809 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1810
1811 return total_vf;
1812}
1813#endif /* SRIOV */
1814
Simon Glassb94dc892015-03-05 12:25:25 -07001815UCLASS_DRIVER(pci) = {
1816 .id = UCLASS_PCI,
1817 .name = "pci",
Simon Glassbe706102020-12-16 21:20:18 -07001818 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
Simon Glass18230342016-07-05 17:10:10 -06001819 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001820 .pre_probe = pci_uclass_pre_probe,
1821 .post_probe = pci_uclass_post_probe,
1822 .child_post_bind = pci_uclass_child_post_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001823 .per_device_auto = sizeof(struct pci_controller),
Simon Glassb75b15b2020-12-03 16:55:23 -07001824 .per_child_plat_auto = sizeof(struct pci_child_plat),
Simon Glassb94dc892015-03-05 12:25:25 -07001825};
1826
1827static const struct dm_pci_ops pci_bridge_ops = {
1828 .read_config = pci_bridge_read_config,
1829 .write_config = pci_bridge_write_config,
1830};
1831
1832static const struct udevice_id pci_bridge_ids[] = {
1833 { .compatible = "pci-bridge" },
1834 { }
1835};
1836
1837U_BOOT_DRIVER(pci_bridge_drv) = {
1838 .name = "pci_bridge_drv",
1839 .id = UCLASS_PCI,
1840 .of_match = pci_bridge_ids,
1841 .ops = &pci_bridge_ops,
1842};
1843
1844UCLASS_DRIVER(pci_generic) = {
1845 .id = UCLASS_PCI_GENERIC,
1846 .name = "pci_generic",
1847};
1848
1849static const struct udevice_id pci_generic_ids[] = {
1850 { .compatible = "pci-generic" },
1851 { }
1852};
1853
1854U_BOOT_DRIVER(pci_generic_drv) = {
1855 .name = "pci_generic_drv",
1856 .id = UCLASS_PCI_GENERIC,
1857 .of_match = pci_generic_ids,
1858};
Stephen Warren04eb2692016-01-26 11:10:11 -07001859
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001860int pci_init(void)
Stephen Warren04eb2692016-01-26 11:10:11 -07001861{
1862 struct udevice *bus;
1863
1864 /*
1865 * Enumerate all known controller devices. Enumeration has the side-
1866 * effect of probing them, so PCIe devices will be enumerated too.
1867 */
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001868 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warren04eb2692016-01-26 11:10:11 -07001869 bus;
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001870 uclass_next_device_check(&bus)) {
Stephen Warren04eb2692016-01-26 11:10:11 -07001871 ;
1872 }
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001873
1874 return 0;
Stephen Warren04eb2692016-01-26 11:10:11 -07001875}