virt-dma.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_complete(&vd->tx);
  74. dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n",
  75. vd, vd->tx.cookie);
  76. list_add_tail(&vd->node, &vc->desc_completed);
  77. tasklet_schedule(&vc->task);
  78. }
  79. /**
  80. * vchan_cyclic_callback - report the completion of a period
  81. * vd: virtual descriptor
  82. */
  83. static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
  84. {
  85. struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
  86. vc->cyclic = vd;
  87. tasklet_schedule(&vc->task);
  88. }
  89. /**
  90. * vchan_next_desc - peek at the next descriptor to be processed
  91. * vc: virtual channel to obtain descriptor from
  92. *
  93. * vc.lock must be held by caller
  94. */
  95. static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
  96. {
  97. if (list_empty(&vc->desc_issued))
  98. return NULL;
  99. return list_first_entry(&vc->desc_issued, struct virt_dma_desc, node);
  100. }
  101. /**
  102. * vchan_get_all_descriptors - obtain all submitted and issued descriptors
  103. * vc: virtual channel to get descriptors from
  104. * head: list of descriptors found
  105. *
  106. * vc.lock must be held by caller
  107. *
  108. * Removes all submitted and issued descriptors from internal lists, and
  109. * provides a list of all descriptors found
  110. */
  111. static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
  112. struct list_head *head)
  113. {
  114. list_splice_tail_init(&vc->desc_submitted, head);
  115. list_splice_tail_init(&vc->desc_issued, head);
  116. list_splice_tail_init(&vc->desc_completed, head);
  117. }
  118. static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
  119. {
  120. unsigned long flags;
  121. LIST_HEAD(head);
  122. spin_lock_irqsave(&vc->lock, flags);
  123. vchan_get_all_descriptors(vc, &head);
  124. spin_unlock_irqrestore(&vc->lock, flags);
  125. vchan_dma_desc_free_list(vc, &head);
  126. }
  127. #endif