vfio-mediated-device.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. include:: <isonum.txt>
  3. =====================
  4. VFIO Mediated devices
  5. =====================
  6. :Copyright: |copy| 2016, NVIDIA CORPORATION. All rights reserved.
  7. :Author: Neo Jia <cjia@nvidia.com>
  8. :Author: Kirti Wankhede <kwankhede@nvidia.com>
  9. Virtual Function I/O (VFIO) Mediated devices[1]
  10. ===============================================
  11. The number of use cases for virtualizing DMA devices that do not have built-in
  12. SR_IOV capability is increasing. Previously, to virtualize such devices,
  13. developers had to create their own management interfaces and APIs, and then
  14. integrate them with user space software. To simplify integration with user space
  15. software, we have identified common requirements and a unified management
  16. interface for such devices.
  17. The VFIO driver framework provides unified APIs for direct device access. It is
  18. an IOMMU/device-agnostic framework for exposing direct device access to user
  19. space in a secure, IOMMU-protected environment. This framework is used for
  20. multiple devices, such as GPUs, network adapters, and compute accelerators. With
  21. direct device access, virtual machines or user space applications have direct
  22. access to the physical device. This framework is reused for mediated devices.
  23. The mediated core driver provides a common interface for mediated device
  24. management that can be used by drivers of different devices. This module
  25. provides a generic interface to perform these operations:
  26. * Create and destroy a mediated device
  27. * Add a mediated device to and remove it from a mediated bus driver
  28. * Add a mediated device to and remove it from an IOMMU group
  29. The mediated core driver also provides an interface to register a bus driver.
  30. For example, the mediated VFIO mdev driver is designed for mediated devices and
  31. supports VFIO APIs. The mediated bus driver adds a mediated device to and
  32. removes it from a VFIO group.
  33. The following high-level block diagram shows the main components and interfaces
  34. in the VFIO mediated driver framework. The diagram shows NVIDIA, Intel, and IBM
  35. devices as examples, as these devices are the first devices to use this module::
  36. +---------------+
  37. | |
  38. | +-----------+ | mdev_register_driver() +--------------+
  39. | | | +<------------------------+ |
  40. | | mdev | | | |
  41. | | bus | +------------------------>+ vfio_mdev.ko |<-> VFIO user
  42. | | driver | | probe()/remove() | | APIs
  43. | | | | +--------------+
  44. | +-----------+ |
  45. | |
  46. | MDEV CORE |
  47. | MODULE |
  48. | mdev.ko |
  49. | +-----------+ | mdev_register_parent() +--------------+
  50. | | | +<------------------------+ |
  51. | | | | | ccw_device.ko|<-> physical
  52. | | | +------------------------>+ | device
  53. | | | | callbacks +--------------+
  54. | | Physical | |
  55. | | device | | mdev_register_parent() +--------------+
  56. | | interface | |<------------------------+ |
  57. | | | | | i915.ko |<-> physical
  58. | | | +------------------------>+ | device
  59. | | | | callbacks +--------------+
  60. | +-----------+ |
  61. +---------------+
  62. Registration Interfaces
  63. =======================
  64. The mediated core driver provides the following types of registration
  65. interfaces:
  66. * Registration interface for a mediated bus driver
  67. * Physical device driver interface
  68. Registration Interface for a Mediated Bus Driver
  69. ------------------------------------------------
  70. The registration interface for a mediated device driver provides the following
  71. structure to represent a mediated device's driver::
  72. /*
  73. * struct mdev_driver [2] - Mediated device's driver
  74. * @probe: called when new device created
  75. * @remove: called when device removed
  76. * @driver: device driver structure
  77. */
  78. struct mdev_driver {
  79. int (*probe) (struct mdev_device *dev);
  80. void (*remove) (struct mdev_device *dev);
  81. unsigned int (*get_available)(struct mdev_type *mtype);
  82. ssize_t (*show_description)(struct mdev_type *mtype, char *buf);
  83. struct device_driver driver;
  84. };
  85. A mediated bus driver for mdev should use this structure in the function calls
  86. to register and unregister itself with the core driver:
  87. * Register::
  88. int mdev_register_driver(struct mdev_driver *drv);
  89. * Unregister::
  90. void mdev_unregister_driver(struct mdev_driver *drv);
  91. The mediated bus driver's probe function should create a vfio_device on top of
  92. the mdev_device and connect it to an appropriate implementation of
  93. vfio_device_ops.
  94. When a driver wants to add the GUID creation sysfs to an existing device it has
  95. probe'd to then it should call::
  96. int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
  97. struct mdev_driver *mdev_driver);
  98. This will provide the 'mdev_supported_types/XX/create' files which can then be
  99. used to trigger the creation of a mdev_device. The created mdev_device will be
  100. attached to the specified driver.
  101. When the driver needs to remove itself it calls::
  102. void mdev_unregister_parent(struct mdev_parent *parent);
  103. Which will unbind and destroy all the created mdevs and remove the sysfs files.
  104. Mediated Device Management Interface Through sysfs
  105. ==================================================
  106. The management interface through sysfs enables user space software, such as
  107. libvirt, to query and configure mediated devices in a hardware-agnostic fashion.
  108. This management interface provides flexibility to the underlying physical
  109. device's driver to support features such as:
  110. * Mediated device hot plug
  111. * Multiple mediated devices in a single virtual machine
  112. * Multiple mediated devices from different physical devices
  113. Links in the mdev_bus Class Directory
  114. -------------------------------------
  115. The /sys/class/mdev_bus/ directory contains links to devices that are registered
  116. with the mdev core driver.
  117. Directories and files under the sysfs for Each Physical Device
  118. --------------------------------------------------------------
  119. ::
  120. |- [parent physical device]
  121. |--- Vendor-specific-attributes [optional]
  122. |--- [mdev_supported_types]
  123. | |--- [<type-id>]
  124. | | |--- create
  125. | | |--- name
  126. | | |--- available_instances
  127. | | |--- device_api
  128. | | |--- description
  129. | | |--- [devices]
  130. | |--- [<type-id>]
  131. | | |--- create
  132. | | |--- name
  133. | | |--- available_instances
  134. | | |--- device_api
  135. | | |--- description
  136. | | |--- [devices]
  137. | |--- [<type-id>]
  138. | |--- create
  139. | |--- name
  140. | |--- available_instances
  141. | |--- device_api
  142. | |--- description
  143. | |--- [devices]
  144. * [mdev_supported_types]
  145. The list of currently supported mediated device types and their details.
  146. [<type-id>], device_api, and available_instances are mandatory attributes
  147. that should be provided by vendor driver.
  148. * [<type-id>]
  149. The [<type-id>] name is created by adding the device driver string as a prefix
  150. to the string provided by the vendor driver. This format of this name is as
  151. follows::
  152. sprintf(buf, "%s-%s", dev_driver_string(parent->dev), group->name);
  153. * device_api
  154. This attribute shows which device API is being created, for example,
  155. "vfio-pci" for a PCI device.
  156. * available_instances
  157. This attribute shows the number of devices of type <type-id> that can be
  158. created.
  159. * [device]
  160. This directory contains links to the devices of type <type-id> that have been
  161. created.
  162. * name
  163. This attribute shows a human readable name.
  164. * description
  165. This attribute can show brief features/description of the type. This is an
  166. optional attribute.
  167. Directories and Files Under the sysfs for Each mdev Device
  168. ----------------------------------------------------------
  169. ::
  170. |- [parent phy device]
  171. |--- [$MDEV_UUID]
  172. |--- remove
  173. |--- mdev_type {link to its type}
  174. |--- vendor-specific-attributes [optional]
  175. * remove (write only)
  176. Writing '1' to the 'remove' file destroys the mdev device. The vendor driver can
  177. fail the remove() callback if that device is active and the vendor driver
  178. doesn't support hot unplug.
  179. Example::
  180. # echo 1 > /sys/bus/mdev/devices/$mdev_UUID/remove
  181. Mediated device Hot plug
  182. ------------------------
  183. Mediated devices can be created and assigned at runtime. The procedure to hot
  184. plug a mediated device is the same as the procedure to hot plug a PCI device.
  185. Translation APIs for Mediated Devices
  186. =====================================
  187. The following APIs are provided for translating user pfn to host pfn in a VFIO
  188. driver::
  189. int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
  190. int npage, int prot, struct page **pages);
  191. void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova,
  192. int npage);
  193. These functions call back into the back-end IOMMU module by using the pin_pages
  194. and unpin_pages callbacks of the struct vfio_iommu_driver_ops[4]. Currently
  195. these callbacks are supported in the TYPE1 IOMMU module. To enable them for
  196. other IOMMU backend modules, such as PPC64 sPAPR module, they need to provide
  197. these two callback functions.
  198. References
  199. ==========
  200. 1. See Documentation/driver-api/vfio.rst for more information on VFIO.
  201. 2. struct mdev_driver in include/linux/mdev.h
  202. 3. struct mdev_parent_ops in include/linux/mdev.h
  203. 4. struct vfio_iommu_driver_ops in include/linux/vfio.h