vfio-mediated-device.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. .. include:: <isonum.txt>
  2. =====================
  3. VFIO Mediated devices
  4. =====================
  5. :Copyright: |copy| 2016, NVIDIA CORPORATION. All rights reserved.
  6. :Author: Neo Jia <cjia@nvidia.com>
  7. :Author: Kirti Wankhede <kwankhede@nvidia.com>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License version 2 as
  10. published by the Free Software Foundation.
  11. Virtual Function I/O (VFIO) Mediated devices[1]
  12. ===============================================
  13. The number of use cases for virtualizing DMA devices that do not have built-in
  14. SR_IOV capability is increasing. Previously, to virtualize such devices,
  15. developers had to create their own management interfaces and APIs, and then
  16. integrate them with user space software. To simplify integration with user space
  17. software, we have identified common requirements and a unified management
  18. interface for such devices.
  19. The VFIO driver framework provides unified APIs for direct device access. It is
  20. an IOMMU/device-agnostic framework for exposing direct device access to user
  21. space in a secure, IOMMU-protected environment. This framework is used for
  22. multiple devices, such as GPUs, network adapters, and compute accelerators. With
  23. direct device access, virtual machines or user space applications have direct
  24. access to the physical device. This framework is reused for mediated devices.
  25. The mediated core driver provides a common interface for mediated device
  26. management that can be used by drivers of different devices. This module
  27. provides a generic interface to perform these operations:
  28. * Create and destroy a mediated device
  29. * Add a mediated device to and remove it from a mediated bus driver
  30. * Add a mediated device to and remove it from an IOMMU group
  31. The mediated core driver also provides an interface to register a bus driver.
  32. For example, the mediated VFIO mdev driver is designed for mediated devices and
  33. supports VFIO APIs. The mediated bus driver adds a mediated device to and
  34. removes it from a VFIO group.
  35. The following high-level block diagram shows the main components and interfaces
  36. in the VFIO mediated driver framework. The diagram shows NVIDIA, Intel, and IBM
  37. devices as examples, as these devices are the first devices to use this module::
  38. +---------------+
  39. | |
  40. | +-----------+ | mdev_register_driver() +--------------+
  41. | | | +<------------------------+ |
  42. | | mdev | | | |
  43. | | bus | +------------------------>+ vfio_mdev.ko |<-> VFIO user
  44. | | driver | | probe()/remove() | | APIs
  45. | | | | +--------------+
  46. | +-----------+ |
  47. | |
  48. | MDEV CORE |
  49. | MODULE |
  50. | mdev.ko |
  51. | +-----------+ | mdev_register_device() +--------------+
  52. | | | +<------------------------+ |
  53. | | | | | nvidia.ko |<-> physical
  54. | | | +------------------------>+ | device
  55. | | | | callbacks +--------------+
  56. | | Physical | |
  57. | | device | | mdev_register_device() +--------------+
  58. | | interface | |<------------------------+ |
  59. | | | | | i915.ko |<-> physical
  60. | | | +------------------------>+ | device
  61. | | | | callbacks +--------------+
  62. | | | |
  63. | | | | mdev_register_device() +--------------+
  64. | | | +<------------------------+ |
  65. | | | | | ccw_device.ko|<-> physical
  66. | | | +------------------------>+ | device
  67. | | | | callbacks +--------------+
  68. | +-----------+ |
  69. +---------------+
  70. Registration Interfaces
  71. =======================
  72. The mediated core driver provides the following types of registration
  73. interfaces:
  74. * Registration interface for a mediated bus driver
  75. * Physical device driver interface
  76. Registration Interface for a Mediated Bus Driver
  77. ------------------------------------------------
  78. The registration interface for a mediated bus driver provides the following
  79. structure to represent a mediated device's driver::
  80. /*
  81. * struct mdev_driver [2] - Mediated device's driver
  82. * @name: driver name
  83. * @probe: called when new device created
  84. * @remove: called when device removed
  85. * @driver: device driver structure
  86. */
  87. struct mdev_driver {
  88. const char *name;
  89. int (*probe) (struct device *dev);
  90. void (*remove) (struct device *dev);
  91. struct device_driver driver;
  92. };
  93. A mediated bus driver for mdev should use this structure in the function calls
  94. to register and unregister itself with the core driver:
  95. * Register::
  96. extern int mdev_register_driver(struct mdev_driver *drv,
  97. struct module *owner);
  98. * Unregister::
  99. extern void mdev_unregister_driver(struct mdev_driver *drv);
  100. The mediated bus driver is responsible for adding mediated devices to the VFIO
  101. group when devices are bound to the driver and removing mediated devices from
  102. the VFIO when devices are unbound from the driver.
  103. Physical Device Driver Interface
  104. --------------------------------
  105. The physical device driver interface provides the mdev_parent_ops[3] structure
  106. to define the APIs to manage work in the mediated core driver that is related
  107. to the physical device.
  108. The structures in the mdev_parent_ops structure are as follows:
  109. * dev_attr_groups: attributes of the parent device
  110. * mdev_attr_groups: attributes of the mediated device
  111. * supported_config: attributes to define supported configurations
  112. The functions in the mdev_parent_ops structure are as follows:
  113. * create: allocate basic resources in a driver for a mediated device
  114. * remove: free resources in a driver when a mediated device is destroyed
  115. (Note that mdev-core provides no implicit serialization of create/remove
  116. callbacks per mdev parent device, per mdev type, or any other categorization.
  117. Vendor drivers are expected to be fully asynchronous in this respect or
  118. provide their own internal resource protection.)
  119. The callbacks in the mdev_parent_ops structure are as follows:
  120. * open: open callback of mediated device
  121. * close: close callback of mediated device
  122. * ioctl: ioctl callback of mediated device
  123. * read : read emulation callback
  124. * write: write emulation callback
  125. * mmap: mmap emulation callback
  126. A driver should use the mdev_parent_ops structure in the function call to
  127. register itself with the mdev core driver::
  128. extern int mdev_register_device(struct device *dev,
  129. const struct mdev_parent_ops *ops);
  130. However, the mdev_parent_ops structure is not required in the function call
  131. that a driver should use to unregister itself with the mdev core driver::
  132. extern void mdev_unregister_device(struct device *dev);
  133. Mediated Device Management Interface Through sysfs
  134. ==================================================
  135. The management interface through sysfs enables user space software, such as
  136. libvirt, to query and configure mediated devices in a hardware-agnostic fashion.
  137. This management interface provides flexibility to the underlying physical
  138. device's driver to support features such as:
  139. * Mediated device hot plug
  140. * Multiple mediated devices in a single virtual machine
  141. * Multiple mediated devices from different physical devices
  142. Links in the mdev_bus Class Directory
  143. -------------------------------------
  144. The /sys/class/mdev_bus/ directory contains links to devices that are registered
  145. with the mdev core driver.
  146. Directories and files under the sysfs for Each Physical Device
  147. --------------------------------------------------------------
  148. ::
  149. |- [parent physical device]
  150. |--- Vendor-specific-attributes [optional]
  151. |--- [mdev_supported_types]
  152. | |--- [<type-id>]
  153. | | |--- create
  154. | | |--- name
  155. | | |--- available_instances
  156. | | |--- device_api
  157. | | |--- description
  158. | | |--- [devices]
  159. | |--- [<type-id>]
  160. | | |--- create
  161. | | |--- name
  162. | | |--- available_instances
  163. | | |--- device_api
  164. | | |--- description
  165. | | |--- [devices]
  166. | |--- [<type-id>]
  167. | |--- create
  168. | |--- name
  169. | |--- available_instances
  170. | |--- device_api
  171. | |--- description
  172. | |--- [devices]
  173. * [mdev_supported_types]
  174. The list of currently supported mediated device types and their details.
  175. [<type-id>], device_api, and available_instances are mandatory attributes
  176. that should be provided by vendor driver.
  177. * [<type-id>]
  178. The [<type-id>] name is created by adding the device driver string as a prefix
  179. to the string provided by the vendor driver. This format of this name is as
  180. follows::
  181. sprintf(buf, "%s-%s", dev_driver_string(parent->dev), group->name);
  182. (or using mdev_parent_dev(mdev) to arrive at the parent device outside
  183. of the core mdev code)
  184. * device_api
  185. This attribute should show which device API is being created, for example,
  186. "vfio-pci" for a PCI device.
  187. * available_instances
  188. This attribute should show the number of devices of type <type-id> that can be
  189. created.
  190. * [device]
  191. This directory contains links to the devices of type <type-id> that have been
  192. created.
  193. * name
  194. This attribute should show human readable name. This is optional attribute.
  195. * description
  196. This attribute should show brief features/description of the type. This is
  197. optional attribute.
  198. Directories and Files Under the sysfs for Each mdev Device
  199. ----------------------------------------------------------
  200. ::
  201. |- [parent phy device]
  202. |--- [$MDEV_UUID]
  203. |--- remove
  204. |--- mdev_type {link to its type}
  205. |--- vendor-specific-attributes [optional]
  206. * remove (write only)
  207. Writing '1' to the 'remove' file destroys the mdev device. The vendor driver can
  208. fail the remove() callback if that device is active and the vendor driver
  209. doesn't support hot unplug.
  210. Example::
  211. # echo 1 > /sys/bus/mdev/devices/$mdev_UUID/remove
  212. Mediated device Hot plug
  213. ------------------------
  214. Mediated devices can be created and assigned at runtime. The procedure to hot
  215. plug a mediated device is the same as the procedure to hot plug a PCI device.
  216. Translation APIs for Mediated Devices
  217. =====================================
  218. The following APIs are provided for translating user pfn to host pfn in a VFIO
  219. driver::
  220. extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
  221. int npage, int prot, unsigned long *phys_pfn);
  222. extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
  223. int npage);
  224. These functions call back into the back-end IOMMU module by using the pin_pages
  225. and unpin_pages callbacks of the struct vfio_iommu_driver_ops[4]. Currently
  226. these callbacks are supported in the TYPE1 IOMMU module. To enable them for
  227. other IOMMU backend modules, such as PPC64 sPAPR module, they need to provide
  228. these two callback functions.
  229. Using the Sample Code
  230. =====================
  231. mtty.c in samples/vfio-mdev/ directory is a sample driver program to
  232. demonstrate how to use the mediated device framework.
  233. The sample driver creates an mdev device that simulates a serial port over a PCI
  234. card.
  235. 1. Build and load the mtty.ko module.
  236. This step creates a dummy device, /sys/devices/virtual/mtty/mtty/
  237. Files in this device directory in sysfs are similar to the following::
  238. # tree /sys/devices/virtual/mtty/mtty/
  239. /sys/devices/virtual/mtty/mtty/
  240. |-- mdev_supported_types
  241. | |-- mtty-1
  242. | | |-- available_instances
  243. | | |-- create
  244. | | |-- device_api
  245. | | |-- devices
  246. | | `-- name
  247. | `-- mtty-2
  248. | |-- available_instances
  249. | |-- create
  250. | |-- device_api
  251. | |-- devices
  252. | `-- name
  253. |-- mtty_dev
  254. | `-- sample_mtty_dev
  255. |-- power
  256. | |-- autosuspend_delay_ms
  257. | |-- control
  258. | |-- runtime_active_time
  259. | |-- runtime_status
  260. | `-- runtime_suspended_time
  261. |-- subsystem -> ../../../../class/mtty
  262. `-- uevent
  263. 2. Create a mediated device by using the dummy device that you created in the
  264. previous step::
  265. # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \
  266. /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create
  267. 3. Add parameters to qemu-kvm::
  268. -device vfio-pci,\
  269. sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001
  270. 4. Boot the VM.
  271. In the Linux guest VM, with no hardware on the host, the device appears
  272. as follows::
  273. # lspci -s 00:05.0 -xxvv
  274. 00:05.0 Serial controller: Device 4348:3253 (rev 10) (prog-if 02 [16550])
  275. Subsystem: Device 4348:3253
  276. Physical Slot: 5
  277. Control: I/O+ Mem- BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr-
  278. Stepping- SERR- FastB2B- DisINTx-
  279. Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=medium >TAbort-
  280. <TAbort- <MAbort- >SERR- <PERR- INTx-
  281. Interrupt: pin A routed to IRQ 10
  282. Region 0: I/O ports at c150 [size=8]
  283. Region 1: I/O ports at c158 [size=8]
  284. Kernel driver in use: serial
  285. 00: 48 43 53 32 01 00 00 02 10 02 00 07 00 00 00 00
  286. 10: 51 c1 00 00 59 c1 00 00 00 00 00 00 00 00 00 00
  287. 20: 00 00 00 00 00 00 00 00 00 00 00 00 48 43 53 32
  288. 30: 00 00 00 00 00 00 00 00 00 00 00 00 0a 01 00 00
  289. In the Linux guest VM, dmesg output for the device is as follows:
  290. serial 0000:00:05.0: PCI INT A -> Link[LNKA] -> GSI 10 (level, high) -> IRQ 10
  291. 0000:00:05.0: ttyS1 at I/O 0xc150 (irq = 10) is a 16550A
  292. 0000:00:05.0: ttyS2 at I/O 0xc158 (irq = 10) is a 16550A
  293. 5. In the Linux guest VM, check the serial ports::
  294. # setserial -g /dev/ttyS*
  295. /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
  296. /dev/ttyS1, UART: 16550A, Port: 0xc150, IRQ: 10
  297. /dev/ttyS2, UART: 16550A, Port: 0xc158, IRQ: 10
  298. 6. Using minicom or any terminal emulation program, open port /dev/ttyS1 or
  299. /dev/ttyS2 with hardware flow control disabled.
  300. 7. Type data on the minicom terminal or send data to the terminal emulation
  301. program and read the data.
  302. Data is loop backed from hosts mtty driver.
  303. 8. Destroy the mediated device that you created::
  304. # echo 1 > /sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001/remove
  305. References
  306. ==========
  307. 1. See Documentation/driver-api/vfio.rst for more information on VFIO.
  308. 2. struct mdev_driver in include/linux/mdev.h
  309. 3. struct mdev_parent_ops in include/linux/mdev.h
  310. 4. struct vfio_iommu_driver_ops in include/linux/vfio.h