On Thu, Oct 21, 2021 at 06:42:58PM +0800, Peter Xu wrote:
> With all the prepared infrastructures, we can easily add one helper now to
> loop
> over all the existing PCI devices across the whole system.
>
> Signed-off-by: Peter Xu <[email protected]>
> ---
> hw/pci/pci.c | 25 +++++++++++++++++++++++++
> include/hw/pci/pci.h | 2 ++
> 2 files changed, 27 insertions(+)
>
> diff --git a/hw/pci/pci.c b/hw/pci/pci.c
> index 1623bc9099..5c970f0727 100644
> --- a/hw/pci/pci.c
> +++ b/hw/pci/pci.c
> @@ -2124,6 +2124,31 @@ void pci_for_each_root_bus(pci_bus_fn fn, void *opaque)
> object_child_foreach_recursive(object_get_root(), pci_find_root_bus,
> &args);
> }
>
> +typedef struct {
> + pci_bus_dev_fn fn;
> + void *opaque;
> +} pci_bus_dev_args;
code style violation. CamelCase for structs pls.
> +
> +static void pci_single_bus_hook(PCIBus *bus, void *opaque)
> +{
> + pci_bus_dev_args *args = opaque;
> +
> + pci_for_each_device_under_bus(bus, args->fn, args->opaque);
> +}
> +
> +static void pci_root_bus_hook(PCIBus *bus, void *opaque)
> +{
> + assert(pci_bus_is_root(bus));
> + pci_for_each_bus(bus, pci_single_bus_hook, opaque);
> +}
> +
> +void pci_for_each_device_all(pci_bus_dev_fn fn, void *opaque)
> +{
> + pci_bus_dev_args args = { .fn = fn, .opaque = opaque };
> +
> + pci_for_each_root_bus(pci_root_bus_hook, &args);
> +}
> +
> PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
> {
> bus = pci_find_bus_nr(bus, bus_num);
> diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
> index 9e490d8969..1a862d1903 100644
> --- a/include/hw/pci/pci.h
> +++ b/include/hw/pci/pci.h
> @@ -476,6 +476,8 @@ 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);
> +/* Call 'fn' for each pci device on the system */
> +void pci_for_each_device_all(pci_bus_dev_fn fn, void *opaque);
Instead of hacking pci making initialization o(N^2),
can't we add a variant of object_resolve_path_type ?
> PCIDevice *pci_get_function_0(PCIDevice *pci_dev);
>
> /* Use this wrapper when specific scan order is not required. */
> --
> 2.32.0