STM32 gotchas
325. In Cortex-M7, DMA to/from cached memory requires extra care

Cache is a relatively small portion of RAM, fast enough to run at the processor's clock, together with logic detecting need of, and performing, actual read and write from/to the potentially slow memory. It is organized in rows of predetermined width, and reads/writes between cache row and memory are performed with addresses aligned to the row's length.

The basic assumption behind cache's logic is, that only the processor reads and writes data from/to given portion of memory. This is not the case when other bus-masters read/write from/to the memory. This includes DMA controllers, and peripherals which perform DMA on their own, such as ETH or OTG.

The simplest and indeed, very common way to tackle this issue, is to disable caching for a portion of memory used for DMA, in the MPU. This is a viable option especially for smaller DMA buffers, however, when more data are to be moved around and quickly, the performance penalty for not using the cache may be detrimental.

When cache is used, and data for DMA are written by the processor (i.e. for some transmitting (Tx) peripheral), it is mostly enough to perform a cache Clean operation (to the address range of the buffer) after data have been written and before DMA is started. This tells the cache logic to write data from cache to memory. There is no risk here of data corruption even if the buffer is not aligned to cache lines, as the cache surrounding the buffer contains data which from processor's point of view are correct and writing them to memory causes no harm except some minor inefficiency.

Using DMA for Rx (i.e. where peripheral receives (Rx) data and DMA writes them to memory, and then processor is going to read them after DMA is finished) is trickier. At the surface, it may appear, that it's enough to perform the inverse to the above, i.e. perform an Invalidate on the cache. That makes the cache controller deem the data in cache unusable, and an attempt to read from given area by the processor leads to reading data from memory to cache. This is indeed necessary, but then there's more to it, too.

First, the DMA buffer must be aligned by address AND by size to the line width of cache, and the Invalidate operation must operate only on this exact area. Otherwise, neighbouring data, written by processor to cache and potentially not in memory yet, would be forgotten, and old data from memory would be read upon next attempt to read them.

Second, if the data in buffer are to be processed by processor in-situ, i.e. some intermediate results are written by processor to the buffer, even if those data won't be needed after the calculation is finished and next DMA process is started, the cache has that line marked as "dirty" and may decide to write it back (evict) to memory during the DMA process, damaging the already received new data. If this is the case, it is necessary to perform a cache Invalidate operation on the given area also before starting the DMA process. Even if this Invalidation is performed, it is still necessary to perform the Invalidate also after DMA finished, as the cache controller might have decided to perform a speculative read amidst the DMA process, while not all new data have been received yet.

A FAQ for this issue here, and some more extensive discussion here and here.