xref: /linux/Documentation/driver-api/vfio-mediated-device.rst (revision 91b9969d9c6bb7c02253bbfc536bfd892f636fdc)
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
96baa293e9SMauro Carvalho ChehabThe registration interface for a mediated bus 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
116*91b9969dSJason 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
139baa293e9SMauro Carvalho Chehab
140baa293e9SMauro Carvalho ChehabThe functions in the mdev_parent_ops structure are as follows:
141baa293e9SMauro Carvalho Chehab
142baa293e9SMauro Carvalho Chehab* create: allocate basic resources in a driver for a mediated device
143baa293e9SMauro Carvalho Chehab* remove: free resources in a driver when a mediated device is destroyed
144baa293e9SMauro Carvalho Chehab
145baa293e9SMauro Carvalho Chehab(Note that mdev-core provides no implicit serialization of create/remove
146baa293e9SMauro Carvalho Chehabcallbacks per mdev parent device, per mdev type, or any other categorization.
147baa293e9SMauro Carvalho ChehabVendor drivers are expected to be fully asynchronous in this respect or
148baa293e9SMauro Carvalho Chehabprovide their own internal resource protection.)
149baa293e9SMauro Carvalho Chehab
150baa293e9SMauro Carvalho ChehabThe callbacks in the mdev_parent_ops structure are as follows:
151baa293e9SMauro Carvalho Chehab
152baa293e9SMauro Carvalho Chehab* open: open callback of mediated device
153baa293e9SMauro Carvalho Chehab* close: close callback of mediated device
154baa293e9SMauro Carvalho Chehab* ioctl: ioctl callback of mediated device
155baa293e9SMauro Carvalho Chehab* read : read emulation callback
156baa293e9SMauro Carvalho Chehab* write: write emulation callback
157baa293e9SMauro Carvalho Chehab* mmap: mmap emulation callback
158baa293e9SMauro Carvalho Chehab
159baa293e9SMauro Carvalho ChehabA driver should use the mdev_parent_ops structure in the function call to
160baa293e9SMauro Carvalho Chehabregister itself with the mdev core driver::
161baa293e9SMauro Carvalho Chehab
162baa293e9SMauro Carvalho Chehab	extern int  mdev_register_device(struct device *dev,
163baa293e9SMauro Carvalho Chehab	                                 const struct mdev_parent_ops *ops);
164baa293e9SMauro Carvalho Chehab
165baa293e9SMauro Carvalho ChehabHowever, the mdev_parent_ops structure is not required in the function call
166baa293e9SMauro Carvalho Chehabthat a driver should use to unregister itself with the mdev core driver::
167baa293e9SMauro Carvalho Chehab
168baa293e9SMauro Carvalho Chehab	extern void mdev_unregister_device(struct device *dev);
169baa293e9SMauro Carvalho Chehab
170baa293e9SMauro Carvalho Chehab
171baa293e9SMauro Carvalho ChehabMediated Device Management Interface Through sysfs
172baa293e9SMauro Carvalho Chehab==================================================
173baa293e9SMauro Carvalho Chehab
174baa293e9SMauro Carvalho ChehabThe management interface through sysfs enables user space software, such as
175baa293e9SMauro Carvalho Chehablibvirt, to query and configure mediated devices in a hardware-agnostic fashion.
176baa293e9SMauro Carvalho ChehabThis management interface provides flexibility to the underlying physical
177baa293e9SMauro Carvalho Chehabdevice's driver to support features such as:
178baa293e9SMauro Carvalho Chehab
179baa293e9SMauro Carvalho Chehab* Mediated device hot plug
180baa293e9SMauro Carvalho Chehab* Multiple mediated devices in a single virtual machine
181baa293e9SMauro Carvalho Chehab* Multiple mediated devices from different physical devices
182baa293e9SMauro Carvalho Chehab
183baa293e9SMauro Carvalho ChehabLinks in the mdev_bus Class Directory
184baa293e9SMauro Carvalho Chehab-------------------------------------
185baa293e9SMauro Carvalho ChehabThe /sys/class/mdev_bus/ directory contains links to devices that are registered
186baa293e9SMauro Carvalho Chehabwith the mdev core driver.
187baa293e9SMauro Carvalho Chehab
188baa293e9SMauro Carvalho ChehabDirectories and files under the sysfs for Each Physical Device
189baa293e9SMauro Carvalho Chehab--------------------------------------------------------------
190baa293e9SMauro Carvalho Chehab
191baa293e9SMauro Carvalho Chehab::
192baa293e9SMauro Carvalho Chehab
193baa293e9SMauro Carvalho Chehab  |- [parent physical device]
194baa293e9SMauro Carvalho Chehab  |--- Vendor-specific-attributes [optional]
195baa293e9SMauro Carvalho Chehab  |--- [mdev_supported_types]
196baa293e9SMauro Carvalho Chehab  |     |--- [<type-id>]
197baa293e9SMauro Carvalho Chehab  |     |   |--- create
198baa293e9SMauro Carvalho Chehab  |     |   |--- name
199baa293e9SMauro Carvalho Chehab  |     |   |--- available_instances
200baa293e9SMauro Carvalho Chehab  |     |   |--- device_api
201baa293e9SMauro Carvalho Chehab  |     |   |--- description
202baa293e9SMauro Carvalho Chehab  |     |   |--- [devices]
203baa293e9SMauro Carvalho Chehab  |     |--- [<type-id>]
204baa293e9SMauro Carvalho Chehab  |     |   |--- create
205baa293e9SMauro Carvalho Chehab  |     |   |--- name
206baa293e9SMauro Carvalho Chehab  |     |   |--- available_instances
207baa293e9SMauro Carvalho Chehab  |     |   |--- device_api
208baa293e9SMauro Carvalho Chehab  |     |   |--- description
209baa293e9SMauro Carvalho Chehab  |     |   |--- [devices]
210baa293e9SMauro Carvalho Chehab  |     |--- [<type-id>]
211baa293e9SMauro Carvalho Chehab  |          |--- create
212baa293e9SMauro Carvalho Chehab  |          |--- name
213baa293e9SMauro Carvalho Chehab  |          |--- available_instances
214baa293e9SMauro Carvalho Chehab  |          |--- device_api
215baa293e9SMauro Carvalho Chehab  |          |--- description
216baa293e9SMauro Carvalho Chehab  |          |--- [devices]
217baa293e9SMauro Carvalho Chehab
218baa293e9SMauro Carvalho Chehab* [mdev_supported_types]
219baa293e9SMauro Carvalho Chehab
220baa293e9SMauro Carvalho Chehab  The list of currently supported mediated device types and their details.
221baa293e9SMauro Carvalho Chehab
222baa293e9SMauro Carvalho Chehab  [<type-id>], device_api, and available_instances are mandatory attributes
223baa293e9SMauro Carvalho Chehab  that should be provided by vendor driver.
224baa293e9SMauro Carvalho Chehab
225baa293e9SMauro Carvalho Chehab* [<type-id>]
226baa293e9SMauro Carvalho Chehab
227baa293e9SMauro Carvalho Chehab  The [<type-id>] name is created by adding the device driver string as a prefix
228baa293e9SMauro Carvalho Chehab  to the string provided by the vendor driver. This format of this name is as
229baa293e9SMauro Carvalho Chehab  follows::
230baa293e9SMauro Carvalho Chehab
231baa293e9SMauro Carvalho Chehab	sprintf(buf, "%s-%s", dev_driver_string(parent->dev), group->name);
232baa293e9SMauro Carvalho Chehab
233baa293e9SMauro Carvalho Chehab  (or using mdev_parent_dev(mdev) to arrive at the parent device outside
234baa293e9SMauro Carvalho Chehab  of the core mdev code)
235baa293e9SMauro Carvalho Chehab
236baa293e9SMauro Carvalho Chehab* device_api
237baa293e9SMauro Carvalho Chehab
238baa293e9SMauro Carvalho Chehab  This attribute should show which device API is being created, for example,
239baa293e9SMauro Carvalho Chehab  "vfio-pci" for a PCI device.
240baa293e9SMauro Carvalho Chehab
241baa293e9SMauro Carvalho Chehab* available_instances
242baa293e9SMauro Carvalho Chehab
243baa293e9SMauro Carvalho Chehab  This attribute should show the number of devices of type <type-id> that can be
244baa293e9SMauro Carvalho Chehab  created.
245baa293e9SMauro Carvalho Chehab
246baa293e9SMauro Carvalho Chehab* [device]
247baa293e9SMauro Carvalho Chehab
248baa293e9SMauro Carvalho Chehab  This directory contains links to the devices of type <type-id> that have been
249baa293e9SMauro Carvalho Chehab  created.
250baa293e9SMauro Carvalho Chehab
251baa293e9SMauro Carvalho Chehab* name
252baa293e9SMauro Carvalho Chehab
253baa293e9SMauro Carvalho Chehab  This attribute should show human readable name. This is optional attribute.
254baa293e9SMauro Carvalho Chehab
255baa293e9SMauro Carvalho Chehab* description
256baa293e9SMauro Carvalho Chehab
257baa293e9SMauro Carvalho Chehab  This attribute should show brief features/description of the type. This is
258baa293e9SMauro Carvalho Chehab  optional attribute.
259baa293e9SMauro Carvalho Chehab
260baa293e9SMauro Carvalho ChehabDirectories and Files Under the sysfs for Each mdev Device
261baa293e9SMauro Carvalho Chehab----------------------------------------------------------
262baa293e9SMauro Carvalho Chehab
263baa293e9SMauro Carvalho Chehab::
264baa293e9SMauro Carvalho Chehab
265baa293e9SMauro Carvalho Chehab  |- [parent phy device]
266baa293e9SMauro Carvalho Chehab  |--- [$MDEV_UUID]
267baa293e9SMauro Carvalho Chehab         |--- remove
268baa293e9SMauro Carvalho Chehab         |--- mdev_type {link to its type}
269baa293e9SMauro Carvalho Chehab         |--- vendor-specific-attributes [optional]
270baa293e9SMauro Carvalho Chehab
271baa293e9SMauro Carvalho Chehab* remove (write only)
272baa293e9SMauro Carvalho Chehab
273baa293e9SMauro Carvalho ChehabWriting '1' to the 'remove' file destroys the mdev device. The vendor driver can
274baa293e9SMauro Carvalho Chehabfail the remove() callback if that device is active and the vendor driver
275baa293e9SMauro Carvalho Chehabdoesn't support hot unplug.
276baa293e9SMauro Carvalho Chehab
277baa293e9SMauro Carvalho ChehabExample::
278baa293e9SMauro Carvalho Chehab
279baa293e9SMauro Carvalho Chehab	# echo 1 > /sys/bus/mdev/devices/$mdev_UUID/remove
280baa293e9SMauro Carvalho Chehab
281baa293e9SMauro Carvalho ChehabMediated device Hot plug
282baa293e9SMauro Carvalho Chehab------------------------
283baa293e9SMauro Carvalho Chehab
284baa293e9SMauro Carvalho ChehabMediated devices can be created and assigned at runtime. The procedure to hot
285baa293e9SMauro Carvalho Chehabplug a mediated device is the same as the procedure to hot plug a PCI device.
286baa293e9SMauro Carvalho Chehab
287baa293e9SMauro Carvalho ChehabTranslation APIs for Mediated Devices
288baa293e9SMauro Carvalho Chehab=====================================
289baa293e9SMauro Carvalho Chehab
290baa293e9SMauro Carvalho ChehabThe following APIs are provided for translating user pfn to host pfn in a VFIO
291baa293e9SMauro Carvalho Chehabdriver::
292baa293e9SMauro Carvalho Chehab
293baa293e9SMauro Carvalho Chehab	extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
294baa293e9SMauro Carvalho Chehab				  int npage, int prot, unsigned long *phys_pfn);
295baa293e9SMauro Carvalho Chehab
296baa293e9SMauro Carvalho Chehab	extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
297baa293e9SMauro Carvalho Chehab				    int npage);
298baa293e9SMauro Carvalho Chehab
299baa293e9SMauro Carvalho ChehabThese functions call back into the back-end IOMMU module by using the pin_pages
300baa293e9SMauro Carvalho Chehaband unpin_pages callbacks of the struct vfio_iommu_driver_ops[4]. Currently
301baa293e9SMauro Carvalho Chehabthese callbacks are supported in the TYPE1 IOMMU module. To enable them for
302baa293e9SMauro Carvalho Chehabother IOMMU backend modules, such as PPC64 sPAPR module, they need to provide
303baa293e9SMauro Carvalho Chehabthese two callback functions.
304baa293e9SMauro Carvalho Chehab
305baa293e9SMauro Carvalho ChehabUsing the Sample Code
306baa293e9SMauro Carvalho Chehab=====================
307baa293e9SMauro Carvalho Chehab
308baa293e9SMauro Carvalho Chehabmtty.c in samples/vfio-mdev/ directory is a sample driver program to
309baa293e9SMauro Carvalho Chehabdemonstrate how to use the mediated device framework.
310baa293e9SMauro Carvalho Chehab
311baa293e9SMauro Carvalho ChehabThe sample driver creates an mdev device that simulates a serial port over a PCI
312baa293e9SMauro Carvalho Chehabcard.
313baa293e9SMauro Carvalho Chehab
314baa293e9SMauro Carvalho Chehab1. Build and load the mtty.ko module.
315baa293e9SMauro Carvalho Chehab
316baa293e9SMauro Carvalho Chehab   This step creates a dummy device, /sys/devices/virtual/mtty/mtty/
317baa293e9SMauro Carvalho Chehab
318baa293e9SMauro Carvalho Chehab   Files in this device directory in sysfs are similar to the following::
319baa293e9SMauro Carvalho Chehab
320baa293e9SMauro Carvalho Chehab     # tree /sys/devices/virtual/mtty/mtty/
321baa293e9SMauro Carvalho Chehab        /sys/devices/virtual/mtty/mtty/
322baa293e9SMauro Carvalho Chehab        |-- mdev_supported_types
323baa293e9SMauro Carvalho Chehab        |   |-- mtty-1
324baa293e9SMauro Carvalho Chehab        |   |   |-- available_instances
325baa293e9SMauro Carvalho Chehab        |   |   |-- create
326baa293e9SMauro Carvalho Chehab        |   |   |-- device_api
327baa293e9SMauro Carvalho Chehab        |   |   |-- devices
328baa293e9SMauro Carvalho Chehab        |   |   `-- name
329baa293e9SMauro Carvalho Chehab        |   `-- mtty-2
330baa293e9SMauro Carvalho Chehab        |       |-- available_instances
331baa293e9SMauro Carvalho Chehab        |       |-- create
332baa293e9SMauro Carvalho Chehab        |       |-- device_api
333baa293e9SMauro Carvalho Chehab        |       |-- devices
334baa293e9SMauro Carvalho Chehab        |       `-- name
335baa293e9SMauro Carvalho Chehab        |-- mtty_dev
336baa293e9SMauro Carvalho Chehab        |   `-- sample_mtty_dev
337baa293e9SMauro Carvalho Chehab        |-- power
338baa293e9SMauro Carvalho Chehab        |   |-- autosuspend_delay_ms
339baa293e9SMauro Carvalho Chehab        |   |-- control
340baa293e9SMauro Carvalho Chehab        |   |-- runtime_active_time
341baa293e9SMauro Carvalho Chehab        |   |-- runtime_status
342baa293e9SMauro Carvalho Chehab        |   `-- runtime_suspended_time
343baa293e9SMauro Carvalho Chehab        |-- subsystem -> ../../../../class/mtty
344baa293e9SMauro Carvalho Chehab        `-- uevent
345baa293e9SMauro Carvalho Chehab
346baa293e9SMauro Carvalho Chehab2. Create a mediated device by using the dummy device that you created in the
347baa293e9SMauro Carvalho Chehab   previous step::
348baa293e9SMauro Carvalho Chehab
349baa293e9SMauro Carvalho Chehab     # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" >	\
350baa293e9SMauro Carvalho Chehab              /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create
351baa293e9SMauro Carvalho Chehab
352baa293e9SMauro Carvalho Chehab3. Add parameters to qemu-kvm::
353baa293e9SMauro Carvalho Chehab
354baa293e9SMauro Carvalho Chehab     -device vfio-pci,\
355baa293e9SMauro Carvalho Chehab      sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001
356baa293e9SMauro Carvalho Chehab
357baa293e9SMauro Carvalho Chehab4. Boot the VM.
358baa293e9SMauro Carvalho Chehab
359baa293e9SMauro Carvalho Chehab   In the Linux guest VM, with no hardware on the host, the device appears
360baa293e9SMauro Carvalho Chehab   as  follows::
361baa293e9SMauro Carvalho Chehab
362baa293e9SMauro Carvalho Chehab     # lspci -s 00:05.0 -xxvv
363baa293e9SMauro Carvalho Chehab     00:05.0 Serial controller: Device 4348:3253 (rev 10) (prog-if 02 [16550])
364baa293e9SMauro Carvalho Chehab             Subsystem: Device 4348:3253
365baa293e9SMauro Carvalho Chehab             Physical Slot: 5
366baa293e9SMauro Carvalho Chehab             Control: I/O+ Mem- BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr-
367baa293e9SMauro Carvalho Chehab     Stepping- SERR- FastB2B- DisINTx-
368baa293e9SMauro Carvalho Chehab             Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=medium >TAbort-
369baa293e9SMauro Carvalho Chehab     <TAbort- <MAbort- >SERR- <PERR- INTx-
370baa293e9SMauro Carvalho Chehab             Interrupt: pin A routed to IRQ 10
371baa293e9SMauro Carvalho Chehab             Region 0: I/O ports at c150 [size=8]
372baa293e9SMauro Carvalho Chehab             Region 1: I/O ports at c158 [size=8]
373baa293e9SMauro Carvalho Chehab             Kernel driver in use: serial
374baa293e9SMauro Carvalho Chehab     00: 48 43 53 32 01 00 00 02 10 02 00 07 00 00 00 00
375baa293e9SMauro Carvalho Chehab     10: 51 c1 00 00 59 c1 00 00 00 00 00 00 00 00 00 00
376baa293e9SMauro Carvalho Chehab     20: 00 00 00 00 00 00 00 00 00 00 00 00 48 43 53 32
377baa293e9SMauro Carvalho Chehab     30: 00 00 00 00 00 00 00 00 00 00 00 00 0a 01 00 00
378baa293e9SMauro Carvalho Chehab
379baa293e9SMauro Carvalho Chehab     In the Linux guest VM, dmesg output for the device is as follows:
380baa293e9SMauro Carvalho Chehab
381baa293e9SMauro Carvalho Chehab     serial 0000:00:05.0: PCI INT A -> Link[LNKA] -> GSI 10 (level, high) -> IRQ 10
382baa293e9SMauro Carvalho Chehab     0000:00:05.0: ttyS1 at I/O 0xc150 (irq = 10) is a 16550A
383baa293e9SMauro Carvalho Chehab     0000:00:05.0: ttyS2 at I/O 0xc158 (irq = 10) is a 16550A
384baa293e9SMauro Carvalho Chehab
385baa293e9SMauro Carvalho Chehab
386baa293e9SMauro Carvalho Chehab5. In the Linux guest VM, check the serial ports::
387baa293e9SMauro Carvalho Chehab
388baa293e9SMauro Carvalho Chehab     # setserial -g /dev/ttyS*
389baa293e9SMauro Carvalho Chehab     /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
390baa293e9SMauro Carvalho Chehab     /dev/ttyS1, UART: 16550A, Port: 0xc150, IRQ: 10
391baa293e9SMauro Carvalho Chehab     /dev/ttyS2, UART: 16550A, Port: 0xc158, IRQ: 10
392baa293e9SMauro Carvalho Chehab
393baa293e9SMauro Carvalho Chehab6. Using minicom or any terminal emulation program, open port /dev/ttyS1 or
394baa293e9SMauro Carvalho Chehab   /dev/ttyS2 with hardware flow control disabled.
395baa293e9SMauro Carvalho Chehab
396baa293e9SMauro Carvalho Chehab7. Type data on the minicom terminal or send data to the terminal emulation
397baa293e9SMauro Carvalho Chehab   program and read the data.
398baa293e9SMauro Carvalho Chehab
399baa293e9SMauro Carvalho Chehab   Data is loop backed from hosts mtty driver.
400baa293e9SMauro Carvalho Chehab
401baa293e9SMauro Carvalho Chehab8. Destroy the mediated device that you created::
402baa293e9SMauro Carvalho Chehab
403baa293e9SMauro Carvalho Chehab     # echo 1 > /sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001/remove
404baa293e9SMauro Carvalho Chehab
405baa293e9SMauro Carvalho ChehabReferences
406baa293e9SMauro Carvalho Chehab==========
407baa293e9SMauro Carvalho Chehab
408baa293e9SMauro Carvalho Chehab1. See Documentation/driver-api/vfio.rst for more information on VFIO.
409baa293e9SMauro Carvalho Chehab2. struct mdev_driver in include/linux/mdev.h
410baa293e9SMauro Carvalho Chehab3. struct mdev_parent_ops in include/linux/mdev.h
411baa293e9SMauro Carvalho Chehab4. struct vfio_iommu_driver_ops in include/linux/vfio.h
412