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