Why dma_alloc_coherent don't return direct mapped vaddr?

Arnd Bergmann arnd at arndb.de
Thu Jul 21 23:50:17 PDT 2022


On Fri, Jul 22, 2022 at 4:57 AM Li Chen <me at linux.beauty> wrote:
>  ---- On Thu, 21 Jul 2022 15:06:57 +0800  Arnd Bergmann <arnd at arndb.de> wrote ---
>  > in between.
>
> Thanks for your answer! My device is a misc character device, just like
> https://lwn.net/ml/linux-kernel/20220711122459.13773-5-me@linux.beauty/
> IIUC, its dma_addr is always the same with phy addr. If I want to alloc from
> reserved memory and then mmap to userspace with vm_insert_pages, are
> cma_alloc/dma_alloc_contigous/dma_alloc_from_contigous better choices?

In the driver, you should only ever use dma_alloc_coherent() for getting
a coherent DMA buffer, the other functions are just the implementation
details behind that.

To map this buffer to user space, your mmap() function should call
dma_mmap_coherent(), which in turn does the correct translation
from device specific dma_addr_t values into pages and uses the
correct caching attributes.

       Arnd



More information about the linux-arm-kernel mailing list