Memory-mapped I/O

Also known as: MMIO

On this page 7

Memory-mapped I/O (MMIO) is a technique in which device registers and device memory are assigned addresses in the same physical address space as RAM, so the processor controls hardware with ordinary load and store instructions. In the area of Drivers & devices, it is the dominant way modern operating systems and device drivers communicate with graphics cards, network adapters, storage controllers and almost every peripheral on PCI Express or on-chip buses.

Instead of using special input/output instructions, a driver reads a status register or writes a command register exactly as it would read or write a variable. The difference is that the access does not reach RAM: the memory controller or interconnect routes it to the device, which reacts immediately. This simplicity is powerful, but it requires careful handling of caching, ordering and access width.

How memory-mapped I/O routes accesses to devices

The system firmware and the operating system divide the physical address space into regions. Some ranges map to DRAM, while others are claimed by devices through mechanisms such as PCI Base Address Registers (BARs) or entries in a device tree on ARM systems. When the CPU issues a load or store to one of these device ranges, the interconnect forwards the transaction to the device instead of to memory.

A kernel cannot use physical addresses directly once paging is enabled, so it first maps the device region into virtual memory. Linux drivers call functions such as ioremap or pci_iomap, and Windows drivers use MmMapIoSpace. The resulting virtual address is then accessed only through dedicated accessor functions such as readl and writel, which guarantee the correct width and ordering.

StageWhat happensComponent involved
Address assignmentDevice region placed in physical address spaceFirmware and PCI enumeration
MappingRegion mapped into kernel virtual memoryDriver and kernel
AccessDriver issues load or store instructionCPU
RoutingTransaction forwarded to the deviceInterconnect or root complex
Side effectDevice updates state or returns register valueDevice
Main stages of an MMIO access

Memory-mapped I/O compared with port-mapped I/O

The main alternative is port-mapped I/O, in which devices live in a separate I/O address space reached only through special instructions such as IN and OUT on x86. Port-mapped I/O remains in use for legacy x86 devices, but most current architectures, including ARM and RISC-V, offer only memory-mapped I/O.

AspectMemory-mapped I/OPort-mapped I/O
Address spaceShared with system memorySeparate I/O space
Instructions usedNormal load and storeDedicated IN and OUT
Address range sizeLarge, up to many gigabytes64 KB on x86
Architecture supportNearly universalMainly x86
Access from user spacePossible through page mappingRequires special privileges
Typical devicesGPUs, NICs, NVMe controllersLegacy serial ports, PS/2, PIT
Memory-mapped and port-mapped I/O side by side

Memory-mapped I/O wins in modern hardware because it supports large register windows and device memory, works with all addressing modes and lets the operating system use the normal page protection mechanism to grant or deny access.

Caching, ordering and access width

Device registers are not ordinary memory: reading a register can have side effects, such as clearing an interrupt flag, and writes must reach the device in program order. For this reason, MMIO regions are mapped with special memory types that control caching and write behavior.

Memory typeBehaviorTypical use
UncachedEvery access goes to the device in orderControl and status registers
Write-combiningWrites may be merged and bufferedFrame buffers, GPU apertures
Write-back cachedNormal caching as for RAMNever used for device registers
Device-nGnRE on ARMNo gathering, no reordering, early write ackMost ARM peripheral registers
Memory types used for device mappings

Even with uncached mappings, the CPU and compiler may still reorder accesses relative to normal memory. Drivers therefore use memory barriers and the kernel’s accessor functions, which include the required ordering. Accessing a 32-bit register with a wrong access width, for example two 16-bit writes, can produce undefined device behavior or a bus error.

MMIO, DMA and interrupts working together

Memory-mapped I/O is best suited to small control operations, not to moving bulk data. A typical driver writes descriptors and a doorbell register through MMIO, lets the device transfer the actual payload by direct memory access (DMA), and learns about completion through an interrupt or message signaled interrupt.

Reads across PCI Express are particularly expensive because the CPU must wait for a round trip to the device, often around one microsecond. Well-designed drivers therefore avoid MMIO reads in the hot path, preferring status information that the device writes into host memory. An IOMMU complements this design by restricting which memory a device may reach with DMA.

Security and reliability considerations

  • Only privileged code should map device registers, because direct access can bypass operating system controls; user-space drivers receive narrowly scoped mappings through frameworks such as VFIO or UIO.
  • Speculative execution must not touch MMIO, which is one reason device regions are marked as non-cacheable and non-speculative.
  • Hot-unplugged devices return all-ones values on reads, so robust drivers check for 0xFFFFFFFF before trusting a register.
  • Overlapping or misconfigured BARs can cause hangs at boot and are a classic firmware bug.

A plain-language summary

Picture a large building where most doors lead to storage rooms, but a few doors, which look exactly the same, open into control rooms for the elevators, lights and heating.

Memory-mapped I/O works like those special doors. The computer talks to its hardware by writing to and reading from certain addresses, just as it would with normal memory. The difference is that these addresses belong to devices, so writing a value can switch something on, start a transfer or reset a component.

An example from practice

A driver developer brings up support for a new network interface card on PCI Express in a Linux server. During enumeration, the card exposes a 128 KB register window through its first BAR, and the driver maps it with pci_iomap.

To send a packet, the driver fills a transmit descriptor in host memory and then writes the new tail index to a doorbell register through writel. The card reads the descriptor and the packet by DMA and raises a message signaled interrupt when it is done. An early prototype also read a status register after every packet, which capped throughput at roughly one million packets per second.

After the developer replaced that register read with a completion status written by the card into host memory, throughput more than doubled on the same hardware, illustrating why MMIO reads should stay out of the fast path.