iommufd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
  4. */
  5. #include <linux/vfio.h>
  6. #include <linux/iommufd.h>
  7. #include "vfio.h"
  8. MODULE_IMPORT_NS(IOMMUFD);
  9. MODULE_IMPORT_NS(IOMMUFD_VFIO);
  10. bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev,
  11. struct iommufd_ctx *ictx)
  12. {
  13. u32 ioas_id;
  14. return !iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id);
  15. }
  16. int vfio_df_iommufd_bind(struct vfio_device_file *df)
  17. {
  18. struct vfio_device *vdev = df->device;
  19. struct iommufd_ctx *ictx = df->iommufd;
  20. lockdep_assert_held(&vdev->dev_set->lock);
  21. /* Returns 0 to permit device opening under noiommu mode */
  22. if (vfio_device_is_noiommu(vdev))
  23. return 0;
  24. return vdev->ops->bind_iommufd(vdev, ictx, &df->devid);
  25. }
  26. int vfio_iommufd_compat_attach_ioas(struct vfio_device *vdev,
  27. struct iommufd_ctx *ictx)
  28. {
  29. u32 ioas_id;
  30. int ret;
  31. lockdep_assert_held(&vdev->dev_set->lock);
  32. /* compat noiommu does not need to do ioas attach */
  33. if (vfio_device_is_noiommu(vdev))
  34. return 0;
  35. ret = iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id);
  36. if (ret)
  37. return ret;
  38. /* The legacy path has no way to return the selected pt_id */
  39. return vdev->ops->attach_ioas(vdev, &ioas_id);
  40. }
  41. void vfio_df_iommufd_unbind(struct vfio_device_file *df)
  42. {
  43. struct vfio_device *vdev = df->device;
  44. lockdep_assert_held(&vdev->dev_set->lock);
  45. if (vfio_device_is_noiommu(vdev))
  46. return;
  47. if (vdev->ops->unbind_iommufd)
  48. vdev->ops->unbind_iommufd(vdev);
  49. }
  50. struct iommufd_ctx *vfio_iommufd_device_ictx(struct vfio_device *vdev)
  51. {
  52. if (vdev->iommufd_device)
  53. return iommufd_device_to_ictx(vdev->iommufd_device);
  54. return NULL;
  55. }
  56. EXPORT_SYMBOL_GPL(vfio_iommufd_device_ictx);
  57. static int vfio_iommufd_device_id(struct vfio_device *vdev)
  58. {
  59. if (vdev->iommufd_device)
  60. return iommufd_device_to_id(vdev->iommufd_device);
  61. return -EINVAL;
  62. }
  63. /*
  64. * Return devid for a device.
  65. * valid ID for the device that is owned by the ictx
  66. * -ENOENT = device is owned but there is no ID
  67. * -ENODEV or other error = device is not owned
  68. */
  69. int vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx)
  70. {
  71. struct iommu_group *group;
  72. int devid;
  73. if (vfio_iommufd_device_ictx(vdev) == ictx)
  74. return vfio_iommufd_device_id(vdev);
  75. group = iommu_group_get(vdev->dev);
  76. if (!group)
  77. return -ENODEV;
  78. if (iommufd_ctx_has_group(ictx, group))
  79. devid = -ENOENT;
  80. else
  81. devid = -ENODEV;
  82. iommu_group_put(group);
  83. return devid;
  84. }
  85. EXPORT_SYMBOL_GPL(vfio_iommufd_get_dev_id);
  86. /*
  87. * The physical standard ops mean that the iommufd_device is bound to the
  88. * physical device vdev->dev that was provided to vfio_init_group_dev(). Drivers
  89. * using this ops set should call vfio_register_group_dev()
  90. */
  91. int vfio_iommufd_physical_bind(struct vfio_device *vdev,
  92. struct iommufd_ctx *ictx, u32 *out_device_id)
  93. {
  94. struct iommufd_device *idev;
  95. idev = iommufd_device_bind(ictx, vdev->dev, out_device_id);
  96. if (IS_ERR(idev))
  97. return PTR_ERR(idev);
  98. vdev->iommufd_device = idev;
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(vfio_iommufd_physical_bind);
  102. void vfio_iommufd_physical_unbind(struct vfio_device *vdev)
  103. {
  104. lockdep_assert_held(&vdev->dev_set->lock);
  105. if (vdev->iommufd_attached) {
  106. iommufd_device_detach(vdev->iommufd_device);
  107. vdev->iommufd_attached = false;
  108. }
  109. iommufd_device_unbind(vdev->iommufd_device);
  110. vdev->iommufd_device = NULL;
  111. }
  112. EXPORT_SYMBOL_GPL(vfio_iommufd_physical_unbind);
  113. int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
  114. {
  115. int rc;
  116. lockdep_assert_held(&vdev->dev_set->lock);
  117. if (WARN_ON(!vdev->iommufd_device))
  118. return -EINVAL;
  119. if (vdev->iommufd_attached)
  120. rc = iommufd_device_replace(vdev->iommufd_device, pt_id);
  121. else
  122. rc = iommufd_device_attach(vdev->iommufd_device, pt_id);
  123. if (rc)
  124. return rc;
  125. vdev->iommufd_attached = true;
  126. return 0;
  127. }
  128. EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas);
  129. void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev)
  130. {
  131. lockdep_assert_held(&vdev->dev_set->lock);
  132. if (WARN_ON(!vdev->iommufd_device) || !vdev->iommufd_attached)
  133. return;
  134. iommufd_device_detach(vdev->iommufd_device);
  135. vdev->iommufd_attached = false;
  136. }
  137. EXPORT_SYMBOL_GPL(vfio_iommufd_physical_detach_ioas);
  138. /*
  139. * The emulated standard ops mean that vfio_device is going to use the
  140. * "mdev path" and will call vfio_pin_pages()/vfio_dma_rw(). Drivers using this
  141. * ops set should call vfio_register_emulated_iommu_dev(). Drivers that do
  142. * not call vfio_pin_pages()/vfio_dma_rw() have no need to provide dma_unmap.
  143. */
  144. static void vfio_emulated_unmap(void *data, unsigned long iova,
  145. unsigned long length)
  146. {
  147. struct vfio_device *vdev = data;
  148. if (vdev->ops->dma_unmap)
  149. vdev->ops->dma_unmap(vdev, iova, length);
  150. }
  151. static const struct iommufd_access_ops vfio_user_ops = {
  152. .needs_pin_pages = 1,
  153. .unmap = vfio_emulated_unmap,
  154. };
  155. int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
  156. struct iommufd_ctx *ictx, u32 *out_device_id)
  157. {
  158. struct iommufd_access *user;
  159. lockdep_assert_held(&vdev->dev_set->lock);
  160. user = iommufd_access_create(ictx, &vfio_user_ops, vdev, out_device_id);
  161. if (IS_ERR(user))
  162. return PTR_ERR(user);
  163. vdev->iommufd_access = user;
  164. return 0;
  165. }
  166. EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_bind);
  167. void vfio_iommufd_emulated_unbind(struct vfio_device *vdev)
  168. {
  169. lockdep_assert_held(&vdev->dev_set->lock);
  170. if (vdev->iommufd_access) {
  171. iommufd_access_destroy(vdev->iommufd_access);
  172. vdev->iommufd_attached = false;
  173. vdev->iommufd_access = NULL;
  174. }
  175. }
  176. EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_unbind);
  177. int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id)
  178. {
  179. int rc;
  180. lockdep_assert_held(&vdev->dev_set->lock);
  181. if (vdev->iommufd_attached)
  182. rc = iommufd_access_replace(vdev->iommufd_access, *pt_id);
  183. else
  184. rc = iommufd_access_attach(vdev->iommufd_access, *pt_id);
  185. if (rc)
  186. return rc;
  187. vdev->iommufd_attached = true;
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas);
  191. void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev)
  192. {
  193. lockdep_assert_held(&vdev->dev_set->lock);
  194. if (WARN_ON(!vdev->iommufd_access) ||
  195. !vdev->iommufd_attached)
  196. return;
  197. iommufd_access_detach(vdev->iommufd_access);
  198. vdev->iommufd_attached = false;
  199. }
  200. EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_detach_ioas);