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 | |
| 65 | static 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 | |
| 84 | static 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 | } |
| 106 | DEFINE_SHOW_ATTRIBUTE(dmaengine_summary); |
| 107 | |
| 108 | static 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 |
| 117 | static inline void dmaengine_debugfs_init(void) { } |
| 118 | #endif /* DEBUG_FS */ |
| 119 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 120 | /* --- sysfs implementation --- */ |
| 121 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 122 | #define DMA_SLAVE_NAME "slave" |
| 123 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 124 | /** |
Geert Uytterhoeven | fe33338 | 2019-06-07 13:30:39 +0200 | [diff] [blame] | 125 | * 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] | 126 | * @dev - device node |
| 127 | * |
| 128 | * Must be called under dma_list_mutex |
| 129 | */ |
| 130 | static 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-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 138 | static ssize_t memcpy_count_show(struct device *dev, |
| 139 | struct device_attribute *attr, char *buf) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 140 | { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 141 | struct dma_chan *chan; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 142 | unsigned long count = 0; |
| 143 | int i; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 144 | int err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 145 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 146 | 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 Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 155 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 156 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 157 | } |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 158 | static DEVICE_ATTR_RO(memcpy_count); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 159 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 160 | static ssize_t bytes_transferred_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)->bytes_transferred; |
| 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(bytes_transferred); |
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 in_use_show(struct device *dev, struct device_attribute *attr, |
| 183 | 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; |
| 186 | int err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 187 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 188 | 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 Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 197 | } |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 198 | static DEVICE_ATTR_RO(in_use); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 199 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 200 | static 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 Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 205 | }; |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 206 | ATTRIBUTE_GROUPS(dma_dev); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 207 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 208 | static 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 Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 213 | if (atomic_dec_and_test(chan_dev->idr_ref)) { |
Matthew Wilcox | 485258b | 2018-06-18 15:41:48 -0400 | [diff] [blame] | 214 | ida_free(&dma_ida, chan_dev->dev_id); |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 215 | kfree(chan_dev->idr_ref); |
| 216 | } |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 217 | kfree(chan_dev); |
| 218 | } |
| 219 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 220 | static struct class dma_devclass = { |
Tony Jones | 891f78e | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 221 | .name = "dma", |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 222 | .dev_groups = dma_dev_groups, |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 223 | .dev_release = chan_dev_release, |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | /* --- client and device registration --- */ |
| 227 | |
Logan Gunthorpe | 11a0fd2 | 2019-12-16 12:01:18 -0700 | [diff] [blame] | 228 | /** |
| 229 | * dma_cap_mask_all - enable iteration over all operation types |
| 230 | */ |
| 231 | static 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 | */ |
| 237 | struct 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 | */ |
| 244 | static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END]; |
| 245 | |
| 246 | static 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 Koul | 08baca4 | 2019-12-24 10:26:14 +0530 | [diff] [blame] | 270 | pr_err("dmaengine dma_channel_table_init failure: %d\n", err); |
Logan Gunthorpe | 11a0fd2 | 2019-12-16 12:01:18 -0700 | [diff] [blame] | 271 | for_each_dma_cap_mask(cap, dma_cap_mask_all) |
| 272 | free_percpu(channel_table[cap]); |
| 273 | } |
| 274 | |
| 275 | return err; |
| 276 | } |
| 277 | arch_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 | */ |
| 283 | static 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 | */ |
| 301 | static 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 | */ |
| 341 | static 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 Uytterhoeven | 69b1189 | 2020-01-21 10:33:09 +0100 | [diff] [blame] | 372 | static int dma_device_satisfies_mask(struct dma_device *device, |
| 373 | const dma_cap_mask_t *want) |
Dan Williams | d379b01 | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 374 | { |
| 375 | dma_cap_mask_t has; |
| 376 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 377 | bitmap_and(has.bits, want->bits, device->cap_mask.bits, |
Dan Williams | d379b01 | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 378 | DMA_TX_TYPE_END); |
| 379 | return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END); |
| 380 | } |
| 381 | |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 382 | static struct module *dma_chan_to_owner(struct dma_chan *chan) |
| 383 | { |
Logan Gunthorpe | dae7a58 | 2019-12-16 12:01:16 -0700 | [diff] [blame] | 384 | return chan->device->owner; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 385 | } |
| 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 | */ |
| 393 | static 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 Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 403 | static 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 | |
| 414 | static 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 Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 420 | /** |
| 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 | */ |
| 426 | static int dma_chan_get(struct dma_chan *chan) |
| 427 | { |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 428 | struct module *owner = dma_chan_to_owner(chan); |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 429 | int ret; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 430 | |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 431 | /* The channel is already in use, update client count */ |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 432 | if (chan->client_count) { |
| 433 | __module_get(owner); |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 434 | goto out; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 437 | if (!try_module_get(owner)) |
| 438 | return -ENODEV; |
| 439 | |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 440 | ret = kref_get_unless_zero(&chan->device->ref); |
| 441 | if (!ret) { |
| 442 | ret = -ENODEV; |
| 443 | goto module_put_out; |
| 444 | } |
| 445 | |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 446 | /* allocate upon first client reference */ |
Maxime Ripard | c4b54a6 | 2014-11-17 14:41:59 +0100 | [diff] [blame] | 447 | 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 Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 452 | |
| 453 | if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask)) |
| 454 | balance_ref_count(chan); |
| 455 | |
| 456 | out: |
| 457 | chan->client_count++; |
| 458 | return 0; |
| 459 | |
| 460 | err_out: |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 461 | dma_device_put(chan->device); |
| 462 | module_put_out: |
Maxime Ripard | d2f4f99 | 2014-11-17 14:41:58 +0100 | [diff] [blame] | 463 | module_put(owner); |
| 464 | return ret; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 465 | } |
| 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 | */ |
| 473 | static void dma_chan_put(struct dma_chan *chan) |
| 474 | { |
Maxime Ripard | c4b54a6 | 2014-11-17 14:41:59 +0100 | [diff] [blame] | 475 | /* This channel is not in use, bail out */ |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 476 | if (!chan->client_count) |
Maxime Ripard | c4b54a6 | 2014-11-17 14:41:59 +0100 | [diff] [blame] | 477 | return; |
| 478 | |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 479 | chan->client_count--; |
Maxime Ripard | c4b54a6 | 2014-11-17 14:41:59 +0100 | [diff] [blame] | 480 | |
| 481 | /* This channel is not in use anymore, free it */ |
Lars-Peter Clausen | b36f09c | 2015-10-20 11:46:28 +0200 | [diff] [blame] | 482 | if (!chan->client_count && chan->device->device_free_chan_resources) { |
| 483 | /* Make sure all operations have completed */ |
| 484 | dmaengine_synchronize(chan); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 485 | chan->device->device_free_chan_resources(chan); |
Lars-Peter Clausen | b36f09c | 2015-10-20 11:46:28 +0200 | [diff] [blame] | 486 | } |
Peter Ujfalusi | 56f13c0 | 2015-04-09 12:35:47 +0300 | [diff] [blame] | 487 | |
| 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 Koul | 83c7794 | 2019-12-24 10:22:15 +0530 | [diff] [blame] | 494 | |
| 495 | dma_device_put(chan->device); |
| 496 | module_put(dma_chan_to_owner(chan)); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 499 | enum 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 Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 508 | dev_err(chan->device->dev, "%s: timeout!\n", __func__); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 509 | return DMA_ERROR; |
| 510 | } |
Bartlomiej Zolnierkiewicz | 2cbe7fe | 2012-11-08 10:02:07 +0000 | [diff] [blame] | 511 | if (status != DMA_IN_PROGRESS) |
| 512 | break; |
| 513 | cpu_relax(); |
| 514 | } while (1); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 515 | |
| 516 | return status; |
| 517 | } |
| 518 | EXPORT_SYMBOL(dma_sync_wait); |
| 519 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 520 | /** |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 521 | * dma_find_channel - find a channel to carry out the operation |
| 522 | * @tx_type: transaction type |
| 523 | */ |
| 524 | struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type) |
| 525 | { |
Christoph Lameter | e7dcaa4 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 526 | return this_cpu_read(channel_table[tx_type]->chan); |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 527 | } |
| 528 | EXPORT_SYMBOL(dma_find_channel); |
| 529 | |
| 530 | /** |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 531 | * dma_issue_pending_all - flush all pending operations across all channels |
| 532 | */ |
| 533 | void dma_issue_pending_all(void) |
| 534 | { |
| 535 | struct dma_device *device; |
| 536 | struct dma_chan *chan; |
| 537 | |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 538 | rcu_read_lock(); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 539 | list_for_each_entry_rcu(device, &dma_device_list, global_node) { |
| 540 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 541 | continue; |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 542 | list_for_each_entry(chan, &device->channels, device_node) |
| 543 | if (chan->client_count) |
| 544 | device->device_issue_pending(chan); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 545 | } |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 546 | rcu_read_unlock(); |
| 547 | } |
| 548 | EXPORT_SYMBOL(dma_issue_pending_all); |
| 549 | |
Laurent Pinchart | 0d5484b | 2014-10-29 00:30:58 +0200 | [diff] [blame] | 550 | int 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 Shevchenko | dd4e91d | 2016-05-10 20:43:34 +0300 | [diff] [blame] | 560 | if (!(test_bit(DMA_SLAVE, device->cap_mask.bits) || |
| 561 | test_bit(DMA_CYCLIC, device->cap_mask.bits))) |
Laurent Pinchart | 0d5484b | 2014-10-29 00:30:58 +0200 | [diff] [blame] | 562 | 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 Lin | 6d5bbed | 2016-01-22 19:06:50 +0800 | [diff] [blame] | 575 | caps->max_burst = device->max_burst; |
Laurent Pinchart | 0d5484b | 2014-10-29 00:30:58 +0200 | [diff] [blame] | 576 | caps->residue_granularity = device->residue_granularity; |
Robert Jarzmik | 9eeacd3 | 2015-10-13 21:54:29 +0200 | [diff] [blame] | 577 | caps->descriptor_reuse = device->descriptor_reuse; |
Marek Szyprowski | d8095f9 | 2018-07-02 15:08:10 +0200 | [diff] [blame] | 578 | caps->cmd_pause = !!device->device_pause; |
| 579 | caps->cmd_resume = !!device->device_resume; |
Laurent Pinchart | 0d5484b | 2014-10-29 00:30:58 +0200 | [diff] [blame] | 580 | caps->cmd_terminate = !!device->device_terminate_all; |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | EXPORT_SYMBOL_GPL(dma_get_slave_caps); |
| 585 | |
Lars-Peter Clausen | a53e28d | 2013-03-25 13:23:52 +0100 | [diff] [blame] | 586 | static struct dma_chan *private_candidate(const dma_cap_mask_t *mask, |
| 587 | struct dma_device *dev, |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 588 | dma_filter_fn fn, void *fn_param) |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 589 | { |
| 590 | struct dma_chan *chan; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 591 | |
Geert Uytterhoeven | 69b1189 | 2020-01-21 10:33:09 +0100 | [diff] [blame] | 592 | if (mask && !dma_device_satisfies_mask(dev, mask)) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 593 | dev_dbg(dev->dev, "%s: wrong capabilities\n", __func__); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 594 | 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 Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 608 | dev_dbg(dev->dev, "%s: %s busy\n", |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 609 | __func__, dma_chan_name(chan)); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 610 | continue; |
| 611 | } |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 612 | if (fn && !fn(chan, fn_param)) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 613 | dev_dbg(dev->dev, "%s: %s filter said false\n", |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 614 | __func__, dma_chan_name(chan)); |
| 615 | continue; |
| 616 | } |
| 617 | return chan; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 620 | return NULL; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 623 | static 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 Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 642 | dev_dbg(device->dev, "%s: %s module removed\n", |
| 643 | __func__, dma_chan_name(chan)); |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 644 | list_del_rcu(&device->global_node); |
| 645 | } else |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 646 | dev_dbg(device->dev, |
| 647 | "%s: failed to get %s: (%d)\n", |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 648 | __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 Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 660 | /** |
Stefan Agner | 19d643d | 2015-06-01 23:53:43 +0200 | [diff] [blame] | 661 | * dma_get_slave_channel - try to get specific channel exclusively |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 662 | * @chan: target channel |
| 663 | */ |
| 664 | struct 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 Koul | d9a6c8f | 2013-08-19 10:47:26 +0530 | [diff] [blame] | 671 | if (chan->client_count == 0) { |
Peter Ujfalusi | 214fc4e | 2015-09-24 12:03:35 +0300 | [diff] [blame] | 672 | struct dma_device *device = chan->device; |
| 673 | |
| 674 | dma_cap_set(DMA_PRIVATE, device->cap_mask); |
| 675 | device->privatecnt++; |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 676 | err = dma_chan_get(chan); |
Peter Ujfalusi | 214fc4e | 2015-09-24 12:03:35 +0300 | [diff] [blame] | 677 | if (err) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 678 | dev_dbg(chan->device->dev, |
| 679 | "%s: failed to get %s: (%d)\n", |
Vinod Koul | d9a6c8f | 2013-08-19 10:47:26 +0530 | [diff] [blame] | 680 | __func__, dma_chan_name(chan), err); |
Peter Ujfalusi | 214fc4e | 2015-09-24 12:03:35 +0300 | [diff] [blame] | 681 | chan = NULL; |
| 682 | if (--device->privatecnt == 0) |
| 683 | dma_cap_clear(DMA_PRIVATE, device->cap_mask); |
| 684 | } |
Vinod Koul | d9a6c8f | 2013-08-19 10:47:26 +0530 | [diff] [blame] | 685 | } else |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 686 | chan = NULL; |
| 687 | |
| 688 | mutex_unlock(&dma_list_mutex); |
| 689 | |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 690 | |
| 691 | return chan; |
| 692 | } |
| 693 | EXPORT_SYMBOL_GPL(dma_get_slave_channel); |
| 694 | |
Stephen Warren | 8010dad | 2013-11-26 12:40:51 -0700 | [diff] [blame] | 695 | struct dma_chan *dma_get_any_slave_channel(struct dma_device *device) |
| 696 | { |
| 697 | dma_cap_mask_t mask; |
| 698 | struct dma_chan *chan; |
Stephen Warren | 8010dad | 2013-11-26 12:40:51 -0700 | [diff] [blame] | 699 | |
| 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 Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 706 | chan = find_candidate(device, &mask, NULL, NULL); |
Stephen Warren | 8010dad | 2013-11-26 12:40:51 -0700 | [diff] [blame] | 707 | |
| 708 | mutex_unlock(&dma_list_mutex); |
| 709 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 710 | return IS_ERR(chan) ? NULL : chan; |
Stephen Warren | 8010dad | 2013-11-26 12:40:51 -0700 | [diff] [blame] | 711 | } |
| 712 | EXPORT_SYMBOL_GPL(dma_get_any_slave_channel); |
| 713 | |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 714 | /** |
Daniel Mack | 6b9019a | 2013-08-14 18:35:03 +0200 | [diff] [blame] | 715 | * __dma_request_channel - try to allocate an exclusive channel |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 716 | * @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 Wang | f515131 | 2019-05-20 19:32:14 +0800 | [diff] [blame] | 719 | * @np: device node to look for DMA channels |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 720 | * |
| 721 | * Returns pointer to appropriate DMA channel on success or NULL. |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 722 | */ |
Lars-Peter Clausen | a53e28d | 2013-03-25 13:23:52 +0100 | [diff] [blame] | 723 | struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask, |
Baolin Wang | f515131 | 2019-05-20 19:32:14 +0800 | [diff] [blame] | 724 | dma_filter_fn fn, void *fn_param, |
| 725 | struct device_node *np) |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 726 | { |
| 727 | struct dma_device *device, *_d; |
| 728 | struct dma_chan *chan = NULL; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 729 | |
| 730 | /* Find a channel */ |
| 731 | mutex_lock(&dma_list_mutex); |
| 732 | list_for_each_entry_safe(device, _d, &dma_device_list, global_node) { |
Baolin Wang | f515131 | 2019-05-20 19:32:14 +0800 | [diff] [blame] | 733 | /* Finds a DMA controller with matching device node */ |
| 734 | if (np && device->dev->of_node && np != device->dev->of_node) |
| 735 | continue; |
| 736 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 737 | chan = find_candidate(device, mask, fn, fn_param); |
| 738 | if (!IS_ERR(chan)) |
| 739 | break; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 740 | |
Peter Ujfalusi | 7bd903c | 2015-12-14 22:47:39 +0200 | [diff] [blame] | 741 | chan = NULL; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 742 | } |
| 743 | mutex_unlock(&dma_list_mutex); |
| 744 | |
Jarkko Nikula | 4c4d7f87 | 2016-04-07 16:49:43 +0300 | [diff] [blame] | 745 | pr_debug("%s: %s (%s)\n", |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 746 | __func__, |
| 747 | chan ? "success" : "fail", |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 748 | chan ? dma_chan_name(chan) : NULL); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 749 | |
| 750 | return chan; |
| 751 | } |
| 752 | EXPORT_SYMBOL_GPL(__dma_request_channel); |
| 753 | |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 754 | static 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 Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 774 | /** |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 775 | * dma_request_chan - try to allocate an exclusive slave channel |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 776 | * @dev: pointer to client device structure |
| 777 | * @name: slave channel name |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 778 | * |
| 779 | * Returns pointer to appropriate DMA channel on success or an error pointer. |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 780 | */ |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 781 | struct dma_chan *dma_request_chan(struct device *dev, const char *name) |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 782 | { |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 783 | struct dma_device *d, *_d; |
| 784 | struct dma_chan *chan = NULL; |
| 785 | |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 786 | /* If device-tree is present get slave info from here */ |
| 787 | if (dev->of_node) |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 788 | chan = of_dma_request_slave_channel(dev->of_node, name); |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 789 | |
Andy Shevchenko | 4e82f5d | 2013-04-09 14:05:44 +0300 | [diff] [blame] | 790 | /* If device was enumerated by ACPI get slave info from here */ |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 791 | if (has_acpi_companion(dev) && !chan) |
| 792 | chan = acpi_dma_request_slave_chan_by_name(dev, name); |
Andy Shevchenko | 4e82f5d | 2013-04-09 14:05:44 +0300 | [diff] [blame] | 793 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 794 | if (PTR_ERR(chan) == -EPROBE_DEFER) |
| 795 | return chan; |
| 796 | |
| 797 | if (!IS_ERR_OR_NULL(chan)) |
| 798 | goto found; |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 799 | |
| 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 Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 818 | if (IS_ERR_OR_NULL(chan)) |
| 819 | return chan ? chan : ERR_PTR(-EPROBE_DEFER); |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 820 | |
| 821 | found: |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame^] | 822 | #ifdef CONFIG_DEBUG_FS |
| 823 | chan->dbg_client_name = kasprintf(GFP_KERNEL, "%s:%s", dev_name(dev), |
| 824 | name); |
| 825 | #endif |
| 826 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 827 | chan->name = kasprintf(GFP_KERNEL, "dma:%s", name); |
| 828 | if (!chan->name) |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 829 | return chan; |
| 830 | chan->slave = dev; |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 831 | |
| 832 | if (sysfs_create_link(&chan->dev->device.kobj, &dev->kobj, |
| 833 | DMA_SLAVE_NAME)) |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 834 | dev_warn(dev, "Cannot create DMA %s symlink\n", DMA_SLAVE_NAME); |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 835 | if (sysfs_create_link(&dev->kobj, &chan->dev->device.kobj, chan->name)) |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 836 | dev_warn(dev, "Cannot create DMA %s symlink\n", chan->name); |
| 837 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 838 | return chan; |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 839 | } |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 840 | EXPORT_SYMBOL_GPL(dma_request_chan); |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 841 | |
| 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 | */ |
| 849 | struct dma_chan *dma_request_slave_channel(struct device *dev, |
| 850 | const char *name) |
| 851 | { |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 852 | struct dma_chan *ch = dma_request_chan(dev, name); |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 853 | if (IS_ERR(ch)) |
| 854 | return NULL; |
Robert Baldyga | 05aa1a7 | 2015-08-07 12:26:47 +0200 | [diff] [blame] | 855 | |
Stephen Warren | 0ad7c00 | 2013-11-26 10:04:22 -0700 | [diff] [blame] | 856 | return ch; |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 857 | } |
| 858 | EXPORT_SYMBOL_GPL(dma_request_slave_channel); |
| 859 | |
Peter Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 860 | /** |
| 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 | */ |
| 866 | struct 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 Wang | f515131 | 2019-05-20 19:32:14 +0800 | [diff] [blame] | 873 | chan = __dma_request_channel(mask, NULL, NULL, NULL); |
Peter Ujfalusi | ec8ca8e | 2018-07-18 12:29:57 +0300 | [diff] [blame] | 874 | 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 Ujfalusi | a8135d0 | 2015-12-14 22:47:40 +0200 | [diff] [blame] | 882 | |
| 883 | return chan; |
| 884 | } |
| 885 | EXPORT_SYMBOL_GPL(dma_request_chan_by_mask); |
| 886 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 887 | void 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 Nemoto | 0f57151 | 2009-03-06 20:07:14 +0900 | [diff] [blame] | 893 | /* 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 Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 896 | |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 897 | if (chan->slave) { |
Peter Ujfalusi | bad8356 | 2020-01-31 11:38:58 +0200 | [diff] [blame] | 898 | sysfs_remove_link(&chan->dev->device.kobj, DMA_SLAVE_NAME); |
Geert Uytterhoeven | 71723a9 | 2020-01-17 16:30:56 +0100 | [diff] [blame] | 899 | sysfs_remove_link(&chan->slave->kobj, chan->name); |
| 900 | kfree(chan->name); |
| 901 | chan->name = NULL; |
| 902 | chan->slave = NULL; |
| 903 | } |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame^] | 904 | |
| 905 | #ifdef CONFIG_DEBUG_FS |
| 906 | kfree(chan->dbg_client_name); |
| 907 | chan->dbg_client_name = NULL; |
| 908 | #endif |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 909 | mutex_unlock(&dma_list_mutex); |
| 910 | } |
| 911 | EXPORT_SYMBOL_GPL(dma_release_channel); |
| 912 | |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 913 | /** |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 914 | * dmaengine_get - register interest in dma_channels |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 915 | */ |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 916 | void dmaengine_get(void) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 917 | { |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 918 | struct dma_device *device, *_d; |
| 919 | struct dma_chan *chan; |
| 920 | int err; |
| 921 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 922 | mutex_lock(&dma_list_mutex); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 923 | dmaengine_ref_count++; |
| 924 | |
| 925 | /* try to grab channels */ |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 926 | 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 Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 929 | 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 Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 933 | list_del_rcu(&device->global_node); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 934 | break; |
| 935 | } else if (err) |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 936 | dev_dbg(chan->device->dev, |
| 937 | "%s: failed to get %s: (%d)\n", |
| 938 | __func__, dma_chan_name(chan), err); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 939 | } |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 940 | } |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 941 | |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 942 | /* 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 Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 948 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 949 | } |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 950 | EXPORT_SYMBOL(dmaengine_get); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 951 | |
| 952 | /** |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 953 | * dmaengine_put - let dma drivers be removed when ref_count == 0 |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 954 | */ |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 955 | void dmaengine_put(void) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 956 | { |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 957 | struct dma_device *device, *_d; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 958 | struct dma_chan *chan; |
| 959 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 960 | mutex_lock(&dma_list_mutex); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 961 | dmaengine_ref_count--; |
| 962 | BUG_ON(dmaengine_ref_count < 0); |
| 963 | /* drop channel references */ |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 964 | list_for_each_entry_safe(device, _d, &dma_device_list, global_node) { |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 965 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 966 | continue; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 967 | list_for_each_entry(chan, &device->channels, device_node) |
| 968 | dma_chan_put(chan); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 969 | } |
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_put); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 973 | |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 974 | static 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 Canillas | d57d3a4 | 2016-05-11 13:39:27 -0400 | [diff] [blame] | 985 | #if IS_ENABLED(CONFIG_ASYNC_MEMCPY) |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 986 | if (!dma_has_cap(DMA_MEMCPY, device->cap_mask)) |
| 987 | return false; |
| 988 | #endif |
| 989 | |
Javier Martinez Canillas | d57d3a4 | 2016-05-11 13:39:27 -0400 | [diff] [blame] | 990 | #if IS_ENABLED(CONFIG_ASYNC_XOR) |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 991 | if (!dma_has_cap(DMA_XOR, device->cap_mask)) |
| 992 | return false; |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 993 | |
| 994 | #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA |
Dan Williams | 4499a24 | 2009-11-19 17:10:25 -0700 | [diff] [blame] | 995 | if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask)) |
| 996 | return false; |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 997 | #endif |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 998 | #endif |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 999 | |
Javier Martinez Canillas | d57d3a4 | 2016-05-11 13:39:27 -0400 | [diff] [blame] | 1000 | #if IS_ENABLED(CONFIG_ASYNC_PQ) |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1001 | if (!dma_has_cap(DMA_PQ, device->cap_mask)) |
| 1002 | return false; |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 1003 | |
| 1004 | #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA |
Dan Williams | 4499a24 | 2009-11-19 17:10:25 -0700 | [diff] [blame] | 1005 | if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask)) |
| 1006 | return false; |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1007 | #endif |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 1008 | #endif |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1009 | |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1013 | static int get_dma_id(struct dma_device *device) |
| 1014 | { |
Matthew Wilcox | 485258b | 2018-06-18 15:41:48 -0400 | [diff] [blame] | 1015 | int rc = ida_alloc(&dma_ida, GFP_KERNEL); |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1016 | |
Matthew Wilcox | 485258b | 2018-06-18 15:41:48 -0400 | [diff] [blame] | 1017 | if (rc < 0) |
| 1018 | return rc; |
| 1019 | device->dev_id = rc; |
| 1020 | return 0; |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1023 | static 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 Jiang | 5429b51 | 2020-01-31 10:58:39 -0700 | [diff] [blame] | 1034 | if (!tchan) |
| 1035 | return -ENODEV; |
| 1036 | |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1037 | 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 Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1086 | int 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 | } |
| 1098 | EXPORT_SYMBOL_GPL(dma_async_device_channel_register); |
| 1099 | |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1100 | static 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 Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1107 | list_del(&chan->device_node); |
| 1108 | device->chancnt--; |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1109 | chan->dev->chan = NULL; |
| 1110 | mutex_unlock(&dma_list_mutex); |
| 1111 | device_unregister(&chan->dev->device); |
| 1112 | free_percpu(chan->local); |
| 1113 | } |
| 1114 | |
Dave Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1115 | void 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 | } |
| 1121 | EXPORT_SYMBOL_GPL(dma_async_device_channel_unregister); |
| 1122 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1123 | /** |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 1124 | * dma_async_device_register - registers DMA devices found |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1125 | * @device: &dma_device |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 1126 | * |
| 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 Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1130 | */ |
| 1131 | int dma_async_device_register(struct dma_device *device) |
| 1132 | { |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1133 | int rc, i = 0; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1134 | struct dma_chan* chan; |
| 1135 | |
| 1136 | if (!device) |
| 1137 | return -ENODEV; |
| 1138 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1139 | /* validate device routines */ |
Vinod Koul | 3eeb515 | 2017-08-27 16:55:32 +0530 | [diff] [blame] | 1140 | if (!device->dev) { |
| 1141 | pr_err("DMAdevice must have dev\n"); |
| 1142 | return -EIO; |
| 1143 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1144 | |
Logan Gunthorpe | dae7a58 | 2019-12-16 12:01:16 -0700 | [diff] [blame] | 1145 | device->owner = device->dev->driver->owner; |
| 1146 | |
Vinod Koul | 3eeb515 | 2017-08-27 16:55:32 +0530 | [diff] [blame] | 1147 | 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 Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1221 | |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 1222 | 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 Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1228 | /* note: this only matters in the |
Dan Williams | 5fc6d89 | 2010-10-07 16:44:50 -0700 | [diff] [blame] | 1229 | * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 1230 | */ |
| 1231 | if (device_has_all_tx_types(device)) |
| 1232 | dma_cap_set(DMA_ASYNC_TX, device->cap_mask); |
| 1233 | |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1234 | rc = get_dma_id(device); |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1235 | if (rc != 0) |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1236 | return rc; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1237 | |
| 1238 | /* represent channels in sysfs. Probably want devs too */ |
| 1239 | list_for_each_entry(chan, &device->channels, device_node) { |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1240 | rc = __dma_async_device_channel_register(device, chan, i++); |
| 1241 | if (rc < 0) |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1242 | goto err_out; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1243 | } |
Viresh Kumar | 76d7b84 | 2016-07-27 14:32:58 -0700 | [diff] [blame] | 1244 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1245 | mutex_lock(&dma_list_mutex); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 1246 | /* take references on public channels */ |
| 1247 | if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1248 | 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 Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1262 | list_add_tail_rcu(&device->global_node, &dma_device_list); |
Atsushi Nemoto | 0f57151 | 2009-03-06 20:07:14 +0900 | [diff] [blame] | 1263 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 1264 | device->privatecnt++; /* Always private */ |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1265 | dma_channel_rebalance(); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1266 | mutex_unlock(&dma_list_mutex); |
| 1267 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1268 | return 0; |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 1269 | |
| 1270 | err_out: |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1271 | /* if we never registered a channel just release the idr */ |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1272 | if (!device->chancnt) { |
Matthew Wilcox | 485258b | 2018-06-18 15:41:48 -0400 | [diff] [blame] | 1273 | ida_free(&dma_ida, device->dev_id); |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 1274 | return rc; |
| 1275 | } |
| 1276 | |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 1277 | list_for_each_entry(chan, &device->channels, device_node) { |
| 1278 | if (chan->local == NULL) |
| 1279 | continue; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1280 | mutex_lock(&dma_list_mutex); |
| 1281 | chan->dev->chan = NULL; |
| 1282 | mutex_unlock(&dma_list_mutex); |
| 1283 | device_unregister(&chan->dev->device); |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 1284 | free_percpu(chan->local); |
| 1285 | } |
| 1286 | return rc; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1287 | } |
David Brownell | 765e3d8 | 2007-03-16 13:38:05 -0800 | [diff] [blame] | 1288 | EXPORT_SYMBOL(dma_async_device_register); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1289 | |
| 1290 | /** |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1291 | * dma_async_device_unregister - unregister a DMA device |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 1292 | * @device: &dma_device |
Dan Williams | f27c580 | 2009-01-06 11:38:18 -0700 | [diff] [blame] | 1293 | * |
| 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 Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 1296 | */ |
| 1297 | void dma_async_device_unregister(struct dma_device *device) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1298 | { |
Dave Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1299 | struct dma_chan *chan, *n; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1300 | |
Dave Jiang | e81274c | 2020-01-21 16:43:53 -0700 | [diff] [blame] | 1301 | list_for_each_entry_safe(chan, n, &device->channels, device_node) |
Dave Jiang | d2fb0a0 | 2020-01-21 16:43:47 -0700 | [diff] [blame] | 1302 | __dma_async_device_channel_unregister(device, chan); |
Logan Gunthorpe | 8ad342a | 2019-12-16 12:01:19 -0700 | [diff] [blame] | 1303 | |
| 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 Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1313 | } |
David Brownell | 765e3d8 | 2007-03-16 13:38:05 -0800 | [diff] [blame] | 1314 | EXPORT_SYMBOL(dma_async_device_unregister); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1315 | |
Huang Shijie | f39b948 | 2018-07-26 14:45:53 +0800 | [diff] [blame] | 1316 | static 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 | */ |
| 1330 | int 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 | } |
| 1349 | EXPORT_SYMBOL(dmaenginem_async_device_register); |
| 1350 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1351 | struct 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) } |
| 1359 | static struct dmaengine_unmap_pool unmap_pool[] = { |
| 1360 | __UNMAP_POOL(2), |
Dan Williams | 3cc377b | 2013-12-09 10:33:16 -0800 | [diff] [blame] | 1361 | #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID) |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1362 | __UNMAP_POOL(16), |
| 1363 | __UNMAP_POOL(128), |
| 1364 | __UNMAP_POOL(256), |
| 1365 | #endif |
| 1366 | }; |
| 1367 | |
| 1368 | static struct dmaengine_unmap_pool *__get_unmap_pool(int nr) |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1369 | { |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1370 | int order = get_count_order(nr); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1371 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1372 | switch (order) { |
| 1373 | case 0 ... 1: |
| 1374 | return &unmap_pool[0]; |
Matthias Kaehlcke | 23f963e9 | 2017-03-13 14:30:29 -0700 | [diff] [blame] | 1375 | #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID) |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1376 | 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 Kaehlcke | 23f963e9 | 2017-03-13 14:30:29 -0700 | [diff] [blame] | 1382 | #endif |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1383 | default: |
| 1384 | BUG(); |
| 1385 | return NULL; |
| 1386 | } |
| 1387 | } |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1388 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1389 | static 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 Williams | 7476bd79 | 2013-10-18 19:35:29 +0200 | [diff] [blame] | 1404 | for (; i < cnt; i++) { |
| 1405 | if (unmap->addr[i] == 0) |
| 1406 | continue; |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1407 | dma_unmap_page(dev, unmap->addr[i], unmap->len, |
| 1408 | DMA_BIDIRECTIONAL); |
Dan Williams | 7476bd79 | 2013-10-18 19:35:29 +0200 | [diff] [blame] | 1409 | } |
Xuelin Shi | c1f43dd | 2014-05-21 14:02:37 -0700 | [diff] [blame] | 1410 | cnt = unmap->map_cnt; |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1411 | mempool_free(unmap, __get_unmap_pool(cnt)->pool); |
| 1412 | } |
| 1413 | |
| 1414 | void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap) |
| 1415 | { |
| 1416 | if (unmap) |
| 1417 | kref_put(&unmap->kref, dmaengine_unmap); |
| 1418 | } |
| 1419 | EXPORT_SYMBOL_GPL(dmaengine_unmap_put); |
| 1420 | |
| 1421 | static 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 Lawall | 240eb916 | 2015-09-13 14:15:19 +0200 | [diff] [blame] | 1428 | mempool_destroy(p->pool); |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1429 | p->pool = NULL; |
Julia Lawall | 240eb916 | 2015-09-13 14:15:19 +0200 | [diff] [blame] | 1430 | kmem_cache_destroy(p->cache); |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1431 | p->cache = NULL; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | static 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 Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1453 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1454 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1455 | if (i == ARRAY_SIZE(unmap_pool)) |
| 1456 | return 0; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1457 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1458 | dmaengine_destroy_unmap_pool(); |
| 1459 | return -ENOMEM; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1460 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1461 | |
Dan Williams | 8971646 | 2013-10-18 19:35:25 +0200 | [diff] [blame] | 1462 | struct dmaengine_unmap_data * |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1463 | dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags) |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1464 | { |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1465 | struct dmaengine_unmap_data *unmap; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1466 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1467 | unmap = mempool_alloc(__get_unmap_pool(nr)->pool, flags); |
| 1468 | if (!unmap) |
| 1469 | return NULL; |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1470 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1471 | memset(unmap, 0, sizeof(*unmap)); |
| 1472 | kref_init(&unmap->kref); |
| 1473 | unmap->dev = dev; |
Xuelin Shi | c1f43dd | 2014-05-21 14:02:37 -0700 | [diff] [blame] | 1474 | unmap->map_cnt = nr; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1475 | |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1476 | return unmap; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1477 | } |
Dan Williams | 8971646 | 2013-10-18 19:35:25 +0200 | [diff] [blame] | 1478 | EXPORT_SYMBOL(dmaengine_get_unmap_data); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1479 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1480 | void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx, |
| 1481 | struct dma_chan *chan) |
| 1482 | { |
| 1483 | tx->chan = chan; |
Dan Williams | 5fc6d89 | 2010-10-07 16:44:50 -0700 | [diff] [blame] | 1484 | #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1485 | spin_lock_init(&tx->lock); |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1486 | #endif |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1487 | } |
| 1488 | EXPORT_SYMBOL(dma_async_tx_descriptor_init); |
| 1489 | |
Peter Ujfalusi | 4db8fd3 | 2019-12-23 13:04:44 +0200 | [diff] [blame] | 1490 | static 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 | |
| 1506 | int 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 | } |
| 1523 | EXPORT_SYMBOL_GPL(dmaengine_desc_attach_metadata); |
| 1524 | |
| 1525 | void *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 | } |
| 1542 | EXPORT_SYMBOL_GPL(dmaengine_desc_get_metadata_ptr); |
| 1543 | |
| 1544 | int 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 | } |
| 1561 | EXPORT_SYMBOL_GPL(dmaengine_desc_set_metadata_len); |
| 1562 | |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1563 | /* dma_wait_for_async_tx - spin wait for a transaction to complete |
| 1564 | * @tx: in-flight transaction to wait on |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1565 | */ |
| 1566 | enum dma_status |
| 1567 | dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx) |
| 1568 | { |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1569 | unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1570 | |
| 1571 | if (!tx) |
Vinod Koul | adfedd9 | 2013-10-16 13:29:02 +0530 | [diff] [blame] | 1572 | return DMA_COMPLETE; |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1573 | |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1574 | while (tx->cookie == -EBUSY) { |
| 1575 | if (time_after_eq(jiffies, dma_sync_wait_timeout)) { |
Jarkko Nikula | ef85931 | 2016-03-14 16:51:09 +0200 | [diff] [blame] | 1576 | dev_err(tx->chan->device->dev, |
| 1577 | "%s timeout waiting for descriptor submission\n", |
| 1578 | __func__); |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1579 | return DMA_ERROR; |
| 1580 | } |
| 1581 | cpu_relax(); |
| 1582 | } |
| 1583 | return dma_sync_wait(tx->chan, tx->cookie); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1584 | } |
| 1585 | EXPORT_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 | */ |
| 1591 | void dma_run_dependencies(struct dma_async_tx_descriptor *tx) |
| 1592 | { |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1593 | struct dma_async_tx_descriptor *dep = txd_next(tx); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1594 | struct dma_async_tx_descriptor *dep_next; |
| 1595 | struct dma_chan *chan; |
| 1596 | |
| 1597 | if (!dep) |
| 1598 | return; |
| 1599 | |
Yuri Tikhonov | dd59b85 | 2009-01-12 15:17:20 -0700 | [diff] [blame] | 1600 | /* we'll submit tx->next now, so clear the link */ |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1601 | txd_clear_next(tx); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1602 | 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 Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1609 | txd_lock(dep); |
| 1610 | txd_clear_parent(dep); |
| 1611 | dep_next = txd_next(dep); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1612 | if (dep_next && dep_next->chan == chan) |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1613 | txd_clear_next(dep); /* ->next will be submitted */ |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1614 | else |
| 1615 | dep_next = NULL; /* submit current dep and terminate */ |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1616 | txd_unlock(dep); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1617 | |
| 1618 | dep->tx_submit(dep); |
| 1619 | } |
| 1620 | |
| 1621 | chan->device->device_issue_pending(chan); |
| 1622 | } |
| 1623 | EXPORT_SYMBOL_GPL(dma_run_dependencies); |
| 1624 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1625 | static int __init dma_bus_init(void) |
| 1626 | { |
Dan Williams | 45c463a | 2013-10-18 19:35:24 +0200 | [diff] [blame] | 1627 | int err = dmaengine_init_unmap_pool(); |
| 1628 | |
| 1629 | if (err) |
| 1630 | return err; |
Peter Ujfalusi | e937cc1 | 2020-03-06 16:28:37 +0200 | [diff] [blame^] | 1631 | |
| 1632 | err = class_register(&dma_devclass); |
| 1633 | if (!err) |
| 1634 | dmaengine_debugfs_init(); |
| 1635 | |
| 1636 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1637 | } |
Dan Williams | 652afc2 | 2009-01-06 11:38:22 -0700 | [diff] [blame] | 1638 | arch_initcall(dma_bus_init); |