
Recall first from PCI interrupts and MSI
- 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.
Find the registers in BAR0
3 min readThe last lesson set up PCI interrupts in general. This lesson answers one concrete device, the QEMU edu card, its BAR0 register file, the factorial register, and its interrupt and DMA registers.
A first PCI driver needs a way to tell whether it found the right device, wrote to the right register, and received an interrupt for the right reason. QEMU edu gives you an observable test for each step. Its liveness register inverts a value you write, its factorial register produces a result you can calculate by hand, and a separate register lets you request an interrupt without starting a calculation. These are features of the device described in the QEMU edu specification.
Add -device edu to the QEMU command that starts your Ubuntu guest. Inside that guest, run lspci -nn -d 1234:11e8. The filter uses edu's hexadecimal PCI vendor and device IDs. Use lspci -vv -d 1234:11e8 to inspect Region 0 and the MSI capability. Run the verbose command with sudo if access to capability details is restricted. The assigned address and PCI slot depend on the guest. A board is unnecessary because QEMU supplies this PCI function to the guest.
BAR0 describes a 1 MiB MMIO region. Register offsets are relative to the beginning of the mapping returned by pci_iomap(). For example, reading readl(bar + 0x04) accesses the liveness register for this particular PCI function. Obtain the mapping from the PCI core so the same code works when the device appears at a different address. Edu accepts only 32-bit accesses in the range 0x00 through 0x7f. The documented registers in that range are:
| Offset | Register | Driver use |
|---|---|---|
0x00 |
Identification | Read the device identification and revision value. |
0x04 |
Liveness | Write a 32-bit value and read back its bitwise inverse. |
0x08 |
Factorial | Write the input and read the result after completion. |
0x20 |
Status | Read bit 0 for busy. Set bit 7 to enable the factorial completion IRQ. |
0x24 |
Interrupt status | Read the pending cause bits. |
0x60 |
Interrupt raise | Write a bit mask to make those causes pending. |
0x64 |
Interrupt acknowledge | Write a bit mask to clear those pending causes. |
Use aligned readl() and writel() operations on these offsets. The table lists the defined low registers. DMA starts at 0x80 in the same BAR. This distinction matters when you map only a prefix of a resource. The example maps the entire BAR by passing zero as the maximum length to pci_iomap().
Set bit 7 of status before writing the factorial input if you want an interrupt. A write to 0x08 while busy is ignored, so submit one calculation at a time. For the first run, use 5, whose factorial is 120. Check the returned value as well as the interrupt. A pending cause tells you why the handler ran, while the result tells you whether the intended request actually completed. With factorial interrupts disabled, you can also observe completion by polling status bit 0 with a timeout and reading the result once it clears. A fast calculation can finish before your first status read, so seeing busy clear is sufficient. You do not need to observe it set first.
Raise, identify, and acknowledge an interrupt
2 min readStart by testing interrupt delivery independently. Register the handler first and then write 0x02 to 0x60. The lab uses that synthetic mask so it can distinguish the software-triggered event from factorial completion, which has mask 0x01. Success means that the handler read 0x24, saw 0x02, and acknowledged the pending bit. The factorial test follows the same register sequence after the computation finishes, except that edu sets 0x01 itself.
The Linux IRQ number identifies where the notification arrived. The value read at 0x24 identifies the work that edu reports. Keep those values separate in your code and logs. Read the pending mask once, write the captured mask to 0x64, and use the captured value to decide which waiter to wake. A read of 0x24 alone leaves the bits pending. A readback after the acknowledgement brings the posted write through to the device before the handler signals completion.
The driver below requests one MSI vector. Edu also supports legacy INTx. With a shared INTx handler, a zero pending mask means this function has no cause to service, so return IRQ_NONE. Clearing the last pending bit allows edu to deassert its INTx line. MSI delivers a message, and edu still requires the driver to clear its pending bits through 0x64. The same read-and-acknowledge sequence applies to both delivery modes.
Going deeperHow QEMU tracks pending causes
QEMU maintains the pending mask in irq_status. Raising a cause ORs its mask into that state and acknowledging a cause clears the bits selected by the write. Factorial completion uses 0x01, and DMA completion, when requested, uses 0x100. If the read reports 0x03, both the synthetic event and factorial are pending. Acknowledge 0x03 and test each bit independently. Writing a constant such as 0x01 would leave the synthetic bit pending in that case. The mask records pending causes. Repeated occurrences of one cause can occupy the same bit, so the mask cannot tell you how many times that cause occurred.
Bind the driver and verify the result
6 min readThe complete factorial and IRQ driver below runs one test sequence during probe(). Save the four C blocks in order in edu_lab.c. The module was built and run against the Ubuntu 6.8.0-151-generic kernel in QEMU for this lesson. The module uses the guest kernel's Kbuild flags. Its two completion objects correspond to the two causes under test. Each is initialized before request_irq() because a handler may run as soon as it is registered. The tests run in process context, where they can wait with timeouts. The handler only reads and acknowledges the device, signals completions, and returns.
pci_enable_device() enables access to the function. pci_request_regions() claims its resources, and pci_iomap() maps BAR0. The driver disables factorial interrupt generation and clears stale pending causes before registering the handler. It calls pci_set_master() because an MSI is a memory write that the function itself initiates. In the check run for this lesson, edu never delivered the interrupt without that call, and the synthetic test failed with -ETIMEDOUT (error -110). The driver then allocates one MSI vector and converts vector index zero into the Linux IRQ number using pci_irq_vector(). A failure at any step releases the resources already acquired. A successful probe keeps the binding until removal.
#include <linux/completion.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/slab.h>
enum {
EDU_LIVE = 0x04, EDU_FACT = 0x08, EDU_STATUS = 0x20,
EDU_PENDING = 0x24, EDU_RAISE = 0x60, EDU_ACK = 0x64,
EDU_FACT_DONE = 0x01, EDU_TEST_IRQ = 0x02, EDU_FACT_IRQ = 0x80,
};
struct edu_lab {
void __iomem *bar;
int irq;
struct completion test_done;
struct completion fact_done;
};
static irqreturn_t edu_irq(int irq, void *data)
{
struct edu_lab *edu = data;
u32 pending = readl(edu->bar + EDU_PENDING);
(void)irq;
if (!pending)
return IRQ_NONE;
writel(pending, edu->bar + EDU_ACK);
readl(edu->bar + EDU_PENDING);
if (pending & EDU_TEST_IRQ)
complete(&edu->test_done);
if (pending & EDU_FACT_DONE)
complete(&edu->fact_done);
return IRQ_HANDLED;
}
static void edu_quiet(struct edu_lab *edu)
{
writel(0, edu->bar + EDU_STATUS);
writel(~0U, edu->bar + EDU_ACK);
readl(edu->bar + EDU_PENDING);
}
static int edu_test(struct pci_dev *pdev, struct edu_lab *edu)
{
unsigned long timeout = msecs_to_jiffies(1000);
u32 result;
writel(0x12345678U, edu->bar + EDU_LIVE);
if (readl(edu->bar + EDU_LIVE) != ~0x12345678U)
return -EIO;
writel(EDU_TEST_IRQ, edu->bar + EDU_RAISE);
if (!wait_for_completion_timeout(&edu->test_done, timeout))
return -ETIMEDOUT;
dev_info(&pdev->dev, "synthetic IRQ acknowledged\n");
writel(EDU_FACT_IRQ, edu->bar + EDU_STATUS);
writel(5, edu->bar + EDU_FACT);
if (!wait_for_completion_timeout(&edu->fact_done, timeout))
return -ETIMEDOUT;
result = readl(edu->bar + EDU_FACT);
if (result != 120)
return -EIO;
dev_info(&pdev->dev, "factorial result: %u\n", result);
return 0;
}
static int edu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
struct edu_lab *edu;
int ret;
(void)id;
edu = devm_kzalloc(&pdev->dev, sizeof(*edu), GFP_KERNEL);
if (!edu)
return -ENOMEM;
init_completion(&edu->test_done);
init_completion(&edu->fact_done);
ret = pci_enable_device(pdev);
if (ret)
return ret;
ret = pci_request_regions(pdev, "edu_lab");
if (ret)
goto disable;
edu->bar = pci_iomap(pdev, 0, 0);
if (!edu->bar) {
ret = -ENOMEM;
goto release;
}
edu_quiet(edu);
pci_set_master(pdev);
ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
if (ret < 0)
goto unmap;
edu->irq = pci_irq_vector(pdev, 0);
ret = edu->irq;
if (ret < 0)
goto vectors;
ret = request_irq(edu->irq, edu_irq, 0, "edu_lab", edu);
if (ret)
goto vectors;
ret = edu_test(pdev, edu);
if (!ret) {
pci_set_drvdata(pdev, edu);
return 0;
}
dev_err(&pdev->dev, "edu test failed: %d\n", ret);
goto stop;
stop:
edu_quiet(edu);
free_irq(edu->irq, edu);
vectors:
pci_free_irq_vectors(pdev);
unmap:
pci_iounmap(pdev, edu->bar);
release:
pci_release_regions(pdev);
disable:
pci_disable_device(pdev);
return ret;
}
static void edu_remove(struct pci_dev *pdev)
{
struct edu_lab *edu = pci_get_drvdata(pdev);
edu_quiet(edu);
free_irq(edu->irq, edu);
pci_free_irq_vectors(pdev);
pci_iounmap(pdev, edu->bar);
pci_release_regions(pdev);
pci_disable_device(pdev);
}
static const struct pci_device_id edu_ids[] = {
{ PCI_DEVICE(0x1234, 0x11e8) },
{ }
};
MODULE_DEVICE_TABLE(pci, edu_ids);
static struct pci_driver edu_driver = {
.name = "edu_lab",
.id_table = edu_ids,
.probe = edu_probe,
.remove = edu_remove,
};
module_pci_driver(edu_driver);
MODULE_DESCRIPTION("edu factorial and IRQ lab");
MODULE_LICENSE("GPL");
Put obj-m := edu_lab.o in a file named Makefile beside edu_lab.c. From that directory in the guest, build against the running kernel and load the result:
make -C "/lib/modules/$(uname -r)/build" M="$PWD" modules &&
sudo insmod ./edu_lab.ko
sudo dmesg | tail -n 20
Check lspci -nnk -d 1234:11e8 for edu_lab as the driver in use. A module can load even when its PCI probe fails, so check the binding and the driver's messages. Look for the synthetic acknowledgement and factorial result, then find edu_lab in /proc/interrupts. The interrupt number and counts belong to your guest. This is what the check run printed (an example, your timestamps and IRQ number will differ):
[ 7.845245] edu_lab 0000:00:04.0: synthetic IRQ acknowledged
[ 7.846009] edu_lab 0000:00:04.0: factorial result: 120
Kernel driver in use: edu_lab
30: 2 0 PCI-MSI-0000:00:04.0 0-edge edu_lab
The count of 2 is one synthetic interrupt plus one factorial completion. The status bits and the result of 5! come from the device contract and the input.
The order of cleanup matters because the handler uses the BAR mapping. free_irq() waits for active handlers before returning, so the driver calls it before freeing the vector or unmapping BAR0. Use sudo rmmod edu_lab to unload before replacing or rerunning the module. If you add a second factorial request, start it after the first result has been read. Reset the completion before submitting the new request and keep the single-request rule. After a timeout, a late event can still belong to the original request, so the sample fails probe instead of reusing the completion for another request.
DMA registers in the same BAR
Edu defines the DMA source at 0x80, destination at 0x88, byte count at 0x90, and command at 0x98. In this address range the model accepts 32-bit or 64-bit accesses. The command uses bit 0 to start a transfer, bit 1 to select the direction, and bit 2 to request a DMA completion interrupt. Direction zero means guest memory to edu. Direction one means edu to guest memory.
Going deeperHow edu interprets the DMA endpoints
QEMU stores the programmed source, destination, and count as separate values. One endpoint is a device-visible guest DMA address. The other is an address in edu's internal 4096-byte buffer, which starts at 0x40000 in the device's DMA address convention. For a transfer into edu starting at the beginning of that buffer, program the guest DMA address as the source and 0x40000 as the destination. Reverse the endpoint roles for the return transfer. Program the byte count before setting the start bit. QEMU clears that bit on completion and raises pending cause 0x100 if the command requested an interrupt. The interrupt handler can acknowledge that cause with the same captured-mask write used for factorial. A driver that waits for DMA must also test 0x100 and signal its DMA waiter.
The factorial and synthetic tests need no guest DMA buffer. The next lesson, DMA concepts and coherent memory, explains how Linux supplies a device-visible address for guest memory. That address is what lets you use edu's source and destination registers for a real transfer.
Key takeaways
- Edu exposes a 1 MiB BAR0 and uses 32-bit accesses in the low register range from 0x00 through 0x7f.
- Writing the factorial register starts a calculation, and reading it after completion returns the result.
- Edu requires a device-level acknowledgement for both MSI and legacy INTx.
- The DMA command selects direction, starts the transfer, and can request a completion interrupt.
Reflect
- Explain to a colleague, in two sentences: locate edu by PCI ID and identify its BAR0 register offsets.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- 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
- Kernel documentationHow To Write Linux PCI Drivers
- Kernel documentationThe MSI Driver Guide HOWTO
- Kernel documentationBus-Independent Device Accesses
Official documentation first. Links checked when the lesson was published.