Experimenting with MDEV-Based Virtualization for PCI Serial Port Devices
This practice is based on the following Linux kernel documentation: Documentation/driver-api/vfio-mediated-device.rst Documentation/driver-api/vfio.rst
Compiling and Loading the MTTY Kernel Module
This step establishes a dummy device at /sys/devices/virtual/mtty/mtty/.
Enable the CONFIG_SAMPLE_VFIO_MDEV_MTTY kernel configuration option. Recompile the kernel to generate the mtty.ko module.
make CONFIG_VFIO_MDEV=m -C /lib/modules/$(uname -r)/build M=/path/to/linux/source/drivers/vfio/mdev/ modules
make CONFIG_SAMPLE_VFIO_MDEV_MTTY=m -C /lib/modules/$(uname -r)/build M=/path/to/linux/source/samples/vfio-mdev/ modules
After rebooting the system, proceed to load the modules:
sudo insmod /path/to/mdev.ko
sudo insmod /path/to/vfio_mdev.ko
sudo insmod /path/to/mtty.ko
Understanding MDEV Groups:
When mutliple types of MDEV devices are supported, separate subdirectories are created under the device's sysfs directory for managing each distinct type.
Upon successful installation, the difference between device types can be observed in /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-#NUM/name. One type represents a single-port TTY, while the other corresponds to a dual-port TTY.
Creating a Mediated Device Instance
Use the dummy device created earlier to instantiate a mediated device. Any valid UUID is acceptable; it is common to generate one using a uuid utility.
echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create
This sysfs write operation triggers the following kernel call stack, which handles the device creation. The new device is implemented as a PCI device, requiring the configuration of its PCI configuration space. A dedicated IOMMU group is established for the device, and the MDEV intsance is associated with it. The vendor and product IDs in the configuration space (e.g., 0x32534348) correspond to a device such as a CH352 16550 PCI UART serial driver, thereby linking the PCI abstraction to the MTTY MDEV device.