Skip to content
systemcoding.dev
Kernels

Enable MSI and MSI-X Interrupts With pci_alloc_irq_vectors

Request MSI, MSI-X or legacy interrupts in a Linux 6.12 PCI driver with pci_alloc_irq_vectors, bind handlers per vector and release them in the right order.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and FreeBSD logos.

A PCI device signals the CPU in one of three ways: a shared legacy INTx line, Message Signaled Interrupts (MSI), or MSI-X. The old way of probing each one by hand is gone. Since Linux 4.8 a single call, pci_alloc_irq_vectors(), asks the PCI core for as many vectors as the device and platform can provide, in the best mode available, and pci_irq_vector() turns a vector index into the Linux interrupt number you pass to request_irq(). This post uses Linux 6.12 to request a range of vectors, attach one handler per queue, and tear the whole setup down in the right order. It extends the driver from the PCI skeleton.

The interface is described in the kernel's MSI HOWTO.

Why message interrupts

A legacy INTx interrupt is a physical wire, shared by several devices and level-triggered. The handler for a shared line must check whether its own device raised it, and the line stays asserted until every device is serviced. Wires are scarce, so devices share them, and every sharing device wakes for every interrupt.

MSI replaces the wire with a memory write. The device writes a small value to an address the kernel programmed, and the interrupt controller turns that write into an interrupt on a chosen CPU. Because it is a write, there is no sharing and no level to clear. A device can have up to 32 MSI vectors, and MSI-X extends that to 2048 vectors, each with its own address, data and mask bit in a table held in a BAR, which lets a multi-queue device give every queue its own interrupt and pin it to its own CPU.

Allocate vectors

c
int pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
			  unsigned int max_vecs, unsigned int flags);

The function tries to allocate between min_vecs and max_vecs and returns the number it got, or a negative errno. The flags choose which modes are acceptable. They are tried from the most capable to the least:

FlagMeaning
PCI_IRQ_MSIXMSI-X is acceptable
PCI_IRQ_MSIMSI is acceptable
PCI_IRQ_INTXA legacy line is acceptable, one vector only
PCI_IRQ_ALL_TYPESAll three, in that order
PCI_IRQ_AFFINITYSpread the vectors over CPUs automatically

A driver for a device that can use any mode passes PCI_IRQ_ALL_TYPES. A driver that must have message interrupts leaves out PCI_IRQ_INTX and treats a failure as fatal. The legacy flag was named PCI_IRQ_LEGACY in older kernels, and PCI_IRQ_ALL_TYPES is the spelling that is stable across versions.

sample_irq.cc
#include <linux/interrupt.h>
#include <linux/pci.h>

#define SAMPLE_MAX_QUEUES	4

struct sample_queue {
	struct sample	*s;
	int		index;
	char		name[24];
};

struct sample {
	struct pci_dev		*pdev;
	void __iomem		*regs;
	int			nvec;
	struct sample_queue	queue[SAMPLE_MAX_QUEUES];
};

static irqreturn_t sample_irq(int irq, void *data)
{
	struct sample_queue *q = data;
	struct sample *s = q->s;
	u32 cause = ioread32(s->regs + 0x24);

	if (!cause)
		return IRQ_NONE;

	iowrite32(cause, s->regs + 0x64);
	dev_dbg(&s->pdev->dev, "queue %d cause 0x%x\n", q->index, cause);
	return IRQ_HANDLED;
}

static int sample_setup_irqs(struct sample *s)
{
	struct pci_dev *pdev = s->pdev;
	int nvec, i, ret;

	nvec = pci_alloc_irq_vectors(pdev, 1, SAMPLE_MAX_QUEUES,
				     PCI_IRQ_ALL_TYPES);
	if (nvec < 0)
		return nvec;
	s->nvec = nvec;

	for (i = 0; i < nvec; i++) {
		struct sample_queue *q = &s->queue[i];

		q->s = s;
		q->index = i;
		snprintf(q->name, sizeof(q->name), "sample-q%d", i);

		ret = request_irq(pci_irq_vector(pdev, i), sample_irq,
				  pdev->msi_enabled || pdev->msix_enabled ?
				  0 : IRQF_SHARED,
				  q->name, q);
		if (ret)
			goto err_free;
	}

	dev_info(&pdev->dev, "using %d interrupt vector(s), msi=%d msix=%d\n",
		 nvec, pdev->msi_enabled, pdev->msix_enabled);
	return 0;

err_free:
	while (--i >= 0)
		free_irq(pci_irq_vector(pdev, i), &s->queue[i]);
	pci_free_irq_vectors(pdev);
	return ret;
}

static void sample_teardown_irqs(struct sample *s)
{
	int i;

	for (i = 0; i < s->nvec; i++)
		free_irq(pci_irq_vector(s->pdev, i), &s->queue[i]);
	pci_free_irq_vectors(s->pdev);
}

On a device with MSI-X support that grants all four vectors, the log shows:

Text
sample_pci 0000:00:04.0: using 4 interrupt vector(s), msi=0 msix=1

A device offering only a legacy line returns one vector, and the message reads using 1 interrupt vector(s), msi=0 msix=0.

What each piece does

pci_irq_vector(pdev, i) converts the vector index into the Linux IRQ number. With INTx the single vector maps to the device's legacy IRQ, so the same call works in every mode, and the handler code does not need to know which mode was granted. This is the main reason to use the new interface: one code path for all three modes.

The IRQF_SHARED flag is needed only for a legacy line, and it must be passed together with a non-NULL cookie, here the queue pointer, so the kernel can tell shared handlers apart. A message interrupt belongs to one device, so it needs no sharing. The test on msi_enabled and msix_enabled in the example sets the flag only when it is needed. A handler registered with IRQF_SHARED must return IRQ_NONE when the interrupt is not its own, as the if (!cause) line does.

The cookie argument is the same pointer you pass to free_irq(). For a multi-queue device it points at the per-queue structure, so the handler knows which queue fired without a lookup.

Per-CPU spreading

For a device with many queues, ask the kernel to distribute the vectors over the CPUs by adding PCI_IRQ_AFFINITY and, optionally, a struct irq_affinity descriptor for vectors that should not be spread, such as an admin queue:

c
struct irq_affinity affd = { .pre_vectors = 1 };

nvec = pci_alloc_irq_affinity(pdev, 2, SAMPLE_MAX_QUEUES,
			      PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY, &affd);

pre_vectors = 1 leaves vector 0 unspread (for a control queue), and the remaining vectors get their own CPU sets. This is how the NVMe driver gives every I/O queue to a CPU or group of CPUs. The affinity is managed by the kernel: a user cannot change it through /proc/irq/*/smp_affinity, and the CPUs of a managed vector are taken offline only after its queue is drained.

Checking what you got

Look at /proc/interrupts to see the vectors:

Shell
grep sample /proc/interrupts
Text
 45:       0        312          0          0  PCI-MSIX-0000:00:04.0   0-edge      sample-q0
 46:       0          0        118          0  PCI-MSIX-0000:00:04.0   1-edge      sample-q1

The PCI-MSIX word is the interrupt domain and tells you the mode, and 0-edge and 1-edge are the vector indexes. The per-CPU counts show where each vector landed. Also confirm the mode the device itself reports with lspci -vv:

Text
Capabilities: [50] MSI-X: Enable+ Count=4 Masked-

Enable+ is set by the kernel once vectors are allocated, and Count is the table size the device offers.

Teardown order

Free the handlers first and the vectors second. pci_free_irq_vectors() frees the vectors and disables MSI or MSI-X, and it complains with a warning if a handler is still attached to one. In remove, also stop the device from generating interrupts before freeing, for example by clearing its interrupt-enable register, so that an interrupt does not arrive between free_irq() and the end of the function. Disable the source, then free the sink.

If request_irq() fails after some succeeded, free the earlier ones in reverse, as the error path above does, then free the vectors. A partial failure that leaves an earlier handler registered is a leak that makes the next modprobe fail with -EBUSY.

Falling back

pci_alloc_irq_vectors() returns fewer vectors than max_vecs when the device or the platform cannot offer more, and your code must run with whatever it got. A driver written for four queues should create nvec queues and not assume four. If min_vecs cannot be met the call fails, usually with -ENOSPC, which is the cue to try a lower minimum or give up. Platforms may also disable MSI altogether, for example with the pci=nomsi boot parameter, and a driver that accepts PCI_IRQ_INTX keeps working there.

Also inHardware
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.