How do drivers do DMA in Linux?
Short answer: Drivers use the DMA API, which hides IOMMUs, bus-address translation, bounce buffers and cache maintenance. Coherent mappings (dma_alloc_coherent()) give a buffer that CPU and device can both access at any time without explicit cache operations, used for descriptor rings and long-lived shared structures. Streaming mappings (dma_map_single(), dma_map_sg()) map an existing buffer for one transfer in one direction, with ownership handed between CPU and device by dma_unmap_* or dma_sync_*_for_cpu/for_device. The device is told the returned dma_addr_t, never the CPU virtual or physical address. Transfers through a separate DMA controller use the dmaengine API.
Setup and coherent buffer
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); /* device can address 32 bits */
if (ret) return ret;
d->ring = dma_alloc_coherent(dev, RING_BYTES, &d->ring_dma, GFP_KERNEL);
if (!d->ring) return -ENOMEM;
writel(lower_32_bits(d->ring_dma), d->base + RING_BASE); /* the device gets the DMA address */
Streaming mapping for a transmit buffer
dma_addr_t a = dma_map_single(dev, skb_data, len, DMA_TO_DEVICE); /* cleans caches */
if (dma_mapping_error(dev, a)) return -ENOMEM;
desc->addr = cpu_to_le64(a);
desc->len = cpu_to_le32(len);
dma_wmb(); /* descriptor fields visible before ownership flag */
desc->flags = cpu_to_le32(DESC_OWN_HW);
writel(1, d->base + DOORBELL);
/* in the completion handler: dma_unmap_single(dev, a, len, DMA_TO_DEVICE); */
dmaengine (memory ↔ peripheral via a system DMA controller)
chan = dma_request_chan(dev, "rx"); /* from DT dmas/dma-names */
dmaengine_slave_config(chan, &cfg); /* FIFO address, width, burst */
desc = dmaengine_prep_slave_single(chan, buf_dma, len, DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
desc->callback = rx_done; desc->callback_param = d;
dmaengine_submit(desc);
dma_async_issue_pending(chan);
Rules
- Never DMA to or from stack memory or
vmallocmemory (not physically contiguous, may share cache lines). - Do not touch a streaming buffer while the device owns it.
- With non-coherent devices, the DMA API performs cache clean/invalidate; buffers should be cache-line aligned (kmalloc guarantees
ARCH_DMA_MINALIGN). - IOMMU (Arm SMMU, Intel VT-d) translates device addresses, gives isolation and makes scattered pages look contiguous to the device.
- dma-buf shares DMA buffers between drivers (camera → GPU → display) without copying.