</>EmbeddedInterview

What are the cache coherency problems with DMA?

HardEmbedded C

Short answer: On CPUs with a data cache (Cortex-M7, Cortex-A, some Cortex-M55/M85 systems), the CPU reads and writes through its cache, but most DMA controllers on MCUs access main memory directly. The two can see different data: DMA may transmit stale data still waiting in the cache, and the CPU may read stale cached data after the DMA has written new data to memory. You must clean the cache before DMA reads memory, and invalidate it after DMA writes memory (or place DMA buffers in non-cacheable memory).

The two cases

Direction Problem Fix
CPU writes buffer → DMA sends it (TX) new data is in the (write-back) cache, not yet in RAM clean (write back) the buffer's lines before starting DMA
DMA writes buffer → CPU reads it (RX) the CPU's cache holds old data for those addresses invalidate the lines after DMA completes (and also before starting, if the CPU might write-back dirty lines over the DMA data)
/* Cortex-M7 with CMSIS */
SCB_CleanDCache_by_Addr((uint32_t *)tx_buf, sizeof tx_buf);
start_dma_tx(tx_buf, sizeof tx_buf);

/* ... RX complete interrupt ... */
SCB_InvalidateDCache_by_Addr((uint32_t *)rx_buf, sizeof rx_buf);
process(rx_buf);

Alignment rule: cache operations work on whole lines (32 bytes on Cortex-M7, 64 bytes on most Cortex-A). If a DMA buffer shares a cache line with another variable, invalidating the buffer also discards recent writes to that variable, and cleaning it writes stale data over DMA results. Align DMA buffers to the cache line size and round their size up to a multiple of it:

__attribute__((aligned(32))) static uint8_t rx_buf[ALIGN_UP(RX_LEN, 32)];

Alternatives

  • Non-cacheable region: use the MPU to make a RAM area non-cacheable (or write-through for TX-only buffers) and put DMA buffers there.
  • TCM memory (DTCM on Cortex-M7) is not cached; check that the DMA controller can access it (on STM32H7, some DMAs cannot reach DTCM).
  • Hardware coherency: Cortex-A systems with an ACP or CCI/CMN interconnect can make some DMA masters cache-coherent.

Linux: drivers never do this manually; the DMA API does it: dma_map_single()/dma_unmap_single() (streaming, does the clean/invalidate), and dma_alloc_coherent() (memory that is uncached or hardware-coherent).

Symptom: "DMA works with the cache disabled but returns old/corrupted data with the cache enabled" is almost always this problem.