[DMA] How to ensure the consistency of DMA and cache

On the one hand , when the CPU wants to read data from the cache, it will first check whether the cache hits. If it hits, it will return directly, and the memory will no longer be accessed. On the other hand , DMA is writing data to the memory. As a result, the content transferred by DMA is inconsistent with the content cached in the cache.

 

DMA writes data to memory

When the CPU receives a write request from DMA, it means that new data is coming. In order to ensure consistency, the CPU must first clear the cache.

  •  First perform a clean operation on the cache (check the dirty bit, if it is true, write it back to the next level storage)
  • Then perform the invalid operation to invalidate the data in the cache. The next time you access the cache, you will directly access the memory.

When there is new data in the memory, because of the buffering strategy, on the one hand, the data will be returned to the CPU, and on the other hand, a copy of the new data will be saved in the cache.

 

DMA reads data from memory

When the CPU receives a read request from the DMA, it means that the DMA is about to write data to the peripheral. At this time, the CPU needs to flush the cache and flush the data into the memory. At this time, what the DMA reads is the latest data. .

 

Reference article:

How to maintain DMA cache consistency (under modification)_Xingkongyu's Blog-CSDN Blog

The functions of invalid, clean, and flush in memory CACHE_cache flush_Qiu Hui's blog-CSDN blog

Guess you like

Origin blog.csdn.net/challenglistic/article/details/132168679