2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-04-13 19:07:23 +08:00
|
|
|
/*
|
|
|
|
* Virtual DMA channel support for DMAengine
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Russell King
|
|
|
|
*/
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/dmaengine.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
|
|
|
|
#include "virt-dma.h"
|
|
|
|
|
|
|
|
static struct virt_dma_desc *to_virt_desc(struct dma_async_tx_descriptor *tx)
|
|
|
|
{
|
|
|
|
return container_of(tx, struct virt_dma_desc, tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *tx)
|
|
|
|
{
|
|
|
|
struct virt_dma_chan *vc = to_virt_chan(tx->chan);
|
|
|
|
struct virt_dma_desc *vd = to_virt_desc(tx);
|
|
|
|
unsigned long flags;
|
|
|
|
dma_cookie_t cookie;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vc->lock, flags);
|
|
|
|
cookie = dma_cookie_assign(tx);
|
|
|
|
|
2015-10-14 03:54:28 +08:00
|
|
|
list_move_tail(&vd->node, &vc->desc_submitted);
|
2012-04-13 19:07:23 +08:00
|
|
|
spin_unlock_irqrestore(&vc->lock, flags);
|
|
|
|
|
|
|
|
dev_dbg(vc->chan.device->dev, "vchan %p: txd %p[%x]: submitted\n",
|
|
|
|
vc, vd, cookie);
|
|
|
|
|
|
|
|
return cookie;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vchan_tx_submit);
|
|
|
|
|
2015-10-14 03:54:28 +08:00
|
|
|
/**
|
|
|
|
* vchan_tx_desc_free - free a reusable descriptor
|
|
|
|
* @tx: the transfer
|
|
|
|
*
|
|
|
|
* This function frees a previously allocated reusable descriptor. The only
|
|
|
|
* other way is to clear the DMA_CTRL_REUSE flag and submit one last time the
|
|
|
|
* transfer.
|
|
|
|
*
|
|
|
|
* Returns 0 upon success
|
|
|
|
*/
|
|
|
|
int vchan_tx_desc_free(struct dma_async_tx_descriptor *tx)
|
|
|
|
{
|
|
|
|
struct virt_dma_chan *vc = to_virt_chan(tx->chan);
|
|
|
|
struct virt_dma_desc *vd = to_virt_desc(tx);
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vc->lock, flags);
|
|
|
|
list_del(&vd->node);
|
|
|
|
spin_unlock_irqrestore(&vc->lock, flags);
|
|
|
|
|
|
|
|
dev_dbg(vc->chan.device->dev, "vchan %p: txd %p[%x]: freeing\n",
|
|
|
|
vc, vd, vd->tx.cookie);
|
|
|
|
vc->desc_free(vd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vchan_tx_desc_free);
|
|
|
|
|
2012-05-11 06:39:27 +08:00
|
|
|
struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *vc,
|
|
|
|
dma_cookie_t cookie)
|
|
|
|
{
|
|
|
|
struct virt_dma_desc *vd;
|
|
|
|
|
|
|
|
list_for_each_entry(vd, &vc->desc_issued, node)
|
|
|
|
if (vd->tx.cookie == cookie)
|
|
|
|
return vd;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vchan_find_desc);
|
|
|
|
|
2012-04-13 19:07:23 +08:00
|
|
|
/*
|
|
|
|
* This tasklet handles the completion of a DMA descriptor by
|
|
|
|
* calling its callback and freeing it.
|
|
|
|
*/
|
2020-08-31 18:35:37 +08:00
|
|
|
static void vchan_complete(struct tasklet_struct *t)
|
2012-04-13 19:07:23 +08:00
|
|
|
{
|
2020-08-31 18:35:37 +08:00
|
|
|
struct virt_dma_chan *vc = from_tasklet(vc, t, task);
|
2017-04-26 03:36:38 +08:00
|
|
|
struct virt_dma_desc *vd, *_vd;
|
2016-07-21 04:13:33 +08:00
|
|
|
struct dmaengine_desc_callback cb;
|
2012-04-13 19:07:23 +08:00
|
|
|
LIST_HEAD(head);
|
|
|
|
|
|
|
|
spin_lock_irq(&vc->lock);
|
|
|
|
list_splice_tail_init(&vc->desc_completed, &head);
|
2012-05-14 22:17:20 +08:00
|
|
|
vd = vc->cyclic;
|
|
|
|
if (vd) {
|
|
|
|
vc->cyclic = NULL;
|
2016-07-21 04:13:33 +08:00
|
|
|
dmaengine_desc_get_callback(&vd->tx, &cb);
|
|
|
|
} else {
|
|
|
|
memset(&cb, 0, sizeof(cb));
|
2012-05-14 22:17:20 +08:00
|
|
|
}
|
2012-04-13 19:07:23 +08:00
|
|
|
spin_unlock_irq(&vc->lock);
|
|
|
|
|
2019-06-06 18:45:47 +08:00
|
|
|
dmaengine_desc_callback_invoke(&cb, &vd->tx_result);
|
2012-05-14 22:17:20 +08:00
|
|
|
|
2017-04-26 03:36:38 +08:00
|
|
|
list_for_each_entry_safe(vd, _vd, &head, node) {
|
2016-07-21 04:13:33 +08:00
|
|
|
dmaengine_desc_get_callback(&vd->tx, &cb);
|
2012-04-13 19:07:23 +08:00
|
|
|
|
|
|
|
list_del(&vd->node);
|
2019-06-06 18:45:47 +08:00
|
|
|
dmaengine_desc_callback_invoke(&cb, &vd->tx_result);
|
2019-12-20 21:11:00 +08:00
|
|
|
vchan_vdesc_fini(vd);
|
2012-04-13 19:07:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head)
|
|
|
|
{
|
2017-04-26 03:36:38 +08:00
|
|
|
struct virt_dma_desc *vd, *_vd;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(vd, _vd, head, node) {
|
2019-12-16 18:53:25 +08:00
|
|
|
list_del(&vd->node);
|
|
|
|
vchan_vdesc_fini(vd);
|
2012-04-13 19:07:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vchan_dma_desc_free_list);
|
|
|
|
|
|
|
|
void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev)
|
|
|
|
{
|
|
|
|
dma_cookie_init(&vc->chan);
|
|
|
|
|
|
|
|
spin_lock_init(&vc->lock);
|
2015-10-14 03:54:28 +08:00
|
|
|
INIT_LIST_HEAD(&vc->desc_allocated);
|
2012-04-13 19:07:23 +08:00
|
|
|
INIT_LIST_HEAD(&vc->desc_submitted);
|
|
|
|
INIT_LIST_HEAD(&vc->desc_issued);
|
|
|
|
INIT_LIST_HEAD(&vc->desc_completed);
|
2019-12-16 18:53:23 +08:00
|
|
|
INIT_LIST_HEAD(&vc->desc_terminated);
|
2012-04-13 19:07:23 +08:00
|
|
|
|
2020-08-31 18:35:37 +08:00
|
|
|
tasklet_setup(&vc->task, vchan_complete);
|
2012-04-13 19:07:23 +08:00
|
|
|
|
|
|
|
vc->chan.device = dmadev;
|
|
|
|
list_add_tail(&vc->chan.device_node, &dmadev->channels);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vchan_init);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Russell King");
|
|
|
|
MODULE_LICENSE("GPL");
|