title: DMA memcpy operation in Linux
body: <p>I want to dma using dma_async_memcpy_buf_to_buf function which is in dmaengine.c file (linux/drivers/dma). For this, I add a function in dmatest.c file (linux/drivers/dma) as following:</p>
  
  <pre><code>void foo ()
  {
      int index = 0;
      dma_cookie_t cookie;
      size_t len = 0x20000;
  
      ktime_t start, end, end1, end2, end3;
      s64 actual_time;    
  
      u16* dest;
      u16* src;
  
      dest = kmalloc(len, GFP_KERNEL);
      src = kmalloc(len, GFP_KERNEL);
  
      for (index = 0; index &lt; len/2; index++)
      {
          dest[index] = 0xAA55;
          src[index] = 0xDEAD;
      }
  
      start = ktime_get();
      cookie = dma_async_memcpy_buf_to_buf(chan, dest, src, len);
  
      while (dma_async_is_tx_complete(chan, cookie, NULL, NULL) == DMA_IN_PROGRESS)
      {
         dma_sync_wait(chan, cookie);
      }
      end = ktime_get();
      actual_time = ktime_to_ns(ktime_sub(end, start));
      printk("Time taken for function() execution     dma: %lld\n",(long long)actual_time);   
  
      memset(dest, 0 , len);
  
      start = ktime_get();
      memcpy(dest, src, len);
  
      end = ktime_get();
      actual_time = ktime_to_ns(ktime_sub(end, start));
      printk("Time taken for function() execution non-dma: %lld\n",(long long)actual_time);
  }
  </code></pre>
  
  <p>There are some issues with DMA:</p>
  
  <ol>
  <li><p>Interestingly, memcpy function execution time is less than dma_async_memcpy_buf_to_buf function. Maybe, its related with  ktime_get() function problem.</p></li>
  <li><p>My method with foo function is correct or incorrect to perform DMA operation? I'm not sure about this.</p></li>
  <li><p>How can I measure tick counts of memcpy and dma_async_memcpy_buf_to_buf functions in terms of cpu usage</p></li>
  <li><p>Finally, Is DMA operation possible at application level? Up to now I used in kernel level as you can see above(dmatest.c is inserted kernel module)</p></li>
  </ol>