virt-dma.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. LIST_HEAD(head);
  52. spin_lock_irq(&vc->lock);
  53. list_splice_tail_init(&vc->desc_completed, &head);
  54. spin_unlock_irq(&vc->lock);
  55. while (!list_empty(&head)) {
  56. struct virt_dma_desc *vd = list_first_entry(&head,
  57. struct virt_dma_desc, node);
  58. dma_async_tx_callback cb = vd->tx.callback;
  59. void *cb_data = vd->tx.callback_param;
  60. list_del(&vd->node);
  61. vc->desc_free(vd);
  62. if (cb)
  63. cb(cb_data);
  64. }
  65. }
  66. void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head)
  67. {
  68. while (!list_empty(head)) {
  69. struct virt_dma_desc *vd = list_first_entry(head,
  70. struct virt_dma_desc, node);
  71. list_del(&vd->node);
  72. dev_dbg(vc->chan.device->dev, "txd %p: freeing\n", vd);
  73. vc->desc_free(vd);
  74. }
  75. }
  76. EXPORT_SYMBOL_GPL(vchan_dma_desc_free_list);
  77. void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev)
  78. {
  79. dma_cookie_init(&vc->chan);
  80. spin_lock_init(&vc->lock);
  81. INIT_LIST_HEAD(&vc->desc_submitted);
  82. INIT_LIST_HEAD(&vc->desc_issued);
  83. INIT_LIST_HEAD(&vc->desc_completed);
  84. tasklet_init(&vc->task, vchan_complete, (unsigned long)vc);
  85. vc->chan.device = dmadev;
  86. list_add_tail(&vc->chan.device_node, &dmadev->channels);
  87. }
  88. EXPORT_SYMBOL_GPL(vchan_init);
  89. MODULE_AUTHOR("Russell King");
  90. MODULE_LICENSE("GPL");