Linux DMA:使用 DMA 引擎进行分散-聚集事务

Linux DMA: Using the DMAengine for scatter-gather transactions

我尝试使用自定义内核驱动程序中的 DMA 引擎 API 来执行 分散-收集 操作。我有一个连续的内存区域作为源,我想通过 scatterlist 结构将其数据复制到几个分布式缓冲区中。 DMA 控制器是支持 DMAengine API 的 PL330(参见 PL330 DMA controller)。

我的测试代码如下:

在我的驱动头文件中(test_driver.h):

#ifndef __TEST_DRIVER_H__
#define __TEST_DRIVER_H__

#include <linux/platform_device.h>
#include <linux/device.h>

#include <linux/scatterlist.h>
#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/of_dma.h>

#define SG_ENTRIES 3
#define BUF_SIZE 16
#define DEV_BUF 0x10000000

struct dma_block {
    void * data;
    int size;
};

struct dma_private_info {

    struct sg_table sgt;

    struct dma_block * blocks;
    int nblocks;

    int dma_started;

    struct dma_chan * dma_chan;
    struct dma_slave_config dma_config;
    struct dma_async_tx_descriptor * dma_desc;
    dma_cookie_t cookie;
};

struct test_platform_device {
    struct platform_device * pdev;

    struct dma_private_info dma_priv;
};

#define _get_devp(tdev) (&((tdev)->pdev->dev))
#define _get_dmapip(tdev) (&((tdev)->dma_priv))

int dma_stop(struct test_platform_device * tdev);
int dma_start(struct test_platform_device * tdev);
int dma_start_block(struct test_platform_device * tdev);
int dma_init(struct test_platform_device * tdev);
int dma_exit(struct test_platform_device * tdev);

#endif

在我的源代码中包含 dma 函数 (dma_functions.c):

#include <linux/slab.h>

#include "test_driver.h"

#define BARE_RAM_BASE 0x10000000
#define BARE_RAM_SIZE 0x10000000

struct ram_bare {
    uint32_t * __iomem map;

    uint32_t base;
    uint32_t size;
};

static void dma_sg_check(struct test_platform_device * tdev)
{
    struct dma_private_info * dma_priv = _get_dmapip(tdev);
    struct device * dev = _get_devp(tdev);
    uint32_t * buf;
    unsigned int bufsize;
    int nwords;
    int nbytes_word = sizeof(uint32_t);
    int nblocks;
    struct ram_bare ramb;
    uint32_t * p;
    int i;
    int j;

    ramb.map = ioremap(BARE_RAM_BASE,BARE_RAM_SIZE);
    ramb.base = BARE_RAM_BASE;
    ramb.size = BARE_RAM_SIZE;

    dev_info(dev,"nblocks: %d \n",dma_priv->nblocks);

    p = ramb.map;

    nblocks = dma_priv->nblocks;

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

        buf = (uint32_t *) dma_priv->blocks[i].data;
        bufsize = dma_priv->blocks[i].size;
        nwords = dma_priv->blocks[i].size/nbytes_word;

        dev_info(dev,"block[%d],size %d: ",i,bufsize);

        for ( j = 0 ; j <  nwords; j++, p++) {
            dev_info(dev,"DMA: 0x%x, RAM: 0x%x",buf[j],ioread32(p));
        }
    }

    iounmap(ramb.map);
}

static int dma_sg_exit(struct test_platform_device * tdev)
{
    struct dma_private_info * dma_priv = _get_dmapip(tdev);
    int ret = 0;
    int i;

    for( i = 0 ; i < dma_priv->nblocks ; i++ ) {
        kfree(dma_priv->blocks[i].data);
    }

    kfree(dma_priv->blocks);

    sg_free_table(&(dma_priv->sgt));

    return ret;
}

int dma_stop(struct test_platform_device * tdev)
{
    struct dma_private_info * dma_priv = _get_dmapip(tdev);
    struct device * dev = _get_devp(tdev);
    int ret = 0;

    dma_unmap_sg(dev,dma_priv->sgt.sgl,\
        dma_priv->sgt.nents, DMA_FROM_DEVICE);

    dma_sg_exit(tdev);

    dma_priv->dma_started = 0;

    return ret;
}

static void dma_callback(void * param)
{
    enum dma_status dma_stat;
    struct test_platform_device * tdev = (struct test_platform_device *) param;
    struct dma_private_info * dma_priv = _get_dmapip(tdev);
    struct device * dev = _get_devp(tdev);

    dev_info(dev,"Checking the DMA state....\n");

    dma_stat = dma_async_is_tx_complete(dma_priv->dma_chan,\
        dma_priv->cookie, NULL, NULL);

    if(dma_stat == DMA_COMPLETE) {
        dev_info(dev,"DMA complete! \n");
        dma_sg_check(tdev);
        dma_stop(tdev);
    } else if (unlikely(dma_stat == DMA_ERROR)) {
        dev_info(dev,"DMA error! \n");
        dma_stop(tdev);
    }
}

static void dma_busy_loop(struct test_platform_device * tdev)
{
    struct dma_private_info * dma_priv = _get_dmapip(tdev);
    struct device * dev = _get_devp(tdev);

    enum dma_status status;
    int status_change = -1;

    do {
        status = dma_async_is_tx_complete(dma_priv->dma_chan, dma_priv->cookie, NULL, NULL);

        switch(status) {
        case DMA_COMPLETE:
            if(status_change != 0)
                dev_info(dev,"DMA status: COMPLETE\n");
            status_change = 0;
            break;
        case DMA_PAUSED:
            if (status_change != 1)
                dev_info(dev,"DMA status: PAUSED\n");
            status_change = 1;
            break;
        case DMA_IN_PROGRESS:
            if(status_change != 2)
                dev_info(dev,"DMA status: IN PROGRESS\n");
            status_change = 2;
            break;
        case DMA_ERROR:
            if (status_change != 3)
                dev_info(dev,"DMA status: ERROR\n");
            status_change = 3;
            break;
        default:
            dev_info(dev,"DMA status: UNKNOWN\n");
            status_change = -1;
            break;
        }
    } while(status != DMA_COMPLETE);

    dev_info(dev,"DMA transaction completed! \n");
}

static int dma_sg_init(struct test_platform_device * tdev)
{

    struct dma_private_info * dma_priv = _get_dmapip(tdev);
    struct scatterlist *sg;
    int ret = 0;
    int i;

    ret = sg_alloc_table(&(dma_priv->sgt), SG_ENTRIES, GFP_ATOMIC);
    if(ret)
        goto out_mem2;

    dma_priv->nblocks = SG_ENTRIES;
    dma_priv->blocks = (struct dma_block *) kmalloc(dma_priv->nblocks\
        *sizeof(struct dma_block), GFP_ATOMIC);
    if(dma_priv->blocks == NULL) 
         goto out_mem1;


    for( i = 0 ; i < dma_priv->nblocks ; i++ ) {
        dma_priv->blocks[i].size = BUF_SIZE;
        dma_priv->blocks[i].data = kmalloc(dma_priv->blocks[i].size, GFP_ATOMIC);
        if(dma_priv->blocks[i].data == NULL)
            goto out_mem3;
    }

    for_each_sg(dma_priv->sgt.sgl, sg, dma_priv->sgt.nents, i)
        sg_set_buf(sg,dma_priv->blocks[i].data,dma_priv->blocks[i].size);

    return ret;

out_mem3:
    i--;

    while(i >= 0)
        kfree(dma_priv->blocks[i].data);

    kfree(dma_priv->blocks);

out_mem2:
    sg_free_table(&(dma_priv->sgt));

out_mem1:
    ret = -ENOMEM;  

    return ret;

}

static int _dma_start(struct test_platform_device * tdev,int block)
{
    struct dma_private_info * dma_priv = _get_dmapip(tdev);
    struct device * dev = _get_devp(tdev);
    int ret = 0;
    int sglen;

    /* Step 1: Allocate and initialize the SG list */
    dma_sg_init(tdev);

    /* Step 2: Map the SG list */
    sglen = dma_map_sg(dev,dma_priv->sgt.sgl,\
        dma_priv->sgt.nents, DMA_FROM_DEVICE);
    if(! sglen)
        goto out2;

    /* Step 3: Configure the DMA */
    (dma_priv->dma_config).direction = DMA_DEV_TO_MEM;
    (dma_priv->dma_config).src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
    (dma_priv->dma_config).src_maxburst = 1;
    (dma_priv->dma_config).src_addr = (dma_addr_t) DEV_BUF;

    dmaengine_slave_config(dma_priv->dma_chan, \
        &(dma_priv->dma_config));

    /* Step 4: Prepare the SG descriptor */
    dma_priv->dma_desc = dmaengine_prep_slave_sg(dma_priv->dma_chan, \
        dma_priv->sgt.sgl, dma_priv->sgt.nents, DMA_DEV_TO_MEM, \
        DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
    if (dma_priv->dma_desc == NULL) {
        dev_err(dev,"DMA could not assign a descriptor! \n");
        goto out1;
    }

    /* Step 5: Set the callback method */
    (dma_priv->dma_desc)->callback = dma_callback;
    (dma_priv->dma_desc)->callback_param = (void *) tdev;

    /* Step 6: Put the DMA descriptor in the queue */
    dma_priv->cookie = dmaengine_submit(dma_priv->dma_desc);

    /* Step 7: Fires the DMA transaction */
    dma_async_issue_pending(dma_priv->dma_chan);

    dma_priv->dma_started = 1;

    if(block)
        dma_busy_loop(tdev);

    return ret;

out1:
    dma_stop(tdev);
out2:
    ret = -1;

    return ret;
}

int dma_start(struct test_platform_device * tdev) {
    return _dma_start(tdev,0);
}

int dma_start_block(struct test_platform_device * tdev) {
    return _dma_start(tdev,1);
}

int dma_init(struct test_platform_device * tdev)
{
    int ret = 0;
    struct dma_private_info * dma_priv = _get_dmapip(tdev);
    struct device * dev = _get_devp(tdev);

    dma_priv->dma_chan = dma_request_slave_channel(dev, \
        "dma_chan0");
    if (dma_priv->dma_chan == NULL) {
        dev_err(dev,"DMA channel busy! \n");
        ret = -1;
    }

    dma_priv->dma_started = 0;

    return ret;
}

int dma_exit(struct test_platform_device * tdev)
{
    int ret = 0;
    struct dma_private_info * dma_priv = _get_dmapip(tdev);

    if(dma_priv->dma_started) {
        dmaengine_terminate_all(dma_priv->dma_chan);
        dma_stop(tdev);
        dma_priv->dma_started = 0;
    }

    if(dma_priv->dma_chan != NULL)
        dma_release_channel(dma_priv->dma_chan);

    return ret;
}

在我的驱动程序源文件中(test_driver.c):

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>

#include "test_driver.h"

static int dma_block=0;
module_param_named(dma_block, dma_block, int, 0444);

static struct test_platform_device tdev;

static struct of_device_id test_of_match[] = {
  { .compatible = "custom,test-driver-1.0", },
  {}
};

static int test_probe(struct platform_device *op)
{
    int ret = 0;
    struct device * dev = &(op->dev);

    const struct of_device_id *match = of_match_device(test_of_match, &op->dev);

    if (!match)
        return -EINVAL;

    tdev.pdev = op;

    dma_init(&tdev);

    if(dma_block)
        ret = dma_start_block(&tdev);
    else
        ret = dma_start(&tdev);

    if(ret) {
        dev_err(dev,"Error to start DMA transaction! \n");
    } else {
        dev_info(dev,"DMA OK! \n");
    }

    return ret;
}

static int test_remove(struct platform_device *op)
{       
    dma_exit(&tdev);

    return 0;
}

static struct platform_driver test_platform_driver = {
  .probe = test_probe,
  .remove = test_remove,
  .driver = {
    .name = "test-driver",
    .owner = THIS_MODULE,
    .of_match_table = test_of_match,
  },
};

static int test_init(void)
{
    platform_driver_register(&test_platform_driver);
    return 0;
}

static void test_exit(void)
{
    platform_driver_unregister(&test_platform_driver);
}

module_init(test_init);
module_exit(test_exit);

MODULE_AUTHOR("klyone");
MODULE_DESCRIPTION("DMA SG test module");
MODULE_LICENSE("GPL");

但是,DMA 从不调用我的回调函数,我也不知道为什么会这样。也许,我误会了什么...

谁能帮帮我?

提前致谢。

警告:我没有适合您的最终解决方案,只是关于如何调试的一些观察和建议[基于多年的经验writing/debugging linux 设备驱动程序].

我假设您认为回调没有完成,因为您没有收到任何 printk 消息。但是,回调是 唯一 有它们的地方。但是,printk 级别是否设置得足够高以查看消息?我会在您的模块初始化中添加一个 dev_info,以证明它按预期打印。

此外,如果 dma_start 没有按预期工作,你 [可能] 不会收到回调,所以我也会在那里添加一些 dev_info 调用(例如之前和之后步骤 7 中的调用)。我还注意到,并非 dma_start 中的所有调用都检查错误 returns [可能正常或无效 return,只是提一下,以防你错过一个]

此时需要注意的是,这里确实有两个问题:(1)你的DMA请求是否启动成功[并且完成]? (2) 你收到回电了吗?

所以,我将一些代码从 dma_complete 拆分成(例如)dma_test_done。后者进行相同的检查,但只打印 "complete" 消息。您可以在轮询模式下调用它来验证 DMA 完成。

因此,如果您 [最终] 获得了完成,那么问题就归结为您没有获得回调的原因。但是,如果您 [甚至] 没有完成,那就是一个更根本的问题。

这让我想起了。您没有显示任何调用 dma_start 的代码或您如何 等待 完成。我假设如果你的回调有效,它会发出某种唤醒,基层将等待。或者,回调将执行请求 deallocate/cleanup(即您编写的更多代码)

在第 7 步,您正在调用 dma_async_issue_pending,它应该调用 pl330_issue_pendingpl330_issue_pending 会调用 pl330_tasklet.

pl330_tasklet是一个tasklet函数,但也可以直接调用[在没有活动请求时启动DMA]。

pl330_tasklet 将在其 "work" 队列上循环并将任何已完成的项目移至其 "completed" 队列。然后它会尝试启动新请求。然后它在其完成的队列上循环并发出回调。

pl330_tasklet 获取回调指针,但如果它为空,则它会被静默忽略。您已经设置了回调,但最好验证 设置回调的位置与 pl330_tasklet 将获取回调的位置相同 [或传播到]来自.

当你打电话的时候,一切都可能很忙,所以有没有个完成的请求,没有个空间来开始新的请求, 所以没有 完成。在这种情况下,pl330_tasklet稍后将再次调用。

因此,当 dma_async_issue_pending return 时, 可能 什么都没发生。这对你的情况来说很有可能。

pl330_tasklet 尝试通过调用 fill_queue 来启动新的 DMA。它将通过查看 status != BUSY 检查描述符是否 [已经] 忙。因此,您可能希望验证您的值是否正确。否则,您将永远 得到回调[甚至任何 DMA 启动]。

然后,fill_queue 将尝试通过 pl330_submit_req 启动请求。但是,这可能 return 一个错误(例如队列已经满了),所以,再次,事情被推迟了。

作为参考,请注意 pl330_submit_req 顶部的以下注释:

Submit a list of xfers after which the client wants notification.
Client is not notified after each xfer unit, just once after all
xfer units are done or some error occurs.

我要做的是开始破解 pl330.c 并添加调试消息和交叉检查。如果您的系统是 pl330 正在为 许多 其他请求提供服务,您可以通过检查设备的私有数据指针是否与您的匹配来限制调试消息。

特别是,您希望在请求真正开始时收到一条消息,因此您可以在 pl330_submit_req

的末尾添加一条调试消息

然后,在 pl330_tasklet 内为请求添加消息也会有所帮助。

这是两个很好的起点。但是,不要害怕根据需要添加更多 printk 调用。您可能会对被调用的内容[或没有被调用]或按什么顺序感到惊讶。


更新:

If I install the kernel module with the blocking behaviour, everything is initialized well. However, the dma_busy_loop function shows that the DMA descriptor is always IN PROGESS and the DMA transaction never completes. For this reason, the callback function is not executed. What could be happening?

做了更多的研究。 Cookie 只是递增的序列号。例如,如果您发出的请求被分解为 [say] 10 个单独的 scatter/gather 操作 [描述符],每个操作都会获得一个唯一的 cookie 值。 cookie return 值是 latest/last 串(例如 10)。

当您调用 (1) dma_async_is_tx_complete 时,(2) 它调用 chan->device->device_tx_status,(3) 即 pl330_tx_status,(4) 调用 dma_cookie_status

边note/tip:我在追这个的时候一直在dmaengine.hpl330.c之间来回翻来覆去。就像:看 (1),它调用 (2)。那个套装在哪里?在 pl330.c,我想。所以,我 grepp 了字符串并得到了 pl330 函数的名称(即(3))。所以,我去那里,看到它确实如此 (4)。所以...回到dmaengine.h ...

但是,当您进行外部调用时,您将忽略[设置为 NULL] 最后两个参数。这些可能很有用,因为它们 return "last" 和 "used" cookie。因此,即使您没有完全完成,这些值也可能会更改并显示部分进度。

其中一个最终应该 >= "return" cookie 值。 (即)整个操作应该是完整的。因此,这将有助于区分可能发生的情况。

另外,请注意 dmaengine.hdma_async_is_tx_complete 的正下方有 dma_async_is_complete。此函数根据您传递的 cookie 值以及 "last" 和 "used" cookie 值来决定是 return DMA_COMPLETE 还是 DMA_IN_PROGRESS。它是被动的,未在代码路径中使用 [AFAICT],但它确实展示了如何自己计算完成。