blob: 189e75dbcb15f95876a81848ecde8196a4c82905 [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 Jarzmikb9855f02015-05-26 23:06:33 +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);
Russell King50437bf2012-04-13 12:07:23 +010048
49/**
50 * vchan_tx_prep - prepare a descriptor
51 * vc: virtual channel allocating this descriptor
52 * vd: virtual descriptor to prepare
53 * tx_flags: flags argument passed in to prepare function
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{
58 extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *);
Robert Jarzmikb9855f02015-05-26 23:06:33 +020059 unsigned long flags;
Russell King50437bf2012-04-13 12:07:23 +010060
61 dma_async_tx_descriptor_init(&vd->tx, &vc->chan);
62 vd->tx.flags = tx_flags;
63 vd->tx.tx_submit = vchan_tx_submit;
64
Robert Jarzmikb9855f02015-05-26 23:06:33 +020065 spin_lock_irqsave(&vc->lock, flags);
66 list_add_tail(&vd->node, &vc->desc_allocated);
67 spin_unlock_irqrestore(&vc->lock, flags);
68
Russell King50437bf2012-04-13 12:07:23 +010069 return &vd->tx;
70}
71
72/**
73 * vchan_issue_pending - move submitted descriptors to issued list
74 * vc: virtual channel to update
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
86 * vd: virtual descriptor to update
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);
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/**
Russell King571fa742012-05-14 15:17:20 +0100105 * vchan_cyclic_callback - report the completion of a period
106 * vd: virtual descriptor
107 */
108static inline void vchan_cyclic_callback(struct virt_dma_desc *vd)
109{
110 struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan);
111
112 vc->cyclic = vd;
113 tasklet_schedule(&vc->task);
114}
115
116/**
Russell King50437bf2012-04-13 12:07:23 +0100117 * vchan_next_desc - peek at the next descriptor to be processed
118 * vc: virtual channel to obtain descriptor from
119 *
120 * vc.lock must be held by caller
121 */
122static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc)
123{
124 if (list_empty(&vc->desc_issued))
125 return NULL;
126
127 return list_first_entry(&vc->desc_issued, struct virt_dma_desc, node);
128}
129
130/**
Robert Jarzmikb9855f02015-05-26 23:06:33 +0200131 * vchan_get_all_descriptors - obtain all allocated, submitted and issued
132 * descriptors
Russell King50437bf2012-04-13 12:07:23 +0100133 * vc: virtual channel to get descriptors from
134 * head: list of descriptors found
135 *
136 * vc.lock must be held by caller
137 *
138 * Removes all submitted and issued descriptors from internal lists, and
139 * provides a list of all descriptors found
140 */
141static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc,
142 struct list_head *head)
143{
Robert Jarzmikb9855f02015-05-26 23:06:33 +0200144 list_splice_tail_init(&vc->desc_allocated, head);
Russell King50437bf2012-04-13 12:07:23 +0100145 list_splice_tail_init(&vc->desc_submitted, head);
146 list_splice_tail_init(&vc->desc_issued, head);
147 list_splice_tail_init(&vc->desc_completed, head);
148}
149
150static inline void vchan_free_chan_resources(struct virt_dma_chan *vc)
151{
Robert Jarzmikb9855f02015-05-26 23:06:33 +0200152 struct virt_dma_desc *vd;
Russell King50437bf2012-04-13 12:07:23 +0100153 unsigned long flags;
154 LIST_HEAD(head);
155
156 spin_lock_irqsave(&vc->lock, flags);
157 vchan_get_all_descriptors(vc, &head);
Robert Jarzmikb9855f02015-05-26 23:06:33 +0200158 list_for_each_entry(vd, &head, node)
159 async_tx_clear_ack(&vd->tx);
Russell King50437bf2012-04-13 12:07:23 +0100160 spin_unlock_irqrestore(&vc->lock, flags);
161
162 vchan_dma_desc_free_list(vc, &head);
163}
164
165#endif