virt-dma.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /*
  35. * This tasklet handles the completion of a DMA descriptor by
  36. * calling its callback and freeing it.
  37. */
  38. static void vchan_complete(unsigned long arg)
  39. {
  40. struct virt_dma_chan *vc = (struct virt_dma_chan *)arg;
  41. LIST_HEAD(head);
  42. spin_lock_irq(&vc->lock);
  43. list_splice_tail_init(&vc->desc_completed, &head);
  44. spin_unlock_irq(&vc->lock);
  45. while (!list_empty(&head)) {
  46. struct virt_dma_desc *vd = list_first_entry(&head,
  47. struct virt_dma_desc, node);
  48. dma_async_tx_callback cb = vd->tx.callback;
  49. void *cb_data = vd->tx.callback_param;
  50. list_del(&vd->node);
  51. vc->desc_free(vd);
  52. if (cb)
  53. cb(cb_data);
  54. }
  55. }
  56. void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head)
  57. {
  58. while (!list_empty(head)) {
  59. struct virt_dma_desc *vd = list_first_entry(head,
  60. struct virt_dma_desc, node);
  61. list_del(&vd->node);
  62. dev_dbg(vc->chan.device->dev, "txd %p: freeing\n", vd);
  63. vc->desc_free(vd);
  64. }
  65. }
  66. EXPORT_SYMBOL_GPL(vchan_dma_desc_free_list);
  67. void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev)
  68. {
  69. dma_cookie_init(&vc->chan);
  70. spin_lock_init(&vc->lock);
  71. INIT_LIST_HEAD(&vc->desc_submitted);
  72. INIT_LIST_HEAD(&vc->desc_issued);
  73. INIT_LIST_HEAD(&vc->desc_completed);
  74. tasklet_init(&vc->task, vchan_complete, (unsigned long)vc);
  75. vc->chan.device = dmadev;
  76. list_add_tail(&vc->chan.device_node, &dmadev->channels);
  77. }
  78. EXPORT_SYMBOL_GPL(vchan_init);
  79. MODULE_AUTHOR("Russell King");
  80. MODULE_LICENSE("GPL");