iommufd.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_HWPT_PAGING, representing an actual hardware I/O page table
  32. (i.e. a single struct iommu_domain) managed by the iommu driver. "PAGING"
  33. primarily indicates this type of HWPT should be linked to an IOAS. It also
  34. indicates that it is backed by an iommu_domain with __IOMMU_DOMAIN_PAGING
  35. feature flag. This can be either an UNMANAGED stage-1 domain for a device
  36. running in the user space, or a nesting parent stage-2 domain for mappings
  37. from guest-level physical addresses to host-level physical addresses.
  38. The IOAS has a list of HWPT_PAGINGs that share the same IOVA mapping and
  39. it will synchronize its mapping with each member HWPT_PAGING.
  40. - IOMMUFD_OBJ_HWPT_NESTED, representing an actual hardware I/O page table
  41. (i.e. a single struct iommu_domain) managed by user space (e.g. guest OS).
  42. "NESTED" indicates that this type of HWPT should be linked to an HWPT_PAGING.
  43. It also indicates that it is backed by an iommu_domain that has a type of
  44. IOMMU_DOMAIN_NESTED. This must be a stage-1 domain for a device running in
  45. the user space (e.g. in a guest VM enabling the IOMMU nested translation
  46. feature.) As such, it must be created with a given nesting parent stage-2
  47. domain to associate to. This nested stage-1 page table managed by the user
  48. space usually has mappings from guest-level I/O virtual addresses to guest-
  49. level physical addresses.
  50. - IOMMUFD_FAULT, representing a software queue for an HWPT reporting IO page
  51. faults using the IOMMU HW's PRI (Page Request Interface). This queue object
  52. provides user space an FD to poll the page fault events and also to respond
  53. to those events. A FAULT object must be created first to get a fault_id that
  54. could be then used to allocate a fault-enabled HWPT via the IOMMU_HWPT_ALLOC
  55. command by setting the IOMMU_HWPT_FAULT_ID_VALID bit in its flags field.
  56. - IOMMUFD_OBJ_VIOMMU, representing a slice of the physical IOMMU instance,
  57. passed to or shared with a VM. It may be some HW-accelerated virtualization
  58. features and some SW resources used by the VM. For examples:
  59. * Security namespace for guest owned ID, e.g. guest-controlled cache tags
  60. * Non-device-affiliated event reporting, e.g. invalidation queue errors
  61. * Access to a shareable nesting parent pagetable across physical IOMMUs
  62. * Virtualization of various platforms IDs, e.g. RIDs and others
  63. * Delivery of paravirtualized invalidation
  64. * Direct assigned invalidation queues
  65. * Direct assigned interrupts
  66. Such a vIOMMU object generally has the access to a nesting parent pagetable
  67. to support some HW-accelerated virtualization features. So, a vIOMMU object
  68. must be created given a nesting parent HWPT_PAGING object, and then it would
  69. encapsulate that HWPT_PAGING object. Therefore, a vIOMMU object can be used
  70. to allocate an HWPT_NESTED object in place of the encapsulated HWPT_PAGING.
  71. .. note::
  72. The name "vIOMMU" isn't necessarily identical to a virtualized IOMMU in a
  73. VM. A VM can have one giant virtualized IOMMU running on a machine having
  74. multiple physical IOMMUs, in which case the VMM will dispatch the requests
  75. or configurations from this single virtualized IOMMU instance to multiple
  76. vIOMMU objects created for individual slices of different physical IOMMUs.
  77. In other words, a vIOMMU object is always a representation of one physical
  78. IOMMU, not necessarily of a virtualized IOMMU. For VMMs that want the full
  79. virtualization features from physical IOMMUs, it is suggested to build the
  80. same number of virtualized IOMMUs as the number of physical IOMMUs, so the
  81. passed-through devices would be connected to their own virtualized IOMMUs
  82. backed by corresponding vIOMMU objects, in which case a guest OS would do
  83. the "dispatch" naturally instead of VMM trappings.
  84. - IOMMUFD_OBJ_VDEVICE, representing a virtual device for an IOMMUFD_OBJ_DEVICE
  85. against an IOMMUFD_OBJ_VIOMMU. This virtual device holds the device's virtual
  86. information or attributes (related to the vIOMMU) in a VM. An immediate vDATA
  87. example can be the virtual ID of the device on a vIOMMU, which is a unique ID
  88. that VMM assigns to the device for a translation channel/port of the vIOMMU,
  89. e.g. vSID of ARM SMMUv3, vDeviceID of AMD IOMMU, and vRID of Intel VT-d to a
  90. Context Table. Potential use cases of some advanced security information can
  91. be forwarded via this object too, such as security level or realm information
  92. in a Confidential Compute Architecture. A VMM should create a vDEVICE object
  93. to forward all the device information in a VM, when it connects a device to a
  94. vIOMMU, which is a separate ioctl call from attaching the same device to an
  95. HWPT_PAGING that the vIOMMU holds.
  96. - IOMMUFD_OBJ_VEVENTQ, representing a software queue for a vIOMMU to report its
  97. events such as translation faults occurred to a nested stage-1 (excluding I/O
  98. page faults that should go through IOMMUFD_OBJ_FAULT) and HW-specific events.
  99. This queue object provides user space an FD to poll/read the vIOMMU events. A
  100. vIOMMU object must be created first to get its viommu_id, which could be then
  101. used to allocate a vEVENTQ. Each vIOMMU can support multiple types of vEVENTS,
  102. but is confined to one vEVENTQ per vEVENTQ type.
  103. - IOMMUFD_OBJ_HW_QUEUE, representing a hardware accelerated queue, as a subset
  104. of IOMMU's virtualization features, for the IOMMU HW to directly read or write
  105. the virtual queue memory owned by a guest OS. This HW-acceleration feature can
  106. allow VM to work with the IOMMU HW directly without a VM Exit, so as to reduce
  107. overhead from the hypercalls. Along with the HW QUEUE object, iommufd provides
  108. user space an mmap interface for VMM to mmap a physical MMIO region from the
  109. host physical address space to the guest physical address space, allowing the
  110. guest OS to directly control the allocated HW QUEUE. Thus, when allocating a
  111. HW QUEUE, the VMM must request a pair of mmap info (offset/length) and pass in
  112. exactly to an mmap syscall via its offset and length arguments.
  113. All user-visible objects are destroyed via the IOMMU_DESTROY uAPI.
  114. The diagrams below show relationships between user-visible objects and kernel
  115. datastructures (external to iommufd), with numbers referred to operations
  116. creating the objects and links::
  117. _______________________________________________________________________
  118. | iommufd (HWPT_PAGING only) |
  119. | |
  120. | [1] [3] [2] |
  121. | ________________ _____________ ________ |
  122. | | | | | | | |
  123. | | IOAS |<---| HWPT_PAGING |<---------------------| DEVICE | |
  124. | |________________| |_____________| |________| |
  125. | | | | |
  126. |_________|____________________|__________________________________|_____|
  127. | | |
  128. | ______v_____ ___v__
  129. | PFN storage | (paging) | |struct|
  130. |------------>|iommu_domain|<-----------------------|device|
  131. |____________| |______|
  132. _______________________________________________________________________
  133. | iommufd (with HWPT_NESTED) |
  134. | |
  135. | [1] [3] [4] [2] |
  136. | ________________ _____________ _____________ ________ |
  137. | | | | | | | | | |
  138. | | IOAS |<---| HWPT_PAGING |<---| HWPT_NESTED |<--| DEVICE | |
  139. | |________________| |_____________| |_____________| |________| |
  140. | | | | | |
  141. |_________|____________________|__________________|_______________|_____|
  142. | | | |
  143. | ______v_____ ______v_____ ___v__
  144. | PFN storage | (paging) | | (nested) | |struct|
  145. |------------>|iommu_domain|<----|iommu_domain|<----|device|
  146. |____________| |____________| |______|
  147. _______________________________________________________________________
  148. | iommufd (with vIOMMU/vDEVICE) |
  149. | |
  150. | [5] [6] |
  151. | _____________ _____________ |
  152. | | | | | |
  153. | |----------------| vIOMMU |<---| vDEVICE |<----| |
  154. | | | | |_____________| | |
  155. | | | | | |
  156. | | [1] | | [4] | [2] |
  157. | | ______ | | _____________ _|______ |
  158. | | | | | [3] | | | | | |
  159. | | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | |
  160. | | |______| |_____________| |_____________| |________| |
  161. | | | | | | |
  162. |______|________|______________|__________________|_______________|_____|
  163. | | | | |
  164. ______v_____ | ______v_____ ______v_____ ___v__
  165. | struct | | PFN | (paging) | | (nested) | |struct|
  166. |iommu_device| |------>|iommu_domain|<----|iommu_domain|<----|device|
  167. |____________| storage|____________| |____________| |______|
  168. 1. IOMMUFD_OBJ_IOAS is created via the IOMMU_IOAS_ALLOC uAPI. An iommufd can
  169. hold multiple IOAS objects. IOAS is the most generic object and does not
  170. expose interfaces that are specific to single IOMMU drivers. All operations
  171. on the IOAS must operate equally on each of the iommu_domains inside of it.
  172. 2. IOMMUFD_OBJ_DEVICE is created when an external driver calls the IOMMUFD kAPI
  173. to bind a device to an iommufd. The driver is expected to implement a set of
  174. ioctls to allow userspace to initiate the binding operation. Successful
  175. completion of this operation establishes the desired DMA ownership over the
  176. device. The driver must also set the driver_managed_dma flag and must not
  177. touch the device until this operation succeeds.
  178. 3. IOMMUFD_OBJ_HWPT_PAGING can be created in two ways:
  179. * IOMMUFD_OBJ_HWPT_PAGING is automatically created when an external driver
  180. calls the IOMMUFD kAPI to attach a bound device to an IOAS. Similarly the
  181. external driver uAPI allows userspace to initiate the attaching operation.
  182. If a compatible member HWPT_PAGING object exists in the IOAS's HWPT_PAGING
  183. list, then it will be reused. Otherwise a new HWPT_PAGING that represents
  184. an iommu_domain to userspace will be created, and then added to the list.
  185. Successful completion of this operation sets up the linkages among IOAS,
  186. device and iommu_domain. Once this completes the device could do DMA.
  187. * IOMMUFD_OBJ_HWPT_PAGING can be manually created via the IOMMU_HWPT_ALLOC
  188. uAPI, provided an ioas_id via @pt_id to associate the new HWPT_PAGING to
  189. the corresponding IOAS object. The benefit of this manual allocation is to
  190. allow allocation flags (defined in enum iommufd_hwpt_alloc_flags), e.g. it
  191. allocates a nesting parent HWPT_PAGING if the IOMMU_HWPT_ALLOC_NEST_PARENT
  192. flag is set.
  193. 4. IOMMUFD_OBJ_HWPT_NESTED can be only manually created via the IOMMU_HWPT_ALLOC
  194. uAPI, provided an hwpt_id or a viommu_id of a vIOMMU object encapsulating a
  195. nesting parent HWPT_PAGING via @pt_id to associate the new HWPT_NESTED object
  196. to the corresponding HWPT_PAGING object. The associating HWPT_PAGING object
  197. must be a nesting parent manually allocated via the same uAPI previously with
  198. an IOMMU_HWPT_ALLOC_NEST_PARENT flag, otherwise the allocation will fail. The
  199. allocation will be further validated by the IOMMU driver to ensure that the
  200. nesting parent domain and the nested domain being allocated are compatible.
  201. Successful completion of this operation sets up linkages among IOAS, device,
  202. and iommu_domains. Once this completes the device could do DMA via a 2-stage
  203. translation, a.k.a nested translation. Note that multiple HWPT_NESTED objects
  204. can be allocated by (and then associated to) the same nesting parent.
  205. .. note::
  206. Either a manual IOMMUFD_OBJ_HWPT_PAGING or an IOMMUFD_OBJ_HWPT_NESTED is
  207. created via the same IOMMU_HWPT_ALLOC uAPI. The difference is at the type
  208. of the object passed in via the @pt_id field of struct iommufd_hwpt_alloc.
  209. 5. IOMMUFD_OBJ_VIOMMU can be only manually created via the IOMMU_VIOMMU_ALLOC
  210. uAPI, provided a dev_id (for the device's physical IOMMU to back the vIOMMU)
  211. and an hwpt_id (to associate the vIOMMU to a nesting parent HWPT_PAGING). The
  212. iommufd core will link the vIOMMU object to the struct iommu_device that the
  213. struct device is behind. And an IOMMU driver can implement a viommu_alloc op
  214. to allocate its own vIOMMU data structure embedding the core-level structure
  215. iommufd_viommu and some driver-specific data. If necessary, the driver can
  216. also configure its HW virtualization feature for that vIOMMU (and thus for
  217. the VM). Successful completion of this operation sets up the linkages between
  218. the vIOMMU object and the HWPT_PAGING, then this vIOMMU object can be used
  219. as a nesting parent object to allocate an HWPT_NESTED object described above.
  220. 6. IOMMUFD_OBJ_VDEVICE can be only manually created via the IOMMU_VDEVICE_ALLOC
  221. uAPI, provided a viommu_id for an iommufd_viommu object and a dev_id for an
  222. iommufd_device object. The vDEVICE object will be the binding between these
  223. two parent objects. Another @virt_id will be also set via the uAPI providing
  224. the iommufd core an index to store the vDEVICE object to a vDEVICE array per
  225. vIOMMU. If necessary, the IOMMU driver may choose to implement a vdevce_alloc
  226. op to init its HW for virtualization feature related to a vDEVICE. Successful
  227. completion of this operation sets up the linkages between vIOMMU and device.
  228. A device can only bind to an iommufd due to DMA ownership claim and attach to at
  229. most one IOAS object (no support of PASID yet).
  230. Kernel Datastructure
  231. --------------------
  232. User visible objects are backed by following datastructures:
  233. - iommufd_ioas for IOMMUFD_OBJ_IOAS.
  234. - iommufd_device for IOMMUFD_OBJ_DEVICE.
  235. - iommufd_hwpt_paging for IOMMUFD_OBJ_HWPT_PAGING.
  236. - iommufd_hwpt_nested for IOMMUFD_OBJ_HWPT_NESTED.
  237. - iommufd_fault for IOMMUFD_OBJ_FAULT.
  238. - iommufd_viommu for IOMMUFD_OBJ_VIOMMU.
  239. - iommufd_vdevice for IOMMUFD_OBJ_VDEVICE.
  240. - iommufd_veventq for IOMMUFD_OBJ_VEVENTQ.
  241. - iommufd_hw_queue for IOMMUFD_OBJ_HW_QUEUE.
  242. Several terminologies when looking at these datastructures:
  243. - Automatic domain - refers to an iommu domain created automatically when
  244. attaching a device to an IOAS object. This is compatible to the semantics of
  245. VFIO type1.
  246. - Manual domain - refers to an iommu domain designated by the user as the
  247. target pagetable to be attached to by a device. Though currently there are
  248. no uAPIs to directly create such domain, the datastructure and algorithms
  249. are ready for handling that use case.
  250. - In-kernel user - refers to something like a VFIO mdev that is using the
  251. IOMMUFD access interface to access the IOAS. This starts by creating an
  252. iommufd_access object that is similar to the domain binding a physical device
  253. would do. The access object will then allow converting IOVA ranges into struct
  254. page * lists, or doing direct read/write to an IOVA.
  255. iommufd_ioas serves as the metadata datastructure to manage how IOVA ranges are
  256. mapped to memory pages, composed of:
  257. - struct io_pagetable holding the IOVA map
  258. - struct iopt_area's representing populated portions of IOVA
  259. - struct iopt_pages representing the storage of PFNs
  260. - struct iommu_domain representing the IO page table in the IOMMU
  261. - struct iopt_pages_access representing in-kernel users of PFNs
  262. - struct xarray pinned_pfns holding a list of pages pinned by in-kernel users
  263. Each iopt_pages represents a logical linear array of full PFNs. The PFNs are
  264. ultimately derived from userspace VAs via an mm_struct. Once they have been
  265. pinned the PFNs are stored in IOPTEs of an iommu_domain or inside the pinned_pfns
  266. xarray if they have been pinned through an iommufd_access.
  267. PFN have to be copied between all combinations of storage locations, depending
  268. on what domains are present and what kinds of in-kernel "software access" users
  269. exist. The mechanism ensures that a page is pinned only once.
  270. An io_pagetable is composed of iopt_areas pointing at iopt_pages, along with a
  271. list of iommu_domains that mirror the IOVA to PFN map.
  272. Multiple io_pagetable-s, through their iopt_area-s, can share a single
  273. iopt_pages which avoids multi-pinning and double accounting of page
  274. consumption.
  275. iommufd_ioas is shareable between subsystems, e.g. VFIO and VDPA, as long as
  276. devices managed by different subsystems are bound to a same iommufd.
  277. IOMMUFD User API
  278. ================
  279. .. kernel-doc:: include/uapi/linux/iommufd.h
  280. IOMMUFD Kernel API
  281. ==================
  282. The IOMMUFD kAPI is device-centric with group-related tricks managed behind the
  283. scene. This allows the external drivers calling such kAPI to implement a simple
  284. device-centric uAPI for connecting its device to an iommufd, instead of
  285. explicitly imposing the group semantics in its uAPI as VFIO does.
  286. .. kernel-doc:: drivers/iommu/iommufd/device.c
  287. :export:
  288. .. kernel-doc:: drivers/iommu/iommufd/main.c
  289. :export:
  290. VFIO and IOMMUFD
  291. ----------------
  292. Connecting a VFIO device to iommufd can be done in two ways.
  293. First is a VFIO compatible way by directly implementing the /dev/vfio/vfio
  294. container IOCTLs by mapping them into io_pagetable operations. Doing so allows
  295. the use of iommufd in legacy VFIO applications by symlinking /dev/vfio/vfio to
  296. /dev/iommufd or extending VFIO to SET_CONTAINER using an iommufd instead of a
  297. container fd.
  298. The second approach directly extends VFIO to support a new set of device-centric
  299. user API based on aforementioned IOMMUFD kernel API. It requires userspace
  300. change but better matches the IOMMUFD API semantics and easier to support new
  301. iommufd features when comparing it to the first approach.
  302. Currently both approaches are still work-in-progress.
  303. There are still a few gaps to be resolved to catch up with VFIO type1, as
  304. documented in iommufd_vfio_check_extension().
  305. Future TODOs
  306. ============
  307. Currently IOMMUFD supports only kernel-managed I/O page table, similar to VFIO
  308. type1. New features on the radar include:
  309. - Binding iommu_domain's to PASID/SSID
  310. - Userspace page tables, for ARM, x86 and S390
  311. - Kernel bypass'd invalidation of user page tables
  312. - Re-use of the KVM page table in the IOMMU
  313. - Dirty page tracking in the IOMMU
  314. - Runtime Increase/Decrease of IOPTE size
  315. - PRI support with faults resolved in userspace