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