]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/dma/virt-dma.h
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[mirror_ubuntu-hirsute-kernel.git] / drivers / dma / virt-dma.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
50437bff
RK
2/*
3 * Virtual DMA channel support for DMAengine
4 *
5 * Copyright (C) 2012 Russell King
50437bff
RK
6 */
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 */
13bb26ae 29 struct list_head desc_allocated;
50437bff
RK
30 struct list_head desc_submitted;
31 struct list_head desc_issued;
32 struct list_head desc_completed;
571fa740
RK
33
34 struct virt_dma_desc *cyclic;
1c7f072d 35 struct virt_dma_desc *vd_terminated;
50437bff
RK
36};
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);
50437bff 44void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev);
fe045874 45struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *, dma_cookie_t);
02aa8486
BX
46extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *);
47extern int vchan_tx_desc_free(struct dma_async_tx_descriptor *);
50437bff
RK
48
49/**
50 * vchan_tx_prep - prepare a descriptor
28ca3e85
LPC
51 * @vc: virtual channel allocating this descriptor
52 * @vd: virtual descriptor to prepare
53 * @tx_flags: flags argument passed in to prepare function
50437bff
RK
54 */
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{
13bb26ae 58 unsigned long flags;
50437bff
RK
59
60 dma_async_tx_descriptor_init(&vd->tx, &vc->chan);
61 vd->tx.flags = tx_flags;
62 vd->tx.tx_submit = vchan_tx_submit;
13bb26ae
RJ
63 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);
50437bff
RK
68
69 return &vd->tx;
70}
71
72/**
73 * vchan_issue_pending - move submitted descriptors to issued list
28ca3e85 74 * @vc: virtual channel to update
50437bff
RK
75 *
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
28ca3e85 86 * @vd: virtual descriptor to update
50437bff
RK
87 *
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);
af58652a 93 dma_cookie_t cookie;
50437bff 94
af58652a 95 cookie = vd->tx.cookie;
50437bff
RK
96 dma_cookie_complete(&vd->tx);
97 dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n",
af58652a 98 vd, cookie);
50437bff
RK
99 list_add_tail(&vd->node, &vc->desc_completed);
100
101 tasklet_schedule(&vc->task);
102}
103
6af149d2
PU
104/**
105 * 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
571fa740
RK
118/**
119 * vchan_cyclic_callback - report the completion of a period
28ca3e85 120 * @vd: virtual descriptor
571fa740
RK
121 */
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
1c7f072d
PU
130/**
131 * 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
50437bff
RK
149/**
150 * vchan_next_desc - peek at the next descriptor to be processed
28ca3e85 151 * @vc: virtual channel to obtain descriptor from
50437bff
RK
152 *
153 * vc.lock must be held by caller
154 */
155static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
156{
360af35b
MY
157 return list_first_entry_or_null(&vc->desc_issued,
158 struct virt_dma_desc, node);
50437bff
RK
159}
160
161/**
8c8fe97b 162 * vchan_get_all_descriptors - obtain all submitted and issued descriptors
28ca3e85
LPC
163 * @vc: virtual channel to get descriptors from
164 * @head: list of descriptors found
50437bff
RK
165 *
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{
13bb26ae 174 list_splice_tail_init(&vc->desc_allocated, head);
50437bff
RK
175 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{
13bb26ae 182 struct virt_dma_desc *vd;
50437bff
RK
183 unsigned long flags;
184 LIST_HEAD(head);
185
186 spin_lock_irqsave(&vc->lock, flags);
187 vchan_get_all_descriptors(vc, &head);
13bb26ae
RJ
188 list_for_each_entry(vd, &head, node)
189 dmaengine_desc_clear_reuse(&vd->tx);
50437bff
RK
190 spin_unlock_irqrestore(&vc->lock, flags);
191
192 vchan_dma_desc_free_list(vc, &head);
193}
194
2ed08629
LPC
195/**
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.
1c7f072d 202 * Free up the terminated cyclic descriptor to prevent memory leakage.
2ed08629
LPC
203 */
204static inline void vchan_synchronize(struct virt_dma_chan *vc)
205{
1c7f072d
PU
206 unsigned long flags;
207
2ed08629 208 tasklet_kill(&vc->task);
1c7f072d
PU
209
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);
2ed08629
LPC
216}
217
50437bff 218#endif