Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between dma_alloc_coherent and kalloc+dma_map_single

In a system with IOMMU enabled, what is the difference between -dma_alloc_coherent or kalloc followed by dma_map_single.

I am confused in this

like image 766
user970251 Avatar asked Sep 01 '26 17:09

user970251


1 Answers

dma_alloc_coherent() works pretty well if your kernel program just need to allocate a DMA buffer and keep using it until the program exits. It ensures coherency by flushing cache before CPU (your program) or DMA controller read from the DMA buffer so you don't have to pay much attention to 'sync' CPU/cache/DMA controller before read/write to the DMA buffer. The only thing annoys me is that you need to keep track of the physical address dma_addr_t, buffer address and allocated size so you can dma_free_coherent() it properly later.

dma_map_single()/dma_unmap_single() are stream APIs and are supposed to have better performance than dma_alloc_coherent() in case the DMA buffer is single use for each call to DMA controller. Although I haven't seen any significant performance difference between them. And you are supposed to use dma_sync_single_for_device()/dma_sync_single_for_cpu() to assure coherency by yourself.

In some cases dma_map_single() is preferred over dma_alloc_coherent(). In one of my projects the DMA function has to take an allocated buffer (allocated by kzalloc()/kmalloc()) as parameter from the caller and map it to a DMA region. In this case I didn't have control over how and when the buffer would be allocated/free but can only map it to DMA region using dma_map_single()/dma_unmap_single().

like image 138
luke Avatar answered Sep 03 '26 10:09

luke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!