vfio/nvgpu: Add vfio pci variant module for grace hopper
v3 cover vfio/nvgpu: Add vfio pci variant module for grace hopper
From: Ankit Agrawal <[email protected]>
There are upcoming devices which allow CPU to cache coherently access their memory. It is sensible to expose such memory as NUMA nodes separate from the sysmem node to the OS. The ACPI spec provides a scheme in SRAT called Generic Initiator Affinity Structure [1] to allow an association between a Proximity Domain (PXM) and a Generic Initiator (GI) (e.g. heterogeneous processors and accelerators, GPUs, and I/O devices with integrated compute or DMA engines).
While a single node per device may cover several use cases, it is however insufficient for a full utilization of the NVIDIA GPUs MIG (Mult-Instance GPUs) [2] feature. The feature allows partitioning of the GPU device resources (including device memory) into several (upto 8) isolated instances. Each of the partitioned memory requires a dedicated NUMA node to operate. The partitions are not fixed and they can be created/deleted at runtime.
Linux OS does not provide a means to dynamically create/destroy NUMA nodes and such feature implementation is expected to be non-trivial. The nodes that OS discovers at the boot time while parsing SRAT remains fixed. So we utilize the GI Affinity structures that allows association between nodes and devices. Multiple GI structures per device/BDF is possible, allowing creation of multiple nodes in the VM by exposing unique PXM in each of these structures.
Implement the mechanism to build the GI affinity structures as Qemu currently does not. Introduce a new acpi-generic-initiator object that allows an association of a set of nodes with a device. During SRAT creation, all such objected are identified and used to add the GI Affinity Structures.
The admin will create a range of 8 nodes and associate that with the device using the acpi-generic-initiator object. While a configuration of less than 8 nodes per device is allowed, such configuration will prevent utilization of the feature to the fullest. This setting is applicable to all the Grace+Hopper systems. The following is an example of the Qemu command line arguments to create 8 nodes and link them to the device 'dev0':
-numa node,nodeid=2 \
-numa node,nodeid=3 \
-numa node,nodeid=4 \
-numa node,nodeid=5 \
-numa node,nodeid=6 \
-numa node,nodeid=7 \
-numa node,nodeid=8 \
-numa node,nodeid=9 \
-device vfio-pci-nohotplug,host=0009:01:00.0,bus=pcie.0,addr=04.0,rombar=0,id=dev0 \
-object acpi-generic-initiator,id=gi0,device=dev0,numalist=2:3:4:5:6:7:8:9 \
The performance benefits can be realized by providing the NUMA node distances appropriately (through libvirt tags or Qemu params). The admin can get the distance among nodes in hardware using numactl -H.
This series goes along with the vfio-pci variant driver [3] under review.
Applied over v8.1.2.
[1] ACPI Spec 6.5, Section 5.2.16.6
[2] https://www.nvidia.com/en-in/technologies/multi-instance-gpu
[3] https://lore.kernel.org/all/[email protected]/
Link for v2:
https://lore.kernel.org/all/[email protected]/
v2 -> v3
- changed param to accept a ':' delimited list of numa nodes, instead of a range.
- Removed nvidia-acpi-generic-initiator object.
- Addressed miscellaneous comments in v2.
v1 -> v2
- Removed dependency on sysfs to communicate the feature with variant module.
- Use GI Affinity SRAT structure instead of Memory Affinity.
- No DSDT entries needed to communicate the PXM for the device. SRAT GI structure is used instead.
- New objects introduced to establish link between device and nodes.
Ankit Agrawal (2):
qom: new object to associate device to numa node hw/acpi: Implement the SRAT GI affinity structure
hw/acpi/acpi-generic-initiator.c | 159 +++++++++++++++++++++++ hw/acpi/meson.build | 1 + hw/arm/virt-acpi-build.c | 3 + include/hw/acpi/acpi-generic-initiator.h | 50 +++++++ qapi/qom.json | 16 +++ 5 files changed, 229 insertions(+) create mode 100644 hw/acpi/acpi-generic-initiator.c create mode 100644 include/hw/acpi/acpi-generic-initiator.h
Signature · 1 line
2.17.1
PATCH v3 1/2 qom: new object to associate device to numa node
From: Ankit Agrawal <[email protected]>
NVIDIA GPU's support MIG (Mult-Instance GPUs) feature [1], which allows partitioning of the GPU device resources (including device memory) into several (upto 8) isolated instances. Each of the partitioned memory needs a dedicated NUMA node to operate. The partitions are not fixed and they can be created/deleted at runtime.
Unfortunately Linux OS does not provide a means to dynamically create/destroy NUMA nodes and such feature implementation is not expected to be trivial. The nodes that OS discovers at the boot time while parsing SRAT remains fixed. So we utilize the Generic Initiator Affinity structures that allows association between nodes and devices. Multiple GI structures per BDF is possible, allowing creation of multiple nodes by exposing unique PXM in each of these structures.
Introduce a new acpi-generic-initiator object to allow host admin provide the device and the corresponding NUMA nodes. Qemu maintain this association and use this object to build the requisite GI Affinity Structure.
An admin can provide the range of nodes using a ':' delimited numalist and link it to a device by providing its id. The node ids are extracted from numalist and stores as a uint16List. The following sample creates 8 nodes and link them to the device dev0:
-numa node,nodeid=2 \
-numa node,nodeid=3 \
-numa node,nodeid=4 \
-numa node,nodeid=5 \
-numa node,nodeid=6 \
-numa node,nodeid=7 \
-numa node,nodeid=8 \
-numa node,nodeid=9 \
-device vfio-pci-nohotplug,host=0009:01:00.0,bus=pcie.0,addr=04.0,rombar=0,id=dev0 \
-object acpi-generic-initiator,id=gi0,device=dev0,numalist=2:3:4:5:6:7:8:9 \
[1] https://www.nvidia.com/en-in/technologies/multi-instance-gpu
--- hw/acpi/acpi-generic-initiator.c | 80 ++++++++++++++++++++++++ hw/acpi/meson.build | 1 + include/hw/acpi/acpi-generic-initiator.h | 29 +++++++++ qapi/qom.json | 16 +++++ 4 files changed, 126 insertions(+) create mode 100644 hw/acpi/acpi-generic-initiator.c create mode 100644 include/hw/acpi/acpi-generic-initiator.h
hw/acpi/acpi-generic-initiator.c new file +80 -0
@@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved +*/ + +#include "qemu/osdep.h" +#include "hw/qdev-properties.h" +#include "qapi/error.h" +#include "qapi/visitor.h" +#include "qom/object_interfaces.h" +#include "qom/object.h" +#include "hw/qdev-core.h" +#include "hw/vfio/vfio-common.h" +#include "hw/vfio/pci.h" +#include "hw/pci/pci_device.h" +#include "sysemu/numa.h" +#include "hw/acpi/acpi-generic-initiator.h" + +OBJECT_DEFINE_TYPE_WITH_INTERFACES (AcpiGenericInitiator, acpi_generic_initiator, +ACPI_GENERIC_INITIATOR ,OBJECT , + { TYPE_USER_CREATABLE }, + { NULL }) + +OBJECT_DECLARE_SIMPLE_TYPE(AcpiGenericInitiator, ACPI_GENERIC_INITIATOR) + +static void acpi_generic_initiator_init (Object *obj) +{ +AcpiGenericInitiator *gi =ACPI_GENERIC_INITIATOR (obj); + gi->device =NULL ; + gi->nodelist =NULL ; +} + +static void acpi_generic_initiator_finalize (Object *obj) +{ +AcpiGenericInitiator *gi =ACPI_GENERIC_INITIATOR (obj); + +g_free (gi->device); +qapi_free_uint16List (gi->nodelist); +} + +static void acpi_generic_initiator_set_device (Object *obj,const char *val, +Error **errp) +{ +AcpiGenericInitiator *gi =ACPI_GENERIC_INITIATOR (obj); + + gi->device =g_strdup (val); +} + +static void acpi_generic_initiator_set_nodelist (Object *obj,const char *val, +Error **errp) +{ +AcpiGenericInitiator *gi =ACPI_GENERIC_INITIATOR (obj); +char *value =g_strdup (val); +uint16_t node; +uint16List **tail = &(gi->nodelist); +char *nodestr = value ?strtok (value,":" ) :NULL ; + +while (nodestr) { +if (sscanf (nodestr,"%hu" , &node) !=1 ) { +error_setg (errp,"failed to read node-id" ); +return ; + } + +if (node >=MAX_NODES ) { +error_setg (errp,"invalid node-id" ); +return ; + } + +QAPI_LIST_APPEND (tail, node); + nodestr =strtok (NULL ,":" ); + } +} + +static void acpi_generic_initiator_class_init (ObjectClass *oc,void *data) +{ +object_class_property_add_str (oc,ACPI_GENERIC_INITIATOR_DEVICE_PROP ,NULL , + acpi_generic_initiator_set_device); +object_class_property_add_str (oc,ACPI_GENERIC_INITIATOR_NODELIST_PROP , +NULL , acpi_generic_initiator_set_nodelist); +}
hw/acpi/meson.build +1 -0
@@ -1,5 +1,6 @@ acpi_ss = ss.source_set() acpi_ss.add(files( + 'acpi-generic-initiator.c', 'acpi_interface.c', 'aml-build.c', 'bios-linker-loader.c',
include/hw/acpi/acpi-generic-initiator.h new file +29 -0
@@ -0,0 +1,29 @@ +#ifndef ACPI_GENERIC_INITIATOR_H +#define ACPI_GENERIC_INITIATOR_H + +#include "hw/mem/pc-dimm.h" +#include "hw/acpi/bios-linker-loader.h" +#include "qemu/uuid.h" +#include "hw/acpi/aml-build.h" +#include "qom/object.h" +#include "qom/object_interfaces.h" + +#define TYPE_ACPI_GENERIC_INITIATOR "acpi-generic-initiator" + +#define ACPI_GENERIC_INITIATOR_DEVICE_PROP "device" +#define ACPI_GENERIC_INITIATOR_NODELIST_PROP "nodelist" + +typedef struct AcpiGenericInitiator { +/* private */ +Object parent; + +/* public */ +char *device; +uint16List *nodelist; +}AcpiGenericInitiator ; + +typedef struct AcpiGenericInitiatorClass { +ObjectClass parent_class; +}AcpiGenericInitiatorClass ; + +#endif
qapi/qom.json +16 -0
@@ -779,6 +779,20 @@ { 'struct': 'VfioUserServerProperties', 'data': { 'socket': 'SocketAddress', 'device': 'str' } } +## +# @AcpiGenericInitiatorProperties: +# +# Properties for acpi-generic-initiator objects. +# +# @device: the ID of the device to be associated with the node +# +# @nodelist: delimited numa node list +# +# Since: 8.2 +## +{ 'struct': 'AcpiGenericInitiatorProperties', + 'data': { 'device': 'str', 'nodelist': 'str' } } + ## # @RngProperties: #
@@ -896,6 +910,7 @@ ## { 'enum': 'ObjectType', 'data': [ + 'acpi-generic-initiator', 'authz-list', 'authz-listfile', 'authz-pam',
@@ -966,6 +981,7 @@ 'id': 'str' }, 'discriminator': 'qom-type', 'data': { + 'acpi-generic-initiator': 'AcpiGenericInitiatorProperties', 'authz-list': 'AuthZListProperties', 'authz-listfile': 'AuthZListFileProperties', 'authz-pam': 'AuthZPAMProperties',
Signature · 1 line
2.17.1
Re: [PATCH v3 1/2] qom: new object to associate device to numa node
Quoted 16 lines · <[email protected]> writes:
From: Ankit Agrawal <[email protected]>
NVIDIA GPU's support MIG (Mult-Instance GPUs) feature [1], which allows partitioning of the GPU device resources (including device memory) into several (upto 8) isolated instances. Each of the partitioned memory needs a dedicated NUMA node to operate. The partitions are not fixed and they can be created/deleted at runtime.
Unfortunately Linux OS does not provide a means to dynamically create/destroy NUMA nodes and such feature implementation is not expected to be trivial. The nodes that OS discovers at the boot time while parsing SRAT remains fixed. So we utilize the Generic Initiator Affinity structures that allows association between nodes and devices. Multiple GI structures per BDF is possible, allowing creation of multiple nodes by exposing unique PXM in each of these structures.
Quoted 5 lines
Introduce a new acpi-generic-initiator object to allow host admin provide the device and the corresponding NUMA nodes. Qemu maintain this association and use this object to build the requisite GI Affinity Structure.
An admin can provide the range of nodes using a ':' delimited numalist and
Please don't create special-purpose syntax, use existing general-purpose syntax. See also review of qom.json below.
Quoted 170 lines
link it to a device by providing its id. The node ids are extracted from numalist and stores as a uint16List. The following sample creates 8 nodes and link them to the device dev0:
(quoted diff) +0
-10-numa node,nodeid=2 \ -numa node,nodeid=3 \ -numa node,nodeid=4 \ -numa node,nodeid=5 \ -numa node,nodeid=6 \ -numa node,nodeid=7 \ -numa node,nodeid=8 \ -numa node,nodeid=9 \ -device vfio-pci-nohotplug,host=0009:01:00.0,bus=pcie.0,addr=04.0,rombar=0,id=dev0 \ -object acpi-generic-initiator,id=gi0,device=dev0,numalist=2:3:4:5:6:7:8:9 \[1] https://www.nvidia.com/en-in/technologies/multi-instance-gpu
Signed-off-by Ankit Agrawal <[email protected]>--- hw/acpi/acpi-generic-initiator.c | 80 ++++++++++++++++++++++++ hw/acpi/meson.build | 1 + include/hw/acpi/acpi-generic-initiator.h | 29 +++++++++ qapi/qom.json | 16 +++++ 4 files changed, 126 insertions(+) create mode 100644 hw/acpi/acpi-generic-initiator.c create mode 100644 include/hw/acpi/acpi-generic-initiator.hhw/acpi/acpi-generic-initiator.c new file +80
-0@@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved +*/ + +#include "qemu/osdep.h" +#include "hw/qdev-properties.h" +#include "qapi/error.h" +#include "qapi/visitor.h" +#include "qom/object_interfaces.h" +#include "qom/object.h" +#include "hw/qdev-core.h" +#include "hw/vfio/vfio-common.h" +#include "hw/vfio/pci.h" +#include "hw/pci/pci_device.h" +#include "sysemu/numa.h" +#include "hw/acpi/acpi-generic-initiator.h" + +OBJECT_DEFINE_TYPE_WITH_INTERFACES (AcpiGenericInitiator, acpi_generic_initiator, +ACPI_GENERIC_INITIATOR ,OBJECT , + { TYPE_USER_CREATABLE }, + { NULL }) + +OBJECT_DECLARE_SIMPLE_TYPE(AcpiGenericInitiator, ACPI_GENERIC_INITIATOR) + +static void acpi_generic_initiator_init (Object *obj) +{ +AcpiGenericInitiator *gi =ACPI_GENERIC_INITIATOR (obj); + gi->device =NULL ; + gi->nodelist =NULL ; +} + +static void acpi_generic_initiator_finalize (Object *obj) +{ +AcpiGenericInitiator *gi =ACPI_GENERIC_INITIATOR (obj); + +g_free (gi->device); +qapi_free_uint16List (gi->nodelist); +} + +static void acpi_generic_initiator_set_device (Object *obj,const char *val, +Error **errp) +{ +AcpiGenericInitiator *gi =ACPI_GENERIC_INITIATOR (obj); + + gi->device =g_strdup (val); +} + +static void acpi_generic_initiator_set_nodelist (Object *obj,const char *val, +Error **errp) +{ +AcpiGenericInitiator *gi =ACPI_GENERIC_INITIATOR (obj); +char *value =g_strdup (val); +uint16_t node; +uint16List **tail = &(gi->nodelist); +char *nodestr = value ?strtok (value,":" ) :NULL ; + +while (nodestr) { +if (sscanf (nodestr,"%hu" , &node) !=1 ) { +error_setg (errp,"failed to read node-id" ); +return ; + } + +if (node >=MAX_NODES ) { +error_setg (errp,"invalid node-id" ); +return ; + } + +QAPI_LIST_APPEND (tail, node); + nodestr =strtok (NULL ,":" ); + } +} + +static void acpi_generic_initiator_class_init (ObjectClass *oc,void *data) +{ +object_class_property_add_str (oc,ACPI_GENERIC_INITIATOR_DEVICE_PROP ,NULL , + acpi_generic_initiator_set_device); +object_class_property_add_str (oc,ACPI_GENERIC_INITIATOR_NODELIST_PROP , +NULL , acpi_generic_initiator_set_nodelist); +}hw/acpi/meson.build +1
-0@@ -1,5 +1,6 @@ acpi_ss = ss.source_set() acpi_ss.add(files( + 'acpi-generic-initiator.c', 'acpi_interface.c', 'aml-build.c', 'bios-linker-loader.c',include/hw/acpi/acpi-generic-initiator.h new file +29
-0@@ -0,0 +1,29 @@ +#ifndef ACPI_GENERIC_INITIATOR_H +#define ACPI_GENERIC_INITIATOR_H + +#include "hw/mem/pc-dimm.h" +#include "hw/acpi/bios-linker-loader.h" +#include "qemu/uuid.h" +#include "hw/acpi/aml-build.h" +#include "qom/object.h" +#include "qom/object_interfaces.h" + +#define TYPE_ACPI_GENERIC_INITIATOR "acpi-generic-initiator" + +#define ACPI_GENERIC_INITIATOR_DEVICE_PROP "device" +#define ACPI_GENERIC_INITIATOR_NODELIST_PROP "nodelist" + +typedef struct AcpiGenericInitiator { +/* private */ +Object parent; + +/* public */ +char *device; +uint16List *nodelist; +}AcpiGenericInitiator ; + +typedef struct AcpiGenericInitiatorClass { +ObjectClass parent_class; +}AcpiGenericInitiatorClass ; + +#endif qapi/qom.json +3
-0@@ -779,6 +779,20 @@ { 'struct': 'VfioUserServerProperties', 'data': { 'socket': 'SocketAddress', 'device': 'str' } } +## +# @AcpiGenericInitiatorProperties: +#
qapi/qom.json line 794
Quoted 10 lines
qapi/qom.json +10
-0+# Properties for acpi-generic-initiator objects. +# +# @device: the ID of the device to be associated with the node +# +# @nodelist: delimited numa node list +# +# Since: 8.2 +## +{ 'struct': 'AcpiGenericInitiatorProperties', + 'data': { 'device': 'str', 'nodelist': 'str' } }
Do not encode structured data in strings. Instead:
'nodes': ['uint16']
This matches MemoryBackendProperties member @host-nodes.
Check out host_memory_backend_get_host_nodes() and host_memory_backend_set_host_nodes() to see how to work with such a member.
Quoted 20 lines
qapi/qom.json +3
-0+ ## # @RngProperties: #@@ -896,6 +910,7 @@ ## { 'enum': 'ObjectType', 'data': [ + 'acpi-generic-initiator', 'authz-list', 'authz-listfile', 'authz-pam',@@ -966,6 +981,7 @@ 'id': 'str' }, 'discriminator': 'qom-type', 'data': { + 'acpi-generic-initiator': 'AcpiGenericInitiatorProperties', 'authz-list': 'AuthZListProperties', 'authz-listfile': 'AuthZListFileProperties', 'authz-pam': 'AuthZPAMProperties',
PATCH v3 2/2 hw/acpi: Implement the SRAT GI affinity structure
In reply to [email protected]From: Ankit Agrawal <[email protected]>
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 devices and nodes 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.5, Section 5.2.16.6
[2] ACPI Spec 6.5, Table 5.66
--- hw/acpi/acpi-generic-initiator.c | 79 ++++++++++++++++++++++++ hw/arm/virt-acpi-build.c | 3 + include/hw/acpi/acpi-generic-initiator.h | 21 +++++++ 3 files changed, 103 insertions(+)
hw/acpi/acpi-generic-initiator.c +79 -0
@@ -78,3 +78,82 @@ static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data)object_class_property_add_str (oc,ACPI_GENERIC_INITIATOR_NODELIST_PROP ,NULL , acpi_generic_initiator_set_nodelist); } + +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); +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); +return list; +} + +/* +* ACPI spec, Revision 6.5 +* 5.2.16.6 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 */ +build_append_int_noprefix (table_data, node,4 );/* Proximity Domain */ +build_append_int_noprefix (table_data, handle->segment,2 ); +build_append_int_noprefix (table_data, handle->bdf,2 ); + +/* Reserved */ +for (index =0 ; index <12 ; index++) { +build_append_int_noprefix (table_data, handle->res[index],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 (); +for (gi_list = list; gi_list; gi_list = gi_list->next) { +AcpiGenericInitiator *gi = gi_list->data; +Object *o; +uint16List *l; + + o =object_resolve_path_type (gi->device,TYPE_VFIO_PCI ,NULL ); +if (!o) { +continue ; + } + +for (l = gi->nodelist; l; l = l->next) { +PCIDeviceHandle dev_handle = {0 }; +PCIDevice *pci_dev =PCI_DEVICE (o); + 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, + l->value, &dev_handle); + } + } +g_slist_free (list); +}
hw/arm/virt-acpi-build.c +3 -0
@@ -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); +if (ms->nvdimms_state->is_enabled) {nvdimm_build_srat (table_data); }
include/hw/acpi/acpi-generic-initiator.h +21 -0
@@ -26,4 +26,25 @@ typedef struct AcpiGenericInitiatorClass { ObjectClass parent_class; } AcpiGenericInitiatorClass; +/* +* ACPI 6.5: Table 5-68 Flags - Generic Initiator +*/ +typedef enum { +GEN_AFFINITY_NOFLAGS =0 , +GEN_AFFINITY_ENABLED = (1 <<0 ), +GEN_AFFINITY_ARCH_TRANS = (1 <<1 ), +}GenericAffinityFlags ; + +/* +* ACPI 6.5: Table 5-66 Device Handle - PCI +* Device Handle definition +*/ +typedef struct PCIDeviceHandle { +uint16_t segment; +uint16_t bdf; +uint8_t res[12 ]; +}PCIDeviceHandle ; + +void build_srat_generic_pci_initiator (GArray *table_data); +#endif
Signature · 1 line
2.17.1
Re: [PATCH v3 2/2] hw/acpi: Implement the SRAT GI affinity structure
Quoted 68 lines · On Wed, 8 Nov 2023 00:30:39 +0530 <[email protected]> wrote:
From: Ankit Agrawal <[email protected]>
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 devices and nodes 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.5, Section 5.2.16.6
[2] ACPI Spec 6.5, Table 5.66Signed-off-by Ankit Agrawal <[email protected]>--- hw/acpi/acpi-generic-initiator.c | 79 ++++++++++++++++++++++++ hw/arm/virt-acpi-build.c | 3 + include/hw/acpi/acpi-generic-initiator.h | 21 +++++++ 3 files changed, 103 insertions(+)hw/acpi/acpi-generic-initiator.c +32
-0@@ -78,3 +78,82 @@ static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data)object_class_property_add_str (oc,ACPI_GENERIC_INITIATOR_NODELIST_PROP ,NULL , acpi_generic_initiator_set_nodelist); } + +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); +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); +return list; +} + +/* +* ACPI spec, Revision 6.5 +* 5.2.16.6 Generic Initiator Affinity Structure +*/
hw/acpi/acpi-generic-initiator.c static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data) · line 122
Quoted 10 lines
hw/acpi/acpi-generic-initiator.c +10
-0+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 */
/* Device Handle Type: PCI */
Quoted 12 lines
hw/acpi/acpi-generic-initiator.c +12
-0+build_append_int_noprefix (table_data, node,4 );/* Proximity Domain */ +build_append_int_noprefix (table_data, handle->segment,2 ); +build_append_int_noprefix (table_data, handle->bdf,2 ); + +/* Reserved */ +for (index =0 ; index <12 ; index++) { +build_append_int_noprefix (table_data, handle->res[index],1 ); + } + +build_append_int_noprefix (table_data,GEN_AFFINITY_ENABLED ,4 );/* Flags */ +build_append_int_noprefix (table_data,0 ,4 );/* Reserved */ +}
hw/acpi/acpi-generic-initiator.c static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data) · line 144
Quoted 10 lines
hw/acpi/acpi-generic-initiator.c +10
-0+ +void build_srat_generic_pci_initiator (GArray *table_data) +{ +GSList *gi_list, *list =acpi_generic_initiator_get_list (); +for (gi_list = list; gi_list; gi_list = gi_list->next) { +AcpiGenericInitiator *gi = gi_list->data; +Object *o; +uint16List *l; + + o =object_resolve_path_type (gi->device,TYPE_VFIO_PCI ,NULL );
As per previous comments, this should not be tied to vfio. This should be able to describe an association between any PCI device and various proximity domains, even those beyond this current use case.
It also looks like this support just silently fails if the device string isn't the right type or isn't found. That's not good. Should the previous patch validate the device where the Error return is more readily available rather than only doing a strdup there? Maybe then we should store the object there rather than a char buffer.
Don't we also still need to enforce that the device is not hotpluggable since we're tying it to this fixed ACPI object? That was implicit when previously testing for the non-hotpluggable vfio-pci device type, but should rely on something like device_get_hotpluggable() now.
Also the ACPI Generic Initiator supports either a PCI or ACPI device handle, where we're only adding PCI support here. What do we want ACPI device support to look like? Is it sufficient that device= only accepts a PCI device now and fails on anything else and would later be updated to accept an ACPI device or should the object have different entry points, ex. pci_dev = vs acpi_dev= where it might later be introspected whether ACPI device support exists?
hw/acpi/acpi-generic-initiator.c static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data) · line 151
Quoted 7 lines
hw/acpi/acpi-generic-initiator.c +7
-0+if (!o) { +continue ; + } + +for (l = gi->nodelist; l; l = l->next) { +PCIDeviceHandle dev_handle = {0 }; +PCIDevice *pci_dev =PCI_DEVICE (o);
I'd explicitly set the segment to zero just to make it more apparent that it would need to be addressed when QEMU adds multi-segment support. Thanks,
Alex
Quoted 59 lines
hw/acpi/acpi-generic-initiator.c +8
-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, + l->value, &dev_handle); + } + } +g_slist_free (list); +}hw/arm/virt-acpi-build.c +3
-0@@ -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); +if (ms->nvdimms_state->is_enabled) {nvdimm_build_srat (table_data); }include/hw/acpi/acpi-generic-initiator.h +21
-0@@ -26,4 +26,25 @@ typedef struct AcpiGenericInitiatorClass { ObjectClass parent_class; } AcpiGenericInitiatorClass; +/* +* ACPI 6.5: Table 5-68 Flags - Generic Initiator +*/ +typedef enum { +GEN_AFFINITY_NOFLAGS =0 , +GEN_AFFINITY_ENABLED = (1 <<0 ), +GEN_AFFINITY_ARCH_TRANS = (1 <<1 ), +}GenericAffinityFlags ; + +/* +* ACPI 6.5: Table 5-66 Device Handle - PCI +* Device Handle definition +*/ +typedef struct PCIDeviceHandle { +uint16_t segment; +uint16_t bdf; +uint8_t res[12 ]; +}PCIDeviceHandle ; + +void build_srat_generic_pci_initiator (GArray *table_data); +#endif
Re: [PATCH v3 2/2] hw/acpi: Implement the SRAT GI affinity structure
In reply to [email protected]Quoted 56 lines · On Wed, Nov 08, 2023 at 12:30:39AM +0530, [email protected] wrote:
From: Ankit Agrawal <[email protected]>
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 devices and nodes 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.5, Section 5.2.16.6
[2] ACPI Spec 6.5, Table 5.66Signed-off-by Ankit Agrawal <[email protected]>--- hw/acpi/acpi-generic-initiator.c | 79 ++++++++++++++++++++++++ hw/arm/virt-acpi-build.c | 3 + include/hw/acpi/acpi-generic-initiator.h | 21 +++++++ 3 files changed, 103 insertions(+)hw/acpi/acpi-generic-initiator.c +20
-0@@ -78,3 +78,82 @@ static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data)object_class_property_add_str (oc,ACPI_GENERIC_INITIATOR_NODELIST_PROP ,NULL , acpi_generic_initiator_set_nodelist); } + +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); +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. +*/
hw/acpi/acpi-generic-initiator.c static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data) · line 110
Quoted 10 lines
hw/acpi/acpi-generic-initiator.c +10
-0+static GSList *acpi_generic_initiator_get_list (void ) +{ +GSList *list =NULL ; + +object_child_foreach (object_get_root (), acpi_generic_initiator_list, &list); +return list; +} + +/* +* ACPI spec, Revision 6.5
we normally just say ACPI 6.5 even though a couple of places are more verbose.
Quoted 76 lines
hw/acpi/acpi-generic-initiator.c +49
-0+* 5.2.16.6 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 */ +build_append_int_noprefix (table_data, node,4 );/* Proximity Domain */ +build_append_int_noprefix (table_data, handle->segment,2 ); +build_append_int_noprefix (table_data, handle->bdf,2 ); + +/* Reserved */ +for (index =0 ; index <12 ; index++) { +build_append_int_noprefix (table_data, handle->res[index],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 (); +for (gi_list = list; gi_list; gi_list = gi_list->next) { +AcpiGenericInitiator *gi = gi_list->data; +Object *o; +uint16List *l; + + o =object_resolve_path_type (gi->device,TYPE_VFIO_PCI ,NULL ); +if (!o) { +continue ; + } + +for (l = gi->nodelist; l; l = l->next) { +PCIDeviceHandle dev_handle = {0 }; +PCIDevice *pci_dev =PCI_DEVICE (o); + 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, + l->value, &dev_handle); + } + } +g_slist_free (list); +}hw/arm/virt-acpi-build.c +3
-0@@ -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); +if (ms->nvdimms_state->is_enabled) {nvdimm_build_srat (table_data); }include/hw/acpi/acpi-generic-initiator.h +0
-0@@ -26,4 +26,25 @@ typedef struct AcpiGenericInitiatorClass {ObjectClass parent_class;
include/hw/acpi/acpi-generic-initiator.h typedef struct AcpiGenericInitiatorClass { · line 36
Quoted 10 lines
include/hw/acpi/acpi-generic-initiator.h +8
-0} AcpiGenericInitiatorClass; +/* +* ACPI 6.5: Table 5-68 Flags - Generic Initiator +*/ +typedef enum { +GEN_AFFINITY_NOFLAGS =0 , +GEN_AFFINITY_ENABLED = (1 <<0 ), +GEN_AFFINITY_ARCH_TRANS = (1 <<1 ), +}GenericAffinityFlags ;
Don't add these one-time use flags. They are impossible to match to spec without reading and memorizing all of it. The way we do it in ACPI code is this:
(1 << 0) /* [text matching ACPI spec verbatim ] */
this also means you will not add a ton of dead code just because it is in the spec.
include/hw/acpi/acpi-generic-initiator.h typedef struct AcpiGenericInitiatorClass { · line 39
Quoted 3 lines
include/hw/acpi/acpi-generic-initiator.h +3
-0+ +/* +* ACPI 6.5: Table 5-66 Device Handle - PCI
In ACPI we document *earliest* spec version that includes this, not just a random one you looked at. I checked 6.3 and it's there. Pls find earliest one.
Same applies everywhere
include/hw/acpi/acpi-generic-initiator.h typedef struct AcpiGenericInitiatorClass { · line 40
Quoted 1 line
include/hw/acpi/acpi-generic-initiator.h +1
-0+* Device Handle definition
Again match spec text exactly. one line, and "definition" is not there.
include/hw/acpi/acpi-generic-initiator.h typedef struct AcpiGenericInitiatorClass { · line 45
Quoted 5 lines
include/hw/acpi/acpi-generic-initiator.h +5
-0+*/ +typedef struct PCIDeviceHandle { + uint16_t segment; + uint16_t bdf; + uint8_t res[12];
what is this "res" and why do you need to pass it? It's always 0 isn't it?
Quoted 7 lines
include/hw/acpi/acpi-generic-initiator.h +4
-0+} PCIDeviceHandle; + +void build_srat_generic_pci_initiator (GArray *table_data); +#endif Signature · 1 line
2.17.1
Re: [PATCH v3 2/2] hw/acpi: Implement the SRAT GI affinity structure
In reply to [email protected]hw/acpi/acpi-generic-initiator.c static void acpi_generic_initiator_class_init(ObjectClass *oc, void *data) · line 157
Quoted 9 lines · On Wed, Nov 08, 2023 at 12:30:39AM +0530, [email protected] wrote:
hw/acpi/acpi-generic-initiator.c +9
-0+for (l = gi->nodelist; l; l = l->next) { +PCIDeviceHandle dev_handle = {0 }; +PCIDevice *pci_dev =PCI_DEVICE (o); + 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, + l->value, &dev_handle); + } + }
if you never initialize segment then I don't see why have it. It's just the bdf, just pass that as parameter no need for a struct.
Signature · 1 line
MST
Re: [PATCH v3 2/2] hw/acpi: Implement the SRAT GI affinity structure
Quoted 10 lines
Quoted 9 lines
+ for (l =
gi->nodelist; l; l =l->next) {
+ PCIDeviceHandledev_handle= {0};
+ PCIDevice*pci_dev=PCI_DEVICE(o);
+ 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, +l->value,&dev_handle); + }
+ }
Quoted 6 lines
if you never initialize segment then I don't see why have it. It's just the bdf, just pass that as parameter no need for a struct.
I'd explicitly set the segment to zero just to make it more apparent that it would need to be addressed when QEMU adds multi-segment support.
Okay, so I'll keep the segment id, but set it to 0 explicitly.
Quoted 8 lines
Quoted 1 line
+ * ACPI spec, Revision 6.5
we normally just say ACPI 6.5 even though a couple of places are more verbose.
In ACPI we document *earliest* spec version that includes this, not just a random one you looked at. I checked 6.3 and it's there. Pls find earliest one.
Will make the change.
Quoted 10 lines
include/hw/acpi/acpi-generic-initiator.h typedef struct AcpiGenericInitiatorClass { · line 36
Quoted 5 lines
include/hw/acpi/acpi-generic-initiator.h +5
-0+typedef enum { +GEN_AFFINITY_NOFLAGS =0 , +GEN_AFFINITY_ENABLED = (1 <<0 ), +GEN_AFFINITY_ARCH_TRANS = (1 <<1 ), +}GenericAffinityFlags ;Don't add these one-time use flags. They are impossible to match to spec without reading and memorizing all of it. The way we do it in ACPI code is this:
Quoted 4 lines
(1 << 0) /* [text matching ACPI spec verbatim ] */
this also means you will not add a ton of dead code just because it is in the spec.
Ack.
Quoted 7 lines
include/hw/acpi/acpi-generic-initiator.h typedef struct AcpiGenericInitiatorClass { · line 45
Quoted 4 lines
include/hw/acpi/acpi-generic-initiator.h +4
-0+typedef struct PCIDeviceHandle { + uint16_t segment; + uint16_t bdf; + uint8_t res[12];what is this "res" and why do you need to pass it? It's always 0 isn't it?
It is 12 bytes reserved field in the "Device Handle - PCI" described in ACPI 6.5, Table 5.66. I'll remove it.
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + o = object_resolve_path_type(gi->device, TYPE_VFIO_PCI, NULL);As per previous comments, this should not be tied to vfio. This should be able to describe an association between any PCI device and various proximity domains, even those beyond this current use case.
Sure, will change it to use TYPE_PCI_DEVICE.
Quoted 5 lines
It also looks like this support just silently fails if the device string isn't the right type or isn't found. That's not good. Should the previous patch validate the device where the Error return is more readily available rather than only doing a strdup there? Maybe then we should store the object there rather than a char buffer.
AFAIU in a normal flow currently, a qemu -object is (parsed and) created much earlier that a -device. This complicates the situation as when the acpi-generic-initiator object is being created, the device is not available for error check. Maybe I should treat this object specially to create much later?
Quoted 4 lines
Don't we also still need to enforce that the device is not hotpluggable since we're tying it to this fixed ACPI object? That was implicit when previously testing for the non-hotpluggable vfio-pci device type, but should rely on something like
device_get_hotpluggable()now.
I think this will be similarly problematic as above due to the sequence of object creation.
Quoted 7 lines
Also the ACPI Generic Initiator supports either a PCI or ACPI device handle, where we're only adding PCI support here. What do we want ACPI device support to look like? Is it sufficient that device= only accepts a PCI device now and fails on anything else and would later be updated to accept an ACPI device or should the object have different entry points, ex.
pci_dev= vs acpi_dev= where it might later be introspected whether ACPI device support exists?
I am fine with either way. If we prefer different entry points, I can make the change.
Re: [PATCH v3 2/2] hw/acpi: Implement the SRAT GI affinity structure
Quoted 27 lines · Ankit Agrawal
Quoted 5 lines
It also looks like this support just silently fails if the device string isn't the right type or isn't found. That's not good. Should the previous patch validate the device where the Error return is more readily available rather than only doing a strdup there? Maybe then we should store the object there rather than a char buffer.
AFAIU in a normal flow currently, a qemu -object is (parsed and) created much earlier that a -device. This complicates the situation as when the acpi-generic-initiator object is being created, the device is not available for error check. Maybe I should treat this object specially to create much later?
Quoted 4 lines
Don't we also still need to enforce that the device is not hotpluggable since we're tying it to this fixed ACPI object? That was implicit when previously testing for the non-hotpluggable vfio-pci device type, but should rely on something like
device_get_hotpluggable()now.I think this will be similarly problematic as above due to the sequence of object creation.
Quoted 7 lines
Also the ACPI Generic Initiator supports either a PCI or ACPI device handle, where we're only adding PCI support here. What do we want ACPI device support to look like? Is it sufficient that device= only accepts a PCI device now and fails on anything else and would later be updated to accept an ACPI device or should the object have different entry points, ex.
pci_dev= vs acpi_dev= where it might later be introspected whether ACPI device support exists?
Quoted 2 lines
I am fine with either way. If we prefer different entry points, I can make the change.
Not the expert on QOM. Hope one of QOM maintainers can answer.