Here's an example of copying data using DMA in the Linux 4.19 kernel. In this example, we'll use a PCIe endpoint device to transmit and receive data over DMA.
Step 1: Setting Up the DMA Channel
We first need to reserve a DMA channel and configure it for use. We can define the DMA channel in the device tree as follows:
dma@0 {
#dma-cells = <1>;
compatible = "xlnx,axi-dma";
reg = <0x40400000 0x10000>;
interrupt-parent = <&GIC>;
interrupts = <0 28 4>;
xlnx,include-sg = <1>;
xlnx,s2mm-dma-ch = <0>;
xlnx,mm2s-dma-ch = <1>;
};
Here, we define the DMA channel as an AXI DMA controller from Xilinx. We also specify the registers for the controller, the parent interrupt controller, the interrupt number, and the number of scatter-gather entries to include. We also specify the DMA channel numbers for the S2MM (stream to memory) and MM2S (memory to stream) transfers.
Step 2: Allocating DMA Buffers
We can allocate the DMA buffers using the dma_alloc_coherent() function as follows:
#define BUF_SIZE 1024
static dma_addr_t src_phys_addr;
static dma_addr_t dst_phys_addr;
static u32 *src_buf;
static u32 *dst_buf;
src_buf = dma_alloc_coherent(dev, BUF_SIZE, &src_phys_addr, GFP_KERNEL);
if (!src_buf) {
dev_err(dev, "Failed to allocate source buffer\n");
return -ENOMEM;
}
dst_buf = dma_alloc_coherent(dev, BUF_SIZE, &dst_phys_addr, GFP_KERNEL);
if (!dst_buf) {
dev_err(dev, "Failed to allocate destination buffer\n");
dma_free_coherent(dev, BUF_SIZE, src_buf, src_phys_addr);
return -ENOMEM;
}
Here, we allocate two DMA buffers of size BUF_SIZE using the dma_alloc_coherent() function. The function returns the virtual address of the allocated memory in src_buf and dst_buf. The corresponding physical addresses are returned in src_phys_addr and dst_phys_addr.
Step 3: Setting Up the DMA Transfer
We can set up the DMA transfer using the DMA engine API as follows:
struct dma_async_tx_descriptor *tx_desc;
struct dma_slave_config config = {0, };
config.direction = DMA_MEM_TO_DEV;
config.dst_addr = device_addr; // PCIe endpoint device address
config.dst_maxburst = 4; // maximum transfer size
config.src_addr = src_phys_addr;
config.src_maxburst = 4;
tx_desc = dmaengine_prep_slave_dma(dev->dma_chan, &config,
BUF_SIZE, DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
if (!tx_desc) {
dev_err(dev, "Failed to prepare DMA\n");
dma_free_coherent(dev, BUF_SIZE, src_buf, src_phys_addr);
dma_free_coherent(dev, BUF_SIZE, dst_buf, dst_phys_addr);
return -EIO;
}
dmaengine_submit(tx_desc);
dma_async_issue_pending(dev->dma_chan);
Here, we create a DMA slave configuration structure config specifying the direction of transfer and the source and destination addresses. We then call the dmaengine_prep_slave_dma() function to prepare a DMA slave transaction with the specified configuration, the transfer size, and the transfer direction. We also specify the flags for the transaction.
Finally, we submit the transaction to the DMA channel with dmaengine_submit() and start the transfer with dma_async_issue_pending().
Step 4: Waiting for DMA Completion
We need to wait for the DMA transfer to complete before accessing the destination buffer. We can use the dma_async_is_tx_complete() function to check if the DMA transfer has completed.
bool dma_tx_complete;
dma_tx_complete = dma_async_is_tx_complete(dev->dma_chan, tx_desc,
dst_phys_addr, BUF_SIZE, BUF_SIZE, false);
if (!dma_tx_complete) {
dev_err(dev, "DMA transfer not complete\n");
dmaengine_terminate_all(dev->dma_chan);
dma_free_coherent(dev, BUF_SIZE, src_buf, src_phys_addr);
dma_free_coherent(dev, BUF_SIZE, dst_buf, dst_phys_addr);
return -EIO;
}
This function returns true if the transfer is complete.
Step 5: Freeing the DMA Resources
We should free the resources allocated for DMA transfer after completion or in the case of error.
dmaengine_terminate_all(dev->dma_chan);
dma_free_coherent(dev, BUF_SIZE, src_buf, src_phys_addr);
dma_free_coherent(dev, BUF_SIZE, dst_buf, dst_phys_addr);
The dmaengine_terminate_all() function stops any queued DMA transactions on the channel and frees any resources associated with the transaction.
Conclusion
We can use the DMA engine API in the Linux kernel to set up and manage DMA channels for data transfer between devices and the main memory. The above example shows how to use the DMA engine API to transfer data using a PCIe endpoint device.