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_chan_satisfies_mask(chan, mask) \
138 __dma_chan_satisfies_mask((chan), &(mask))
140 __dma_chan_satisfies_mask(struct dma_chan *chan, dma_cap_mask_t *want)
144 bitmap_and(has.bits, want->bits, chan->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;
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 /* Does the client require a specific DMA controller? */
236 if (client->slave && client->slave->dma_dev
237 && client->slave->dma_dev != device->dev)
240 list_for_each_entry(chan, &device->channels, device_node) {
241 if (!dma_chan_satisfies_mask(chan, client->cap_mask))
243 if (!chan->client_count)
245 ack = client->event_callback(client, chan,
246 DMA_RESOURCE_AVAILABLE);
248 /* we are done once this client rejects
249 * an available resource
257 enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
259 enum dma_status status;
260 unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
262 dma_async_issue_pending(chan);
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");
269 } while (status == DMA_IN_PROGRESS);
273 EXPORT_SYMBOL(dma_sync_wait);
276 * dma_chan_cleanup - release a DMA channel's resources
277 * @kref: kernel reference structure that contains the DMA channel device
279 void dma_chan_cleanup(struct kref *kref)
281 struct dma_chan *chan = container_of(kref, struct dma_chan, refcount);
282 kref_put(&chan->device->refcount, dma_async_device_cleanup);
284 EXPORT_SYMBOL(dma_chan_cleanup);
286 static void dma_chan_free_rcu(struct rcu_head *rcu)
288 struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu);
290 kref_put(&chan->refcount, dma_chan_cleanup);
293 static void dma_chan_release(struct dma_chan *chan)
295 call_rcu(&chan->rcu, dma_chan_free_rcu);
299 * dma_cap_mask_all - enable iteration over all operation types
301 static dma_cap_mask_t dma_cap_mask_all;
304 * dma_chan_tbl_ent - tracks channel allocations per core/operation
305 * @chan - associated channel for this entry
307 struct dma_chan_tbl_ent {
308 struct dma_chan *chan;
312 * channel_table - percpu lookup table for memory-to-memory offload providers
314 static struct dma_chan_tbl_ent *channel_table[DMA_TX_TYPE_END];
316 static int __init dma_channel_table_init(void)
318 enum dma_transaction_type cap;
321 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
323 /* 'interrupt' and 'slave' are channel capabilities, but are not
324 * associated with an operation so they do not need an entry in the
327 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
328 clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
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]) {
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]);
347 subsys_initcall(dma_channel_table_init);
350 * dma_find_channel - find a channel to carry out the operation
351 * @tx_type: transaction type
353 struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
355 struct dma_chan *chan;
358 WARN_ONCE(dmaengine_ref_count == 0,
359 "client called %s without a reference", __func__);
362 chan = per_cpu_ptr(channel_table[tx_type], cpu)->chan;
367 EXPORT_SYMBOL(dma_find_channel);
370 * dma_issue_pending_all - flush all pending operations across all channels
372 void dma_issue_pending_all(void)
374 struct dma_device *device;
375 struct dma_chan *chan;
377 WARN_ONCE(dmaengine_ref_count == 0,
378 "client called %s without a reference", __func__);
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);
387 EXPORT_SYMBOL(dma_issue_pending_all);
390 * nth_chan - returns the nth channel of the given capability
391 * @cap: capability to match
392 * @n: nth channel desired
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.
398 static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
400 struct dma_device *device;
401 struct dma_chan *chan;
402 struct dma_chan *ret = NULL;
403 struct dma_chan *min = NULL;
405 list_for_each_entry(device, &dma_device_list, global_node) {
406 if (!dma_has_cap(cap, device->cap_mask))
408 list_for_each_entry(chan, &device->channels, device_node) {
409 if (!chan->client_count)
413 else if (chan->table_count < min->table_count)
435 * dma_channel_rebalance - redistribute the available channels
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
442 static void dma_channel_rebalance(void)
444 struct dma_chan *chan;
445 struct dma_device *device;
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;
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;
459 /* don't populate the channel_table if no clients are available */
460 if (!dmaengine_ref_count)
463 /* redistribute available channels */
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++);
470 chan = nth_chan(cap, -1);
472 per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
477 * dma_chans_notify_available - broadcast available channels to the clients
479 static void dma_clients_notify_available(void)
481 struct dma_client *client;
483 mutex_lock(&dma_list_mutex);
485 list_for_each_entry(client, &dma_client_list, global_node)
486 dma_client_chan_alloc(client);
488 mutex_unlock(&dma_list_mutex);
492 * dma_async_client_register - register a &dma_client
493 * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask'
495 void dma_async_client_register(struct dma_client *client)
497 struct dma_device *device, *_d;
498 struct dma_chan *chan;
501 /* validate client data */
502 BUG_ON(dma_has_cap(DMA_SLAVE, client->cap_mask) &&
505 mutex_lock(&dma_list_mutex);
506 dmaengine_ref_count++;
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 */
514 list_del_rcu(&device->global_node);
517 pr_err("dmaengine: failed to get %s: (%d)\n",
518 dev_name(&chan->dev), err);
521 /* 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
525 if (dmaengine_ref_count == 1)
526 dma_channel_rebalance();
527 list_add_tail(&client->global_node, &dma_client_list);
528 mutex_unlock(&dma_list_mutex);
530 EXPORT_SYMBOL(dma_async_client_register);
533 * dma_async_client_unregister - unregister a client and free the &dma_client
534 * @client: &dma_client to free
536 * Force frees any allocated DMA channels, frees the &dma_client memory
538 void dma_async_client_unregister(struct dma_client *client)
540 struct dma_device *device;
541 struct dma_chan *chan;
546 mutex_lock(&dma_list_mutex);
547 dmaengine_ref_count--;
548 BUG_ON(dmaengine_ref_count < 0);
549 /* drop channel references */
550 list_for_each_entry(device, &dma_device_list, global_node)
551 list_for_each_entry(chan, &device->channels, device_node)
554 list_del(&client->global_node);
555 mutex_unlock(&dma_list_mutex);
557 EXPORT_SYMBOL(dma_async_client_unregister);
560 * dma_async_client_chan_request - send all available channels to the
561 * client that satisfy the capability mask
562 * @client - requester
564 void dma_async_client_chan_request(struct dma_client *client)
566 mutex_lock(&dma_list_mutex);
567 dma_client_chan_alloc(client);
568 mutex_unlock(&dma_list_mutex);
570 EXPORT_SYMBOL(dma_async_client_chan_request);
573 * dma_async_device_register - registers DMA devices found
574 * @device: &dma_device
576 int dma_async_device_register(struct dma_device *device)
580 struct dma_chan* chan;
585 /* 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);
594 BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
595 !device->device_prep_dma_interrupt);
596 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);
601 BUG_ON(!device->device_alloc_chan_resources);
602 BUG_ON(!device->device_free_chan_resources);
603 BUG_ON(!device->device_is_tx_complete);
604 BUG_ON(!device->device_issue_pending);
605 BUG_ON(!device->dev);
607 init_completion(&device->done);
608 kref_init(&device->refcount);
610 mutex_lock(&dma_list_mutex);
611 device->dev_id = id++;
612 mutex_unlock(&dma_list_mutex);
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)
620 chan->chan_id = chancnt++;
621 chan->dev.class = &dma_devclass;
622 chan->dev.parent = device->dev;
623 dev_set_name(&chan->dev, "dma%dchan%d",
624 device->dev_id, chan->chan_id);
626 rc = device_register(&chan->dev);
629 free_percpu(chan->local);
634 /* One for the channel, one of the class device */
635 kref_get(&device->refcount);
636 kref_get(&device->refcount);
637 kref_init(&chan->refcount);
638 chan->client_count = 0;
640 INIT_RCU_HEAD(&chan->rcu);
643 mutex_lock(&dma_list_mutex);
644 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
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
655 mutex_unlock(&dma_list_mutex);
659 list_add_tail_rcu(&device->global_node, &dma_device_list);
660 dma_channel_rebalance();
661 mutex_unlock(&dma_list_mutex);
663 dma_clients_notify_available();
668 list_for_each_entry(chan, &device->channels, device_node) {
669 if (chan->local == NULL)
671 kref_put(&device->refcount, dma_async_device_cleanup);
672 device_unregister(&chan->dev);
674 free_percpu(chan->local);
678 EXPORT_SYMBOL(dma_async_device_register);
681 * dma_async_device_cleanup - function called when all references are released
682 * @kref: kernel reference object
684 static void dma_async_device_cleanup(struct kref *kref)
686 struct dma_device *device;
688 device = container_of(kref, struct dma_device, refcount);
689 complete(&device->done);
693 * dma_async_device_unregister - unregister a DMA device
694 * @device: &dma_device
696 void dma_async_device_unregister(struct dma_device *device)
698 struct dma_chan *chan;
700 mutex_lock(&dma_list_mutex);
701 list_del_rcu(&device->global_node);
702 dma_channel_rebalance();
703 mutex_unlock(&dma_list_mutex);
705 list_for_each_entry(chan, &device->channels, device_node) {
706 WARN_ONCE(chan->client_count,
707 "%s called while %d clients hold a reference\n",
708 __func__, chan->client_count);
709 device_unregister(&chan->dev);
710 dma_chan_release(chan);
713 kref_put(&device->refcount, dma_async_device_cleanup);
714 wait_for_completion(&device->done);
716 EXPORT_SYMBOL(dma_async_device_unregister);
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)
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
731 dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
732 void *src, size_t len)
734 struct dma_device *dev = chan->device;
735 struct dma_async_tx_descriptor *tx;
736 dma_addr_t dma_dest, dma_src;
740 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);
742 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
746 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
747 dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
752 cookie = tx->tx_submit(tx);
755 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
756 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
761 EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
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)
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)
777 dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
778 unsigned int offset, void *kdata, size_t len)
780 struct dma_device *dev = chan->device;
781 struct dma_async_tx_descriptor *tx;
782 dma_addr_t dma_dest, dma_src;
786 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);
788 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
792 dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
793 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
798 cookie = tx->tx_submit(tx);
801 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
802 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
807 EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
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
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).
824 dma_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,
828 struct dma_device *dev = chan->device;
829 struct dma_async_tx_descriptor *tx;
830 dma_addr_t dma_dest, dma_src;
834 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,
837 tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len,
841 dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
842 dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
847 cookie = tx->tx_submit(tx);
850 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
851 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
856 EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
858 void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
859 struct dma_chan *chan)
862 spin_lock_init(&tx->lock);
864 EXPORT_SYMBOL(dma_async_tx_descriptor_init);
866 /* dma_wait_for_async_tx - spin wait for a transaction to complete
867 * @tx: in-flight transaction to wait on
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.
876 dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
878 enum dma_status status;
879 struct dma_async_tx_descriptor *iter;
880 struct dma_async_tx_descriptor *parent;
885 WARN_ONCE(tx->parent, "%s: speculatively walking dependency chain for"
886 " %s\n", __func__, dev_name(&tx->chan->dev));
888 /* poll through the dependency chain, return when tx is complete */
892 /* find the root of the unsubmitted dependency chain */
894 parent = iter->parent;
901 /* there is a small window for ->parent == NULL and
904 while (iter->cookie == -EBUSY)
907 status = dma_sync_wait(iter->chan, iter->cookie);
908 } while (status == DMA_IN_PROGRESS || (iter != tx));
912 EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
914 /* dma_run_dependencies - helper routine for dma drivers to process
915 * (start) dependent operations on their target channel
916 * @tx: transaction with dependencies
918 void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
920 struct dma_async_tx_descriptor *dep = tx->next;
921 struct dma_async_tx_descriptor *dep_next;
922 struct dma_chan *chan;
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
933 for (; dep; dep = dep_next) {
934 spin_lock_bh(&dep->lock);
936 dep_next = dep->next;
937 if (dep_next && dep_next->chan == chan)
938 dep->next = NULL; /* ->next will be submitted */
940 dep_next = NULL; /* submit current dep and terminate */
941 spin_unlock_bh(&dep->lock);
946 chan->device->device_issue_pending(chan);
948 EXPORT_SYMBOL_GPL(dma_run_dependencies);
950 static int __init dma_bus_init(void)
952 mutex_init(&dma_list_mutex);
953 return class_register(&dma_devclass);
955 subsys_initcall(dma_bus_init);