Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; either version 2 of the License, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 59 |
| 16 | * Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * The full GNU General Public License is included in this distribution in the |
| 19 | * file called COPYING. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This code implements the DMA subsystem. It provides a HW-neutral interface |
| 24 | * for other kernel code to use asynchronous memory copy capabilities, |
| 25 | * if present, and allows different HW DMA drivers to register as providing |
| 26 | * this capability. |
| 27 | * |
| 28 | * Due to the fact we are accelerating what is already a relatively fast |
| 29 | * operation, the code goes to great lengths to avoid additional overhead, |
| 30 | * such as locking. |
| 31 | * |
| 32 | * LOCKING: |
| 33 | * |
Dan Williams | aa1e6f1 | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 34 | * The subsystem keeps a global list of dma_device structs it is protected by a |
| 35 | * mutex, dma_list_mutex. |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 36 | * |
Dan Williams | f27c580 | 2009-01-06 11:38:18 -0700 | [diff] [blame] | 37 | * A subsystem can get access to a channel by calling dmaengine_get() followed |
| 38 | * by dma_find_channel(), or if it has need for an exclusive channel it can call |
| 39 | * dma_request_channel(). Once a channel is allocated a reference is taken |
| 40 | * against its corresponding driver to disable removal. |
| 41 | * |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 42 | * Each device has a channels list, which runs unlocked but is never modified |
| 43 | * once the device is registered, it's just setup by the driver. |
| 44 | * |
Dan Williams | f27c580 | 2009-01-06 11:38:18 -0700 | [diff] [blame] | 45 | * See Documentation/dmaengine.txt for more details |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 46 | */ |
| 47 | |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 48 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 49 | |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 50 | #include <linux/dma-mapping.h> |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 51 | #include <linux/init.h> |
| 52 | #include <linux/module.h> |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 53 | #include <linux/mm.h> |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 54 | #include <linux/device.h> |
| 55 | #include <linux/dmaengine.h> |
| 56 | #include <linux/hardirq.h> |
| 57 | #include <linux/spinlock.h> |
| 58 | #include <linux/percpu.h> |
| 59 | #include <linux/rcupdate.h> |
| 60 | #include <linux/mutex.h> |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 61 | #include <linux/jiffies.h> |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 62 | #include <linux/rculist.h> |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 63 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 64 | #include <linux/slab.h> |
Andy Shevchenko | 4e82f5d | 2013-04-09 14:05:44 +0300 | [diff] [blame] | 65 | #include <linux/acpi.h> |
| 66 | #include <linux/acpi_dma.h> |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 67 | #include <linux/of_dma.h> |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 68 | |
| 69 | static DEFINE_MUTEX(dma_list_mutex); |
Axel Lin | 21ef4b8 | 2011-07-20 11:32:28 +0800 | [diff] [blame] | 70 | static DEFINE_IDR(dma_idr); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 71 | static LIST_HEAD(dma_device_list); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 72 | static long dmaengine_ref_count; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 73 | |
| 74 | /* --- sysfs implementation --- */ |
| 75 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 76 | /** |
| 77 | * dev_to_dma_chan - convert a device pointer to the its sysfs container object |
| 78 | * @dev - device node |
| 79 | * |
| 80 | * Must be called under dma_list_mutex |
| 81 | */ |
| 82 | static struct dma_chan *dev_to_dma_chan(struct device *dev) |
| 83 | { |
| 84 | struct dma_chan_dev *chan_dev; |
| 85 | |
| 86 | chan_dev = container_of(dev, typeof(*chan_dev), device); |
| 87 | return chan_dev->chan; |
| 88 | } |
| 89 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 90 | static ssize_t memcpy_count_show(struct device *dev, |
| 91 | struct device_attribute *attr, char *buf) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 92 | { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 93 | struct dma_chan *chan; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 94 | unsigned long count = 0; |
| 95 | int i; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 96 | int err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 97 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 98 | mutex_lock(&dma_list_mutex); |
| 99 | chan = dev_to_dma_chan(dev); |
| 100 | if (chan) { |
| 101 | for_each_possible_cpu(i) |
| 102 | count += per_cpu_ptr(chan->local, i)->memcpy_count; |
| 103 | err = sprintf(buf, "%lu\n", count); |
| 104 | } else |
| 105 | err = -ENODEV; |
| 106 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 107 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 108 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 109 | } |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 110 | static DEVICE_ATTR_RO(memcpy_count); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 111 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 112 | static ssize_t bytes_transferred_show(struct device *dev, |
| 113 | struct device_attribute *attr, char *buf) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 114 | { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 115 | struct dma_chan *chan; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 116 | unsigned long count = 0; |
| 117 | int i; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 118 | int err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 119 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 120 | mutex_lock(&dma_list_mutex); |
| 121 | chan = dev_to_dma_chan(dev); |
| 122 | if (chan) { |
| 123 | for_each_possible_cpu(i) |
| 124 | count += per_cpu_ptr(chan->local, i)->bytes_transferred; |
| 125 | err = sprintf(buf, "%lu\n", count); |
| 126 | } else |
| 127 | err = -ENODEV; |
| 128 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 129 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 130 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 131 | } |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 132 | static DEVICE_ATTR_RO(bytes_transferred); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 133 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 134 | static ssize_t in_use_show(struct device *dev, struct device_attribute *attr, |
| 135 | char *buf) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 136 | { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 137 | struct dma_chan *chan; |
| 138 | int err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 139 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 140 | mutex_lock(&dma_list_mutex); |
| 141 | chan = dev_to_dma_chan(dev); |
| 142 | if (chan) |
| 143 | err = sprintf(buf, "%d\n", chan->client_count); |
| 144 | else |
| 145 | err = -ENODEV; |
| 146 | mutex_unlock(&dma_list_mutex); |
| 147 | |
| 148 | return err; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 149 | } |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 150 | static DEVICE_ATTR_RO(in_use); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 151 | |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 152 | static struct attribute *dma_dev_attrs[] = { |
| 153 | &dev_attr_memcpy_count.attr, |
| 154 | &dev_attr_bytes_transferred.attr, |
| 155 | &dev_attr_in_use.attr, |
| 156 | NULL, |
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 | ATTRIBUTE_GROUPS(dma_dev); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 159 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 160 | static void chan_dev_release(struct device *dev) |
| 161 | { |
| 162 | struct dma_chan_dev *chan_dev; |
| 163 | |
| 164 | chan_dev = container_of(dev, typeof(*chan_dev), device); |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 165 | if (atomic_dec_and_test(chan_dev->idr_ref)) { |
| 166 | mutex_lock(&dma_list_mutex); |
| 167 | idr_remove(&dma_idr, chan_dev->dev_id); |
| 168 | mutex_unlock(&dma_list_mutex); |
| 169 | kfree(chan_dev->idr_ref); |
| 170 | } |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 171 | kfree(chan_dev); |
| 172 | } |
| 173 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 174 | static struct class dma_devclass = { |
Tony Jones | 891f78e | 2007-09-25 02:03:03 +0200 | [diff] [blame] | 175 | .name = "dma", |
Greg Kroah-Hartman | 58b267d | 2013-07-24 15:05:08 -0700 | [diff] [blame] | 176 | .dev_groups = dma_dev_groups, |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 177 | .dev_release = chan_dev_release, |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | /* --- client and device registration --- */ |
| 181 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 182 | #define dma_device_satisfies_mask(device, mask) \ |
| 183 | __dma_device_satisfies_mask((device), &(mask)) |
Dan Williams | d379b01 | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 184 | static int |
Lars-Peter Clausen | a53e28d | 2013-03-25 13:23:52 +0100 | [diff] [blame] | 185 | __dma_device_satisfies_mask(struct dma_device *device, |
| 186 | const dma_cap_mask_t *want) |
Dan Williams | d379b01 | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 187 | { |
| 188 | dma_cap_mask_t has; |
| 189 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 190 | bitmap_and(has.bits, want->bits, device->cap_mask.bits, |
Dan Williams | d379b01 | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 191 | DMA_TX_TYPE_END); |
| 192 | return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END); |
| 193 | } |
| 194 | |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 195 | static struct module *dma_chan_to_owner(struct dma_chan *chan) |
| 196 | { |
| 197 | return chan->device->dev->driver->owner; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * balance_ref_count - catch up the channel reference count |
| 202 | * @chan - channel to balance ->client_count versus dmaengine_ref_count |
| 203 | * |
| 204 | * balance_ref_count must be called under dma_list_mutex |
| 205 | */ |
| 206 | static void balance_ref_count(struct dma_chan *chan) |
| 207 | { |
| 208 | struct module *owner = dma_chan_to_owner(chan); |
| 209 | |
| 210 | while (chan->client_count < dmaengine_ref_count) { |
| 211 | __module_get(owner); |
| 212 | chan->client_count++; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * dma_chan_get - try to grab a dma channel's parent driver module |
| 218 | * @chan - channel to grab |
| 219 | * |
| 220 | * Must be called under dma_list_mutex |
| 221 | */ |
| 222 | static int dma_chan_get(struct dma_chan *chan) |
| 223 | { |
| 224 | int err = -ENODEV; |
| 225 | struct module *owner = dma_chan_to_owner(chan); |
| 226 | |
| 227 | if (chan->client_count) { |
| 228 | __module_get(owner); |
| 229 | err = 0; |
| 230 | } else if (try_module_get(owner)) |
| 231 | err = 0; |
| 232 | |
| 233 | if (err == 0) |
| 234 | chan->client_count++; |
| 235 | |
| 236 | /* allocate upon first client reference */ |
| 237 | if (chan->client_count == 1 && err == 0) { |
Dan Williams | aa1e6f1 | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 238 | int desc_cnt = chan->device->device_alloc_chan_resources(chan); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 239 | |
| 240 | if (desc_cnt < 0) { |
| 241 | err = desc_cnt; |
| 242 | chan->client_count = 0; |
| 243 | module_put(owner); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 244 | } else if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask)) |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 245 | balance_ref_count(chan); |
| 246 | } |
| 247 | |
| 248 | return err; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * dma_chan_put - drop a reference to a dma channel's parent driver module |
| 253 | * @chan - channel to release |
| 254 | * |
| 255 | * Must be called under dma_list_mutex |
| 256 | */ |
| 257 | static void dma_chan_put(struct dma_chan *chan) |
| 258 | { |
| 259 | if (!chan->client_count) |
| 260 | return; /* this channel failed alloc_chan_resources */ |
| 261 | chan->client_count--; |
| 262 | module_put(dma_chan_to_owner(chan)); |
| 263 | if (chan->client_count == 0) |
| 264 | chan->device->device_free_chan_resources(chan); |
| 265 | } |
| 266 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 267 | enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie) |
| 268 | { |
| 269 | enum dma_status status; |
| 270 | unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000); |
| 271 | |
| 272 | dma_async_issue_pending(chan); |
| 273 | do { |
| 274 | status = dma_async_is_tx_complete(chan, cookie, NULL, NULL); |
| 275 | if (time_after_eq(jiffies, dma_sync_wait_timeout)) { |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 276 | pr_err("%s: timeout!\n", __func__); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 277 | return DMA_ERROR; |
| 278 | } |
Bartlomiej Zolnierkiewicz | 2cbe7fe | 2012-11-08 10:02:07 +0000 | [diff] [blame] | 279 | if (status != DMA_IN_PROGRESS) |
| 280 | break; |
| 281 | cpu_relax(); |
| 282 | } while (1); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 283 | |
| 284 | return status; |
| 285 | } |
| 286 | EXPORT_SYMBOL(dma_sync_wait); |
| 287 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 288 | /** |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 289 | * dma_cap_mask_all - enable iteration over all operation types |
| 290 | */ |
| 291 | static dma_cap_mask_t dma_cap_mask_all; |
| 292 | |
| 293 | /** |
| 294 | * dma_chan_tbl_ent - tracks channel allocations per core/operation |
| 295 | * @chan - associated channel for this entry |
| 296 | */ |
| 297 | struct dma_chan_tbl_ent { |
| 298 | struct dma_chan *chan; |
| 299 | }; |
| 300 | |
| 301 | /** |
| 302 | * channel_table - percpu lookup table for memory-to-memory offload providers |
| 303 | */ |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 304 | static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END]; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 305 | |
| 306 | static int __init dma_channel_table_init(void) |
| 307 | { |
| 308 | enum dma_transaction_type cap; |
| 309 | int err = 0; |
| 310 | |
| 311 | bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END); |
| 312 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 313 | /* 'interrupt', 'private', and 'slave' are channel capabilities, |
| 314 | * but are not associated with an operation so they do not need |
| 315 | * an entry in the channel_table |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 316 | */ |
| 317 | clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 318 | clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits); |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 319 | clear_bit(DMA_SLAVE, dma_cap_mask_all.bits); |
| 320 | |
| 321 | for_each_dma_cap_mask(cap, dma_cap_mask_all) { |
| 322 | channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent); |
| 323 | if (!channel_table[cap]) { |
| 324 | err = -ENOMEM; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (err) { |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 330 | pr_err("initialization failure\n"); |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 331 | for_each_dma_cap_mask(cap, dma_cap_mask_all) |
| 332 | if (channel_table[cap]) |
| 333 | free_percpu(channel_table[cap]); |
| 334 | } |
| 335 | |
| 336 | return err; |
| 337 | } |
Dan Williams | 652afc2 | 2009-01-06 11:38:22 -0700 | [diff] [blame] | 338 | arch_initcall(dma_channel_table_init); |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * dma_find_channel - find a channel to carry out the operation |
| 342 | * @tx_type: transaction type |
| 343 | */ |
| 344 | struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type) |
| 345 | { |
Christoph Lameter | e7dcaa4 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 346 | return this_cpu_read(channel_table[tx_type]->chan); |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 347 | } |
| 348 | EXPORT_SYMBOL(dma_find_channel); |
| 349 | |
Dave Jiang | a2bd114 | 2012-04-04 16:10:46 -0700 | [diff] [blame] | 350 | /* |
| 351 | * net_dma_find_channel - find a channel for net_dma |
| 352 | * net_dma has alignment requirements |
| 353 | */ |
| 354 | struct dma_chan *net_dma_find_channel(void) |
| 355 | { |
| 356 | struct dma_chan *chan = dma_find_channel(DMA_MEMCPY); |
| 357 | if (chan && !is_dma_copy_aligned(chan->device, 1, 1, 1)) |
| 358 | return NULL; |
| 359 | |
| 360 | return chan; |
| 361 | } |
| 362 | EXPORT_SYMBOL(net_dma_find_channel); |
| 363 | |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 364 | /** |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 365 | * dma_issue_pending_all - flush all pending operations across all channels |
| 366 | */ |
| 367 | void dma_issue_pending_all(void) |
| 368 | { |
| 369 | struct dma_device *device; |
| 370 | struct dma_chan *chan; |
| 371 | |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 372 | rcu_read_lock(); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 373 | list_for_each_entry_rcu(device, &dma_device_list, global_node) { |
| 374 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 375 | continue; |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 376 | list_for_each_entry(chan, &device->channels, device_node) |
| 377 | if (chan->client_count) |
| 378 | device->device_issue_pending(chan); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 379 | } |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 380 | rcu_read_unlock(); |
| 381 | } |
| 382 | EXPORT_SYMBOL(dma_issue_pending_all); |
| 383 | |
| 384 | /** |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 385 | * dma_chan_is_local - returns true if the channel is in the same numa-node as the cpu |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 386 | */ |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 387 | static bool dma_chan_is_local(struct dma_chan *chan, int cpu) |
| 388 | { |
| 389 | int node = dev_to_node(chan->device->dev); |
| 390 | return node == -1 || cpumask_test_cpu(cpu, cpumask_of_node(node)); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * min_chan - returns the channel with min count and in the same numa-node as the cpu |
| 395 | * @cap: capability to match |
| 396 | * @cpu: cpu index which the channel should be close to |
| 397 | * |
| 398 | * If some channels are close to the given cpu, the one with the lowest |
| 399 | * reference count is returned. Otherwise, cpu is ignored and only the |
| 400 | * reference count is taken into account. |
| 401 | * Must be called under dma_list_mutex. |
| 402 | */ |
| 403 | static struct dma_chan *min_chan(enum dma_transaction_type cap, int cpu) |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 404 | { |
| 405 | struct dma_device *device; |
| 406 | struct dma_chan *chan; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 407 | struct dma_chan *min = NULL; |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 408 | struct dma_chan *localmin = NULL; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 409 | |
| 410 | list_for_each_entry(device, &dma_device_list, global_node) { |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 411 | if (!dma_has_cap(cap, device->cap_mask) || |
| 412 | dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 413 | continue; |
| 414 | list_for_each_entry(chan, &device->channels, device_node) { |
| 415 | if (!chan->client_count) |
| 416 | continue; |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 417 | if (!min || chan->table_count < min->table_count) |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 418 | min = chan; |
| 419 | |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 420 | if (dma_chan_is_local(chan, cpu)) |
| 421 | if (!localmin || |
| 422 | chan->table_count < localmin->table_count) |
| 423 | localmin = chan; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 424 | } |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 427 | chan = localmin ? localmin : min; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 428 | |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 429 | if (chan) |
| 430 | chan->table_count++; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 431 | |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 432 | return chan; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
| 436 | * dma_channel_rebalance - redistribute the available channels |
| 437 | * |
| 438 | * Optimize for cpu isolation (each cpu gets a dedicated channel for an |
| 439 | * operation type) in the SMP case, and operation isolation (avoid |
| 440 | * multi-tasking channels) in the non-SMP case. Must be called under |
| 441 | * dma_list_mutex. |
| 442 | */ |
| 443 | static void dma_channel_rebalance(void) |
| 444 | { |
| 445 | struct dma_chan *chan; |
| 446 | struct dma_device *device; |
| 447 | int cpu; |
| 448 | int cap; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 449 | |
| 450 | /* undo the last distribution */ |
| 451 | for_each_dma_cap_mask(cap, dma_cap_mask_all) |
| 452 | for_each_possible_cpu(cpu) |
| 453 | per_cpu_ptr(channel_table[cap], cpu)->chan = NULL; |
| 454 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 455 | list_for_each_entry(device, &dma_device_list, global_node) { |
| 456 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 457 | continue; |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 458 | list_for_each_entry(chan, &device->channels, device_node) |
| 459 | chan->table_count = 0; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 460 | } |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 461 | |
| 462 | /* don't populate the channel_table if no clients are available */ |
| 463 | if (!dmaengine_ref_count) |
| 464 | return; |
| 465 | |
| 466 | /* redistribute available channels */ |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 467 | for_each_dma_cap_mask(cap, dma_cap_mask_all) |
| 468 | for_each_online_cpu(cpu) { |
Brice Goglin | c4d27c4 | 2013-08-19 11:43:35 +0200 | [diff] [blame] | 469 | chan = min_chan(cap, cpu); |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 470 | per_cpu_ptr(channel_table[cap], cpu)->chan = chan; |
| 471 | } |
| 472 | } |
| 473 | |
Lars-Peter Clausen | a53e28d | 2013-03-25 13:23:52 +0100 | [diff] [blame] | 474 | static struct dma_chan *private_candidate(const dma_cap_mask_t *mask, |
| 475 | struct dma_device *dev, |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 476 | dma_filter_fn fn, void *fn_param) |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 477 | { |
| 478 | struct dma_chan *chan; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 479 | |
| 480 | if (!__dma_device_satisfies_mask(dev, mask)) { |
| 481 | pr_debug("%s: wrong capabilities\n", __func__); |
| 482 | return NULL; |
| 483 | } |
| 484 | /* devices with multiple channels need special handling as we need to |
| 485 | * ensure that all channels are either private or public. |
| 486 | */ |
| 487 | if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask)) |
| 488 | list_for_each_entry(chan, &dev->channels, device_node) { |
| 489 | /* some channels are already publicly allocated */ |
| 490 | if (chan->client_count) |
| 491 | return NULL; |
| 492 | } |
| 493 | |
| 494 | list_for_each_entry(chan, &dev->channels, device_node) { |
| 495 | if (chan->client_count) { |
| 496 | pr_debug("%s: %s busy\n", |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 497 | __func__, dma_chan_name(chan)); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 498 | continue; |
| 499 | } |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 500 | if (fn && !fn(chan, fn_param)) { |
| 501 | pr_debug("%s: %s filter said false\n", |
| 502 | __func__, dma_chan_name(chan)); |
| 503 | continue; |
| 504 | } |
| 505 | return chan; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 508 | return NULL; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
Daniel Mack | 6b9019a | 2013-08-14 18:35:03 +0200 | [diff] [blame] | 512 | * dma_request_slave_channel - try to get specific channel exclusively |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 513 | * @chan: target channel |
| 514 | */ |
| 515 | struct dma_chan *dma_get_slave_channel(struct dma_chan *chan) |
| 516 | { |
| 517 | int err = -EBUSY; |
| 518 | |
| 519 | /* lock against __dma_request_channel */ |
| 520 | mutex_lock(&dma_list_mutex); |
| 521 | |
Vinod Koul | d9a6c8f | 2013-08-19 10:47:26 +0530 | [diff] [blame] | 522 | if (chan->client_count == 0) { |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 523 | err = dma_chan_get(chan); |
Vinod Koul | d9a6c8f | 2013-08-19 10:47:26 +0530 | [diff] [blame] | 524 | if (err) |
| 525 | pr_debug("%s: failed to get %s: (%d)\n", |
| 526 | __func__, dma_chan_name(chan), err); |
| 527 | } else |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 528 | chan = NULL; |
| 529 | |
| 530 | mutex_unlock(&dma_list_mutex); |
| 531 | |
Zhangfei Gao | 7bb587f | 2013-06-28 20:39:12 +0800 | [diff] [blame] | 532 | |
| 533 | return chan; |
| 534 | } |
| 535 | EXPORT_SYMBOL_GPL(dma_get_slave_channel); |
| 536 | |
| 537 | /** |
Daniel Mack | 6b9019a | 2013-08-14 18:35:03 +0200 | [diff] [blame] | 538 | * __dma_request_channel - try to allocate an exclusive channel |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 539 | * @mask: capabilities that the channel must satisfy |
| 540 | * @fn: optional callback to disposition available channels |
| 541 | * @fn_param: opaque parameter to pass to dma_filter_fn |
| 542 | */ |
Lars-Peter Clausen | a53e28d | 2013-03-25 13:23:52 +0100 | [diff] [blame] | 543 | struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask, |
| 544 | dma_filter_fn fn, void *fn_param) |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 545 | { |
| 546 | struct dma_device *device, *_d; |
| 547 | struct dma_chan *chan = NULL; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 548 | int err; |
| 549 | |
| 550 | /* Find a channel */ |
| 551 | mutex_lock(&dma_list_mutex); |
| 552 | list_for_each_entry_safe(device, _d, &dma_device_list, global_node) { |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 553 | chan = private_candidate(mask, device, fn, fn_param); |
| 554 | if (chan) { |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 555 | /* Found a suitable channel, try to grab, prep, and |
| 556 | * return it. We first set DMA_PRIVATE to disable |
| 557 | * balance_ref_count as this channel will not be |
| 558 | * published in the general-purpose allocator |
| 559 | */ |
| 560 | dma_cap_set(DMA_PRIVATE, device->cap_mask); |
Atsushi Nemoto | 0f57151 | 2009-03-06 20:07:14 +0900 | [diff] [blame] | 561 | device->privatecnt++; |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 562 | err = dma_chan_get(chan); |
| 563 | |
| 564 | if (err == -ENODEV) { |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 565 | pr_debug("%s: %s module removed\n", |
| 566 | __func__, dma_chan_name(chan)); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 567 | list_del_rcu(&device->global_node); |
| 568 | } else if (err) |
Fabio Estevam | d8b5348 | 2012-02-21 12:51:59 -0200 | [diff] [blame] | 569 | pr_debug("%s: failed to get %s: (%d)\n", |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 570 | __func__, dma_chan_name(chan), err); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 571 | else |
| 572 | break; |
Atsushi Nemoto | 0f57151 | 2009-03-06 20:07:14 +0900 | [diff] [blame] | 573 | if (--device->privatecnt == 0) |
| 574 | dma_cap_clear(DMA_PRIVATE, device->cap_mask); |
Dan Williams | e234667 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 575 | chan = NULL; |
| 576 | } |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 577 | } |
| 578 | mutex_unlock(&dma_list_mutex); |
| 579 | |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 580 | pr_debug("%s: %s (%s)\n", |
| 581 | __func__, |
| 582 | chan ? "success" : "fail", |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 583 | chan ? dma_chan_name(chan) : NULL); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 584 | |
| 585 | return chan; |
| 586 | } |
| 587 | EXPORT_SYMBOL_GPL(__dma_request_channel); |
| 588 | |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 589 | /** |
| 590 | * dma_request_slave_channel - try to allocate an exclusive slave channel |
| 591 | * @dev: pointer to client device structure |
| 592 | * @name: slave channel name |
| 593 | */ |
Markus Pargmann | bef29ec | 2013-02-24 16:36:09 +0100 | [diff] [blame] | 594 | struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name) |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 595 | { |
| 596 | /* If device-tree is present get slave info from here */ |
| 597 | if (dev->of_node) |
| 598 | return of_dma_request_slave_channel(dev->of_node, name); |
| 599 | |
Andy Shevchenko | 4e82f5d | 2013-04-09 14:05:44 +0300 | [diff] [blame] | 600 | /* If device was enumerated by ACPI get slave info from here */ |
| 601 | if (ACPI_HANDLE(dev)) |
| 602 | return acpi_dma_request_slave_chan_by_name(dev, name); |
| 603 | |
Jon Hunter | 9a6cecc | 2012-09-14 17:41:57 -0500 | [diff] [blame] | 604 | return NULL; |
| 605 | } |
| 606 | EXPORT_SYMBOL_GPL(dma_request_slave_channel); |
| 607 | |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 608 | void dma_release_channel(struct dma_chan *chan) |
| 609 | { |
| 610 | mutex_lock(&dma_list_mutex); |
| 611 | WARN_ONCE(chan->client_count != 1, |
| 612 | "chan reference count %d != 1\n", chan->client_count); |
| 613 | dma_chan_put(chan); |
Atsushi Nemoto | 0f57151 | 2009-03-06 20:07:14 +0900 | [diff] [blame] | 614 | /* drop PRIVATE cap enabled by __dma_request_channel() */ |
| 615 | if (--chan->device->privatecnt == 0) |
| 616 | dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 617 | mutex_unlock(&dma_list_mutex); |
| 618 | } |
| 619 | EXPORT_SYMBOL_GPL(dma_release_channel); |
| 620 | |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 621 | /** |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 622 | * dmaengine_get - register interest in dma_channels |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 623 | */ |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 624 | void dmaengine_get(void) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 625 | { |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 626 | struct dma_device *device, *_d; |
| 627 | struct dma_chan *chan; |
| 628 | int err; |
| 629 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 630 | mutex_lock(&dma_list_mutex); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 631 | dmaengine_ref_count++; |
| 632 | |
| 633 | /* try to grab channels */ |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 634 | list_for_each_entry_safe(device, _d, &dma_device_list, global_node) { |
| 635 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 636 | continue; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 637 | list_for_each_entry(chan, &device->channels, device_node) { |
| 638 | err = dma_chan_get(chan); |
| 639 | if (err == -ENODEV) { |
| 640 | /* module removed before we could use it */ |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 641 | list_del_rcu(&device->global_node); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 642 | break; |
| 643 | } else if (err) |
Fabio Estevam | 0eb5a35 | 2012-10-04 17:11:16 -0700 | [diff] [blame] | 644 | pr_debug("%s: failed to get %s: (%d)\n", |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 645 | __func__, dma_chan_name(chan), err); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 646 | } |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 647 | } |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 648 | |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 649 | /* if this is the first reference and there were channels |
| 650 | * waiting we need to rebalance to get those channels |
| 651 | * incorporated into the channel table |
| 652 | */ |
| 653 | if (dmaengine_ref_count == 1) |
| 654 | dma_channel_rebalance(); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 655 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 656 | } |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 657 | EXPORT_SYMBOL(dmaengine_get); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 658 | |
| 659 | /** |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 660 | * dmaengine_put - let dma drivers be removed when ref_count == 0 |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 661 | */ |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 662 | void dmaengine_put(void) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 663 | { |
Dan Williams | d379b01 | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 664 | struct dma_device *device; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 665 | struct dma_chan *chan; |
| 666 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 667 | mutex_lock(&dma_list_mutex); |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 668 | dmaengine_ref_count--; |
| 669 | BUG_ON(dmaengine_ref_count < 0); |
| 670 | /* drop channel references */ |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 671 | list_for_each_entry(device, &dma_device_list, global_node) { |
| 672 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 673 | continue; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 674 | list_for_each_entry(chan, &device->channels, device_node) |
| 675 | dma_chan_put(chan); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 676 | } |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 677 | mutex_unlock(&dma_list_mutex); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 678 | } |
Dan Williams | 209b84a | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 679 | EXPORT_SYMBOL(dmaengine_put); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 680 | |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 681 | static bool device_has_all_tx_types(struct dma_device *device) |
| 682 | { |
| 683 | /* A device that satisfies this test has channels that will never cause |
| 684 | * an async_tx channel switch event as all possible operation types can |
| 685 | * be handled. |
| 686 | */ |
| 687 | #ifdef CONFIG_ASYNC_TX_DMA |
| 688 | if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask)) |
| 689 | return false; |
| 690 | #endif |
| 691 | |
| 692 | #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE) |
| 693 | if (!dma_has_cap(DMA_MEMCPY, device->cap_mask)) |
| 694 | return false; |
| 695 | #endif |
| 696 | |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 697 | #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE) |
| 698 | if (!dma_has_cap(DMA_XOR, device->cap_mask)) |
| 699 | return false; |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 700 | |
| 701 | #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA |
Dan Williams | 4499a24 | 2009-11-19 17:10:25 -0700 | [diff] [blame] | 702 | if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask)) |
| 703 | return false; |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 704 | #endif |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 705 | #endif |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 706 | |
| 707 | #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE) |
| 708 | if (!dma_has_cap(DMA_PQ, device->cap_mask)) |
| 709 | return false; |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 710 | |
| 711 | #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA |
Dan Williams | 4499a24 | 2009-11-19 17:10:25 -0700 | [diff] [blame] | 712 | if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask)) |
| 713 | return false; |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 714 | #endif |
Dan Williams | 7b3cc2b | 2009-11-19 17:10:37 -0700 | [diff] [blame] | 715 | #endif |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 716 | |
| 717 | return true; |
| 718 | } |
| 719 | |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 720 | static int get_dma_id(struct dma_device *device) |
| 721 | { |
| 722 | int rc; |
| 723 | |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 724 | mutex_lock(&dma_list_mutex); |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 725 | |
Tejun Heo | 69ee266 | 2013-02-27 17:04:03 -0800 | [diff] [blame] | 726 | rc = idr_alloc(&dma_idr, NULL, 0, 0, GFP_KERNEL); |
| 727 | if (rc >= 0) |
| 728 | device->dev_id = rc; |
| 729 | |
| 730 | mutex_unlock(&dma_list_mutex); |
| 731 | return rc < 0 ? rc : 0; |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 734 | /** |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 735 | * dma_async_device_register - registers DMA devices found |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 736 | * @device: &dma_device |
| 737 | */ |
| 738 | int dma_async_device_register(struct dma_device *device) |
| 739 | { |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 740 | int chancnt = 0, rc; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 741 | struct dma_chan* chan; |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 742 | atomic_t *idr_ref; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 743 | |
| 744 | if (!device) |
| 745 | return -ENODEV; |
| 746 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 747 | /* validate device routines */ |
| 748 | BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) && |
| 749 | !device->device_prep_dma_memcpy); |
| 750 | BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) && |
| 751 | !device->device_prep_dma_xor); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 752 | BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) && |
| 753 | !device->device_prep_dma_xor_val); |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 754 | BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) && |
| 755 | !device->device_prep_dma_pq); |
| 756 | BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) && |
| 757 | !device->device_prep_dma_pq_val); |
Zhang Wei | 9b941c6 | 2008-03-13 17:45:28 -0700 | [diff] [blame] | 758 | BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) && |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 759 | !device->device_prep_dma_interrupt); |
Ira Snyder | a86ee03 | 2010-09-30 11:46:44 +0000 | [diff] [blame] | 760 | BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) && |
| 761 | !device->device_prep_dma_sg); |
Sascha Hauer | 782bc95 | 2010-09-30 13:56:32 +0000 | [diff] [blame] | 762 | BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) && |
| 763 | !device->device_prep_dma_cyclic); |
Haavard Skinnemoen | dc0ee643 | 2008-07-08 11:59:35 -0700 | [diff] [blame] | 764 | BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) && |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 765 | !device->device_control); |
Jassi Brar | b14dab7 | 2011-10-13 12:33:30 +0530 | [diff] [blame] | 766 | BUG_ON(dma_has_cap(DMA_INTERLEAVE, device->cap_mask) && |
| 767 | !device->device_prep_interleaved_dma); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 768 | |
| 769 | BUG_ON(!device->device_alloc_chan_resources); |
| 770 | BUG_ON(!device->device_free_chan_resources); |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 771 | BUG_ON(!device->device_tx_status); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 772 | BUG_ON(!device->device_issue_pending); |
| 773 | BUG_ON(!device->dev); |
| 774 | |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 775 | /* note: this only matters in the |
Dan Williams | 5fc6d89 | 2010-10-07 16:44:50 -0700 | [diff] [blame] | 776 | * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case |
Dan Williams | 138f4c3 | 2009-09-08 17:42:51 -0700 | [diff] [blame] | 777 | */ |
| 778 | if (device_has_all_tx_types(device)) |
| 779 | dma_cap_set(DMA_ASYNC_TX, device->cap_mask); |
| 780 | |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 781 | idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL); |
| 782 | if (!idr_ref) |
| 783 | return -ENOMEM; |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 784 | rc = get_dma_id(device); |
| 785 | if (rc != 0) { |
| 786 | kfree(idr_ref); |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 787 | return rc; |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | atomic_set(idr_ref, 0); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 791 | |
| 792 | /* represent channels in sysfs. Probably want devs too */ |
| 793 | list_for_each_entry(chan, &device->channels, device_node) { |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 794 | rc = -ENOMEM; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 795 | chan->local = alloc_percpu(typeof(*chan->local)); |
| 796 | if (chan->local == NULL) |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 797 | goto err_out; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 798 | chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL); |
| 799 | if (chan->dev == NULL) { |
| 800 | free_percpu(chan->local); |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 801 | chan->local = NULL; |
| 802 | goto err_out; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 803 | } |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 804 | |
| 805 | chan->chan_id = chancnt++; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 806 | chan->dev->device.class = &dma_devclass; |
| 807 | chan->dev->device.parent = device->dev; |
| 808 | chan->dev->chan = chan; |
Dan Williams | 864498a | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 809 | chan->dev->idr_ref = idr_ref; |
| 810 | chan->dev->dev_id = device->dev_id; |
| 811 | atomic_inc(idr_ref); |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 812 | dev_set_name(&chan->dev->device, "dma%dchan%d", |
Kay Sievers | 06190d8 | 2008-11-11 13:12:33 -0700 | [diff] [blame] | 813 | device->dev_id, chan->chan_id); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 814 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 815 | rc = device_register(&chan->dev->device); |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 816 | if (rc) { |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 817 | free_percpu(chan->local); |
| 818 | chan->local = NULL; |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 819 | kfree(chan->dev); |
| 820 | atomic_dec(idr_ref); |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 821 | goto err_out; |
| 822 | } |
Dan Williams | 7cc5bf9 | 2008-07-08 11:58:21 -0700 | [diff] [blame] | 823 | chan->client_count = 0; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 824 | } |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 825 | device->chancnt = chancnt; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 826 | |
| 827 | mutex_lock(&dma_list_mutex); |
Dan Williams | 59b5ec2 | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 828 | /* take references on public channels */ |
| 829 | if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 830 | list_for_each_entry(chan, &device->channels, device_node) { |
| 831 | /* if clients are already waiting for channels we need |
| 832 | * to take references on their behalf |
| 833 | */ |
| 834 | if (dma_chan_get(chan) == -ENODEV) { |
| 835 | /* note we can only get here for the first |
| 836 | * channel as the remaining channels are |
| 837 | * guaranteed to get a reference |
| 838 | */ |
| 839 | rc = -ENODEV; |
| 840 | mutex_unlock(&dma_list_mutex); |
| 841 | goto err_out; |
| 842 | } |
| 843 | } |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 844 | list_add_tail_rcu(&device->global_node, &dma_device_list); |
Atsushi Nemoto | 0f57151 | 2009-03-06 20:07:14 +0900 | [diff] [blame] | 845 | if (dma_has_cap(DMA_PRIVATE, device->cap_mask)) |
| 846 | device->privatecnt++; /* Always private */ |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 847 | dma_channel_rebalance(); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 848 | mutex_unlock(&dma_list_mutex); |
| 849 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 850 | return 0; |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 851 | |
| 852 | err_out: |
Dan Williams | 257b17c | 2009-03-25 09:13:23 -0700 | [diff] [blame] | 853 | /* if we never registered a channel just release the idr */ |
| 854 | if (atomic_read(idr_ref) == 0) { |
| 855 | mutex_lock(&dma_list_mutex); |
| 856 | idr_remove(&dma_idr, device->dev_id); |
| 857 | mutex_unlock(&dma_list_mutex); |
| 858 | kfree(idr_ref); |
| 859 | return rc; |
| 860 | } |
| 861 | |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 862 | list_for_each_entry(chan, &device->channels, device_node) { |
| 863 | if (chan->local == NULL) |
| 864 | continue; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 865 | mutex_lock(&dma_list_mutex); |
| 866 | chan->dev->chan = NULL; |
| 867 | mutex_unlock(&dma_list_mutex); |
| 868 | device_unregister(&chan->dev->device); |
Jeff Garzik | ff487fb | 2007-03-08 09:57:34 -0800 | [diff] [blame] | 869 | free_percpu(chan->local); |
| 870 | } |
| 871 | return rc; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 872 | } |
David Brownell | 765e3d8 | 2007-03-16 13:38:05 -0800 | [diff] [blame] | 873 | EXPORT_SYMBOL(dma_async_device_register); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 874 | |
| 875 | /** |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 876 | * dma_async_device_unregister - unregister a DMA device |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 877 | * @device: &dma_device |
Dan Williams | f27c580 | 2009-01-06 11:38:18 -0700 | [diff] [blame] | 878 | * |
| 879 | * This routine is called by dma driver exit routines, dmaengine holds module |
| 880 | * references to prevent it being called while channels are in use. |
Randy Dunlap | 6508871 | 2006-07-03 19:45:31 -0700 | [diff] [blame] | 881 | */ |
| 882 | void dma_async_device_unregister(struct dma_device *device) |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 883 | { |
| 884 | struct dma_chan *chan; |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 885 | |
| 886 | mutex_lock(&dma_list_mutex); |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 887 | list_del_rcu(&device->global_node); |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 888 | dma_channel_rebalance(); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 889 | mutex_unlock(&dma_list_mutex); |
| 890 | |
| 891 | list_for_each_entry(chan, &device->channels, device_node) { |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 892 | WARN_ONCE(chan->client_count, |
| 893 | "%s called while %d clients hold a reference\n", |
| 894 | __func__, chan->client_count); |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 895 | mutex_lock(&dma_list_mutex); |
| 896 | chan->dev->chan = NULL; |
| 897 | mutex_unlock(&dma_list_mutex); |
| 898 | device_unregister(&chan->dev->device); |
Anatolij Gustschin | adef477 | 2010-01-26 10:26:06 +0100 | [diff] [blame] | 899 | free_percpu(chan->local); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 900 | } |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 901 | } |
David Brownell | 765e3d8 | 2007-03-16 13:38:05 -0800 | [diff] [blame] | 902 | EXPORT_SYMBOL(dma_async_device_unregister); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 903 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 904 | /** |
| 905 | * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses |
| 906 | * @chan: DMA channel to offload copy to |
| 907 | * @dest: destination address (virtual) |
| 908 | * @src: source address (virtual) |
| 909 | * @len: length |
| 910 | * |
| 911 | * Both @dest and @src must be mappable to a bus address according to the |
| 912 | * DMA mapping API rules for streaming mappings. |
| 913 | * Both @dest and @src must stay memory resident (kernel memory or locked |
| 914 | * user space pages). |
| 915 | */ |
| 916 | dma_cookie_t |
| 917 | dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest, |
| 918 | void *src, size_t len) |
| 919 | { |
| 920 | struct dma_device *dev = chan->device; |
| 921 | struct dma_async_tx_descriptor *tx; |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 922 | dma_addr_t dma_dest, dma_src; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 923 | dma_cookie_t cookie; |
Maciej Sosnowski | 4f005db | 2009-04-23 12:31:51 +0200 | [diff] [blame] | 924 | unsigned long flags; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 925 | |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 926 | dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE); |
| 927 | dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE); |
Maciej Sosnowski | 4f005db | 2009-04-23 12:31:51 +0200 | [diff] [blame] | 928 | flags = DMA_CTRL_ACK | |
| 929 | DMA_COMPL_SRC_UNMAP_SINGLE | |
| 930 | DMA_COMPL_DEST_UNMAP_SINGLE; |
| 931 | tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags); |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 932 | |
| 933 | if (!tx) { |
| 934 | dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE); |
| 935 | dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 936 | return -ENOMEM; |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 937 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 938 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 939 | tx->callback = NULL; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 940 | cookie = tx->tx_submit(tx); |
| 941 | |
Christoph Lameter | e7dcaa4 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 942 | preempt_disable(); |
| 943 | __this_cpu_add(chan->local->bytes_transferred, len); |
| 944 | __this_cpu_inc(chan->local->memcpy_count); |
| 945 | preempt_enable(); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 946 | |
| 947 | return cookie; |
| 948 | } |
| 949 | EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf); |
| 950 | |
| 951 | /** |
| 952 | * dma_async_memcpy_buf_to_pg - offloaded copy from address to page |
| 953 | * @chan: DMA channel to offload copy to |
| 954 | * @page: destination page |
| 955 | * @offset: offset in page to copy to |
| 956 | * @kdata: source address (virtual) |
| 957 | * @len: length |
| 958 | * |
| 959 | * Both @page/@offset and @kdata must be mappable to a bus address according |
| 960 | * to the DMA mapping API rules for streaming mappings. |
| 961 | * Both @page/@offset and @kdata must stay memory resident (kernel memory or |
| 962 | * locked user space pages) |
| 963 | */ |
| 964 | dma_cookie_t |
| 965 | dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page, |
| 966 | unsigned int offset, void *kdata, size_t len) |
| 967 | { |
| 968 | struct dma_device *dev = chan->device; |
| 969 | struct dma_async_tx_descriptor *tx; |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 970 | dma_addr_t dma_dest, dma_src; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 971 | dma_cookie_t cookie; |
Maciej Sosnowski | 4f005db | 2009-04-23 12:31:51 +0200 | [diff] [blame] | 972 | unsigned long flags; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 973 | |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 974 | dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE); |
| 975 | dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE); |
Maciej Sosnowski | 4f005db | 2009-04-23 12:31:51 +0200 | [diff] [blame] | 976 | flags = DMA_CTRL_ACK | DMA_COMPL_SRC_UNMAP_SINGLE; |
| 977 | tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags); |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 978 | |
| 979 | if (!tx) { |
| 980 | dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE); |
| 981 | dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 982 | return -ENOMEM; |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 983 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 984 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 985 | tx->callback = NULL; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 986 | cookie = tx->tx_submit(tx); |
| 987 | |
Christoph Lameter | e7dcaa4 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 988 | preempt_disable(); |
| 989 | __this_cpu_add(chan->local->bytes_transferred, len); |
| 990 | __this_cpu_inc(chan->local->memcpy_count); |
| 991 | preempt_enable(); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 992 | |
| 993 | return cookie; |
| 994 | } |
| 995 | EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg); |
| 996 | |
| 997 | /** |
| 998 | * dma_async_memcpy_pg_to_pg - offloaded copy from page to page |
| 999 | * @chan: DMA channel to offload copy to |
| 1000 | * @dest_pg: destination page |
| 1001 | * @dest_off: offset in page to copy to |
| 1002 | * @src_pg: source page |
| 1003 | * @src_off: offset in page to copy from |
| 1004 | * @len: length |
| 1005 | * |
| 1006 | * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus |
| 1007 | * address according to the DMA mapping API rules for streaming mappings. |
| 1008 | * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident |
| 1009 | * (kernel memory or locked user space pages). |
| 1010 | */ |
| 1011 | dma_cookie_t |
| 1012 | dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg, |
| 1013 | unsigned int dest_off, struct page *src_pg, unsigned int src_off, |
| 1014 | size_t len) |
| 1015 | { |
| 1016 | struct dma_device *dev = chan->device; |
| 1017 | struct dma_async_tx_descriptor *tx; |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1018 | dma_addr_t dma_dest, dma_src; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1019 | dma_cookie_t cookie; |
Maciej Sosnowski | 4f005db | 2009-04-23 12:31:51 +0200 | [diff] [blame] | 1020 | unsigned long flags; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1021 | |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1022 | dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE); |
| 1023 | dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len, |
| 1024 | DMA_FROM_DEVICE); |
Maciej Sosnowski | 4f005db | 2009-04-23 12:31:51 +0200 | [diff] [blame] | 1025 | flags = DMA_CTRL_ACK; |
| 1026 | tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags); |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1027 | |
| 1028 | if (!tx) { |
| 1029 | dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE); |
| 1030 | dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1031 | return -ENOMEM; |
Dan Williams | 0036731 | 2008-02-02 19:49:57 -0700 | [diff] [blame] | 1032 | } |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1033 | |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1034 | tx->callback = NULL; |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1035 | cookie = tx->tx_submit(tx); |
| 1036 | |
Christoph Lameter | e7dcaa4 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 1037 | preempt_disable(); |
| 1038 | __this_cpu_add(chan->local->bytes_transferred, len); |
| 1039 | __this_cpu_inc(chan->local->memcpy_count); |
| 1040 | preempt_enable(); |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1041 | |
| 1042 | return cookie; |
| 1043 | } |
| 1044 | EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg); |
| 1045 | |
| 1046 | void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx, |
| 1047 | struct dma_chan *chan) |
| 1048 | { |
| 1049 | tx->chan = chan; |
Dan Williams | 5fc6d89 | 2010-10-07 16:44:50 -0700 | [diff] [blame] | 1050 | #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1051 | spin_lock_init(&tx->lock); |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1052 | #endif |
Dan Williams | 7405f74 | 2007-01-02 11:10:43 -0700 | [diff] [blame] | 1053 | } |
| 1054 | EXPORT_SYMBOL(dma_async_tx_descriptor_init); |
| 1055 | |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1056 | /* dma_wait_for_async_tx - spin wait for a transaction to complete |
| 1057 | * @tx: in-flight transaction to wait on |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1058 | */ |
| 1059 | enum dma_status |
| 1060 | dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx) |
| 1061 | { |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1062 | unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1063 | |
| 1064 | if (!tx) |
Vinod Koul | adfedd9 | 2013-10-16 13:29:02 +0530 | [diff] [blame] | 1065 | return DMA_COMPLETE; |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1066 | |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1067 | while (tx->cookie == -EBUSY) { |
| 1068 | if (time_after_eq(jiffies, dma_sync_wait_timeout)) { |
| 1069 | pr_err("%s timeout waiting for descriptor submission\n", |
Joe Perches | 6343325 | 2012-07-18 09:51:28 -0700 | [diff] [blame] | 1070 | __func__); |
Dan Williams | 95475e5 | 2009-07-14 12:19:02 -0700 | [diff] [blame] | 1071 | return DMA_ERROR; |
| 1072 | } |
| 1073 | cpu_relax(); |
| 1074 | } |
| 1075 | return dma_sync_wait(tx->chan, tx->cookie); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1076 | } |
| 1077 | EXPORT_SYMBOL_GPL(dma_wait_for_async_tx); |
| 1078 | |
| 1079 | /* dma_run_dependencies - helper routine for dma drivers to process |
| 1080 | * (start) dependent operations on their target channel |
| 1081 | * @tx: transaction with dependencies |
| 1082 | */ |
| 1083 | void dma_run_dependencies(struct dma_async_tx_descriptor *tx) |
| 1084 | { |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1085 | struct dma_async_tx_descriptor *dep = txd_next(tx); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1086 | struct dma_async_tx_descriptor *dep_next; |
| 1087 | struct dma_chan *chan; |
| 1088 | |
| 1089 | if (!dep) |
| 1090 | return; |
| 1091 | |
Yuri Tikhonov | dd59b85 | 2009-01-12 15:17:20 -0700 | [diff] [blame] | 1092 | /* we'll submit tx->next now, so clear the link */ |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1093 | txd_clear_next(tx); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1094 | chan = dep->chan; |
| 1095 | |
| 1096 | /* keep submitting up until a channel switch is detected |
| 1097 | * in that case we will be called again as a result of |
| 1098 | * processing the interrupt from async_tx_channel_switch |
| 1099 | */ |
| 1100 | for (; dep; dep = dep_next) { |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1101 | txd_lock(dep); |
| 1102 | txd_clear_parent(dep); |
| 1103 | dep_next = txd_next(dep); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1104 | if (dep_next && dep_next->chan == chan) |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1105 | txd_clear_next(dep); /* ->next will be submitted */ |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1106 | else |
| 1107 | dep_next = NULL; /* submit current dep and terminate */ |
Dan Williams | caa20d97 | 2010-05-17 16:24:16 -0700 | [diff] [blame] | 1108 | txd_unlock(dep); |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 1109 | |
| 1110 | dep->tx_submit(dep); |
| 1111 | } |
| 1112 | |
| 1113 | chan->device->device_issue_pending(chan); |
| 1114 | } |
| 1115 | EXPORT_SYMBOL_GPL(dma_run_dependencies); |
| 1116 | |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1117 | static int __init dma_bus_init(void) |
| 1118 | { |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1119 | return class_register(&dma_devclass); |
| 1120 | } |
Dan Williams | 652afc2 | 2009-01-06 11:38:22 -0700 | [diff] [blame] | 1121 | arch_initcall(dma_bus_init); |
Chris Leech | c13c826 | 2006-05-23 17:18:44 -0700 | [diff] [blame] | 1122 | |
Dan Williams | bec0851 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1123 | |