blob: 90aca505a1df109eb5b14fda1a5ae448ed8629de [file] [log] [blame]
Chris Leechc13c8262006-05-23 17:18:44 -07001/*
2 * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called COPYING.
20 */
21
22/*
23 * This code implements the DMA subsystem. It provides a HW-neutral interface
24 * for other kernel code to use asynchronous memory copy capabilities,
25 * if present, and allows different HW DMA drivers to register as providing
26 * this capability.
27 *
28 * Due to the fact we are accelerating what is already a relatively fast
29 * operation, the code goes to great lengths to avoid additional overhead,
30 * such as locking.
31 *
32 * LOCKING:
33 *
34 * The subsystem keeps two global lists, dma_device_list and dma_client_list.
35 * Both of these are protected by a mutex, dma_list_mutex.
36 *
37 * Each device has a channels list, which runs unlocked but is never modified
38 * once the device is registered, it's just setup by the driver.
39 *
Dan Williamsd379b012007-07-09 11:56:42 -070040 * Each client is responsible for keeping track of the channels it uses. See
41 * the definition of dma_event_callback in dmaengine.h.
Chris Leechc13c8262006-05-23 17:18:44 -070042 *
43 * Each device has a kref, which is initialized to 1 when the device is
Tony Jones891f78e2007-09-25 02:03:03 +020044 * registered. A kref_get is done for each device registered. When the
Sebastian Siewior8a5703f2008-04-21 22:38:45 +000045 * device is released, the corresponding kref_put is done in the release
Chris Leechc13c8262006-05-23 17:18:44 -070046 * method. Every time one of the device's channels is allocated to a client,
Sebastian Siewior8a5703f2008-04-21 22:38:45 +000047 * a kref_get occurs. When the channel is freed, the corresponding kref_put
Chris Leechc13c8262006-05-23 17:18:44 -070048 * happens. The device's release function does a completion, so
Tony Jones891f78e2007-09-25 02:03:03 +020049 * unregister_device does a remove event, device_unregister, a kref_put
Chris Leechc13c8262006-05-23 17:18:44 -070050 * for the first reference, then waits on the completion for all other
51 * references to finish.
52 *
53 * Each channel has an open-coded implementation of Rusty Russell's "bigref,"
Dan Williamsd379b012007-07-09 11:56:42 -070054 * with a kref and a per_cpu local_t. A dma_chan_get is called when a client
55 * signals that it wants to use a channel, and dma_chan_put is called when
Sebastian Siewior8a5703f2008-04-21 22:38:45 +000056 * a channel is removed or a client using it is unregistered. A client can
Dan Williamsd379b012007-07-09 11:56:42 -070057 * take extra references per outstanding transaction, as is the case with
58 * the NET DMA client. The release function does a kref_put on the device.
59 * -ChrisL, DanW
Chris Leechc13c8262006-05-23 17:18:44 -070060 */
61
62#include <linux/init.h>
63#include <linux/module.h>
Dan Williams7405f742007-01-02 11:10:43 -070064#include <linux/mm.h>
Chris Leechc13c8262006-05-23 17:18:44 -070065#include <linux/device.h>
66#include <linux/dmaengine.h>
67#include <linux/hardirq.h>
68#include <linux/spinlock.h>
69#include <linux/percpu.h>
70#include <linux/rcupdate.h>
71#include <linux/mutex.h>
Dan Williams7405f742007-01-02 11:10:43 -070072#include <linux/jiffies.h>
Dan Williams2ba05622009-01-06 11:38:14 -070073#include <linux/rculist.h>
Chris Leechc13c8262006-05-23 17:18:44 -070074
75static DEFINE_MUTEX(dma_list_mutex);
76static LIST_HEAD(dma_device_list);
77static LIST_HEAD(dma_client_list);
Dan Williams6f49a572009-01-06 11:38:14 -070078static long dmaengine_ref_count;
Chris Leechc13c8262006-05-23 17:18:44 -070079
80/* --- sysfs implementation --- */
81
Tony Jones891f78e2007-09-25 02:03:03 +020082static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -070083{
Tony Jones891f78e2007-09-25 02:03:03 +020084 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -070085 unsigned long count = 0;
86 int i;
87
Andrew Morton17f3ae02006-05-25 13:26:53 -070088 for_each_possible_cpu(i)
Chris Leechc13c8262006-05-23 17:18:44 -070089 count += per_cpu_ptr(chan->local, i)->memcpy_count;
90
91 return sprintf(buf, "%lu\n", count);
92}
93
Tony Jones891f78e2007-09-25 02:03:03 +020094static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr,
95 char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -070096{
Tony Jones891f78e2007-09-25 02:03:03 +020097 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -070098 unsigned long count = 0;
99 int i;
100
Andrew Morton17f3ae02006-05-25 13:26:53 -0700101 for_each_possible_cpu(i)
Chris Leechc13c8262006-05-23 17:18:44 -0700102 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
103
104 return sprintf(buf, "%lu\n", count);
105}
106
Tony Jones891f78e2007-09-25 02:03:03 +0200107static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700108{
Tony Jones891f78e2007-09-25 02:03:03 +0200109 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700110
Dan Williams6f49a572009-01-06 11:38:14 -0700111 return sprintf(buf, "%d\n", chan->client_count);
Chris Leechc13c8262006-05-23 17:18:44 -0700112}
113
Tony Jones891f78e2007-09-25 02:03:03 +0200114static struct device_attribute dma_attrs[] = {
Chris Leechc13c8262006-05-23 17:18:44 -0700115 __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
116 __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
117 __ATTR(in_use, S_IRUGO, show_in_use, NULL),
118 __ATTR_NULL
119};
120
121static void dma_async_device_cleanup(struct kref *kref);
122
Tony Jones891f78e2007-09-25 02:03:03 +0200123static void dma_dev_release(struct device *dev)
Chris Leechc13c8262006-05-23 17:18:44 -0700124{
Tony Jones891f78e2007-09-25 02:03:03 +0200125 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700126 kref_put(&chan->device->refcount, dma_async_device_cleanup);
127}
128
129static struct class dma_devclass = {
Tony Jones891f78e2007-09-25 02:03:03 +0200130 .name = "dma",
131 .dev_attrs = dma_attrs,
132 .dev_release = dma_dev_release,
Chris Leechc13c8262006-05-23 17:18:44 -0700133};
134
135/* --- client and device registration --- */
136
Dan Williams59b5ec22009-01-06 11:38:15 -0700137#define dma_device_satisfies_mask(device, mask) \
138 __dma_device_satisfies_mask((device), &(mask))
Dan Williamsd379b012007-07-09 11:56:42 -0700139static int
Dan Williams59b5ec22009-01-06 11:38:15 -0700140__dma_device_satisfies_mask(struct dma_device *device, dma_cap_mask_t *want)
Dan Williamsd379b012007-07-09 11:56:42 -0700141{
142 dma_cap_mask_t has;
143
Dan Williams59b5ec22009-01-06 11:38:15 -0700144 bitmap_and(has.bits, want->bits, device->cap_mask.bits,
Dan Williamsd379b012007-07-09 11:56:42 -0700145 DMA_TX_TYPE_END);
146 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
147}
148
Dan Williams6f49a572009-01-06 11:38:14 -0700149static struct module *dma_chan_to_owner(struct dma_chan *chan)
150{
151 return chan->device->dev->driver->owner;
152}
153
154/**
155 * balance_ref_count - catch up the channel reference count
156 * @chan - channel to balance ->client_count versus dmaengine_ref_count
157 *
158 * balance_ref_count must be called under dma_list_mutex
159 */
160static void balance_ref_count(struct dma_chan *chan)
161{
162 struct module *owner = dma_chan_to_owner(chan);
163
164 while (chan->client_count < dmaengine_ref_count) {
165 __module_get(owner);
166 chan->client_count++;
167 }
168}
169
170/**
171 * dma_chan_get - try to grab a dma channel's parent driver module
172 * @chan - channel to grab
173 *
174 * Must be called under dma_list_mutex
175 */
176static int dma_chan_get(struct dma_chan *chan)
177{
178 int err = -ENODEV;
179 struct module *owner = dma_chan_to_owner(chan);
180
181 if (chan->client_count) {
182 __module_get(owner);
183 err = 0;
184 } else if (try_module_get(owner))
185 err = 0;
186
187 if (err == 0)
188 chan->client_count++;
189
190 /* allocate upon first client reference */
191 if (chan->client_count == 1 && err == 0) {
192 int desc_cnt = chan->device->device_alloc_chan_resources(chan, NULL);
193
194 if (desc_cnt < 0) {
195 err = desc_cnt;
196 chan->client_count = 0;
197 module_put(owner);
Dan Williams59b5ec22009-01-06 11:38:15 -0700198 } else if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
Dan Williams6f49a572009-01-06 11:38:14 -0700199 balance_ref_count(chan);
200 }
201
202 return err;
203}
204
205/**
206 * dma_chan_put - drop a reference to a dma channel's parent driver module
207 * @chan - channel to release
208 *
209 * Must be called under dma_list_mutex
210 */
211static void dma_chan_put(struct dma_chan *chan)
212{
213 if (!chan->client_count)
214 return; /* this channel failed alloc_chan_resources */
215 chan->client_count--;
216 module_put(dma_chan_to_owner(chan));
217 if (chan->client_count == 0)
218 chan->device->device_free_chan_resources(chan);
219}
220
Chris Leechc13c8262006-05-23 17:18:44 -0700221/**
Dan Williamsd379b012007-07-09 11:56:42 -0700222 * dma_client_chan_alloc - try to allocate channels to a client
Chris Leechc13c8262006-05-23 17:18:44 -0700223 * @client: &dma_client
224 *
225 * Called with dma_list_mutex held.
226 */
Dan Williamsd379b012007-07-09 11:56:42 -0700227static void dma_client_chan_alloc(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700228{
229 struct dma_device *device;
230 struct dma_chan *chan;
Dan Williamsd379b012007-07-09 11:56:42 -0700231 enum dma_state_client ack;
Chris Leechc13c8262006-05-23 17:18:44 -0700232
Dan Williamsd379b012007-07-09 11:56:42 -0700233 /* Find a channel */
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700234 list_for_each_entry(device, &dma_device_list, global_node) {
Dan Williams59b5ec22009-01-06 11:38:15 -0700235 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
236 continue;
Dan Williams59b5ec22009-01-06 11:38:15 -0700237 if (!dma_device_satisfies_mask(device, client->cap_mask))
238 continue;
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700239
Chris Leechc13c8262006-05-23 17:18:44 -0700240 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williams6f49a572009-01-06 11:38:14 -0700241 if (!chan->client_count)
242 continue;
243 ack = client->event_callback(client, chan,
244 DMA_RESOURCE_AVAILABLE);
Chris Leechc13c8262006-05-23 17:18:44 -0700245
Dan Williams6f49a572009-01-06 11:38:14 -0700246 /* we are done once this client rejects
247 * an available resource
248 */
249 if (ack == DMA_NAK)
250 return;
Chris Leechc13c8262006-05-23 17:18:44 -0700251 }
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700252 }
Chris Leechc13c8262006-05-23 17:18:44 -0700253}
254
Dan Williams7405f742007-01-02 11:10:43 -0700255enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
256{
257 enum dma_status status;
258 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
259
260 dma_async_issue_pending(chan);
261 do {
262 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
263 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
264 printk(KERN_ERR "dma_sync_wait_timeout!\n");
265 return DMA_ERROR;
266 }
267 } while (status == DMA_IN_PROGRESS);
268
269 return status;
270}
271EXPORT_SYMBOL(dma_sync_wait);
272
Chris Leechc13c8262006-05-23 17:18:44 -0700273/**
Randy Dunlap65088712006-07-03 19:45:31 -0700274 * dma_chan_cleanup - release a DMA channel's resources
275 * @kref: kernel reference structure that contains the DMA channel device
Chris Leechc13c8262006-05-23 17:18:44 -0700276 */
277void dma_chan_cleanup(struct kref *kref)
278{
279 struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
Chris Leechc13c8262006-05-23 17:18:44 -0700280 kref_put(&chan->device->refcount, dma_async_device_cleanup);
281}
David Brownell765e3d82007-03-16 13:38:05 -0800282EXPORT_SYMBOL(dma_chan_cleanup);
Chris Leechc13c8262006-05-23 17:18:44 -0700283
284static void dma_chan_free_rcu(struct rcu_head *rcu)
285{
286 struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
Dan Williams6f49a572009-01-06 11:38:14 -0700287
Chris Leechc13c8262006-05-23 17:18:44 -0700288 kref_put(&chan->refcount, dma_chan_cleanup);
289}
290
Dan Williamsd379b012007-07-09 11:56:42 -0700291static void dma_chan_release(struct dma_chan *chan)
Chris Leechc13c8262006-05-23 17:18:44 -0700292{
Chris Leechc13c8262006-05-23 17:18:44 -0700293 call_rcu(&chan->rcu, dma_chan_free_rcu);
294}
295
296/**
Dan Williamsbec08512009-01-06 11:38:14 -0700297 * dma_cap_mask_all - enable iteration over all operation types
298 */
299static dma_cap_mask_t dma_cap_mask_all;
300
301/**
302 * dma_chan_tbl_ent - tracks channel allocations per core/operation
303 * @chan - associated channel for this entry
304 */
305struct dma_chan_tbl_ent {
306 struct dma_chan *chan;
307};
308
309/**
310 * channel_table - percpu lookup table for memory-to-memory offload providers
311 */
312static struct dma_chan_tbl_ent *channel_table[DMA_TX_TYPE_END];
313
314static int __init dma_channel_table_init(void)
315{
316 enum dma_transaction_type cap;
317 int err = 0;
318
319 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
320
Dan Williams59b5ec22009-01-06 11:38:15 -0700321 /* 'interrupt', 'private', and 'slave' are channel capabilities,
322 * but are not associated with an operation so they do not need
323 * an entry in the channel_table
Dan Williamsbec08512009-01-06 11:38:14 -0700324 */
325 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
Dan Williams59b5ec22009-01-06 11:38:15 -0700326 clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
Dan Williamsbec08512009-01-06 11:38:14 -0700327 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
328
329 for_each_dma_cap_mask(cap, dma_cap_mask_all) {
330 channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
331 if (!channel_table[cap]) {
332 err = -ENOMEM;
333 break;
334 }
335 }
336
337 if (err) {
338 pr_err("dmaengine: initialization failure\n");
339 for_each_dma_cap_mask(cap, dma_cap_mask_all)
340 if (channel_table[cap])
341 free_percpu(channel_table[cap]);
342 }
343
344 return err;
345}
346subsys_initcall(dma_channel_table_init);
347
348/**
349 * dma_find_channel - find a channel to carry out the operation
350 * @tx_type: transaction type
351 */
352struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
353{
354 struct dma_chan *chan;
355 int cpu;
356
357 WARN_ONCE(dmaengine_ref_count == 0,
358 "client called %s without a reference", __func__);
359
360 cpu = get_cpu();
361 chan = per_cpu_ptr(channel_table[tx_type], cpu)->chan;
362 put_cpu();
363
364 return chan;
365}
366EXPORT_SYMBOL(dma_find_channel);
367
368/**
Dan Williams2ba05622009-01-06 11:38:14 -0700369 * dma_issue_pending_all - flush all pending operations across all channels
370 */
371void dma_issue_pending_all(void)
372{
373 struct dma_device *device;
374 struct dma_chan *chan;
375
376 WARN_ONCE(dmaengine_ref_count == 0,
377 "client called %s without a reference", __func__);
378
379 rcu_read_lock();
Dan Williams59b5ec22009-01-06 11:38:15 -0700380 list_for_each_entry_rcu(device, &dma_device_list, global_node) {
381 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
382 continue;
Dan Williams2ba05622009-01-06 11:38:14 -0700383 list_for_each_entry(chan, &device->channels, device_node)
384 if (chan->client_count)
385 device->device_issue_pending(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700386 }
Dan Williams2ba05622009-01-06 11:38:14 -0700387 rcu_read_unlock();
388}
389EXPORT_SYMBOL(dma_issue_pending_all);
390
391/**
Dan Williamsbec08512009-01-06 11:38:14 -0700392 * nth_chan - returns the nth channel of the given capability
393 * @cap: capability to match
394 * @n: nth channel desired
395 *
396 * Defaults to returning the channel with the desired capability and the
397 * lowest reference count when 'n' cannot be satisfied. Must be called
398 * under dma_list_mutex.
399 */
400static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
401{
402 struct dma_device *device;
403 struct dma_chan *chan;
404 struct dma_chan *ret = NULL;
405 struct dma_chan *min = NULL;
406
407 list_for_each_entry(device, &dma_device_list, global_node) {
Dan Williams59b5ec22009-01-06 11:38:15 -0700408 if (!dma_has_cap(cap, device->cap_mask) ||
409 dma_has_cap(DMA_PRIVATE, device->cap_mask))
Dan Williamsbec08512009-01-06 11:38:14 -0700410 continue;
411 list_for_each_entry(chan, &device->channels, device_node) {
412 if (!chan->client_count)
413 continue;
414 if (!min)
415 min = chan;
416 else if (chan->table_count < min->table_count)
417 min = chan;
418
419 if (n-- == 0) {
420 ret = chan;
421 break; /* done */
422 }
423 }
424 if (ret)
425 break; /* done */
426 }
427
428 if (!ret)
429 ret = min;
430
431 if (ret)
432 ret->table_count++;
433
434 return ret;
435}
436
437/**
438 * dma_channel_rebalance - redistribute the available channels
439 *
440 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
441 * operation type) in the SMP case, and operation isolation (avoid
442 * multi-tasking channels) in the non-SMP case. Must be called under
443 * dma_list_mutex.
444 */
445static void dma_channel_rebalance(void)
446{
447 struct dma_chan *chan;
448 struct dma_device *device;
449 int cpu;
450 int cap;
451 int n;
452
453 /* undo the last distribution */
454 for_each_dma_cap_mask(cap, dma_cap_mask_all)
455 for_each_possible_cpu(cpu)
456 per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
457
Dan Williams59b5ec22009-01-06 11:38:15 -0700458 list_for_each_entry(device, &dma_device_list, global_node) {
459 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
460 continue;
Dan Williamsbec08512009-01-06 11:38:14 -0700461 list_for_each_entry(chan, &device->channels, device_node)
462 chan->table_count = 0;
Dan Williams59b5ec22009-01-06 11:38:15 -0700463 }
Dan Williamsbec08512009-01-06 11:38:14 -0700464
465 /* don't populate the channel_table if no clients are available */
466 if (!dmaengine_ref_count)
467 return;
468
469 /* redistribute available channels */
470 n = 0;
471 for_each_dma_cap_mask(cap, dma_cap_mask_all)
472 for_each_online_cpu(cpu) {
473 if (num_possible_cpus() > 1)
474 chan = nth_chan(cap, n++);
475 else
476 chan = nth_chan(cap, -1);
477
478 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
479 }
480}
481
Dan Williams59b5ec22009-01-06 11:38:15 -0700482static struct dma_chan *private_candidate(dma_cap_mask_t *mask, struct dma_device *dev)
483{
484 struct dma_chan *chan;
485 struct dma_chan *ret = NULL;
486
487 if (!__dma_device_satisfies_mask(dev, mask)) {
488 pr_debug("%s: wrong capabilities\n", __func__);
489 return NULL;
490 }
491 /* devices with multiple channels need special handling as we need to
492 * ensure that all channels are either private or public.
493 */
494 if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
495 list_for_each_entry(chan, &dev->channels, device_node) {
496 /* some channels are already publicly allocated */
497 if (chan->client_count)
498 return NULL;
499 }
500
501 list_for_each_entry(chan, &dev->channels, device_node) {
502 if (chan->client_count) {
503 pr_debug("%s: %s busy\n",
504 __func__, dev_name(&chan->dev));
505 continue;
506 }
507 ret = chan;
508 break;
509 }
510
511 return ret;
512}
513
514/**
515 * dma_request_channel - try to allocate an exclusive channel
516 * @mask: capabilities that the channel must satisfy
517 * @fn: optional callback to disposition available channels
518 * @fn_param: opaque parameter to pass to dma_filter_fn
519 */
520struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param)
521{
522 struct dma_device *device, *_d;
523 struct dma_chan *chan = NULL;
524 enum dma_state_client ack;
525 int err;
526
527 /* Find a channel */
528 mutex_lock(&dma_list_mutex);
529 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
530 chan = private_candidate(mask, device);
531 if (!chan)
532 continue;
533
534 if (fn)
535 ack = fn(chan, fn_param);
536 else
537 ack = DMA_ACK;
538
539 if (ack == DMA_ACK) {
540 /* Found a suitable channel, try to grab, prep, and
541 * return it. We first set DMA_PRIVATE to disable
542 * balance_ref_count as this channel will not be
543 * published in the general-purpose allocator
544 */
545 dma_cap_set(DMA_PRIVATE, device->cap_mask);
546 err = dma_chan_get(chan);
547
548 if (err == -ENODEV) {
549 pr_debug("%s: %s module removed\n", __func__,
550 dev_name(&chan->dev));
551 list_del_rcu(&device->global_node);
552 } else if (err)
553 pr_err("dmaengine: failed to get %s: (%d)\n",
554 dev_name(&chan->dev), err);
555 else
556 break;
557 } else if (ack == DMA_DUP) {
558 pr_debug("%s: %s filter said DMA_DUP\n",
559 __func__, dev_name(&chan->dev));
560 } else if (ack == DMA_NAK) {
561 pr_debug("%s: %s filter said DMA_NAK\n",
562 __func__, dev_name(&chan->dev));
563 break;
564 } else
565 WARN_ONCE(1, "filter_fn: unknown response?\n");
566 chan = NULL;
567 }
568 mutex_unlock(&dma_list_mutex);
569
570 pr_debug("%s: %s (%s)\n", __func__, chan ? "success" : "fail",
571 chan ? dev_name(&chan->dev) : NULL);
572
573 return chan;
574}
575EXPORT_SYMBOL_GPL(__dma_request_channel);
576
577void dma_release_channel(struct dma_chan *chan)
578{
579 mutex_lock(&dma_list_mutex);
580 WARN_ONCE(chan->client_count != 1,
581 "chan reference count %d != 1\n", chan->client_count);
582 dma_chan_put(chan);
583 mutex_unlock(&dma_list_mutex);
584}
585EXPORT_SYMBOL_GPL(dma_release_channel);
586
Dan Williamsbec08512009-01-06 11:38:14 -0700587/**
Dan Williamsd379b012007-07-09 11:56:42 -0700588 * dma_chans_notify_available - broadcast available channels to the clients
Chris Leechc13c8262006-05-23 17:18:44 -0700589 */
Dan Williamsd379b012007-07-09 11:56:42 -0700590static void dma_clients_notify_available(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700591{
592 struct dma_client *client;
Dan Williamsd379b012007-07-09 11:56:42 -0700593
594 mutex_lock(&dma_list_mutex);
595
596 list_for_each_entry(client, &dma_client_list, global_node)
597 dma_client_chan_alloc(client);
598
599 mutex_unlock(&dma_list_mutex);
600}
601
602/**
Dan Williamsd379b012007-07-09 11:56:42 -0700603 * dma_async_client_register - register a &dma_client
604 * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask'
Chris Leechc13c8262006-05-23 17:18:44 -0700605 */
Dan Williamsd379b012007-07-09 11:56:42 -0700606void dma_async_client_register(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700607{
Dan Williams6f49a572009-01-06 11:38:14 -0700608 struct dma_device *device, *_d;
609 struct dma_chan *chan;
610 int err;
611
Chris Leechc13c8262006-05-23 17:18:44 -0700612 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700613 dmaengine_ref_count++;
614
615 /* try to grab channels */
Dan Williams59b5ec22009-01-06 11:38:15 -0700616 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
617 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
618 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700619 list_for_each_entry(chan, &device->channels, device_node) {
620 err = dma_chan_get(chan);
621 if (err == -ENODEV) {
622 /* module removed before we could use it */
Dan Williams2ba05622009-01-06 11:38:14 -0700623 list_del_rcu(&device->global_node);
Dan Williams6f49a572009-01-06 11:38:14 -0700624 break;
625 } else if (err)
626 pr_err("dmaengine: failed to get %s: (%d)\n",
627 dev_name(&chan->dev), err);
628 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700629 }
Dan Williams6f49a572009-01-06 11:38:14 -0700630
Dan Williamsbec08512009-01-06 11:38:14 -0700631 /* if this is the first reference and there were channels
632 * waiting we need to rebalance to get those channels
633 * incorporated into the channel table
634 */
635 if (dmaengine_ref_count == 1)
636 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700637 list_add_tail(&client->global_node, &dma_client_list);
638 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700639}
David Brownell765e3d82007-03-16 13:38:05 -0800640EXPORT_SYMBOL(dma_async_client_register);
Chris Leechc13c8262006-05-23 17:18:44 -0700641
642/**
643 * dma_async_client_unregister - unregister a client and free the &dma_client
Randy Dunlap65088712006-07-03 19:45:31 -0700644 * @client: &dma_client to free
Chris Leechc13c8262006-05-23 17:18:44 -0700645 *
646 * Force frees any allocated DMA channels, frees the &dma_client memory
647 */
648void dma_async_client_unregister(struct dma_client *client)
649{
Dan Williamsd379b012007-07-09 11:56:42 -0700650 struct dma_device *device;
Chris Leechc13c8262006-05-23 17:18:44 -0700651 struct dma_chan *chan;
652
653 if (!client)
654 return;
655
Chris Leechc13c8262006-05-23 17:18:44 -0700656 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700657 dmaengine_ref_count--;
658 BUG_ON(dmaengine_ref_count < 0);
659 /* drop channel references */
Dan Williams59b5ec22009-01-06 11:38:15 -0700660 list_for_each_entry(device, &dma_device_list, global_node) {
661 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
662 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700663 list_for_each_entry(chan, &device->channels, device_node)
664 dma_chan_put(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700665 }
Dan Williamsd379b012007-07-09 11:56:42 -0700666
Chris Leechc13c8262006-05-23 17:18:44 -0700667 list_del(&client->global_node);
668 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700669}
David Brownell765e3d82007-03-16 13:38:05 -0800670EXPORT_SYMBOL(dma_async_client_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -0700671
672/**
Dan Williamsd379b012007-07-09 11:56:42 -0700673 * dma_async_client_chan_request - send all available channels to the
674 * client that satisfy the capability mask
675 * @client - requester
Chris Leechc13c8262006-05-23 17:18:44 -0700676 */
Dan Williamsd379b012007-07-09 11:56:42 -0700677void dma_async_client_chan_request(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700678{
Dan Williamsd379b012007-07-09 11:56:42 -0700679 mutex_lock(&dma_list_mutex);
680 dma_client_chan_alloc(client);
681 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700682}
David Brownell765e3d82007-03-16 13:38:05 -0800683EXPORT_SYMBOL(dma_async_client_chan_request);
Chris Leechc13c8262006-05-23 17:18:44 -0700684
685/**
Randy Dunlap65088712006-07-03 19:45:31 -0700686 * dma_async_device_register - registers DMA devices found
Chris Leechc13c8262006-05-23 17:18:44 -0700687 * @device: &dma_device
688 */
689int dma_async_device_register(struct dma_device *device)
690{
691 static int id;
Jeff Garzikff487fb2007-03-08 09:57:34 -0800692 int chancnt = 0, rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700693 struct dma_chan* chan;
694
695 if (!device)
696 return -ENODEV;
697
Dan Williams7405f742007-01-02 11:10:43 -0700698 /* validate device routines */
699 BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
700 !device->device_prep_dma_memcpy);
701 BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
702 !device->device_prep_dma_xor);
703 BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
704 !device->device_prep_dma_zero_sum);
705 BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
706 !device->device_prep_dma_memset);
Zhang Wei9b941c62008-03-13 17:45:28 -0700707 BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
Dan Williams7405f742007-01-02 11:10:43 -0700708 !device->device_prep_dma_interrupt);
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700709 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
710 !device->device_prep_slave_sg);
711 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
712 !device->device_terminate_all);
Dan Williams7405f742007-01-02 11:10:43 -0700713
714 BUG_ON(!device->device_alloc_chan_resources);
715 BUG_ON(!device->device_free_chan_resources);
Dan Williams7405f742007-01-02 11:10:43 -0700716 BUG_ON(!device->device_is_tx_complete);
717 BUG_ON(!device->device_issue_pending);
718 BUG_ON(!device->dev);
719
Chris Leechc13c8262006-05-23 17:18:44 -0700720 init_completion(&device->done);
721 kref_init(&device->refcount);
Dan Williamsb0b42b12008-12-03 17:17:07 -0700722
723 mutex_lock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700724 device->dev_id = id++;
Dan Williamsb0b42b12008-12-03 17:17:07 -0700725 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700726
727 /* represent channels in sysfs. Probably want devs too */
728 list_for_each_entry(chan, &device->channels, device_node) {
729 chan->local = alloc_percpu(typeof(*chan->local));
730 if (chan->local == NULL)
731 continue;
732
733 chan->chan_id = chancnt++;
Tony Jones891f78e2007-09-25 02:03:03 +0200734 chan->dev.class = &dma_devclass;
Haavard Skinnemoen1099dc72008-07-08 11:58:05 -0700735 chan->dev.parent = device->dev;
Kay Sievers06190d82008-11-11 13:12:33 -0700736 dev_set_name(&chan->dev, "dma%dchan%d",
737 device->dev_id, chan->chan_id);
Chris Leechc13c8262006-05-23 17:18:44 -0700738
Tony Jones891f78e2007-09-25 02:03:03 +0200739 rc = device_register(&chan->dev);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800740 if (rc) {
741 chancnt--;
742 free_percpu(chan->local);
743 chan->local = NULL;
744 goto err_out;
745 }
746
Haavard Skinnemoen348badf2007-11-14 16:59:27 -0800747 /* One for the channel, one of the class device */
748 kref_get(&device->refcount);
Chris Leechc13c8262006-05-23 17:18:44 -0700749 kref_get(&device->refcount);
Dan Williamsd379b012007-07-09 11:56:42 -0700750 kref_init(&chan->refcount);
Dan Williams7cc5bf92008-07-08 11:58:21 -0700751 chan->client_count = 0;
Dan Williamsd379b012007-07-09 11:56:42 -0700752 chan->slow_ref = 0;
753 INIT_RCU_HEAD(&chan->rcu);
Chris Leechc13c8262006-05-23 17:18:44 -0700754 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700755 device->chancnt = chancnt;
Chris Leechc13c8262006-05-23 17:18:44 -0700756
757 mutex_lock(&dma_list_mutex);
Dan Williams59b5ec22009-01-06 11:38:15 -0700758 /* take references on public channels */
759 if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
Dan Williams6f49a572009-01-06 11:38:14 -0700760 list_for_each_entry(chan, &device->channels, device_node) {
761 /* if clients are already waiting for channels we need
762 * to take references on their behalf
763 */
764 if (dma_chan_get(chan) == -ENODEV) {
765 /* note we can only get here for the first
766 * channel as the remaining channels are
767 * guaranteed to get a reference
768 */
769 rc = -ENODEV;
770 mutex_unlock(&dma_list_mutex);
771 goto err_out;
772 }
773 }
Dan Williams2ba05622009-01-06 11:38:14 -0700774 list_add_tail_rcu(&device->global_node, &dma_device_list);
Dan Williamsbec08512009-01-06 11:38:14 -0700775 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700776 mutex_unlock(&dma_list_mutex);
777
Dan Williamsd379b012007-07-09 11:56:42 -0700778 dma_clients_notify_available();
Chris Leechc13c8262006-05-23 17:18:44 -0700779
780 return 0;
Jeff Garzikff487fb2007-03-08 09:57:34 -0800781
782err_out:
783 list_for_each_entry(chan, &device->channels, device_node) {
784 if (chan->local == NULL)
785 continue;
786 kref_put(&device->refcount, dma_async_device_cleanup);
Tony Jones891f78e2007-09-25 02:03:03 +0200787 device_unregister(&chan->dev);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800788 chancnt--;
789 free_percpu(chan->local);
790 }
791 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700792}
David Brownell765e3d82007-03-16 13:38:05 -0800793EXPORT_SYMBOL(dma_async_device_register);
Chris Leechc13c8262006-05-23 17:18:44 -0700794
795/**
Randy Dunlap65088712006-07-03 19:45:31 -0700796 * dma_async_device_cleanup - function called when all references are released
797 * @kref: kernel reference object
Chris Leechc13c8262006-05-23 17:18:44 -0700798 */
799static void dma_async_device_cleanup(struct kref *kref)
800{
801 struct dma_device *device;
802
803 device = container_of(kref, struct dma_device, refcount);
804 complete(&device->done);
805}
806
Randy Dunlap65088712006-07-03 19:45:31 -0700807/**
Dan Williams6f49a572009-01-06 11:38:14 -0700808 * dma_async_device_unregister - unregister a DMA device
Randy Dunlap65088712006-07-03 19:45:31 -0700809 * @device: &dma_device
810 */
811void dma_async_device_unregister(struct dma_device *device)
Chris Leechc13c8262006-05-23 17:18:44 -0700812{
813 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700814
815 mutex_lock(&dma_list_mutex);
Dan Williams2ba05622009-01-06 11:38:14 -0700816 list_del_rcu(&device->global_node);
Dan Williamsbec08512009-01-06 11:38:14 -0700817 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700818 mutex_unlock(&dma_list_mutex);
819
820 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williams6f49a572009-01-06 11:38:14 -0700821 WARN_ONCE(chan->client_count,
822 "%s called while %d clients hold a reference\n",
823 __func__, chan->client_count);
Tony Jones891f78e2007-09-25 02:03:03 +0200824 device_unregister(&chan->dev);
Dan Williamsd379b012007-07-09 11:56:42 -0700825 dma_chan_release(chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700826 }
Chris Leechc13c8262006-05-23 17:18:44 -0700827
828 kref_put(&device->refcount, dma_async_device_cleanup);
829 wait_for_completion(&device->done);
830}
David Brownell765e3d82007-03-16 13:38:05 -0800831EXPORT_SYMBOL(dma_async_device_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -0700832
Dan Williams7405f742007-01-02 11:10:43 -0700833/**
834 * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
835 * @chan: DMA channel to offload copy to
836 * @dest: destination address (virtual)
837 * @src: source address (virtual)
838 * @len: length
839 *
840 * Both @dest and @src must be mappable to a bus address according to the
841 * DMA mapping API rules for streaming mappings.
842 * Both @dest and @src must stay memory resident (kernel memory or locked
843 * user space pages).
844 */
845dma_cookie_t
846dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
847 void *src, size_t len)
848{
849 struct dma_device *dev = chan->device;
850 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700851 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700852 dma_cookie_t cookie;
853 int cpu;
854
Dan Williams00367312008-02-02 19:49:57 -0700855 dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
856 dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700857 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
858 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700859
860 if (!tx) {
861 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
862 dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700863 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700864 }
Dan Williams7405f742007-01-02 11:10:43 -0700865
Dan Williams7405f742007-01-02 11:10:43 -0700866 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700867 cookie = tx->tx_submit(tx);
868
869 cpu = get_cpu();
870 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
871 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
872 put_cpu();
873
874 return cookie;
875}
876EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
877
878/**
879 * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
880 * @chan: DMA channel to offload copy to
881 * @page: destination page
882 * @offset: offset in page to copy to
883 * @kdata: source address (virtual)
884 * @len: length
885 *
886 * Both @page/@offset and @kdata must be mappable to a bus address according
887 * to the DMA mapping API rules for streaming mappings.
888 * Both @page/@offset and @kdata must stay memory resident (kernel memory or
889 * locked user space pages)
890 */
891dma_cookie_t
892dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
893 unsigned int offset, void *kdata, size_t len)
894{
895 struct dma_device *dev = chan->device;
896 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700897 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700898 dma_cookie_t cookie;
899 int cpu;
900
Dan Williams00367312008-02-02 19:49:57 -0700901 dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
902 dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700903 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
904 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700905
906 if (!tx) {
907 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
908 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700909 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700910 }
Dan Williams7405f742007-01-02 11:10:43 -0700911
Dan Williams7405f742007-01-02 11:10:43 -0700912 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700913 cookie = tx->tx_submit(tx);
914
915 cpu = get_cpu();
916 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
917 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
918 put_cpu();
919
920 return cookie;
921}
922EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
923
924/**
925 * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
926 * @chan: DMA channel to offload copy to
927 * @dest_pg: destination page
928 * @dest_off: offset in page to copy to
929 * @src_pg: source page
930 * @src_off: offset in page to copy from
931 * @len: length
932 *
933 * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
934 * address according to the DMA mapping API rules for streaming mappings.
935 * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
936 * (kernel memory or locked user space pages).
937 */
938dma_cookie_t
939dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
940 unsigned int dest_off, struct page *src_pg, unsigned int src_off,
941 size_t len)
942{
943 struct dma_device *dev = chan->device;
944 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700945 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700946 dma_cookie_t cookie;
947 int cpu;
948
Dan Williams00367312008-02-02 19:49:57 -0700949 dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
950 dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
951 DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700952 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
953 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700954
955 if (!tx) {
956 dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
957 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700958 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700959 }
Dan Williams7405f742007-01-02 11:10:43 -0700960
Dan Williams7405f742007-01-02 11:10:43 -0700961 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700962 cookie = tx->tx_submit(tx);
963
964 cpu = get_cpu();
965 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
966 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
967 put_cpu();
968
969 return cookie;
970}
971EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
972
973void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
974 struct dma_chan *chan)
975{
976 tx->chan = chan;
977 spin_lock_init(&tx->lock);
Dan Williams7405f742007-01-02 11:10:43 -0700978}
979EXPORT_SYMBOL(dma_async_tx_descriptor_init);
980
Dan Williams07f22112009-01-05 17:14:31 -0700981/* dma_wait_for_async_tx - spin wait for a transaction to complete
982 * @tx: in-flight transaction to wait on
983 *
984 * This routine assumes that tx was obtained from a call to async_memcpy,
985 * async_xor, async_memset, etc which ensures that tx is "in-flight" (prepped
986 * and submitted). Walking the parent chain is only meant to cover for DMA
987 * drivers that do not implement the DMA_INTERRUPT capability and may race with
988 * the driver's descriptor cleanup routine.
989 */
990enum dma_status
991dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
992{
993 enum dma_status status;
994 struct dma_async_tx_descriptor *iter;
995 struct dma_async_tx_descriptor *parent;
996
997 if (!tx)
998 return DMA_SUCCESS;
999
1000 WARN_ONCE(tx->parent, "%s: speculatively walking dependency chain for"
1001 " %s\n", __func__, dev_name(&tx->chan->dev));
1002
1003 /* poll through the dependency chain, return when tx is complete */
1004 do {
1005 iter = tx;
1006
1007 /* find the root of the unsubmitted dependency chain */
1008 do {
1009 parent = iter->parent;
1010 if (!parent)
1011 break;
1012 else
1013 iter = parent;
1014 } while (parent);
1015
1016 /* there is a small window for ->parent == NULL and
1017 * ->cookie == -EBUSY
1018 */
1019 while (iter->cookie == -EBUSY)
1020 cpu_relax();
1021
1022 status = dma_sync_wait(iter->chan, iter->cookie);
1023 } while (status == DMA_IN_PROGRESS || (iter != tx));
1024
1025 return status;
1026}
1027EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
1028
1029/* dma_run_dependencies - helper routine for dma drivers to process
1030 * (start) dependent operations on their target channel
1031 * @tx: transaction with dependencies
1032 */
1033void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
1034{
1035 struct dma_async_tx_descriptor *dep = tx->next;
1036 struct dma_async_tx_descriptor *dep_next;
1037 struct dma_chan *chan;
1038
1039 if (!dep)
1040 return;
1041
1042 chan = dep->chan;
1043
1044 /* keep submitting up until a channel switch is detected
1045 * in that case we will be called again as a result of
1046 * processing the interrupt from async_tx_channel_switch
1047 */
1048 for (; dep; dep = dep_next) {
1049 spin_lock_bh(&dep->lock);
1050 dep->parent = NULL;
1051 dep_next = dep->next;
1052 if (dep_next && dep_next->chan == chan)
1053 dep->next = NULL; /* ->next will be submitted */
1054 else
1055 dep_next = NULL; /* submit current dep and terminate */
1056 spin_unlock_bh(&dep->lock);
1057
1058 dep->tx_submit(dep);
1059 }
1060
1061 chan->device->device_issue_pending(chan);
1062}
1063EXPORT_SYMBOL_GPL(dma_run_dependencies);
1064
Chris Leechc13c8262006-05-23 17:18:44 -07001065static int __init dma_bus_init(void)
1066{
1067 mutex_init(&dma_list_mutex);
1068 return class_register(&dma_devclass);
1069}
Chris Leechc13c8262006-05-23 17:18:44 -07001070subsys_initcall(dma_bus_init);
1071
Dan Williamsbec08512009-01-06 11:38:14 -07001072