vfio.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. ==================================
  2. VFIO - "Virtual Function I/O" [1]_
  3. ==================================
  4. Many modern systems now provide DMA and interrupt remapping facilities
  5. to help ensure I/O devices behave within the boundaries they've been
  6. allotted. This includes x86 hardware with AMD-Vi and Intel VT-d,
  7. POWER systems with Partitionable Endpoints (PEs) and embedded PowerPC
  8. systems such as Freescale PAMU. The VFIO driver is an IOMMU/device
  9. agnostic framework for exposing direct device access to userspace, in
  10. a secure, IOMMU protected environment. In other words, this allows
  11. safe [2]_, non-privileged, userspace drivers.
  12. Why do we want that? Virtual machines often make use of direct device
  13. access ("device assignment") when configured for the highest possible
  14. I/O performance. From a device and host perspective, this simply
  15. turns the VM into a userspace driver, with the benefits of
  16. significantly reduced latency, higher bandwidth, and direct use of
  17. bare-metal device drivers [3]_.
  18. Some applications, particularly in the high performance computing
  19. field, also benefit from low-overhead, direct device access from
  20. userspace. Examples include network adapters (often non-TCP/IP based)
  21. and compute accelerators. Prior to VFIO, these drivers had to either
  22. go through the full development cycle to become proper upstream
  23. driver, be maintained out of tree, or make use of the UIO framework,
  24. which has no notion of IOMMU protection, limited interrupt support,
  25. and requires root privileges to access things like PCI configuration
  26. space.
  27. The VFIO driver framework intends to unify these, replacing both the
  28. KVM PCI specific device assignment code as well as provide a more
  29. secure, more featureful userspace driver environment than UIO.
  30. Groups, Devices, and IOMMUs
  31. ---------------------------
  32. Devices are the main target of any I/O driver. Devices typically
  33. create a programming interface made up of I/O access, interrupts,
  34. and DMA. Without going into the details of each of these, DMA is
  35. by far the most critical aspect for maintaining a secure environment
  36. as allowing a device read-write access to system memory imposes the
  37. greatest risk to the overall system integrity.
  38. To help mitigate this risk, many modern IOMMUs now incorporate
  39. isolation properties into what was, in many cases, an interface only
  40. meant for translation (ie. solving the addressing problems of devices
  41. with limited address spaces). With this, devices can now be isolated
  42. from each other and from arbitrary memory access, thus allowing
  43. things like secure direct assignment of devices into virtual machines.
  44. This isolation is not always at the granularity of a single device
  45. though. Even when an IOMMU is capable of this, properties of devices,
  46. interconnects, and IOMMU topologies can each reduce this isolation.
  47. For instance, an individual device may be part of a larger multi-
  48. function enclosure. While the IOMMU may be able to distinguish
  49. between devices within the enclosure, the enclosure may not require
  50. transactions between devices to reach the IOMMU. Examples of this
  51. could be anything from a multi-function PCI device with backdoors
  52. between functions to a non-PCI-ACS (Access Control Services) capable
  53. bridge allowing redirection without reaching the IOMMU. Topology
  54. can also play a factor in terms of hiding devices. A PCIe-to-PCI
  55. bridge masks the devices behind it, making transaction appear as if
  56. from the bridge itself. Obviously IOMMU design plays a major factor
  57. as well.
  58. Therefore, while for the most part an IOMMU may have device level
  59. granularity, any system is susceptible to reduced granularity. The
  60. IOMMU API therefore supports a notion of IOMMU groups. A group is
  61. a set of devices which is isolatable from all other devices in the
  62. system. Groups are therefore the unit of ownership used by VFIO.
  63. While the group is the minimum granularity that must be used to
  64. ensure secure user access, it's not necessarily the preferred
  65. granularity. In IOMMUs which make use of page tables, it may be
  66. possible to share a set of page tables between different groups,
  67. reducing the overhead both to the platform (reduced TLB thrashing,
  68. reduced duplicate page tables), and to the user (programming only
  69. a single set of translations). For this reason, VFIO makes use of
  70. a container class, which may hold one or more groups. A container
  71. is created by simply opening the /dev/vfio/vfio character device.
  72. On its own, the container provides little functionality, with all
  73. but a couple version and extension query interfaces locked away.
  74. The user needs to add a group into the container for the next level
  75. of functionality. To do this, the user first needs to identify the
  76. group associated with the desired device. This can be done using
  77. the sysfs links described in the example below. By unbinding the
  78. device from the host driver and binding it to a VFIO driver, a new
  79. VFIO group will appear for the group as /dev/vfio/$GROUP, where
  80. $GROUP is the IOMMU group number of which the device is a member.
  81. If the IOMMU group contains multiple devices, each will need to
  82. be bound to a VFIO driver before operations on the VFIO group
  83. are allowed (it's also sufficient to only unbind the device from
  84. host drivers if a VFIO driver is unavailable; this will make the
  85. group available, but not that particular device). TBD - interface
  86. for disabling driver probing/locking a device.
  87. Once the group is ready, it may be added to the container by opening
  88. the VFIO group character device (/dev/vfio/$GROUP) and using the
  89. VFIO_GROUP_SET_CONTAINER ioctl, passing the file descriptor of the
  90. previously opened container file. If desired and if the IOMMU driver
  91. supports sharing the IOMMU context between groups, multiple groups may
  92. be set to the same container. If a group fails to set to a container
  93. with existing groups, a new empty container will need to be used
  94. instead.
  95. With a group (or groups) attached to a container, the remaining
  96. ioctls become available, enabling access to the VFIO IOMMU interfaces.
  97. Additionally, it now becomes possible to get file descriptors for each
  98. device within a group using an ioctl on the VFIO group file descriptor.
  99. The VFIO device API includes ioctls for describing the device, the I/O
  100. regions and their read/write/mmap offsets on the device descriptor, as
  101. well as mechanisms for describing and registering interrupt
  102. notifications.
  103. VFIO Usage Example
  104. ------------------
  105. Assume user wants to access PCI device 0000:06:0d.0::
  106. $ readlink /sys/bus/pci/devices/0000:06:0d.0/iommu_group
  107. ../../../../kernel/iommu_groups/26
  108. This device is therefore in IOMMU group 26. This device is on the
  109. pci bus, therefore the user will make use of vfio-pci to manage the
  110. group::
  111. # modprobe vfio-pci
  112. Binding this device to the vfio-pci driver creates the VFIO group
  113. character devices for this group::
  114. $ lspci -n -s 0000:06:0d.0
  115. 06:0d.0 0401: 1102:0002 (rev 08)
  116. # echo 0000:06:0d.0 > /sys/bus/pci/devices/0000:06:0d.0/driver/unbind
  117. # echo 1102 0002 > /sys/bus/pci/drivers/vfio-pci/new_id
  118. Now we need to look at what other devices are in the group to free
  119. it for use by VFIO::
  120. $ ls -l /sys/bus/pci/devices/0000:06:0d.0/iommu_group/devices
  121. total 0
  122. lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:00:1e.0 ->
  123. ../../../../devices/pci0000:00/0000:00:1e.0
  124. lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:06:0d.0 ->
  125. ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.0
  126. lrwxrwxrwx. 1 root root 0 Apr 23 16:13 0000:06:0d.1 ->
  127. ../../../../devices/pci0000:00/0000:00:1e.0/0000:06:0d.1
  128. This device is behind a PCIe-to-PCI bridge [4]_, therefore we also
  129. need to add device 0000:06:0d.1 to the group following the same
  130. procedure as above. Device 0000:00:1e.0 is a bridge that does
  131. not currently have a host driver, therefore it's not required to
  132. bind this device to the vfio-pci driver (vfio-pci does not currently
  133. support PCI bridges).
  134. The final step is to provide the user with access to the group if
  135. unprivileged operation is desired (note that /dev/vfio/vfio provides
  136. no capabilities on its own and is therefore expected to be set to
  137. mode 0666 by the system)::
  138. # chown user:user /dev/vfio/26
  139. The user now has full access to all the devices and the iommu for this
  140. group and can access them as follows::
  141. int container, group, device, i;
  142. struct vfio_group_status group_status =
  143. { .argsz = sizeof(group_status) };
  144. struct vfio_iommu_type1_info iommu_info = { .argsz = sizeof(iommu_info) };
  145. struct vfio_iommu_type1_dma_map dma_map = { .argsz = sizeof(dma_map) };
  146. struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
  147. /* Create a new container */
  148. container = open("/dev/vfio/vfio", O_RDWR);
  149. if (ioctl(container, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
  150. /* Unknown API version */
  151. if (!ioctl(container, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU))
  152. /* Doesn't support the IOMMU driver we want. */
  153. /* Open the group */
  154. group = open("/dev/vfio/26", O_RDWR);
  155. /* Test the group is viable and available */
  156. ioctl(group, VFIO_GROUP_GET_STATUS, &group_status);
  157. if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
  158. /* Group is not viable (ie, not all devices bound for vfio) */
  159. /* Add the group to the container */
  160. ioctl(group, VFIO_GROUP_SET_CONTAINER, &container);
  161. /* Enable the IOMMU model we want */
  162. ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
  163. /* Get addition IOMMU info */
  164. ioctl(container, VFIO_IOMMU_GET_INFO, &iommu_info);
  165. /* Allocate some space and setup a DMA mapping */
  166. dma_map.vaddr = mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE,
  167. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  168. dma_map.size = 1024 * 1024;
  169. dma_map.iova = 0; /* 1MB starting at 0x0 from device view */
  170. dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
  171. ioctl(container, VFIO_IOMMU_MAP_DMA, &dma_map);
  172. /* Get a file descriptor for the device */
  173. device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, "0000:06:0d.0");
  174. /* Test and setup the device */
  175. ioctl(device, VFIO_DEVICE_GET_INFO, &device_info);
  176. for (i = 0; i < device_info.num_regions; i++) {
  177. struct vfio_region_info reg = { .argsz = sizeof(reg) };
  178. reg.index = i;
  179. ioctl(device, VFIO_DEVICE_GET_REGION_INFO, &reg);
  180. /* Setup mappings... read/write offsets, mmaps
  181. * For PCI devices, config space is a region */
  182. }
  183. for (i = 0; i < device_info.num_irqs; i++) {
  184. struct vfio_irq_info irq = { .argsz = sizeof(irq) };
  185. irq.index = i;
  186. ioctl(device, VFIO_DEVICE_GET_IRQ_INFO, &irq);
  187. /* Setup IRQs... eventfds, VFIO_DEVICE_SET_IRQS */
  188. }
  189. /* Gratuitous device reset and go... */
  190. ioctl(device, VFIO_DEVICE_RESET);
  191. IOMMUFD and vfio_iommu_type1
  192. ----------------------------
  193. IOMMUFD is the new user API to manage I/O page tables from userspace.
  194. It intends to be the portal of delivering advanced userspace DMA
  195. features (nested translation [5]_, PASID [6]_, etc.) while also providing
  196. a backwards compatibility interface for existing VFIO_TYPE1v2_IOMMU use
  197. cases. Eventually the vfio_iommu_type1 driver, as well as the legacy
  198. vfio container and group model is intended to be deprecated.
  199. The IOMMUFD backwards compatibility interface can be enabled two ways.
  200. In the first method, the kernel can be configured with
  201. CONFIG_IOMMUFD_VFIO_CONTAINER, in which case the IOMMUFD subsystem
  202. transparently provides the entire infrastructure for the VFIO
  203. container and IOMMU backend interfaces. The compatibility mode can
  204. also be accessed if the VFIO container interface, ie. /dev/vfio/vfio is
  205. simply symlink'd to /dev/iommu. Note that at the time of writing, the
  206. compatibility mode is not entirely feature complete relative to
  207. VFIO_TYPE1v2_IOMMU (ex. DMA mapping MMIO) and does not attempt to
  208. provide compatibility to the VFIO_SPAPR_TCE_IOMMU interface. Therefore
  209. it is not generally advisable at this time to switch from native VFIO
  210. implementations to the IOMMUFD compatibility interfaces.
  211. Long term, VFIO users should migrate to device access through the cdev
  212. interface described below, and native access through the IOMMUFD
  213. provided interfaces.
  214. VFIO Device cdev
  215. ----------------
  216. Traditionally user acquires a device fd via VFIO_GROUP_GET_DEVICE_FD
  217. in a VFIO group.
  218. With CONFIG_VFIO_DEVICE_CDEV=y the user can now acquire a device fd
  219. by directly opening a character device /dev/vfio/devices/vfioX where
  220. "X" is the number allocated uniquely by VFIO for registered devices.
  221. cdev interface does not support noiommu devices, so user should use
  222. the legacy group interface if noiommu is wanted.
  223. The cdev only works with IOMMUFD. Both VFIO drivers and applications
  224. must adapt to the new cdev security model which requires using
  225. VFIO_DEVICE_BIND_IOMMUFD to claim DMA ownership before starting to
  226. actually use the device. Once BIND succeeds then a VFIO device can
  227. be fully accessed by the user.
  228. VFIO device cdev doesn't rely on VFIO group/container/iommu drivers.
  229. Hence those modules can be fully compiled out in an environment
  230. where no legacy VFIO application exists.
  231. So far SPAPR does not support IOMMUFD yet. So it cannot support device
  232. cdev either.
  233. vfio device cdev access is still bound by IOMMU group semantics, ie. there
  234. can be only one DMA owner for the group. Devices belonging to the same
  235. group can not be bound to multiple iommufd_ctx or shared between native
  236. kernel and vfio bus driver or other driver supporting the driver_managed_dma
  237. flag. A violation of this ownership requirement will fail at the
  238. VFIO_DEVICE_BIND_IOMMUFD ioctl, which gates full device access.
  239. Device cdev Example
  240. -------------------
  241. Assume user wants to access PCI device 0000:6a:01.0::
  242. $ ls /sys/bus/pci/devices/0000:6a:01.0/vfio-dev/
  243. vfio0
  244. This device is therefore represented as vfio0. The user can verify
  245. its existence::
  246. $ ls -l /dev/vfio/devices/vfio0
  247. crw------- 1 root root 511, 0 Feb 16 01:22 /dev/vfio/devices/vfio0
  248. $ cat /sys/bus/pci/devices/0000:6a:01.0/vfio-dev/vfio0/dev
  249. 511:0
  250. $ ls -l /dev/char/511\:0
  251. lrwxrwxrwx 1 root root 21 Feb 16 01:22 /dev/char/511:0 -> ../vfio/devices/vfio0
  252. Then provide the user with access to the device if unprivileged
  253. operation is desired::
  254. $ chown user:user /dev/vfio/devices/vfio0
  255. Finally the user could get cdev fd by::
  256. cdev_fd = open("/dev/vfio/devices/vfio0", O_RDWR);
  257. An opened cdev_fd doesn't give the user any permission of accessing
  258. the device except binding the cdev_fd to an iommufd. After that point
  259. then the device is fully accessible including attaching it to an
  260. IOMMUFD IOAS/HWPT to enable userspace DMA::
  261. struct vfio_device_bind_iommufd bind = {
  262. .argsz = sizeof(bind),
  263. .flags = 0,
  264. };
  265. struct iommu_ioas_alloc alloc_data = {
  266. .size = sizeof(alloc_data),
  267. .flags = 0,
  268. };
  269. struct vfio_device_attach_iommufd_pt attach_data = {
  270. .argsz = sizeof(attach_data),
  271. .flags = 0,
  272. };
  273. struct iommu_ioas_map map = {
  274. .size = sizeof(map),
  275. .flags = IOMMU_IOAS_MAP_READABLE |
  276. IOMMU_IOAS_MAP_WRITEABLE |
  277. IOMMU_IOAS_MAP_FIXED_IOVA,
  278. .__reserved = 0,
  279. };
  280. iommufd = open("/dev/iommu", O_RDWR);
  281. bind.iommufd = iommufd;
  282. ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind);
  283. ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data);
  284. attach_data.pt_id = alloc_data.out_ioas_id;
  285. ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data);
  286. /* Allocate some space and setup a DMA mapping */
  287. map.user_va = (int64_t)mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE,
  288. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  289. map.iova = 0; /* 1MB starting at 0x0 from device view */
  290. map.length = 1024 * 1024;
  291. map.ioas_id = alloc_data.out_ioas_id;
  292. ioctl(iommufd, IOMMU_IOAS_MAP, &map);
  293. /* Other device operations as stated in "VFIO Usage Example" */
  294. VFIO User API
  295. -------------------------------------------------------------------------------
  296. Please see include/uapi/linux/vfio.h for complete API documentation.
  297. VFIO bus driver API
  298. -------------------------------------------------------------------------------
  299. VFIO bus drivers, such as vfio-pci make use of only a few interfaces
  300. into VFIO core. When devices are bound and unbound to the driver,
  301. Following interfaces are called when devices are bound to and
  302. unbound from the driver::
  303. int vfio_register_group_dev(struct vfio_device *device);
  304. int vfio_register_emulated_iommu_dev(struct vfio_device *device);
  305. void vfio_unregister_group_dev(struct vfio_device *device);
  306. The driver should embed the vfio_device in its own structure and use
  307. vfio_alloc_device() to allocate the structure, and can register
  308. @init/@release callbacks to manage any private state wrapping the
  309. vfio_device::
  310. vfio_alloc_device(dev_struct, member, dev, ops);
  311. void vfio_put_device(struct vfio_device *device);
  312. vfio_register_group_dev() indicates to the core to begin tracking the
  313. iommu_group of the specified dev and register the dev as owned by a VFIO bus
  314. driver. Once vfio_register_group_dev() returns it is possible for userspace to
  315. start accessing the driver, thus the driver should ensure it is completely
  316. ready before calling it. The driver provides an ops structure for callbacks
  317. similar to a file operations structure::
  318. struct vfio_device_ops {
  319. char *name;
  320. int (*init)(struct vfio_device *vdev);
  321. void (*release)(struct vfio_device *vdev);
  322. int (*bind_iommufd)(struct vfio_device *vdev,
  323. struct iommufd_ctx *ictx, u32 *out_device_id);
  324. void (*unbind_iommufd)(struct vfio_device *vdev);
  325. int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id);
  326. void (*detach_ioas)(struct vfio_device *vdev);
  327. int (*open_device)(struct vfio_device *vdev);
  328. void (*close_device)(struct vfio_device *vdev);
  329. ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
  330. size_t count, loff_t *ppos);
  331. ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
  332. size_t count, loff_t *size);
  333. long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
  334. unsigned long arg);
  335. int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
  336. void (*request)(struct vfio_device *vdev, unsigned int count);
  337. int (*match)(struct vfio_device *vdev, char *buf);
  338. void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
  339. int (*device_feature)(struct vfio_device *device, u32 flags,
  340. void __user *arg, size_t argsz);
  341. };
  342. Each function is passed the vdev that was originally registered
  343. in the vfio_register_group_dev() or vfio_register_emulated_iommu_dev()
  344. call above. This allows the bus driver to obtain its private data using
  345. container_of().
  346. ::
  347. - The init/release callbacks are issued when vfio_device is initialized
  348. and released.
  349. - The open/close device callbacks are issued when the first
  350. instance of a file descriptor for the device is created (eg.
  351. via VFIO_GROUP_GET_DEVICE_FD) for a user session.
  352. - The ioctl callback provides a direct pass through for some VFIO_DEVICE_*
  353. ioctls.
  354. - The [un]bind_iommufd callbacks are issued when the device is bound to
  355. and unbound from iommufd.
  356. - The [de]attach_ioas callback is issued when the device is attached to
  357. and detached from an IOAS managed by the bound iommufd. However, the
  358. attached IOAS can also be automatically detached when the device is
  359. unbound from iommufd.
  360. - The read/write/mmap callbacks implement the device region access defined
  361. by the device's own VFIO_DEVICE_GET_REGION_INFO ioctl.
  362. - The request callback is issued when device is going to be unregistered,
  363. such as when trying to unbind the device from the vfio bus driver.
  364. - The dma_unmap callback is issued when a range of iovas are unmapped
  365. in the container or IOAS attached by the device. Drivers which make
  366. use of the vfio page pinning interface must implement this callback in
  367. order to unpin pages within the dma_unmap range. Drivers must tolerate
  368. this callback even before calls to open_device().
  369. PPC64 sPAPR implementation note
  370. -------------------------------
  371. This implementation has some specifics:
  372. 1) On older systems (POWER7 with P5IOC2/IODA1) only one IOMMU group per
  373. container is supported as an IOMMU table is allocated at the boot time,
  374. one table per a IOMMU group which is a Partitionable Endpoint (PE)
  375. (PE is often a PCI domain but not always).
  376. Newer systems (POWER8 with IODA2) have improved hardware design which allows
  377. to remove this limitation and have multiple IOMMU groups per a VFIO
  378. container.
  379. 2) The hardware supports so called DMA windows - the PCI address range
  380. within which DMA transfer is allowed, any attempt to access address space
  381. out of the window leads to the whole PE isolation.
  382. 3) PPC64 guests are paravirtualized but not fully emulated. There is an API
  383. to map/unmap pages for DMA, and it normally maps 1..32 pages per call and
  384. currently there is no way to reduce the number of calls. In order to make
  385. things faster, the map/unmap handling has been implemented in real mode
  386. which provides an excellent performance which has limitations such as
  387. inability to do locked pages accounting in real time.
  388. 4) According to sPAPR specification, A Partitionable Endpoint (PE) is an I/O
  389. subtree that can be treated as a unit for the purposes of partitioning and
  390. error recovery. A PE may be a single or multi-function IOA (IO Adapter), a
  391. function of a multi-function IOA, or multiple IOAs (possibly including
  392. switch and bridge structures above the multiple IOAs). PPC64 guests detect
  393. PCI errors and recover from them via EEH RTAS services, which works on the
  394. basis of additional ioctl commands.
  395. So 4 additional ioctls have been added:
  396. VFIO_IOMMU_SPAPR_TCE_GET_INFO
  397. returns the size and the start of the DMA window on the PCI bus.
  398. VFIO_IOMMU_ENABLE
  399. enables the container. The locked pages accounting
  400. is done at this point. This lets user first to know what
  401. the DMA window is and adjust rlimit before doing any real job.
  402. VFIO_IOMMU_DISABLE
  403. disables the container.
  404. VFIO_EEH_PE_OP
  405. provides an API for EEH setup, error detection and recovery.
  406. The code flow from the example above should be slightly changed::
  407. struct vfio_eeh_pe_op pe_op = { .argsz = sizeof(pe_op), .flags = 0 };
  408. .....
  409. /* Add the group to the container */
  410. ioctl(group, VFIO_GROUP_SET_CONTAINER, &container);
  411. /* Enable the IOMMU model we want */
  412. ioctl(container, VFIO_SET_IOMMU, VFIO_SPAPR_TCE_IOMMU)
  413. /* Get addition sPAPR IOMMU info */
  414. vfio_iommu_spapr_tce_info spapr_iommu_info;
  415. ioctl(container, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &spapr_iommu_info);
  416. if (ioctl(container, VFIO_IOMMU_ENABLE))
  417. /* Cannot enable container, may be low rlimit */
  418. /* Allocate some space and setup a DMA mapping */
  419. dma_map.vaddr = mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE,
  420. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  421. dma_map.size = 1024 * 1024;
  422. dma_map.iova = 0; /* 1MB starting at 0x0 from device view */
  423. dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
  424. /* Check here is .iova/.size are within DMA window from spapr_iommu_info */
  425. ioctl(container, VFIO_IOMMU_MAP_DMA, &dma_map);
  426. /* Get a file descriptor for the device */
  427. device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, "0000:06:0d.0");
  428. ....
  429. /* Gratuitous device reset and go... */
  430. ioctl(device, VFIO_DEVICE_RESET);
  431. /* Make sure EEH is supported */
  432. ioctl(container, VFIO_CHECK_EXTENSION, VFIO_EEH);
  433. /* Enable the EEH functionality on the device */
  434. pe_op.op = VFIO_EEH_PE_ENABLE;
  435. ioctl(container, VFIO_EEH_PE_OP, &pe_op);
  436. /* You're suggested to create additional data struct to represent
  437. * PE, and put child devices belonging to same IOMMU group to the
  438. * PE instance for later reference.
  439. */
  440. /* Check the PE's state and make sure it's in functional state */
  441. pe_op.op = VFIO_EEH_PE_GET_STATE;
  442. ioctl(container, VFIO_EEH_PE_OP, &pe_op);
  443. /* Save device state using pci_save_state().
  444. * EEH should be enabled on the specified device.
  445. */
  446. ....
  447. /* Inject EEH error, which is expected to be caused by 32-bits
  448. * config load.
  449. */
  450. pe_op.op = VFIO_EEH_PE_INJECT_ERR;
  451. pe_op.err.type = EEH_ERR_TYPE_32;
  452. pe_op.err.func = EEH_ERR_FUNC_LD_CFG_ADDR;
  453. pe_op.err.addr = 0ul;
  454. pe_op.err.mask = 0ul;
  455. ioctl(container, VFIO_EEH_PE_OP, &pe_op);
  456. ....
  457. /* When 0xFF's returned from reading PCI config space or IO BARs
  458. * of the PCI device. Check the PE's state to see if that has been
  459. * frozen.
  460. */
  461. ioctl(container, VFIO_EEH_PE_OP, &pe_op);
  462. /* Waiting for pending PCI transactions to be completed and don't
  463. * produce any more PCI traffic from/to the affected PE until
  464. * recovery is finished.
  465. */
  466. /* Enable IO for the affected PE and collect logs. Usually, the
  467. * standard part of PCI config space, AER registers are dumped
  468. * as logs for further analysis.
  469. */
  470. pe_op.op = VFIO_EEH_PE_UNFREEZE_IO;
  471. ioctl(container, VFIO_EEH_PE_OP, &pe_op);
  472. /*
  473. * Issue PE reset: hot or fundamental reset. Usually, hot reset
  474. * is enough. However, the firmware of some PCI adapters would
  475. * require fundamental reset.
  476. */
  477. pe_op.op = VFIO_EEH_PE_RESET_HOT;
  478. ioctl(container, VFIO_EEH_PE_OP, &pe_op);
  479. pe_op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
  480. ioctl(container, VFIO_EEH_PE_OP, &pe_op);
  481. /* Configure the PCI bridges for the affected PE */
  482. pe_op.op = VFIO_EEH_PE_CONFIGURE;
  483. ioctl(container, VFIO_EEH_PE_OP, &pe_op);
  484. /* Restored state we saved at initialization time. pci_restore_state()
  485. * is good enough as an example.
  486. */
  487. /* Hopefully, error is recovered successfully. Now, you can resume to
  488. * start PCI traffic to/from the affected PE.
  489. */
  490. ....
  491. 5) There is v2 of SPAPR TCE IOMMU. It deprecates VFIO_IOMMU_ENABLE/
  492. VFIO_IOMMU_DISABLE and implements 2 new ioctls:
  493. VFIO_IOMMU_SPAPR_REGISTER_MEMORY and VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY
  494. (which are unsupported in v1 IOMMU).
  495. PPC64 paravirtualized guests generate a lot of map/unmap requests,
  496. and the handling of those includes pinning/unpinning pages and updating
  497. mm::locked_vm counter to make sure we do not exceed the rlimit.
  498. The v2 IOMMU splits accounting and pinning into separate operations:
  499. - VFIO_IOMMU_SPAPR_REGISTER_MEMORY/VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY ioctls
  500. receive a user space address and size of the block to be pinned.
  501. Bisecting is not supported and VFIO_IOMMU_UNREGISTER_MEMORY is expected to
  502. be called with the exact address and size used for registering
  503. the memory block. The userspace is not expected to call these often.
  504. The ranges are stored in a linked list in a VFIO container.
  505. - VFIO_IOMMU_MAP_DMA/VFIO_IOMMU_UNMAP_DMA ioctls only update the actual
  506. IOMMU table and do not do pinning; instead these check that the userspace
  507. address is from pre-registered range.
  508. This separation helps in optimizing DMA for guests.
  509. 6) sPAPR specification allows guests to have an additional DMA window(s) on
  510. a PCI bus with a variable page size. Two ioctls have been added to support
  511. this: VFIO_IOMMU_SPAPR_TCE_CREATE and VFIO_IOMMU_SPAPR_TCE_REMOVE.
  512. The platform has to support the functionality or error will be returned to
  513. the userspace. The existing hardware supports up to 2 DMA windows, one is
  514. 2GB long, uses 4K pages and called "default 32bit window"; the other can
  515. be as big as entire RAM, use different page size, it is optional - guests
  516. create those in run-time if the guest driver supports 64bit DMA.
  517. VFIO_IOMMU_SPAPR_TCE_CREATE receives a page shift, a DMA window size and
  518. a number of TCE table levels (if a TCE table is going to be big enough and
  519. the kernel may not be able to allocate enough of physically contiguous
  520. memory). It creates a new window in the available slot and returns the bus
  521. address where the new window starts. Due to hardware limitation, the user
  522. space cannot choose the location of DMA windows.
  523. VFIO_IOMMU_SPAPR_TCE_REMOVE receives the bus start address of the window
  524. and removes it.
  525. -------------------------------------------------------------------------------
  526. .. [1] VFIO was originally an acronym for "Virtual Function I/O" in its
  527. initial implementation by Tom Lyon while as Cisco. We've since
  528. outgrown the acronym, but it's catchy.
  529. .. [2] "safe" also depends upon a device being "well behaved". It's
  530. possible for multi-function devices to have backdoors between
  531. functions and even for single function devices to have alternative
  532. access to things like PCI config space through MMIO registers. To
  533. guard against the former we can include additional precautions in the
  534. IOMMU driver to group multi-function PCI devices together
  535. (iommu=group_mf). The latter we can't prevent, but the IOMMU should
  536. still provide isolation. For PCI, SR-IOV Virtual Functions are the
  537. best indicator of "well behaved", as these are designed for
  538. virtualization usage models.
  539. .. [3] As always there are trade-offs to virtual machine device
  540. assignment that are beyond the scope of VFIO. It's expected that
  541. future IOMMU technologies will reduce some, but maybe not all, of
  542. these trade-offs.
  543. .. [4] In this case the device is below a PCI bridge, so transactions
  544. from either function of the device are indistinguishable to the iommu::
  545. -[0000:00]-+-1e.0-[06]--+-0d.0
  546. \-0d.1
  547. 00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev 90)
  548. .. [5] Nested translation is an IOMMU feature which supports two stage
  549. address translations. This improves the address translation efficiency
  550. in IOMMU virtualization.
  551. .. [6] PASID stands for Process Address Space ID, introduced by PCI
  552. Express. It is a prerequisite for Shared Virtual Addressing (SVA)
  553. and Scalable I/O Virtualization (Scalable IOV).