client.rst 13 KB

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