Nvidia jetson orin nx cma memory

hi ,
i am trying to allocate memory in cma and map to dma and fill the data and mmap to user space ,when i run the code the system hangs without any log in nvidia jetson orin nx
my mmap function
static int my_mmap(struct file *filp, struct vm_area_struct *vma) {
unsigned long pfn;
size_t size = vma->vm_end - vma->vm_start;
int buffer_index = (int)filp->private_data; // Get the buffer index from private_data

// Check if requested size exceeds buffer size
if (size > BUFFER_SIZE) {
    return -EINVAL;
}

// Remap the current buffer
pfn = virt_to_phys(buffers[buffer_index]) >> PAGE_SHIFT;

if (remap_pfn_range(vma, vma->vm_start, pfn, size, vma->vm_page_prot)) {
    return -EAGAIN;
}
return 0;

}
// Set up DMA mask (forces coherent memory)
if (dma_set_coherent_mask(dma_dev, DMA_BIT_MASK(32))) {
dev_err(dma_dev, “Failed to set coherent DMA mask\n”);
return -EIO;
}

// Allocate DMA buffers using `dma_alloc_coherent`
for (i = 0; i < NUM_BUFFERS; i++) {
    buffers[i] = dma_alloc_coherent(dma_dev, BUFFER_SIZE, &dma_handles[i], GFP_KERNEL);
    if (!buffers[i]) {
        pr_err("Failed to allocate DMA buffer %d\n", i);

        // Free already allocated buffers before returning
        while (--i >= 0) {
            dma_free_coherent(dma_dev, BUFFER_SIZE, buffers[i], dma_handles[i]);
        }

        device_destroy(my_class, dev_number);
        class_destroy(my_class);
        cdev_del(&my_cdev);
        unregister_chrdev_region(dev_number, 1);
        return -ENOMEM;
    }

    pr_info("DMA buffer %d allocated: VA = %p, PA = %pad\n", i, buffers[i], &dma_handles[i]);
}

with above code i am able to allocate the cma memory and able to get virtual and physical address ,but my mmap function is not woking when i tried to mmap the buffer 0 and try to access the data in userspace system hangs without any log ,can anyone please help me on this .

Hi,
Do you use Orin Nano or Jetson Nano? The topic is in Jetson Nano category but you mention Orin NX in description.

Generally the size of CMA memory cannot be arbitarily changed. Please keep the default setting.

@DaneLLL i am using jetson orin nx in my device tree 256mb is cma memory default setting in that i am allocating 80mb from my driver and successfully getting physical and virtul address
In proc/meminfo cma free size also reduced by 80mb .

I am writing 0xaabb to in virtual address ,after that from userspace i am calling mmap from userspace for particular driver and remap_pnf_range function returns zero ,but when i am trying to read the return memory pointer of mmap system hangs and reboots

Hi,
Please help check whether the device dma_dev has SMMU enabled or not.