virt-dma.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_submitted;
  27. struct list_head desc_issued;
  28. struct list_head desc_completed;
  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. /**
  39. * vchan_tx_prep - prepare a descriptor
  40. * @vc: virtual channel allocating this descriptor
  41. * @vd: virtual descriptor to prepare
  42. * @tx_flags: flags argument passed in to prepare function
  43. */
  44. static inline struct dma_async_tx_descriptor *vchan_tx_prep(struct virt_dma_chan *vc,
  45. struct virt_dma_desc *vd, unsigned long tx_flags)
  46. {
  47. extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *);
  48. dma_async_tx_descriptor_init(&vd->tx, &vc->chan);
  49. vd->tx.flags = tx_flags;
  50. vd->tx.tx_submit = vchan_tx_submit;
  51. return &vd->tx;
  52. }
  53. /**
  54. * vchan_issue_pending - move submitted descriptors to issued list
  55. * @vc: virtual channel to update
  56. *
  57. * vc.lock must be held by caller
  58. */
  59. static inline bool vchan_issue_pending(struct virt_dma_chan *vc)
  60. {
  61. list_splice_tail_init(&vc->desc_submitted, &vc->desc_issued);
  62. return !list_empty(&vc->desc_issued);
  63. }
  64. /**
  65. * vchan_cookie_complete - report completion of a descriptor
  66. * @vd: virtual descriptor to update
  67. *
  68. * vc.lock must be held by caller
  69. */
  70. static inline void vchan_cookie_complete(struct virt_dma_desc *vd)
  71. {
  72. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  73. dma_cookie_t cookie;
  74. cookie = vd->tx.cookie;
  75. dma_cookie_complete(&vd->tx);
  76. dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n",
  77. vd, cookie);
  78. list_add_tail(&vd->node, &vc->desc_completed);
  79. tasklet_schedule(&vc->task);
  80. }
  81. /**
  82. * vchan_cyclic_callback - report the completion of a period
  83. * @vd: virtual descriptor
  84. */
  85. static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
  86. {
  87. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  88. vc->cyclic = vd;
  89. tasklet_schedule(&vc->task);
  90. }
  91. /**
  92. * vchan_next_desc - peek at the next descriptor to be processed
  93. * @vc: virtual channel to obtain descriptor from
  94. *
  95. * vc.lock must be held by caller
  96. */
  97. static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
  98. {
  99. if (list_empty(&vc->desc_issued))
  100. return NULL;
  101. return list_first_entry(&vc->desc_issued, struct virt_dma_desc, node);
  102. }
  103. /**
  104. * vchan_get_all_descriptors - obtain all submitted and issued descriptors
  105. * @vc: virtual channel to get descriptors from
  106. * @head: list of descriptors found
  107. *
  108. * vc.lock must be held by caller
  109. *
  110. * Removes all submitted and issued descriptors from internal lists, and
  111. * provides a list of all descriptors found
  112. */
  113. static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
  114. struct list_head *head)
  115. {
  116. list_splice_tail_init(&vc->desc_submitted, head);
  117. list_splice_tail_init(&vc->desc_issued, head);
  118. list_splice_tail_init(&vc->desc_completed, head);
  119. }
  120. static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
  121. {
  122. unsigned long flags;
  123. LIST_HEAD(head);
  124. spin_lock_irqsave(&vc->lock, flags);
  125. vchan_get_all_descriptors(vc, &head);
  126. spin_unlock_irqrestore(&vc->lock, flags);
  127. vchan_dma_desc_free_list(vc, &head);
  128. }
  129. #endif