1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7 #define LOG_CATEGORY UCLASS_PCI
8
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <init.h>
13 #include <log.h>
14 #include <malloc.h>
15 #include <pci.h>
16 #include <asm/global_data.h>
17 #include <asm/io.h>
18 #include <dm/device-internal.h>
19 #include <dm/lists.h>
20 #include <dm/uclass-internal.h>
21 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
22 #include <asm/fsp/fsp_support.h>
23 #endif
24 #include <dt-bindings/pci/pci.h>
25 #include <linux/delay.h>
26 #include "pci_internal.h"
27
28 DECLARE_GLOBAL_DATA_PTR;
29
pci_get_bus(int busnum,struct udevice ** busp)30 int pci_get_bus(int busnum, struct udevice **busp)
31 {
32 int ret;
33
34 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
35
36 /* Since buses may not be numbered yet try a little harder with bus 0 */
37 if (ret == -ENODEV) {
38 ret = uclass_first_device_err(UCLASS_PCI, busp);
39 if (ret)
40 return ret;
41 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
42 }
43
44 return ret;
45 }
46
pci_get_controller(struct udevice * dev)47 struct udevice *pci_get_controller(struct udevice *dev)
48 {
49 while (device_is_on_pci_bus(dev))
50 dev = dev->parent;
51
52 return dev;
53 }
54
dm_pci_get_bdf(const struct udevice * dev)55 pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
56 {
57 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
58 struct udevice *bus = dev->parent;
59
60 /*
61 * This error indicates that @dev is a device on an unprobed PCI bus.
62 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
63 * will produce a bad BDF>
64 *
65 * A common cause of this problem is that this function is called in the
66 * of_to_plat() method of @dev. Accessing the PCI bus in that
67 * method is not allowed, since it has not yet been probed. To fix this,
68 * move that access to the probe() method of @dev instead.
69 */
70 if (!device_active(bus))
71 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
72 bus->name);
73 return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
74 }
75
76 /**
77 * pci_get_bus_max() - returns the bus number of the last active bus
78 *
79 * Return: last bus number, or -1 if no active buses
80 */
pci_get_bus_max(void)81 static int pci_get_bus_max(void)
82 {
83 struct udevice *bus;
84 struct uclass *uc;
85 int ret = -1;
86
87 ret = uclass_get(UCLASS_PCI, &uc);
88 uclass_foreach_dev(bus, uc) {
89 if (dev_seq(bus) > ret)
90 ret = dev_seq(bus);
91 }
92
93 debug("%s: ret=%d\n", __func__, ret);
94
95 return ret;
96 }
97
pci_last_busno(void)98 int pci_last_busno(void)
99 {
100 return pci_get_bus_max();
101 }
102
pci_get_ff(enum pci_size_t size)103 int pci_get_ff(enum pci_size_t size)
104 {
105 switch (size) {
106 case PCI_SIZE_8:
107 return 0xff;
108 case PCI_SIZE_16:
109 return 0xffff;
110 default:
111 return 0xffffffff;
112 }
113 }
114
pci_dev_find_ofnode(struct udevice * bus,phys_addr_t bdf,ofnode * rnode)115 static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
116 ofnode *rnode)
117 {
118 struct fdt_pci_addr addr;
119 ofnode node;
120 int ret;
121
122 dev_for_each_subnode(node, bus) {
123 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
124 &addr);
125 if (ret)
126 continue;
127
128 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
129 continue;
130
131 *rnode = node;
132 break;
133 }
134 };
135
pci_bus_find_devfn(const struct udevice * bus,pci_dev_t find_devfn,struct udevice ** devp)136 int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
137 struct udevice **devp)
138 {
139 struct udevice *dev;
140
141 for (device_find_first_child(bus, &dev);
142 dev;
143 device_find_next_child(&dev)) {
144 struct pci_child_plat *pplat;
145
146 pplat = dev_get_parent_plat(dev);
147 if (pplat && pplat->devfn == find_devfn) {
148 *devp = dev;
149 return 0;
150 }
151 }
152
153 return -ENODEV;
154 }
155
dm_pci_bus_find_bdf(pci_dev_t bdf,struct udevice ** devp)156 int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
157 {
158 struct udevice *bus;
159 int ret;
160
161 ret = pci_get_bus(PCI_BUS(bdf), &bus);
162 if (ret)
163 return ret;
164 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
165 }
166
pci_device_matches_ids(struct udevice * dev,const struct pci_device_id * ids)167 static int pci_device_matches_ids(struct udevice *dev,
168 const struct pci_device_id *ids)
169 {
170 struct pci_child_plat *pplat;
171 int i;
172
173 pplat = dev_get_parent_plat(dev);
174 if (!pplat)
175 return -EINVAL;
176 for (i = 0; ids[i].vendor != 0; i++) {
177 if (pplat->vendor == ids[i].vendor &&
178 pplat->device == ids[i].device)
179 return i;
180 }
181
182 return -EINVAL;
183 }
184
pci_bus_find_devices(struct udevice * bus,const struct pci_device_id * ids,int * indexp,struct udevice ** devp)185 int pci_bus_find_devices(struct udevice *bus, const struct pci_device_id *ids,
186 int *indexp, struct udevice **devp)
187 {
188 struct udevice *dev;
189
190 /* Scan all devices on this bus */
191 for (device_find_first_child(bus, &dev);
192 dev;
193 device_find_next_child(&dev)) {
194 if (pci_device_matches_ids(dev, ids) >= 0) {
195 if ((*indexp)-- <= 0) {
196 *devp = dev;
197 return 0;
198 }
199 }
200 }
201
202 return -ENODEV;
203 }
204
pci_find_device_id(const struct pci_device_id * ids,int index,struct udevice ** devp)205 int pci_find_device_id(const struct pci_device_id *ids, int index,
206 struct udevice **devp)
207 {
208 struct udevice *bus;
209
210 /* Scan all known buses */
211 for (uclass_first_device(UCLASS_PCI, &bus);
212 bus;
213 uclass_next_device(&bus)) {
214 if (!pci_bus_find_devices(bus, ids, &index, devp))
215 return 0;
216 }
217 *devp = NULL;
218
219 return -ENODEV;
220 }
221
dm_pci_bus_find_device(struct udevice * bus,unsigned int vendor,unsigned int device,int * indexp,struct udevice ** devp)222 static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
223 unsigned int device, int *indexp,
224 struct udevice **devp)
225 {
226 struct pci_child_plat *pplat;
227 struct udevice *dev;
228
229 for (device_find_first_child(bus, &dev);
230 dev;
231 device_find_next_child(&dev)) {
232 pplat = dev_get_parent_plat(dev);
233 if (pplat->vendor == vendor && pplat->device == device) {
234 if (!(*indexp)--) {
235 *devp = dev;
236 return 0;
237 }
238 }
239 }
240
241 return -ENODEV;
242 }
243
dm_pci_find_device(unsigned int vendor,unsigned int device,int index,struct udevice ** devp)244 int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
245 struct udevice **devp)
246 {
247 struct udevice *bus;
248
249 /* Scan all known buses */
250 for (uclass_first_device(UCLASS_PCI, &bus);
251 bus;
252 uclass_next_device(&bus)) {
253 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
254 return device_probe(*devp);
255 }
256 *devp = NULL;
257
258 return -ENODEV;
259 }
260
dm_pci_find_class(uint find_class,int index,struct udevice ** devp)261 int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
262 {
263 struct udevice *dev;
264
265 /* Scan all known buses */
266 for (pci_find_first_device(&dev);
267 dev;
268 pci_find_next_device(&dev)) {
269 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
270
271 if (pplat->class == find_class && !index--) {
272 *devp = dev;
273 return device_probe(*devp);
274 }
275 }
276 *devp = NULL;
277
278 return -ENODEV;
279 }
280
pci_bus_write_config(struct udevice * bus,pci_dev_t bdf,int offset,unsigned long value,enum pci_size_t size)281 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
282 unsigned long value, enum pci_size_t size)
283 {
284 struct dm_pci_ops *ops;
285
286 ops = pci_get_ops(bus);
287 if (!ops->write_config)
288 return -ENOSYS;
289 if (offset < 0 || offset >= 4096)
290 return -EINVAL;
291 return ops->write_config(bus, bdf, offset, value, size);
292 }
293
pci_bus_clrset_config32(struct udevice * bus,pci_dev_t bdf,int offset,u32 clr,u32 set)294 int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
295 u32 clr, u32 set)
296 {
297 ulong val;
298 int ret;
299
300 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
301 if (ret)
302 return ret;
303 val &= ~clr;
304 val |= set;
305
306 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
307 }
308
pci_write_config(pci_dev_t bdf,int offset,unsigned long value,enum pci_size_t size)309 static int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
310 enum pci_size_t size)
311 {
312 struct udevice *bus;
313 int ret;
314
315 ret = pci_get_bus(PCI_BUS(bdf), &bus);
316 if (ret)
317 return ret;
318
319 return pci_bus_write_config(bus, bdf, offset, value, size);
320 }
321
dm_pci_write_config(struct udevice * dev,int offset,unsigned long value,enum pci_size_t size)322 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
323 enum pci_size_t size)
324 {
325 struct udevice *bus;
326
327 for (bus = dev; device_is_on_pci_bus(bus);)
328 bus = bus->parent;
329 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
330 size);
331 }
332
pci_write_config32(pci_dev_t bdf,int offset,u32 value)333 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
334 {
335 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
336 }
337
pci_write_config16(pci_dev_t bdf,int offset,u16 value)338 int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
339 {
340 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
341 }
342
pci_write_config8(pci_dev_t bdf,int offset,u8 value)343 int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
344 {
345 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
346 }
347
dm_pci_write_config8(struct udevice * dev,int offset,u8 value)348 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
349 {
350 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
351 }
352
dm_pci_write_config16(struct udevice * dev,int offset,u16 value)353 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
354 {
355 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
356 }
357
dm_pci_write_config32(struct udevice * dev,int offset,u32 value)358 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
359 {
360 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
361 }
362
pci_bus_read_config(const struct udevice * bus,pci_dev_t bdf,int offset,unsigned long * valuep,enum pci_size_t size)363 int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
364 unsigned long *valuep, enum pci_size_t size)
365 {
366 struct dm_pci_ops *ops;
367
368 ops = pci_get_ops(bus);
369 if (!ops->read_config) {
370 *valuep = pci_conv_32_to_size(~0, offset, size);
371 return -ENOSYS;
372 }
373 if (offset < 0 || offset >= 4096) {
374 *valuep = pci_conv_32_to_size(0, offset, size);
375 return -EINVAL;
376 }
377 return ops->read_config(bus, bdf, offset, valuep, size);
378 }
379
pci_read_config(pci_dev_t bdf,int offset,unsigned long * valuep,enum pci_size_t size)380 static int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
381 enum pci_size_t size)
382 {
383 struct udevice *bus;
384 int ret;
385
386 ret = pci_get_bus(PCI_BUS(bdf), &bus);
387 if (ret)
388 return ret;
389
390 return pci_bus_read_config(bus, bdf, offset, valuep, size);
391 }
392
dm_pci_read_config(const struct udevice * dev,int offset,unsigned long * valuep,enum pci_size_t size)393 int dm_pci_read_config(const struct udevice *dev, int offset,
394 unsigned long *valuep, enum pci_size_t size)
395 {
396 const struct udevice *bus;
397
398 for (bus = dev; device_is_on_pci_bus(bus);)
399 bus = bus->parent;
400 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
401 size);
402 }
403
pci_read_config32(pci_dev_t bdf,int offset,u32 * valuep)404 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
405 {
406 unsigned long value;
407 int ret;
408
409 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
410 if (ret)
411 return ret;
412 *valuep = value;
413
414 return 0;
415 }
416
pci_read_config16(pci_dev_t bdf,int offset,u16 * valuep)417 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
418 {
419 unsigned long value;
420 int ret;
421
422 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
423 if (ret)
424 return ret;
425 *valuep = value;
426
427 return 0;
428 }
429
pci_read_config8(pci_dev_t bdf,int offset,u8 * valuep)430 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
431 {
432 unsigned long value;
433 int ret;
434
435 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
436 if (ret)
437 return ret;
438 *valuep = value;
439
440 return 0;
441 }
442
dm_pci_read_config8(const struct udevice * dev,int offset,u8 * valuep)443 int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
444 {
445 unsigned long value;
446 int ret;
447
448 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
449 if (ret)
450 return ret;
451 *valuep = value;
452
453 return 0;
454 }
455
dm_pci_read_config16(const struct udevice * dev,int offset,u16 * valuep)456 int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
457 {
458 unsigned long value;
459 int ret;
460
461 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
462 if (ret)
463 return ret;
464 *valuep = value;
465
466 return 0;
467 }
468
dm_pci_read_config32(const struct udevice * dev,int offset,u32 * valuep)469 int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
470 {
471 unsigned long value;
472 int ret;
473
474 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
475 if (ret)
476 return ret;
477 *valuep = value;
478
479 return 0;
480 }
481
dm_pci_clrset_config8(struct udevice * dev,int offset,u32 clr,u32 set)482 int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
483 {
484 u8 val;
485 int ret;
486
487 ret = dm_pci_read_config8(dev, offset, &val);
488 if (ret)
489 return ret;
490 val &= ~clr;
491 val |= set;
492
493 return dm_pci_write_config8(dev, offset, val);
494 }
495
dm_pci_clrset_config16(struct udevice * dev,int offset,u32 clr,u32 set)496 int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
497 {
498 u16 val;
499 int ret;
500
501 ret = dm_pci_read_config16(dev, offset, &val);
502 if (ret)
503 return ret;
504 val &= ~clr;
505 val |= set;
506
507 return dm_pci_write_config16(dev, offset, val);
508 }
509
dm_pci_clrset_config32(struct udevice * dev,int offset,u32 clr,u32 set)510 int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
511 {
512 u32 val;
513 int ret;
514
515 ret = dm_pci_read_config32(dev, offset, &val);
516 if (ret)
517 return ret;
518 val &= ~clr;
519 val |= set;
520
521 return dm_pci_write_config32(dev, offset, val);
522 }
523
set_vga_bridge_bits(struct udevice * dev)524 static void set_vga_bridge_bits(struct udevice *dev)
525 {
526 struct udevice *parent = dev->parent;
527 u16 bc;
528
529 while (dev_seq(parent) != 0) {
530 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
531 bc |= PCI_BRIDGE_CTL_VGA;
532 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
533 parent = parent->parent;
534 }
535 }
536
pci_auto_config_devices(struct udevice * bus)537 int pci_auto_config_devices(struct udevice *bus)
538 {
539 struct pci_controller *hose = dev_get_uclass_priv(bus);
540 struct pci_child_plat *pplat;
541 unsigned int sub_bus;
542 struct udevice *dev;
543 int ret;
544
545 sub_bus = dev_seq(bus);
546 debug("%s: start\n", __func__);
547 pciauto_config_init(hose);
548 for (ret = device_find_first_child(bus, &dev);
549 !ret && dev;
550 ret = device_find_next_child(&dev)) {
551 unsigned int max_bus;
552 int ret;
553
554 debug("%s: device %s\n", __func__, dev->name);
555 if (dev_has_ofnode(dev) &&
556 dev_read_bool(dev, "pci,no-autoconfig"))
557 continue;
558 ret = dm_pciauto_config_device(dev);
559 if (ret < 0)
560 return log_msg_ret("auto", ret);
561 max_bus = ret;
562 sub_bus = max(sub_bus, max_bus);
563
564 if (dev_get_parent(dev) == bus)
565 continue;
566
567 pplat = dev_get_parent_plat(dev);
568 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
569 set_vga_bridge_bits(dev);
570 }
571 if (hose->last_busno < sub_bus)
572 hose->last_busno = sub_bus;
573 debug("%s: done\n", __func__);
574
575 return log_msg_ret("sub", sub_bus);
576 }
577
pci_generic_mmap_write_config(const struct udevice * bus,int (* addr_f)(const struct udevice * bus,pci_dev_t bdf,uint offset,void ** addrp),pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)578 int pci_generic_mmap_write_config(
579 const struct udevice *bus,
580 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
581 void **addrp),
582 pci_dev_t bdf,
583 uint offset,
584 ulong value,
585 enum pci_size_t size)
586 {
587 void *address;
588
589 if (addr_f(bus, bdf, offset, &address) < 0)
590 return 0;
591
592 switch (size) {
593 case PCI_SIZE_8:
594 writeb(value, address);
595 return 0;
596 case PCI_SIZE_16:
597 writew(value, address);
598 return 0;
599 case PCI_SIZE_32:
600 writel(value, address);
601 return 0;
602 default:
603 return -EINVAL;
604 }
605 }
606
pci_generic_mmap_read_config(const struct udevice * bus,int (* addr_f)(const struct udevice * bus,pci_dev_t bdf,uint offset,void ** addrp),pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)607 int pci_generic_mmap_read_config(
608 const struct udevice *bus,
609 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
610 void **addrp),
611 pci_dev_t bdf,
612 uint offset,
613 ulong *valuep,
614 enum pci_size_t size)
615 {
616 void *address;
617
618 if (addr_f(bus, bdf, offset, &address) < 0) {
619 *valuep = pci_get_ff(size);
620 return 0;
621 }
622
623 switch (size) {
624 case PCI_SIZE_8:
625 *valuep = readb(address);
626 return 0;
627 case PCI_SIZE_16:
628 *valuep = readw(address);
629 return 0;
630 case PCI_SIZE_32:
631 *valuep = readl(address);
632 return 0;
633 default:
634 return -EINVAL;
635 }
636 }
637
dm_pci_hose_probe_bus(struct udevice * bus)638 int dm_pci_hose_probe_bus(struct udevice *bus)
639 {
640 u8 header_type;
641 int sub_bus;
642 int ret;
643 int ea_pos;
644 u8 reg;
645
646 debug("%s\n", __func__);
647
648 dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);
649 header_type &= 0x7f;
650 if (header_type != PCI_HEADER_TYPE_BRIDGE) {
651 debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n",
652 __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type);
653 return log_msg_ret("probe", -EINVAL);
654 }
655
656 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
657 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
658 else
659 ea_pos = 0;
660
661 if (ea_pos) {
662 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
663 ®);
664 sub_bus = reg;
665 } else {
666 sub_bus = pci_get_bus_max() + 1;
667 }
668 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
669 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
670
671 ret = device_probe(bus);
672 if (ret) {
673 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
674 ret);
675 return log_msg_ret("probe", ret);
676 }
677
678 if (!ea_pos)
679 sub_bus = pci_get_bus_max();
680
681 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
682
683 return sub_bus;
684 }
685
686 /**
687 * pci_match_one_device - Tell if a PCI device structure has a matching
688 * PCI device id structure
689 * @id: single PCI device id structure to match
690 * @find: the PCI device id structure to match against
691 *
692 * Returns true if the finding pci_device_id structure matched or false if
693 * there is no match.
694 */
pci_match_one_id(const struct pci_device_id * id,const struct pci_device_id * find)695 static bool pci_match_one_id(const struct pci_device_id *id,
696 const struct pci_device_id *find)
697 {
698 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
699 (id->device == PCI_ANY_ID || id->device == find->device) &&
700 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
701 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
702 !((id->class ^ find->class) & id->class_mask))
703 return true;
704
705 return false;
706 }
707
708 /**
709 * pci_need_device_pre_reloc() - Check if a device should be bound
710 *
711 * This checks a list of vendor/device-ID values indicating devices that should
712 * be bound before relocation.
713 *
714 * @bus: Bus to check
715 * @vendor: Vendor ID to check
716 * @device: Device ID to check
717 * Return: true if the vendor/device is in the list, false if not
718 */
pci_need_device_pre_reloc(struct udevice * bus,uint vendor,uint device)719 static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor,
720 uint device)
721 {
722 u32 vendev;
723 int index;
724
725 for (index = 0;
726 !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index,
727 &vendev);
728 index++) {
729 if (vendev == PCI_VENDEV(vendor, device))
730 return true;
731 }
732
733 return false;
734 }
735
736 /**
737 * pci_find_and_bind_driver() - Find and bind the right PCI driver
738 *
739 * This only looks at certain fields in the descriptor.
740 *
741 * @parent: Parent bus
742 * @find_id: Specification of the driver to find
743 * @bdf: Bus/device/function addreess - see PCI_BDF()
744 * @devp: Returns a pointer to the device created
745 * Return: 0 if OK, -EPERM if the device is not needed before relocation and
746 * therefore was not created, other -ve value on error
747 */
pci_find_and_bind_driver(struct udevice * parent,struct pci_device_id * find_id,pci_dev_t bdf,struct udevice ** devp)748 static int pci_find_and_bind_driver(struct udevice *parent,
749 struct pci_device_id *find_id,
750 pci_dev_t bdf, struct udevice **devp)
751 {
752 struct pci_driver_entry *start, *entry;
753 ofnode node = ofnode_null();
754 const char *drv;
755 int n_ents;
756 int ret;
757 char name[30], *str;
758 bool bridge;
759
760 *devp = NULL;
761
762 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
763 find_id->vendor, find_id->device);
764
765 /* Determine optional OF node */
766 if (ofnode_valid(dev_ofnode(parent)))
767 pci_dev_find_ofnode(parent, bdf, &node);
768
769 if (ofnode_valid(node) && !ofnode_is_enabled(node)) {
770 debug("%s: Ignoring disabled device\n", __func__);
771 return log_msg_ret("dis", -EPERM);
772 }
773
774 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
775 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
776 for (entry = start; entry != start + n_ents; entry++) {
777 const struct pci_device_id *id;
778 struct udevice *dev;
779 const struct driver *drv;
780
781 for (id = entry->match;
782 id->vendor || id->subvendor || id->class_mask;
783 id++) {
784 if (!pci_match_one_id(id, find_id))
785 continue;
786
787 drv = entry->driver;
788
789 /*
790 * In the pre-relocation phase, we only bind devices
791 * whose driver has the DM_FLAG_PRE_RELOC set, to save
792 * precious memory space as on some platforms as that
793 * space is pretty limited (ie: using Cache As RAM).
794 */
795 if (!(gd->flags & GD_FLG_RELOC) &&
796 !(drv->flags & DM_FLAG_PRE_RELOC))
797 return log_msg_ret("pre", -EPERM);
798
799 /*
800 * We could pass the descriptor to the driver as
801 * plat (instead of NULL) and allow its bind()
802 * method to return -ENOENT if it doesn't support this
803 * device. That way we could continue the search to
804 * find another driver. For now this doesn't seem
805 * necesssary, so just bind the first match.
806 */
807 ret = device_bind(parent, drv, drv->name, NULL, node,
808 &dev);
809 if (ret)
810 goto error;
811 debug("%s: Match found: %s\n", __func__, drv->name);
812 dev->driver_data = id->driver_data;
813 *devp = dev;
814 return 0;
815 }
816 }
817
818 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
819 /*
820 * In the pre-relocation phase, we only bind bridge devices to save
821 * precious memory space as on some platforms as that space is pretty
822 * limited (ie: using Cache As RAM).
823 */
824 if (!(gd->flags & GD_FLG_RELOC) && !bridge &&
825 !pci_need_device_pre_reloc(parent, find_id->vendor,
826 find_id->device))
827 return log_msg_ret("notbr", -EPERM);
828
829 /* Bind a generic driver so that the device can be used */
830 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
831 PCI_FUNC(bdf));
832 str = strdup(name);
833 if (!str)
834 return -ENOMEM;
835 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
836
837 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
838 if (ret) {
839 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
840 free(str);
841 return ret;
842 }
843 debug("%s: No match found: bound generic driver instead\n", __func__);
844
845 return 0;
846
847 error:
848 debug("%s: No match found: error %d\n", __func__, ret);
849 return ret;
850 }
851
board_pci_fixup_dev(struct udevice * bus,struct udevice * dev)852 __weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
853 {
854 }
855
pci_bind_bus_devices(struct udevice * bus)856 int pci_bind_bus_devices(struct udevice *bus)
857 {
858 ulong vendor, device;
859 ulong header_type;
860 pci_dev_t bdf, end;
861 bool found_multi;
862 int ari_off;
863 int ret;
864
865 found_multi = false;
866 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
867 PCI_MAX_PCI_FUNCTIONS - 1);
868 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
869 bdf += PCI_BDF(0, 0, 1)) {
870 struct pci_child_plat *pplat;
871 struct udevice *dev;
872 ulong class;
873
874 if (!PCI_FUNC(bdf))
875 found_multi = false;
876 if (PCI_FUNC(bdf) && !found_multi)
877 continue;
878
879 /* Check only the first access, we don't expect problems */
880 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
881 PCI_SIZE_16);
882 if (ret || vendor == 0xffff || vendor == 0x0000)
883 continue;
884
885 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
886 &header_type, PCI_SIZE_8);
887
888 if (!PCI_FUNC(bdf))
889 found_multi = header_type & 0x80;
890
891 debug("%s: bus %d/%s: found device %x, function %d", __func__,
892 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
893 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
894 PCI_SIZE_16);
895 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
896 PCI_SIZE_32);
897 class >>= 8;
898
899 /* Find this device in the device tree */
900 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
901 debug(": find ret=%d\n", ret);
902
903 /* If nothing in the device tree, bind a device */
904 if (ret == -ENODEV) {
905 struct pci_device_id find_id;
906 ulong val;
907
908 memset(&find_id, '\0', sizeof(find_id));
909 find_id.vendor = vendor;
910 find_id.device = device;
911 find_id.class = class;
912 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
913 pci_bus_read_config(bus, bdf,
914 PCI_SUBSYSTEM_VENDOR_ID,
915 &val, PCI_SIZE_32);
916 find_id.subvendor = val & 0xffff;
917 find_id.subdevice = val >> 16;
918 }
919 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
920 &dev);
921 }
922 if (ret == -EPERM)
923 continue;
924 else if (ret)
925 return ret;
926
927 /* Update the platform data */
928 pplat = dev_get_parent_plat(dev);
929 pplat->devfn = PCI_MASK_BUS(bdf);
930 pplat->vendor = vendor;
931 pplat->device = device;
932 pplat->class = class;
933
934 if (IS_ENABLED(CONFIG_PCI_ARID)) {
935 ari_off = dm_pci_find_ext_capability(dev,
936 PCI_EXT_CAP_ID_ARI);
937 if (ari_off) {
938 u16 ari_cap;
939
940 /*
941 * Read Next Function number in ARI Cap
942 * Register
943 */
944 dm_pci_read_config16(dev, ari_off + 4,
945 &ari_cap);
946 /*
947 * Update next scan on this function number,
948 * subtract 1 in BDF to satisfy loop increment.
949 */
950 if (ari_cap & 0xff00) {
951 bdf = PCI_BDF(PCI_BUS(bdf),
952 PCI_DEV(ari_cap),
953 PCI_FUNC(ari_cap));
954 bdf = bdf - 0x100;
955 }
956 }
957 }
958
959 board_pci_fixup_dev(bus, dev);
960 }
961
962 return 0;
963 }
964
decode_regions(struct pci_controller * hose,ofnode parent_node,ofnode node)965 static int decode_regions(struct pci_controller *hose, ofnode parent_node,
966 ofnode node)
967 {
968 int pci_addr_cells, addr_cells, size_cells;
969 int cells_per_record;
970 struct bd_info *bd;
971 const u32 *prop;
972 int max_regions;
973 int len;
974 int i;
975
976 /* handle booting from coreboot, etc. */
977 if (!ll_boot_init())
978 return 0;
979
980 prop = ofnode_get_property(node, "ranges", &len);
981 if (!prop) {
982 debug("%s: Cannot decode regions\n", __func__);
983 return -EINVAL;
984 }
985
986 pci_addr_cells = ofnode_read_simple_addr_cells(node);
987 addr_cells = ofnode_read_simple_addr_cells(parent_node);
988 size_cells = ofnode_read_simple_size_cells(node);
989
990 /* PCI addresses are always 3-cells */
991 len /= sizeof(u32);
992 cells_per_record = pci_addr_cells + addr_cells + size_cells;
993 hose->region_count = 0;
994 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
995 cells_per_record);
996
997 /* Dynamically allocate the regions array */
998 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
999 hose->regions = (struct pci_region *)
1000 calloc(1, max_regions * sizeof(struct pci_region));
1001 if (!hose->regions)
1002 return -ENOMEM;
1003
1004 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
1005 u64 pci_addr, addr, size;
1006 int space_code;
1007 u32 flags;
1008 int type;
1009 int pos;
1010
1011 if (len < cells_per_record)
1012 break;
1013 flags = fdt32_to_cpu(prop[0]);
1014 space_code = (flags >> 24) & 3;
1015 pci_addr = fdtdec_get_number(prop + 1, 2);
1016 prop += pci_addr_cells;
1017 addr = fdtdec_get_number(prop, addr_cells);
1018 prop += addr_cells;
1019 size = fdtdec_get_number(prop, size_cells);
1020 prop += size_cells;
1021 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
1022 __func__, hose->region_count, pci_addr, addr, size, space_code);
1023 if (space_code & 2) {
1024 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
1025 PCI_REGION_MEM;
1026 } else if (space_code & 1) {
1027 type = PCI_REGION_IO;
1028 } else {
1029 continue;
1030 }
1031
1032 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
1033 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
1034 debug(" - pci_addr beyond the 32-bit boundary, ignoring\n");
1035 continue;
1036 }
1037
1038 if (!IS_ENABLED(CONFIG_PHYS_64BIT) && upper_32_bits(addr)) {
1039 debug(" - addr beyond the 32-bit boundary, ignoring\n");
1040 continue;
1041 }
1042
1043 if (~((pci_addr_t)0) - pci_addr < size) {
1044 debug(" - PCI range exceeds max address, ignoring\n");
1045 continue;
1046 }
1047
1048 if (~((phys_addr_t)0) - addr < size) {
1049 debug(" - phys range exceeds max address, ignoring\n");
1050 continue;
1051 }
1052
1053 pos = -1;
1054 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
1055 for (i = 0; i < hose->region_count; i++) {
1056 if (hose->regions[i].flags == type)
1057 pos = i;
1058 }
1059 }
1060
1061 if (pos == -1)
1062 pos = hose->region_count++;
1063 debug(" - type=%d, pos=%d\n", type, pos);
1064 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
1065 }
1066
1067 /* Add a region for our local memory */
1068 bd = gd->bd;
1069 if (!bd)
1070 return 0;
1071
1072 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1073 if (bd->bi_dram[i].size) {
1074 phys_addr_t start = bd->bi_dram[i].start;
1075
1076 if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY))
1077 start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start);
1078
1079 pci_set_region(hose->regions + hose->region_count++,
1080 start, start, bd->bi_dram[i].size,
1081 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1082 }
1083 }
1084
1085 return 0;
1086 }
1087
pci_uclass_pre_probe(struct udevice * bus)1088 static int pci_uclass_pre_probe(struct udevice *bus)
1089 {
1090 struct pci_controller *hose;
1091 struct uclass *uc;
1092 int ret;
1093
1094 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
1095 bus->parent->name);
1096 hose = dev_get_uclass_priv(bus);
1097
1098 /*
1099 * Set the sequence number, if device_bind() doesn't. We want control
1100 * of this so that numbers are allocated as devices are probed. That
1101 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1102 * higher than their parents)
1103 */
1104 if (dev_seq(bus) == -1) {
1105 ret = uclass_get(UCLASS_PCI, &uc);
1106 if (ret)
1107 return ret;
1108 bus->seq_ = uclass_find_next_free_seq(uc);
1109 }
1110
1111 /* For bridges, use the top-level PCI controller */
1112 if (!device_is_on_pci_bus(bus)) {
1113 hose->ctlr = bus;
1114 ret = decode_regions(hose, dev_ofnode(bus->parent),
1115 dev_ofnode(bus));
1116 if (ret)
1117 return ret;
1118 } else {
1119 struct pci_controller *parent_hose;
1120
1121 parent_hose = dev_get_uclass_priv(bus->parent);
1122 hose->ctlr = parent_hose->bus;
1123 }
1124
1125 hose->bus = bus;
1126 hose->first_busno = dev_seq(bus);
1127 hose->last_busno = dev_seq(bus);
1128 if (dev_has_ofnode(bus)) {
1129 hose->skip_auto_config_until_reloc =
1130 dev_read_bool(bus,
1131 "u-boot,skip-auto-config-until-reloc");
1132 }
1133
1134 return 0;
1135 }
1136
pci_uclass_post_probe(struct udevice * bus)1137 static int pci_uclass_post_probe(struct udevice *bus)
1138 {
1139 struct pci_controller *hose = dev_get_uclass_priv(bus);
1140 int ret;
1141
1142 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
1143 ret = pci_bind_bus_devices(bus);
1144 if (ret)
1145 return log_msg_ret("bind", ret);
1146
1147 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
1148 (!hose->skip_auto_config_until_reloc ||
1149 (gd->flags & GD_FLG_RELOC))) {
1150 ret = pci_auto_config_devices(bus);
1151 if (ret < 0)
1152 return log_msg_ret("cfg", ret);
1153 }
1154
1155 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1156 /*
1157 * Per Intel FSP specification, we should call FSP notify API to
1158 * inform FSP that PCI enumeration has been done so that FSP will
1159 * do any necessary initialization as required by the chipset's
1160 * BIOS Writer's Guide (BWG).
1161 *
1162 * Unfortunately we have to put this call here as with driver model,
1163 * the enumeration is all done on a lazy basis as needed, so until
1164 * something is touched on PCI it won't happen.
1165 *
1166 * Note we only call this 1) after U-Boot is relocated, and 2)
1167 * root bus has finished probing.
1168 */
1169 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
1170 ret = fsp_init_phase_pci();
1171 if (ret)
1172 return log_msg_ret("fsp", ret);
1173 }
1174 #endif
1175
1176 return 0;
1177 }
1178
pci_uclass_child_post_bind(struct udevice * dev)1179 static int pci_uclass_child_post_bind(struct udevice *dev)
1180 {
1181 struct pci_child_plat *pplat;
1182
1183 if (!dev_has_ofnode(dev))
1184 return 0;
1185
1186 pplat = dev_get_parent_plat(dev);
1187
1188 /* Extract vendor id and device id if available */
1189 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1190
1191 /* Extract the devfn from fdt_pci_addr */
1192 pplat->devfn = pci_get_devfn(dev);
1193
1194 return 0;
1195 }
1196
pci_bridge_read_config(const struct udevice * bus,pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)1197 static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
1198 uint offset, ulong *valuep,
1199 enum pci_size_t size)
1200 {
1201 struct pci_controller *hose = dev_get_uclass_priv(bus);
1202
1203 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1204 }
1205
pci_bridge_write_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)1206 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1207 uint offset, ulong value,
1208 enum pci_size_t size)
1209 {
1210 struct pci_controller *hose = dev_get_uclass_priv(bus);
1211
1212 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1213 }
1214
skip_to_next_device(struct udevice * bus,struct udevice ** devp)1215 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1216 {
1217 struct udevice *dev;
1218
1219 /*
1220 * Scan through all the PCI controllers. On x86 there will only be one
1221 * but that is not necessarily true on other hardware.
1222 */
1223 while (bus) {
1224 device_find_first_child(bus, &dev);
1225 if (dev) {
1226 *devp = dev;
1227 return 0;
1228 }
1229 uclass_next_device(&bus);
1230 }
1231
1232 return 0;
1233 }
1234
pci_find_next_device(struct udevice ** devp)1235 int pci_find_next_device(struct udevice **devp)
1236 {
1237 struct udevice *child = *devp;
1238 struct udevice *bus = child->parent;
1239
1240 /* First try all the siblings */
1241 *devp = NULL;
1242 while (child) {
1243 device_find_next_child(&child);
1244 if (child) {
1245 *devp = child;
1246 return 0;
1247 }
1248 }
1249
1250 /* We ran out of siblings. Try the next bus */
1251 uclass_next_device(&bus);
1252
1253 return bus ? skip_to_next_device(bus, devp) : 0;
1254 }
1255
pci_find_first_device(struct udevice ** devp)1256 int pci_find_first_device(struct udevice **devp)
1257 {
1258 struct udevice *bus;
1259
1260 *devp = NULL;
1261 uclass_first_device(UCLASS_PCI, &bus);
1262
1263 return skip_to_next_device(bus, devp);
1264 }
1265
pci_conv_32_to_size(ulong value,uint offset,enum pci_size_t size)1266 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1267 {
1268 switch (size) {
1269 case PCI_SIZE_8:
1270 return (value >> ((offset & 3) * 8)) & 0xff;
1271 case PCI_SIZE_16:
1272 return (value >> ((offset & 2) * 8)) & 0xffff;
1273 default:
1274 return value;
1275 }
1276 }
1277
pci_conv_size_to_32(ulong old,ulong value,uint offset,enum pci_size_t size)1278 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1279 enum pci_size_t size)
1280 {
1281 uint off_mask;
1282 uint val_mask, shift;
1283 ulong ldata, mask;
1284
1285 switch (size) {
1286 case PCI_SIZE_8:
1287 off_mask = 3;
1288 val_mask = 0xff;
1289 break;
1290 case PCI_SIZE_16:
1291 off_mask = 2;
1292 val_mask = 0xffff;
1293 break;
1294 default:
1295 return value;
1296 }
1297 shift = (offset & off_mask) * 8;
1298 ldata = (value & val_mask) << shift;
1299 mask = val_mask << shift;
1300 value = (old & ~mask) | ldata;
1301
1302 return value;
1303 }
1304
pci_get_dma_regions(struct udevice * dev,struct pci_region * memp,int index)1305 int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1306 {
1307 int pci_addr_cells, addr_cells, size_cells;
1308 int cells_per_record;
1309 const u32 *prop;
1310 int len;
1311 int i = 0;
1312
1313 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1314 if (!prop) {
1315 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1316 dev->name);
1317 return -EINVAL;
1318 }
1319
1320 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1321 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1322 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1323
1324 /* PCI addresses are always 3-cells */
1325 len /= sizeof(u32);
1326 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1327 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1328 cells_per_record);
1329
1330 while (len) {
1331 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1332 prop += pci_addr_cells;
1333 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1334 prop += addr_cells;
1335 memp->size = fdtdec_get_number(prop, size_cells);
1336 prop += size_cells;
1337
1338 if (i == index)
1339 return 0;
1340 i++;
1341 len -= cells_per_record;
1342 }
1343
1344 return -EINVAL;
1345 }
1346
pci_get_regions(struct udevice * dev,struct pci_region ** iop,struct pci_region ** memp,struct pci_region ** prefp)1347 int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1348 struct pci_region **memp, struct pci_region **prefp)
1349 {
1350 struct udevice *bus = pci_get_controller(dev);
1351 struct pci_controller *hose = dev_get_uclass_priv(bus);
1352 int i;
1353
1354 *iop = NULL;
1355 *memp = NULL;
1356 *prefp = NULL;
1357 for (i = 0; i < hose->region_count; i++) {
1358 switch (hose->regions[i].flags) {
1359 case PCI_REGION_IO:
1360 if (!*iop || (*iop)->size < hose->regions[i].size)
1361 *iop = hose->regions + i;
1362 break;
1363 case PCI_REGION_MEM:
1364 if (!*memp || (*memp)->size < hose->regions[i].size)
1365 *memp = hose->regions + i;
1366 break;
1367 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1368 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1369 *prefp = hose->regions + i;
1370 break;
1371 }
1372 }
1373
1374 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1375 }
1376
dm_pci_read_bar32(const struct udevice * dev,int barnum)1377 u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
1378 {
1379 u32 addr;
1380 int bar;
1381
1382 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1383 dm_pci_read_config32(dev, bar, &addr);
1384
1385 /*
1386 * If we get an invalid address, return this so that comparisons with
1387 * FDT_ADDR_T_NONE work correctly
1388 */
1389 if (addr == 0xffffffff)
1390 return addr;
1391 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1392 return addr & PCI_BASE_ADDRESS_IO_MASK;
1393 else
1394 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1395 }
1396
dm_pci_write_bar32(struct udevice * dev,int barnum,u32 addr)1397 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1398 {
1399 int bar;
1400
1401 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1402 dm_pci_write_config32(dev, bar, addr);
1403 }
1404
dm_pci_bus_to_phys(struct udevice * dev,pci_addr_t bus_addr,size_t len,unsigned long mask,unsigned long flags)1405 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1406 size_t len, unsigned long mask,
1407 unsigned long flags)
1408 {
1409 struct udevice *ctlr;
1410 struct pci_controller *hose;
1411 struct pci_region *res;
1412 pci_addr_t offset;
1413 int i;
1414
1415 /* The root controller has the region information */
1416 ctlr = pci_get_controller(dev);
1417 hose = dev_get_uclass_priv(ctlr);
1418
1419 if (hose->region_count == 0)
1420 return bus_addr;
1421
1422 for (i = 0; i < hose->region_count; i++) {
1423 res = &hose->regions[i];
1424
1425 if ((res->flags & mask) != flags)
1426 continue;
1427
1428 if (bus_addr < res->bus_start)
1429 continue;
1430
1431 offset = bus_addr - res->bus_start;
1432 if (offset >= res->size)
1433 continue;
1434
1435 if (len > res->size - offset)
1436 continue;
1437
1438 return res->phys_start + offset;
1439 }
1440
1441 puts("pci_hose_bus_to_phys: invalid physical address\n");
1442 return 0;
1443 }
1444
dm_pci_phys_to_bus(struct udevice * dev,phys_addr_t phys_addr,size_t len,unsigned long mask,unsigned long flags)1445 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1446 size_t len, unsigned long mask,
1447 unsigned long flags)
1448 {
1449 struct udevice *ctlr;
1450 struct pci_controller *hose;
1451 struct pci_region *res;
1452 phys_addr_t offset;
1453 int i;
1454
1455 /* The root controller has the region information */
1456 ctlr = pci_get_controller(dev);
1457 hose = dev_get_uclass_priv(ctlr);
1458
1459 if (hose->region_count == 0)
1460 return phys_addr;
1461
1462 for (i = 0; i < hose->region_count; i++) {
1463 res = &hose->regions[i];
1464
1465 if ((res->flags & mask) != flags)
1466 continue;
1467
1468 if (phys_addr < res->phys_start)
1469 continue;
1470
1471 offset = phys_addr - res->phys_start;
1472 if (offset >= res->size)
1473 continue;
1474
1475 if (len > res->size - offset)
1476 continue;
1477
1478 return res->bus_start + offset;
1479 }
1480
1481 puts("pci_hose_phys_to_bus: invalid physical address\n");
1482 return 0;
1483 }
1484
dm_pci_map_ea_virt(struct udevice * dev,int ea_off,struct pci_child_plat * pdata)1485 static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
1486 struct pci_child_plat *pdata)
1487 {
1488 phys_addr_t addr = 0;
1489
1490 /*
1491 * In the case of a Virtual Function device using BAR
1492 * base and size, add offset for VFn BAR(1, 2, 3...n)
1493 */
1494 if (pdata->is_virtfn) {
1495 size_t sz;
1496 u32 ea_entry;
1497
1498 /* MaxOffset, 1st DW */
1499 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1500 sz = ea_entry & PCI_EA_FIELD_MASK;
1501 /* Fill up lower 2 bits */
1502 sz |= (~PCI_EA_FIELD_MASK);
1503
1504 if (ea_entry & PCI_EA_IS_64) {
1505 /* MaxOffset 2nd DW */
1506 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1507 sz |= ((u64)ea_entry) << 32;
1508 }
1509
1510 addr = (pdata->virtid - 1) * (sz + 1);
1511 }
1512
1513 return addr;
1514 }
1515
dm_pci_map_ea_bar(struct udevice * dev,int bar,size_t offset,size_t len,int ea_off,struct pci_child_plat * pdata)1516 static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, size_t offset,
1517 size_t len, int ea_off,
1518 struct pci_child_plat *pdata)
1519 {
1520 int ea_cnt, i, entry_size;
1521 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1522 u32 ea_entry;
1523 phys_addr_t addr;
1524
1525 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1526 /*
1527 * In the case of a Virtual Function device, device is
1528 * Physical function, so pdata will point to required VF
1529 * specific data.
1530 */
1531 if (pdata->is_virtfn)
1532 bar_id += PCI_EA_BEI_VF_BAR0;
1533 }
1534
1535 /* EA capability structure header */
1536 dm_pci_read_config32(dev, ea_off, &ea_entry);
1537 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1538 ea_off += PCI_EA_FIRST_ENT;
1539
1540 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1541 /* Entry header */
1542 dm_pci_read_config32(dev, ea_off, &ea_entry);
1543 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1544
1545 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1546 continue;
1547
1548 /* Base address, 1st DW */
1549 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1550 addr = ea_entry & PCI_EA_FIELD_MASK;
1551 if (ea_entry & PCI_EA_IS_64) {
1552 /* Base address, 2nd DW, skip over 4B MaxOffset */
1553 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1554 addr |= ((u64)ea_entry) << 32;
1555 }
1556
1557 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1558 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1559
1560 if (~((phys_addr_t)0) - addr < offset)
1561 return NULL;
1562
1563 /* size ignored for now */
1564 return map_physmem(addr + offset, len, MAP_NOCACHE);
1565 }
1566
1567 return 0;
1568 }
1569
dm_pci_map_bar(struct udevice * dev,int bar,size_t offset,size_t len,unsigned long mask,unsigned long flags)1570 void *dm_pci_map_bar(struct udevice *dev, int bar, size_t offset, size_t len,
1571 unsigned long mask, unsigned long flags)
1572 {
1573 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
1574 struct udevice *udev = dev;
1575 pci_addr_t pci_bus_addr;
1576 u32 bar_response;
1577 int ea_off;
1578
1579 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1580 /*
1581 * In case of Virtual Function devices, use PF udevice
1582 * as EA capability is defined in Physical Function
1583 */
1584 if (pdata->is_virtfn)
1585 udev = pdata->pfdev;
1586 }
1587
1588 /*
1589 * if the function supports Enhanced Allocation use that instead of
1590 * BARs
1591 * Incase of virtual functions, pdata will help read VF BEI
1592 * and EA entry size.
1593 */
1594 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
1595 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
1596 else
1597 ea_off = 0;
1598
1599 if (ea_off)
1600 return dm_pci_map_ea_bar(udev, bar, offset, len, ea_off, pdata);
1601
1602 /* read BAR address */
1603 dm_pci_read_config32(udev, bar, &bar_response);
1604 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1605
1606 if (~((pci_addr_t)0) - pci_bus_addr < offset)
1607 return NULL;
1608
1609 /*
1610 * Forward the length argument to dm_pci_bus_to_virt. The length will
1611 * be used to check that the entire address range has been declared as
1612 * a PCI range, but a better check would be to probe for the size of
1613 * the bar and prevent overflow more locally.
1614 */
1615 return dm_pci_bus_to_virt(udev, pci_bus_addr + offset, len, mask, flags,
1616 MAP_NOCACHE);
1617 }
1618
_dm_pci_find_next_capability(struct udevice * dev,u8 pos,int cap)1619 static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
1620 {
1621 int ttl = PCI_FIND_CAP_TTL;
1622 u8 id;
1623 u16 ent;
1624
1625 dm_pci_read_config8(dev, pos, &pos);
1626
1627 while (ttl--) {
1628 if (pos < PCI_STD_HEADER_SIZEOF)
1629 break;
1630 pos &= ~3;
1631 dm_pci_read_config16(dev, pos, &ent);
1632
1633 id = ent & 0xff;
1634 if (id == 0xff)
1635 break;
1636 if (id == cap)
1637 return pos;
1638 pos = (ent >> 8);
1639 }
1640
1641 return 0;
1642 }
1643
dm_pci_find_next_capability(struct udevice * dev,u8 start,int cap)1644 int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1645 {
1646 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1647 cap);
1648 }
1649
dm_pci_find_capability(struct udevice * dev,int cap)1650 int dm_pci_find_capability(struct udevice *dev, int cap)
1651 {
1652 u16 status;
1653 u8 header_type;
1654 u8 pos;
1655
1656 dm_pci_read_config16(dev, PCI_STATUS, &status);
1657 if (!(status & PCI_STATUS_CAP_LIST))
1658 return 0;
1659
1660 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1661 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1662 pos = PCI_CB_CAPABILITY_LIST;
1663 else
1664 pos = PCI_CAPABILITY_LIST;
1665
1666 return _dm_pci_find_next_capability(dev, pos, cap);
1667 }
1668
dm_pci_find_next_ext_capability(struct udevice * dev,int start,int cap)1669 int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
1670 {
1671 u32 header;
1672 int ttl;
1673 int pos = PCI_CFG_SPACE_SIZE;
1674
1675 /* minimum 8 bytes per capability */
1676 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1677
1678 if (start)
1679 pos = start;
1680
1681 dm_pci_read_config32(dev, pos, &header);
1682 /*
1683 * If we have no capabilities, this is indicated by cap ID,
1684 * cap version and next pointer all being 0.
1685 */
1686 if (header == 0)
1687 return 0;
1688
1689 while (ttl--) {
1690 if (PCI_EXT_CAP_ID(header) == cap)
1691 return pos;
1692
1693 pos = PCI_EXT_CAP_NEXT(header);
1694 if (pos < PCI_CFG_SPACE_SIZE)
1695 break;
1696
1697 dm_pci_read_config32(dev, pos, &header);
1698 }
1699
1700 return 0;
1701 }
1702
dm_pci_find_ext_capability(struct udevice * dev,int cap)1703 int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1704 {
1705 return dm_pci_find_next_ext_capability(dev, 0, cap);
1706 }
1707
dm_pci_flr(struct udevice * dev)1708 int dm_pci_flr(struct udevice *dev)
1709 {
1710 int pcie_off;
1711 u32 cap;
1712
1713 /* look for PCI Express Capability */
1714 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1715 if (!pcie_off)
1716 return -ENOENT;
1717
1718 /* check FLR capability */
1719 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1720 if (!(cap & PCI_EXP_DEVCAP_FLR))
1721 return -ENOENT;
1722
1723 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1724 PCI_EXP_DEVCTL_BCR_FLR);
1725
1726 /* wait 100ms, per PCI spec */
1727 mdelay(100);
1728
1729 return 0;
1730 }
1731
1732 #if defined(CONFIG_PCI_SRIOV)
pci_sriov_init(struct udevice * pdev,int vf_en)1733 int pci_sriov_init(struct udevice *pdev, int vf_en)
1734 {
1735 u16 vendor, device;
1736 struct udevice *bus;
1737 struct udevice *dev;
1738 pci_dev_t bdf;
1739 u16 ctrl;
1740 u16 num_vfs;
1741 u16 total_vf;
1742 u16 vf_offset;
1743 u16 vf_stride;
1744 int vf, ret;
1745 int pos;
1746
1747 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1748 if (!pos) {
1749 debug("Error: SRIOV capability not found\n");
1750 return -ENOENT;
1751 }
1752
1753 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1754
1755 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1756 if (vf_en > total_vf)
1757 vf_en = total_vf;
1758 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1759
1760 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1761 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1762
1763 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1764 if (num_vfs > vf_en)
1765 num_vfs = vf_en;
1766
1767 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1768 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1769
1770 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1771 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1772
1773 bdf = dm_pci_get_bdf(pdev);
1774
1775 ret = pci_get_bus(PCI_BUS(bdf), &bus);
1776 if (ret)
1777 return ret;
1778
1779 bdf += PCI_BDF(0, 0, vf_offset);
1780
1781 for (vf = 0; vf < num_vfs; vf++) {
1782 struct pci_child_plat *pplat;
1783 ulong class;
1784
1785 pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1786 &class, PCI_SIZE_16);
1787
1788 debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
1789 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
1790
1791 /* Find this device in the device tree */
1792 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1793
1794 if (ret == -ENODEV) {
1795 struct pci_device_id find_id;
1796
1797 memset(&find_id, '\0', sizeof(find_id));
1798 find_id.vendor = vendor;
1799 find_id.device = device;
1800 find_id.class = class;
1801
1802 ret = pci_find_and_bind_driver(bus, &find_id,
1803 bdf, &dev);
1804
1805 if (ret)
1806 return ret;
1807 }
1808
1809 /* Update the platform data */
1810 pplat = dev_get_parent_plat(dev);
1811 pplat->devfn = PCI_MASK_BUS(bdf);
1812 pplat->vendor = vendor;
1813 pplat->device = device;
1814 pplat->class = class;
1815 pplat->is_virtfn = true;
1816 pplat->pfdev = pdev;
1817 pplat->virtid = vf * vf_stride + vf_offset;
1818
1819 debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
1820 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
1821 PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1822 bdf += PCI_BDF(0, 0, vf_stride);
1823 }
1824
1825 return 0;
1826 }
1827
pci_sriov_get_totalvfs(struct udevice * pdev)1828 int pci_sriov_get_totalvfs(struct udevice *pdev)
1829 {
1830 u16 total_vf;
1831 int pos;
1832
1833 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1834 if (!pos) {
1835 debug("Error: SRIOV capability not found\n");
1836 return -ENOENT;
1837 }
1838
1839 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1840
1841 return total_vf;
1842 }
1843 #endif /* SRIOV */
1844
1845 UCLASS_DRIVER(pci) = {
1846 .id = UCLASS_PCI,
1847 .name = "pci",
1848 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
1849 .post_bind = dm_scan_fdt_dev,
1850 .pre_probe = pci_uclass_pre_probe,
1851 .post_probe = pci_uclass_post_probe,
1852 .child_post_bind = pci_uclass_child_post_bind,
1853 .per_device_auto = sizeof(struct pci_controller),
1854 .per_child_plat_auto = sizeof(struct pci_child_plat),
1855 };
1856
1857 static const struct dm_pci_ops pci_bridge_ops = {
1858 .read_config = pci_bridge_read_config,
1859 .write_config = pci_bridge_write_config,
1860 };
1861
1862 static const struct udevice_id pci_bridge_ids[] = {
1863 { .compatible = "pci-bridge" },
1864 { }
1865 };
1866
1867 U_BOOT_DRIVER(pci_bridge_drv) = {
1868 .name = "pci_bridge_drv",
1869 .id = UCLASS_PCI,
1870 .of_match = pci_bridge_ids,
1871 .ops = &pci_bridge_ops,
1872 };
1873
1874 UCLASS_DRIVER(pci_generic) = {
1875 .id = UCLASS_PCI_GENERIC,
1876 .name = "pci_generic",
1877 };
1878
1879 static const struct udevice_id pci_generic_ids[] = {
1880 { .compatible = "pci-generic" },
1881 { }
1882 };
1883
1884 U_BOOT_DRIVER(pci_generic_drv) = {
1885 .name = "pci_generic_drv",
1886 .id = UCLASS_PCI_GENERIC,
1887 .of_match = pci_generic_ids,
1888 };
1889
pci_init(void)1890 int pci_init(void)
1891 {
1892 struct udevice *bus;
1893
1894 /*
1895 * Enumerate all known controller devices. Enumeration has the side-
1896 * effect of probing them, so PCIe devices will be enumerated too.
1897 */
1898 for (uclass_first_device_check(UCLASS_PCI, &bus);
1899 bus;
1900 uclass_next_device_check(&bus)) {
1901 ;
1902 }
1903
1904 return 0;
1905 }
1906