blob: dfd54b339f45e0f0558ca95dd903ddc32fdc53ac [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
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Simon Glassb94dc892015-03-05 12:25:25 -070013#include <pci.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glassc5f053b2015-11-29 13:18:03 -070015#include <asm/io.h>
Simon Glassb94dc892015-03-05 12:25:25 -070016#include <dm/device-internal.h>
Simon Glass89d83232017-05-18 20:09:51 -060017#include <dm/lists.h>
Simon Glassbe706102020-12-16 21:20:18 -070018#include <dm/uclass-internal.h>
Bin Mengc0820a42015-08-20 06:40:23 -070019#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
Simon Glassef8a2dd2019-08-24 14:19:05 -060020#include <asm/fsp/fsp_support.h>
Bin Mengc0820a42015-08-20 06:40:23 -070021#endif
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Simon Glass37a3f94b2015-11-29 13:17:49 -070023#include "pci_internal.h"
Simon Glassb94dc892015-03-05 12:25:25 -070024
25DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass2e4e4432016-01-18 20:19:14 -070027int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass7d07e592015-08-31 18:55:35 -060028{
29 int ret;
30
31 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
32
33 /* Since buses may not be numbered yet try a little harder with bus 0 */
34 if (ret == -ENODEV) {
Simon Glassc7298e72016-02-11 13:23:26 -070035 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass7d07e592015-08-31 18:55:35 -060036 if (ret)
37 return ret;
Simon Glass7d07e592015-08-31 18:55:35 -060038 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
39 }
40
41 return ret;
42}
43
Simon Glass6256d672015-11-19 20:27:00 -070044struct udevice *pci_get_controller(struct udevice *dev)
45{
46 while (device_is_on_pci_bus(dev))
47 dev = dev->parent;
48
49 return dev;
50}
51
Simon Glassc92aac12020-01-27 08:49:38 -070052pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glassc9118d42015-07-06 16:47:46 -060053{
Simon Glassb75b15b2020-12-03 16:55:23 -070054 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassc9118d42015-07-06 16:47:46 -060055 struct udevice *bus = dev->parent;
56
Simon Glass1c6449c2019-12-29 21:19:14 -070057 /*
58 * This error indicates that @dev is a device on an unprobed PCI bus.
59 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
60 * will produce a bad BDF>
61 *
62 * A common cause of this problem is that this function is called in the
Simon Glassaad29ae2020-12-03 16:55:21 -070063 * of_to_plat() method of @dev. Accessing the PCI bus in that
Simon Glass1c6449c2019-12-29 21:19:14 -070064 * method is not allowed, since it has not yet been probed. To fix this,
65 * move that access to the probe() method of @dev instead.
66 */
67 if (!device_active(bus))
68 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
69 bus->name);
Simon Glass75e534b2020-12-16 21:20:07 -070070 return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
Simon Glassc9118d42015-07-06 16:47:46 -060071}
72
Simon Glassb94dc892015-03-05 12:25:25 -070073/**
74 * pci_get_bus_max() - returns the bus number of the last active bus
75 *
76 * @return last bus number, or -1 if no active buses
77 */
78static int pci_get_bus_max(void)
79{
80 struct udevice *bus;
81 struct uclass *uc;
82 int ret = -1;
83
84 ret = uclass_get(UCLASS_PCI, &uc);
85 uclass_foreach_dev(bus, uc) {
Simon Glass75e534b2020-12-16 21:20:07 -070086 if (dev_seq(bus) > ret)
87 ret = dev_seq(bus);
Simon Glassb94dc892015-03-05 12:25:25 -070088 }
89
90 debug("%s: ret=%d\n", __func__, ret);
91
92 return ret;
93}
94
95int pci_last_busno(void)
96{
Bin Meng5bc3f8a2015-10-01 00:36:01 -070097 return pci_get_bus_max();
Simon Glassb94dc892015-03-05 12:25:25 -070098}
99
100int pci_get_ff(enum pci_size_t size)
101{
102 switch (size) {
103 case PCI_SIZE_8:
104 return 0xff;
105 case PCI_SIZE_16:
106 return 0xffff;
107 default:
108 return 0xffffffff;
109 }
110}
111
Marek Vasutb4535792018-10-10 21:27:06 +0200112static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
113 ofnode *rnode)
114{
115 struct fdt_pci_addr addr;
116 ofnode node;
117 int ret;
118
119 dev_for_each_subnode(node, bus) {
120 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
121 &addr);
122 if (ret)
123 continue;
124
125 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
126 continue;
127
128 *rnode = node;
129 break;
130 }
131};
132
Simon Glass2a311e82020-01-27 08:49:37 -0700133int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassb94dc892015-03-05 12:25:25 -0700134 struct udevice **devp)
135{
136 struct udevice *dev;
137
138 for (device_find_first_child(bus, &dev);
139 dev;
140 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700141 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700142
Simon Glass71fa5b42020-12-03 16:55:18 -0700143 pplat = dev_get_parent_plat(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700144 if (pplat && pplat->devfn == find_devfn) {
145 *devp = dev;
146 return 0;
147 }
148 }
149
150 return -ENODEV;
151}
152
Simon Glass84283d52015-11-29 13:17:48 -0700153int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassb94dc892015-03-05 12:25:25 -0700154{
155 struct udevice *bus;
156 int ret;
157
Simon Glass7d07e592015-08-31 18:55:35 -0600158 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700159 if (ret)
160 return ret;
161 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
162}
163
164static int pci_device_matches_ids(struct udevice *dev,
165 struct pci_device_id *ids)
166{
Simon Glassb75b15b2020-12-03 16:55:23 -0700167 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700168 int i;
169
Simon Glass71fa5b42020-12-03 16:55:18 -0700170 pplat = dev_get_parent_plat(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700171 if (!pplat)
172 return -EINVAL;
173 for (i = 0; ids[i].vendor != 0; i++) {
174 if (pplat->vendor == ids[i].vendor &&
175 pplat->device == ids[i].device)
176 return i;
177 }
178
179 return -EINVAL;
180}
181
182int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
183 int *indexp, struct udevice **devp)
184{
185 struct udevice *dev;
186
187 /* Scan all devices on this bus */
188 for (device_find_first_child(bus, &dev);
189 dev;
190 device_find_next_child(&dev)) {
191 if (pci_device_matches_ids(dev, ids) >= 0) {
192 if ((*indexp)-- <= 0) {
193 *devp = dev;
194 return 0;
195 }
196 }
197 }
198
199 return -ENODEV;
200}
201
202int pci_find_device_id(struct pci_device_id *ids, int index,
203 struct udevice **devp)
204{
205 struct udevice *bus;
206
207 /* Scan all known buses */
208 for (uclass_first_device(UCLASS_PCI, &bus);
209 bus;
210 uclass_next_device(&bus)) {
211 if (!pci_bus_find_devices(bus, ids, &index, devp))
212 return 0;
213 }
214 *devp = NULL;
215
216 return -ENODEV;
217}
218
Simon Glass70e0c582015-11-29 13:17:50 -0700219static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
220 unsigned int device, int *indexp,
221 struct udevice **devp)
222{
Simon Glassb75b15b2020-12-03 16:55:23 -0700223 struct pci_child_plat *pplat;
Simon Glass70e0c582015-11-29 13:17:50 -0700224 struct udevice *dev;
225
226 for (device_find_first_child(bus, &dev);
227 dev;
228 device_find_next_child(&dev)) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700229 pplat = dev_get_parent_plat(dev);
Simon Glass70e0c582015-11-29 13:17:50 -0700230 if (pplat->vendor == vendor && pplat->device == device) {
231 if (!(*indexp)--) {
232 *devp = dev;
233 return 0;
234 }
235 }
236 }
237
238 return -ENODEV;
239}
240
241int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
242 struct udevice **devp)
243{
244 struct udevice *bus;
245
246 /* Scan all known buses */
247 for (uclass_first_device(UCLASS_PCI, &bus);
248 bus;
249 uclass_next_device(&bus)) {
250 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
251 return device_probe(*devp);
252 }
253 *devp = NULL;
254
255 return -ENODEV;
256}
257
Simon Glassb639d512015-11-29 13:17:52 -0700258int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
259{
260 struct udevice *dev;
261
262 /* Scan all known buses */
263 for (pci_find_first_device(&dev);
264 dev;
265 pci_find_next_device(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700266 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassb639d512015-11-29 13:17:52 -0700267
268 if (pplat->class == find_class && !index--) {
269 *devp = dev;
270 return device_probe(*devp);
271 }
272 }
273 *devp = NULL;
274
275 return -ENODEV;
276}
277
Simon Glassb94dc892015-03-05 12:25:25 -0700278int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
279 unsigned long value, enum pci_size_t size)
280{
281 struct dm_pci_ops *ops;
282
283 ops = pci_get_ops(bus);
284 if (!ops->write_config)
285 return -ENOSYS;
286 return ops->write_config(bus, bdf, offset, value, size);
287}
288
Simon Glass9cec2df2016-03-06 19:27:52 -0700289int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
290 u32 clr, u32 set)
291{
292 ulong val;
293 int ret;
294
295 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
296 if (ret)
297 return ret;
298 val &= ~clr;
299 val |= set;
300
301 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
302}
303
Simon Glassb94dc892015-03-05 12:25:25 -0700304int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
305 enum pci_size_t size)
306{
307 struct udevice *bus;
308 int ret;
309
Simon Glass7d07e592015-08-31 18:55:35 -0600310 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700311 if (ret)
312 return ret;
313
Bin Meng0a721522015-07-19 00:20:04 +0800314 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700315}
316
Simon Glass94ef2422015-08-10 07:05:03 -0600317int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
318 enum pci_size_t size)
319{
320 struct udevice *bus;
321
Bin Meng05bedb12015-09-11 03:24:34 -0700322 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600323 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700324 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
325 size);
Simon Glass94ef2422015-08-10 07:05:03 -0600326}
327
Simon Glassb94dc892015-03-05 12:25:25 -0700328int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
329{
330 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
331}
332
333int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
334{
335 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
336}
337
338int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
339{
340 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
341}
342
Simon Glass94ef2422015-08-10 07:05:03 -0600343int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
344{
345 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
346}
347
348int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
349{
350 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
351}
352
353int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
354{
355 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
356}
357
Simon Glassc92aac12020-01-27 08:49:38 -0700358int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassb94dc892015-03-05 12:25:25 -0700359 unsigned long *valuep, enum pci_size_t size)
360{
361 struct dm_pci_ops *ops;
362
363 ops = pci_get_ops(bus);
364 if (!ops->read_config)
365 return -ENOSYS;
366 return ops->read_config(bus, bdf, offset, valuep, size);
367}
368
369int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
370 enum pci_size_t size)
371{
372 struct udevice *bus;
373 int ret;
374
Simon Glass7d07e592015-08-31 18:55:35 -0600375 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700376 if (ret)
377 return ret;
378
Bin Meng0a721522015-07-19 00:20:04 +0800379 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700380}
381
Simon Glassc92aac12020-01-27 08:49:38 -0700382int dm_pci_read_config(const struct udevice *dev, int offset,
383 unsigned long *valuep, enum pci_size_t size)
Simon Glass94ef2422015-08-10 07:05:03 -0600384{
Simon Glassc92aac12020-01-27 08:49:38 -0700385 const struct udevice *bus;
Simon Glass94ef2422015-08-10 07:05:03 -0600386
Bin Meng05bedb12015-09-11 03:24:34 -0700387 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600388 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700389 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass94ef2422015-08-10 07:05:03 -0600390 size);
391}
392
Simon Glassb94dc892015-03-05 12:25:25 -0700393int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
394{
395 unsigned long value;
396 int ret;
397
398 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
399 if (ret)
400 return ret;
401 *valuep = value;
402
403 return 0;
404}
405
406int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
407{
408 unsigned long value;
409 int ret;
410
411 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
412 if (ret)
413 return ret;
414 *valuep = value;
415
416 return 0;
417}
418
419int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
420{
421 unsigned long value;
422 int ret;
423
424 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
425 if (ret)
426 return ret;
427 *valuep = value;
428
429 return 0;
430}
431
Simon Glassc92aac12020-01-27 08:49:38 -0700432int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600433{
434 unsigned long value;
435 int ret;
436
437 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
438 if (ret)
439 return ret;
440 *valuep = value;
441
442 return 0;
443}
444
Simon Glassc92aac12020-01-27 08:49:38 -0700445int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600446{
447 unsigned long value;
448 int ret;
449
450 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
451 if (ret)
452 return ret;
453 *valuep = value;
454
455 return 0;
456}
457
Simon Glassc92aac12020-01-27 08:49:38 -0700458int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600459{
460 unsigned long value;
461 int ret;
462
463 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
464 if (ret)
465 return ret;
466 *valuep = value;
467
468 return 0;
469}
470
Simon Glass9cec2df2016-03-06 19:27:52 -0700471int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
472{
473 u8 val;
474 int ret;
475
476 ret = dm_pci_read_config8(dev, offset, &val);
477 if (ret)
478 return ret;
479 val &= ~clr;
480 val |= set;
481
482 return dm_pci_write_config8(dev, offset, val);
483}
484
485int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
486{
487 u16 val;
488 int ret;
489
490 ret = dm_pci_read_config16(dev, offset, &val);
491 if (ret)
492 return ret;
493 val &= ~clr;
494 val |= set;
495
496 return dm_pci_write_config16(dev, offset, val);
497}
498
499int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
500{
501 u32 val;
502 int ret;
503
504 ret = dm_pci_read_config32(dev, offset, &val);
505 if (ret)
506 return ret;
507 val &= ~clr;
508 val |= set;
509
510 return dm_pci_write_config32(dev, offset, val);
511}
512
Bin Menga0705782015-10-01 00:36:02 -0700513static void set_vga_bridge_bits(struct udevice *dev)
514{
515 struct udevice *parent = dev->parent;
516 u16 bc;
517
Simon Glass75e534b2020-12-16 21:20:07 -0700518 while (dev_seq(parent) != 0) {
Bin Menga0705782015-10-01 00:36:02 -0700519 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
520 bc |= PCI_BRIDGE_CTL_VGA;
521 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
522 parent = parent->parent;
523 }
524}
525
Simon Glassb94dc892015-03-05 12:25:25 -0700526int pci_auto_config_devices(struct udevice *bus)
527{
Simon Glass95588622020-12-22 19:30:28 -0700528 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700529 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700530 unsigned int sub_bus;
531 struct udevice *dev;
532 int ret;
533
Simon Glass75e534b2020-12-16 21:20:07 -0700534 sub_bus = dev_seq(bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700535 debug("%s: start\n", __func__);
536 pciauto_config_init(hose);
537 for (ret = device_find_first_child(bus, &dev);
538 !ret && dev;
539 ret = device_find_next_child(&dev)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700540 unsigned int max_bus;
Simon Glassb072d522015-09-08 17:52:47 -0600541 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700542
Simon Glassb94dc892015-03-05 12:25:25 -0700543 debug("%s: device %s\n", __func__, dev->name);
Simon Glassf1d50f72020-12-19 10:40:13 -0700544 if (dev_has_ofnode(dev) &&
Suneel Garapatif8c86282020-05-04 21:25:25 -0700545 dev_read_bool(dev, "pci,no-autoconfig"))
Simon Glassf3005fb2020-04-08 16:57:26 -0600546 continue;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700547 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600548 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -0700549 return log_msg_ret("auto", ret);
Simon Glassb072d522015-09-08 17:52:47 -0600550 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700551 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700552
Simon Glass71fa5b42020-12-03 16:55:18 -0700553 pplat = dev_get_parent_plat(dev);
Bin Menga0705782015-10-01 00:36:02 -0700554 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
555 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700556 }
557 debug("%s: done\n", __func__);
558
Simon Glassbe706102020-12-16 21:20:18 -0700559 return log_msg_ret("sub", sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700560}
561
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300562int pci_generic_mmap_write_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700563 const struct udevice *bus,
564 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
565 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300566 pci_dev_t bdf,
567 uint offset,
568 ulong value,
569 enum pci_size_t size)
570{
571 void *address;
572
573 if (addr_f(bus, bdf, offset, &address) < 0)
574 return 0;
575
576 switch (size) {
577 case PCI_SIZE_8:
578 writeb(value, address);
579 return 0;
580 case PCI_SIZE_16:
581 writew(value, address);
582 return 0;
583 case PCI_SIZE_32:
584 writel(value, address);
585 return 0;
586 default:
587 return -EINVAL;
588 }
589}
590
591int pci_generic_mmap_read_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700592 const struct udevice *bus,
593 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
594 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300595 pci_dev_t bdf,
596 uint offset,
597 ulong *valuep,
598 enum pci_size_t size)
599{
600 void *address;
601
602 if (addr_f(bus, bdf, offset, &address) < 0) {
603 *valuep = pci_get_ff(size);
604 return 0;
605 }
606
607 switch (size) {
608 case PCI_SIZE_8:
609 *valuep = readb(address);
610 return 0;
611 case PCI_SIZE_16:
612 *valuep = readw(address);
613 return 0;
614 case PCI_SIZE_32:
615 *valuep = readl(address);
616 return 0;
617 default:
618 return -EINVAL;
619 }
620}
621
Simon Glass37a3f94b2015-11-29 13:17:49 -0700622int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700623{
Simon Glassb94dc892015-03-05 12:25:25 -0700624 int sub_bus;
625 int ret;
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700626 int ea_pos;
627 u8 reg;
Simon Glassb94dc892015-03-05 12:25:25 -0700628
629 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700630
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700631 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
632 if (ea_pos) {
633 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
634 &reg);
635 sub_bus = reg;
636 } else {
637 sub_bus = pci_get_bus_max() + 1;
638 }
Simon Glassb94dc892015-03-05 12:25:25 -0700639 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700640 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700641
642 ret = device_probe(bus);
643 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600644 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700645 ret);
Simon Glassbe706102020-12-16 21:20:18 -0700646 return log_msg_ret("probe", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700647 }
Suneel Garapati1b9c44e2019-10-19 15:52:32 -0700648
Simon Glass37a3f94b2015-11-29 13:17:49 -0700649 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700650
651 return sub_bus;
652}
653
Simon Glass318d71c2015-07-06 16:47:44 -0600654/**
655 * pci_match_one_device - Tell if a PCI device structure has a matching
656 * PCI device id structure
657 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800658 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600659 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800660 * Returns true if the finding pci_device_id structure matched or false if
661 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600662 */
663static bool pci_match_one_id(const struct pci_device_id *id,
664 const struct pci_device_id *find)
665{
666 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
667 (id->device == PCI_ANY_ID || id->device == find->device) &&
668 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
669 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
670 !((id->class ^ find->class) & id->class_mask))
671 return true;
672
673 return false;
674}
675
676/**
677 * pci_find_and_bind_driver() - Find and bind the right PCI driver
678 *
679 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600680 *
681 * @parent: Parent bus
682 * @find_id: Specification of the driver to find
683 * @bdf: Bus/device/function addreess - see PCI_BDF()
684 * @devp: Returns a pointer to the device created
685 * @return 0 if OK, -EPERM if the device is not needed before relocation and
686 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600687 */
688static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600689 struct pci_device_id *find_id,
690 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600691{
692 struct pci_driver_entry *start, *entry;
Marek Vasutb4535792018-10-10 21:27:06 +0200693 ofnode node = ofnode_null();
Simon Glass318d71c2015-07-06 16:47:44 -0600694 const char *drv;
695 int n_ents;
696 int ret;
697 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700698 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600699
700 *devp = NULL;
701
702 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
703 find_id->vendor, find_id->device);
Marek Vasutb4535792018-10-10 21:27:06 +0200704
705 /* Determine optional OF node */
Suneel Garapaticb7093d2019-10-19 16:02:48 -0700706 if (ofnode_valid(dev_ofnode(parent)))
707 pci_dev_find_ofnode(parent, bdf, &node);
Marek Vasutb4535792018-10-10 21:27:06 +0200708
Michael Walle2e21f372019-12-01 17:45:18 +0100709 if (ofnode_valid(node) && !ofnode_is_available(node)) {
710 debug("%s: Ignoring disabled device\n", __func__);
Simon Glassbe706102020-12-16 21:20:18 -0700711 return log_msg_ret("dis", -EPERM);
Michael Walle2e21f372019-12-01 17:45:18 +0100712 }
713
Simon Glass318d71c2015-07-06 16:47:44 -0600714 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
715 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
716 for (entry = start; entry != start + n_ents; entry++) {
717 const struct pci_device_id *id;
718 struct udevice *dev;
719 const struct driver *drv;
720
721 for (id = entry->match;
722 id->vendor || id->subvendor || id->class_mask;
723 id++) {
724 if (!pci_match_one_id(id, find_id))
725 continue;
726
727 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700728
729 /*
730 * In the pre-relocation phase, we only bind devices
731 * whose driver has the DM_FLAG_PRE_RELOC set, to save
732 * precious memory space as on some platforms as that
733 * space is pretty limited (ie: using Cache As RAM).
734 */
735 if (!(gd->flags & GD_FLG_RELOC) &&
736 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassbe706102020-12-16 21:20:18 -0700737 return log_msg_ret("pre", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700738
Simon Glass318d71c2015-07-06 16:47:44 -0600739 /*
740 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700741 * plat (instead of NULL) and allow its bind()
Simon Glass318d71c2015-07-06 16:47:44 -0600742 * method to return -ENOENT if it doesn't support this
743 * device. That way we could continue the search to
744 * find another driver. For now this doesn't seem
745 * necesssary, so just bind the first match.
746 */
Simon Glass884870f2020-11-28 17:50:01 -0700747 ret = device_bind(parent, drv, drv->name, NULL, node,
748 &dev);
Simon Glass318d71c2015-07-06 16:47:44 -0600749 if (ret)
750 goto error;
751 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700752 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600753 *devp = dev;
754 return 0;
755 }
756 }
757
Bin Meng984c0dc2015-08-20 06:40:17 -0700758 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
759 /*
760 * In the pre-relocation phase, we only bind bridge devices to save
761 * precious memory space as on some platforms as that space is pretty
762 * limited (ie: using Cache As RAM).
763 */
764 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glassbe706102020-12-16 21:20:18 -0700765 return log_msg_ret("notbr", -EPERM);
Bin Meng984c0dc2015-08-20 06:40:17 -0700766
Simon Glass318d71c2015-07-06 16:47:44 -0600767 /* Bind a generic driver so that the device can be used */
Simon Glass75e534b2020-12-16 21:20:07 -0700768 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
Bin Meng0a721522015-07-19 00:20:04 +0800769 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600770 str = strdup(name);
771 if (!str)
772 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700773 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
774
Marek Vasutb4535792018-10-10 21:27:06 +0200775 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glass318d71c2015-07-06 16:47:44 -0600776 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600777 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200778 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600779 return ret;
780 }
781 debug("%s: No match found: bound generic driver instead\n", __func__);
782
783 return 0;
784
785error:
786 debug("%s: No match found: error %d\n", __func__, ret);
787 return ret;
788}
789
Simon Glassb94dc892015-03-05 12:25:25 -0700790int pci_bind_bus_devices(struct udevice *bus)
791{
792 ulong vendor, device;
793 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800794 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700795 bool found_multi;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700796 int ari_off;
Simon Glassb94dc892015-03-05 12:25:25 -0700797 int ret;
798
799 found_multi = false;
Simon Glass75e534b2020-12-16 21:20:07 -0700800 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
Bin Meng0a721522015-07-19 00:20:04 +0800801 PCI_MAX_PCI_FUNCTIONS - 1);
Simon Glass75e534b2020-12-16 21:20:07 -0700802 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800803 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700804 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700805 struct udevice *dev;
806 ulong class;
807
Bin Meng20bdc1e2018-08-03 01:14:37 -0700808 if (!PCI_FUNC(bdf))
809 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800810 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700811 continue;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800812
Simon Glassb94dc892015-03-05 12:25:25 -0700813 /* Check only the first access, we don't expect problems */
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800814 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
815 PCI_SIZE_16);
Simon Glassb94dc892015-03-05 12:25:25 -0700816 if (ret)
817 goto error;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800818
Simon Glassb94dc892015-03-05 12:25:25 -0700819 if (vendor == 0xffff || vendor == 0x0000)
820 continue;
821
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800822 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
823 &header_type, PCI_SIZE_8);
824
Bin Meng0a721522015-07-19 00:20:04 +0800825 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700826 found_multi = header_type & 0x80;
827
Simon Glass25916d62019-09-25 08:56:12 -0600828 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -0700829 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Bin Meng0a721522015-07-19 00:20:04 +0800830 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700831 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800832 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600833 PCI_SIZE_32);
834 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700835
836 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800837 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass25916d62019-09-25 08:56:12 -0600838 debug(": find ret=%d\n", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700839
Simon Glass413ebdb2015-11-29 13:18:09 -0700840 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700841 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600842 struct pci_device_id find_id;
843 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700844
Simon Glass318d71c2015-07-06 16:47:44 -0600845 memset(&find_id, '\0', sizeof(find_id));
846 find_id.vendor = vendor;
847 find_id.device = device;
848 find_id.class = class;
849 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800850 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600851 PCI_SUBSYSTEM_VENDOR_ID,
852 &val, PCI_SIZE_32);
853 find_id.subvendor = val & 0xffff;
854 find_id.subdevice = val >> 16;
855 }
Bin Meng0a721522015-07-19 00:20:04 +0800856 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600857 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700858 }
Simon Glassc45abf12015-09-08 17:52:49 -0600859 if (ret == -EPERM)
860 continue;
861 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700862 return ret;
863
864 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -0700865 pplat = dev_get_parent_plat(dev);
Simon Glassc45abf12015-09-08 17:52:49 -0600866 pplat->devfn = PCI_MASK_BUS(bdf);
867 pplat->vendor = vendor;
868 pplat->device = device;
869 pplat->class = class;
Suneel Garapatia99a5eb2019-10-23 18:40:36 -0700870
871 if (IS_ENABLED(CONFIG_PCI_ARID)) {
872 ari_off = dm_pci_find_ext_capability(dev,
873 PCI_EXT_CAP_ID_ARI);
874 if (ari_off) {
875 u16 ari_cap;
876
877 /*
878 * Read Next Function number in ARI Cap
879 * Register
880 */
881 dm_pci_read_config16(dev, ari_off + 4,
882 &ari_cap);
883 /*
884 * Update next scan on this function number,
885 * subtract 1 in BDF to satisfy loop increment.
886 */
887 if (ari_cap & 0xff00) {
888 bdf = PCI_BDF(PCI_BUS(bdf),
889 PCI_DEV(ari_cap),
890 PCI_FUNC(ari_cap));
891 bdf = bdf - 0x100;
892 }
893 }
894 }
Simon Glassb94dc892015-03-05 12:25:25 -0700895 }
896
897 return 0;
898error:
899 printf("Cannot read bus configuration: %d\n", ret);
900
901 return ret;
902}
903
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700904static void decode_regions(struct pci_controller *hose, ofnode parent_node,
905 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700906{
907 int pci_addr_cells, addr_cells, size_cells;
908 int cells_per_record;
Stefan Roesebbc88462020-08-12 11:55:46 +0200909 struct bd_info *bd;
Simon Glassb94dc892015-03-05 12:25:25 -0700910 const u32 *prop;
Stefan Roese950864f2020-07-23 16:34:10 +0200911 int max_regions;
Simon Glassb94dc892015-03-05 12:25:25 -0700912 int len;
913 int i;
914
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900915 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700916 if (!prop) {
917 debug("%s: Cannot decode regions\n", __func__);
918 return;
919 }
920
Simon Glass4191dc12017-06-12 06:21:31 -0600921 pci_addr_cells = ofnode_read_simple_addr_cells(node);
922 addr_cells = ofnode_read_simple_addr_cells(parent_node);
923 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700924
925 /* PCI addresses are always 3-cells */
926 len /= sizeof(u32);
927 cells_per_record = pci_addr_cells + addr_cells + size_cells;
928 hose->region_count = 0;
929 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
930 cells_per_record);
Stefan Roese950864f2020-07-23 16:34:10 +0200931
932 /* Dynamically allocate the regions array */
933 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
934 hose->regions = (struct pci_region *)
935 calloc(1, max_regions * sizeof(struct pci_region));
936
937 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassb94dc892015-03-05 12:25:25 -0700938 u64 pci_addr, addr, size;
939 int space_code;
940 u32 flags;
941 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -0700942 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -0700943
944 if (len < cells_per_record)
945 break;
946 flags = fdt32_to_cpu(prop[0]);
947 space_code = (flags >> 24) & 3;
948 pci_addr = fdtdec_get_number(prop + 1, 2);
949 prop += pci_addr_cells;
950 addr = fdtdec_get_number(prop, addr_cells);
951 prop += addr_cells;
952 size = fdtdec_get_number(prop, size_cells);
953 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900954 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
955 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -0700956 if (space_code & 2) {
957 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
958 PCI_REGION_MEM;
959 } else if (space_code & 1) {
960 type = PCI_REGION_IO;
961 } else {
962 continue;
963 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +0300964
965 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
966 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
967 debug(" - beyond the 32-bit boundary, ignoring\n");
968 continue;
969 }
970
Simon Glass7efc9ba2015-11-19 20:26:58 -0700971 pos = -1;
Suneel Garapati3ac3aec2019-10-19 17:10:20 -0700972 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
973 for (i = 0; i < hose->region_count; i++) {
974 if (hose->regions[i].flags == type)
975 pos = i;
976 }
Simon Glass7efc9ba2015-11-19 20:26:58 -0700977 }
Suneel Garapati3ac3aec2019-10-19 17:10:20 -0700978
Simon Glass7efc9ba2015-11-19 20:26:58 -0700979 if (pos == -1)
980 pos = hose->region_count++;
981 debug(" - type=%d, pos=%d\n", type, pos);
982 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -0700983 }
984
985 /* Add a region for our local memory */
Stefan Roesebbc88462020-08-12 11:55:46 +0200986 bd = gd->bd;
Bin Mengae0bdde2018-03-27 00:46:05 -0700987 if (!bd)
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700988 return;
Bin Mengae0bdde2018-03-27 00:46:05 -0700989
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100990 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
991 if (bd->bi_dram[i].size) {
992 pci_set_region(hose->regions + hose->region_count++,
993 bd->bi_dram[i].start,
994 bd->bi_dram[i].start,
995 bd->bi_dram[i].size,
996 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
997 }
998 }
Simon Glassb94dc892015-03-05 12:25:25 -0700999
Christian Gmeiner5f4e0942018-06-10 06:25:05 -07001000 return;
Simon Glassb94dc892015-03-05 12:25:25 -07001001}
1002
1003static int pci_uclass_pre_probe(struct udevice *bus)
1004{
1005 struct pci_controller *hose;
Simon Glassbe706102020-12-16 21:20:18 -07001006 struct uclass *uc;
1007 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -07001008
Simon Glass75e534b2020-12-16 21:20:07 -07001009 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -07001010 bus->parent->name);
Simon Glass95588622020-12-22 19:30:28 -07001011 hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001012
Simon Glassbe706102020-12-16 21:20:18 -07001013 /*
1014 * Set the sequence number, if device_bind() doesn't. We want control
1015 * of this so that numbers are allocated as devices are probed. That
1016 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1017 * higher than their parents)
1018 */
1019 if (dev_seq(bus) == -1) {
1020 ret = uclass_get(UCLASS_PCI, &uc);
1021 if (ret)
1022 return ret;
Simon Glass5e349922020-12-19 10:40:09 -07001023 bus->seq_ = uclass_find_next_free_seq(uc);
Simon Glassbe706102020-12-16 21:20:18 -07001024 }
1025
Simon Glassb94dc892015-03-05 12:25:25 -07001026 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +01001027 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -07001028 hose->ctlr = bus;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -07001029 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001030 } else {
1031 struct pci_controller *parent_hose;
1032
1033 parent_hose = dev_get_uclass_priv(bus->parent);
1034 hose->ctlr = parent_hose->bus;
1035 }
Simon Glassbe706102020-12-16 21:20:18 -07001036
Simon Glassb94dc892015-03-05 12:25:25 -07001037 hose->bus = bus;
Simon Glass75e534b2020-12-16 21:20:07 -07001038 hose->first_busno = dev_seq(bus);
1039 hose->last_busno = dev_seq(bus);
Simon Glassf1d50f72020-12-19 10:40:13 -07001040 if (dev_has_ofnode(bus)) {
Suneel Garapatif8c86282020-05-04 21:25:25 -07001041 hose->skip_auto_config_until_reloc =
1042 dev_read_bool(bus,
1043 "u-boot,skip-auto-config-until-reloc");
1044 }
Simon Glassb94dc892015-03-05 12:25:25 -07001045
1046 return 0;
1047}
1048
1049static int pci_uclass_post_probe(struct udevice *bus)
1050{
Simon Glass68e35a72019-12-06 21:41:37 -07001051 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001052 int ret;
1053
Simon Glass75e534b2020-12-16 21:20:07 -07001054 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
Simon Glassb94dc892015-03-05 12:25:25 -07001055 ret = pci_bind_bus_devices(bus);
1056 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001057 return log_msg_ret("bind", ret);
Simon Glassb94dc892015-03-05 12:25:25 -07001058
Simon Glassbd165e72020-04-26 09:12:56 -06001059 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass68e35a72019-12-06 21:41:37 -07001060 (!hose->skip_auto_config_until_reloc ||
1061 (gd->flags & GD_FLG_RELOC))) {
1062 ret = pci_auto_config_devices(bus);
1063 if (ret < 0)
Simon Glassbe706102020-12-16 21:20:18 -07001064 return log_msg_ret("cfg", ret);
Simon Glass68e35a72019-12-06 21:41:37 -07001065 }
Simon Glassb94dc892015-03-05 12:25:25 -07001066
Bin Mengc0820a42015-08-20 06:40:23 -07001067#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1068 /*
1069 * Per Intel FSP specification, we should call FSP notify API to
1070 * inform FSP that PCI enumeration has been done so that FSP will
1071 * do any necessary initialization as required by the chipset's
1072 * BIOS Writer's Guide (BWG).
1073 *
1074 * Unfortunately we have to put this call here as with driver model,
1075 * the enumeration is all done on a lazy basis as needed, so until
1076 * something is touched on PCI it won't happen.
1077 *
1078 * Note we only call this 1) after U-Boot is relocated, and 2)
1079 * root bus has finished probing.
1080 */
Simon Glass75e534b2020-12-16 21:20:07 -07001081 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
Bin Mengc0820a42015-08-20 06:40:23 -07001082 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -06001083 if (ret)
Simon Glassbe706102020-12-16 21:20:18 -07001084 return log_msg_ret("fsp", ret);
Simon Glassb072d522015-09-08 17:52:47 -06001085 }
Bin Mengc0820a42015-08-20 06:40:23 -07001086#endif
1087
Simon Glassb072d522015-09-08 17:52:47 -06001088 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001089}
1090
1091static int pci_uclass_child_post_bind(struct udevice *dev)
1092{
Simon Glassb75b15b2020-12-03 16:55:23 -07001093 struct pci_child_plat *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -07001094
Simon Glassf1d50f72020-12-19 10:40:13 -07001095 if (!dev_has_ofnode(dev))
Simon Glassb94dc892015-03-05 12:25:25 -07001096 return 0;
1097
Simon Glass71fa5b42020-12-03 16:55:18 -07001098 pplat = dev_get_parent_plat(dev);
Bin Meng00d808e2018-08-03 01:14:36 -07001099
1100 /* Extract vendor id and device id if available */
1101 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1102
1103 /* Extract the devfn from fdt_pci_addr */
Stefan Roesea74eb552019-01-25 11:52:42 +01001104 pplat->devfn = pci_get_devfn(dev);
Simon Glassb94dc892015-03-05 12:25:25 -07001105
1106 return 0;
1107}
1108
Simon Glass2a311e82020-01-27 08:49:37 -07001109static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng0a721522015-07-19 00:20:04 +08001110 uint offset, ulong *valuep,
1111 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001112{
Simon Glass95588622020-12-22 19:30:28 -07001113 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001114
1115 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1116}
1117
Bin Meng0a721522015-07-19 00:20:04 +08001118static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1119 uint offset, ulong value,
1120 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001121{
Simon Glass95588622020-12-22 19:30:28 -07001122 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001123
1124 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1125}
1126
Simon Glass04c8b6a2015-08-10 07:05:04 -06001127static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1128{
1129 struct udevice *dev;
1130 int ret = 0;
1131
1132 /*
1133 * Scan through all the PCI controllers. On x86 there will only be one
1134 * but that is not necessarily true on other hardware.
1135 */
1136 do {
1137 device_find_first_child(bus, &dev);
1138 if (dev) {
1139 *devp = dev;
1140 return 0;
1141 }
1142 ret = uclass_next_device(&bus);
1143 if (ret)
1144 return ret;
1145 } while (bus);
1146
1147 return 0;
1148}
1149
1150int pci_find_next_device(struct udevice **devp)
1151{
1152 struct udevice *child = *devp;
1153 struct udevice *bus = child->parent;
1154 int ret;
1155
1156 /* First try all the siblings */
1157 *devp = NULL;
1158 while (child) {
1159 device_find_next_child(&child);
1160 if (child) {
1161 *devp = child;
1162 return 0;
1163 }
1164 }
1165
1166 /* We ran out of siblings. Try the next bus */
1167 ret = uclass_next_device(&bus);
1168 if (ret)
1169 return ret;
1170
1171 return bus ? skip_to_next_device(bus, devp) : 0;
1172}
1173
1174int pci_find_first_device(struct udevice **devp)
1175{
1176 struct udevice *bus;
1177 int ret;
1178
1179 *devp = NULL;
1180 ret = uclass_first_device(UCLASS_PCI, &bus);
1181 if (ret)
1182 return ret;
1183
1184 return skip_to_next_device(bus, devp);
1185}
1186
Simon Glass27a733f2015-11-19 20:26:59 -07001187ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1188{
1189 switch (size) {
1190 case PCI_SIZE_8:
1191 return (value >> ((offset & 3) * 8)) & 0xff;
1192 case PCI_SIZE_16:
1193 return (value >> ((offset & 2) * 8)) & 0xffff;
1194 default:
1195 return value;
1196 }
1197}
1198
1199ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1200 enum pci_size_t size)
1201{
1202 uint off_mask;
1203 uint val_mask, shift;
1204 ulong ldata, mask;
1205
1206 switch (size) {
1207 case PCI_SIZE_8:
1208 off_mask = 3;
1209 val_mask = 0xff;
1210 break;
1211 case PCI_SIZE_16:
1212 off_mask = 2;
1213 val_mask = 0xffff;
1214 break;
1215 default:
1216 return value;
1217 }
1218 shift = (offset & off_mask) * 8;
1219 ldata = (value & val_mask) << shift;
1220 mask = val_mask << shift;
1221 value = (old & ~mask) | ldata;
1222
1223 return value;
1224}
1225
Rayagonda Kokatanurcdc7ed32020-05-12 13:29:49 +05301226int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1227{
1228 int pci_addr_cells, addr_cells, size_cells;
1229 int cells_per_record;
1230 const u32 *prop;
1231 int len;
1232 int i = 0;
1233
1234 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1235 if (!prop) {
1236 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1237 dev->name);
1238 return -EINVAL;
1239 }
1240
1241 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1242 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1243 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1244
1245 /* PCI addresses are always 3-cells */
1246 len /= sizeof(u32);
1247 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1248 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1249 cells_per_record);
1250
1251 while (len) {
1252 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1253 prop += pci_addr_cells;
1254 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1255 prop += addr_cells;
1256 memp->size = fdtdec_get_number(prop, size_cells);
1257 prop += size_cells;
1258
1259 if (i == index)
1260 return 0;
1261 i++;
1262 len -= cells_per_record;
1263 }
1264
1265 return -EINVAL;
1266}
1267
Simon Glassdcdc0122015-11-19 20:27:01 -07001268int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1269 struct pci_region **memp, struct pci_region **prefp)
1270{
1271 struct udevice *bus = pci_get_controller(dev);
1272 struct pci_controller *hose = dev_get_uclass_priv(bus);
1273 int i;
1274
1275 *iop = NULL;
1276 *memp = NULL;
1277 *prefp = NULL;
1278 for (i = 0; i < hose->region_count; i++) {
1279 switch (hose->regions[i].flags) {
1280 case PCI_REGION_IO:
1281 if (!*iop || (*iop)->size < hose->regions[i].size)
1282 *iop = hose->regions + i;
1283 break;
1284 case PCI_REGION_MEM:
1285 if (!*memp || (*memp)->size < hose->regions[i].size)
1286 *memp = hose->regions + i;
1287 break;
1288 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1289 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1290 *prefp = hose->regions + i;
1291 break;
1292 }
1293 }
1294
1295 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1296}
1297
Simon Glassc92aac12020-01-27 08:49:38 -07001298u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glass3452cb12015-11-29 13:17:53 -07001299{
1300 u32 addr;
1301 int bar;
1302
1303 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1304 dm_pci_read_config32(dev, bar, &addr);
Simon Glass71fafd12020-04-09 10:27:36 -06001305
1306 /*
1307 * If we get an invalid address, return this so that comparisons with
1308 * FDT_ADDR_T_NONE work correctly
1309 */
1310 if (addr == 0xffffffff)
1311 return addr;
1312 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glass3452cb12015-11-29 13:17:53 -07001313 return addr & PCI_BASE_ADDRESS_IO_MASK;
1314 else
1315 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1316}
1317
Simon Glasse2b6b562016-01-18 20:19:15 -07001318void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1319{
1320 int bar;
1321
1322 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1323 dm_pci_write_config32(dev, bar, addr);
1324}
1325
Simon Glassc5f053b2015-11-29 13:18:03 -07001326static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1327 pci_addr_t bus_addr, unsigned long flags,
1328 unsigned long skip_mask, phys_addr_t *pa)
1329{
1330 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1331 struct pci_region *res;
1332 int i;
1333
Christian Gmeiner7241f802018-06-10 06:25:06 -07001334 if (hose->region_count == 0) {
1335 *pa = bus_addr;
1336 return 0;
1337 }
1338
Simon Glassc5f053b2015-11-29 13:18:03 -07001339 for (i = 0; i < hose->region_count; i++) {
1340 res = &hose->regions[i];
1341
1342 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1343 continue;
1344
1345 if (res->flags & skip_mask)
1346 continue;
1347
1348 if (bus_addr >= res->bus_start &&
1349 (bus_addr - res->bus_start) < res->size) {
1350 *pa = (bus_addr - res->bus_start + res->phys_start);
1351 return 0;
1352 }
1353 }
1354
1355 return 1;
1356}
1357
1358phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1359 unsigned long flags)
1360{
1361 phys_addr_t phys_addr = 0;
1362 struct udevice *ctlr;
1363 int ret;
1364
1365 /* The root controller has the region information */
1366 ctlr = pci_get_controller(dev);
1367
1368 /*
1369 * if PCI_REGION_MEM is set we do a two pass search with preference
1370 * on matches that don't have PCI_REGION_SYS_MEMORY set
1371 */
1372 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1373 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1374 flags, PCI_REGION_SYS_MEMORY,
1375 &phys_addr);
1376 if (!ret)
1377 return phys_addr;
1378 }
1379
1380 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1381
1382 if (ret)
1383 puts("pci_hose_bus_to_phys: invalid physical address\n");
1384
1385 return phys_addr;
1386}
1387
1388int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1389 unsigned long flags, unsigned long skip_mask,
1390 pci_addr_t *ba)
1391{
1392 struct pci_region *res;
1393 struct udevice *ctlr;
1394 pci_addr_t bus_addr;
1395 int i;
1396 struct pci_controller *hose;
1397
1398 /* The root controller has the region information */
1399 ctlr = pci_get_controller(dev);
1400 hose = dev_get_uclass_priv(ctlr);
1401
Christian Gmeiner7241f802018-06-10 06:25:06 -07001402 if (hose->region_count == 0) {
1403 *ba = phys_addr;
1404 return 0;
1405 }
1406
Simon Glassc5f053b2015-11-29 13:18:03 -07001407 for (i = 0; i < hose->region_count; i++) {
1408 res = &hose->regions[i];
1409
1410 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1411 continue;
1412
1413 if (res->flags & skip_mask)
1414 continue;
1415
1416 bus_addr = phys_addr - res->phys_start + res->bus_start;
1417
1418 if (bus_addr >= res->bus_start &&
1419 (bus_addr - res->bus_start) < res->size) {
1420 *ba = bus_addr;
1421 return 0;
1422 }
1423 }
1424
1425 return 1;
1426}
1427
1428pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1429 unsigned long flags)
1430{
1431 pci_addr_t bus_addr = 0;
1432 int ret;
1433
1434 /*
1435 * if PCI_REGION_MEM is set we do a two pass search with preference
1436 * on matches that don't have PCI_REGION_SYS_MEMORY set
1437 */
1438 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1439 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1440 PCI_REGION_SYS_MEMORY, &bus_addr);
1441 if (!ret)
1442 return bus_addr;
1443 }
1444
1445 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1446
1447 if (ret)
1448 puts("pci_hose_phys_to_bus: invalid physical address\n");
1449
1450 return bus_addr;
1451}
1452
Suneel Garapati5858ba82019-10-19 16:34:16 -07001453static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
Simon Glassb75b15b2020-12-03 16:55:23 -07001454 struct pci_child_plat *pdata)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001455{
1456 phys_addr_t addr = 0;
1457
1458 /*
1459 * In the case of a Virtual Function device using BAR
1460 * base and size, add offset for VFn BAR(1, 2, 3...n)
1461 */
1462 if (pdata->is_virtfn) {
1463 size_t sz;
1464 u32 ea_entry;
1465
1466 /* MaxOffset, 1st DW */
1467 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1468 sz = ea_entry & PCI_EA_FIELD_MASK;
1469 /* Fill up lower 2 bits */
1470 sz |= (~PCI_EA_FIELD_MASK);
1471
1472 if (ea_entry & PCI_EA_IS_64) {
1473 /* MaxOffset 2nd DW */
1474 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1475 sz |= ((u64)ea_entry) << 32;
1476 }
1477
1478 addr = (pdata->virtid - 1) * (sz + 1);
1479 }
1480
1481 return addr;
1482}
1483
Alex Marginean1c934a62019-06-07 11:24:23 +03001484static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
Simon Glassb75b15b2020-12-03 16:55:23 -07001485 int ea_off, struct pci_child_plat *pdata)
Alex Marginean1c934a62019-06-07 11:24:23 +03001486{
1487 int ea_cnt, i, entry_size;
1488 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1489 u32 ea_entry;
1490 phys_addr_t addr;
1491
Suneel Garapati5858ba82019-10-19 16:34:16 -07001492 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1493 /*
1494 * In the case of a Virtual Function device, device is
1495 * Physical function, so pdata will point to required VF
1496 * specific data.
1497 */
1498 if (pdata->is_virtfn)
1499 bar_id += PCI_EA_BEI_VF_BAR0;
1500 }
1501
Alex Marginean1c934a62019-06-07 11:24:23 +03001502 /* EA capability structure header */
1503 dm_pci_read_config32(dev, ea_off, &ea_entry);
1504 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1505 ea_off += PCI_EA_FIRST_ENT;
1506
1507 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1508 /* Entry header */
1509 dm_pci_read_config32(dev, ea_off, &ea_entry);
1510 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1511
1512 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1513 continue;
1514
1515 /* Base address, 1st DW */
1516 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1517 addr = ea_entry & PCI_EA_FIELD_MASK;
1518 if (ea_entry & PCI_EA_IS_64) {
1519 /* Base address, 2nd DW, skip over 4B MaxOffset */
1520 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1521 addr |= ((u64)ea_entry) << 32;
1522 }
1523
Suneel Garapati5858ba82019-10-19 16:34:16 -07001524 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1525 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1526
Alex Marginean1c934a62019-06-07 11:24:23 +03001527 /* size ignored for now */
Suneel Garapati47f19622019-10-19 16:44:35 -07001528 return map_physmem(addr, 0, flags);
Alex Marginean1c934a62019-06-07 11:24:23 +03001529 }
1530
1531 return 0;
1532}
1533
Simon Glassc5f053b2015-11-29 13:18:03 -07001534void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1535{
Simon Glassb75b15b2020-12-03 16:55:23 -07001536 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
Suneel Garapati5858ba82019-10-19 16:34:16 -07001537 struct udevice *udev = dev;
Simon Glassc5f053b2015-11-29 13:18:03 -07001538 pci_addr_t pci_bus_addr;
1539 u32 bar_response;
Alex Marginean1c934a62019-06-07 11:24:23 +03001540 int ea_off;
1541
Suneel Garapati5858ba82019-10-19 16:34:16 -07001542 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1543 /*
1544 * In case of Virtual Function devices, use PF udevice
1545 * as EA capability is defined in Physical Function
1546 */
1547 if (pdata->is_virtfn)
1548 udev = pdata->pfdev;
1549 }
1550
Alex Marginean1c934a62019-06-07 11:24:23 +03001551 /*
1552 * if the function supports Enhanced Allocation use that instead of
1553 * BARs
Suneel Garapati5858ba82019-10-19 16:34:16 -07001554 * Incase of virtual functions, pdata will help read VF BEI
1555 * and EA entry size.
Alex Marginean1c934a62019-06-07 11:24:23 +03001556 */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001557 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
Alex Marginean1c934a62019-06-07 11:24:23 +03001558 if (ea_off)
Suneel Garapati5858ba82019-10-19 16:34:16 -07001559 return dm_pci_map_ea_bar(udev, bar, flags, ea_off, pdata);
Simon Glassc5f053b2015-11-29 13:18:03 -07001560
1561 /* read BAR address */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001562 dm_pci_read_config32(udev, bar, &bar_response);
Simon Glassc5f053b2015-11-29 13:18:03 -07001563 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1564
1565 /*
1566 * Pass "0" as the length argument to pci_bus_to_virt. The arg
Suneel Garapati47f19622019-10-19 16:44:35 -07001567 * isn't actually used on any platform because U-Boot assumes a static
Simon Glassc5f053b2015-11-29 13:18:03 -07001568 * linear mapping. In the future, this could read the BAR size
1569 * and pass that as the size if needed.
1570 */
Suneel Garapati5858ba82019-10-19 16:34:16 -07001571 return dm_pci_bus_to_virt(udev, pci_bus_addr, flags, 0, MAP_NOCACHE);
Simon Glassc5f053b2015-11-29 13:18:03 -07001572}
1573
Bin Meng631f3482018-10-15 02:21:21 -07001574static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001575{
Bin Menga7366f02018-08-03 01:14:52 -07001576 int ttl = PCI_FIND_CAP_TTL;
1577 u8 id;
1578 u16 ent;
Bin Menga7366f02018-08-03 01:14:52 -07001579
1580 dm_pci_read_config8(dev, pos, &pos);
Bin Meng631f3482018-10-15 02:21:21 -07001581
Bin Menga7366f02018-08-03 01:14:52 -07001582 while (ttl--) {
1583 if (pos < PCI_STD_HEADER_SIZEOF)
1584 break;
1585 pos &= ~3;
1586 dm_pci_read_config16(dev, pos, &ent);
1587
1588 id = ent & 0xff;
1589 if (id == 0xff)
1590 break;
1591 if (id == cap)
1592 return pos;
1593 pos = (ent >> 8);
1594 }
1595
1596 return 0;
1597}
1598
Bin Meng631f3482018-10-15 02:21:21 -07001599int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1600{
1601 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1602 cap);
1603}
1604
1605int dm_pci_find_capability(struct udevice *dev, int cap)
1606{
1607 u16 status;
1608 u8 header_type;
1609 u8 pos;
1610
1611 dm_pci_read_config16(dev, PCI_STATUS, &status);
1612 if (!(status & PCI_STATUS_CAP_LIST))
1613 return 0;
1614
1615 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1616 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1617 pos = PCI_CB_CAPABILITY_LIST;
1618 else
1619 pos = PCI_CAPABILITY_LIST;
1620
1621 return _dm_pci_find_next_capability(dev, pos, cap);
1622}
1623
1624int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001625{
1626 u32 header;
1627 int ttl;
1628 int pos = PCI_CFG_SPACE_SIZE;
1629
1630 /* minimum 8 bytes per capability */
1631 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1632
Bin Meng631f3482018-10-15 02:21:21 -07001633 if (start)
1634 pos = start;
1635
Bin Menga7366f02018-08-03 01:14:52 -07001636 dm_pci_read_config32(dev, pos, &header);
1637 /*
1638 * If we have no capabilities, this is indicated by cap ID,
1639 * cap version and next pointer all being 0.
1640 */
1641 if (header == 0)
1642 return 0;
1643
1644 while (ttl--) {
1645 if (PCI_EXT_CAP_ID(header) == cap)
1646 return pos;
1647
1648 pos = PCI_EXT_CAP_NEXT(header);
1649 if (pos < PCI_CFG_SPACE_SIZE)
1650 break;
1651
1652 dm_pci_read_config32(dev, pos, &header);
1653 }
1654
1655 return 0;
1656}
1657
Bin Meng631f3482018-10-15 02:21:21 -07001658int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1659{
1660 return dm_pci_find_next_ext_capability(dev, 0, cap);
1661}
1662
Alex Marginean09467d32019-06-07 11:24:25 +03001663int dm_pci_flr(struct udevice *dev)
1664{
1665 int pcie_off;
1666 u32 cap;
1667
1668 /* look for PCI Express Capability */
1669 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1670 if (!pcie_off)
1671 return -ENOENT;
1672
1673 /* check FLR capability */
1674 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1675 if (!(cap & PCI_EXP_DEVCAP_FLR))
1676 return -ENOENT;
1677
1678 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1679 PCI_EXP_DEVCTL_BCR_FLR);
1680
1681 /* wait 100ms, per PCI spec */
1682 mdelay(100);
1683
1684 return 0;
1685}
1686
Suneel Garapati13822f72019-10-19 16:07:20 -07001687#if defined(CONFIG_PCI_SRIOV)
1688int pci_sriov_init(struct udevice *pdev, int vf_en)
1689{
1690 u16 vendor, device;
1691 struct udevice *bus;
1692 struct udevice *dev;
1693 pci_dev_t bdf;
1694 u16 ctrl;
1695 u16 num_vfs;
1696 u16 total_vf;
1697 u16 vf_offset;
1698 u16 vf_stride;
1699 int vf, ret;
1700 int pos;
1701
1702 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1703 if (!pos) {
1704 debug("Error: SRIOV capability not found\n");
1705 return -ENOENT;
1706 }
1707
1708 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1709
1710 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1711 if (vf_en > total_vf)
1712 vf_en = total_vf;
1713 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1714
1715 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1716 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1717
1718 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1719 if (num_vfs > vf_en)
1720 num_vfs = vf_en;
1721
1722 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1723 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1724
1725 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1726 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1727
1728 bdf = dm_pci_get_bdf(pdev);
1729
1730 pci_get_bus(PCI_BUS(bdf), &bus);
1731
1732 if (!bus)
1733 return -ENODEV;
1734
1735 bdf += PCI_BDF(0, 0, vf_offset);
1736
1737 for (vf = 0; vf < num_vfs; vf++) {
Simon Glassb75b15b2020-12-03 16:55:23 -07001738 struct pci_child_plat *pplat;
Suneel Garapati13822f72019-10-19 16:07:20 -07001739 ulong class;
1740
1741 pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1742 &class, PCI_SIZE_16);
1743
1744 debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
Simon Glass75e534b2020-12-16 21:20:07 -07001745 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
Suneel Garapati13822f72019-10-19 16:07:20 -07001746
1747 /* Find this device in the device tree */
1748 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1749
1750 if (ret == -ENODEV) {
1751 struct pci_device_id find_id;
1752
1753 memset(&find_id, '\0', sizeof(find_id));
1754 find_id.vendor = vendor;
1755 find_id.device = device;
1756 find_id.class = class;
1757
1758 ret = pci_find_and_bind_driver(bus, &find_id,
1759 bdf, &dev);
1760
1761 if (ret)
1762 return ret;
1763 }
1764
1765 /* Update the platform data */
Simon Glass71fa5b42020-12-03 16:55:18 -07001766 pplat = dev_get_parent_plat(dev);
Suneel Garapati13822f72019-10-19 16:07:20 -07001767 pplat->devfn = PCI_MASK_BUS(bdf);
1768 pplat->vendor = vendor;
1769 pplat->device = device;
1770 pplat->class = class;
1771 pplat->is_virtfn = true;
1772 pplat->pfdev = pdev;
1773 pplat->virtid = vf * vf_stride + vf_offset;
1774
1775 debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
Simon Glass75e534b2020-12-16 21:20:07 -07001776 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
Suneel Garapati13822f72019-10-19 16:07:20 -07001777 PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1778 bdf += PCI_BDF(0, 0, vf_stride);
1779 }
1780
1781 return 0;
1782}
1783
1784int pci_sriov_get_totalvfs(struct udevice *pdev)
1785{
1786 u16 total_vf;
1787 int pos;
1788
1789 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1790 if (!pos) {
1791 debug("Error: SRIOV capability not found\n");
1792 return -ENOENT;
1793 }
1794
1795 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1796
1797 return total_vf;
1798}
1799#endif /* SRIOV */
1800
Simon Glassb94dc892015-03-05 12:25:25 -07001801UCLASS_DRIVER(pci) = {
1802 .id = UCLASS_PCI,
1803 .name = "pci",
Simon Glassbe706102020-12-16 21:20:18 -07001804 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
Simon Glass18230342016-07-05 17:10:10 -06001805 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001806 .pre_probe = pci_uclass_pre_probe,
1807 .post_probe = pci_uclass_post_probe,
1808 .child_post_bind = pci_uclass_child_post_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001809 .per_device_auto = sizeof(struct pci_controller),
Simon Glassb75b15b2020-12-03 16:55:23 -07001810 .per_child_plat_auto = sizeof(struct pci_child_plat),
Simon Glassb94dc892015-03-05 12:25:25 -07001811};
1812
1813static const struct dm_pci_ops pci_bridge_ops = {
1814 .read_config = pci_bridge_read_config,
1815 .write_config = pci_bridge_write_config,
1816};
1817
1818static const struct udevice_id pci_bridge_ids[] = {
1819 { .compatible = "pci-bridge" },
1820 { }
1821};
1822
1823U_BOOT_DRIVER(pci_bridge_drv) = {
1824 .name = "pci_bridge_drv",
1825 .id = UCLASS_PCI,
1826 .of_match = pci_bridge_ids,
1827 .ops = &pci_bridge_ops,
1828};
1829
1830UCLASS_DRIVER(pci_generic) = {
1831 .id = UCLASS_PCI_GENERIC,
1832 .name = "pci_generic",
1833};
1834
1835static const struct udevice_id pci_generic_ids[] = {
1836 { .compatible = "pci-generic" },
1837 { }
1838};
1839
1840U_BOOT_DRIVER(pci_generic_drv) = {
1841 .name = "pci_generic_drv",
1842 .id = UCLASS_PCI_GENERIC,
1843 .of_match = pci_generic_ids,
1844};
Stephen Warren04eb2692016-01-26 11:10:11 -07001845
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001846int pci_init(void)
Stephen Warren04eb2692016-01-26 11:10:11 -07001847{
1848 struct udevice *bus;
1849
1850 /*
1851 * Enumerate all known controller devices. Enumeration has the side-
1852 * effect of probing them, so PCIe devices will be enumerated too.
1853 */
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001854 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warren04eb2692016-01-26 11:10:11 -07001855 bus;
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001856 uclass_next_device_check(&bus)) {
Stephen Warren04eb2692016-01-26 11:10:11 -07001857 ;
1858 }
Ovidiu Panaite353edb2020-11-28 10:43:12 +02001859
1860 return 0;
Stephen Warren04eb2692016-01-26 11:10:11 -07001861}