1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
7 #define LOG_CATEGORY UCLASS_PCI
16 #include <asm/global_data.h>
18 #include <dm/device-internal.h>
20 #include <dm/uclass-internal.h>
21 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
22 #include <asm/fsp/fsp_support.h>
24 #include <dt-bindings/pci/pci.h>
25 #include <linux/delay.h>
26 #include <linux/printk.h>
27 #include "pci_internal.h"
29 DECLARE_GLOBAL_DATA_PTR;
31 int pci_get_bus(int busnum, struct udevice **busp)
35 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
37 /* Since buses may not be numbered yet try a little harder with bus 0 */
39 ret = uclass_first_device_err(UCLASS_PCI, busp);
42 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
48 struct udevice *pci_get_controller(struct udevice *dev)
50 while (device_is_on_pci_bus(dev))
56 pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
58 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
59 struct udevice *bus = dev->parent;
62 * This error indicates that @dev is a device on an unprobed PCI bus.
63 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
64 * will produce a bad BDF>
66 * A common cause of this problem is that this function is called in the
67 * of_to_plat() method of @dev. Accessing the PCI bus in that
68 * method is not allowed, since it has not yet been probed. To fix this,
69 * move that access to the probe() method of @dev instead.
71 if (!device_active(bus))
72 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
74 return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
78 * pci_get_bus_max() - returns the bus number of the last active bus
80 * Return: last bus number, or -1 if no active buses
82 static int pci_get_bus_max(void)
88 ret = uclass_get(UCLASS_PCI, &uc);
89 uclass_foreach_dev(bus, uc) {
90 if (dev_seq(bus) > ret)
94 debug("%s: ret=%d\n", __func__, ret);
99 int pci_last_busno(void)
101 return pci_get_bus_max();
104 int pci_get_ff(enum pci_size_t size)
116 static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
119 struct fdt_pci_addr addr;
123 dev_for_each_subnode(node, bus) {
124 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
129 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
137 int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
138 struct udevice **devp)
142 for (device_find_first_child(bus, &dev);
144 device_find_next_child(&dev)) {
145 struct pci_child_plat *pplat;
147 pplat = dev_get_parent_plat(dev);
148 if (pplat && pplat->devfn == find_devfn) {
157 int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
162 ret = pci_get_bus(PCI_BUS(bdf), &bus);
165 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
168 static int pci_device_matches_ids(struct udevice *dev,
169 const struct pci_device_id *ids)
171 struct pci_child_plat *pplat;
174 pplat = dev_get_parent_plat(dev);
177 for (i = 0; ids[i].vendor != 0; i++) {
178 if (pplat->vendor == ids[i].vendor &&
179 pplat->device == ids[i].device)
186 int pci_bus_find_devices(struct udevice *bus, const struct pci_device_id *ids,
187 int *indexp, struct udevice **devp)
191 /* Scan all devices on this bus */
192 for (device_find_first_child(bus, &dev);
194 device_find_next_child(&dev)) {
195 if (pci_device_matches_ids(dev, ids) >= 0) {
196 if ((*indexp)-- <= 0) {
206 int pci_find_device_id(const struct pci_device_id *ids, int index,
207 struct udevice **devp)
211 /* Scan all known buses */
212 for (uclass_first_device(UCLASS_PCI, &bus);
214 uclass_next_device(&bus)) {
215 if (!pci_bus_find_devices(bus, ids, &index, devp))
223 static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
224 unsigned int device, int *indexp,
225 struct udevice **devp)
227 struct pci_child_plat *pplat;
230 for (device_find_first_child(bus, &dev);
232 device_find_next_child(&dev)) {
233 pplat = dev_get_parent_plat(dev);
234 if (pplat->vendor == vendor && pplat->device == device) {
245 int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
246 struct udevice **devp)
250 /* Scan all known buses */
251 for (uclass_first_device(UCLASS_PCI, &bus);
253 uclass_next_device(&bus)) {
254 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
255 return device_probe(*devp);
262 int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
266 /* Scan all known buses */
267 for (pci_find_first_device(&dev);
269 pci_find_next_device(&dev)) {
270 struct pci_child_plat *pplat = dev_get_parent_plat(dev);
272 if (pplat->class == find_class && !index--) {
274 return device_probe(*devp);
282 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
283 unsigned long value, enum pci_size_t size)
285 struct dm_pci_ops *ops;
287 ops = pci_get_ops(bus);
288 if (!ops->write_config)
290 if (offset < 0 || offset >= 4096)
292 return ops->write_config(bus, bdf, offset, value, size);
295 int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
301 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
307 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
310 static int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
311 enum pci_size_t size)
316 ret = pci_get_bus(PCI_BUS(bdf), &bus);
320 return pci_bus_write_config(bus, bdf, offset, value, size);
323 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
324 enum pci_size_t size)
328 for (bus = dev; device_is_on_pci_bus(bus);)
330 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
334 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
336 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
339 int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
341 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
344 int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
346 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
349 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
351 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
354 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
356 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
359 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
361 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
364 int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
365 unsigned long *valuep, enum pci_size_t size)
367 struct dm_pci_ops *ops;
369 ops = pci_get_ops(bus);
370 if (!ops->read_config) {
371 *valuep = pci_conv_32_to_size(~0, offset, size);
374 if (offset < 0 || offset >= 4096) {
375 *valuep = pci_conv_32_to_size(0, offset, size);
378 return ops->read_config(bus, bdf, offset, valuep, size);
381 static int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
382 enum pci_size_t size)
387 ret = pci_get_bus(PCI_BUS(bdf), &bus);
391 return pci_bus_read_config(bus, bdf, offset, valuep, size);
394 int dm_pci_read_config(const struct udevice *dev, int offset,
395 unsigned long *valuep, enum pci_size_t size)
397 const struct udevice *bus;
399 for (bus = dev; device_is_on_pci_bus(bus);)
401 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
405 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
410 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
418 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
423 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
431 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
436 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
444 int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
449 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
457 int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
462 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
470 int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
475 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
483 int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
488 ret = dm_pci_read_config8(dev, offset, &val);
494 return dm_pci_write_config8(dev, offset, val);
497 int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
502 ret = dm_pci_read_config16(dev, offset, &val);
508 return dm_pci_write_config16(dev, offset, val);
511 int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
516 ret = dm_pci_read_config32(dev, offset, &val);
522 return dm_pci_write_config32(dev, offset, val);
525 static void set_vga_bridge_bits(struct udevice *dev)
527 struct udevice *parent = dev->parent;
530 while (dev_seq(parent) != 0) {
531 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
532 bc |= PCI_BRIDGE_CTL_VGA;
533 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
534 parent = parent->parent;
538 int pci_auto_config_devices(struct udevice *bus)
540 struct pci_controller *hose = dev_get_uclass_priv(bus);
541 struct pci_child_plat *pplat;
542 unsigned int sub_bus;
545 sub_bus = dev_seq(bus);
546 debug("%s: start\n", __func__);
547 pciauto_config_init(hose);
548 for (device_find_first_child(bus, &dev);
550 device_find_next_child(&dev)) {
551 unsigned int max_bus;
554 debug("%s: device %s\n", __func__, dev->name);
555 if (dev_has_ofnode(dev) &&
556 dev_read_bool(dev, "pci,no-autoconfig"))
558 ret = dm_pciauto_config_device(dev);
560 return log_msg_ret("auto", ret);
562 sub_bus = max(sub_bus, max_bus);
564 if (dev_get_parent(dev) == bus)
567 pplat = dev_get_parent_plat(dev);
568 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
569 set_vga_bridge_bits(dev);
571 if (hose->last_busno < sub_bus)
572 hose->last_busno = sub_bus;
573 debug("%s: done\n", __func__);
575 return log_msg_ret("sub", sub_bus);
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,
585 enum pci_size_t size)
589 if (addr_f(bus, bdf, offset, &address) < 0)
594 writeb(value, address);
597 writew(value, address);
600 writel(value, address);
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,
614 enum pci_size_t size)
618 if (addr_f(bus, bdf, offset, &address) < 0) {
619 *valuep = pci_get_ff(size);
625 *valuep = readb(address);
628 *valuep = readw(address);
631 *valuep = readl(address);
638 int dm_pci_hose_probe_bus(struct udevice *bus)
646 debug("%s\n", __func__);
648 dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);
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);
656 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
657 ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
662 dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
666 sub_bus = pci_get_bus_max() + 1;
668 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
669 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
671 ret = device_probe(bus);
673 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
675 return log_msg_ret("probe", ret);
679 sub_bus = pci_get_bus_max();
681 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
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
692 * Returns true if the finding pci_device_id structure matched or false if
695 static bool pci_match_one_id(const struct pci_device_id *id,
696 const struct pci_device_id *find)
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))
709 * pci_need_device_pre_reloc() - Check if a device should be bound
711 * This checks a list of vendor/device-ID values indicating devices that should
712 * be bound before relocation.
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
719 static bool pci_need_device_pre_reloc(struct udevice *bus, uint vendor,
725 if (xpl_phase() == PHASE_SPL && CONFIG_IS_ENABLED(PCI_PNP))
729 !dev_read_u32_index(bus, "u-boot,pci-pre-reloc", index,
732 if (vendev == PCI_VENDEV(vendor, device))
740 * pci_find_and_bind_driver() - Find and bind the right PCI driver
742 * This only looks at certain fields in the descriptor.
744 * @parent: Parent bus
745 * @find_id: Specification of the driver to find
746 * @bdf: Bus/device/function addreess - see PCI_BDF()
747 * @devp: Returns a pointer to the device created
748 * Return: 0 if OK, -EPERM if the device is not needed before relocation and
749 * therefore was not created, other -ve value on error
751 static int pci_find_and_bind_driver(struct udevice *parent,
752 struct pci_device_id *find_id,
753 pci_dev_t bdf, struct udevice **devp)
755 struct pci_driver_entry *start, *entry;
756 ofnode node = ofnode_null();
765 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
766 find_id->vendor, find_id->device);
768 /* Determine optional OF node */
769 if (ofnode_valid(dev_ofnode(parent)))
770 pci_dev_find_ofnode(parent, bdf, &node);
772 if (ofnode_valid(node) && !ofnode_is_enabled(node)) {
773 debug("%s: Ignoring disabled device\n", __func__);
774 return log_msg_ret("dis", -EPERM);
777 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
778 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
779 for (entry = start; entry != start + n_ents; entry++) {
780 const struct pci_device_id *id;
782 const struct driver *drv;
784 for (id = entry->match;
785 id->vendor || id->subvendor || id->class_mask;
787 if (!pci_match_one_id(id, find_id))
793 * In the pre-relocation phase, we only bind devices
794 * whose driver has the DM_FLAG_PRE_RELOC set, to save
795 * precious memory space as on some platforms as that
796 * space is pretty limited (ie: using Cache As RAM).
798 if (!(gd->flags & GD_FLG_RELOC) &&
799 !(drv->flags & DM_FLAG_PRE_RELOC) &&
800 (!CONFIG_IS_ENABLED(PCI_PNP) ||
801 xpl_phase() != PHASE_SPL))
802 return log_msg_ret("pre", -EPERM);
805 * We could pass the descriptor to the driver as
806 * plat (instead of NULL) and allow its bind()
807 * method to return -ENOENT if it doesn't support this
808 * device. That way we could continue the search to
809 * find another driver. For now this doesn't seem
810 * necesssary, so just bind the first match.
812 ret = device_bind(parent, drv, drv->name, NULL, node,
816 debug("%s: Match found: %s\n", __func__, drv->name);
817 dev->driver_data = id->driver_data;
823 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
825 * In the pre-relocation phase, we only bind bridge devices to save
826 * precious memory space as on some platforms as that space is pretty
827 * limited (ie: using Cache As RAM).
829 if (!(gd->flags & GD_FLG_RELOC) && !bridge &&
830 !pci_need_device_pre_reloc(parent, find_id->vendor,
832 return log_msg_ret("notbr", -EPERM);
834 /* Bind a generic driver so that the device can be used */
835 sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
840 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
842 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
844 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
848 debug("%s: No match found: bound generic driver instead\n", __func__);
853 debug("%s: No match found: error %d\n", __func__, ret);
857 __weak extern void board_pci_fixup_dev(struct udevice *bus, struct udevice *dev)
861 int pci_bind_bus_devices(struct udevice *bus)
863 ulong vendor, device;
871 end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
872 PCI_MAX_PCI_FUNCTIONS - 1);
873 for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
874 bdf += PCI_BDF(0, 0, 1)) {
875 struct pci_child_plat *pplat;
881 if (PCI_FUNC(bdf) && !found_multi)
884 /* Check only the first access, we don't expect problems */
885 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
887 if (ret || vendor == 0xffff || vendor == 0x0000)
890 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
891 &header_type, PCI_SIZE_8);
894 found_multi = header_type & 0x80;
896 debug("%s: bus %d/%s: found device %x, function %d", __func__,
897 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
898 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
900 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
904 /* Find this device in the device tree */
905 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
906 debug(": find ret=%d\n", ret);
908 /* If nothing in the device tree, bind a device */
909 if (ret == -ENODEV) {
910 struct pci_device_id find_id;
913 memset(&find_id, '\0', sizeof(find_id));
914 find_id.vendor = vendor;
915 find_id.device = device;
916 find_id.class = class;
917 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
918 pci_bus_read_config(bus, bdf,
919 PCI_SUBSYSTEM_VENDOR_ID,
921 find_id.subvendor = val & 0xffff;
922 find_id.subdevice = val >> 16;
924 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
927 debug("device: %s\n", dev->name);
934 /* Update the platform data */
935 pplat = dev_get_parent_plat(dev);
936 pplat->devfn = PCI_MASK_BUS(bdf);
937 pplat->vendor = vendor;
938 pplat->device = device;
939 pplat->class = class;
941 if (IS_ENABLED(CONFIG_PCI_ARID)) {
942 ari_off = dm_pci_find_ext_capability(dev,
948 * Read Next Function number in ARI Cap
951 dm_pci_read_config16(dev, ari_off + 4,
954 * Update next scan on this function number,
955 * subtract 1 in BDF to satisfy loop increment.
957 if (ari_cap & 0xff00) {
958 bdf = PCI_BDF(PCI_BUS(bdf),
966 board_pci_fixup_dev(bus, dev);
972 static int decode_regions(struct pci_controller *hose, ofnode parent_node,
975 int pci_addr_cells, addr_cells, size_cells;
976 int cells_per_record;
983 /* handle booting from coreboot, etc. */
987 prop = ofnode_get_property(node, "ranges", &len);
989 debug("%s: Cannot decode regions\n", __func__);
993 pci_addr_cells = ofnode_read_simple_addr_cells(node);
994 addr_cells = ofnode_read_simple_addr_cells(parent_node);
995 size_cells = ofnode_read_simple_size_cells(node);
997 /* PCI addresses are always 3-cells */
999 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1000 hose->region_count = 0;
1001 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1004 /* Dynamically allocate the regions array */
1005 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
1006 hose->regions = (struct pci_region *)
1007 calloc(1, max_regions * sizeof(struct pci_region));
1011 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
1012 u64 pci_addr, addr, size;
1018 if (len < cells_per_record)
1020 flags = fdt32_to_cpu(prop[0]);
1021 space_code = (flags >> 24) & 3;
1022 pci_addr = fdtdec_get_number(prop + 1, 2);
1023 prop += pci_addr_cells;
1024 addr = fdtdec_get_number(prop, addr_cells);
1026 size = fdtdec_get_number(prop, size_cells);
1028 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
1029 __func__, hose->region_count, pci_addr, addr, size, space_code);
1030 if (space_code & 2) {
1031 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
1033 } else if (space_code & 1) {
1034 type = PCI_REGION_IO;
1039 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
1040 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
1041 debug(" - pci_addr beyond the 32-bit boundary, ignoring\n");
1045 if (!IS_ENABLED(CONFIG_PHYS_64BIT) && upper_32_bits(addr)) {
1046 debug(" - addr beyond the 32-bit boundary, ignoring\n");
1050 if (~((pci_addr_t)0) - pci_addr < size) {
1051 debug(" - PCI range exceeds max address, ignoring\n");
1055 if (~((phys_addr_t)0) - addr < size) {
1056 debug(" - phys range exceeds max address, ignoring\n");
1061 if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
1062 for (i = 0; i < hose->region_count; i++) {
1063 if (hose->regions[i].flags == type)
1069 pos = hose->region_count++;
1070 debug(" - type=%d, pos=%d\n", type, pos);
1071 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
1074 /* Add a region for our local memory */
1079 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
1080 if (bd->bi_dram[i].size) {
1081 phys_addr_t start = bd->bi_dram[i].start;
1083 if (IS_ENABLED(CONFIG_PCI_MAP_SYSTEM_MEMORY))
1084 start = virt_to_phys((void *)(uintptr_t)bd->bi_dram[i].start);
1086 pci_set_region(hose->regions + hose->region_count++,
1087 start, start, bd->bi_dram[i].size,
1088 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
1095 static int pci_uclass_pre_probe(struct udevice *bus)
1097 struct pci_controller *hose;
1101 debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
1103 hose = dev_get_uclass_priv(bus);
1106 * Set the sequence number, if device_bind() doesn't. We want control
1107 * of this so that numbers are allocated as devices are probed. That
1108 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1109 * higher than their parents)
1111 if (dev_seq(bus) == -1) {
1112 ret = uclass_get(UCLASS_PCI, &uc);
1115 bus->seq_ = uclass_find_next_free_seq(uc);
1118 /* For bridges, use the top-level PCI controller */
1119 if (!device_is_on_pci_bus(bus)) {
1121 ret = decode_regions(hose, dev_ofnode(bus->parent),
1126 struct pci_controller *parent_hose;
1128 parent_hose = dev_get_uclass_priv(bus->parent);
1129 hose->ctlr = parent_hose->bus;
1133 hose->first_busno = dev_seq(bus);
1134 hose->last_busno = dev_seq(bus);
1135 if (dev_has_ofnode(bus)) {
1136 hose->skip_auto_config_until_reloc =
1138 "u-boot,skip-auto-config-until-reloc");
1144 static int pci_uclass_post_probe(struct udevice *bus)
1146 struct pci_controller *hose = dev_get_uclass_priv(bus);
1149 debug("%s: probing bus %d\n", __func__, dev_seq(bus));
1150 ret = pci_bind_bus_devices(bus);
1152 return log_msg_ret("bind", ret);
1154 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
1155 (!hose->skip_auto_config_until_reloc ||
1156 (gd->flags & GD_FLG_RELOC))) {
1157 ret = pci_auto_config_devices(bus);
1159 return log_msg_ret("cfg", ret);
1162 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1164 * Per Intel FSP specification, we should call FSP notify API to
1165 * inform FSP that PCI enumeration has been done so that FSP will
1166 * do any necessary initialization as required by the chipset's
1167 * BIOS Writer's Guide (BWG).
1169 * Unfortunately we have to put this call here as with driver model,
1170 * the enumeration is all done on a lazy basis as needed, so until
1171 * something is touched on PCI it won't happen.
1173 * Note we only call this 1) after U-Boot is relocated, and 2)
1174 * root bus has finished probing.
1176 if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
1177 ret = fsp_init_phase_pci();
1179 return log_msg_ret("fsp", ret);
1186 static int pci_uclass_child_post_bind(struct udevice *dev)
1188 struct pci_child_plat *pplat;
1190 if (!dev_has_ofnode(dev))
1193 pplat = dev_get_parent_plat(dev);
1195 /* Extract vendor id and device id if available */
1196 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1198 /* Extract the devfn from fdt_pci_addr */
1199 pplat->devfn = pci_get_devfn(dev);
1204 static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
1205 uint offset, ulong *valuep,
1206 enum pci_size_t size)
1208 struct pci_controller *hose = dev_get_uclass_priv(bus);
1210 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1213 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1214 uint offset, ulong value,
1215 enum pci_size_t size)
1217 struct pci_controller *hose = dev_get_uclass_priv(bus);
1219 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1222 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1224 struct udevice *dev;
1227 * Scan through all the PCI controllers. On x86 there will only be one
1228 * but that is not necessarily true on other hardware.
1231 device_find_first_child(bus, &dev);
1236 uclass_next_device(&bus);
1242 int pci_find_next_device(struct udevice **devp)
1244 struct udevice *child = *devp;
1245 struct udevice *bus = child->parent;
1247 /* First try all the siblings */
1250 device_find_next_child(&child);
1257 /* We ran out of siblings. Try the next bus */
1258 uclass_next_device(&bus);
1260 return bus ? skip_to_next_device(bus, devp) : 0;
1263 int pci_find_first_device(struct udevice **devp)
1265 struct udevice *bus;
1268 uclass_first_device(UCLASS_PCI, &bus);
1270 return skip_to_next_device(bus, devp);
1273 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1277 return (value >> ((offset & 3) * 8)) & 0xff;
1279 return (value >> ((offset & 2) * 8)) & 0xffff;
1285 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1286 enum pci_size_t size)
1289 uint val_mask, shift;
1304 shift = (offset & off_mask) * 8;
1305 ldata = (value & val_mask) << shift;
1306 mask = val_mask << shift;
1307 value = (old & ~mask) | ldata;
1312 int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1314 int pci_addr_cells, addr_cells, size_cells;
1315 int cells_per_record;
1320 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1322 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1327 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1328 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1329 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1331 /* PCI addresses are always 3-cells */
1333 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1334 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1338 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1339 prop += pci_addr_cells;
1340 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1342 memp->size = fdtdec_get_number(prop, size_cells);
1348 len -= cells_per_record;
1354 int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1355 struct pci_region **memp, struct pci_region **prefp)
1357 struct udevice *bus = pci_get_controller(dev);
1358 struct pci_controller *hose = dev_get_uclass_priv(bus);
1364 for (i = 0; i < hose->region_count; i++) {
1365 switch (hose->regions[i].flags) {
1367 if (!*iop || (*iop)->size < hose->regions[i].size)
1368 *iop = hose->regions + i;
1370 case PCI_REGION_MEM:
1371 if (!*memp || (*memp)->size < hose->regions[i].size)
1372 *memp = hose->regions + i;
1374 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1375 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1376 *prefp = hose->regions + i;
1381 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1384 u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
1389 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1390 dm_pci_read_config32(dev, bar, &addr);
1393 * If we get an invalid address, return this so that comparisons with
1394 * FDT_ADDR_T_NONE work correctly
1396 if (addr == 0xffffffff)
1398 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1399 return addr & PCI_BASE_ADDRESS_IO_MASK;
1401 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1404 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1408 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1409 dm_pci_write_config32(dev, bar, addr);
1412 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1413 size_t len, unsigned long mask,
1414 unsigned long flags)
1416 struct udevice *ctlr;
1417 struct pci_controller *hose;
1418 struct pci_region *res;
1422 /* The root controller has the region information */
1423 ctlr = pci_get_controller(dev);
1424 hose = dev_get_uclass_priv(ctlr);
1426 if (hose->region_count == 0)
1429 for (i = 0; i < hose->region_count; i++) {
1430 res = &hose->regions[i];
1432 if ((res->flags & mask) != flags)
1435 if (bus_addr < res->bus_start)
1438 offset = bus_addr - res->bus_start;
1439 if (offset >= res->size)
1442 if (len > res->size - offset)
1445 return res->phys_start + offset;
1448 puts("dm_pci_bus_to_phys: invalid physical address\n");
1452 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1453 size_t len, unsigned long mask,
1454 unsigned long flags)
1456 struct udevice *ctlr;
1457 struct pci_controller *hose;
1458 struct pci_region *res;
1462 /* The root controller has the region information */
1463 ctlr = pci_get_controller(dev);
1464 hose = dev_get_uclass_priv(ctlr);
1466 if (hose->region_count == 0)
1469 for (i = 0; i < hose->region_count; i++) {
1470 res = &hose->regions[i];
1472 if ((res->flags & mask) != flags)
1475 if (phys_addr < res->phys_start)
1478 offset = phys_addr - res->phys_start;
1479 if (offset >= res->size)
1482 if (len > res->size - offset)
1485 return res->bus_start + offset;
1488 puts("dm_pci_phys_to_bus: invalid physical address\n");
1492 static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
1493 struct pci_child_plat *pdata)
1495 phys_addr_t addr = 0;
1498 * In the case of a Virtual Function device using BAR
1499 * base and size, add offset for VFn BAR(1, 2, 3...n)
1501 if (pdata->is_virtfn) {
1505 /* MaxOffset, 1st DW */
1506 dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1507 sz = ea_entry & PCI_EA_FIELD_MASK;
1508 /* Fill up lower 2 bits */
1509 sz |= (~PCI_EA_FIELD_MASK);
1511 if (ea_entry & PCI_EA_IS_64) {
1512 /* MaxOffset 2nd DW */
1513 dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1514 sz |= ((u64)ea_entry) << 32;
1517 addr = (pdata->virtid - 1) * (sz + 1);
1523 static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, size_t offset,
1524 size_t len, int ea_off,
1525 struct pci_child_plat *pdata)
1527 int ea_cnt, i, entry_size;
1528 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1532 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1534 * In the case of a Virtual Function device, device is
1535 * Physical function, so pdata will point to required VF
1538 if (pdata->is_virtfn)
1539 bar_id += PCI_EA_BEI_VF_BAR0;
1542 /* EA capability structure header */
1543 dm_pci_read_config32(dev, ea_off, &ea_entry);
1544 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1545 ea_off += PCI_EA_FIRST_ENT;
1547 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1549 dm_pci_read_config32(dev, ea_off, &ea_entry);
1550 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1552 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1555 /* Base address, 1st DW */
1556 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1557 addr = ea_entry & PCI_EA_FIELD_MASK;
1558 if (ea_entry & PCI_EA_IS_64) {
1559 /* Base address, 2nd DW, skip over 4B MaxOffset */
1560 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1561 addr |= ((u64)ea_entry) << 32;
1564 if (IS_ENABLED(CONFIG_PCI_SRIOV))
1565 addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1567 if (~((phys_addr_t)0) - addr < offset)
1570 /* size ignored for now */
1571 return map_physmem(addr + offset, len, MAP_NOCACHE);
1577 void *dm_pci_map_bar(struct udevice *dev, int bar, size_t offset, size_t len,
1578 unsigned long mask, unsigned long flags)
1580 struct pci_child_plat *pdata = dev_get_parent_plat(dev);
1581 struct udevice *udev = dev;
1582 pci_addr_t pci_bus_addr;
1586 if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1588 * In case of Virtual Function devices, use PF udevice
1589 * as EA capability is defined in Physical Function
1591 if (pdata->is_virtfn)
1592 udev = pdata->pfdev;
1596 * if the function supports Enhanced Allocation use that instead of
1598 * Incase of virtual functions, pdata will help read VF BEI
1599 * and EA entry size.
1601 if (IS_ENABLED(CONFIG_PCI_ENHANCED_ALLOCATION))
1602 ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
1607 return dm_pci_map_ea_bar(udev, bar, offset, len, ea_off, pdata);
1609 /* read BAR address */
1610 dm_pci_read_config32(udev, bar, &bar_response);
1611 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1613 /* This has a lot of baked in assumptions, but essentially tries
1614 * to mirror the behavior of BAR assignment for 64 Bit enabled
1615 * hosts and 64 bit placeable BARs in the auto assign code.
1617 #if defined(CONFIG_SYS_PCI_64BIT)
1618 if (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1619 dm_pci_read_config32(udev, bar + 4, &bar_response);
1620 pci_bus_addr |= (pci_addr_t)bar_response << 32;
1622 #endif /* CONFIG_SYS_PCI_64BIT */
1624 if (~((pci_addr_t)0) - pci_bus_addr < offset)
1628 * Forward the length argument to dm_pci_bus_to_virt. The length will
1629 * be used to check that the entire address range has been declared as
1630 * a PCI range, but a better check would be to probe for the size of
1631 * the bar and prevent overflow more locally.
1633 return dm_pci_bus_to_virt(udev, pci_bus_addr + offset, len, mask, flags,
1637 static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
1639 int ttl = PCI_FIND_CAP_TTL;
1643 dm_pci_read_config8(dev, pos, &pos);
1646 if (pos < PCI_STD_HEADER_SIZEOF)
1649 dm_pci_read_config16(dev, pos, &ent);
1662 int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1664 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1668 int dm_pci_find_capability(struct udevice *dev, int cap)
1674 dm_pci_read_config16(dev, PCI_STATUS, &status);
1675 if (!(status & PCI_STATUS_CAP_LIST))
1678 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1679 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1680 pos = PCI_CB_CAPABILITY_LIST;
1682 pos = PCI_CAPABILITY_LIST;
1684 return _dm_pci_find_next_capability(dev, pos, cap);
1687 int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
1691 int pos = PCI_CFG_SPACE_SIZE;
1693 /* minimum 8 bytes per capability */
1694 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1699 dm_pci_read_config32(dev, pos, &header);
1701 * If we have no capabilities, this is indicated by cap ID,
1702 * cap version and next pointer all being 0.
1708 if (PCI_EXT_CAP_ID(header) == cap)
1711 pos = PCI_EXT_CAP_NEXT(header);
1712 if (pos < PCI_CFG_SPACE_SIZE)
1715 dm_pci_read_config32(dev, pos, &header);
1721 int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1723 return dm_pci_find_next_ext_capability(dev, 0, cap);
1726 int dm_pci_flr(struct udevice *dev)
1731 /* look for PCI Express Capability */
1732 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1736 /* check FLR capability */
1737 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1738 if (!(cap & PCI_EXP_DEVCAP_FLR))
1741 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1742 PCI_EXP_DEVCTL_BCR_FLR);
1744 /* wait 100ms, per PCI spec */
1750 #if defined(CONFIG_PCI_SRIOV)
1751 int pci_sriov_init(struct udevice *pdev, int vf_en)
1754 struct udevice *bus;
1755 struct udevice *dev;
1765 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1767 debug("Error: SRIOV capability not found\n");
1771 dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1773 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1774 if (vf_en > total_vf)
1776 dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1778 ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1779 dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1781 dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1782 if (num_vfs > vf_en)
1785 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1786 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1788 dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1789 dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1791 bdf = dm_pci_get_bdf(pdev);
1793 ret = pci_get_bus(PCI_BUS(bdf), &bus);
1797 bdf += PCI_BDF(0, 0, vf_offset);
1799 for (vf = 0; vf < num_vfs; vf++) {
1800 struct pci_child_plat *pplat;
1803 pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1804 &class, PCI_SIZE_16);
1806 debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
1807 dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
1809 /* Find this device in the device tree */
1810 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1812 if (ret == -ENODEV) {
1813 struct pci_device_id find_id;
1815 memset(&find_id, '\0', sizeof(find_id));
1816 find_id.vendor = vendor;
1817 find_id.device = device;
1818 find_id.class = class;
1820 ret = pci_find_and_bind_driver(bus, &find_id,
1827 /* Update the platform data */
1828 pplat = dev_get_parent_plat(dev);
1829 pplat->devfn = PCI_MASK_BUS(bdf);
1830 pplat->vendor = vendor;
1831 pplat->device = device;
1832 pplat->class = class;
1833 pplat->is_virtfn = true;
1834 pplat->pfdev = pdev;
1835 pplat->virtid = vf * vf_stride + vf_offset;
1837 debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
1838 __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
1839 PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1840 bdf += PCI_BDF(0, 0, vf_stride);
1846 int pci_sriov_get_totalvfs(struct udevice *pdev)
1851 pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1853 debug("Error: SRIOV capability not found\n");
1857 dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1863 UCLASS_DRIVER(pci) = {
1866 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
1867 .post_bind = dm_scan_fdt_dev,
1868 .pre_probe = pci_uclass_pre_probe,
1869 .post_probe = pci_uclass_post_probe,
1870 .child_post_bind = pci_uclass_child_post_bind,
1871 .per_device_auto = sizeof(struct pci_controller),
1872 .per_child_plat_auto = sizeof(struct pci_child_plat),
1875 static const struct dm_pci_ops pci_bridge_ops = {
1876 .read_config = pci_bridge_read_config,
1877 .write_config = pci_bridge_write_config,
1880 static const struct udevice_id pci_bridge_ids[] = {
1881 { .compatible = "pci-bridge" },
1885 U_BOOT_DRIVER(pci_bridge_drv) = {
1886 .name = "pci_bridge_drv",
1888 .of_match = pci_bridge_ids,
1889 .ops = &pci_bridge_ops,
1892 UCLASS_DRIVER(pci_generic) = {
1893 .id = UCLASS_PCI_GENERIC,
1894 .name = "pci_generic",
1897 static const struct udevice_id pci_generic_ids[] = {
1898 { .compatible = "pci-generic" },
1902 U_BOOT_DRIVER(pci_generic_drv) = {
1903 .name = "pci_generic_drv",
1904 .id = UCLASS_PCI_GENERIC,
1905 .of_match = pci_generic_ids,
1910 struct udevice *bus;
1913 * Enumerate all known controller devices. Enumeration has the side-
1914 * effect of probing them, so PCIe devices will be enumerated too.
1916 for (uclass_first_device_check(UCLASS_PCI, &bus);
1918 uclass_next_device_check(&bus)) {