iommufd.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. =======
  3. IOMMUFD
  4. =======
  5. :Author: Jason Gunthorpe
  6. :Author: Kevin Tian
  7. Overview
  8. ========
  9. IOMMUFD is the user API to control the IOMMU subsystem as it relates to managing
  10. IO page tables from userspace using file descriptors. It intends to be general
  11. and consumable by any driver that wants to expose DMA to userspace. These
  12. drivers are eventually expected to deprecate any internal IOMMU logic
  13. they may already/historically implement (e.g. vfio_iommu_type1.c).
  14. At minimum iommufd provides universal support of managing I/O address spaces and
  15. I/O page tables for all IOMMUs, with room in the design to add non-generic
  16. features to cater to specific hardware functionality.
  17. In this context the capital letter (IOMMUFD) refers to the subsystem while the
  18. small letter (iommufd) refers to the file descriptors created via /dev/iommu for
  19. use by userspace.
  20. Key Concepts
  21. ============
  22. User Visible Objects
  23. --------------------
  24. Following IOMMUFD objects are exposed to userspace:
  25. - IOMMUFD_OBJ_IOAS, representing an I/O address space (IOAS), allowing map/unmap
  26. of user space memory into ranges of I/O Virtual Address (IOVA).
  27. The IOAS is a functional replacement for the VFIO container, and like the VFIO
  28. container it copies an IOVA map to a list of iommu_domains held within it.
  29. - IOMMUFD_OBJ_DEVICE, representing a device that is bound to iommufd by an
  30. external driver.
  31. - IOMMUFD_OBJ_HW_PAGETABLE, representing an actual hardware I/O page table
  32. (i.e. a single struct iommu_domain) managed by the iommu driver.
  33. The IOAS has a list of HW_PAGETABLES that share the same IOVA mapping and
  34. it will synchronize its mapping with each member HW_PAGETABLE.
  35. All user-visible objects are destroyed via the IOMMU_DESTROY uAPI.
  36. The diagram below shows relationship between user-visible objects and kernel
  37. datastructures (external to iommufd), with numbers referred to operations
  38. creating the objects and links::
  39. _________________________________________________________
  40. | iommufd |
  41. | [1] |
  42. | _________________ |
  43. | | | |
  44. | | | |
  45. | | | |
  46. | | | |
  47. | | | |
  48. | | | |
  49. | | | [3] [2] |
  50. | | | ____________ __________ |
  51. | | IOAS |<--| |<------| | |
  52. | | | |HW_PAGETABLE| | DEVICE | |
  53. | | | |____________| |__________| |
  54. | | | | | |
  55. | | | | | |
  56. | | | | | |
  57. | | | | | |
  58. | | | | | |
  59. | |_________________| | | |
  60. | | | | |
  61. |_________|___________________|___________________|_______|
  62. | | |
  63. | _____v______ _______v_____
  64. | PFN storage | | | |
  65. |------------>|iommu_domain| |struct device|
  66. |____________| |_____________|
  67. 1. IOMMUFD_OBJ_IOAS is created via the IOMMU_IOAS_ALLOC uAPI. An iommufd can
  68. hold multiple IOAS objects. IOAS is the most generic object and does not
  69. expose interfaces that are specific to single IOMMU drivers. All operations
  70. on the IOAS must operate equally on each of the iommu_domains inside of it.
  71. 2. IOMMUFD_OBJ_DEVICE is created when an external driver calls the IOMMUFD kAPI
  72. to bind a device to an iommufd. The driver is expected to implement a set of
  73. ioctls to allow userspace to initiate the binding operation. Successful
  74. completion of this operation establishes the desired DMA ownership over the
  75. device. The driver must also set the driver_managed_dma flag and must not
  76. touch the device until this operation succeeds.
  77. 3. IOMMUFD_OBJ_HW_PAGETABLE is created when an external driver calls the IOMMUFD
  78. kAPI to attach a bound device to an IOAS. Similarly the external driver uAPI
  79. allows userspace to initiate the attaching operation. If a compatible
  80. pagetable already exists then it is reused for the attachment. Otherwise a
  81. new pagetable object and iommu_domain is created. Successful completion of
  82. this operation sets up the linkages among IOAS, device and iommu_domain. Once
  83. this completes the device could do DMA.
  84. Every iommu_domain inside the IOAS is also represented to userspace as a
  85. HW_PAGETABLE object.
  86. .. note::
  87. Future IOMMUFD updates will provide an API to create and manipulate the
  88. HW_PAGETABLE directly.
  89. A device can only bind to an iommufd due to DMA ownership claim and attach to at
  90. most one IOAS object (no support of PASID yet).
  91. Kernel Datastructure
  92. --------------------
  93. User visible objects are backed by following datastructures:
  94. - iommufd_ioas for IOMMUFD_OBJ_IOAS.
  95. - iommufd_device for IOMMUFD_OBJ_DEVICE.
  96. - iommufd_hw_pagetable for IOMMUFD_OBJ_HW_PAGETABLE.
  97. Several terminologies when looking at these datastructures:
  98. - Automatic domain - refers to an iommu domain created automatically when
  99. attaching a device to an IOAS object. This is compatible to the semantics of
  100. VFIO type1.
  101. - Manual domain - refers to an iommu domain designated by the user as the
  102. target pagetable to be attached to by a device. Though currently there are
  103. no uAPIs to directly create such domain, the datastructure and algorithms
  104. are ready for handling that use case.
  105. - In-kernel user - refers to something like a VFIO mdev that is using the
  106. IOMMUFD access interface to access the IOAS. This starts by creating an
  107. iommufd_access object that is similar to the domain binding a physical device
  108. would do. The access object will then allow converting IOVA ranges into struct
  109. page * lists, or doing direct read/write to an IOVA.
  110. iommufd_ioas serves as the metadata datastructure to manage how IOVA ranges are
  111. mapped to memory pages, composed of:
  112. - struct io_pagetable holding the IOVA map
  113. - struct iopt_area's representing populated portions of IOVA
  114. - struct iopt_pages representing the storage of PFNs
  115. - struct iommu_domain representing the IO page table in the IOMMU
  116. - struct iopt_pages_access representing in-kernel users of PFNs
  117. - struct xarray pinned_pfns holding a list of pages pinned by in-kernel users
  118. Each iopt_pages represents a logical linear array of full PFNs. The PFNs are
  119. ultimately derived from userspace VAs via an mm_struct. Once they have been
  120. pinned the PFNs are stored in IOPTEs of an iommu_domain or inside the pinned_pfns
  121. xarray if they have been pinned through an iommufd_access.
  122. PFN have to be copied between all combinations of storage locations, depending
  123. on what domains are present and what kinds of in-kernel "software access" users
  124. exist. The mechanism ensures that a page is pinned only once.
  125. An io_pagetable is composed of iopt_areas pointing at iopt_pages, along with a
  126. list of iommu_domains that mirror the IOVA to PFN map.
  127. Multiple io_pagetable-s, through their iopt_area-s, can share a single
  128. iopt_pages which avoids multi-pinning and double accounting of page
  129. consumption.
  130. iommufd_ioas is shareable between subsystems, e.g. VFIO and VDPA, as long as
  131. devices managed by different subsystems are bound to a same iommufd.
  132. IOMMUFD User API
  133. ================
  134. .. kernel-doc:: include/uapi/linux/iommufd.h
  135. IOMMUFD Kernel API
  136. ==================
  137. The IOMMUFD kAPI is device-centric with group-related tricks managed behind the
  138. scene. This allows the external drivers calling such kAPI to implement a simple
  139. device-centric uAPI for connecting its device to an iommufd, instead of
  140. explicitly imposing the group semantics in its uAPI as VFIO does.
  141. .. kernel-doc:: drivers/iommu/iommufd/device.c
  142. :export:
  143. .. kernel-doc:: drivers/iommu/iommufd/main.c
  144. :export:
  145. VFIO and IOMMUFD
  146. ----------------
  147. Connecting a VFIO device to iommufd can be done in two ways.
  148. First is a VFIO compatible way by directly implementing the /dev/vfio/vfio
  149. container IOCTLs by mapping them into io_pagetable operations. Doing so allows
  150. the use of iommufd in legacy VFIO applications by symlinking /dev/vfio/vfio to
  151. /dev/iommufd or extending VFIO to SET_CONTAINER using an iommufd instead of a
  152. container fd.
  153. The second approach directly extends VFIO to support a new set of device-centric
  154. user API based on aforementioned IOMMUFD kernel API. It requires userspace
  155. change but better matches the IOMMUFD API semantics and easier to support new
  156. iommufd features when comparing it to the first approach.
  157. Currently both approaches are still work-in-progress.
  158. There are still a few gaps to be resolved to catch up with VFIO type1, as
  159. documented in iommufd_vfio_check_extension().
  160. Future TODOs
  161. ============
  162. Currently IOMMUFD supports only kernel-managed I/O page table, similar to VFIO
  163. type1. New features on the radar include:
  164. - Binding iommu_domain's to PASID/SSID
  165. - Userspace page tables, for ARM, x86 and S390
  166. - Kernel bypass'd invalidation of user page tables
  167. - Re-use of the KVM page table in the IOMMU
  168. - Dirty page tracking in the IOMMU
  169. - Runtime Increase/Decrease of IOPTE size
  170. - PRI support with faults resolved in userspace