我想为我的 char 驱动程序在 DMAble 内存上实现 mmap 方法。起初我想用 DMA API 来实现它,然后是 PCI 设备。
dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)
dma_alloc_coherent 的第一个参数是指向结构设备的指针。对于 ISA 或 EISA,它可以为 NULL,但我需要支持 DMA 的有效设备。以下代码应该可以正常工作,但分配失败。
/* register device in sysfs */
mmrmp->udev = device_create(mmrmp->class, NULL, mmrmp->major, NULL, DEVICE_NAME);
if (mmrmp->udev == NULL)
{
goto devcreate_failed;
}
printk(KERN_INFO "%s: Char driver initialized and added to the system at addr %p", __func__, mmrmp->udev);
if(dma_supported(mmrmp->udev,DMA_BIT_MASK(32)))
printk(KERN_INFO "device supports DMA");
else
printk(KERN_INFO "device does not support DMA");
if(is_device_dma_capable(mmrmp->udev))
printk(KERN_INFO "is_device_dma_capable: device supports DMA");
else
printk(KERN_INFO "is_device_dma_capable: device does not support DMA");
if (!dma_set_mask(mmrmp->udev, dma_get_required_mask(mmrmp->udev)))
printk(KERN_INFO "set DMA mask failed");
if ((mmrmp->dma_handle & dma_get_required_mask(mmrmp->udev)) == mmrmp->dma_handle)
printk(KERN_INFO "Allocated physical address is within DMA region");
mmrmp->data = dma_alloc_coherent(mmrmp->udev, PAGE_SIZE, &mmrmp->dma_handle, GFP_KERNEL);
为什么 dma_supported 返回 1 而 is_dma_capable_returns 0?我应该如何创建我的设备来支持 dma_alloc_coherent?