Does every dma_map_single call require a corresponding dma_unmap_single?

痴心易碎 提交于 2019-12-12 14:06:56

问题


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? Does 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.

Thanks!


回答1:


With 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.

With dma_unmap_single you unmap the memory mapped above. You should do this when your transfers are over.

You can map a memory region, and use it for multiple DMA transfers and then you unmap 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 dma_sync_single_for_cpu




回答2:


the dma_map api may allocate bounce buffers, in this case, you will need to call the dma_unmap function.



来源:https://stackoverflow.com/questions/16172118/does-every-dma-map-single-call-require-a-corresponding-dma-unmap-single

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!