/*1* core routines for the asynchronous memory transfer/transform api2*3* Copyright © 2006, Intel Corporation.4*5* Dan Williams <[email protected]>6*7* with architecture considerations by:8* Neil Brown <[email protected]>9* Jeff Garzik <[email protected]>10*11* This program is free software; you can redistribute it and/or modify it12* under the terms and conditions of the GNU General Public License,13* version 2, as published by the Free Software Foundation.14*15* This program is distributed in the hope it will be useful, but WITHOUT16* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or17* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for18* more details.19*20* You should have received a copy of the GNU General Public License along with21* this program; if not, write to the Free Software Foundation, Inc.,22* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.23*24*/25#include <linux/rculist.h>26#include <linux/kernel.h>27#include <linux/async_tx.h>2829#ifdef CONFIG_DMA_ENGINE30static int __init async_tx_init(void)31{32async_dmaengine_get();3334printk(KERN_INFO "async_tx: api initialized (async)\n");3536return 0;37}3839static void __exit async_tx_exit(void)40{41async_dmaengine_put();42}4344module_init(async_tx_init);45module_exit(async_tx_exit);4647/**48* __async_tx_find_channel - find a channel to carry out the operation or let49* the transaction execute synchronously50* @submit: transaction dependency and submission modifiers51* @tx_type: transaction type52*/53struct dma_chan *54__async_tx_find_channel(struct async_submit_ctl *submit,55enum dma_transaction_type tx_type)56{57struct dma_async_tx_descriptor *depend_tx = submit->depend_tx;5859/* see if we can keep the chain on one channel */60if (depend_tx &&61dma_has_cap(tx_type, depend_tx->chan->device->cap_mask))62return depend_tx->chan;63return async_dma_find_channel(tx_type);64}65EXPORT_SYMBOL_GPL(__async_tx_find_channel);66#endif676869/**70* async_tx_channel_switch - queue an interrupt descriptor with a dependency71* pre-attached.72* @depend_tx: the operation that must finish before the new operation runs73* @tx: the new operation74*/75static void76async_tx_channel_switch(struct dma_async_tx_descriptor *depend_tx,77struct dma_async_tx_descriptor *tx)78{79struct dma_chan *chan = depend_tx->chan;80struct dma_device *device = chan->device;81struct dma_async_tx_descriptor *intr_tx = (void *) ~0;8283/* first check to see if we can still append to depend_tx */84txd_lock(depend_tx);85if (txd_parent(depend_tx) && depend_tx->chan == tx->chan) {86txd_chain(depend_tx, tx);87intr_tx = NULL;88}89txd_unlock(depend_tx);9091/* attached dependency, flush the parent channel */92if (!intr_tx) {93device->device_issue_pending(chan);94return;95}9697/* see if we can schedule an interrupt98* otherwise poll for completion99*/100if (dma_has_cap(DMA_INTERRUPT, device->cap_mask))101intr_tx = device->device_prep_dma_interrupt(chan, 0);102else103intr_tx = NULL;104105if (intr_tx) {106intr_tx->callback = NULL;107intr_tx->callback_param = NULL;108/* safe to chain outside the lock since we know we are109* not submitted yet110*/111txd_chain(intr_tx, tx);112113/* check if we need to append */114txd_lock(depend_tx);115if (txd_parent(depend_tx)) {116txd_chain(depend_tx, intr_tx);117async_tx_ack(intr_tx);118intr_tx = NULL;119}120txd_unlock(depend_tx);121122if (intr_tx) {123txd_clear_parent(intr_tx);124intr_tx->tx_submit(intr_tx);125async_tx_ack(intr_tx);126}127device->device_issue_pending(chan);128} else {129if (dma_wait_for_async_tx(depend_tx) == DMA_ERROR)130panic("%s: DMA_ERROR waiting for depend_tx\n",131__func__);132tx->tx_submit(tx);133}134}135136137/**138* submit_disposition - flags for routing an incoming operation139* @ASYNC_TX_SUBMITTED: we were able to append the new operation under the lock140* @ASYNC_TX_CHANNEL_SWITCH: when the lock is dropped schedule a channel switch141* @ASYNC_TX_DIRECT_SUBMIT: when the lock is dropped submit directly142*143* while holding depend_tx->lock we must avoid submitting new operations144* to prevent a circular locking dependency with drivers that already145* hold a channel lock when calling async_tx_run_dependencies.146*/147enum submit_disposition {148ASYNC_TX_SUBMITTED,149ASYNC_TX_CHANNEL_SWITCH,150ASYNC_TX_DIRECT_SUBMIT,151};152153void154async_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx,155struct async_submit_ctl *submit)156{157struct dma_async_tx_descriptor *depend_tx = submit->depend_tx;158159tx->callback = submit->cb_fn;160tx->callback_param = submit->cb_param;161162if (depend_tx) {163enum submit_disposition s;164165/* sanity check the dependency chain:166* 1/ if ack is already set then we cannot be sure167* we are referring to the correct operation168* 2/ dependencies are 1:1 i.e. two transactions can169* not depend on the same parent170*/171BUG_ON(async_tx_test_ack(depend_tx) || txd_next(depend_tx) ||172txd_parent(tx));173174/* the lock prevents async_tx_run_dependencies from missing175* the setting of ->next when ->parent != NULL176*/177txd_lock(depend_tx);178if (txd_parent(depend_tx)) {179/* we have a parent so we can not submit directly180* if we are staying on the same channel: append181* else: channel switch182*/183if (depend_tx->chan == chan) {184txd_chain(depend_tx, tx);185s = ASYNC_TX_SUBMITTED;186} else187s = ASYNC_TX_CHANNEL_SWITCH;188} else {189/* we do not have a parent so we may be able to submit190* directly if we are staying on the same channel191*/192if (depend_tx->chan == chan)193s = ASYNC_TX_DIRECT_SUBMIT;194else195s = ASYNC_TX_CHANNEL_SWITCH;196}197txd_unlock(depend_tx);198199switch (s) {200case ASYNC_TX_SUBMITTED:201break;202case ASYNC_TX_CHANNEL_SWITCH:203async_tx_channel_switch(depend_tx, tx);204break;205case ASYNC_TX_DIRECT_SUBMIT:206txd_clear_parent(tx);207tx->tx_submit(tx);208break;209}210} else {211txd_clear_parent(tx);212tx->tx_submit(tx);213}214215if (submit->flags & ASYNC_TX_ACK)216async_tx_ack(tx);217218if (depend_tx)219async_tx_ack(depend_tx);220}221EXPORT_SYMBOL_GPL(async_tx_submit);222223/**224* async_trigger_callback - schedules the callback function to be run225* @submit: submission and completion parameters226*227* honored flags: ASYNC_TX_ACK228*229* The callback is run after any dependent operations have completed.230*/231struct dma_async_tx_descriptor *232async_trigger_callback(struct async_submit_ctl *submit)233{234struct dma_chan *chan;235struct dma_device *device;236struct dma_async_tx_descriptor *tx;237struct dma_async_tx_descriptor *depend_tx = submit->depend_tx;238239if (depend_tx) {240chan = depend_tx->chan;241device = chan->device;242243/* see if we can schedule an interrupt244* otherwise poll for completion245*/246if (device && !dma_has_cap(DMA_INTERRUPT, device->cap_mask))247device = NULL;248249tx = device ? device->device_prep_dma_interrupt(chan, 0) : NULL;250} else251tx = NULL;252253if (tx) {254pr_debug("%s: (async)\n", __func__);255256async_tx_submit(chan, tx, submit);257} else {258pr_debug("%s: (sync)\n", __func__);259260/* wait for any prerequisite operations */261async_tx_quiesce(&submit->depend_tx);262263async_tx_sync_epilog(submit);264}265266return tx;267}268EXPORT_SYMBOL_GPL(async_trigger_callback);269270/**271* async_tx_quiesce - ensure tx is complete and freeable upon return272* @tx - transaction to quiesce273*/274void async_tx_quiesce(struct dma_async_tx_descriptor **tx)275{276if (*tx) {277/* if ack is already set then we cannot be sure278* we are referring to the correct operation279*/280BUG_ON(async_tx_test_ack(*tx));281if (dma_wait_for_async_tx(*tx) == DMA_ERROR)282panic("DMA_ERROR waiting for transaction\n");283async_tx_ack(*tx);284*tx = NULL;285}286}287EXPORT_SYMBOL_GPL(async_tx_quiesce);288289MODULE_AUTHOR("Intel Corporation");290MODULE_DESCRIPTION("Asynchronous Bulk Memory Transactions API");291MODULE_LICENSE("GPL");292293294