[PATCH for-9.0 08/10] vfio/iommufd: Introduce a VFIOIOMMU iommufd QOM interface

Cédric Le Goater posted 10 patches 11 months, 3 weeks ago
There is a newer version of this series
[PATCH for-9.0 08/10] vfio/iommufd: Introduce a VFIOIOMMU iommufd QOM interface
Posted by Cédric Le Goater 11 months, 3 weeks ago
As previously done for the sPAPR and legacy IOMMU backends, convert
the VFIOIOMMUOps struct to a QOM interface. The set of of operations
for this backend can be referenced with a literal typename instead of
a C struct.

Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 include/hw/vfio/vfio-common.h         |  1 -
 include/hw/vfio/vfio-container-base.h |  2 +-
 hw/vfio/common.c                      |  2 +-
 hw/vfio/iommufd.c                     | 36 ++++++++++++++++++++-------
 4 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index 14c497b6b0a79466e8f567aceed384ec2c75ea90..9b7ef7d02b5a0ad5266bcc4d06cd6874178978e4 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -210,7 +210,6 @@ typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
 extern VFIOGroupList vfio_group_list;
 extern VFIODeviceList vfio_device_list;
-extern const VFIOIOMMUOps vfio_iommufd_ops;
 extern const MemoryListener vfio_memory_listener;
 extern int vfio_kvm_device_fd;
 
diff --git a/include/hw/vfio/vfio-container-base.h b/include/hw/vfio/vfio-container-base.h
index 4012360c07b7c0a23f170f94a19455c79d3504b1..5fd02fab5fd627dc6010685e9d956ba20ee329fd 100644
--- a/include/hw/vfio/vfio-container-base.h
+++ b/include/hw/vfio/vfio-container-base.h
@@ -17,7 +17,6 @@
 
 typedef struct VFIODevice VFIODevice;
 typedef struct VFIOIOMMUClass VFIOIOMMUClass;
-#define VFIOIOMMUOps VFIOIOMMUClass /* To remove */
 
 typedef struct {
     unsigned long *bitmap;
@@ -97,6 +96,7 @@ typedef struct VFIOIOMMU VFIOIOMMU;
 #define TYPE_VFIO_IOMMU "vfio-iommu"
 #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
 #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
+#define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
 
 #define VFIO_IOMMU(obj) INTERFACE_CHECK(VFIOIOMMU, (obj), TYPE_VFIO_IOMMU)
 DECLARE_CLASS_CHECKERS(VFIOIOMMUClass, VFIO_IOMMU, TYPE_VFIO_IOMMU)
diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 2329d0efc8c1d617f0bfee5283e82b295d2d477d..89ff1c7aeda14d20b2e24f8bc251db0a71d4527c 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -1508,7 +1508,7 @@ int vfio_attach_device(char *name, VFIODevice *vbasedev,
 
 #ifdef CONFIG_IOMMUFD
     if (vbasedev->iommufd) {
-        ops = &vfio_iommufd_ops;
+        ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
     }
 #endif
 
diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index 87a561c54580adc6d7b2711331a00940ff13bd43..807be2545eb147a6fac973752cf37eeede1a4ff6 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -319,6 +319,8 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     int ret, devfd;
     uint32_t ioas_id;
     Error *err = NULL;
+    const VFIOIOMMUClass *iommufd_vioc =
+        VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
 
     if (vbasedev->fd < 0) {
         devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
@@ -340,7 +342,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     /* try to attach to an existing container in this space */
     QLIST_FOREACH(bcontainer, &space->containers, next) {
         container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
-        if (bcontainer->ops != &vfio_iommufd_ops ||
+        if (bcontainer->ops != iommufd_vioc ||
             vbasedev->iommufd != container->be) {
             continue;
         }
@@ -374,7 +376,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     container->ioas_id = ioas_id;
 
     bcontainer = &container->bcontainer;
-    vfio_container_init(bcontainer, space, &vfio_iommufd_ops);
+    vfio_container_init(bcontainer, space, iommufd_vioc);
     QLIST_INSERT_HEAD(&space->containers, bcontainer, next);
 
     ret = iommufd_cdev_attach_container(vbasedev, container, errp);
@@ -476,9 +478,11 @@ static void iommufd_cdev_detach(VFIODevice *vbasedev)
 static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
 {
     VFIODevice *vbasedev_iter;
+    const VFIOIOMMUClass *iommufd_vioc =
+        VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
 
     QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
-        if (vbasedev_iter->bcontainer->ops != &vfio_iommufd_ops) {
+        if (vbasedev_iter->bcontainer->ops != iommufd_vioc) {
             continue;
         }
         if (devid == vbasedev_iter->devid) {
@@ -621,10 +625,24 @@ out_single:
     return ret;
 }
 
-const VFIOIOMMUOps vfio_iommufd_ops = {
-    .dma_map = iommufd_cdev_map,
-    .dma_unmap = iommufd_cdev_unmap,
-    .attach_device = iommufd_cdev_attach,
-    .detach_device = iommufd_cdev_detach,
-    .pci_hot_reset = iommufd_cdev_pci_hot_reset,
+static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
+{
+    VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
+
+    vioc->dma_map = iommufd_cdev_map;
+    vioc->dma_unmap = iommufd_cdev_unmap;
+    vioc->attach_device = iommufd_cdev_attach;
+    vioc->detach_device = iommufd_cdev_detach;
+    vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
 };
+
+static const TypeInfo types[] = {
+    {
+        .name = TYPE_VFIO_IOMMU_IOMMUFD,
+        .parent = TYPE_VFIO_IOMMU,
+        .class_init = vfio_iommu_iommufd_class_init,
+        .class_size = sizeof(VFIOIOMMUClass),
+    },
+};
+
+DEFINE_TYPES(types)
-- 
2.43.0


RE: [PATCH for-9.0 08/10] vfio/iommufd: Introduce a VFIOIOMMU iommufd QOM interface
Posted by Duan, Zhenzhong 11 months, 3 weeks ago

>-----Original Message-----
>From: Cédric Le Goater <clg@redhat.com>
>Sent: Friday, December 8, 2023 4:46 PM
>Subject: [PATCH for-9.0 08/10] vfio/iommufd: Introduce a VFIOIOMMU
>iommufd QOM interface
>
>As previously done for the sPAPR and legacy IOMMU backends, convert
>the VFIOIOMMUOps struct to a QOM interface. The set of of operations
>for this backend can be referenced with a literal typename instead of
>a C struct.
>
>Signed-off-by: Cédric Le Goater <clg@redhat.com>

Reviewed-by: Zhenzhong Duan <zhenzhong.duan@intel.com>

Thanks
Zhenzhong

>---
> include/hw/vfio/vfio-common.h         |  1 -
> include/hw/vfio/vfio-container-base.h |  2 +-
> hw/vfio/common.c                      |  2 +-
> hw/vfio/iommufd.c                     | 36 ++++++++++++++++++++-------
> 4 files changed, 29 insertions(+), 12 deletions(-)
>
>diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-
>common.h
>index
>14c497b6b0a79466e8f567aceed384ec2c75ea90..9b7ef7d02b5a0ad5266bcc
>4d06cd6874178978e4 100644
>--- a/include/hw/vfio/vfio-common.h
>+++ b/include/hw/vfio/vfio-common.h
>@@ -210,7 +210,6 @@ typedef QLIST_HEAD(VFIOGroupList, VFIOGroup)
>VFIOGroupList;
> typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
> extern VFIOGroupList vfio_group_list;
> extern VFIODeviceList vfio_device_list;
>-extern const VFIOIOMMUOps vfio_iommufd_ops;
> extern const MemoryListener vfio_memory_listener;
> extern int vfio_kvm_device_fd;
>
>diff --git a/include/hw/vfio/vfio-container-base.h b/include/hw/vfio/vfio-
>container-base.h
>index
>4012360c07b7c0a23f170f94a19455c79d3504b1..5fd02fab5fd627dc601068
>5e9d956ba20ee329fd 100644
>--- a/include/hw/vfio/vfio-container-base.h
>+++ b/include/hw/vfio/vfio-container-base.h
>@@ -17,7 +17,6 @@
>
> typedef struct VFIODevice VFIODevice;
> typedef struct VFIOIOMMUClass VFIOIOMMUClass;
>-#define VFIOIOMMUOps VFIOIOMMUClass /* To remove */
>
> typedef struct {
>     unsigned long *bitmap;
>@@ -97,6 +96,7 @@ typedef struct VFIOIOMMU VFIOIOMMU;
> #define TYPE_VFIO_IOMMU "vfio-iommu"
> #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
> #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
>+#define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
>
> #define VFIO_IOMMU(obj) INTERFACE_CHECK(VFIOIOMMU, (obj),
>TYPE_VFIO_IOMMU)
> DECLARE_CLASS_CHECKERS(VFIOIOMMUClass, VFIO_IOMMU,
>TYPE_VFIO_IOMMU)
>diff --git a/hw/vfio/common.c b/hw/vfio/common.c
>index
>2329d0efc8c1d617f0bfee5283e82b295d2d477d..89ff1c7aeda14d20b2e24f
>8bc251db0a71d4527c 100644
>--- a/hw/vfio/common.c
>+++ b/hw/vfio/common.c
>@@ -1508,7 +1508,7 @@ int vfio_attach_device(char *name, VFIODevice
>*vbasedev,
>
> #ifdef CONFIG_IOMMUFD
>     if (vbasedev->iommufd) {
>-        ops = &vfio_iommufd_ops;
>+        ops =
>VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUF
>D));
>     }
> #endif
>
>diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
>index
>87a561c54580adc6d7b2711331a00940ff13bd43..807be2545eb147a6fac97
>3752cf37eeede1a4ff6 100644
>--- a/hw/vfio/iommufd.c
>+++ b/hw/vfio/iommufd.c
>@@ -319,6 +319,8 @@ static int iommufd_cdev_attach(const char *name,
>VFIODevice *vbasedev,
>     int ret, devfd;
>     uint32_t ioas_id;
>     Error *err = NULL;
>+    const VFIOIOMMUClass *iommufd_vioc =
>+
>VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUF
>D));
>
>     if (vbasedev->fd < 0) {
>         devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
>@@ -340,7 +342,7 @@ static int iommufd_cdev_attach(const char *name,
>VFIODevice *vbasedev,
>     /* try to attach to an existing container in this space */
>     QLIST_FOREACH(bcontainer, &space->containers, next) {
>         container = container_of(bcontainer, VFIOIOMMUFDContainer,
>bcontainer);
>-        if (bcontainer->ops != &vfio_iommufd_ops ||
>+        if (bcontainer->ops != iommufd_vioc ||
>             vbasedev->iommufd != container->be) {
>             continue;
>         }
>@@ -374,7 +376,7 @@ static int iommufd_cdev_attach(const char *name,
>VFIODevice *vbasedev,
>     container->ioas_id = ioas_id;
>
>     bcontainer = &container->bcontainer;
>-    vfio_container_init(bcontainer, space, &vfio_iommufd_ops);
>+    vfio_container_init(bcontainer, space, iommufd_vioc);
>     QLIST_INSERT_HEAD(&space->containers, bcontainer, next);
>
>     ret = iommufd_cdev_attach_container(vbasedev, container, errp);
>@@ -476,9 +478,11 @@ static void iommufd_cdev_detach(VFIODevice
>*vbasedev)
> static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
> {
>     VFIODevice *vbasedev_iter;
>+    const VFIOIOMMUClass *iommufd_vioc =
>+
>VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUF
>D));
>
>     QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
>-        if (vbasedev_iter->bcontainer->ops != &vfio_iommufd_ops) {
>+        if (vbasedev_iter->bcontainer->ops != iommufd_vioc) {
>             continue;
>         }
>         if (devid == vbasedev_iter->devid) {
>@@ -621,10 +625,24 @@ out_single:
>     return ret;
> }
>
>-const VFIOIOMMUOps vfio_iommufd_ops = {
>-    .dma_map = iommufd_cdev_map,
>-    .dma_unmap = iommufd_cdev_unmap,
>-    .attach_device = iommufd_cdev_attach,
>-    .detach_device = iommufd_cdev_detach,
>-    .pci_hot_reset = iommufd_cdev_pci_hot_reset,
>+static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
>+{
>+    VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
>+
>+    vioc->dma_map = iommufd_cdev_map;
>+    vioc->dma_unmap = iommufd_cdev_unmap;
>+    vioc->attach_device = iommufd_cdev_attach;
>+    vioc->detach_device = iommufd_cdev_detach;
>+    vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
> };
>+
>+static const TypeInfo types[] = {
>+    {
>+        .name = TYPE_VFIO_IOMMU_IOMMUFD,
>+        .parent = TYPE_VFIO_IOMMU,
>+        .class_init = vfio_iommu_iommufd_class_init,
>+        .class_size = sizeof(VFIOIOMMUClass),
>+    },
>+};
>+
>+DEFINE_TYPES(types)
>--
>2.43.0