DMA接口函数
arch/arm/mm/dma-mapping.c 主要的作用是将虚拟地址映射到物理地址。
一致性DMA映射,分配指定大小的内存,并将物理地址映射到虚拟地址
dma_alloc_coherent
dma_mmap_coherent
dma_free_coherent
dma_alloc_writecombine
dma_mmap_writecombine
dma_free_writecombine
dma_alloc_coherent和dma_alloc_writecombine的区别是dma_alloc_coherent(cache disable,buffer disable),dma_alloc_writecombine(cache disable,buffer enable)
流式DMA映射,使用这个函数前需要事先分配连续物理内存,这个函数只是物理地址映射到虚拟地址
dma_map_single
dma_unmap_single
DMA池
DMA cache一致性问题
DMA如果使用cache,那么一定要考虑cache的一致性。解决DMA导致的一致性的方法最简单的就是禁止DMA目标地址范围内的cache功能。但是这样就会牺牲性能。
从dma_alloc_coherent和dma_alloc_writecombie源码可以看出,
Dma_alloc_coherent pgprot_noncached 禁止了页表项中的C(Cacheable)域和B(Bufferable)域
此时由于关闭了cache/buffer,性能自然比较低
dma_alloc_writecombie pgprot_writecombine 只禁止了C (Cacheable) 域
DMA接口函数使用实例
完整代码位置:dma映射函数
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>
#include <linux/device.h>
// int direction = PCI_DMA_TODEVICE ;
// int direction = PCI_DMA_FROMDEVICE ;
static int direction = PCI_DMA_BIDIRECTIONAL;
//int direction = PCI_DMA_NONE;
static char *kbuf;
static dma_addr_t handle;
static size_t size = (10 * PAGE_SIZE);
static struct dma_pool *mypool;
static size_t pool_size = 1024;
static size_t pool_align = 8;
static void my_release(struct device *dev)
{
pr_info("releasing DMA device\n");
}
static struct device dev = {
.release = my_release
};
static void output(char *kbuf, dma_addr_t handle, size_t size, char *string)
{
unsigned long diff;
diff = (unsigned long)kbuf - handle;
pr_info("kbuf=%12p, handle=%12p, size = %d\n", kbuf,
(unsigned long *)handle, (int)size);
pr_info("(kbuf-handle)= %12p, %12lu, PAGE_OFFSET=%12lu, compare=%lu\n",
(void *)diff, diff, PAGE_OFFSET, diff - PAGE_OFFSET);
strcpy(kbuf, string);
pr_info("string written was, %s\n", kbuf);
}
static int __init my_init(void)
{
dev_set_name(&dev, "my0");
device_register(&dev);
/* dma_alloc_coherent method */
pr_info("Loading DMA allocation test module\n");
pr_info("\nTesting dma_alloc_coherent()..........\n\n");
kbuf = dma_alloc_coherent(NULL, size, &handle, GFP_KERNEL);
output(kbuf, handle, size, "This is the dma_alloc_coherent() string");
dma_free_coherent(NULL, size, kbuf, handle);
/* dma_map/unmap_single */
pr_info("\nTesting dma_map_single()................\n\n");
kbuf = kmalloc(size, GFP_KERNEL);
handle = dma_map_single(&dev, kbuf, size, direction);
output(kbuf, handle, size, "This is the dma_map_single() string");
dma_unmap_single(&dev, handle, size, direction);
kfree(kbuf);
/* dma_pool method */
pr_info("\nTesting dma_pool_alloc()..........\n\n");
mypool = dma_pool_create("mypool", NULL, pool_size, pool_align, 0);
kbuf = dma_pool_alloc(mypool, GFP_KERNEL, &handle);
output(kbuf, handle, size, "This is the dma_pool_alloc() string");
dma_pool_free(mypool, kbuf, handle);
dma_pool_destroy(mypool);
device_unregister(&dev);
return 0;
}
static void __exit my_exit(void)
{
pr_info("Module Unloading\n");
}
module_init(my_init);
module_exit(my_exit);
MODULE_AUTHOR("Jerry Cooperstein");
MODULE_DESCRIPTION("LDD:2.0 s_23/lab1_dma.c");
MODULE_LICENSE("GPL v2");