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

3.6 L1 caches, cache maintenance and DMA coherency

Caches are optional. Armv8.1-M cores such as the Cortex-M55 and Cortex-M85 can have L1 I- and D-caches, and these are not coherent with DMA masters. The Armv7E-M Cortex-M7 uses the same interface. There are no cache maintenance instructions like the A-profile DC/IC. Maintenance is done with stores to memory-mapped SCB registers (Mainline):

  • ICIALLU: invalidate the whole I-cache to the Point of Unification.
  • ICIMVAU / DCCMVAU: invalidate an I-cache line, or clean a D-cache line, by address to the PoU.
  • DCIMVAC: invalidate a D-cache line by address to the Point of Coherency.
  • DCCMVAC: clean a D-cache line by address to the PoC.
  • DCCIMVAC: clean and invalidate a D-cache line by address to the PoC.

Set/way variants (DCISW, DCCSW, DCCISW) are for whole-cache operations at init. Each by-address operation acts on one line, so loop over the buffer using the minimum line size from CTR.DminLine. Finish with a DSB, which guarantees that the maintenance has completed. For I-cache changes, add an ISB.

The DMA rules:

  • Memory to device (CPU wrote the buffer, DMA reads it): DCCMVAC over the buffer, then DSB, then start the DMA. Otherwise dirty data is still in the cache and the DMA reads stale memory.
  • Device to memory (DMA writes, CPU reads): invalidate before you start the DMA, so a dirty line cannot be evicted on top of the DMA data. Invalidate again (DCIMVAC, then DSB) after completion and before reading. Normal cacheable memory may be speculatively read into the cache while the DMA is running, so the first invalidate is not enough.
  • Align buffers to the cache line size and make their size a multiple of it. Otherwise invalidating a shared line throws away unrelated CPU data, or cleaning it writes stale data over DMA data.

The portable alternative is to map DMA buffers through the MPU as Normal Non-cacheable (MAIR 0x44). Some cores, for example the Cortex-M7, do not cache Shareable Normal regions by default, but that is implementation behaviour and not an architectural guarantee.

Self-modifying or loaded code needs: DCCMVAU over the code, DSB, ICIALLU, DSB, ISB.

Coming from A-profile: The concepts (PoC/PoU, clean vs invalidate, speculative line fills) are the same as on A-profile, but the operations are SCB register stores instead of DC CIVAC and similar instructions. These MCUs usually lack the hardware I/O coherency that an A-profile SoC gets from an SMMU and a coherent interconnect (CCI/CMN).

Checkpoint

Q3.6.1 On a Cortex-M55 with the D-cache enabled, a DMA engine writes received data into a cacheable SRAM buffer. The driver invalidated the buffer before starting the DMA and then reads it after the completion interrupt without further maintenance. Occasionally stale data appears. Why?

  • A. The pre-DMA invalidate should have been a clean
  • B. Normal cacheable memory can be speculatively read into the cache during the transfer, so an invalidate after completion is also needed
  • C. The DMA completion interrupt needs an ISB
  • D. DCIMVAC only works on the I-cache
Answer

B. Normal memory allows speculative reads, so lines of the buffer can be allocated again while the DMA runs, and the CPU then reads those stale lines. You need an invalidate after completion, followed by a DSB. Cleaning before the DMA would write possibly stale CPU data over the buffer, so it is the wrong fix for an RX buffer.

Read more: Armv8-M ARM (DDI0553), cache maintenance operations; Cortex-M55 TRM, L1 cache

Q3.6.2 The CPU fills a TX buffer in Write-Back cacheable SRAM and then starts a DMA that reads it. What is the correct sequence?

  • A. DCIMVAC over the buffer, then start the DMA
  • B. DMB, then start the DMA
  • C. DCCMVAC over the buffer, DSB, then start the DMA
  • D. ICIALLU, ISB, then start the DMA
Answer

C. The data may be dirty in the D-cache. Cleaning to the PoC pushes it to memory, and the DSB ensures that the clean has completed before the store that starts the DMA. Invalidating would throw away the dirty data, and a DMB orders accesses but does not make cached data visible to a non-coherent master.

Read more: Armv8-M ARM (DDI0553), SCB cache maintenance registers (DCCMVAC)

Q3.6.3 Why must DMA buffers that are maintained with DCIMVAC start on a cache line boundary and have a size that is a multiple of the cache line size?

  • A. Because DCIMVAC faults on unaligned addresses
  • B. Because the MPU requires 32-byte aligned regions
  • C. Because the DMA engine cannot write across a cache line
  • D. Because invalidating a line that the buffer shares with other variables discards the CPU’s dirty updates to those variables
Answer

D. Maintenance acts on whole lines, so a partial line also holds neighbouring data, and invalidating it silently loses dirty CPU writes to that data. DCIMVAC does not fault on unaligned addresses. It acts on the line that contains the address, which is exactly why the line-sharing problem happens.

Read more: Armv8-M ARM (DDI0553), cache maintenance by address