Section 4.35

Memory-mapped I/O (MMIO)

Full name: Memory-mapped input/output.

Definition

Memory-mapped I/O (MMIO) is a way for a processor to perform input and output by reading and writing ordinary memory addresses. Each device register, a small storage location inside a peripheral such as a timer or serial port, is given an address in the processor’s address space. The processor accesses the register with the same load and store instructions it uses for RAM, but the device answers, and the access can make the device act. The alternative, port-mapped I/O, uses a separate address space and special instructions.

The idea

Software must be able to tell a serial port to send a byte, ask whether a byte has arrived, and fetch that byte. MMIO gives each of these actions an address. A store of the byte 0x41 to the transmit register’s address sends the letter A. A load from the status register’s address returns bits that describe what the port is doing. A load from the receive register’s address returns the oldest byte that has arrived and removes it from the port’s receive queue. No special instructions are needed, and a C program reaches the device with ordinary pointer operations.

The price is that the address no longer behaves like memory. A cell of RAM returns the value last written to it, however often it is read. A device register returns what the device says at that moment, and the read itself can be a command to the device. A RAM cell is like a page in a notebook: look twice and you see the same words. A receive register is like a ticket dispenser: every pull gives the next ticket, and an unneeded pull wastes one. The analogy stops at registers that do behave like storage: many control registers simply read back the last value written.

How it works

  1. The address map. The chip designer assigns each peripheral a range of addresses in the processor’s address space, the set of all addresses its instructions can name. The Armv7-M architecture, used by processors such as the Arm Cortex-M4 §6.3, reserves the range 0x40000000 to 0x5FFFFFFF for on-chip peripherals. Each chip’s reference manual lists which register sits at which address.

  2. Routing. When the processor issues a load or a store, the bus, the set of connections between the processor, memory and devices, delivers the request to whichever component owns the address. For a peripheral address, the peripheral itself produces the value read or takes the value written.

  3. The register’s documented behavior. For each register, the device’s documentation defines the bit fields; whether software may read it, write it or both; what a read does, for example clear a flag or remove a byte from a queue; what a write does, for example start an operation, or clear only the bits written as 1; and which access widths are allowed.

  4. The processor’s memory type. The processor treats the peripheral range differently from RAM. The Armv7-M architecture gives it the Device memory type. For Device memory the processor performs every access at the size the program issued and exactly as many times as the program issued it, never repeats an access, never keeps a copy in a cache, never reads ahead speculatively, that is, before the program is known to need the access, and never combines several accesses into one. The architecture allows one kind of repetition: an instruction that makes a sequence of accesses, such as a load or store of several registers, can be abandoned when an exception interrupts it and is then restarted, which repeats some of its accesses. Device code therefore reaches registers with single loads and stores.

  5. The compiler’s duty. In C, the program reaches a register through a pointer to a volatile-qualified type, for example *(volatile uint32_t *)0x40002000 for a hypothetical 32-bit register at that address. The volatile qualifier §4.34 obliges the compiler to perform every read and write that the source contains, in source order relative to other volatile accesses. Turning an integer into a pointer is implementation-defined behavior §4.13 in C; the compiler for the platform documents that the result refers to that address.

Three parties each guarantee a different part. The compiler keeps the accesses written in the source. The processor’s memory type keeps their number and size, and keeps accesses to one peripheral’s registers in program order. The device’s documentation says what each access means. Ordering a device access against an access to ordinary memory is a further matter. The Armv7-M architecture lets an access to Normal memory, its name for ordinary memory, and a later access to Device memory be observed in either order. A program that writes a buffer in RAM and then tells a device to read it by direct memory access §4.36 (DMA) therefore needs a memory barrier §4.37 between the two writes: an instruction that holds later accesses back until earlier ones have reached a stated point.

Port-mapped I/O, the alternative, gives devices a separate address space reached only through special instructions; on x86 processors these are in and out. x86 processors support both schemes. Arm processors have no port instructions and use MMIO only.

Worked example

Take a hypothetical event register at address 0x40001000. Its documentation says that bit 0 is 1 when an event has occurred, and that reading the register clears bit 0. The register holds 1, and the program reads it twice:

a = EVT;
b = EVT;

  1. The first read returns 1 and clears the bit, so a=1a = 1 and the register now holds 0.

  2. The second read returns 0, unless a new event arrived in between, so b=0b = 0.

If EVT were an ordinary variable, the compiler could perform one read and give both aa and bb the value 1, and the program would handle one event twice. A debugger window that displays the register, or a diagnostic print that reads it, performs a read too, and consumes the event before the program’s own handler sees it.

A serial port has received the bytes 0x4F and 0x4B, the letters O and K, into its receive queue. The first read of the receive register returns 0x4F and removes it from the queue; the second read of the same address returns 0x4B. One instruction, one address, two different values, and each read changed the device.

Writes behave the same way. A hypothetical status register holds the value 5, binary 101: bits 0 and 2 are set, meaning two pending events. Its documentation says that writing 1 to a bit clears that bit, and writing 0 leaves the bit unchanged. To acknowledge only event 0, the program writes 1, binary 001, and the register becomes 4, binary 100. A careless read-modify-write, STATUS |= 1, reads 5, computes 5 OR 1, which is 5, and writes 5. That write clears both bits, the register becomes 0, and event 2 is lost before anything handled it.

Stated precisely

Model a device register as a state machine §8.33: a system with a current state that moves to a new state in response to each input. A read is an input that returns a value computed from the current state and may change the state. A write is an input that carries a value and may change the state. Ordinary memory is the special case in which a write sets the state to the value written and a read returns the state without changing it, so two reads with no write between them return the same value. A device register need not behave that way, for two separate reasons:

The register’s documentation defines the value each read returns and each state change. A program is correct only if the number, width and order of its accesses on the bus are the ones that documentation requires. The compiler guarantees the accesses written in the source when they go through a volatile-qualified type. On Armv7-M, the Device memory type guarantees that the processor performs exactly those accesses, each at its program size, without merging or speculating them and without repeating them, except when an interrupted multi-access instruction is restarted, and that accesses to Device memory with the same memory settings, such as the registers of one peripheral, are observed in program order.

Limits and common mistakes

Converting a number to a pointer does not by itself grant access to a device. The C standard leaves that conversion to the implementation, and whether a register exists at the address, and which widths it accepts, comes from the chip’s documentation. Depending on the device, an access of the wrong width can be ignored, raise a bus fault (an error the bus reports back to the processor), or affect neighboring bits.

Generic copy routines such as memcpy are wrong for registers. They may read or write with a different width, number or order of accesses than the register requires.

Neither volatile nor the Device memory type says that a device has finished what an access asked it to do. A write that starts a flash erase completes as a bus transfer long before the erase finishes. Software learns of the finish from the status bit or interrupt that the documentation names.

Extra reads are not harmless. Reading a register for a log message, or displaying it in a debugger, can clear a flag or consume data.

A device can change a register at any moment, so a value read is already a past observation. A read-modify-write of a register can overwrite a change the device made between the read and the write. Write-1-to-clear bits let software clear one flag with a single write, without a read-modify-write.

Questions answered

Why may reading a register twice have different side effects?

Reading a device register twice can have different side effects because a read of a memory-mapped register is a request to the device, not a look at stored bytes, and the device decides what each request does. A side effect here is any change an access makes beyond returning a value. Two things make the second read differ from the first.

Ordinary RAM does neither, which is why a compiler may merge or delete repeated RAM reads but must not do so for a register. The register’s documentation says which reads have side effects. Software must perform exactly the reads it intends: access the register through a volatile-qualified type, avoid extra reads in logging or debugging, and never use generic copy routines on registers.

Related entries: Volatile qualifier (§4.34), Memory barrier (§4.37), Pointer provenance (§4.24), Direct memory access (DMA) (§4.36), Interrupt (§6.11). Further reading: [5, 81].