1baa293e9SMauro Carvalho Chehab.. include:: <isonum.txt> 2baa293e9SMauro Carvalho Chehab 3baa293e9SMauro Carvalho Chehab===================== 4baa293e9SMauro Carvalho ChehabVFIO Mediated devices 5baa293e9SMauro Carvalho Chehab===================== 6baa293e9SMauro Carvalho Chehab 7baa293e9SMauro Carvalho Chehab:Copyright: |copy| 2016, NVIDIA CORPORATION. All rights reserved. 8baa293e9SMauro Carvalho Chehab:Author: Neo Jia <cjia@nvidia.com> 9baa293e9SMauro Carvalho Chehab:Author: Kirti Wankhede <kwankhede@nvidia.com> 10baa293e9SMauro Carvalho Chehab 11baa293e9SMauro Carvalho ChehabThis program is free software; you can redistribute it and/or modify 12baa293e9SMauro Carvalho Chehabit under the terms of the GNU General Public License version 2 as 13baa293e9SMauro Carvalho Chehabpublished by the Free Software Foundation. 14baa293e9SMauro Carvalho Chehab 15baa293e9SMauro Carvalho Chehab 16baa293e9SMauro Carvalho ChehabVirtual Function I/O (VFIO) Mediated devices[1] 17baa293e9SMauro Carvalho Chehab=============================================== 18baa293e9SMauro Carvalho Chehab 19baa293e9SMauro Carvalho ChehabThe number of use cases for virtualizing DMA devices that do not have built-in 20baa293e9SMauro Carvalho ChehabSR_IOV capability is increasing. Previously, to virtualize such devices, 21baa293e9SMauro Carvalho Chehabdevelopers had to create their own management interfaces and APIs, and then 22baa293e9SMauro Carvalho Chehabintegrate them with user space software. To simplify integration with user space 23baa293e9SMauro Carvalho Chehabsoftware, we have identified common requirements and a unified management 24baa293e9SMauro Carvalho Chehabinterface for such devices. 25baa293e9SMauro Carvalho Chehab 26baa293e9SMauro Carvalho ChehabThe VFIO driver framework provides unified APIs for direct device access. It is 27baa293e9SMauro Carvalho Chehaban IOMMU/device-agnostic framework for exposing direct device access to user 28baa293e9SMauro Carvalho Chehabspace in a secure, IOMMU-protected environment. This framework is used for 29baa293e9SMauro Carvalho Chehabmultiple devices, such as GPUs, network adapters, and compute accelerators. With 30baa293e9SMauro Carvalho Chehabdirect device access, virtual machines or user space applications have direct 31baa293e9SMauro Carvalho Chehabaccess to the physical device. This framework is reused for mediated devices. 32baa293e9SMauro Carvalho Chehab 33baa293e9SMauro Carvalho ChehabThe mediated core driver provides a common interface for mediated device 34baa293e9SMauro Carvalho Chehabmanagement that can be used by drivers of different devices. This module 35baa293e9SMauro Carvalho Chehabprovides a generic interface to perform these operations: 36baa293e9SMauro Carvalho Chehab 37baa293e9SMauro Carvalho Chehab* Create and destroy a mediated device 38baa293e9SMauro Carvalho Chehab* Add a mediated device to and remove it from a mediated bus driver 39baa293e9SMauro Carvalho Chehab* Add a mediated device to and remove it from an IOMMU group 40baa293e9SMauro Carvalho Chehab 41baa293e9SMauro Carvalho ChehabThe mediated core driver also provides an interface to register a bus driver. 42baa293e9SMauro Carvalho ChehabFor example, the mediated VFIO mdev driver is designed for mediated devices and 43baa293e9SMauro Carvalho Chehabsupports VFIO APIs. The mediated bus driver adds a mediated device to and 44baa293e9SMauro Carvalho Chehabremoves it from a VFIO group. 45baa293e9SMauro Carvalho Chehab 46baa293e9SMauro Carvalho ChehabThe following high-level block diagram shows the main components and interfaces 47baa293e9SMauro Carvalho Chehabin the VFIO mediated driver framework. The diagram shows NVIDIA, Intel, and IBM 48baa293e9SMauro Carvalho Chehabdevices as examples, as these devices are the first devices to use this module:: 49baa293e9SMauro Carvalho Chehab 50baa293e9SMauro Carvalho Chehab +---------------+ 51baa293e9SMauro Carvalho Chehab | | 52baa293e9SMauro Carvalho Chehab | +-----------+ | mdev_register_driver() +--------------+ 53baa293e9SMauro Carvalho Chehab | | | +<------------------------+ | 54baa293e9SMauro Carvalho Chehab | | mdev | | | | 55baa293e9SMauro Carvalho Chehab | | bus | +------------------------>+ vfio_mdev.ko |<-> VFIO user 56baa293e9SMauro Carvalho Chehab | | driver | | probe()/remove() | | APIs 57baa293e9SMauro Carvalho Chehab | | | | +--------------+ 58baa293e9SMauro Carvalho Chehab | +-----------+ | 59baa293e9SMauro Carvalho Chehab | | 60baa293e9SMauro Carvalho Chehab | MDEV CORE | 61baa293e9SMauro Carvalho Chehab | MODULE | 62baa293e9SMauro Carvalho Chehab | mdev.ko | 63baa293e9SMauro Carvalho Chehab | +-----------+ | mdev_register_device() +--------------+ 64baa293e9SMauro Carvalho Chehab | | | +<------------------------+ | 65baa293e9SMauro Carvalho Chehab | | | | | nvidia.ko |<-> physical 66baa293e9SMauro Carvalho Chehab | | | +------------------------>+ | device 67baa293e9SMauro Carvalho Chehab | | | | callbacks +--------------+ 68baa293e9SMauro Carvalho Chehab | | Physical | | 69baa293e9SMauro Carvalho Chehab | | device | | mdev_register_device() +--------------+ 70baa293e9SMauro Carvalho Chehab | | interface | |<------------------------+ | 71baa293e9SMauro Carvalho Chehab | | | | | i915.ko |<-> physical 72baa293e9SMauro Carvalho Chehab | | | +------------------------>+ | device 73baa293e9SMauro Carvalho Chehab | | | | callbacks +--------------+ 74baa293e9SMauro Carvalho Chehab | | | | 75baa293e9SMauro Carvalho Chehab | | | | mdev_register_device() +--------------+ 76baa293e9SMauro Carvalho Chehab | | | +<------------------------+ | 77baa293e9SMauro Carvalho Chehab | | | | | ccw_device.ko|<-> physical 78baa293e9SMauro Carvalho Chehab | | | +------------------------>+ | device 79baa293e9SMauro Carvalho Chehab | | | | callbacks +--------------+ 80baa293e9SMauro Carvalho Chehab | +-----------+ | 81baa293e9SMauro Carvalho Chehab +---------------+ 82baa293e9SMauro Carvalho Chehab 83baa293e9SMauro Carvalho Chehab 84baa293e9SMauro Carvalho ChehabRegistration Interfaces 85baa293e9SMauro Carvalho Chehab======================= 86baa293e9SMauro Carvalho Chehab 87baa293e9SMauro Carvalho ChehabThe mediated core driver provides the following types of registration 88baa293e9SMauro Carvalho Chehabinterfaces: 89baa293e9SMauro Carvalho Chehab 90baa293e9SMauro Carvalho Chehab* Registration interface for a mediated bus driver 91baa293e9SMauro Carvalho Chehab* Physical device driver interface 92baa293e9SMauro Carvalho Chehab 93baa293e9SMauro Carvalho ChehabRegistration Interface for a Mediated Bus Driver 94baa293e9SMauro Carvalho Chehab------------------------------------------------ 95baa293e9SMauro Carvalho Chehab 96*88a21f26SJason GunthorpeThe registration interface for a mediated device driver provides the following 97baa293e9SMauro Carvalho Chehabstructure to represent a mediated device's driver:: 98baa293e9SMauro Carvalho Chehab 99baa293e9SMauro Carvalho Chehab /* 100baa293e9SMauro Carvalho Chehab * struct mdev_driver [2] - Mediated device's driver 101baa293e9SMauro Carvalho Chehab * @probe: called when new device created 102baa293e9SMauro Carvalho Chehab * @remove: called when device removed 103baa293e9SMauro Carvalho Chehab * @driver: device driver structure 104baa293e9SMauro Carvalho Chehab */ 105baa293e9SMauro Carvalho Chehab struct mdev_driver { 1062a3d15f2SJason Gunthorpe int (*probe) (struct mdev_device *dev); 1072a3d15f2SJason Gunthorpe void (*remove) (struct mdev_device *dev); 108baa293e9SMauro Carvalho Chehab struct device_driver driver; 109baa293e9SMauro Carvalho Chehab }; 110baa293e9SMauro Carvalho Chehab 111baa293e9SMauro Carvalho ChehabA mediated bus driver for mdev should use this structure in the function calls 112baa293e9SMauro Carvalho Chehabto register and unregister itself with the core driver: 113baa293e9SMauro Carvalho Chehab 114baa293e9SMauro Carvalho Chehab* Register:: 115baa293e9SMauro Carvalho Chehab 11691b9969dSJason Gunthorpe extern int mdev_register_driver(struct mdev_driver *drv); 117baa293e9SMauro Carvalho Chehab 118baa293e9SMauro Carvalho Chehab* Unregister:: 119baa293e9SMauro Carvalho Chehab 120baa293e9SMauro Carvalho Chehab extern void mdev_unregister_driver(struct mdev_driver *drv); 121baa293e9SMauro Carvalho Chehab 122baa293e9SMauro Carvalho ChehabThe mediated bus driver is responsible for adding mediated devices to the VFIO 123baa293e9SMauro Carvalho Chehabgroup when devices are bound to the driver and removing mediated devices from 124baa293e9SMauro Carvalho Chehabthe VFIO when devices are unbound from the driver. 125baa293e9SMauro Carvalho Chehab 126baa293e9SMauro Carvalho Chehab 127baa293e9SMauro Carvalho ChehabPhysical Device Driver Interface 128baa293e9SMauro Carvalho Chehab-------------------------------- 129baa293e9SMauro Carvalho Chehab 130baa293e9SMauro Carvalho ChehabThe physical device driver interface provides the mdev_parent_ops[3] structure 131baa293e9SMauro Carvalho Chehabto define the APIs to manage work in the mediated core driver that is related 132baa293e9SMauro Carvalho Chehabto the physical device. 133baa293e9SMauro Carvalho Chehab 134baa293e9SMauro Carvalho ChehabThe structures in the mdev_parent_ops structure are as follows: 135baa293e9SMauro Carvalho Chehab 136baa293e9SMauro Carvalho Chehab* dev_attr_groups: attributes of the parent device 137baa293e9SMauro Carvalho Chehab* mdev_attr_groups: attributes of the mediated device 138baa293e9SMauro Carvalho Chehab* supported_config: attributes to define supported configurations 139*88a21f26SJason Gunthorpe* device_driver: device driver to bind for mediated device instances 140baa293e9SMauro Carvalho Chehab 141*88a21f26SJason GunthorpeThe mdev_parent_ops also still has various functions pointers. Theses exist 142*88a21f26SJason Gunthorpefor historical reasons only and shall not be used for new drivers. 143baa293e9SMauro Carvalho Chehab 144*88a21f26SJason GunthorpeWhen a driver wants to add the GUID creation sysfs to an existing device it has 145*88a21f26SJason Gunthorpeprobe'd to then it should call:: 146baa293e9SMauro Carvalho Chehab 147baa293e9SMauro Carvalho Chehab extern int mdev_register_device(struct device *dev, 148baa293e9SMauro Carvalho Chehab const struct mdev_parent_ops *ops); 149baa293e9SMauro Carvalho Chehab 150*88a21f26SJason GunthorpeThis will provide the 'mdev_supported_types/XX/create' files which can then be 151*88a21f26SJason Gunthorpeused to trigger the creation of a mdev_device. The created mdev_device will be 152*88a21f26SJason Gunthorpeattached to the specified driver. 153*88a21f26SJason Gunthorpe 154*88a21f26SJason GunthorpeWhen the driver needs to remove itself it calls:: 155baa293e9SMauro Carvalho Chehab 156baa293e9SMauro Carvalho Chehab extern void mdev_unregister_device(struct device *dev); 157baa293e9SMauro Carvalho Chehab 158*88a21f26SJason GunthorpeWhich will unbind and destroy all the created mdevs and remove the sysfs files. 159baa293e9SMauro Carvalho Chehab 160baa293e9SMauro Carvalho ChehabMediated Device Management Interface Through sysfs 161baa293e9SMauro Carvalho Chehab================================================== 162baa293e9SMauro Carvalho Chehab 163baa293e9SMauro Carvalho ChehabThe management interface through sysfs enables user space software, such as 164baa293e9SMauro Carvalho Chehablibvirt, to query and configure mediated devices in a hardware-agnostic fashion. 165baa293e9SMauro Carvalho ChehabThis management interface provides flexibility to the underlying physical 166baa293e9SMauro Carvalho Chehabdevice's driver to support features such as: 167baa293e9SMauro Carvalho Chehab 168baa293e9SMauro Carvalho Chehab* Mediated device hot plug 169baa293e9SMauro Carvalho Chehab* Multiple mediated devices in a single virtual machine 170baa293e9SMauro Carvalho Chehab* Multiple mediated devices from different physical devices 171baa293e9SMauro Carvalho Chehab 172baa293e9SMauro Carvalho ChehabLinks in the mdev_bus Class Directory 173baa293e9SMauro Carvalho Chehab------------------------------------- 174baa293e9SMauro Carvalho ChehabThe /sys/class/mdev_bus/ directory contains links to devices that are registered 175baa293e9SMauro Carvalho Chehabwith the mdev core driver. 176baa293e9SMauro Carvalho Chehab 177baa293e9SMauro Carvalho ChehabDirectories and files under the sysfs for Each Physical Device 178baa293e9SMauro Carvalho Chehab-------------------------------------------------------------- 179baa293e9SMauro Carvalho Chehab 180baa293e9SMauro Carvalho Chehab:: 181baa293e9SMauro Carvalho Chehab 182baa293e9SMauro Carvalho Chehab |- [parent physical device] 183baa293e9SMauro Carvalho Chehab |--- Vendor-specific-attributes [optional] 184baa293e9SMauro Carvalho Chehab |--- [mdev_supported_types] 185baa293e9SMauro Carvalho Chehab | |--- [<type-id>] 186baa293e9SMauro Carvalho Chehab | | |--- create 187baa293e9SMauro Carvalho Chehab | | |--- name 188baa293e9SMauro Carvalho Chehab | | |--- available_instances 189baa293e9SMauro Carvalho Chehab | | |--- device_api 190baa293e9SMauro Carvalho Chehab | | |--- description 191baa293e9SMauro Carvalho Chehab | | |--- [devices] 192baa293e9SMauro Carvalho Chehab | |--- [<type-id>] 193baa293e9SMauro Carvalho Chehab | | |--- create 194baa293e9SMauro Carvalho Chehab | | |--- name 195baa293e9SMauro Carvalho Chehab | | |--- available_instances 196baa293e9SMauro Carvalho Chehab | | |--- device_api 197baa293e9SMauro Carvalho Chehab | | |--- description 198baa293e9SMauro Carvalho Chehab | | |--- [devices] 199baa293e9SMauro Carvalho Chehab | |--- [<type-id>] 200baa293e9SMauro Carvalho Chehab | |--- create 201baa293e9SMauro Carvalho Chehab | |--- name 202baa293e9SMauro Carvalho Chehab | |--- available_instances 203baa293e9SMauro Carvalho Chehab | |--- device_api 204baa293e9SMauro Carvalho Chehab | |--- description 205baa293e9SMauro Carvalho Chehab | |--- [devices] 206baa293e9SMauro Carvalho Chehab 207baa293e9SMauro Carvalho Chehab* [mdev_supported_types] 208baa293e9SMauro Carvalho Chehab 209baa293e9SMauro Carvalho Chehab The list of currently supported mediated device types and their details. 210baa293e9SMauro Carvalho Chehab 211baa293e9SMauro Carvalho Chehab [<type-id>], device_api, and available_instances are mandatory attributes 212baa293e9SMauro Carvalho Chehab that should be provided by vendor driver. 213baa293e9SMauro Carvalho Chehab 214baa293e9SMauro Carvalho Chehab* [<type-id>] 215baa293e9SMauro Carvalho Chehab 216baa293e9SMauro Carvalho Chehab The [<type-id>] name is created by adding the device driver string as a prefix 217baa293e9SMauro Carvalho Chehab to the string provided by the vendor driver. This format of this name is as 218baa293e9SMauro Carvalho Chehab follows:: 219baa293e9SMauro Carvalho Chehab 220baa293e9SMauro Carvalho Chehab sprintf(buf, "%s-%s", dev_driver_string(parent->dev), group->name); 221baa293e9SMauro Carvalho Chehab 222baa293e9SMauro Carvalho Chehab (or using mdev_parent_dev(mdev) to arrive at the parent device outside 223baa293e9SMauro Carvalho Chehab of the core mdev code) 224baa293e9SMauro Carvalho Chehab 225baa293e9SMauro Carvalho Chehab* device_api 226baa293e9SMauro Carvalho Chehab 227baa293e9SMauro Carvalho Chehab This attribute should show which device API is being created, for example, 228baa293e9SMauro Carvalho Chehab "vfio-pci" for a PCI device. 229baa293e9SMauro Carvalho Chehab 230baa293e9SMauro Carvalho Chehab* available_instances 231baa293e9SMauro Carvalho Chehab 232baa293e9SMauro Carvalho Chehab This attribute should show the number of devices of type <type-id> that can be 233baa293e9SMauro Carvalho Chehab created. 234baa293e9SMauro Carvalho Chehab 235baa293e9SMauro Carvalho Chehab* [device] 236baa293e9SMauro Carvalho Chehab 237baa293e9SMauro Carvalho Chehab This directory contains links to the devices of type <type-id> that have been 238baa293e9SMauro Carvalho Chehab created. 239baa293e9SMauro Carvalho Chehab 240baa293e9SMauro Carvalho Chehab* name 241baa293e9SMauro Carvalho Chehab 242baa293e9SMauro Carvalho Chehab This attribute should show human readable name. This is optional attribute. 243baa293e9SMauro Carvalho Chehab 244baa293e9SMauro Carvalho Chehab* description 245baa293e9SMauro Carvalho Chehab 246baa293e9SMauro Carvalho Chehab This attribute should show brief features/description of the type. This is 247baa293e9SMauro Carvalho Chehab optional attribute. 248baa293e9SMauro Carvalho Chehab 249baa293e9SMauro Carvalho ChehabDirectories and Files Under the sysfs for Each mdev Device 250baa293e9SMauro Carvalho Chehab---------------------------------------------------------- 251baa293e9SMauro Carvalho Chehab 252baa293e9SMauro Carvalho Chehab:: 253baa293e9SMauro Carvalho Chehab 254baa293e9SMauro Carvalho Chehab |- [parent phy device] 255baa293e9SMauro Carvalho Chehab |--- [$MDEV_UUID] 256baa293e9SMauro Carvalho Chehab |--- remove 257baa293e9SMauro Carvalho Chehab |--- mdev_type {link to its type} 258baa293e9SMauro Carvalho Chehab |--- vendor-specific-attributes [optional] 259baa293e9SMauro Carvalho Chehab 260baa293e9SMauro Carvalho Chehab* remove (write only) 261baa293e9SMauro Carvalho Chehab 262baa293e9SMauro Carvalho ChehabWriting '1' to the 'remove' file destroys the mdev device. The vendor driver can 263baa293e9SMauro Carvalho Chehabfail the remove() callback if that device is active and the vendor driver 264baa293e9SMauro Carvalho Chehabdoesn't support hot unplug. 265baa293e9SMauro Carvalho Chehab 266baa293e9SMauro Carvalho ChehabExample:: 267baa293e9SMauro Carvalho Chehab 268baa293e9SMauro Carvalho Chehab # echo 1 > /sys/bus/mdev/devices/$mdev_UUID/remove 269baa293e9SMauro Carvalho Chehab 270baa293e9SMauro Carvalho ChehabMediated device Hot plug 271baa293e9SMauro Carvalho Chehab------------------------ 272baa293e9SMauro Carvalho Chehab 273baa293e9SMauro Carvalho ChehabMediated devices can be created and assigned at runtime. The procedure to hot 274baa293e9SMauro Carvalho Chehabplug a mediated device is the same as the procedure to hot plug a PCI device. 275baa293e9SMauro Carvalho Chehab 276baa293e9SMauro Carvalho ChehabTranslation APIs for Mediated Devices 277baa293e9SMauro Carvalho Chehab===================================== 278baa293e9SMauro Carvalho Chehab 279baa293e9SMauro Carvalho ChehabThe following APIs are provided for translating user pfn to host pfn in a VFIO 280baa293e9SMauro Carvalho Chehabdriver:: 281baa293e9SMauro Carvalho Chehab 282baa293e9SMauro Carvalho Chehab extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, 283baa293e9SMauro Carvalho Chehab int npage, int prot, unsigned long *phys_pfn); 284baa293e9SMauro Carvalho Chehab 285baa293e9SMauro Carvalho Chehab extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, 286baa293e9SMauro Carvalho Chehab int npage); 287baa293e9SMauro Carvalho Chehab 288baa293e9SMauro Carvalho ChehabThese functions call back into the back-end IOMMU module by using the pin_pages 289baa293e9SMauro Carvalho Chehaband unpin_pages callbacks of the struct vfio_iommu_driver_ops[4]. Currently 290baa293e9SMauro Carvalho Chehabthese callbacks are supported in the TYPE1 IOMMU module. To enable them for 291baa293e9SMauro Carvalho Chehabother IOMMU backend modules, such as PPC64 sPAPR module, they need to provide 292baa293e9SMauro Carvalho Chehabthese two callback functions. 293baa293e9SMauro Carvalho Chehab 294baa293e9SMauro Carvalho ChehabUsing the Sample Code 295baa293e9SMauro Carvalho Chehab===================== 296baa293e9SMauro Carvalho Chehab 297baa293e9SMauro Carvalho Chehabmtty.c in samples/vfio-mdev/ directory is a sample driver program to 298baa293e9SMauro Carvalho Chehabdemonstrate how to use the mediated device framework. 299baa293e9SMauro Carvalho Chehab 300baa293e9SMauro Carvalho ChehabThe sample driver creates an mdev device that simulates a serial port over a PCI 301baa293e9SMauro Carvalho Chehabcard. 302baa293e9SMauro Carvalho Chehab 303baa293e9SMauro Carvalho Chehab1. Build and load the mtty.ko module. 304baa293e9SMauro Carvalho Chehab 305baa293e9SMauro Carvalho Chehab This step creates a dummy device, /sys/devices/virtual/mtty/mtty/ 306baa293e9SMauro Carvalho Chehab 307baa293e9SMauro Carvalho Chehab Files in this device directory in sysfs are similar to the following:: 308baa293e9SMauro Carvalho Chehab 309baa293e9SMauro Carvalho Chehab # tree /sys/devices/virtual/mtty/mtty/ 310baa293e9SMauro Carvalho Chehab /sys/devices/virtual/mtty/mtty/ 311baa293e9SMauro Carvalho Chehab |-- mdev_supported_types 312baa293e9SMauro Carvalho Chehab | |-- mtty-1 313baa293e9SMauro Carvalho Chehab | | |-- available_instances 314baa293e9SMauro Carvalho Chehab | | |-- create 315baa293e9SMauro Carvalho Chehab | | |-- device_api 316baa293e9SMauro Carvalho Chehab | | |-- devices 317baa293e9SMauro Carvalho Chehab | | `-- name 318baa293e9SMauro Carvalho Chehab | `-- mtty-2 319baa293e9SMauro Carvalho Chehab | |-- available_instances 320baa293e9SMauro Carvalho Chehab | |-- create 321baa293e9SMauro Carvalho Chehab | |-- device_api 322baa293e9SMauro Carvalho Chehab | |-- devices 323baa293e9SMauro Carvalho Chehab | `-- name 324baa293e9SMauro Carvalho Chehab |-- mtty_dev 325baa293e9SMauro Carvalho Chehab | `-- sample_mtty_dev 326baa293e9SMauro Carvalho Chehab |-- power 327baa293e9SMauro Carvalho Chehab | |-- autosuspend_delay_ms 328baa293e9SMauro Carvalho Chehab | |-- control 329baa293e9SMauro Carvalho Chehab | |-- runtime_active_time 330baa293e9SMauro Carvalho Chehab | |-- runtime_status 331baa293e9SMauro Carvalho Chehab | `-- runtime_suspended_time 332baa293e9SMauro Carvalho Chehab |-- subsystem -> ../../../../class/mtty 333baa293e9SMauro Carvalho Chehab `-- uevent 334baa293e9SMauro Carvalho Chehab 335baa293e9SMauro Carvalho Chehab2. Create a mediated device by using the dummy device that you created in the 336baa293e9SMauro Carvalho Chehab previous step:: 337baa293e9SMauro Carvalho Chehab 338baa293e9SMauro Carvalho Chehab # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \ 339baa293e9SMauro Carvalho Chehab /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create 340baa293e9SMauro Carvalho Chehab 341baa293e9SMauro Carvalho Chehab3. Add parameters to qemu-kvm:: 342baa293e9SMauro Carvalho Chehab 343baa293e9SMauro Carvalho Chehab -device vfio-pci,\ 344baa293e9SMauro Carvalho Chehab sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001 345baa293e9SMauro Carvalho Chehab 346baa293e9SMauro Carvalho Chehab4. Boot the VM. 347baa293e9SMauro Carvalho Chehab 348baa293e9SMauro Carvalho Chehab In the Linux guest VM, with no hardware on the host, the device appears 349baa293e9SMauro Carvalho Chehab as follows:: 350baa293e9SMauro Carvalho Chehab 351baa293e9SMauro Carvalho Chehab # lspci -s 00:05.0 -xxvv 352baa293e9SMauro Carvalho Chehab 00:05.0 Serial controller: Device 4348:3253 (rev 10) (prog-if 02 [16550]) 353baa293e9SMauro Carvalho Chehab Subsystem: Device 4348:3253 354baa293e9SMauro Carvalho Chehab Physical Slot: 5 355baa293e9SMauro Carvalho Chehab Control: I/O+ Mem- BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr- 356baa293e9SMauro Carvalho Chehab Stepping- SERR- FastB2B- DisINTx- 357baa293e9SMauro Carvalho Chehab Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=medium >TAbort- 358baa293e9SMauro Carvalho Chehab <TAbort- <MAbort- >SERR- <PERR- INTx- 359baa293e9SMauro Carvalho Chehab Interrupt: pin A routed to IRQ 10 360baa293e9SMauro Carvalho Chehab Region 0: I/O ports at c150 [size=8] 361baa293e9SMauro Carvalho Chehab Region 1: I/O ports at c158 [size=8] 362baa293e9SMauro Carvalho Chehab Kernel driver in use: serial 363baa293e9SMauro Carvalho Chehab 00: 48 43 53 32 01 00 00 02 10 02 00 07 00 00 00 00 364baa293e9SMauro Carvalho Chehab 10: 51 c1 00 00 59 c1 00 00 00 00 00 00 00 00 00 00 365baa293e9SMauro Carvalho Chehab 20: 00 00 00 00 00 00 00 00 00 00 00 00 48 43 53 32 366baa293e9SMauro Carvalho Chehab 30: 00 00 00 00 00 00 00 00 00 00 00 00 0a 01 00 00 367baa293e9SMauro Carvalho Chehab 368baa293e9SMauro Carvalho Chehab In the Linux guest VM, dmesg output for the device is as follows: 369baa293e9SMauro Carvalho Chehab 370baa293e9SMauro Carvalho Chehab serial 0000:00:05.0: PCI INT A -> Link[LNKA] -> GSI 10 (level, high) -> IRQ 10 371baa293e9SMauro Carvalho Chehab 0000:00:05.0: ttyS1 at I/O 0xc150 (irq = 10) is a 16550A 372baa293e9SMauro Carvalho Chehab 0000:00:05.0: ttyS2 at I/O 0xc158 (irq = 10) is a 16550A 373baa293e9SMauro Carvalho Chehab 374baa293e9SMauro Carvalho Chehab 375baa293e9SMauro Carvalho Chehab5. In the Linux guest VM, check the serial ports:: 376baa293e9SMauro Carvalho Chehab 377baa293e9SMauro Carvalho Chehab # setserial -g /dev/ttyS* 378baa293e9SMauro Carvalho Chehab /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4 379baa293e9SMauro Carvalho Chehab /dev/ttyS1, UART: 16550A, Port: 0xc150, IRQ: 10 380baa293e9SMauro Carvalho Chehab /dev/ttyS2, UART: 16550A, Port: 0xc158, IRQ: 10 381baa293e9SMauro Carvalho Chehab 382baa293e9SMauro Carvalho Chehab6. Using minicom or any terminal emulation program, open port /dev/ttyS1 or 383baa293e9SMauro Carvalho Chehab /dev/ttyS2 with hardware flow control disabled. 384baa293e9SMauro Carvalho Chehab 385baa293e9SMauro Carvalho Chehab7. Type data on the minicom terminal or send data to the terminal emulation 386baa293e9SMauro Carvalho Chehab program and read the data. 387baa293e9SMauro Carvalho Chehab 388baa293e9SMauro Carvalho Chehab Data is loop backed from hosts mtty driver. 389baa293e9SMauro Carvalho Chehab 390baa293e9SMauro Carvalho Chehab8. Destroy the mediated device that you created:: 391baa293e9SMauro Carvalho Chehab 392baa293e9SMauro Carvalho Chehab # echo 1 > /sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001/remove 393baa293e9SMauro Carvalho Chehab 394baa293e9SMauro Carvalho ChehabReferences 395baa293e9SMauro Carvalho Chehab========== 396baa293e9SMauro Carvalho Chehab 397baa293e9SMauro Carvalho Chehab1. See Documentation/driver-api/vfio.rst for more information on VFIO. 398baa293e9SMauro Carvalho Chehab2. struct mdev_driver in include/linux/mdev.h 399baa293e9SMauro Carvalho Chehab3. struct mdev_parent_ops in include/linux/mdev.h 400baa293e9SMauro Carvalho Chehab4. struct vfio_iommu_driver_ops in include/linux/vfio.h 401