Thomas Gleixner | 9ab65af | 2019-05-19 15:51:37 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved. |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * This code implements the DMA subsystem. It provides a HW-neutral interface |
| 8 | * for other kernel code to use asynchronous memory copy capabilities, |
| 9 | * if present, and allows different HW DMA drivers to register as providing |
| 10 | * this capability. |
| 11 | * |
| 12 | * Due to the fact we are accelerating what is already a relatively fast |
| 13 | * operation, the code goes to great lengths to avoid additional overhead, |
| 14 | * such as locking. |
| 15 | * |
| 16 | * LOCKING: |
| 17 | * |
Dan Williams | aa1e6f1 | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 18 | * The subsystem keeps a global list of dma_device structs it is protected by a |
| 19 | * mutex, dma_list_mutex. |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 20 | * |
Dan Williams | f27c580 | 2009-01-06 11:38:18 -0700 | [diff] [blame] | 21 | * A subsystem can get access to a channel by calling dmaengine_get() followed |
| 22 | * by dma_find_channel(), or if it has need for an exclusive channel it can call |
| 23 | * dma_request_channel(). Once a channel is allocated a reference is taken |
| 24 | * against its corresponding driver to disable removal. |
| 25 | * |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 26 | * Each device has a channels list, which runs unlocked but is never modified |
| 27 | * once the device is registered, it's just setup by the driver. |
| 28 | * |
Mauro Carvalho Chehab | 44348e8a | 2018-06-14 12:34:32 -0300 | [diff] [blame] | 29 | * See Documentation/driver-api/dmaengine for more details |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 30 | */ |
| 31 | |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 33 | |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 34 | #include <linux/platform_device.h> |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 35 | #include <linux/dma-mapping.h> |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 36 | #include <linux/init.h> |
| 37 | #include <linux/module.h> |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 38 | #include <linux/mm.h> |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 39 | #include <linux/device.h> |
| 40 | #include <linux/dmaengine.h> |
| 41 | #include <linux/hardirq.h> |
| 42 | #include <linux/spinlock.h> |
| 43 | #include <linux/percpu.h> |
| 44 | #include <linux/rcupdate.h> |
| 45 | #include <linux/mutex.h> |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 46 | #include <linux/jiffies.h> |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 47 | #include <linux/rculist.h> |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 48 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 49 | #include <linux/slab.h> |
Andy Shevchenko | 4e82f5d | 2013-04-09 14:05:44 +0300 | [diff] [blame] | 50 | #include <linux/acpi.h> |
| 51 | #include <linux/acpi_dma.h> |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 52 | #include <linux/of_dma.h> |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 53 | #include <linux/mempool.h> |
Anshuman Khandual | 98fa15f | 2019-03-05 15:42:58 -0800 | [diff] [blame] | 54 | #include <linux/numa.h> |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 55 | |
| 56 | static DEFINE_MUTEX(dma_list_mutex); |
Matthew Wilcox | adc064c | 2016-12-15 08:57:51 -0800 | [diff] [blame] | 57 | static DEFINE_IDA(dma_ida); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 58 | static LIST_HEAD(dma_device_list); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 59 | static long dmaengine_ref_count; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 60 | |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame] | 61 | /* --- debugfs implementation --- */ |
| 62 | #ifdef CONFIG_DEBUG_FS |
| 63 | #include <linux/debugfs.h> |
| 64 | |
Peter Ujfalusi | 26cf132 | 2020-03-06 16:28:39 +0200 | [diff] [blame^] | 65 | static struct dentry *rootdir; |
| 66 | |
| 67 | static 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 | |
| 75 | static 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 Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame] | 81 | static 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 | |
| 100 | static 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 | } |
| 122 | DEFINE_SHOW_ATTRIBUTE(dmaengine_summary); |
| 123 | |
| 124 | static void __init dmaengine_debugfs_init(void) |
| 125 | { |
Peter Ujfalusi | 26cf132 | 2020-03-06 16:28:39 +0200 | [diff] [blame^] | 126 | rootdir = debugfs_create_dir("dmaengine", NULL); |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame] | 127 | |
| 128 | /* /sys/kernel/debug/dmaengine/summary */ |
| 129 | debugfs_create_file("summary", 0444, rootdir, NULL, |
| 130 | &dmaengine_summary_fops); |
| 131 | } |
| 132 | #else |
| 133 | static inline void dmaengine_debugfs_init(void) { } |
Peter Ujfalusi | 26cf132 | 2020-03-06 16:28:39 +0200 | [diff] [blame^] | 134 | static inline int dmaengine_debug_register(struct dma_device *dma_dev) |
| 135 | { |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static inline void dmaengine_debug_unregister(struct dma_device *dma_dev) { } |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame] | 140 | #endif /* DEBUG_FS */ |
| 141 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 142 | /* --- sysfs implementation --- */ |
| 143 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 144 | #define DMA_SLAVE_NAME "slave" |
| 145 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 146 | /** |
Geert Uytterhoeven | fe33338 | 2019-06-07 13:30:39 +0200 | [diff] [blame] | 147 | * dev_to_dma_chan - convert a device pointer to its sysfs container object |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 148 | * @dev - device node |
| 149 | * |
| 150 | * Must be called under dma_list_mutex |
| 151 | */ |
| 152 | static 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-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 160 | static ssize_t memcpy_count_show(struct device *dev, |
| 161 | struct device_attribute *attr, char *buf) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 162 | { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 163 | struct dma_chan *chan; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 164 | unsigned long count = 0; |
| 165 | int i; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 166 | int err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 167 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 168 | mutex_lock(&dma_list_mutex); |
| 169 | chan = dev_to_dma_chan(dev); |
| 170 | if (chan) { |
| 171 | for_each_possible_cpu(i) |
| 172 | count += per_cpu_ptr(chan->local, i)->memcpy_count; |
| 173 | err = sprintf(buf, "%lu\n", count); |
| 174 | } else |
| 175 | err = -ENODEV; |
| 176 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 177 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 178 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 179 | } |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 180 | static DEVICE_ATTR_RO(memcpy_count); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 181 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 182 | static ssize_t bytes_transferred_show(struct device *dev, |
| 183 | struct device_attribute *attr, char *buf) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 184 | { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 185 | struct dma_chan *chan; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 186 | unsigned long count = 0; |
| 187 | int i; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 188 | int err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 189 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 190 | mutex_lock(&dma_list_mutex); |
| 191 | chan = dev_to_dma_chan(dev); |
| 192 | if (chan) { |
| 193 | for_each_possible_cpu(i) |
| 194 | count += per_cpu_ptr(chan->local, i)->bytes_transferred; |
| 195 | err = sprintf(buf, "%lu\n", count); |
| 196 | } else |
| 197 | err = -ENODEV; |
| 198 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 199 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 200 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 201 | } |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 202 | static DEVICE_ATTR_RO(bytes_transferred); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 203 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 204 | static ssize_t in_use_show(struct device *dev, struct device_attribute *attr, |
| 205 | char *buf) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 206 | { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 207 | struct dma_chan *chan; |
| 208 | int err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 209 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 210 | mutex_lock(&dma_list_mutex); |
| 211 | chan = dev_to_dma_chan(dev); |
| 212 | if (chan) |
| 213 | err = sprintf(buf, "%d\n", chan->client_count); |
| 214 | else |
| 215 | err = -ENODEV; |
| 216 | mutex_unlock(&dma_list_mutex); |
| 217 | |
| 218 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 219 | } |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 220 | static DEVICE_ATTR_RO(in_use); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 221 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 222 | static 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 Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 227 | }; |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 228 | ATTRIBUTE_GROUPS(dma_dev); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 229 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 230 | static 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 Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 235 | if (atomic_dec_and_test(chan_dev->idr_ref)) { |
Matthew Wilcox | 485258b | 2018-06-18 15:41:48 -0400 | [diff] [blame] | 236 | ida_free(&dma_ida, chan_dev->dev_id); |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 237 | kfree(chan_dev->idr_ref); |
| 238 | } |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 239 | kfree(chan_dev); |
| 240 | } |
| 241 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 242 | static struct class dma_devclass = { |
Tony Jones | 891f78e | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 243 | .name = "dma", |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 244 | .dev_groups = dma_dev_groups, |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 245 | .dev_release = chan_dev_release, |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | /* --- client and device registration --- */ |
| 249 | |
Logan Gunthorpe | 11a0fd2 | 2019-12-16 12:01:18 -0700 | [diff] [blame] | 250 | /** |
| 251 | * dma_cap_mask_all - enable iteration over all operation types |
| 252 | */ |
| 253 | static 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 | */ |
| 259 | struct 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 | */ |
| 266 | static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END]; |
| 267 | |
| 268 | static 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 Koul | 08baca4 | 2019-12-24 10:26:14 +0530 | [diff] [blame] | 292 | pr_err("dmaengine dma_channel_table_init failure: %d\n", err); |
Logan Gunthorpe | 11a0fd2 | 2019-12-16 12:01:18 -0700 | [diff] [blame] | 293 | for_each_dma_cap_mask(cap, dma_cap_mask_all) |
| 294 | free_percpu(channel_table[cap]); |
| 295 | } |
| 296 | |
| 297 | return err; |
| 298 | } |
| 299 | arch_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 | */ |
| 305 | static 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 | */ |
| 323 | static 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 | */ |
| 363 | static 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 Uytterhoeven | 69b1189 | 2020-01-21 10:33:09 +0100 | [diff] [blame] | 394 | static int dma_device_satisfies_mask(struct dma_device *device, |
| 395 | const dma_cap_mask_t *want) |
Dan Williams | d379b01 | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 396 | { |
| 397 | dma_cap_mask_t has; |
| 398 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 399 | bitmap_and(has.bits, want->bits, device->cap_mask.bits, |
Dan Williams | d379b01 | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 400 | DMA_TX_TYPE_END); |
| 401 | return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END); |
| 402 | } |
| 403 | |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 404 | static struct module *dma_chan_to_owner(struct dma_chan *chan) |
| 405 | { |
Logan Gunthorpe | dae7a58 | 2019-12-16 12:01:16 -0700 | [diff] [blame] | 406 | return chan->device->owner; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /** |
| 410 | * balance_ref_count - catch up the channel reference count |
| 411 | * @chan - channel to balance ->client_count versus dmaengine_ref_count |
| 412 | * |
| 413 | * balance_ref_count must be called under dma_list_mutex |
| 414 | */ |
| 415 | static 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 Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 425 | static 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 | |
| 436 | static 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 Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 442 | /** |
| 443 | * dma_chan_get - try to grab a dma channel's parent driver module |
| 444 | * @chan - channel to grab |
| 445 | * |
| 446 | * Must be called under dma_list_mutex |
| 447 | */ |
| 448 | static int dma_chan_get(struct dma_chan *chan) |
| 449 | { |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 450 | struct module *owner = dma_chan_to_owner(chan); |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 451 | int ret; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 452 | |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 453 | /* The channel is already in use, update client count */ |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 454 | if (chan->client_count) { |
| 455 | __module_get(owner); |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 456 | goto out; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 459 | if (!try_module_get(owner)) |
| 460 | return -ENODEV; |
| 461 | |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 462 | ret = kref_get_unless_zero(&chan->device->ref); |
| 463 | if (!ret) { |
| 464 | ret = -ENODEV; |
| 465 | goto module_put_out; |
| 466 | } |
| 467 | |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 468 | /* allocate upon first client reference */ |
Maxime Ripard | c4b54a6 | 2014-11-17 14:41:59 +0100 | [diff] [blame] | 469 | if (chan->device->device_alloc_chan_resources) { |
| 470 | ret = chan->device->device_alloc_chan_resources(chan); |
| 471 | if (ret < 0) |
| 472 | goto err_out; |
| 473 | } |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 474 | |
| 475 | if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask)) |
| 476 | balance_ref_count(chan); |
| 477 | |
| 478 | out: |
| 479 | chan->client_count++; |
| 480 | return 0; |
| 481 | |
| 482 | err_out: |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 483 | dma_device_put(chan->device); |
| 484 | module_put_out: |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 485 | module_put(owner); |
| 486 | return ret; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** |
| 490 | * dma_chan_put - drop a reference to a dma channel's parent driver module |
| 491 | * @chan - channel to release |
| 492 | * |
| 493 | * Must be called under dma_list_mutex |
| 494 | */ |
| 495 | static void dma_chan_put(struct dma_chan *chan) |
| 496 | { |
Maxime Ripard | c4b54a6 | 2014-11-17 14:41:59 +0100 | [diff] [blame] | 497 | /* This channel is not in use, bail out */ |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 498 | if (!chan->client_count) |
Maxime Ripard | c4b54a6 | 2014-11-17 14:41:59 +0100 | [diff] [blame] | 499 | return; |
| 500 | |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 501 | chan->client_count--; |
Maxime Ripard | c4b54a6 | 2014-11-17 14:41:59 +0100 | [diff] [blame] | 502 | |
| 503 | /* This channel is not in use anymore, free it */ |
Lars-Peter Clausen | b36f09c | 2015-10-20 11:46:28 +0200 | [diff] [blame] | 504 | if (!chan->client_count && chan->device->device_free_chan_resources) { |
| 505 | /* Make sure all operations have completed */ |
| 506 | dmaengine_synchronize(chan); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 507 | chan->device->device_free_chan_resources(chan); |
Lars-Peter Clausen | b36f09c | 2015-10-20 11:46:28 +0200 | [diff] [blame] | 508 | } |
Peter Ujfalusi | 56f13c0 | 2015-04-09 12:35:47 +0300 | [diff] [blame] | 509 | |
| 510 | /* If the channel is used via a DMA request router, free the mapping */ |
| 511 | if (chan->router && chan->router->route_free) { |
| 512 | chan->router->route_free(chan->router->dev, chan->route_data); |
| 513 | chan->router = NULL; |
| 514 | chan->route_data = NULL; |
| 515 | } |
Vinod Koul | 83c7794 | 2019-12-24 10:22:15 +0530 | [diff] [blame] | 516 | |
| 517 | dma_device_put(chan->device); |
| 518 | module_put(dma_chan_to_owner(chan)); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 521 | enum 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 Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 530 | dev_err(chan->device->dev, "%s: timeout!\n", __func__); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 531 | return DMA_ERROR; |
| 532 | } |
Bartlomiej Zolnierkiewicz | 2cbe7fe | 2012-11-08 10:02:07 +0000 | [diff] [blame] | 533 | if (status != DMA_IN_PROGRESS) |
| 534 | break; |
| 535 | cpu_relax(); |
| 536 | } while (1); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 537 | |
| 538 | return status; |
| 539 | } |
| 540 | EXPORT_SYMBOL(dma_sync_wait); |
| 541 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 542 | /** |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 543 | * dma_find_channel - find a channel to carry out the operation |
| 544 | * @tx_type: transaction type |
| 545 | */ |
| 546 | struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type) |
| 547 | { |
Christoph Lameter | e7dcaa4 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 548 | return this_cpu_read(channel_table[tx_type]->chan); |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 549 | } |
| 550 | EXPORT_SYMBOL(dma_find_channel); |
| 551 | |
| 552 | /** |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 553 | * dma_issue_pending_all - flush all pending operations across all channels |
| 554 | */ |
| 555 | void dma_issue_pending_all(void) |
| 556 | { |
| 557 | struct dma_device *device; |
| 558 | struct dma_chan *chan; |
| 559 | |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 560 | rcu_read_lock(); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 561 | list_for_each_entry_rcu(device, &dma_device_list, global_node) { |
| 562 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 563 | continue; |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 564 | list_for_each_entry(chan, &device->channels, device_node) |
| 565 | if (chan->client_count) |
| 566 | device->device_issue_pending(chan); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 567 | } |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 568 | rcu_read_unlock(); |
| 569 | } |
| 570 | EXPORT_SYMBOL(dma_issue_pending_all); |
| 571 | |
Laurent Pinchart | 0d5484b | 2014-10-29 00:30:58 +0200 | [diff] [blame] | 572 | int 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 Shevchenko | dd4e91d | 2016-05-10 20:43:34 +0300 | [diff] [blame] | 582 | if (!(test_bit(DMA_SLAVE, device->cap_mask.bits) || |
| 583 | test_bit(DMA_CYCLIC, device->cap_mask.bits))) |
Laurent Pinchart | 0d5484b | 2014-10-29 00:30:58 +0200 | [diff] [blame] | 584 | return -ENXIO; |
| 585 | |
| 586 | /* |
| 587 | * Check whether it reports it uses the generic slave |
| 588 | * capabilities, if not, that means it doesn't support any |
| 589 | * kind of slave capabilities reporting. |
| 590 | */ |
| 591 | if (!device->directions) |
| 592 | return -ENXIO; |
| 593 | |
| 594 | caps->src_addr_widths = device->src_addr_widths; |
| 595 | caps->dst_addr_widths = device->dst_addr_widths; |
| 596 | caps->directions = device->directions; |
Shawn Lin | 6d5bbed | 2016-01-22 19:06:50 +0800 | [diff] [blame] | 597 | caps->max_burst = device->max_burst; |
Laurent Pinchart | 0d5484b | 2014-10-29 00:30:58 +0200 | [diff] [blame] | 598 | caps->residue_granularity = device->residue_granularity; |
Robert Jarzmik | 9eeacd3 | 2015-10-13 21:54:29 +0200 | [diff] [blame] | 599 | caps->descriptor_reuse = device->descriptor_reuse; |
Marek Szyprowski | d8095f9 | 2018-07-02 15:08:10 +0200 | [diff] [blame] | 600 | caps->cmd_pause = !!device->device_pause; |
| 601 | caps->cmd_resume = !!device->device_resume; |
Laurent Pinchart | 0d5484b | 2014-10-29 00:30:58 +0200 | [diff] [blame] | 602 | caps->cmd_terminate = !!device->device_terminate_all; |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | EXPORT_SYMBOL_GPL(dma_get_slave_caps); |
| 607 | |
Lars-Peter Clausen | a53e28d | 2013-03-25 13:23:52 +0100 | [diff] [blame] | 608 | static struct dma_chan *private_candidate(const dma_cap_mask_t *mask, |
| 609 | struct dma_device *dev, |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 610 | dma_filter_fn fn, void *fn_param) |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 611 | { |
| 612 | struct dma_chan *chan; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 613 | |
Geert Uytterhoeven | 69b1189 | 2020-01-21 10:33:09 +0100 | [diff] [blame] | 614 | if (mask && !dma_device_satisfies_mask(dev, mask)) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 615 | dev_dbg(dev->dev, "%s: wrong capabilities\n", __func__); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 616 | return NULL; |
| 617 | } |
| 618 | /* devices with multiple channels need special handling as we need to |
| 619 | * ensure that all channels are either private or public. |
| 620 | */ |
| 621 | if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask)) |
| 622 | list_for_each_entry(chan, &dev->channels, device_node) { |
| 623 | /* some channels are already publicly allocated */ |
| 624 | if (chan->client_count) |
| 625 | return NULL; |
| 626 | } |
| 627 | |
| 628 | list_for_each_entry(chan, &dev->channels, device_node) { |
| 629 | if (chan->client_count) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 630 | dev_dbg(dev->dev, "%s: %s busy\n", |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 631 | __func__, dma_chan_name(chan)); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 632 | continue; |
| 633 | } |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 634 | if (fn && !fn(chan, fn_param)) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 635 | dev_dbg(dev->dev, "%s: %s filter said false\n", |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 636 | __func__, dma_chan_name(chan)); |
| 637 | continue; |
| 638 | } |
| 639 | return chan; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 642 | return NULL; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 645 | static 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 Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 664 | dev_dbg(device->dev, "%s: %s module removed\n", |
| 665 | __func__, dma_chan_name(chan)); |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 666 | list_del_rcu(&device->global_node); |
| 667 | } else |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 668 | dev_dbg(device->dev, |
| 669 | "%s: failed to get %s: (%d)\n", |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 670 | __func__, dma_chan_name(chan), err); |
| 671 | |
| 672 | if (--device->privatecnt == 0) |
| 673 | dma_cap_clear(DMA_PRIVATE, device->cap_mask); |
| 674 | |
| 675 | chan = ERR_PTR(err); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | return chan ? chan : ERR_PTR(-EPROBE_DEFER); |
| 680 | } |
| 681 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 682 | /** |
Stefan Agner | 19d643d | 2015-06-01 23:53:43 +0200 | [diff] [blame] | 683 | * dma_get_slave_channel - try to get specific channel exclusively |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 684 | * @chan: target channel |
| 685 | */ |
| 686 | struct 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 Koul | d9a6c8f | 2013-08-19 10:47:26 +0530 | [diff] [blame] | 693 | if (chan->client_count == 0) { |
Peter Ujfalusi | 214fc4e | 2015-09-24 12:03:35 +0300 | [diff] [blame] | 694 | struct dma_device *device = chan->device; |
| 695 | |
| 696 | dma_cap_set(DMA_PRIVATE, device->cap_mask); |
| 697 | device->privatecnt++; |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 698 | err = dma_chan_get(chan); |
Peter Ujfalusi | 214fc4e | 2015-09-24 12:03:35 +0300 | [diff] [blame] | 699 | if (err) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 700 | dev_dbg(chan->device->dev, |
| 701 | "%s: failed to get %s: (%d)\n", |
Vinod Koul | d9a6c8f | 2013-08-19 10:47:26 +0530 | [diff] [blame] | 702 | __func__, dma_chan_name(chan), err); |
Peter Ujfalusi | 214fc4e | 2015-09-24 12:03:35 +0300 | [diff] [blame] | 703 | chan = NULL; |
| 704 | if (--device->privatecnt == 0) |
| 705 | dma_cap_clear(DMA_PRIVATE, device->cap_mask); |
| 706 | } |
Vinod Koul | d9a6c8f | 2013-08-19 10:47:26 +0530 | [diff] [blame] | 707 | } else |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 708 | chan = NULL; |
| 709 | |
| 710 | mutex_unlock(&dma_list_mutex); |
| 711 | |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 712 | |
| 713 | return chan; |
| 714 | } |
| 715 | EXPORT_SYMBOL_GPL(dma_get_slave_channel); |
| 716 | |
Stephen Warren | 8010dad | 2013-11-26 12:40:51 -0700 | [diff] [blame] | 717 | struct dma_chan *dma_get_any_slave_channel(struct dma_device *device) |
| 718 | { |
| 719 | dma_cap_mask_t mask; |
| 720 | struct dma_chan *chan; |
Stephen Warren | 8010dad | 2013-11-26 12:40:51 -0700 | [diff] [blame] | 721 | |
| 722 | dma_cap_zero(mask); |
| 723 | dma_cap_set(DMA_SLAVE, mask); |
| 724 | |
| 725 | /* lock against __dma_request_channel */ |
| 726 | mutex_lock(&dma_list_mutex); |
| 727 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 728 | chan = find_candidate(device, &mask, NULL, NULL); |
Stephen Warren | 8010dad | 2013-11-26 12:40:51 -0700 | [diff] [blame] | 729 | |
| 730 | mutex_unlock(&dma_list_mutex); |
| 731 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 732 | return IS_ERR(chan) ? NULL : chan; |
Stephen Warren | 8010dad | 2013-11-26 12:40:51 -0700 | [diff] [blame] | 733 | } |
| 734 | EXPORT_SYMBOL_GPL(dma_get_any_slave_channel); |
| 735 | |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 736 | /** |
Daniel Mack | 6b9019a | 2013-08-14 18:35:03 +0200 | [diff] [blame] | 737 | * __dma_request_channel - try to allocate an exclusive channel |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 738 | * @mask: capabilities that the channel must satisfy |
| 739 | * @fn: optional callback to disposition available channels |
| 740 | * @fn_param: opaque parameter to pass to dma_filter_fn |
Baolin Wang | f515131 | 2019-05-20 19:32:14 +0800 | [diff] [blame] | 741 | * @np: device node to look for DMA channels |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 742 | * |
| 743 | * Returns pointer to appropriate DMA channel on success or NULL. |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 744 | */ |
Lars-Peter Clausen | a53e28d | 2013-03-25 13:23:52 +0100 | [diff] [blame] | 745 | struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask, |
Baolin Wang | f515131 | 2019-05-20 19:32:14 +0800 | [diff] [blame] | 746 | dma_filter_fn fn, void *fn_param, |
| 747 | struct device_node *np) |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 748 | { |
| 749 | struct dma_device *device, *_d; |
| 750 | struct dma_chan *chan = NULL; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 751 | |
| 752 | /* Find a channel */ |
| 753 | mutex_lock(&dma_list_mutex); |
| 754 | list_for_each_entry_safe(device, _d, &dma_device_list, global_node) { |
Baolin Wang | f515131 | 2019-05-20 19:32:14 +0800 | [diff] [blame] | 755 | /* Finds a DMA controller with matching device node */ |
| 756 | if (np && device->dev->of_node && np != device->dev->of_node) |
| 757 | continue; |
| 758 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 759 | chan = find_candidate(device, mask, fn, fn_param); |
| 760 | if (!IS_ERR(chan)) |
| 761 | break; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 762 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 763 | chan = NULL; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 764 | } |
| 765 | mutex_unlock(&dma_list_mutex); |
| 766 | |
Jarkko Nikula | 4c4d7f87 | 2016-04-07 16:49:43 +0300 | [diff] [blame] | 767 | pr_debug("%s: %s (%s)\n", |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 768 | __func__, |
| 769 | chan ? "success" : "fail", |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 770 | chan ? dma_chan_name(chan) : NULL); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 771 | |
| 772 | return chan; |
| 773 | } |
| 774 | EXPORT_SYMBOL_GPL(__dma_request_channel); |
| 775 | |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 776 | static 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 Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 796 | /** |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 797 | * dma_request_chan - try to allocate an exclusive slave channel |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 798 | * @dev: pointer to client device structure |
| 799 | * @name: slave channel name |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 800 | * |
| 801 | * Returns pointer to appropriate DMA channel on success or an error pointer. |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 802 | */ |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 803 | struct dma_chan *dma_request_chan(struct device *dev, const char *name) |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 804 | { |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 805 | struct dma_device *d, *_d; |
| 806 | struct dma_chan *chan = NULL; |
| 807 | |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 808 | /* If device-tree is present get slave info from here */ |
| 809 | if (dev->of_node) |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 810 | chan = of_dma_request_slave_channel(dev->of_node, name); |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 811 | |
Andy Shevchenko | 4e82f5d | 2013-04-09 14:05:44 +0300 | [diff] [blame] | 812 | /* If device was enumerated by ACPI get slave info from here */ |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 813 | if (has_acpi_companion(dev) && !chan) |
| 814 | chan = acpi_dma_request_slave_chan_by_name(dev, name); |
Andy Shevchenko | 4e82f5d | 2013-04-09 14:05:44 +0300 | [diff] [blame] | 815 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 816 | if (PTR_ERR(chan) == -EPROBE_DEFER) |
| 817 | return chan; |
| 818 | |
| 819 | if (!IS_ERR_OR_NULL(chan)) |
| 820 | goto found; |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 821 | |
| 822 | /* Try to find the channel via the DMA filter map(s) */ |
| 823 | mutex_lock(&dma_list_mutex); |
| 824 | list_for_each_entry_safe(d, _d, &dma_device_list, global_node) { |
| 825 | dma_cap_mask_t mask; |
| 826 | const struct dma_slave_map *map = dma_filter_match(d, name, dev); |
| 827 | |
| 828 | if (!map) |
| 829 | continue; |
| 830 | |
| 831 | dma_cap_zero(mask); |
| 832 | dma_cap_set(DMA_SLAVE, mask); |
| 833 | |
| 834 | chan = find_candidate(d, &mask, d->filter.fn, map->param); |
| 835 | if (!IS_ERR(chan)) |
| 836 | break; |
| 837 | } |
| 838 | mutex_unlock(&dma_list_mutex); |
| 839 | |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 840 | if (IS_ERR_OR_NULL(chan)) |
| 841 | return chan ? chan : ERR_PTR(-EPROBE_DEFER); |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 842 | |
| 843 | found: |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame] | 844 | #ifdef CONFIG_DEBUG_FS |
| 845 | chan->dbg_client_name = kasprintf(GFP_KERNEL, "%s:%s", dev_name(dev), |
| 846 | name); |
| 847 | #endif |
| 848 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 849 | chan->name = kasprintf(GFP_KERNEL, "dma:%s", name); |
| 850 | if (!chan->name) |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 851 | return chan; |
| 852 | chan->slave = dev; |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 853 | |
| 854 | if (sysfs_create_link(&chan->dev->device.kobj, &dev->kobj, |
| 855 | DMA_SLAVE_NAME)) |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 856 | dev_warn(dev, "Cannot create DMA %s symlink\n", DMA_SLAVE_NAME); |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 857 | if (sysfs_create_link(&dev->kobj, &chan->dev->device.kobj, chan->name)) |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 858 | dev_warn(dev, "Cannot create DMA %s symlink\n", chan->name); |
| 859 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 860 | return chan; |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 861 | } |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 862 | EXPORT_SYMBOL_GPL(dma_request_chan); |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 863 | |
| 864 | /** |
| 865 | * dma_request_slave_channel - try to allocate an exclusive slave channel |
| 866 | * @dev: pointer to client device structure |
| 867 | * @name: slave channel name |
| 868 | * |
| 869 | * Returns pointer to appropriate DMA channel on success or NULL. |
| 870 | */ |
| 871 | struct dma_chan *dma_request_slave_channel(struct device *dev, |
| 872 | const char *name) |
| 873 | { |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 874 | struct dma_chan *ch = dma_request_chan(dev, name); |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 875 | if (IS_ERR(ch)) |
| 876 | return NULL; |
Robert Baldyga | 05aa1a7 | 2015-08-07 12:26:47 +0200 | [diff] [blame] | 877 | |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 878 | return ch; |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 879 | } |
| 880 | EXPORT_SYMBOL_GPL(dma_request_slave_channel); |
| 881 | |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 882 | /** |
| 883 | * dma_request_chan_by_mask - allocate a channel satisfying certain capabilities |
| 884 | * @mask: capabilities that the channel must satisfy |
| 885 | * |
| 886 | * Returns pointer to appropriate DMA channel on success or an error pointer. |
| 887 | */ |
| 888 | struct 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 Wang | f515131 | 2019-05-20 19:32:14 +0800 | [diff] [blame] | 895 | chan = __dma_request_channel(mask, NULL, NULL, NULL); |
Peter Ujfalusi | ec8ca8e | 2018-07-18 12:29:57 +0300 | [diff] [blame] | 896 | if (!chan) { |
| 897 | mutex_lock(&dma_list_mutex); |
| 898 | if (list_empty(&dma_device_list)) |
| 899 | chan = ERR_PTR(-EPROBE_DEFER); |
| 900 | else |
| 901 | chan = ERR_PTR(-ENODEV); |
| 902 | mutex_unlock(&dma_list_mutex); |
| 903 | } |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 904 | |
| 905 | return chan; |
| 906 | } |
| 907 | EXPORT_SYMBOL_GPL(dma_request_chan_by_mask); |
| 908 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 909 | void 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 Nemoto | 0f57151 | 2009-03-06 20:07:14 +0900 | [diff] [blame] | 915 | /* drop PRIVATE cap enabled by __dma_request_channel() */ |
| 916 | if (--chan->device->privatecnt == 0) |
| 917 | dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask); |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 918 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 919 | if (chan->slave) { |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 920 | sysfs_remove_link(&chan->dev->device.kobj, DMA_SLAVE_NAME); |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 921 | sysfs_remove_link(&chan->slave->kobj, chan->name); |
| 922 | kfree(chan->name); |
| 923 | chan->name = NULL; |
| 924 | chan->slave = NULL; |
| 925 | } |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame] | 926 | |
| 927 | #ifdef CONFIG_DEBUG_FS |
| 928 | kfree(chan->dbg_client_name); |
| 929 | chan->dbg_client_name = NULL; |
| 930 | #endif |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 931 | mutex_unlock(&dma_list_mutex); |
| 932 | } |
| 933 | EXPORT_SYMBOL_GPL(dma_release_channel); |
| 934 | |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 935 | /** |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 936 | * dmaengine_get - register interest in dma_channels |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 937 | */ |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 938 | void dmaengine_get(void) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 939 | { |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 940 | struct dma_device *device, *_d; |
| 941 | struct dma_chan *chan; |
| 942 | int err; |
| 943 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 944 | mutex_lock(&dma_list_mutex); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 945 | dmaengine_ref_count++; |
| 946 | |
| 947 | /* try to grab channels */ |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 948 | list_for_each_entry_safe(device, _d, &dma_device_list, global_node) { |
| 949 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 950 | continue; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 951 | list_for_each_entry(chan, &device->channels, device_node) { |
| 952 | err = dma_chan_get(chan); |
| 953 | if (err == -ENODEV) { |
| 954 | /* module removed before we could use it */ |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 955 | list_del_rcu(&device->global_node); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 956 | break; |
| 957 | } else if (err) |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 958 | dev_dbg(chan->device->dev, |
| 959 | "%s: failed to get %s: (%d)\n", |
| 960 | __func__, dma_chan_name(chan), err); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 961 | } |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 962 | } |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 963 | |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 964 | /* if this is the first reference and there were channels |
| 965 | * waiting we need to rebalance to get those channels |
| 966 | * incorporated into the channel table |
| 967 | */ |
| 968 | if (dmaengine_ref_count == 1) |
| 969 | dma_channel_rebalance(); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 970 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 971 | } |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 972 | EXPORT_SYMBOL(dmaengine_get); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 973 | |
| 974 | /** |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 975 | * dmaengine_put - let dma drivers be removed when ref_count == 0 |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 976 | */ |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 977 | void dmaengine_put(void) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 978 | { |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 979 | struct dma_device *device, *_d; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 980 | struct dma_chan *chan; |
| 981 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 982 | mutex_lock(&dma_list_mutex); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 983 | dmaengine_ref_count--; |
| 984 | BUG_ON(dmaengine_ref_count < 0); |
| 985 | /* drop channel references */ |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 986 | list_for_each_entry_safe(device, _d, &dma_device_list, global_node) { |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 987 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 988 | continue; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 989 | list_for_each_entry(chan, &device->channels, device_node) |
| 990 | dma_chan_put(chan); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 991 | } |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 992 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 993 | } |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 994 | EXPORT_SYMBOL(dmaengine_put); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 995 | |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 996 | static 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 Canillas | d57d3a4 | 2016-05-11 13:39:27 -0400 | [diff] [blame] | 1007 | #if IS_ENABLED(CONFIG_ASYNC_MEMCPY) |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1008 | if (!dma_has_cap(DMA_MEMCPY, device->cap_mask)) |
| 1009 | return false; |
| 1010 | #endif |
| 1011 | |
Javier Martinez Canillas | d57d3a4 | 2016-05-11 13:39:27 -0400 | [diff] [blame] | 1012 | #if IS_ENABLED(CONFIG_ASYNC_XOR) |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1013 | if (!dma_has_cap(DMA_XOR, device->cap_mask)) |
| 1014 | return false; |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 1015 | |
| 1016 | #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA |
Dan Williams | 4499a24 | 2009-11-19 17:10:25 -0700 | [diff] [blame] | 1017 | if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask)) |
| 1018 | return false; |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1019 | #endif |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 1020 | #endif |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1021 | |
Javier Martinez Canillas | d57d3a4 | 2016-05-11 13:39:27 -0400 | [diff] [blame] | 1022 | #if IS_ENABLED(CONFIG_ASYNC_PQ) |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1023 | if (!dma_has_cap(DMA_PQ, device->cap_mask)) |
| 1024 | return false; |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 1025 | |
| 1026 | #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA |
Dan Williams | 4499a24 | 2009-11-19 17:10:25 -0700 | [diff] [blame] | 1027 | if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask)) |
| 1028 | return false; |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1029 | #endif |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 1030 | #endif |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1031 | |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1035 | static int get_dma_id(struct dma_device *device) |
| 1036 | { |
Matthew Wilcox | 485258b | 2018-06-18 15:41:48 -0400 | [diff] [blame] | 1037 | int rc = ida_alloc(&dma_ida, GFP_KERNEL); |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1038 | |
Matthew Wilcox | 485258b | 2018-06-18 15:41:48 -0400 | [diff] [blame] | 1039 | if (rc < 0) |
| 1040 | return rc; |
| 1041 | device->dev_id = rc; |
| 1042 | return 0; |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1045 | static 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 Jiang | 5429b51 | 2020-01-31 10:58:39 -0700 | [diff] [blame] | 1056 | if (!tchan) |
| 1057 | return -ENODEV; |
| 1058 | |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1059 | if (tchan->dev) { |
| 1060 | idr_ref = tchan->dev->idr_ref; |
| 1061 | } else { |
| 1062 | idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL); |
| 1063 | if (!idr_ref) |
| 1064 | return -ENOMEM; |
| 1065 | atomic_set(idr_ref, 0); |
| 1066 | } |
| 1067 | |
| 1068 | chan->local = alloc_percpu(typeof(*chan->local)); |
| 1069 | if (!chan->local) |
| 1070 | goto err_out; |
| 1071 | chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL); |
| 1072 | if (!chan->dev) { |
| 1073 | free_percpu(chan->local); |
| 1074 | chan->local = NULL; |
| 1075 | goto err_out; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * When the chan_id is a negative value, we are dynamically adding |
| 1080 | * the channel. Otherwise we are static enumerating. |
| 1081 | */ |
| 1082 | chan->chan_id = chan_id < 0 ? chancnt : chan_id; |
| 1083 | chan->dev->device.class = &dma_devclass; |
| 1084 | chan->dev->device.parent = device->dev; |
| 1085 | chan->dev->chan = chan; |
| 1086 | chan->dev->idr_ref = idr_ref; |
| 1087 | chan->dev->dev_id = device->dev_id; |
| 1088 | atomic_inc(idr_ref); |
| 1089 | dev_set_name(&chan->dev->device, "dma%dchan%d", |
| 1090 | device->dev_id, chan->chan_id); |
| 1091 | |
| 1092 | rc = device_register(&chan->dev->device); |
| 1093 | if (rc) |
| 1094 | goto err_out; |
| 1095 | chan->client_count = 0; |
| 1096 | device->chancnt = chan->chan_id + 1; |
| 1097 | |
| 1098 | return 0; |
| 1099 | |
| 1100 | err_out: |
| 1101 | free_percpu(chan->local); |
| 1102 | kfree(chan->dev); |
| 1103 | if (atomic_dec_return(idr_ref) == 0) |
| 1104 | kfree(idr_ref); |
| 1105 | return rc; |
| 1106 | } |
| 1107 | |
Dave Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1108 | int 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 | } |
| 1120 | EXPORT_SYMBOL_GPL(dma_async_device_channel_register); |
| 1121 | |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1122 | static 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 Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1129 | list_del(&chan->device_node); |
| 1130 | device->chancnt--; |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1131 | chan->dev->chan = NULL; |
| 1132 | mutex_unlock(&dma_list_mutex); |
| 1133 | device_unregister(&chan->dev->device); |
| 1134 | free_percpu(chan->local); |
| 1135 | } |
| 1136 | |
Dave Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1137 | void 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 | } |
| 1143 | EXPORT_SYMBOL_GPL(dma_async_device_channel_unregister); |
| 1144 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1145 | /** |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 1146 | * dma_async_device_register - registers DMA devices found |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1147 | * @device: &dma_device |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 1148 | * |
| 1149 | * After calling this routine the structure should not be freed except in the |
| 1150 | * device_release() callback which will be called after |
| 1151 | * dma_async_device_unregister() is called and no further references are taken. |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1152 | */ |
| 1153 | int dma_async_device_register(struct dma_device *device) |
| 1154 | { |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1155 | int rc, i = 0; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1156 | struct dma_chan* chan; |
| 1157 | |
| 1158 | if (!device) |
| 1159 | return -ENODEV; |
| 1160 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1161 | /* validate device routines */ |
Vinod Koul | 3eeb515 | 2017-08-27 16:55:32 +0530 | [diff] [blame] | 1162 | if (!device->dev) { |
| 1163 | pr_err("DMAdevice must have dev\n"); |
| 1164 | return -EIO; |
| 1165 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1166 | |
Logan Gunthorpe | dae7a58 | 2019-12-16 12:01:16 -0700 | [diff] [blame] | 1167 | device->owner = device->dev->driver->owner; |
| 1168 | |
Vinod Koul | 3eeb515 | 2017-08-27 16:55:32 +0530 | [diff] [blame] | 1169 | if (dma_has_cap(DMA_MEMCPY, device->cap_mask) && !device->device_prep_dma_memcpy) { |
| 1170 | dev_err(device->dev, |
| 1171 | "Device claims capability %s, but op is not defined\n", |
| 1172 | "DMA_MEMCPY"); |
| 1173 | return -EIO; |
| 1174 | } |
| 1175 | |
| 1176 | if (dma_has_cap(DMA_XOR, device->cap_mask) && !device->device_prep_dma_xor) { |
| 1177 | dev_err(device->dev, |
| 1178 | "Device claims capability %s, but op is not defined\n", |
| 1179 | "DMA_XOR"); |
| 1180 | return -EIO; |
| 1181 | } |
| 1182 | |
| 1183 | if (dma_has_cap(DMA_XOR_VAL, device->cap_mask) && !device->device_prep_dma_xor_val) { |
| 1184 | dev_err(device->dev, |
| 1185 | "Device claims capability %s, but op is not defined\n", |
| 1186 | "DMA_XOR_VAL"); |
| 1187 | return -EIO; |
| 1188 | } |
| 1189 | |
| 1190 | if (dma_has_cap(DMA_PQ, device->cap_mask) && !device->device_prep_dma_pq) { |
| 1191 | dev_err(device->dev, |
| 1192 | "Device claims capability %s, but op is not defined\n", |
| 1193 | "DMA_PQ"); |
| 1194 | return -EIO; |
| 1195 | } |
| 1196 | |
| 1197 | if (dma_has_cap(DMA_PQ_VAL, device->cap_mask) && !device->device_prep_dma_pq_val) { |
| 1198 | dev_err(device->dev, |
| 1199 | "Device claims capability %s, but op is not defined\n", |
| 1200 | "DMA_PQ_VAL"); |
| 1201 | return -EIO; |
| 1202 | } |
| 1203 | |
| 1204 | if (dma_has_cap(DMA_MEMSET, device->cap_mask) && !device->device_prep_dma_memset) { |
| 1205 | dev_err(device->dev, |
| 1206 | "Device claims capability %s, but op is not defined\n", |
| 1207 | "DMA_MEMSET"); |
| 1208 | return -EIO; |
| 1209 | } |
| 1210 | |
| 1211 | if (dma_has_cap(DMA_INTERRUPT, device->cap_mask) && !device->device_prep_dma_interrupt) { |
| 1212 | dev_err(device->dev, |
| 1213 | "Device claims capability %s, but op is not defined\n", |
| 1214 | "DMA_INTERRUPT"); |
| 1215 | return -EIO; |
| 1216 | } |
| 1217 | |
| 1218 | if (dma_has_cap(DMA_CYCLIC, device->cap_mask) && !device->device_prep_dma_cyclic) { |
| 1219 | dev_err(device->dev, |
| 1220 | "Device claims capability %s, but op is not defined\n", |
| 1221 | "DMA_CYCLIC"); |
| 1222 | return -EIO; |
| 1223 | } |
| 1224 | |
| 1225 | if (dma_has_cap(DMA_INTERLEAVE, device->cap_mask) && !device->device_prep_interleaved_dma) { |
| 1226 | dev_err(device->dev, |
| 1227 | "Device claims capability %s, but op is not defined\n", |
| 1228 | "DMA_INTERLEAVE"); |
| 1229 | return -EIO; |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | if (!device->device_tx_status) { |
| 1234 | dev_err(device->dev, "Device tx_status is not defined\n"); |
| 1235 | return -EIO; |
| 1236 | } |
| 1237 | |
| 1238 | |
| 1239 | if (!device->device_issue_pending) { |
| 1240 | dev_err(device->dev, "Device issue_pending is not defined\n"); |
| 1241 | return -EIO; |
| 1242 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1243 | |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 1244 | 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 Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1250 | /* note: this only matters in the |
Dan Williams | 5fc6d89 | 2010-10-07 16:44:50 -0700 | [diff] [blame] | 1251 | * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1252 | */ |
| 1253 | if (device_has_all_tx_types(device)) |
| 1254 | dma_cap_set(DMA_ASYNC_TX, device->cap_mask); |
| 1255 | |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1256 | rc = get_dma_id(device); |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1257 | if (rc != 0) |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1258 | return rc; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1259 | |
| 1260 | /* represent channels in sysfs. Probably want devs too */ |
| 1261 | list_for_each_entry(chan, &device->channels, device_node) { |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1262 | rc = __dma_async_device_channel_register(device, chan, i++); |
| 1263 | if (rc < 0) |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1264 | goto err_out; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1265 | } |
Viresh Kumar | 76d7b84 | 2016-07-27 14:32:58 -0700 | [diff] [blame] | 1266 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1267 | mutex_lock(&dma_list_mutex); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 1268 | /* take references on public channels */ |
| 1269 | if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1270 | list_for_each_entry(chan, &device->channels, device_node) { |
| 1271 | /* if clients are already waiting for channels we need |
| 1272 | * to take references on their behalf |
| 1273 | */ |
| 1274 | if (dma_chan_get(chan) == -ENODEV) { |
| 1275 | /* note we can only get here for the first |
| 1276 | * channel as the remaining channels are |
| 1277 | * guaranteed to get a reference |
| 1278 | */ |
| 1279 | rc = -ENODEV; |
| 1280 | mutex_unlock(&dma_list_mutex); |
| 1281 | goto err_out; |
| 1282 | } |
| 1283 | } |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1284 | list_add_tail_rcu(&device->global_node, &dma_device_list); |
Atsushi Nemoto | 0f57151 | 2009-03-06 20:07:14 +0900 | [diff] [blame] | 1285 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 1286 | device->privatecnt++; /* Always private */ |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1287 | dma_channel_rebalance(); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1288 | mutex_unlock(&dma_list_mutex); |
| 1289 | |
Peter Ujfalusi | 26cf132 | 2020-03-06 16:28:39 +0200 | [diff] [blame^] | 1290 | dmaengine_debug_register(device); |
| 1291 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1292 | return 0; |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 1293 | |
| 1294 | err_out: |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1295 | /* if we never registered a channel just release the idr */ |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1296 | if (!device->chancnt) { |
Matthew Wilcox | 485258b | 2018-06-18 15:41:48 -0400 | [diff] [blame] | 1297 | ida_free(&dma_ida, device->dev_id); |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1298 | return rc; |
| 1299 | } |
| 1300 | |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 1301 | list_for_each_entry(chan, &device->channels, device_node) { |
| 1302 | if (chan->local == NULL) |
| 1303 | continue; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1304 | mutex_lock(&dma_list_mutex); |
| 1305 | chan->dev->chan = NULL; |
| 1306 | mutex_unlock(&dma_list_mutex); |
| 1307 | device_unregister(&chan->dev->device); |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 1308 | free_percpu(chan->local); |
| 1309 | } |
| 1310 | return rc; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1311 | } |
David Brownell | 765e3d8 | 2007-03-16 13:38:05 -0800 | [diff] [blame] | 1312 | EXPORT_SYMBOL(dma_async_device_register); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1313 | |
| 1314 | /** |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1315 | * dma_async_device_unregister - unregister a DMA device |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 1316 | * @device: &dma_device |
Dan Williams | f27c580 | 2009-01-06 11:38:18 -0700 | [diff] [blame] | 1317 | * |
| 1318 | * This routine is called by dma driver exit routines, dmaengine holds module |
| 1319 | * references to prevent it being called while channels are in use. |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 1320 | */ |
| 1321 | void dma_async_device_unregister(struct dma_device *device) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1322 | { |
Dave Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1323 | struct dma_chan *chan, *n; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1324 | |
Peter Ujfalusi | 26cf132 | 2020-03-06 16:28:39 +0200 | [diff] [blame^] | 1325 | dmaengine_debug_unregister(device); |
| 1326 | |
Dave Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1327 | list_for_each_entry_safe(chan, n, &device->channels, device_node) |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1328 | __dma_async_device_channel_unregister(device, chan); |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 1329 | |
| 1330 | mutex_lock(&dma_list_mutex); |
| 1331 | /* |
| 1332 | * setting DMA_PRIVATE ensures the device being torn down will not |
| 1333 | * be used in the channel_table |
| 1334 | */ |
| 1335 | dma_cap_set(DMA_PRIVATE, device->cap_mask); |
| 1336 | dma_channel_rebalance(); |
| 1337 | dma_device_put(device); |
| 1338 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1339 | } |
David Brownell | 765e3d8 | 2007-03-16 13:38:05 -0800 | [diff] [blame] | 1340 | EXPORT_SYMBOL(dma_async_device_unregister); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1341 | |
Huang Shijie | f39b948 | 2018-07-26 14:45:53 +0800 | [diff] [blame] | 1342 | static 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 | */ |
| 1356 | int 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 | } |
| 1375 | EXPORT_SYMBOL(dmaenginem_async_device_register); |
| 1376 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1377 | struct 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) } |
| 1385 | static struct dmaengine_unmap_pool unmap_pool[] = { |
| 1386 | __UNMAP_POOL(2), |
Dan Williams | 3cc377b | 2013-12-09 10:33:16 -0800 | [diff] [blame] | 1387 | #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID) |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1388 | __UNMAP_POOL(16), |
| 1389 | __UNMAP_POOL(128), |
| 1390 | __UNMAP_POOL(256), |
| 1391 | #endif |
| 1392 | }; |
| 1393 | |
| 1394 | static struct dmaengine_unmap_pool *__get_unmap_pool(int nr) |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1395 | { |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1396 | int order = get_count_order(nr); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1397 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1398 | switch (order) { |
| 1399 | case 0 ... 1: |
| 1400 | return &unmap_pool[0]; |
Matthias Kaehlcke | 23f963e9 | 2017-03-13 14:30:29 -0700 | [diff] [blame] | 1401 | #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID) |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1402 | case 2 ... 4: |
| 1403 | return &unmap_pool[1]; |
| 1404 | case 5 ... 7: |
| 1405 | return &unmap_pool[2]; |
| 1406 | case 8: |
| 1407 | return &unmap_pool[3]; |
Matthias Kaehlcke | 23f963e9 | 2017-03-13 14:30:29 -0700 | [diff] [blame] | 1408 | #endif |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1409 | default: |
| 1410 | BUG(); |
| 1411 | return NULL; |
| 1412 | } |
| 1413 | } |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1414 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1415 | static 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 Williams | 7476bd79 | 2013-10-18 19:35:29 +0200 | [diff] [blame] | 1430 | for (; i < cnt; i++) { |
| 1431 | if (unmap->addr[i] == 0) |
| 1432 | continue; |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1433 | dma_unmap_page(dev, unmap->addr[i], unmap->len, |
| 1434 | DMA_BIDIRECTIONAL); |
Dan Williams | 7476bd79 | 2013-10-18 19:35:29 +0200 | [diff] [blame] | 1435 | } |
Xuelin Shi | c1f43dd | 2014-05-21 14:02:37 -0700 | [diff] [blame] | 1436 | cnt = unmap->map_cnt; |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1437 | mempool_free(unmap, __get_unmap_pool(cnt)->pool); |
| 1438 | } |
| 1439 | |
| 1440 | void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap) |
| 1441 | { |
| 1442 | if (unmap) |
| 1443 | kref_put(&unmap->kref, dmaengine_unmap); |
| 1444 | } |
| 1445 | EXPORT_SYMBOL_GPL(dmaengine_unmap_put); |
| 1446 | |
| 1447 | static 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 Lawall | 240eb916 | 2015-09-13 14:15:19 +0200 | [diff] [blame] | 1454 | mempool_destroy(p->pool); |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1455 | p->pool = NULL; |
Julia Lawall | 240eb916 | 2015-09-13 14:15:19 +0200 | [diff] [blame] | 1456 | kmem_cache_destroy(p->cache); |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1457 | p->cache = NULL; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | static 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 Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1479 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1480 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1481 | if (i == ARRAY_SIZE(unmap_pool)) |
| 1482 | return 0; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1483 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1484 | dmaengine_destroy_unmap_pool(); |
| 1485 | return -ENOMEM; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1486 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1487 | |
Dan Williams | 8971646 | 2013-10-18 19:35:25 +0200 | [diff] [blame] | 1488 | struct dmaengine_unmap_data * |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1489 | dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags) |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1490 | { |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1491 | struct dmaengine_unmap_data *unmap; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1492 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1493 | unmap = mempool_alloc(__get_unmap_pool(nr)->pool, flags); |
| 1494 | if (!unmap) |
| 1495 | return NULL; |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1496 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1497 | memset(unmap, 0, sizeof(*unmap)); |
| 1498 | kref_init(&unmap->kref); |
| 1499 | unmap->dev = dev; |
Xuelin Shi | c1f43dd | 2014-05-21 14:02:37 -0700 | [diff] [blame] | 1500 | unmap->map_cnt = nr; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1501 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1502 | return unmap; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1503 | } |
Dan Williams | 8971646 | 2013-10-18 19:35:25 +0200 | [diff] [blame] | 1504 | EXPORT_SYMBOL(dmaengine_get_unmap_data); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1505 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1506 | void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx, |
| 1507 | struct dma_chan *chan) |
| 1508 | { |
| 1509 | tx->chan = chan; |
Dan Williams | 5fc6d89 | 2010-10-07 16:44:50 -0700 | [diff] [blame] | 1510 | #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1511 | spin_lock_init(&tx->lock); |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1512 | #endif |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1513 | } |
| 1514 | EXPORT_SYMBOL(dma_async_tx_descriptor_init); |
| 1515 | |
Peter Ujfalusi | 4db8fd3 | 2019-12-23 13:04:44 +0200 | [diff] [blame] | 1516 | static 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 | |
| 1532 | int 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 | } |
| 1549 | EXPORT_SYMBOL_GPL(dmaengine_desc_attach_metadata); |
| 1550 | |
| 1551 | void *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 | } |
| 1568 | EXPORT_SYMBOL_GPL(dmaengine_desc_get_metadata_ptr); |
| 1569 | |
| 1570 | int 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 | } |
| 1587 | EXPORT_SYMBOL_GPL(dmaengine_desc_set_metadata_len); |
| 1588 | |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1589 | /* dma_wait_for_async_tx - spin wait for a transaction to complete |
| 1590 | * @tx: in-flight transaction to wait on |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1591 | */ |
| 1592 | enum dma_status |
| 1593 | dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx) |
| 1594 | { |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1595 | unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1596 | |
| 1597 | if (!tx) |
Vinod Koul | adfedd9 | 2013-10-16 13:29:02 +0530 | [diff] [blame] | 1598 | return DMA_COMPLETE; |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1599 | |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1600 | while (tx->cookie == -EBUSY) { |
| 1601 | if (time_after_eq(jiffies, dma_sync_wait_timeout)) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 1602 | dev_err(tx->chan->device->dev, |
| 1603 | "%s timeout waiting for descriptor submission\n", |
| 1604 | __func__); |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1605 | return DMA_ERROR; |
| 1606 | } |
| 1607 | cpu_relax(); |
| 1608 | } |
| 1609 | return dma_sync_wait(tx->chan, tx->cookie); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1610 | } |
| 1611 | EXPORT_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 | */ |
| 1617 | void dma_run_dependencies(struct dma_async_tx_descriptor *tx) |
| 1618 | { |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1619 | struct dma_async_tx_descriptor *dep = txd_next(tx); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1620 | struct dma_async_tx_descriptor *dep_next; |
| 1621 | struct dma_chan *chan; |
| 1622 | |
| 1623 | if (!dep) |
| 1624 | return; |
| 1625 | |
Yuri Tikhonov | dd59b85 | 2009-01-12 15:17:20 -0700 | [diff] [blame] | 1626 | /* we'll submit tx->next now, so clear the link */ |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1627 | txd_clear_next(tx); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1628 | chan = dep->chan; |
| 1629 | |
| 1630 | /* keep submitting up until a channel switch is detected |
| 1631 | * in that case we will be called again as a result of |
| 1632 | * processing the interrupt from async_tx_channel_switch |
| 1633 | */ |
| 1634 | for (; dep; dep = dep_next) { |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1635 | txd_lock(dep); |
| 1636 | txd_clear_parent(dep); |
| 1637 | dep_next = txd_next(dep); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1638 | if (dep_next && dep_next->chan == chan) |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1639 | txd_clear_next(dep); /* ->next will be submitted */ |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1640 | else |
| 1641 | dep_next = NULL; /* submit current dep and terminate */ |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1642 | txd_unlock(dep); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1643 | |
| 1644 | dep->tx_submit(dep); |
| 1645 | } |
| 1646 | |
| 1647 | chan->device->device_issue_pending(chan); |
| 1648 | } |
| 1649 | EXPORT_SYMBOL_GPL(dma_run_dependencies); |
| 1650 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1651 | static int __init dma_bus_init(void) |
| 1652 | { |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1653 | int err = dmaengine_init_unmap_pool(); |
| 1654 | |
| 1655 | if (err) |
| 1656 | return err; |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame] | 1657 | |
| 1658 | err = class_register(&dma_devclass); |
| 1659 | if (!err) |
| 1660 | dmaengine_debugfs_init(); |
| 1661 | |
| 1662 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1663 | } |
Dan Williams | 652afc2 | 2009-01-06 11:38:22 -0700 | [diff] [blame] | 1664 | arch_initcall(dma_bus_init); |