18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * core routines for the asynchronous memory transfer/transform api 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright © 2006, Intel Corporation. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Dan Williams <dan.j.williams@intel.com> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * with architecture considerations by: 108c2ecf20Sopenharmony_ci * Neil Brown <neilb@suse.de> 118c2ecf20Sopenharmony_ci * Jeff Garzik <jeff@garzik.org> 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci#include <linux/rculist.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/kernel.h> 168c2ecf20Sopenharmony_ci#include <linux/async_tx.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#ifdef CONFIG_DMA_ENGINE 198c2ecf20Sopenharmony_cistatic int __init async_tx_init(void) 208c2ecf20Sopenharmony_ci{ 218c2ecf20Sopenharmony_ci async_dmaengine_get(); 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci printk(KERN_INFO "async_tx: api initialized (async)\n"); 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci return 0; 268c2ecf20Sopenharmony_ci} 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic void __exit async_tx_exit(void) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci async_dmaengine_put(); 318c2ecf20Sopenharmony_ci} 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cimodule_init(async_tx_init); 348c2ecf20Sopenharmony_cimodule_exit(async_tx_exit); 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/** 378c2ecf20Sopenharmony_ci * __async_tx_find_channel - find a channel to carry out the operation or let 388c2ecf20Sopenharmony_ci * the transaction execute synchronously 398c2ecf20Sopenharmony_ci * @submit: transaction dependency and submission modifiers 408c2ecf20Sopenharmony_ci * @tx_type: transaction type 418c2ecf20Sopenharmony_ci */ 428c2ecf20Sopenharmony_cistruct dma_chan * 438c2ecf20Sopenharmony_ci__async_tx_find_channel(struct async_submit_ctl *submit, 448c2ecf20Sopenharmony_ci enum dma_transaction_type tx_type) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci struct dma_async_tx_descriptor *depend_tx = submit->depend_tx; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci /* see if we can keep the chain on one channel */ 498c2ecf20Sopenharmony_ci if (depend_tx && 508c2ecf20Sopenharmony_ci dma_has_cap(tx_type, depend_tx->chan->device->cap_mask)) 518c2ecf20Sopenharmony_ci return depend_tx->chan; 528c2ecf20Sopenharmony_ci return async_dma_find_channel(tx_type); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__async_tx_find_channel); 558c2ecf20Sopenharmony_ci#endif 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/** 598c2ecf20Sopenharmony_ci * async_tx_channel_switch - queue an interrupt descriptor with a dependency 608c2ecf20Sopenharmony_ci * pre-attached. 618c2ecf20Sopenharmony_ci * @depend_tx: the operation that must finish before the new operation runs 628c2ecf20Sopenharmony_ci * @tx: the new operation 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_cistatic void 658c2ecf20Sopenharmony_ciasync_tx_channel_switch(struct dma_async_tx_descriptor *depend_tx, 668c2ecf20Sopenharmony_ci struct dma_async_tx_descriptor *tx) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci struct dma_chan *chan = depend_tx->chan; 698c2ecf20Sopenharmony_ci struct dma_device *device = chan->device; 708c2ecf20Sopenharmony_ci struct dma_async_tx_descriptor *intr_tx = (void *) ~0; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci /* first check to see if we can still append to depend_tx */ 738c2ecf20Sopenharmony_ci txd_lock(depend_tx); 748c2ecf20Sopenharmony_ci if (txd_parent(depend_tx) && depend_tx->chan == tx->chan) { 758c2ecf20Sopenharmony_ci txd_chain(depend_tx, tx); 768c2ecf20Sopenharmony_ci intr_tx = NULL; 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci txd_unlock(depend_tx); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci /* attached dependency, flush the parent channel */ 818c2ecf20Sopenharmony_ci if (!intr_tx) { 828c2ecf20Sopenharmony_ci device->device_issue_pending(chan); 838c2ecf20Sopenharmony_ci return; 848c2ecf20Sopenharmony_ci } 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci /* see if we can schedule an interrupt 878c2ecf20Sopenharmony_ci * otherwise poll for completion 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ci if (dma_has_cap(DMA_INTERRUPT, device->cap_mask)) 908c2ecf20Sopenharmony_ci intr_tx = device->device_prep_dma_interrupt(chan, 0); 918c2ecf20Sopenharmony_ci else 928c2ecf20Sopenharmony_ci intr_tx = NULL; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci if (intr_tx) { 958c2ecf20Sopenharmony_ci intr_tx->callback = NULL; 968c2ecf20Sopenharmony_ci intr_tx->callback_param = NULL; 978c2ecf20Sopenharmony_ci /* safe to chain outside the lock since we know we are 988c2ecf20Sopenharmony_ci * not submitted yet 998c2ecf20Sopenharmony_ci */ 1008c2ecf20Sopenharmony_ci txd_chain(intr_tx, tx); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* check if we need to append */ 1038c2ecf20Sopenharmony_ci txd_lock(depend_tx); 1048c2ecf20Sopenharmony_ci if (txd_parent(depend_tx)) { 1058c2ecf20Sopenharmony_ci txd_chain(depend_tx, intr_tx); 1068c2ecf20Sopenharmony_ci async_tx_ack(intr_tx); 1078c2ecf20Sopenharmony_ci intr_tx = NULL; 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci txd_unlock(depend_tx); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci if (intr_tx) { 1128c2ecf20Sopenharmony_ci txd_clear_parent(intr_tx); 1138c2ecf20Sopenharmony_ci intr_tx->tx_submit(intr_tx); 1148c2ecf20Sopenharmony_ci async_tx_ack(intr_tx); 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci device->device_issue_pending(chan); 1178c2ecf20Sopenharmony_ci } else { 1188c2ecf20Sopenharmony_ci if (dma_wait_for_async_tx(depend_tx) != DMA_COMPLETE) 1198c2ecf20Sopenharmony_ci panic("%s: DMA error waiting for depend_tx\n", 1208c2ecf20Sopenharmony_ci __func__); 1218c2ecf20Sopenharmony_ci tx->tx_submit(tx); 1228c2ecf20Sopenharmony_ci } 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/** 1278c2ecf20Sopenharmony_ci * submit_disposition - flags for routing an incoming operation 1288c2ecf20Sopenharmony_ci * @ASYNC_TX_SUBMITTED: we were able to append the new operation under the lock 1298c2ecf20Sopenharmony_ci * @ASYNC_TX_CHANNEL_SWITCH: when the lock is dropped schedule a channel switch 1308c2ecf20Sopenharmony_ci * @ASYNC_TX_DIRECT_SUBMIT: when the lock is dropped submit directly 1318c2ecf20Sopenharmony_ci * 1328c2ecf20Sopenharmony_ci * while holding depend_tx->lock we must avoid submitting new operations 1338c2ecf20Sopenharmony_ci * to prevent a circular locking dependency with drivers that already 1348c2ecf20Sopenharmony_ci * hold a channel lock when calling async_tx_run_dependencies. 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_cienum submit_disposition { 1378c2ecf20Sopenharmony_ci ASYNC_TX_SUBMITTED, 1388c2ecf20Sopenharmony_ci ASYNC_TX_CHANNEL_SWITCH, 1398c2ecf20Sopenharmony_ci ASYNC_TX_DIRECT_SUBMIT, 1408c2ecf20Sopenharmony_ci}; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_civoid 1438c2ecf20Sopenharmony_ciasync_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx, 1448c2ecf20Sopenharmony_ci struct async_submit_ctl *submit) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci struct dma_async_tx_descriptor *depend_tx = submit->depend_tx; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci tx->callback = submit->cb_fn; 1498c2ecf20Sopenharmony_ci tx->callback_param = submit->cb_param; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if (depend_tx) { 1528c2ecf20Sopenharmony_ci enum submit_disposition s; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci /* sanity check the dependency chain: 1558c2ecf20Sopenharmony_ci * 1/ if ack is already set then we cannot be sure 1568c2ecf20Sopenharmony_ci * we are referring to the correct operation 1578c2ecf20Sopenharmony_ci * 2/ dependencies are 1:1 i.e. two transactions can 1588c2ecf20Sopenharmony_ci * not depend on the same parent 1598c2ecf20Sopenharmony_ci */ 1608c2ecf20Sopenharmony_ci BUG_ON(async_tx_test_ack(depend_tx) || txd_next(depend_tx) || 1618c2ecf20Sopenharmony_ci txd_parent(tx)); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* the lock prevents async_tx_run_dependencies from missing 1648c2ecf20Sopenharmony_ci * the setting of ->next when ->parent != NULL 1658c2ecf20Sopenharmony_ci */ 1668c2ecf20Sopenharmony_ci txd_lock(depend_tx); 1678c2ecf20Sopenharmony_ci if (txd_parent(depend_tx)) { 1688c2ecf20Sopenharmony_ci /* we have a parent so we can not submit directly 1698c2ecf20Sopenharmony_ci * if we are staying on the same channel: append 1708c2ecf20Sopenharmony_ci * else: channel switch 1718c2ecf20Sopenharmony_ci */ 1728c2ecf20Sopenharmony_ci if (depend_tx->chan == chan) { 1738c2ecf20Sopenharmony_ci txd_chain(depend_tx, tx); 1748c2ecf20Sopenharmony_ci s = ASYNC_TX_SUBMITTED; 1758c2ecf20Sopenharmony_ci } else 1768c2ecf20Sopenharmony_ci s = ASYNC_TX_CHANNEL_SWITCH; 1778c2ecf20Sopenharmony_ci } else { 1788c2ecf20Sopenharmony_ci /* we do not have a parent so we may be able to submit 1798c2ecf20Sopenharmony_ci * directly if we are staying on the same channel 1808c2ecf20Sopenharmony_ci */ 1818c2ecf20Sopenharmony_ci if (depend_tx->chan == chan) 1828c2ecf20Sopenharmony_ci s = ASYNC_TX_DIRECT_SUBMIT; 1838c2ecf20Sopenharmony_ci else 1848c2ecf20Sopenharmony_ci s = ASYNC_TX_CHANNEL_SWITCH; 1858c2ecf20Sopenharmony_ci } 1868c2ecf20Sopenharmony_ci txd_unlock(depend_tx); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci switch (s) { 1898c2ecf20Sopenharmony_ci case ASYNC_TX_SUBMITTED: 1908c2ecf20Sopenharmony_ci break; 1918c2ecf20Sopenharmony_ci case ASYNC_TX_CHANNEL_SWITCH: 1928c2ecf20Sopenharmony_ci async_tx_channel_switch(depend_tx, tx); 1938c2ecf20Sopenharmony_ci break; 1948c2ecf20Sopenharmony_ci case ASYNC_TX_DIRECT_SUBMIT: 1958c2ecf20Sopenharmony_ci txd_clear_parent(tx); 1968c2ecf20Sopenharmony_ci tx->tx_submit(tx); 1978c2ecf20Sopenharmony_ci break; 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci } else { 2008c2ecf20Sopenharmony_ci txd_clear_parent(tx); 2018c2ecf20Sopenharmony_ci tx->tx_submit(tx); 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci if (submit->flags & ASYNC_TX_ACK) 2058c2ecf20Sopenharmony_ci async_tx_ack(tx); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci if (depend_tx) 2088c2ecf20Sopenharmony_ci async_tx_ack(depend_tx); 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(async_tx_submit); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci/** 2138c2ecf20Sopenharmony_ci * async_trigger_callback - schedules the callback function to be run 2148c2ecf20Sopenharmony_ci * @submit: submission and completion parameters 2158c2ecf20Sopenharmony_ci * 2168c2ecf20Sopenharmony_ci * honored flags: ASYNC_TX_ACK 2178c2ecf20Sopenharmony_ci * 2188c2ecf20Sopenharmony_ci * The callback is run after any dependent operations have completed. 2198c2ecf20Sopenharmony_ci */ 2208c2ecf20Sopenharmony_cistruct dma_async_tx_descriptor * 2218c2ecf20Sopenharmony_ciasync_trigger_callback(struct async_submit_ctl *submit) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci struct dma_chan *chan; 2248c2ecf20Sopenharmony_ci struct dma_device *device; 2258c2ecf20Sopenharmony_ci struct dma_async_tx_descriptor *tx; 2268c2ecf20Sopenharmony_ci struct dma_async_tx_descriptor *depend_tx = submit->depend_tx; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci if (depend_tx) { 2298c2ecf20Sopenharmony_ci chan = depend_tx->chan; 2308c2ecf20Sopenharmony_ci device = chan->device; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci /* see if we can schedule an interrupt 2338c2ecf20Sopenharmony_ci * otherwise poll for completion 2348c2ecf20Sopenharmony_ci */ 2358c2ecf20Sopenharmony_ci if (device && !dma_has_cap(DMA_INTERRUPT, device->cap_mask)) 2368c2ecf20Sopenharmony_ci device = NULL; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci tx = device ? device->device_prep_dma_interrupt(chan, 0) : NULL; 2398c2ecf20Sopenharmony_ci } else 2408c2ecf20Sopenharmony_ci tx = NULL; 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci if (tx) { 2438c2ecf20Sopenharmony_ci pr_debug("%s: (async)\n", __func__); 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci async_tx_submit(chan, tx, submit); 2468c2ecf20Sopenharmony_ci } else { 2478c2ecf20Sopenharmony_ci pr_debug("%s: (sync)\n", __func__); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci /* wait for any prerequisite operations */ 2508c2ecf20Sopenharmony_ci async_tx_quiesce(&submit->depend_tx); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci async_tx_sync_epilog(submit); 2538c2ecf20Sopenharmony_ci } 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci return tx; 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(async_trigger_callback); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci/** 2608c2ecf20Sopenharmony_ci * async_tx_quiesce - ensure tx is complete and freeable upon return 2618c2ecf20Sopenharmony_ci * @tx - transaction to quiesce 2628c2ecf20Sopenharmony_ci */ 2638c2ecf20Sopenharmony_civoid async_tx_quiesce(struct dma_async_tx_descriptor **tx) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci if (*tx) { 2668c2ecf20Sopenharmony_ci /* if ack is already set then we cannot be sure 2678c2ecf20Sopenharmony_ci * we are referring to the correct operation 2688c2ecf20Sopenharmony_ci */ 2698c2ecf20Sopenharmony_ci BUG_ON(async_tx_test_ack(*tx)); 2708c2ecf20Sopenharmony_ci if (dma_wait_for_async_tx(*tx) != DMA_COMPLETE) 2718c2ecf20Sopenharmony_ci panic("%s: DMA error waiting for transaction\n", 2728c2ecf20Sopenharmony_ci __func__); 2738c2ecf20Sopenharmony_ci async_tx_ack(*tx); 2748c2ecf20Sopenharmony_ci *tx = NULL; 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci} 2778c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(async_tx_quiesce); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ciMODULE_AUTHOR("Intel Corporation"); 2808c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Asynchronous Bulk Memory Transactions API"); 2818c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 282