I am working on pcie based network driver. Different examples use one of pci_alloc_consistent or dma_alloc_coherent to get memory for transmission and reception descriptors. Which one is better if any and what is the difference between the two?
dma_alloc_coherent() returns two values: the virtual address which you can use to access it from the CPU and dma_handle which you pass to the card. The CPU virtual address and the DMA address are both guaranteed to be aligned to the smallest PAGE_SIZE order which is greater than or equal to the requested size.
Direct memory access, or DMA, is the advanced topic that completes our overview of memory issues. DMA is the hardware mechanism that allows peripheral components to transfer their I/O data directly to and from main memory without the need for the system processor to be involved in the transfer.
The difference is subtle but quite important.
pci_alloc_consistent()
is the older function of the two and legacy drivers still use it.
Nowadays, pci_alloc_consistent()
just calls dma_alloc_coherent()
.
The difference? The type of the allocated memory.
pci_alloc_consistent()
- Allocates memory of type GFP_ATOMIC
.
Allocation does not sleep, for use in e.g. interrupt handlers, bottom
halves.
dma_alloc_coherent()
- You specify yourself what type of memory to
allocate. You should not use the high priority GFP_ATOMIC
memory
unless you need it and in most cases, you will be fine with
GFP_KERNEL
allocations.
Kernel 3.18 definition of pci_alloc_consistent()
is very simple, namely:
static inline void *
pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
dma_addr_t *dma_handle)
{
return dma_alloc_coherent(hwdev == NULL ? NULL : &hwdev->dev, size, dma_handle, GFP_ATOMIC);
}
In short, use dma_alloc_coherent()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With