client.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ====================
  2. DMA Engine API Guide
  3. ====================
  4. Vinod Koul <vinod dot koul at intel.com>
  5. .. note:: For DMA Engine usage in async_tx please see:
  6. ``Documentation/crypto/async-tx-api.txt``
  7. Below is a guide to device driver writers on how to use the Slave-DMA API of the
  8. DMA Engine. This is applicable only for slave DMA usage only.
  9. DMA usage
  10. =========
  11. The slave DMA usage consists of following steps:
  12. - Allocate a DMA slave channel
  13. - Set slave and controller specific parameters
  14. - Get a descriptor for transaction
  15. - Submit the transaction
  16. - Issue pending requests and wait for callback notification
  17. The details of these operations are:
  18. 1. Allocate a DMA slave channel
  19. Channel allocation is slightly different in the slave DMA context,
  20. client drivers typically need a channel from a particular DMA
  21. controller only and even in some cases a specific channel is desired.
  22. To request a channel dma_request_chan() API is used.
  23. Interface:
  24. .. code-block:: c
  25. struct dma_chan *dma_request_chan(struct device *dev, const char *name);
  26. Which will find and return the ``name`` DMA channel associated with the 'dev'
  27. device. The association is done via DT, ACPI or board file based
  28. dma_slave_map matching table.
  29. A channel allocated via this interface is exclusive to the caller,
  30. until dma_release_channel() is called.
  31. 2. Set slave and controller specific parameters
  32. Next step is always to pass some specific information to the DMA
  33. driver. Most of the generic information which a slave DMA can use
  34. is in struct dma_slave_config. This allows the clients to specify
  35. DMA direction, DMA addresses, bus widths, DMA burst lengths etc
  36. for the peripheral.
  37. If some DMA controllers have more parameters to be sent then they
  38. should try to embed struct dma_slave_config in their controller
  39. specific structure. That gives flexibility to client to pass more
  40. parameters, if required.
  41. Interface:
  42. .. code-block:: c
  43. int dmaengine_slave_config(struct dma_chan *chan,
  44. struct dma_slave_config *config)
  45. Please see the dma_slave_config structure definition in dmaengine.h
  46. for a detailed explanation of the struct members. Please note
  47. that the 'direction' member will be going away as it duplicates the
  48. direction given in the prepare call.
  49. 3. Get a descriptor for transaction
  50. For slave usage the various modes of slave transfers supported by the
  51. DMA-engine are:
  52. - slave_sg: DMA a list of scatter gather buffers from/to a peripheral
  53. - dma_cyclic: Perform a cyclic DMA operation from/to a peripheral till the
  54. operation is explicitly stopped.
  55. - interleaved_dma: This is common to Slave as well as M2M clients. For slave
  56. address of devices' fifo could be already known to the driver.
  57. Various types of operations could be expressed by setting
  58. appropriate values to the 'dma_interleaved_template' members.
  59. A non-NULL return of this transfer API represents a "descriptor" for
  60. the given transaction.
  61. Interface:
  62. .. code-block:: c
  63. struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
  64. struct dma_chan *chan, struct scatterlist *sgl,
  65. unsigned int sg_len, enum dma_data_direction direction,
  66. unsigned long flags);
  67. struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
  68. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  69. size_t period_len, enum dma_data_direction direction);
  70. struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
  71. struct dma_chan *chan, struct dma_interleaved_template *xt,
  72. unsigned long flags);
  73. The peripheral driver is expected to have mapped the scatterlist for
  74. the DMA operation prior to calling dmaengine_prep_slave_sg(), and must
  75. keep the scatterlist mapped until the DMA operation has completed.
  76. The scatterlist must be mapped using the DMA struct device.
  77. If a mapping needs to be synchronized later, dma_sync_*_for_*() must be
  78. called using the DMA struct device, too.
  79. So, normal setup should look like this:
  80. .. code-block:: c
  81. nr_sg = dma_map_sg(chan->device->dev, sgl, sg_len);
  82. if (nr_sg == 0)
  83. /* error */
  84. desc = dmaengine_prep_slave_sg(chan, sgl, nr_sg, direction, flags);
  85. Once a descriptor has been obtained, the callback information can be
  86. added and the descriptor must then be submitted. Some DMA engine
  87. drivers may hold a spinlock between a successful preparation and
  88. submission so it is important that these two operations are closely
  89. paired.
  90. .. note::
  91. Although the async_tx API specifies that completion callback
  92. routines cannot submit any new operations, this is not the
  93. case for slave/cyclic DMA.
  94. For slave DMA, the subsequent transaction may not be available
  95. for submission prior to callback function being invoked, so
  96. slave DMA callbacks are permitted to prepare and submit a new
  97. transaction.
  98. For cyclic DMA, a callback function may wish to terminate the
  99. DMA via dmaengine_terminate_async().
  100. Therefore, it is important that DMA engine drivers drop any
  101. locks before calling the callback function which may cause a
  102. deadlock.
  103. Note that callbacks will always be invoked from the DMA
  104. engines tasklet, never from interrupt context.
  105. 4. Submit the transaction
  106. Once the descriptor has been prepared and the callback information
  107. added, it must be placed on the DMA engine drivers pending queue.
  108. Interface:
  109. .. code-block:: c
  110. dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
  111. This returns a cookie can be used to check the progress of DMA engine
  112. activity via other DMA engine calls not covered in this document.
  113. dmaengine_submit() will not start the DMA operation, it merely adds
  114. it to the pending queue. For this, see step 5, dma_async_issue_pending.
  115. 5. Issue pending DMA requests and wait for callback notification
  116. The transactions in the pending queue can be activated by calling the
  117. issue_pending API. If channel is idle then the first transaction in
  118. queue is started and subsequent ones queued up.
  119. On completion of each DMA operation, the next in queue is started and
  120. a tasklet triggered. The tasklet will then call the client driver
  121. completion callback routine for notification, if set.
  122. Interface:
  123. .. code-block:: c
  124. void dma_async_issue_pending(struct dma_chan *chan);
  125. Further APIs:
  126. -------------
  127. 1. Terminate APIs
  128. .. code-block:: c
  129. int dmaengine_terminate_sync(struct dma_chan *chan)
  130. int dmaengine_terminate_async(struct dma_chan *chan)
  131. int dmaengine_terminate_all(struct dma_chan *chan) /* DEPRECATED */
  132. This causes all activity for the DMA channel to be stopped, and may
  133. discard data in the DMA FIFO which hasn't been fully transferred.
  134. No callback functions will be called for any incomplete transfers.
  135. Two variants of this function are available.
  136. dmaengine_terminate_async() might not wait until the DMA has been fully
  137. stopped or until any running complete callbacks have finished. But it is
  138. possible to call dmaengine_terminate_async() from atomic context or from
  139. within a complete callback. dmaengine_synchronize() must be called before it
  140. is safe to free the memory accessed by the DMA transfer or free resources
  141. accessed from within the complete callback.
  142. dmaengine_terminate_sync() will wait for the transfer and any running
  143. complete callbacks to finish before it returns. But the function must not be
  144. called from atomic context or from within a complete callback.
  145. dmaengine_terminate_all() is deprecated and should not be used in new code.
  146. 2. Pause API
  147. .. code-block:: c
  148. int dmaengine_pause(struct dma_chan *chan)
  149. This pauses activity on the DMA channel without data loss.
  150. 3. Resume API
  151. .. code-block:: c
  152. int dmaengine_resume(struct dma_chan *chan)
  153. Resume a previously paused DMA channel. It is invalid to resume a
  154. channel which is not currently paused.
  155. 4. Check Txn complete
  156. .. code-block:: c
  157. enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
  158. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  159. This can be used to check the status of the channel. Please see
  160. the documentation in include/linux/dmaengine.h for a more complete
  161. description of this API.
  162. This can be used in conjunction with dma_async_is_complete() and
  163. the cookie returned from dmaengine_submit() to check for
  164. completion of a specific DMA transaction.
  165. .. note::
  166. Not all DMA engine drivers can return reliable information for
  167. a running DMA channel. It is recommended that DMA engine users
  168. pause or stop (via dmaengine_terminate_all()) the channel before
  169. using this API.
  170. 5. Synchronize termination API
  171. .. code-block:: c
  172. void dmaengine_synchronize(struct dma_chan *chan)
  173. Synchronize the termination of the DMA channel to the current context.
  174. This function should be used after dmaengine_terminate_async() to synchronize
  175. the termination of the DMA channel to the current context. The function will
  176. wait for the transfer and any running complete callbacks to finish before it
  177. returns.
  178. If dmaengine_terminate_async() is used to stop the DMA channel this function
  179. must be called before it is safe to free memory accessed by previously
  180. submitted descriptors or to free any resources accessed within the complete
  181. callback of previously submitted descriptors.
  182. The behavior of this function is undefined if dma_async_issue_pending() has
  183. been called between dmaengine_terminate_async() and this function.