Skip to content
systemcoding.dev
Kernels

Develop a PCI Driver Against QEMU's edu Device

Write a PCI driver for QEMU 9.2's educational edu device on Linux 6.12: ID check, liveness register, factorial engine and interrupt, with the register map.

7 min read
Summarize in AI
Flat illustration of guest machines running on a hypervisor over hardware, with the QEMU, Linux and C logos.

Real PCI hardware is a poor place to learn driver development: a wrong write can hang the machine, and each experiment ends in a reboot. QEMU ships a purpose-built virtual PCI device for exactly this, called edu. It has a small register map, a factorial engine that raises an interrupt, and a DMA engine, and all of it is documented in QEMU's specification. This post builds a driver for it on a Linux 6.12 guest and QEMU 9.2: it reads the device ID, uses the liveness register, computes a factorial through the registers and waits for the completion interrupt. It builds on the driver skeleton and BAR mapping.

The register map below is from the edu device specification in the QEMU documentation, and the guest kernel side follows the PCI driver guide.

The device

QEMU adds the device to a guest with one option, -device edu. It appears on the guest's PCI bus with vendor ID 0x1234 and device ID 0x11e8, which is the pair the driver's table matches. It has one BAR, BAR 0, a 1 MiB memory region, and it supports legacy interrupts and MSI.

The registers, as the specification lists them, are:

OffsetAccessMeaning
0x00readIdentification: 0xRRrr00ed, major RR and minor rr version
0x04read/writeLiveness check: a read returns the bitwise inverse of the last value written
0x08read/writeFactorial: write a value, the device computes its factorial, read the result
0x20read/writeStatus: bit 0 set while computing, bit 7 enables the interrupt on completion
0x24readInterrupt status
0x60writeRaise an interrupt with the written value
0x64writeAcknowledge interrupts: clears the bits written
0x80 to 0x98read/writeDMA source, destination, count and command

The factorial register has a useful rule. While the device is computing, writes to it are ignored, and the status register's bit 0 reads 1. When the computation finishes, bit 0 clears, and if bit 7 of status was set the device raises an interrupt with status bit 0 of the interrupt status register set. The computation is deliberately slow enough to observe.

The driver

The skeleton is the one from the earlier post, with the table matching 0x1234:0x11e8, and a probe that maps BAR 0 and runs three checks. The register names and the poll loop are the new part:

edu.cc
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/iopoll.h>

#define DRV_NAME	"sample_edu"

#define EDU_REG_ID	0x00
#define EDU_REG_LIVE	0x04
#define EDU_REG_FACT	0x08
#define EDU_REG_STATUS	0x20
#define EDU_STATUS_BUSY	BIT(0)

static const struct pci_device_id edu_ids[] = {
	{ PCI_DEVICE(0x1234, 0x11e8) },
	{ }
};
MODULE_DEVICE_TABLE(pci, edu_ids);

struct edu {
	struct pci_dev	*pdev;
	void __iomem	*regs;
};

static int edu_factorial(struct edu *e, u32 n, u32 *result)
{
	u32 status;
	int ret;

	iowrite32(n, e->regs + EDU_REG_FACT);

	ret = readl_poll_timeout(e->regs + EDU_REG_STATUS, status,
				 !(status & EDU_STATUS_BUSY), 10, 100000);
	if (ret)
		return ret;

	*result = ioread32(e->regs + EDU_REG_FACT);
	return 0;
}

static int edu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
	struct edu *e;
	u32 val, fact;
	int ret;

	e = kzalloc(sizeof(*e), GFP_KERNEL);
	if (!e)
		return -ENOMEM;
	e->pdev = pdev;

	ret = pci_enable_device(pdev);
	if (ret)
		goto err_free;
	ret = pci_request_regions(pdev, DRV_NAME);
	if (ret)
		goto err_disable;

	e->regs = pci_iomap(pdev, 0, 0);
	if (!e->regs) {
		ret = -ENOMEM;
		goto err_release;
	}

	val = ioread32(e->regs + EDU_REG_ID);
	dev_info(&pdev->dev, "edu id 0x%08x\n", val);

	iowrite32(0x12345678, e->regs + EDU_REG_LIVE);
	val = ioread32(e->regs + EDU_REG_LIVE);
	dev_info(&pdev->dev, "liveness: wrote 0x12345678, read 0x%08x\n", val);

	ret = edu_factorial(e, 5, &fact);
	if (ret) {
		dev_err(&pdev->dev, "factorial timed out: %d\n", ret);
		goto err_unmap;
	}
	dev_info(&pdev->dev, "5! = %u\n", fact);

	pci_set_drvdata(pdev, e);
	return 0;

err_unmap:
	pci_iounmap(pdev, e->regs);
err_release:
	pci_release_regions(pdev);
err_disable:
	pci_disable_device(pdev);
err_free:
	kfree(e);
	return ret;
}

static void edu_remove(struct pci_dev *pdev)
{
	struct edu *e = pci_get_drvdata(pdev);

	pci_iounmap(pdev, e->regs);
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	kfree(e);
}

static struct pci_driver edu_driver = {
	.name		= DRV_NAME,
	.id_table	= edu_ids,
	.probe		= edu_probe,
	.remove		= edu_remove,
};
module_pci_driver(edu_driver);

MODULE_DESCRIPTION("Driver for QEMU's edu device");
MODULE_LICENSE("GPL");

With the device at guest address 0000:00:04.0 and edu version 1.0, the log after insmod reads:

Text
sample_edu 0000:00:04.0: edu id 0x010000ed
sample_edu 0000:00:04.0: liveness: wrote 0x12345678, read 0xedcba987
sample_edu 0000:00:04.0: 5! = 120

The liveness value is the bitwise inverse of what was written, since ~0x12345678 is 0xedcba987, which is how the device proves it is alive and not a stale register read.

Why readl_poll_timeout

The factorial is not instant, so the driver cannot read the result right after writing. readl_poll_timeout() from <linux/iopoll.h> repeats a register read until a condition holds or a timeout expires. The arguments are the address, the variable that receives each read, the condition, the sleep between reads in microseconds, and the total timeout in microseconds. It returns zero on success and -ETIMEDOUT on failure.

The function sleeps between reads, so it can be called only from process context, which probe is. For code that must not sleep, there is readl_poll_timeout_atomic(), which busy-waits with udelay(). Never write your own while (...) ; loop for hardware: it has no timeout, so a dead device hangs the CPU forever.

Interrupt instead of polling

Polling is fine for a probe-time check, but a driver that does real work wants to sleep until the device signals completion. The device supports this: set bit 7 of the status register before starting, and when the computation ends, the device raises an interrupt and sets bit 0 in the interrupt status register at 0x24. The handler reads that register, writes the same bits to 0x64 to acknowledge, and wakes the waiting thread:

c
static irqreturn_t edu_irq(int irq, void *data)
{
	struct edu *e = data;
	u32 status = ioread32(e->regs + 0x24);

	if (!status)
		return IRQ_NONE;

	iowrite32(status, e->regs + 0x64);
	complete(&e->done);
	return IRQ_HANDLED;
}

Returning IRQ_NONE when no cause bit is set is required with a shared legacy interrupt line, since other devices on the line will also reach the handler. Acknowledging the interrupt before returning is not optional: a level-triggered interrupt that is never cleared fires again immediately and locks the CPU in the handler. Requesting the interrupt properly, with MSI where available, is the subject of the MSI post.

Running it in a guest

The guest needs a Linux 6.12 kernel with module support and the headers to build against. The QEMU command line adds the device to an ordinary virtual machine with -device edu, and the driver is built in the guest or cross-built and copied in. The kernel log of the guest shows the lines above. Run nothing of this on a host with real hardware at the same address: the whole point of the device is that nothing a wrong driver does can reach anything real.

The DMA engine at 0x80 to 0x98 is the next step. It moves bytes between guest RAM and a 4 KiB buffer inside the device. Using it needs a DMA mapping and bus mastering, which belongs to the DMA post.

What goes wrong, and what it tells you

Three failures are common on a first run. If the driver never binds, the device is not in the guest at all, or another driver holds it: run lspci -nn in the guest and look for 1234:11e8, then lspci -k for the bound driver. If the ID prints as 0xffffffff, the device is not enabled or memory decoding is off, so pci_enable_device() was skipped or failed without being checked. If the factorial times out with -110, the status bit never cleared, which on this device means the write to the factorial register went to the wrong offset or the BAR was mapped from the wrong index. Each symptom points to one cause, and that is exactly why a virtual device with a documented register map is a better teacher than a real one, where the same symptoms could be a hardware fault, a firmware quirk or your code.

The same driver structure carries over to real hardware unchanged. Only the register names, the offsets and the ID table differ, so time spent on the checks above transfers directly.

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.