二十二、DMA驱动 32.Linux-2440下的DMA驱动(详解)

一、DMA简介

DMA(Direct Memory Access,直接内存存取),DMA传输将数据从一个地址空间复制到另外一个地址空间。传输过程由DMA控制器独立完成,它并没有拖延CPU的工作,可以让CPU效率提高。

既然DMA用于传输,那么就需要具备传输三要素:源、目的、长度。在传输完成后,DMA会通过产生中断的方式汇报。

由于DMA不使用页表机制,因此必须分配连续的物理内存,这一点需要我们注意,我们可以使用dma_alloc_writecombine()或dma_alloc_coherent()。dma_alloc_coherent()分配的内存不使用缓存,也不会使用写缓冲区,性能较差,dma_alloc_writecombine()不使用缓存,使用写缓冲区。

我们来看4412数据手册Direct Memory Access Controller一章。

可以看到4412上有3个DMA控制器,图中DMA是支持内存到内存的DMA控制器,DMA0和DMA1是同时支持外设到内存和内存到外设的DMA控制器。

那么为什么图中把DMA单独分类,DMA0和DMA1一类呢?

从方向上来说,DMA传输可以分为4类:memory到memory、memory到device、device到memory和device到device。从CPU的角度看,device都是slave,因此将这些有device参与的传输分为一类,称为Slave-DMA传输。而另一种memory到memory的传输,被称为Async TX。

扫描二维码关注公众号,回复: 6957300 查看本文章

读者若希望了解DMA传输更多信息,可参考:32.Linux-2440下的DMA驱动(详解)

二、DMA Engine介绍和DMA设备驱动步骤

DMA驱动框架定义在drivers/dma/dmaengine.c中,整体关系如下图。下面介绍DMA需要使用到的概念。

1. DMA Channels:如下图,一个DMA控制器同时传输的数据个数是有限的,这个限度称为channel

2. DMA Request Lines:DMA控制器和DMA传输设备之间需要有多条数据线线(称作DMA request,DRQ

3. 传输描述符:DMA传输属于异步传输,在传输前,slave驱动需要将本次传输的信息(如传输大小、方向等)提交给engine,engine返回描述符

编写DMA的设备驱动一般步骤如下:

1. 使用dma_request_channel()函数申请一个DMA通道

2. 使用dmaengine_slave_config()设置DMA通道参数

3. 使用dmaengine_prep_slave_single()或dmaengine_prep_slave_sg()或dmaengine_prep_dma_cyclic()获取传输描述符

4. 使用dmaengine_submit()将描述符提交到DMA等待队列

5. 使用dma_async_issue_pending()启动传输

6. 等待传输完成

步骤中所使用函数声明如下:

/* 1. 申请一个DMA通道 */
/* mask是使用dma_cap_sets()指定的DMA传输类型
 * filter_param是slave ID
 * eg:
 * dma_cap_mask_t mask;
 * dma_cap_zero(mask);
 * dma_cap_set(DMA_MEMCPY, mask);
 * dma_chan0 = dma_request_channel(mask, 0, NULL);
 */
struct dma_chan *dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param)

/* 2. 设置DMA通道参数 */
/* config用于设置DMA通道宽度、数据传输宽带、源和目的等信息 */
int dmaengine_slave_config(struct dma_chan *chan, struct dma_slave_config *config)

/* 3. 获取传输描述符 */
struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
    struct dma_chan *chan, dma_addr_t buf, size_t len,
    enum dma_transfer_direction dir, unsigned long flags)

struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
    struct dma_chan *chan, struct scatterlist *sgl,    unsigned int sg_len,
    enum dma_transfer_direction dir, unsigned long flags)

struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
        struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
        size_t period_len, enum dma_transfer_direction dir)

/* 4. 将描述符提交到DMA等待队列 */
dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)

/* 5. 启动传输 */
void dma_async_issue_pending(struct dma_chan *chan)

其中,

1. 传输类型具体列为:

enum dma_transaction_type {
    DMA_MEMCPY,
    DMA_XOR,
    DMA_PQ,
    DMA_XOR_VAL,
    DMA_PQ_VAL,
    DMA_MEMSET,
    DMA_INTERRUPT,
    DMA_SG,
    DMA_PRIVATE,
    DMA_ASYNC_TX,
    DMA_SLAVE,
    DMA_CYCLIC,
    DMA_INTERLEAVE,
/* last transaction type for creation of the capabilities mask */
    DMA_TX_TYPE_END,
};

2. struct dma_slave_config含有DMA传输所需要的参数

struct dma_slave_config {
    enum dma_transfer_direction direction;      /* 传输方向,DMA_MEM_TO_MEM、DMA_MEM_TO_DEV等 */
    dma_addr_t src_addr;                        /* 源,读数据的地址 */
    dma_addr_t dst_addr;                        /* 目的,写数据的地址 */
    enum dma_slave_buswidth src_addr_width;     /* 最大可传输的burst size */
    enum dma_slave_buswidth dst_addr_width;
    u32 src_maxburst;
    u32 dst_maxburst;
    bool device_fc;                             /* 当device是flow controller时,需要设置为true */
};

3. struct dma_async_tx_descriptor是DMA的传输描述符

struct dma_async_tx_descriptor {
    dma_cookie_t cookie;                    /* 用于追踪本次传输 */
    enum dma_ctrl_flags flags;              /* DMA_CTRL_开头的标志, */
    dma_addr_t phys;
    struct dma_chan *chan;                  /* 对应的通道 */
    dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);    /* 控制器驱动提供的回调函数 */
    dma_async_tx_callback callback;         /* 传输完成的回调函数和参数 */
    void *callback_param;
#ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
    struct dma_async_tx_descriptor *next;
    struct dma_async_tx_descriptor *parent;
    spinlock_t lock;
#endif
};

4. 对于struct scatterlist,读者可参考:Linux内核scatterlist API介绍

三、DMA设备驱动程序

源代码:

  1 #include <linux/init.h>
  2 #include <linux/module.h>
  3 #include <linux/fs.h>
  4 #include <linux/sched.h>
  5 #include <linux/miscdevice.h>
  6 #include <linux/device.h>
  7 #include <linux/string.h>
  8 #include <linux/errno.h>
  9 #include <linux/types.h>
 10 #include <linux/slab.h>
 11 #include <linux/dmaengine.h>
 12 #include <linux/dma-mapping.h>
 13 
 14 #include <asm/io.h>
 15 #include <asm/uaccess.h>
 16 
 17 #include <linux/amba/pl330.h>
 18 #include <mach/dma.h>
 19 
 20 #define BUF_SIZE            512
 21 #define PL_NO_DMA            _IOW('M', 0x1, int)        /* magic num */
 22 #define PL_USE_DMA            _IOW('M', 0x2, int)
 23 
 24 static char *src = NULL;
 25 static char *dst = NULL;
 26 static dma_addr_t dma_src;
 27 static dma_addr_t dma_dst;
 28 static enum dma_ctrl_flags flags;
 29 static dma_cookie_t cookie;
 30 static struct dma_chan *chan0 = NULL;
 31 static struct dma_device *dev0 = NULL;
 32 static struct dma_async_tx_descriptor *tx0 = NULL;
 33 
 34 static void dma_callback(void *dma_async_param)
 35 {
 36     if (0 == memcmp(src, dst, BUF_SIZE))
 37         printk("PL_USE_DMA succeed\n");
 38     else
 39         printk("PL_USE_DMA error\n");
 40 }
 41 
 42 static int pl330_dma_open(struct inode *inode, struct file *filp)
 43 {
 44     return 0;
 45 }
 46 
 47 static long pl330_dma_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 48 {
 49     int i = 0;
 50 
 51     memset(src, 0xAA, BUF_SIZE);
 52     memset(dst, 0xBB, BUF_SIZE);
 53 
 54     switch (cmd)
 55     {
 56     case PL_NO_DMA:
 57         for (i = 0; i < BUF_SIZE; i++)
 58             dst[i] = src[i];
 59 
 60         if (0 == memcmp(src, dst, BUF_SIZE))
 61             printk("PL_NO_DMA succeed\n");
 62         else
 63             printk("PL_NO_DMA error\n");
 64         break;
 65 
 66     case PL_USE_DMA:
 67         flags    = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
 68         dev0    = chan0->device;
 69         tx0        = dev0->device_prep_dma_memcpy(chan0, dma_dst, dma_src, BUF_SIZE, flags);
 70         if (!tx0)
 71             printk("device_prep_dma_memcpy error\n");
 72 
 73         tx0->callback        = dma_callback;
 74         tx0->callback_param    = NULL;
 75         cookie = tx0->tx_submit(tx0);
 76         if (dma_submit_error(cookie))
 77             printk("tx_submit error\n");
 78 
 79         dma_async_issue_pending(chan0);
 80         break;
 81 
 82     default:
 83         break;
 84     }
 85 
 86     return 0;
 87 }
 88 
 89 static int pl330_dma_release(struct inode *inode, struct file *filp)
 90 {
 91     return 0;
 92 }
 93 
 94 static struct file_operations pl330_dma_fops = {
 95     .open        = pl330_dma_open,
 96     .unlocked_ioctl = pl330_dma_ioctl,
 97     .release    = pl330_dma_release,
 98 };
 99 
100 static struct miscdevice dma_misc = {
101     .minor  = MISC_DYNAMIC_MINOR,
102     .name   = "dma_test",
103     .fops   = &pl330_dma_fops,
104 };
105 
106 extern bool pl330_filter(struct dma_chan *chan, void *param);
107 extern void msleep(unsigned int msecs);
108 
109 static int __init pl330_dma_init(void)
110 {
111     int ret = misc_register(&dma_misc);
112     if (ret < 0) {
113         printk("misc_register error\n");
114         return -EINVAL;
115     }
116 
117     src = dma_alloc_writecombine(NULL, BUF_SIZE, &dma_src, GFP_KERNEL);
118     dst = dma_alloc_writecombine(NULL, BUF_SIZE, &dma_dst, GFP_KERNEL);
119 
120     dma_cap_mask_t mask;
121     dma_cap_zero(mask);
122     dma_cap_set(DMA_SLAVE, mask);
123     
124     chan0 = dma_request_channel(mask, pl330_filter, NULL);
125     if (NULL == chan0){
126         msleep(100);
127         chan0 = dma_request_channel(mask, NULL, NULL);
128     }
129  
130     if (NULL == chan0)
131         printk("dma_request_channel error\n");
132  
133     return 0;
134 }
135 
136 static void __exit pl330_dma_exit(void)
137 {
138     dma_release_channel(chan0);
139     dma_free_writecombine(NULL, BUF_SIZE, src, dma_src);
140     dma_free_writecombine(NULL, BUF_SIZE, dst, dma_dst);
141     misc_deregister(&dma_misc);
142 }
143 
144 module_init(pl330_dma_init);
145 module_exit(pl330_dma_exit);
146 
147 MODULE_LICENSE("GPL");
View Code

Makefile:

 1 KERN_DIR = /work/itop4412/tools/linux-3.5
 2 
 3 all:
 4     make -C $(KERN_DIR) M=`pwd` modules
 5 
 6 clean:
 7     make -C $(KERN_DIR) M=`pwd` modules clean
 8     rm -rf modules.order
 9 
10 obj-m    += pl330dma.o
View Code

测试文件:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 #include <sys/types.h>
 6 #include <sys/stat.h>
 7 #include <sys/ioctl.h>
 8 #include <unistd.h>
 9 #include <fcntl.h>
10 
11 #define PL_NO_DMA            _IOW('M', 0x1, int)        /* magic num */
12 #define PL_USE_DMA            _IOW('M', 0x2, int)
13 
14 int main(void)
15 {
16     int fd;
17     fd = open("/dev/dma_test", O_RDWR);
18     if (fd < 0) {
19         printf("can't open /dev/dma_test\n");
20         return -1;
21     }
22 
23     ioctl(fd, PL_NO_DMA,    0);
24     sleep(1);
25 
26     ioctl(fd, PL_USE_DMA,    0);
27     sleep(1);
28 
29     return 0;
30 }
View Code

在测试之前,我们需要确定内核中是否配置了DMA框架:

Device Drivers --->
    [*] DMA Engine support  --->
        [*]   DMA Engine debugging
        <*>   DMA API Driver for PL330
        [*]   Async_tx: Offload support for the async_tx api 

测试:

在编译并在开发板上insmod后执行测试文件,可以看到设备驱动printk()输出。

猜你喜欢

转载自www.cnblogs.com/Lioker/p/11301195.html