virt-dma.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. list_splice_tail_init(&vc->desc_submitted, &vc->desc_issued);
  70. return !list_empty(&vc->desc_issued);
  71. }
  72. /**
  73. * vchan_cookie_complete - report completion of a descriptor
  74. * @vd: virtual descriptor to update
  75. *
  76. * vc.lock must be held by caller
  77. */
  78. static inline void vchan_cookie_complete(struct virt_dma_desc *vd)
  79. {
  80. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  81. dma_cookie_t cookie;
  82. cookie = vd->tx.cookie;
  83. dma_cookie_complete(&vd->tx);
  84. dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n",
  85. vd, cookie);
  86. list_add_tail(&vd->node, &vc->desc_completed);
  87. tasklet_schedule(&vc->task);
  88. }
  89. /**
  90. * vchan_vdesc_fini - Free or reuse a descriptor
  91. * @vd: virtual descriptor to free/reuse
  92. */
  93. static inline void vchan_vdesc_fini(struct virt_dma_desc *vd)
  94. {
  95. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  96. if (dmaengine_desc_test_reuse(&vd->tx)) {
  97. unsigned long flags;
  98. spin_lock_irqsave(&vc->lock, flags);
  99. list_add(&vd->node, &vc->desc_allocated);
  100. spin_unlock_irqrestore(&vc->lock, flags);
  101. } else {
  102. vc->desc_free(vd);
  103. }
  104. }
  105. /**
  106. * vchan_cyclic_callback - report the completion of a period
  107. * @vd: virtual descriptor
  108. */
  109. static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
  110. {
  111. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  112. vc->cyclic = vd;
  113. tasklet_schedule(&vc->task);
  114. }
  115. /**
  116. * vchan_terminate_vdesc - Disable pending cyclic callback
  117. * @vd: virtual descriptor to be terminated
  118. *
  119. * vc.lock must be held by caller
  120. */
  121. static inline void vchan_terminate_vdesc(struct virt_dma_desc *vd)
  122. {
  123. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  124. list_add_tail(&vd->node, &vc->desc_terminated);
  125. if (vc->cyclic == vd)
  126. vc->cyclic = NULL;
  127. }
  128. /**
  129. * vchan_next_desc - peek at the next descriptor to be processed
  130. * @vc: virtual channel to obtain descriptor from
  131. *
  132. * vc.lock must be held by caller
  133. */
  134. static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
  135. {
  136. return list_first_entry_or_null(&vc->desc_issued,
  137. struct virt_dma_desc, node);
  138. }
  139. /**
  140. * vchan_get_all_descriptors - obtain all submitted and issued descriptors
  141. * @vc: virtual channel to get descriptors from
  142. * @head: list of descriptors found
  143. *
  144. * vc.lock must be held by caller
  145. *
  146. * Removes all submitted and issued descriptors from internal lists, and
  147. * provides a list of all descriptors found
  148. */
  149. static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
  150. struct list_head *head)
  151. {
  152. list_splice_tail_init(&vc->desc_allocated, head);
  153. list_splice_tail_init(&vc->desc_submitted, head);
  154. list_splice_tail_init(&vc->desc_issued, head);
  155. list_splice_tail_init(&vc->desc_completed, head);
  156. list_splice_tail_init(&vc->desc_terminated, head);
  157. }
  158. static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
  159. {
  160. struct virt_dma_desc *vd;
  161. unsigned long flags;
  162. LIST_HEAD(head);
  163. spin_lock_irqsave(&vc->lock, flags);
  164. vchan_get_all_descriptors(vc, &head);
  165. list_for_each_entry(vd, &head, node)
  166. dmaengine_desc_clear_reuse(&vd->tx);
  167. spin_unlock_irqrestore(&vc->lock, flags);
  168. vchan_dma_desc_free_list(vc, &head);
  169. }
  170. /**
  171. * vchan_synchronize() - synchronize callback execution to the current context
  172. * @vc: virtual channel to synchronize
  173. *
  174. * Makes sure that all scheduled or active callbacks have finished running. For
  175. * proper operation the caller has to ensure that no new callbacks are scheduled
  176. * after the invocation of this function started.
  177. * Free up the terminated cyclic descriptor to prevent memory leakage.
  178. */
  179. static inline void vchan_synchronize(struct virt_dma_chan *vc)
  180. {
  181. LIST_HEAD(head);
  182. unsigned long flags;
  183. tasklet_kill(&vc->task);
  184. spin_lock_irqsave(&vc->lock, flags);
  185. list_splice_tail_init(&vc->desc_terminated, &head);
  186. spin_unlock_irqrestore(&vc->lock, flags);
  187. vchan_dma_desc_free_list(vc, &head);
  188. }
  189. #endif