blob: 60615fedcf5ed00c2b96018251a7238a2f91606f [file] [log] [blame]
Dan Williams9bc89cd2007-01-02 11:10:44 -07001/*
2 * core routines for the asynchronous memory transfer/transform api
3 *
4 * Copyright © 2006, Intel Corporation.
5 *
6 * Dan Williams <dan.j.williams@intel.com>
7 *
8 * with architecture considerations by:
9 * Neil Brown <neilb@suse.de>
10 * Jeff Garzik <jeff@garzik.org>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms and conditions of the GNU General Public License,
14 * version 2, as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
Franck Bui-Huu82524742008-05-12 21:21:05 +020026#include <linux/rculist.h>
Dan Williams9bc89cd2007-01-02 11:10:44 -070027#include <linux/kernel.h>
28#include <linux/async_tx.h>
29
30#ifdef CONFIG_DMA_ENGINE
Dan Williamsbec08512009-01-06 11:38:14 -070031static int __init async_tx_init(void)
Dan Williams9bc89cd2007-01-02 11:10:44 -070032{
Dan Williams729b5d12009-03-25 09:13:25 -070033 async_dmaengine_get();
Dan Williams9bc89cd2007-01-02 11:10:44 -070034
35 printk(KERN_INFO "async_tx: api initialized (async)\n");
36
37 return 0;
Dan Williams9bc89cd2007-01-02 11:10:44 -070038}
39
40static void __exit async_tx_exit(void)
41{
Dan Williams729b5d12009-03-25 09:13:25 -070042 async_dmaengine_put();
Dan Williams9bc89cd2007-01-02 11:10:44 -070043}
44
Dan Williamsaf1f9512009-08-29 19:09:26 -070045module_init(async_tx_init);
46module_exit(async_tx_exit);
47
Dan Williams9bc89cd2007-01-02 11:10:44 -070048/**
Dan Williams47437b22008-02-02 19:49:59 -070049 * __async_tx_find_channel - find a channel to carry out the operation or let
Dan Williams9bc89cd2007-01-02 11:10:44 -070050 * the transaction execute synchronously
Dan Williamsa08abd82009-06-03 11:43:59 -070051 * @submit: transaction dependency and submission modifiers
Dan Williams9bc89cd2007-01-02 11:10:44 -070052 * @tx_type: transaction type
53 */
54struct dma_chan *
Dan Williamsa08abd82009-06-03 11:43:59 -070055__async_tx_find_channel(struct async_submit_ctl *submit,
56 enum dma_transaction_type tx_type)
Dan Williams9bc89cd2007-01-02 11:10:44 -070057{
Dan Williamsa08abd82009-06-03 11:43:59 -070058 struct dma_async_tx_descriptor *depend_tx = submit->depend_tx;
59
Dan Williams9bc89cd2007-01-02 11:10:44 -070060 /* see if we can keep the chain on one channel */
61 if (depend_tx &&
Dan Williamsbec08512009-01-06 11:38:14 -070062 dma_has_cap(tx_type, depend_tx->chan->device->cap_mask))
Dan Williams9bc89cd2007-01-02 11:10:44 -070063 return depend_tx->chan;
Dan Williams729b5d12009-03-25 09:13:25 -070064 return async_dma_find_channel(tx_type);
Dan Williams9bc89cd2007-01-02 11:10:44 -070065}
Dan Williams47437b22008-02-02 19:49:59 -070066EXPORT_SYMBOL_GPL(__async_tx_find_channel);
Dan Williams9bc89cd2007-01-02 11:10:44 -070067#endif
68
Dan Williams19242d72008-04-17 20:17:25 -070069
70/**
71 * async_tx_channel_switch - queue an interrupt descriptor with a dependency
72 * pre-attached.
73 * @depend_tx: the operation that must finish before the new operation runs
74 * @tx: the new operation
75 */
76static void
77async_tx_channel_switch(struct dma_async_tx_descriptor *depend_tx,
78 struct dma_async_tx_descriptor *tx)
79{
Dan Williams95475e52009-07-14 12:19:02 -070080 struct dma_chan *chan = depend_tx->chan;
81 struct dma_device *device = chan->device;
Dan Williams19242d72008-04-17 20:17:25 -070082 struct dma_async_tx_descriptor *intr_tx = (void *) ~0;
83
84 /* first check to see if we can still append to depend_tx */
85 spin_lock_bh(&depend_tx->lock);
86 if (depend_tx->parent && depend_tx->chan == tx->chan) {
87 tx->parent = depend_tx;
88 depend_tx->next = tx;
89 intr_tx = NULL;
90 }
91 spin_unlock_bh(&depend_tx->lock);
92
Dan Williams95475e52009-07-14 12:19:02 -070093 /* attached dependency, flush the parent channel */
94 if (!intr_tx) {
95 device->device_issue_pending(chan);
Dan Williams19242d72008-04-17 20:17:25 -070096 return;
Dan Williams95475e52009-07-14 12:19:02 -070097 }
Dan Williams19242d72008-04-17 20:17:25 -070098
99 /* see if we can schedule an interrupt
100 * otherwise poll for completion
101 */
102 if (dma_has_cap(DMA_INTERRUPT, device->cap_mask))
Dan Williams636bdea2008-04-17 20:17:26 -0700103 intr_tx = device->device_prep_dma_interrupt(chan, 0);
Dan Williams19242d72008-04-17 20:17:25 -0700104 else
105 intr_tx = NULL;
106
107 if (intr_tx) {
108 intr_tx->callback = NULL;
109 intr_tx->callback_param = NULL;
110 tx->parent = intr_tx;
111 /* safe to set ->next outside the lock since we know we are
112 * not submitted yet
113 */
114 intr_tx->next = tx;
115
116 /* check if we need to append */
117 spin_lock_bh(&depend_tx->lock);
118 if (depend_tx->parent) {
119 intr_tx->parent = depend_tx;
120 depend_tx->next = intr_tx;
121 async_tx_ack(intr_tx);
122 intr_tx = NULL;
123 }
124 spin_unlock_bh(&depend_tx->lock);
125
126 if (intr_tx) {
127 intr_tx->parent = NULL;
128 intr_tx->tx_submit(intr_tx);
129 async_tx_ack(intr_tx);
130 }
Dan Williams95475e52009-07-14 12:19:02 -0700131 device->device_issue_pending(chan);
Dan Williams19242d72008-04-17 20:17:25 -0700132 } else {
133 if (dma_wait_for_async_tx(depend_tx) == DMA_ERROR)
134 panic("%s: DMA_ERROR waiting for depend_tx\n",
135 __func__);
136 tx->tx_submit(tx);
137 }
138}
139
140
141/**
Dan Williamsa08abd82009-06-03 11:43:59 -0700142 * submit_disposition - flags for routing an incoming operation
Dan Williams19242d72008-04-17 20:17:25 -0700143 * @ASYNC_TX_SUBMITTED: we were able to append the new operation under the lock
144 * @ASYNC_TX_CHANNEL_SWITCH: when the lock is dropped schedule a channel switch
145 * @ASYNC_TX_DIRECT_SUBMIT: when the lock is dropped submit directly
Dan Williamsa08abd82009-06-03 11:43:59 -0700146 *
147 * while holding depend_tx->lock we must avoid submitting new operations
148 * to prevent a circular locking dependency with drivers that already
149 * hold a channel lock when calling async_tx_run_dependencies.
Dan Williams19242d72008-04-17 20:17:25 -0700150 */
151enum submit_disposition {
152 ASYNC_TX_SUBMITTED,
153 ASYNC_TX_CHANNEL_SWITCH,
154 ASYNC_TX_DIRECT_SUBMIT,
155};
156
Dan Williams9bc89cd2007-01-02 11:10:44 -0700157void
158async_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx,
Dan Williamsa08abd82009-06-03 11:43:59 -0700159 struct async_submit_ctl *submit)
Dan Williams9bc89cd2007-01-02 11:10:44 -0700160{
Dan Williamsa08abd82009-06-03 11:43:59 -0700161 struct dma_async_tx_descriptor *depend_tx = submit->depend_tx;
162
163 tx->callback = submit->cb_fn;
164 tx->callback_param = submit->cb_param;
Dan Williams9bc89cd2007-01-02 11:10:44 -0700165
Dan Williams19242d72008-04-17 20:17:25 -0700166 if (depend_tx) {
167 enum submit_disposition s;
Dan Williams9bc89cd2007-01-02 11:10:44 -0700168
Dan Williams19242d72008-04-17 20:17:25 -0700169 /* sanity check the dependency chain:
170 * 1/ if ack is already set then we cannot be sure
171 * we are referring to the correct operation
172 * 2/ dependencies are 1:1 i.e. two transactions can
173 * not depend on the same parent
174 */
Dan Williams636bdea2008-04-17 20:17:26 -0700175 BUG_ON(async_tx_test_ack(depend_tx) || depend_tx->next ||
176 tx->parent);
Dan Williams19242d72008-04-17 20:17:25 -0700177
178 /* the lock prevents async_tx_run_dependencies from missing
179 * the setting of ->next when ->parent != NULL
180 */
Dan Williams9bc89cd2007-01-02 11:10:44 -0700181 spin_lock_bh(&depend_tx->lock);
Dan Williams19242d72008-04-17 20:17:25 -0700182 if (depend_tx->parent) {
183 /* we have a parent so we can not submit directly
184 * if we are staying on the same channel: append
185 * else: channel switch
186 */
187 if (depend_tx->chan == chan) {
188 tx->parent = depend_tx;
189 depend_tx->next = tx;
190 s = ASYNC_TX_SUBMITTED;
191 } else
192 s = ASYNC_TX_CHANNEL_SWITCH;
193 } else {
194 /* we do not have a parent so we may be able to submit
195 * directly if we are staying on the same channel
196 */
197 if (depend_tx->chan == chan)
198 s = ASYNC_TX_DIRECT_SUBMIT;
199 else
200 s = ASYNC_TX_CHANNEL_SWITCH;
Dan Williams9bc89cd2007-01-02 11:10:44 -0700201 }
202 spin_unlock_bh(&depend_tx->lock);
203
Dan Williams19242d72008-04-17 20:17:25 -0700204 switch (s) {
205 case ASYNC_TX_SUBMITTED:
206 break;
207 case ASYNC_TX_CHANNEL_SWITCH:
208 async_tx_channel_switch(depend_tx, tx);
209 break;
210 case ASYNC_TX_DIRECT_SUBMIT:
211 tx->parent = NULL;
212 tx->tx_submit(tx);
213 break;
214 }
Dan Williams9bc89cd2007-01-02 11:10:44 -0700215 } else {
216 tx->parent = NULL;
217 tx->tx_submit(tx);
218 }
219
Dan Williamsa08abd82009-06-03 11:43:59 -0700220 if (submit->flags & ASYNC_TX_ACK)
Dan Williams9bc89cd2007-01-02 11:10:44 -0700221 async_tx_ack(tx);
222
Dan Williams88ba2aa2009-04-09 16:16:18 -0700223 if (depend_tx)
Dan Williams9bc89cd2007-01-02 11:10:44 -0700224 async_tx_ack(depend_tx);
225}
226EXPORT_SYMBOL_GPL(async_tx_submit);
227
228/**
Dan Williamsa08abd82009-06-03 11:43:59 -0700229 * async_trigger_callback - schedules the callback function to be run
230 * @submit: submission and completion parameters
231 *
232 * honored flags: ASYNC_TX_ACK
233 *
234 * The callback is run after any dependent operations have completed.
Dan Williams9bc89cd2007-01-02 11:10:44 -0700235 */
236struct dma_async_tx_descriptor *
Dan Williamsa08abd82009-06-03 11:43:59 -0700237async_trigger_callback(struct async_submit_ctl *submit)
Dan Williams9bc89cd2007-01-02 11:10:44 -0700238{
239 struct dma_chan *chan;
240 struct dma_device *device;
241 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700242 struct dma_async_tx_descriptor *depend_tx = submit->depend_tx;
Dan Williams9bc89cd2007-01-02 11:10:44 -0700243
244 if (depend_tx) {
245 chan = depend_tx->chan;
246 device = chan->device;
247
248 /* see if we can schedule an interrupt
249 * otherwise poll for completion
250 */
251 if (device && !dma_has_cap(DMA_INTERRUPT, device->cap_mask))
252 device = NULL;
253
Dan Williams636bdea2008-04-17 20:17:26 -0700254 tx = device ? device->device_prep_dma_interrupt(chan, 0) : NULL;
Dan Williams9bc89cd2007-01-02 11:10:44 -0700255 } else
256 tx = NULL;
257
258 if (tx) {
Dan Williams3280ab3e2008-03-13 17:45:28 -0700259 pr_debug("%s: (async)\n", __func__);
Dan Williams9bc89cd2007-01-02 11:10:44 -0700260
Dan Williamsa08abd82009-06-03 11:43:59 -0700261 async_tx_submit(chan, tx, submit);
Dan Williams9bc89cd2007-01-02 11:10:44 -0700262 } else {
Dan Williams3280ab3e2008-03-13 17:45:28 -0700263 pr_debug("%s: (sync)\n", __func__);
Dan Williams9bc89cd2007-01-02 11:10:44 -0700264
265 /* wait for any prerequisite operations */
Dan Williamsa08abd82009-06-03 11:43:59 -0700266 async_tx_quiesce(&submit->depend_tx);
Dan Williams9bc89cd2007-01-02 11:10:44 -0700267
Dan Williamsa08abd82009-06-03 11:43:59 -0700268 async_tx_sync_epilog(submit);
Dan Williams9bc89cd2007-01-02 11:10:44 -0700269 }
270
271 return tx;
272}
273EXPORT_SYMBOL_GPL(async_trigger_callback);
274
Dan Williamsd2c52b72008-07-17 17:59:55 -0700275/**
276 * async_tx_quiesce - ensure tx is complete and freeable upon return
277 * @tx - transaction to quiesce
278 */
279void async_tx_quiesce(struct dma_async_tx_descriptor **tx)
280{
281 if (*tx) {
282 /* if ack is already set then we cannot be sure
283 * we are referring to the correct operation
284 */
285 BUG_ON(async_tx_test_ack(*tx));
286 if (dma_wait_for_async_tx(*tx) == DMA_ERROR)
287 panic("DMA_ERROR waiting for transaction\n");
288 async_tx_ack(*tx);
289 *tx = NULL;
290 }
291}
292EXPORT_SYMBOL_GPL(async_tx_quiesce);
293
Dan Williams9bc89cd2007-01-02 11:10:44 -0700294MODULE_AUTHOR("Intel Corporation");
295MODULE_DESCRIPTION("Asynchronous Bulk Memory Transactions API");
296MODULE_LICENSE("GPL");