diff mbox series

[04/26] vfio: add vfio_attach_device_by_iommu_type()

Message ID 20250108115032.1677686-5-john.levon@nutanix.com (mailing list archive)
State New
Headers show
Series vfio-user client | expand

Commit Message

John Levon Jan. 8, 2025, 11:50 a.m. UTC
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/common.c              | 30 +++++++++++++++++++-----------
 include/hw/vfio/vfio-common.h |  3 +++
 2 files changed, 22 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 49e3543c89..cb299fc3bf 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -1551,25 +1551,20 @@  retry:
     return info;
 }
 
-bool vfio_attach_device(char *name, VFIODevice *vbasedev,
-                        AddressSpace *as, Error **errp)
+bool vfio_attach_device_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));
-    }
-
-    assert(ops);
-
+    const VFIOIOMMUClass *ops =
+        VFIO_IOMMU_CLASS(object_class_by_name(iommu_type));
 
     if (!vbasedev->mdev) {
         hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
         vbasedev->hiod = hiod;
     }
 
+
     if (!ops->attach_device(name, vbasedev, as, errp)) {
         object_unref(hiod);
         vbasedev->hiod = NULL;
@@ -1579,6 +1574,19 @@  bool vfio_attach_device(char *name, VFIODevice *vbasedev,
     return true;
 }
 
+bool vfio_attach_device(char *name, VFIODevice *vbasedev,
+                       AddressSpace *as, Error **errp)
+{
+    const char *iommu_type = TYPE_VFIO_IOMMU_LEGACY;
+
+    if (vbasedev->iommufd) {
+        iommu_type = TYPE_VFIO_IOMMU_IOMMUFD;
+    }
+
+    return vfio_attach_device_by_iommu_type(iommu_type, name, vbasedev,
+                                            as, errp);
+}
+
 void vfio_detach_device(VFIODevice *vbasedev)
 {
     if (!vbasedev->bcontainer) {
diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index 13c67d25cb..387854cb0b 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -252,6 +252,9 @@  bool vfio_device_is_mdev(VFIODevice *vbasedev);
 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp);
 bool vfio_attach_device(char *name, VFIODevice *vbasedev,
                         AddressSpace *as, Error **errp);
+bool vfio_attach_device_by_iommu_type(const char *iommu_type, char *name,
+                                      VFIODevice *vbasedev, AddressSpace *as,
+                                      Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);