Dma_coherent_alloc fail to allocate memory in cma

Description:

I am writing a kernel driver that requires contiguous memory allocated from the CMA (Contiguous Memory Allocator) region. I have configured the CMA region in the device tree and set the DMA mask correctly in my driver using dma_set_mask_and_coherent(), but I am observing unexpected allocation behavior between two DMA allocation APIs.

Observed behavior:

  • dma_alloc_coherent() — allocates memory from normal RAM, completely ignoring the CMA region, even though the DMA mask is set , cma memory defined in kernel arg cma=512m

  • dma_alloc_noncoherent() — correctly allocates memory from the CMA region as expected.

Questions:

  1. Why does dma_alloc_coherent bypass the CMA region on aarch64/Tegra even when the DMA mask and device tree memory region are configured correctly? Is this a known limitation of the Tegra DMA ops implementation?

  2. Is there a way to force dma_alloc_coherent to allocate from CMA on the Jetson platform, or is dma_alloc_noncoherent with manual cache synchronization the only supported path for CMA-backed DMA buffers?

  3. Are there any Tegra-specific kernel configurations or device tree properties required to make dma_alloc_coherent use the CMA pool?

My current setup:

c

/* DMA mask setup in probe */
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));

/* This does NOT allocate from CMA */
vaddr = dma_alloc_coherent(dev, size, &dma_handle, GFP_KERNEL);

/* This DOES allocate from CMA */
vaddr = dma_alloc_noncoherent(dev, size, &dma_handle,
                               DMA_BIDIRECTIONAL, GFP_KERNEL);

How do you confirm the dma_alloc_coherent() result?

Do you confirm by dma_alloc_from_contiguous()?

test->orig_addr = dma_alloc_coherent(dev, test->buffer_size,
&test->orig_phys_addr,
GFP_KERNEL);
if (!test->orig_addr) {
dev_err(dev, “Failed to allocate DMA buffer (%zu bytes)\n”, test->buffer_size);
return false;
}

vi@tegra-ubuntu:~$ cat /proc/meminfo | grep Cma
CmaTotal: 524288 kB
CmaFree: 446960 kB

if we use non coherent the cma memory will do down

vi@tegra-ubuntu:~/$ cat /proc/meminfo | grep Cma
CmaTotal: 524288 kB
CmaFree: 36272 kB

The vi5_fops.c in kernel driver using the dma_alloc_coherent() without problem.

Maybe have a reference to this driver.