virt-dma.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. };
  32. static inline struct virt_dma_chan *to_virt_chan(struct dma_chan *chan)
  33. {
  34. return container_of(chan, struct virt_dma_chan, chan);
  35. }
  36. void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head);
  37. void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev);
  38. struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *, dma_cookie_t);
  39. extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *);
  40. extern int vchan_tx_desc_free(struct dma_async_tx_descriptor *);
  41. /**
  42. * vchan_tx_prep - prepare a descriptor
  43. * @vc: virtual channel allocating this descriptor
  44. * @vd: virtual descriptor to prepare
  45. * @tx_flags: flags argument passed in to prepare function
  46. */
  47. static inline struct dma_async_tx_descriptor *vchan_tx_prep(struct virt_dma_chan *vc,
  48. struct virt_dma_desc *vd, unsigned long tx_flags)
  49. {
  50. unsigned long flags;
  51. dma_async_tx_descriptor_init(&vd->tx, &vc->chan);
  52. vd->tx.flags = tx_flags;
  53. vd->tx.tx_submit = vchan_tx_submit;
  54. vd->tx.desc_free = vchan_tx_desc_free;
  55. spin_lock_irqsave(&vc->lock, flags);
  56. list_add_tail(&vd->node, &vc->desc_allocated);
  57. spin_unlock_irqrestore(&vc->lock, flags);
  58. return &vd->tx;
  59. }
  60. /**
  61. * vchan_issue_pending - move submitted descriptors to issued list
  62. * @vc: virtual channel to update
  63. *
  64. * vc.lock must be held by caller
  65. */
  66. static inline bool vchan_issue_pending(struct virt_dma_chan *vc)
  67. {
  68. list_splice_tail_init(&vc->desc_submitted, &vc->desc_issued);
  69. return !list_empty(&vc->desc_issued);
  70. }
  71. /**
  72. * vchan_cookie_complete - report completion of a descriptor
  73. * @vd: virtual descriptor to update
  74. *
  75. * vc.lock must be held by caller
  76. */
  77. static inline void vchan_cookie_complete(struct virt_dma_desc *vd)
  78. {
  79. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  80. dma_cookie_t cookie;
  81. cookie = vd->tx.cookie;
  82. dma_cookie_complete(&vd->tx);
  83. dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n",
  84. vd, cookie);
  85. list_add_tail(&vd->node, &vc->desc_completed);
  86. tasklet_schedule(&vc->task);
  87. }
  88. /**
  89. * vchan_vdesc_fini - Free or reuse a descriptor
  90. * @vd: virtual descriptor to free/reuse
  91. */
  92. static inline void vchan_vdesc_fini(struct virt_dma_desc *vd)
  93. {
  94. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  95. if (dmaengine_desc_test_reuse(&vd->tx))
  96. list_add(&vd->node, &vc->desc_allocated);
  97. else
  98. vc->desc_free(vd);
  99. }
  100. /**
  101. * vchan_cyclic_callback - report the completion of a period
  102. * @vd: virtual descriptor
  103. */
  104. static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
  105. {
  106. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  107. vc->cyclic = vd;
  108. tasklet_schedule(&vc->task);
  109. }
  110. /**
  111. * vchan_next_desc - peek at the next descriptor to be processed
  112. * @vc: virtual channel to obtain descriptor from
  113. *
  114. * vc.lock must be held by caller
  115. */
  116. static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
  117. {
  118. return list_first_entry_or_null(&vc->desc_issued,
  119. struct virt_dma_desc, node);
  120. }
  121. /**
  122. * vchan_get_all_descriptors - obtain all submitted and issued descriptors
  123. * @vc: virtual channel to get descriptors from
  124. * @head: list of descriptors found
  125. *
  126. * vc.lock must be held by caller
  127. *
  128. * Removes all submitted and issued descriptors from internal lists, and
  129. * provides a list of all descriptors found
  130. */
  131. static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
  132. struct list_head *head)
  133. {
  134. list_splice_tail_init(&vc->desc_allocated, head);
  135. list_splice_tail_init(&vc->desc_submitted, head);
  136. list_splice_tail_init(&vc->desc_issued, head);
  137. list_splice_tail_init(&vc->desc_completed, head);
  138. }
  139. static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
  140. {
  141. struct virt_dma_desc *vd;
  142. unsigned long flags;
  143. LIST_HEAD(head);
  144. spin_lock_irqsave(&vc->lock, flags);
  145. vchan_get_all_descriptors(vc, &head);
  146. list_for_each_entry(vd, &head, node)
  147. dmaengine_desc_clear_reuse(&vd->tx);
  148. spin_unlock_irqrestore(&vc->lock, flags);
  149. vchan_dma_desc_free_list(vc, &head);
  150. }
  151. /**
  152. * vchan_synchronize() - synchronize callback execution to the current context
  153. * @vc: virtual channel to synchronize
  154. *
  155. * Makes sure that all scheduled or active callbacks have finished running. For
  156. * proper operation the caller has to ensure that no new callbacks are scheduled
  157. * after the invocation of this function started.
  158. */
  159. static inline void vchan_synchronize(struct virt_dma_chan *vc)
  160. {
  161. tasklet_kill(&vc->task);
  162. }
  163. #endif