I'm porting a large code base to a Linux kernel device driver. The ASIC uses a huge number of DMA channels.
I kmalloc memory with
GFP_KERNEL|GFP_DMA. Before starting the DMA, I use dma_map_single to get the hardware (physical) memory address to feed to the hardware. (Also does a flush/invalidate the memory from dcache?) I sometimes need CPU access to the data once DMA is complete, but not often. Before I access the data via code, I do a dma_unmap_single to avoid cache coherency issues.
In the cases where I do not ever need CPU access, do I still need to call
dma_unmap_single? Should I
dma_unmap_single every pointer I
dma_map_single consume a resource (e.g., table entry) that
dma_unmap_single will release?
The DMA-API.txt isn't clear on good DMA memory hygiene.
dma_map_single you map the memory for DMA transfer. You get the physical pointer to the memory, so the device can DMA to that address.
dma_unmap_single you unmap the memory mapped above. You should do this when your transfers are over.
You can <em>map</em> a memory region, and use it for multiple DMA transfers and then you <em>unmap</em> when the job is done. Each time you want to access the DMA memory you must synchronize it. If the device is going to access the memory, you should do
dma_sync_single_for_device; it the host is going to access the memory, you should do
the dma_map api may allocate bounce buffers, in this case, you will need to call the dma_unmap function.