dmaengine.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. }
  73. return dma_async_is_complete(cookie, complete, used);
  74. }
  75. static inline void dma_set_residue(struct dma_tx_state *state, u32 residue)
  76. {
  77. if (state)
  78. state->residue = residue;
  79. }
  80. struct dmaengine_desc_callback {
  81. dma_async_tx_callback callback;
  82. dma_async_tx_callback_result callback_result;
  83. void *callback_param;
  84. };
  85. /**
  86. * dmaengine_desc_get_callback - get the passed in callback function
  87. * @tx: tx descriptor
  88. * @cb: temp struct to hold the callback info
  89. *
  90. * Fill the passed in cb struct with what's available in the passed in
  91. * tx descriptor struct
  92. * No locking is required.
  93. */
  94. static inline void
  95. dmaengine_desc_get_callback(struct dma_async_tx_descriptor *tx,
  96. struct dmaengine_desc_callback *cb)
  97. {
  98. cb->callback = tx->callback;
  99. cb->callback_result = tx->callback_result;
  100. cb->callback_param = tx->callback_param;
  101. }
  102. /**
  103. * dmaengine_desc_callback_invoke - call the callback function in cb struct
  104. * @cb: temp struct that is holding the callback info
  105. * @result: transaction result
  106. *
  107. * Call the callback function provided in the cb struct with the parameter
  108. * in the cb struct.
  109. * Locking is dependent on the driver.
  110. */
  111. static inline void
  112. dmaengine_desc_callback_invoke(struct dmaengine_desc_callback *cb,
  113. const struct dmaengine_result *result)
  114. {
  115. struct dmaengine_result dummy_result = {
  116. .result = DMA_TRANS_NOERROR,
  117. .residue = 0
  118. };
  119. if (cb->callback_result) {
  120. if (!result)
  121. result = &dummy_result;
  122. cb->callback_result(cb->callback_param, result);
  123. } else if (cb->callback) {
  124. cb->callback(cb->callback_param);
  125. }
  126. }
  127. /**
  128. * dmaengine_desc_get_callback_invoke - get the callback in tx descriptor and
  129. * then immediately call the callback.
  130. * @tx: dma async tx descriptor
  131. * @result: transaction result
  132. *
  133. * Call dmaengine_desc_get_callback() and dmaengine_desc_callback_invoke()
  134. * in a single function since no work is necessary in between for the driver.
  135. * Locking is dependent on the driver.
  136. */
  137. static inline void
  138. dmaengine_desc_get_callback_invoke(struct dma_async_tx_descriptor *tx,
  139. const struct dmaengine_result *result)
  140. {
  141. struct dmaengine_desc_callback cb;
  142. dmaengine_desc_get_callback(tx, &cb);
  143. dmaengine_desc_callback_invoke(&cb, result);
  144. }
  145. /**
  146. * dmaengine_desc_callback_valid - verify the callback is valid in cb
  147. * @cb: callback info struct
  148. *
  149. * Return a bool that verifies whether callback in cb is valid or not.
  150. * No locking is required.
  151. */
  152. static inline bool
  153. dmaengine_desc_callback_valid(struct dmaengine_desc_callback *cb)
  154. {
  155. return (cb->callback) ? true : false;
  156. }
  157. #endif