@@ -288,8 +288,109 @@ these callbacks are supported in the TYPE1 IOMMU module. To enable them for
288288other IOMMU backend modules, such as PPC64 sPAPR module, they need to provide
289289these two callback functions.
290290
291+ Using the Sample Code
292+ =====================
293+
294+ mtty.c in samples/vfio-mdev/ directory is a sample driver program to
295+ demonstrate how to use the mediated device framework.
296+
297+ The sample driver creates an mdev device that simulates a serial port over a PCI
298+ card.
299+
300+ 1. Build and load the mtty.ko module.
301+
302+ This step creates a dummy device, /sys/devices/virtual/mtty/mtty/
303+
304+ Files in this device directory in sysfs are similar to the following:
305+
306+ # tree /sys/devices/virtual/mtty/mtty/
307+ /sys/devices/virtual/mtty/mtty/
308+ |-- mdev_supported_types
309+ | |-- mtty-1
310+ | | |-- available_instances
311+ | | |-- create
312+ | | |-- device_api
313+ | | |-- devices
314+ | | `-- name
315+ | `-- mtty-2
316+ | |-- available_instances
317+ | |-- create
318+ | |-- device_api
319+ | |-- devices
320+ | `-- name
321+ |-- mtty_dev
322+ | `-- sample_mtty_dev
323+ |-- power
324+ | |-- autosuspend_delay_ms
325+ | |-- control
326+ | |-- runtime_active_time
327+ | |-- runtime_status
328+ | `-- runtime_suspended_time
329+ |-- subsystem -> ../../../../class/mtty
330+ `-- uevent
331+
332+ 2. Create a mediated device by using the dummy device that you created in the
333+ previous step.
334+
335+ # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > \
336+ /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create
337+
338+ 3. Add parameters to qemu-kvm.
339+
340+ -device vfio-pci,\
341+ sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001
342+
343+ 4. Boot the VM.
344+
345+ In the Linux guest VM, with no hardware on the host, the device appears
346+ as follows:
347+
348+ # lspci -s 00:05.0 -xxvv
349+ 00:05.0 Serial controller: Device 4348:3253 (rev 10) (prog-if 02 [16550])
350+ Subsystem: Device 4348:3253
351+ Physical Slot: 5
352+ Control: I/O+ Mem- BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr-
353+ Stepping- SERR- FastB2B- DisINTx-
354+ Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=medium >TAbort-
355+ <TAbort- <MAbort- >SERR- <PERR- INTx-
356+ Interrupt: pin A routed to IRQ 10
357+ Region 0: I/O ports at c150 [size=8]
358+ Region 1: I/O ports at c158 [size=8]
359+ Kernel driver in use: serial
360+ 00: 48 43 53 32 01 00 00 02 10 02 00 07 00 00 00 00
361+ 10: 51 c1 00 00 59 c1 00 00 00 00 00 00 00 00 00 00
362+ 20: 00 00 00 00 00 00 00 00 00 00 00 00 48 43 53 32
363+ 30: 00 00 00 00 00 00 00 00 00 00 00 00 0a 01 00 00
364+
365+ In the Linux guest VM, dmesg output for the device is as follows:
366+
367+ serial 0000:00:05.0: PCI INT A -> Link[LNKA] -> GSI 10 (level, high) -> IRQ
368+ 10
369+ 0000:00:05.0: ttyS1 at I/O 0xc150 (irq = 10) is a 16550A
370+ 0000:00:05.0: ttyS2 at I/O 0xc158 (irq = 10) is a 16550A
371+
372+
373+ 5. In the Linux guest VM, check the serial ports.
374+
375+ # setserial -g /dev/ttyS*
376+ /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
377+ /dev/ttyS1, UART: 16550A, Port: 0xc150, IRQ: 10
378+ /dev/ttyS2, UART: 16550A, Port: 0xc158, IRQ: 10
379+
380+ 6. Using a minicom or any terminal enulation program, open port /dev/ttyS1 or
381+ /dev/ttyS2 with hardware flow control disabled.
382+
383+ 7. Type data on the minicom terminal or send data to the terminal emulation
384+ program and read the data.
385+
386+ Data is loop backed from hosts mtty driver.
387+
388+ 8. Destroy the mediated device that you created.
389+
390+ # echo 1 > /sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001/remove
391+
291392References
292- ----------
393+ ==========
293394
294395[1] See Documentation/vfio.txt for more information on VFIO.
295396[2] struct mdev_driver in include/linux/mdev.h
0 commit comments