</>EmbeddedInterview

What is the difference between normal and device memory?

MediumARM Architecture

Short answer: Normal memory is suitable for RAM and may be cacheable. Device memory has stricter access attributes suitable for MMIO registers. Linux drivers use ioremap and appropriate I/O accessors.

NormalRAM / cacheDeviceMMIO / peripheral
void __iomem *base = ioremap(phys, size);
/* After checking base: */
writel(1, base);

1. Normal memory: Used for program code, heap and buffers. Depending on attributes, it can be cached, speculatively accessed and reordered.

2. Device memory: Used for registers whose reads or writes can have side effects. Device attributes restrict gathering, reordering and early acknowledgement to varying degrees.

Normal Device
RAM Peripheral MMIO
Often cacheable Not ordinary cacheable RAM
Speculation allowed as appropriate Restricted access behavior

3. Linux driver: Use ioremap or managed resource mapping, then readl/writel. Check mapping errors and release unmanaged mappings. Do not treat MMIO as an ordinary pointer or assume volatile supplies hardware ordering.

Interview takeaway: Memory attributes tell the CPU how accesses may behave; driver accessors enforce the appropriate semantics.

Device-memory attribute subtypes

AArch64 defines Device-nGnRnE, Device-nGnRE, Device-nGRE and Device-GRE. The letters describe permitted Gathering, Reordering and Early write acknowledgement. The n prefix disables that behavior. For example, nGnRnE provides the strictest of these device attributes, while nGnRE permits early write acknowledgement. Device memory is not ordinary cacheable RAM.

Consider a UART receive-data register. Each read may consume a byte from its FIFO. Speculatively reading it or merging two reads would change device behavior. A correctly mapped device register and appropriate accessor preserve the required access semantics.

Normal non-cacheable memory is not identical to Device memory: its permitted speculation and access behavior differ. Linux chooses mapping attributes through ioremap, resource helpers and platform configuration. A posted device write can require a documented readback to ensure it has reached the relevant device before a subsequent action. Do not use memcpy on an arbitrary MMIO register range; use suitable I/O accessors and the device's documented widths.