1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
5 #include <linux/vfio.h>
6 #include <linux/iommufd.h>
10 MODULE_IMPORT_NS(IOMMUFD);
11 MODULE_IMPORT_NS(IOMMUFD_VFIO);
13 int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
19 lockdep_assert_held(&vdev->dev_set->lock);
21 if (vfio_device_is_noiommu(vdev)) {
22 if (!capable(CAP_SYS_RAWIO))
26 * Require no compat ioas to be assigned to proceed. The basic
27 * statement is that the user cannot have done something that
28 * implies they expected translation to exist
30 if (!iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id))
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.
39 if (!vdev->ops->bind_iommufd)
42 ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id);
46 ret = iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id);
49 ret = vdev->ops->attach_ioas(vdev, &ioas_id);
54 * The legacy path has no way to return the device id or the selected
60 if (vdev->ops->unbind_iommufd)
61 vdev->ops->unbind_iommufd(vdev);
65 void vfio_iommufd_unbind(struct vfio_device *vdev)
67 lockdep_assert_held(&vdev->dev_set->lock);
69 if (vfio_device_is_noiommu(vdev))
72 if (vdev->ops->unbind_iommufd)
73 vdev->ops->unbind_iommufd(vdev);
77 * The physical standard ops mean that the iommufd_device is bound to the
78 * physical device vdev->dev that was provided to vfio_init_group_dev(). Drivers
79 * using this ops set should call vfio_register_group_dev()
81 int vfio_iommufd_physical_bind(struct vfio_device *vdev,
82 struct iommufd_ctx *ictx, u32 *out_device_id)
84 struct iommufd_device *idev;
86 idev = iommufd_device_bind(ictx, vdev->dev, out_device_id);
89 vdev->iommufd_device = idev;
92 EXPORT_SYMBOL_GPL(vfio_iommufd_physical_bind);
94 void vfio_iommufd_physical_unbind(struct vfio_device *vdev)
96 lockdep_assert_held(&vdev->dev_set->lock);
98 if (vdev->iommufd_attached) {
99 iommufd_device_detach(vdev->iommufd_device);
100 vdev->iommufd_attached = false;
102 iommufd_device_unbind(vdev->iommufd_device);
103 vdev->iommufd_device = NULL;
105 EXPORT_SYMBOL_GPL(vfio_iommufd_physical_unbind);
107 int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
111 rc = iommufd_device_attach(vdev->iommufd_device, pt_id);
114 vdev->iommufd_attached = true;
117 EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas);
120 * The emulated standard ops mean that vfio_device is going to use the
121 * "mdev path" and will call vfio_pin_pages()/vfio_dma_rw(). Drivers using this
122 * ops set should call vfio_register_emulated_iommu_dev().
125 static void vfio_emulated_unmap(void *data, unsigned long iova,
126 unsigned long length)
128 struct vfio_device *vdev = data;
130 vdev->ops->dma_unmap(vdev, iova, length);
133 static const struct iommufd_access_ops vfio_user_ops = {
134 .needs_pin_pages = 1,
135 .unmap = vfio_emulated_unmap,
138 int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
139 struct iommufd_ctx *ictx, u32 *out_device_id)
141 lockdep_assert_held(&vdev->dev_set->lock);
143 vdev->iommufd_ictx = ictx;
144 iommufd_ctx_get(ictx);
147 EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_bind);
149 void vfio_iommufd_emulated_unbind(struct vfio_device *vdev)
151 lockdep_assert_held(&vdev->dev_set->lock);
153 if (vdev->iommufd_access) {
154 iommufd_access_destroy(vdev->iommufd_access);
155 vdev->iommufd_access = NULL;
157 iommufd_ctx_put(vdev->iommufd_ictx);
158 vdev->iommufd_ictx = NULL;
160 EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_unbind);
162 int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
164 struct iommufd_access *user;
166 lockdep_assert_held(&vdev->dev_set->lock);
168 user = iommufd_access_create(vdev->iommufd_ictx, *pt_id, &vfio_user_ops,
171 return PTR_ERR(user);
172 vdev->iommufd_access = user;
175 EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas);