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 .