我想在DMAble memmory上为我的char驱动程序实现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?
答案 0 :(得分:0)
确保mmrmp->udev->dma_mask pointer
不为NULL。按如下方式初始化:
static const u64 dmamask = DMA_BIT_MASK(32);
mmrmp->udev->dma_mask = (u64 *)&dmamask;
mmrmp->udev->coherent_dma_mask = DMA_BIT_MASK(32);