dma_alloc_coherent
From: Ran Shalit <hidden>
Date: 2016-02-08 06:24:59
On Mon, Feb 8, 2016 at 6:01 AM, Vishwas Srivastava [off-list ref] wrote:
Hi Ran,
the api which you have mentioned...
void *
dma_alloc_coherent(struct device *dev, size_t size,
dma_addr_t *dma_handle, gfp_t flag)
is the kernel api to alloc consistent memory.
DMA devices understand the physical addresses not the virtual addresses.
which means that we must supply to the dma device, the physical address, to
read
from or to write to.
The second argument of this api is an input argument which is updated
by the kernel if this api returns a success (and contains the physical base
address of the allocated memory) and the returned value of this api is the
kernel virtual address.
if the *CPU* has to operate on this memory (assume that the memory is dma'ed
by the dma device and cpu want to read it for further processing ) it should
use the virtual address, so the returned value of this api, as the base
address.
However, if the dma device has to operate on this memory (assume device want
to write to this memory), it should use the *dma_handle* , which is the
physical address (base) of the dma memory.
Now the question is how the dma device knows about this *physical* address?
The answer is that the "dma controller" register would have a register to
accept this physical address.
So the sequence of steps probably would be, in your case:
1: allocate the dma memory
2: programme the dma controller register with the physical address returned
by this api, plus the size of the transaction and may be some more registers
for setting some kind of flags (depends on your dma device)
3: programme the dma controller's dma *start* bit.
after this the dma starts and dma device starts writing to the memory .
I hope, this clarifies you.
Hi Vishwas, That's fully clarify the questions about dma_alloc_coherent. I also try to figure out what's the difference between dma_alloc_coherent and dma_map_single. I could not find and important difference between these two methods. 1. With both methods I stil need to program the dma controller with the physical address and the start trigger. 2. I can still do the allocation whenever I want with both methods (for example at the initialization of the driver), 3. Not sure what the actuall dma_map_single does (and if it really necessary to use it), becuase it seems I could just translate the virtual value from kmalloc into physical address and return it to the dma controller. Thank you for the time, Ran