Module 11 · Lesson 53 of 107

The MMIO and PCI transport drivers

Trace how Linux discovers virtio devices, configures queues, and handles completion interrupts over the MMIO and PCI transports.

  • Beginner to advanced
  • 9 min read
  • 13 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM for the written exercises, plus familiarity with Linux driver binding and virtqueues. A guest with a virtio device is optional for sysfs inspection.

Your place in the module
By the end you can 4 objectives
  • Identify the Linux bus and driver at each stage of virtio device binding.
  • Trace MMIO queue setup and completion for both transport versions.
  • Locate modern PCI transport regions and explain how Linux assigns MSI-X vectors.
  • Explain how VIRTIO_F_ACCESS_PLATFORM changes the addresses supplied to a device.
Video coming soonThe MMIO and PCI transport drivers
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from virtio-blk and virtio-net drivers
  • A virtio-blk read gives the device writable data buffers, and a write gives it readable data buffers. Both have a device-written status.
  • virtio-net submits outbound packets on TX queues and posts writable buffers in advance on RX queues.
  • virtnet_poll drains completed RX buffers under a NAPI budget.
01

Two driver bindings for one virtio device

3 min read

The last lesson covered the device drivers. This lesson answers the transport drivers beneath them, virtio_mmio and virtio_pci, and how this reuses the M5 driver model.

A driver-binding problem in a Linux guest can start with a device that the kernel has already discovered. A PCI function may be present and bound to virtio-pci, yet the virtio child may still lack a functional driver. To tell where initialization stopped, identify the device object you are looking at and the bus that owns it. Linux uses the transport abstraction documented in Writing Virtio Drivers to keep those two bindings distinct.

For MMIO, Linux can discover a virtio,mmio node in the guest device tree. The platform device supplies a register range and an interrupt resource. The virtio_mmio platform driver maps the register range and reads the device identification registers. For PCI, enumeration supplies a PCI function. The virtio-pci driver probes it, locates its virtio interface, and obtains the virtio device identity. Either transport can then register a struct virtio_device with the virtio core.

The second binding takes place on the virtio bus. The virtio core matches the child against a functional driver's ID table. For example, virtio_blk binds to the virtio block device ID and requests the queues it needs through the virtio interface. The transport supplies a struct virtio_config_ops with operations for tasks such as reading features, accessing device configuration, and finding queues. The functional driver requests queues through those operations. The transport programs the registers for the hardware interface it owns.

Two Linux driver bindingsFunctional driverFor example, virtio_blkVirtio deviceMatched on the virtio busTransport driverRegisters the virtio childParent devicePlatform device or PCI functionFunctional driverFor example, virtio_blkVirtio deviceMatched on the virtio busTransport driverRegisters the virtio childParent devicePlatform device or PCIfunction
The platform and PCI buses provide transport devices, and the virtio bus matches the registered child to a functional driver

This is the driver model from M5 in two successive bindings. The platform and PCI buses each match a device to a transport driver and call its probe routine. The virtio bus does the same for the child and its functional driver. Register accesses use the kernel's I/O accessors, and completion handling uses Linux IRQ registration and dispatch. The transport owns those resources and connects their events to the virtio core and the queue callbacks.

In a guest exposing virtio devices, entries under /sys/bus/virtio/devices/ let you inspect the second binding. A child's driver symlink, when present, identifies its functional driver. Resolving the child's sysfs path shows where it sits under the parent device. For a detected transport with no registered child, investigate transport probing and device identification. For a registered child with no functional driver bound, investigate matching and functional probe. Which devices appear depends on what the guest machine exposes.

02

How virtio-mmio programs and services a queue

3 min read

The MMIO offsets below are relative to the base of one device's mapped register window. Linux reads MagicValue at 0x000 and expects 0x74726976, the value representing virt. It reads Version at 0x004 to choose the layout and DeviceID at 0x008 to identify the type of virtio child to register. A zero device ID indicates an empty device slot. Version 1 selects the legacy layout, and version 2 selects the modern layout. Checking the version early matters because queue activation uses different registers in the two layouts.

An MMIO queue operationSelectqueueWriteQueueSelSet queuelayoutWrite sizeand layoutfieldsActivatequeueWriteQueuePFN orQueueReadyNotifydeviceWriteQueueNotifyAcknowledgeinterruptRead statusandacknowledgebitsCheck usedbuffersCheck eachregisteredqueueSelectqueueWriteQueueSelSet queuelayoutWrite sizeand layoutfieldsActivatequeueWriteQueuePFN orQueueReadyNotifydeviceWriteQueueNotifyAcknowledgeinterruptRead statusandacknowledgebitsCheck usedbuffersCheck eachregisteredqueue
Linux selects and configures an MMIO queue, writes a notification, and acknowledges the device interrupt

When a functional driver requests a queue, Linux writes its index to QueueSel at 0x030. That selects which queue the setup registers address. Linux reads QueueNumMax at 0x034 for the selected queue and writes the allocated size to QueueNum at 0x038. A zero maximum means the selected queue is unavailable. If a driver needs two queues, the transport performs the setup for each selector value. The register window stays at the same address for both.

In legacy MMIO, Linux writes the guest page size to GuestPageSize at 0x028 during transport setup and supplies the selected queue's alignment through QueueAlign at 0x03c. The legacy layout derives the ring locations from the base of a contiguous queue allocation, its size, and the requested alignment. Linux activates the queue by writing the queue base address divided by the guest page size to QueuePFN at 0x040. That value is a page frame number. A zero QueuePFN indicates an inactive queue. When debugging a legacy initialization trace, check both the selector and the units of the value before interpreting the PFN write.

Modern MMIO provides three address pairs. The descriptor area uses 0x080 and 0x084, the driver area uses 0x090 and 0x094, and the device area uses 0x0a0 and 0x0a4. In each pair the low 32 bits come first. For a split ring, those three addresses locate the descriptor table, available ring, and used ring respectively. Once the addresses and size are set for the selected queue, Linux writes 1 to QueueReady at 0x044. A version 2 initialization trace should show those address writes before the ready write.

Once the queue is running, a kick uses QueueNotify at 0x050. In the basic notification form, Linux writes the queue index directly to that register. QueueSel determines which queue's setup registers are being accessed, so the last setup selector can still name a different queue when Linux sends a kick. The notification tells the device to check queue memory for work the driver has made available. The request and its buffers are described in memory.

For completions, virtio_mmio requests the platform IRQ. The handler reads InterruptStatus at 0x060 and writes the observed bits to InterruptACK at 0x064 to clear those indications. Bit 0 indicates a queue interrupt and bit 1 indicates a device configuration change. A queue indication can cover multiple queues, so the handler checks the registered virtqueues through vring_interrupt(). The ring code checks for used buffers and invokes the queue callback when appropriate. For a configuration indication, the transport calls virtio_config_changed(). Both indications can be present in one status read. Acknowledging the transport interrupt and consuming used buffers are distinct actions.

03

How virtio-pci locates registers and assigns interrupts

5 min read

A modern virtio PCI function advertises its transport layout through vendor-specific capabilities in PCI configuration space. Each virtio capability has a type identifying the region, a BAR index, a byte offset into that BAR, and a region length. The BAR is a PCI Base Address Register describing a resource assigned to the function. Linux walks the capability list, validates the regions against the PCI resources, and maps the regions it uses. To interpret an offset in one of these capabilities, first identify its BAR. Different functions can place equivalent virtio regions at different BAR offsets.

The common configuration region contains device status, feature selectors and values, queue selection and size, queue addresses, queue enable, and MSI-X vector selectors. Linux selects a queue in the common configuration region, writes its size and the addresses of its descriptor, driver, and device areas, and enables it. The device-specific configuration region, when supplied, holds fields defined by the device type. The ISR status region is used to identify the cause of an interrupt in the shared interrupt path. Reading the ISR status clears it.

The notification capability also supplies notify_off_multiplier. For a queue, Linux reads queue_notify_off in the common configuration region while that queue is selected. The queue's notification address is the mapped notification region base plus queue_notify_off multiplied by notify_off_multiplier. In the basic form Linux writes the queue index at that address to notify the device. The capability identifies where the notification region begins, and the per-queue field locates the register within it. A trace with different notification addresses for different queues can therefore still describe one PCI function using a single notification capability.

Modern transport layoutModern MMIOModern PCILocate registersfixed offsetsfrom basecapabilitiesidentify BARregionsConfigure queueaddresses andQueueReadyaddresses andqueue enableNotify devicefixedQueueNotifyoffsetqueue offsettimesmultiplierHandle interruptplatform IRQand statusbitsMSI-X orshared INTx
MMIO and modern PCI expose equivalent queue operations through different register layouts and interrupt mechanisms

For interrupts, virtio-pci first tries to allocate MSI-X vectors for configuration changes and individual queues that have callbacks. The PCI subsystem allocates the vectors and gives Linux IRQ numbers for them. The transport writes the device's MSI-X table entry indices to the configuration vector field and the relevant queue vector fields, then associates handlers with the Linux IRQs. The index in the virtio register and the IRQ number used inside Linux belong to different numbering spaces. With an individual vector assigned, the queue handler can check its queue directly.

If individual vectors cannot be set up, Linux can try a shared MSI-X vector for queue callbacks, with a separate configuration vector. A shared queue handler checks the queues associated with it for used buffers. Linux can also fall back to the shared INTx interrupt path when available. In that path the ISR status determines whether the handler has a queue indication, a configuration indication, or both. The MSI Driver Guide explains the PCI vector allocation and IRQ interface used by drivers.

A transitional virtio PCI device supports the modern capability layout and the legacy interface for older guests. If Linux successfully selects the modern interface, it uses the capability-described regions. With the legacy interface, Linux accesses fixed offsets in the I/O region described by BAR 0. Queue setup there uses a queue selector and a queue PFN for a contiguous ring allocation. The legacy register layout also provides queue notification and ISR status, and includes vector fields when MSI-X is enabled. A transitional PCI identity alone therefore does not tell you which register layout a guest driver is using. Check which interface it selected before decoding its register accesses.

Going deeperQueue memory, DMA addresses, and IOMMUs

The queue addresses identify memory accessible to the device implementation. The descriptors likewise contain addresses for the buffers. For an emulated device, host software can access the guest memory that implements these queues and buffers. The register region contains control fields and queue addresses. The queue data itself resides in memory that the guest driver and the device can access.

When VIRTIO_F_ACCESS_PLATFORM is negotiated, Linux uses the DMA API for the virtqueue's memory and buffer mappings. The driver supplies addresses valid for the device's DMA view. On a platform with a translated DMA domain, a DMA address can be an I/O virtual address, or IOVA. The mapping associates that IOVA with the memory backing the buffer. The DMA address may equal the guest physical address on a platform that uses identity mappings. In the usual legacy-compatible virtio path without this feature, the virtio ring code supplies guest physical addresses directly. The Dynamic DMA mapping Guide explains the distinction between CPU and device addresses.

A guest-visible IOMMU, such as one exposed through virtio-iommu, lets the guest establish DMA mappings for endpoints attached to it. The backend or virtualization platform must honor those mappings when the endpoint uses translated addresses. The host IOMMU governs DMA issued by physical hardware on the host. For example, when physical hardware accesses memory backing a guest buffer, the host IOMMU can restrict that hardware to mapped host pages. An emulated backend reading guest memory through CPU loads uses the host CPU's address translation. The presence of a guest IOMMU alone does not determine how the host implements access to that memory.

For a device bring-up review, identify the transport driver, find the registered virtio child, and follow one queue from setup to notification to completion. At the register level, record which queue Linux selected, where it supplied the queue addresses, and how the interrupt handler identified a completion. This gives you a concrete way to relate the device visible in Linux to the interface the virtual machine exposes.

The next lesson, libvirt, the domain XML and virsh, explains how to describe and inspect a virtual machine and its devices from the management side, where the guest's choice of transport begins.

Key takeaways

  • The platform or PCI driver registers a child device on the virtio bus.
  • Legacy MMIO activates a queue through QueuePFN, while modern MMIO supplies separate queue addresses and sets QueueReady.
  • Modern virtio PCI capabilities describe BAR regions, and MSI-X mappings associate configuration changes and queues with interrupt vectors.
  • With VIRTIO_F_ACCESS_PLATFORM negotiated, Linux uses DMA API addresses for virtqueue memory and buffers.
Reflect
  1. Explain to a colleague, in two sentences: identify the Linux bus and driver at each stage of virtio device binding.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close