blob: 509abc8e8378135ae0527f6901ed4c1a6fc426da [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
65static void dmaengine_dbg_summary_show(struct seq_file *s,
66 struct dma_device *dma_dev)
67{
68 struct dma_chan *chan;
69
70 list_for_each_entry(chan, &dma_dev->channels, device_node) {
71 if (chan->client_count) {
72 seq_printf(s, " %-13s| %s", dma_chan_name(chan),
73 chan->dbg_client_name ?: "in-use");
74
75 if (chan->router)
76 seq_printf(s, " (via router: %s)\n",
77 dev_name(chan->router->dev));
78 else
79 seq_puts(s, "\n");
80 }
81 }
82}
83
84static int dmaengine_summary_show(struct seq_file *s, void *data)
85{
86 struct dma_device *dma_dev = NULL;
87
88 mutex_lock(&dma_list_mutex);
89 list_for_each_entry(dma_dev, &dma_device_list, global_node) {
90 seq_printf(s, "dma%d (%s): number of channels: %u\n",
91 dma_dev->dev_id, dev_name(dma_dev->dev),
92 dma_dev->chancnt);
93
94 if (dma_dev->dbg_summary_show)
95 dma_dev->dbg_summary_show(s, dma_dev);
96 else
97 dmaengine_dbg_summary_show(s, dma_dev);
98
99 if (!list_is_last(&dma_dev->global_node, &dma_device_list))
100 seq_puts(s, "\n");
101 }
102 mutex_unlock(&dma_list_mutex);
103
104 return 0;
105}
106DEFINE_SHOW_ATTRIBUTE(dmaengine_summary);
107
108static void __init dmaengine_debugfs_init(void)
109{
110 struct dentry *rootdir = debugfs_create_dir("dmaengine", NULL);
111
112 /* /sys/kernel/debug/dmaengine/summary */
113 debugfs_create_file("summary", 0444, rootdir, NULL,
114 &dmaengine_summary_fops);
115}
116#else
117static inline void dmaengine_debugfs_init(void) { }
118#endif /* DEBUG_FS */
119
Chris Leechc13c8262006-05-23 17:18:44 -0700120/* --- sysfs implementation --- */
121
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100122#define DMA_SLAVE_NAME "slave"
123
Dan Williams41d5e592009-01-06 11:38:21 -0700124/**
Geert Uytterhoevenfe333382019-06-07 13:30:39 +0200125 * dev_to_dma_chan - convert a device pointer to its sysfs container object
Dan Williams41d5e592009-01-06 11:38:21 -0700126 * @dev - device node
127 *
128 * Must be called under dma_list_mutex
129 */
130static struct dma_chan *dev_to_dma_chan(struct device *dev)
131{
132 struct dma_chan_dev *chan_dev;
133
134 chan_dev = container_of(dev, typeof(*chan_dev), device);
135 return chan_dev->chan;
136}
137
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700138static ssize_t memcpy_count_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700140{
Dan Williams41d5e592009-01-06 11:38:21 -0700141 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700142 unsigned long count = 0;
143 int i;
Dan Williams41d5e592009-01-06 11:38:21 -0700144 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700145
Dan Williams41d5e592009-01-06 11:38:21 -0700146 mutex_lock(&dma_list_mutex);
147 chan = dev_to_dma_chan(dev);
148 if (chan) {
149 for_each_possible_cpu(i)
150 count += per_cpu_ptr(chan->local, i)->memcpy_count;
151 err = sprintf(buf, "%lu\n", count);
152 } else
153 err = -ENODEV;
154 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700155
Dan Williams41d5e592009-01-06 11:38:21 -0700156 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700157}
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700158static DEVICE_ATTR_RO(memcpy_count);
Chris Leechc13c8262006-05-23 17:18:44 -0700159
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700160static ssize_t bytes_transferred_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)->bytes_transferred;
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(bytes_transferred);
Chris Leechc13c8262006-05-23 17:18:44 -0700181
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700182static ssize_t in_use_show(struct device *dev, struct device_attribute *attr,
183 char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700184{
Dan Williams41d5e592009-01-06 11:38:21 -0700185 struct dma_chan *chan;
186 int err;
Chris Leechc13c8262006-05-23 17:18:44 -0700187
Dan Williams41d5e592009-01-06 11:38:21 -0700188 mutex_lock(&dma_list_mutex);
189 chan = dev_to_dma_chan(dev);
190 if (chan)
191 err = sprintf(buf, "%d\n", chan->client_count);
192 else
193 err = -ENODEV;
194 mutex_unlock(&dma_list_mutex);
195
196 return err;
Chris Leechc13c8262006-05-23 17:18:44 -0700197}
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700198static DEVICE_ATTR_RO(in_use);
Chris Leechc13c8262006-05-23 17:18:44 -0700199
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700200static struct attribute *dma_dev_attrs[] = {
201 &dev_attr_memcpy_count.attr,
202 &dev_attr_bytes_transferred.attr,
203 &dev_attr_in_use.attr,
204 NULL,
Chris Leechc13c8262006-05-23 17:18:44 -0700205};
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700206ATTRIBUTE_GROUPS(dma_dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700207
Dan Williams41d5e592009-01-06 11:38:21 -0700208static void chan_dev_release(struct device *dev)
209{
210 struct dma_chan_dev *chan_dev;
211
212 chan_dev = container_of(dev, typeof(*chan_dev), device);
Dan Williams864498a2009-01-06 11:38:21 -0700213 if (atomic_dec_and_test(chan_dev->idr_ref)) {
Matthew Wilcox485258b2018-06-18 15:41:48 -0400214 ida_free(&dma_ida, chan_dev->dev_id);
Dan Williams864498a2009-01-06 11:38:21 -0700215 kfree(chan_dev->idr_ref);
216 }
Dan Williams41d5e592009-01-06 11:38:21 -0700217 kfree(chan_dev);
218}
219
Chris Leechc13c8262006-05-23 17:18:44 -0700220static struct class dma_devclass = {
Tony Jones891f78e2007-09-25 02:03:03 +0200221 .name = "dma",
Greg Kroah-Hartman58b267d2013-07-24 15:05:08 -0700222 .dev_groups = dma_dev_groups,
Dan Williams41d5e592009-01-06 11:38:21 -0700223 .dev_release = chan_dev_release,
Chris Leechc13c8262006-05-23 17:18:44 -0700224};
225
226/* --- client and device registration --- */
227
Logan Gunthorpe11a0fd22019-12-16 12:01:18 -0700228/**
229 * dma_cap_mask_all - enable iteration over all operation types
230 */
231static dma_cap_mask_t dma_cap_mask_all;
232
233/**
234 * dma_chan_tbl_ent - tracks channel allocations per core/operation
235 * @chan - associated channel for this entry
236 */
237struct dma_chan_tbl_ent {
238 struct dma_chan *chan;
239};
240
241/**
242 * channel_table - percpu lookup table for memory-to-memory offload providers
243 */
244static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
245
246static int __init dma_channel_table_init(void)
247{
248 enum dma_transaction_type cap;
249 int err = 0;
250
251 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
252
253 /* 'interrupt', 'private', and 'slave' are channel capabilities,
254 * but are not associated with an operation so they do not need
255 * an entry in the channel_table
256 */
257 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
258 clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
259 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
260
261 for_each_dma_cap_mask(cap, dma_cap_mask_all) {
262 channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
263 if (!channel_table[cap]) {
264 err = -ENOMEM;
265 break;
266 }
267 }
268
269 if (err) {
Vinod Koul08baca42019-12-24 10:26:14 +0530270 pr_err("dmaengine dma_channel_table_init failure: %d\n", err);
Logan Gunthorpe11a0fd22019-12-16 12:01:18 -0700271 for_each_dma_cap_mask(cap, dma_cap_mask_all)
272 free_percpu(channel_table[cap]);
273 }
274
275 return err;
276}
277arch_initcall(dma_channel_table_init);
278
279/**
280 * dma_chan_is_local - returns true if the channel is in the same numa-node as
281 * the cpu
282 */
283static bool dma_chan_is_local(struct dma_chan *chan, int cpu)
284{
285 int node = dev_to_node(chan->device->dev);
286 return node == NUMA_NO_NODE ||
287 cpumask_test_cpu(cpu, cpumask_of_node(node));
288}
289
290/**
291 * min_chan - returns the channel with min count and in the same numa-node as
292 * the cpu
293 * @cap: capability to match
294 * @cpu: cpu index which the channel should be close to
295 *
296 * If some channels are close to the given cpu, the one with the lowest
297 * reference count is returned. Otherwise, cpu is ignored and only the
298 * reference count is taken into account.
299 * Must be called under dma_list_mutex.
300 */
301static struct dma_chan *min_chan(enum dma_transaction_type cap, int cpu)
302{
303 struct dma_device *device;
304 struct dma_chan *chan;
305 struct dma_chan *min = NULL;
306 struct dma_chan *localmin = NULL;
307
308 list_for_each_entry(device, &dma_device_list, global_node) {
309 if (!dma_has_cap(cap, device->cap_mask) ||
310 dma_has_cap(DMA_PRIVATE, device->cap_mask))
311 continue;
312 list_for_each_entry(chan, &device->channels, device_node) {
313 if (!chan->client_count)
314 continue;
315 if (!min || chan->table_count < min->table_count)
316 min = chan;
317
318 if (dma_chan_is_local(chan, cpu))
319 if (!localmin ||
320 chan->table_count < localmin->table_count)
321 localmin = chan;
322 }
323 }
324
325 chan = localmin ? localmin : min;
326
327 if (chan)
328 chan->table_count++;
329
330 return chan;
331}
332
333/**
334 * dma_channel_rebalance - redistribute the available channels
335 *
336 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
337 * operation type) in the SMP case, and operation isolation (avoid
338 * multi-tasking channels) in the non-SMP case. Must be called under
339 * dma_list_mutex.
340 */
341static void dma_channel_rebalance(void)
342{
343 struct dma_chan *chan;
344 struct dma_device *device;
345 int cpu;
346 int cap;
347
348 /* undo the last distribution */
349 for_each_dma_cap_mask(cap, dma_cap_mask_all)
350 for_each_possible_cpu(cpu)
351 per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
352
353 list_for_each_entry(device, &dma_device_list, global_node) {
354 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
355 continue;
356 list_for_each_entry(chan, &device->channels, device_node)
357 chan->table_count = 0;
358 }
359
360 /* don't populate the channel_table if no clients are available */
361 if (!dmaengine_ref_count)
362 return;
363
364 /* redistribute available channels */
365 for_each_dma_cap_mask(cap, dma_cap_mask_all)
366 for_each_online_cpu(cpu) {
367 chan = min_chan(cap, cpu);
368 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
369 }
370}
371
Geert Uytterhoeven69b11892020-01-21 10:33:09 +0100372static int dma_device_satisfies_mask(struct dma_device *device,
373 const dma_cap_mask_t *want)
Dan Williamsd379b012007-07-09 11:56:42 -0700374{
375 dma_cap_mask_t has;
376
Dan Williams59b5ec22009-01-06 11:38:15 -0700377 bitmap_and(has.bits, want->bits, device->cap_mask.bits,
Dan Williamsd379b012007-07-09 11:56:42 -0700378 DMA_TX_TYPE_END);
379 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
380}
381
Dan Williams6f49a572009-01-06 11:38:14 -0700382static struct module *dma_chan_to_owner(struct dma_chan *chan)
383{
Logan Gunthorpedae7a582019-12-16 12:01:16 -0700384 return chan->device->owner;
Dan Williams6f49a572009-01-06 11:38:14 -0700385}
386
387/**
388 * balance_ref_count - catch up the channel reference count
389 * @chan - channel to balance ->client_count versus dmaengine_ref_count
390 *
391 * balance_ref_count must be called under dma_list_mutex
392 */
393static void balance_ref_count(struct dma_chan *chan)
394{
395 struct module *owner = dma_chan_to_owner(chan);
396
397 while (chan->client_count < dmaengine_ref_count) {
398 __module_get(owner);
399 chan->client_count++;
400 }
401}
402
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700403static void dma_device_release(struct kref *ref)
404{
405 struct dma_device *device = container_of(ref, struct dma_device, ref);
406
407 list_del_rcu(&device->global_node);
408 dma_channel_rebalance();
409
410 if (device->device_release)
411 device->device_release(device);
412}
413
414static void dma_device_put(struct dma_device *device)
415{
416 lockdep_assert_held(&dma_list_mutex);
417 kref_put(&device->ref, dma_device_release);
418}
419
Dan Williams6f49a572009-01-06 11:38:14 -0700420/**
421 * dma_chan_get - try to grab a dma channel's parent driver module
422 * @chan - channel to grab
423 *
424 * Must be called under dma_list_mutex
425 */
426static int dma_chan_get(struct dma_chan *chan)
427{
Dan Williams6f49a572009-01-06 11:38:14 -0700428 struct module *owner = dma_chan_to_owner(chan);
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100429 int ret;
Dan Williams6f49a572009-01-06 11:38:14 -0700430
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100431 /* The channel is already in use, update client count */
Dan Williams6f49a572009-01-06 11:38:14 -0700432 if (chan->client_count) {
433 __module_get(owner);
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100434 goto out;
Dan Williams6f49a572009-01-06 11:38:14 -0700435 }
436
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100437 if (!try_module_get(owner))
438 return -ENODEV;
439
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700440 ret = kref_get_unless_zero(&chan->device->ref);
441 if (!ret) {
442 ret = -ENODEV;
443 goto module_put_out;
444 }
445
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100446 /* allocate upon first client reference */
Maxime Ripardc4b54a62014-11-17 14:41:59 +0100447 if (chan->device->device_alloc_chan_resources) {
448 ret = chan->device->device_alloc_chan_resources(chan);
449 if (ret < 0)
450 goto err_out;
451 }
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100452
453 if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
454 balance_ref_count(chan);
455
456out:
457 chan->client_count++;
458 return 0;
459
460err_out:
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700461 dma_device_put(chan->device);
462module_put_out:
Maxime Ripardd2f4f992014-11-17 14:41:58 +0100463 module_put(owner);
464 return ret;
Dan Williams6f49a572009-01-06 11:38:14 -0700465}
466
467/**
468 * dma_chan_put - drop a reference to a dma channel's parent driver module
469 * @chan - channel to release
470 *
471 * Must be called under dma_list_mutex
472 */
473static void dma_chan_put(struct dma_chan *chan)
474{
Maxime Ripardc4b54a62014-11-17 14:41:59 +0100475 /* This channel is not in use, bail out */
Dan Williams6f49a572009-01-06 11:38:14 -0700476 if (!chan->client_count)
Maxime Ripardc4b54a62014-11-17 14:41:59 +0100477 return;
478
Dan Williams6f49a572009-01-06 11:38:14 -0700479 chan->client_count--;
Maxime Ripardc4b54a62014-11-17 14:41:59 +0100480
481 /* This channel is not in use anymore, free it */
Lars-Peter Clausenb36f09c2015-10-20 11:46:28 +0200482 if (!chan->client_count && chan->device->device_free_chan_resources) {
483 /* Make sure all operations have completed */
484 dmaengine_synchronize(chan);
Dan Williams6f49a572009-01-06 11:38:14 -0700485 chan->device->device_free_chan_resources(chan);
Lars-Peter Clausenb36f09c2015-10-20 11:46:28 +0200486 }
Peter Ujfalusi56f13c02015-04-09 12:35:47 +0300487
488 /* If the channel is used via a DMA request router, free the mapping */
489 if (chan->router && chan->router->route_free) {
490 chan->router->route_free(chan->router->dev, chan->route_data);
491 chan->router = NULL;
492 chan->route_data = NULL;
493 }
Vinod Koul83c77942019-12-24 10:22:15 +0530494
495 dma_device_put(chan->device);
496 module_put(dma_chan_to_owner(chan));
Dan Williams6f49a572009-01-06 11:38:14 -0700497}
498
Dan Williams7405f742007-01-02 11:10:43 -0700499enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
500{
501 enum dma_status status;
502 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
503
504 dma_async_issue_pending(chan);
505 do {
506 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
507 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200508 dev_err(chan->device->dev, "%s: timeout!\n", __func__);
Dan Williams7405f742007-01-02 11:10:43 -0700509 return DMA_ERROR;
510 }
Bartlomiej Zolnierkiewicz2cbe7fe2012-11-08 10:02:07 +0000511 if (status != DMA_IN_PROGRESS)
512 break;
513 cpu_relax();
514 } while (1);
Dan Williams7405f742007-01-02 11:10:43 -0700515
516 return status;
517}
518EXPORT_SYMBOL(dma_sync_wait);
519
Chris Leechc13c8262006-05-23 17:18:44 -0700520/**
Dan Williamsbec08512009-01-06 11:38:14 -0700521 * dma_find_channel - find a channel to carry out the operation
522 * @tx_type: transaction type
523 */
524struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
525{
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900526 return this_cpu_read(channel_table[tx_type]->chan);
Dan Williamsbec08512009-01-06 11:38:14 -0700527}
528EXPORT_SYMBOL(dma_find_channel);
529
530/**
Dan Williams2ba05622009-01-06 11:38:14 -0700531 * dma_issue_pending_all - flush all pending operations across all channels
532 */
533void dma_issue_pending_all(void)
534{
535 struct dma_device *device;
536 struct dma_chan *chan;
537
Dan Williams2ba05622009-01-06 11:38:14 -0700538 rcu_read_lock();
Dan Williams59b5ec22009-01-06 11:38:15 -0700539 list_for_each_entry_rcu(device, &dma_device_list, global_node) {
540 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
541 continue;
Dan Williams2ba05622009-01-06 11:38:14 -0700542 list_for_each_entry(chan, &device->channels, device_node)
543 if (chan->client_count)
544 device->device_issue_pending(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700545 }
Dan Williams2ba05622009-01-06 11:38:14 -0700546 rcu_read_unlock();
547}
548EXPORT_SYMBOL(dma_issue_pending_all);
549
Laurent Pinchart0d5484b2014-10-29 00:30:58 +0200550int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps)
551{
552 struct dma_device *device;
553
554 if (!chan || !caps)
555 return -EINVAL;
556
557 device = chan->device;
558
559 /* check if the channel supports slave transactions */
Andy Shevchenkodd4e91d2016-05-10 20:43:34 +0300560 if (!(test_bit(DMA_SLAVE, device->cap_mask.bits) ||
561 test_bit(DMA_CYCLIC, device->cap_mask.bits)))
Laurent Pinchart0d5484b2014-10-29 00:30:58 +0200562 return -ENXIO;
563
564 /*
565 * Check whether it reports it uses the generic slave
566 * capabilities, if not, that means it doesn't support any
567 * kind of slave capabilities reporting.
568 */
569 if (!device->directions)
570 return -ENXIO;
571
572 caps->src_addr_widths = device->src_addr_widths;
573 caps->dst_addr_widths = device->dst_addr_widths;
574 caps->directions = device->directions;
Shawn Lin6d5bbed2016-01-22 19:06:50 +0800575 caps->max_burst = device->max_burst;
Laurent Pinchart0d5484b2014-10-29 00:30:58 +0200576 caps->residue_granularity = device->residue_granularity;
Robert Jarzmik9eeacd32015-10-13 21:54:29 +0200577 caps->descriptor_reuse = device->descriptor_reuse;
Marek Szyprowskid8095f92018-07-02 15:08:10 +0200578 caps->cmd_pause = !!device->device_pause;
579 caps->cmd_resume = !!device->device_resume;
Laurent Pinchart0d5484b2014-10-29 00:30:58 +0200580 caps->cmd_terminate = !!device->device_terminate_all;
581
582 return 0;
583}
584EXPORT_SYMBOL_GPL(dma_get_slave_caps);
585
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +0100586static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
587 struct dma_device *dev,
Dan Williamse2346672009-01-06 11:38:21 -0700588 dma_filter_fn fn, void *fn_param)
Dan Williams59b5ec22009-01-06 11:38:15 -0700589{
590 struct dma_chan *chan;
Dan Williams59b5ec22009-01-06 11:38:15 -0700591
Geert Uytterhoeven69b11892020-01-21 10:33:09 +0100592 if (mask && !dma_device_satisfies_mask(dev, mask)) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200593 dev_dbg(dev->dev, "%s: wrong capabilities\n", __func__);
Dan Williams59b5ec22009-01-06 11:38:15 -0700594 return NULL;
595 }
596 /* devices with multiple channels need special handling as we need to
597 * ensure that all channels are either private or public.
598 */
599 if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
600 list_for_each_entry(chan, &dev->channels, device_node) {
601 /* some channels are already publicly allocated */
602 if (chan->client_count)
603 return NULL;
604 }
605
606 list_for_each_entry(chan, &dev->channels, device_node) {
607 if (chan->client_count) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200608 dev_dbg(dev->dev, "%s: %s busy\n",
Dan Williams41d5e592009-01-06 11:38:21 -0700609 __func__, dma_chan_name(chan));
Dan Williams59b5ec22009-01-06 11:38:15 -0700610 continue;
611 }
Dan Williamse2346672009-01-06 11:38:21 -0700612 if (fn && !fn(chan, fn_param)) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200613 dev_dbg(dev->dev, "%s: %s filter said false\n",
Dan Williamse2346672009-01-06 11:38:21 -0700614 __func__, dma_chan_name(chan));
615 continue;
616 }
617 return chan;
Dan Williams59b5ec22009-01-06 11:38:15 -0700618 }
619
Dan Williamse2346672009-01-06 11:38:21 -0700620 return NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700621}
622
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200623static struct dma_chan *find_candidate(struct dma_device *device,
624 const dma_cap_mask_t *mask,
625 dma_filter_fn fn, void *fn_param)
626{
627 struct dma_chan *chan = private_candidate(mask, device, fn, fn_param);
628 int err;
629
630 if (chan) {
631 /* Found a suitable channel, try to grab, prep, and return it.
632 * We first set DMA_PRIVATE to disable balance_ref_count as this
633 * channel will not be published in the general-purpose
634 * allocator
635 */
636 dma_cap_set(DMA_PRIVATE, device->cap_mask);
637 device->privatecnt++;
638 err = dma_chan_get(chan);
639
640 if (err) {
641 if (err == -ENODEV) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200642 dev_dbg(device->dev, "%s: %s module removed\n",
643 __func__, dma_chan_name(chan));
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200644 list_del_rcu(&device->global_node);
645 } else
Jarkko Nikulaef859312016-03-14 16:51:09 +0200646 dev_dbg(device->dev,
647 "%s: failed to get %s: (%d)\n",
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200648 __func__, dma_chan_name(chan), err);
649
650 if (--device->privatecnt == 0)
651 dma_cap_clear(DMA_PRIVATE, device->cap_mask);
652
653 chan = ERR_PTR(err);
654 }
655 }
656
657 return chan ? chan : ERR_PTR(-EPROBE_DEFER);
658}
659
Dan Williams59b5ec22009-01-06 11:38:15 -0700660/**
Stefan Agner19d643d2015-06-01 23:53:43 +0200661 * dma_get_slave_channel - try to get specific channel exclusively
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800662 * @chan: target channel
663 */
664struct dma_chan *dma_get_slave_channel(struct dma_chan *chan)
665{
666 int err = -EBUSY;
667
668 /* lock against __dma_request_channel */
669 mutex_lock(&dma_list_mutex);
670
Vinod Kould9a6c8f2013-08-19 10:47:26 +0530671 if (chan->client_count == 0) {
Peter Ujfalusi214fc4e2015-09-24 12:03:35 +0300672 struct dma_device *device = chan->device;
673
674 dma_cap_set(DMA_PRIVATE, device->cap_mask);
675 device->privatecnt++;
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800676 err = dma_chan_get(chan);
Peter Ujfalusi214fc4e2015-09-24 12:03:35 +0300677 if (err) {
Jarkko Nikulaef859312016-03-14 16:51:09 +0200678 dev_dbg(chan->device->dev,
679 "%s: failed to get %s: (%d)\n",
Vinod Kould9a6c8f2013-08-19 10:47:26 +0530680 __func__, dma_chan_name(chan), err);
Peter Ujfalusi214fc4e2015-09-24 12:03:35 +0300681 chan = NULL;
682 if (--device->privatecnt == 0)
683 dma_cap_clear(DMA_PRIVATE, device->cap_mask);
684 }
Vinod Kould9a6c8f2013-08-19 10:47:26 +0530685 } else
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800686 chan = NULL;
687
688 mutex_unlock(&dma_list_mutex);
689
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800690
691 return chan;
692}
693EXPORT_SYMBOL_GPL(dma_get_slave_channel);
694
Stephen Warren8010dad2013-11-26 12:40:51 -0700695struct dma_chan *dma_get_any_slave_channel(struct dma_device *device)
696{
697 dma_cap_mask_t mask;
698 struct dma_chan *chan;
Stephen Warren8010dad2013-11-26 12:40:51 -0700699
700 dma_cap_zero(mask);
701 dma_cap_set(DMA_SLAVE, mask);
702
703 /* lock against __dma_request_channel */
704 mutex_lock(&dma_list_mutex);
705
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200706 chan = find_candidate(device, &mask, NULL, NULL);
Stephen Warren8010dad2013-11-26 12:40:51 -0700707
708 mutex_unlock(&dma_list_mutex);
709
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200710 return IS_ERR(chan) ? NULL : chan;
Stephen Warren8010dad2013-11-26 12:40:51 -0700711}
712EXPORT_SYMBOL_GPL(dma_get_any_slave_channel);
713
Zhangfei Gao7bb587f2013-06-28 20:39:12 +0800714/**
Daniel Mack6b9019a2013-08-14 18:35:03 +0200715 * __dma_request_channel - try to allocate an exclusive channel
Dan Williams59b5ec22009-01-06 11:38:15 -0700716 * @mask: capabilities that the channel must satisfy
717 * @fn: optional callback to disposition available channels
718 * @fn_param: opaque parameter to pass to dma_filter_fn
Baolin Wangf5151312019-05-20 19:32:14 +0800719 * @np: device node to look for DMA channels
Stephen Warren0ad7c002013-11-26 10:04:22 -0700720 *
721 * Returns pointer to appropriate DMA channel on success or NULL.
Dan Williams59b5ec22009-01-06 11:38:15 -0700722 */
Lars-Peter Clausena53e28d2013-03-25 13:23:52 +0100723struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
Baolin Wangf5151312019-05-20 19:32:14 +0800724 dma_filter_fn fn, void *fn_param,
725 struct device_node *np)
Dan Williams59b5ec22009-01-06 11:38:15 -0700726{
727 struct dma_device *device, *_d;
728 struct dma_chan *chan = NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700729
730 /* Find a channel */
731 mutex_lock(&dma_list_mutex);
732 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
Baolin Wangf5151312019-05-20 19:32:14 +0800733 /* Finds a DMA controller with matching device node */
734 if (np && device->dev->of_node && np != device->dev->of_node)
735 continue;
736
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200737 chan = find_candidate(device, mask, fn, fn_param);
738 if (!IS_ERR(chan))
739 break;
Dan Williams59b5ec22009-01-06 11:38:15 -0700740
Peter Ujfalusi7bd903c2015-12-14 22:47:39 +0200741 chan = NULL;
Dan Williams59b5ec22009-01-06 11:38:15 -0700742 }
743 mutex_unlock(&dma_list_mutex);
744
Jarkko Nikula4c4d7f872016-04-07 16:49:43 +0300745 pr_debug("%s: %s (%s)\n",
Joe Perches63433252012-07-18 09:51:28 -0700746 __func__,
747 chan ? "success" : "fail",
Dan Williams41d5e592009-01-06 11:38:21 -0700748 chan ? dma_chan_name(chan) : NULL);
Dan Williams59b5ec22009-01-06 11:38:15 -0700749
750 return chan;
751}
752EXPORT_SYMBOL_GPL(__dma_request_channel);
753
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200754static const struct dma_slave_map *dma_filter_match(struct dma_device *device,
755 const char *name,
756 struct device *dev)
757{
758 int i;
759
760 if (!device->filter.mapcnt)
761 return NULL;
762
763 for (i = 0; i < device->filter.mapcnt; i++) {
764 const struct dma_slave_map *map = &device->filter.map[i];
765
766 if (!strcmp(map->devname, dev_name(dev)) &&
767 !strcmp(map->slave, name))
768 return map;
769 }
770
771 return NULL;
772}
773
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500774/**
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200775 * dma_request_chan - try to allocate an exclusive slave channel
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500776 * @dev: pointer to client device structure
777 * @name: slave channel name
Stephen Warren0ad7c002013-11-26 10:04:22 -0700778 *
779 * Returns pointer to appropriate DMA channel on success or an error pointer.
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500780 */
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200781struct dma_chan *dma_request_chan(struct device *dev, const char *name)
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500782{
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200783 struct dma_device *d, *_d;
784 struct dma_chan *chan = NULL;
785
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500786 /* If device-tree is present get slave info from here */
787 if (dev->of_node)
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200788 chan = of_dma_request_slave_channel(dev->of_node, name);
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500789
Andy Shevchenko4e82f5d2013-04-09 14:05:44 +0300790 /* If device was enumerated by ACPI get slave info from here */
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200791 if (has_acpi_companion(dev) && !chan)
792 chan = acpi_dma_request_slave_chan_by_name(dev, name);
Andy Shevchenko4e82f5d2013-04-09 14:05:44 +0300793
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100794 if (PTR_ERR(chan) == -EPROBE_DEFER)
795 return chan;
796
797 if (!IS_ERR_OR_NULL(chan))
798 goto found;
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200799
800 /* Try to find the channel via the DMA filter map(s) */
801 mutex_lock(&dma_list_mutex);
802 list_for_each_entry_safe(d, _d, &dma_device_list, global_node) {
803 dma_cap_mask_t mask;
804 const struct dma_slave_map *map = dma_filter_match(d, name, dev);
805
806 if (!map)
807 continue;
808
809 dma_cap_zero(mask);
810 dma_cap_set(DMA_SLAVE, mask);
811
812 chan = find_candidate(d, &mask, d->filter.fn, map->param);
813 if (!IS_ERR(chan))
814 break;
815 }
816 mutex_unlock(&dma_list_mutex);
817
Peter Ujfalusibad83562020-01-31 11:38:58 +0200818 if (IS_ERR_OR_NULL(chan))
819 return chan ? chan : ERR_PTR(-EPROBE_DEFER);
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100820
821found:
Peter Ujfalusie937cc12020-03-06 16:28:37 +0200822#ifdef CONFIG_DEBUG_FS
823 chan->dbg_client_name = kasprintf(GFP_KERNEL, "%s:%s", dev_name(dev),
824 name);
825#endif
826
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100827 chan->name = kasprintf(GFP_KERNEL, "dma:%s", name);
828 if (!chan->name)
Peter Ujfalusibad83562020-01-31 11:38:58 +0200829 return chan;
830 chan->slave = dev;
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100831
832 if (sysfs_create_link(&chan->dev->device.kobj, &dev->kobj,
833 DMA_SLAVE_NAME))
Peter Ujfalusibad83562020-01-31 11:38:58 +0200834 dev_warn(dev, "Cannot create DMA %s symlink\n", DMA_SLAVE_NAME);
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100835 if (sysfs_create_link(&dev->kobj, &chan->dev->device.kobj, chan->name))
Peter Ujfalusibad83562020-01-31 11:38:58 +0200836 dev_warn(dev, "Cannot create DMA %s symlink\n", chan->name);
837
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100838 return chan;
Stephen Warren0ad7c002013-11-26 10:04:22 -0700839}
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200840EXPORT_SYMBOL_GPL(dma_request_chan);
Stephen Warren0ad7c002013-11-26 10:04:22 -0700841
842/**
843 * dma_request_slave_channel - try to allocate an exclusive slave channel
844 * @dev: pointer to client device structure
845 * @name: slave channel name
846 *
847 * Returns pointer to appropriate DMA channel on success or NULL.
848 */
849struct dma_chan *dma_request_slave_channel(struct device *dev,
850 const char *name)
851{
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200852 struct dma_chan *ch = dma_request_chan(dev, name);
Stephen Warren0ad7c002013-11-26 10:04:22 -0700853 if (IS_ERR(ch))
854 return NULL;
Robert Baldyga05aa1a72015-08-07 12:26:47 +0200855
Stephen Warren0ad7c002013-11-26 10:04:22 -0700856 return ch;
Jon Hunter9a6cecc2012-09-14 17:41:57 -0500857}
858EXPORT_SYMBOL_GPL(dma_request_slave_channel);
859
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200860/**
861 * dma_request_chan_by_mask - allocate a channel satisfying certain capabilities
862 * @mask: capabilities that the channel must satisfy
863 *
864 * Returns pointer to appropriate DMA channel on success or an error pointer.
865 */
866struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask)
867{
868 struct dma_chan *chan;
869
870 if (!mask)
871 return ERR_PTR(-ENODEV);
872
Baolin Wangf5151312019-05-20 19:32:14 +0800873 chan = __dma_request_channel(mask, NULL, NULL, NULL);
Peter Ujfalusiec8ca8e2018-07-18 12:29:57 +0300874 if (!chan) {
875 mutex_lock(&dma_list_mutex);
876 if (list_empty(&dma_device_list))
877 chan = ERR_PTR(-EPROBE_DEFER);
878 else
879 chan = ERR_PTR(-ENODEV);
880 mutex_unlock(&dma_list_mutex);
881 }
Peter Ujfalusia8135d02015-12-14 22:47:40 +0200882
883 return chan;
884}
885EXPORT_SYMBOL_GPL(dma_request_chan_by_mask);
886
Dan Williams59b5ec22009-01-06 11:38:15 -0700887void dma_release_channel(struct dma_chan *chan)
888{
889 mutex_lock(&dma_list_mutex);
890 WARN_ONCE(chan->client_count != 1,
891 "chan reference count %d != 1\n", chan->client_count);
892 dma_chan_put(chan);
Atsushi Nemoto0f571512009-03-06 20:07:14 +0900893 /* drop PRIVATE cap enabled by __dma_request_channel() */
894 if (--chan->device->privatecnt == 0)
895 dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
Peter Ujfalusibad83562020-01-31 11:38:58 +0200896
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100897 if (chan->slave) {
Peter Ujfalusibad83562020-01-31 11:38:58 +0200898 sysfs_remove_link(&chan->dev->device.kobj, DMA_SLAVE_NAME);
Geert Uytterhoeven71723a92020-01-17 16:30:56 +0100899 sysfs_remove_link(&chan->slave->kobj, chan->name);
900 kfree(chan->name);
901 chan->name = NULL;
902 chan->slave = NULL;
903 }
Peter Ujfalusie937cc12020-03-06 16:28:37 +0200904
905#ifdef CONFIG_DEBUG_FS
906 kfree(chan->dbg_client_name);
907 chan->dbg_client_name = NULL;
908#endif
Dan Williams59b5ec22009-01-06 11:38:15 -0700909 mutex_unlock(&dma_list_mutex);
910}
911EXPORT_SYMBOL_GPL(dma_release_channel);
912
Dan Williamsbec08512009-01-06 11:38:14 -0700913/**
Dan Williams209b84a2009-01-06 11:38:17 -0700914 * dmaengine_get - register interest in dma_channels
Chris Leechc13c8262006-05-23 17:18:44 -0700915 */
Dan Williams209b84a2009-01-06 11:38:17 -0700916void dmaengine_get(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700917{
Dan Williams6f49a572009-01-06 11:38:14 -0700918 struct dma_device *device, *_d;
919 struct dma_chan *chan;
920 int err;
921
Chris Leechc13c8262006-05-23 17:18:44 -0700922 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700923 dmaengine_ref_count++;
924
925 /* try to grab channels */
Dan Williams59b5ec22009-01-06 11:38:15 -0700926 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
927 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
928 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700929 list_for_each_entry(chan, &device->channels, device_node) {
930 err = dma_chan_get(chan);
931 if (err == -ENODEV) {
932 /* module removed before we could use it */
Dan Williams2ba05622009-01-06 11:38:14 -0700933 list_del_rcu(&device->global_node);
Dan Williams6f49a572009-01-06 11:38:14 -0700934 break;
935 } else if (err)
Jarkko Nikulaef859312016-03-14 16:51:09 +0200936 dev_dbg(chan->device->dev,
937 "%s: failed to get %s: (%d)\n",
938 __func__, dma_chan_name(chan), err);
Dan Williams6f49a572009-01-06 11:38:14 -0700939 }
Dan Williams59b5ec22009-01-06 11:38:15 -0700940 }
Dan Williams6f49a572009-01-06 11:38:14 -0700941
Dan Williamsbec08512009-01-06 11:38:14 -0700942 /* if this is the first reference and there were channels
943 * waiting we need to rebalance to get those channels
944 * incorporated into the channel table
945 */
946 if (dmaengine_ref_count == 1)
947 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700948 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700949}
Dan Williams209b84a2009-01-06 11:38:17 -0700950EXPORT_SYMBOL(dmaengine_get);
Chris Leechc13c8262006-05-23 17:18:44 -0700951
952/**
Dan Williams209b84a2009-01-06 11:38:17 -0700953 * dmaengine_put - let dma drivers be removed when ref_count == 0
Chris Leechc13c8262006-05-23 17:18:44 -0700954 */
Dan Williams209b84a2009-01-06 11:38:17 -0700955void dmaengine_put(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700956{
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700957 struct dma_device *device, *_d;
Chris Leechc13c8262006-05-23 17:18:44 -0700958 struct dma_chan *chan;
959
Chris Leechc13c8262006-05-23 17:18:44 -0700960 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700961 dmaengine_ref_count--;
962 BUG_ON(dmaengine_ref_count < 0);
963 /* drop channel references */
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -0700964 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
Dan Williams59b5ec22009-01-06 11:38:15 -0700965 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
966 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700967 list_for_each_entry(chan, &device->channels, device_node)
968 dma_chan_put(chan);
Dan Williams59b5ec22009-01-06 11:38:15 -0700969 }
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_put);
Chris Leechc13c8262006-05-23 17:18:44 -0700973
Dan Williams138f4c32009-09-08 17:42:51 -0700974static bool device_has_all_tx_types(struct dma_device *device)
975{
976 /* A device that satisfies this test has channels that will never cause
977 * an async_tx channel switch event as all possible operation types can
978 * be handled.
979 */
980 #ifdef CONFIG_ASYNC_TX_DMA
981 if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
982 return false;
983 #endif
984
Javier Martinez Canillasd57d3a42016-05-11 13:39:27 -0400985 #if IS_ENABLED(CONFIG_ASYNC_MEMCPY)
Dan Williams138f4c32009-09-08 17:42:51 -0700986 if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
987 return false;
988 #endif
989
Javier Martinez Canillasd57d3a42016-05-11 13:39:27 -0400990 #if IS_ENABLED(CONFIG_ASYNC_XOR)
Dan Williams138f4c32009-09-08 17:42:51 -0700991 if (!dma_has_cap(DMA_XOR, device->cap_mask))
992 return false;
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700993
994 #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
Dan Williams4499a242009-11-19 17:10:25 -0700995 if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
996 return false;
Dan Williams138f4c32009-09-08 17:42:51 -0700997 #endif
Dan Williams7b3cc2b2009-11-19 17:10:37 -0700998 #endif
Dan Williams138f4c32009-09-08 17:42:51 -0700999
Javier Martinez Canillasd57d3a42016-05-11 13:39:27 -04001000 #if IS_ENABLED(CONFIG_ASYNC_PQ)
Dan Williams138f4c32009-09-08 17:42:51 -07001001 if (!dma_has_cap(DMA_PQ, device->cap_mask))
1002 return false;
Dan Williams7b3cc2b2009-11-19 17:10:37 -07001003
1004 #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
Dan Williams4499a242009-11-19 17:10:25 -07001005 if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
1006 return false;
Dan Williams138f4c32009-09-08 17:42:51 -07001007 #endif
Dan Williams7b3cc2b2009-11-19 17:10:37 -07001008 #endif
Dan Williams138f4c32009-09-08 17:42:51 -07001009
1010 return true;
1011}
1012
Dan Williams257b17c2009-03-25 09:13:23 -07001013static int get_dma_id(struct dma_device *device)
1014{
Matthew Wilcox485258b2018-06-18 15:41:48 -04001015 int rc = ida_alloc(&dma_ida, GFP_KERNEL);
Dan Williams257b17c2009-03-25 09:13:23 -07001016
Matthew Wilcox485258b2018-06-18 15:41:48 -04001017 if (rc < 0)
1018 return rc;
1019 device->dev_id = rc;
1020 return 0;
Dan Williams257b17c2009-03-25 09:13:23 -07001021}
1022
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001023static int __dma_async_device_channel_register(struct dma_device *device,
1024 struct dma_chan *chan,
1025 int chan_id)
1026{
1027 int rc = 0;
1028 int chancnt = device->chancnt;
1029 atomic_t *idr_ref;
1030 struct dma_chan *tchan;
1031
1032 tchan = list_first_entry_or_null(&device->channels,
1033 struct dma_chan, device_node);
Dave Jiang5429b512020-01-31 10:58:39 -07001034 if (!tchan)
1035 return -ENODEV;
1036
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001037 if (tchan->dev) {
1038 idr_ref = tchan->dev->idr_ref;
1039 } else {
1040 idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
1041 if (!idr_ref)
1042 return -ENOMEM;
1043 atomic_set(idr_ref, 0);
1044 }
1045
1046 chan->local = alloc_percpu(typeof(*chan->local));
1047 if (!chan->local)
1048 goto err_out;
1049 chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
1050 if (!chan->dev) {
1051 free_percpu(chan->local);
1052 chan->local = NULL;
1053 goto err_out;
1054 }
1055
1056 /*
1057 * When the chan_id is a negative value, we are dynamically adding
1058 * the channel. Otherwise we are static enumerating.
1059 */
1060 chan->chan_id = chan_id < 0 ? chancnt : chan_id;
1061 chan->dev->device.class = &dma_devclass;
1062 chan->dev->device.parent = device->dev;
1063 chan->dev->chan = chan;
1064 chan->dev->idr_ref = idr_ref;
1065 chan->dev->dev_id = device->dev_id;
1066 atomic_inc(idr_ref);
1067 dev_set_name(&chan->dev->device, "dma%dchan%d",
1068 device->dev_id, chan->chan_id);
1069
1070 rc = device_register(&chan->dev->device);
1071 if (rc)
1072 goto err_out;
1073 chan->client_count = 0;
1074 device->chancnt = chan->chan_id + 1;
1075
1076 return 0;
1077
1078 err_out:
1079 free_percpu(chan->local);
1080 kfree(chan->dev);
1081 if (atomic_dec_return(idr_ref) == 0)
1082 kfree(idr_ref);
1083 return rc;
1084}
1085
Dave Jiange81274c2020-01-21 16:43:53 -07001086int dma_async_device_channel_register(struct dma_device *device,
1087 struct dma_chan *chan)
1088{
1089 int rc;
1090
1091 rc = __dma_async_device_channel_register(device, chan, -1);
1092 if (rc < 0)
1093 return rc;
1094
1095 dma_channel_rebalance();
1096 return 0;
1097}
1098EXPORT_SYMBOL_GPL(dma_async_device_channel_register);
1099
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001100static void __dma_async_device_channel_unregister(struct dma_device *device,
1101 struct dma_chan *chan)
1102{
1103 WARN_ONCE(!device->device_release && chan->client_count,
1104 "%s called while %d clients hold a reference\n",
1105 __func__, chan->client_count);
1106 mutex_lock(&dma_list_mutex);
Dave Jiange81274c2020-01-21 16:43:53 -07001107 list_del(&chan->device_node);
1108 device->chancnt--;
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001109 chan->dev->chan = NULL;
1110 mutex_unlock(&dma_list_mutex);
1111 device_unregister(&chan->dev->device);
1112 free_percpu(chan->local);
1113}
1114
Dave Jiange81274c2020-01-21 16:43:53 -07001115void dma_async_device_channel_unregister(struct dma_device *device,
1116 struct dma_chan *chan)
1117{
1118 __dma_async_device_channel_unregister(device, chan);
1119 dma_channel_rebalance();
1120}
1121EXPORT_SYMBOL_GPL(dma_async_device_channel_unregister);
1122
Chris Leechc13c8262006-05-23 17:18:44 -07001123/**
Randy Dunlap65088712006-07-03 19:45:31 -07001124 * dma_async_device_register - registers DMA devices found
Chris Leechc13c8262006-05-23 17:18:44 -07001125 * @device: &dma_device
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -07001126 *
1127 * After calling this routine the structure should not be freed except in the
1128 * device_release() callback which will be called after
1129 * dma_async_device_unregister() is called and no further references are taken.
Chris Leechc13c8262006-05-23 17:18:44 -07001130 */
1131int dma_async_device_register(struct dma_device *device)
1132{
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001133 int rc, i = 0;
Chris Leechc13c8262006-05-23 17:18:44 -07001134 struct dma_chan* chan;
1135
1136 if (!device)
1137 return -ENODEV;
1138
Dan Williams7405f742007-01-02 11:10:43 -07001139 /* validate device routines */
Vinod Koul3eeb5152017-08-27 16:55:32 +05301140 if (!device->dev) {
1141 pr_err("DMAdevice must have dev\n");
1142 return -EIO;
1143 }
Dan Williams7405f742007-01-02 11:10:43 -07001144
Logan Gunthorpedae7a582019-12-16 12:01:16 -07001145 device->owner = device->dev->driver->owner;
1146
Vinod Koul3eeb5152017-08-27 16:55:32 +05301147 if (dma_has_cap(DMA_MEMCPY, device->cap_mask) && !device->device_prep_dma_memcpy) {
1148 dev_err(device->dev,
1149 "Device claims capability %s, but op is not defined\n",
1150 "DMA_MEMCPY");
1151 return -EIO;
1152 }
1153
1154 if (dma_has_cap(DMA_XOR, device->cap_mask) && !device->device_prep_dma_xor) {
1155 dev_err(device->dev,
1156 "Device claims capability %s, but op is not defined\n",
1157 "DMA_XOR");
1158 return -EIO;
1159 }
1160
1161 if (dma_has_cap(DMA_XOR_VAL, device->cap_mask) && !device->device_prep_dma_xor_val) {
1162 dev_err(device->dev,
1163 "Device claims capability %s, but op is not defined\n",
1164 "DMA_XOR_VAL");
1165 return -EIO;
1166 }
1167
1168 if (dma_has_cap(DMA_PQ, device->cap_mask) && !device->device_prep_dma_pq) {
1169 dev_err(device->dev,
1170 "Device claims capability %s, but op is not defined\n",
1171 "DMA_PQ");
1172 return -EIO;
1173 }
1174
1175 if (dma_has_cap(DMA_PQ_VAL, device->cap_mask) && !device->device_prep_dma_pq_val) {
1176 dev_err(device->dev,
1177 "Device claims capability %s, but op is not defined\n",
1178 "DMA_PQ_VAL");
1179 return -EIO;
1180 }
1181
1182 if (dma_has_cap(DMA_MEMSET, device->cap_mask) && !device->device_prep_dma_memset) {
1183 dev_err(device->dev,
1184 "Device claims capability %s, but op is not defined\n",
1185 "DMA_MEMSET");
1186 return -EIO;
1187 }
1188
1189 if (dma_has_cap(DMA_INTERRUPT, device->cap_mask) && !device->device_prep_dma_interrupt) {
1190 dev_err(device->dev,
1191 "Device claims capability %s, but op is not defined\n",
1192 "DMA_INTERRUPT");
1193 return -EIO;
1194 }
1195
1196 if (dma_has_cap(DMA_CYCLIC, device->cap_mask) && !device->device_prep_dma_cyclic) {
1197 dev_err(device->dev,
1198 "Device claims capability %s, but op is not defined\n",
1199 "DMA_CYCLIC");
1200 return -EIO;
1201 }
1202
1203 if (dma_has_cap(DMA_INTERLEAVE, device->cap_mask) && !device->device_prep_interleaved_dma) {
1204 dev_err(device->dev,
1205 "Device claims capability %s, but op is not defined\n",
1206 "DMA_INTERLEAVE");
1207 return -EIO;
1208 }
1209
1210
1211 if (!device->device_tx_status) {
1212 dev_err(device->dev, "Device tx_status is not defined\n");
1213 return -EIO;
1214 }
1215
1216
1217 if (!device->device_issue_pending) {
1218 dev_err(device->dev, "Device issue_pending is not defined\n");
1219 return -EIO;
1220 }
Dan Williams7405f742007-01-02 11:10:43 -07001221
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -07001222 if (!device->device_release)
1223 dev_warn(device->dev,
1224 "WARN: Device release is not defined so it is not safe to unbind this driver while in use\n");
1225
1226 kref_init(&device->ref);
1227
Dan Williams138f4c32009-09-08 17:42:51 -07001228 /* note: this only matters in the
Dan Williams5fc6d892010-10-07 16:44:50 -07001229 * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
Dan Williams138f4c32009-09-08 17:42:51 -07001230 */
1231 if (device_has_all_tx_types(device))
1232 dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
1233
Dan Williams257b17c2009-03-25 09:13:23 -07001234 rc = get_dma_id(device);
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001235 if (rc != 0)
Dan Williams864498a2009-01-06 11:38:21 -07001236 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -07001237
1238 /* represent channels in sysfs. Probably want devs too */
1239 list_for_each_entry(chan, &device->channels, device_node) {
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001240 rc = __dma_async_device_channel_register(device, chan, i++);
1241 if (rc < 0)
Dan Williams257b17c2009-03-25 09:13:23 -07001242 goto err_out;
Chris Leechc13c8262006-05-23 17:18:44 -07001243 }
Viresh Kumar76d7b842016-07-27 14:32:58 -07001244
Chris Leechc13c8262006-05-23 17:18:44 -07001245 mutex_lock(&dma_list_mutex);
Dan Williams59b5ec22009-01-06 11:38:15 -07001246 /* take references on public channels */
1247 if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
Dan Williams6f49a572009-01-06 11:38:14 -07001248 list_for_each_entry(chan, &device->channels, device_node) {
1249 /* if clients are already waiting for channels we need
1250 * to take references on their behalf
1251 */
1252 if (dma_chan_get(chan) == -ENODEV) {
1253 /* note we can only get here for the first
1254 * channel as the remaining channels are
1255 * guaranteed to get a reference
1256 */
1257 rc = -ENODEV;
1258 mutex_unlock(&dma_list_mutex);
1259 goto err_out;
1260 }
1261 }
Dan Williams2ba05622009-01-06 11:38:14 -07001262 list_add_tail_rcu(&device->global_node, &dma_device_list);
Atsushi Nemoto0f571512009-03-06 20:07:14 +09001263 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
1264 device->privatecnt++; /* Always private */
Dan Williamsbec08512009-01-06 11:38:14 -07001265 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -07001266 mutex_unlock(&dma_list_mutex);
1267
Chris Leechc13c8262006-05-23 17:18:44 -07001268 return 0;
Jeff Garzikff487fb2007-03-08 09:57:34 -08001269
1270err_out:
Dan Williams257b17c2009-03-25 09:13:23 -07001271 /* if we never registered a channel just release the idr */
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001272 if (!device->chancnt) {
Matthew Wilcox485258b2018-06-18 15:41:48 -04001273 ida_free(&dma_ida, device->dev_id);
Dan Williams257b17c2009-03-25 09:13:23 -07001274 return rc;
1275 }
1276
Jeff Garzikff487fb2007-03-08 09:57:34 -08001277 list_for_each_entry(chan, &device->channels, device_node) {
1278 if (chan->local == NULL)
1279 continue;
Dan Williams41d5e592009-01-06 11:38:21 -07001280 mutex_lock(&dma_list_mutex);
1281 chan->dev->chan = NULL;
1282 mutex_unlock(&dma_list_mutex);
1283 device_unregister(&chan->dev->device);
Jeff Garzikff487fb2007-03-08 09:57:34 -08001284 free_percpu(chan->local);
1285 }
1286 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -07001287}
David Brownell765e3d82007-03-16 13:38:05 -08001288EXPORT_SYMBOL(dma_async_device_register);
Chris Leechc13c8262006-05-23 17:18:44 -07001289
1290/**
Dan Williams6f49a572009-01-06 11:38:14 -07001291 * dma_async_device_unregister - unregister a DMA device
Randy Dunlap65088712006-07-03 19:45:31 -07001292 * @device: &dma_device
Dan Williamsf27c5802009-01-06 11:38:18 -07001293 *
1294 * This routine is called by dma driver exit routines, dmaengine holds module
1295 * references to prevent it being called while channels are in use.
Randy Dunlap65088712006-07-03 19:45:31 -07001296 */
1297void dma_async_device_unregister(struct dma_device *device)
Chris Leechc13c8262006-05-23 17:18:44 -07001298{
Dave Jiange81274c2020-01-21 16:43:53 -07001299 struct dma_chan *chan, *n;
Chris Leechc13c8262006-05-23 17:18:44 -07001300
Dave Jiange81274c2020-01-21 16:43:53 -07001301 list_for_each_entry_safe(chan, n, &device->channels, device_node)
Dave Jiangd2fb0a02020-01-21 16:43:47 -07001302 __dma_async_device_channel_unregister(device, chan);
Logan Gunthorpe8ad342a2019-12-16 12:01:19 -07001303
1304 mutex_lock(&dma_list_mutex);
1305 /*
1306 * setting DMA_PRIVATE ensures the device being torn down will not
1307 * be used in the channel_table
1308 */
1309 dma_cap_set(DMA_PRIVATE, device->cap_mask);
1310 dma_channel_rebalance();
1311 dma_device_put(device);
1312 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -07001313}
David Brownell765e3d82007-03-16 13:38:05 -08001314EXPORT_SYMBOL(dma_async_device_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -07001315
Huang Shijief39b9482018-07-26 14:45:53 +08001316static void dmam_device_release(struct device *dev, void *res)
1317{
1318 struct dma_device *device;
1319
1320 device = *(struct dma_device **)res;
1321 dma_async_device_unregister(device);
1322}
1323
1324/**
1325 * dmaenginem_async_device_register - registers DMA devices found
1326 * @device: &dma_device
1327 *
1328 * The operation is managed and will be undone on driver detach.
1329 */
1330int dmaenginem_async_device_register(struct dma_device *device)
1331{
1332 void *p;
1333 int ret;
1334
1335 p = devres_alloc(dmam_device_release, sizeof(void *), GFP_KERNEL);
1336 if (!p)
1337 return -ENOMEM;
1338
1339 ret = dma_async_device_register(device);
1340 if (!ret) {
1341 *(struct dma_device **)p = device;
1342 devres_add(device->dev, p);
1343 } else {
1344 devres_free(p);
1345 }
1346
1347 return ret;
1348}
1349EXPORT_SYMBOL(dmaenginem_async_device_register);
1350
Dan Williams45c463a2013-10-18 19:35:24 +02001351struct dmaengine_unmap_pool {
1352 struct kmem_cache *cache;
1353 const char *name;
1354 mempool_t *pool;
1355 size_t size;
1356};
1357
1358#define __UNMAP_POOL(x) { .size = x, .name = "dmaengine-unmap-" __stringify(x) }
1359static struct dmaengine_unmap_pool unmap_pool[] = {
1360 __UNMAP_POOL(2),
Dan Williams3cc377b2013-12-09 10:33:16 -08001361 #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
Dan Williams45c463a2013-10-18 19:35:24 +02001362 __UNMAP_POOL(16),
1363 __UNMAP_POOL(128),
1364 __UNMAP_POOL(256),
1365 #endif
1366};
1367
1368static struct dmaengine_unmap_pool *__get_unmap_pool(int nr)
Dan Williams7405f742007-01-02 11:10:43 -07001369{
Dan Williams45c463a2013-10-18 19:35:24 +02001370 int order = get_count_order(nr);
Dan Williams7405f742007-01-02 11:10:43 -07001371
Dan Williams45c463a2013-10-18 19:35:24 +02001372 switch (order) {
1373 case 0 ... 1:
1374 return &unmap_pool[0];
Matthias Kaehlcke23f963e92017-03-13 14:30:29 -07001375#if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
Dan Williams45c463a2013-10-18 19:35:24 +02001376 case 2 ... 4:
1377 return &unmap_pool[1];
1378 case 5 ... 7:
1379 return &unmap_pool[2];
1380 case 8:
1381 return &unmap_pool[3];
Matthias Kaehlcke23f963e92017-03-13 14:30:29 -07001382#endif
Dan Williams45c463a2013-10-18 19:35:24 +02001383 default:
1384 BUG();
1385 return NULL;
1386 }
1387}
Dan Williams00367312008-02-02 19:49:57 -07001388
Dan Williams45c463a2013-10-18 19:35:24 +02001389static void dmaengine_unmap(struct kref *kref)
1390{
1391 struct dmaengine_unmap_data *unmap = container_of(kref, typeof(*unmap), kref);
1392 struct device *dev = unmap->dev;
1393 int cnt, i;
1394
1395 cnt = unmap->to_cnt;
1396 for (i = 0; i < cnt; i++)
1397 dma_unmap_page(dev, unmap->addr[i], unmap->len,
1398 DMA_TO_DEVICE);
1399 cnt += unmap->from_cnt;
1400 for (; i < cnt; i++)
1401 dma_unmap_page(dev, unmap->addr[i], unmap->len,
1402 DMA_FROM_DEVICE);
1403 cnt += unmap->bidi_cnt;
Dan Williams7476bd792013-10-18 19:35:29 +02001404 for (; i < cnt; i++) {
1405 if (unmap->addr[i] == 0)
1406 continue;
Dan Williams45c463a2013-10-18 19:35:24 +02001407 dma_unmap_page(dev, unmap->addr[i], unmap->len,
1408 DMA_BIDIRECTIONAL);
Dan Williams7476bd792013-10-18 19:35:29 +02001409 }
Xuelin Shic1f43dd2014-05-21 14:02:37 -07001410 cnt = unmap->map_cnt;
Dan Williams45c463a2013-10-18 19:35:24 +02001411 mempool_free(unmap, __get_unmap_pool(cnt)->pool);
1412}
1413
1414void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
1415{
1416 if (unmap)
1417 kref_put(&unmap->kref, dmaengine_unmap);
1418}
1419EXPORT_SYMBOL_GPL(dmaengine_unmap_put);
1420
1421static void dmaengine_destroy_unmap_pool(void)
1422{
1423 int i;
1424
1425 for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
1426 struct dmaengine_unmap_pool *p = &unmap_pool[i];
1427
Julia Lawall240eb9162015-09-13 14:15:19 +02001428 mempool_destroy(p->pool);
Dan Williams45c463a2013-10-18 19:35:24 +02001429 p->pool = NULL;
Julia Lawall240eb9162015-09-13 14:15:19 +02001430 kmem_cache_destroy(p->cache);
Dan Williams45c463a2013-10-18 19:35:24 +02001431 p->cache = NULL;
1432 }
1433}
1434
1435static int __init dmaengine_init_unmap_pool(void)
1436{
1437 int i;
1438
1439 for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
1440 struct dmaengine_unmap_pool *p = &unmap_pool[i];
1441 size_t size;
1442
1443 size = sizeof(struct dmaengine_unmap_data) +
1444 sizeof(dma_addr_t) * p->size;
1445
1446 p->cache = kmem_cache_create(p->name, size, 0,
1447 SLAB_HWCACHE_ALIGN, NULL);
1448 if (!p->cache)
1449 break;
1450 p->pool = mempool_create_slab_pool(1, p->cache);
1451 if (!p->pool)
1452 break;
Dan Williams00367312008-02-02 19:49:57 -07001453 }
Dan Williams7405f742007-01-02 11:10:43 -07001454
Dan Williams45c463a2013-10-18 19:35:24 +02001455 if (i == ARRAY_SIZE(unmap_pool))
1456 return 0;
Dan Williams7405f742007-01-02 11:10:43 -07001457
Dan Williams45c463a2013-10-18 19:35:24 +02001458 dmaengine_destroy_unmap_pool();
1459 return -ENOMEM;
Dan Williams7405f742007-01-02 11:10:43 -07001460}
Dan Williams7405f742007-01-02 11:10:43 -07001461
Dan Williams89716462013-10-18 19:35:25 +02001462struct dmaengine_unmap_data *
Dan Williams45c463a2013-10-18 19:35:24 +02001463dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
Dan Williams7405f742007-01-02 11:10:43 -07001464{
Dan Williams45c463a2013-10-18 19:35:24 +02001465 struct dmaengine_unmap_data *unmap;
Dan Williams7405f742007-01-02 11:10:43 -07001466
Dan Williams45c463a2013-10-18 19:35:24 +02001467 unmap = mempool_alloc(__get_unmap_pool(nr)->pool, flags);
1468 if (!unmap)
1469 return NULL;
Dan Williams00367312008-02-02 19:49:57 -07001470
Dan Williams45c463a2013-10-18 19:35:24 +02001471 memset(unmap, 0, sizeof(*unmap));
1472 kref_init(&unmap->kref);
1473 unmap->dev = dev;
Xuelin Shic1f43dd2014-05-21 14:02:37 -07001474 unmap->map_cnt = nr;
Dan Williams7405f742007-01-02 11:10:43 -07001475
Dan Williams45c463a2013-10-18 19:35:24 +02001476 return unmap;
Dan Williams7405f742007-01-02 11:10:43 -07001477}
Dan Williams89716462013-10-18 19:35:25 +02001478EXPORT_SYMBOL(dmaengine_get_unmap_data);
Dan Williams7405f742007-01-02 11:10:43 -07001479
Dan Williams7405f742007-01-02 11:10:43 -07001480void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1481 struct dma_chan *chan)
1482{
1483 tx->chan = chan;
Dan Williams5fc6d892010-10-07 16:44:50 -07001484 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
Dan Williams7405f742007-01-02 11:10:43 -07001485 spin_lock_init(&tx->lock);
Dan Williamscaa20d972010-05-17 16:24:16 -07001486 #endif
Dan Williams7405f742007-01-02 11:10:43 -07001487}
1488EXPORT_SYMBOL(dma_async_tx_descriptor_init);
1489
Peter Ujfalusi4db8fd32019-12-23 13:04:44 +02001490static inline int desc_check_and_set_metadata_mode(
1491 struct dma_async_tx_descriptor *desc, enum dma_desc_metadata_mode mode)
1492{
1493 /* Make sure that the metadata mode is not mixed */
1494 if (!desc->desc_metadata_mode) {
1495 if (dmaengine_is_metadata_mode_supported(desc->chan, mode))
1496 desc->desc_metadata_mode = mode;
1497 else
1498 return -ENOTSUPP;
1499 } else if (desc->desc_metadata_mode != mode) {
1500 return -EINVAL;
1501 }
1502
1503 return 0;
1504}
1505
1506int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
1507 void *data, size_t len)
1508{
1509 int ret;
1510
1511 if (!desc)
1512 return -EINVAL;
1513
1514 ret = desc_check_and_set_metadata_mode(desc, DESC_METADATA_CLIENT);
1515 if (ret)
1516 return ret;
1517
1518 if (!desc->metadata_ops || !desc->metadata_ops->attach)
1519 return -ENOTSUPP;
1520
1521 return desc->metadata_ops->attach(desc, data, len);
1522}
1523EXPORT_SYMBOL_GPL(dmaengine_desc_attach_metadata);
1524
1525void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
1526 size_t *payload_len, size_t *max_len)
1527{
1528 int ret;
1529
1530 if (!desc)
1531 return ERR_PTR(-EINVAL);
1532
1533 ret = desc_check_and_set_metadata_mode(desc, DESC_METADATA_ENGINE);
1534 if (ret)
1535 return ERR_PTR(ret);
1536
1537 if (!desc->metadata_ops || !desc->metadata_ops->get_ptr)
1538 return ERR_PTR(-ENOTSUPP);
1539
1540 return desc->metadata_ops->get_ptr(desc, payload_len, max_len);
1541}
1542EXPORT_SYMBOL_GPL(dmaengine_desc_get_metadata_ptr);
1543
1544int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
1545 size_t payload_len)
1546{
1547 int ret;
1548
1549 if (!desc)
1550 return -EINVAL;
1551
1552 ret = desc_check_and_set_metadata_mode(desc, DESC_METADATA_ENGINE);
1553 if (ret)
1554 return ret;
1555
1556 if (!desc->metadata_ops || !desc->metadata_ops->set_len)
1557 return -ENOTSUPP;
1558
1559 return desc->metadata_ops->set_len(desc, payload_len);
1560}
1561EXPORT_SYMBOL_GPL(dmaengine_desc_set_metadata_len);
1562
Dan Williams07f22112009-01-05 17:14:31 -07001563/* dma_wait_for_async_tx - spin wait for a transaction to complete
1564 * @tx: in-flight transaction to wait on
Dan Williams07f22112009-01-05 17:14:31 -07001565 */
1566enum dma_status
1567dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1568{
Dan Williams95475e52009-07-14 12:19:02 -07001569 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
Dan Williams07f22112009-01-05 17:14:31 -07001570
1571 if (!tx)
Vinod Kouladfedd92013-10-16 13:29:02 +05301572 return DMA_COMPLETE;
Dan Williams07f22112009-01-05 17:14:31 -07001573
Dan Williams95475e52009-07-14 12:19:02 -07001574 while (tx->cookie == -EBUSY) {
1575 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
Jarkko Nikulaef859312016-03-14 16:51:09 +02001576 dev_err(tx->chan->device->dev,
1577 "%s timeout waiting for descriptor submission\n",
1578 __func__);
Dan Williams95475e52009-07-14 12:19:02 -07001579 return DMA_ERROR;
1580 }
1581 cpu_relax();
1582 }
1583 return dma_sync_wait(tx->chan, tx->cookie);
Dan Williams07f22112009-01-05 17:14:31 -07001584}
1585EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
1586
1587/* dma_run_dependencies - helper routine for dma drivers to process
1588 * (start) dependent operations on their target channel
1589 * @tx: transaction with dependencies
1590 */
1591void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
1592{
Dan Williamscaa20d972010-05-17 16:24:16 -07001593 struct dma_async_tx_descriptor *dep = txd_next(tx);
Dan Williams07f22112009-01-05 17:14:31 -07001594 struct dma_async_tx_descriptor *dep_next;
1595 struct dma_chan *chan;
1596
1597 if (!dep)
1598 return;
1599
Yuri Tikhonovdd59b852009-01-12 15:17:20 -07001600 /* we'll submit tx->next now, so clear the link */
Dan Williamscaa20d972010-05-17 16:24:16 -07001601 txd_clear_next(tx);
Dan Williams07f22112009-01-05 17:14:31 -07001602 chan = dep->chan;
1603
1604 /* keep submitting up until a channel switch is detected
1605 * in that case we will be called again as a result of
1606 * processing the interrupt from async_tx_channel_switch
1607 */
1608 for (; dep; dep = dep_next) {
Dan Williamscaa20d972010-05-17 16:24:16 -07001609 txd_lock(dep);
1610 txd_clear_parent(dep);
1611 dep_next = txd_next(dep);
Dan Williams07f22112009-01-05 17:14:31 -07001612 if (dep_next && dep_next->chan == chan)
Dan Williamscaa20d972010-05-17 16:24:16 -07001613 txd_clear_next(dep); /* ->next will be submitted */
Dan Williams07f22112009-01-05 17:14:31 -07001614 else
1615 dep_next = NULL; /* submit current dep and terminate */
Dan Williamscaa20d972010-05-17 16:24:16 -07001616 txd_unlock(dep);
Dan Williams07f22112009-01-05 17:14:31 -07001617
1618 dep->tx_submit(dep);
1619 }
1620
1621 chan->device->device_issue_pending(chan);
1622}
1623EXPORT_SYMBOL_GPL(dma_run_dependencies);
1624
Chris Leechc13c8262006-05-23 17:18:44 -07001625static int __init dma_bus_init(void)
1626{
Dan Williams45c463a2013-10-18 19:35:24 +02001627 int err = dmaengine_init_unmap_pool();
1628
1629 if (err)
1630 return err;
Peter Ujfalusie937cc12020-03-06 16:28:37 +02001631
1632 err = class_register(&dma_devclass);
1633 if (!err)
1634 dmaengine_debugfs_init();
1635
1636 return err;
Chris Leechc13c8262006-05-23 17:18:44 -07001637}
Dan Williams652afc22009-01-06 11:38:22 -07001638arch_initcall(dma_bus_init);