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