dmaengine.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * The contents of this file are private to DMA engine drivers, and is not
  4. * part of the API to be used by DMA engine users.
  5. */
  6. #ifndef DMAENGINE_H
  7. #define DMAENGINE_H
  8. #include <linux/bug.h>
  9. #include <linux/dmaengine.h>
  10. /**
  11. * dma_cookie_init - initialize the cookies for a DMA channel
  12. * @chan: dma channel to initialize
  13. */
  14. static inline void dma_cookie_init(struct dma_chan *chan)
  15. {
  16. chan->cookie = DMA_MIN_COOKIE;
  17. chan->completed_cookie = DMA_MIN_COOKIE;
  18. }
  19. /**
  20. * dma_cookie_assign - assign a DMA engine cookie to the descriptor
  21. * @tx: descriptor needing cookie
  22. *
  23. * Assign a unique non-zero per-channel cookie to the descriptor.
  24. * Note: caller is expected to hold a lock to prevent concurrency.
  25. */
  26. static inline dma_cookie_t dma_cookie_assign(struct dma_async_tx_descriptor *tx)
  27. {
  28. struct dma_chan *chan = tx->chan;
  29. dma_cookie_t cookie;
  30. cookie = chan->cookie + 1;
  31. if (cookie < DMA_MIN_COOKIE)
  32. cookie = DMA_MIN_COOKIE;
  33. tx->cookie = chan->cookie = cookie;
  34. return cookie;
  35. }
  36. /**
  37. * dma_cookie_complete - complete a descriptor
  38. * @tx: descriptor to complete
  39. *
  40. * Mark this descriptor complete by updating the channels completed
  41. * cookie marker. Zero the descriptors cookie to prevent accidental
  42. * repeated completions.
  43. *
  44. * Note: caller is expected to hold a lock to prevent concurrency.
  45. */
  46. static inline void dma_cookie_complete(struct dma_async_tx_descriptor *tx)
  47. {
  48. BUG_ON(tx->cookie < DMA_MIN_COOKIE);
  49. tx->chan->completed_cookie = tx->cookie;
  50. tx->cookie = 0;
  51. }
  52. /**
  53. * dma_cookie_status - report cookie status
  54. * @chan: dma channel
  55. * @cookie: cookie we are interested in
  56. * @state: dma_tx_state structure to return last/used cookies
  57. *
  58. * Report the status of the cookie, filling in the state structure if
  59. * non-NULL. No locking is required.
  60. */
  61. static inline enum dma_status dma_cookie_status(struct dma_chan *chan,
  62. dma_cookie_t cookie, struct dma_tx_state *state)
  63. {
  64. dma_cookie_t used, complete;
  65. used = chan->cookie;
  66. complete = chan->completed_cookie;
  67. barrier();
  68. if (state) {
  69. state->last = complete;
  70. state->used = used;
  71. state->residue = 0;
  72. state->in_flight_bytes = 0;
  73. }
  74. return dma_async_is_complete(cookie, complete, used);
  75. }
  76. static inline void dma_set_residue(struct dma_tx_state *state, u32 residue)
  77. {
  78. if (state)
  79. state->residue = residue;
  80. }
  81. static inline void dma_set_in_flight_bytes(struct dma_tx_state *state,
  82. u32 in_flight_bytes)
  83. {
  84. if (state)
  85. state->in_flight_bytes = in_flight_bytes;
  86. }
  87. struct dmaengine_desc_callback {
  88. dma_async_tx_callback callback;
  89. dma_async_tx_callback_result callback_result;
  90. void *callback_param;
  91. };
  92. /**
  93. * dmaengine_desc_get_callback - get the passed in callback function
  94. * @tx: tx descriptor
  95. * @cb: temp struct to hold the callback info
  96. *
  97. * Fill the passed in cb struct with what's available in the passed in
  98. * tx descriptor struct
  99. * No locking is required.
  100. */
  101. static inline void
  102. dmaengine_desc_get_callback(struct dma_async_tx_descriptor *tx,
  103. struct dmaengine_desc_callback *cb)
  104. {
  105. cb->callback = tx->callback;
  106. cb->callback_result = tx->callback_result;
  107. cb->callback_param = tx->callback_param;
  108. }
  109. /**
  110. * dmaengine_desc_callback_invoke - call the callback function in cb struct
  111. * @cb: temp struct that is holding the callback info
  112. * @result: transaction result
  113. *
  114. * Call the callback function provided in the cb struct with the parameter
  115. * in the cb struct.
  116. * Locking is dependent on the driver.
  117. */
  118. static inline void
  119. dmaengine_desc_callback_invoke(struct dmaengine_desc_callback *cb,
  120. const struct dmaengine_result *result)
  121. {
  122. struct dmaengine_result dummy_result = {
  123. .result = DMA_TRANS_NOERROR,
  124. .residue = 0
  125. };
  126. if (cb->callback_result) {
  127. if (!result)
  128. result = &dummy_result;
  129. cb->callback_result(cb->callback_param, result);
  130. } else if (cb->callback) {
  131. cb->callback(cb->callback_param);
  132. }
  133. }
  134. /**
  135. * dmaengine_desc_get_callback_invoke - get the callback in tx descriptor and
  136. * then immediately call the callback.
  137. * @tx: dma async tx descriptor
  138. * @result: transaction result
  139. *
  140. * Call dmaengine_desc_get_callback() and dmaengine_desc_callback_invoke()
  141. * in a single function since no work is necessary in between for the driver.
  142. * Locking is dependent on the driver.
  143. */
  144. static inline void
  145. dmaengine_desc_get_callback_invoke(struct dma_async_tx_descriptor *tx,
  146. const struct dmaengine_result *result)
  147. {
  148. struct dmaengine_desc_callback cb;
  149. dmaengine_desc_get_callback(tx, &cb);
  150. dmaengine_desc_callback_invoke(&cb, result);
  151. }
  152. /**
  153. * dmaengine_desc_callback_valid - verify the callback is valid in cb
  154. * @cb: callback info struct
  155. *
  156. * Return a bool that verifies whether callback in cb is valid or not.
  157. * No locking is required.
  158. */
  159. static inline bool
  160. dmaengine_desc_callback_valid(struct dmaengine_desc_callback *cb)
  161. {
  162. return cb->callback || cb->callback_result;
  163. }
  164. struct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
  165. struct dma_chan *dma_get_any_slave_channel(struct dma_device *device);
  166. #ifdef CONFIG_DEBUG_FS
  167. #include <linux/debugfs.h>
  168. static inline struct dentry *
  169. dmaengine_get_debugfs_root(struct dma_device *dma_dev) {
  170. return dma_dev->dbg_dev_root;
  171. }
  172. #else
  173. struct dentry;
  174. static inline struct dentry *
  175. dmaengine_get_debugfs_root(struct dma_device *dma_dev)
  176. {
  177. return NULL;
  178. }
  179. #endif /* CONFIG_DEBUG_FS */
  180. #endif