blob: 23342ca23d4a97135273394f666e4531d65fd56a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Russell King50437bf2012-04-13 12:07:23 +01002/*
3 * Virtual DMA channel support for DMAengine
4 *
5 * Copyright (C) 2012 Russell King
Russell King50437bf2012-04-13 12:07:23 +01006 */
7#ifndef VIRT_DMA_H
8#define VIRT_DMA_H
9
10#include <linux/dmaengine.h>
11#include <linux/interrupt.h>
12
13#include "dmaengine.h"
14
15struct virt_dma_desc {
16 struct dma_async_tx_descriptor tx;
17 /* protected by vc.lock */
18 struct list_head node;
19};
20
21struct virt_dma_chan {
22 struct dma_chan chan;
23 struct tasklet_struct task;
24 void (*desc_free)(struct virt_dma_desc *);
25
26 spinlock_t lock;
27
28 /* protected by vc.lock */
Robert Jarzmik13bb26a2015-10-13 21:54:28 +020029 struct list_head desc_allocated;
Russell King50437bf2012-04-13 12:07:23 +010030 struct list_head desc_submitted;
31 struct list_head desc_issued;
32 struct list_head desc_completed;
Russell King571fa742012-05-14 15:17:20 +010033
34 struct virt_dma_desc *cyclic;
Peter Ujfalusi1c7f0722017-11-14 16:32:04 +020035 struct virt_dma_desc *vd_terminated;
Russell King50437bf2012-04-13 12:07:23 +010036};
37
38static inline struct virt_dma_chan *to_virt_chan(struct dma_chan *chan)
39{
40 return container_of(chan, struct virt_dma_chan, chan);
41}
42
43void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head);
Russell King50437bf2012-04-13 12:07:23 +010044void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev);
Russell Kingfe045872012-05-10 23:39:27 +010045struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *, dma_cookie_t);
Baoyou Xie02aa8482016-09-24 12:37:05 +080046extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *);
47extern int vchan_tx_desc_free(struct dma_async_tx_descriptor *);
Russell King50437bf2012-04-13 12:07:23 +010048
49/**
50 * vchan_tx_prep - prepare a descriptor
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +020051 * @vc: virtual channel allocating this descriptor
52 * @vd: virtual descriptor to prepare
53 * @tx_flags: flags argument passed in to prepare function
Russell King50437bf2012-04-13 12:07:23 +010054 */
55static inline struct dma_async_tx_descriptor *vchan_tx_prep(struct virt_dma_chan *vc,
56 struct virt_dma_desc *vd, unsigned long tx_flags)
57{
Robert Jarzmik13bb26a2015-10-13 21:54:28 +020058 unsigned long flags;
Russell King50437bf2012-04-13 12:07:23 +010059
60 dma_async_tx_descriptor_init(&vd->tx, &vc->chan);
61 vd->tx.flags = tx_flags;
62 vd->tx.tx_submit = vchan_tx_submit;
Robert Jarzmik13bb26a2015-10-13 21:54:28 +020063 vd->tx.desc_free = vchan_tx_desc_free;
64
65 spin_lock_irqsave(&vc->lock, flags);
66 list_add_tail(&vd->node, &vc->desc_allocated);
67 spin_unlock_irqrestore(&vc->lock, flags);
Russell King50437bf2012-04-13 12:07:23 +010068
69 return &vd->tx;
70}
71
72/**
73 * vchan_issue_pending - move submitted descriptors to issued list
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +020074 * @vc: virtual channel to update
Russell King50437bf2012-04-13 12:07:23 +010075 *
76 * vc.lock must be held by caller
77 */
78static inline bool vchan_issue_pending(struct virt_dma_chan *vc)
79{
80 list_splice_tail_init(&vc->desc_submitted, &vc->desc_issued);
81 return !list_empty(&vc->desc_issued);
82}
83
84/**
85 * vchan_cookie_complete - report completion of a descriptor
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +020086 * @vd: virtual descriptor to update
Russell King50437bf2012-04-13 12:07:23 +010087 *
88 * vc.lock must be held by caller
89 */
90static inline void vchan_cookie_complete(struct virt_dma_desc *vd)
91{
92 struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
Jonas Jensenaf586522013-12-06 16:42:09 +010093 dma_cookie_t cookie;
Russell King50437bf2012-04-13 12:07:23 +010094
Jonas Jensenaf586522013-12-06 16:42:09 +010095 cookie = vd->tx.cookie;
Russell King50437bf2012-04-13 12:07:23 +010096 dma_cookie_complete(&vd->tx);
97 dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n",
Jonas Jensenaf586522013-12-06 16:42:09 +010098 vd, cookie);
Russell King50437bf2012-04-13 12:07:23 +010099 list_add_tail(&vd->node, &vc->desc_completed);
100
101 tasklet_schedule(&vc->task);
102}
103
104/**
Peter Ujfalusi6af149d2017-11-14 16:32:03 +0200105 * vchan_vdesc_fini - Free or reuse a descriptor
106 * @vd: virtual descriptor to free/reuse
107 */
108static inline void vchan_vdesc_fini(struct virt_dma_desc *vd)
109{
110 struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
111
112 if (dmaengine_desc_test_reuse(&vd->tx))
113 list_add(&vd->node, &vc->desc_allocated);
114 else
115 vc->desc_free(vd);
116}
117
118/**
Russell King571fa742012-05-14 15:17:20 +0100119 * vchan_cyclic_callback - report the completion of a period
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +0200120 * @vd: virtual descriptor
Russell King571fa742012-05-14 15:17:20 +0100121 */
122static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
123{
124 struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
125
126 vc->cyclic = vd;
127 tasklet_schedule(&vc->task);
128}
129
130/**
Peter Ujfalusi1c7f0722017-11-14 16:32:04 +0200131 * vchan_terminate_vdesc - Disable pending cyclic callback
132 * @vd: virtual descriptor to be terminated
133 *
134 * vc.lock must be held by caller
135 */
136static inline void vchan_terminate_vdesc(struct virt_dma_desc *vd)
137{
138 struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
139
140 /* free up stuck descriptor */
141 if (vc->vd_terminated)
142 vchan_vdesc_fini(vc->vd_terminated);
143
144 vc->vd_terminated = vd;
145 if (vc->cyclic == vd)
146 vc->cyclic = NULL;
147}
148
149/**
Russell King50437bf2012-04-13 12:07:23 +0100150 * vchan_next_desc - peek at the next descriptor to be processed
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +0200151 * @vc: virtual channel to obtain descriptor from
Russell King50437bf2012-04-13 12:07:23 +0100152 *
153 * vc.lock must be held by caller
154 */
155static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
156{
Masahiro Yamada360af352016-09-13 03:08:17 +0900157 return list_first_entry_or_null(&vc->desc_issued,
158 struct virt_dma_desc, node);
Russell King50437bf2012-04-13 12:07:23 +0100159}
160
161/**
Jun Nie8c8fe972015-07-10 20:02:49 +0800162 * vchan_get_all_descriptors - obtain all submitted and issued descriptors
Lars-Peter Clausen28ca3e82015-10-20 13:14:45 +0200163 * @vc: virtual channel to get descriptors from
164 * @head: list of descriptors found
Russell King50437bf2012-04-13 12:07:23 +0100165 *
166 * vc.lock must be held by caller
167 *
168 * Removes all submitted and issued descriptors from internal lists, and
169 * provides a list of all descriptors found
170 */
171static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
172 struct list_head *head)
173{
Robert Jarzmik13bb26a2015-10-13 21:54:28 +0200174 list_splice_tail_init(&vc->desc_allocated, head);
Russell King50437bf2012-04-13 12:07:23 +0100175 list_splice_tail_init(&vc->desc_submitted, head);
176 list_splice_tail_init(&vc->desc_issued, head);
177 list_splice_tail_init(&vc->desc_completed, head);
178}
179
180static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
181{
Robert Jarzmik13bb26a2015-10-13 21:54:28 +0200182 struct virt_dma_desc *vd;
Russell King50437bf2012-04-13 12:07:23 +0100183 unsigned long flags;
184 LIST_HEAD(head);
185
186 spin_lock_irqsave(&vc->lock, flags);
187 vchan_get_all_descriptors(vc, &head);
Robert Jarzmik13bb26a2015-10-13 21:54:28 +0200188 list_for_each_entry(vd, &head, node)
189 dmaengine_desc_clear_reuse(&vd->tx);
Russell King50437bf2012-04-13 12:07:23 +0100190 spin_unlock_irqrestore(&vc->lock, flags);
191
192 vchan_dma_desc_free_list(vc, &head);
193}
194
Lars-Peter Clausen2ed08622015-10-20 11:46:29 +0200195/**
196 * vchan_synchronize() - synchronize callback execution to the current context
197 * @vc: virtual channel to synchronize
198 *
199 * Makes sure that all scheduled or active callbacks have finished running. For
200 * proper operation the caller has to ensure that no new callbacks are scheduled
201 * after the invocation of this function started.
Peter Ujfalusi1c7f0722017-11-14 16:32:04 +0200202 * Free up the terminated cyclic descriptor to prevent memory leakage.
Lars-Peter Clausen2ed08622015-10-20 11:46:29 +0200203 */
204static inline void vchan_synchronize(struct virt_dma_chan *vc)
205{
Peter Ujfalusi1c7f0722017-11-14 16:32:04 +0200206 unsigned long flags;
207
Lars-Peter Clausen2ed08622015-10-20 11:46:29 +0200208 tasklet_kill(&vc->task);
Peter Ujfalusi1c7f0722017-11-14 16:32:04 +0200209
210 spin_lock_irqsave(&vc->lock, flags);
211 if (vc->vd_terminated) {
212 vchan_vdesc_fini(vc->vd_terminated);
213 vc->vd_terminated = NULL;
214 }
215 spin_unlock_irqrestore(&vc->lock, flags);
Lars-Peter Clausen2ed08622015-10-20 11:46:29 +0200216}
217
Russell King50437bf2012-04-13 12:07:23 +0100218#endif