From: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
Bus:Device:Function (BDF) numbers are used to uniquely identify a
device/function on a PCI bus. Hence, add an API to get the BDF from the
devicetree node of a device.
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
---
drivers/pci/of.c | 21 +++++++++++++++++++++
include/linux/of_pci.h | 6 ++++++
2 files changed, 27 insertions(+)
diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index 3579265f119845637e163d9051437c89662762f8..5e584d25434291430145e510b1d49a371dce9165 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -183,6 +183,27 @@ int of_pci_get_devfn(struct device_node *np)
}
EXPORT_SYMBOL_GPL(of_pci_get_devfn);
+/**
+ * of_pci_get_bdf() - Get Bus:Device:Function (BDF) numbers for a device node
+ * @np: device node
+ *
+ * Parses a standard 5-cell PCI resource and returns an 16-bit value that
+ * corresponds to the BDF of the node. On error, a negative error code is
+ * returned.
+ */
+int of_pci_get_bdf(struct device_node *np)
+{
+ u32 reg[5];
+ int error;
+
+ error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
+ if (error)
+ return error;
+
+ return (reg[0] >> 8) & 0xffff;
+}
+EXPORT_SYMBOL_GPL(of_pci_get_bdf);
+
/**
* of_pci_parse_bus_range() - parse the bus-range property of a PCI device
* @node: device node
diff --git a/include/linux/of_pci.h b/include/linux/of_pci.h
index 29658c0ee71ff10122760214d04ee2bab01709fd..b36ac10653c82f4efdbb57cea56d2ec9d581212f 100644
--- a/include/linux/of_pci.h
+++ b/include/linux/of_pci.h
@@ -12,6 +12,7 @@ struct device_node;
struct device_node *of_pci_find_child_device(struct device_node *parent,
unsigned int devfn);
int of_pci_get_devfn(struct device_node *np);
+int of_pci_get_bdf(struct device_node *np);
void of_pci_check_probe_only(void);
#else
static inline struct device_node *of_pci_find_child_device(struct device_node *parent,
@@ -25,6 +26,11 @@ static inline int of_pci_get_devfn(struct device_node *np)
return -EINVAL;
}
+static inline int of_pci_get_bdf(struct device_node *np)
+{
+ return -EINVAL;
+}
+
static inline void of_pci_check_probe_only(void) { }
#endif
--
2.45.2
On Tue, Aug 19, 2025 at 12:44:53PM +0530, Manivannan Sadhasivam wrote: > Bus:Device:Function (BDF) numbers are used to uniquely identify a > device/function on a PCI bus. Hence, add an API to get the BDF from the > devicetree node of a device. For FDT, the bus should always be 0. It doesn't make sense for FDT. The bus number in DT reflects how firmware configured the PCI buses, but there's no firmware configuration of PCI for FDT.
On Fri, Aug 22, 2025 at 08:51:47AM GMT, Rob Herring wrote: > On Tue, Aug 19, 2025 at 12:44:53PM +0530, Manivannan Sadhasivam wrote: > > Bus:Device:Function (BDF) numbers are used to uniquely identify a > > device/function on a PCI bus. Hence, add an API to get the BDF from the > > devicetree node of a device. > > For FDT, the bus should always be 0. It doesn't make sense for FDT. The > bus number in DT reflects how firmware configured the PCI buses, but > there's no firmware configuration of PCI for FDT. This API is targeted for DT platforms only, where it is used to uniquely identify a devfn. What should I do to make it DT specific and not FDT? - Mani -- மணிவண்ணன் சதாசிவம்
On Fri, Aug 22, 2025 at 07:57:41PM +0530, Manivannan Sadhasivam wrote: > On Fri, Aug 22, 2025 at 08:51:47AM GMT, Rob Herring wrote: > > On Tue, Aug 19, 2025 at 12:44:53PM +0530, Manivannan Sadhasivam wrote: > > > Bus:Device:Function (BDF) numbers are used to uniquely identify a > > > device/function on a PCI bus. Hence, add an API to get the BDF from the > > > devicetree node of a device. > > > > For FDT, the bus should always be 0. It doesn't make sense for FDT. The > > bus number in DT reflects how firmware configured the PCI buses, but > > there's no firmware configuration of PCI for FDT. > > This API is targeted for DT platforms only, where it is used to uniquely > identify a devfn. What should I do to make it DT specific and not FDT? I don't understand. There are FDT and OF (actual OpenFirmware) platforms. I'm pretty sure you don't care about the latter. Rob
On Mon, Aug 25, 2025 at 05:43:15PM GMT, Rob Herring wrote: > On Fri, Aug 22, 2025 at 07:57:41PM +0530, Manivannan Sadhasivam wrote: > > On Fri, Aug 22, 2025 at 08:51:47AM GMT, Rob Herring wrote: > > > On Tue, Aug 19, 2025 at 12:44:53PM +0530, Manivannan Sadhasivam wrote: > > > > Bus:Device:Function (BDF) numbers are used to uniquely identify a > > > > device/function on a PCI bus. Hence, add an API to get the BDF from the > > > > devicetree node of a device. > > > > > > For FDT, the bus should always be 0. It doesn't make sense for FDT. The > > > bus number in DT reflects how firmware configured the PCI buses, but > > > there's no firmware configuration of PCI for FDT. > > > > This API is targeted for DT platforms only, where it is used to uniquely > > identify a devfn. What should I do to make it DT specific and not FDT? > > I don't understand. There are FDT and OF (actual OpenFirmware) > platforms. I'm pretty sure you don't care about the latter. > Sorry, I mixed the terminologies. Yes, I did refer the platforms making use of the FDT binary and not OF platforms. In the DTS, we do use bus number to differentiate between devices, not just devfn. But I get your point, bus no other than 0 are not fixed and allocated by the OS during runtime or by the firmware. So how should we uniquely identify a PCIe node here, if not by BDF? - Mani -- மணிவண்ணன் சதாசிவம்
On Tue, Aug 26, 2025 at 2:15 AM Manivannan Sadhasivam <mani@kernel.org> wrote: > > On Mon, Aug 25, 2025 at 05:43:15PM GMT, Rob Herring wrote: > > On Fri, Aug 22, 2025 at 07:57:41PM +0530, Manivannan Sadhasivam wrote: > > > On Fri, Aug 22, 2025 at 08:51:47AM GMT, Rob Herring wrote: > > > > On Tue, Aug 19, 2025 at 12:44:53PM +0530, Manivannan Sadhasivam wrote: > > > > > Bus:Device:Function (BDF) numbers are used to uniquely identify a > > > > > device/function on a PCI bus. Hence, add an API to get the BDF from the > > > > > devicetree node of a device. > > > > > > > > For FDT, the bus should always be 0. It doesn't make sense for FDT. The > > > > bus number in DT reflects how firmware configured the PCI buses, but > > > > there's no firmware configuration of PCI for FDT. > > > > > > This API is targeted for DT platforms only, where it is used to uniquely > > > identify a devfn. What should I do to make it DT specific and not FDT? > > > > I don't understand. There are FDT and OF (actual OpenFirmware) > > platforms. I'm pretty sure you don't care about the latter. > > > > Sorry, I mixed the terminologies. Yes, I did refer the platforms making use of > the FDT binary and not OF platforms. > > In the DTS, we do use bus number to differentiate between devices, not just > devfn. But I get your point, bus no other than 0 are not fixed and allocated by > the OS during runtime or by the firmware. > > So how should we uniquely identify a PCIe node here, if not by BDF? By path. Which is consistent since there is also no bus num in the unit-address. Rob
On Tue, Aug 26, 2025 at 08:12:53AM GMT, Rob Herring wrote: > On Tue, Aug 26, 2025 at 2:15 AM Manivannan Sadhasivam <mani@kernel.org> wrote: > > > > On Mon, Aug 25, 2025 at 05:43:15PM GMT, Rob Herring wrote: > > > On Fri, Aug 22, 2025 at 07:57:41PM +0530, Manivannan Sadhasivam wrote: > > > > On Fri, Aug 22, 2025 at 08:51:47AM GMT, Rob Herring wrote: > > > > > On Tue, Aug 19, 2025 at 12:44:53PM +0530, Manivannan Sadhasivam wrote: > > > > > > Bus:Device:Function (BDF) numbers are used to uniquely identify a > > > > > > device/function on a PCI bus. Hence, add an API to get the BDF from the > > > > > > devicetree node of a device. > > > > > > > > > > For FDT, the bus should always be 0. It doesn't make sense for FDT. The > > > > > bus number in DT reflects how firmware configured the PCI buses, but > > > > > there's no firmware configuration of PCI for FDT. > > > > > > > > This API is targeted for DT platforms only, where it is used to uniquely > > > > identify a devfn. What should I do to make it DT specific and not FDT? > > > > > > I don't understand. There are FDT and OF (actual OpenFirmware) > > > platforms. I'm pretty sure you don't care about the latter. > > > > > > > Sorry, I mixed the terminologies. Yes, I did refer the platforms making use of > > the FDT binary and not OF platforms. > > > > In the DTS, we do use bus number to differentiate between devices, not just > > devfn. But I get your point, bus no other than 0 are not fixed and allocated by > > the OS during runtime or by the firmware. > > > > So how should we uniquely identify a PCIe node here, if not by BDF? > > By path. Which is consistent since there is also no bus num in the unit-address. > But there is no straightforward way to know the full path, isn't it? Anyway, for simplicity, I'll just use the node pointer itself to identify the node. - Mani -- மணிவண்ணன் சதாசிவம்
© 2016 - 2025 Red Hat, Inc.