diff mbox series

[2/3] pci: introduce a function to get PCIDevice

Message ID 20241207105537.542441-3-julia.zhang@amd.com (mailing list archive)
State New
Headers show
Series Support getting p2pdma_distance | expand

Commit Message

Julia Zhang Dec. 7, 2024, 10:55 a.m. UTC
Introduce a helper function to get PCIDevice from qdev pci notation.

Signed-off-by: Julia Zhang <julia.zhang@amd.com>
---
 hw/pci/pci.c         | 22 ++++++++++++++++++++++
 include/hw/pci/pci.h |  2 ++
 2 files changed, 24 insertions(+)
diff mbox series

Patch

diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 1416ae202c..95806ead4f 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -2617,6 +2617,28 @@  static int pci_qdev_find_recursive(PCIBus *bus,
     return -EINVAL;
 }
 
+int pci_qdev_get_device(uint32_t virt_bus, uint32_t virt_slot, uint32_t virt_func,
+			PCIDevice **pci_dev)
+{
+    PCIHostState *host_bridge;
+    PCIDevice *d;
+    int devfn;
+    int rc = -ENODEV;
+
+    QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
+        for(devfn = 0; devfn < ARRAY_SIZE(host_bridge->bus->devices); devfn++) {
+	    d = host_bridge->bus->devices[devfn];
+	    if (d && d->devfn == PCI_DEVFN(virt_slot, virt_func) &&
+		pci_bus_num(pci_get_bus(d)) == virt_bus) {
+		*pci_dev = d;
+		rc = 0;
+		break;
+	    }
+	}
+    }
+    return rc;
+}
+
 int pci_qdev_find_device(const char *id, PCIDevice **pdev)
 {
     PCIHostState *host_bridge;
diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
index 463d9984b3..1b493ab95e 100644
--- a/include/hw/pci/pci.h
+++ b/include/hw/pci/pci.h
@@ -366,6 +366,8 @@  const char *pci_root_bus_path(PCIDevice *dev);
 bool pci_bus_bypass_iommu(PCIBus *bus);
 PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn);
 int pci_qdev_find_device(const char *id, PCIDevice **pdev);
+int pci_qdev_get_device(uint32_t virt_bus, uint32_t virt_slot,
+			uint32_t virt_func, PCIDevice **pci_dev);
 void pci_bus_get_w64_range(PCIBus *bus, Range *range);
 
 void pci_device_deassert_intx(PCIDevice *dev);