DMA with Cache is Fraught with Peril

Showing results for 
Search instead for 
Did you mean: 

DMA with Cache is Fraught with Peril

Contributor V

I've been writing a number of device drivers that use DMA, and now I am trying to get them working properly with offchip (FlexBus) attached memory.  It looks like in these processors the DMA is completely agnostic to the data cache.  My question is what is the most efficient way to maintain cache coherency in general applications?  

It looks like the steps I should take are the following:

For DMA write to memory:

1.  Use "write-through" cache mode (instead of "write-back") which will immediately execute any write commits to memory and make sure there are no pending writes that may happen after DMA to a memory block is started.

2.  Do DMA write

3.  After the DMA operation is complete "invalidate" all cache lines that are in the memory region over which DMA occurred.

For DMA read from memory:

1.  Use "write-through" cache mode.

2.  Flush all cache lines that are in the memory region over which DMA will occur.

3.  Do DMA read.

It seems like "write-back" cache mode is more or less incompatible with DMA, as this would require a cache flush before DMA and a cache invalidate after DMA, with the need to do this in an atomic fashion (disabling interrupts). 

Is there a better way?  It seems that with all the invalidation and flushing of the caches that you might be better off only DMA'ing to non-cacheable memory then using the CPU to copy to the cacheable region.  All this manipulation of the caches sort of defeats the purpose of DMA however.  Was it an issue to have DMA work with the cache in this processor?  Any good application note that explains all of this?



Tags (3)
0 Kudos
1 Reply

NXP TechSupport
NXP TechSupport

For cache write mechanism is below:

Write-through: Write is done synchronously both to the cache and to the backing store.

Write-back (or Write-behind): Writing is done only to the cache. A modified cache block is written back to the store, just before it is replaced.

For the DAM transfer is from source address to destination address, DMA works way is match with "write-through" way.

Or customer can consider disable cache function of DMA transfer address range.

Wish it help.

best regards,
Ma Hui

Note: If this post answers your question, please click the Correct Answer button. Thank you!

0 Kudos