From 53a82c6a5a22bb41e9bd3f754479baf4ce0845bf Mon Sep 17 00:00:00 2001 From: Zhenzhong Duan Date: Mon, 5 Aug 2024 09:29:00 +0800 Subject: [PATCH] HostIOMMUDevice: Introduce realize_late callback Previously we have a realize() callback which is called before attachment. But there are still some elements e.g., ioas not ready before attachment. So we need a realize_late() callback to further initialize them. Currently, this callback is only useful for iommufd backend. For legacy backend nothing needs to be initialized after attachment. Signed-off-by: Zhenzhong Duan --- hw/vfio/common.c | 18 +++++++++++++++--- include/sysemu/host_iommu_device.h | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index a8bc1c6055..0be63c5fbc 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1654,6 +1654,7 @@ int vfio_attach_device(char *name, VFIODevice *vbasedev, const VFIOIOMMUClass *ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY)); HostIOMMUDevice *hiod = NULL; + HostIOMMUDeviceClass *hiod_ops = NULL; int ret; if (vbasedev->iommufd) { @@ -1664,17 +1665,28 @@ int vfio_attach_device(char *name, VFIODevice *vbasedev, if (!vbasedev->mdev) { hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename)); + hiod_ops = HOST_IOMMU_DEVICE_GET_CLASS(hiod); vbasedev->hiod = hiod; } ret = ops->attach_device(name, vbasedev, as, errp); if (ret) { - object_unref(hiod); - vbasedev->hiod = NULL; - return ret; + goto err_attach; + } + + if (hiod_ops && hiod_ops->realize_late && + !hiod_ops->realize_late(hiod, vbasedev, errp)) { + ops->detach_device(vbasedev); + ret = -EINVAL; + goto err_attach; } return 0; + +err_attach: + object_unref(hiod); + vbasedev->hiod = NULL; + return ret; } void vfio_detach_device(VFIODevice *vbasedev) diff --git a/include/sysemu/host_iommu_device.h b/include/sysemu/host_iommu_device.h index e4d8300350..84131f5495 100644 --- a/include/sysemu/host_iommu_device.h +++ b/include/sysemu/host_iommu_device.h @@ -64,6 +64,23 @@ struct HostIOMMUDeviceClass { * Returns: true on success, false on failure. */ bool (*realize)(HostIOMMUDevice *hiod, void *opaque, Error **errp); + /** + * @realize_late: initialize host IOMMU device instance after attachment, + * some elements e.g., ioas are ready only after attachment. + * This callback initialize them. + * + * Optional callback. + * + * @hiod: pointer to a host IOMMU device instance. + * + * @opaque: pointer to agent device of this host IOMMU device, + * e.g., VFIO base device or VDPA device. + * + * @errp: pass an Error out when realize fails. + * + * Returns: true on success, false on failure. + */ + bool (*realize_late)(HostIOMMUDevice *hiod, void *opaque, Error **errp); /** * @get_cap: check if a host IOMMU device capability is supported. * -- 2.41.0.windows.1