LINUX CMA 初探

一、什么是CMA

CMA,Contiguous Memory Allocator,是内存管理子系统中的一个模块,负责物理地址连续的内存分配。一般系统会在启动过程中,从整个memory中配置一段连续内存用于CMA,然后内核其他的模块可以通过CMA的接口API进行连续内存的分配。CMA的核心并不是设计精巧的算法来管理地址连续的内存块,实际上它的底层还是依赖内核伙伴系统这样的内存管理机制,或者说CMA是处于需要连续内存块的其他内核模块(例如DMA mapping framework)和内存管理模块之间的一个中间层模块,主要功能包括:

1、解析DTS或者命令行中的参数,确定CMA内存的区域,这样的区域我们定义为CMA area。

2、提供cma_alloc和cma_release两个接口函数用于分配和释放CMA pages

3、记录和跟踪CMA area中各个pages的状态

4、调用伙伴系统接口,进行真正的内存分配

cma

二、使用CMA

示例一,https://stackoverflow.com/questions/35556593/how-to-work-with-reserved-cma-memory

To use dma_alloc_coherent() on reserved memory node, you need to declare that area as dma_coherent. You can do some thing like:

In dt:

cmadev_region: mycma {
    compatible = "compatible-name"
    no-map;
    reg = <0x02000000 0x00100000>;      
};
In your driver:

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);
Now on this cma_dev you can perform dma_alloc_coherent and get memory.

 

示例二,http://www.sohu.com/a/226744867_467784

/*

* kernel module helper for testing CMA

*

* Licensed under GPLv2 or later.

*/

#include <linux/module.h>

#include <linux/device.h>

#include <linux/fs.h>

#include <linux/miscdevice.h>

#include <linux/dma-mapping.h>

#define CMA_NUM 10

static struct device *cma_dev;

static dma_addr_t dma_phys[CMA_NUM];

static void *dma_virt[CMA_NUM];

/* any read request will free coherent memory, eg.

* cat /dev/cma_test

*/

static ssize_t

cma_test_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)

{

		int i;

		for (i = 0; i < CMA_NUM; i++) {

		if (dma_virt[i]) {

		dma_free_coherent(cma_dev, (i + 1) * SZ_1M, dma_virt[i], dma_phys[i]);

		_dev_info(cma_dev, "free virt: %p phys: %pn", dma_virt[i], (void *)dma_phys[i]);

		dma_virt[i] = NULL;

		break;

		}

		}

		return 0;

}

/*

* any write request will alloc coherent memory, eg.

* echo 0 > /dev/cma_test

*/

static ssize_t

cma_test_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)

{

		int i;

		int ret;

		for (i = 0; i < CMA_NUM; i++) {

				if (!dma_virt[i]) {

				dma_virt[i] = dma_alloc_coherent(cma_dev, (i + 1) * SZ_1M, &dma_phys[i], GFP_KERNEL);

				if (dma_virt[i]) {

					void *p;

					/* touch every page in the allocated memory */

					for (p = dma_virt[i]; p < dma_virt[i] + (i + 1) * SZ_1M; p += PAGE_SIZE)

					*(u32 *)p = 0;

					_dev_info(cma_dev, "alloc virt: %p phys: %pn", dma_virt[i], (void *)dma_phys[i]);

					} else {

					dev_err(cma_dev, "no mem in CMA arean");

					ret = -ENOMEM;

					}

				break;

				}

		}

		return count;

}

static const struct file_operations cma_test_fops = {

.owner = THIS_MODULE,

.read = cma_test_read,

.write = cma_test_write,

};

static struct miscdevice cma_test_misc = {

.name = "cma_test",

.fops = &cma_test_fops,

};

static int __init cma_test_init(void)

{

		int ret = 0;

		ret = misc_register(&cma_test_misc);

		if (unlikely(ret)) {

			pr_err("failed to register cma test misc device!n");

			return ret;

		}

		cma_dev = cma_test_misc.this_device;

		cma_dev->coherent_dma_mask = ~0;

		_dev_info(cma_dev, "registered.n");

		return ret;

}

	module_init(cma_test_init);

static void __exit cma_test_exit(void)

{

	misc_deregister(&cma_test_misc);

}

module_exit(cma_test_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");

MODULE_DEION("kernel module to help the test of CMA");

MODULE_ALIAS("CMA test");

申请内存:

# echo 0 > /dev/cma_test

释放内存:

# cat /dev/cma_test

 

三、doc

*** Reserved memory regions ***

Reserved memory is specified as a node under the /reserved-memory node.
The operating system shall exclude reserved memory from normal usage
one can create child nodes describing particular reserved (excluded from
normal use) memory regions. Such memory regions are usually designed for
the special usage by various device drivers.

Parameters for each memory region can be encoded into the device tree
with the following nodes:

/reserved-memory node
---------------------
#address-cells, #size-cells (required) - standard definition
    - Should use the same values as the root node
ranges (required) - standard definition
    - Should be empty

/reserved-memory/ child nodes
-----------------------------
Each child of the reserved-memory node specifies one or more regions of
reserved memory. Each child node may either use a 'reg' property to
specify a specific range of reserved memory, or a 'size' property with
optional constraints to request a dynamically allocated block of memory.

Following the generic-names recommended practice, node names should
reflect the purpose of the node (ie. "framebuffer" or "dma-pool"). Unit
address (@<address>) should be appended to the name if the node is a
static allocation.

Properties:
Requires either a) or b) below.
a) static allocation
   reg (required) - standard definition
b) dynamic allocation
   size (required) - length based on parent's #size-cells
                   - Size in bytes of memory to reserve.
   alignment (optional) - length based on parent's #size-cells
                        - Address boundary for alignment of allocation.
   alloc-ranges (optional) - prop-encoded-array (address, length pairs).
                           - Specifies regions of memory that are
                             acceptable to allocate from.

If both reg and size are present, then the reg property takes precedence
and size is ignored.

Additional properties:
compatible (optional) - standard definition
    - may contain the following strings:
        - shared-dma-pool: This indicates a region of memory meant to be
          used as a shared pool of DMA buffers for a set of devices. It can
          be used by an operating system to instanciate the necessary pool
          management subsystem if necessary.
        - vendor specific string in the form <vendor>,[<device>-]<usage>
no-map (optional) - empty property
    - Indicates the operating system must not create a virtual mapping
      of the region as part of its standard mapping of system memory,
      nor permit speculative access to it under any circumstances other
      than under the control of the device driver using the region.
reusable (optional) - empty property
    - The operating system can use the memory in this region with the
      limitation that the device driver(s) owning the region need to be
      able to reclaim it back. Typically that means that the operating
      system can use that region to store volatile or cached data that
      can be otherwise regenerated or migrated elsewhere.

Linux implementation note:
- If a "linux,cma-default" property is present, then Linux will use the
  region for the default pool of the contiguous memory allocator.

- If a "linux,dma-default" property is present, then Linux will use the
  region for the default pool of the consistent DMA allocator.

Device node references to reserved memory
-----------------------------------------
Regions in the /reserved-memory node may be referenced by other device
nodes by adding a memory-region property to the device node.

memory-region (optional) - phandle, specifier pairs to children of /reserved-memory

Example
-------
This example defines 3 contiguous regions are defined for Linux kernel:
one default of all device drivers (named linux,cma@72000000 and 64MiB in size),
one dedicated to the framebuffer device (named framebuffer@78000000, 8MiB), and
one for multimedia processing (named multimedia-memory@77000000, 64MiB).

/ {
	#address-cells = <1>;
	#size-cells = <1>;

	memory {
		reg = <0x40000000 0x40000000>;
	};

	reserved-memory {
		#address-cells = <1>;
		#size-cells = <1>;
		ranges;

		/* global autoconfigured region for contiguous allocations */
		linux,cma {
			compatible = "shared-dma-pool";
			reusable;
			size = <0x4000000>;
			alignment = <0x2000>;
			linux,cma-default;
		};

		display_reserved: framebuffer@78000000 {
			reg = <0x78000000 0x800000>;
		};

		multimedia_reserved: multimedia@77000000 {
			compatible = "acme,multimedia-memory";
			reg = <0x77000000 0x4000000>;
		};
	};

	/* ... */

	fb0: video@12300000 {
		memory-region = <&display_reserved>;
		/* ... */
	};

	scaler: scaler@12500000 {
		memory-region = <&multimedia_reserved>;
		/* ... */
	};

	codec: codec@12600000 {
		memory-region = <&multimedia_reserved>;
		/* ... */
	};
};

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值