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 is responsible for keeping track of the channels it uses. See
41 * the definition of dma_event_callback in dmaengine.h.
43 * Each device has a kref, which is initialized to 1 when the device is
44 * registered. A kref_get is done for each device registered. When the
45 * device is released, the corresponding 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 corresponding kref_put
48 * happens. The device's release function does a completion, so
49 * unregister_device does a remove event, 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 dma_chan_get is called when a client
55 * signals that it wants to use a channel, and dma_chan_put is called when
56 * a channel is removed or a client using it is unregistered. A client can
57 * 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.
62 #include <linux/init.h>
63 #include <linux/module.h>
65 #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>
72 #include <linux/jiffies.h>
73 #include <linux/rculist.h>
75 static DEFINE_MUTEX(dma_list_mutex);
76 static LIST_HEAD(dma_device_list);
77 static LIST_HEAD(dma_client_list);
78 static long dmaengine_ref_count;
80 /* --- sysfs implementation --- */
82 static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf)
84 struct dma_chan *chan = to_dma_chan(dev);
85 unsigned long count = 0;
88 for_each_possible_cpu(i)
89 count += per_cpu_ptr(chan->local, i)->memcpy_count;
91 return sprintf(buf, "%lu\n", count);
94 static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr,
97 struct dma_chan *chan = to_dma_chan(dev);
98 unsigned long count = 0;
101 for_each_possible_cpu(i)
102 count += per_cpu_ptr(chan->local, i)->bytes_transferred;
104 return sprintf(buf, "%lu\n", count);
107 static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
109 struct dma_chan *chan = to_dma_chan(dev);
111 return sprintf(buf, "%d\n", chan->client_count);
114 static struct device_attribute dma_attrs[] = {
115 __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),
121 static void dma_async_device_cleanup(struct kref *kref);
123 static void dma_dev_release(struct device *dev)
125 struct dma_chan *chan = to_dma_chan(dev);
126 kref_put(&chan->device->refcount, dma_async_device_cleanup);
129 static struct class dma_devclass = {
131 .dev_attrs = dma_attrs,
132 .dev_release = dma_dev_release,
135 /* --- client and device registration --- */
137 #define dma_device_satisfies_mask(device, mask) \
138 __dma_device_satisfies_mask((device), &(mask))
140 __dma_device_satisfies_mask(struct dma_device *device, dma_cap_mask_t *want)
144 bitmap_and(has.bits, want->bits, device->cap_mask.bits,
146 return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
149 static struct module *dma_chan_to_owner(struct dma_chan *chan)
151 return chan->device->dev->driver->owner;
155 * balance_ref_count - catch up the channel reference count
156 * @chan - channel to balance ->client_count versus dmaengine_ref_count
158 * balance_ref_count must be called under dma_list_mutex
160 static void balance_ref_count(struct dma_chan *chan)
162 struct module *owner = dma_chan_to_owner(chan);
164 while (chan->client_count < dmaengine_ref_count) {
166 chan->client_count++;
171 * dma_chan_get - try to grab a dma channel's parent driver module
172 * @chan - channel to grab
174 * Must be called under dma_list_mutex
176 static int dma_chan_get(struct dma_chan *chan)
179 struct module *owner = dma_chan_to_owner(chan);
181 if (chan->client_count) {
184 } else if (try_module_get(owner))
188 chan->client_count++;
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);
196 chan->client_count = 0;
198 } else if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
199 balance_ref_count(chan);
206 * dma_chan_put - drop a reference to a dma channel's parent driver module
207 * @chan - channel to release
209 * Must be called under dma_list_mutex
211 static void dma_chan_put(struct dma_chan *chan)
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);
222 * dma_client_chan_alloc - try to allocate channels to a client
223 * @client: &dma_client
225 * Called with dma_list_mutex held.
227 static void dma_client_chan_alloc(struct dma_client *client)
229 struct dma_device *device;
230 struct dma_chan *chan;
231 enum dma_state_client ack;
234 list_for_each_entry(device, &dma_device_list, global_node) {
235 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
237 if (!dma_device_satisfies_mask(device, client->cap_mask))
240 list_for_each_entry(chan, &device->channels, device_node) {
241 if (!chan->client_count)
243 ack = client->event_callback(client, chan,
244 DMA_RESOURCE_AVAILABLE);
246 /* we are done once this client rejects
247 * an available resource
255 enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
257 enum dma_status status;
258 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
260 dma_async_issue_pending(chan);
262 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
263 if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
264 printk(KERN_ERR "dma_sync_wait_timeout!\n");
267 } while (status == DMA_IN_PROGRESS);
271 EXPORT_SYMBOL(dma_sync_wait);
274 * dma_chan_cleanup - release a DMA channel's resources
275 * @kref: kernel reference structure that contains the DMA channel device
277 void dma_chan_cleanup(struct kref *kref)
279 struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
280 kref_put(&chan->device->refcount, dma_async_device_cleanup);
282 EXPORT_SYMBOL(dma_chan_cleanup);
284 static void dma_chan_free_rcu(struct rcu_head *rcu)
286 struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
288 kref_put(&chan->refcount, dma_chan_cleanup);
291 static void dma_chan_release(struct dma_chan *chan)
293 call_rcu(&chan->rcu, dma_chan_free_rcu);
297 * dma_cap_mask_all - enable iteration over all operation types
299 static dma_cap_mask_t dma_cap_mask_all;
302 * dma_chan_tbl_ent - tracks channel allocations per core/operation
303 * @chan - associated channel for this entry
305 struct dma_chan_tbl_ent {
306 struct dma_chan *chan;
310 * channel_table - percpu lookup table for memory-to-memory offload providers
312 static struct dma_chan_tbl_ent *channel_table[DMA_TX_TYPE_END];
314 static int __init dma_channel_table_init(void)
316 enum dma_transaction_type cap;
319 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
321 /* 'interrupt', 'private', and 'slave' are channel capabilities,
322 * but are not associated with an operation so they do not need
323 * an entry in the channel_table
325 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
326 clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
327 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
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]) {
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]);
346 subsys_initcall(dma_channel_table_init);
349 * dma_find_channel - find a channel to carry out the operation
350 * @tx_type: transaction type
352 struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
354 struct dma_chan *chan;
357 WARN_ONCE(dmaengine_ref_count == 0,
358 "client called %s without a reference", __func__);
361 chan = per_cpu_ptr(channel_table[tx_type], cpu)->chan;
366 EXPORT_SYMBOL(dma_find_channel);
369 * dma_issue_pending_all - flush all pending operations across all channels
371 void dma_issue_pending_all(void)
373 struct dma_device *device;
374 struct dma_chan *chan;
376 WARN_ONCE(dmaengine_ref_count == 0,
377 "client called %s without a reference", __func__);
380 list_for_each_entry_rcu(device, &dma_device_list, global_node) {
381 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
383 list_for_each_entry(chan, &device->channels, device_node)
384 if (chan->client_count)
385 device->device_issue_pending(chan);
389 EXPORT_SYMBOL(dma_issue_pending_all);
392 * nth_chan - returns the nth channel of the given capability
393 * @cap: capability to match
394 * @n: nth channel desired
396 * Defaults to returning the channel with the desired capability and the
397 * lowest reference count when 'n' cannot be satisfied. Must be called
398 * under dma_list_mutex.
400 static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
402 struct dma_device *device;
403 struct dma_chan *chan;
404 struct dma_chan *ret = NULL;
405 struct dma_chan *min = NULL;
407 list_for_each_entry(device, &dma_device_list, global_node) {
408 if (!dma_has_cap(cap, device->cap_mask) ||
409 dma_has_cap(DMA_PRIVATE, device->cap_mask))
411 list_for_each_entry(chan, &device->channels, device_node) {
412 if (!chan->client_count)
416 else if (chan->table_count < min->table_count)
438 * dma_channel_rebalance - redistribute the available channels
440 * Optimize for cpu isolation (each cpu gets a dedicated channel for an
441 * operation type) in the SMP case, and operation isolation (avoid
442 * multi-tasking channels) in the non-SMP case. Must be called under
445 static void dma_channel_rebalance(void)
447 struct dma_chan *chan;
448 struct dma_device *device;
453 /* undo the last distribution */
454 for_each_dma_cap_mask(cap, dma_cap_mask_all)
455 for_each_possible_cpu(cpu)
456 per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
458 list_for_each_entry(device, &dma_device_list, global_node) {
459 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
461 list_for_each_entry(chan, &device->channels, device_node)
462 chan->table_count = 0;
465 /* don't populate the channel_table if no clients are available */
466 if (!dmaengine_ref_count)
469 /* redistribute available channels */
471 for_each_dma_cap_mask(cap, dma_cap_mask_all)
472 for_each_online_cpu(cpu) {
473 if (num_possible_cpus() > 1)
474 chan = nth_chan(cap, n++);
476 chan = nth_chan(cap, -1);
478 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
482 static struct dma_chan *private_candidate(dma_cap_mask_t *mask, struct dma_device *dev)
484 struct dma_chan *chan;
485 struct dma_chan *ret = NULL;
487 if (!__dma_device_satisfies_mask(dev, mask)) {
488 pr_debug("%s: wrong capabilities\n", __func__);
491 /* devices with multiple channels need special handling as we need to
492 * ensure that all channels are either private or public.
494 if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
495 list_for_each_entry(chan, &dev->channels, device_node) {
496 /* some channels are already publicly allocated */
497 if (chan->client_count)
501 list_for_each_entry(chan, &dev->channels, device_node) {
502 if (chan->client_count) {
503 pr_debug("%s: %s busy\n",
504 __func__, dev_name(&chan->dev));
515 * dma_request_channel - try to allocate an exclusive channel
516 * @mask: capabilities that the channel must satisfy
517 * @fn: optional callback to disposition available channels
518 * @fn_param: opaque parameter to pass to dma_filter_fn
520 struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param)
522 struct dma_device *device, *_d;
523 struct dma_chan *chan = NULL;
524 enum dma_state_client ack;
528 mutex_lock(&dma_list_mutex);
529 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
530 chan = private_candidate(mask, device);
535 ack = fn(chan, fn_param);
539 if (ack == DMA_ACK) {
540 /* Found a suitable channel, try to grab, prep, and
541 * return it. We first set DMA_PRIVATE to disable
542 * balance_ref_count as this channel will not be
543 * published in the general-purpose allocator
545 dma_cap_set(DMA_PRIVATE, device->cap_mask);
546 err = dma_chan_get(chan);
548 if (err == -ENODEV) {
549 pr_debug("%s: %s module removed\n", __func__,
550 dev_name(&chan->dev));
551 list_del_rcu(&device->global_node);
553 pr_err("dmaengine: failed to get %s: (%d)\n",
554 dev_name(&chan->dev), err);
557 } else if (ack == DMA_DUP) {
558 pr_debug("%s: %s filter said DMA_DUP\n",
559 __func__, dev_name(&chan->dev));
560 } else if (ack == DMA_NAK) {
561 pr_debug("%s: %s filter said DMA_NAK\n",
562 __func__, dev_name(&chan->dev));
565 WARN_ONCE(1, "filter_fn: unknown response?\n");
568 mutex_unlock(&dma_list_mutex);
570 pr_debug("%s: %s (%s)\n", __func__, chan ? "success" : "fail",
571 chan ? dev_name(&chan->dev) : NULL);
575 EXPORT_SYMBOL_GPL(__dma_request_channel);
577 void dma_release_channel(struct dma_chan *chan)
579 mutex_lock(&dma_list_mutex);
580 WARN_ONCE(chan->client_count != 1,
581 "chan reference count %d != 1\n", chan->client_count);
583 mutex_unlock(&dma_list_mutex);
585 EXPORT_SYMBOL_GPL(dma_release_channel);
588 * dma_chans_notify_available - broadcast available channels to the clients
590 static void dma_clients_notify_available(void)
592 struct dma_client *client;
594 mutex_lock(&dma_list_mutex);
596 list_for_each_entry(client, &dma_client_list, global_node)
597 dma_client_chan_alloc(client);
599 mutex_unlock(&dma_list_mutex);
603 * dma_async_client_register - register a &dma_client
604 * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask'
606 void dma_async_client_register(struct dma_client *client)
608 struct dma_device *device, *_d;
609 struct dma_chan *chan;
612 mutex_lock(&dma_list_mutex);
613 dmaengine_ref_count++;
615 /* try to grab channels */
616 list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
617 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
619 list_for_each_entry(chan, &device->channels, device_node) {
620 err = dma_chan_get(chan);
621 if (err == -ENODEV) {
622 /* module removed before we could use it */
623 list_del_rcu(&device->global_node);
626 pr_err("dmaengine: failed to get %s: (%d)\n",
627 dev_name(&chan->dev), err);
631 /* if this is the first reference and there were channels
632 * waiting we need to rebalance to get those channels
633 * incorporated into the channel table
635 if (dmaengine_ref_count == 1)
636 dma_channel_rebalance();
637 list_add_tail(&client->global_node, &dma_client_list);
638 mutex_unlock(&dma_list_mutex);
640 EXPORT_SYMBOL(dma_async_client_register);
643 * dma_async_client_unregister - unregister a client and free the &dma_client
644 * @client: &dma_client to free
646 * Force frees any allocated DMA channels, frees the &dma_client memory
648 void dma_async_client_unregister(struct dma_client *client)
650 struct dma_device *device;
651 struct dma_chan *chan;
656 mutex_lock(&dma_list_mutex);
657 dmaengine_ref_count--;
658 BUG_ON(dmaengine_ref_count < 0);
659 /* drop channel references */
660 list_for_each_entry(device, &dma_device_list, global_node) {
661 if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
663 list_for_each_entry(chan, &device->channels, device_node)
667 list_del(&client->global_node);
668 mutex_unlock(&dma_list_mutex);
670 EXPORT_SYMBOL(dma_async_client_unregister);
673 * dma_async_client_chan_request - send all available channels to the
674 * client that satisfy the capability mask
675 * @client - requester
677 void dma_async_client_chan_request(struct dma_client *client)
679 mutex_lock(&dma_list_mutex);
680 dma_client_chan_alloc(client);
681 mutex_unlock(&dma_list_mutex);
683 EXPORT_SYMBOL(dma_async_client_chan_request);
686 * dma_async_device_register - registers DMA devices found
687 * @device: &dma_device
689 int dma_async_device_register(struct dma_device *device)
693 struct dma_chan* chan;
698 /* validate device routines */
699 BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
700 !device->device_prep_dma_memcpy);
701 BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
702 !device->device_prep_dma_xor);
703 BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) &&
704 !device->device_prep_dma_zero_sum);
705 BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
706 !device->device_prep_dma_memset);
707 BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
708 !device->device_prep_dma_interrupt);
709 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
710 !device->device_prep_slave_sg);
711 BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
712 !device->device_terminate_all);
714 BUG_ON(!device->device_alloc_chan_resources);
715 BUG_ON(!device->device_free_chan_resources);
716 BUG_ON(!device->device_is_tx_complete);
717 BUG_ON(!device->device_issue_pending);
718 BUG_ON(!device->dev);
720 init_completion(&device->done);
721 kref_init(&device->refcount);
723 mutex_lock(&dma_list_mutex);
724 device->dev_id = id++;
725 mutex_unlock(&dma_list_mutex);
727 /* represent channels in sysfs. Probably want devs too */
728 list_for_each_entry(chan, &device->channels, device_node) {
729 chan->local = alloc_percpu(typeof(*chan->local));
730 if (chan->local == NULL)
733 chan->chan_id = chancnt++;
734 chan->dev.class = &dma_devclass;
735 chan->dev.parent = device->dev;
736 dev_set_name(&chan->dev, "dma%dchan%d",
737 device->dev_id, chan->chan_id);
739 rc = device_register(&chan->dev);
742 free_percpu(chan->local);
747 /* One for the channel, one of the class device */
748 kref_get(&device->refcount);
749 kref_get(&device->refcount);
750 kref_init(&chan->refcount);
751 chan->client_count = 0;
753 INIT_RCU_HEAD(&chan->rcu);
755 device->chancnt = chancnt;
757 mutex_lock(&dma_list_mutex);
758 /* take references on public channels */
759 if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
760 list_for_each_entry(chan, &device->channels, device_node) {
761 /* if clients are already waiting for channels we need
762 * to take references on their behalf
764 if (dma_chan_get(chan) == -ENODEV) {
765 /* note we can only get here for the first
766 * channel as the remaining channels are
767 * guaranteed to get a reference
770 mutex_unlock(&dma_list_mutex);
774 list_add_tail_rcu(&device->global_node, &dma_device_list);
775 dma_channel_rebalance();
776 mutex_unlock(&dma_list_mutex);
778 dma_clients_notify_available();
783 list_for_each_entry(chan, &device->channels, device_node) {
784 if (chan->local == NULL)
786 kref_put(&device->refcount, dma_async_device_cleanup);
787 device_unregister(&chan->dev);
789 free_percpu(chan->local);
793 EXPORT_SYMBOL(dma_async_device_register);
796 * dma_async_device_cleanup - function called when all references are released
797 * @kref: kernel reference object
799 static void dma_async_device_cleanup(struct kref *kref)
801 struct dma_device *device;
803 device = container_of(kref, struct dma_device, refcount);
804 complete(&device->done);
808 * dma_async_device_unregister - unregister a DMA device
809 * @device: &dma_device
811 void dma_async_device_unregister(struct dma_device *device)
813 struct dma_chan *chan;
815 mutex_lock(&dma_list_mutex);
816 list_del_rcu(&device->global_node);
817 dma_channel_rebalance();
818 mutex_unlock(&dma_list_mutex);
820 list_for_each_entry(chan, &device->channels, device_node) {
821 WARN_ONCE(chan->client_count,
822 "%s called while %d clients hold a reference\n",
823 __func__, chan->client_count);
824 device_unregister(&chan->dev);
825 dma_chan_release(chan);
828 kref_put(&device->refcount, dma_async_device_cleanup);
829 wait_for_completion(&device->done);
831 EXPORT_SYMBOL(dma_async_device_unregister);
834 * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
835 * @chan: DMA channel to offload copy to
836 * @dest: destination address (virtual)
837 * @src: source address (virtual)
840 * Both @dest and @src must be mappable to a bus address according to the
841 * DMA mapping API rules for streaming mappings.
842 * Both @dest and @src must stay memory resident (kernel memory or locked
846 dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
847 void *src, size_t len)
849 struct dma_device *dev = chan->device;
850 struct dma_async_tx_descriptor *tx;
851 dma_addr_t dma_dest, dma_src;
855 dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
856 dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
857 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
861 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
862 dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
867 cookie = tx->tx_submit(tx);
870 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
871 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
876 EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
879 * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
880 * @chan: DMA channel to offload copy to
881 * @page: destination page
882 * @offset: offset in page to copy to
883 * @kdata: source address (virtual)
886 * Both @page/@offset and @kdata must be mappable to a bus address according
887 * to the DMA mapping API rules for streaming mappings.
888 * Both @page/@offset and @kdata must stay memory resident (kernel memory or
889 * locked user space pages)
892 dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
893 unsigned int offset, void *kdata, size_t len)
895 struct dma_device *dev = chan->device;
896 struct dma_async_tx_descriptor *tx;
897 dma_addr_t dma_dest, dma_src;
901 dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
902 dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
903 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
907 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
908 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
913 cookie = tx->tx_submit(tx);
916 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
917 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
922 EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
925 * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
926 * @chan: DMA channel to offload copy to
927 * @dest_pg: destination page
928 * @dest_off: offset in page to copy to
929 * @src_pg: source page
930 * @src_off: offset in page to copy from
933 * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
934 * address according to the DMA mapping API rules for streaming mappings.
935 * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
936 * (kernel memory or locked user space pages).
939 dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
940 unsigned int dest_off, struct page *src_pg, unsigned int src_off,
943 struct dma_device *dev = chan->device;
944 struct dma_async_tx_descriptor *tx;
945 dma_addr_t dma_dest, dma_src;
949 dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
950 dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
952 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
956 dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
957 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
962 cookie = tx->tx_submit(tx);
965 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
966 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
971 EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
973 void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
974 struct dma_chan *chan)
977 spin_lock_init(&tx->lock);
979 EXPORT_SYMBOL(dma_async_tx_descriptor_init);
981 /* dma_wait_for_async_tx - spin wait for a transaction to complete
982 * @tx: in-flight transaction to wait on
984 * This routine assumes that tx was obtained from a call to async_memcpy,
985 * async_xor, async_memset, etc which ensures that tx is "in-flight" (prepped
986 * and submitted). Walking the parent chain is only meant to cover for DMA
987 * drivers that do not implement the DMA_INTERRUPT capability and may race with
988 * the driver's descriptor cleanup routine.
991 dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
993 enum dma_status status;
994 struct dma_async_tx_descriptor *iter;
995 struct dma_async_tx_descriptor *parent;
1000 WARN_ONCE(tx->parent, "%s: speculatively walking dependency chain for"
1001 " %s\n", __func__, dev_name(&tx->chan->dev));
1003 /* poll through the dependency chain, return when tx is complete */
1007 /* find the root of the unsubmitted dependency chain */
1009 parent = iter->parent;
1016 /* there is a small window for ->parent == NULL and
1017 * ->cookie == -EBUSY
1019 while (iter->cookie == -EBUSY)
1022 status = dma_sync_wait(iter->chan, iter->cookie);
1023 } while (status == DMA_IN_PROGRESS || (iter != tx));
1027 EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
1029 /* dma_run_dependencies - helper routine for dma drivers to process
1030 * (start) dependent operations on their target channel
1031 * @tx: transaction with dependencies
1033 void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
1035 struct dma_async_tx_descriptor *dep = tx->next;
1036 struct dma_async_tx_descriptor *dep_next;
1037 struct dma_chan *chan;
1044 /* keep submitting up until a channel switch is detected
1045 * in that case we will be called again as a result of
1046 * processing the interrupt from async_tx_channel_switch
1048 for (; dep; dep = dep_next) {
1049 spin_lock_bh(&dep->lock);
1051 dep_next = dep->next;
1052 if (dep_next && dep_next->chan == chan)
1053 dep->next = NULL; /* ->next will be submitted */
1055 dep_next = NULL; /* submit current dep and terminate */
1056 spin_unlock_bh(&dep->lock);
1058 dep->tx_submit(dep);
1061 chan->device->device_issue_pending(chan);
1063 EXPORT_SYMBOL_GPL(dma_run_dependencies);
1065 static int __init dma_bus_init(void)
1067 mutex_init(&dma_list_mutex);
1068 return class_register(&dma_devclass);
1070 subsys_initcall(dma_bus_init);