Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix C: Barrier and maintenance recipes

This appendix is a lookup page. Each recipe gives the minimal sequence, which point (PoU, PoC, PoP, PoPA) and which domain (ISH, OSH, SY) it uses and why, and the section that derives it. The recipes assume AArch64, Normal Inner Shareable Write-Back memory for data shared between PEs, and Device-nGnRE for MMIO, unless they say otherwise. They do not replace the derivations: if your case differs from the stated assumptions, go back to the linked section.

How to read the domain and point choices

Four independent questions decide every recipe. Mixing them up causes most barrier bugs (7.8 separates the memory axes, ordering and completion, from context synchronization).

QuestionToolChoice
In what order must other observers see my accesses?DMB, LDAR/STLR, dependenciesThe access type (LD, ST or full) is what matters (7.5)
Must an access or maintenance operation have finished before something that is not a memory access (a system-register write, SEV, WFI, a TLBI taking effect, a non-coherent master reading DRAM)?DSBThe scope must cover every PE or master that the TLBI or IC broadcast must reach: ISH for PEs, OSH when TLBI ...OS or a master outside the Inner Shareable domain is involved, SY when in doubt (7.6)
Must later instructions run with a new context (system registers, translation, fetched code)?ISB or another context synchronization eventLocal to the PE that executes it (7.8)
Where must data reach for a given observer to see it?DC / IC by VA (or by PA at EL3)PoU for this PE’s or the Inner Shareable domain’s instruction fetch; PoC for any master that does not snoop; PoP/PoDP for persistence; PoPA for a change of physical address space (8.4)

The domain on a DMB: this book follows the position, described in 7.5, that the DMB shareability argument has no functional effect in an other-multi-copy-atomic model. The recipes below still write DMB OSH* for anything involving Device memory or a DMA master and DMB ISH* between PEs. That spelling is correct under both the classical reading and the current one, and it matches what TF-A and Linux emit. Where the choice really matters is on a DSB that completes a TLBI or IC operation, and on whether a cache maintenance operation reaches the right point.

1. Ordering between PEs (coherent memory)

#CaseSequenceDomain and whySee
1.1Message passing: payload, then flagProducer: payload stores; STLR flag. Consumer: LDAR flag; payload loads. Equivalent: DMB ISHST before the flag store and DMB ISHLD after the flag loadISH: both ends are PEs in one Inner Shareable domain7.3, 7.7
1.2Lock releaseSTLR on the lock word, or DMB ISH; STR. Not DMB ISHST: the critical section’s loads must also be ordered before the releaseISH; full barrier because load→store order is needed7.5, 10.4
1.3Consumer-side ordering by dependencyLDR flag/pointer; address dependency into the payload loadNo barrier; a control dependency alone orders only later stores, so add ISB (control+ISB) to order a later load7.4
1.4Store-buffering (Dekker): each side writes its flag and then reads the other’sSTLR then LDAR (RCsc), or STR; DMB ISH; LDR. LDAPR is not enoughFull barrier: store→load order7.3, 7.7
1.5Wake a waiter with an eventProducer: STLR flag; DSB ISH; SEV. Consumer: SEVL; 1: WFE; LDAR; CBZ 1bDSB because SEV is not a memory access; without it, a lost wake-up7.10 §4
1.6Wait without SEVConsumer: LDAXR the flag; WFE. The producer’s store to the monitored granule clears the monitor and generates the eventNo barrier on the producer side beyond release semantics10.4
1.7Send an SGI after writing dataData stores; DSB ISHST; MSR ICC_SGI1R_EL1, x0; ISBDSB: a DMB cannot order a system-register write7.10 §4
1.8Seqlock writerSTR seq (odd); DMB ISHST; data stores; STLR seq (even)The first increment cannot be a release; it needs store→store order after it7.10 §6
1.9Seqlock readerLDAR seq; data loads; DMB ISHLD; LDR seq; compare and retryThe re-read cannot be LDAR: earlier loads must be ordered before it7.10 §6
1.10Mailbox between dies in one coherent domainAs 1.1 or 1.5Same as on one die; only latency changes7.10 §5
1.11Mailbox between agents that are not mutually coherentMap the buffer Normal Non-cacheable on both sides (identical attributes); payload stores; DSB SY; doorbell store. Receiver: doorbell interrupt, or poll then DMB OSHLD; payload loadsDSB SY makes the payload complete regardless of whether the far agent is in this PE’s Outer Shareable domain7.10 §5, 10.4

2. Device memory and DMA

#CaseSequencePoint / domain and whySee
2.1Doorbell after descriptors, coherent masterDescriptor stores (ownership bit last); DMB OSHST; doorbell STR. Or STLR to the doorbellOSH: Device and Normal Non-cacheable memory are always Outer Shareable, and the master may be outside the Inner Shareable domain7.9, 7.10 §1
2.2To-device buffer, non-coherent masterDC CVAC over the buffer (step CTR_EL0.DminLine); DSB SY (or DSB OSH if the master is known to be in the Outer Shareable domain); doorbellPoC, because the master reads DRAM. A DMB would not do: the clean must have completed8.7
2.3From-device buffer, non-coherent masterBefore: DC CIVAC (or DC CVAC) over the buffer; DSB SY; start DMA. After completion: DC IVAC over the buffer; DSB SY; readPoC both times. The post-DMA invalidate is mandatory, because the PE may allocate lines speculatively during the transfer8.7
2.4Buffer edges not aligned to CTR_EL0.CWGAlign and pad DMA buffers to CWG; otherwise use DC CIVAC on the partial edge lines, and never let both sides write an edge line at onceDC IVAC on a shared line destroys the neighbour’s dirty data8.7
2.5Read completions after a status or index readDevice LDR of index; DMB OSHLD; entry loads. Or LDAR for the index readLD orders the earlier load against later loads and stores7.10 §2
2.6Give ring slots back after reading entriesEntry loads; DMB OSHLD; consumer-index STRload→store order is all that is needed7.10 §2
2.7Two writes to one peripheralProgram order, no barrier (Device-nR arrival order)The extent of “one peripheral” is IMPLEMENTATION DEFINED7.9
2.8Configure peripheral A, then act on peripheral BWrite A; read back from A or poll A’s own completion status; DSB SY; write BnR does not span peripherals; a DSB on an E mapping only says that a buffer accepted the write7.9
2.9Clear an interrupt source, then EOISTR to the device’s clear register; LDR read-back from the same device; DSB SY; MSR ICC_EOIR1_EL1DSB before a system-register write; the read-back proves arrival even on nGnRE7.9
2.10SMMU tables and queues when SMMU_IDR0.COHACC == 0 (or Non-cacheable attributes in SMMU_CR1)Write STE/CD/page tables/commands; DC CVAC to the PoC; DSB SY; CMD_CFGI_* or SMMU_CMDQ_PROD updatePoC: the SMMU does not snoop8.7, 11.5
2.11Persistent memory recordRecord stores; DC CVAP (or DC CVADP); DSB; then publish “durable”PoP / PoDP; on a system without them the operations fall back to PoC / PoP8.5

3. Instruction-side maintenance

#CaseSequencePoint / domain and whySee
3.1New code, executed by this PESTR; DC CVAU; DSB ISH; IC IVAU; DSB ISH; ISBPoU, where instruction fetch meets the data side. The first DSB orders the invalidate after the clean8.6
3.2Same, rangeLoop DC CVAU by CTR_EL0.DminLine; DSB ISH; loop IC IVAU by CTR_EL0.IminLine; DSB ISH; ISBRead line sizes from CTR_EL0; use the system-wide minimum on heterogeneous systems8.6
3.3CTR_EL0.IDC == 1Omit DC CVAU; keep DSB ISH before IC IVAUThe store must still be visible before the invalidate8.6
3.4CTR_EL0.IDC == 1 and DIC == 1STR; DSB ISH; ISBDSB and ISB are never optional. Check the core’s errata before trusting DIC8.6
3.5New code, executed by other PEsModifier: 3.1 without the ISB, then publish a flag. Each executor: observe the flag; ISB (or take an exception, or IPI); branchThe IC IVAU broadcast covers the Inner Shareable domain; the ISB is per PE8.6
3.6Patch code that may be executing (CMODX)Only between instructions in the CMODX set (B, BL, NOP, BRK, SVC, HVC, SMC, ISB), with one aligned 32-bit store; then 3.5 before relying on the new instructionAnything else is CONSTRAINED UNPREDICTABLE8.6
3.7Loader: image written by this PE with caches on, next stage runs with caches onClean the image to PoU (DC CVAU); DSB ISH; IC IALLU (or IC IVAU over the range); DSB ISH; ISB or ERET into itPoU8.6
3.8Loader: next stage starts with caches off, or on another masterDC CVAC (or DC CIVAC) over the image to the PoC; DSB SY; IC IALLU; DSB; ISB/ERETThe next reader bypasses the caches8.6, 6.6
3.9Image written by DMA or by a debugger into memory this PE has cachedDC IVAC over the image to the PoC (drop stale data lines); DSB; IC IALLU; DSB; ISBThe writer bypassed the PE’s D-cache, but stale lines may still be there on both sides6.6, 8.6

4. System registers and context

#CaseSequenceSee
4.1System-register write that changes how later instructions execute (SCTLR_ELx, TCR_ELx, TTBRn_ELx, MAIR_ELx, CPACR_EL1/CPTR_ELx, ZCR_ELx, SMCR_ELx, VBAR_ELx, MDSCR_EL1, PMCR_EL0)MSR; ISB before the first instruction that depends on it7.8
4.2Registers that configure the lower EL, at the end of an EL3 or EL2 exit pathMSR…; ERET (the exception return is the CSE, unless SCTLR_ELx.EOS == 0 under FEAT_ExS)7.8
4.3ICC_SRE_ELxTop down, EL3 then EL2 then EL1; MSR; ISB after each; read back SRE4.5
4.4Counter read after an earlier load or branchISB; MRS CNTVCT_EL0 (or a self-synchronized CNTVCTSS_EL0 with FEAT_ECV)4.7
4.5Counter read after an earlier store has completedDSB SY; ISB; MRS4.7
4.6Counter read before a later loadMRS; artificial address dependency into the load (EOR, ADD sp, dummy LDR), or ISB4.7
4.7PSTATE fields written with MSR (immediate) (PAN, UAO, SSBS, DIT, TCO)No ISB needed for later instructions; check the register’s own text7.8
4.8Unmask interrupts and bound when a pending one is takenMSR DAIFClr, #...; ISB7.8

5. Translation tables and TLBs

#CaseSequenceDomain and whySee
5.1Invalid → validSTR descriptor; DSB ISHST; ISBNo TLBI: faulting translations are never cached. With FEAT_ETS2, the ISB is not needed for later data accesses; keep it for code9.1
5.2Valid → invalid, or a permission-only changeSTR; DSB ISHST; TLBI VAE1IS, x0 (or VALE1IS for a leaf); DSB ISH; ISBISH for the broadcast and its completion; OS forms with DSB OSH when the sharing PEs are only Outer Shareable9.1
5.3Change of OA, memory type, shareability or block size on a live entryBreak-before-make: STR xzr; DSB ISHST; non-leaf TLBI over the whole old range; DSB ISH; STR new; DSB ISHST; ISBAny other order allows two valid translations at once9.2
5.4Block → table split with ID_AA64MMFR2_EL1.BBM ≥ 2, equivalent mappingBuild the table; DSB ISHST; write the table descriptor over the block; DSB ISHST; TLBI the block range; DSB ISH; ISBThe TLBI is still required9.2
5.5Free a translation-table pageOnly after the non-leaf TLBI that removes walk-cache references to it, and its DSB, have completedOtherwise a hardware use-after-free9.2
5.6Stage 2 change (at EL2, current VMID loaded)STR; DSB ISHST; TLBI IPAS2E1IS, x0; DSB ISH; TLBI VMALLE1IS; DSB ISH; ISBThe middle DSB stops a fresh combined entry being built from the old stage 2 entry9.1, 11.3
5.7Translation walks that are Non-cacheable (TCR_ELx.IRGNn/ORGNn)Add DC CVAC on the descriptor before the DSB of 5.1–5.3PoC: the walker reads memory9.1
5.8Local-only flush (this PE, e.g. before enabling the MMU)TLBI ALLE3 (or ALLE2, VMALLE1); DSB NSH or DSB ISH; ISBA local TLBI completes with any scope9.3
5.9TLB conflict abort handler (DFSC 0b110000)Local TLBI VMALLE1 (or by the faulting VA); DSB NSH; ISB; ERETRemoves the duplicate entries on this PE only9.2
5.10Shootdown that must not wait for slow (XS=1) accessesTLBI ...nXS; DSB ...nXSA correctness claim: nothing afterwards may depend on XS=1 accesses through the old mapping having completed9.1
5.11Change of a granule’s physical address space (RME, at EL3)DC CIPAPA per line; DSB OSH; GPT write; DSB OSHST; TLBI RPALOS; DSB OSH; DC CIPAPA (NS alias); DSB OSH. The full delegate and undelegate orders are in 12.4PoPA; OSH because PEs and SMMUs cache GPT information12.4

6. MMU, caches and power transitions

#CaseSequenceSee
6.1MMU on at EL3TLBI ALLE3; MSR MAIR_EL3, TCR_EL3, TTBR0_EL3 (any order); DSB ISH; ISB; set SCTLR_EL3.{M,C} (I usually earlier); ISB. The enabling code, stack and vectors must be identity-mapped9.3
6.2Tables or data written with the MMU or D-cache off, read later with caches onDC IVAC the region to the PoC before writing it (no stale dirty line can be evicted over it) and again after (drop lines allocated in between). Not a clean9.3, 6.6
6.3MMU and caches off before handing to a caches-off stageClear SCTLR_ELx.{M,C} together; ISB; DC CIVAC to the PoC over the data the next stage reads; DSB SY. Code must be identity-mapped9.3
6.4Core power-down, pre-DynamIQ (Cortex-A53/A57/A72 class)Clear SCTLR_EL3.C; ISB; set/way clean+invalidate L1 (DC CISW, levels from CLIDR_EL1); last core: also L2; clear CPUECTLR_EL1.SMPEN; ISB; DSB SY; program power controller; WFI8.8
6.5Core power-down, DynamIQSet CPUPWRCTLR_EL1.CORE_PWRDN_EN; ISB; program power controller; DSB SY; WFI. Hardware flushes and leaves coherency8.8
6.6Data the warm-boot path reads before caches are onDC CIVAC (TF-A flush_dcache_range) to the PoC after every update; DSB8.8, 17.4
6.7IdleDSB SY; WFI7.6, 7.10 §3
6.8System reset through an SoC registerClean to the PoC anything that must survive; DSB SY; reset STR; DSB SY; WFI loop7.10 §3
6.9Warm reset through RMR_EL3.RRDSB SY; ISB; MSR RMR_EL3; ISB; WFI loop (IMPLEMENTATION DEFINED whether RR acts)17.1
6.10Release a secondary from a caches-off hold penWrite the per-core mailbox; DC CVAC it to the PoC (or map it Non-cacheable); DSB SY; SEV17.4

Bring-up note: Set/way operations (DC ISW, CSW, CISW) appear in this appendix only in 6.4. They are local and not atomic with respect to allocation, so they are never a way to make an address visible to anyone (8.5).

7. Linux barrier API on arm64

Linux is a reference mapping here. The lowering is from arch/arm64/include/asm/barrier.h, io.h, tlbflush.h and cacheflush.h in recent kernels. Check your tree, because these definitions have changed over time (for example dma_mb() is a relatively recent addition).

Linux APIarm64 loweringMeaningBare-metal / TF-A equivalent
mb()DSB SYFull completion barrier, including for MMIO and DMAdsb sy (dsb() / dsbsy())
rmb()DSB LDEarlier loads completedsb ld
wmb()DSB STEarlier stores completedsb st
dma_mb()DMB OSHFull ordering for coherent DMA memorydmb osh
dma_rmb()DMB OSHLDOrder reads of a coherent DMA buffer (status, then data)dmb oshld
dma_wmb()DMB OSHSTOrder writes to a coherent DMA buffer (data, then ownership flag)dmb oshst (dmboshst())
smp_mb()DMB ISHFull ordering between CPUsdmb ish (dmbish())
smp_rmb()DMB ISHLDLoad→load and load→store between CPUsdmb ishld
smp_wmb()DMB ISHSTStore→store between CPUsdmb ishst
smp_load_acquire()LDARAcquire (RCsc)ldar
smp_store_release()STLRReleasestlr
smp_cond_load_acquire()Loop: smp_load_acquire() (LDAR) re-read; while the condition fails, __cmpwait (SEVL; WFE; LDXR; compare; WFE)Wait for a value with acquire1.6 above
readl()LDR; DMB OSHLD; dummy control dependency (EOR; CBNZ) on the loaded valueMMIO read ordered before later loads, and before a following udelay()mmio_read_32() + dmb oshld
writel()DMB OSHST; STREarlier memory writes (DMA buffers) ordered before the MMIO writedmb oshst + mmio_write_32()
readl_relaxed() / writel_relaxed()LDR / STR onlyDevice-nR order to the same peripheral, nothing elseTF-A mmio_read_32() / mmio_write_32()
ioremap() / ioremap_np()Device-nGnRE / Device-nGnRnEMapping type for MMIO / non-posted MMIOTF-A MT_DEVICE (nGnRE)
io_stop_wc()DGH (FEAT_DGH; a NOP where not implemented)Stop write-combining of earlier Normal-NC stores with later onesdgh
__arch_counter_get_cntvct()ISB; MRS CNTVCT_EL0 (or MRS CNTVCTSS_EL0 with FEAT_ECV); dummy address-dependent loadCounter read ordered after earlier and before later instructions4.4 and 4.6 above
flush_icache_range()DC CVAU loop (skipped if IDC); DSB ISH; IC IVAU loop (skipped if DIC); DSB ISH; ISB; then an IPI to all CPUs (kick_all_cpus_sync())3.2 + 3.53.2 above
dcache_clean_poc()DC CVAC loop; DSB SYClean to the PoCTF-A clean_dcache_range()
dcache_inval_poc()DC CIVAC on partial edge lines, DC IVAC on whole lines; DSB SYInvalidate to the PoCTF-A inv_dcache_range() (no edge handling: align your buffers)
dcache_clean_inval_poc()DC CIVAC loop; DSB SYClean and invalidate to the PoCTF-A flush_dcache_range()
arch_sync_dma_for_device()dcache_clean_poc()Before a non-coherent master reads or writes2.2, first half of 2.3
arch_sync_dma_for_cpu()dcache_inval_poc() for DMA_FROM_DEVICE and DMA_BIDIRECTIONALAfter a non-coherent master wrotesecond half of 2.3
flush_tlb_all()DSB ISHST; TLBI VMALLE1IS; DSB ISH; ISBAll EL1&0 entries, all CPUs5.2
local_flush_tlb_all()DSB NSHST; TLBI VMALLE1; DSB NSH; ISBThis CPU only5.8
flush_tlb_mm()DSB ISHST; TLBI ASIDE1IS; DSB ISHOne ASID, all CPUs5.2
flush_tlb_page()DSB ISHST; TLBI VALE1IS; DSB ISHOne leaf entry, all CPUs5.2

Three notes on the table:

  • mb(), rmb() and wmb() are DSBs, not DMBs, because Linux defines them to order against DMA and MMIO in every case, including non-coherent ones. In firmware, the DMB forms are usually what you want, with an explicit DSB where a recipe above asks for completion.
  • The Linux smp_* barriers carry no DSB, so they never complete cache or TLB maintenance. The TLB flush helpers carry their own DSBs.
  • C11 stdatomic.h lowers everything to the ISH forms (7.7). It is not the right tool for DMA, MMIO or non-coherent agents.