virt-dma.h 3.5 KB

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