On Fri, 23 Feb 2024 12:42:23 +0000 <ank...@nvidia.com> wrote: > From: Ankit Agrawal <ank...@nvidia.com> > > ACPI spec provides a scheme to associate "Generic Initiators" [1] > (e.g. heterogeneous processors and accelerators, GPUs, and I/O devices with > integrated compute or DMA engines GPUs) with Proximity Domains. This is > achieved using Generic Initiator Affinity Structure in SRAT. During bootup, > Linux kernel parse the ACPI SRAT to determine the PXM ids and create a NUMA > node for each unique PXM ID encountered. Qemu currently do not implement > these structures while building SRAT. > > Add GI structures while building VM ACPI SRAT. The association between > device and node are stored using acpi-generic-initiator object. Lookup > presence of all such objects and use them to build these structures. > > The structure needs a PCI device handle [2] that consists of the device BDF. > The vfio-pci device corresponding to the acpi-generic-initiator object is > located to determine the BDF. > > [1] ACPI Spec 6.3, Section 5.2.16.6 > [2] ACPI Spec 6.3, Table 5.80 > > Signed-off-by: Ankit Agrawal <ank...@nvidia.com> Hi Ankit,
As the code stands the use of a list seems overkill. Otherwise looks good to me. I need Generic Ports support for CXL stuff so will copy your approach for that as it's ended up nice and simple. Jonathan > --- > hw/acpi/acpi-generic-initiator.c | 84 ++++++++++++++++++++++++ > hw/arm/virt-acpi-build.c | 3 + > include/hw/acpi/acpi-generic-initiator.h | 26 ++++++++ > 3 files changed, 113 insertions(+) > > diff --git a/hw/acpi/acpi-generic-initiator.c > b/hw/acpi/acpi-generic-initiator.c > index 1ade2f723f..d78382bc63 100644 > --- a/hw/acpi/acpi-generic-initiator.c > +++ b/hw/acpi/acpi-generic-initiator.c > @@ -68,3 +68,87 @@ static void acpi_generic_initiator_class_init(ObjectClass > *oc, void *data) > object_class_property_add(oc, "node", "int", NULL, > acpi_generic_initiator_set_node, NULL, NULL); > } > + > +static int acpi_generic_initiator_list(Object *obj, void *opaque) > +{ > + GSList **list = opaque; > + > + if (object_dynamic_cast(obj, TYPE_ACPI_GENERIC_INITIATOR)) { > + *list = g_slist_append(*list, ACPI_GENERIC_INITIATOR(obj)); > + } > + > + object_child_foreach(obj, acpi_generic_initiator_list, opaque); See below. There is a recursive helper that avoids need for this. > + return 0; > +} > + > +/* > + * Identify Generic Initiator objects and link them into the list which is > + * returned to the caller. > + * > + * Note: it is the caller's responsibility to free the list to avoid > + * memory leak. > + */ > +static GSList *acpi_generic_initiator_get_list(void) > +{ > + GSList *list = NULL; > + > + object_child_foreach(object_get_root(), > + acpi_generic_initiator_list, &list); I think you can use object_child_foreach_recursive() and skip the manual calling above? > + return list; > +} > + > +/* > + * ACPI 6.3: > + * Table 5-78 Generic Initiator Affinity Structure > + */ > +static void > +build_srat_generic_pci_initiator_affinity(GArray *table_data, int node, > + PCIDeviceHandle *handle) > +{ > + uint8_t index; > + > + build_append_int_noprefix(table_data, 5, 1); /* Type */ > + build_append_int_noprefix(table_data, 32, 1); /* Length */ > + build_append_int_noprefix(table_data, 0, 1); /* Reserved */ > + build_append_int_noprefix(table_data, 1, 1); /* Device Handle Type: PCI > */ > + build_append_int_noprefix(table_data, node, 4); /* Proximity Domain */ > + > + /* Device Handle - PCI */ > + build_append_int_noprefix(table_data, handle->segment, 2); > + build_append_int_noprefix(table_data, handle->bdf, 2); > + for (index = 0; index < 12; index++) { > + build_append_int_noprefix(table_data, 0, 1); > + } > + > + build_append_int_noprefix(table_data, GEN_AFFINITY_ENABLED, 4); /* Flags > */ > + build_append_int_noprefix(table_data, 0, 4); /* Reserved */ > +} > + > +void build_srat_generic_pci_initiator(GArray *table_data) > +{ > + GSList *gi_list, *list = acpi_generic_initiator_get_list(); Did you consider just have the functional called in the scan do this? Not sure you need anything as a parameter beyond the GArray *table_data Something like... static int acpi_generic_initiator_list(Object *obj, void *opaque) { uint8_t index; AcpiGenericInitiator *gi; GArray *table_data = opaque; PCIDeviceHandle dev_handle; PCIDevice *pci_dev; Object *o; if (!object_dynamic_cast(obj, TYPE_ACPI_GENERIC_INITIATOR)) { return 0; } gi = ACPI_GENERIC_INITIATOR(obj); o = object_resolve_path_type(gi->pci_dev, TYPE_PCI_DEVICE, NULL); if (!o) { error_setg(&error_abort, "GI: Specified device must be a PCI device.\n") return 1; } pci_dev = PCI_DEVICE(o); dev_handle.segment = 0; dev_handle.bdf = PCI_BUILD_BDF(pci_bus_num(pci_get_bus(pci_dev)), pci_dev->devfn); build_srat_generic_pci_initiator_affinity(table_data, gi->node, &dev_handle); } + a call to. object_child_foreach_recursive(object_get_root(), acpi_generic_srat, table_data); > + AcpiGenericInitiator *gi; > + > + for (gi_list = list; gi_list; gi_list = gi_list->next) { > + PCIDeviceHandle dev_handle; > + PCIDevice *pci_dev; > + Object *o; > + > + gi = gi_list->data; > + > + o = object_resolve_path_type(gi->pci_dev, TYPE_PCI_DEVICE, NULL); > + if (!o) { > + error_printf("Specified device must be a PCI device.\n"); as above, use an errp rather than exit(1); > + exit(1); > + } > + pci_dev = PCI_DEVICE(o); > + > + dev_handle.segment = 0; > + dev_handle.bdf = PCI_BUILD_BDF(pci_bus_num(pci_get_bus(pci_dev)), > + pci_dev->devfn); > + build_srat_generic_pci_initiator_affinity(table_data, > + gi->node, &dev_handle); Should we check for consistency of gi->node and -numa node,id=X entries? Maybe just check less than numa_state->num_nodes as that's the variable used to walk the other structures when building srat. > + } > + > + g_slist_free(list); > +} > diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c > index 8bc35a483c..00d77327e0 100644 > --- a/hw/arm/virt-acpi-build.c > +++ b/hw/arm/virt-acpi-build.c > @@ -58,6 +58,7 @@ > #include "migration/vmstate.h" > #include "hw/acpi/ghes.h" > #include "hw/acpi/viot.h" > +#include "hw/acpi/acpi-generic-initiator.h" > > #define ARM_SPI_BASE 32 > > @@ -558,6 +559,8 @@ build_srat(GArray *table_data, BIOSLinker *linker, > VirtMachineState *vms) > } > } > > + build_srat_generic_pci_initiator(table_data); Perhaps passing in a suitable Error ** would be sensible. > + > if (ms->nvdimms_state->is_enabled) { > nvdimm_build_srat(table_data); > } > diff --git a/include/hw/acpi/acpi-generic-initiator.h > b/include/hw/acpi/acpi-generic-initiator.h > index 2f183b029a..213545e614 100644 > --- a/include/hw/acpi/acpi-generic-initiator.h > +++ b/include/hw/acpi/acpi-generic-initiator.h > @@ -29,4 +29,30 @@ typedef struct AcpiGenericInitiatorClass { > ObjectClass parent_class; > } AcpiGenericInitiatorClass; > > +/* > + * ACPI 6.3: > + * Table 5-81 Flags – Generic Initiator Affinity Structure > + */ > +typedef enum { > + GEN_AFFINITY_ENABLED = (1 << 0), /* > + * If clear, the OSPM ignores the > contents > + * of the Generic Initiator/Port > Affinity > + * Structure. This allows system > firmware > + * to populate the SRAT with a static > + * number of structures, but only enable > + * them as necessary. > + */ I'd put the comment above the definition to avoid wrapping so much! > +} GenericAffinityFlags; > + > +/* > + * ACPI 6.3: > + * Table 5-80 Device Handle - PCI > + */ > +typedef struct PCIDeviceHandle { > + uint16_t segment; > + uint16_t bdf; > +} PCIDeviceHandle; > + > +void build_srat_generic_pci_initiator(GArray *table_data); > + > #endif