virt-dma.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_cyclic_callback - report the completion of a period
  90. * @vd: virtual descriptor
  91. */
  92. static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
  93. {
  94. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  95. vc->cyclic = vd;
  96. tasklet_schedule(&vc->task);
  97. }
  98. /**
  99. * vchan_next_desc - peek at the next descriptor to be processed
  100. * @vc: virtual channel to obtain descriptor from
  101. *
  102. * vc.lock must be held by caller
  103. */
  104. static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
  105. {
  106. return list_first_entry_or_null(&vc->desc_issued,
  107. struct virt_dma_desc, node);
  108. }
  109. /**
  110. * vchan_get_all_descriptors - obtain all submitted and issued descriptors
  111. * @vc: virtual channel to get descriptors from
  112. * @head: list of descriptors found
  113. *
  114. * vc.lock must be held by caller
  115. *
  116. * Removes all submitted and issued descriptors from internal lists, and
  117. * provides a list of all descriptors found
  118. */
  119. static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
  120. struct list_head *head)
  121. {
  122. list_splice_tail_init(&vc->desc_allocated, head);
  123. list_splice_tail_init(&vc->desc_submitted, head);
  124. list_splice_tail_init(&vc->desc_issued, head);
  125. list_splice_tail_init(&vc->desc_completed, head);
  126. }
  127. static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
  128. {
  129. struct virt_dma_desc *vd;
  130. unsigned long flags;
  131. LIST_HEAD(head);
  132. spin_lock_irqsave(&vc->lock, flags);
  133. vchan_get_all_descriptors(vc, &head);
  134. list_for_each_entry(vd, &head, node)
  135. dmaengine_desc_clear_reuse(&vd->tx);
  136. spin_unlock_irqrestore(&vc->lock, flags);
  137. vchan_dma_desc_free_list(vc, &head);
  138. }
  139. /**
  140. * vchan_synchronize() - synchronize callback execution to the current context
  141. * @vc: virtual channel to synchronize
  142. *
  143. * Makes sure that all scheduled or active callbacks have finished running. For
  144. * proper operation the caller has to ensure that no new callbacks are scheduled
  145. * after the invocation of this function started.
  146. */
  147. static inline void vchan_synchronize(struct virt_dma_chan *vc)
  148. {
  149. tasklet_kill(&vc->task);
  150. }
  151. #endif