]> git.sur5r.net Git - u-boot/blobdiff - drivers/pci/pci-uclass.c
sbc8641d: increase malloc pool size to a sane default
[u-boot] / drivers / pci / pci-uclass.c
index 3be76c99ee471646dc5307164c6817d69563f793..0756bbe8f1326b6377d24f6e7ae0fa60f2f4470d 100644 (file)
 #include <dm/lists.h>
 #include <dm/root.h>
 #include <dm/device-internal.h>
+#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
+#include <asm/fsp/fsp_support.h>
+#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
+static int pci_get_bus(int busnum, struct udevice **busp)
+{
+       int ret;
+
+       ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
+
+       /* Since buses may not be numbered yet try a little harder with bus 0 */
+       if (ret == -ENODEV) {
+               ret = uclass_first_device(UCLASS_PCI, busp);
+               if (ret)
+                       return ret;
+               else if (!*busp)
+                       return -ENODEV;
+               ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
+       }
+
+       return ret;
+}
+
 struct pci_controller *pci_bus_to_hose(int busnum)
 {
        struct udevice *bus;
        int ret;
 
-       ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, &bus);
+       ret = pci_get_bus(busnum, &bus);
        if (ret) {
                debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret);
                return NULL;
        }
+
        return dev_get_uclass_priv(bus);
 }
 
@@ -125,7 +148,7 @@ int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
        struct udevice *bus;
        int ret;
 
-       ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
+       ret = pci_get_bus(PCI_BUS(bdf), &bus);
        if (ret)
                return ret;
        return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
@@ -203,14 +226,24 @@ int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
        struct udevice *bus;
        int ret;
 
-       ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
+       ret = pci_get_bus(PCI_BUS(bdf), &bus);
        if (ret)
                return ret;
 
-       return pci_bus_write_config(bus, PCI_MASK_BUS(bdf), offset, value,
-                                   size);
+       return pci_bus_write_config(bus, bdf, offset, value, size);
+}
+
+int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
+                       enum pci_size_t size)
+{
+       struct udevice *bus;
+
+       for (bus = dev; device_is_on_pci_bus(bus);)
+               bus = bus->parent;
+       return pci_bus_write_config(bus, pci_get_bdf(dev), offset, value, size);
 }
 
+
 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
 {
        return pci_write_config(bdf, offset, value, PCI_SIZE_32);
@@ -226,6 +259,21 @@ int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
        return pci_write_config(bdf, offset, value, PCI_SIZE_8);
 }
 
+int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
+{
+       return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
+}
+
+int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
+{
+       return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
+}
+
+int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
+{
+       return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
+}
+
 int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
                        unsigned long *valuep, enum pci_size_t size)
 {
@@ -243,11 +291,21 @@ int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
        struct udevice *bus;
        int ret;
 
-       ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
+       ret = pci_get_bus(PCI_BUS(bdf), &bus);
        if (ret)
                return ret;
 
-       return pci_bus_read_config(bus, PCI_MASK_BUS(bdf), offset, valuep,
+       return pci_bus_read_config(bus, bdf, offset, valuep, size);
+}
+
+int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
+                      enum pci_size_t size)
+{
+       struct udevice *bus;
+
+       for (bus = dev; device_is_on_pci_bus(bus);)
+               bus = bus->parent;
+       return pci_bus_read_config(bus, pci_get_bdf(dev), offset, valuep,
                                   size);
 }
 
@@ -290,6 +348,45 @@ int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
        return 0;
 }
 
+int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
+{
+       unsigned long value;
+       int ret;
+
+       ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
+       if (ret)
+               return ret;
+       *valuep = value;
+
+       return 0;
+}
+
+int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
+{
+       unsigned long value;
+       int ret;
+
+       ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
+       if (ret)
+               return ret;
+       *valuep = value;
+
+       return 0;
+}
+
+int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
+{
+       unsigned long value;
+       int ret;
+
+       ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
+       if (ret)
+               return ret;
+       *valuep = value;
+
+       return 0;
+}
+
 int pci_auto_config_devices(struct udevice *bus)
 {
        struct pci_controller *hose = bus->uclass_priv;
@@ -303,14 +400,10 @@ int pci_auto_config_devices(struct udevice *bus)
        for (ret = device_find_first_child(bus, &dev);
             !ret && dev;
             ret = device_find_next_child(&dev)) {
-               struct pci_controller *ctlr_hose;
                unsigned int max_bus;
 
                debug("%s: device %s\n", __func__, dev->name);
-
-               /* The root controller has the region information */
-               ctlr_hose = hose->ctlr->uclass_priv;
-               max_bus = pciauto_config_device(ctlr_hose, pci_get_bdf(dev));
+               max_bus = pciauto_config_device(hose, pci_get_bdf(dev));
                sub_bus = max(sub_bus, max_bus);
        }
        debug("%s: done\n", __func__);
@@ -328,7 +421,7 @@ int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
        parent = hose->bus;
 
        /* Find the bus within the parent */
-       ret = pci_bus_find_devfn(parent, bdf, &bus);
+       ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
        if (ret) {
                debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
                      bdf, parent->name, ret);
@@ -383,7 +476,7 @@ static bool pci_match_one_id(const struct pci_device_id *id,
  * This only looks at certain fields in the descriptor.
  */
 static int pci_find_and_bind_driver(struct udevice *parent,
-                                   struct pci_device_id *find_id, int devfn,
+                                   struct pci_device_id *find_id, pci_dev_t bdf,
                                    struct udevice **devp)
 {
        struct pci_driver_entry *start, *entry;
@@ -391,6 +484,7 @@ static int pci_find_and_bind_driver(struct udevice *parent,
        int n_ents;
        int ret;
        char name[30], *str;
+       bool bridge;
 
        *devp = NULL;
 
@@ -410,6 +504,17 @@ static int pci_find_and_bind_driver(struct udevice *parent,
                                continue;
 
                        drv = entry->driver;
+
+                       /*
+                        * In the pre-relocation phase, we only bind devices
+                        * whose driver has the DM_FLAG_PRE_RELOC set, to save
+                        * precious memory space as on some platforms as that
+                        * space is pretty limited (ie: using Cache As RAM).
+                        */
+                       if (!(gd->flags & GD_FLG_RELOC) &&
+                           !(drv->flags & DM_FLAG_PRE_RELOC))
+                               return 0;
+
                        /*
                         * We could pass the descriptor to the driver as
                         * platdata (instead of NULL) and allow its bind()
@@ -429,14 +534,23 @@ static int pci_find_and_bind_driver(struct udevice *parent,
                }
        }
 
+       bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
+       /*
+        * In the pre-relocation phase, we only bind bridge devices to save
+        * precious memory space as on some platforms as that space is pretty
+        * limited (ie: using Cache As RAM).
+        */
+       if (!(gd->flags & GD_FLG_RELOC) && !bridge)
+               return 0;
+
        /* Bind a generic driver so that the device can be used */
-       sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(devfn),
-               PCI_FUNC(devfn));
+       sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
+               PCI_FUNC(bdf));
        str = strdup(name);
        if (!str)
                return -ENOMEM;
-       drv = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI ? "pci_bridge_drv" :
-                       "pci_generic_drv";
+       drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
+
        ret = device_bind_driver(parent, drv, str, devp);
        if (ret) {
                debug("%s: Failed to bind generic driver: %d", __func__, ret);
@@ -455,42 +569,44 @@ int pci_bind_bus_devices(struct udevice *bus)
 {
        ulong vendor, device;
        ulong header_type;
-       pci_dev_t devfn, end;
+       pci_dev_t bdf, end;
        bool found_multi;
        int ret;
 
        found_multi = false;
-       end = PCI_DEVFN(PCI_MAX_PCI_DEVICES - 1, PCI_MAX_PCI_FUNCTIONS - 1);
-       for (devfn = PCI_DEVFN(0, 0); devfn < end; devfn += PCI_DEVFN(0, 1)) {
+       end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
+                     PCI_MAX_PCI_FUNCTIONS - 1);
+       for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
+            bdf += PCI_BDF(0, 0, 1)) {
                struct pci_child_platdata *pplat;
                struct udevice *dev;
                ulong class;
 
-               if (PCI_FUNC(devfn) && !found_multi)
+               if (PCI_FUNC(bdf) && !found_multi)
                        continue;
                /* Check only the first access, we don't expect problems */
-               ret = pci_bus_read_config(bus, devfn, PCI_HEADER_TYPE,
+               ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
                                          &header_type, PCI_SIZE_8);
                if (ret)
                        goto error;
-               pci_bus_read_config(bus, devfn, PCI_VENDOR_ID, &vendor,
+               pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
                                    PCI_SIZE_16);
                if (vendor == 0xffff || vendor == 0x0000)
                        continue;
 
-               if (!PCI_FUNC(devfn))
+               if (!PCI_FUNC(bdf))
                        found_multi = header_type & 0x80;
 
                debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
-                     bus->seq, bus->name, PCI_DEV(devfn), PCI_FUNC(devfn));
-               pci_bus_read_config(bus, devfn, PCI_DEVICE_ID, &device,
+                     bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
+               pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
                                    PCI_SIZE_16);
-               pci_bus_read_config(bus, devfn, PCI_CLASS_REVISION, &class,
+               pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
                                    PCI_SIZE_32);
                class >>= 8;
 
                /* Find this device in the device tree */
-               ret = pci_bus_find_devfn(bus, devfn, &dev);
+               ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
 
                /* Search for a driver */
 
@@ -504,24 +620,26 @@ int pci_bind_bus_devices(struct udevice *bus)
                        find_id.device = device;
                        find_id.class = class;
                        if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
-                               pci_bus_read_config(bus, devfn,
+                               pci_bus_read_config(bus, bdf,
                                                    PCI_SUBSYSTEM_VENDOR_ID,
                                                    &val, PCI_SIZE_32);
                                find_id.subvendor = val & 0xffff;
                                find_id.subdevice = val >> 16;
                        }
-                       ret = pci_find_and_bind_driver(bus, &find_id, devfn,
+                       ret = pci_find_and_bind_driver(bus, &find_id, bdf,
                                                       &dev);
                }
                if (ret)
                        return ret;
 
                /* Update the platform data */
-               pplat = dev_get_parent_platdata(dev);
-               pplat->devfn = devfn;
-               pplat->vendor = vendor;
-               pplat->device = device;
-               pplat->class = class;
+               if (dev) {
+                       pplat = dev_get_parent_platdata(dev);
+                       pplat->devfn = PCI_MASK_BUS(bdf);
+                       pplat->vendor = vendor;
+                       pplat->device = device;
+                       pplat->class = class;
+               }
        }
 
        return 0;
@@ -533,6 +651,13 @@ error:
 
 static int pci_uclass_post_bind(struct udevice *bus)
 {
+       /*
+        * If there is no pci device listed in the device tree,
+        * don't bother scanning the device tree.
+        */
+       if (bus->of_offset == -1)
+               return 0;
+
        /*
         * Scan the device tree for devices. This does not probe the PCI bus,
         * as this is not permitted while binding. It just finds devices
@@ -645,10 +770,6 @@ static int pci_uclass_post_probe(struct udevice *bus)
 {
        int ret;
 
-       /* Don't scan buses before relocation */
-       if (!(gd->flags & GD_FLG_RELOC))
-               return 0;
-
        debug("%s: probing bus %d\n", __func__, bus->seq);
        ret = pci_bind_bus_devices(bus);
        if (ret)
@@ -658,6 +779,24 @@ static int pci_uclass_post_probe(struct udevice *bus)
        ret = pci_auto_config_devices(bus);
 #endif
 
+#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
+       /*
+        * Per Intel FSP specification, we should call FSP notify API to
+        * inform FSP that PCI enumeration has been done so that FSP will
+        * do any necessary initialization as required by the chipset's
+        * BIOS Writer's Guide (BWG).
+        *
+        * Unfortunately we have to put this call here as with driver model,
+        * the enumeration is all done on a lazy basis as needed, so until
+        * something is touched on PCI it won't happen.
+        *
+        * Note we only call this 1) after U-Boot is relocated, and 2)
+        * root bus has finished probing.
+        */
+       if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0))
+               ret = fsp_init_phase_pci();
+#endif
+
        return ret < 0 ? ret : 0;
 }
 
@@ -682,31 +821,91 @@ static int pci_uclass_child_post_bind(struct udevice *dev)
                if (ret != -ENOENT)
                        return -EINVAL;
        } else {
-               /* extract the bdf from fdt_pci_addr */
-               pplat->devfn = addr.phys_hi & 0xffff00;
+               /* extract the devfn from fdt_pci_addr */
+               pplat->devfn = addr.phys_hi & 0xff00;
        }
 
        return 0;
 }
 
-int pci_bridge_read_config(struct udevice *bus, pci_dev_t devfn, uint offset,
-                          ulong *valuep, enum pci_size_t size)
+static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
+                                 uint offset, ulong *valuep,
+                                 enum pci_size_t size)
 {
        struct pci_controller *hose = bus->uclass_priv;
-       pci_dev_t bdf = PCI_ADD_BUS(bus->seq, devfn);
 
        return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
 }
 
-int pci_bridge_write_config(struct udevice *bus, pci_dev_t devfn, uint offset,
-                           ulong value, enum pci_size_t size)
+static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
+                                  uint offset, ulong value,
+                                  enum pci_size_t size)
 {
        struct pci_controller *hose = bus->uclass_priv;
-       pci_dev_t bdf = PCI_ADD_BUS(bus->seq, devfn);
 
        return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
 }
 
+static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
+{
+       struct udevice *dev;
+       int ret = 0;
+
+       /*
+        * Scan through all the PCI controllers. On x86 there will only be one
+        * but that is not necessarily true on other hardware.
+        */
+       do {
+               device_find_first_child(bus, &dev);
+               if (dev) {
+                       *devp = dev;
+                       return 0;
+               }
+               ret = uclass_next_device(&bus);
+               if (ret)
+                       return ret;
+       } while (bus);
+
+       return 0;
+}
+
+int pci_find_next_device(struct udevice **devp)
+{
+       struct udevice *child = *devp;
+       struct udevice *bus = child->parent;
+       int ret;
+
+       /* First try all the siblings */
+       *devp = NULL;
+       while (child) {
+               device_find_next_child(&child);
+               if (child) {
+                       *devp = child;
+                       return 0;
+               }
+       }
+
+       /* We ran out of siblings. Try the next bus */
+       ret = uclass_next_device(&bus);
+       if (ret)
+               return ret;
+
+       return bus ? skip_to_next_device(bus, devp) : 0;
+}
+
+int pci_find_first_device(struct udevice **devp)
+{
+       struct udevice *bus;
+       int ret;
+
+       *devp = NULL;
+       ret = uclass_first_device(UCLASS_PCI, &bus);
+       if (ret)
+               return ret;
+
+       return skip_to_next_device(bus, devp);
+}
+
 UCLASS_DRIVER(pci) = {
        .id             = UCLASS_PCI,
        .name           = "pci",