简体   繁体   中英

Questions about dma_sync_single_for_cpu() / dma_sync_single_for_device()

My code uses stream DMA APIs to map an allocated buffer to DMA region like below:

void perform_dma(void *buffer)
{
    dma_map_single(buffer...  DMA_BIDIRECTIONAL);  <- map buffer to physical address

    ring_doorbell()  -> notify device to read DMA content

    // wait until DMA is done or wake up by interrupts 
    .....

    dma_unmap_single(... , DMA_BIDIRECTIONAL)   <- unmap buffer
}

The DMA direction is BIDIRECTIONAL and buffer is allocated, filled by caller before perform_dma() is called and freed after it is done.

Should I use

  • dma_sync_single_for_device() after dma_map_single() is called (but before notifying device to perform DMA)

and

  • dma_sync_single_for_cpu() right before dma_unmap_single() (as the buffer would be read by caller after perform_dma() ) ?

Yes. You can lookup in here in this KernelTLV presentation (slide 13). There's an explanation there regarding buffer responsibility.

In the example in the presentation it is said you (the driver) can alter the buffer content after dma_sync_single_for_cpu() and return ownership to the device by calling dma_sync_single_for_device() .

It's a question of ownership. dma_sync_single_for_device() grants ownership to the DMA controller where dma_sync_single_for_cpu() gains ownership back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM