Linux下的DMA學習

       最近在忙一個豎屏轉橫屏項目,使用的是spi接口的豎屏(cpu不帶dpu,橫屏lcd比豎屏lcd成本高),使用的方案就是利用dma的特性,將橫屏的數據進行一定規則的搬運(設置dma的搬運規則,從左下角搬運第一列作爲第一行,搬完所有的列)轉換成豎屏的數據,然後發送到lcd。

常見的通信接口i2c,spi,i2s有fifo,dma兩種通信方式。fifo每次傳輸的數據量不大,dma能進行大規模的數據量傳輸,減輕cpu的負擔。一般驅動裏要使用dma的傳輸方式,只需在相應的控制器中使能dma的開關即可(如dma-mode = <1>),設備驅動調用底層的接口時,cpu廠商的控制器驅動已經配好了dma的傳輸方式。

拿sprd的代碼來學習下,如下

kernel/drivers/spi/spi-sprd.c
kernel/drivers/dma/sprd_dma.c

1.probe函數配置相應的接口函數,註冊dma

static int sprd_dma_probe(struct platform_device *pdev)
{
    sdev->dma_dev.device_alloc_chan_resources =sprd_dma_alloc_chan_resources;
    sdev->dma_dev.device_prep_dma_memcpy = sprd_dma_prep_dma_memcpy;
    sdev->dma_dev.device_prep_dma_sg = sprd_prep_dma_sg;
    sdev->dma_dev.device_config = sprd_dma_control;
    dma_async_device_register(&sdev->dma_dev);
    of_dma_controller_register(pdev->dev.of_node,sprd_dma_simple_xlate, &sprd_dma_info);
}

2.申請dma資源

sprd_spi_request_dma--->of_dma_request_slave_channel---->ofdma->of_dma_xlate---->sprd_dma_simple_xlate

struct dma_chan *sprd_dma_simple_xlate(struct of_phandle_args *dma_spec,struct of_dma *ofdma)
{
    return dma_request_channel(info->dma_cap, info->filter_fn,dma_spec);
}

struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,dma_filter_fn fn, void *fn_param)
{
    err = dma_chan_get(chan);
}
static int dma_chan_get(struct dma_chan *chan)
{
    chan->device->device_alloc_chan_resources(chan);   ---------------------mdesc->desc.tx_submit = sprd_desc_submit;
}

分配資源,主要是描述符初始化(tx_submit),註冊鏈表用於管理數據(不同平臺有差異)。

struct dma_async_tx_descriptor {
	dma_cookie_t cookie;
	enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
	dma_addr_t phys;
	struct dma_chan *chan;
	dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
	int (*desc_free)(struct dma_async_tx_descriptor *tx);
	dma_async_tx_callback callback;
	void *callback_param;
	struct dmaengine_unmap_data *unmap;
};

static int sprd_dma_alloc_chan_resources(struct dma_chan *chan)
{
    ...
	dma_async_tx_descriptor_init(&mdesc->desc, chan);
	mdesc->desc.flags = DMA_CTRL_ACK;
	mdesc->desc.tx_submit = sprd_desc_submit;

	mdesc->dma_chn_reg = &chn_reg[i];
	mdesc->done = 0;
	mdesc->cycle = 0;
	mdesc->dma_flags = 0;
	INIT_LIST_HEAD(&mdesc->node);
	INIT_LIST_HEAD(&mdesc->next_node);
	list_add_tail(&mdesc->node, &descs);
    ...
}

3.使用流式dma映射,刷新cache到內存,參考https://blog.csdn.net/jasonchen_gbd/article/details/79462064

sprd_spi_dma_buf_mapping--->t->tx_dma = dma_map_single(spi_chip->dev, (void *)t->tx_buf,t->len, DMA_TO_DEVICE)

4.配置dma傳輸方式,設置發送完成回掉函數,使能dma

sprd_spi_dma_tx_config--->sprd_spi_dma_config(spi_chip, &dma_config, 0)--->
dmaengine_slave_config(spi_chip->dma[rx],&(c->config))--->chan->device->device_config
dma_dev->device_prep_dma_memcpy( spi_chip->dma[rx], 0, 0, 0, DMA_CFG_FLAG|DMA_HARDWARE_FLAG)
dma_des->callback = spi_chip->callback[rx];
dmaengine_submit(dma_des)--->desc->tx_submit(desc);

所以,每次發送數據,會進行dma映射,配置dma模式,並使能dma。

發送完成後,硬件觸發中斷,調用回掉函數

dma_irq_handle
tasklet_schedule(&sdev->tasklet);//tasklet_init(&sdev->tasklet, sprd_dma_tasklet,(unsigned long)sdev);
sprd_dma_process_completed
desc->callback(desc->callback_param);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章