virt-dma.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Virtual DMA channel support for DMAengine
  3. *
  4. * Copyright (C) 2012 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #ifndef VIRT_DMA_H
  11. #define VIRT_DMA_H
  12. #include <linux/dmaengine.h>
  13. #include <linux/interrupt.h>
  14. #include "dmaengine.h"
  15. struct virt_dma_desc {
  16. struct dma_async_tx_descriptor tx;
  17. /* protected by vc.lock */
  18. struct list_head node;
  19. };
  20. struct virt_dma_chan {
  21. struct dma_chan chan;
  22. struct tasklet_struct task;
  23. void (*desc_free)(struct virt_dma_desc *);
  24. spinlock_t lock;
  25. /* protected by vc.lock */
  26. struct list_head desc_allocated;
  27. struct list_head desc_submitted;
  28. struct list_head desc_issued;
  29. struct list_head desc_completed;
  30. struct virt_dma_desc *cyclic;
  31. struct virt_dma_desc *vd_terminated;
  32. };
  33. static inline struct virt_dma_chan *to_virt_chan(struct dma_chan *chan)
  34. {
  35. return container_of(chan, struct virt_dma_chan, chan);
  36. }
  37. void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head);
  38. void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev);
  39. struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *, dma_cookie_t);
  40. extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *);
  41. extern int vchan_tx_desc_free(struct dma_async_tx_descriptor *);
  42. /**
  43. * vchan_tx_prep - prepare a descriptor
  44. * @vc: virtual channel allocating this descriptor
  45. * @vd: virtual descriptor to prepare
  46. * @tx_flags: flags argument passed in to prepare function
  47. */
  48. static inline struct dma_async_tx_descriptor *vchan_tx_prep(struct virt_dma_chan *vc,
  49. struct virt_dma_desc *vd, unsigned long tx_flags)
  50. {
  51. unsigned long flags;
  52. dma_async_tx_descriptor_init(&vd->tx, &vc->chan);
  53. vd->tx.flags = tx_flags;
  54. vd->tx.tx_submit = vchan_tx_submit;
  55. vd->tx.desc_free = vchan_tx_desc_free;
  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. list_add(&vd->node, &vc->desc_allocated);
  98. else
  99. vc->desc_free(vd);
  100. }
  101. /**
  102. * vchan_cyclic_callback - report the completion of a period
  103. * @vd: virtual descriptor
  104. */
  105. static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
  106. {
  107. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  108. vc->cyclic = vd;
  109. tasklet_schedule(&vc->task);
  110. }
  111. /**
  112. * vchan_terminate_vdesc - Disable pending cyclic callback
  113. * @vd: virtual descriptor to be terminated
  114. *
  115. * vc.lock must be held by caller
  116. */
  117. static inline void vchan_terminate_vdesc(struct virt_dma_desc *vd)
  118. {
  119. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  120. /* free up stuck descriptor */
  121. if (vc->vd_terminated)
  122. vchan_vdesc_fini(vc->vd_terminated);
  123. vc->vd_terminated = vd;
  124. if (vc->cyclic == vd)
  125. vc->cyclic = NULL;
  126. }
  127. /**
  128. * vchan_next_desc - peek at the next descriptor to be processed
  129. * @vc: virtual channel to obtain descriptor from
  130. *
  131. * vc.lock must be held by caller
  132. */
  133. static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
  134. {
  135. return list_first_entry_or_null(&vc->desc_issued,
  136. struct virt_dma_desc, node);
  137. }
  138. /**
  139. * vchan_get_all_descriptors - obtain all submitted and issued descriptors
  140. * @vc: virtual channel to get descriptors from
  141. * @head: list of descriptors found
  142. *
  143. * vc.lock must be held by caller
  144. *
  145. * Removes all submitted and issued descriptors from internal lists, and
  146. * provides a list of all descriptors found
  147. */
  148. static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
  149. struct list_head *head)
  150. {
  151. list_splice_tail_init(&vc->desc_allocated, head);
  152. list_splice_tail_init(&vc->desc_submitted, head);
  153. list_splice_tail_init(&vc->desc_issued, head);
  154. list_splice_tail_init(&vc->desc_completed, head);
  155. }
  156. static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
  157. {
  158. struct virt_dma_desc *vd;
  159. unsigned long flags;
  160. LIST_HEAD(head);
  161. spin_lock_irqsave(&vc->lock, flags);
  162. vchan_get_all_descriptors(vc, &head);
  163. list_for_each_entry(vd, &head, node)
  164. dmaengine_desc_clear_reuse(&vd->tx);
  165. spin_unlock_irqrestore(&vc->lock, flags);
  166. vchan_dma_desc_free_list(vc, &head);
  167. }
  168. /**
  169. * vchan_synchronize() - synchronize callback execution to the current context
  170. * @vc: virtual channel to synchronize
  171. *
  172. * Makes sure that all scheduled or active callbacks have finished running. For
  173. * proper operation the caller has to ensure that no new callbacks are scheduled
  174. * after the invocation of this function started.
  175. * Free up the terminated cyclic descriptor to prevent memory leakage.
  176. */
  177. static inline void vchan_synchronize(struct virt_dma_chan *vc)
  178. {
  179. unsigned long flags;
  180. tasklet_kill(&vc->task);
  181. spin_lock_irqsave(&vc->lock, flags);
  182. if (vc->vd_terminated) {
  183. vchan_vdesc_fini(vc->vd_terminated);
  184. vc->vd_terminated = NULL;
  185. }
  186. spin_unlock_irqrestore(&vc->lock, flags);
  187. }
  188. #endif