Q&A-1What is Memory-Mapped I/O (MMIO), and how does it compare to Port-Mapped I/O (PMIO) in terms of instruction set architecture and bus operations?
Answer
What a strong candidate says first
MMIO maps a device's control, status and data registers into the same physical address space as RAM, so the CPU reaches them with ordinary loads and stores. PMIO gives devices a second, separate address space that only dedicated instructions can touch. MMIO inherits the full instruction set, every addressing mode, MMU/MPU protection and DMA reachability, and also inherits caching and speculation, which you must explicitly turn off. PMIO gets a small, cache-free, naturally serialized access path in exchange for a 64 KB address space, a two-instruction vocabulary and extra bus pins. Arm and RISC-V have no PMIO at all; x86 is the architecture that still carries it.
| MMIO | PMIO | |
|---|---|---|
| Instruction set | Any load/store (LDR/STR on Arm, MOV to memory on x86) with all addressing modes and widths. | IN/OUT only. Operand is AL/AX/EAX; port is an 8-bit immediate or DX. |
| Address space | Shared with RAM; consumes physical address space. | Separate, 64 KB on x86, 16-bit port numbers. |
| Bus signalling | Ordinary address/data/control; decode by address range. | Extra pins mark I/O cycles: M/IO#, IOR#, IOW#. |
| Protection | MMU/MPU region or page attributes. | x86: CPL vs IOPL in EFLAGS, plus the TSS I/O permission bitmap. |
| Caching and ordering | Must be typed non-cacheable/Device; barriers required. | Never cached; not reordered with other I/O accesses. |
| DMA | Any bus master can reach it. | CPU-mediated; DMA engines address memory. |
One MMIO store, traced on a Cortex-M4 (STM32F407)
#define GPIOC_ODR (*(volatile uint32_t *)0x40020814UL)
GPIOC_ODR |= (1UL << 13); /* set PC13 */The read-modify-write compiles to:
LDR r3, [r2] @ r2 = 0x40020814
ORR r3, r3, #0x2000
STR r3, [r2]0x40020814sits in0x40000000–0x5FFFFFFF, which the Armv7-M default memory map already types as Device: non-cacheable, and not accessed speculatively.- The store leaves the core on the AHB system bus. The AHB1 matrix decodes the 1 KB window
0x40020800–0x40020BFFand selects GPIOC. - The value lands in ODR; PC13 moves on the next peripheral clock edge.
GPIOC->BSRR = (1UL << 13);is a single store with no read, and is preferable: the RMW above can drop a concurrent write to a different pin in ODR.
An APB peripheral adds a bridge hop. USART2 at 0x40004400 hangs off an AHB-to-APB bridge that asserts PSEL, drives PADDR/PWDATA/PWRITE, and stretches the transfer with wait states.
PMIO in practice
x86 still reaches PCI configuration space through ports: write 0x80000000 | bus<<16 | dev<<11 | func<<8 | reg to 0xCF8, then read or write 0xCFC. IN/OUT take only AL/AX/EAX, with the port as an 8-bit immediate or in DX, so any port above 255 must be materialised in DX first. The whole I/O space is 64 KB. Modern x86 devices (PCIe BARs, LAPIC at 0xFEE00000, IOAPIC at 0xFEC00000) are MMIO; PMIO survives for legacy and configuration access.
Failure modes
- Without
volatile, the compiler deletes the store or hoists the load out of a polling loop.volatilefixes that and nothing else. - Ordering: on Arm a normal-memory store (a DMA descriptor) can become visible after a Device store (the doorbell) unless a
DSBsits between them.DMBorders without waiting. - Read side effects: a status register that clears on read is consumed by a second read, including one from a debugger. Device memory is not speculated, which is why the region is typed that way.
- Faults: an unmapped MMIO address, or a peripheral whose clock is gated (
RCC->AHB1ENRbit clear on STM32), raises BusFault or HardFault. An x86INfrom an unassigned port typically returns0xFFand does not trap. - DMA engines issue memory addresses, so they can drive MMIO directly and cannot drive PMIO; port I/O stays CPU-mediated.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.