blob: 3f776a46a29c419541ea8648eb6220c9a0d710e3 [file] [log] [blame]
Russell King50437bf2012-04-13 12:07:23 +01001/*
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#ifndef VIRT_DMA_H
11#define VIRT_DMA_H
12
13#include <linux/dmaengine.h>
14#include <linux/interrupt.h>
15
16#include "dmaengine.h"
17
18struct virt_dma_desc {
19 struct dma_async_tx_descriptor tx;
20 /* protected by vc.lock */
21 struct list_head node;
22};
23
24struct virt_dma_chan {
25 struct dma_chan chan;
26 struct tasklet_struct task;
27 void (*desc_free)(struct virt_dma_desc *);
28
29 spinlock_t lock;
30
31 /* protected by vc.lock */
Robert Jarzmik13bb26a2015-10-13 21:54:28 +020032 struct list_head desc_allocated;
Russell King50437bf2012-04-13 12:07:23 +010033 struct list_head desc_submitted;
34 struct list_head desc_issued;
35 struct list_head desc_completed;
Russell King571fa742012-05-14 15:17:20 +010036
37 struct virt_dma_desc *cyclic;
Russell King50437bf2012-04-13 12:07:23 +010038};
39
40static inline struct virt_dma_chan *to_virt_chan(struct dma_chan *chan)
41{
42 return container_of(chan, struct virt_dma_chan, chan);
43}
44
45void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head);
Russell King50437bf2012-04-13 12:07:23 +010046void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev);
Russell Kingfe045872012-05-10 23:39:27 +010047struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *, dma_cookie_t);
Baoyou Xie02aa8482016-09-24 12:37:05 +080048extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *);
49extern int vchan_tx_desc_free(struct dma_async_tx_descriptor *);
Russell King50437bf2012-04-13 12:07:23 +010050
51/**
52 * vchan_tx_prep - prepare a descriptor
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +020053 * @vc: virtual channel allocating this descriptor
54 * @vd: virtual descriptor to prepare
55 * @tx_flags: flags argument passed in to prepare function
Russell King50437bf2012-04-13 12:07:23 +010056 */
57static inline struct dma_async_tx_descriptor *vchan_tx_prep(struct virt_dma_chan *vc,
58 struct virt_dma_desc *vd, unsigned long tx_flags)
59{
Robert Jarzmik13bb26a2015-10-13 21:54:28 +020060 unsigned long flags;
Russell King50437bf2012-04-13 12:07:23 +010061
62 dma_async_tx_descriptor_init(&vd->tx, &vc->chan);
63 vd->tx.flags = tx_flags;
64 vd->tx.tx_submit = vchan_tx_submit;
Robert Jarzmik13bb26a2015-10-13 21:54:28 +020065 vd->tx.desc_free = vchan_tx_desc_free;
66
67 spin_lock_irqsave(&vc->lock, flags);
68 list_add_tail(&vd->node, &vc->desc_allocated);
69 spin_unlock_irqrestore(&vc->lock, flags);
Russell King50437bf2012-04-13 12:07:23 +010070
71 return &vd->tx;
72}
73
74/**
75 * vchan_issue_pending - move submitted descriptors to issued list
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +020076 * @vc: virtual channel to update
Russell King50437bf2012-04-13 12:07:23 +010077 *
78 * vc.lock must be held by caller
79 */
80static inline bool vchan_issue_pending(struct virt_dma_chan *vc)
81{
82 list_splice_tail_init(&vc->desc_submitted, &vc->desc_issued);
83 return !list_empty(&vc->desc_issued);
84}
85
86/**
87 * vchan_cookie_complete - report completion of a descriptor
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +020088 * @vd: virtual descriptor to update
Russell King50437bf2012-04-13 12:07:23 +010089 *
90 * vc.lock must be held by caller
91 */
92static inline void vchan_cookie_complete(struct virt_dma_desc *vd)
93{
94 struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
Jonas Jensenaf586522013-12-06 16:42:09 +010095 dma_cookie_t cookie;
Russell King50437bf2012-04-13 12:07:23 +010096
Jonas Jensenaf586522013-12-06 16:42:09 +010097 cookie = vd->tx.cookie;
Russell King50437bf2012-04-13 12:07:23 +010098 dma_cookie_complete(&vd->tx);
99 dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n",
Jonas Jensenaf586522013-12-06 16:42:09 +0100100 vd, cookie);
Russell King50437bf2012-04-13 12:07:23 +0100101 list_add_tail(&vd->node, &vc->desc_completed);
102
103 tasklet_schedule(&vc->task);
104}
105
106/**
Russell King571fa742012-05-14 15:17:20 +0100107 * vchan_cyclic_callback - report the completion of a period
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +0200108 * @vd: virtual descriptor
Russell King571fa742012-05-14 15:17:20 +0100109 */
110static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
111{
112 struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
113
114 vc->cyclic = vd;
115 tasklet_schedule(&vc->task);
116}
117
118/**
Russell King50437bf2012-04-13 12:07:23 +0100119 * vchan_next_desc - peek at the next descriptor to be processed
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +0200120 * @vc: virtual channel to obtain descriptor from
Russell King50437bf2012-04-13 12:07:23 +0100121 *
122 * vc.lock must be held by caller
123 */
124static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
125{
Masahiro Yamada360af352016-09-13 03:08:17 +0900126 return list_first_entry_or_null(&vc->desc_issued,
127 struct virt_dma_desc, node);
Russell King50437bf2012-04-13 12:07:23 +0100128}
129
130/**
Jun Nie8c8fe972015-07-10 20:02:49 +0800131 * vchan_get_all_descriptors - obtain all submitted and issued descriptors
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +0200132 * @vc: virtual channel to get descriptors from
133 * @head: list of descriptors found
Russell King50437bf2012-04-13 12:07:23 +0100134 *
135 * vc.lock must be held by caller
136 *
137 * Removes all submitted and issued descriptors from internal lists, and
138 * provides a list of all descriptors found
139 */
140static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
141 struct list_head *head)
142{
Robert Jarzmik13bb26a2015-10-13 21:54:28 +0200143 list_splice_tail_init(&vc->desc_allocated, head);
Russell King50437bf2012-04-13 12:07:23 +0100144 list_splice_tail_init(&vc->desc_submitted, head);
145 list_splice_tail_init(&vc->desc_issued, head);
146 list_splice_tail_init(&vc->desc_completed, head);
147}
148
149static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
150{
Robert Jarzmik13bb26a2015-10-13 21:54:28 +0200151 struct virt_dma_desc *vd;
Russell King50437bf2012-04-13 12:07:23 +0100152 unsigned long flags;
153 LIST_HEAD(head);
154
155 spin_lock_irqsave(&vc->lock, flags);
156 vchan_get_all_descriptors(vc, &head);
Robert Jarzmik13bb26a2015-10-13 21:54:28 +0200157 list_for_each_entry(vd, &head, node)
158 dmaengine_desc_clear_reuse(&vd->tx);
Russell King50437bf2012-04-13 12:07:23 +0100159 spin_unlock_irqrestore(&vc->lock, flags);
160
161 vchan_dma_desc_free_list(vc, &head);
162}
163
Lars-Peter Clausen2ed08622015-10-20 11:46:29 +0200164/**
165 * vchan_synchronize() - synchronize callback execution to the current context
166 * @vc: virtual channel to synchronize
167 *
168 * Makes sure that all scheduled or active callbacks have finished running. For
169 * proper operation the caller has to ensure that no new callbacks are scheduled
170 * after the invocation of this function started.
171 */
172static inline void vchan_synchronize(struct virt_dma_chan *vc)
173{
174 tasklet_kill(&vc->task);
175}
176
Russell King50437bf2012-04-13 12:07:23 +0100177#endif