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);
22 * If the driver doesn't provide this op then it means the device does
23 * not do DMA at all. So nothing to do.
25 if (!vdev->ops->bind_iommufd)
28 ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id);
32 ret = iommufd_vfio_compat_ioas_id(ictx, &ioas_id);
35 ret = vdev->ops->attach_ioas(vdev, &ioas_id);
40 * The legacy path has no way to return the device id or the selected
46 if (vdev->ops->unbind_iommufd)
47 vdev->ops->unbind_iommufd(vdev);
51 void vfio_iommufd_unbind(struct vfio_device *vdev)
53 lockdep_assert_held(&vdev->dev_set->lock);
55 if (vdev->ops->unbind_iommufd)
56 vdev->ops->unbind_iommufd(vdev);
60 * The physical standard ops mean that the iommufd_device is bound to the
61 * physical device vdev->dev that was provided to vfio_init_group_dev(). Drivers
62 * using this ops set should call vfio_register_group_dev()
64 int vfio_iommufd_physical_bind(struct vfio_device *vdev,
65 struct iommufd_ctx *ictx, u32 *out_device_id)
67 struct iommufd_device *idev;
69 idev = iommufd_device_bind(ictx, vdev->dev, out_device_id);
72 vdev->iommufd_device = idev;
75 EXPORT_SYMBOL_GPL(vfio_iommufd_physical_bind);
77 void vfio_iommufd_physical_unbind(struct vfio_device *vdev)
79 lockdep_assert_held(&vdev->dev_set->lock);
81 if (vdev->iommufd_attached) {
82 iommufd_device_detach(vdev->iommufd_device);
83 vdev->iommufd_attached = false;
85 iommufd_device_unbind(vdev->iommufd_device);
86 vdev->iommufd_device = NULL;
88 EXPORT_SYMBOL_GPL(vfio_iommufd_physical_unbind);
90 int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
94 rc = iommufd_device_attach(vdev->iommufd_device, pt_id);
97 vdev->iommufd_attached = true;
100 EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas);
103 * The emulated standard ops mean that vfio_device is going to use the
104 * "mdev path" and will call vfio_pin_pages()/vfio_dma_rw(). Drivers using this
105 * ops set should call vfio_register_emulated_iommu_dev().
108 static void vfio_emulated_unmap(void *data, unsigned long iova,
109 unsigned long length)
111 struct vfio_device *vdev = data;
113 vdev->ops->dma_unmap(vdev, iova, length);
116 static const struct iommufd_access_ops vfio_user_ops = {
117 .needs_pin_pages = 1,
118 .unmap = vfio_emulated_unmap,
121 int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
122 struct iommufd_ctx *ictx, u32 *out_device_id)
124 lockdep_assert_held(&vdev->dev_set->lock);
126 vdev->iommufd_ictx = ictx;
127 iommufd_ctx_get(ictx);
130 EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_bind);
132 void vfio_iommufd_emulated_unbind(struct vfio_device *vdev)
134 lockdep_assert_held(&vdev->dev_set->lock);
136 if (vdev->iommufd_access) {
137 iommufd_access_destroy(vdev->iommufd_access);
138 vdev->iommufd_access = NULL;
140 iommufd_ctx_put(vdev->iommufd_ictx);
141 vdev->iommufd_ictx = NULL;
143 EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_unbind);
145 int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
147 struct iommufd_access *user;
149 lockdep_assert_held(&vdev->dev_set->lock);
151 user = iommufd_access_create(vdev->iommufd_ictx, *pt_id, &vfio_user_ops,
154 return PTR_ERR(user);
155 vdev->iommufd_access = user;
158 EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas);