Skip to content

Commit 0a782d1

Browse files
yiliu1765jgunthorpe
authored andcommitted
vfio/mdev: Uses the vfio emulated iommufd ops set in the mdev sample drivers
This harmonizes the no-DMA devices (the vfio-mdev sample drivers) with the emulated devices (gvt-g, vfio-ap etc.). It makes it easier to add BIND_IOMMUFD user interface which requires to return an iommufd ID to represent the device/iommufd bond. Link: https://lore.kernel.org/r/20230327093351.44505-6-yi.l.liu@intel.com Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Tested-by: Terrence Xu <terrence.xu@intel.com> Tested-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> Acked-by: Alex Williamson <alex.williamson@redhat.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 632fda7 commit 0a782d1

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

drivers/vfio/iommufd.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
3232
return 0;
3333
}
3434

35-
/*
36-
* If the driver doesn't provide this op then it means the device does
37-
* not do DMA at all. So nothing to do.
38-
*/
39-
if (!vdev->ops->bind_iommufd)
40-
return 0;
35+
if (WARN_ON(!vdev->ops->bind_iommufd))
36+
return -ENODEV;
4137

4238
ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id);
4339
if (ret)
@@ -119,15 +115,17 @@ EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas);
119115
/*
120116
* The emulated standard ops mean that vfio_device is going to use the
121117
* "mdev path" and will call vfio_pin_pages()/vfio_dma_rw(). Drivers using this
122-
* ops set should call vfio_register_emulated_iommu_dev().
118+
* ops set should call vfio_register_emulated_iommu_dev(). Drivers that do
119+
* not call vfio_pin_pages()/vfio_dma_rw() have no need to provide dma_unmap.
123120
*/
124121

125122
static void vfio_emulated_unmap(void *data, unsigned long iova,
126123
unsigned long length)
127124
{
128125
struct vfio_device *vdev = data;
129126

130-
vdev->ops->dma_unmap(vdev, iova, length);
127+
if (vdev->ops->dma_unmap)
128+
vdev->ops->dma_unmap(vdev, iova, length);
131129
}
132130

133131
static const struct iommufd_access_ops vfio_user_ops = {

samples/vfio-mdev/mbochs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,9 @@ static const struct vfio_device_ops mbochs_dev_ops = {
13741374
.write = mbochs_write,
13751375
.ioctl = mbochs_ioctl,
13761376
.mmap = mbochs_mmap,
1377+
.bind_iommufd = vfio_iommufd_emulated_bind,
1378+
.unbind_iommufd = vfio_iommufd_emulated_unbind,
1379+
.attach_ioas = vfio_iommufd_emulated_attach_ioas,
13771380
};
13781381

13791382
static struct mdev_driver mbochs_driver = {

samples/vfio-mdev/mdpy.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,9 @@ static const struct vfio_device_ops mdpy_dev_ops = {
663663
.write = mdpy_write,
664664
.ioctl = mdpy_ioctl,
665665
.mmap = mdpy_mmap,
666+
.bind_iommufd = vfio_iommufd_emulated_bind,
667+
.unbind_iommufd = vfio_iommufd_emulated_unbind,
668+
.attach_ioas = vfio_iommufd_emulated_attach_ioas,
666669
};
667670

668671
static struct mdev_driver mdpy_driver = {

samples/vfio-mdev/mtty.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,9 @@ static const struct vfio_device_ops mtty_dev_ops = {
12691269
.read = mtty_read,
12701270
.write = mtty_write,
12711271
.ioctl = mtty_ioctl,
1272+
.bind_iommufd = vfio_iommufd_emulated_bind,
1273+
.unbind_iommufd = vfio_iommufd_emulated_unbind,
1274+
.attach_ioas = vfio_iommufd_emulated_attach_ioas,
12721275
};
12731276

12741277
static struct mdev_driver mtty_driver = {

0 commit comments

Comments
 (0)