blob: 418eca28d472baf28909c6b4f30110dcef8af61e [file] [log] [blame]
Chris Leechc13c8262006-05-23 17:18:44 -07001/*
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 *
34 * The subsystem keeps two global lists, dma_device_list and dma_client_list.
35 * Both of these are protected by a mutex, dma_list_mutex.
36 *
37 * Each device has a channels list, which runs unlocked but is never modified
38 * once the device is registered, it's just setup by the driver.
39 *
Dan Williamsd379b012007-07-09 11:56:42 -070040 * Each client is responsible for keeping track of the channels it uses. See
41 * the definition of dma_event_callback in dmaengine.h.
Chris Leechc13c8262006-05-23 17:18:44 -070042 *
43 * Each device has a kref, which is initialized to 1 when the device is
Tony Jones891f78e2007-09-25 02:03:03 +020044 * registered. A kref_get is done for each device registered. When the
Sebastian Siewior8a5703f2008-04-21 22:38:45 +000045 * device is released, the corresponding kref_put is done in the release
Chris Leechc13c8262006-05-23 17:18:44 -070046 * method. Every time one of the device's channels is allocated to a client,
Sebastian Siewior8a5703f2008-04-21 22:38:45 +000047 * a kref_get occurs. When the channel is freed, the corresponding kref_put
Chris Leechc13c8262006-05-23 17:18:44 -070048 * happens. The device's release function does a completion, so
Tony Jones891f78e2007-09-25 02:03:03 +020049 * unregister_device does a remove event, device_unregister, a kref_put
Chris Leechc13c8262006-05-23 17:18:44 -070050 * for the first reference, then waits on the completion for all other
51 * references to finish.
52 *
53 * Each channel has an open-coded implementation of Rusty Russell's "bigref,"
Dan Williamsd379b012007-07-09 11:56:42 -070054 * with a kref and a per_cpu local_t. A dma_chan_get is called when a client
55 * signals that it wants to use a channel, and dma_chan_put is called when
Sebastian Siewior8a5703f2008-04-21 22:38:45 +000056 * a channel is removed or a client using it is unregistered. A client can
Dan Williamsd379b012007-07-09 11:56:42 -070057 * take extra references per outstanding transaction, as is the case with
58 * the NET DMA client. The release function does a kref_put on the device.
59 * -ChrisL, DanW
Chris Leechc13c8262006-05-23 17:18:44 -070060 */
61
62#include <linux/init.h>
63#include <linux/module.h>
Dan Williams7405f742007-01-02 11:10:43 -070064#include <linux/mm.h>
Chris Leechc13c8262006-05-23 17:18:44 -070065#include <linux/device.h>
66#include <linux/dmaengine.h>
67#include <linux/hardirq.h>
68#include <linux/spinlock.h>
69#include <linux/percpu.h>
70#include <linux/rcupdate.h>
71#include <linux/mutex.h>
Dan Williams7405f742007-01-02 11:10:43 -070072#include <linux/jiffies.h>
Dan Williams2ba05622009-01-06 11:38:14 -070073#include <linux/rculist.h>
Chris Leechc13c8262006-05-23 17:18:44 -070074
75static DEFINE_MUTEX(dma_list_mutex);
76static LIST_HEAD(dma_device_list);
77static LIST_HEAD(dma_client_list);
Dan Williams6f49a572009-01-06 11:38:14 -070078static long dmaengine_ref_count;
Chris Leechc13c8262006-05-23 17:18:44 -070079
80/* --- sysfs implementation --- */
81
Tony Jones891f78e2007-09-25 02:03:03 +020082static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -070083{
Tony Jones891f78e2007-09-25 02:03:03 +020084 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -070085 unsigned long count = 0;
86 int i;
87
Andrew Morton17f3ae02006-05-25 13:26:53 -070088 for_each_possible_cpu(i)
Chris Leechc13c8262006-05-23 17:18:44 -070089 count += per_cpu_ptr(chan->local, i)->memcpy_count;
90
91 return sprintf(buf, "%lu\n", count);
92}
93
Tony Jones891f78e2007-09-25 02:03:03 +020094static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr,
95 char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -070096{
Tony Jones891f78e2007-09-25 02:03:03 +020097 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -070098 unsigned long count = 0;
99 int i;
100
Andrew Morton17f3ae02006-05-25 13:26:53 -0700101 for_each_possible_cpu(i)
Chris Leechc13c8262006-05-23 17:18:44 -0700102 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
103
104 return sprintf(buf, "%lu\n", count);
105}
106
Tony Jones891f78e2007-09-25 02:03:03 +0200107static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700108{
Tony Jones891f78e2007-09-25 02:03:03 +0200109 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700110
Dan Williams6f49a572009-01-06 11:38:14 -0700111 return sprintf(buf, "%d\n", chan->client_count);
Chris Leechc13c8262006-05-23 17:18:44 -0700112}
113
Tony Jones891f78e2007-09-25 02:03:03 +0200114static struct device_attribute dma_attrs[] = {
Chris Leechc13c8262006-05-23 17:18:44 -0700115 __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
116 __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
117 __ATTR(in_use, S_IRUGO, show_in_use, NULL),
118 __ATTR_NULL
119};
120
121static void dma_async_device_cleanup(struct kref *kref);
122
Tony Jones891f78e2007-09-25 02:03:03 +0200123static void dma_dev_release(struct device *dev)
Chris Leechc13c8262006-05-23 17:18:44 -0700124{
Tony Jones891f78e2007-09-25 02:03:03 +0200125 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700126 kref_put(&chan->device->refcount, dma_async_device_cleanup);
127}
128
129static struct class dma_devclass = {
Tony Jones891f78e2007-09-25 02:03:03 +0200130 .name = "dma",
131 .dev_attrs = dma_attrs,
132 .dev_release = dma_dev_release,
Chris Leechc13c8262006-05-23 17:18:44 -0700133};
134
135/* --- client and device registration --- */
136
Dan Williamsd379b012007-07-09 11:56:42 -0700137#define dma_chan_satisfies_mask(chan, mask) \
138 __dma_chan_satisfies_mask((chan), &(mask))
139static int
140__dma_chan_satisfies_mask(struct dma_chan *chan, dma_cap_mask_t *want)
141{
142 dma_cap_mask_t has;
143
144 bitmap_and(has.bits, want->bits, chan->device->cap_mask.bits,
145 DMA_TX_TYPE_END);
146 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
147}
148
Dan Williams6f49a572009-01-06 11:38:14 -0700149static struct module *dma_chan_to_owner(struct dma_chan *chan)
150{
151 return chan->device->dev->driver->owner;
152}
153
154/**
155 * balance_ref_count - catch up the channel reference count
156 * @chan - channel to balance ->client_count versus dmaengine_ref_count
157 *
158 * balance_ref_count must be called under dma_list_mutex
159 */
160static void balance_ref_count(struct dma_chan *chan)
161{
162 struct module *owner = dma_chan_to_owner(chan);
163
164 while (chan->client_count < dmaengine_ref_count) {
165 __module_get(owner);
166 chan->client_count++;
167 }
168}
169
170/**
171 * dma_chan_get - try to grab a dma channel's parent driver module
172 * @chan - channel to grab
173 *
174 * Must be called under dma_list_mutex
175 */
176static int dma_chan_get(struct dma_chan *chan)
177{
178 int err = -ENODEV;
179 struct module *owner = dma_chan_to_owner(chan);
180
181 if (chan->client_count) {
182 __module_get(owner);
183 err = 0;
184 } else if (try_module_get(owner))
185 err = 0;
186
187 if (err == 0)
188 chan->client_count++;
189
190 /* allocate upon first client reference */
191 if (chan->client_count == 1 && err == 0) {
192 int desc_cnt = chan->device->device_alloc_chan_resources(chan, NULL);
193
194 if (desc_cnt < 0) {
195 err = desc_cnt;
196 chan->client_count = 0;
197 module_put(owner);
198 } else
199 balance_ref_count(chan);
200 }
201
202 return err;
203}
204
205/**
206 * dma_chan_put - drop a reference to a dma channel's parent driver module
207 * @chan - channel to release
208 *
209 * Must be called under dma_list_mutex
210 */
211static void dma_chan_put(struct dma_chan *chan)
212{
213 if (!chan->client_count)
214 return; /* this channel failed alloc_chan_resources */
215 chan->client_count--;
216 module_put(dma_chan_to_owner(chan));
217 if (chan->client_count == 0)
218 chan->device->device_free_chan_resources(chan);
219}
220
Chris Leechc13c8262006-05-23 17:18:44 -0700221/**
Dan Williamsd379b012007-07-09 11:56:42 -0700222 * dma_client_chan_alloc - try to allocate channels to a client
Chris Leechc13c8262006-05-23 17:18:44 -0700223 * @client: &dma_client
224 *
225 * Called with dma_list_mutex held.
226 */
Dan Williamsd379b012007-07-09 11:56:42 -0700227static void dma_client_chan_alloc(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700228{
229 struct dma_device *device;
230 struct dma_chan *chan;
Dan Williamsd379b012007-07-09 11:56:42 -0700231 enum dma_state_client ack;
Chris Leechc13c8262006-05-23 17:18:44 -0700232
Dan Williamsd379b012007-07-09 11:56:42 -0700233 /* Find a channel */
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700234 list_for_each_entry(device, &dma_device_list, global_node) {
235 /* Does the client require a specific DMA controller? */
236 if (client->slave && client->slave->dma_dev
237 && client->slave->dma_dev != device->dev)
238 continue;
239
Chris Leechc13c8262006-05-23 17:18:44 -0700240 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williamsd379b012007-07-09 11:56:42 -0700241 if (!dma_chan_satisfies_mask(chan, client->cap_mask))
Chris Leechc13c8262006-05-23 17:18:44 -0700242 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700243 if (!chan->client_count)
244 continue;
245 ack = client->event_callback(client, chan,
246 DMA_RESOURCE_AVAILABLE);
Chris Leechc13c8262006-05-23 17:18:44 -0700247
Dan Williams6f49a572009-01-06 11:38:14 -0700248 /* we are done once this client rejects
249 * an available resource
250 */
251 if (ack == DMA_NAK)
252 return;
Chris Leechc13c8262006-05-23 17:18:44 -0700253 }
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700254 }
Chris Leechc13c8262006-05-23 17:18:44 -0700255}
256
Dan Williams7405f742007-01-02 11:10:43 -0700257enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
258{
259 enum dma_status status;
260 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
261
262 dma_async_issue_pending(chan);
263 do {
264 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
265 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
266 printk(KERN_ERR "dma_sync_wait_timeout!\n");
267 return DMA_ERROR;
268 }
269 } while (status == DMA_IN_PROGRESS);
270
271 return status;
272}
273EXPORT_SYMBOL(dma_sync_wait);
274
Chris Leechc13c8262006-05-23 17:18:44 -0700275/**
Randy Dunlap65088712006-07-03 19:45:31 -0700276 * dma_chan_cleanup - release a DMA channel's resources
277 * @kref: kernel reference structure that contains the DMA channel device
Chris Leechc13c8262006-05-23 17:18:44 -0700278 */
279void dma_chan_cleanup(struct kref *kref)
280{
281 struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
Chris Leechc13c8262006-05-23 17:18:44 -0700282 kref_put(&chan->device->refcount, dma_async_device_cleanup);
283}
David Brownell765e3d82007-03-16 13:38:05 -0800284EXPORT_SYMBOL(dma_chan_cleanup);
Chris Leechc13c8262006-05-23 17:18:44 -0700285
286static void dma_chan_free_rcu(struct rcu_head *rcu)
287{
288 struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
Dan Williams6f49a572009-01-06 11:38:14 -0700289
Chris Leechc13c8262006-05-23 17:18:44 -0700290 kref_put(&chan->refcount, dma_chan_cleanup);
291}
292
Dan Williamsd379b012007-07-09 11:56:42 -0700293static void dma_chan_release(struct dma_chan *chan)
Chris Leechc13c8262006-05-23 17:18:44 -0700294{
Chris Leechc13c8262006-05-23 17:18:44 -0700295 call_rcu(&chan->rcu, dma_chan_free_rcu);
296}
297
298/**
Dan Williamsbec08512009-01-06 11:38:14 -0700299 * dma_cap_mask_all - enable iteration over all operation types
300 */
301static dma_cap_mask_t dma_cap_mask_all;
302
303/**
304 * dma_chan_tbl_ent - tracks channel allocations per core/operation
305 * @chan - associated channel for this entry
306 */
307struct dma_chan_tbl_ent {
308 struct dma_chan *chan;
309};
310
311/**
312 * channel_table - percpu lookup table for memory-to-memory offload providers
313 */
314static struct dma_chan_tbl_ent *channel_table[DMA_TX_TYPE_END];
315
316static int __init dma_channel_table_init(void)
317{
318 enum dma_transaction_type cap;
319 int err = 0;
320
321 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
322
323 /* 'interrupt' and 'slave' are channel capabilities, but are not
324 * associated with an operation so they do not need an entry in the
325 * channel_table
326 */
327 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
328 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
329
330 for_each_dma_cap_mask(cap, dma_cap_mask_all) {
331 channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
332 if (!channel_table[cap]) {
333 err = -ENOMEM;
334 break;
335 }
336 }
337
338 if (err) {
339 pr_err("dmaengine: initialization failure\n");
340 for_each_dma_cap_mask(cap, dma_cap_mask_all)
341 if (channel_table[cap])
342 free_percpu(channel_table[cap]);
343 }
344
345 return err;
346}
347subsys_initcall(dma_channel_table_init);
348
349/**
350 * dma_find_channel - find a channel to carry out the operation
351 * @tx_type: transaction type
352 */
353struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
354{
355 struct dma_chan *chan;
356 int cpu;
357
358 WARN_ONCE(dmaengine_ref_count == 0,
359 "client called %s without a reference", __func__);
360
361 cpu = get_cpu();
362 chan = per_cpu_ptr(channel_table[tx_type], cpu)->chan;
363 put_cpu();
364
365 return chan;
366}
367EXPORT_SYMBOL(dma_find_channel);
368
369/**
Dan Williams2ba05622009-01-06 11:38:14 -0700370 * dma_issue_pending_all - flush all pending operations across all channels
371 */
372void dma_issue_pending_all(void)
373{
374 struct dma_device *device;
375 struct dma_chan *chan;
376
377 WARN_ONCE(dmaengine_ref_count == 0,
378 "client called %s without a reference", __func__);
379
380 rcu_read_lock();
381 list_for_each_entry_rcu(device, &dma_device_list, global_node)
382 list_for_each_entry(chan, &device->channels, device_node)
383 if (chan->client_count)
384 device->device_issue_pending(chan);
385 rcu_read_unlock();
386}
387EXPORT_SYMBOL(dma_issue_pending_all);
388
389/**
Dan Williamsbec08512009-01-06 11:38:14 -0700390 * nth_chan - returns the nth channel of the given capability
391 * @cap: capability to match
392 * @n: nth channel desired
393 *
394 * Defaults to returning the channel with the desired capability and the
395 * lowest reference count when 'n' cannot be satisfied. Must be called
396 * under dma_list_mutex.
397 */
398static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
399{
400 struct dma_device *device;
401 struct dma_chan *chan;
402 struct dma_chan *ret = NULL;
403 struct dma_chan *min = NULL;
404
405 list_for_each_entry(device, &dma_device_list, global_node) {
406 if (!dma_has_cap(cap, device->cap_mask))
407 continue;
408 list_for_each_entry(chan, &device->channels, device_node) {
409 if (!chan->client_count)
410 continue;
411 if (!min)
412 min = chan;
413 else if (chan->table_count < min->table_count)
414 min = chan;
415
416 if (n-- == 0) {
417 ret = chan;
418 break; /* done */
419 }
420 }
421 if (ret)
422 break; /* done */
423 }
424
425 if (!ret)
426 ret = min;
427
428 if (ret)
429 ret->table_count++;
430
431 return ret;
432}
433
434/**
435 * dma_channel_rebalance - redistribute the available channels
436 *
437 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
438 * operation type) in the SMP case, and operation isolation (avoid
439 * multi-tasking channels) in the non-SMP case. Must be called under
440 * dma_list_mutex.
441 */
442static void dma_channel_rebalance(void)
443{
444 struct dma_chan *chan;
445 struct dma_device *device;
446 int cpu;
447 int cap;
448 int n;
449
450 /* undo the last distribution */
451 for_each_dma_cap_mask(cap, dma_cap_mask_all)
452 for_each_possible_cpu(cpu)
453 per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
454
455 list_for_each_entry(device, &dma_device_list, global_node)
456 list_for_each_entry(chan, &device->channels, device_node)
457 chan->table_count = 0;
458
459 /* don't populate the channel_table if no clients are available */
460 if (!dmaengine_ref_count)
461 return;
462
463 /* redistribute available channels */
464 n = 0;
465 for_each_dma_cap_mask(cap, dma_cap_mask_all)
466 for_each_online_cpu(cpu) {
467 if (num_possible_cpus() > 1)
468 chan = nth_chan(cap, n++);
469 else
470 chan = nth_chan(cap, -1);
471
472 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
473 }
474}
475
476/**
Dan Williamsd379b012007-07-09 11:56:42 -0700477 * dma_chans_notify_available - broadcast available channels to the clients
Chris Leechc13c8262006-05-23 17:18:44 -0700478 */
Dan Williamsd379b012007-07-09 11:56:42 -0700479static void dma_clients_notify_available(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700480{
481 struct dma_client *client;
Dan Williamsd379b012007-07-09 11:56:42 -0700482
483 mutex_lock(&dma_list_mutex);
484
485 list_for_each_entry(client, &dma_client_list, global_node)
486 dma_client_chan_alloc(client);
487
488 mutex_unlock(&dma_list_mutex);
489}
490
491/**
Dan Williamsd379b012007-07-09 11:56:42 -0700492 * dma_async_client_register - register a &dma_client
493 * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask'
Chris Leechc13c8262006-05-23 17:18:44 -0700494 */
Dan Williamsd379b012007-07-09 11:56:42 -0700495void dma_async_client_register(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700496{
Dan Williams6f49a572009-01-06 11:38:14 -0700497 struct dma_device *device, *_d;
498 struct dma_chan *chan;
499 int err;
500
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700501 /* validate client data */
502 BUG_ON(dma_has_cap(DMA_SLAVE, client->cap_mask) &&
503 !client->slave);
504
Chris Leechc13c8262006-05-23 17:18:44 -0700505 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700506 dmaengine_ref_count++;
507
508 /* try to grab channels */
509 list_for_each_entry_safe(device, _d, &dma_device_list, global_node)
510 list_for_each_entry(chan, &device->channels, device_node) {
511 err = dma_chan_get(chan);
512 if (err == -ENODEV) {
513 /* module removed before we could use it */
Dan Williams2ba05622009-01-06 11:38:14 -0700514 list_del_rcu(&device->global_node);
Dan Williams6f49a572009-01-06 11:38:14 -0700515 break;
516 } else if (err)
517 pr_err("dmaengine: failed to get %s: (%d)\n",
518 dev_name(&chan->dev), err);
519 }
520
Dan Williamsbec08512009-01-06 11:38:14 -0700521 /* if this is the first reference and there were channels
522 * waiting we need to rebalance to get those channels
523 * incorporated into the channel table
524 */
525 if (dmaengine_ref_count == 1)
526 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700527 list_add_tail(&client->global_node, &dma_client_list);
528 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700529}
David Brownell765e3d82007-03-16 13:38:05 -0800530EXPORT_SYMBOL(dma_async_client_register);
Chris Leechc13c8262006-05-23 17:18:44 -0700531
532/**
533 * dma_async_client_unregister - unregister a client and free the &dma_client
Randy Dunlap65088712006-07-03 19:45:31 -0700534 * @client: &dma_client to free
Chris Leechc13c8262006-05-23 17:18:44 -0700535 *
536 * Force frees any allocated DMA channels, frees the &dma_client memory
537 */
538void dma_async_client_unregister(struct dma_client *client)
539{
Dan Williamsd379b012007-07-09 11:56:42 -0700540 struct dma_device *device;
Chris Leechc13c8262006-05-23 17:18:44 -0700541 struct dma_chan *chan;
542
543 if (!client)
544 return;
545
Chris Leechc13c8262006-05-23 17:18:44 -0700546 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700547 dmaengine_ref_count--;
548 BUG_ON(dmaengine_ref_count < 0);
549 /* drop channel references */
Dan Williamsd379b012007-07-09 11:56:42 -0700550 list_for_each_entry(device, &dma_device_list, global_node)
Dan Williams6f49a572009-01-06 11:38:14 -0700551 list_for_each_entry(chan, &device->channels, device_node)
552 dma_chan_put(chan);
Dan Williamsd379b012007-07-09 11:56:42 -0700553
Chris Leechc13c8262006-05-23 17:18:44 -0700554 list_del(&client->global_node);
555 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700556}
David Brownell765e3d82007-03-16 13:38:05 -0800557EXPORT_SYMBOL(dma_async_client_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -0700558
559/**
Dan Williamsd379b012007-07-09 11:56:42 -0700560 * dma_async_client_chan_request - send all available channels to the
561 * client that satisfy the capability mask
562 * @client - requester
Chris Leechc13c8262006-05-23 17:18:44 -0700563 */
Dan Williamsd379b012007-07-09 11:56:42 -0700564void dma_async_client_chan_request(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700565{
Dan Williamsd379b012007-07-09 11:56:42 -0700566 mutex_lock(&dma_list_mutex);
567 dma_client_chan_alloc(client);
568 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700569}
David Brownell765e3d82007-03-16 13:38:05 -0800570EXPORT_SYMBOL(dma_async_client_chan_request);
Chris Leechc13c8262006-05-23 17:18:44 -0700571
572/**
Randy Dunlap65088712006-07-03 19:45:31 -0700573 * dma_async_device_register - registers DMA devices found
Chris Leechc13c8262006-05-23 17:18:44 -0700574 * @device: &dma_device
575 */
576int dma_async_device_register(struct dma_device *device)
577{
578 static int id;
Jeff Garzikff487fb2007-03-08 09:57:34 -0800579 int chancnt = 0, rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700580 struct dma_chan* chan;
581
582 if (!device)
583 return -ENODEV;
584
Dan Williams7405f742007-01-02 11:10:43 -0700585 /* validate device routines */
586 BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
587 !device->device_prep_dma_memcpy);
588 BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
589 !device->device_prep_dma_xor);
590 BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
591 !device->device_prep_dma_zero_sum);
592 BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
593 !device->device_prep_dma_memset);
Zhang Wei9b941c62008-03-13 17:45:28 -0700594 BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
Dan Williams7405f742007-01-02 11:10:43 -0700595 !device->device_prep_dma_interrupt);
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700596 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
597 !device->device_prep_slave_sg);
598 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
599 !device->device_terminate_all);
Dan Williams7405f742007-01-02 11:10:43 -0700600
601 BUG_ON(!device->device_alloc_chan_resources);
602 BUG_ON(!device->device_free_chan_resources);
Dan Williams7405f742007-01-02 11:10:43 -0700603 BUG_ON(!device->device_is_tx_complete);
604 BUG_ON(!device->device_issue_pending);
605 BUG_ON(!device->dev);
606
Chris Leechc13c8262006-05-23 17:18:44 -0700607 init_completion(&device->done);
608 kref_init(&device->refcount);
Dan Williamsb0b42b12008-12-03 17:17:07 -0700609
610 mutex_lock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700611 device->dev_id = id++;
Dan Williamsb0b42b12008-12-03 17:17:07 -0700612 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700613
614 /* represent channels in sysfs. Probably want devs too */
615 list_for_each_entry(chan, &device->channels, device_node) {
616 chan->local = alloc_percpu(typeof(*chan->local));
617 if (chan->local == NULL)
618 continue;
619
620 chan->chan_id = chancnt++;
Tony Jones891f78e2007-09-25 02:03:03 +0200621 chan->dev.class = &dma_devclass;
Haavard Skinnemoen1099dc72008-07-08 11:58:05 -0700622 chan->dev.parent = device->dev;
Kay Sievers06190d82008-11-11 13:12:33 -0700623 dev_set_name(&chan->dev, "dma%dchan%d",
624 device->dev_id, chan->chan_id);
Chris Leechc13c8262006-05-23 17:18:44 -0700625
Tony Jones891f78e2007-09-25 02:03:03 +0200626 rc = device_register(&chan->dev);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800627 if (rc) {
628 chancnt--;
629 free_percpu(chan->local);
630 chan->local = NULL;
631 goto err_out;
632 }
633
Haavard Skinnemoen348badf2007-11-14 16:59:27 -0800634 /* One for the channel, one of the class device */
635 kref_get(&device->refcount);
Chris Leechc13c8262006-05-23 17:18:44 -0700636 kref_get(&device->refcount);
Dan Williamsd379b012007-07-09 11:56:42 -0700637 kref_init(&chan->refcount);
Dan Williams7cc5bf92008-07-08 11:58:21 -0700638 chan->client_count = 0;
Dan Williamsd379b012007-07-09 11:56:42 -0700639 chan->slow_ref = 0;
640 INIT_RCU_HEAD(&chan->rcu);
Chris Leechc13c8262006-05-23 17:18:44 -0700641 }
642
643 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700644 if (dmaengine_ref_count)
645 list_for_each_entry(chan, &device->channels, device_node) {
646 /* if clients are already waiting for channels we need
647 * to take references on their behalf
648 */
649 if (dma_chan_get(chan) == -ENODEV) {
650 /* note we can only get here for the first
651 * channel as the remaining channels are
652 * guaranteed to get a reference
653 */
654 rc = -ENODEV;
655 mutex_unlock(&dma_list_mutex);
656 goto err_out;
657 }
658 }
Dan Williams2ba05622009-01-06 11:38:14 -0700659 list_add_tail_rcu(&device->global_node, &dma_device_list);
Dan Williamsbec08512009-01-06 11:38:14 -0700660 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700661 mutex_unlock(&dma_list_mutex);
662
Dan Williamsd379b012007-07-09 11:56:42 -0700663 dma_clients_notify_available();
Chris Leechc13c8262006-05-23 17:18:44 -0700664
665 return 0;
Jeff Garzikff487fb2007-03-08 09:57:34 -0800666
667err_out:
668 list_for_each_entry(chan, &device->channels, device_node) {
669 if (chan->local == NULL)
670 continue;
671 kref_put(&device->refcount, dma_async_device_cleanup);
Tony Jones891f78e2007-09-25 02:03:03 +0200672 device_unregister(&chan->dev);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800673 chancnt--;
674 free_percpu(chan->local);
675 }
676 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700677}
David Brownell765e3d82007-03-16 13:38:05 -0800678EXPORT_SYMBOL(dma_async_device_register);
Chris Leechc13c8262006-05-23 17:18:44 -0700679
680/**
Randy Dunlap65088712006-07-03 19:45:31 -0700681 * dma_async_device_cleanup - function called when all references are released
682 * @kref: kernel reference object
Chris Leechc13c8262006-05-23 17:18:44 -0700683 */
684static void dma_async_device_cleanup(struct kref *kref)
685{
686 struct dma_device *device;
687
688 device = container_of(kref, struct dma_device, refcount);
689 complete(&device->done);
690}
691
Randy Dunlap65088712006-07-03 19:45:31 -0700692/**
Dan Williams6f49a572009-01-06 11:38:14 -0700693 * dma_async_device_unregister - unregister a DMA device
Randy Dunlap65088712006-07-03 19:45:31 -0700694 * @device: &dma_device
695 */
696void dma_async_device_unregister(struct dma_device *device)
Chris Leechc13c8262006-05-23 17:18:44 -0700697{
698 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700699
700 mutex_lock(&dma_list_mutex);
Dan Williams2ba05622009-01-06 11:38:14 -0700701 list_del_rcu(&device->global_node);
Dan Williamsbec08512009-01-06 11:38:14 -0700702 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700703 mutex_unlock(&dma_list_mutex);
704
705 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williams6f49a572009-01-06 11:38:14 -0700706 WARN_ONCE(chan->client_count,
707 "%s called while %d clients hold a reference\n",
708 __func__, chan->client_count);
Tony Jones891f78e2007-09-25 02:03:03 +0200709 device_unregister(&chan->dev);
Dan Williamsd379b012007-07-09 11:56:42 -0700710 dma_chan_release(chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700711 }
Chris Leechc13c8262006-05-23 17:18:44 -0700712
713 kref_put(&device->refcount, dma_async_device_cleanup);
714 wait_for_completion(&device->done);
715}
David Brownell765e3d82007-03-16 13:38:05 -0800716EXPORT_SYMBOL(dma_async_device_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -0700717
Dan Williams7405f742007-01-02 11:10:43 -0700718/**
719 * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
720 * @chan: DMA channel to offload copy to
721 * @dest: destination address (virtual)
722 * @src: source address (virtual)
723 * @len: length
724 *
725 * Both @dest and @src must be mappable to a bus address according to the
726 * DMA mapping API rules for streaming mappings.
727 * Both @dest and @src must stay memory resident (kernel memory or locked
728 * user space pages).
729 */
730dma_cookie_t
731dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
732 void *src, size_t len)
733{
734 struct dma_device *dev = chan->device;
735 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700736 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700737 dma_cookie_t cookie;
738 int cpu;
739
Dan Williams00367312008-02-02 19:49:57 -0700740 dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
741 dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700742 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
743 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700744
745 if (!tx) {
746 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
747 dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700748 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700749 }
Dan Williams7405f742007-01-02 11:10:43 -0700750
Dan Williams7405f742007-01-02 11:10:43 -0700751 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700752 cookie = tx->tx_submit(tx);
753
754 cpu = get_cpu();
755 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
756 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
757 put_cpu();
758
759 return cookie;
760}
761EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
762
763/**
764 * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
765 * @chan: DMA channel to offload copy to
766 * @page: destination page
767 * @offset: offset in page to copy to
768 * @kdata: source address (virtual)
769 * @len: length
770 *
771 * Both @page/@offset and @kdata must be mappable to a bus address according
772 * to the DMA mapping API rules for streaming mappings.
773 * Both @page/@offset and @kdata must stay memory resident (kernel memory or
774 * locked user space pages)
775 */
776dma_cookie_t
777dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
778 unsigned int offset, void *kdata, size_t len)
779{
780 struct dma_device *dev = chan->device;
781 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700782 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700783 dma_cookie_t cookie;
784 int cpu;
785
Dan Williams00367312008-02-02 19:49:57 -0700786 dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
787 dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700788 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
789 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700790
791 if (!tx) {
792 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
793 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700794 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700795 }
Dan Williams7405f742007-01-02 11:10:43 -0700796
Dan Williams7405f742007-01-02 11:10:43 -0700797 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700798 cookie = tx->tx_submit(tx);
799
800 cpu = get_cpu();
801 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
802 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
803 put_cpu();
804
805 return cookie;
806}
807EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
808
809/**
810 * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
811 * @chan: DMA channel to offload copy to
812 * @dest_pg: destination page
813 * @dest_off: offset in page to copy to
814 * @src_pg: source page
815 * @src_off: offset in page to copy from
816 * @len: length
817 *
818 * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
819 * address according to the DMA mapping API rules for streaming mappings.
820 * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
821 * (kernel memory or locked user space pages).
822 */
823dma_cookie_t
824dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
825 unsigned int dest_off, struct page *src_pg, unsigned int src_off,
826 size_t len)
827{
828 struct dma_device *dev = chan->device;
829 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700830 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700831 dma_cookie_t cookie;
832 int cpu;
833
Dan Williams00367312008-02-02 19:49:57 -0700834 dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
835 dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
836 DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700837 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
838 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700839
840 if (!tx) {
841 dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
842 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700843 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700844 }
Dan Williams7405f742007-01-02 11:10:43 -0700845
Dan Williams7405f742007-01-02 11:10:43 -0700846 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700847 cookie = tx->tx_submit(tx);
848
849 cpu = get_cpu();
850 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
851 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
852 put_cpu();
853
854 return cookie;
855}
856EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
857
858void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
859 struct dma_chan *chan)
860{
861 tx->chan = chan;
862 spin_lock_init(&tx->lock);
Dan Williams7405f742007-01-02 11:10:43 -0700863}
864EXPORT_SYMBOL(dma_async_tx_descriptor_init);
865
Dan Williams07f22112009-01-05 17:14:31 -0700866/* dma_wait_for_async_tx - spin wait for a transaction to complete
867 * @tx: in-flight transaction to wait on
868 *
869 * This routine assumes that tx was obtained from a call to async_memcpy,
870 * async_xor, async_memset, etc which ensures that tx is "in-flight" (prepped
871 * and submitted). Walking the parent chain is only meant to cover for DMA
872 * drivers that do not implement the DMA_INTERRUPT capability and may race with
873 * the driver's descriptor cleanup routine.
874 */
875enum dma_status
876dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
877{
878 enum dma_status status;
879 struct dma_async_tx_descriptor *iter;
880 struct dma_async_tx_descriptor *parent;
881
882 if (!tx)
883 return DMA_SUCCESS;
884
885 WARN_ONCE(tx->parent, "%s: speculatively walking dependency chain for"
886 " %s\n", __func__, dev_name(&tx->chan->dev));
887
888 /* poll through the dependency chain, return when tx is complete */
889 do {
890 iter = tx;
891
892 /* find the root of the unsubmitted dependency chain */
893 do {
894 parent = iter->parent;
895 if (!parent)
896 break;
897 else
898 iter = parent;
899 } while (parent);
900
901 /* there is a small window for ->parent == NULL and
902 * ->cookie == -EBUSY
903 */
904 while (iter->cookie == -EBUSY)
905 cpu_relax();
906
907 status = dma_sync_wait(iter->chan, iter->cookie);
908 } while (status == DMA_IN_PROGRESS || (iter != tx));
909
910 return status;
911}
912EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
913
914/* dma_run_dependencies - helper routine for dma drivers to process
915 * (start) dependent operations on their target channel
916 * @tx: transaction with dependencies
917 */
918void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
919{
920 struct dma_async_tx_descriptor *dep = tx->next;
921 struct dma_async_tx_descriptor *dep_next;
922 struct dma_chan *chan;
923
924 if (!dep)
925 return;
926
927 chan = dep->chan;
928
929 /* keep submitting up until a channel switch is detected
930 * in that case we will be called again as a result of
931 * processing the interrupt from async_tx_channel_switch
932 */
933 for (; dep; dep = dep_next) {
934 spin_lock_bh(&dep->lock);
935 dep->parent = NULL;
936 dep_next = dep->next;
937 if (dep_next && dep_next->chan == chan)
938 dep->next = NULL; /* ->next will be submitted */
939 else
940 dep_next = NULL; /* submit current dep and terminate */
941 spin_unlock_bh(&dep->lock);
942
943 dep->tx_submit(dep);
944 }
945
946 chan->device->device_issue_pending(chan);
947}
948EXPORT_SYMBOL_GPL(dma_run_dependencies);
949
Chris Leechc13c8262006-05-23 17:18:44 -0700950static int __init dma_bus_init(void)
951{
952 mutex_init(&dma_list_mutex);
953 return class_register(&dma_devclass);
954}
Chris Leechc13c8262006-05-23 17:18:44 -0700955subsys_initcall(dma_bus_init);
956
Dan Williamsbec08512009-01-06 11:38:14 -0700957