Module 5 · Lesson 81 of 129

PCI interrupts and MSI

Allocate PCI interrupt vectors, register handlers for MSI or legacy INTx, and release the resources in the correct order.

  • Beginner to advanced
  • 6 min read
  • 12 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM for the written exercises, and familiarity with PCI probe and removal

Your place in the module
By the end you can 4 objectives
  • Explain how INTx, MSI and MSI-X deliver interrupts
  • Choose allocation flags and interpret the vector count returned by pci_alloc_irq_vectors
  • Map a vector index to a Linux IRQ with pci_irq_vector
  • Specify the handler behavior and cleanup sequence for shared INTx
Video coming soonPCI interrupts and MSI
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from The pci_driver lifecycle
  • The PCI core calls probe for an eligible matching function and calls remove when a successful binding is torn down.
  • Enabling PCI address decoding, reserving a BAR, mapping it and enabling bus mastering are separate operations.
  • Managed PCI helpers can unwind resource acquisitions, while the driver remains responsible for stopping device activity.
01

How a PCI device signals an interrupt

2 min read

The last lesson enabled the device and mapped its BARs. This lesson answers how it then receives interrupts, with pci_alloc_irq_vectors and the choice between legacy INTx, MSI and MSI-X.

The Linux PCI MSI guide describes a problem with traditional PCI interrupt delivery. Several devices can share an INTx interrupt, so Linux can call several handlers when a single device signals it. Each handler must read its own device's status to determine whether it has work to do. A message-signaled interrupt gives the device its own allocated IRQ and avoids that shared-line dispatch.

Legacy INTx uses a level-triggered interrupt. On conventional PCI, a device asserts an interrupt pin. PCI Express can convey equivalent assertion and deassertion with protocol messages. In either case the device can keep the interrupt asserted while a cause remains pending. To service it, the handler follows the device's register protocol, which may require writing an acknowledgment or reading a register that clears status. If the driver leaves the cause asserted, Linux may keep receiving interrupts.

With , the device sends a write transaction to signal an event. MSI-X uses the same delivery principle and supports more independently configurable vectors. A device may offer one vector for all events or support several vectors for different sources. In either case, the hardware specification tells the handler how to identify and service the event. Switching from INTx to MSI changes delivery, and the device can still require its interrupt status to be cleared.

PCI interrupt deliveryINTxMSIMSI-XSignalAssert ordeassertMessage writeMessage writeIRQ sharingMay be sharedOwn IRQ pervectorOwn IRQ pervectorVector countOneOne or moreOne or more
INTx carries assertion state, while MSI and MSI-X deliver writes

Keep two numbers separate when reading a driver. The vector index selects an interrupt within the device's allocation. The Linux IRQ number identifies the interrupt to the kernel's IRQ APIs. Index zero is the first allocated vector. Its Linux IRQ number comes from pci_irq_vector(pdev, 0). Save the resulting IRQ for registration and cleanup. The allocation count tells you which indices are valid, and each index needs its own lookup.

02

Allocate, register and release

3 min read

For a driver that can handle all its events through one interrupt, call pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES). The two 1 arguments are the minimum usable count and the maximum requested count. PCI_IRQ_ALL_TYPES permits MSI-X, MSI and legacy INTx. Linux tries the permitted modes in that order. The device, the PCI host and the system configuration determine which modes can actually be used. A driver that supports MSI and INTx can pass PCI_IRQ_MSI | PCI_IRQ_INTX on Linux 6.8 to permit exactly those two modes. Older code spells the INTx flag PCI_IRQ_LEGACY, which 6.8 still defines as a deprecated alias.

Check the return value. A negative value is an error, and probe should unwind the resources it acquired earlier. A positive value is the allocated count. With a minimum and maximum of one, success returns one in any of the permitted modes. A driver that requires two independent vectors must set its minimum to two. Legacy INTx can supply only one vector, so it cannot satisfy that request. When the maximum exceeds the minimum, build the device's interrupt routing around the count Linux actually returned.

Set up a PCI interruptMask devicesourcesAllocatevectorsResolveLinux IRQRegisterhandlerEnabledevicesourcesMask devicesourcesAllocatevectorsResolveLinux IRQRegisterhandlerEnabledevicesources
The driver resolves an IRQ number before registering a handler and enables device sources last

Message-signaled interrupts are writes that the function initiates, so the function must be a bus master. Call pci_set_master(pdev) before relying on MSI or MSI-X. In a QEMU edu test, an MSI never arrived until that call was added. Keep the device's interrupt sources masked while setting up. Initialize the driver state the handler will read, including its register mapping and the mask of causes it owns. After a successful allocation, call pci_irq_vector(pdev, 0) and check for a negative result before using the number. On Linux 6.8, the PCI device's msi_enabled and msix_enabled fields tell you whether the allocation selected a message-signaled mode. If both are clear after a successful allocation, the selected mode is INTx.

Register the handler with request_irq, passing IRQF_SHARED for INTx and zero flags for the basic MSI or MSI-X case. Use the device's private state pointer as dev_id. A shared registration requires a non-null cookie that identifies the registration, and removal must pass the same cookie. Check the return from request_irq. Linux can call the handler as soon as it is registered, including when another device asserts a shared INTx interrupt. Enable the device's sources only after registration succeeds and the device is ready.

On entry to a shared handler, read the device's pending status and check for causes this handler owns. If none are pending, return IRQ_NONE. If a relevant cause is pending, acknowledge or clear it as the device specifies, perform the required handling and return IRQ_HANDLED. Reporting IRQ_HANDLED unconditionally on a shared line hides whether your device actually needed service. Clearing a cause without checking ownership can also discard an event assigned elsewhere in the driver.

If IRQ lookup or registration fails after allocation, call pci_free_irq_vectors(pdev) on the error path. If registration succeeded and a later setup step fails, release that registration first. On normal removal, stop device interrupt generation, call free_irq(irq, dev_id) and then call pci_free_irq_vectors(pdev). Mask the device's own sources on a shared interrupt so peers can continue using the IRQ. Keep the BAR mapping and handler state valid until free_irq has waited for executing handlers to finish.

03

Configure multiple vectors

2 min read

Linux programs the MSI capability in PCI configuration space with the message address, data and enabled message count. Conventional MSI supports power-of-two vector counts and requires a consecutive allocation of hardware vectors. That requirement does not let a driver calculate Linux IRQ numbers by adding offsets. With MSI-X, the capability identifies a vector table in a BAR. Linux programs each allocated table entry with its message address, message data and mask state. Drivers use the PCI IRQ APIs for this configuration. They use the device's own registers to assign hardware causes to the allocated vectors when the hardware supports configurable routing.

Route device causes to handlersDevicesourceQueueeventErroreventVector indexIndex 0Index 1Linux IRQLookupforindex 0Lookupforindex 1HandlercontextQueuestateErrorstate
Separate device sources can use separate allocated vectors and handler contexts

For a device with independent event sources, look up each allocated index with pci_irq_vector and register a handler for the resulting IRQ. The handler function can be common to all vectors, with a different dev_id pointing to each vector's context. For example, one context can identify a queue and another can identify an error source. Configure the device so each source uses the index assigned by the driver. If the hardware can combine sources and Linux grants fewer vectors than requested, adapt the routing before enabling interrupts. If registration fails in the middle of setup, free the handlers that succeeded before freeing the vector allocation.

Pass PCI_IRQ_AFFINITY in the allocation flags to ask Linux to spread the allocated interrupts across available CPUs. pci_irq_get_affinity(pdev, index) can report the allocation's affinity mask when one is available. The mask describes CPU placement, while the handler context identifies the device work to service. Different vectors can run on different CPUs, so handlers that modify shared state need appropriate synchronization even when each vector has its own context.

Older code may show the pci_enable_msix family, which takes a caller-supplied array of struct msix_entry records for explicitly selected MSI-X table entries. The original pci_enable_msix() no longer exists in Linux 6.8. Only the legacy pci_enable_msix_range() and pci_enable_msix_exact() remain, and new code should not use them. For a new driver that requires MSI-X, use pci_alloc_irq_vectors with PCI_IRQ_MSIX and resolve each Linux IRQ with pci_irq_vector.

In the next lesson, The QEMU edu device, you will connect this Linux setup to a concrete device by seeing how edu raises an interrupt and which registers its driver uses to recognize and acknowledge it.

Key takeaways

  • Linux prefers MSI-X, then MSI, then INTx among the modes the driver permits and the system can provide.
  • pci_alloc_irq_vectors returns a vector count on success, and pci_irq_vector maps an allocated index to a Linux IRQ number.
  • A shared INTx handler reads device status and returns IRQ_NONE when its device has no relevant interrupt pending.
  • The driver stops device interrupt generation and frees its handlers before releasing the PCI vectors.
Reflect
  1. Explain to a colleague, in two sentences: explain how INTx, MSI and MSI-X deliver interrupts
  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