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

8.5 Data cache maintenance

Data cache maintenance instructions are SYS instructions, written DC <op>, Xt. They fall into three groups: by VA (the everyday ones, broadcast, defined against a point from 8.4), by set/way (local, for power management and initialisation only), and DC ZVA and its MTE relatives, which write memory rather than maintain caches. The instruction-side and DMA sequences built from these are in 8.6 and 8.7. By-PA operations to PoPA/PoE are covered in 8.4 and 12.4.

The by-VA operations

InstructionOperationPointEL0 (when enabled)FeatureEL2 trap
DC IVACInvalidatePoCneverv8.0HCR_EL2.TPCP
DC CVACCleanPoCSCTLR_EL1.UCIv8.0HCR_EL2.TPCP
DC CIVACClean and invalidatePoCSCTLR_EL1.UCIv8.0HCR_EL2.TPCP
DC CVAUCleanPoUSCTLR_EL1.UCIv8.0HCR_EL2.TPU
DC CVAPCleanPoPSCTLR_EL1.UCIFEAT_DPB (mandatory v8.2)HCR_EL2.TPCP
DC CVADPCleanPoDPSCTLR_EL1.UCIFEAT_DPB2 (mandatory v8.5)HCR_EL2.TPCP
DC ZVAZero a block(a store)SCTLR_EL1.DZEv8.0HCR_EL2.TDZ

(HCR_EL2.TPCP was called TPC before FEAT_DPB extended it to cover the PoP. FEAT_FGT adds per-instruction traps in HFGITR_EL2.)

Semantics common to all by-VA operations:

  • Whole line. The operation acts on the line containing the address. There is no alignment requirement, and loops step by CTR_EL0.DminLine (8.1).
  • Broadcast. The operation affects every cache, up to the named point, that can hold the location for the shareability domain of the VA’s mapping. For normal Inner Shareable memory that means other PEs’ caches too. No IPI is needed.
  • Translated and checked. The VA is translated by the current regime and can take a translation or access-flag fault, reported as a Data Abort with ESR_ELx.ISS.CM = 1 (5.8). DC IVAC is permission-checked as a write, because it can destroy data. The clean operations are generally permitted on read-only mappings. See the per-instruction pages in DDI0487 for the exact permission and EL0 rules.
  • Attribute-independent. The operation acts on cached copies of the PA regardless of the cacheability of the mapping used to name it. This is what lets software clean the cacheable alias of a buffer through a Non-cacheable mapping (6.6). With the MMU off, the VA is the PA and the operations still work.
  • Ordering and completion. A by-VA DC operation is ordered after earlier loads and stores by the same PE to the same cache line without a barrier, when those accesses are to Normal Write-Back memory. For accesses through Device or Non-cacheable mappings, a DMB is needed between them. Linux’s dcache_clean_poc loop starts with no barrier for this reason. Relative to accesses to other addresses, a DMB orders it. It is only guaranteed to have taken effect for other observers once a later DSB has completed, so every sequence that hands data to an observer ends with a DSB of the right domain (7.6).
// Clean [x0, x1) to PoC. Clobbers x2, x3. (Same shape as TF-A's flush_dcache_range.)
clean_dcache_range_poc:
    mrs   x3, ctr_el0
    ubfx  x3, x3, #16, #4
    mov   x2, #4
    lsl   x2, x2, x3              // line size
    sub   x3, x2, #1
    bic   x0, x0, x3              // align down
1:  dc    cvac, x0
    add   x0, x0, x2
    cmp   x0, x1
    b.lo  1b
    dsb   sy                      // completion; use the narrowest domain covering the observer
    ret

DC IVAC is the dangerous one

Invalidate discards a dirty line. Any data in the same line that the CPU wrote and has not yet cleaned is lost, including data outside the buffer you meant to invalidate. Three consequences follow:

  1. EL0 can never use it. Otherwise it would be a way to roll memory back to an older value that EL0 could not otherwise write.
  2. With EL2 enabled and HCR_EL2.VM == 1, DC IVAC executed at EL1 is performed as DC CIVAC. A guest cannot use it to discard data it does not own at the physical level. Some implementations always perform an invalidate as clean+invalidate, which is permitted.
  3. It is only safe on whole lines that are owned exclusively by the device side. This is the “invalidate before read” problem worked through in 8.7. Most code should use DC CIVAC.

DC CVAP and DC CVADP

These give persistence. Use them after writing a persistent-memory region and before a DSB that tells software the record is durable. If the system has no PoP, DC CVAP behaves as DC CVAC. If it has no PoDP, DC CVADP behaves as DC CVAP. Software can therefore use the strongest operation it needs wherever the feature is implemented.

DC ZVA and DCZID_EL0

DC ZVA writes zeros to an aligned block of memory. The block size is reported in DCZID_EL0:

DCZID_EL0 fieldBitsMeaning
BS[3:0]log2(words) of the block size: 4 means 64 bytes. The architectural maximum is 2 KB
DZP[4]1 = DC ZVA (and DC GVA/DC GZVA) prohibited at this EL. The value reflects SCTLR_EL1.DZE at EL0 and HCR_EL2.TDZ

Properties:

  • The address is aligned down to the block size, so DC ZVA on an unaligned pointer zeroes bytes before it. memset implementations use it only for the aligned middle of a range.
  • It behaves as a set of stores for permissions, watchpoints, ordering and coherency. It is not a cache maintenance operation and does not need any CMO.
  • It is permitted to avoid reading the line from memory (CHI MakeUnique or a write-zero transaction, 8.3). That is why it is fast.
  • On Device memory it generates an Alignment fault. With the MMU or D-cache off, data accesses are Device-nGnRnE, so an optimised memset that uses DC ZVA faults in early boot code. This is one reason bare-metal firmware must not link a generic libc memset before the MMU is on (6.5).
  • BS can differ between core types. Read DCZID_EL0 per use, or take the system-wide value as Linux does.

The MTE tag variants (brief)

With FEAT_MTE2 the allocation tags have their own storage and may be cached separately (13.4). The DC family therefore gains tag-only (G) and tag-and-data (GD) forms:

Data-onlyTags onlyTags and data
DC ZVADC GVA (set tags of a block)DC GZVA (zero data and set tags)
DC CVACDC CGVACDC CGDVAC
DC CIVACDC CIGVACDC CIGDVAC
DC IVACDC IGVACDC IGDVAC
DC CVAP / DC CVADPDC CGVAP / DC CGVADPDC CGDVAP / DC CGDVADP
DC ISW / CSW / CISWDC IGSW / CGSW / CIGSWDC IGDSW / CGDSW / CIGDSW

The rule for a DMA or power-down sequence on tagged memory is simple: if tags must survive or be seen by another agent, use the GD form wherever you would have used the data form.

Set/way operations, and why they are unsafe for coherency

DC ISW, DC CSW and DC CISW name a cache line by level, set and way rather than by address. The operand is built from CCSIDR_EL1 (8.1): Level in [3:1], the set index shifted left by log2(line length in bytes), and the way index in the top bits of the low 32 bits, shifted left by 32 − ⌈log2(ways)⌉.

// Clean+invalidate one cache level by set/way. x0 = level << 1 (CSSELR format).
    msr   csselr_el1, x0
    isb
    mrs   x1, ccsidr_el1              // assumes no FEAT_CCIDX; check ID_AA64MMFR2_EL1.CCIDX
    and   x2, x1, #7
    add   x2, x2, #4                  // x2 = log2(line bytes) = set shift
    ubfx  x3, x1, #3, #10             // x3 = ways - 1
    clz   w4, w3                      // w4 = way shift = 32 - ceil(log2(ways))
    ubfx  x5, x1, #13, #15            // x5 = sets - 1
1:  mov   x6, x3                      // way loop counter
2:  lsl   w7, w6, w4
    orr   w7, w0, w7                  // level | way
    lsl   w8, w5, w2
    orr   w7, w7, w8                  // | set
    dc    cisw, x7
    subs  x6, x6, #1
    b.ge  2b
    subs  x5, x5, #1
    b.ge  1b
    dsb   sy

(For a direct-mapped cache clz returns 32, which is harmless here because the way index is always 0. TF-A’s dcsw_op_all/dcsw_op_level* are the production versions and iterate the levels from 1 to LoC or LoUIS.)

Set/way operations are architecturally restricted to cache initialisation and power-down of the local hierarchy. Using them for coherency, for example “clean the whole cache before DMA”, is wrong for structural reasons:

  1. Local only. They act on the caches of the executing PE’s own hierarchy and are not broadcast. A dirty copy in another PE’s L1, or in a cluster cache the walk does not cover, is untouched.
  2. Not atomic with respect to allocation. While the loop runs, lines keep arriving through speculation, prefetch and coherent fills from other PEs. With exclusive or NINE caches (8.2), a line can also move from a level not yet processed to one already processed. The loop’s final state guarantees nothing about any given address.
  3. No defined relation to a point. They operate on the levels you choose to walk. A system cache beyond the levels reported by CLIDR_EL1 is not covered, and you only reach a point if your loop bounds match LoC/LoUU/LoUIS.
  4. Invisible to virtualization. A guest’s set/way operations act on whatever physical lines happen to sit at that set and way, which is meaningless to the guest. Hypervisors trap them with HCR_EL2.TSW (and, with HCR_EL2.VM == 1 or HCR_EL2.SWIO == 1, DC ISW at EL1 is performed as DC CISW). KVM emulates them with by-VA cleaning of the guest’s memory.

The power-down use is safe because the preconditions remove problems 1 and 2. The local caches are no longer allocating (the C bit is cleared, or the core has left coherency so that no snoops refill it), and other PEs no longer hold coherent copies that matter. The exact order is in 8.8. DC ISW at reset is only needed on cores that do not invalidate their caches in hardware. Most current cores do, which is IMPLEMENTATION DEFINED (9.3).

Security note: DC IVAC at EL1 on a buffer shared with a more privileged world is a rollback primitive against any write from that world that is still cached. This is why EL0 can never use it, why EL2 upgrades it, and why secure firmware that shares memory with Non-secure software must not rely on the Non-secure side’s maintenance for integrity. RME closes the cross-PAS variant with the PoPA rules (8.4).

Validation note: each by-VA CMO has a PMU fingerprint. Make a line dirty, then execute DC CVAC. L1D_CACHE_WB (and the L2/L3 writeback events if the line was there) should increment by one, and a following load should hit, because the line is clean but still present. After DC CIVAC the following load should refill (L1D_CACHE_REFILL). A CMO that leaves the counters unchanged on a known-dirty line is a finding: the dirty data never left. If the load after a DC CVAC refills, that is not an architectural violation, because a line may be evicted at any time. It does mean the implementation invalidates on clean, which matters for performance and should match the TRM. For DC ZVA, sweep DCZID_EL0.BS alignment edges and check that exactly one block is zeroed, and that Device mappings fault.

Checkpoint

Q8.5.1 Before handing a buffer to a non-coherent DMA engine on a 4-core cluster, firmware runs a DC CISW loop over all levels up to LoC on core 0. Another core wrote half of the buffer a moment earlier. What is the problem?

  • A. Set/way operations are local and not atomic with respect to allocation, so dirty lines in other cores’ caches (and lines refilled during the loop) are not guaranteed to reach the PoC
  • B. None, provided the loop runs to LoC
  • C. DC CISW is UNDEFINED at EL3
  • D. None, provided a DSB SY follows the loop
Answer

A. Set/way acts on the executing PE’s hierarchy only and gives no per-address guarantee. A by-VA DC CVAC loop, which is broadcast and address-precise, followed by a DSB, is the correct sequence. B and D are tempting because the loop does cover every level up to the PoC on this core. The barrier only waits for the operations that were issued, and none of them touched the other cores’ caches.

Read more: Arm ARM (DDI0487), data cache maintenance by set/way; the restrictions on set/way operations

Q8.5.2 Early BL2 code, running with SCTLR_EL3.M = 0, calls an optimised memset and takes an Alignment fault on a DC ZVA with a 64-byte-aligned address. Why?

  • A. DCZID_EL0.DZP is always 1 at EL3
  • B. DC ZVA needs the address aligned to the cache size, not the block size
  • C. With the MMU off, data accesses are treated as Device-nGnRnE, and DC ZVA to Device memory generates an Alignment fault regardless of address alignment
  • D. DC ZVA requires FEAT_MTE
Answer

C. The fault comes from the memory type, not the address. B is the tempting option because the fault is an “alignment” fault, but the address is suitably aligned. The fix is a plain-store memset until the MMU and caches are on, or building with -mstrict-align and not using DC ZVA early.

Read more: Arm ARM (DDI0487), DC ZVA; behaviour of data accesses when stage 1 translation is disabled

Q8.5.3 A guest kernel at EL1 executes DC IVAC on a buffer while HCR_EL2.VM = 1. What does the architecture require?

  • A. The invalidate is performed as written, so dirty data is discarded
  • B. The operation is performed as a clean and invalidate (DC CIVAC), so dirty data is written back, not discarded
  • C. It always traps to EL2
  • D. It is UNDEFINED at EL1 when stage 2 is enabled
Answer

B. With stage 2 enabled, an EL1 invalidate by VA is upgraded to clean and invalidate. Otherwise a guest could discard data at the physical level, including data belonging to lines it shares with other owners. C is tempting, but trapping is separately controlled by HCR_EL2.TPCP and is not implied by VM.

Read more: Arm ARM (DDI0487), DC IVAC; effects of HCR_EL2.VM on data cache invalidate instructions