blob: 99af4db5948bc9406d85ef841077baff2ffd5f40 [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>
Andy Shevchenko4e82f5d2013-04-09 14:05:44 +030065#include <linux/acpi.h>
66#include <linux/acpi_dma.h>
Jon Hunter9a6cecc2012-09-14 17:41:57 -050067#include <linux/of_dma.h>
Chris Leechc13c8262006-05-23 17:18:44 -070068
69static DEFINE_MUTEX(dma_list_mutex);
Axel Lin21ef4b82011-07-20 11:32:28 +080070static DEFINE_IDR(dma_idr);
Chris Leechc13c8262006-05-23 17:18:44 -070071static LIST_HEAD(dma_device_list);
Dan Williams6f49a572009-01-06 11:38:14 -070072static long dmaengine_ref_count;
Chris Leechc13c8262006-05-23 17:18:44 -070073
74/* --- sysfs implementation --- */
75
Dan Williams41d5e592009-01-06 11:38:21 -070076/**
77 * dev_to_dma_chan - convert a device pointer to the its sysfs container object
78 * @dev - device node
79 *
80 * Must be called under dma_list_mutex
81 */
82static struct dma_chan *dev_to_dma_chan(struct device *dev)
83{
84 struct dma_chan_dev *chan_dev;
85
86 chan_dev = container_of(dev, typeof(*chan_dev), device);
87 return chan_dev->chan;
88}
89
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -070090static ssize_t memcpy_count_show(struct device *dev,
91 struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -070092{
Dan Williams41d5e592009-01-06 11:38:21 -070093 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -070094 unsigned long count = 0;
95 int i;
Dan Williams41d5e592009-01-06 11:38:21 -070096 int err;
Chris Leechc13c8262006-05-23 17:18:44 -070097
Dan Williams41d5e592009-01-06 11:38:21 -070098 mutex_lock(&dma_list_mutex);
99 chan = dev_to_dma_chan(dev);
100 if (chan) {
101 for_each_possible_cpu(i)
102 count += per_cpu_ptr(chan->local, i)->memcpy_count;
103 err = sprintf(buf, "%lu\n", count);
104 } else
105 err = -ENODEV;
106 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700107
Dan Williams41d5e592009-01-06 11:38:21 -0700108 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700109}
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700110static DEVICE_ATTR_RO(memcpy_count);
Chris Leechc13c8262006-05-23 17:18:44 -0700111
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700112static ssize_t bytes_transferred_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700114{
Dan Williams41d5e592009-01-06 11:38:21 -0700115 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700116 unsigned long count = 0;
117 int i;
Dan Williams41d5e592009-01-06 11:38:21 -0700118 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700119
Dan Williams41d5e592009-01-06 11:38:21 -0700120 mutex_lock(&dma_list_mutex);
121 chan = dev_to_dma_chan(dev);
122 if (chan) {
123 for_each_possible_cpu(i)
124 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
125 err = sprintf(buf, "%lu\n", count);
126 } else
127 err = -ENODEV;
128 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700129
Dan Williams41d5e592009-01-06 11:38:21 -0700130 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700131}
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700132static DEVICE_ATTR_RO(bytes_transferred);
Chris Leechc13c8262006-05-23 17:18:44 -0700133
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700134static ssize_t in_use_show(struct device *dev, struct device_attribute *attr,
135 char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700136{
Dan Williams41d5e592009-01-06 11:38:21 -0700137 struct dma_chan *chan;
138 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700139
Dan Williams41d5e592009-01-06 11:38:21 -0700140 mutex_lock(&dma_list_mutex);
141 chan = dev_to_dma_chan(dev);
142 if (chan)
143 err = sprintf(buf, "%d\n", chan->client_count);
144 else
145 err = -ENODEV;
146 mutex_unlock(&dma_list_mutex);
147
148 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700149}
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700150static DEVICE_ATTR_RO(in_use);
Chris Leechc13c8262006-05-23 17:18:44 -0700151
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700152static struct attribute *dma_dev_attrs[] = {
153 &dev_attr_memcpy_count.attr,
154 &dev_attr_bytes_transferred.attr,
155 &dev_attr_in_use.attr,
156 NULL,
Chris Leechc13c8262006-05-23 17:18:44 -0700157};
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700158ATTRIBUTE_GROUPS(dma_dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700159
Dan Williams41d5e592009-01-06 11:38:21 -0700160static void chan_dev_release(struct device *dev)
161{
162 struct dma_chan_dev *chan_dev;
163
164 chan_dev = container_of(dev, typeof(*chan_dev), device);
Dan Williams864498a2009-01-06 11:38:21 -0700165 if (atomic_dec_and_test(chan_dev->idr_ref)) {
166 mutex_lock(&dma_list_mutex);
167 idr_remove(&dma_idr, chan_dev->dev_id);
168 mutex_unlock(&dma_list_mutex);
169 kfree(chan_dev->idr_ref);
170 }
Dan Williams41d5e592009-01-06 11:38:21 -0700171 kfree(chan_dev);
172}
173
Chris Leechc13c8262006-05-23 17:18:44 -0700174static struct class dma_devclass = {
Tony Jones891f78e2007-09-25 02:03:03 +0200175 .name = "dma",
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700176 .dev_groups = dma_dev_groups,
Dan Williams41d5e592009-01-06 11:38:21 -0700177 .dev_release = chan_dev_release,
Chris Leechc13c8262006-05-23 17:18:44 -0700178};
179
180/* --- client and device registration --- */
181
Dan Williams59b5ec22009-01-06 11:38:15 -0700182#define dma_device_satisfies_mask(device, mask) \
183 __dma_device_satisfies_mask((device), &(mask))
Dan Williamsd379b012007-07-09 11:56:42 -0700184static int
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +0100185__dma_device_satisfies_mask(struct dma_device *device,
186 const dma_cap_mask_t *want)
Dan Williamsd379b012007-07-09 11:56:42 -0700187{
188 dma_cap_mask_t has;
189
Dan Williams59b5ec22009-01-06 11:38:15 -0700190 bitmap_and(has.bits, want->bits, device->cap_mask.bits,
Dan Williamsd379b012007-07-09 11:56:42 -0700191 DMA_TX_TYPE_END);
192 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
193}
194
Dan Williams6f49a572009-01-06 11:38:14 -0700195static struct module *dma_chan_to_owner(struct dma_chan *chan)
196{
197 return chan->device->dev->driver->owner;
198}
199
200/**
201 * balance_ref_count - catch up the channel reference count
202 * @chan - channel to balance ->client_count versus dmaengine_ref_count
203 *
204 * balance_ref_count must be called under dma_list_mutex
205 */
206static void balance_ref_count(struct dma_chan *chan)
207{
208 struct module *owner = dma_chan_to_owner(chan);
209
210 while (chan->client_count < dmaengine_ref_count) {
211 __module_get(owner);
212 chan->client_count++;
213 }
214}
215
216/**
217 * dma_chan_get - try to grab a dma channel's parent driver module
218 * @chan - channel to grab
219 *
220 * Must be called under dma_list_mutex
221 */
222static int dma_chan_get(struct dma_chan *chan)
223{
224 int err = -ENODEV;
225 struct module *owner = dma_chan_to_owner(chan);
226
227 if (chan->client_count) {
228 __module_get(owner);
229 err = 0;
230 } else if (try_module_get(owner))
231 err = 0;
232
233 if (err == 0)
234 chan->client_count++;
235
236 /* allocate upon first client reference */
237 if (chan->client_count == 1 && err == 0) {
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700238 int desc_cnt = chan->device->device_alloc_chan_resources(chan);
Dan Williams6f49a572009-01-06 11:38:14 -0700239
240 if (desc_cnt < 0) {
241 err = desc_cnt;
242 chan->client_count = 0;
243 module_put(owner);
Dan Williams59b5ec22009-01-06 11:38:15 -0700244 } else if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
Dan Williams6f49a572009-01-06 11:38:14 -0700245 balance_ref_count(chan);
246 }
247
248 return err;
249}
250
251/**
252 * dma_chan_put - drop a reference to a dma channel's parent driver module
253 * @chan - channel to release
254 *
255 * Must be called under dma_list_mutex
256 */
257static void dma_chan_put(struct dma_chan *chan)
258{
259 if (!chan->client_count)
260 return; /* this channel failed alloc_chan_resources */
261 chan->client_count--;
262 module_put(dma_chan_to_owner(chan));
263 if (chan->client_count == 0)
264 chan->device->device_free_chan_resources(chan);
265}
266
Dan Williams7405f742007-01-02 11:10:43 -0700267enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
268{
269 enum dma_status status;
270 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
271
272 dma_async_issue_pending(chan);
273 do {
274 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
275 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
Joe Perches63433252012-07-18 09:51:28 -0700276 pr_err("%s: timeout!\n", __func__);
Dan Williams7405f742007-01-02 11:10:43 -0700277 return DMA_ERROR;
278 }
Bartlomiej Zolnierkiewicz2cbe7fe2012-11-08 10:02:07 +0000279 if (status != DMA_IN_PROGRESS)
280 break;
281 cpu_relax();
282 } while (1);
Dan Williams7405f742007-01-02 11:10:43 -0700283
284 return status;
285}
286EXPORT_SYMBOL(dma_sync_wait);
287
Chris Leechc13c8262006-05-23 17:18:44 -0700288/**
Dan Williamsbec08512009-01-06 11:38:14 -0700289 * dma_cap_mask_all - enable iteration over all operation types
290 */
291static dma_cap_mask_t dma_cap_mask_all;
292
293/**
294 * dma_chan_tbl_ent - tracks channel allocations per core/operation
295 * @chan - associated channel for this entry
296 */
297struct dma_chan_tbl_ent {
298 struct dma_chan *chan;
299};
300
301/**
302 * channel_table - percpu lookup table for memory-to-memory offload providers
303 */
Tejun Heoa29d8b82010-02-02 14:39:15 +0900304static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
Dan Williamsbec08512009-01-06 11:38:14 -0700305
306static int __init dma_channel_table_init(void)
307{
308 enum dma_transaction_type cap;
309 int err = 0;
310
311 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
312
Dan Williams59b5ec22009-01-06 11:38:15 -0700313 /* 'interrupt', 'private', and 'slave' are channel capabilities,
314 * but are not associated with an operation so they do not need
315 * an entry in the channel_table
Dan Williamsbec08512009-01-06 11:38:14 -0700316 */
317 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
Dan Williams59b5ec22009-01-06 11:38:15 -0700318 clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
Dan Williamsbec08512009-01-06 11:38:14 -0700319 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
320
321 for_each_dma_cap_mask(cap, dma_cap_mask_all) {
322 channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
323 if (!channel_table[cap]) {
324 err = -ENOMEM;
325 break;
326 }
327 }
328
329 if (err) {
Joe Perches63433252012-07-18 09:51:28 -0700330 pr_err("initialization failure\n");
Dan Williamsbec08512009-01-06 11:38:14 -0700331 for_each_dma_cap_mask(cap, dma_cap_mask_all)
332 if (channel_table[cap])
333 free_percpu(channel_table[cap]);
334 }
335
336 return err;
337}
Dan Williams652afc22009-01-06 11:38:22 -0700338arch_initcall(dma_channel_table_init);
Dan Williamsbec08512009-01-06 11:38:14 -0700339
340/**
341 * dma_find_channel - find a channel to carry out the operation
342 * @tx_type: transaction type
343 */
344struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
345{
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900346 return this_cpu_read(channel_table[tx_type]->chan);
Dan Williamsbec08512009-01-06 11:38:14 -0700347}
348EXPORT_SYMBOL(dma_find_channel);
349
Dave Jianga2bd1142012-04-04 16:10:46 -0700350/*
351 * net_dma_find_channel - find a channel for net_dma
352 * net_dma has alignment requirements
353 */
354struct dma_chan *net_dma_find_channel(void)
355{
356 struct dma_chan *chan = dma_find_channel(DMA_MEMCPY);
357 if (chan && !is_dma_copy_aligned(chan->device, 1, 1, 1))
358 return NULL;
359
360 return chan;
361}
362EXPORT_SYMBOL(net_dma_find_channel);
363
Dan Williamsbec08512009-01-06 11:38:14 -0700364/**
Dan Williams2ba05622009-01-06 11:38:14 -0700365 * dma_issue_pending_all - flush all pending operations across all channels
366 */
367void dma_issue_pending_all(void)
368{
369 struct dma_device *device;
370 struct dma_chan *chan;
371
Dan Williams2ba05622009-01-06 11:38:14 -0700372 rcu_read_lock();
Dan Williams59b5ec22009-01-06 11:38:15 -0700373 list_for_each_entry_rcu(device, &dma_device_list, global_node) {
374 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
375 continue;
Dan Williams2ba05622009-01-06 11:38:14 -0700376 list_for_each_entry(chan, &device->channels, device_node)
377 if (chan->client_count)
378 device->device_issue_pending(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700379 }
Dan Williams2ba05622009-01-06 11:38:14 -0700380 rcu_read_unlock();
381}
382EXPORT_SYMBOL(dma_issue_pending_all);
383
384/**
Dan Williamsbec08512009-01-06 11:38:14 -0700385 * nth_chan - returns the nth channel of the given capability
386 * @cap: capability to match
387 * @n: nth channel desired
388 *
389 * Defaults to returning the channel with the desired capability and the
390 * lowest reference count when 'n' cannot be satisfied. Must be called
391 * under dma_list_mutex.
392 */
393static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
394{
395 struct dma_device *device;
396 struct dma_chan *chan;
397 struct dma_chan *ret = NULL;
398 struct dma_chan *min = NULL;
399
400 list_for_each_entry(device, &dma_device_list, global_node) {
Dan Williams59b5ec22009-01-06 11:38:15 -0700401 if (!dma_has_cap(cap, device->cap_mask) ||
402 dma_has_cap(DMA_PRIVATE, device->cap_mask))
Dan Williamsbec08512009-01-06 11:38:14 -0700403 continue;
404 list_for_each_entry(chan, &device->channels, device_node) {
405 if (!chan->client_count)
406 continue;
407 if (!min)
408 min = chan;
409 else if (chan->table_count < min->table_count)
410 min = chan;
411
412 if (n-- == 0) {
413 ret = chan;
414 break; /* done */
415 }
416 }
417 if (ret)
418 break; /* done */
419 }
420
421 if (!ret)
422 ret = min;
423
424 if (ret)
425 ret->table_count++;
426
427 return ret;
428}
429
430/**
431 * dma_channel_rebalance - redistribute the available channels
432 *
433 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
434 * operation type) in the SMP case, and operation isolation (avoid
435 * multi-tasking channels) in the non-SMP case. Must be called under
436 * dma_list_mutex.
437 */
438static void dma_channel_rebalance(void)
439{
440 struct dma_chan *chan;
441 struct dma_device *device;
442 int cpu;
443 int cap;
444 int n;
445
446 /* undo the last distribution */
447 for_each_dma_cap_mask(cap, dma_cap_mask_all)
448 for_each_possible_cpu(cpu)
449 per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
450
Dan Williams59b5ec22009-01-06 11:38:15 -0700451 list_for_each_entry(device, &dma_device_list, global_node) {
452 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
453 continue;
Dan Williamsbec08512009-01-06 11:38:14 -0700454 list_for_each_entry(chan, &device->channels, device_node)
455 chan->table_count = 0;
Dan Williams59b5ec22009-01-06 11:38:15 -0700456 }
Dan Williamsbec08512009-01-06 11:38:14 -0700457
458 /* don't populate the channel_table if no clients are available */
459 if (!dmaengine_ref_count)
460 return;
461
462 /* redistribute available channels */
463 n = 0;
464 for_each_dma_cap_mask(cap, dma_cap_mask_all)
465 for_each_online_cpu(cpu) {
466 if (num_possible_cpus() > 1)
467 chan = nth_chan(cap, n++);
468 else
469 chan = nth_chan(cap, -1);
470
471 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
472 }
473}
474
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +0100475static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
476 struct dma_device *dev,
Dan Williamse2346672009-01-06 11:38:21 -0700477 dma_filter_fn fn, void *fn_param)
Dan Williams59b5ec22009-01-06 11:38:15 -0700478{
479 struct dma_chan *chan;
Dan Williams59b5ec22009-01-06 11:38:15 -0700480
481 if (!__dma_device_satisfies_mask(dev, mask)) {
482 pr_debug("%s: wrong capabilities\n", __func__);
483 return NULL;
484 }
485 /* devices with multiple channels need special handling as we need to
486 * ensure that all channels are either private or public.
487 */
488 if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
489 list_for_each_entry(chan, &dev->channels, device_node) {
490 /* some channels are already publicly allocated */
491 if (chan->client_count)
492 return NULL;
493 }
494
495 list_for_each_entry(chan, &dev->channels, device_node) {
496 if (chan->client_count) {
497 pr_debug("%s: %s busy\n",
Dan Williams41d5e592009-01-06 11:38:21 -0700498 __func__, dma_chan_name(chan));
Dan Williams59b5ec22009-01-06 11:38:15 -0700499 continue;
500 }
Dan Williamse2346672009-01-06 11:38:21 -0700501 if (fn && !fn(chan, fn_param)) {
502 pr_debug("%s: %s filter said false\n",
503 __func__, dma_chan_name(chan));
504 continue;
505 }
506 return chan;
Dan Williams59b5ec22009-01-06 11:38:15 -0700507 }
508
Dan Williamse2346672009-01-06 11:38:21 -0700509 return NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700510}
511
512/**
513 * dma_request_channel - try to allocate an exclusive channel
514 * @mask: capabilities that the channel must satisfy
515 * @fn: optional callback to disposition available channels
516 * @fn_param: opaque parameter to pass to dma_filter_fn
517 */
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +0100518struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
519 dma_filter_fn fn, void *fn_param)
Dan Williams59b5ec22009-01-06 11:38:15 -0700520{
521 struct dma_device *device, *_d;
522 struct dma_chan *chan = NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700523 int err;
524
525 /* Find a channel */
526 mutex_lock(&dma_list_mutex);
527 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
Dan Williamse2346672009-01-06 11:38:21 -0700528 chan = private_candidate(mask, device, fn, fn_param);
529 if (chan) {
Dan Williams59b5ec22009-01-06 11:38:15 -0700530 /* Found a suitable channel, try to grab, prep, and
531 * return it. We first set DMA_PRIVATE to disable
532 * balance_ref_count as this channel will not be
533 * published in the general-purpose allocator
534 */
535 dma_cap_set(DMA_PRIVATE, device->cap_mask);
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900536 device->privatecnt++;
Dan Williams59b5ec22009-01-06 11:38:15 -0700537 err = dma_chan_get(chan);
538
539 if (err == -ENODEV) {
Joe Perches63433252012-07-18 09:51:28 -0700540 pr_debug("%s: %s module removed\n",
541 __func__, dma_chan_name(chan));
Dan Williams59b5ec22009-01-06 11:38:15 -0700542 list_del_rcu(&device->global_node);
543 } else if (err)
Fabio Estevamd8b53482012-02-21 12:51:59 -0200544 pr_debug("%s: failed to get %s: (%d)\n",
Joe Perches63433252012-07-18 09:51:28 -0700545 __func__, dma_chan_name(chan), err);
Dan Williams59b5ec22009-01-06 11:38:15 -0700546 else
547 break;
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900548 if (--device->privatecnt == 0)
549 dma_cap_clear(DMA_PRIVATE, device->cap_mask);
Dan Williamse2346672009-01-06 11:38:21 -0700550 chan = NULL;
551 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700552 }
553 mutex_unlock(&dma_list_mutex);
554
Joe Perches63433252012-07-18 09:51:28 -0700555 pr_debug("%s: %s (%s)\n",
556 __func__,
557 chan ? "success" : "fail",
Dan Williams41d5e592009-01-06 11:38:21 -0700558 chan ? dma_chan_name(chan) : NULL);
Dan Williams59b5ec22009-01-06 11:38:15 -0700559
560 return chan;
561}
562EXPORT_SYMBOL_GPL(__dma_request_channel);
563
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500564/**
565 * dma_request_slave_channel - try to allocate an exclusive slave channel
566 * @dev: pointer to client device structure
567 * @name: slave channel name
568 */
Markus Pargmannbef29ec2013-02-24 16:36:09 +0100569struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name)
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500570{
571 /* If device-tree is present get slave info from here */
572 if (dev->of_node)
573 return of_dma_request_slave_channel(dev->of_node, name);
574
Andy Shevchenko4e82f5d2013-04-09 14:05:44 +0300575 /* If device was enumerated by ACPI get slave info from here */
576 if (ACPI_HANDLE(dev))
577 return acpi_dma_request_slave_chan_by_name(dev, name);
578
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500579 return NULL;
580}
581EXPORT_SYMBOL_GPL(dma_request_slave_channel);
582
Dan Williams59b5ec22009-01-06 11:38:15 -0700583void dma_release_channel(struct dma_chan *chan)
584{
585 mutex_lock(&dma_list_mutex);
586 WARN_ONCE(chan->client_count != 1,
587 "chan reference count %d != 1\n", chan->client_count);
588 dma_chan_put(chan);
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900589 /* drop PRIVATE cap enabled by __dma_request_channel() */
590 if (--chan->device->privatecnt == 0)
591 dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
Dan Williams59b5ec22009-01-06 11:38:15 -0700592 mutex_unlock(&dma_list_mutex);
593}
594EXPORT_SYMBOL_GPL(dma_release_channel);
595
Dan Williamsbec08512009-01-06 11:38:14 -0700596/**
Dan Williams209b84a2009-01-06 11:38:17 -0700597 * dmaengine_get - register interest in dma_channels
Chris Leechc13c8262006-05-23 17:18:44 -0700598 */
Dan Williams209b84a2009-01-06 11:38:17 -0700599void dmaengine_get(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700600{
Dan Williams6f49a572009-01-06 11:38:14 -0700601 struct dma_device *device, *_d;
602 struct dma_chan *chan;
603 int err;
604
Chris Leechc13c8262006-05-23 17:18:44 -0700605 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700606 dmaengine_ref_count++;
607
608 /* try to grab channels */
Dan Williams59b5ec22009-01-06 11:38:15 -0700609 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
610 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
611 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700612 list_for_each_entry(chan, &device->channels, device_node) {
613 err = dma_chan_get(chan);
614 if (err == -ENODEV) {
615 /* module removed before we could use it */
Dan Williams2ba05622009-01-06 11:38:14 -0700616 list_del_rcu(&device->global_node);
Dan Williams6f49a572009-01-06 11:38:14 -0700617 break;
618 } else if (err)
Fabio Estevam0eb5a352012-10-04 17:11:16 -0700619 pr_debug("%s: failed to get %s: (%d)\n",
Joe Perches63433252012-07-18 09:51:28 -0700620 __func__, dma_chan_name(chan), err);
Dan Williams6f49a572009-01-06 11:38:14 -0700621 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700622 }
Dan Williams6f49a572009-01-06 11:38:14 -0700623
Dan Williamsbec08512009-01-06 11:38:14 -0700624 /* if this is the first reference and there were channels
625 * waiting we need to rebalance to get those channels
626 * incorporated into the channel table
627 */
628 if (dmaengine_ref_count == 1)
629 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700630 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700631}
Dan Williams209b84a2009-01-06 11:38:17 -0700632EXPORT_SYMBOL(dmaengine_get);
Chris Leechc13c8262006-05-23 17:18:44 -0700633
634/**
Dan Williams209b84a2009-01-06 11:38:17 -0700635 * dmaengine_put - let dma drivers be removed when ref_count == 0
Chris Leechc13c8262006-05-23 17:18:44 -0700636 */
Dan Williams209b84a2009-01-06 11:38:17 -0700637void dmaengine_put(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700638{
Dan Williamsd379b012007-07-09 11:56:42 -0700639 struct dma_device *device;
Chris Leechc13c8262006-05-23 17:18:44 -0700640 struct dma_chan *chan;
641
Chris Leechc13c8262006-05-23 17:18:44 -0700642 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700643 dmaengine_ref_count--;
644 BUG_ON(dmaengine_ref_count < 0);
645 /* drop channel references */
Dan Williams59b5ec22009-01-06 11:38:15 -0700646 list_for_each_entry(device, &dma_device_list, global_node) {
647 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
648 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700649 list_for_each_entry(chan, &device->channels, device_node)
650 dma_chan_put(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700651 }
Chris Leechc13c8262006-05-23 17:18:44 -0700652 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700653}
Dan Williams209b84a2009-01-06 11:38:17 -0700654EXPORT_SYMBOL(dmaengine_put);
Chris Leechc13c8262006-05-23 17:18:44 -0700655
Dan Williams138f4c32009-09-08 17:42:51 -0700656static bool device_has_all_tx_types(struct dma_device *device)
657{
658 /* A device that satisfies this test has channels that will never cause
659 * an async_tx channel switch event as all possible operation types can
660 * be handled.
661 */
662 #ifdef CONFIG_ASYNC_TX_DMA
663 if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
664 return false;
665 #endif
666
667 #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE)
668 if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
669 return false;
670 #endif
671
Dan Williams138f4c32009-09-08 17:42:51 -0700672 #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE)
673 if (!dma_has_cap(DMA_XOR, device->cap_mask))
674 return false;
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700675
676 #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
Dan Williams4499a242009-11-19 17:10:25 -0700677 if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
678 return false;
Dan Williams138f4c32009-09-08 17:42:51 -0700679 #endif
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700680 #endif
Dan Williams138f4c32009-09-08 17:42:51 -0700681
682 #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE)
683 if (!dma_has_cap(DMA_PQ, device->cap_mask))
684 return false;
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700685
686 #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
Dan Williams4499a242009-11-19 17:10:25 -0700687 if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
688 return false;
Dan Williams138f4c32009-09-08 17:42:51 -0700689 #endif
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700690 #endif
Dan Williams138f4c32009-09-08 17:42:51 -0700691
692 return true;
693}
694
Dan Williams257b17c2009-03-25 09:13:23 -0700695static int get_dma_id(struct dma_device *device)
696{
697 int rc;
698
Dan Williams257b17c2009-03-25 09:13:23 -0700699 mutex_lock(&dma_list_mutex);
Dan Williams257b17c2009-03-25 09:13:23 -0700700
Tejun Heo69ee2662013-02-27 17:04:03 -0800701 rc = idr_alloc(&dma_idr, NULL, 0, 0, GFP_KERNEL);
702 if (rc >= 0)
703 device->dev_id = rc;
704
705 mutex_unlock(&dma_list_mutex);
706 return rc < 0 ? rc : 0;
Dan Williams257b17c2009-03-25 09:13:23 -0700707}
708
Chris Leechc13c8262006-05-23 17:18:44 -0700709/**
Randy Dunlap65088712006-07-03 19:45:31 -0700710 * dma_async_device_register - registers DMA devices found
Chris Leechc13c8262006-05-23 17:18:44 -0700711 * @device: &dma_device
712 */
713int dma_async_device_register(struct dma_device *device)
714{
Jeff Garzikff487fb2007-03-08 09:57:34 -0800715 int chancnt = 0, rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700716 struct dma_chan* chan;
Dan Williams864498a2009-01-06 11:38:21 -0700717 atomic_t *idr_ref;
Chris Leechc13c8262006-05-23 17:18:44 -0700718
719 if (!device)
720 return -ENODEV;
721
Dan Williams7405f742007-01-02 11:10:43 -0700722 /* validate device routines */
723 BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
724 !device->device_prep_dma_memcpy);
725 BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
726 !device->device_prep_dma_xor);
Dan Williams099f53c2009-04-08 14:28:37 -0700727 BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) &&
728 !device->device_prep_dma_xor_val);
Dan Williamsb2f46fd2009-07-14 12:20:36 -0700729 BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) &&
730 !device->device_prep_dma_pq);
731 BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) &&
732 !device->device_prep_dma_pq_val);
Zhang Wei9b941c62008-03-13 17:45:28 -0700733 BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
Dan Williams7405f742007-01-02 11:10:43 -0700734 !device->device_prep_dma_interrupt);
Ira Snydera86ee032010-09-30 11:46:44 +0000735 BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) &&
736 !device->device_prep_dma_sg);
Sascha Hauer782bc952010-09-30 13:56:32 +0000737 BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) &&
738 !device->device_prep_dma_cyclic);
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700739 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
Linus Walleijc3635c72010-03-26 16:44:01 -0700740 !device->device_control);
Jassi Brarb14dab72011-10-13 12:33:30 +0530741 BUG_ON(dma_has_cap(DMA_INTERLEAVE, device->cap_mask) &&
742 !device->device_prep_interleaved_dma);
Dan Williams7405f742007-01-02 11:10:43 -0700743
744 BUG_ON(!device->device_alloc_chan_resources);
745 BUG_ON(!device->device_free_chan_resources);
Linus Walleij07934482010-03-26 16:50:49 -0700746 BUG_ON(!device->device_tx_status);
Dan Williams7405f742007-01-02 11:10:43 -0700747 BUG_ON(!device->device_issue_pending);
748 BUG_ON(!device->dev);
749
Dan Williams138f4c32009-09-08 17:42:51 -0700750 /* note: this only matters in the
Dan Williams5fc6d892010-10-07 16:44:50 -0700751 * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
Dan Williams138f4c32009-09-08 17:42:51 -0700752 */
753 if (device_has_all_tx_types(device))
754 dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
755
Dan Williams864498a2009-01-06 11:38:21 -0700756 idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
757 if (!idr_ref)
758 return -ENOMEM;
Dan Williams257b17c2009-03-25 09:13:23 -0700759 rc = get_dma_id(device);
760 if (rc != 0) {
761 kfree(idr_ref);
Dan Williams864498a2009-01-06 11:38:21 -0700762 return rc;
Dan Williams257b17c2009-03-25 09:13:23 -0700763 }
764
765 atomic_set(idr_ref, 0);
Chris Leechc13c8262006-05-23 17:18:44 -0700766
767 /* represent channels in sysfs. Probably want devs too */
768 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williams257b17c2009-03-25 09:13:23 -0700769 rc = -ENOMEM;
Chris Leechc13c8262006-05-23 17:18:44 -0700770 chan->local = alloc_percpu(typeof(*chan->local));
771 if (chan->local == NULL)
Dan Williams257b17c2009-03-25 09:13:23 -0700772 goto err_out;
Dan Williams41d5e592009-01-06 11:38:21 -0700773 chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
774 if (chan->dev == NULL) {
775 free_percpu(chan->local);
Dan Williams257b17c2009-03-25 09:13:23 -0700776 chan->local = NULL;
777 goto err_out;
Dan Williams41d5e592009-01-06 11:38:21 -0700778 }
Chris Leechc13c8262006-05-23 17:18:44 -0700779
780 chan->chan_id = chancnt++;
Dan Williams41d5e592009-01-06 11:38:21 -0700781 chan->dev->device.class = &dma_devclass;
782 chan->dev->device.parent = device->dev;
783 chan->dev->chan = chan;
Dan Williams864498a2009-01-06 11:38:21 -0700784 chan->dev->idr_ref = idr_ref;
785 chan->dev->dev_id = device->dev_id;
786 atomic_inc(idr_ref);
Dan Williams41d5e592009-01-06 11:38:21 -0700787 dev_set_name(&chan->dev->device, "dma%dchan%d",
Kay Sievers06190d82008-11-11 13:12:33 -0700788 device->dev_id, chan->chan_id);
Chris Leechc13c8262006-05-23 17:18:44 -0700789
Dan Williams41d5e592009-01-06 11:38:21 -0700790 rc = device_register(&chan->dev->device);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800791 if (rc) {
Jeff Garzikff487fb2007-03-08 09:57:34 -0800792 free_percpu(chan->local);
793 chan->local = NULL;
Dan Williams257b17c2009-03-25 09:13:23 -0700794 kfree(chan->dev);
795 atomic_dec(idr_ref);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800796 goto err_out;
797 }
Dan Williams7cc5bf92008-07-08 11:58:21 -0700798 chan->client_count = 0;
Chris Leechc13c8262006-05-23 17:18:44 -0700799 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700800 device->chancnt = chancnt;
Chris Leechc13c8262006-05-23 17:18:44 -0700801
802 mutex_lock(&dma_list_mutex);
Dan Williams59b5ec22009-01-06 11:38:15 -0700803 /* take references on public channels */
804 if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
Dan Williams6f49a572009-01-06 11:38:14 -0700805 list_for_each_entry(chan, &device->channels, device_node) {
806 /* if clients are already waiting for channels we need
807 * to take references on their behalf
808 */
809 if (dma_chan_get(chan) == -ENODEV) {
810 /* note we can only get here for the first
811 * channel as the remaining channels are
812 * guaranteed to get a reference
813 */
814 rc = -ENODEV;
815 mutex_unlock(&dma_list_mutex);
816 goto err_out;
817 }
818 }
Dan Williams2ba05622009-01-06 11:38:14 -0700819 list_add_tail_rcu(&device->global_node, &dma_device_list);
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900820 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
821 device->privatecnt++; /* Always private */
Dan Williamsbec08512009-01-06 11:38:14 -0700822 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700823 mutex_unlock(&dma_list_mutex);
824
Chris Leechc13c8262006-05-23 17:18:44 -0700825 return 0;
Jeff Garzikff487fb2007-03-08 09:57:34 -0800826
827err_out:
Dan Williams257b17c2009-03-25 09:13:23 -0700828 /* if we never registered a channel just release the idr */
829 if (atomic_read(idr_ref) == 0) {
830 mutex_lock(&dma_list_mutex);
831 idr_remove(&dma_idr, device->dev_id);
832 mutex_unlock(&dma_list_mutex);
833 kfree(idr_ref);
834 return rc;
835 }
836
Jeff Garzikff487fb2007-03-08 09:57:34 -0800837 list_for_each_entry(chan, &device->channels, device_node) {
838 if (chan->local == NULL)
839 continue;
Dan Williams41d5e592009-01-06 11:38:21 -0700840 mutex_lock(&dma_list_mutex);
841 chan->dev->chan = NULL;
842 mutex_unlock(&dma_list_mutex);
843 device_unregister(&chan->dev->device);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800844 free_percpu(chan->local);
845 }
846 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700847}
David Brownell765e3d82007-03-16 13:38:05 -0800848EXPORT_SYMBOL(dma_async_device_register);
Chris Leechc13c8262006-05-23 17:18:44 -0700849
850/**
Dan Williams6f49a572009-01-06 11:38:14 -0700851 * dma_async_device_unregister - unregister a DMA device
Randy Dunlap65088712006-07-03 19:45:31 -0700852 * @device: &dma_device
Dan Williamsf27c5802009-01-06 11:38:18 -0700853 *
854 * This routine is called by dma driver exit routines, dmaengine holds module
855 * references to prevent it being called while channels are in use.
Randy Dunlap65088712006-07-03 19:45:31 -0700856 */
857void dma_async_device_unregister(struct dma_device *device)
Chris Leechc13c8262006-05-23 17:18:44 -0700858{
859 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700860
861 mutex_lock(&dma_list_mutex);
Dan Williams2ba05622009-01-06 11:38:14 -0700862 list_del_rcu(&device->global_node);
Dan Williamsbec08512009-01-06 11:38:14 -0700863 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700864 mutex_unlock(&dma_list_mutex);
865
866 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williams6f49a572009-01-06 11:38:14 -0700867 WARN_ONCE(chan->client_count,
868 "%s called while %d clients hold a reference\n",
869 __func__, chan->client_count);
Dan Williams41d5e592009-01-06 11:38:21 -0700870 mutex_lock(&dma_list_mutex);
871 chan->dev->chan = NULL;
872 mutex_unlock(&dma_list_mutex);
873 device_unregister(&chan->dev->device);
Anatolij Gustschinadef4772010-01-26 10:26:06 +0100874 free_percpu(chan->local);
Chris Leechc13c8262006-05-23 17:18:44 -0700875 }
Chris Leechc13c8262006-05-23 17:18:44 -0700876}
David Brownell765e3d82007-03-16 13:38:05 -0800877EXPORT_SYMBOL(dma_async_device_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -0700878
Dan Williams7405f742007-01-02 11:10:43 -0700879/**
880 * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
881 * @chan: DMA channel to offload copy to
882 * @dest: destination address (virtual)
883 * @src: source address (virtual)
884 * @len: length
885 *
886 * Both @dest and @src must be mappable to a bus address according to the
887 * DMA mapping API rules for streaming mappings.
888 * Both @dest and @src must stay memory resident (kernel memory or locked
889 * user space pages).
890 */
891dma_cookie_t
892dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
893 void *src, 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;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200899 unsigned long flags;
Dan Williams7405f742007-01-02 11:10:43 -0700900
Dan Williams00367312008-02-02 19:49:57 -0700901 dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
902 dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200903 flags = DMA_CTRL_ACK |
904 DMA_COMPL_SRC_UNMAP_SINGLE |
905 DMA_COMPL_DEST_UNMAP_SINGLE;
906 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
Dan Williams00367312008-02-02 19:49:57 -0700907
908 if (!tx) {
909 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
910 dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700911 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700912 }
Dan Williams7405f742007-01-02 11:10:43 -0700913
Dan Williams7405f742007-01-02 11:10:43 -0700914 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700915 cookie = tx->tx_submit(tx);
916
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900917 preempt_disable();
918 __this_cpu_add(chan->local->bytes_transferred, len);
919 __this_cpu_inc(chan->local->memcpy_count);
920 preempt_enable();
Dan Williams7405f742007-01-02 11:10:43 -0700921
922 return cookie;
923}
924EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
925
926/**
927 * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
928 * @chan: DMA channel to offload copy to
929 * @page: destination page
930 * @offset: offset in page to copy to
931 * @kdata: source address (virtual)
932 * @len: length
933 *
934 * Both @page/@offset and @kdata must be mappable to a bus address according
935 * to the DMA mapping API rules for streaming mappings.
936 * Both @page/@offset and @kdata must stay memory resident (kernel memory or
937 * locked user space pages)
938 */
939dma_cookie_t
940dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
941 unsigned int offset, void *kdata, 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;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200947 unsigned long flags;
Dan Williams7405f742007-01-02 11:10:43 -0700948
Dan Williams00367312008-02-02 19:49:57 -0700949 dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
950 dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200951 flags = DMA_CTRL_ACK | DMA_COMPL_SRC_UNMAP_SINGLE;
952 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
Dan Williams00367312008-02-02 19:49:57 -0700953
954 if (!tx) {
955 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
956 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700957 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700958 }
Dan Williams7405f742007-01-02 11:10:43 -0700959
Dan Williams7405f742007-01-02 11:10:43 -0700960 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700961 cookie = tx->tx_submit(tx);
962
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900963 preempt_disable();
964 __this_cpu_add(chan->local->bytes_transferred, len);
965 __this_cpu_inc(chan->local->memcpy_count);
966 preempt_enable();
Dan Williams7405f742007-01-02 11:10:43 -0700967
968 return cookie;
969}
970EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
971
972/**
973 * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
974 * @chan: DMA channel to offload copy to
975 * @dest_pg: destination page
976 * @dest_off: offset in page to copy to
977 * @src_pg: source page
978 * @src_off: offset in page to copy from
979 * @len: length
980 *
981 * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
982 * address according to the DMA mapping API rules for streaming mappings.
983 * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
984 * (kernel memory or locked user space pages).
985 */
986dma_cookie_t
987dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
988 unsigned int dest_off, struct page *src_pg, unsigned int src_off,
989 size_t len)
990{
991 struct dma_device *dev = chan->device;
992 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700993 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700994 dma_cookie_t cookie;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200995 unsigned long flags;
Dan Williams7405f742007-01-02 11:10:43 -0700996
Dan Williams00367312008-02-02 19:49:57 -0700997 dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
998 dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
999 DMA_FROM_DEVICE);
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001000 flags = DMA_CTRL_ACK;
1001 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
Dan Williams00367312008-02-02 19:49:57 -07001002
1003 if (!tx) {
1004 dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
1005 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -07001006 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -07001007 }
Dan Williams7405f742007-01-02 11:10:43 -07001008
Dan Williams7405f742007-01-02 11:10:43 -07001009 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -07001010 cookie = tx->tx_submit(tx);
1011
Christoph Lametere7dcaa42009-10-03 19:48:23 +09001012 preempt_disable();
1013 __this_cpu_add(chan->local->bytes_transferred, len);
1014 __this_cpu_inc(chan->local->memcpy_count);
1015 preempt_enable();
Dan Williams7405f742007-01-02 11:10:43 -07001016
1017 return cookie;
1018}
1019EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
1020
1021void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1022 struct dma_chan *chan)
1023{
1024 tx->chan = chan;
Dan Williams5fc6d892010-10-07 16:44:50 -07001025 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williams7405f742007-01-02 11:10:43 -07001026 spin_lock_init(&tx->lock);
Dan Williamscaa20d972010-05-17 16:24:16 -07001027 #endif
Dan Williams7405f742007-01-02 11:10:43 -07001028}
1029EXPORT_SYMBOL(dma_async_tx_descriptor_init);
1030
Dan Williams07f22112009-01-05 17:14:31 -07001031/* dma_wait_for_async_tx - spin wait for a transaction to complete
1032 * @tx: in-flight transaction to wait on
Dan Williams07f22112009-01-05 17:14:31 -07001033 */
1034enum dma_status
1035dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1036{
Dan Williams95475e52009-07-14 12:19:02 -07001037 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
Dan Williams07f22112009-01-05 17:14:31 -07001038
1039 if (!tx)
1040 return DMA_SUCCESS;
1041
Dan Williams95475e52009-07-14 12:19:02 -07001042 while (tx->cookie == -EBUSY) {
1043 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
1044 pr_err("%s timeout waiting for descriptor submission\n",
Joe Perches63433252012-07-18 09:51:28 -07001045 __func__);
Dan Williams95475e52009-07-14 12:19:02 -07001046 return DMA_ERROR;
1047 }
1048 cpu_relax();
1049 }
1050 return dma_sync_wait(tx->chan, tx->cookie);
Dan Williams07f22112009-01-05 17:14:31 -07001051}
1052EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
1053
1054/* dma_run_dependencies - helper routine for dma drivers to process
1055 * (start) dependent operations on their target channel
1056 * @tx: transaction with dependencies
1057 */
1058void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
1059{
Dan Williamscaa20d972010-05-17 16:24:16 -07001060 struct dma_async_tx_descriptor *dep = txd_next(tx);
Dan Williams07f22112009-01-05 17:14:31 -07001061 struct dma_async_tx_descriptor *dep_next;
1062 struct dma_chan *chan;
1063
1064 if (!dep)
1065 return;
1066
Yuri Tikhonovdd59b852009-01-12 15:17:20 -07001067 /* we'll submit tx->next now, so clear the link */
Dan Williamscaa20d972010-05-17 16:24:16 -07001068 txd_clear_next(tx);
Dan Williams07f22112009-01-05 17:14:31 -07001069 chan = dep->chan;
1070
1071 /* keep submitting up until a channel switch is detected
1072 * in that case we will be called again as a result of
1073 * processing the interrupt from async_tx_channel_switch
1074 */
1075 for (; dep; dep = dep_next) {
Dan Williamscaa20d972010-05-17 16:24:16 -07001076 txd_lock(dep);
1077 txd_clear_parent(dep);
1078 dep_next = txd_next(dep);
Dan Williams07f22112009-01-05 17:14:31 -07001079 if (dep_next && dep_next->chan == chan)
Dan Williamscaa20d972010-05-17 16:24:16 -07001080 txd_clear_next(dep); /* ->next will be submitted */
Dan Williams07f22112009-01-05 17:14:31 -07001081 else
1082 dep_next = NULL; /* submit current dep and terminate */
Dan Williamscaa20d972010-05-17 16:24:16 -07001083 txd_unlock(dep);
Dan Williams07f22112009-01-05 17:14:31 -07001084
1085 dep->tx_submit(dep);
1086 }
1087
1088 chan->device->device_issue_pending(chan);
1089}
1090EXPORT_SYMBOL_GPL(dma_run_dependencies);
1091
Chris Leechc13c8262006-05-23 17:18:44 -07001092static int __init dma_bus_init(void)
1093{
Chris Leechc13c8262006-05-23 17:18:44 -07001094 return class_register(&dma_devclass);
1095}
Dan Williams652afc22009-01-06 11:38:22 -07001096arch_initcall(dma_bus_init);
Chris Leechc13c8262006-05-23 17:18:44 -07001097
Dan Williamsbec08512009-01-06 11:38:14 -07001098