client.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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.rst``
  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. Cyclic
  59. interleaved DMA transfers are also possible if supported by the channel by
  60. setting the DMA_PREP_REPEAT transfer flag.
  61. A non-NULL return of this transfer API represents a "descriptor" for
  62. the given transaction.
  63. Interface:
  64. .. code-block:: c
  65. struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
  66. struct dma_chan *chan, struct scatterlist *sgl,
  67. unsigned int sg_len, enum dma_data_direction direction,
  68. unsigned long flags);
  69. struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
  70. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  71. size_t period_len, enum dma_data_direction direction);
  72. struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
  73. struct dma_chan *chan, struct dma_interleaved_template *xt,
  74. unsigned long flags);
  75. The peripheral driver is expected to have mapped the scatterlist for
  76. the DMA operation prior to calling dmaengine_prep_slave_sg(), and must
  77. keep the scatterlist mapped until the DMA operation has completed.
  78. The scatterlist must be mapped using the DMA struct device.
  79. If a mapping needs to be synchronized later, dma_sync_*_for_*() must be
  80. called using the DMA struct device, too.
  81. So, normal setup should look like this:
  82. .. code-block:: c
  83. struct device *dma_dev = dmaengine_get_dma_device(chan);
  84. nr_sg = dma_map_sg(dma_dev, sgl, sg_len);
  85. if (nr_sg == 0)
  86. /* error */
  87. desc = dmaengine_prep_slave_sg(chan, sgl, nr_sg, direction, flags);
  88. Once a descriptor has been obtained, the callback information can be
  89. added and the descriptor must then be submitted. Some DMA engine
  90. drivers may hold a spinlock between a successful preparation and
  91. submission so it is important that these two operations are closely
  92. paired.
  93. .. note::
  94. Although the async_tx API specifies that completion callback
  95. routines cannot submit any new operations, this is not the
  96. case for slave/cyclic DMA.
  97. For slave DMA, the subsequent transaction may not be available
  98. for submission prior to callback function being invoked, so
  99. slave DMA callbacks are permitted to prepare and submit a new
  100. transaction.
  101. For cyclic DMA, a callback function may wish to terminate the
  102. DMA via dmaengine_terminate_async().
  103. Therefore, it is important that DMA engine drivers drop any
  104. locks before calling the callback function which may cause a
  105. deadlock.
  106. Note that callbacks will always be invoked from the DMA
  107. engines tasklet, never from interrupt context.
  108. **Optional: per descriptor metadata**
  109. DMAengine provides two ways for metadata support.
  110. DESC_METADATA_CLIENT
  111. The metadata buffer is allocated/provided by the client driver and it is
  112. attached to the descriptor.
  113. .. code-block:: c
  114. int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
  115. void *data, size_t len);
  116. DESC_METADATA_ENGINE
  117. The metadata buffer is allocated/managed by the DMA driver. The client
  118. driver can ask for the pointer, maximum size and the currently used size of
  119. the metadata and can directly update or read it.
  120. Becasue the DMA driver manages the memory area containing the metadata,
  121. clients must make sure that they do not try to access or get the pointer
  122. after their transfer completion callback has run for the descriptor.
  123. If no completion callback has been defined for the transfer, then the
  124. metadata must not be accessed after issue_pending.
  125. In other words: if the aim is to read back metadata after the transfer is
  126. completed, then the client must use completion callback.
  127. .. code-block:: c
  128. void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
  129. size_t *payload_len, size_t *max_len);
  130. int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
  131. size_t payload_len);
  132. Client drivers can query if a given mode is supported with:
  133. .. code-block:: c
  134. bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan,
  135. enum dma_desc_metadata_mode mode);
  136. Depending on the used mode client drivers must follow different flow.
  137. DESC_METADATA_CLIENT
  138. - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
  139. 1. prepare the descriptor (dmaengine_prep_*)
  140. construct the metadata in the client's buffer
  141. 2. use dmaengine_desc_attach_metadata() to attach the buffer to the
  142. descriptor
  143. 3. submit the transfer
  144. - DMA_DEV_TO_MEM:
  145. 1. prepare the descriptor (dmaengine_prep_*)
  146. 2. use dmaengine_desc_attach_metadata() to attach the buffer to the
  147. descriptor
  148. 3. submit the transfer
  149. 4. when the transfer is completed, the metadata should be available in the
  150. attached buffer
  151. DESC_METADATA_ENGINE
  152. - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
  153. 1. prepare the descriptor (dmaengine_prep_*)
  154. 2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the
  155. engine's metadata area
  156. 3. update the metadata at the pointer
  157. 4. use dmaengine_desc_set_metadata_len() to tell the DMA engine the
  158. amount of data the client has placed into the metadata buffer
  159. 5. submit the transfer
  160. - DMA_DEV_TO_MEM:
  161. 1. prepare the descriptor (dmaengine_prep_*)
  162. 2. submit the transfer
  163. 3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get
  164. the pointer to the engine's metadata area
  165. 4. read out the metadata from the pointer
  166. .. note::
  167. When DESC_METADATA_ENGINE mode is used the metadata area for the descriptor
  168. is no longer valid after the transfer has been completed (valid up to the
  169. point when the completion callback returns if used).
  170. Mixed use of DESC_METADATA_CLIENT / DESC_METADATA_ENGINE is not allowed,
  171. client drivers must use either of the modes per descriptor.
  172. 4. Submit the transaction
  173. Once the descriptor has been prepared and the callback information
  174. added, it must be placed on the DMA engine drivers pending queue.
  175. Interface:
  176. .. code-block:: c
  177. dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
  178. This returns a cookie can be used to check the progress of DMA engine
  179. activity via other DMA engine calls not covered in this document.
  180. dmaengine_submit() will not start the DMA operation, it merely adds
  181. it to the pending queue. For this, see step 5, dma_async_issue_pending.
  182. .. note::
  183. After calling ``dmaengine_submit()`` the submitted transfer descriptor
  184. (``struct dma_async_tx_descriptor``) belongs to the DMA engine.
  185. Consequently, the client must consider invalid the pointer to that
  186. descriptor.
  187. 5. Issue pending DMA requests and wait for callback notification
  188. The transactions in the pending queue can be activated by calling the
  189. issue_pending API. If channel is idle then the first transaction in
  190. queue is started and subsequent ones queued up.
  191. On completion of each DMA operation, the next in queue is started and
  192. a tasklet triggered. The tasklet will then call the client driver
  193. completion callback routine for notification, if set.
  194. Interface:
  195. .. code-block:: c
  196. void dma_async_issue_pending(struct dma_chan *chan);
  197. Further APIs
  198. ------------
  199. 1. Terminate APIs
  200. .. code-block:: c
  201. int dmaengine_terminate_sync(struct dma_chan *chan)
  202. int dmaengine_terminate_async(struct dma_chan *chan)
  203. int dmaengine_terminate_all(struct dma_chan *chan) /* DEPRECATED */
  204. This causes all activity for the DMA channel to be stopped, and may
  205. discard data in the DMA FIFO which hasn't been fully transferred.
  206. No callback functions will be called for any incomplete transfers.
  207. Two variants of this function are available.
  208. dmaengine_terminate_async() might not wait until the DMA has been fully
  209. stopped or until any running complete callbacks have finished. But it is
  210. possible to call dmaengine_terminate_async() from atomic context or from
  211. within a complete callback. dmaengine_synchronize() must be called before it
  212. is safe to free the memory accessed by the DMA transfer or free resources
  213. accessed from within the complete callback.
  214. dmaengine_terminate_sync() will wait for the transfer and any running
  215. complete callbacks to finish before it returns. But the function must not be
  216. called from atomic context or from within a complete callback.
  217. dmaengine_terminate_all() is deprecated and should not be used in new code.
  218. 2. Pause API
  219. .. code-block:: c
  220. int dmaengine_pause(struct dma_chan *chan)
  221. This pauses activity on the DMA channel without data loss.
  222. 3. Resume API
  223. .. code-block:: c
  224. int dmaengine_resume(struct dma_chan *chan)
  225. Resume a previously paused DMA channel. It is invalid to resume a
  226. channel which is not currently paused.
  227. 4. Check Txn complete
  228. .. code-block:: c
  229. enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
  230. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  231. This can be used to check the status of the channel. Please see
  232. the documentation in include/linux/dmaengine.h for a more complete
  233. description of this API.
  234. This can be used in conjunction with dma_async_is_complete() and
  235. the cookie returned from dmaengine_submit() to check for
  236. completion of a specific DMA transaction.
  237. .. note::
  238. Not all DMA engine drivers can return reliable information for
  239. a running DMA channel. It is recommended that DMA engine users
  240. pause or stop (via dmaengine_terminate_all()) the channel before
  241. using this API.
  242. 5. Synchronize termination API
  243. .. code-block:: c
  244. void dmaengine_synchronize(struct dma_chan *chan)
  245. Synchronize the termination of the DMA channel to the current context.
  246. This function should be used after dmaengine_terminate_async() to synchronize
  247. the termination of the DMA channel to the current context. The function will
  248. wait for the transfer and any running complete callbacks to finish before it
  249. returns.
  250. If dmaengine_terminate_async() is used to stop the DMA channel this function
  251. must be called before it is safe to free memory accessed by previously
  252. submitted descriptors or to free any resources accessed within the complete
  253. callback of previously submitted descriptors.
  254. The behavior of this function is undefined if dma_async_issue_pending() has
  255. been called between dmaengine_terminate_async() and this function.