6.2 Normal and Device memory
Every data and instruction access in AArch64 has a memory type, either Normal or Device, plus further attributes.
- Where the type comes from. The type belongs to the access, not to the physical address. With stage 1 enabled, it comes from
MAIR_ELxthrough the descriptor’sAttrIndx(6.5). It is then possibly combined with stage 2 (11.3). With stage 1 disabled, fixed defaults apply (6.5). - One PA, two types. Nothing stops two VAs from giving the same PA different types. What that costs you is in 6.6.
The two types differ in one thing: what the PE and the memory system may do that the program did not literally ask for.
Normal memory
Normal memory is for locations with no side effects: reading a location twice returns the same value, as long as no one writes it in between, and reading has no effect on the location. The architecture therefore permits the PE and the memory system to:
- read speculatively. The PE can perform loads (and instruction fetches, if the region is executable) that the program never architecturally executes, and it can read lines that no instruction asked for (prefetch).
- allocate into caches at any time, if the location is cacheable (6.3). This includes speculative allocation.
- merge and split accesses. Adjacent stores can be combined into one transaction. An access that is not single-copy atomic can be performed as several smaller accesses. One cache-line read can satisfy several loads.
- reorder accesses, subject only to the ordering model of Part 7: dependencies, barriers, acquire/release and same-address coherence.
- perform unaligned accesses. Most loads and stores may be unaligned when
SCTLR_ELx.Ais 0. Exclusives, acquire/release and atomics have stricter rules (10.3).
None of these freedoms changes the architectural result of a single-threaded program. They are visible only to other observers, and the ordering model governs what those observers may see.
Two things are not permitted, even for Normal memory:
- a speculative write becoming observable;
- a spurious write of a value the program did not write. Merging combines bytes the program did write. It does not write the bytes in between.
Device memory
Device memory is for locations where an access has side effects or where reads are not idempotent, such as FIFO pops, clear-on-read status registers and doorbells. Compared with Normal memory, Device memory adds these guarantees:
- No speculative data access. A load or store to Device memory is performed only when the instruction is architecturally executed. This is the property that makes it safe to map a FIFO.
- No caching. Device memory is never allocated into a cache, and there is no “cacheable Device” type.
- Unaligned accesses always fault. An unaligned access to any Device type generates an Alignment fault, whatever
SCTLR_ELx.Asays: DFSC0b100001inESR_ELx(5.8).DC ZVAto Device memory also generates an Alignment fault. - Restricted exclusives and atomics. Whether Load-Exclusive/Store-Exclusive and atomic instructions work on Device memory is IMPLEMENTATION DEFINED. They can fail, or fault with “Unsupported Exclusive or Atomic access” (6.5, 10.1).
- Shareability is fixed. Device memory is always treated as Outer Shareable, whatever the descriptor’s SH field says (6.4).
Two caveats people get wrong
Instruction fetch. “No speculation” covers data accesses. If a Device region is executable, the PE can speculatively fetch instructions from it, and that is a read with side effects on a read-sensitive peripheral. The only architectural protection is to make every Device mapping execute-never (UXN/PXN, or XN at EL2/EL3). TF-A’s xlat library does this unconditionally for MT_DEVICE regions, and it is a common review item. With the MMU off, instruction fetches are Normal memory and there is no XN at all. The only protection is the architectural limit on how far speculative fetches may run ahead of the executed code (see 6.5), so keep read-sensitive peripherals away from boot code (14.2).
Repeated accesses. An instruction that makes several accesses, such as LDP/STP or a multi-register SIMD load or store, can be interrupted by an exception after some of its accesses have been made. It is then re-executed from the start when the exception returns, which repeats the accesses already made. On a clear-on-read or FIFO register, the repeat is a real second access. Use single-register accesses of the register’s natural width (LDR W/STR W for a 32-bit register) for anything with side effects. This is also why readl/writel-style accessors are written in inline assembly rather than left to the compiler.
The G, R and E attributes
Device memory is divided by three attributes. Each is either permitted (the letter) or forbidden (“n” plus the letter).
| Attribute | If permitted | If forbidden (nX) |
|---|---|---|
| G: Gathering | Multiple accesses (to the same or adjacent locations) may be merged into one transaction, and one load may satisfy another | The number and size of accesses are exactly those the program specified |
| R: Reordering | Accesses to the same peripheral may arrive out of program order (barriers still apply) | Accesses from one PE to the same peripheral arrive in program order. “Same peripheral” is an IMPLEMENTATION DEFINED block of address space |
| E: Early write acknowledgement | An intermediate buffer may return the write response | The response should come from the endpoint |
The architecture defines four combinations, ordered from most to least restrictive:
| Type | MAIR encoding | Typical use |
|---|---|---|
| Device-nGnRnE | 0x00 | Strongly ordered registers. This is also the data memory type when stage 1 is off. TF-A maps all MT_DEVICE as nGnRnE |
| Device-nGnRE | 0x04 | General MMIO. Linux ioremap() |
| Device-nGRE | 0x08 | Peripherals that tolerate reordering but not merging. Rare |
| Device-GRE | 0x0C | Write-only bulk regions, such as some frame buffers or accelerator command windows, that tolerate merging but not speculation |
Notes on the precise meaning:
- nR orders by peripheral, not against Normal memory. It orders accesses to the same peripheral block by one PE. It does not order Device accesses against Normal memory, nor accesses to two different peripherals. Those need barriers (7.9). Since the block size is IMPLEMENTATION DEFINED, do not rely on nR between two register blocks that happen to be close in the address map.
- E is a hint. DDI0487 describes the early-write-acknowledgement attribute as a hint, so an interconnect may still acknowledge a write early even when the attribute is nE. Arm recommends nE where the programmer needs a write response that reflects the endpoint. The PCIe configuration space, whose writes are non-posted, is the standard example. Even with nE,
STRfollowed byDSBguarantees only that the write’s response has been received. It does not guarantee that the peripheral has finished acting on the write, for example by deasserting an interrupt line (7.9). - G applies to accesses the program actually makes. Gathering never adds speculative accesses. Device-GRE is still non-speculative. That is the difference between Device-GRE and Normal Non-cacheable, which also merges and reorders but can be read speculatively.
Normal Non-cacheable vs Device-GRE
People often choose between these two for write-combining buffers (Linux ioremap_wc() uses Normal Non-cacheable).
| Property | Normal NC (0x44) | Device-GRE (0x0C) |
|---|---|---|
| Speculative reads | Permitted | Not permitted |
| Gathering / reordering | Permitted | Permitted |
| Unaligned access | Permitted (if SCTLR_ELx.A=0) | Alignment fault |
DC ZVA | Permitted | Alignment fault |
| Shareability | Treated as Outer Shareable | Treated as Outer Shareable |
| Exclusives | Requires global monitor support (IMPLEMENTATION DEFINED) | IMPLEMENTATION DEFINED |
Use Normal NC for memory that is genuinely RAM but must bypass caches, such as DMA buffers for a non-coherent master (8.7). Use a Device type for anything where an unrequested read could be harmful.
The XS attribute (brief)
With FEAT_XS (v8.7), each access also carries an XS attribute, which marks memory that may be slow to respond. It matters for TLBI ... nXS and DSB ... nXS, which do not wait for XS=1 accesses to complete (9.1).
- Defaults. Under the original MAIR encodings, Device, Normal Non-cacheable and Normal Write-Through memory have XS=1, and Inner and Outer Write-Back memory has XS=0. The extra encodings below exist to give exactly those XS=1 types an XS=0 variant. For mixed Inner/Outer cacheability, see the
MAIR_ELxdescription. - Extra encodings. FEAT_XS adds MAIR encodings with XS=0 for Device memory and for some Normal types (6.5).
Security note: A Normal mapping of a peripheral is a covert “read anything at any time” licence for the PE’s speculation. If a Secure peripheral, or a TZASC-protected carve-out, is mapped Normal in the Non-secure world’s linear map, speculative reads can reach it. The access is blocked, but blocked transactions may be reported as asynchronous SErrors that are very hard to attribute. Map peripherals as Device and XN, and keep protected carve-outs out of other worlds’ tables entirely.
Validation note: Two directed tests are worth having for every new interconnect or peripheral bridge.
- Device speculation. Map a read-sensitive register (a counter that increments on read, or a FIFO) as Device-nGnRE behind a mispredicted branch, and check that the counter does not move. Repeat with a Normal NC mapping to prove that the test can detect speculation.
- Gathering. Issue two adjacent
STRHto an nG mapping and check on a bus monitor, ELA trace or RTL waveform that there are two transactions, not one merged 32-bit write.An unexpected Alignment fault (DFSC
0b100001) on an apparently aligned access usually means the region is mapped Device when it was meant to be Normal, or that the MMU is off.
Checkpoint
Q6.2.1 A UART’s receive FIFO register pops a byte on every read. It is mapped Device-nGnRE with PXN=0 at EL1, and the code never branches to that address. Bytes are occasionally lost. What is the most plausible architectural explanation?
- A. Device-nGnRE permits gathering, so two reads were merged into one
- B. The region is executable, so the PE can speculatively fetch instructions from it, which pops the FIFO
- C. nGnRE allows speculative data reads; nGnRnE is needed to forbid them
- D. The early write acknowledgement attribute allows reads to be repeated
Answer
B. Device memory forbids speculative data accesses, but not speculative instruction fetch from an executable region. Device mappings must be XN. C is the tempting error: no Device type, including nGnRE, is ever speculatively read for data. The G/R/E attributes concern merging, ordering and write responses, not speculation. A is wrong because “nG” forbids gathering.
Read more: Arm ARM (DDI0487), “Device memory” and “Execute-never controls”
Q6.2.2 A driver does STR W1, [X0] (Device-nGnRnE) to clear an interrupt status bit, then DSB SY, then exception return. The interrupt still re-fires once. Which statement is correct?
- A. nGnRnE guarantees the peripheral has acted on the write before the
STRretires, so the GIC must be at fault - B. The
DSBwas unnecessary, because nE already makes the store complete before the next instruction - C. Using nGnRE instead would fix it, because early acknowledgement is faster
- D. The
DSBguarantees only that the write’s response was received; the peripheral may not have deasserted its line yet, so a read-back of the device (or a status poll) is needed
Answer
D. Completion means the write response was received. With nE, that response should come from the endpoint, but E is a hint, and the peripheral’s side effect (deasserting the interrupt) can still lag. B confuses a memory attribute with a barrier: nothing makes the PE wait for a store except a DSB, or a dependent read. A overstates what nE promises.
Read more: Arm ARM (DDI0487), “Early Write Acknowledgement” and “Completion of memory accesses”
Q6.2.3 A firmware routine copies a structure to a peripheral’s register window with memcpy(). It works at -O0 and takes an exception at -O2. ESR shows an Alignment fault. Why?
- A. At
-O2, the compiler emittedLDP/STPor unaligned wide accesses, and any unaligned access to Device memory faults whateverSCTLR_ELx.Asays - B. Device memory forbids
STPentirely - C. The window must be mapped Device-GRE to permit
memcpy() - D.
-O2enabled speculation, which Device memory forbids
Answer
A. An optimised memcpy uses wide, possibly unaligned accesses, and possibly DC ZVA in memset. Any unaligned access to Device memory generates an Alignment fault. Even when the accesses are aligned, LDP/STP can repeat accesses after an interrupt and may not match the register widths. B overstates the rule: an aligned STP to Device memory is legal. Use explicit accessors of the register width.
Read more: Arm ARM (DDI0487), “Alignment of data accesses” and “Device memory”