0001 .. SPDX-License-Identifier: GPL-2.0-only
0002 .. include:: <isonum.txt>
0003
0004 =====================
0005 VFIO Mediated devices
0006 =====================
0007
0008 :Copyright: |copy| 2016, NVIDIA CORPORATION. All rights reserved.
0009 :Author: Neo Jia <cjia@nvidia.com>
0010 :Author: Kirti Wankhede <kwankhede@nvidia.com>
0011
0012
0013
0014 Virtual Function I/O (VFIO) Mediated devices[1]
0015 ===============================================
0016
0017 The number of use cases for virtualizing DMA devices that do not have built-in
0018 SR_IOV capability is increasing. Previously, to virtualize such devices,
0019 developers had to create their own management interfaces and APIs, and then
0020 integrate them with user space software. To simplify integration with user space
0021 software, we have identified common requirements and a unified management
0022 interface for such devices.
0023
0024 The VFIO driver framework provides unified APIs for direct device access. It is
0025 an IOMMU/device-agnostic framework for exposing direct device access to user
0026 space in a secure, IOMMU-protected environment. This framework is used for
0027 multiple devices, such as GPUs, network adapters, and compute accelerators. With
0028 direct device access, virtual machines or user space applications have direct
0029 access to the physical device. This framework is reused for mediated devices.
0030
0031 The mediated core driver provides a common interface for mediated device
0032 management that can be used by drivers of different devices. This module
0033 provides a generic interface to perform these operations:
0034
0035 * Create and destroy a mediated device
0036 * Add a mediated device to and remove it from a mediated bus driver
0037 * Add a mediated device to and remove it from an IOMMU group
0038
0039 The mediated core driver also provides an interface to register a bus driver.
0040 For example, the mediated VFIO mdev driver is designed for mediated devices and
0041 supports VFIO APIs. The mediated bus driver adds a mediated device to and
0042 removes it from a VFIO group.
0043
0044 The following high-level block diagram shows the main components and interfaces
0045 in the VFIO mediated driver framework. The diagram shows NVIDIA, Intel, and IBM
0046 devices as examples, as these devices are the first devices to use this module::
0047
0048 +---------------+
0049 | |
0050 | +-----------+ | mdev_register_driver() +--------------+
0051 | | | +<------------------------+ |
0052 | | mdev | | | |
0053 | | bus | +------------------------>+ vfio_mdev.ko |<-> VFIO user
0054 | | driver | | probe()/remove() | | APIs
0055 | | | | +--------------+
0056 | +-----------+ |
0057 | |
0058 | MDEV CORE |
0059 | MODULE |
0060 | mdev.ko |
0061 | +-----------+ | mdev_register_device() +--------------+
0062 | | | +<------------------------+ |
0063 | | | | | nvidia.ko |<-> physical
0064 | | | +------------------------>+ | device
0065 | | | | callbacks +--------------+
0066 | | Physical | |
0067 | | device | | mdev_register_device() +--------------+
0068 | | interface | |<------------------------+ |
0069 | | | | | i915.ko |<-> physical
0070 | | | +------------------------>+ | device
0071 | | | | callbacks +--------------+
0072 | | | |
0073 | | | | mdev_register_device() +--------------+
0074 | | | +<------------------------+ |
0075 | | | | | ccw_device.ko|<-> physical
0076 | | | +------------------------>+ | device
0077 | | | | callbacks +--------------+
0078 | +-----------+ |
0079 +---------------+
0080
0081
0082 Registration Interfaces
0083 =======================
0084
0085 The mediated core driver provides the following types of registration
0086 interfaces:
0087
0088 * Registration interface for a mediated bus driver
0089 * Physical device driver interface
0090
0091 Registration Interface for a Mediated Bus Driver
0092 ------------------------------------------------
0093
0094 The registration interface for a mediated device driver provides the following
0095 structure to represent a mediated device's driver::
0096
0097 /*
0098 * struct mdev_driver [2] - Mediated device's driver
0099 * @probe: called when new device created
0100 * @remove: called when device removed
0101 * @driver: device driver structure
0102 */
0103 struct mdev_driver {
0104 int (*probe) (struct mdev_device *dev);
0105 void (*remove) (struct mdev_device *dev);
0106 struct attribute_group **supported_type_groups;
0107 struct device_driver driver;
0108 };
0109
0110 A mediated bus driver for mdev should use this structure in the function calls
0111 to register and unregister itself with the core driver:
0112
0113 * Register::
0114
0115 int mdev_register_driver(struct mdev_driver *drv);
0116
0117 * Unregister::
0118
0119 void mdev_unregister_driver(struct mdev_driver *drv);
0120
0121 The mediated bus driver's probe function should create a vfio_device on top of
0122 the mdev_device and connect it to an appropriate implementation of
0123 vfio_device_ops.
0124
0125 When a driver wants to add the GUID creation sysfs to an existing device it has
0126 probe'd to then it should call::
0127
0128 int mdev_register_device(struct device *dev,
0129 struct mdev_driver *mdev_driver);
0130
0131 This will provide the 'mdev_supported_types/XX/create' files which can then be
0132 used to trigger the creation of a mdev_device. The created mdev_device will be
0133 attached to the specified driver.
0134
0135 When the driver needs to remove itself it calls::
0136
0137 void mdev_unregister_device(struct device *dev);
0138
0139 Which will unbind and destroy all the created mdevs and remove the sysfs files.
0140
0141 Mediated Device Management Interface Through sysfs
0142 ==================================================
0143
0144 The management interface through sysfs enables user space software, such as
0145 libvirt, to query and configure mediated devices in a hardware-agnostic fashion.
0146 This management interface provides flexibility to the underlying physical
0147 device's driver to support features such as:
0148
0149 * Mediated device hot plug
0150 * Multiple mediated devices in a single virtual machine
0151 * Multiple mediated devices from different physical devices
0152
0153 Links in the mdev_bus Class Directory
0154 -------------------------------------
0155 The /sys/class/mdev_bus/ directory contains links to devices that are registered
0156 with the mdev core driver.
0157
0158 Directories and files under the sysfs for Each Physical Device
0159 --------------------------------------------------------------
0160
0161 ::
0162
0163 |- [parent physical device]
0164 |--- Vendor-specific-attributes [optional]
0165 |--- [mdev_supported_types]
0166 | |--- [<type-id>]
0167 | | |--- create
0168 | | |--- name
0169 | | |--- available_instances
0170 | | |--- device_api
0171 | | |--- description
0172 | | |--- [devices]
0173 | |--- [<type-id>]
0174 | | |--- create
0175 | | |--- name
0176 | | |--- available_instances
0177 | | |--- device_api
0178 | | |--- description
0179 | | |--- [devices]
0180 | |--- [<type-id>]
0181 | |--- create
0182 | |--- name
0183 | |--- available_instances
0184 | |--- device_api
0185 | |--- description
0186 | |--- [devices]
0187
0188 * [mdev_supported_types]
0189
0190 The list of currently supported mediated device types and their details.
0191
0192 [<type-id>], device_api, and available_instances are mandatory attributes
0193 that should be provided by vendor driver.
0194
0195 * [<type-id>]
0196
0197 The [<type-id>] name is created by adding the device driver string as a prefix
0198 to the string provided by the vendor driver. This format of this name is as
0199 follows::
0200
0201 sprintf(buf, "%s-%s", dev_driver_string(parent->dev), group->name);
0202
0203 (or using mdev_parent_dev(mdev) to arrive at the parent device outside
0204 of the core mdev code)
0205
0206 * device_api
0207
0208 This attribute should show which device API is being created, for example,
0209 "vfio-pci" for a PCI device.
0210
0211 * available_instances
0212
0213 This attribute should show the number of devices of type <type-id> that can be
0214 created.
0215
0216 * [device]
0217
0218 This directory contains links to the devices of type <type-id> that have been
0219 created.
0220
0221 * name
0222
0223 This attribute should show human readable name. This is optional attribute.
0224
0225 * description
0226
0227 This attribute should show brief features/description of the type. This is
0228 optional attribute.
0229
0230 Directories and Files Under the sysfs for Each mdev Device
0231 ----------------------------------------------------------
0232
0233 ::
0234
0235 |- [parent phy device]
0236 |--- [$MDEV_UUID]
0237 |--- remove
0238 |--- mdev_type {link to its type}
0239 |--- vendor-specific-attributes [optional]
0240
0241 * remove (write only)
0242
0243 Writing '1' to the 'remove' file destroys the mdev device. The vendor driver can
0244 fail the remove() callback if that device is active and the vendor driver
0245 doesn't support hot unplug.
0246
0247 Example::
0248
0249 # echo 1 > /sys/bus/mdev/devices/$mdev_UUID/remove
0250
0251 Mediated device Hot plug
0252 ------------------------
0253
0254 Mediated devices can be created and assigned at runtime. The procedure to hot
0255 plug a mediated device is the same as the procedure to hot plug a PCI device.
0256
0257 Translation APIs for Mediated Devices
0258 =====================================
0259
0260 The following APIs are provided for translating user pfn to host pfn in a VFIO
0261 driver::
0262
0263 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
0264 int npage, int prot, struct page **pages);
0265
0266 void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova,
0267 int npage);
0268
0269 These functions call back into the back-end IOMMU module by using the pin_pages
0270 and unpin_pages callbacks of the struct vfio_iommu_driver_ops[4]. Currently
0271 these callbacks are supported in the TYPE1 IOMMU module. To enable them for
0272 other IOMMU backend modules, such as PPC64 sPAPR module, they need to provide
0273 these two callback functions.
0274
0275 Using the Sample Code
0276 =====================
0277
0278 mtty.c in samples/vfio-mdev/ directory is a sample driver program to
0279 demonstrate how to use the mediated device framework.
0280
0281 The sample driver creates an mdev device that simulates a serial port over a PCI
0282 card.
0283
0284 1. Build and load the mtty.ko module.
0285
0286 This step creates a dummy device, /sys/devices/virtual/mtty/mtty/
0287
0288 Files in this device directory in sysfs are similar to the following::
0289
0290 # tree /sys/devices/virtual/mtty/mtty/
0291 /sys/devices/virtual/mtty/mtty/
0292 |-- mdev_supported_types
0293 | |-- mtty-1
0294 | | |-- available_instances
0295 | | |-- create
0296 | | |-- device_api
0297 | | |-- devices
0298 | | `-- name
0299 | `-- mtty-2
0300 | |-- available_instances
0301 | |-- create
0302 | |-- device_api
0303 | |-- devices
0304 | `-- name
0305 |-- mtty_dev
0306 | `-- sample_mtty_dev
0307 |-- power
0308 | |-- autosuspend_delay_ms
0309 | |-- control
0310 | |-- runtime_active_time
0311 | |-- runtime_status
0312 | `-- runtime_suspended_time
0313 |-- subsystem -> ../../../../class/mtty
0314 `-- uevent
0315
0316 2. Create a mediated device by using the dummy device that you created in the
0317 previous step::
0318
0319 # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \
0320 /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create
0321
0322 3. Add parameters to qemu-kvm::
0323
0324 -device vfio-pci,\
0325 sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001
0326
0327 4. Boot the VM.
0328
0329 In the Linux guest VM, with no hardware on the host, the device appears
0330 as follows::
0331
0332 # lspci -s 00:05.0 -xxvv
0333 00:05.0 Serial controller: Device 4348:3253 (rev 10) (prog-if 02 [16550])
0334 Subsystem: Device 4348:3253
0335 Physical Slot: 5
0336 Control: I/O+ Mem- BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr-
0337 Stepping- SERR- FastB2B- DisINTx-
0338 Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=medium >TAbort-
0339 <TAbort- <MAbort- >SERR- <PERR- INTx-
0340 Interrupt: pin A routed to IRQ 10
0341 Region 0: I/O ports at c150 [size=8]
0342 Region 1: I/O ports at c158 [size=8]
0343 Kernel driver in use: serial
0344 00: 48 43 53 32 01 00 00 02 10 02 00 07 00 00 00 00
0345 10: 51 c1 00 00 59 c1 00 00 00 00 00 00 00 00 00 00
0346 20: 00 00 00 00 00 00 00 00 00 00 00 00 48 43 53 32
0347 30: 00 00 00 00 00 00 00 00 00 00 00 00 0a 01 00 00
0348
0349 In the Linux guest VM, dmesg output for the device is as follows:
0350
0351 serial 0000:00:05.0: PCI INT A -> Link[LNKA] -> GSI 10 (level, high) -> IRQ 10
0352 0000:00:05.0: ttyS1 at I/O 0xc150 (irq = 10) is a 16550A
0353 0000:00:05.0: ttyS2 at I/O 0xc158 (irq = 10) is a 16550A
0354
0355
0356 5. In the Linux guest VM, check the serial ports::
0357
0358 # setserial -g /dev/ttyS*
0359 /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
0360 /dev/ttyS1, UART: 16550A, Port: 0xc150, IRQ: 10
0361 /dev/ttyS2, UART: 16550A, Port: 0xc158, IRQ: 10
0362
0363 6. Using minicom or any terminal emulation program, open port /dev/ttyS1 or
0364 /dev/ttyS2 with hardware flow control disabled.
0365
0366 7. Type data on the minicom terminal or send data to the terminal emulation
0367 program and read the data.
0368
0369 Data is loop backed from hosts mtty driver.
0370
0371 8. Destroy the mediated device that you created::
0372
0373 # echo 1 > /sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001/remove
0374
0375 References
0376 ==========
0377
0378 1. See Documentation/driver-api/vfio.rst for more information on VFIO.
0379 2. struct mdev_driver in include/linux/mdev.h
0380 3. struct mdev_parent_ops in include/linux/mdev.h
0381 4. struct vfio_iommu_driver_ops in include/linux/vfio.h