virt-dma.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Virtual DMA channel support for DMAengine
  4. *
  5. * Copyright (C) 2012 Russell King
  6. */
  7. #include <linux/device.h>
  8. #include <linux/dmaengine.h>
  9. #include <linux/module.h>
  10. #include <linux/spinlock.h>
  11. #include "virt-dma.h"
  12. static struct virt_dma_desc *to_virt_desc(struct dma_async_tx_descriptor *tx)
  13. {
  14. return container_of(tx, struct virt_dma_desc, tx);
  15. }
  16. dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *tx)
  17. {
  18. struct virt_dma_chan *vc = to_virt_chan(tx->chan);
  19. struct virt_dma_desc *vd = to_virt_desc(tx);
  20. unsigned long flags;
  21. dma_cookie_t cookie;
  22. spin_lock_irqsave(&vc->lock, flags);
  23. cookie = dma_cookie_assign(tx);
  24. list_move_tail(&vd->node, &vc->desc_submitted);
  25. spin_unlock_irqrestore(&vc->lock, flags);
  26. dev_dbg(vc->chan.device->dev, "vchan %p: txd %p[%x]: submitted\n",
  27. vc, vd, cookie);
  28. return cookie;
  29. }
  30. EXPORT_SYMBOL_GPL(vchan_tx_submit);
  31. /**
  32. * vchan_tx_desc_free - free a reusable descriptor
  33. * @tx: the transfer
  34. *
  35. * This function frees a previously allocated reusable descriptor. The only
  36. * other way is to clear the DMA_CTRL_REUSE flag and submit one last time the
  37. * transfer.
  38. *
  39. * Returns 0 upon success
  40. */
  41. int vchan_tx_desc_free(struct dma_async_tx_descriptor *tx)
  42. {
  43. struct virt_dma_chan *vc = to_virt_chan(tx->chan);
  44. struct virt_dma_desc *vd = to_virt_desc(tx);
  45. unsigned long flags;
  46. spin_lock_irqsave(&vc->lock, flags);
  47. list_del(&vd->node);
  48. spin_unlock_irqrestore(&vc->lock, flags);
  49. dev_dbg(vc->chan.device->dev, "vchan %p: txd %p[%x]: freeing\n",
  50. vc, vd, vd->tx.cookie);
  51. vc->desc_free(vd);
  52. return 0;
  53. }
  54. EXPORT_SYMBOL_GPL(vchan_tx_desc_free);
  55. struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *vc,
  56. dma_cookie_t cookie)
  57. {
  58. struct virt_dma_desc *vd;
  59. list_for_each_entry(vd, &vc->desc_issued, node)
  60. if (vd->tx.cookie == cookie)
  61. return vd;
  62. return NULL;
  63. }
  64. EXPORT_SYMBOL_GPL(vchan_find_desc);
  65. /*
  66. * This tasklet handles the completion of a DMA descriptor by
  67. * calling its callback and freeing it.
  68. */
  69. static void vchan_complete(struct tasklet_struct *t)
  70. {
  71. struct virt_dma_chan *vc = from_tasklet(vc, t, task);
  72. struct virt_dma_desc *vd, *_vd;
  73. struct dmaengine_desc_callback cb;
  74. LIST_HEAD(head);
  75. spin_lock_irq(&vc->lock);
  76. list_splice_tail_init(&vc->desc_completed, &head);
  77. vd = vc->cyclic;
  78. if (vd) {
  79. vc->cyclic = NULL;
  80. dmaengine_desc_get_callback(&vd->tx, &cb);
  81. } else {
  82. memset(&cb, 0, sizeof(cb));
  83. }
  84. spin_unlock_irq(&vc->lock);
  85. dmaengine_desc_callback_invoke(&cb, &vd->tx_result);
  86. list_for_each_entry_safe(vd, _vd, &head, node) {
  87. dmaengine_desc_get_callback(&vd->tx, &cb);
  88. list_del(&vd->node);
  89. dmaengine_desc_callback_invoke(&cb, &vd->tx_result);
  90. vchan_vdesc_fini(vd);
  91. }
  92. }
  93. void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head)
  94. {
  95. struct virt_dma_desc *vd, *_vd;
  96. list_for_each_entry_safe(vd, _vd, head, node) {
  97. list_del(&vd->node);
  98. vchan_vdesc_fini(vd);
  99. }
  100. }
  101. EXPORT_SYMBOL_GPL(vchan_dma_desc_free_list);
  102. void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev)
  103. {
  104. dma_cookie_init(&vc->chan);
  105. spin_lock_init(&vc->lock);
  106. INIT_LIST_HEAD(&vc->desc_allocated);
  107. INIT_LIST_HEAD(&vc->desc_submitted);
  108. INIT_LIST_HEAD(&vc->desc_issued);
  109. INIT_LIST_HEAD(&vc->desc_completed);
  110. INIT_LIST_HEAD(&vc->desc_terminated);
  111. tasklet_setup(&vc->task, vchan_complete);
  112. vc->chan.device = dmadev;
  113. list_add_tail(&vc->chan.device_node, &dmadev->channels);
  114. }
  115. EXPORT_SYMBOL_GPL(vchan_init);
  116. MODULE_AUTHOR("Russell King");
  117. MODULE_LICENSE("GPL");