vfio-iommufd: Add detach_ioas support for emulated VFIO devices
authorYi Liu <yi.l.liu@intel.com>
Tue, 18 Jul 2023 13:55:40 +0000 (06:55 -0700)
committerAlex Williamson <alex.williamson@redhat.com>
Tue, 25 Jul 2023 16:19:18 +0000 (10:19 -0600)
This prepares for adding DETACH ioctl for emulated VFIO devices.

Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Tested-by: Terrence Xu <terrence.xu@intel.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
Tested-by: Yanting Jiang <yanting.jiang@intel.com>
Tested-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
Tested-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Link: https://lore.kernel.org/r/20230718135551.6592-16-yi.l.liu@intel.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/gpu/drm/i915/gvt/kvmgt.c
drivers/s390/cio/vfio_ccw_ops.c
drivers/s390/crypto/vfio_ap_ops.c
drivers/vfio/iommufd.c
include/linux/vfio.h
samples/vfio-mdev/mbochs.c
samples/vfio-mdev/mdpy.c
samples/vfio-mdev/mtty.c

index de675d799c7d80ae767c97716b8afb953d220179..9cd9e9da60ddb28bf02b4449480aabcdee95375c 100644 (file)
@@ -1474,6 +1474,7 @@ static const struct vfio_device_ops intel_vgpu_dev_ops = {
        .bind_iommufd   = vfio_iommufd_emulated_bind,
        .unbind_iommufd = vfio_iommufd_emulated_unbind,
        .attach_ioas    = vfio_iommufd_emulated_attach_ioas,
+       .detach_ioas    = vfio_iommufd_emulated_detach_ioas,
 };
 
 static int intel_vgpu_probe(struct mdev_device *mdev)
index 5b53b94f13c741cc089c98373eaecc9a30e74f34..cba4971618ff6d0be24d1dca4b18695801fb61ab 100644 (file)
@@ -632,6 +632,7 @@ static const struct vfio_device_ops vfio_ccw_dev_ops = {
        .bind_iommufd = vfio_iommufd_emulated_bind,
        .unbind_iommufd = vfio_iommufd_emulated_unbind,
        .attach_ioas = vfio_iommufd_emulated_attach_ioas,
+       .detach_ioas = vfio_iommufd_emulated_detach_ioas,
 };
 
 struct mdev_driver vfio_ccw_mdev_driver = {
index b441745b041845dd344afdfcf1c6f8924387f9e6..2d3c3a79b6873ef90dbf3dff156d25c831f82076 100644 (file)
@@ -1975,6 +1975,7 @@ static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
        .bind_iommufd = vfio_iommufd_emulated_bind,
        .unbind_iommufd = vfio_iommufd_emulated_unbind,
        .attach_ioas = vfio_iommufd_emulated_attach_ioas,
+       .detach_ioas = vfio_iommufd_emulated_detach_ioas,
        .request = vfio_ap_mdev_request
 };
 
index 86df5415759a2a5ba5b0b43fe162ade3e2f1ec3c..4d84904fd927a47ccdf4f20a1a5b00b6b17026ac 100644 (file)
@@ -231,3 +231,16 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
        return 0;
 }
 EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas);
+
+void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev)
+{
+       lockdep_assert_held(&vdev->dev_set->lock);
+
+       if (WARN_ON(!vdev->iommufd_access) ||
+           !vdev->iommufd_attached)
+               return;
+
+       iommufd_access_detach(vdev->iommufd_access);
+       vdev->iommufd_attached = false;
+}
+EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_detach_ioas);
index f2f02273ece196fe7961ba30cd6ddec227aeeba9..24091a7c7bdbd2241e4572c691d5d4a38be34a9e 100644 (file)
@@ -128,6 +128,7 @@ int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
                               struct iommufd_ctx *ictx, u32 *out_device_id);
 void vfio_iommufd_emulated_unbind(struct vfio_device *vdev);
 int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
+void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev);
 #else
 static inline struct iommufd_ctx *
 vfio_iommufd_device_ictx(struct vfio_device *vdev)
@@ -157,6 +158,8 @@ vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx)
        ((void (*)(struct vfio_device *vdev)) NULL)
 #define vfio_iommufd_emulated_attach_ioas \
        ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
+#define vfio_iommufd_emulated_detach_ioas \
+       ((void (*)(struct vfio_device *vdev)) NULL)
 #endif
 
 static inline bool vfio_device_cdev_opened(struct vfio_device *device)
index c6c6b5d26670952096d4cd55fd84e50962fbb2ae..3764d1911b5128677178771d2b0a373d997a5126 100644 (file)
@@ -1377,6 +1377,7 @@ static const struct vfio_device_ops mbochs_dev_ops = {
        .bind_iommufd   = vfio_iommufd_emulated_bind,
        .unbind_iommufd = vfio_iommufd_emulated_unbind,
        .attach_ioas    = vfio_iommufd_emulated_attach_ioas,
+       .detach_ioas    = vfio_iommufd_emulated_detach_ioas,
 };
 
 static struct mdev_driver mbochs_driver = {
index a62ea11e20ecd72fc4218644c1b0b3e9034c6d5b..064e1c0a7aa832640c77fa9694b214f3c1c3693d 100644 (file)
@@ -666,6 +666,7 @@ static const struct vfio_device_ops mdpy_dev_ops = {
        .bind_iommufd   = vfio_iommufd_emulated_bind,
        .unbind_iommufd = vfio_iommufd_emulated_unbind,
        .attach_ioas    = vfio_iommufd_emulated_attach_ioas,
+       .detach_ioas    = vfio_iommufd_emulated_detach_ioas,
 };
 
 static struct mdev_driver mdpy_driver = {
index a60801fb86606817bb669fd0042b7a4e12f4acaf..5af00387c519e245cb3a923d0e5b3b7cc0f8e7cc 100644 (file)
@@ -1272,6 +1272,7 @@ static const struct vfio_device_ops mtty_dev_ops = {
        .bind_iommufd   = vfio_iommufd_emulated_bind,
        .unbind_iommufd = vfio_iommufd_emulated_unbind,
        .attach_ioas    = vfio_iommufd_emulated_attach_ioas,
+       .detach_ioas    = vfio_iommufd_emulated_detach_ioas,
 };
 
 static struct mdev_driver mtty_driver = {