7.10 Ordering patterns
This section assembles 7.5–7.9 into the sequences firmware actually writes. The idiom is bare-metal and TF-A style: explicit barriers, and MMIO accessors that do not contain barriers. The Linux barrier API (wmb, dma_wmb, smp_store_release, writel vs writel_relaxed…) and its A64 lowering are tabulated in Appendix C.
Bring-up note: TF-A’s
mmio_write_32()/mmio_read_32()are plain volatile accesses with no barrier, so they behave like Linux’swritel_relaxed/readl_relaxed, not likewritel/readl. Code ported from a Linux driver that relied onwritelordering a preceding DMA-buffer store must add the barrier explicitly. FreeRTOS ports are the same.
1. Doorbell after a descriptor (to-device, coherent master)
/* desc[] in Normal WB memory; the DMA engine is IO-coherent (snoops CPU caches) */
desc[tail].addr = buf_pa;
desc[tail].len = len;
desc[tail].flags = DESC_OWN_HW; /* ownership last */
tail = (tail + 1) % RING_SZ;
dmboshst(); /* dmb oshst: descriptor stores before doorbell */
mmio_write_32(DMA_BASE + TAIL_REG, tail);
STis enough because the ordering needed is store→store.OSHis needed because the doorbell is Device (Outer Shareable) and the engine is not guaranteed to be in the Inner Shareable domain (7.9).- The alternative is
STLRon the doorbell:stlr w1, [x0]. - If the engine is not coherent, the descriptor and buffer must be cleaned to the PoC first (
DC CVAC…). Cache maintenance is completed by aDSB, not ordered by aDMB, so the sequence becomesDC CVACloop;DSB OSH(TF-A’sclean_dcache_rangeends withdsb sy); doorbell (8.7). After aDSB, no furtherDMBis required.
2. Consuming completions (from-device)
The device writes completion entries and then advances a producer index. The index may be in memory or in a Device register.
// x0 = &ring, x1 = DMA_BASE, w2 = my consumer index
ldr w3, [x1, #PROD_REG] // Device read of producer index
dmb oshld // index read before entry reads (and before later stores)
1: cmp w2, w3
b.eq 2f
ldr x4, [x0, x2, lsl #4] // read entry w2
// ... process ...
add w2, w2, #1
and w2, w2, #(RING_SZ - 1)
b 1b
2: // entry reads before handing slots back
dmb oshld // LD orders earlier loads against later loads AND stores
str w2, [x1, #CONS_REG] // give slots back
Two non-obvious points:
DMB OSHLDis sufficient before the consumer-index store. TheLDvariant orders earlier loads against later loads and stores. That covers load→store, which is exactly “do not let the device overwrite an entry I have not read yet”.- If the index lives in coherent memory instead of a register,
LDARon the index replaces the firstDMB. For a non-coherent master, entries must be invalidated (DC IVAC;DSB) before they are read, and the buffer must not be dirty in the CPU cache while the device writes it (8.7).
When completion is signalled by an interrupt, entering the handler is a context synchronization event (7.8). The handler’s loads are therefore not performed before the interrupt is taken. The device and interconnect must still make the data visible before the interrupt is delivered. With PCIe MSIs, the PCIe ordering rules take care of this. For a wired interrupt it is a property of the device and its integration. A polled loop gets no such help and needs the barrier shown above.
3. DSB before WFI, power-down and reset
| Situation | Sequence | Why |
|---|---|---|
Idle (vPortSuppressTicksAndSleep, TF-A psci_power_down_wfi) | DSB SY; WFI | outstanding accesses complete before the core may lose clocks; WFI itself does not wait for earlier accesses to complete (7.6) |
| CPU_OFF / cluster power-down | clean to PoC, exit coherency, DSB SY, ISB, WFI | 8.8, 17.4 |
| System reset via an SoC reset register | clean any data that must survive to PoC; DSB SY; reset STR; DSB SY; WFI loop | the reset write must leave the PE, and the PE must not run on into code that assumes it did not |
Warm reset via RMR_EL3.RR | DSB SY; ISB; MSR RMR_EL3, x0; ISB; WFI loop | sequence given in core TRMs; on those cores the reset is acted on once the PE enters WFI, which is IMPLEMENTATION DEFINED rather than architectural |
The “clean before reset” step is easy to miss. Boot flags, crash records and “reason for reset” words written with the caches on are lost if they are still dirty in a cache that the reset invalidates.
4. Inter-core mailbox (same Inner Shareable domain)
// Producer (PE0) // Consumer (PE1)
stp x2, x3, [x0] // payload sevl
mov w4, #1 1: wfe
stlr w4, [x1] // flag ldar w5, [x1] // flag
dsb ish // flag done cbz w5, 1b
sev ldp x2, x3, [x0] // payload
STLR/LDAR(orDMB ISHon each side) provides message passing (7.7).DSB ISHbeforeSEVis required becauseSEVis not a memory access. Without it, PE1 can wake, still seeflag == 0, and sleep with no further event coming. That is a lost wake-up.- The consumer can avoid
SEVentirely: load the flag withLDAXR, so that the producer’s store clears the exclusive monitor and generates the wake-up event (10.4). - If the doorbell is an SGI rather than an event, the sequence is: payload stores;
DSB ISHST;MSR ICC_SGI1R_EL1, x0;ISB. ADMBcannot order a system-register write. - Put the request and response mailboxes on separate cache lines (use
CTR_EL0.CWGas the safe granule). Otherwise the two directions ping-pong one line.
5. Inter-chiplet mailboxes
Nothing in the architecture mentions chiplets. What matters is which shareability domain and which coherency the two ends share.
Both ends in the same coherent Inner Shareable domain (for example, CHI C2C extending one coherent system across dies, 8.3). The pattern is identical to §4. Only the timing changes: cross-die line migration costs several times more than on-die. Windows that were nanoseconds wide become wide enough to hit, so a missing barrier that “never failed” on a monolithic die shows up here first.
Ends that are not mutually coherent (a management controller on another die, separate OS islands, or an accelerator’s control CPU). Then:
- Map the shared buffer Normal Non-cacheable on both sides, with identical attributes. A Cacheable mapping on one side and a Non-cacheable mapping on the other is the mismatched-alias case of 6.6.
- Producer: payload stores;
DSB SY; doorbell store to the receiver’s MHU-style Device register. ADMB OSHSTorders observation only for observers in the producer’s Outer Shareable domain. Whether the far die is in that domain is a platform property. ADSBmakes the payload stores complete before the doorbell is issued, which does not depend on that answer. - Receiver: take the doorbell interrupt (a CSE) or poll the doorbell status, then
DMB OSHLDif polling, then read the payload. - Send the acknowledgement the same way in the opposite direction, through its own buffer and doorbell.
Validation note: Stress each mailbox pattern with the endpoints placed on every pair of dies, not only on die 0. Use a sequence number plus a payload checksum: the receiver checks
checksum(payload) == expected(seq). A stale payload with a fresh sequence number is the signature of a missing barrier or a mismatched attribute. Count failures over 107–109 iterations. On the interconnect side, CMN PMU events for cross-die (CCG/C2C) traffic confirm that the test really crossed the link (16.5).
6. Seqlocks
A seqlock lets readers run without writing shared state. They retry if a writer was active.
// Writer (writers already serialised by a lock)
ldr w0, [x9] // seq
add w0, w0, #1 // odd: write in progress
str w0, [x9]
dmb ishst // seq(odd) before data stores
stp x1, x2, [x10] // data
add w0, w0, #1 // even: stable
stlr w0, [x9] // release: data stores before seq(even)
// Reader
1: ldar w3, [x9] // acquire: data loads after seq
tbnz w3, #0, 1b // writer active
ldp x4, x5, [x10] // data (possibly torn)
dmb ishld // data loads before re-reading seq
ldr w6, [x9]
cmp w3, w6
b.ne 1b // retry; only now are x4/x5 valid
The asymmetry is the point of the example:
- The writer’s first increment cannot be an
STLR. Release orders earlier accesses before the store, so the data stores could still become visible beforeseqturns odd. The writer needs the store→storeDMB ISHSTafter it. - The reader’s second
seqread cannot simply beLDAR. Acquire orders later accesses, and the issue here is the earlier data loads being satisfied after theseqre-read. That needsDMB ISHLDbefore the re-read. - In C, the data must be read with relaxed atomics (or
volatilein firmware code), and the reader fence isatomic_thread_fence(memory_order_acquire), which isDMB ISHLD(7.7). Otherwise the compiler may cache or tear the data loads in ways the hardware barrier cannot repair.
Security note: Data read inside a seqlock retry loop is untrusted until the final comparison succeeds. Using a torn field as an index or length before the retry check is a functional bug. It is also a speculation gadget, because the comparison is a conditional branch (14.3). Validate first, and mask indices if they cross a privilege boundary.
Checkpoint
Q7.10.1 A consumer reads completion entries written by a coherent DMA engine and then writes the consumer index to a Device register, freeing those slots. Which barrier between the last entry read and the index write is the minimum sufficient?
- A.
DMB OSHST, because the operation to order is the index store - B.
DMB OSHLD, because the LD variant orders earlier loads against later loads and stores - C.
DSB SY, because a Device store cannot be ordered by aDMB - D. None, because the Device-nR attribute orders the store after earlier loads
Answer
B. The hazard is load→store: the device must not refill a slot before the CPU has read it. DMB LD covers earlier loads against later loads and stores. A is the tempting wrong answer, because DMB ST orders only stores against stores. C is wrong because a DMB does order Device accesses. D is wrong because nR covers only accesses to the same peripheral.
Read more: Arm ARM (DDI0487), Data Memory Barrier (the LD, ST and full variants)
Q7.10.2 In the seqlock reader, replacing DMB ISHLD; LDR seq with a single LDAR seq for the re-read is wrong. Why?
- A.
LDARorders only later accesses after itself. The earlier data loads may still be satisfied after the re-read, so a torn read could pass the check - B.
LDARcannot be used on a location that another PE writes withSTLR - C.
LDARis RCsc, so it deadlocks against the writer’sSTLR - D. It is not wrong.
LDARis strictly stronger thanDMB ISHLD
Answer
A. Acquire is one-way: it stops later accesses moving above it, not earlier ones moving below it. The second read needs every earlier data load ordered before it, which is exactly what DMB ISHLD provides. D is the misconception this question targets.
Read more: Arm ARM (DDI0487), Load-Acquire and barrier-ordered-before
Q7.10.3 A mailbox between two dies that are not mutually coherent uses a Normal Non-cacheable buffer and a Device doorbell register on the receiving die. Why does the producer use DSB SY rather than DMB OSHST between the payload stores and the doorbell store?
- A.
DMBdoes not order accesses to Normal Non-cacheable memory - B.
DSB SYmakes the payload coherent with the receiver’s caches - C. Whether the remote die is an observer in the producer’s Outer Shareable domain is platform-defined.
DSBmakes the payload stores complete before the doorbell is issued, regardless of that - D. The doorbell is a system register, so only a
DSBcan order it
Answer
C. DMB ordering is defined relative to the observers in its shareability domain. DSB SY waits for the payload stores to complete for the full system, a guarantee that does not depend on how the platform draws the Outer Shareable domain. B is wrong because a barrier performs no cache maintenance, and the buffer is Non-cacheable anyway. D is wrong because the doorbell here is memory-mapped.
Read more: Arm ARM (DDI0487), Data Synchronization Barrier and shareability domains