blob: 5a442752e07d351c3f37c8f6d8839f4e16890f3f [file] [log] [blame]
Thomas Gleixner9ab65af2019-05-19 15:51:37 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Chris Leechc13c8262006-05-23 17:18:44 -07002/*
3 * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
Chris Leechc13c8262006-05-23 17:18:44 -07004 */
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 *
Dan Williamsaa1e6f12009-01-06 11:38:17 -070018 * The subsystem keeps a global list of dma_device structs it is protected by a
19 * mutex, dma_list_mutex.
Chris Leechc13c8262006-05-23 17:18:44 -070020 *
Dan Williamsf27c5802009-01-06 11:38:18 -070021 * 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 *
Chris Leechc13c8262006-05-23 17:18:44 -070026 * 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 *
Mauro Carvalho Chehab44348e8a2018-06-14 12:34:32 -030029 * See Documentation/driver-api/dmaengine for more details
Chris Leechc13c8262006-05-23 17:18:44 -070030 */
31
Joe Perches63433252012-07-18 09:51:28 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Peter Ujfalusia8135d02015-12-14 22:47:40 +020034#include <linux/platform_device.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000035#include <linux/dma-mapping.h>
Chris Leechc13c8262006-05-23 17:18:44 -070036#include <linux/init.h>
37#include <linux/module.h>
Dan Williams7405f742007-01-02 11:10:43 -070038#include <linux/mm.h>
Chris Leechc13c8262006-05-23 17:18:44 -070039#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>
Dan Williams7405f742007-01-02 11:10:43 -070046#include <linux/jiffies.h>
Dan Williams2ba05622009-01-06 11:38:14 -070047#include <linux/rculist.h>
Dan Williams864498a2009-01-06 11:38:21 -070048#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Andy Shevchenko4e82f5d2013-04-09 14:05:44 +030050#include <linux/acpi.h>
51#include <linux/acpi_dma.h>
Jon Hunter9a6cecc2012-09-14 17:41:57 -050052#include <linux/of_dma.h>
Dan Williams45c463a2013-10-18 19:35:24 +020053#include <linux/mempool.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080054#include <linux/numa.h>
Chris Leechc13c8262006-05-23 17:18:44 -070055
56static DEFINE_MUTEX(dma_list_mutex);
Matthew Wilcoxadc064c2016-12-15 08:57:51 -080057static DEFINE_IDA(dma_ida);
Chris Leechc13c8262006-05-23 17:18:44 -070058static LIST_HEAD(dma_device_list);
Dan Williams6f49a572009-01-06 11:38:14 -070059static long dmaengine_ref_count;
Chris Leechc13c8262006-05-23 17:18:44 -070060
Peter Ujfalusie937cc12020-03-06 16:28:37 +020061/* --- debugfs implementation --- */
62#ifdef CONFIG_DEBUG_FS
63#include <linux/debugfs.h>
64
Peter Ujfalusi26cf1322020-03-06 16:28:39 +020065static 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
Peter Ujfalusie937cc12020-03-06 16:28:37 +020081static 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{
Peter Ujfalusi26cf1322020-03-06 16:28:39 +0200126 rootdir = debugfs_create_dir("dmaengine", NULL);
Peter Ujfalusie937cc12020-03-06 16:28:37 +0200127
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) { }
Peter Ujfalusi26cf1322020-03-06 16:28:39 +0200134static 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) { }
Peter Ujfalusie937cc12020-03-06 16:28:37 +0200140#endif /* DEBUG_FS */
141
Chris Leechc13c8262006-05-23 17:18:44 -0700142/* --- sysfs implementation --- */
143
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100144#define DMA_SLAVE_NAME "slave"
145
Dan Williams41d5e592009-01-06 11:38:21 -0700146/**
Geert Uytterhoevenfe333382019-06-07 13:30:39 +0200147 * dev_to_dma_chan - convert a device pointer to its sysfs container object
Dan Williams41d5e592009-01-06 11:38:21 -0700148 * @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
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700160static ssize_t memcpy_count_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700162{
Dan Williams41d5e592009-01-06 11:38:21 -0700163 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700164 unsigned long count = 0;
165 int i;
Dan Williams41d5e592009-01-06 11:38:21 -0700166 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700167
Dan Williams41d5e592009-01-06 11:38:21 -0700168 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);
Chris Leechc13c8262006-05-23 17:18:44 -0700177
Dan Williams41d5e592009-01-06 11:38:21 -0700178 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700179}
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700180static DEVICE_ATTR_RO(memcpy_count);
Chris Leechc13c8262006-05-23 17:18:44 -0700181
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700182static ssize_t bytes_transferred_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700184{
Dan Williams41d5e592009-01-06 11:38:21 -0700185 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700186 unsigned long count = 0;
187 int i;
Dan Williams41d5e592009-01-06 11:38:21 -0700188 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700189
Dan Williams41d5e592009-01-06 11:38:21 -0700190 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);
Chris Leechc13c8262006-05-23 17:18:44 -0700199
Dan Williams41d5e592009-01-06 11:38:21 -0700200 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700201}
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700202static DEVICE_ATTR_RO(bytes_transferred);
Chris Leechc13c8262006-05-23 17:18:44 -0700203
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700204static ssize_t in_use_show(struct device *dev, struct device_attribute *attr,
205 char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700206{
Dan Williams41d5e592009-01-06 11:38:21 -0700207 struct dma_chan *chan;
208 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700209
Dan Williams41d5e592009-01-06 11:38:21 -0700210 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;
Chris Leechc13c8262006-05-23 17:18:44 -0700219}
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700220static DEVICE_ATTR_RO(in_use);
Chris Leechc13c8262006-05-23 17:18:44 -0700221
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700222static 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,
Chris Leechc13c8262006-05-23 17:18:44 -0700227};
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700228ATTRIBUTE_GROUPS(dma_dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700229
Dan Williams41d5e592009-01-06 11:38:21 -0700230static 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);
Dan Williams864498a2009-01-06 11:38:21 -0700235 if (atomic_dec_and_test(chan_dev->idr_ref)) {
Matthew Wilcox485258b2018-06-18 15:41:48 -0400236 ida_free(&dma_ida, chan_dev->dev_id);
Dan Williams864498a2009-01-06 11:38:21 -0700237 kfree(chan_dev->idr_ref);
238 }
Dan Williams41d5e592009-01-06 11:38:21 -0700239 kfree(chan_dev);
240}
241
Chris Leechc13c8262006-05-23 17:18:44 -0700242static struct class dma_devclass = {
Tony Jones891f78e2007-09-25 02:03:03 +0200243 .name = "dma",
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700244 .dev_groups = dma_dev_groups,
Dan Williams41d5e592009-01-06 11:38:21 -0700245 .dev_release = chan_dev_release,
Chris Leechc13c8262006-05-23 17:18:44 -0700246};
247
248/* --- client and device registration --- */
249
Logan Gunthorpe11a0fd22019-12-16 12:01:18 -0700250/**
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) {
Vinod Koul08baca42019-12-24 10:26:14 +0530292 pr_err("dmaengine dma_channel_table_init failure: %d\n", err);
Logan Gunthorpe11a0fd22019-12-16 12:01:18 -0700293 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
Geert Uytterhoeven69b11892020-01-21 10:33:09 +0100394static int dma_device_satisfies_mask(struct dma_device *device,
395 const dma_cap_mask_t *want)
Dan Williamsd379b012007-07-09 11:56:42 -0700396{
397 dma_cap_mask_t has;
398
Dan Williams59b5ec22009-01-06 11:38:15 -0700399 bitmap_and(has.bits, want->bits, device->cap_mask.bits,
Dan Williamsd379b012007-07-09 11:56:42 -0700400 DMA_TX_TYPE_END);
401 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
402}
403
Dan Williams6f49a572009-01-06 11:38:14 -0700404static struct module *dma_chan_to_owner(struct dma_chan *chan)
405{
Logan Gunthorpedae7a582019-12-16 12:01:16 -0700406 return chan->device->owner;
Dan Williams6f49a572009-01-06 11:38:14 -0700407}
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
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700425static 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
Dan Williams6f49a572009-01-06 11:38:14 -0700442/**
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{
Dan Williams6f49a572009-01-06 11:38:14 -0700450 struct module *owner = dma_chan_to_owner(chan);
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100451 int ret;
Dan Williams6f49a572009-01-06 11:38:14 -0700452
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100453 /* The channel is already in use, update client count */
Dan Williams6f49a572009-01-06 11:38:14 -0700454 if (chan->client_count) {
455 __module_get(owner);
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100456 goto out;
Dan Williams6f49a572009-01-06 11:38:14 -0700457 }
458
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100459 if (!try_module_get(owner))
460 return -ENODEV;
461
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700462 ret = kref_get_unless_zero(&chan->device->ref);
463 if (!ret) {
464 ret = -ENODEV;
465 goto module_put_out;
466 }
467
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100468 /* allocate upon first client reference */
Maxime Ripardc4b54a62014-11-17 14:41:59 +0100469 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 }
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100474
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:
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700483 dma_device_put(chan->device);
484module_put_out:
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100485 module_put(owner);
486 return ret;
Dan Williams6f49a572009-01-06 11:38:14 -0700487}
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{
Maxime Ripardc4b54a62014-11-17 14:41:59 +0100497 /* This channel is not in use, bail out */
Dan Williams6f49a572009-01-06 11:38:14 -0700498 if (!chan->client_count)
Maxime Ripardc4b54a62014-11-17 14:41:59 +0100499 return;
500
Dan Williams6f49a572009-01-06 11:38:14 -0700501 chan->client_count--;
Maxime Ripardc4b54a62014-11-17 14:41:59 +0100502
503 /* This channel is not in use anymore, free it */
Lars-Peter Clausenb36f09c2015-10-20 11:46:28 +0200504 if (!chan->client_count && chan->device->device_free_chan_resources) {
505 /* Make sure all operations have completed */
506 dmaengine_synchronize(chan);
Dan Williams6f49a572009-01-06 11:38:14 -0700507 chan->device->device_free_chan_resources(chan);
Lars-Peter Clausenb36f09c2015-10-20 11:46:28 +0200508 }
Peter Ujfalusi56f13c02015-04-09 12:35:47 +0300509
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 }
Vinod Koul83c77942019-12-24 10:22:15 +0530516
517 dma_device_put(chan->device);
518 module_put(dma_chan_to_owner(chan));
Dan Williams6f49a572009-01-06 11:38:14 -0700519}
520
Dan Williams7405f742007-01-02 11:10:43 -0700521enum 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)) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200530 dev_err(chan->device->dev, "%s: timeout!\n", __func__);
Dan Williams7405f742007-01-02 11:10:43 -0700531 return DMA_ERROR;
532 }
Bartlomiej Zolnierkiewicz2cbe7fe2012-11-08 10:02:07 +0000533 if (status != DMA_IN_PROGRESS)
534 break;
535 cpu_relax();
536 } while (1);
Dan Williams7405f742007-01-02 11:10:43 -0700537
538 return status;
539}
540EXPORT_SYMBOL(dma_sync_wait);
541
Chris Leechc13c8262006-05-23 17:18:44 -0700542/**
Dan Williamsbec08512009-01-06 11:38:14 -0700543 * 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{
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900548 return this_cpu_read(channel_table[tx_type]->chan);
Dan Williamsbec08512009-01-06 11:38:14 -0700549}
550EXPORT_SYMBOL(dma_find_channel);
551
552/**
Dan Williams2ba05622009-01-06 11:38:14 -0700553 * 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
Dan Williams2ba05622009-01-06 11:38:14 -0700560 rcu_read_lock();
Dan Williams59b5ec22009-01-06 11:38:15 -0700561 list_for_each_entry_rcu(device, &dma_device_list, global_node) {
562 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
563 continue;
Dan Williams2ba05622009-01-06 11:38:14 -0700564 list_for_each_entry(chan, &device->channels, device_node)
565 if (chan->client_count)
566 device->device_issue_pending(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700567 }
Dan Williams2ba05622009-01-06 11:38:14 -0700568 rcu_read_unlock();
569}
570EXPORT_SYMBOL(dma_issue_pending_all);
571
Laurent Pinchart0d5484b2014-10-29 00:30:58 +0200572int 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 */
Andy Shevchenkodd4e91d2016-05-10 20:43:34 +0300582 if (!(test_bit(DMA_SLAVE, device->cap_mask.bits) ||
583 test_bit(DMA_CYCLIC, device->cap_mask.bits)))
Laurent Pinchart0d5484b2014-10-29 00:30:58 +0200584 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;
Shawn Lin6d5bbed2016-01-22 19:06:50 +0800597 caps->max_burst = device->max_burst;
Laurent Pinchart0d5484b2014-10-29 00:30:58 +0200598 caps->residue_granularity = device->residue_granularity;
Robert Jarzmik9eeacd32015-10-13 21:54:29 +0200599 caps->descriptor_reuse = device->descriptor_reuse;
Marek Szyprowskid8095f92018-07-02 15:08:10 +0200600 caps->cmd_pause = !!device->device_pause;
601 caps->cmd_resume = !!device->device_resume;
Laurent Pinchart0d5484b2014-10-29 00:30:58 +0200602 caps->cmd_terminate = !!device->device_terminate_all;
603
604 return 0;
605}
606EXPORT_SYMBOL_GPL(dma_get_slave_caps);
607
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +0100608static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
609 struct dma_device *dev,
Dan Williamse2346672009-01-06 11:38:21 -0700610 dma_filter_fn fn, void *fn_param)
Dan Williams59b5ec22009-01-06 11:38:15 -0700611{
612 struct dma_chan *chan;
Dan Williams59b5ec22009-01-06 11:38:15 -0700613
Geert Uytterhoeven69b11892020-01-21 10:33:09 +0100614 if (mask && !dma_device_satisfies_mask(dev, mask)) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200615 dev_dbg(dev->dev, "%s: wrong capabilities\n", __func__);
Dan Williams59b5ec22009-01-06 11:38:15 -0700616 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) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200630 dev_dbg(dev->dev, "%s: %s busy\n",
Dan Williams41d5e592009-01-06 11:38:21 -0700631 __func__, dma_chan_name(chan));
Dan Williams59b5ec22009-01-06 11:38:15 -0700632 continue;
633 }
Dan Williamse2346672009-01-06 11:38:21 -0700634 if (fn && !fn(chan, fn_param)) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200635 dev_dbg(dev->dev, "%s: %s filter said false\n",
Dan Williamse2346672009-01-06 11:38:21 -0700636 __func__, dma_chan_name(chan));
637 continue;
638 }
639 return chan;
Dan Williams59b5ec22009-01-06 11:38:15 -0700640 }
641
Dan Williamse2346672009-01-06 11:38:21 -0700642 return NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700643}
644
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200645static 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) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200664 dev_dbg(device->dev, "%s: %s module removed\n",
665 __func__, dma_chan_name(chan));
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200666 list_del_rcu(&device->global_node);
667 } else
Jarkko Nikulaef859312016-03-14 16:51:09 +0200668 dev_dbg(device->dev,
669 "%s: failed to get %s: (%d)\n",
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200670 __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
Dan Williams59b5ec22009-01-06 11:38:15 -0700682/**
Stefan Agner19d643d2015-06-01 23:53:43 +0200683 * dma_get_slave_channel - try to get specific channel exclusively
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800684 * @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
Vinod Kould9a6c8f2013-08-19 10:47:26 +0530693 if (chan->client_count == 0) {
Peter Ujfalusi214fc4e2015-09-24 12:03:35 +0300694 struct dma_device *device = chan->device;
695
696 dma_cap_set(DMA_PRIVATE, device->cap_mask);
697 device->privatecnt++;
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800698 err = dma_chan_get(chan);
Peter Ujfalusi214fc4e2015-09-24 12:03:35 +0300699 if (err) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200700 dev_dbg(chan->device->dev,
701 "%s: failed to get %s: (%d)\n",
Vinod Kould9a6c8f2013-08-19 10:47:26 +0530702 __func__, dma_chan_name(chan), err);
Peter Ujfalusi214fc4e2015-09-24 12:03:35 +0300703 chan = NULL;
704 if (--device->privatecnt == 0)
705 dma_cap_clear(DMA_PRIVATE, device->cap_mask);
706 }
Vinod Kould9a6c8f2013-08-19 10:47:26 +0530707 } else
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800708 chan = NULL;
709
710 mutex_unlock(&dma_list_mutex);
711
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800712
713 return chan;
714}
715EXPORT_SYMBOL_GPL(dma_get_slave_channel);
716
Stephen Warren8010dad2013-11-26 12:40:51 -0700717struct dma_chan *dma_get_any_slave_channel(struct dma_device *device)
718{
719 dma_cap_mask_t mask;
720 struct dma_chan *chan;
Stephen Warren8010dad2013-11-26 12:40:51 -0700721
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
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200728 chan = find_candidate(device, &mask, NULL, NULL);
Stephen Warren8010dad2013-11-26 12:40:51 -0700729
730 mutex_unlock(&dma_list_mutex);
731
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200732 return IS_ERR(chan) ? NULL : chan;
Stephen Warren8010dad2013-11-26 12:40:51 -0700733}
734EXPORT_SYMBOL_GPL(dma_get_any_slave_channel);
735
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800736/**
Daniel Mack6b9019a2013-08-14 18:35:03 +0200737 * __dma_request_channel - try to allocate an exclusive channel
Dan Williams59b5ec22009-01-06 11:38:15 -0700738 * @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
Baolin Wangf5151312019-05-20 19:32:14 +0800741 * @np: device node to look for DMA channels
Stephen Warren0ad7c002013-11-26 10:04:22 -0700742 *
743 * Returns pointer to appropriate DMA channel on success or NULL.
Dan Williams59b5ec22009-01-06 11:38:15 -0700744 */
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +0100745struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
Baolin Wangf5151312019-05-20 19:32:14 +0800746 dma_filter_fn fn, void *fn_param,
747 struct device_node *np)
Dan Williams59b5ec22009-01-06 11:38:15 -0700748{
749 struct dma_device *device, *_d;
750 struct dma_chan *chan = NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700751
752 /* Find a channel */
753 mutex_lock(&dma_list_mutex);
754 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
Baolin Wangf5151312019-05-20 19:32:14 +0800755 /* Finds a DMA controller with matching device node */
756 if (np && device->dev->of_node && np != device->dev->of_node)
757 continue;
758
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200759 chan = find_candidate(device, mask, fn, fn_param);
760 if (!IS_ERR(chan))
761 break;
Dan Williams59b5ec22009-01-06 11:38:15 -0700762
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200763 chan = NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700764 }
765 mutex_unlock(&dma_list_mutex);
766
Jarkko Nikula4c4d7f872016-04-07 16:49:43 +0300767 pr_debug("%s: %s (%s)\n",
Joe Perches63433252012-07-18 09:51:28 -0700768 __func__,
769 chan ? "success" : "fail",
Dan Williams41d5e592009-01-06 11:38:21 -0700770 chan ? dma_chan_name(chan) : NULL);
Dan Williams59b5ec22009-01-06 11:38:15 -0700771
772 return chan;
773}
774EXPORT_SYMBOL_GPL(__dma_request_channel);
775
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200776static 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
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500796/**
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200797 * dma_request_chan - try to allocate an exclusive slave channel
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500798 * @dev: pointer to client device structure
799 * @name: slave channel name
Stephen Warren0ad7c002013-11-26 10:04:22 -0700800 *
801 * Returns pointer to appropriate DMA channel on success or an error pointer.
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500802 */
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200803struct dma_chan *dma_request_chan(struct device *dev, const char *name)
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500804{
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200805 struct dma_device *d, *_d;
806 struct dma_chan *chan = NULL;
807
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500808 /* If device-tree is present get slave info from here */
809 if (dev->of_node)
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200810 chan = of_dma_request_slave_channel(dev->of_node, name);
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500811
Andy Shevchenko4e82f5d2013-04-09 14:05:44 +0300812 /* If device was enumerated by ACPI get slave info from here */
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200813 if (has_acpi_companion(dev) && !chan)
814 chan = acpi_dma_request_slave_chan_by_name(dev, name);
Andy Shevchenko4e82f5d2013-04-09 14:05:44 +0300815
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100816 if (PTR_ERR(chan) == -EPROBE_DEFER)
817 return chan;
818
819 if (!IS_ERR_OR_NULL(chan))
820 goto found;
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200821
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
Peter Ujfalusibad83562020-01-31 11:38:58 +0200840 if (IS_ERR_OR_NULL(chan))
841 return chan ? chan : ERR_PTR(-EPROBE_DEFER);
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100842
843found:
Peter Ujfalusie937cc12020-03-06 16:28:37 +0200844#ifdef CONFIG_DEBUG_FS
845 chan->dbg_client_name = kasprintf(GFP_KERNEL, "%s:%s", dev_name(dev),
846 name);
847#endif
848
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100849 chan->name = kasprintf(GFP_KERNEL, "dma:%s", name);
850 if (!chan->name)
Peter Ujfalusibad83562020-01-31 11:38:58 +0200851 return chan;
852 chan->slave = dev;
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100853
854 if (sysfs_create_link(&chan->dev->device.kobj, &dev->kobj,
855 DMA_SLAVE_NAME))
Peter Ujfalusibad83562020-01-31 11:38:58 +0200856 dev_warn(dev, "Cannot create DMA %s symlink\n", DMA_SLAVE_NAME);
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100857 if (sysfs_create_link(&dev->kobj, &chan->dev->device.kobj, chan->name))
Peter Ujfalusibad83562020-01-31 11:38:58 +0200858 dev_warn(dev, "Cannot create DMA %s symlink\n", chan->name);
859
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100860 return chan;
Stephen Warren0ad7c002013-11-26 10:04:22 -0700861}
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200862EXPORT_SYMBOL_GPL(dma_request_chan);
Stephen Warren0ad7c002013-11-26 10:04:22 -0700863
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{
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200874 struct dma_chan *ch = dma_request_chan(dev, name);
Stephen Warren0ad7c002013-11-26 10:04:22 -0700875 if (IS_ERR(ch))
876 return NULL;
Robert Baldyga05aa1a72015-08-07 12:26:47 +0200877
Stephen Warren0ad7c002013-11-26 10:04:22 -0700878 return ch;
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500879}
880EXPORT_SYMBOL_GPL(dma_request_slave_channel);
881
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200882/**
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
Baolin Wangf5151312019-05-20 19:32:14 +0800895 chan = __dma_request_channel(mask, NULL, NULL, NULL);
Peter Ujfalusiec8ca8e2018-07-18 12:29:57 +0300896 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 }
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200904
905 return chan;
906}
907EXPORT_SYMBOL_GPL(dma_request_chan_by_mask);
908
Dan Williams59b5ec22009-01-06 11:38:15 -0700909void 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);
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900915 /* drop PRIVATE cap enabled by __dma_request_channel() */
916 if (--chan->device->privatecnt == 0)
917 dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
Peter Ujfalusibad83562020-01-31 11:38:58 +0200918
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100919 if (chan->slave) {
Peter Ujfalusibad83562020-01-31 11:38:58 +0200920 sysfs_remove_link(&chan->dev->device.kobj, DMA_SLAVE_NAME);
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100921 sysfs_remove_link(&chan->slave->kobj, chan->name);
922 kfree(chan->name);
923 chan->name = NULL;
924 chan->slave = NULL;
925 }
Peter Ujfalusie937cc12020-03-06 16:28:37 +0200926
927#ifdef CONFIG_DEBUG_FS
928 kfree(chan->dbg_client_name);
929 chan->dbg_client_name = NULL;
930#endif
Dan Williams59b5ec22009-01-06 11:38:15 -0700931 mutex_unlock(&dma_list_mutex);
932}
933EXPORT_SYMBOL_GPL(dma_release_channel);
934
Dan Williamsbec08512009-01-06 11:38:14 -0700935/**
Dan Williams209b84a2009-01-06 11:38:17 -0700936 * dmaengine_get - register interest in dma_channels
Chris Leechc13c8262006-05-23 17:18:44 -0700937 */
Dan Williams209b84a2009-01-06 11:38:17 -0700938void dmaengine_get(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700939{
Dan Williams6f49a572009-01-06 11:38:14 -0700940 struct dma_device *device, *_d;
941 struct dma_chan *chan;
942 int err;
943
Chris Leechc13c8262006-05-23 17:18:44 -0700944 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700945 dmaengine_ref_count++;
946
947 /* try to grab channels */
Dan Williams59b5ec22009-01-06 11:38:15 -0700948 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
949 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
950 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700951 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 */
Dan Williams2ba05622009-01-06 11:38:14 -0700955 list_del_rcu(&device->global_node);
Dan Williams6f49a572009-01-06 11:38:14 -0700956 break;
957 } else if (err)
Jarkko Nikulaef859312016-03-14 16:51:09 +0200958 dev_dbg(chan->device->dev,
959 "%s: failed to get %s: (%d)\n",
960 __func__, dma_chan_name(chan), err);
Dan Williams6f49a572009-01-06 11:38:14 -0700961 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700962 }
Dan Williams6f49a572009-01-06 11:38:14 -0700963
Dan Williamsbec08512009-01-06 11:38:14 -0700964 /* 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();
Chris Leechc13c8262006-05-23 17:18:44 -0700970 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700971}
Dan Williams209b84a2009-01-06 11:38:17 -0700972EXPORT_SYMBOL(dmaengine_get);
Chris Leechc13c8262006-05-23 17:18:44 -0700973
974/**
Dan Williams209b84a2009-01-06 11:38:17 -0700975 * dmaengine_put - let dma drivers be removed when ref_count == 0
Chris Leechc13c8262006-05-23 17:18:44 -0700976 */
Dan Williams209b84a2009-01-06 11:38:17 -0700977void dmaengine_put(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700978{
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700979 struct dma_device *device, *_d;
Chris Leechc13c8262006-05-23 17:18:44 -0700980 struct dma_chan *chan;
981
Chris Leechc13c8262006-05-23 17:18:44 -0700982 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700983 dmaengine_ref_count--;
984 BUG_ON(dmaengine_ref_count < 0);
985 /* drop channel references */
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700986 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
Dan Williams59b5ec22009-01-06 11:38:15 -0700987 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
988 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700989 list_for_each_entry(chan, &device->channels, device_node)
990 dma_chan_put(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700991 }
Chris Leechc13c8262006-05-23 17:18:44 -0700992 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700993}
Dan Williams209b84a2009-01-06 11:38:17 -0700994EXPORT_SYMBOL(dmaengine_put);
Chris Leechc13c8262006-05-23 17:18:44 -0700995
Dan Williams138f4c32009-09-08 17:42:51 -0700996static 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
Javier Martinez Canillasd57d3a42016-05-11 13:39:27 -04001007 #if IS_ENABLED(CONFIG_ASYNC_MEMCPY)
Dan Williams138f4c32009-09-08 17:42:51 -07001008 if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
1009 return false;
1010 #endif
1011
Javier Martinez Canillasd57d3a42016-05-11 13:39:27 -04001012 #if IS_ENABLED(CONFIG_ASYNC_XOR)
Dan Williams138f4c32009-09-08 17:42:51 -07001013 if (!dma_has_cap(DMA_XOR, device->cap_mask))
1014 return false;
Dan Williams7b3cc2b2009-11-19 17:10:37 -07001015
1016 #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
Dan Williams4499a242009-11-19 17:10:25 -07001017 if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
1018 return false;
Dan Williams138f4c32009-09-08 17:42:51 -07001019 #endif
Dan Williams7b3cc2b2009-11-19 17:10:37 -07001020 #endif
Dan Williams138f4c32009-09-08 17:42:51 -07001021
Javier Martinez Canillasd57d3a42016-05-11 13:39:27 -04001022 #if IS_ENABLED(CONFIG_ASYNC_PQ)
Dan Williams138f4c32009-09-08 17:42:51 -07001023 if (!dma_has_cap(DMA_PQ, device->cap_mask))
1024 return false;
Dan Williams7b3cc2b2009-11-19 17:10:37 -07001025
1026 #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
Dan Williams4499a242009-11-19 17:10:25 -07001027 if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
1028 return false;
Dan Williams138f4c32009-09-08 17:42:51 -07001029 #endif
Dan Williams7b3cc2b2009-11-19 17:10:37 -07001030 #endif
Dan Williams138f4c32009-09-08 17:42:51 -07001031
1032 return true;
1033}
1034
Dan Williams257b17c2009-03-25 09:13:23 -07001035static int get_dma_id(struct dma_device *device)
1036{
Matthew Wilcox485258b2018-06-18 15:41:48 -04001037 int rc = ida_alloc(&dma_ida, GFP_KERNEL);
Dan Williams257b17c2009-03-25 09:13:23 -07001038
Matthew Wilcox485258b2018-06-18 15:41:48 -04001039 if (rc < 0)
1040 return rc;
1041 device->dev_id = rc;
1042 return 0;
Dan Williams257b17c2009-03-25 09:13:23 -07001043}
1044
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001045static 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);
Dave Jiang5429b512020-01-31 10:58:39 -07001056 if (!tchan)
1057 return -ENODEV;
1058
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001059 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
Dave Jiange81274c2020-01-21 16:43:53 -07001108int 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
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001122static 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);
Dave Jiange81274c2020-01-21 16:43:53 -07001129 list_del(&chan->device_node);
1130 device->chancnt--;
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001131 chan->dev->chan = NULL;
1132 mutex_unlock(&dma_list_mutex);
1133 device_unregister(&chan->dev->device);
1134 free_percpu(chan->local);
1135}
1136
Dave Jiange81274c2020-01-21 16:43:53 -07001137void 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
Chris Leechc13c8262006-05-23 17:18:44 -07001145/**
Randy Dunlap65088712006-07-03 19:45:31 -07001146 * dma_async_device_register - registers DMA devices found
Chris Leechc13c8262006-05-23 17:18:44 -07001147 * @device: &dma_device
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -07001148 *
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.
Chris Leechc13c8262006-05-23 17:18:44 -07001152 */
1153int dma_async_device_register(struct dma_device *device)
1154{
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001155 int rc, i = 0;
Chris Leechc13c8262006-05-23 17:18:44 -07001156 struct dma_chan* chan;
1157
1158 if (!device)
1159 return -ENODEV;
1160
Dan Williams7405f742007-01-02 11:10:43 -07001161 /* validate device routines */
Vinod Koul3eeb5152017-08-27 16:55:32 +05301162 if (!device->dev) {
1163 pr_err("DMAdevice must have dev\n");
1164 return -EIO;
1165 }
Dan Williams7405f742007-01-02 11:10:43 -07001166
Logan Gunthorpedae7a582019-12-16 12:01:16 -07001167 device->owner = device->dev->driver->owner;
1168
Vinod Koul3eeb5152017-08-27 16:55:32 +05301169 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 }
Dan Williams7405f742007-01-02 11:10:43 -07001243
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -07001244 if (!device->device_release)
1245 dev_warn(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
Dan Williams138f4c32009-09-08 17:42:51 -07001250 /* note: this only matters in the
Dan Williams5fc6d892010-10-07 16:44:50 -07001251 * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
Dan Williams138f4c32009-09-08 17:42:51 -07001252 */
1253 if (device_has_all_tx_types(device))
1254 dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
1255
Dan Williams257b17c2009-03-25 09:13:23 -07001256 rc = get_dma_id(device);
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001257 if (rc != 0)
Dan Williams864498a2009-01-06 11:38:21 -07001258 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -07001259
1260 /* represent channels in sysfs. Probably want devs too */
1261 list_for_each_entry(chan, &device->channels, device_node) {
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001262 rc = __dma_async_device_channel_register(device, chan, i++);
1263 if (rc < 0)
Dan Williams257b17c2009-03-25 09:13:23 -07001264 goto err_out;
Chris Leechc13c8262006-05-23 17:18:44 -07001265 }
Viresh Kumar76d7b842016-07-27 14:32:58 -07001266
Chris Leechc13c8262006-05-23 17:18:44 -07001267 mutex_lock(&dma_list_mutex);
Dan Williams59b5ec22009-01-06 11:38:15 -07001268 /* take references on public channels */
1269 if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
Dan Williams6f49a572009-01-06 11:38:14 -07001270 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 }
Dan Williams2ba05622009-01-06 11:38:14 -07001284 list_add_tail_rcu(&device->global_node, &dma_device_list);
Atsushi Nemoto0f571512009-03-06 20:07:14 +09001285 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
1286 device->privatecnt++; /* Always private */
Dan Williamsbec08512009-01-06 11:38:14 -07001287 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -07001288 mutex_unlock(&dma_list_mutex);
1289
Peter Ujfalusi26cf1322020-03-06 16:28:39 +02001290 dmaengine_debug_register(device);
1291
Chris Leechc13c8262006-05-23 17:18:44 -07001292 return 0;
Jeff Garzikff487fb2007-03-08 09:57:34 -08001293
1294err_out:
Dan Williams257b17c2009-03-25 09:13:23 -07001295 /* if we never registered a channel just release the idr */
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001296 if (!device->chancnt) {
Matthew Wilcox485258b2018-06-18 15:41:48 -04001297 ida_free(&dma_ida, device->dev_id);
Dan Williams257b17c2009-03-25 09:13:23 -07001298 return rc;
1299 }
1300
Jeff Garzikff487fb2007-03-08 09:57:34 -08001301 list_for_each_entry(chan, &device->channels, device_node) {
1302 if (chan->local == NULL)
1303 continue;
Dan Williams41d5e592009-01-06 11:38:21 -07001304 mutex_lock(&dma_list_mutex);
1305 chan->dev->chan = NULL;
1306 mutex_unlock(&dma_list_mutex);
1307 device_unregister(&chan->dev->device);
Jeff Garzikff487fb2007-03-08 09:57:34 -08001308 free_percpu(chan->local);
1309 }
1310 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -07001311}
David Brownell765e3d82007-03-16 13:38:05 -08001312EXPORT_SYMBOL(dma_async_device_register);
Chris Leechc13c8262006-05-23 17:18:44 -07001313
1314/**
Dan Williams6f49a572009-01-06 11:38:14 -07001315 * dma_async_device_unregister - unregister a DMA device
Randy Dunlap65088712006-07-03 19:45:31 -07001316 * @device: &dma_device
Dan Williamsf27c5802009-01-06 11:38:18 -07001317 *
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.
Randy Dunlap65088712006-07-03 19:45:31 -07001320 */
1321void dma_async_device_unregister(struct dma_device *device)
Chris Leechc13c8262006-05-23 17:18:44 -07001322{
Dave Jiange81274c2020-01-21 16:43:53 -07001323 struct dma_chan *chan, *n;
Chris Leechc13c8262006-05-23 17:18:44 -07001324
Peter Ujfalusi26cf1322020-03-06 16:28:39 +02001325 dmaengine_debug_unregister(device);
1326
Dave Jiange81274c2020-01-21 16:43:53 -07001327 list_for_each_entry_safe(chan, n, &device->channels, device_node)
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001328 __dma_async_device_channel_unregister(device, chan);
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -07001329
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);
Chris Leechc13c8262006-05-23 17:18:44 -07001339}
David Brownell765e3d82007-03-16 13:38:05 -08001340EXPORT_SYMBOL(dma_async_device_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -07001341
Huang Shijief39b9482018-07-26 14:45:53 +08001342static 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
Dan Williams45c463a2013-10-18 19:35:24 +02001377struct 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),
Dan Williams3cc377b2013-12-09 10:33:16 -08001387 #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
Dan Williams45c463a2013-10-18 19:35:24 +02001388 __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)
Dan Williams7405f742007-01-02 11:10:43 -07001395{
Dan Williams45c463a2013-10-18 19:35:24 +02001396 int order = get_count_order(nr);
Dan Williams7405f742007-01-02 11:10:43 -07001397
Dan Williams45c463a2013-10-18 19:35:24 +02001398 switch (order) {
1399 case 0 ... 1:
1400 return &unmap_pool[0];
Matthias Kaehlcke23f963e92017-03-13 14:30:29 -07001401#if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
Dan Williams45c463a2013-10-18 19:35:24 +02001402 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];
Matthias Kaehlcke23f963e92017-03-13 14:30:29 -07001408#endif
Dan Williams45c463a2013-10-18 19:35:24 +02001409 default:
1410 BUG();
1411 return NULL;
1412 }
1413}
Dan Williams00367312008-02-02 19:49:57 -07001414
Dan Williams45c463a2013-10-18 19:35:24 +02001415static 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;
Dan Williams7476bd792013-10-18 19:35:29 +02001430 for (; i < cnt; i++) {
1431 if (unmap->addr[i] == 0)
1432 continue;
Dan Williams45c463a2013-10-18 19:35:24 +02001433 dma_unmap_page(dev, unmap->addr[i], unmap->len,
1434 DMA_BIDIRECTIONAL);
Dan Williams7476bd792013-10-18 19:35:29 +02001435 }
Xuelin Shic1f43dd2014-05-21 14:02:37 -07001436 cnt = unmap->map_cnt;
Dan Williams45c463a2013-10-18 19:35:24 +02001437 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
Julia Lawall240eb9162015-09-13 14:15:19 +02001454 mempool_destroy(p->pool);
Dan Williams45c463a2013-10-18 19:35:24 +02001455 p->pool = NULL;
Julia Lawall240eb9162015-09-13 14:15:19 +02001456 kmem_cache_destroy(p->cache);
Dan Williams45c463a2013-10-18 19:35:24 +02001457 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;
Dan Williams00367312008-02-02 19:49:57 -07001479 }
Dan Williams7405f742007-01-02 11:10:43 -07001480
Dan Williams45c463a2013-10-18 19:35:24 +02001481 if (i == ARRAY_SIZE(unmap_pool))
1482 return 0;
Dan Williams7405f742007-01-02 11:10:43 -07001483
Dan Williams45c463a2013-10-18 19:35:24 +02001484 dmaengine_destroy_unmap_pool();
1485 return -ENOMEM;
Dan Williams7405f742007-01-02 11:10:43 -07001486}
Dan Williams7405f742007-01-02 11:10:43 -07001487
Dan Williams89716462013-10-18 19:35:25 +02001488struct dmaengine_unmap_data *
Dan Williams45c463a2013-10-18 19:35:24 +02001489dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
Dan Williams7405f742007-01-02 11:10:43 -07001490{
Dan Williams45c463a2013-10-18 19:35:24 +02001491 struct dmaengine_unmap_data *unmap;
Dan Williams7405f742007-01-02 11:10:43 -07001492
Dan Williams45c463a2013-10-18 19:35:24 +02001493 unmap = mempool_alloc(__get_unmap_pool(nr)->pool, flags);
1494 if (!unmap)
1495 return NULL;
Dan Williams00367312008-02-02 19:49:57 -07001496
Dan Williams45c463a2013-10-18 19:35:24 +02001497 memset(unmap, 0, sizeof(*unmap));
1498 kref_init(&unmap->kref);
1499 unmap->dev = dev;
Xuelin Shic1f43dd2014-05-21 14:02:37 -07001500 unmap->map_cnt = nr;
Dan Williams7405f742007-01-02 11:10:43 -07001501
Dan Williams45c463a2013-10-18 19:35:24 +02001502 return unmap;
Dan Williams7405f742007-01-02 11:10:43 -07001503}
Dan Williams89716462013-10-18 19:35:25 +02001504EXPORT_SYMBOL(dmaengine_get_unmap_data);
Dan Williams7405f742007-01-02 11:10:43 -07001505
Dan Williams7405f742007-01-02 11:10:43 -07001506void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1507 struct dma_chan *chan)
1508{
1509 tx->chan = chan;
Dan Williams5fc6d892010-10-07 16:44:50 -07001510 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williams7405f742007-01-02 11:10:43 -07001511 spin_lock_init(&tx->lock);
Dan Williamscaa20d972010-05-17 16:24:16 -07001512 #endif
Dan Williams7405f742007-01-02 11:10:43 -07001513}
1514EXPORT_SYMBOL(dma_async_tx_descriptor_init);
1515
Peter Ujfalusi4db8fd32019-12-23 13:04:44 +02001516static 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
Dan Williams07f22112009-01-05 17:14:31 -07001589/* dma_wait_for_async_tx - spin wait for a transaction to complete
1590 * @tx: in-flight transaction to wait on
Dan Williams07f22112009-01-05 17:14:31 -07001591 */
1592enum dma_status
1593dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1594{
Dan Williams95475e52009-07-14 12:19:02 -07001595 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
Dan Williams07f22112009-01-05 17:14:31 -07001596
1597 if (!tx)
Vinod Kouladfedd92013-10-16 13:29:02 +05301598 return DMA_COMPLETE;
Dan Williams07f22112009-01-05 17:14:31 -07001599
Dan Williams95475e52009-07-14 12:19:02 -07001600 while (tx->cookie == -EBUSY) {
1601 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
Jarkko Nikulaef859312016-03-14 16:51:09 +02001602 dev_err(tx->chan->device->dev,
1603 "%s timeout waiting for descriptor submission\n",
1604 __func__);
Dan Williams95475e52009-07-14 12:19:02 -07001605 return DMA_ERROR;
1606 }
1607 cpu_relax();
1608 }
1609 return dma_sync_wait(tx->chan, tx->cookie);
Dan Williams07f22112009-01-05 17:14:31 -07001610}
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{
Dan Williamscaa20d972010-05-17 16:24:16 -07001619 struct dma_async_tx_descriptor *dep = txd_next(tx);
Dan Williams07f22112009-01-05 17:14:31 -07001620 struct dma_async_tx_descriptor *dep_next;
1621 struct dma_chan *chan;
1622
1623 if (!dep)
1624 return;
1625
Yuri Tikhonovdd59b852009-01-12 15:17:20 -07001626 /* we'll submit tx->next now, so clear the link */
Dan Williamscaa20d972010-05-17 16:24:16 -07001627 txd_clear_next(tx);
Dan Williams07f22112009-01-05 17:14:31 -07001628 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) {
Dan Williamscaa20d972010-05-17 16:24:16 -07001635 txd_lock(dep);
1636 txd_clear_parent(dep);
1637 dep_next = txd_next(dep);
Dan Williams07f22112009-01-05 17:14:31 -07001638 if (dep_next && dep_next->chan == chan)
Dan Williamscaa20d972010-05-17 16:24:16 -07001639 txd_clear_next(dep); /* ->next will be submitted */
Dan Williams07f22112009-01-05 17:14:31 -07001640 else
1641 dep_next = NULL; /* submit current dep and terminate */
Dan Williamscaa20d972010-05-17 16:24:16 -07001642 txd_unlock(dep);
Dan Williams07f22112009-01-05 17:14:31 -07001643
1644 dep->tx_submit(dep);
1645 }
1646
1647 chan->device->device_issue_pending(chan);
1648}
1649EXPORT_SYMBOL_GPL(dma_run_dependencies);
1650
Chris Leechc13c8262006-05-23 17:18:44 -07001651static int __init dma_bus_init(void)
1652{
Dan Williams45c463a2013-10-18 19:35:24 +02001653 int err = dmaengine_init_unmap_pool();
1654
1655 if (err)
1656 return err;
Peter Ujfalusie937cc12020-03-06 16:28:37 +02001657
1658 err = class_register(&dma_devclass);
1659 if (!err)
1660 dmaengine_debugfs_init();
1661
1662 return err;
Chris Leechc13c8262006-05-23 17:18:44 -07001663}
Dan Williams652afc22009-01-06 11:38:22 -07001664arch_initcall(dma_bus_init);