Add a helper to loop over each root bus of the system, either the default root bus or extended buses like pxb-pcie.
Signed-off-by: Peter Xu <pet...@redhat.com> --- hw/pci/pci.c | 26 ++++++++++++++++++++++++++ include/hw/pci/pci.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 4a84e478ce..1623bc9099 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2097,6 +2097,32 @@ void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin, } } +typedef struct { + pci_bus_fn fn; + void *opaque; +} pci_root_bus_args; + +static int pci_find_root_bus(Object *obj, void *opaque) +{ + pci_root_bus_args *args = opaque; + + if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) { + PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus; + + if (bus) { + args->fn(bus, args->opaque); + } + } + + return 0; +} + +void pci_for_each_root_bus(pci_bus_fn fn, void *opaque) +{ + pci_root_bus_args args = { .fn = fn, .opaque = opaque }; + + object_child_foreach_recursive(object_get_root(), pci_find_root_bus, &args); +} PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn) { diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index a7e81f04d3..9e490d8969 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -474,6 +474,8 @@ void pci_for_each_device_under_bus_reverse(PCIBus *bus, void *opaque); void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin, pci_bus_fn end, void *parent_state); +/* Call `fn' for each pci root bus on the system */ +void pci_for_each_root_bus(pci_bus_fn fn, void *opaque); PCIDevice *pci_get_function_0(PCIDevice *pci_dev); /* Use this wrapper when specific scan order is not required. */ -- 2.32.0