2 * core routines for the asynchronous memory transfer/transform api
4 * Copyright © 2006, Intel Corporation.
6 * Dan Williams <dan.j.williams@intel.com>
8 * with architecture considerations by:
9 * Neil Brown <neilb@suse.de>
10 * Jeff Garzik <jeff@garzik.org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms and conditions of the GNU General Public License,
14 * version 2, as published by the Free Software Foundation.
16 * This program is distributed in the hope it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * You should have received a copy of the GNU General Public License along with
22 * this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <linux/rculist.h>
27 #include <linux/kernel.h>
28 #include <linux/async_tx.h>
30 #ifdef CONFIG_DMA_ENGINE
31 static enum dma_state_client
32 dma_channel_add_remove(struct dma_client *client,
33 struct dma_chan *chan, enum dma_state state);
35 static struct dma_client async_tx_dma = {
36 .event_callback = dma_channel_add_remove,
37 /* .cap_mask == 0 defaults to all channels */
41 * dma_cap_mask_all - enable iteration over all operation types
43 static dma_cap_mask_t dma_cap_mask_all;
46 * chan_ref_percpu - tracks channel allocations per core/opertion
48 struct chan_ref_percpu {
49 struct dma_chan_ref *ref;
52 static int channel_table_initialized;
53 static struct chan_ref_percpu *channel_table[DMA_TX_TYPE_END];
56 * async_tx_lock - protect modification of async_tx_master_list and serialize
57 * rebalance operations
59 static spinlock_t async_tx_lock;
61 static LIST_HEAD(async_tx_master_list);
63 /* async_tx_issue_pending_all - start all transactions on all channels */
64 void async_tx_issue_pending_all(void)
66 struct dma_chan_ref *ref;
69 list_for_each_entry_rcu(ref, &async_tx_master_list, node)
70 ref->chan->device->device_issue_pending(ref->chan);
73 EXPORT_SYMBOL_GPL(async_tx_issue_pending_all);
75 /* dma_wait_for_async_tx - spin wait for a transcation to complete
76 * @tx: transaction to wait on
79 dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
81 enum dma_status status;
82 struct dma_async_tx_descriptor *iter;
83 struct dma_async_tx_descriptor *parent;
88 /* poll through the dependency chain, return when tx is complete */
92 /* find the root of the unsubmitted dependency chain */
94 parent = iter->parent;
101 /* there is a small window for ->parent == NULL and
104 while (iter->cookie == -EBUSY)
107 status = dma_sync_wait(iter->chan, iter->cookie);
108 } while (status == DMA_IN_PROGRESS || (iter != tx));
112 EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
114 /* async_tx_run_dependencies - helper routine for dma drivers to process
115 * (start) dependent operations on their target channel
116 * @tx: transaction with dependencies
118 void async_tx_run_dependencies(struct dma_async_tx_descriptor *tx)
120 struct dma_async_tx_descriptor *dep = tx->next;
121 struct dma_async_tx_descriptor *dep_next;
122 struct dma_chan *chan;
129 /* keep submitting up until a channel switch is detected
130 * in that case we will be called again as a result of
131 * processing the interrupt from async_tx_channel_switch
133 for (; dep; dep = dep_next) {
134 spin_lock_bh(&dep->lock);
136 dep_next = dep->next;
137 if (dep_next && dep_next->chan == chan)
138 dep->next = NULL; /* ->next will be submitted */
140 dep_next = NULL; /* submit current dep and terminate */
141 spin_unlock_bh(&dep->lock);
146 chan->device->device_issue_pending(chan);
148 EXPORT_SYMBOL_GPL(async_tx_run_dependencies);
151 free_dma_chan_ref(struct rcu_head *rcu)
153 struct dma_chan_ref *ref;
154 ref = container_of(rcu, struct dma_chan_ref, rcu);
159 init_dma_chan_ref(struct dma_chan_ref *ref, struct dma_chan *chan)
161 INIT_LIST_HEAD(&ref->node);
162 INIT_RCU_HEAD(&ref->rcu);
164 atomic_set(&ref->count, 0);
168 * get_chan_ref_by_cap - returns the nth channel of the given capability
169 * defaults to returning the channel with the desired capability and the
170 * lowest reference count if the index can not be satisfied
171 * @cap: capability to match
172 * @index: nth channel desired, passing -1 has the effect of forcing the
173 * default return value
175 static struct dma_chan_ref *
176 get_chan_ref_by_cap(enum dma_transaction_type cap, int index)
178 struct dma_chan_ref *ret_ref = NULL, *min_ref = NULL, *ref;
181 list_for_each_entry_rcu(ref, &async_tx_master_list, node)
182 if (dma_has_cap(cap, ref->chan->device->cap_mask)) {
185 else if (atomic_read(&ref->count) <
186 atomic_read(&min_ref->count))
200 atomic_inc(&ret_ref->count);
206 * async_tx_rebalance - redistribute the available channels, optimize
207 * for cpu isolation in the SMP case, and opertaion isolation in the
210 static void async_tx_rebalance(void)
212 int cpu, cap, cpu_idx = 0;
215 if (!channel_table_initialized)
218 spin_lock_irqsave(&async_tx_lock, flags);
220 /* undo the last distribution */
221 for_each_dma_cap_mask(cap, dma_cap_mask_all)
222 for_each_possible_cpu(cpu) {
223 struct dma_chan_ref *ref =
224 per_cpu_ptr(channel_table[cap], cpu)->ref;
226 atomic_set(&ref->count, 0);
227 per_cpu_ptr(channel_table[cap], cpu)->ref =
232 for_each_dma_cap_mask(cap, dma_cap_mask_all)
233 for_each_online_cpu(cpu) {
234 struct dma_chan_ref *new;
236 new = get_chan_ref_by_cap(cap, cpu_idx++);
238 new = get_chan_ref_by_cap(cap, -1);
240 per_cpu_ptr(channel_table[cap], cpu)->ref = new;
243 spin_unlock_irqrestore(&async_tx_lock, flags);
246 static enum dma_state_client
247 dma_channel_add_remove(struct dma_client *client,
248 struct dma_chan *chan, enum dma_state state)
250 unsigned long found, flags;
251 struct dma_chan_ref *master_ref, *ref;
252 enum dma_state_client ack = DMA_DUP; /* default: take no action */
255 case DMA_RESOURCE_AVAILABLE:
258 list_for_each_entry_rcu(ref, &async_tx_master_list, node)
259 if (ref->chan == chan) {
265 pr_debug("async_tx: dma resource available [%s]\n",
266 found ? "old" : "new");
273 /* add the channel to the generic management list */
274 master_ref = kmalloc(sizeof(*master_ref), GFP_KERNEL);
276 /* keep a reference until async_tx is unloaded */
278 init_dma_chan_ref(master_ref, chan);
279 spin_lock_irqsave(&async_tx_lock, flags);
280 list_add_tail_rcu(&master_ref->node,
281 &async_tx_master_list);
282 spin_unlock_irqrestore(&async_tx_lock,
285 printk(KERN_WARNING "async_tx: unable to create"
286 " new master entry in response to"
287 " a DMA_RESOURCE_ADDED event"
292 async_tx_rebalance();
294 case DMA_RESOURCE_REMOVED:
296 spin_lock_irqsave(&async_tx_lock, flags);
297 list_for_each_entry(ref, &async_tx_master_list, node)
298 if (ref->chan == chan) {
299 /* permit backing devices to go away */
300 dma_chan_put(ref->chan);
301 list_del_rcu(&ref->node);
302 call_rcu(&ref->rcu, free_dma_chan_ref);
306 spin_unlock_irqrestore(&async_tx_lock, flags);
308 pr_debug("async_tx: dma resource removed [%s]\n",
309 found ? "ours" : "not ours");
316 async_tx_rebalance();
318 case DMA_RESOURCE_SUSPEND:
319 case DMA_RESOURCE_RESUME:
320 printk(KERN_WARNING "async_tx: does not support dma channel"
321 " suspend/resume\n");
333 enum dma_transaction_type cap;
335 spin_lock_init(&async_tx_lock);
336 bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
338 /* an interrupt will never be an explicit operation type.
339 * clearing this bit prevents allocation to a slot in 'channel_table'
341 clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
343 for_each_dma_cap_mask(cap, dma_cap_mask_all) {
344 channel_table[cap] = alloc_percpu(struct chan_ref_percpu);
345 if (!channel_table[cap])
349 channel_table_initialized = 1;
350 dma_async_client_register(&async_tx_dma);
351 dma_async_client_chan_request(&async_tx_dma);
353 printk(KERN_INFO "async_tx: api initialized (async)\n");
357 printk(KERN_ERR "async_tx: initialization failure\n");
360 free_percpu(channel_table[cap]);
365 static void __exit async_tx_exit(void)
367 enum dma_transaction_type cap;
369 channel_table_initialized = 0;
371 for_each_dma_cap_mask(cap, dma_cap_mask_all)
372 if (channel_table[cap])
373 free_percpu(channel_table[cap]);
375 dma_async_client_unregister(&async_tx_dma);
379 * __async_tx_find_channel - find a channel to carry out the operation or let
380 * the transaction execute synchronously
381 * @depend_tx: transaction dependency
382 * @tx_type: transaction type
385 __async_tx_find_channel(struct dma_async_tx_descriptor *depend_tx,
386 enum dma_transaction_type tx_type)
388 /* see if we can keep the chain on one channel */
390 dma_has_cap(tx_type, depend_tx->chan->device->cap_mask))
391 return depend_tx->chan;
392 else if (likely(channel_table_initialized)) {
393 struct dma_chan_ref *ref;
395 ref = per_cpu_ptr(channel_table[tx_type], cpu)->ref;
397 return ref ? ref->chan : NULL;
401 EXPORT_SYMBOL_GPL(__async_tx_find_channel);
403 static int __init async_tx_init(void)
405 printk(KERN_INFO "async_tx: api initialized (sync-only)\n");
409 static void __exit async_tx_exit(void)
417 * async_tx_channel_switch - queue an interrupt descriptor with a dependency
419 * @depend_tx: the operation that must finish before the new operation runs
420 * @tx: the new operation
423 async_tx_channel_switch(struct dma_async_tx_descriptor *depend_tx,
424 struct dma_async_tx_descriptor *tx)
426 struct dma_chan *chan;
427 struct dma_device *device;
428 struct dma_async_tx_descriptor *intr_tx = (void *) ~0;
430 /* first check to see if we can still append to depend_tx */
431 spin_lock_bh(&depend_tx->lock);
432 if (depend_tx->parent && depend_tx->chan == tx->chan) {
433 tx->parent = depend_tx;
434 depend_tx->next = tx;
437 spin_unlock_bh(&depend_tx->lock);
442 chan = depend_tx->chan;
443 device = chan->device;
445 /* see if we can schedule an interrupt
446 * otherwise poll for completion
448 if (dma_has_cap(DMA_INTERRUPT, device->cap_mask))
449 intr_tx = device->device_prep_dma_interrupt(chan, 0);
454 intr_tx->callback = NULL;
455 intr_tx->callback_param = NULL;
456 tx->parent = intr_tx;
457 /* safe to set ->next outside the lock since we know we are
462 /* check if we need to append */
463 spin_lock_bh(&depend_tx->lock);
464 if (depend_tx->parent) {
465 intr_tx->parent = depend_tx;
466 depend_tx->next = intr_tx;
467 async_tx_ack(intr_tx);
470 spin_unlock_bh(&depend_tx->lock);
473 intr_tx->parent = NULL;
474 intr_tx->tx_submit(intr_tx);
475 async_tx_ack(intr_tx);
478 if (dma_wait_for_async_tx(depend_tx) == DMA_ERROR)
479 panic("%s: DMA_ERROR waiting for depend_tx\n",
487 * submit_disposition - while holding depend_tx->lock we must avoid submitting
488 * new operations to prevent a circular locking dependency with
489 * drivers that already hold a channel lock when calling
490 * async_tx_run_dependencies.
491 * @ASYNC_TX_SUBMITTED: we were able to append the new operation under the lock
492 * @ASYNC_TX_CHANNEL_SWITCH: when the lock is dropped schedule a channel switch
493 * @ASYNC_TX_DIRECT_SUBMIT: when the lock is dropped submit directly
495 enum submit_disposition {
497 ASYNC_TX_CHANNEL_SWITCH,
498 ASYNC_TX_DIRECT_SUBMIT,
502 async_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx,
503 enum async_tx_flags flags, struct dma_async_tx_descriptor *depend_tx,
504 dma_async_tx_callback cb_fn, void *cb_param)
506 tx->callback = cb_fn;
507 tx->callback_param = cb_param;
510 enum submit_disposition s;
512 /* sanity check the dependency chain:
513 * 1/ if ack is already set then we cannot be sure
514 * we are referring to the correct operation
515 * 2/ dependencies are 1:1 i.e. two transactions can
516 * not depend on the same parent
518 BUG_ON(async_tx_test_ack(depend_tx) || depend_tx->next ||
521 /* the lock prevents async_tx_run_dependencies from missing
522 * the setting of ->next when ->parent != NULL
524 spin_lock_bh(&depend_tx->lock);
525 if (depend_tx->parent) {
526 /* we have a parent so we can not submit directly
527 * if we are staying on the same channel: append
528 * else: channel switch
530 if (depend_tx->chan == chan) {
531 tx->parent = depend_tx;
532 depend_tx->next = tx;
533 s = ASYNC_TX_SUBMITTED;
535 s = ASYNC_TX_CHANNEL_SWITCH;
537 /* we do not have a parent so we may be able to submit
538 * directly if we are staying on the same channel
540 if (depend_tx->chan == chan)
541 s = ASYNC_TX_DIRECT_SUBMIT;
543 s = ASYNC_TX_CHANNEL_SWITCH;
545 spin_unlock_bh(&depend_tx->lock);
548 case ASYNC_TX_SUBMITTED:
550 case ASYNC_TX_CHANNEL_SWITCH:
551 async_tx_channel_switch(depend_tx, tx);
553 case ASYNC_TX_DIRECT_SUBMIT:
563 if (flags & ASYNC_TX_ACK)
566 if (depend_tx && (flags & ASYNC_TX_DEP_ACK))
567 async_tx_ack(depend_tx);
569 EXPORT_SYMBOL_GPL(async_tx_submit);
572 * async_trigger_callback - schedules the callback function to be run after
573 * any dependent operations have been completed.
574 * @flags: ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
575 * @depend_tx: 'callback' requires the completion of this transaction
576 * @cb_fn: function to call after depend_tx completes
577 * @cb_param: parameter to pass to the callback routine
579 struct dma_async_tx_descriptor *
580 async_trigger_callback(enum async_tx_flags flags,
581 struct dma_async_tx_descriptor *depend_tx,
582 dma_async_tx_callback cb_fn, void *cb_param)
584 struct dma_chan *chan;
585 struct dma_device *device;
586 struct dma_async_tx_descriptor *tx;
589 chan = depend_tx->chan;
590 device = chan->device;
592 /* see if we can schedule an interrupt
593 * otherwise poll for completion
595 if (device && !dma_has_cap(DMA_INTERRUPT, device->cap_mask))
598 tx = device ? device->device_prep_dma_interrupt(chan, 0) : NULL;
603 pr_debug("%s: (async)\n", __func__);
605 async_tx_submit(chan, tx, flags, depend_tx, cb_fn, cb_param);
607 pr_debug("%s: (sync)\n", __func__);
609 /* wait for any prerequisite operations */
610 async_tx_quiesce(&depend_tx);
612 async_tx_sync_epilog(cb_fn, cb_param);
617 EXPORT_SYMBOL_GPL(async_trigger_callback);
620 * async_tx_quiesce - ensure tx is complete and freeable upon return
621 * @tx - transaction to quiesce
623 void async_tx_quiesce(struct dma_async_tx_descriptor **tx)
626 /* if ack is already set then we cannot be sure
627 * we are referring to the correct operation
629 BUG_ON(async_tx_test_ack(*tx));
630 if (dma_wait_for_async_tx(*tx) == DMA_ERROR)
631 panic("DMA_ERROR waiting for transaction\n");
636 EXPORT_SYMBOL_GPL(async_tx_quiesce);
638 module_init(async_tx_init);
639 module_exit(async_tx_exit);
641 MODULE_AUTHOR("Intel Corporation");
642 MODULE_DESCRIPTION("Asynchronous Bulk Memory Transactions API");
643 MODULE_LICENSE("GPL");