blob: 87a8cd4791ed0adcab1b086db3876702442c5259 [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>
Chris Leechc13c8262006-05-23 17:18:44 -070073
74static DEFINE_MUTEX(dma_list_mutex);
75static LIST_HEAD(dma_device_list);
76static LIST_HEAD(dma_client_list);
Dan Williams6f49a572009-01-06 11:38:14 -070077static long dmaengine_ref_count;
Chris Leechc13c8262006-05-23 17:18:44 -070078
79/* --- sysfs implementation --- */
80
Tony Jones891f78e2007-09-25 02:03:03 +020081static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -070082{
Tony Jones891f78e2007-09-25 02:03:03 +020083 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -070084 unsigned long count = 0;
85 int i;
86
Andrew Morton17f3ae02006-05-25 13:26:53 -070087 for_each_possible_cpu(i)
Chris Leechc13c8262006-05-23 17:18:44 -070088 count += per_cpu_ptr(chan->local, i)->memcpy_count;
89
90 return sprintf(buf, "%lu\n", count);
91}
92
Tony Jones891f78e2007-09-25 02:03:03 +020093static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr,
94 char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -070095{
Tony Jones891f78e2007-09-25 02:03:03 +020096 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -070097 unsigned long count = 0;
98 int i;
99
Andrew Morton17f3ae02006-05-25 13:26:53 -0700100 for_each_possible_cpu(i)
Chris Leechc13c8262006-05-23 17:18:44 -0700101 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
102
103 return sprintf(buf, "%lu\n", count);
104}
105
Tony Jones891f78e2007-09-25 02:03:03 +0200106static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
Chris Leechc13c8262006-05-23 17:18:44 -0700107{
Tony Jones891f78e2007-09-25 02:03:03 +0200108 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700109
Dan Williams6f49a572009-01-06 11:38:14 -0700110 return sprintf(buf, "%d\n", chan->client_count);
Chris Leechc13c8262006-05-23 17:18:44 -0700111}
112
Tony Jones891f78e2007-09-25 02:03:03 +0200113static struct device_attribute dma_attrs[] = {
Chris Leechc13c8262006-05-23 17:18:44 -0700114 __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
115 __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
116 __ATTR(in_use, S_IRUGO, show_in_use, NULL),
117 __ATTR_NULL
118};
119
120static void dma_async_device_cleanup(struct kref *kref);
121
Tony Jones891f78e2007-09-25 02:03:03 +0200122static void dma_dev_release(struct device *dev)
Chris Leechc13c8262006-05-23 17:18:44 -0700123{
Tony Jones891f78e2007-09-25 02:03:03 +0200124 struct dma_chan *chan = to_dma_chan(dev);
Chris Leechc13c8262006-05-23 17:18:44 -0700125 kref_put(&chan->device->refcount, dma_async_device_cleanup);
126}
127
128static struct class dma_devclass = {
Tony Jones891f78e2007-09-25 02:03:03 +0200129 .name = "dma",
130 .dev_attrs = dma_attrs,
131 .dev_release = dma_dev_release,
Chris Leechc13c8262006-05-23 17:18:44 -0700132};
133
134/* --- client and device registration --- */
135
Dan Williamsd379b012007-07-09 11:56:42 -0700136#define dma_chan_satisfies_mask(chan, mask) \
137 __dma_chan_satisfies_mask((chan), &(mask))
138static int
139__dma_chan_satisfies_mask(struct dma_chan *chan, dma_cap_mask_t *want)
140{
141 dma_cap_mask_t has;
142
143 bitmap_and(has.bits, want->bits, chan->device->cap_mask.bits,
144 DMA_TX_TYPE_END);
145 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
146}
147
Dan Williams6f49a572009-01-06 11:38:14 -0700148static struct module *dma_chan_to_owner(struct dma_chan *chan)
149{
150 return chan->device->dev->driver->owner;
151}
152
153/**
154 * balance_ref_count - catch up the channel reference count
155 * @chan - channel to balance ->client_count versus dmaengine_ref_count
156 *
157 * balance_ref_count must be called under dma_list_mutex
158 */
159static void balance_ref_count(struct dma_chan *chan)
160{
161 struct module *owner = dma_chan_to_owner(chan);
162
163 while (chan->client_count < dmaengine_ref_count) {
164 __module_get(owner);
165 chan->client_count++;
166 }
167}
168
169/**
170 * dma_chan_get - try to grab a dma channel's parent driver module
171 * @chan - channel to grab
172 *
173 * Must be called under dma_list_mutex
174 */
175static int dma_chan_get(struct dma_chan *chan)
176{
177 int err = -ENODEV;
178 struct module *owner = dma_chan_to_owner(chan);
179
180 if (chan->client_count) {
181 __module_get(owner);
182 err = 0;
183 } else if (try_module_get(owner))
184 err = 0;
185
186 if (err == 0)
187 chan->client_count++;
188
189 /* allocate upon first client reference */
190 if (chan->client_count == 1 && err == 0) {
191 int desc_cnt = chan->device->device_alloc_chan_resources(chan, NULL);
192
193 if (desc_cnt < 0) {
194 err = desc_cnt;
195 chan->client_count = 0;
196 module_put(owner);
197 } else
198 balance_ref_count(chan);
199 }
200
201 return err;
202}
203
204/**
205 * dma_chan_put - drop a reference to a dma channel's parent driver module
206 * @chan - channel to release
207 *
208 * Must be called under dma_list_mutex
209 */
210static void dma_chan_put(struct dma_chan *chan)
211{
212 if (!chan->client_count)
213 return; /* this channel failed alloc_chan_resources */
214 chan->client_count--;
215 module_put(dma_chan_to_owner(chan));
216 if (chan->client_count == 0)
217 chan->device->device_free_chan_resources(chan);
218}
219
Chris Leechc13c8262006-05-23 17:18:44 -0700220/**
Dan Williamsd379b012007-07-09 11:56:42 -0700221 * dma_client_chan_alloc - try to allocate channels to a client
Chris Leechc13c8262006-05-23 17:18:44 -0700222 * @client: &dma_client
223 *
224 * Called with dma_list_mutex held.
225 */
Dan Williamsd379b012007-07-09 11:56:42 -0700226static void dma_client_chan_alloc(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700227{
228 struct dma_device *device;
229 struct dma_chan *chan;
Dan Williamsd379b012007-07-09 11:56:42 -0700230 enum dma_state_client ack;
Chris Leechc13c8262006-05-23 17:18:44 -0700231
Dan Williamsd379b012007-07-09 11:56:42 -0700232 /* Find a channel */
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700233 list_for_each_entry(device, &dma_device_list, global_node) {
234 /* Does the client require a specific DMA controller? */
235 if (client->slave && client->slave->dma_dev
236 && client->slave->dma_dev != device->dev)
237 continue;
238
Chris Leechc13c8262006-05-23 17:18:44 -0700239 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williamsd379b012007-07-09 11:56:42 -0700240 if (!dma_chan_satisfies_mask(chan, client->cap_mask))
Chris Leechc13c8262006-05-23 17:18:44 -0700241 continue;
Dan Williams6f49a572009-01-06 11:38:14 -0700242 if (!chan->client_count)
243 continue;
244 ack = client->event_callback(client, chan,
245 DMA_RESOURCE_AVAILABLE);
Chris Leechc13c8262006-05-23 17:18:44 -0700246
Dan Williams6f49a572009-01-06 11:38:14 -0700247 /* we are done once this client rejects
248 * an available resource
249 */
250 if (ack == DMA_NAK)
251 return;
Chris Leechc13c8262006-05-23 17:18:44 -0700252 }
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700253 }
Chris Leechc13c8262006-05-23 17:18:44 -0700254}
255
Dan Williams7405f742007-01-02 11:10:43 -0700256enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
257{
258 enum dma_status status;
259 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
260
261 dma_async_issue_pending(chan);
262 do {
263 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
264 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
265 printk(KERN_ERR "dma_sync_wait_timeout!\n");
266 return DMA_ERROR;
267 }
268 } while (status == DMA_IN_PROGRESS);
269
270 return status;
271}
272EXPORT_SYMBOL(dma_sync_wait);
273
Chris Leechc13c8262006-05-23 17:18:44 -0700274/**
Randy Dunlap65088712006-07-03 19:45:31 -0700275 * dma_chan_cleanup - release a DMA channel's resources
276 * @kref: kernel reference structure that contains the DMA channel device
Chris Leechc13c8262006-05-23 17:18:44 -0700277 */
278void dma_chan_cleanup(struct kref *kref)
279{
280 struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
Chris Leechc13c8262006-05-23 17:18:44 -0700281 kref_put(&chan->device->refcount, dma_async_device_cleanup);
282}
David Brownell765e3d82007-03-16 13:38:05 -0800283EXPORT_SYMBOL(dma_chan_cleanup);
Chris Leechc13c8262006-05-23 17:18:44 -0700284
285static void dma_chan_free_rcu(struct rcu_head *rcu)
286{
287 struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
Dan Williams6f49a572009-01-06 11:38:14 -0700288
Chris Leechc13c8262006-05-23 17:18:44 -0700289 kref_put(&chan->refcount, dma_chan_cleanup);
290}
291
Dan Williamsd379b012007-07-09 11:56:42 -0700292static void dma_chan_release(struct dma_chan *chan)
Chris Leechc13c8262006-05-23 17:18:44 -0700293{
Chris Leechc13c8262006-05-23 17:18:44 -0700294 call_rcu(&chan->rcu, dma_chan_free_rcu);
295}
296
297/**
Dan Williamsbec08512009-01-06 11:38:14 -0700298 * dma_cap_mask_all - enable iteration over all operation types
299 */
300static dma_cap_mask_t dma_cap_mask_all;
301
302/**
303 * dma_chan_tbl_ent - tracks channel allocations per core/operation
304 * @chan - associated channel for this entry
305 */
306struct dma_chan_tbl_ent {
307 struct dma_chan *chan;
308};
309
310/**
311 * channel_table - percpu lookup table for memory-to-memory offload providers
312 */
313static struct dma_chan_tbl_ent *channel_table[DMA_TX_TYPE_END];
314
315static int __init dma_channel_table_init(void)
316{
317 enum dma_transaction_type cap;
318 int err = 0;
319
320 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
321
322 /* 'interrupt' and 'slave' are channel capabilities, but are not
323 * associated with an operation so they do not need an entry in the
324 * channel_table
325 */
326 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
327 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
328
329 for_each_dma_cap_mask(cap, dma_cap_mask_all) {
330 channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
331 if (!channel_table[cap]) {
332 err = -ENOMEM;
333 break;
334 }
335 }
336
337 if (err) {
338 pr_err("dmaengine: initialization failure\n");
339 for_each_dma_cap_mask(cap, dma_cap_mask_all)
340 if (channel_table[cap])
341 free_percpu(channel_table[cap]);
342 }
343
344 return err;
345}
346subsys_initcall(dma_channel_table_init);
347
348/**
349 * dma_find_channel - find a channel to carry out the operation
350 * @tx_type: transaction type
351 */
352struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
353{
354 struct dma_chan *chan;
355 int cpu;
356
357 WARN_ONCE(dmaengine_ref_count == 0,
358 "client called %s without a reference", __func__);
359
360 cpu = get_cpu();
361 chan = per_cpu_ptr(channel_table[tx_type], cpu)->chan;
362 put_cpu();
363
364 return chan;
365}
366EXPORT_SYMBOL(dma_find_channel);
367
368/**
369 * nth_chan - returns the nth channel of the given capability
370 * @cap: capability to match
371 * @n: nth channel desired
372 *
373 * Defaults to returning the channel with the desired capability and the
374 * lowest reference count when 'n' cannot be satisfied. Must be called
375 * under dma_list_mutex.
376 */
377static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
378{
379 struct dma_device *device;
380 struct dma_chan *chan;
381 struct dma_chan *ret = NULL;
382 struct dma_chan *min = NULL;
383
384 list_for_each_entry(device, &dma_device_list, global_node) {
385 if (!dma_has_cap(cap, device->cap_mask))
386 continue;
387 list_for_each_entry(chan, &device->channels, device_node) {
388 if (!chan->client_count)
389 continue;
390 if (!min)
391 min = chan;
392 else if (chan->table_count < min->table_count)
393 min = chan;
394
395 if (n-- == 0) {
396 ret = chan;
397 break; /* done */
398 }
399 }
400 if (ret)
401 break; /* done */
402 }
403
404 if (!ret)
405 ret = min;
406
407 if (ret)
408 ret->table_count++;
409
410 return ret;
411}
412
413/**
414 * dma_channel_rebalance - redistribute the available channels
415 *
416 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
417 * operation type) in the SMP case, and operation isolation (avoid
418 * multi-tasking channels) in the non-SMP case. Must be called under
419 * dma_list_mutex.
420 */
421static void dma_channel_rebalance(void)
422{
423 struct dma_chan *chan;
424 struct dma_device *device;
425 int cpu;
426 int cap;
427 int n;
428
429 /* undo the last distribution */
430 for_each_dma_cap_mask(cap, dma_cap_mask_all)
431 for_each_possible_cpu(cpu)
432 per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
433
434 list_for_each_entry(device, &dma_device_list, global_node)
435 list_for_each_entry(chan, &device->channels, device_node)
436 chan->table_count = 0;
437
438 /* don't populate the channel_table if no clients are available */
439 if (!dmaengine_ref_count)
440 return;
441
442 /* redistribute available channels */
443 n = 0;
444 for_each_dma_cap_mask(cap, dma_cap_mask_all)
445 for_each_online_cpu(cpu) {
446 if (num_possible_cpus() > 1)
447 chan = nth_chan(cap, n++);
448 else
449 chan = nth_chan(cap, -1);
450
451 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
452 }
453}
454
455/**
Dan Williamsd379b012007-07-09 11:56:42 -0700456 * dma_chans_notify_available - broadcast available channels to the clients
Chris Leechc13c8262006-05-23 17:18:44 -0700457 */
Dan Williamsd379b012007-07-09 11:56:42 -0700458static void dma_clients_notify_available(void)
Chris Leechc13c8262006-05-23 17:18:44 -0700459{
460 struct dma_client *client;
Dan Williamsd379b012007-07-09 11:56:42 -0700461
462 mutex_lock(&dma_list_mutex);
463
464 list_for_each_entry(client, &dma_client_list, global_node)
465 dma_client_chan_alloc(client);
466
467 mutex_unlock(&dma_list_mutex);
468}
469
470/**
Dan Williamsd379b012007-07-09 11:56:42 -0700471 * dma_async_client_register - register a &dma_client
472 * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask'
Chris Leechc13c8262006-05-23 17:18:44 -0700473 */
Dan Williamsd379b012007-07-09 11:56:42 -0700474void dma_async_client_register(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700475{
Dan Williams6f49a572009-01-06 11:38:14 -0700476 struct dma_device *device, *_d;
477 struct dma_chan *chan;
478 int err;
479
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700480 /* validate client data */
481 BUG_ON(dma_has_cap(DMA_SLAVE, client->cap_mask) &&
482 !client->slave);
483
Chris Leechc13c8262006-05-23 17:18:44 -0700484 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700485 dmaengine_ref_count++;
486
487 /* try to grab channels */
488 list_for_each_entry_safe(device, _d, &dma_device_list, global_node)
489 list_for_each_entry(chan, &device->channels, device_node) {
490 err = dma_chan_get(chan);
491 if (err == -ENODEV) {
492 /* module removed before we could use it */
493 list_del_init(&device->global_node);
494 break;
495 } else if (err)
496 pr_err("dmaengine: failed to get %s: (%d)\n",
497 dev_name(&chan->dev), err);
498 }
499
Dan Williamsbec08512009-01-06 11:38:14 -0700500 /* if this is the first reference and there were channels
501 * waiting we need to rebalance to get those channels
502 * incorporated into the channel table
503 */
504 if (dmaengine_ref_count == 1)
505 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700506 list_add_tail(&client->global_node, &dma_client_list);
507 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700508}
David Brownell765e3d82007-03-16 13:38:05 -0800509EXPORT_SYMBOL(dma_async_client_register);
Chris Leechc13c8262006-05-23 17:18:44 -0700510
511/**
512 * dma_async_client_unregister - unregister a client and free the &dma_client
Randy Dunlap65088712006-07-03 19:45:31 -0700513 * @client: &dma_client to free
Chris Leechc13c8262006-05-23 17:18:44 -0700514 *
515 * Force frees any allocated DMA channels, frees the &dma_client memory
516 */
517void dma_async_client_unregister(struct dma_client *client)
518{
Dan Williamsd379b012007-07-09 11:56:42 -0700519 struct dma_device *device;
Chris Leechc13c8262006-05-23 17:18:44 -0700520 struct dma_chan *chan;
521
522 if (!client)
523 return;
524
Chris Leechc13c8262006-05-23 17:18:44 -0700525 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700526 dmaengine_ref_count--;
527 BUG_ON(dmaengine_ref_count < 0);
528 /* drop channel references */
Dan Williamsd379b012007-07-09 11:56:42 -0700529 list_for_each_entry(device, &dma_device_list, global_node)
Dan Williams6f49a572009-01-06 11:38:14 -0700530 list_for_each_entry(chan, &device->channels, device_node)
531 dma_chan_put(chan);
Dan Williamsd379b012007-07-09 11:56:42 -0700532
Chris Leechc13c8262006-05-23 17:18:44 -0700533 list_del(&client->global_node);
534 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700535}
David Brownell765e3d82007-03-16 13:38:05 -0800536EXPORT_SYMBOL(dma_async_client_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -0700537
538/**
Dan Williamsd379b012007-07-09 11:56:42 -0700539 * dma_async_client_chan_request - send all available channels to the
540 * client that satisfy the capability mask
541 * @client - requester
Chris Leechc13c8262006-05-23 17:18:44 -0700542 */
Dan Williamsd379b012007-07-09 11:56:42 -0700543void dma_async_client_chan_request(struct dma_client *client)
Chris Leechc13c8262006-05-23 17:18:44 -0700544{
Dan Williamsd379b012007-07-09 11:56:42 -0700545 mutex_lock(&dma_list_mutex);
546 dma_client_chan_alloc(client);
547 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700548}
David Brownell765e3d82007-03-16 13:38:05 -0800549EXPORT_SYMBOL(dma_async_client_chan_request);
Chris Leechc13c8262006-05-23 17:18:44 -0700550
551/**
Randy Dunlap65088712006-07-03 19:45:31 -0700552 * dma_async_device_register - registers DMA devices found
Chris Leechc13c8262006-05-23 17:18:44 -0700553 * @device: &dma_device
554 */
555int dma_async_device_register(struct dma_device *device)
556{
557 static int id;
Jeff Garzikff487fb2007-03-08 09:57:34 -0800558 int chancnt = 0, rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700559 struct dma_chan* chan;
560
561 if (!device)
562 return -ENODEV;
563
Dan Williams7405f742007-01-02 11:10:43 -0700564 /* validate device routines */
565 BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
566 !device->device_prep_dma_memcpy);
567 BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
568 !device->device_prep_dma_xor);
569 BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
570 !device->device_prep_dma_zero_sum);
571 BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
572 !device->device_prep_dma_memset);
Zhang Wei9b941c62008-03-13 17:45:28 -0700573 BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
Dan Williams7405f742007-01-02 11:10:43 -0700574 !device->device_prep_dma_interrupt);
Haavard Skinnemoendc0ee6432008-07-08 11:59:35 -0700575 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
576 !device->device_prep_slave_sg);
577 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
578 !device->device_terminate_all);
Dan Williams7405f742007-01-02 11:10:43 -0700579
580 BUG_ON(!device->device_alloc_chan_resources);
581 BUG_ON(!device->device_free_chan_resources);
Dan Williams7405f742007-01-02 11:10:43 -0700582 BUG_ON(!device->device_is_tx_complete);
583 BUG_ON(!device->device_issue_pending);
584 BUG_ON(!device->dev);
585
Chris Leechc13c8262006-05-23 17:18:44 -0700586 init_completion(&device->done);
587 kref_init(&device->refcount);
Dan Williamsb0b42b12008-12-03 17:17:07 -0700588
589 mutex_lock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700590 device->dev_id = id++;
Dan Williamsb0b42b12008-12-03 17:17:07 -0700591 mutex_unlock(&dma_list_mutex);
Chris Leechc13c8262006-05-23 17:18:44 -0700592
593 /* represent channels in sysfs. Probably want devs too */
594 list_for_each_entry(chan, &device->channels, device_node) {
595 chan->local = alloc_percpu(typeof(*chan->local));
596 if (chan->local == NULL)
597 continue;
598
599 chan->chan_id = chancnt++;
Tony Jones891f78e2007-09-25 02:03:03 +0200600 chan->dev.class = &dma_devclass;
Haavard Skinnemoen1099dc72008-07-08 11:58:05 -0700601 chan->dev.parent = device->dev;
Kay Sievers06190d82008-11-11 13:12:33 -0700602 dev_set_name(&chan->dev, "dma%dchan%d",
603 device->dev_id, chan->chan_id);
Chris Leechc13c8262006-05-23 17:18:44 -0700604
Tony Jones891f78e2007-09-25 02:03:03 +0200605 rc = device_register(&chan->dev);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800606 if (rc) {
607 chancnt--;
608 free_percpu(chan->local);
609 chan->local = NULL;
610 goto err_out;
611 }
612
Haavard Skinnemoen348badf2007-11-14 16:59:27 -0800613 /* One for the channel, one of the class device */
614 kref_get(&device->refcount);
Chris Leechc13c8262006-05-23 17:18:44 -0700615 kref_get(&device->refcount);
Dan Williamsd379b012007-07-09 11:56:42 -0700616 kref_init(&chan->refcount);
Dan Williams7cc5bf92008-07-08 11:58:21 -0700617 chan->client_count = 0;
Dan Williamsd379b012007-07-09 11:56:42 -0700618 chan->slow_ref = 0;
619 INIT_RCU_HEAD(&chan->rcu);
Chris Leechc13c8262006-05-23 17:18:44 -0700620 }
621
622 mutex_lock(&dma_list_mutex);
Dan Williams6f49a572009-01-06 11:38:14 -0700623 if (dmaengine_ref_count)
624 list_for_each_entry(chan, &device->channels, device_node) {
625 /* if clients are already waiting for channels we need
626 * to take references on their behalf
627 */
628 if (dma_chan_get(chan) == -ENODEV) {
629 /* note we can only get here for the first
630 * channel as the remaining channels are
631 * guaranteed to get a reference
632 */
633 rc = -ENODEV;
634 mutex_unlock(&dma_list_mutex);
635 goto err_out;
636 }
637 }
Chris Leechc13c8262006-05-23 17:18:44 -0700638 list_add_tail(&device->global_node, &dma_device_list);
Dan Williamsbec08512009-01-06 11:38:14 -0700639 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700640 mutex_unlock(&dma_list_mutex);
641
Dan Williamsd379b012007-07-09 11:56:42 -0700642 dma_clients_notify_available();
Chris Leechc13c8262006-05-23 17:18:44 -0700643
644 return 0;
Jeff Garzikff487fb2007-03-08 09:57:34 -0800645
646err_out:
647 list_for_each_entry(chan, &device->channels, device_node) {
648 if (chan->local == NULL)
649 continue;
650 kref_put(&device->refcount, dma_async_device_cleanup);
Tony Jones891f78e2007-09-25 02:03:03 +0200651 device_unregister(&chan->dev);
Jeff Garzikff487fb2007-03-08 09:57:34 -0800652 chancnt--;
653 free_percpu(chan->local);
654 }
655 return rc;
Chris Leechc13c8262006-05-23 17:18:44 -0700656}
David Brownell765e3d82007-03-16 13:38:05 -0800657EXPORT_SYMBOL(dma_async_device_register);
Chris Leechc13c8262006-05-23 17:18:44 -0700658
659/**
Randy Dunlap65088712006-07-03 19:45:31 -0700660 * dma_async_device_cleanup - function called when all references are released
661 * @kref: kernel reference object
Chris Leechc13c8262006-05-23 17:18:44 -0700662 */
663static void dma_async_device_cleanup(struct kref *kref)
664{
665 struct dma_device *device;
666
667 device = container_of(kref, struct dma_device, refcount);
668 complete(&device->done);
669}
670
Randy Dunlap65088712006-07-03 19:45:31 -0700671/**
Dan Williams6f49a572009-01-06 11:38:14 -0700672 * dma_async_device_unregister - unregister a DMA device
Randy Dunlap65088712006-07-03 19:45:31 -0700673 * @device: &dma_device
674 */
675void dma_async_device_unregister(struct dma_device *device)
Chris Leechc13c8262006-05-23 17:18:44 -0700676{
677 struct dma_chan *chan;
Chris Leechc13c8262006-05-23 17:18:44 -0700678
679 mutex_lock(&dma_list_mutex);
680 list_del(&device->global_node);
Dan Williamsbec08512009-01-06 11:38:14 -0700681 dma_channel_rebalance();
Chris Leechc13c8262006-05-23 17:18:44 -0700682 mutex_unlock(&dma_list_mutex);
683
684 list_for_each_entry(chan, &device->channels, device_node) {
Dan Williams6f49a572009-01-06 11:38:14 -0700685 WARN_ONCE(chan->client_count,
686 "%s called while %d clients hold a reference\n",
687 __func__, chan->client_count);
Tony Jones891f78e2007-09-25 02:03:03 +0200688 device_unregister(&chan->dev);
Dan Williamsd379b012007-07-09 11:56:42 -0700689 dma_chan_release(chan);
Chris Leechc13c8262006-05-23 17:18:44 -0700690 }
Chris Leechc13c8262006-05-23 17:18:44 -0700691
692 kref_put(&device->refcount, dma_async_device_cleanup);
693 wait_for_completion(&device->done);
694}
David Brownell765e3d82007-03-16 13:38:05 -0800695EXPORT_SYMBOL(dma_async_device_unregister);
Chris Leechc13c8262006-05-23 17:18:44 -0700696
Dan Williams7405f742007-01-02 11:10:43 -0700697/**
698 * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
699 * @chan: DMA channel to offload copy to
700 * @dest: destination address (virtual)
701 * @src: source address (virtual)
702 * @len: length
703 *
704 * Both @dest and @src must be mappable to a bus address according to the
705 * DMA mapping API rules for streaming mappings.
706 * Both @dest and @src must stay memory resident (kernel memory or locked
707 * user space pages).
708 */
709dma_cookie_t
710dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
711 void *src, size_t len)
712{
713 struct dma_device *dev = chan->device;
714 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700715 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700716 dma_cookie_t cookie;
717 int cpu;
718
Dan Williams00367312008-02-02 19:49:57 -0700719 dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
720 dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700721 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
722 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700723
724 if (!tx) {
725 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
726 dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700727 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700728 }
Dan Williams7405f742007-01-02 11:10:43 -0700729
Dan Williams7405f742007-01-02 11:10:43 -0700730 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700731 cookie = tx->tx_submit(tx);
732
733 cpu = get_cpu();
734 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
735 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
736 put_cpu();
737
738 return cookie;
739}
740EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
741
742/**
743 * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
744 * @chan: DMA channel to offload copy to
745 * @page: destination page
746 * @offset: offset in page to copy to
747 * @kdata: source address (virtual)
748 * @len: length
749 *
750 * Both @page/@offset and @kdata must be mappable to a bus address according
751 * to the DMA mapping API rules for streaming mappings.
752 * Both @page/@offset and @kdata must stay memory resident (kernel memory or
753 * locked user space pages)
754 */
755dma_cookie_t
756dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
757 unsigned int offset, void *kdata, size_t len)
758{
759 struct dma_device *dev = chan->device;
760 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700761 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700762 dma_cookie_t cookie;
763 int cpu;
764
Dan Williams00367312008-02-02 19:49:57 -0700765 dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
766 dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700767 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
768 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700769
770 if (!tx) {
771 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
772 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700773 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700774 }
Dan Williams7405f742007-01-02 11:10:43 -0700775
Dan Williams7405f742007-01-02 11:10:43 -0700776 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700777 cookie = tx->tx_submit(tx);
778
779 cpu = get_cpu();
780 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
781 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
782 put_cpu();
783
784 return cookie;
785}
786EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
787
788/**
789 * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
790 * @chan: DMA channel to offload copy to
791 * @dest_pg: destination page
792 * @dest_off: offset in page to copy to
793 * @src_pg: source page
794 * @src_off: offset in page to copy from
795 * @len: length
796 *
797 * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
798 * address according to the DMA mapping API rules for streaming mappings.
799 * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
800 * (kernel memory or locked user space pages).
801 */
802dma_cookie_t
803dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
804 unsigned int dest_off, struct page *src_pg, unsigned int src_off,
805 size_t len)
806{
807 struct dma_device *dev = chan->device;
808 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700809 dma_addr_t dma_dest, dma_src;
Dan Williams7405f742007-01-02 11:10:43 -0700810 dma_cookie_t cookie;
811 int cpu;
812
Dan Williams00367312008-02-02 19:49:57 -0700813 dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
814 dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
815 DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -0700816 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
817 DMA_CTRL_ACK);
Dan Williams00367312008-02-02 19:49:57 -0700818
819 if (!tx) {
820 dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
821 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
Dan Williams7405f742007-01-02 11:10:43 -0700822 return -ENOMEM;
Dan Williams00367312008-02-02 19:49:57 -0700823 }
Dan Williams7405f742007-01-02 11:10:43 -0700824
Dan Williams7405f742007-01-02 11:10:43 -0700825 tx->callback = NULL;
Dan Williams7405f742007-01-02 11:10:43 -0700826 cookie = tx->tx_submit(tx);
827
828 cpu = get_cpu();
829 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
830 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
831 put_cpu();
832
833 return cookie;
834}
835EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
836
837void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
838 struct dma_chan *chan)
839{
840 tx->chan = chan;
841 spin_lock_init(&tx->lock);
Dan Williams7405f742007-01-02 11:10:43 -0700842}
843EXPORT_SYMBOL(dma_async_tx_descriptor_init);
844
Dan Williams07f22112009-01-05 17:14:31 -0700845/* dma_wait_for_async_tx - spin wait for a transaction to complete
846 * @tx: in-flight transaction to wait on
847 *
848 * This routine assumes that tx was obtained from a call to async_memcpy,
849 * async_xor, async_memset, etc which ensures that tx is "in-flight" (prepped
850 * and submitted). Walking the parent chain is only meant to cover for DMA
851 * drivers that do not implement the DMA_INTERRUPT capability and may race with
852 * the driver's descriptor cleanup routine.
853 */
854enum dma_status
855dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
856{
857 enum dma_status status;
858 struct dma_async_tx_descriptor *iter;
859 struct dma_async_tx_descriptor *parent;
860
861 if (!tx)
862 return DMA_SUCCESS;
863
864 WARN_ONCE(tx->parent, "%s: speculatively walking dependency chain for"
865 " %s\n", __func__, dev_name(&tx->chan->dev));
866
867 /* poll through the dependency chain, return when tx is complete */
868 do {
869 iter = tx;
870
871 /* find the root of the unsubmitted dependency chain */
872 do {
873 parent = iter->parent;
874 if (!parent)
875 break;
876 else
877 iter = parent;
878 } while (parent);
879
880 /* there is a small window for ->parent == NULL and
881 * ->cookie == -EBUSY
882 */
883 while (iter->cookie == -EBUSY)
884 cpu_relax();
885
886 status = dma_sync_wait(iter->chan, iter->cookie);
887 } while (status == DMA_IN_PROGRESS || (iter != tx));
888
889 return status;
890}
891EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
892
893/* dma_run_dependencies - helper routine for dma drivers to process
894 * (start) dependent operations on their target channel
895 * @tx: transaction with dependencies
896 */
897void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
898{
899 struct dma_async_tx_descriptor *dep = tx->next;
900 struct dma_async_tx_descriptor *dep_next;
901 struct dma_chan *chan;
902
903 if (!dep)
904 return;
905
906 chan = dep->chan;
907
908 /* keep submitting up until a channel switch is detected
909 * in that case we will be called again as a result of
910 * processing the interrupt from async_tx_channel_switch
911 */
912 for (; dep; dep = dep_next) {
913 spin_lock_bh(&dep->lock);
914 dep->parent = NULL;
915 dep_next = dep->next;
916 if (dep_next && dep_next->chan == chan)
917 dep->next = NULL; /* ->next will be submitted */
918 else
919 dep_next = NULL; /* submit current dep and terminate */
920 spin_unlock_bh(&dep->lock);
921
922 dep->tx_submit(dep);
923 }
924
925 chan->device->device_issue_pending(chan);
926}
927EXPORT_SYMBOL_GPL(dma_run_dependencies);
928
Chris Leechc13c8262006-05-23 17:18:44 -0700929static int __init dma_bus_init(void)
930{
931 mutex_init(&dma_list_mutex);
932 return class_register(&dma_devclass);
933}
Chris Leechc13c8262006-05-23 17:18:44 -0700934subsys_initcall(dma_bus_init);
935
Dan Williamsbec08512009-01-06 11:38:14 -0700936