2 * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
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)
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
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.
18 * The full GNU General Public License is included in this distribution in the
19 * file called COPYING.
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
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,
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.
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.
40 * Each client has a channels list, it's only modified under the client->lock
41 * and in an RCU callback, so it's safe to read under rcu_read_lock().
43 * Each device has a kref, which is initialized to 1 when the device is
44 * registered. A kref_put is done for each class_device registered. When the
45 * class_device is released, the coresponding kref_put is done in the release
46 * method. Every time one of the device's channels is allocated to a client,
47 * a kref_get occurs. When the channel is freed, the coresponding kref_put
48 * happens. The device's release function does a completion, so
49 * unregister_device does a remove event, class_device_unregister, a kref_put
50 * for the first reference, then waits on the completion for all other
51 * references to finish.
53 * Each channel has an open-coded implementation of Rusty Russell's "bigref,"
54 * with a kref and a per_cpu local_t. A single reference is set when on an
55 * ADDED event, and removed with a REMOVE event. Net DMA client takes an
56 * extra reference per outstanding transaction. The relase function does a
57 * kref_put on the device. -ChrisL
60 #include <linux/init.h>
61 #include <linux/module.h>
63 #include <linux/device.h>
64 #include <linux/dmaengine.h>
65 #include <linux/hardirq.h>
66 #include <linux/spinlock.h>
67 #include <linux/percpu.h>
68 #include <linux/rcupdate.h>
69 #include <linux/mutex.h>
70 #include <linux/jiffies.h>
72 static DEFINE_MUTEX(dma_list_mutex);
73 static LIST_HEAD(dma_device_list);
74 static LIST_HEAD(dma_client_list);
76 /* --- sysfs implementation --- */
78 static ssize_t show_memcpy_count(struct class_device *cd, char *buf)
80 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
81 unsigned long count = 0;
84 for_each_possible_cpu(i)
85 count += per_cpu_ptr(chan->local, i)->memcpy_count;
87 return sprintf(buf, "%lu\n", count);
90 static ssize_t show_bytes_transferred(struct class_device *cd, char *buf)
92 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
93 unsigned long count = 0;
96 for_each_possible_cpu(i)
97 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
99 return sprintf(buf, "%lu\n", count);
102 static ssize_t show_in_use(struct class_device *cd, char *buf)
104 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
106 return sprintf(buf, "%d\n", (chan->client ? 1 : 0));
109 static struct class_device_attribute dma_class_attrs[] = {
110 __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
111 __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
112 __ATTR(in_use, S_IRUGO, show_in_use, NULL),
116 static void dma_async_device_cleanup(struct kref *kref);
118 static void dma_class_dev_release(struct class_device *cd)
120 struct dma_chan *chan = container_of(cd, struct dma_chan, class_dev);
121 kref_put(&chan->device->refcount, dma_async_device_cleanup);
124 static struct class dma_devclass = {
126 .class_dev_attrs = dma_class_attrs,
127 .release = dma_class_dev_release,
130 /* --- client and device registration --- */
133 * dma_client_chan_alloc - try to allocate a channel to a client
134 * @client: &dma_client
136 * Called with dma_list_mutex held.
138 static struct dma_chan *dma_client_chan_alloc(struct dma_client *client)
140 struct dma_device *device;
141 struct dma_chan *chan;
143 int desc; /* allocated descriptor count */
145 /* Find a channel, any DMA engine will do */
146 list_for_each_entry(device, &dma_device_list, global_node) {
147 list_for_each_entry(chan, &device->channels, device_node) {
151 desc = chan->device->device_alloc_chan_resources(chan);
153 kref_get(&device->refcount);
154 kref_init(&chan->refcount);
156 INIT_RCU_HEAD(&chan->rcu);
157 chan->client = client;
158 spin_lock_irqsave(&client->lock, flags);
159 list_add_tail_rcu(&chan->client_node,
161 spin_unlock_irqrestore(&client->lock, flags);
170 enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
172 enum dma_status status;
173 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
175 dma_async_issue_pending(chan);
177 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
178 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
179 printk(KERN_ERR "dma_sync_wait_timeout!\n");
182 } while (status == DMA_IN_PROGRESS);
186 EXPORT_SYMBOL(dma_sync_wait);
189 * dma_chan_cleanup - release a DMA channel's resources
190 * @kref: kernel reference structure that contains the DMA channel device
192 void dma_chan_cleanup(struct kref *kref)
194 struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
195 chan->device->device_free_chan_resources(chan);
197 kref_put(&chan->device->refcount, dma_async_device_cleanup);
199 EXPORT_SYMBOL(dma_chan_cleanup);
201 static void dma_chan_free_rcu(struct rcu_head *rcu)
203 struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
204 int bias = 0x7FFFFFFF;
206 for_each_possible_cpu(i)
207 bias -= local_read(&per_cpu_ptr(chan->local, i)->refcount);
208 atomic_sub(bias, &chan->refcount.refcount);
209 kref_put(&chan->refcount, dma_chan_cleanup);
212 static void dma_client_chan_free(struct dma_chan *chan)
214 atomic_add(0x7FFFFFFF, &chan->refcount.refcount);
216 call_rcu(&chan->rcu, dma_chan_free_rcu);
220 * dma_chans_rebalance - reallocate channels to clients
222 * When the number of DMA channel in the system changes,
223 * channels need to be rebalanced among clients.
225 static void dma_chans_rebalance(void)
227 struct dma_client *client;
228 struct dma_chan *chan;
231 mutex_lock(&dma_list_mutex);
233 list_for_each_entry(client, &dma_client_list, global_node) {
234 while (client->chans_desired > client->chan_count) {
235 chan = dma_client_chan_alloc(client);
238 client->chan_count++;
239 client->event_callback(client,
243 while (client->chans_desired < client->chan_count) {
244 spin_lock_irqsave(&client->lock, flags);
245 chan = list_entry(client->channels.next,
248 list_del_rcu(&chan->client_node);
249 spin_unlock_irqrestore(&client->lock, flags);
250 client->chan_count--;
251 client->event_callback(client,
253 DMA_RESOURCE_REMOVED);
254 dma_client_chan_free(chan);
258 mutex_unlock(&dma_list_mutex);
262 * dma_async_client_register - allocate and register a &dma_client
263 * @event_callback: callback for notification of channel addition/removal
265 struct dma_client *dma_async_client_register(dma_event_callback event_callback)
267 struct dma_client *client;
269 client = kzalloc(sizeof(*client), GFP_KERNEL);
273 INIT_LIST_HEAD(&client->channels);
274 spin_lock_init(&client->lock);
275 client->chans_desired = 0;
276 client->chan_count = 0;
277 client->event_callback = event_callback;
279 mutex_lock(&dma_list_mutex);
280 list_add_tail(&client->global_node, &dma_client_list);
281 mutex_unlock(&dma_list_mutex);
285 EXPORT_SYMBOL(dma_async_client_register);
288 * dma_async_client_unregister - unregister a client and free the &dma_client
289 * @client: &dma_client to free
291 * Force frees any allocated DMA channels, frees the &dma_client memory
293 void dma_async_client_unregister(struct dma_client *client)
295 struct dma_chan *chan;
301 list_for_each_entry_rcu(chan, &client->channels, client_node)
302 dma_client_chan_free(chan);
305 mutex_lock(&dma_list_mutex);
306 list_del(&client->global_node);
307 mutex_unlock(&dma_list_mutex);
310 dma_chans_rebalance();
312 EXPORT_SYMBOL(dma_async_client_unregister);
315 * dma_async_client_chan_request - request DMA channels
316 * @client: &dma_client
317 * @number: count of DMA channels requested
319 * Clients call dma_async_client_chan_request() to specify how many
320 * DMA channels they need, 0 to free all currently allocated.
321 * The resulting allocations/frees are indicated to the client via the
324 void dma_async_client_chan_request(struct dma_client *client,
327 client->chans_desired = number;
328 dma_chans_rebalance();
330 EXPORT_SYMBOL(dma_async_client_chan_request);
333 * dma_async_device_register - registers DMA devices found
334 * @device: &dma_device
336 int dma_async_device_register(struct dma_device *device)
340 struct dma_chan* chan;
345 /* validate device routines */
346 BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
347 !device->device_prep_dma_memcpy);
348 BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
349 !device->device_prep_dma_xor);
350 BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
351 !device->device_prep_dma_zero_sum);
352 BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
353 !device->device_prep_dma_memset);
354 BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
355 !device->device_prep_dma_interrupt);
357 BUG_ON(!device->device_alloc_chan_resources);
358 BUG_ON(!device->device_free_chan_resources);
359 BUG_ON(!device->device_dependency_added);
360 BUG_ON(!device->device_is_tx_complete);
361 BUG_ON(!device->device_issue_pending);
362 BUG_ON(!device->dev);
364 init_completion(&device->done);
365 kref_init(&device->refcount);
366 device->dev_id = id++;
368 /* represent channels in sysfs. Probably want devs too */
369 list_for_each_entry(chan, &device->channels, device_node) {
370 chan->local = alloc_percpu(typeof(*chan->local));
371 if (chan->local == NULL)
374 chan->chan_id = chancnt++;
375 chan->class_dev.class = &dma_devclass;
376 chan->class_dev.dev = NULL;
377 snprintf(chan->class_dev.class_id, BUS_ID_SIZE, "dma%dchan%d",
378 device->dev_id, chan->chan_id);
380 rc = class_device_register(&chan->class_dev);
383 free_percpu(chan->local);
388 kref_get(&device->refcount);
391 mutex_lock(&dma_list_mutex);
392 list_add_tail(&device->global_node, &dma_device_list);
393 mutex_unlock(&dma_list_mutex);
395 dma_chans_rebalance();
400 list_for_each_entry(chan, &device->channels, device_node) {
401 if (chan->local == NULL)
403 kref_put(&device->refcount, dma_async_device_cleanup);
404 class_device_unregister(&chan->class_dev);
406 free_percpu(chan->local);
410 EXPORT_SYMBOL(dma_async_device_register);
413 * dma_async_device_cleanup - function called when all references are released
414 * @kref: kernel reference object
416 static void dma_async_device_cleanup(struct kref *kref)
418 struct dma_device *device;
420 device = container_of(kref, struct dma_device, refcount);
421 complete(&device->done);
425 * dma_async_device_unregister - unregisters DMA devices
426 * @device: &dma_device
428 void dma_async_device_unregister(struct dma_device *device)
430 struct dma_chan *chan;
433 mutex_lock(&dma_list_mutex);
434 list_del(&device->global_node);
435 mutex_unlock(&dma_list_mutex);
437 list_for_each_entry(chan, &device->channels, device_node) {
439 spin_lock_irqsave(&chan->client->lock, flags);
440 list_del(&chan->client_node);
441 chan->client->chan_count--;
442 spin_unlock_irqrestore(&chan->client->lock, flags);
443 chan->client->event_callback(chan->client,
445 DMA_RESOURCE_REMOVED);
446 dma_client_chan_free(chan);
448 class_device_unregister(&chan->class_dev);
450 dma_chans_rebalance();
452 kref_put(&device->refcount, dma_async_device_cleanup);
453 wait_for_completion(&device->done);
455 EXPORT_SYMBOL(dma_async_device_unregister);
458 * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
459 * @chan: DMA channel to offload copy to
460 * @dest: destination address (virtual)
461 * @src: source address (virtual)
464 * Both @dest and @src must be mappable to a bus address according to the
465 * DMA mapping API rules for streaming mappings.
466 * Both @dest and @src must stay memory resident (kernel memory or locked
470 dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
471 void *src, size_t len)
473 struct dma_device *dev = chan->device;
474 struct dma_async_tx_descriptor *tx;
479 tx = dev->device_prep_dma_memcpy(chan, len, 0);
485 addr = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
486 tx->tx_set_src(addr, tx, 0);
487 addr = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
488 tx->tx_set_dest(addr, tx, 0);
489 cookie = tx->tx_submit(tx);
492 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
493 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
498 EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
501 * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
502 * @chan: DMA channel to offload copy to
503 * @page: destination page
504 * @offset: offset in page to copy to
505 * @kdata: source address (virtual)
508 * Both @page/@offset and @kdata must be mappable to a bus address according
509 * to the DMA mapping API rules for streaming mappings.
510 * Both @page/@offset and @kdata must stay memory resident (kernel memory or
511 * locked user space pages)
514 dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
515 unsigned int offset, void *kdata, size_t len)
517 struct dma_device *dev = chan->device;
518 struct dma_async_tx_descriptor *tx;
523 tx = dev->device_prep_dma_memcpy(chan, len, 0);
529 addr = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
530 tx->tx_set_src(addr, tx, 0);
531 addr = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
532 tx->tx_set_dest(addr, tx, 0);
533 cookie = tx->tx_submit(tx);
536 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
537 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
542 EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
545 * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
546 * @chan: DMA channel to offload copy to
547 * @dest_pg: destination page
548 * @dest_off: offset in page to copy to
549 * @src_pg: source page
550 * @src_off: offset in page to copy from
553 * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
554 * address according to the DMA mapping API rules for streaming mappings.
555 * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
556 * (kernel memory or locked user space pages).
559 dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
560 unsigned int dest_off, struct page *src_pg, unsigned int src_off,
563 struct dma_device *dev = chan->device;
564 struct dma_async_tx_descriptor *tx;
569 tx = dev->device_prep_dma_memcpy(chan, len, 0);
575 addr = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
576 tx->tx_set_src(addr, tx, 0);
577 addr = dma_map_page(dev->dev, dest_pg, dest_off, len, DMA_FROM_DEVICE);
578 tx->tx_set_dest(addr, tx, 0);
579 cookie = tx->tx_submit(tx);
582 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
583 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
588 EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
590 void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
591 struct dma_chan *chan)
594 spin_lock_init(&tx->lock);
595 INIT_LIST_HEAD(&tx->depend_node);
596 INIT_LIST_HEAD(&tx->depend_list);
598 EXPORT_SYMBOL(dma_async_tx_descriptor_init);
600 static int __init dma_bus_init(void)
602 mutex_init(&dma_list_mutex);
603 return class_register(&dma_devclass);
605 subsys_initcall(dma_bus_init);