cma设置 memblock find_in_range(
时间: 2023-07-22 12:44:31 浏览: 100
CMA (Contiguous Memory Allocator) is a feature in the Linux kernel that allows for the allocation of large, physically contiguous memory regions. To use CMA, you need to configure the kernel with the CONFIG_CMA option and provide a reserved memory region for CMA to use.
Once CMA is configured and initialized, you can allocate memory using the dma_alloc_coherent() function. This function takes a device pointer and a size parameter, and returns a pointer to a physically contiguous memory region that can be used for DMA.
To set up a reserved memory region for CMA to use, you can use the memblock_find_in_range() function. This function searches for a contiguous range of memory within a specified address range that meets certain requirements, such as a minimum size or alignment.
Here is an example of how to use memblock_find_in_range() to set up a reserved memory region for CMA:
```
#include <linux/memblock.h>
/* Set up a reserved memory region for CMA */
void __init setup_cma_reserved_memory(void)
{
phys_addr_t start = 0x80000000; /* start of address range */
phys_addr_t end = 0x90000000; /* end of address range */
size_t size = 0x10000000; /* minimum size of reserved region */
unsigned long align = PAGE_SIZE;/* alignment of reserved region */
phys_addr_t addr = memblock_find_in_range(start, end, size, align);
if (!addr) {
printk(KERN_ERR "Failed to find contiguous memory region for CMA\n");
return;
}
memblock_reserve(addr, size);
}
```
In this example, we search for a contiguous memory region within the address range 0x80000000 to 0x90000000 that is at least 0x10000000 bytes in size and aligned to the page size. If a suitable region is found, we reserve it using the memblock_reserve() function. This reserved memory region can then be used by CMA for DMA allocations.
阅读全文