[PATCH 04/14] vfio: add vfio_attach_device_by_iommu_type()

John Levon posted 14 patches 7 months, 1 week ago
There is a newer version of this series
[PATCH 04/14] vfio: add vfio_attach_device_by_iommu_type()
Posted by John Levon 7 months, 1 week ago
Allow attachment by explicitly passing a TYPE_VFIO_IOMMU_* string;
vfio-user will use this later.

Signed-off-by: John Levon <john.levon@nutanix.com>
---
 hw/vfio/device.c              | 24 ++++++++++++++++--------
 include/hw/vfio/vfio-device.h |  3 +++
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 4d940ddb3a..f74b9c25ea 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -367,20 +367,17 @@ VFIODevice *vfio_get_vfio_device(Object *obj)
     }
 }
 
-bool vfio_device_attach(char *name, VFIODevice *vbasedev,
-                        AddressSpace *as, Error **errp)
+bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
+                                      VFIODevice *vbasedev, AddressSpace *as,
+                                      Error **errp)
 {
-    const VFIOIOMMUClass *ops =
-        VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY));
     HostIOMMUDevice *hiod = NULL;
 
-    if (vbasedev->iommufd) {
-        ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
-    }
+    const VFIOIOMMUClass *ops =
+        VFIO_IOMMU_CLASS(object_class_by_name(iommu_type));
 
     assert(ops);
 
-
     if (!vbasedev->mdev) {
         hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
         vbasedev->hiod = hiod;
@@ -395,6 +392,17 @@ bool vfio_device_attach(char *name, VFIODevice *vbasedev,
     return true;
 }
 
+bool vfio_device_attach(char *name, VFIODevice *vbasedev,
+                        AddressSpace *as, Error **errp)
+{
+    const char *iommu_type = vbasedev->iommufd ?
+                             TYPE_VFIO_IOMMU_IOMMUFD :
+                             TYPE_VFIO_IOMMU_LEGACY;
+
+    return vfio_device_attach_by_iommu_type(iommu_type, name, vbasedev,
+                                            as, errp);
+}
+
 void vfio_device_detach(VFIODevice *vbasedev)
 {
     if (!vbasedev->bcontainer) {
diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
index 1a2fe378d0..3563a82ede 100644
--- a/include/hw/vfio/vfio-device.h
+++ b/include/hw/vfio/vfio-device.h
@@ -126,6 +126,9 @@ bool vfio_device_is_mdev(VFIODevice *vbasedev);
 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp);
 bool vfio_device_attach(char *name, VFIODevice *vbasedev,
                         AddressSpace *as, Error **errp);
+bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
+                                      VFIODevice *vbasedev, AddressSpace *as,
+                                      Error **errp);
 void vfio_device_detach(VFIODevice *vbasedev);
 VFIODevice *vfio_get_vfio_device(Object *obj);
 
-- 
2.34.1
Re: [PATCH 04/14] vfio: add vfio_attach_device_by_iommu_type()
Posted by Cédric Le Goater 6 months, 3 weeks ago
Same typo in Subject

On 4/9/25 15:48, John Levon wrote:
> Allow attachment by explicitly passing a TYPE_VFIO_IOMMU_* string;
> vfio-user will use this later.
> 
> Signed-off-by: John Levon <john.levon@nutanix.com>
> ---
>   hw/vfio/device.c              | 24 ++++++++++++++++--------
>   include/hw/vfio/vfio-device.h |  3 +++
>   2 files changed, 19 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/vfio/device.c b/hw/vfio/device.c
> index 4d940ddb3a..f74b9c25ea 100644
> --- a/hw/vfio/device.c
> +++ b/hw/vfio/device.c
> @@ -367,20 +367,17 @@ VFIODevice *vfio_get_vfio_device(Object *obj)
>       }
>   }
>   
> -bool vfio_device_attach(char *name, VFIODevice *vbasedev,
> -                        AddressSpace *as, Error **errp)
> +bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
> +                                      VFIODevice *vbasedev, AddressSpace *as,
> +                                      Error **errp)
>   {
> -    const VFIOIOMMUClass *ops =
> -        VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY));
>       HostIOMMUDevice *hiod = NULL;
>   
> -    if (vbasedev->iommufd) {
> -        ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
> -    }
> +    const VFIOIOMMUClass *ops =
> +        VFIO_IOMMU_CLASS(object_class_by_name(iommu_type));


There are minor conflicts with Zhenzhong's series :

   https://lore.kernel.org/qemu-devel/20250423072824.3647952-1-zhenzhong.duan@intel.com/

which can be fixed easily.

Reviewed-by: Cédric Le Goater <clg@redhat.com>

Thanks,

C.

>       assert(ops);
>   
> -
>       if (!vbasedev->mdev) {
>           hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
>           vbasedev->hiod = hiod;
> @@ -395,6 +392,17 @@ bool vfio_device_attach(char *name, VFIODevice *vbasedev,
>       return true;
>   }
>   
> +bool vfio_device_attach(char *name, VFIODevice *vbasedev,
> +                        AddressSpace *as, Error **errp)
> +{
> +    const char *iommu_type = vbasedev->iommufd ?
> +                             TYPE_VFIO_IOMMU_IOMMUFD :
> +                             TYPE_VFIO_IOMMU_LEGACY;
> +
> +    return vfio_device_attach_by_iommu_type(iommu_type, name, vbasedev,
> +                                            as, errp);
> +}
> +
>   void vfio_device_detach(VFIODevice *vbasedev)
>   {
>       if (!vbasedev->bcontainer) {
> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
> index 1a2fe378d0..3563a82ede 100644
> --- a/include/hw/vfio/vfio-device.h
> +++ b/include/hw/vfio/vfio-device.h
> @@ -126,6 +126,9 @@ bool vfio_device_is_mdev(VFIODevice *vbasedev);
>   bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp);
>   bool vfio_device_attach(char *name, VFIODevice *vbasedev,
>                           AddressSpace *as, Error **errp);
> +bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
> +                                      VFIODevice *vbasedev, AddressSpace *as,
> +                                      Error **errp);
>   void vfio_device_detach(VFIODevice *vbasedev);
>   VFIODevice *vfio_get_vfio_device(Object *obj);
>