This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Question about dma_alloc_coherent and mmap

Hi,all:

I read the source code with regarding to PCIE transmitting data via DMA in the Lightning_PCIE_0.8 by Advandtech. I noted that in the routine pcie_drv_dma_mem_alloc the application firstly call dma_alloc_coherent to alloc dma physical addr and dma virtual addr in the driver like below:

dma_buffer_info->buf_info[i].virtAddr = (uint8_t *)dma_alloc_coherent(

                &ti667x_pci_dev[dev_id]->dev,

                dma_buffer_info->buffer_size,

                (dma_addr_t *)&dma_buffer_info->buf_info[i].pcieAddr,

                GFP_KERNEL);

Then applicaiont call mmap to remap dma physical addr into dma virtual addr like below:

buf_desc[i].virtAddr = mmap(0,

            size_of_buffer,

            PROT_READ | PROT_WRITE,

            MAP_SHARED,

            pcie_drv_inst[dsp_id].dev_desc,

            buf_desc[i].pcieAddr);

Why ? Why we need call mmap after dma_alloc_coherent ?  As I understand, after I call the dma_alloc_coherent,I have already used dma virtual addr. I can copy to or from that dma virtual address in the user space in my application. When I read and write the dma virtual addr That means I read and write the dma physical address. The content of dma physical address and dma virtual address should be the same.

So, there’s no need to call mmap after dma_alloc_coherent. I think in the dma_alloc_coherent application has remap between dma virtual address and dma physical address.   If call mmap again, Doen’t belong to repeat work ?    How about your opinion ? thanks!

  • dma_alloc_coherent() is a kernel-level function that maps a physical address range to a kernel virtual address range.  It does not set up any application-level mappings


    mmap() is an application-level function that maps an application/user-space virtual address range to some kernel-managed range.  The application needs to call mmap() before it can directly access the memory set up by dma_alloc_coherent().