Some questions on dma_alloc_coherent

ravi picture ravi · Jul 9, 2012 · Viewed 15.5k times · Source
  1. Is the DMA address returned from this call the same as the physical address? LDD3 says the DMA address should be treated as opaque by the driver. I want to mmap this DMA buffer so user-space can read/write directly to it. Question is what PFN should I specify for the remap_pfn_range (which to my pleasant surprise now (kernel 3.4+) works for conventional memory same as for I/O memory). Can I just cast the DMA address to unsigned long and convert that to PFN? Isn't this a violation of what LDD3 said about opaqueness?

  2. Does dma_alloc_coherent always use __get_free_pages internally? Does this mean the region is potentially always over-allocated (since first function takes bytes but second function allocates in units of pages)?

  3. Is there a way to setup a single streaming mapping for multiple consecutive pages obtained from call to __get_free_pages? dma_map_page applies to only single pages.

Answer

jleahy picture jleahy · Aug 23, 2012
  1. No, the address returned is a virtual address, otherwise you wouldn't be able to access it from kernel space. It's dma_handle which represents the physical address, but it's opaque. You need to use virt_to_phys on the address it returns and then pass this to remap_pfn_range.

  2. I don't believe it does (it's likely to be platform dependent though), but it does allocate pages. If you want smaller amounts of memory for DMA you should use dma_pool_create and then allocate regions from there.

  3. You can use dma_map_single instead of dma_map_page.

I'd recommend consulting DMA-API.txt for more details on some of this stuff.