Merge tag 'dm-3.12-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/device...
[firefly-linux-kernel-4.4.55.git] / drivers / dma / dmaengine.c
1 /*
2  * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59
16  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called COPYING.
20  */
21
22 /*
23  * This code implements the DMA subsystem. It provides a HW-neutral interface
24  * for other kernel code to use asynchronous memory copy capabilities,
25  * if present, and allows different HW DMA drivers to register as providing
26  * this capability.
27  *
28  * Due to the fact we are accelerating what is already a relatively fast
29  * operation, the code goes to great lengths to avoid additional overhead,
30  * such as locking.
31  *
32  * LOCKING:
33  *
34  * The subsystem keeps a global list of dma_device structs it is protected by a
35  * mutex, dma_list_mutex.
36  *
37  * 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  *
42  * 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  *
45  * See Documentation/dmaengine.txt for more details
46  */
47
48 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49
50 #include <linux/dma-mapping.h>
51 #include <linux/init.h>
52 #include <linux/module.h>
53 #include <linux/mm.h>
54 #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>
61 #include <linux/jiffies.h>
62 #include <linux/rculist.h>
63 #include <linux/idr.h>
64 #include <linux/slab.h>
65 #include <linux/acpi.h>
66 #include <linux/acpi_dma.h>
67 #include <linux/of_dma.h>
68
69 static DEFINE_MUTEX(dma_list_mutex);
70 static DEFINE_IDR(dma_idr);
71 static LIST_HEAD(dma_device_list);
72 static long dmaengine_ref_count;
73
74 /* --- sysfs implementation --- */
75
76 /**
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  */
82 static 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
90 static ssize_t memcpy_count_show(struct device *dev,
91                                  struct device_attribute *attr, char *buf)
92 {
93         struct dma_chan *chan;
94         unsigned long count = 0;
95         int i;
96         int err;
97
98         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);
107
108         return err;
109 }
110 static DEVICE_ATTR_RO(memcpy_count);
111
112 static ssize_t bytes_transferred_show(struct device *dev,
113                                       struct device_attribute *attr, char *buf)
114 {
115         struct dma_chan *chan;
116         unsigned long count = 0;
117         int i;
118         int err;
119
120         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);
129
130         return err;
131 }
132 static DEVICE_ATTR_RO(bytes_transferred);
133
134 static ssize_t in_use_show(struct device *dev, struct device_attribute *attr,
135                            char *buf)
136 {
137         struct dma_chan *chan;
138         int err;
139
140         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;
149 }
150 static DEVICE_ATTR_RO(in_use);
151
152 static 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,
157 };
158 ATTRIBUTE_GROUPS(dma_dev);
159
160 static 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);
165         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         }
171         kfree(chan_dev);
172 }
173
174 static struct class dma_devclass = {
175         .name           = "dma",
176         .dev_groups     = dma_dev_groups,
177         .dev_release    = chan_dev_release,
178 };
179
180 /* --- client and device registration --- */
181
182 #define dma_device_satisfies_mask(device, mask) \
183         __dma_device_satisfies_mask((device), &(mask))
184 static int
185 __dma_device_satisfies_mask(struct dma_device *device,
186                             const dma_cap_mask_t *want)
187 {
188         dma_cap_mask_t has;
189
190         bitmap_and(has.bits, want->bits, device->cap_mask.bits,
191                 DMA_TX_TYPE_END);
192         return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
193 }
194
195 static 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  */
206 static 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  */
222 static 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) {
238                 int desc_cnt = chan->device->device_alloc_chan_resources(chan);
239
240                 if (desc_cnt < 0) {
241                         err = desc_cnt;
242                         chan->client_count = 0;
243                         module_put(owner);
244                 } else if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
245                         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  */
257 static 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
267 enum 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)) {
276                         pr_err("%s: timeout!\n", __func__);
277                         return DMA_ERROR;
278                 }
279                 if (status != DMA_IN_PROGRESS)
280                         break;
281                 cpu_relax();
282         } while (1);
283
284         return status;
285 }
286 EXPORT_SYMBOL(dma_sync_wait);
287
288 /**
289  * dma_cap_mask_all - enable iteration over all operation types
290  */
291 static 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  */
297 struct 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  */
304 static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
305
306 static 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
313         /* '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
316          */
317         clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
318         clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
319         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) {
330                 pr_err("initialization failure\n");
331                 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 }
338 arch_initcall(dma_channel_table_init);
339
340 /**
341  * dma_find_channel - find a channel to carry out the operation
342  * @tx_type: transaction type
343  */
344 struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
345 {
346         return this_cpu_read(channel_table[tx_type]->chan);
347 }
348 EXPORT_SYMBOL(dma_find_channel);
349
350 /*
351  * net_dma_find_channel - find a channel for net_dma
352  * net_dma has alignment requirements
353  */
354 struct 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 }
362 EXPORT_SYMBOL(net_dma_find_channel);
363
364 /**
365  * dma_issue_pending_all - flush all pending operations across all channels
366  */
367 void dma_issue_pending_all(void)
368 {
369         struct dma_device *device;
370         struct dma_chan *chan;
371
372         rcu_read_lock();
373         list_for_each_entry_rcu(device, &dma_device_list, global_node) {
374                 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
375                         continue;
376                 list_for_each_entry(chan, &device->channels, device_node)
377                         if (chan->client_count)
378                                 device->device_issue_pending(chan);
379         }
380         rcu_read_unlock();
381 }
382 EXPORT_SYMBOL(dma_issue_pending_all);
383
384 /**
385  * dma_chan_is_local - returns true if the channel is in the same numa-node as the cpu
386  */
387 static bool dma_chan_is_local(struct dma_chan *chan, int cpu)
388 {
389         int node = dev_to_node(chan->device->dev);
390         return node == -1 || cpumask_test_cpu(cpu, cpumask_of_node(node));
391 }
392
393 /**
394  * min_chan - returns the channel with min count and in the same numa-node as the cpu
395  * @cap: capability to match
396  * @cpu: cpu index which the channel should be close to
397  *
398  * If some channels are close to the given cpu, the one with the lowest
399  * reference count is returned. Otherwise, cpu is ignored and only the
400  * reference count is taken into account.
401  * Must be called under dma_list_mutex.
402  */
403 static struct dma_chan *min_chan(enum dma_transaction_type cap, int cpu)
404 {
405         struct dma_device *device;
406         struct dma_chan *chan;
407         struct dma_chan *min = NULL;
408         struct dma_chan *localmin = NULL;
409
410         list_for_each_entry(device, &dma_device_list, global_node) {
411                 if (!dma_has_cap(cap, device->cap_mask) ||
412                     dma_has_cap(DMA_PRIVATE, device->cap_mask))
413                         continue;
414                 list_for_each_entry(chan, &device->channels, device_node) {
415                         if (!chan->client_count)
416                                 continue;
417                         if (!min || chan->table_count < min->table_count)
418                                 min = chan;
419
420                         if (dma_chan_is_local(chan, cpu))
421                                 if (!localmin ||
422                                     chan->table_count < localmin->table_count)
423                                         localmin = chan;
424                 }
425         }
426
427         chan = localmin ? localmin : min;
428
429         if (chan)
430                 chan->table_count++;
431
432         return chan;
433 }
434
435 /**
436  * dma_channel_rebalance - redistribute the available channels
437  *
438  * Optimize for cpu isolation (each cpu gets a dedicated channel for an
439  * operation type) in the SMP case,  and operation isolation (avoid
440  * multi-tasking channels) in the non-SMP case.  Must be called under
441  * dma_list_mutex.
442  */
443 static void dma_channel_rebalance(void)
444 {
445         struct dma_chan *chan;
446         struct dma_device *device;
447         int cpu;
448         int cap;
449
450         /* undo the last distribution */
451         for_each_dma_cap_mask(cap, dma_cap_mask_all)
452                 for_each_possible_cpu(cpu)
453                         per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
454
455         list_for_each_entry(device, &dma_device_list, global_node) {
456                 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
457                         continue;
458                 list_for_each_entry(chan, &device->channels, device_node)
459                         chan->table_count = 0;
460         }
461
462         /* don't populate the channel_table if no clients are available */
463         if (!dmaengine_ref_count)
464                 return;
465
466         /* redistribute available channels */
467         for_each_dma_cap_mask(cap, dma_cap_mask_all)
468                 for_each_online_cpu(cpu) {
469                         chan = min_chan(cap, cpu);
470                         per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
471                 }
472 }
473
474 static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
475                                           struct dma_device *dev,
476                                           dma_filter_fn fn, void *fn_param)
477 {
478         struct dma_chan *chan;
479
480         if (!__dma_device_satisfies_mask(dev, mask)) {
481                 pr_debug("%s: wrong capabilities\n", __func__);
482                 return NULL;
483         }
484         /* devices with multiple channels need special handling as we need to
485          * ensure that all channels are either private or public.
486          */
487         if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
488                 list_for_each_entry(chan, &dev->channels, device_node) {
489                         /* some channels are already publicly allocated */
490                         if (chan->client_count)
491                                 return NULL;
492                 }
493
494         list_for_each_entry(chan, &dev->channels, device_node) {
495                 if (chan->client_count) {
496                         pr_debug("%s: %s busy\n",
497                                  __func__, dma_chan_name(chan));
498                         continue;
499                 }
500                 if (fn && !fn(chan, fn_param)) {
501                         pr_debug("%s: %s filter said false\n",
502                                  __func__, dma_chan_name(chan));
503                         continue;
504                 }
505                 return chan;
506         }
507
508         return NULL;
509 }
510
511 /**
512  * dma_request_channel - try to allocate an exclusive channel
513  * @mask: capabilities that the channel must satisfy
514  * @fn: optional callback to disposition available channels
515  * @fn_param: opaque parameter to pass to dma_filter_fn
516  */
517 struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
518                                        dma_filter_fn fn, void *fn_param)
519 {
520         struct dma_device *device, *_d;
521         struct dma_chan *chan = NULL;
522         int err;
523
524         /* Find a channel */
525         mutex_lock(&dma_list_mutex);
526         list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
527                 chan = private_candidate(mask, device, fn, fn_param);
528                 if (chan) {
529                         /* Found a suitable channel, try to grab, prep, and
530                          * return it.  We first set DMA_PRIVATE to disable
531                          * balance_ref_count as this channel will not be
532                          * published in the general-purpose allocator
533                          */
534                         dma_cap_set(DMA_PRIVATE, device->cap_mask);
535                         device->privatecnt++;
536                         err = dma_chan_get(chan);
537
538                         if (err == -ENODEV) {
539                                 pr_debug("%s: %s module removed\n",
540                                          __func__, dma_chan_name(chan));
541                                 list_del_rcu(&device->global_node);
542                         } else if (err)
543                                 pr_debug("%s: failed to get %s: (%d)\n",
544                                          __func__, dma_chan_name(chan), err);
545                         else
546                                 break;
547                         if (--device->privatecnt == 0)
548                                 dma_cap_clear(DMA_PRIVATE, device->cap_mask);
549                         chan = NULL;
550                 }
551         }
552         mutex_unlock(&dma_list_mutex);
553
554         pr_debug("%s: %s (%s)\n",
555                  __func__,
556                  chan ? "success" : "fail",
557                  chan ? dma_chan_name(chan) : NULL);
558
559         return chan;
560 }
561 EXPORT_SYMBOL_GPL(__dma_request_channel);
562
563 /**
564  * dma_request_slave_channel - try to allocate an exclusive slave channel
565  * @dev:        pointer to client device structure
566  * @name:       slave channel name
567  */
568 struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name)
569 {
570         /* If device-tree is present get slave info from here */
571         if (dev->of_node)
572                 return of_dma_request_slave_channel(dev->of_node, name);
573
574         /* If device was enumerated by ACPI get slave info from here */
575         if (ACPI_HANDLE(dev))
576                 return acpi_dma_request_slave_chan_by_name(dev, name);
577
578         return NULL;
579 }
580 EXPORT_SYMBOL_GPL(dma_request_slave_channel);
581
582 void dma_release_channel(struct dma_chan *chan)
583 {
584         mutex_lock(&dma_list_mutex);
585         WARN_ONCE(chan->client_count != 1,
586                   "chan reference count %d != 1\n", chan->client_count);
587         dma_chan_put(chan);
588         /* drop PRIVATE cap enabled by __dma_request_channel() */
589         if (--chan->device->privatecnt == 0)
590                 dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
591         mutex_unlock(&dma_list_mutex);
592 }
593 EXPORT_SYMBOL_GPL(dma_release_channel);
594
595 /**
596  * dmaengine_get - register interest in dma_channels
597  */
598 void dmaengine_get(void)
599 {
600         struct dma_device *device, *_d;
601         struct dma_chan *chan;
602         int err;
603
604         mutex_lock(&dma_list_mutex);
605         dmaengine_ref_count++;
606
607         /* try to grab channels */
608         list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
609                 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
610                         continue;
611                 list_for_each_entry(chan, &device->channels, device_node) {
612                         err = dma_chan_get(chan);
613                         if (err == -ENODEV) {
614                                 /* module removed before we could use it */
615                                 list_del_rcu(&device->global_node);
616                                 break;
617                         } else if (err)
618                                 pr_debug("%s: failed to get %s: (%d)\n",
619                                        __func__, dma_chan_name(chan), err);
620                 }
621         }
622
623         /* if this is the first reference and there were channels
624          * waiting we need to rebalance to get those channels
625          * incorporated into the channel table
626          */
627         if (dmaengine_ref_count == 1)
628                 dma_channel_rebalance();
629         mutex_unlock(&dma_list_mutex);
630 }
631 EXPORT_SYMBOL(dmaengine_get);
632
633 /**
634  * dmaengine_put - let dma drivers be removed when ref_count == 0
635  */
636 void dmaengine_put(void)
637 {
638         struct dma_device *device;
639         struct dma_chan *chan;
640
641         mutex_lock(&dma_list_mutex);
642         dmaengine_ref_count--;
643         BUG_ON(dmaengine_ref_count < 0);
644         /* drop channel references */
645         list_for_each_entry(device, &dma_device_list, global_node) {
646                 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
647                         continue;
648                 list_for_each_entry(chan, &device->channels, device_node)
649                         dma_chan_put(chan);
650         }
651         mutex_unlock(&dma_list_mutex);
652 }
653 EXPORT_SYMBOL(dmaengine_put);
654
655 static bool device_has_all_tx_types(struct dma_device *device)
656 {
657         /* A device that satisfies this test has channels that will never cause
658          * an async_tx channel switch event as all possible operation types can
659          * be handled.
660          */
661         #ifdef CONFIG_ASYNC_TX_DMA
662         if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
663                 return false;
664         #endif
665
666         #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE)
667         if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
668                 return false;
669         #endif
670
671         #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE)
672         if (!dma_has_cap(DMA_XOR, device->cap_mask))
673                 return false;
674
675         #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
676         if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
677                 return false;
678         #endif
679         #endif
680
681         #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE)
682         if (!dma_has_cap(DMA_PQ, device->cap_mask))
683                 return false;
684
685         #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
686         if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
687                 return false;
688         #endif
689         #endif
690
691         return true;
692 }
693
694 static int get_dma_id(struct dma_device *device)
695 {
696         int rc;
697
698         mutex_lock(&dma_list_mutex);
699
700         rc = idr_alloc(&dma_idr, NULL, 0, 0, GFP_KERNEL);
701         if (rc >= 0)
702                 device->dev_id = rc;
703
704         mutex_unlock(&dma_list_mutex);
705         return rc < 0 ? rc : 0;
706 }
707
708 /**
709  * dma_async_device_register - registers DMA devices found
710  * @device: &dma_device
711  */
712 int dma_async_device_register(struct dma_device *device)
713 {
714         int chancnt = 0, rc;
715         struct dma_chan* chan;
716         atomic_t *idr_ref;
717
718         if (!device)
719                 return -ENODEV;
720
721         /* validate device routines */
722         BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
723                 !device->device_prep_dma_memcpy);
724         BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
725                 !device->device_prep_dma_xor);
726         BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) &&
727                 !device->device_prep_dma_xor_val);
728         BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) &&
729                 !device->device_prep_dma_pq);
730         BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) &&
731                 !device->device_prep_dma_pq_val);
732         BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
733                 !device->device_prep_dma_interrupt);
734         BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) &&
735                 !device->device_prep_dma_sg);
736         BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) &&
737                 !device->device_prep_dma_cyclic);
738         BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
739                 !device->device_control);
740         BUG_ON(dma_has_cap(DMA_INTERLEAVE, device->cap_mask) &&
741                 !device->device_prep_interleaved_dma);
742
743         BUG_ON(!device->device_alloc_chan_resources);
744         BUG_ON(!device->device_free_chan_resources);
745         BUG_ON(!device->device_tx_status);
746         BUG_ON(!device->device_issue_pending);
747         BUG_ON(!device->dev);
748
749         /* note: this only matters in the
750          * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
751          */
752         if (device_has_all_tx_types(device))
753                 dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
754
755         idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
756         if (!idr_ref)
757                 return -ENOMEM;
758         rc = get_dma_id(device);
759         if (rc != 0) {
760                 kfree(idr_ref);
761                 return rc;
762         }
763
764         atomic_set(idr_ref, 0);
765
766         /* represent channels in sysfs. Probably want devs too */
767         list_for_each_entry(chan, &device->channels, device_node) {
768                 rc = -ENOMEM;
769                 chan->local = alloc_percpu(typeof(*chan->local));
770                 if (chan->local == NULL)
771                         goto err_out;
772                 chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
773                 if (chan->dev == NULL) {
774                         free_percpu(chan->local);
775                         chan->local = NULL;
776                         goto err_out;
777                 }
778
779                 chan->chan_id = chancnt++;
780                 chan->dev->device.class = &dma_devclass;
781                 chan->dev->device.parent = device->dev;
782                 chan->dev->chan = chan;
783                 chan->dev->idr_ref = idr_ref;
784                 chan->dev->dev_id = device->dev_id;
785                 atomic_inc(idr_ref);
786                 dev_set_name(&chan->dev->device, "dma%dchan%d",
787                              device->dev_id, chan->chan_id);
788
789                 rc = device_register(&chan->dev->device);
790                 if (rc) {
791                         free_percpu(chan->local);
792                         chan->local = NULL;
793                         kfree(chan->dev);
794                         atomic_dec(idr_ref);
795                         goto err_out;
796                 }
797                 chan->client_count = 0;
798         }
799         device->chancnt = chancnt;
800
801         mutex_lock(&dma_list_mutex);
802         /* take references on public channels */
803         if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
804                 list_for_each_entry(chan, &device->channels, device_node) {
805                         /* if clients are already waiting for channels we need
806                          * to take references on their behalf
807                          */
808                         if (dma_chan_get(chan) == -ENODEV) {
809                                 /* note we can only get here for the first
810                                  * channel as the remaining channels are
811                                  * guaranteed to get a reference
812                                  */
813                                 rc = -ENODEV;
814                                 mutex_unlock(&dma_list_mutex);
815                                 goto err_out;
816                         }
817                 }
818         list_add_tail_rcu(&device->global_node, &dma_device_list);
819         if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
820                 device->privatecnt++;   /* Always private */
821         dma_channel_rebalance();
822         mutex_unlock(&dma_list_mutex);
823
824         return 0;
825
826 err_out:
827         /* if we never registered a channel just release the idr */
828         if (atomic_read(idr_ref) == 0) {
829                 mutex_lock(&dma_list_mutex);
830                 idr_remove(&dma_idr, device->dev_id);
831                 mutex_unlock(&dma_list_mutex);
832                 kfree(idr_ref);
833                 return rc;
834         }
835
836         list_for_each_entry(chan, &device->channels, device_node) {
837                 if (chan->local == NULL)
838                         continue;
839                 mutex_lock(&dma_list_mutex);
840                 chan->dev->chan = NULL;
841                 mutex_unlock(&dma_list_mutex);
842                 device_unregister(&chan->dev->device);
843                 free_percpu(chan->local);
844         }
845         return rc;
846 }
847 EXPORT_SYMBOL(dma_async_device_register);
848
849 /**
850  * dma_async_device_unregister - unregister a DMA device
851  * @device: &dma_device
852  *
853  * This routine is called by dma driver exit routines, dmaengine holds module
854  * references to prevent it being called while channels are in use.
855  */
856 void dma_async_device_unregister(struct dma_device *device)
857 {
858         struct dma_chan *chan;
859
860         mutex_lock(&dma_list_mutex);
861         list_del_rcu(&device->global_node);
862         dma_channel_rebalance();
863         mutex_unlock(&dma_list_mutex);
864
865         list_for_each_entry(chan, &device->channels, device_node) {
866                 WARN_ONCE(chan->client_count,
867                           "%s called while %d clients hold a reference\n",
868                           __func__, chan->client_count);
869                 mutex_lock(&dma_list_mutex);
870                 chan->dev->chan = NULL;
871                 mutex_unlock(&dma_list_mutex);
872                 device_unregister(&chan->dev->device);
873                 free_percpu(chan->local);
874         }
875 }
876 EXPORT_SYMBOL(dma_async_device_unregister);
877
878 /**
879  * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
880  * @chan: DMA channel to offload copy to
881  * @dest: destination address (virtual)
882  * @src: source address (virtual)
883  * @len: length
884  *
885  * Both @dest and @src must be mappable to a bus address according to the
886  * DMA mapping API rules for streaming mappings.
887  * Both @dest and @src must stay memory resident (kernel memory or locked
888  * user space pages).
889  */
890 dma_cookie_t
891 dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
892                         void *src, size_t len)
893 {
894         struct dma_device *dev = chan->device;
895         struct dma_async_tx_descriptor *tx;
896         dma_addr_t dma_dest, dma_src;
897         dma_cookie_t cookie;
898         unsigned long flags;
899
900         dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
901         dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
902         flags = DMA_CTRL_ACK |
903                 DMA_COMPL_SRC_UNMAP_SINGLE |
904                 DMA_COMPL_DEST_UNMAP_SINGLE;
905         tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
906
907         if (!tx) {
908                 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
909                 dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
910                 return -ENOMEM;
911         }
912
913         tx->callback = NULL;
914         cookie = tx->tx_submit(tx);
915
916         preempt_disable();
917         __this_cpu_add(chan->local->bytes_transferred, len);
918         __this_cpu_inc(chan->local->memcpy_count);
919         preempt_enable();
920
921         return cookie;
922 }
923 EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
924
925 /**
926  * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
927  * @chan: DMA channel to offload copy to
928  * @page: destination page
929  * @offset: offset in page to copy to
930  * @kdata: source address (virtual)
931  * @len: length
932  *
933  * Both @page/@offset and @kdata must be mappable to a bus address according
934  * to the DMA mapping API rules for streaming mappings.
935  * Both @page/@offset and @kdata must stay memory resident (kernel memory or
936  * locked user space pages)
937  */
938 dma_cookie_t
939 dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
940                         unsigned int offset, void *kdata, size_t len)
941 {
942         struct dma_device *dev = chan->device;
943         struct dma_async_tx_descriptor *tx;
944         dma_addr_t dma_dest, dma_src;
945         dma_cookie_t cookie;
946         unsigned long flags;
947
948         dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
949         dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
950         flags = DMA_CTRL_ACK | DMA_COMPL_SRC_UNMAP_SINGLE;
951         tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
952
953         if (!tx) {
954                 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
955                 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
956                 return -ENOMEM;
957         }
958
959         tx->callback = NULL;
960         cookie = tx->tx_submit(tx);
961
962         preempt_disable();
963         __this_cpu_add(chan->local->bytes_transferred, len);
964         __this_cpu_inc(chan->local->memcpy_count);
965         preempt_enable();
966
967         return cookie;
968 }
969 EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
970
971 /**
972  * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
973  * @chan: DMA channel to offload copy to
974  * @dest_pg: destination page
975  * @dest_off: offset in page to copy to
976  * @src_pg: source page
977  * @src_off: offset in page to copy from
978  * @len: length
979  *
980  * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
981  * address according to the DMA mapping API rules for streaming mappings.
982  * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
983  * (kernel memory or locked user space pages).
984  */
985 dma_cookie_t
986 dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
987         unsigned int dest_off, struct page *src_pg, unsigned int src_off,
988         size_t len)
989 {
990         struct dma_device *dev = chan->device;
991         struct dma_async_tx_descriptor *tx;
992         dma_addr_t dma_dest, dma_src;
993         dma_cookie_t cookie;
994         unsigned long flags;
995
996         dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
997         dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
998                                 DMA_FROM_DEVICE);
999         flags = DMA_CTRL_ACK;
1000         tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
1001
1002         if (!tx) {
1003                 dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
1004                 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
1005                 return -ENOMEM;
1006         }
1007
1008         tx->callback = NULL;
1009         cookie = tx->tx_submit(tx);
1010
1011         preempt_disable();
1012         __this_cpu_add(chan->local->bytes_transferred, len);
1013         __this_cpu_inc(chan->local->memcpy_count);
1014         preempt_enable();
1015
1016         return cookie;
1017 }
1018 EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
1019
1020 void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1021         struct dma_chan *chan)
1022 {
1023         tx->chan = chan;
1024         #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
1025         spin_lock_init(&tx->lock);
1026         #endif
1027 }
1028 EXPORT_SYMBOL(dma_async_tx_descriptor_init);
1029
1030 /* dma_wait_for_async_tx - spin wait for a transaction to complete
1031  * @tx: in-flight transaction to wait on
1032  */
1033 enum dma_status
1034 dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1035 {
1036         unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
1037
1038         if (!tx)
1039                 return DMA_SUCCESS;
1040
1041         while (tx->cookie == -EBUSY) {
1042                 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
1043                         pr_err("%s timeout waiting for descriptor submission\n",
1044                                __func__);
1045                         return DMA_ERROR;
1046                 }
1047                 cpu_relax();
1048         }
1049         return dma_sync_wait(tx->chan, tx->cookie);
1050 }
1051 EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
1052
1053 /* dma_run_dependencies - helper routine for dma drivers to process
1054  *      (start) dependent operations on their target channel
1055  * @tx: transaction with dependencies
1056  */
1057 void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
1058 {
1059         struct dma_async_tx_descriptor *dep = txd_next(tx);
1060         struct dma_async_tx_descriptor *dep_next;
1061         struct dma_chan *chan;
1062
1063         if (!dep)
1064                 return;
1065
1066         /* we'll submit tx->next now, so clear the link */
1067         txd_clear_next(tx);
1068         chan = dep->chan;
1069
1070         /* keep submitting up until a channel switch is detected
1071          * in that case we will be called again as a result of
1072          * processing the interrupt from async_tx_channel_switch
1073          */
1074         for (; dep; dep = dep_next) {
1075                 txd_lock(dep);
1076                 txd_clear_parent(dep);
1077                 dep_next = txd_next(dep);
1078                 if (dep_next && dep_next->chan == chan)
1079                         txd_clear_next(dep); /* ->next will be submitted */
1080                 else
1081                         dep_next = NULL; /* submit current dep and terminate */
1082                 txd_unlock(dep);
1083
1084                 dep->tx_submit(dep);
1085         }
1086
1087         chan->device->device_issue_pending(chan);
1088 }
1089 EXPORT_SYMBOL_GPL(dma_run_dependencies);
1090
1091 static int __init dma_bus_init(void)
1092 {
1093         return class_register(&dma_devclass);
1094 }
1095 arch_initcall(dma_bus_init);
1096
1097