Skip to content

Commit d01fa56

Browse files
Johan Hedbergnashif
authored andcommitted
drivers: pcie: Introduce API to look up devices by ID
In some cases we cannot know the BDF up-front, so provide a way to look it up based on the vendor and device ID. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 14da601 commit d01fa56

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

drivers/pcie/host/pcie.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,28 @@ void pcie_irq_enable(pcie_bdf_t bdf, unsigned int irq)
229229
#endif
230230
irq_enable(irq);
231231
}
232+
233+
pcie_bdf_t pcie_bdf_lookup(pcie_id_t id)
234+
{
235+
int bus, dev, func;
236+
237+
for (bus = 0; bus <= PCIE_MAX_BUS; bus++) {
238+
for (dev = 0; dev <= PCIE_MAX_DEV; dev++) {
239+
for (func = 0; func <= PCIE_MAX_FUNC; func++) {
240+
pcie_bdf_t bdf = PCIE_BDF(bus, dev, func);
241+
uint32_t data;
242+
243+
data = pcie_conf_read(bdf, PCIE_CONF_ID);
244+
if (data == PCIE_ID_NONE) {
245+
continue;
246+
}
247+
248+
if (data == id) {
249+
return bdf;
250+
}
251+
}
252+
}
253+
}
254+
255+
return PCIE_BDF_NONE;
256+
}

include/drivers/pcie/pcie.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ struct pcie_mbar {
4545
* These functions are arch-, board-, or SoC-specific.
4646
*/
4747

48+
/**
49+
* @brief Look up the BDF based on PCI(e) vendor & device ID
50+
*
51+
* This function is used to look up the BDF for a device given its
52+
* vendor and device ID.
53+
*
54+
* @param id PCI(e) vendor & device ID encoded using PCIE_ID()
55+
* @return The BDF for the device, or PCIE_BDF_NONE if it was not found
56+
*/
57+
extern pcie_bdf_t pcie_bdf_lookup(pcie_id_t id);
58+
4859
/**
4960
* @brief Read a 32-bit word from an endpoint's configuration space.
5061
*

include/dt-bindings/pcie/pcie.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
#define PCIE_ID_NONE PCIE_ID(0xFFFF, 0xFFFF)
3737

38+
#define PCIE_BDF_NONE 0xFFFFFFFFU
39+
3840
/*
3941
* Since our internal representation of bus/device/function is arbitrary,
4042
* we choose the same format employed in the x86 Configuration Address Port:

0 commit comments

Comments
 (0)