DMA in PCI Drivers: Coherent vs Streaming Mappings
Choose between dma_alloc_coherent and dma_map_single in a Linux 6.12 PCI driver by lifetime, access pattern and cost, with the mask, sync and unmap rules.

A device that moves data by DMA reads and writes system memory on its own, and the memory it sees is not the memory the CPU sees. The device uses bus addresses, the CPU caches the same bytes, and on some systems an IOMMU sits between them. The kernel's DMA API hides that, and the first decision in using it is between two kinds of mapping: coherent memory that both sides always see consistently, and streaming mappings that you create around a buffer for one transfer. This post compares them on Linux 6.12, shows the code for each, and ends in a decision table. It assumes the driver from the PCI skeleton with bus mastering enabled.
The API is specified in the kernel's Dynamic DMA mapping Guide and DMA API reference.
Set the mask first
Before any mapping, tell the DMA layer how many address bits the device can drive. A device that generates only 32-bit addresses cannot reach memory above 4 GiB, and the kernel must either allocate below that line or bounce data through a low buffer. A 64-bit capable device gets any address.
ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
if (ret)
ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
if (ret) {
dev_err(&pdev->dev, "no usable DMA addressing\n");
return ret;
}dma_set_mask_and_coherent() sets the mask for both kinds of mapping at once. The sequence tries 64 bits and falls back to 32, which is the idiom for a device that works either way. If neither succeeds, the machine cannot DMA for this device and the probe must fail. Call this before dma_alloc_coherent() or any map call, and call pci_set_master() so the device may issue the accesses.
Coherent mappings
A coherent allocation returns memory that the CPU and the device can both access at any time without explicit synchronization. The kernel makes it so either by using hardware that keeps caches coherent, or by mapping the memory uncached.
struct sample_ring {
struct sample_desc *desc;
dma_addr_t desc_dma;
};
ring->desc = dma_alloc_coherent(&pdev->dev,
RING_ENTRIES * sizeof(*ring->desc),
&ring->desc_dma, GFP_KERNEL);
if (!ring->desc)
return -ENOMEM;
iowrite32(lower_32_bits(ring->desc_dma), s->regs + REG_RING_LO);
iowrite32(upper_32_bits(ring->desc_dma), s->regs + REG_RING_HI);The function returns two things: a CPU virtual address you use in code and a dma_addr_t you give to the device. They are different numbers, and the second must go into the device's registers. Mixing them up is the standard first bug: a device handed a virtual address reads some random page of memory.
Free it with dma_free_coherent(), passing the same size, address and handle:
dma_free_coherent(&pdev->dev, RING_ENTRIES * sizeof(*ring->desc),
ring->desc, ring->desc_dma);Coherent memory fits things that live as long as the device is in use and that both sides touch in small pieces: descriptor rings, completion queues, shared status blocks. The device updates a status word, the CPU reads it without a cache flush, and neither needs a sync call.
The cost is that coherent memory can be slow on a CPU without coherent DMA. On such a platform, the buffer is uncached, so every CPU access goes to memory. A large buffer that the CPU reads and writes in full is a poor fit.
Streaming mappings
A streaming mapping takes ordinary memory you already have, for example a packet buffer from kmalloc() or a page from the block layer, and hands ownership to the device for one transfer.
dma_addr_t dma;
dma = dma_map_single(&pdev->dev, buf, len, DMA_TO_DEVICE);
if (dma_mapping_error(&pdev->dev, dma))
return -ENOMEM;
iowrite32(lower_32_bits(dma), s->regs + REG_SRC_LO);
iowrite32(upper_32_bits(dma), s->regs + REG_SRC_HI);
iowrite32(len, s->regs + REG_COUNT);
iowrite32(CMD_START, s->regs + REG_CMD);Always check the result with dma_mapping_error(). The mapping can fail when there is no bounce buffer space or the IOMMU is full, and the returned handle is not a valid address in that case.
The direction argument tells the DMA layer which side writes: DMA_TO_DEVICE (CPU wrote, device reads), DMA_FROM_DEVICE (device writes, CPU reads) and DMA_BIDIRECTIONAL. On a system with non-coherent caches the mapping call flushes or invalidates the right lines for that direction. Choose the narrowest direction that is true. DMA_BIDIRECTIONAL forces both operations and is slower.
While the mapping exists, the buffer belongs to the device. The CPU must not touch it. When the transfer is done, unmap, and ownership returns:
dma_unmap_single(&pdev->dev, dma, len, DMA_TO_DEVICE);For a buffer that stays mapped across several transfers, the CPU can reclaim and give back ownership without unmapping, with the sync calls:
dma_sync_single_for_cpu(&pdev->dev, dma, len, DMA_FROM_DEVICE);
/* the CPU may read buf here */
dma_sync_single_for_device(&pdev->dev, dma, len, DMA_FROM_DEVICE);A receive path commonly maps its buffers once at setup and uses the pair to look at each completed packet and then hand the buffer back. This avoids paying the map and unmap cost per packet.
Scatter-gather
Many transfers span several buffers that are not contiguous. dma_map_sg() maps a list of struct scatterlist entries in one call, and may merge adjacent entries when an IOMMU can present them as one range. It returns the number of mapped segments, which can be smaller than the input, and you must loop over that returned count, not the original. Unmap with dma_unmap_sg() and the original count. Using the original count after mapping is a classic bug that works until an IOMMU merges entries.
What the code and the platform do not guarantee
A coherent mapping guarantees visibility of CPU writes to the device and of device writes to the CPU, but not ordering with respect to other accesses. A device that writes data and then a "done" flag can have the flag arrive before the data on weakly ordered systems unless the device design prevents it, and the CPU side reads the flag and then needs a read barrier, dma_rmb(), before reading the data:
if (READ_ONCE(ring->desc[i].status) & DESC_DONE) {
dma_rmb();
handle(&ring->desc[i]);
}The write side has the counterpart: fill the descriptor, issue dma_wmb(), and only then set the ownership bit the device looks at. These barriers are the cheap, device-specific ordering that wmb() would over-provide. They exist because coherence is about visibility, not order.
The decision
| Question | Coherent | Streaming |
|---|---|---|
| Buffer lives as long as the device is in use? | Yes | No |
| Both CPU and device update small pieces often? | Yes | No |
| Large buffer, one transfer, CPU touches it in bulk? | No | Yes |
| Buffer comes from elsewhere (network stack, block layer)? | No | Yes |
| Need to avoid per-transfer map cost on a ring? | Yes | Map once and use sync calls |
| Platform has non-coherent caches? | Allowed but uncached and slow for big buffers | Preferred |
In practice, drivers use both: coherent memory for the descriptor ring and status, and streaming mappings for the data buffers the descriptors point at. A network driver does exactly this, as does the NVMe driver.
Debugging
CONFIG_DMA_API_DEBUG makes the kernel track every mapping and warn on a mismatch: unmapping with the wrong size or direction, mapping the same buffer twice, or freeing memory that is still mapped. Enable it in a development kernel. It turns an intermittent corruption into a message that names the line. The IOMMU adds protection of its own: a device that writes outside a mapping causes a fault that the kernel logs with the address, which is the topic of the IOMMU post.


