diff mbox

[U-Boot,v3,1/4] dm: pci: Allow a PCI bus to be found without an alias

Message ID 1441068938-16305-2-git-send-email-sjg@chromium.org
State Accepted
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass Sept. 1, 2015, 12:55 a.m. UTC
At present, until a PCI bus is probed, it cannot be found by its sequence
number unless it has an alias. This is the same with any device.

However with PCI this is more annoying than usual, since bus 0 is always the
same device.

Add a function that tries a little harder to locate PCI bus 0. This means
that PCI enumeration will happen automatically on the first access.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v3:
- Correct failure logic in pci_get_bus()
- Use pci_get_bus in pci_bus_to_hose()

Changes in v2:
- Adjust pci_get_bus() to probe bus 0 so that the required bus is found

 drivers/pci/pci-uclass.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

Comments

Bin Meng Sept. 1, 2015, 6:25 a.m. UTC | #1
On Tue, Sep 1, 2015 at 8:55 AM, Simon Glass <sjg@chromium.org> wrote:
> At present, until a PCI bus is probed, it cannot be found by its sequence
> number unless it has an alias. This is the same with any device.
>
> However with PCI this is more annoying than usual, since bus 0 is always the
> same device.
>
> Add a function that tries a little harder to locate PCI bus 0. This means
> that PCI enumeration will happen automatically on the first access.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> Changes in v3:
> - Correct failure logic in pci_get_bus()
> - Use pci_get_bus in pci_bus_to_hose()
>
> Changes in v2:
> - Adjust pci_get_bus() to probe bus 0 so that the required bus is found
>
>  drivers/pci/pci-uclass.c | 28 ++++++++++++++++++++++++----
>  1 file changed, 24 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/pci/pci-uclass.c b/drivers/pci/pci-uclass.c
> index b25298f..ea70853 100644
> --- a/drivers/pci/pci-uclass.c
> +++ b/drivers/pci/pci-uclass.c
> @@ -20,16 +20,36 @@
>
>  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);
>  }
>
> @@ -128,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);
> @@ -206,7 +226,7 @@ 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;
>
> @@ -271,7 +291,7 @@ 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;
>
> --

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Simon Glass Sept. 2, 2015, 2:48 a.m. UTC | #2
On 1 September 2015 at 00:25, Bin Meng <bmeng.cn@gmail.com> wrote:
> On Tue, Sep 1, 2015 at 8:55 AM, Simon Glass <sjg@chromium.org> wrote:
>> At present, until a PCI bus is probed, it cannot be found by its sequence
>> number unless it has an alias. This is the same with any device.
>>
>> However with PCI this is more annoying than usual, since bus 0 is always the
>> same device.
>>
>> Add a function that tries a little harder to locate PCI bus 0. This means
>> that PCI enumeration will happen automatically on the first access.
>>
>> Signed-off-by: Simon Glass <sjg@chromium.org>
>> ---
>>
>> Changes in v3:
>> - Correct failure logic in pci_get_bus()
>> - Use pci_get_bus in pci_bus_to_hose()
>>
>> Changes in v2:
>> - Adjust pci_get_bus() to probe bus 0 so that the required bus is found
>>
>>  drivers/pci/pci-uclass.c | 28 ++++++++++++++++++++++++----
>>  1 file changed, 24 insertions(+), 4 deletions(-)
>>
[snip]

> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

Applied to u-boot-x86.
diff mbox

Patch

diff --git a/drivers/pci/pci-uclass.c b/drivers/pci/pci-uclass.c
index b25298f..ea70853 100644
--- a/drivers/pci/pci-uclass.c
+++ b/drivers/pci/pci-uclass.c
@@ -20,16 +20,36 @@ 
 
 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);
 }
 
@@ -128,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);
@@ -206,7 +226,7 @@  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;
 
@@ -271,7 +291,7 @@  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;