如何使用保留的CMA内存?

cos*_*us0 6 memory-management virtual-memory linux-device-driver linux-kernel device-tree

我想为具有DMA支持的设备分配一块物理连续的保留内存(在预定义的物理地址中)。如我所见,CMA具有三个选项:1.通过内核配置文件保留内存。2.通过内核cmdline保留内存。3.通过设备树内存节点保留内存。在第一种情况下:可以保留区域的大小和数量。

CONFIG_DMA_CMA=y
CONFIG_CMA_AREAS=7
CONFIG_CMA_SIZE_MBYTES=8
Run Code Online (Sandbox Code Playgroud)

所以我可以使用:

start_cma_virt = dma_alloc_coherent(dev->cmadev, (size_t)size_cma, &start_cma_dma, GFP_KERNEL);
Run Code Online (Sandbox Code Playgroud)

在我的驱动程序中分配连续的内存。我最多可以使用7次,最多可以分配8M。但遗憾的是

dma_contiguous_reserve(min(arm_dma_limit, arm_lowmem_limit));
Run Code Online (Sandbox Code Playgroud)

从arch / arm / mm / init.c:

void __init arm_memblock_init(struct meminfo *mi,const struct machine_desc *mdesc)
Run Code Online (Sandbox Code Playgroud)

不可能为连续分配设置预定义的物理地址。当然我可以使用内核cmdline:

mem=cma=cmadevlabel=8M@32M cma_map=mydevname=cmadevlabel
//struct device *dev = cmadev->dev; /*dev->name is mydevname*/
Run Code Online (Sandbox Code Playgroud)

之后,dma_alloc_coherent()应该在物理内存区域中从32M + 8M(0x2000000 + 0x800000)到0x27FFFFF分配内存。但不幸的是,我对此解决方案有疑问。也许我的cmdline有错误?下一个尝试是设备树实现。

cmadev_region: mycma {
    /*no-map;*/ /*DMA coherent memory*/
    /*reusable;*/
    reg = <0x02000000 0x00100000>;      
};
Run Code Online (Sandbox Code Playgroud)

并在某些节点中:

memory-region = <&cmadev_region>;
Run Code Online (Sandbox Code Playgroud)

正如我通常在内核中看到的那样,应将其使用为:

of_find_node_by_name(); //find needed node
of_parse_phandle(); //resolve a phandle property to a device_node pointer
of_get_address(); //get DT __be32 physical addresses
of_translate_address(); //DT represent local (bus, device) addresses so translate it to CPU physical addresses 
request_mem_region(); //reserve IOMAP memory (cat /proc/iomem)
ioremap(); //alloc entry in page table for reserved memory and return kernel logical addresses.
Run Code Online (Sandbox Code Playgroud)

但是我想通过DMA使用(因为我只知道一个外部API函数dma_alloc_coherent),而是使用dma_alloc_coherent()而不是IO-MAP ioremap()。但是怎么打电话

start_cma_virt = dma_alloc_coherent(dev-> cmadev,(size_t)size_cma,&start_cma_dma,GFP_KERNEL);

将内存从设备树(reg = <0x02000000 0x00100000>;)关联到dev-> cmadev?如果使用cmdline,很明显它具有设备名称和地址区域。在自动调用of_parse_phandle()之后是否应为您的特殊驱动程序(解析DT)预订保留的内存。接下来的调用dma_alloc_coherent将从cmadev_region中分配内存中的dma区域:mycma?

小智 4

要在保留内存节点上使用 dma_alloc_coherent(),您需要将该区域声明为 dma_coherent。你可以做一些事情,比如:

在 dt 中:

cmadev_region: mycma {
    compatible = "compatible-name"
    no-map;
    reg = <0x02000000 0x00100000>;      
};
Run Code Online (Sandbox Code Playgroud)

在你的驱动程序中:

struct device *cma_dev;

static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
{
    int ret;

    if (!mem) {
        ret = dma_declare_coherent_memory(cma_dev, rmem->base, rmem->base,
                           rmem->size, DMA_MEMORY_EXCLUSIVE);
        if (ret) {
            pr_err("Error");
            return ret;
        }
    }
    return 0;
}

static void rmem_dma_device_release(struct reserved_mem *rmem,
                struct device *dev)
{
    if (dev)
        dev->dma_mem = NULL;
}

static const struct reserved_mem_ops rmem_dma_ops = {
    .device_init    = rmem_dma_device_init,
    .device_release = rmem_dma_device_release,
};

int __init cma_setup(struct reserved_mem *rmem)
{
    rmem->ops = &rmem_dma_ops;
    return 0;
}
RESERVEDMEM_OF_DECLARE(some-name, "compatible-name", cma_setup);
Run Code Online (Sandbox Code Playgroud)

现在,在此 cma_dev 上,您可以执行 dma_alloc_coherent 并获取内存。