@@ -1551,6 +1551,25 @@ retry:
return info;
}
+void vfio_prepare_device(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
+ VFIOGroup *group, struct vfio_device_info *info)
+{
+ vbasedev->group = group;
+
+ vbasedev->num_irqs = info->num_irqs;
+ vbasedev->num_regions = info->num_regions;
+ vbasedev->flags = info->flags;
+ vbasedev->reset_works = !!(info->flags & VFIO_DEVICE_FLAGS_RESET);
+
+ vbasedev->bcontainer = bcontainer;
+ QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
+ if (group) {
+ QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
+ }
+
+ QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
+}
+
bool vfio_attach_device_by_iommu_type(const char *iommu_type, char *name,
VFIODevice *vbasedev, AddressSpace *as,
Error **errp)
@@ -878,17 +878,11 @@ static bool vfio_get_device(VFIOGroup *group, const char *name,
}
vbasedev->fd = fd;
- vbasedev->group = group;
- QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
- vbasedev->num_irqs = info->num_irqs;
- vbasedev->num_regions = info->num_regions;
- vbasedev->flags = info->flags;
+ vfio_prepare_device(vbasedev, &group->container->bcontainer, group, info);
trace_vfio_get_device(name, info->flags, info->num_regions, info->num_irqs);
- vbasedev->reset_works = !!(info->flags & VFIO_DEVICE_FLAGS_RESET);
-
return true;
}
@@ -941,7 +935,6 @@ static bool vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev,
int groupid = vfio_device_groupid(vbasedev, errp);
VFIODevice *vbasedev_iter;
VFIOGroup *group;
- VFIOContainerBase *bcontainer;
if (groupid < 0) {
return false;
@@ -970,11 +963,6 @@ static bool vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev,
return false;
}
- bcontainer = &group->container->bcontainer;
- vbasedev->bcontainer = bcontainer;
- QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
- QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
-
return true;
}
@@ -603,14 +603,7 @@ found_container:
iommufd_cdev_ram_block_discard_disable(false);
}
- vbasedev->group = 0;
- vbasedev->num_irqs = dev_info.num_irqs;
- vbasedev->num_regions = dev_info.num_regions;
- vbasedev->flags = dev_info.flags;
- vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
- vbasedev->bcontainer = bcontainer;
- QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
- QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
+ vfio_prepare_device(vbasedev, bcontainer, NULL, &dev_info);
trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
vbasedev->num_regions, vbasedev->flags);
@@ -250,6 +250,8 @@ void vfio_reset_handler(void *opaque);
struct vfio_device_info *vfio_get_device_info(int fd);
bool vfio_device_is_mdev(VFIODevice *vbasedev);
bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp);
+void vfio_prepare_device(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
+ VFIOGroup *group, struct vfio_device_info *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,
Commonize some initialization code shared by the legacy and iommufd vfio implementations (and later by vfio-user). Signed-off-by: John Levon <john.levon@nutanix.com> --- hw/vfio/common.c | 19 +++++++++++++++++++ hw/vfio/container.c | 14 +------------- hw/vfio/iommufd.c | 9 +-------- include/hw/vfio/vfio-common.h | 2 ++ 4 files changed, 23 insertions(+), 21 deletions(-)