virt-dma.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Virtual DMA channel support for DMAengine
  4. *
  5. * Copyright (C) 2012 Russell King
  6. */
  7. #ifndef VIRT_DMA_H
  8. #define VIRT_DMA_H
  9. #include <linux/dmaengine.h>
  10. #include <linux/interrupt.h>
  11. #include "dmaengine.h"
  12. struct virt_dma_desc {
  13. struct dma_async_tx_descriptor tx;
  14. struct dmaengine_result tx_result;
  15. /* protected by vc.lock */
  16. struct list_head node;
  17. };
  18. struct virt_dma_chan {
  19. struct dma_chan chan;
  20. struct tasklet_struct task;
  21. void (*desc_free)(struct virt_dma_desc *);
  22. spinlock_t lock;
  23. /* protected by vc.lock */
  24. struct list_head desc_allocated;
  25. struct list_head desc_submitted;
  26. struct list_head desc_issued;
  27. struct list_head desc_completed;
  28. struct list_head desc_terminated;
  29. struct virt_dma_desc *cyclic;
  30. };
  31. static inline struct virt_dma_chan *to_virt_chan(struct dma_chan *chan)
  32. {
  33. return container_of(chan, struct virt_dma_chan, chan);
  34. }
  35. void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head);
  36. void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev);
  37. struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *, dma_cookie_t);
  38. extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *);
  39. extern int vchan_tx_desc_free(struct dma_async_tx_descriptor *);
  40. /**
  41. * vchan_tx_prep - prepare a descriptor
  42. * @vc: virtual channel allocating this descriptor
  43. * @vd: virtual descriptor to prepare
  44. * @tx_flags: flags argument passed in to prepare function
  45. */
  46. static inline struct dma_async_tx_descriptor *vchan_tx_prep(struct virt_dma_chan *vc,
  47. struct virt_dma_desc *vd, unsigned long tx_flags)
  48. {
  49. unsigned long flags;
  50. dma_async_tx_descriptor_init(&vd->tx, &vc->chan);
  51. vd->tx.flags = tx_flags;
  52. vd->tx.tx_submit = vchan_tx_submit;
  53. vd->tx.desc_free = vchan_tx_desc_free;
  54. vd->tx_result.result = DMA_TRANS_NOERROR;
  55. vd->tx_result.residue = 0;
  56. spin_lock_irqsave(&vc->lock, flags);
  57. list_add_tail(&vd->node, &vc->desc_allocated);
  58. spin_unlock_irqrestore(&vc->lock, flags);
  59. return &vd->tx;
  60. }
  61. /**
  62. * vchan_issue_pending - move submitted descriptors to issued list
  63. * @vc: virtual channel to update
  64. *
  65. * vc.lock must be held by caller
  66. */
  67. static inline bool vchan_issue_pending(struct virt_dma_chan *vc)
  68. {
  69. lockdep_assert_held(&vc->lock);
  70. list_splice_tail_init(&vc->desc_submitted, &vc->desc_issued);
  71. return !list_empty(&vc->desc_issued);
  72. }
  73. /**
  74. * vchan_cookie_complete - report completion of a descriptor
  75. * @vd: virtual descriptor to update
  76. *
  77. * vc.lock must be held by caller
  78. */
  79. static inline void vchan_cookie_complete(struct virt_dma_desc *vd)
  80. {
  81. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  82. dma_cookie_t cookie;
  83. lockdep_assert_held(&vc->lock);
  84. cookie = vd->tx.cookie;
  85. dma_cookie_complete(&vd->tx);
  86. dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n",
  87. vd, cookie);
  88. list_add_tail(&vd->node, &vc->desc_completed);
  89. tasklet_schedule(&vc->task);
  90. }
  91. /**
  92. * vchan_vdesc_fini - Free or reuse a descriptor
  93. * @vd: virtual descriptor to free/reuse
  94. */
  95. static inline void vchan_vdesc_fini(struct virt_dma_desc *vd)
  96. {
  97. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  98. if (dmaengine_desc_test_reuse(&vd->tx)) {
  99. unsigned long flags;
  100. spin_lock_irqsave(&vc->lock, flags);
  101. list_add(&vd->node, &vc->desc_allocated);
  102. spin_unlock_irqrestore(&vc->lock, flags);
  103. } else {
  104. vc->desc_free(vd);
  105. }
  106. }
  107. /**
  108. * vchan_cyclic_callback - report the completion of a period
  109. * @vd: virtual descriptor
  110. */
  111. static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
  112. {
  113. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  114. vc->cyclic = vd;
  115. tasklet_schedule(&vc->task);
  116. }
  117. /**
  118. * vchan_terminate_vdesc - Disable pending cyclic callback
  119. * @vd: virtual descriptor to be terminated
  120. *
  121. * vc.lock must be held by caller
  122. */
  123. static inline void vchan_terminate_vdesc(struct virt_dma_desc *vd)
  124. {
  125. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  126. lockdep_assert_held(&vc->lock);
  127. list_add_tail(&vd->node, &vc->desc_terminated);
  128. if (vc->cyclic == vd)
  129. vc->cyclic = NULL;
  130. }
  131. /**
  132. * vchan_next_desc - peek at the next descriptor to be processed
  133. * @vc: virtual channel to obtain descriptor from
  134. *
  135. * vc.lock must be held by caller
  136. */
  137. static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
  138. {
  139. lockdep_assert_held(&vc->lock);
  140. return list_first_entry_or_null(&vc->desc_issued,
  141. struct virt_dma_desc, node);
  142. }
  143. /**
  144. * vchan_get_all_descriptors - obtain all submitted and issued descriptors
  145. * @vc: virtual channel to get descriptors from
  146. * @head: list of descriptors found
  147. *
  148. * vc.lock must be held by caller
  149. *
  150. * Removes all submitted and issued descriptors from internal lists, and
  151. * provides a list of all descriptors found
  152. */
  153. static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
  154. struct list_head *head)
  155. {
  156. lockdep_assert_held(&vc->lock);
  157. list_splice_tail_init(&vc->desc_allocated, head);
  158. list_splice_tail_init(&vc->desc_submitted, head);
  159. list_splice_tail_init(&vc->desc_issued, head);
  160. list_splice_tail_init(&vc->desc_completed, head);
  161. list_splice_tail_init(&vc->desc_terminated, head);
  162. }
  163. static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
  164. {
  165. struct virt_dma_desc *vd;
  166. unsigned long flags;
  167. LIST_HEAD(head);
  168. spin_lock_irqsave(&vc->lock, flags);
  169. vchan_get_all_descriptors(vc, &head);
  170. list_for_each_entry(vd, &head, node)
  171. dmaengine_desc_clear_reuse(&vd->tx);
  172. spin_unlock_irqrestore(&vc->lock, flags);
  173. vchan_dma_desc_free_list(vc, &head);
  174. }
  175. /**
  176. * vchan_synchronize() - synchronize callback execution to the current context
  177. * @vc: virtual channel to synchronize
  178. *
  179. * Makes sure that all scheduled or active callbacks have finished running. For
  180. * proper operation the caller has to ensure that no new callbacks are scheduled
  181. * after the invocation of this function started.
  182. * Free up the terminated cyclic descriptor to prevent memory leakage.
  183. */
  184. static inline void vchan_synchronize(struct virt_dma_chan *vc)
  185. {
  186. LIST_HEAD(head);
  187. unsigned long flags;
  188. tasklet_kill(&vc->task);
  189. spin_lock_irqsave(&vc->lock, flags);
  190. list_splice_tail_init(&vc->desc_terminated, &head);
  191. spin_unlock_irqrestore(&vc->lock, flags);
  192. vchan_dma_desc_free_list(vc, &head);
  193. }
  194. #endif