Map PCI BARs Into Kernel Space With pci_iomap
Map a PCI BAR on Linux 6.12 with pci_iomap, read and write device registers with ioread32 and iowrite32, check sizes first and avoid the posted-write trap.

A PCI device's registers live at a physical address chosen by firmware, and the kernel cannot touch a physical address directly. It needs a virtual mapping with the right caching attributes, and an access routine that the architecture knows how to compile into the correct instruction. pci_iomap() provides all of that for a single BAR in one call, and ioread32() and iowrite32() are the accessors that go with it. This post uses Linux 6.12 to map BAR 0, validate it, read and write registers, and avoid the two mistakes that account for most MMIO bugs: treating the mapping as ordinary memory and trusting a write to have arrived. It extends the PCI driver skeleton.
The functions are documented in the kernel's bus-independent device accesses guide.
Why not a plain pointer
A BAR range is device memory, and the CPU must not treat it like RAM. The mapping has to be uncached, or write-combining where the device allows, so that every access really reaches the device. The compiler must not merge, reorder or drop accesses. And on some architectures a special instruction is needed. A pointer dereference gives you none of that guarantees, and it can appear to work on x86 while failing on arm64.
The kernel's answer is a typed pointer, void __iomem *, and a small family of accessors. The __iomem annotation lets the sparse checker catch code that dereferences it directly, and the accessors produce the right access on every architecture.
Map and unmap
void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen);
void pci_iounmap(struct pci_dev *dev, void __iomem *p);pci_iomap() looks at the BAR's flags and does the right thing: for a memory BAR it returns an ioremap()ped address, and for an I/O port BAR it returns a cookie that the same accessors understand. That is why the generic accessors below work for both. The third argument limits the mapped length, and zero maps the whole BAR. It returns NULL on failure, which happens if the BAR has zero length or the mapping cannot be created.
Check the BAR before you map it, because the code that follows assumes a size:
#define SAMPLE_BAR 0
#define SAMPLE_REG_ID 0x00
#define SAMPLE_REG_CTRL 0x04
#define SAMPLE_MIN_LEN 0x08
struct sample {
struct pci_dev *pdev;
void __iomem *regs;
};
static int sample_map(struct sample *s)
{
struct pci_dev *pdev = s->pdev;
if (!(pci_resource_flags(pdev, SAMPLE_BAR) & IORESOURCE_MEM)) {
dev_err(&pdev->dev, "BAR %d is not memory\n", SAMPLE_BAR);
return -ENODEV;
}
if (pci_resource_len(pdev, SAMPLE_BAR) < SAMPLE_MIN_LEN) {
dev_err(&pdev->dev, "BAR %d is too small\n", SAMPLE_BAR);
return -ENODEV;
}
s->regs = pci_iomap(pdev, SAMPLE_BAR, 0);
if (!s->regs)
return -ENOMEM;
return 0;
}In remove, and on every error path after this succeeds, undo it with pci_iounmap(pdev, s->regs). Call it before pci_release_regions(), since the reservation should outlive the mapping.
Read and write registers
The 32-bit accessors are ioread32() and iowrite32(), and there are 8 and 16-bit forms (ioread8(), ioread16()) and a 64-bit pair (ioread64(), from <linux/io-64-nonatomic-lo-hi.h>, which splits it into two 32-bit accesses). Note the argument order of the write: value first, address second, which is the reverse of what many people expect.
u32 id = ioread32(s->regs + SAMPLE_REG_ID);
iowrite32(0x12345678, s->regs + SAMPLE_REG_CTRL);
u32 back = ioread32(s->regs + SAMPLE_REG_CTRL);
dev_info(&s->pdev->dev, "id 0x%08x, ctrl 0x%08x\n", id, back);For a device whose ID register reads 0x010000ed and whose control register stores what you write, the log shows:
sample_pci 0000:00:04.0: id 0x010000ed, ctrl 0x12345678Pointer arithmetic on a void __iomem * counts bytes, so s->regs + 0x04 is the register at offset 4, not the fourth 32-bit register. Keep offsets as named constants taken from the datasheet. A raw number in the call is a bug waiting for the next reader.
The accessors handle endianness in one fixed way: ioread32() and iowrite32() treat the device as little-endian, which is what PCI defines, and convert for a big-endian CPU. For a device that is big-endian internally, use ioread32be() and iowrite32be(). Never byte-swap by hand and then use the plain accessors, since on a big-endian host the swap would cancel the kernel's own.
The posted-write trap
Writes to a PCI device are posted. The CPU hands the write to the interconnect and moves on without waiting for the device to accept it. The consequence is that a write and the code that follows it can run in an order the hardware does not enforce:
iowrite32(RESET_BIT, s->regs + SAMPLE_REG_CTRL);
udelay(10); /* assumes the write reached the device before the delay starts */The delay starts counting from when the CPU issued the write, not from when the device saw it, and on a deep bus the gap can be large. The standard repair is a read back from the same device:
iowrite32(RESET_BIT, s->regs + SAMPLE_REG_CTRL);
(void)ioread32(s->regs + SAMPLE_REG_CTRL); /* flushes the posted write */
udelay(10);A read cannot complete until earlier writes to the same device have been delivered, so it acts as a barrier toward the device. It is the idiom behind every "flush the write" comment in the kernel. Use it whenever a write must take effect before the next step: starting a transfer, clearing an interrupt before re-enabling, or timing a reset.
Ordering between the CPU and the device's DMA is a separate matter. A write that tells the device to start DMA must come after the data it will read is visible in memory, and that needs wmb() or the stronger guarantees of the DMA mapping calls, covered in the DMA post.
Bounds and offsets
The mapping is exactly as large as the BAR. Reading past its end reads whatever the bridge routes there, and on many systems that produces a machine check or a hang. Validate any offset that comes from outside the driver, such as an ioctl argument or a sysfs write, against pci_resource_len(). Also keep to naturally aligned accesses: a 32-bit access to an odd address is rejected by some bridges.
If a BAR returns all ones on read (0xffffffff), the device is not responding. The usual causes are that the device was not enabled with pci_enable_device(), that memory decoding is off in the command register, or that the device fell off the bus after a link error. A driver can treat an all-ones read from a register that should never contain it as a surprise-removal signal, a pattern covered in the hotplug post.
Mapping parts of a BAR
When a BAR is large, mapping all of it is wasteful, and a device may expect only a window to be touched. Pass a length to map only the start, or use pci_iomap_range() with an offset and length for a slice. A frame buffer BAR of 256 MiB that you use only for a 4 KiB command area is the case. pci_iomap_wc() maps with write-combining for prefetchable BARs that you write in bursts, such as a frame buffer, and it must be used only for memory whose reads and writes have no side effects.
Seeing the mapping
/proc/iomem lists the reservation your driver made, with the name you passed to pci_request_regions():
grep sample_pci /proc/iomem febf0000-febfffff : 0000:00:04.0
febf0000-febfffff : sample_pciThe outer line is the BAR as assigned to the device, and the inner one is your claim. It is the quickest way to confirm that the region you think you own is the one the driver holds.


