virt-dma.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <linux/device.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/module.h>
  13. #include <linux/spinlock.h>
  14. #include "virt-dma.h"
  15. static struct virt_dma_desc *to_virt_desc(struct dma_async_tx_descriptor *tx)
  16. {
  17. return container_of(tx, struct virt_dma_desc, tx);
  18. }
  19. dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *tx)
  20. {
  21. struct virt_dma_chan *vc = to_virt_chan(tx->chan);
  22. struct virt_dma_desc *vd = to_virt_desc(tx);
  23. unsigned long flags;
  24. dma_cookie_t cookie;
  25. spin_lock_irqsave(&vc->lock, flags);
  26. cookie = dma_cookie_assign(tx);
  27. list_add_tail(&vd->node, &vc->desc_submitted);
  28. spin_unlock_irqrestore(&vc->lock, flags);
  29. dev_dbg(vc->chan.device->dev, "vchan %p: txd %p[%x]: submitted\n",
  30. vc, vd, cookie);
  31. return cookie;
  32. }
  33. EXPORT_SYMBOL_GPL(vchan_tx_submit);
  34. struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *vc,
  35. dma_cookie_t cookie)
  36. {
  37. struct virt_dma_desc *vd;
  38. list_for_each_entry(vd, &vc->desc_issued, node)
  39. if (vd->tx.cookie == cookie)
  40. return vd;
  41. return NULL;
  42. }
  43. EXPORT_SYMBOL_GPL(vchan_find_desc);
  44. /*
  45. * This tasklet handles the completion of a DMA descriptor by
  46. * calling its callback and freeing it.
  47. */
  48. static void vchan_complete(unsigned long arg)
  49. {
  50. struct virt_dma_chan *vc = (struct virt_dma_chan *)arg;
  51. struct virt_dma_desc *vd;
  52. dma_async_tx_callback cb = NULL;
  53. void *cb_data = NULL;
  54. LIST_HEAD(head);
  55. spin_lock_irq(&vc->lock);
  56. list_splice_tail_init(&vc->desc_completed, &head);
  57. vd = vc->cyclic;
  58. if (vd) {
  59. vc->cyclic = NULL;
  60. cb = vd->tx.callback;
  61. cb_data = vd->tx.callback_param;
  62. }
  63. spin_unlock_irq(&vc->lock);
  64. if (cb)
  65. cb(cb_data);
  66. while (!list_empty(&head)) {
  67. vd = list_first_entry(&head, struct virt_dma_desc, node);
  68. cb = vd->tx.callback;
  69. cb_data = vd->tx.callback_param;
  70. list_del(&vd->node);
  71. vc->desc_free(vd);
  72. if (cb)
  73. cb(cb_data);
  74. }
  75. }
  76. void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head)
  77. {
  78. while (!list_empty(head)) {
  79. struct virt_dma_desc *vd = list_first_entry(head,
  80. struct virt_dma_desc, node);
  81. list_del(&vd->node);
  82. dev_dbg(vc->chan.device->dev, "txd %p: freeing\n", vd);
  83. vc->desc_free(vd);
  84. }
  85. }
  86. EXPORT_SYMBOL_GPL(vchan_dma_desc_free_list);
  87. void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev)
  88. {
  89. dma_cookie_init(&vc->chan);
  90. spin_lock_init(&vc->lock);
  91. INIT_LIST_HEAD(&vc->desc_submitted);
  92. INIT_LIST_HEAD(&vc->desc_issued);
  93. INIT_LIST_HEAD(&vc->desc_completed);
  94. tasklet_init(&vc->task, vchan_complete, (unsigned long)vc);
  95. vc->chan.device = dmadev;
  96. list_add_tail(&vc->chan.device_node, &dmadev->channels);
  97. }
  98. EXPORT_SYMBOL_GPL(vchan_init);
  99. MODULE_AUTHOR("Russell King");
  100. MODULE_LICENSE("GPL");