blob: d37cf95987b64f22bf0705631886d0810bfa026c [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 *
Dan Williamsaa1e6f12009-01-06 11:38:17 -070034 * The subsystem keeps a global list of dma_device structs it is protected by a
35 * mutex, dma_list_mutex.
Chris Leechc13c8262006-05-23 17:18:44 -070036 *
Dan Williamsf27c5802009-01-06 11:38:18 -070037 * A subsystem can get access to a channel by calling dmaengine_get() followed
38 * by dma_find_channel(), or if it has need for an exclusive channel it can call
39 * dma_request_channel(). Once a channel is allocated a reference is taken
40 * against its corresponding driver to disable removal.
41 *
Chris Leechc13c8262006-05-23 17:18:44 -070042 * Each device has a channels list, which runs unlocked but is never modified
43 * once the device is registered, it's just setup by the driver.
44 *
Dan Williamsf27c5802009-01-06 11:38:18 -070045 * See Documentation/dmaengine.txt for more details
Chris Leechc13c8262006-05-23 17:18:44 -070046 */
47
Joe Perches63433252012-07-18 09:51:28 -070048#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000050#include <linux/dma-mapping.h>
Chris Leechc13c8262006-05-23 17:18:44 -070051#include <linux/init.h>
52#include <linux/module.h>
Dan Williams7405f742007-01-02 11:10:43 -070053#include <linux/mm.h>
Chris Leechc13c8262006-05-23 17:18:44 -070054#include <linux/device.h>
55#include <linux/dmaengine.h>
56#include <linux/hardirq.h>
57#include <linux/spinlock.h>
58#include <linux/percpu.h>
59#include <linux/rcupdate.h>
60#include <linux/mutex.h>
Dan Williams7405f742007-01-02 11:10:43 -070061#include <linux/jiffies.h>
Dan Williams2ba05622009-01-06 11:38:14 -070062#include <linux/rculist.h>
Dan Williams864498a2009-01-06 11:38:21 -070063#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090064#include <linux/slab.h>
Jon Hunter9a6cecc2012-09-14 17:41:57 -050065#include <linux/of_dma.h>
Chris Leechc13c8262006-05-23 17:18:44 -070066
67static DEFINE_MUTEX(dma_list_mutex);
Axel Lin21ef4b82011-07-20 11:32:28 +080068static DEFINE_IDR(dma_idr);
Chris Leechc13c8262006-05-23 17:18:44 -070069static LIST_HEAD(dma_device_list);
Dan Williams6f49a572009-01-06 11:38:14 -070070static long dmaengine_ref_count;
Chris Leechc13c8262006-05-23 17:18:44 -070071
72/* --- sysfs implementation --- */
73
Dan Williams41d5e592009-01-06 11:38:21 -070074/**
75 * dev_to_dma_chan - convert a device pointer to the its sysfs container object
76 * @dev - device node
77 *
78 * Must be called under dma_list_mutex
79 */
80static struct dma_chan *dev_to_dma_chan(struct device *dev)
81{
82 struct dma_chan_dev *chan_dev;
83
84 chan_dev = container_of(dev, typeof(*chan_dev), device);
85 return chan_dev->chan;
86}
87
Tony Jones891f78e2007-09-25 02:03:03 +020088static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -070089{
Dan Williams41d5e592009-01-06 11:38:21 -070090 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -070091 unsigned long count = 0;
92 int i;
Dan Williams41d5e592009-01-06 11:38:21 -070093 int err;
Chris Leechc13c8262006-05-23 17:18:44 -070094
Dan Williams41d5e592009-01-06 11:38:21 -070095 mutex_lock(&dma_list_mutex);
96 chan = dev_to_dma_chan(dev);
97 if (chan) {
98 for_each_possible_cpu(i)
99 count += per_cpu_ptr(chan->local, i)->memcpy_count;
100 err = sprintf(buf, "%lu\n", count);
101 } else
102 err = -ENODEV;
103 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700104
Dan Williams41d5e592009-01-06 11:38:21 -0700105 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700106}
107
Tony Jones891f78e2007-09-25 02:03:03 +0200108static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr,
109 char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700110{
Dan Williams41d5e592009-01-06 11:38:21 -0700111 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700112 unsigned long count = 0;
113 int i;
Dan Williams41d5e592009-01-06 11:38:21 -0700114 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700115
Dan Williams41d5e592009-01-06 11:38:21 -0700116 mutex_lock(&dma_list_mutex);
117 chan = dev_to_dma_chan(dev);
118 if (chan) {
119 for_each_possible_cpu(i)
120 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
121 err = sprintf(buf, "%lu\n", count);
122 } else
123 err = -ENODEV;
124 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700125
Dan Williams41d5e592009-01-06 11:38:21 -0700126 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700127}
128
Tony Jones891f78e2007-09-25 02:03:03 +0200129static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700130{
Dan Williams41d5e592009-01-06 11:38:21 -0700131 struct dma_chan *chan;
132 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700133
Dan Williams41d5e592009-01-06 11:38:21 -0700134 mutex_lock(&dma_list_mutex);
135 chan = dev_to_dma_chan(dev);
136 if (chan)
137 err = sprintf(buf, "%d\n", chan->client_count);
138 else
139 err = -ENODEV;
140 mutex_unlock(&dma_list_mutex);
141
142 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700143}
144
Tony Jones891f78e2007-09-25 02:03:03 +0200145static struct device_attribute dma_attrs[] = {
Chris Leechc13c8262006-05-23 17:18:44 -0700146 __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
147 __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
148 __ATTR(in_use, S_IRUGO, show_in_use, NULL),
149 __ATTR_NULL
150};
151
Dan Williams41d5e592009-01-06 11:38:21 -0700152static void chan_dev_release(struct device *dev)
153{
154 struct dma_chan_dev *chan_dev;
155
156 chan_dev = container_of(dev, typeof(*chan_dev), device);
Dan Williams864498a2009-01-06 11:38:21 -0700157 if (atomic_dec_and_test(chan_dev->idr_ref)) {
158 mutex_lock(&dma_list_mutex);
159 idr_remove(&dma_idr, chan_dev->dev_id);
160 mutex_unlock(&dma_list_mutex);
161 kfree(chan_dev->idr_ref);
162 }
Dan Williams41d5e592009-01-06 11:38:21 -0700163 kfree(chan_dev);
164}
165
Chris Leechc13c8262006-05-23 17:18:44 -0700166static struct class dma_devclass = {
Tony Jones891f78e2007-09-25 02:03:03 +0200167 .name = "dma",
168 .dev_attrs = dma_attrs,
Dan Williams41d5e592009-01-06 11:38:21 -0700169 .dev_release = chan_dev_release,
Chris Leechc13c8262006-05-23 17:18:44 -0700170};
171
172/* --- client and device registration --- */
173
Dan Williams59b5ec22009-01-06 11:38:15 -0700174#define dma_device_satisfies_mask(device, mask) \
175 __dma_device_satisfies_mask((device), &(mask))
Dan Williamsd379b012007-07-09 11:56:42 -0700176static int
Dan Williams59b5ec22009-01-06 11:38:15 -0700177__dma_device_satisfies_mask(struct dma_device *device, dma_cap_mask_t *want)
Dan Williamsd379b012007-07-09 11:56:42 -0700178{
179 dma_cap_mask_t has;
180
Dan Williams59b5ec22009-01-06 11:38:15 -0700181 bitmap_and(has.bits, want->bits, device->cap_mask.bits,
Dan Williamsd379b012007-07-09 11:56:42 -0700182 DMA_TX_TYPE_END);
183 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
184}
185
Dan Williams6f49a572009-01-06 11:38:14 -0700186static struct module *dma_chan_to_owner(struct dma_chan *chan)
187{
188 return chan->device->dev->driver->owner;
189}
190
191/**
192 * balance_ref_count - catch up the channel reference count
193 * @chan - channel to balance ->client_count versus dmaengine_ref_count
194 *
195 * balance_ref_count must be called under dma_list_mutex
196 */
197static void balance_ref_count(struct dma_chan *chan)
198{
199 struct module *owner = dma_chan_to_owner(chan);
200
201 while (chan->client_count < dmaengine_ref_count) {
202 __module_get(owner);
203 chan->client_count++;
204 }
205}
206
207/**
208 * dma_chan_get - try to grab a dma channel's parent driver module
209 * @chan - channel to grab
210 *
211 * Must be called under dma_list_mutex
212 */
213static int dma_chan_get(struct dma_chan *chan)
214{
215 int err = -ENODEV;
216 struct module *owner = dma_chan_to_owner(chan);
217
218 if (chan->client_count) {
219 __module_get(owner);
220 err = 0;
221 } else if (try_module_get(owner))
222 err = 0;
223
224 if (err == 0)
225 chan->client_count++;
226
227 /* allocate upon first client reference */
228 if (chan->client_count == 1 && err == 0) {
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700229 int desc_cnt = chan->device->device_alloc_chan_resources(chan);
Dan Williams6f49a572009-01-06 11:38:14 -0700230
231 if (desc_cnt < 0) {
232 err = desc_cnt;
233 chan->client_count = 0;
234 module_put(owner);
Dan Williams59b5ec22009-01-06 11:38:15 -0700235 } else if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
Dan Williams6f49a572009-01-06 11:38:14 -0700236 balance_ref_count(chan);
237 }
238
239 return err;
240}
241
242/**
243 * dma_chan_put - drop a reference to a dma channel's parent driver module
244 * @chan - channel to release
245 *
246 * Must be called under dma_list_mutex
247 */
248static void dma_chan_put(struct dma_chan *chan)
249{
250 if (!chan->client_count)
251 return; /* this channel failed alloc_chan_resources */
252 chan->client_count--;
253 module_put(dma_chan_to_owner(chan));
254 if (chan->client_count == 0)
255 chan->device->device_free_chan_resources(chan);
256}
257
Dan Williams7405f742007-01-02 11:10:43 -0700258enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
259{
260 enum dma_status status;
261 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
262
263 dma_async_issue_pending(chan);
264 do {
265 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
266 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
Joe Perches63433252012-07-18 09:51:28 -0700267 pr_err("%s: timeout!\n", __func__);
Dan Williams7405f742007-01-02 11:10:43 -0700268 return DMA_ERROR;
269 }
270 } while (status == DMA_IN_PROGRESS);
271
272 return status;
273}
274EXPORT_SYMBOL(dma_sync_wait);
275
Chris Leechc13c8262006-05-23 17:18:44 -0700276/**
Dan Williamsbec08512009-01-06 11:38:14 -0700277 * dma_cap_mask_all - enable iteration over all operation types
278 */
279static dma_cap_mask_t dma_cap_mask_all;
280
281/**
282 * dma_chan_tbl_ent - tracks channel allocations per core/operation
283 * @chan - associated channel for this entry
284 */
285struct dma_chan_tbl_ent {
286 struct dma_chan *chan;
287};
288
289/**
290 * channel_table - percpu lookup table for memory-to-memory offload providers
291 */
Tejun Heoa29d8b82010-02-02 14:39:15 +0900292static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
Dan Williamsbec08512009-01-06 11:38:14 -0700293
294static int __init dma_channel_table_init(void)
295{
296 enum dma_transaction_type cap;
297 int err = 0;
298
299 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
300
Dan Williams59b5ec22009-01-06 11:38:15 -0700301 /* 'interrupt', 'private', and 'slave' are channel capabilities,
302 * but are not associated with an operation so they do not need
303 * an entry in the channel_table
Dan Williamsbec08512009-01-06 11:38:14 -0700304 */
305 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
Dan Williams59b5ec22009-01-06 11:38:15 -0700306 clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
Dan Williamsbec08512009-01-06 11:38:14 -0700307 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
308
309 for_each_dma_cap_mask(cap, dma_cap_mask_all) {
310 channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
311 if (!channel_table[cap]) {
312 err = -ENOMEM;
313 break;
314 }
315 }
316
317 if (err) {
Joe Perches63433252012-07-18 09:51:28 -0700318 pr_err("initialization failure\n");
Dan Williamsbec08512009-01-06 11:38:14 -0700319 for_each_dma_cap_mask(cap, dma_cap_mask_all)
320 if (channel_table[cap])
321 free_percpu(channel_table[cap]);
322 }
323
324 return err;
325}
Dan Williams652afc22009-01-06 11:38:22 -0700326arch_initcall(dma_channel_table_init);
Dan Williamsbec08512009-01-06 11:38:14 -0700327
328/**
329 * dma_find_channel - find a channel to carry out the operation
330 * @tx_type: transaction type
331 */
332struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
333{
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900334 return this_cpu_read(channel_table[tx_type]->chan);
Dan Williamsbec08512009-01-06 11:38:14 -0700335}
336EXPORT_SYMBOL(dma_find_channel);
337
Dave Jianga2bd1142012-04-04 16:10:46 -0700338/*
339 * net_dma_find_channel - find a channel for net_dma
340 * net_dma has alignment requirements
341 */
342struct dma_chan *net_dma_find_channel(void)
343{
344 struct dma_chan *chan = dma_find_channel(DMA_MEMCPY);
345 if (chan && !is_dma_copy_aligned(chan->device, 1, 1, 1))
346 return NULL;
347
348 return chan;
349}
350EXPORT_SYMBOL(net_dma_find_channel);
351
Dan Williamsbec08512009-01-06 11:38:14 -0700352/**
Dan Williams2ba05622009-01-06 11:38:14 -0700353 * dma_issue_pending_all - flush all pending operations across all channels
354 */
355void dma_issue_pending_all(void)
356{
357 struct dma_device *device;
358 struct dma_chan *chan;
359
Dan Williams2ba05622009-01-06 11:38:14 -0700360 rcu_read_lock();
Dan Williams59b5ec22009-01-06 11:38:15 -0700361 list_for_each_entry_rcu(device, &dma_device_list, global_node) {
362 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
363 continue;
Dan Williams2ba05622009-01-06 11:38:14 -0700364 list_for_each_entry(chan, &device->channels, device_node)
365 if (chan->client_count)
366 device->device_issue_pending(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700367 }
Dan Williams2ba05622009-01-06 11:38:14 -0700368 rcu_read_unlock();
369}
370EXPORT_SYMBOL(dma_issue_pending_all);
371
372/**
Dan Williamsbec08512009-01-06 11:38:14 -0700373 * nth_chan - returns the nth channel of the given capability
374 * @cap: capability to match
375 * @n: nth channel desired
376 *
377 * Defaults to returning the channel with the desired capability and the
378 * lowest reference count when 'n' cannot be satisfied. Must be called
379 * under dma_list_mutex.
380 */
381static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
382{
383 struct dma_device *device;
384 struct dma_chan *chan;
385 struct dma_chan *ret = NULL;
386 struct dma_chan *min = NULL;
387
388 list_for_each_entry(device, &dma_device_list, global_node) {
Dan Williams59b5ec22009-01-06 11:38:15 -0700389 if (!dma_has_cap(cap, device->cap_mask) ||
390 dma_has_cap(DMA_PRIVATE, device->cap_mask))
Dan Williamsbec08512009-01-06 11:38:14 -0700391 continue;
392 list_for_each_entry(chan, &device->channels, device_node) {
393 if (!chan->client_count)
394 continue;
395 if (!min)
396 min = chan;
397 else if (chan->table_count < min->table_count)
398 min = chan;
399
400 if (n-- == 0) {
401 ret = chan;
402 break; /* done */
403 }
404 }
405 if (ret)
406 break; /* done */
407 }
408
409 if (!ret)
410 ret = min;
411
412 if (ret)
413 ret->table_count++;
414
415 return ret;
416}
417
418/**
419 * dma_channel_rebalance - redistribute the available channels
420 *
421 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
422 * operation type) in the SMP case, and operation isolation (avoid
423 * multi-tasking channels) in the non-SMP case. Must be called under
424 * dma_list_mutex.
425 */
426static void dma_channel_rebalance(void)
427{
428 struct dma_chan *chan;
429 struct dma_device *device;
430 int cpu;
431 int cap;
432 int n;
433
434 /* undo the last distribution */
435 for_each_dma_cap_mask(cap, dma_cap_mask_all)
436 for_each_possible_cpu(cpu)
437 per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
438
Dan Williams59b5ec22009-01-06 11:38:15 -0700439 list_for_each_entry(device, &dma_device_list, global_node) {
440 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
441 continue;
Dan Williamsbec08512009-01-06 11:38:14 -0700442 list_for_each_entry(chan, &device->channels, device_node)
443 chan->table_count = 0;
Dan Williams59b5ec22009-01-06 11:38:15 -0700444 }
Dan Williamsbec08512009-01-06 11:38:14 -0700445
446 /* don't populate the channel_table if no clients are available */
447 if (!dmaengine_ref_count)
448 return;
449
450 /* redistribute available channels */
451 n = 0;
452 for_each_dma_cap_mask(cap, dma_cap_mask_all)
453 for_each_online_cpu(cpu) {
454 if (num_possible_cpus() > 1)
455 chan = nth_chan(cap, n++);
456 else
457 chan = nth_chan(cap, -1);
458
459 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
460 }
461}
462
Dan Williamse2346672009-01-06 11:38:21 -0700463static struct dma_chan *private_candidate(dma_cap_mask_t *mask, struct dma_device *dev,
464 dma_filter_fn fn, void *fn_param)
Dan Williams59b5ec22009-01-06 11:38:15 -0700465{
466 struct dma_chan *chan;
Dan Williams59b5ec22009-01-06 11:38:15 -0700467
468 if (!__dma_device_satisfies_mask(dev, mask)) {
469 pr_debug("%s: wrong capabilities\n", __func__);
470 return NULL;
471 }
472 /* devices with multiple channels need special handling as we need to
473 * ensure that all channels are either private or public.
474 */
475 if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
476 list_for_each_entry(chan, &dev->channels, device_node) {
477 /* some channels are already publicly allocated */
478 if (chan->client_count)
479 return NULL;
480 }
481
482 list_for_each_entry(chan, &dev->channels, device_node) {
483 if (chan->client_count) {
484 pr_debug("%s: %s busy\n",
Dan Williams41d5e592009-01-06 11:38:21 -0700485 __func__, dma_chan_name(chan));
Dan Williams59b5ec22009-01-06 11:38:15 -0700486 continue;
487 }
Dan Williamse2346672009-01-06 11:38:21 -0700488 if (fn && !fn(chan, fn_param)) {
489 pr_debug("%s: %s filter said false\n",
490 __func__, dma_chan_name(chan));
491 continue;
492 }
493 return chan;
Dan Williams59b5ec22009-01-06 11:38:15 -0700494 }
495
Dan Williamse2346672009-01-06 11:38:21 -0700496 return NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700497}
498
499/**
500 * dma_request_channel - try to allocate an exclusive channel
501 * @mask: capabilities that the channel must satisfy
502 * @fn: optional callback to disposition available channels
503 * @fn_param: opaque parameter to pass to dma_filter_fn
504 */
505struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param)
506{
507 struct dma_device *device, *_d;
508 struct dma_chan *chan = NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700509 int err;
510
511 /* Find a channel */
512 mutex_lock(&dma_list_mutex);
513 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
Dan Williamse2346672009-01-06 11:38:21 -0700514 chan = private_candidate(mask, device, fn, fn_param);
515 if (chan) {
Dan Williams59b5ec22009-01-06 11:38:15 -0700516 /* Found a suitable channel, try to grab, prep, and
517 * return it. We first set DMA_PRIVATE to disable
518 * balance_ref_count as this channel will not be
519 * published in the general-purpose allocator
520 */
521 dma_cap_set(DMA_PRIVATE, device->cap_mask);
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900522 device->privatecnt++;
Dan Williams59b5ec22009-01-06 11:38:15 -0700523 err = dma_chan_get(chan);
524
525 if (err == -ENODEV) {
Joe Perches63433252012-07-18 09:51:28 -0700526 pr_debug("%s: %s module removed\n",
527 __func__, dma_chan_name(chan));
Dan Williams59b5ec22009-01-06 11:38:15 -0700528 list_del_rcu(&device->global_node);
529 } else if (err)
Fabio Estevamd8b53482012-02-21 12:51:59 -0200530 pr_debug("%s: failed to get %s: (%d)\n",
Joe Perches63433252012-07-18 09:51:28 -0700531 __func__, dma_chan_name(chan), err);
Dan Williams59b5ec22009-01-06 11:38:15 -0700532 else
533 break;
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900534 if (--device->privatecnt == 0)
535 dma_cap_clear(DMA_PRIVATE, device->cap_mask);
Dan Williamse2346672009-01-06 11:38:21 -0700536 chan = NULL;
537 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700538 }
539 mutex_unlock(&dma_list_mutex);
540
Joe Perches63433252012-07-18 09:51:28 -0700541 pr_debug("%s: %s (%s)\n",
542 __func__,
543 chan ? "success" : "fail",
Dan Williams41d5e592009-01-06 11:38:21 -0700544 chan ? dma_chan_name(chan) : NULL);
Dan Williams59b5ec22009-01-06 11:38:15 -0700545
546 return chan;
547}
548EXPORT_SYMBOL_GPL(__dma_request_channel);
549
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500550/**
551 * dma_request_slave_channel - try to allocate an exclusive slave channel
552 * @dev: pointer to client device structure
553 * @name: slave channel name
554 */
555struct dma_chan *dma_request_slave_channel(struct device *dev, char *name)
556{
557 /* If device-tree is present get slave info from here */
558 if (dev->of_node)
559 return of_dma_request_slave_channel(dev->of_node, name);
560
561 return NULL;
562}
563EXPORT_SYMBOL_GPL(dma_request_slave_channel);
564
Dan Williams59b5ec22009-01-06 11:38:15 -0700565void dma_release_channel(struct dma_chan *chan)
566{
567 mutex_lock(&dma_list_mutex);
568 WARN_ONCE(chan->client_count != 1,
569 "chan reference count %d != 1\n", chan->client_count);
570 dma_chan_put(chan);
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900571 /* drop PRIVATE cap enabled by __dma_request_channel() */
572 if (--chan->device->privatecnt == 0)
573 dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
Dan Williams59b5ec22009-01-06 11:38:15 -0700574 mutex_unlock(&dma_list_mutex);
575}
576EXPORT_SYMBOL_GPL(dma_release_channel);
577
Dan Williamsbec08512009-01-06 11:38:14 -0700578/**
Dan Williams209b84a2009-01-06 11:38:17 -0700579 * dmaengine_get - register interest in dma_channels
Chris Leechc13c8262006-05-23 17:18:44 -0700580 */
Dan Williams209b84a2009-01-06 11:38:17 -0700581void dmaengine_get(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700582{
Dan Williams6f49a572009-01-06 11:38:14 -0700583 struct dma_device *device, *_d;
584 struct dma_chan *chan;
585 int err;
586
Chris Leechc13c8262006-05-23 17:18:44 -0700587 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700588 dmaengine_ref_count++;
589
590 /* try to grab channels */
Dan Williams59b5ec22009-01-06 11:38:15 -0700591 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
592 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
593 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700594 list_for_each_entry(chan, &device->channels, device_node) {
595 err = dma_chan_get(chan);
596 if (err == -ENODEV) {
597 /* module removed before we could use it */
Dan Williams2ba05622009-01-06 11:38:14 -0700598 list_del_rcu(&device->global_node);
Dan Williams6f49a572009-01-06 11:38:14 -0700599 break;
600 } else if (err)
Fabio Estevam0eb5a3582012-10-04 17:11:16 -0700601 pr_debug("%s: failed to get %s: (%d)\n",
Joe Perches63433252012-07-18 09:51:28 -0700602 __func__, dma_chan_name(chan), err);
Dan Williams6f49a572009-01-06 11:38:14 -0700603 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700604 }
Dan Williams6f49a572009-01-06 11:38:14 -0700605
Dan Williamsbec08512009-01-06 11:38:14 -0700606 /* if this is the first reference and there were channels
607 * waiting we need to rebalance to get those channels
608 * incorporated into the channel table
609 */
610 if (dmaengine_ref_count == 1)
611 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700612 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700613}
Dan Williams209b84a2009-01-06 11:38:17 -0700614EXPORT_SYMBOL(dmaengine_get);
Chris Leechc13c8262006-05-23 17:18:44 -0700615
616/**
Dan Williams209b84a2009-01-06 11:38:17 -0700617 * dmaengine_put - let dma drivers be removed when ref_count == 0
Chris Leechc13c8262006-05-23 17:18:44 -0700618 */
Dan Williams209b84a2009-01-06 11:38:17 -0700619void dmaengine_put(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700620{
Dan Williamsd379b012007-07-09 11:56:42 -0700621 struct dma_device *device;
Chris Leechc13c8262006-05-23 17:18:44 -0700622 struct dma_chan *chan;
623
Chris Leechc13c8262006-05-23 17:18:44 -0700624 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700625 dmaengine_ref_count--;
626 BUG_ON(dmaengine_ref_count < 0);
627 /* drop channel references */
Dan Williams59b5ec22009-01-06 11:38:15 -0700628 list_for_each_entry(device, &dma_device_list, global_node) {
629 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
630 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700631 list_for_each_entry(chan, &device->channels, device_node)
632 dma_chan_put(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700633 }
Chris Leechc13c8262006-05-23 17:18:44 -0700634 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700635}
Dan Williams209b84a2009-01-06 11:38:17 -0700636EXPORT_SYMBOL(dmaengine_put);
Chris Leechc13c8262006-05-23 17:18:44 -0700637
Dan Williams138f4c32009-09-08 17:42:51 -0700638static bool device_has_all_tx_types(struct dma_device *device)
639{
640 /* A device that satisfies this test has channels that will never cause
641 * an async_tx channel switch event as all possible operation types can
642 * be handled.
643 */
644 #ifdef CONFIG_ASYNC_TX_DMA
645 if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
646 return false;
647 #endif
648
649 #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE)
650 if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
651 return false;
652 #endif
653
654 #if defined(CONFIG_ASYNC_MEMSET) || defined(CONFIG_ASYNC_MEMSET_MODULE)
655 if (!dma_has_cap(DMA_MEMSET, device->cap_mask))
656 return false;
657 #endif
658
659 #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE)
660 if (!dma_has_cap(DMA_XOR, device->cap_mask))
661 return false;
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700662
663 #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
Dan Williams4499a242009-11-19 17:10:25 -0700664 if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
665 return false;
Dan Williams138f4c32009-09-08 17:42:51 -0700666 #endif
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700667 #endif
Dan Williams138f4c32009-09-08 17:42:51 -0700668
669 #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE)
670 if (!dma_has_cap(DMA_PQ, device->cap_mask))
671 return false;
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700672
673 #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
Dan Williams4499a242009-11-19 17:10:25 -0700674 if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
675 return false;
Dan Williams138f4c32009-09-08 17:42:51 -0700676 #endif
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700677 #endif
Dan Williams138f4c32009-09-08 17:42:51 -0700678
679 return true;
680}
681
Dan Williams257b17c2009-03-25 09:13:23 -0700682static int get_dma_id(struct dma_device *device)
683{
684 int rc;
685
686 idr_retry:
687 if (!idr_pre_get(&dma_idr, GFP_KERNEL))
688 return -ENOMEM;
689 mutex_lock(&dma_list_mutex);
690 rc = idr_get_new(&dma_idr, NULL, &device->dev_id);
691 mutex_unlock(&dma_list_mutex);
692 if (rc == -EAGAIN)
693 goto idr_retry;
694 else if (rc != 0)
695 return rc;
696
697 return 0;
698}
699
Chris Leechc13c8262006-05-23 17:18:44 -0700700/**
Randy Dunlap65088712006-07-03 19:45:31 -0700701 * dma_async_device_register - registers DMA devices found
Chris Leechc13c8262006-05-23 17:18:44 -0700702 * @device: &dma_device
703 */
704int dma_async_device_register(struct dma_device *device)
705{
Jeff Garzikff487fb2007-03-08 09:57:34 -0800706 int chancnt = 0, rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700707 struct dma_chan* chan;
Dan Williams864498a2009-01-06 11:38:21 -0700708 atomic_t *idr_ref;
Chris Leechc13c8262006-05-23 17:18:44 -0700709
710 if (!device)
711 return -ENODEV;
712
Dan Williams7405f742007-01-02 11:10:43 -0700713 /* validate device routines */
714 BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
715 !device->device_prep_dma_memcpy);
716 BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
717 !device->device_prep_dma_xor);
Dan Williams099f53c2009-04-08 14:28:37 -0700718 BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) &&
719 !device->device_prep_dma_xor_val);
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700720 BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) &&
721 !device->device_prep_dma_pq);
722 BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) &&
723 !device->device_prep_dma_pq_val);
Dan Williams7405f742007-01-02 11:10:43 -0700724 BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
725 !device->device_prep_dma_memset);
Zhang Wei9b941c62008-03-13 17:45:28 -0700726 BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
Dan Williams7405f742007-01-02 11:10:43 -0700727 !device->device_prep_dma_interrupt);
Ira Snydera86ee032010-09-30 11:46:44 +0000728 BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) &&
729 !device->device_prep_dma_sg);
Sascha Hauer782bc952010-09-30 13:56:32 +0000730 BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) &&
731 !device->device_prep_dma_cyclic);
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700732 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
Linus Walleijc3635c72010-03-26 16:44:01 -0700733 !device->device_control);
Jassi Brarb14dab72011-10-13 12:33:30 +0530734 BUG_ON(dma_has_cap(DMA_INTERLEAVE, device->cap_mask) &&
735 !device->device_prep_interleaved_dma);
Dan Williams7405f742007-01-02 11:10:43 -0700736
737 BUG_ON(!device->device_alloc_chan_resources);
738 BUG_ON(!device->device_free_chan_resources);
Linus Walleij07934482010-03-26 16:50:49 -0700739 BUG_ON(!device->device_tx_status);
Dan Williams7405f742007-01-02 11:10:43 -0700740 BUG_ON(!device->device_issue_pending);
741 BUG_ON(!device->dev);
742
Dan Williams138f4c32009-09-08 17:42:51 -0700743 /* note: this only matters in the
Dan Williams5fc6d892010-10-07 16:44:50 -0700744 * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
Dan Williams138f4c32009-09-08 17:42:51 -0700745 */
746 if (device_has_all_tx_types(device))
747 dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
748
Dan Williams864498a2009-01-06 11:38:21 -0700749 idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
750 if (!idr_ref)
751 return -ENOMEM;
Dan Williams257b17c2009-03-25 09:13:23 -0700752 rc = get_dma_id(device);
753 if (rc != 0) {
754 kfree(idr_ref);
Dan Williams864498a2009-01-06 11:38:21 -0700755 return rc;
Dan Williams257b17c2009-03-25 09:13:23 -0700756 }
757
758 atomic_set(idr_ref, 0);
Chris Leechc13c8262006-05-23 17:18:44 -0700759
760 /* represent channels in sysfs. Probably want devs too */
761 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williams257b17c2009-03-25 09:13:23 -0700762 rc = -ENOMEM;
Chris Leechc13c8262006-05-23 17:18:44 -0700763 chan->local = alloc_percpu(typeof(*chan->local));
764 if (chan->local == NULL)
Dan Williams257b17c2009-03-25 09:13:23 -0700765 goto err_out;
Dan Williams41d5e592009-01-06 11:38:21 -0700766 chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
767 if (chan->dev == NULL) {
768 free_percpu(chan->local);
Dan Williams257b17c2009-03-25 09:13:23 -0700769 chan->local = NULL;
770 goto err_out;
Dan Williams41d5e592009-01-06 11:38:21 -0700771 }
Chris Leechc13c8262006-05-23 17:18:44 -0700772
773 chan->chan_id = chancnt++;
Dan Williams41d5e592009-01-06 11:38:21 -0700774 chan->dev->device.class = &dma_devclass;
775 chan->dev->device.parent = device->dev;
776 chan->dev->chan = chan;
Dan Williams864498a2009-01-06 11:38:21 -0700777 chan->dev->idr_ref = idr_ref;
778 chan->dev->dev_id = device->dev_id;
779 atomic_inc(idr_ref);
Dan Williams41d5e592009-01-06 11:38:21 -0700780 dev_set_name(&chan->dev->device, "dma%dchan%d",
Kay Sievers06190d82008-11-11 13:12:33 -0700781 device->dev_id, chan->chan_id);
Chris Leechc13c8262006-05-23 17:18:44 -0700782
Dan Williams41d5e592009-01-06 11:38:21 -0700783 rc = device_register(&chan->dev->device);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800784 if (rc) {
Jeff Garzikff487fb2007-03-08 09:57:34 -0800785 free_percpu(chan->local);
786 chan->local = NULL;
Dan Williams257b17c2009-03-25 09:13:23 -0700787 kfree(chan->dev);
788 atomic_dec(idr_ref);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800789 goto err_out;
790 }
Dan Williams7cc5bf92008-07-08 11:58:21 -0700791 chan->client_count = 0;
Chris Leechc13c8262006-05-23 17:18:44 -0700792 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700793 device->chancnt = chancnt;
Chris Leechc13c8262006-05-23 17:18:44 -0700794
795 mutex_lock(&dma_list_mutex);
Dan Williams59b5ec22009-01-06 11:38:15 -0700796 /* take references on public channels */
797 if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
Dan Williams6f49a572009-01-06 11:38:14 -0700798 list_for_each_entry(chan, &device->channels, device_node) {
799 /* if clients are already waiting for channels we need
800 * to take references on their behalf
801 */
802 if (dma_chan_get(chan) == -ENODEV) {
803 /* note we can only get here for the first
804 * channel as the remaining channels are
805 * guaranteed to get a reference
806 */
807 rc = -ENODEV;
808 mutex_unlock(&dma_list_mutex);
809 goto err_out;
810 }
811 }
Dan Williams2ba05622009-01-06 11:38:14 -0700812 list_add_tail_rcu(&device->global_node, &dma_device_list);
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900813 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
814 device->privatecnt++; /* Always private */
Dan Williamsbec08512009-01-06 11:38:14 -0700815 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700816 mutex_unlock(&dma_list_mutex);
817
Chris Leechc13c8262006-05-23 17:18:44 -0700818 return 0;
Jeff Garzikff487fb2007-03-08 09:57:34 -0800819
820err_out:
Dan Williams257b17c2009-03-25 09:13:23 -0700821 /* if we never registered a channel just release the idr */
822 if (atomic_read(idr_ref) == 0) {
823 mutex_lock(&dma_list_mutex);
824 idr_remove(&dma_idr, device->dev_id);
825 mutex_unlock(&dma_list_mutex);
826 kfree(idr_ref);
827 return rc;
828 }
829
Jeff Garzikff487fb2007-03-08 09:57:34 -0800830 list_for_each_entry(chan, &device->channels, device_node) {
831 if (chan->local == NULL)
832 continue;
Dan Williams41d5e592009-01-06 11:38:21 -0700833 mutex_lock(&dma_list_mutex);
834 chan->dev->chan = NULL;
835 mutex_unlock(&dma_list_mutex);
836 device_unregister(&chan->dev->device);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800837 free_percpu(chan->local);
838 }
839 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700840}
David Brownell765e3d82007-03-16 13:38:05 -0800841EXPORT_SYMBOL(dma_async_device_register);
Chris Leechc13c8262006-05-23 17:18:44 -0700842
843/**
Dan Williams6f49a572009-01-06 11:38:14 -0700844 * dma_async_device_unregister - unregister a DMA device
Randy Dunlap65088712006-07-03 19:45:31 -0700845 * @device: &dma_device
Dan Williamsf27c5802009-01-06 11:38:18 -0700846 *
847 * This routine is called by dma driver exit routines, dmaengine holds module
848 * references to prevent it being called while channels are in use.
Randy Dunlap65088712006-07-03 19:45:31 -0700849 */
850void dma_async_device_unregister(struct dma_device *device)
Chris Leechc13c8262006-05-23 17:18:44 -0700851{
852 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700853
854 mutex_lock(&dma_list_mutex);
Dan Williams2ba05622009-01-06 11:38:14 -0700855 list_del_rcu(&device->global_node);
Dan Williamsbec08512009-01-06 11:38:14 -0700856 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700857 mutex_unlock(&dma_list_mutex);
858
859 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williams6f49a572009-01-06 11:38:14 -0700860 WARN_ONCE(chan->client_count,
861 "%s called while %d clients hold a reference\n",
862 __func__, chan->client_count);
Dan Williams41d5e592009-01-06 11:38:21 -0700863 mutex_lock(&dma_list_mutex);
864 chan->dev->chan = NULL;
865 mutex_unlock(&dma_list_mutex);
866 device_unregister(&chan->dev->device);
Anatolij Gustschinadef4772010-01-26 10:26:06 +0100867 free_percpu(chan->local);
Chris Leechc13c8262006-05-23 17:18:44 -0700868 }
Chris Leechc13c8262006-05-23 17:18:44 -0700869}
David Brownell765e3d82007-03-16 13:38:05 -0800870EXPORT_SYMBOL(dma_async_device_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -0700871
Dan Williams7405f742007-01-02 11:10:43 -0700872/**
873 * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
874 * @chan: DMA channel to offload copy to
875 * @dest: destination address (virtual)
876 * @src: source address (virtual)
877 * @len: length
878 *
879 * Both @dest and @src must be mappable to a bus address according to the
880 * DMA mapping API rules for streaming mappings.
881 * Both @dest and @src must stay memory resident (kernel memory or locked
882 * user space pages).
883 */
884dma_cookie_t
885dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
886 void *src, size_t len)
887{
888 struct dma_device *dev = chan->device;
889 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700890 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700891 dma_cookie_t cookie;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200892 unsigned long flags;
Dan Williams7405f742007-01-02 11:10:43 -0700893
Dan Williams00367312008-02-02 19:49:57 -0700894 dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
895 dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200896 flags = DMA_CTRL_ACK |
897 DMA_COMPL_SRC_UNMAP_SINGLE |
898 DMA_COMPL_DEST_UNMAP_SINGLE;
899 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
Dan Williams00367312008-02-02 19:49:57 -0700900
901 if (!tx) {
902 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
903 dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700904 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700905 }
Dan Williams7405f742007-01-02 11:10:43 -0700906
Dan Williams7405f742007-01-02 11:10:43 -0700907 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700908 cookie = tx->tx_submit(tx);
909
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900910 preempt_disable();
911 __this_cpu_add(chan->local->bytes_transferred, len);
912 __this_cpu_inc(chan->local->memcpy_count);
913 preempt_enable();
Dan Williams7405f742007-01-02 11:10:43 -0700914
915 return cookie;
916}
917EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
918
919/**
920 * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
921 * @chan: DMA channel to offload copy to
922 * @page: destination page
923 * @offset: offset in page to copy to
924 * @kdata: source address (virtual)
925 * @len: length
926 *
927 * Both @page/@offset and @kdata must be mappable to a bus address according
928 * to the DMA mapping API rules for streaming mappings.
929 * Both @page/@offset and @kdata must stay memory resident (kernel memory or
930 * locked user space pages)
931 */
932dma_cookie_t
933dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
934 unsigned int offset, void *kdata, size_t len)
935{
936 struct dma_device *dev = chan->device;
937 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700938 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700939 dma_cookie_t cookie;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200940 unsigned long flags;
Dan Williams7405f742007-01-02 11:10:43 -0700941
Dan Williams00367312008-02-02 19:49:57 -0700942 dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
943 dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200944 flags = DMA_CTRL_ACK | DMA_COMPL_SRC_UNMAP_SINGLE;
945 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
Dan Williams00367312008-02-02 19:49:57 -0700946
947 if (!tx) {
948 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
949 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700950 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700951 }
Dan Williams7405f742007-01-02 11:10:43 -0700952
Dan Williams7405f742007-01-02 11:10:43 -0700953 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700954 cookie = tx->tx_submit(tx);
955
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900956 preempt_disable();
957 __this_cpu_add(chan->local->bytes_transferred, len);
958 __this_cpu_inc(chan->local->memcpy_count);
959 preempt_enable();
Dan Williams7405f742007-01-02 11:10:43 -0700960
961 return cookie;
962}
963EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
964
965/**
966 * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
967 * @chan: DMA channel to offload copy to
968 * @dest_pg: destination page
969 * @dest_off: offset in page to copy to
970 * @src_pg: source page
971 * @src_off: offset in page to copy from
972 * @len: length
973 *
974 * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
975 * address according to the DMA mapping API rules for streaming mappings.
976 * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
977 * (kernel memory or locked user space pages).
978 */
979dma_cookie_t
980dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
981 unsigned int dest_off, struct page *src_pg, unsigned int src_off,
982 size_t len)
983{
984 struct dma_device *dev = chan->device;
985 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700986 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700987 dma_cookie_t cookie;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200988 unsigned long flags;
Dan Williams7405f742007-01-02 11:10:43 -0700989
Dan Williams00367312008-02-02 19:49:57 -0700990 dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
991 dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
992 DMA_FROM_DEVICE);
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200993 flags = DMA_CTRL_ACK;
994 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
Dan Williams00367312008-02-02 19:49:57 -0700995
996 if (!tx) {
997 dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
998 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700999 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -07001000 }
Dan Williams7405f742007-01-02 11:10:43 -07001001
Dan Williams7405f742007-01-02 11:10:43 -07001002 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -07001003 cookie = tx->tx_submit(tx);
1004
Christoph Lametere7dcaa42009-10-03 19:48:23 +09001005 preempt_disable();
1006 __this_cpu_add(chan->local->bytes_transferred, len);
1007 __this_cpu_inc(chan->local->memcpy_count);
1008 preempt_enable();
Dan Williams7405f742007-01-02 11:10:43 -07001009
1010 return cookie;
1011}
1012EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
1013
1014void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1015 struct dma_chan *chan)
1016{
1017 tx->chan = chan;
Dan Williams5fc6d892010-10-07 16:44:50 -07001018 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williams7405f742007-01-02 11:10:43 -07001019 spin_lock_init(&tx->lock);
Dan Williamscaa20d972010-05-17 16:24:16 -07001020 #endif
Dan Williams7405f742007-01-02 11:10:43 -07001021}
1022EXPORT_SYMBOL(dma_async_tx_descriptor_init);
1023
Dan Williams07f22112009-01-05 17:14:31 -07001024/* dma_wait_for_async_tx - spin wait for a transaction to complete
1025 * @tx: in-flight transaction to wait on
Dan Williams07f22112009-01-05 17:14:31 -07001026 */
1027enum dma_status
1028dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1029{
Dan Williams95475e52009-07-14 12:19:02 -07001030 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
Dan Williams07f22112009-01-05 17:14:31 -07001031
1032 if (!tx)
1033 return DMA_SUCCESS;
1034
Dan Williams95475e52009-07-14 12:19:02 -07001035 while (tx->cookie == -EBUSY) {
1036 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
1037 pr_err("%s timeout waiting for descriptor submission\n",
Joe Perches63433252012-07-18 09:51:28 -07001038 __func__);
Dan Williams95475e52009-07-14 12:19:02 -07001039 return DMA_ERROR;
1040 }
1041 cpu_relax();
1042 }
1043 return dma_sync_wait(tx->chan, tx->cookie);
Dan Williams07f22112009-01-05 17:14:31 -07001044}
1045EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
1046
1047/* dma_run_dependencies - helper routine for dma drivers to process
1048 * (start) dependent operations on their target channel
1049 * @tx: transaction with dependencies
1050 */
1051void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
1052{
Dan Williamscaa20d972010-05-17 16:24:16 -07001053 struct dma_async_tx_descriptor *dep = txd_next(tx);
Dan Williams07f22112009-01-05 17:14:31 -07001054 struct dma_async_tx_descriptor *dep_next;
1055 struct dma_chan *chan;
1056
1057 if (!dep)
1058 return;
1059
Yuri Tikhonovdd59b852009-01-12 15:17:20 -07001060 /* we'll submit tx->next now, so clear the link */
Dan Williamscaa20d972010-05-17 16:24:16 -07001061 txd_clear_next(tx);
Dan Williams07f22112009-01-05 17:14:31 -07001062 chan = dep->chan;
1063
1064 /* keep submitting up until a channel switch is detected
1065 * in that case we will be called again as a result of
1066 * processing the interrupt from async_tx_channel_switch
1067 */
1068 for (; dep; dep = dep_next) {
Dan Williamscaa20d972010-05-17 16:24:16 -07001069 txd_lock(dep);
1070 txd_clear_parent(dep);
1071 dep_next = txd_next(dep);
Dan Williams07f22112009-01-05 17:14:31 -07001072 if (dep_next && dep_next->chan == chan)
Dan Williamscaa20d972010-05-17 16:24:16 -07001073 txd_clear_next(dep); /* ->next will be submitted */
Dan Williams07f22112009-01-05 17:14:31 -07001074 else
1075 dep_next = NULL; /* submit current dep and terminate */
Dan Williamscaa20d972010-05-17 16:24:16 -07001076 txd_unlock(dep);
Dan Williams07f22112009-01-05 17:14:31 -07001077
1078 dep->tx_submit(dep);
1079 }
1080
1081 chan->device->device_issue_pending(chan);
1082}
1083EXPORT_SYMBOL_GPL(dma_run_dependencies);
1084
Chris Leechc13c8262006-05-23 17:18:44 -07001085static int __init dma_bus_init(void)
1086{
Chris Leechc13c8262006-05-23 17:18:44 -07001087 return class_register(&dma_devclass);
1088}
Dan Williams652afc22009-01-06 11:38:22 -07001089arch_initcall(dma_bus_init);
Chris Leechc13c8262006-05-23 17:18:44 -07001090
Dan Williamsbec08512009-01-06 11:38:14 -07001091