Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
4 */
5
6/*
7 * This code implements the DMA subsystem. It provides a HW-neutral interface
8 * for other kernel code to use asynchronous memory copy capabilities,
9 * if present, and allows different HW DMA drivers to register as providing
10 * this capability.
11 *
12 * Due to the fact we are accelerating what is already a relatively fast
13 * operation, the code goes to great lengths to avoid additional overhead,
14 * such as locking.
15 *
16 * LOCKING:
17 *
18 * The subsystem keeps a global list of dma_device structs it is protected by a
19 * mutex, dma_list_mutex.
20 *
21 * A subsystem can get access to a channel by calling dmaengine_get() followed
22 * by dma_find_channel(), or if it has need for an exclusive channel it can call
23 * dma_request_channel(). Once a channel is allocated a reference is taken
24 * against its corresponding driver to disable removal.
25 *
26 * Each device has a channels list, which runs unlocked but is never modified
27 * once the device is registered, it's just setup by the driver.
28 *
29 * See Documentation/driver-api/dmaengine for more details
30 */
31
32#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
34#include <linux/platform_device.h>
35#include <linux/dma-mapping.h>
36#include <linux/init.h>
37#include <linux/module.h>
38#include <linux/mm.h>
39#include <linux/device.h>
40#include <linux/dmaengine.h>
41#include <linux/hardirq.h>
42#include <linux/spinlock.h>
43#include <linux/percpu.h>
44#include <linux/rcupdate.h>
45#include <linux/mutex.h>
46#include <linux/jiffies.h>
47#include <linux/rculist.h>
48#include <linux/idr.h>
49#include <linux/slab.h>
50#include <linux/acpi.h>
51#include <linux/acpi_dma.h>
52#include <linux/of_dma.h>
53#include <linux/mempool.h>
54#include <linux/numa.h>
55
56static DEFINE_MUTEX(dma_list_mutex);
57static DEFINE_IDA(dma_ida);
58static LIST_HEAD(dma_device_list);
59static long dmaengine_ref_count;
60
61/* --- debugfs implementation --- */
62#ifdef CONFIG_DEBUG_FS
63#include <linux/debugfs.h>
64
65static struct dentry *rootdir;
66
67static void dmaengine_debug_register(struct dma_device *dma_dev)
68{
69 dma_dev->dbg_dev_root = debugfs_create_dir(dev_name(dma_dev->dev),
70 rootdir);
71 if (IS_ERR(dma_dev->dbg_dev_root))
72 dma_dev->dbg_dev_root = NULL;
73}
74
75static void dmaengine_debug_unregister(struct dma_device *dma_dev)
76{
77 debugfs_remove_recursive(dma_dev->dbg_dev_root);
78 dma_dev->dbg_dev_root = NULL;
79}
80
81static void dmaengine_dbg_summary_show(struct seq_file *s,
82 struct dma_device *dma_dev)
83{
84 struct dma_chan *chan;
85
86 list_for_each_entry(chan, &dma_dev->channels, device_node) {
87 if (chan->client_count) {
88 seq_printf(s, " %-13s| %s", dma_chan_name(chan),
89 chan->dbg_client_name ?: "in-use");
90
91 if (chan->router)
92 seq_printf(s, " (via router: %s)\n",
93 dev_name(chan->router->dev));
94 else
95 seq_puts(s, "\n");
96 }
97 }
98}
99
100static int dmaengine_summary_show(struct seq_file *s, void *data)
101{
102 struct dma_device *dma_dev = NULL;
103
104 mutex_lock(&dma_list_mutex);
105 list_for_each_entry(dma_dev, &dma_device_list, global_node) {
106 seq_printf(s, "dma%d (%s): number of channels: %u\n",
107 dma_dev->dev_id, dev_name(dma_dev->dev),
108 dma_dev->chancnt);
109
110 if (dma_dev->dbg_summary_show)
111 dma_dev->dbg_summary_show(s, dma_dev);
112 else
113 dmaengine_dbg_summary_show(s, dma_dev);
114
115 if (!list_is_last(&dma_dev->global_node, &dma_device_list))
116 seq_puts(s, "\n");
117 }
118 mutex_unlock(&dma_list_mutex);
119
120 return 0;
121}
122DEFINE_SHOW_ATTRIBUTE(dmaengine_summary);
123
124static void __init dmaengine_debugfs_init(void)
125{
126 rootdir = debugfs_create_dir("dmaengine", NULL);
127
128 /* /sys/kernel/debug/dmaengine/summary */
129 debugfs_create_file("summary", 0444, rootdir, NULL,
130 &dmaengine_summary_fops);
131}
132#else
133static inline void dmaengine_debugfs_init(void) { }
134static inline int dmaengine_debug_register(struct dma_device *dma_dev)
135{
136 return 0;
137}
138
139static inline void dmaengine_debug_unregister(struct dma_device *dma_dev) { }
140#endif /* DEBUG_FS */
141
142/* --- sysfs implementation --- */
143
144#define DMA_SLAVE_NAME "slave"
145
146/**
147 * dev_to_dma_chan - convert a device pointer to its sysfs container object
148 * @dev - device node
149 *
150 * Must be called under dma_list_mutex
151 */
152static struct dma_chan *dev_to_dma_chan(struct device *dev)
153{
154 struct dma_chan_dev *chan_dev;
155
156 chan_dev = container_of(dev, typeof(*chan_dev), device);
157 return chan_dev->chan;
158}
159
160static ssize_t memcpy_count_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
162{
163 struct dma_chan *chan;
164 unsigned long count = 0;
165 int i;
166 int err;
167
168 mutex_lock(&dma_list_mutex);
169 chan = dev_to_dma_chan(dev);
170 if (chan) {
171 for_each_possible_cpu(i)
172 count += per_cpu_ptr(chan->local, i)->memcpy_count;
173 err = sprintf(buf, "%lu\n", count);
174 } else
175 err = -ENODEV;
176 mutex_unlock(&dma_list_mutex);
177
178 return err;
179}
180static DEVICE_ATTR_RO(memcpy_count);
181
182static ssize_t bytes_transferred_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
185 struct dma_chan *chan;
186 unsigned long count = 0;
187 int i;
188 int err;
189
190 mutex_lock(&dma_list_mutex);
191 chan = dev_to_dma_chan(dev);
192 if (chan) {
193 for_each_possible_cpu(i)
194 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
195 err = sprintf(buf, "%lu\n", count);
196 } else
197 err = -ENODEV;
198 mutex_unlock(&dma_list_mutex);
199
200 return err;
201}
202static DEVICE_ATTR_RO(bytes_transferred);
203
204static ssize_t in_use_show(struct device *dev, struct device_attribute *attr,
205 char *buf)
206{
207 struct dma_chan *chan;
208 int err;
209
210 mutex_lock(&dma_list_mutex);
211 chan = dev_to_dma_chan(dev);
212 if (chan)
213 err = sprintf(buf, "%d\n", chan->client_count);
214 else
215 err = -ENODEV;
216 mutex_unlock(&dma_list_mutex);
217
218 return err;
219}
220static DEVICE_ATTR_RO(in_use);
221
222static struct attribute *dma_dev_attrs[] = {
223 &dev_attr_memcpy_count.attr,
224 &dev_attr_bytes_transferred.attr,
225 &dev_attr_in_use.attr,
226 NULL,
227};
228ATTRIBUTE_GROUPS(dma_dev);
229
230static void chan_dev_release(struct device *dev)
231{
232 struct dma_chan_dev *chan_dev;
233
234 chan_dev = container_of(dev, typeof(*chan_dev), device);
235 if (atomic_dec_and_test(chan_dev->idr_ref)) {
236 ida_free(&dma_ida, chan_dev->dev_id);
237 kfree(chan_dev->idr_ref);
238 }
239 kfree(chan_dev);
240}
241
242static struct class dma_devclass = {
243 .name = "dma",
244 .dev_groups = dma_dev_groups,
245 .dev_release = chan_dev_release,
246};
247
248/* --- client and device registration --- */
249
250/**
251 * dma_cap_mask_all - enable iteration over all operation types
252 */
253static dma_cap_mask_t dma_cap_mask_all;
254
255/**
256 * dma_chan_tbl_ent - tracks channel allocations per core/operation
257 * @chan - associated channel for this entry
258 */
259struct dma_chan_tbl_ent {
260 struct dma_chan *chan;
261};
262
263/**
264 * channel_table - percpu lookup table for memory-to-memory offload providers
265 */
266static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
267
268static int __init dma_channel_table_init(void)
269{
270 enum dma_transaction_type cap;
271 int err = 0;
272
273 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
274
275 /* 'interrupt', 'private', and 'slave' are channel capabilities,
276 * but are not associated with an operation so they do not need
277 * an entry in the channel_table
278 */
279 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
280 clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
281 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
282
283 for_each_dma_cap_mask(cap, dma_cap_mask_all) {
284 channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
285 if (!channel_table[cap]) {
286 err = -ENOMEM;
287 break;
288 }
289 }
290
291 if (err) {
292 pr_err("dmaengine dma_channel_table_init failure: %d\n", err);
293 for_each_dma_cap_mask(cap, dma_cap_mask_all)
294 free_percpu(channel_table[cap]);
295 }
296
297 return err;
298}
299arch_initcall(dma_channel_table_init);
300
301/**
302 * dma_chan_is_local - returns true if the channel is in the same numa-node as
303 * the cpu
304 */
305static bool dma_chan_is_local(struct dma_chan *chan, int cpu)
306{
307 int node = dev_to_node(chan->device->dev);
308 return node == NUMA_NO_NODE ||
309 cpumask_test_cpu(cpu, cpumask_of_node(node));
310}
311
312/**
313 * min_chan - returns the channel with min count and in the same numa-node as
314 * the cpu
315 * @cap: capability to match
316 * @cpu: cpu index which the channel should be close to
317 *
318 * If some channels are close to the given cpu, the one with the lowest
319 * reference count is returned. Otherwise, cpu is ignored and only the
320 * reference count is taken into account.
321 * Must be called under dma_list_mutex.
322 */
323static struct dma_chan *min_chan(enum dma_transaction_type cap, int cpu)
324{
325 struct dma_device *device;
326 struct dma_chan *chan;
327 struct dma_chan *min = NULL;
328 struct dma_chan *localmin = NULL;
329
330 list_for_each_entry(device, &dma_device_list, global_node) {
331 if (!dma_has_cap(cap, device->cap_mask) ||
332 dma_has_cap(DMA_PRIVATE, device->cap_mask))
333 continue;
334 list_for_each_entry(chan, &device->channels, device_node) {
335 if (!chan->client_count)
336 continue;
337 if (!min || chan->table_count < min->table_count)
338 min = chan;
339
340 if (dma_chan_is_local(chan, cpu))
341 if (!localmin ||
342 chan->table_count < localmin->table_count)
343 localmin = chan;
344 }
345 }
346
347 chan = localmin ? localmin : min;
348
349 if (chan)
350 chan->table_count++;
351
352 return chan;
353}
354
355/**
356 * dma_channel_rebalance - redistribute the available channels
357 *
358 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
359 * operation type) in the SMP case, and operation isolation (avoid
360 * multi-tasking channels) in the non-SMP case. Must be called under
361 * dma_list_mutex.
362 */
363static void dma_channel_rebalance(void)
364{
365 struct dma_chan *chan;
366 struct dma_device *device;
367 int cpu;
368 int cap;
369
370 /* undo the last distribution */
371 for_each_dma_cap_mask(cap, dma_cap_mask_all)
372 for_each_possible_cpu(cpu)
373 per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
374
375 list_for_each_entry(device, &dma_device_list, global_node) {
376 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
377 continue;
378 list_for_each_entry(chan, &device->channels, device_node)
379 chan->table_count = 0;
380 }
381
382 /* don't populate the channel_table if no clients are available */
383 if (!dmaengine_ref_count)
384 return;
385
386 /* redistribute available channels */
387 for_each_dma_cap_mask(cap, dma_cap_mask_all)
388 for_each_online_cpu(cpu) {
389 chan = min_chan(cap, cpu);
390 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
391 }
392}
393
394static int dma_device_satisfies_mask(struct dma_device *device,
395 const dma_cap_mask_t *want)
396{
397 dma_cap_mask_t has;
398
399 bitmap_and(has.bits, want->bits, device->cap_mask.bits,
400 DMA_TX_TYPE_END);
401 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
402}
403
404static struct module *dma_chan_to_owner(struct dma_chan *chan)
405{
406 return chan->device->owner;
407}
408
409/**
410 * balance_ref_count - catch up the channel reference count
411 * @chan - channel to balance ->client_count versus dmaengine_ref_count
412 *
413 * balance_ref_count must be called under dma_list_mutex
414 */
415static void balance_ref_count(struct dma_chan *chan)
416{
417 struct module *owner = dma_chan_to_owner(chan);
418
419 while (chan->client_count < dmaengine_ref_count) {
420 __module_get(owner);
421 chan->client_count++;
422 }
423}
424
425static void dma_device_release(struct kref *ref)
426{
427 struct dma_device *device = container_of(ref, struct dma_device, ref);
428
429 list_del_rcu(&device->global_node);
430 dma_channel_rebalance();
431
432 if (device->device_release)
433 device->device_release(device);
434}
435
436static void dma_device_put(struct dma_device *device)
437{
438 lockdep_assert_held(&dma_list_mutex);
439 kref_put(&device->ref, dma_device_release);
440}
441
442/**
443 * dma_chan_get - try to grab a dma channel's parent driver module
444 * @chan - channel to grab
445 *
446 * Must be called under dma_list_mutex
447 */
448static int dma_chan_get(struct dma_chan *chan)
449{
450 struct module *owner = dma_chan_to_owner(chan);
451 int ret;
452
453 /* The channel is already in use, update client count */
454 if (chan->client_count) {
455 __module_get(owner);
456 goto out;
457 }
458
459 if (!try_module_get(owner))
460 return -ENODEV;
461
462 ret = kref_get_unless_zero(&chan->device->ref);
463 if (!ret) {
464 ret = -ENODEV;
465 goto module_put_out;
466 }
467
468 /* allocate upon first client reference */
469 if (chan->device->device_alloc_chan_resources) {
470 ret = chan->device->device_alloc_chan_resources(chan);
471 if (ret < 0)
472 goto err_out;
473 }
474
475 if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
476 balance_ref_count(chan);
477
478out:
479 chan->client_count++;
480 return 0;
481
482err_out:
483 dma_device_put(chan->device);
484module_put_out:
485 module_put(owner);
486 return ret;
487}
488
489/**
490 * dma_chan_put - drop a reference to a dma channel's parent driver module
491 * @chan - channel to release
492 *
493 * Must be called under dma_list_mutex
494 */
495static void dma_chan_put(struct dma_chan *chan)
496{
497 /* This channel is not in use, bail out */
498 if (!chan->client_count)
499 return;
500
501 chan->client_count--;
502
503 /* This channel is not in use anymore, free it */
504 if (!chan->client_count && chan->device->device_free_chan_resources) {
505 /* Make sure all operations have completed */
506 dmaengine_synchronize(chan);
507 chan->device->device_free_chan_resources(chan);
508 }
509
510 /* If the channel is used via a DMA request router, free the mapping */
511 if (chan->router && chan->router->route_free) {
512 chan->router->route_free(chan->router->dev, chan->route_data);
513 chan->router = NULL;
514 chan->route_data = NULL;
515 }
516
517 dma_device_put(chan->device);
518 module_put(dma_chan_to_owner(chan));
519}
520
521enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
522{
523 enum dma_status status;
524 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
525
526 dma_async_issue_pending(chan);
527 do {
528 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
529 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
530 dev_err(chan->device->dev, "%s: timeout!\n", __func__);
531 return DMA_ERROR;
532 }
533 if (status != DMA_IN_PROGRESS)
534 break;
535 cpu_relax();
536 } while (1);
537
538 return status;
539}
540EXPORT_SYMBOL(dma_sync_wait);
541
542/**
543 * dma_find_channel - find a channel to carry out the operation
544 * @tx_type: transaction type
545 */
546struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
547{
548 return this_cpu_read(channel_table[tx_type]->chan);
549}
550EXPORT_SYMBOL(dma_find_channel);
551
552/**
553 * dma_issue_pending_all - flush all pending operations across all channels
554 */
555void dma_issue_pending_all(void)
556{
557 struct dma_device *device;
558 struct dma_chan *chan;
559
560 rcu_read_lock();
561 list_for_each_entry_rcu(device, &dma_device_list, global_node) {
562 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
563 continue;
564 list_for_each_entry(chan, &device->channels, device_node)
565 if (chan->client_count)
566 device->device_issue_pending(chan);
567 }
568 rcu_read_unlock();
569}
570EXPORT_SYMBOL(dma_issue_pending_all);
571
572int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps)
573{
574 struct dma_device *device;
575
576 if (!chan || !caps)
577 return -EINVAL;
578
579 device = chan->device;
580
581 /* check if the channel supports slave transactions */
582 if (!(test_bit(DMA_SLAVE, device->cap_mask.bits) ||
583 test_bit(DMA_CYCLIC, device->cap_mask.bits)))
584 return -ENXIO;
585
586 /*
587 * Check whether it reports it uses the generic slave
588 * capabilities, if not, that means it doesn't support any
589 * kind of slave capabilities reporting.
590 */
591 if (!device->directions)
592 return -ENXIO;
593
594 caps->src_addr_widths = device->src_addr_widths;
595 caps->dst_addr_widths = device->dst_addr_widths;
596 caps->directions = device->directions;
597 caps->max_burst = device->max_burst;
598 caps->residue_granularity = device->residue_granularity;
599 caps->descriptor_reuse = device->descriptor_reuse;
600 caps->cmd_pause = !!device->device_pause;
601 caps->cmd_resume = !!device->device_resume;
602 caps->cmd_terminate = !!device->device_terminate_all;
603
604 return 0;
605}
606EXPORT_SYMBOL_GPL(dma_get_slave_caps);
607
608static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
609 struct dma_device *dev,
610 dma_filter_fn fn, void *fn_param)
611{
612 struct dma_chan *chan;
613
614 if (mask && !dma_device_satisfies_mask(dev, mask)) {
615 dev_dbg(dev->dev, "%s: wrong capabilities\n", __func__);
616 return NULL;
617 }
618 /* devices with multiple channels need special handling as we need to
619 * ensure that all channels are either private or public.
620 */
621 if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
622 list_for_each_entry(chan, &dev->channels, device_node) {
623 /* some channels are already publicly allocated */
624 if (chan->client_count)
625 return NULL;
626 }
627
628 list_for_each_entry(chan, &dev->channels, device_node) {
629 if (chan->client_count) {
630 dev_dbg(dev->dev, "%s: %s busy\n",
631 __func__, dma_chan_name(chan));
632 continue;
633 }
634 if (fn && !fn(chan, fn_param)) {
635 dev_dbg(dev->dev, "%s: %s filter said false\n",
636 __func__, dma_chan_name(chan));
637 continue;
638 }
639 return chan;
640 }
641
642 return NULL;
643}
644
645static struct dma_chan *find_candidate(struct dma_device *device,
646 const dma_cap_mask_t *mask,
647 dma_filter_fn fn, void *fn_param)
648{
649 struct dma_chan *chan = private_candidate(mask, device, fn, fn_param);
650 int err;
651
652 if (chan) {
653 /* Found a suitable channel, try to grab, prep, and return it.
654 * We first set DMA_PRIVATE to disable balance_ref_count as this
655 * channel will not be published in the general-purpose
656 * allocator
657 */
658 dma_cap_set(DMA_PRIVATE, device->cap_mask);
659 device->privatecnt++;
660 err = dma_chan_get(chan);
661
662 if (err) {
663 if (err == -ENODEV) {
664 dev_dbg(device->dev, "%s: %s module removed\n",
665 __func__, dma_chan_name(chan));
666 list_del_rcu(&device->global_node);
667 } else
668 dev_dbg(device->dev,
669 "%s: failed to get %s: (%d)\n",
670 __func__, dma_chan_name(chan), err);
671
672 if (--device->privatecnt == 0)
673 dma_cap_clear(DMA_PRIVATE, device->cap_mask);
674
675 chan = ERR_PTR(err);
676 }
677 }
678
679 return chan ? chan : ERR_PTR(-EPROBE_DEFER);
680}
681
682/**
683 * dma_get_slave_channel - try to get specific channel exclusively
684 * @chan: target channel
685 */
686struct dma_chan *dma_get_slave_channel(struct dma_chan *chan)
687{
688 int err = -EBUSY;
689
690 /* lock against __dma_request_channel */
691 mutex_lock(&dma_list_mutex);
692
693 if (chan->client_count == 0) {
694 struct dma_device *device = chan->device;
695
696 dma_cap_set(DMA_PRIVATE, device->cap_mask);
697 device->privatecnt++;
698 err = dma_chan_get(chan);
699 if (err) {
700 dev_dbg(chan->device->dev,
701 "%s: failed to get %s: (%d)\n",
702 __func__, dma_chan_name(chan), err);
703 chan = NULL;
704 if (--device->privatecnt == 0)
705 dma_cap_clear(DMA_PRIVATE, device->cap_mask);
706 }
707 } else
708 chan = NULL;
709
710 mutex_unlock(&dma_list_mutex);
711
712
713 return chan;
714}
715EXPORT_SYMBOL_GPL(dma_get_slave_channel);
716
717struct dma_chan *dma_get_any_slave_channel(struct dma_device *device)
718{
719 dma_cap_mask_t mask;
720 struct dma_chan *chan;
721
722 dma_cap_zero(mask);
723 dma_cap_set(DMA_SLAVE, mask);
724
725 /* lock against __dma_request_channel */
726 mutex_lock(&dma_list_mutex);
727
728 chan = find_candidate(device, &mask, NULL, NULL);
729
730 mutex_unlock(&dma_list_mutex);
731
732 return IS_ERR(chan) ? NULL : chan;
733}
734EXPORT_SYMBOL_GPL(dma_get_any_slave_channel);
735
736/**
737 * __dma_request_channel - try to allocate an exclusive channel
738 * @mask: capabilities that the channel must satisfy
739 * @fn: optional callback to disposition available channels
740 * @fn_param: opaque parameter to pass to dma_filter_fn
741 * @np: device node to look for DMA channels
742 *
743 * Returns pointer to appropriate DMA channel on success or NULL.
744 */
745struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
746 dma_filter_fn fn, void *fn_param,
747 struct device_node *np)
748{
749 struct dma_device *device, *_d;
750 struct dma_chan *chan = NULL;
751
752 /* Find a channel */
753 mutex_lock(&dma_list_mutex);
754 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
755 /* Finds a DMA controller with matching device node */
756 if (np && device->dev->of_node && np != device->dev->of_node)
757 continue;
758
759 chan = find_candidate(device, mask, fn, fn_param);
760 if (!IS_ERR(chan))
761 break;
762
763 chan = NULL;
764 }
765 mutex_unlock(&dma_list_mutex);
766
767 pr_debug("%s: %s (%s)\n",
768 __func__,
769 chan ? "success" : "fail",
770 chan ? dma_chan_name(chan) : NULL);
771
772 return chan;
773}
774EXPORT_SYMBOL_GPL(__dma_request_channel);
775
776static const struct dma_slave_map *dma_filter_match(struct dma_device *device,
777 const char *name,
778 struct device *dev)
779{
780 int i;
781
782 if (!device->filter.mapcnt)
783 return NULL;
784
785 for (i = 0; i < device->filter.mapcnt; i++) {
786 const struct dma_slave_map *map = &device->filter.map[i];
787
788 if (!strcmp(map->devname, dev_name(dev)) &&
789 !strcmp(map->slave, name))
790 return map;
791 }
792
793 return NULL;
794}
795
796/**
797 * dma_request_chan - try to allocate an exclusive slave channel
798 * @dev: pointer to client device structure
799 * @name: slave channel name
800 *
801 * Returns pointer to appropriate DMA channel on success or an error pointer.
802 */
803struct dma_chan *dma_request_chan(struct device *dev, const char *name)
804{
805 struct dma_device *d, *_d;
806 struct dma_chan *chan = NULL;
807
808 /* If device-tree is present get slave info from here */
809 if (dev->of_node)
810 chan = of_dma_request_slave_channel(dev->of_node, name);
811
812 /* If device was enumerated by ACPI get slave info from here */
813 if (has_acpi_companion(dev) && !chan)
814 chan = acpi_dma_request_slave_chan_by_name(dev, name);
815
816 if (PTR_ERR(chan) == -EPROBE_DEFER)
817 return chan;
818
819 if (!IS_ERR_OR_NULL(chan))
820 goto found;
821
822 /* Try to find the channel via the DMA filter map(s) */
823 mutex_lock(&dma_list_mutex);
824 list_for_each_entry_safe(d, _d, &dma_device_list, global_node) {
825 dma_cap_mask_t mask;
826 const struct dma_slave_map *map = dma_filter_match(d, name, dev);
827
828 if (!map)
829 continue;
830
831 dma_cap_zero(mask);
832 dma_cap_set(DMA_SLAVE, mask);
833
834 chan = find_candidate(d, &mask, d->filter.fn, map->param);
835 if (!IS_ERR(chan))
836 break;
837 }
838 mutex_unlock(&dma_list_mutex);
839
840 if (IS_ERR_OR_NULL(chan))
841 return chan ? chan : ERR_PTR(-EPROBE_DEFER);
842
843found:
844#ifdef CONFIG_DEBUG_FS
845 chan->dbg_client_name = kasprintf(GFP_KERNEL, "%s:%s", dev_name(dev),
846 name);
847#endif
848
849 chan->name = kasprintf(GFP_KERNEL, "dma:%s", name);
850 if (!chan->name)
851 return chan;
852 chan->slave = dev;
853
854 if (sysfs_create_link(&chan->dev->device.kobj, &dev->kobj,
855 DMA_SLAVE_NAME))
856 dev_warn(dev, "Cannot create DMA %s symlink\n", DMA_SLAVE_NAME);
857 if (sysfs_create_link(&dev->kobj, &chan->dev->device.kobj, chan->name))
858 dev_warn(dev, "Cannot create DMA %s symlink\n", chan->name);
859
860 return chan;
861}
862EXPORT_SYMBOL_GPL(dma_request_chan);
863
864/**
865 * dma_request_slave_channel - try to allocate an exclusive slave channel
866 * @dev: pointer to client device structure
867 * @name: slave channel name
868 *
869 * Returns pointer to appropriate DMA channel on success or NULL.
870 */
871struct dma_chan *dma_request_slave_channel(struct device *dev,
872 const char *name)
873{
874 struct dma_chan *ch = dma_request_chan(dev, name);
875 if (IS_ERR(ch))
876 return NULL;
877
878 return ch;
879}
880EXPORT_SYMBOL_GPL(dma_request_slave_channel);
881
882/**
883 * dma_request_chan_by_mask - allocate a channel satisfying certain capabilities
884 * @mask: capabilities that the channel must satisfy
885 *
886 * Returns pointer to appropriate DMA channel on success or an error pointer.
887 */
888struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask)
889{
890 struct dma_chan *chan;
891
892 if (!mask)
893 return ERR_PTR(-ENODEV);
894
895 chan = __dma_request_channel(mask, NULL, NULL, NULL);
896 if (!chan) {
897 mutex_lock(&dma_list_mutex);
898 if (list_empty(&dma_device_list))
899 chan = ERR_PTR(-EPROBE_DEFER);
900 else
901 chan = ERR_PTR(-ENODEV);
902 mutex_unlock(&dma_list_mutex);
903 }
904
905 return chan;
906}
907EXPORT_SYMBOL_GPL(dma_request_chan_by_mask);
908
909void dma_release_channel(struct dma_chan *chan)
910{
911 mutex_lock(&dma_list_mutex);
912 WARN_ONCE(chan->client_count != 1,
913 "chan reference count %d != 1\n", chan->client_count);
914 dma_chan_put(chan);
915 /* drop PRIVATE cap enabled by __dma_request_channel() */
916 if (--chan->device->privatecnt == 0)
917 dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
918
919 if (chan->slave) {
920 sysfs_remove_link(&chan->dev->device.kobj, DMA_SLAVE_NAME);
921 sysfs_remove_link(&chan->slave->kobj, chan->name);
922 kfree(chan->name);
923 chan->name = NULL;
924 chan->slave = NULL;
925 }
926
927#ifdef CONFIG_DEBUG_FS
928 kfree(chan->dbg_client_name);
929 chan->dbg_client_name = NULL;
930#endif
931 mutex_unlock(&dma_list_mutex);
932}
933EXPORT_SYMBOL_GPL(dma_release_channel);
934
935/**
936 * dmaengine_get - register interest in dma_channels
937 */
938void dmaengine_get(void)
939{
940 struct dma_device *device, *_d;
941 struct dma_chan *chan;
942 int err;
943
944 mutex_lock(&dma_list_mutex);
945 dmaengine_ref_count++;
946
947 /* try to grab channels */
948 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
949 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
950 continue;
951 list_for_each_entry(chan, &device->channels, device_node) {
952 err = dma_chan_get(chan);
953 if (err == -ENODEV) {
954 /* module removed before we could use it */
955 list_del_rcu(&device->global_node);
956 break;
957 } else if (err)
958 dev_dbg(chan->device->dev,
959 "%s: failed to get %s: (%d)\n",
960 __func__, dma_chan_name(chan), err);
961 }
962 }
963
964 /* if this is the first reference and there were channels
965 * waiting we need to rebalance to get those channels
966 * incorporated into the channel table
967 */
968 if (dmaengine_ref_count == 1)
969 dma_channel_rebalance();
970 mutex_unlock(&dma_list_mutex);
971}
972EXPORT_SYMBOL(dmaengine_get);
973
974/**
975 * dmaengine_put - let dma drivers be removed when ref_count == 0
976 */
977void dmaengine_put(void)
978{
979 struct dma_device *device, *_d;
980 struct dma_chan *chan;
981
982 mutex_lock(&dma_list_mutex);
983 dmaengine_ref_count--;
984 BUG_ON(dmaengine_ref_count < 0);
985 /* drop channel references */
986 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
987 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
988 continue;
989 list_for_each_entry(chan, &device->channels, device_node)
990 dma_chan_put(chan);
991 }
992 mutex_unlock(&dma_list_mutex);
993}
994EXPORT_SYMBOL(dmaengine_put);
995
996static bool device_has_all_tx_types(struct dma_device *device)
997{
998 /* A device that satisfies this test has channels that will never cause
999 * an async_tx channel switch event as all possible operation types can
1000 * be handled.
1001 */
1002 #ifdef CONFIG_ASYNC_TX_DMA
1003 if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
1004 return false;
1005 #endif
1006
1007 #if IS_ENABLED(CONFIG_ASYNC_MEMCPY)
1008 if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
1009 return false;
1010 #endif
1011
1012 #if IS_ENABLED(CONFIG_ASYNC_XOR)
1013 if (!dma_has_cap(DMA_XOR, device->cap_mask))
1014 return false;
1015
1016 #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
1017 if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
1018 return false;
1019 #endif
1020 #endif
1021
1022 #if IS_ENABLED(CONFIG_ASYNC_PQ)
1023 if (!dma_has_cap(DMA_PQ, device->cap_mask))
1024 return false;
1025
1026 #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
1027 if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
1028 return false;
1029 #endif
1030 #endif
1031
1032 return true;
1033}
1034
1035static int get_dma_id(struct dma_device *device)
1036{
1037 int rc = ida_alloc(&dma_ida, GFP_KERNEL);
1038
1039 if (rc < 0)
1040 return rc;
1041 device->dev_id = rc;
1042 return 0;
1043}
1044
1045static int __dma_async_device_channel_register(struct dma_device *device,
1046 struct dma_chan *chan,
1047 int chan_id)
1048{
1049 int rc = 0;
1050 int chancnt = device->chancnt;
1051 atomic_t *idr_ref;
1052 struct dma_chan *tchan;
1053
1054 tchan = list_first_entry_or_null(&device->channels,
1055 struct dma_chan, device_node);
1056 if (!tchan)
1057 return -ENODEV;
1058
1059 if (tchan->dev) {
1060 idr_ref = tchan->dev->idr_ref;
1061 } else {
1062 idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
1063 if (!idr_ref)
1064 return -ENOMEM;
1065 atomic_set(idr_ref, 0);
1066 }
1067
1068 chan->local = alloc_percpu(typeof(*chan->local));
1069 if (!chan->local)
1070 goto err_out;
1071 chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
1072 if (!chan->dev) {
1073 free_percpu(chan->local);
1074 chan->local = NULL;
1075 goto err_out;
1076 }
1077
1078 /*
1079 * When the chan_id is a negative value, we are dynamically adding
1080 * the channel. Otherwise we are static enumerating.
1081 */
1082 chan->chan_id = chan_id < 0 ? chancnt : chan_id;
1083 chan->dev->device.class = &dma_devclass;
1084 chan->dev->device.parent = device->dev;
1085 chan->dev->chan = chan;
1086 chan->dev->idr_ref = idr_ref;
1087 chan->dev->dev_id = device->dev_id;
1088 atomic_inc(idr_ref);
1089 dev_set_name(&chan->dev->device, "dma%dchan%d",
1090 device->dev_id, chan->chan_id);
1091
1092 rc = device_register(&chan->dev->device);
1093 if (rc)
1094 goto err_out;
1095 chan->client_count = 0;
1096 device->chancnt = chan->chan_id + 1;
1097
1098 return 0;
1099
1100 err_out:
1101 free_percpu(chan->local);
1102 kfree(chan->dev);
1103 if (atomic_dec_return(idr_ref) == 0)
1104 kfree(idr_ref);
1105 return rc;
1106}
1107
1108int dma_async_device_channel_register(struct dma_device *device,
1109 struct dma_chan *chan)
1110{
1111 int rc;
1112
1113 rc = __dma_async_device_channel_register(device, chan, -1);
1114 if (rc < 0)
1115 return rc;
1116
1117 dma_channel_rebalance();
1118 return 0;
1119}
1120EXPORT_SYMBOL_GPL(dma_async_device_channel_register);
1121
1122static void __dma_async_device_channel_unregister(struct dma_device *device,
1123 struct dma_chan *chan)
1124{
1125 WARN_ONCE(!device->device_release && chan->client_count,
1126 "%s called while %d clients hold a reference\n",
1127 __func__, chan->client_count);
1128 mutex_lock(&dma_list_mutex);
1129 list_del(&chan->device_node);
1130 device->chancnt--;
1131 chan->dev->chan = NULL;
1132 mutex_unlock(&dma_list_mutex);
1133 device_unregister(&chan->dev->device);
1134 free_percpu(chan->local);
1135}
1136
1137void dma_async_device_channel_unregister(struct dma_device *device,
1138 struct dma_chan *chan)
1139{
1140 __dma_async_device_channel_unregister(device, chan);
1141 dma_channel_rebalance();
1142}
1143EXPORT_SYMBOL_GPL(dma_async_device_channel_unregister);
1144
1145/**
1146 * dma_async_device_register - registers DMA devices found
1147 * @device: &dma_device
1148 *
1149 * After calling this routine the structure should not be freed except in the
1150 * device_release() callback which will be called after
1151 * dma_async_device_unregister() is called and no further references are taken.
1152 */
1153int dma_async_device_register(struct dma_device *device)
1154{
1155 int rc, i = 0;
1156 struct dma_chan* chan;
1157
1158 if (!device)
1159 return -ENODEV;
1160
1161 /* validate device routines */
1162 if (!device->dev) {
1163 pr_err("DMAdevice must have dev\n");
1164 return -EIO;
1165 }
1166
1167 device->owner = device->dev->driver->owner;
1168
1169 if (dma_has_cap(DMA_MEMCPY, device->cap_mask) && !device->device_prep_dma_memcpy) {
1170 dev_err(device->dev,
1171 "Device claims capability %s, but op is not defined\n",
1172 "DMA_MEMCPY");
1173 return -EIO;
1174 }
1175
1176 if (dma_has_cap(DMA_XOR, device->cap_mask) && !device->device_prep_dma_xor) {
1177 dev_err(device->dev,
1178 "Device claims capability %s, but op is not defined\n",
1179 "DMA_XOR");
1180 return -EIO;
1181 }
1182
1183 if (dma_has_cap(DMA_XOR_VAL, device->cap_mask) && !device->device_prep_dma_xor_val) {
1184 dev_err(device->dev,
1185 "Device claims capability %s, but op is not defined\n",
1186 "DMA_XOR_VAL");
1187 return -EIO;
1188 }
1189
1190 if (dma_has_cap(DMA_PQ, device->cap_mask) && !device->device_prep_dma_pq) {
1191 dev_err(device->dev,
1192 "Device claims capability %s, but op is not defined\n",
1193 "DMA_PQ");
1194 return -EIO;
1195 }
1196
1197 if (dma_has_cap(DMA_PQ_VAL, device->cap_mask) && !device->device_prep_dma_pq_val) {
1198 dev_err(device->dev,
1199 "Device claims capability %s, but op is not defined\n",
1200 "DMA_PQ_VAL");
1201 return -EIO;
1202 }
1203
1204 if (dma_has_cap(DMA_MEMSET, device->cap_mask) && !device->device_prep_dma_memset) {
1205 dev_err(device->dev,
1206 "Device claims capability %s, but op is not defined\n",
1207 "DMA_MEMSET");
1208 return -EIO;
1209 }
1210
1211 if (dma_has_cap(DMA_INTERRUPT, device->cap_mask) && !device->device_prep_dma_interrupt) {
1212 dev_err(device->dev,
1213 "Device claims capability %s, but op is not defined\n",
1214 "DMA_INTERRUPT");
1215 return -EIO;
1216 }
1217
1218 if (dma_has_cap(DMA_CYCLIC, device->cap_mask) && !device->device_prep_dma_cyclic) {
1219 dev_err(device->dev,
1220 "Device claims capability %s, but op is not defined\n",
1221 "DMA_CYCLIC");
1222 return -EIO;
1223 }
1224
1225 if (dma_has_cap(DMA_INTERLEAVE, device->cap_mask) && !device->device_prep_interleaved_dma) {
1226 dev_err(device->dev,
1227 "Device claims capability %s, but op is not defined\n",
1228 "DMA_INTERLEAVE");
1229 return -EIO;
1230 }
1231
1232
1233 if (!device->device_tx_status) {
1234 dev_err(device->dev, "Device tx_status is not defined\n");
1235 return -EIO;
1236 }
1237
1238
1239 if (!device->device_issue_pending) {
1240 dev_err(device->dev, "Device issue_pending is not defined\n");
1241 return -EIO;
1242 }
1243
1244 if (!device->device_release)
1245 dev_dbg(device->dev,
1246 "WARN: Device release is not defined so it is not safe to unbind this driver while in use\n");
1247
1248 kref_init(&device->ref);
1249
1250 /* note: this only matters in the
1251 * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
1252 */
1253 if (device_has_all_tx_types(device))
1254 dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
1255
1256 rc = get_dma_id(device);
1257 if (rc != 0)
1258 return rc;
1259
1260 /* represent channels in sysfs. Probably want devs too */
1261 list_for_each_entry(chan, &device->channels, device_node) {
1262 rc = __dma_async_device_channel_register(device, chan, i++);
1263 if (rc < 0)
1264 goto err_out;
1265 }
1266
1267 mutex_lock(&dma_list_mutex);
1268 /* take references on public channels */
1269 if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
1270 list_for_each_entry(chan, &device->channels, device_node) {
1271 /* if clients are already waiting for channels we need
1272 * to take references on their behalf
1273 */
1274 if (dma_chan_get(chan) == -ENODEV) {
1275 /* note we can only get here for the first
1276 * channel as the remaining channels are
1277 * guaranteed to get a reference
1278 */
1279 rc = -ENODEV;
1280 mutex_unlock(&dma_list_mutex);
1281 goto err_out;
1282 }
1283 }
1284 list_add_tail_rcu(&device->global_node, &dma_device_list);
1285 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
1286 device->privatecnt++; /* Always private */
1287 dma_channel_rebalance();
1288 mutex_unlock(&dma_list_mutex);
1289
1290 dmaengine_debug_register(device);
1291
1292 return 0;
1293
1294err_out:
1295 /* if we never registered a channel just release the idr */
1296 if (!device->chancnt) {
1297 ida_free(&dma_ida, device->dev_id);
1298 return rc;
1299 }
1300
1301 list_for_each_entry(chan, &device->channels, device_node) {
1302 if (chan->local == NULL)
1303 continue;
1304 mutex_lock(&dma_list_mutex);
1305 chan->dev->chan = NULL;
1306 mutex_unlock(&dma_list_mutex);
1307 device_unregister(&chan->dev->device);
1308 free_percpu(chan->local);
1309 }
1310 return rc;
1311}
1312EXPORT_SYMBOL(dma_async_device_register);
1313
1314/**
1315 * dma_async_device_unregister - unregister a DMA device
1316 * @device: &dma_device
1317 *
1318 * This routine is called by dma driver exit routines, dmaengine holds module
1319 * references to prevent it being called while channels are in use.
1320 */
1321void dma_async_device_unregister(struct dma_device *device)
1322{
1323 struct dma_chan *chan, *n;
1324
1325 dmaengine_debug_unregister(device);
1326
1327 list_for_each_entry_safe(chan, n, &device->channels, device_node)
1328 __dma_async_device_channel_unregister(device, chan);
1329
1330 mutex_lock(&dma_list_mutex);
1331 /*
1332 * setting DMA_PRIVATE ensures the device being torn down will not
1333 * be used in the channel_table
1334 */
1335 dma_cap_set(DMA_PRIVATE, device->cap_mask);
1336 dma_channel_rebalance();
1337 dma_device_put(device);
1338 mutex_unlock(&dma_list_mutex);
1339}
1340EXPORT_SYMBOL(dma_async_device_unregister);
1341
1342static void dmam_device_release(struct device *dev, void *res)
1343{
1344 struct dma_device *device;
1345
1346 device = *(struct dma_device **)res;
1347 dma_async_device_unregister(device);
1348}
1349
1350/**
1351 * dmaenginem_async_device_register - registers DMA devices found
1352 * @device: &dma_device
1353 *
1354 * The operation is managed and will be undone on driver detach.
1355 */
1356int dmaenginem_async_device_register(struct dma_device *device)
1357{
1358 void *p;
1359 int ret;
1360
1361 p = devres_alloc(dmam_device_release, sizeof(void *), GFP_KERNEL);
1362 if (!p)
1363 return -ENOMEM;
1364
1365 ret = dma_async_device_register(device);
1366 if (!ret) {
1367 *(struct dma_device **)p = device;
1368 devres_add(device->dev, p);
1369 } else {
1370 devres_free(p);
1371 }
1372
1373 return ret;
1374}
1375EXPORT_SYMBOL(dmaenginem_async_device_register);
1376
1377struct dmaengine_unmap_pool {
1378 struct kmem_cache *cache;
1379 const char *name;
1380 mempool_t *pool;
1381 size_t size;
1382};
1383
1384#define __UNMAP_POOL(x) { .size = x, .name = "dmaengine-unmap-" __stringify(x) }
1385static struct dmaengine_unmap_pool unmap_pool[] = {
1386 __UNMAP_POOL(2),
1387 #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
1388 __UNMAP_POOL(16),
1389 __UNMAP_POOL(128),
1390 __UNMAP_POOL(256),
1391 #endif
1392};
1393
1394static struct dmaengine_unmap_pool *__get_unmap_pool(int nr)
1395{
1396 int order = get_count_order(nr);
1397
1398 switch (order) {
1399 case 0 ... 1:
1400 return &unmap_pool[0];
1401#if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
1402 case 2 ... 4:
1403 return &unmap_pool[1];
1404 case 5 ... 7:
1405 return &unmap_pool[2];
1406 case 8:
1407 return &unmap_pool[3];
1408#endif
1409 default:
1410 BUG();
1411 return NULL;
1412 }
1413}
1414
1415static void dmaengine_unmap(struct kref *kref)
1416{
1417 struct dmaengine_unmap_data *unmap = container_of(kref, typeof(*unmap), kref);
1418 struct device *dev = unmap->dev;
1419 int cnt, i;
1420
1421 cnt = unmap->to_cnt;
1422 for (i = 0; i < cnt; i++)
1423 dma_unmap_page(dev, unmap->addr[i], unmap->len,
1424 DMA_TO_DEVICE);
1425 cnt += unmap->from_cnt;
1426 for (; i < cnt; i++)
1427 dma_unmap_page(dev, unmap->addr[i], unmap->len,
1428 DMA_FROM_DEVICE);
1429 cnt += unmap->bidi_cnt;
1430 for (; i < cnt; i++) {
1431 if (unmap->addr[i] == 0)
1432 continue;
1433 dma_unmap_page(dev, unmap->addr[i], unmap->len,
1434 DMA_BIDIRECTIONAL);
1435 }
1436 cnt = unmap->map_cnt;
1437 mempool_free(unmap, __get_unmap_pool(cnt)->pool);
1438}
1439
1440void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
1441{
1442 if (unmap)
1443 kref_put(&unmap->kref, dmaengine_unmap);
1444}
1445EXPORT_SYMBOL_GPL(dmaengine_unmap_put);
1446
1447static void dmaengine_destroy_unmap_pool(void)
1448{
1449 int i;
1450
1451 for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
1452 struct dmaengine_unmap_pool *p = &unmap_pool[i];
1453
1454 mempool_destroy(p->pool);
1455 p->pool = NULL;
1456 kmem_cache_destroy(p->cache);
1457 p->cache = NULL;
1458 }
1459}
1460
1461static int __init dmaengine_init_unmap_pool(void)
1462{
1463 int i;
1464
1465 for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
1466 struct dmaengine_unmap_pool *p = &unmap_pool[i];
1467 size_t size;
1468
1469 size = sizeof(struct dmaengine_unmap_data) +
1470 sizeof(dma_addr_t) * p->size;
1471
1472 p->cache = kmem_cache_create(p->name, size, 0,
1473 SLAB_HWCACHE_ALIGN, NULL);
1474 if (!p->cache)
1475 break;
1476 p->pool = mempool_create_slab_pool(1, p->cache);
1477 if (!p->pool)
1478 break;
1479 }
1480
1481 if (i == ARRAY_SIZE(unmap_pool))
1482 return 0;
1483
1484 dmaengine_destroy_unmap_pool();
1485 return -ENOMEM;
1486}
1487
1488struct dmaengine_unmap_data *
1489dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
1490{
1491 struct dmaengine_unmap_data *unmap;
1492
1493 unmap = mempool_alloc(__get_unmap_pool(nr)->pool, flags);
1494 if (!unmap)
1495 return NULL;
1496
1497 memset(unmap, 0, sizeof(*unmap));
1498 kref_init(&unmap->kref);
1499 unmap->dev = dev;
1500 unmap->map_cnt = nr;
1501
1502 return unmap;
1503}
1504EXPORT_SYMBOL(dmaengine_get_unmap_data);
1505
1506void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1507 struct dma_chan *chan)
1508{
1509 tx->chan = chan;
1510 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
1511 spin_lock_init(&tx->lock);
1512 #endif
1513}
1514EXPORT_SYMBOL(dma_async_tx_descriptor_init);
1515
1516static inline int desc_check_and_set_metadata_mode(
1517 struct dma_async_tx_descriptor *desc, enum dma_desc_metadata_mode mode)
1518{
1519 /* Make sure that the metadata mode is not mixed */
1520 if (!desc->desc_metadata_mode) {
1521 if (dmaengine_is_metadata_mode_supported(desc->chan, mode))
1522 desc->desc_metadata_mode = mode;
1523 else
1524 return -ENOTSUPP;
1525 } else if (desc->desc_metadata_mode != mode) {
1526 return -EINVAL;
1527 }
1528
1529 return 0;
1530}
1531
1532int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
1533 void *data, size_t len)
1534{
1535 int ret;
1536
1537 if (!desc)
1538 return -EINVAL;
1539
1540 ret = desc_check_and_set_metadata_mode(desc, DESC_METADATA_CLIENT);
1541 if (ret)
1542 return ret;
1543
1544 if (!desc->metadata_ops || !desc->metadata_ops->attach)
1545 return -ENOTSUPP;
1546
1547 return desc->metadata_ops->attach(desc, data, len);
1548}
1549EXPORT_SYMBOL_GPL(dmaengine_desc_attach_metadata);
1550
1551void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
1552 size_t *payload_len, size_t *max_len)
1553{
1554 int ret;
1555
1556 if (!desc)
1557 return ERR_PTR(-EINVAL);
1558
1559 ret = desc_check_and_set_metadata_mode(desc, DESC_METADATA_ENGINE);
1560 if (ret)
1561 return ERR_PTR(ret);
1562
1563 if (!desc->metadata_ops || !desc->metadata_ops->get_ptr)
1564 return ERR_PTR(-ENOTSUPP);
1565
1566 return desc->metadata_ops->get_ptr(desc, payload_len, max_len);
1567}
1568EXPORT_SYMBOL_GPL(dmaengine_desc_get_metadata_ptr);
1569
1570int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
1571 size_t payload_len)
1572{
1573 int ret;
1574
1575 if (!desc)
1576 return -EINVAL;
1577
1578 ret = desc_check_and_set_metadata_mode(desc, DESC_METADATA_ENGINE);
1579 if (ret)
1580 return ret;
1581
1582 if (!desc->metadata_ops || !desc->metadata_ops->set_len)
1583 return -ENOTSUPP;
1584
1585 return desc->metadata_ops->set_len(desc, payload_len);
1586}
1587EXPORT_SYMBOL_GPL(dmaengine_desc_set_metadata_len);
1588
1589/* dma_wait_for_async_tx - spin wait for a transaction to complete
1590 * @tx: in-flight transaction to wait on
1591 */
1592enum dma_status
1593dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1594{
1595 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
1596
1597 if (!tx)
1598 return DMA_COMPLETE;
1599
1600 while (tx->cookie == -EBUSY) {
1601 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
1602 dev_err(tx->chan->device->dev,
1603 "%s timeout waiting for descriptor submission\n",
1604 __func__);
1605 return DMA_ERROR;
1606 }
1607 cpu_relax();
1608 }
1609 return dma_sync_wait(tx->chan, tx->cookie);
1610}
1611EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
1612
1613/* dma_run_dependencies - helper routine for dma drivers to process
1614 * (start) dependent operations on their target channel
1615 * @tx: transaction with dependencies
1616 */
1617void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
1618{
1619 struct dma_async_tx_descriptor *dep = txd_next(tx);
1620 struct dma_async_tx_descriptor *dep_next;
1621 struct dma_chan *chan;
1622
1623 if (!dep)
1624 return;
1625
1626 /* we'll submit tx->next now, so clear the link */
1627 txd_clear_next(tx);
1628 chan = dep->chan;
1629
1630 /* keep submitting up until a channel switch is detected
1631 * in that case we will be called again as a result of
1632 * processing the interrupt from async_tx_channel_switch
1633 */
1634 for (; dep; dep = dep_next) {
1635 txd_lock(dep);
1636 txd_clear_parent(dep);
1637 dep_next = txd_next(dep);
1638 if (dep_next && dep_next->chan == chan)
1639 txd_clear_next(dep); /* ->next will be submitted */
1640 else
1641 dep_next = NULL; /* submit current dep and terminate */
1642 txd_unlock(dep);
1643
1644 dep->tx_submit(dep);
1645 }
1646
1647 chan->device->device_issue_pending(chan);
1648}
1649EXPORT_SYMBOL_GPL(dma_run_dependencies);
1650
1651static int __init dma_bus_init(void)
1652{
1653 int err = dmaengine_init_unmap_pool();
1654
1655 if (err)
1656 return err;
1657
1658 err = class_register(&dma_devclass);
1659 if (!err)
1660 dmaengine_debugfs_init();
1661
1662 return err;
1663}
1664arch_initcall(dma_bus_init);