async_tx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * core routines for the asynchronous memory transfer/transform api
  3. *
  4. * Copyright © 2006, Intel Corporation.
  5. *
  6. * Dan Williams <dan.j.williams@intel.com>
  7. *
  8. * with architecture considerations by:
  9. * Neil Brown <neilb@suse.de>
  10. * Jeff Garzik <jeff@garzik.org>
  11. *
  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.
  15. *
  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
  19. * more details.
  20. *
  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.
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/async_tx.h>
  28. #ifdef CONFIG_DMA_ENGINE
  29. static enum dma_state_client
  30. dma_channel_add_remove(struct dma_client *client,
  31. struct dma_chan *chan, enum dma_state state);
  32. static struct dma_client async_tx_dma = {
  33. .event_callback = dma_channel_add_remove,
  34. /* .cap_mask == 0 defaults to all channels */
  35. };
  36. /**
  37. * dma_cap_mask_all - enable iteration over all operation types
  38. */
  39. static dma_cap_mask_t dma_cap_mask_all;
  40. /**
  41. * chan_ref_percpu - tracks channel allocations per core/opertion
  42. */
  43. struct chan_ref_percpu {
  44. struct dma_chan_ref *ref;
  45. };
  46. static int channel_table_initialized;
  47. static struct chan_ref_percpu *channel_table[DMA_TX_TYPE_END];
  48. /**
  49. * async_tx_lock - protect modification of async_tx_master_list and serialize
  50. * rebalance operations
  51. */
  52. static spinlock_t async_tx_lock;
  53. static LIST_HEAD(async_tx_master_list);
  54. /* async_tx_issue_pending_all - start all transactions on all channels */
  55. void async_tx_issue_pending_all(void)
  56. {
  57. struct dma_chan_ref *ref;
  58. rcu_read_lock();
  59. list_for_each_entry_rcu(ref, &async_tx_master_list, node)
  60. ref->chan->device->device_issue_pending(ref->chan);
  61. rcu_read_unlock();
  62. }
  63. EXPORT_SYMBOL_GPL(async_tx_issue_pending_all);
  64. /* dma_wait_for_async_tx - spin wait for a transcation to complete
  65. * @tx: transaction to wait on
  66. */
  67. enum dma_status
  68. dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  69. {
  70. enum dma_status status;
  71. struct dma_async_tx_descriptor *iter;
  72. struct dma_async_tx_descriptor *parent;
  73. if (!tx)
  74. return DMA_SUCCESS;
  75. /* poll through the dependency chain, return when tx is complete */
  76. do {
  77. iter = tx;
  78. /* find the root of the unsubmitted dependency chain */
  79. while (iter->cookie == -EBUSY) {
  80. parent = iter->parent;
  81. if (parent && parent->cookie == -EBUSY)
  82. iter = iter->parent;
  83. else
  84. break;
  85. }
  86. status = dma_sync_wait(iter->chan, iter->cookie);
  87. } while (status == DMA_IN_PROGRESS || (iter != tx));
  88. return status;
  89. }
  90. EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
  91. /* async_tx_run_dependencies - helper routine for dma drivers to process
  92. * (start) dependent operations on their target channel
  93. * @tx: transaction with dependencies
  94. */
  95. void
  96. async_tx_run_dependencies(struct dma_async_tx_descriptor *tx)
  97. {
  98. struct dma_async_tx_descriptor *dep_tx, *_dep_tx;
  99. struct dma_device *dev;
  100. struct dma_chan *chan;
  101. list_for_each_entry_safe(dep_tx, _dep_tx, &tx->depend_list,
  102. depend_node) {
  103. chan = dep_tx->chan;
  104. dev = chan->device;
  105. /* we can't depend on ourselves */
  106. BUG_ON(chan == tx->chan);
  107. list_del(&dep_tx->depend_node);
  108. tx->tx_submit(dep_tx);
  109. /* we need to poke the engine as client code does not
  110. * know about dependency submission events
  111. */
  112. dev->device_issue_pending(chan);
  113. }
  114. }
  115. EXPORT_SYMBOL_GPL(async_tx_run_dependencies);
  116. static void
  117. free_dma_chan_ref(struct rcu_head *rcu)
  118. {
  119. struct dma_chan_ref *ref;
  120. ref = container_of(rcu, struct dma_chan_ref, rcu);
  121. kfree(ref);
  122. }
  123. static void
  124. init_dma_chan_ref(struct dma_chan_ref *ref, struct dma_chan *chan)
  125. {
  126. INIT_LIST_HEAD(&ref->node);
  127. INIT_RCU_HEAD(&ref->rcu);
  128. ref->chan = chan;
  129. atomic_set(&ref->count, 0);
  130. }
  131. /**
  132. * get_chan_ref_by_cap - returns the nth channel of the given capability
  133. * defaults to returning the channel with the desired capability and the
  134. * lowest reference count if the index can not be satisfied
  135. * @cap: capability to match
  136. * @index: nth channel desired, passing -1 has the effect of forcing the
  137. * default return value
  138. */
  139. static struct dma_chan_ref *
  140. get_chan_ref_by_cap(enum dma_transaction_type cap, int index)
  141. {
  142. struct dma_chan_ref *ret_ref = NULL, *min_ref = NULL, *ref;
  143. rcu_read_lock();
  144. list_for_each_entry_rcu(ref, &async_tx_master_list, node)
  145. if (dma_has_cap(cap, ref->chan->device->cap_mask)) {
  146. if (!min_ref)
  147. min_ref = ref;
  148. else if (atomic_read(&ref->count) <
  149. atomic_read(&min_ref->count))
  150. min_ref = ref;
  151. if (index-- == 0) {
  152. ret_ref = ref;
  153. break;
  154. }
  155. }
  156. rcu_read_unlock();
  157. if (!ret_ref)
  158. ret_ref = min_ref;
  159. if (ret_ref)
  160. atomic_inc(&ret_ref->count);
  161. return ret_ref;
  162. }
  163. /**
  164. * async_tx_rebalance - redistribute the available channels, optimize
  165. * for cpu isolation in the SMP case, and opertaion isolation in the
  166. * uniprocessor case
  167. */
  168. static void async_tx_rebalance(void)
  169. {
  170. int cpu, cap, cpu_idx = 0;
  171. unsigned long flags;
  172. if (!channel_table_initialized)
  173. return;
  174. spin_lock_irqsave(&async_tx_lock, flags);
  175. /* undo the last distribution */
  176. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  177. for_each_possible_cpu(cpu) {
  178. struct dma_chan_ref *ref =
  179. per_cpu_ptr(channel_table[cap], cpu)->ref;
  180. if (ref) {
  181. atomic_set(&ref->count, 0);
  182. per_cpu_ptr(channel_table[cap], cpu)->ref =
  183. NULL;
  184. }
  185. }
  186. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  187. for_each_online_cpu(cpu) {
  188. struct dma_chan_ref *new;
  189. if (NR_CPUS > 1)
  190. new = get_chan_ref_by_cap(cap, cpu_idx++);
  191. else
  192. new = get_chan_ref_by_cap(cap, -1);
  193. per_cpu_ptr(channel_table[cap], cpu)->ref = new;
  194. }
  195. spin_unlock_irqrestore(&async_tx_lock, flags);
  196. }
  197. static enum dma_state_client
  198. dma_channel_add_remove(struct dma_client *client,
  199. struct dma_chan *chan, enum dma_state state)
  200. {
  201. unsigned long found, flags;
  202. struct dma_chan_ref *master_ref, *ref;
  203. enum dma_state_client ack = DMA_DUP; /* default: take no action */
  204. switch (state) {
  205. case DMA_RESOURCE_AVAILABLE:
  206. found = 0;
  207. rcu_read_lock();
  208. list_for_each_entry_rcu(ref, &async_tx_master_list, node)
  209. if (ref->chan == chan) {
  210. found = 1;
  211. break;
  212. }
  213. rcu_read_unlock();
  214. pr_debug("async_tx: dma resource available [%s]\n",
  215. found ? "old" : "new");
  216. if (!found)
  217. ack = DMA_ACK;
  218. else
  219. break;
  220. /* add the channel to the generic management list */
  221. master_ref = kmalloc(sizeof(*master_ref), GFP_KERNEL);
  222. if (master_ref) {
  223. /* keep a reference until async_tx is unloaded */
  224. dma_chan_get(chan);
  225. init_dma_chan_ref(master_ref, chan);
  226. spin_lock_irqsave(&async_tx_lock, flags);
  227. list_add_tail_rcu(&master_ref->node,
  228. &async_tx_master_list);
  229. spin_unlock_irqrestore(&async_tx_lock,
  230. flags);
  231. } else {
  232. printk(KERN_WARNING "async_tx: unable to create"
  233. " new master entry in response to"
  234. " a DMA_RESOURCE_ADDED event"
  235. " (-ENOMEM)\n");
  236. return 0;
  237. }
  238. async_tx_rebalance();
  239. break;
  240. case DMA_RESOURCE_REMOVED:
  241. found = 0;
  242. spin_lock_irqsave(&async_tx_lock, flags);
  243. list_for_each_entry_rcu(ref, &async_tx_master_list, node)
  244. if (ref->chan == chan) {
  245. /* permit backing devices to go away */
  246. dma_chan_put(ref->chan);
  247. list_del_rcu(&ref->node);
  248. call_rcu(&ref->rcu, free_dma_chan_ref);
  249. found = 1;
  250. break;
  251. }
  252. spin_unlock_irqrestore(&async_tx_lock, flags);
  253. pr_debug("async_tx: dma resource removed [%s]\n",
  254. found ? "ours" : "not ours");
  255. if (found)
  256. ack = DMA_ACK;
  257. else
  258. break;
  259. async_tx_rebalance();
  260. break;
  261. case DMA_RESOURCE_SUSPEND:
  262. case DMA_RESOURCE_RESUME:
  263. printk(KERN_WARNING "async_tx: does not support dma channel"
  264. " suspend/resume\n");
  265. break;
  266. default:
  267. BUG();
  268. }
  269. return ack;
  270. }
  271. static int __init
  272. async_tx_init(void)
  273. {
  274. enum dma_transaction_type cap;
  275. spin_lock_init(&async_tx_lock);
  276. bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
  277. /* an interrupt will never be an explicit operation type.
  278. * clearing this bit prevents allocation to a slot in 'channel_table'
  279. */
  280. clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
  281. for_each_dma_cap_mask(cap, dma_cap_mask_all) {
  282. channel_table[cap] = alloc_percpu(struct chan_ref_percpu);
  283. if (!channel_table[cap])
  284. goto err;
  285. }
  286. channel_table_initialized = 1;
  287. dma_async_client_register(&async_tx_dma);
  288. dma_async_client_chan_request(&async_tx_dma);
  289. printk(KERN_INFO "async_tx: api initialized (async)\n");
  290. return 0;
  291. err:
  292. printk(KERN_ERR "async_tx: initialization failure\n");
  293. while (--cap >= 0)
  294. free_percpu(channel_table[cap]);
  295. return 1;
  296. }
  297. static void __exit async_tx_exit(void)
  298. {
  299. enum dma_transaction_type cap;
  300. channel_table_initialized = 0;
  301. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  302. if (channel_table[cap])
  303. free_percpu(channel_table[cap]);
  304. dma_async_client_unregister(&async_tx_dma);
  305. }
  306. /**
  307. * __async_tx_find_channel - find a channel to carry out the operation or let
  308. * the transaction execute synchronously
  309. * @depend_tx: transaction dependency
  310. * @tx_type: transaction type
  311. */
  312. struct dma_chan *
  313. __async_tx_find_channel(struct dma_async_tx_descriptor *depend_tx,
  314. enum dma_transaction_type tx_type)
  315. {
  316. /* see if we can keep the chain on one channel */
  317. if (depend_tx &&
  318. dma_has_cap(tx_type, depend_tx->chan->device->cap_mask))
  319. return depend_tx->chan;
  320. else if (likely(channel_table_initialized)) {
  321. struct dma_chan_ref *ref;
  322. int cpu = get_cpu();
  323. ref = per_cpu_ptr(channel_table[tx_type], cpu)->ref;
  324. put_cpu();
  325. return ref ? ref->chan : NULL;
  326. } else
  327. return NULL;
  328. }
  329. EXPORT_SYMBOL_GPL(__async_tx_find_channel);
  330. #else
  331. static int __init async_tx_init(void)
  332. {
  333. printk(KERN_INFO "async_tx: api initialized (sync-only)\n");
  334. return 0;
  335. }
  336. static void __exit async_tx_exit(void)
  337. {
  338. do { } while (0);
  339. }
  340. #endif
  341. void
  342. async_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx,
  343. enum async_tx_flags flags, struct dma_async_tx_descriptor *depend_tx,
  344. dma_async_tx_callback cb_fn, void *cb_param)
  345. {
  346. tx->callback = cb_fn;
  347. tx->callback_param = cb_param;
  348. /* set this new tx to run after depend_tx if:
  349. * 1/ a dependency exists (depend_tx is !NULL)
  350. * 2/ the tx can not be submitted to the current channel
  351. */
  352. if (depend_tx && depend_tx->chan != chan) {
  353. /* if ack is already set then we cannot be sure
  354. * we are referring to the correct operation
  355. */
  356. BUG_ON(depend_tx->ack);
  357. tx->parent = depend_tx;
  358. spin_lock_bh(&depend_tx->lock);
  359. list_add_tail(&tx->depend_node, &depend_tx->depend_list);
  360. if (depend_tx->cookie == 0) {
  361. struct dma_chan *dep_chan = depend_tx->chan;
  362. struct dma_device *dep_dev = dep_chan->device;
  363. dep_dev->device_dependency_added(dep_chan);
  364. }
  365. spin_unlock_bh(&depend_tx->lock);
  366. /* schedule an interrupt to trigger the channel switch */
  367. async_trigger_callback(ASYNC_TX_ACK, depend_tx, NULL, NULL);
  368. } else {
  369. tx->parent = NULL;
  370. tx->tx_submit(tx);
  371. }
  372. if (flags & ASYNC_TX_ACK)
  373. async_tx_ack(tx);
  374. if (depend_tx && (flags & ASYNC_TX_DEP_ACK))
  375. async_tx_ack(depend_tx);
  376. }
  377. EXPORT_SYMBOL_GPL(async_tx_submit);
  378. /**
  379. * async_trigger_callback - schedules the callback function to be run after
  380. * any dependent operations have been completed.
  381. * @flags: ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
  382. * @depend_tx: 'callback' requires the completion of this transaction
  383. * @cb_fn: function to call after depend_tx completes
  384. * @cb_param: parameter to pass to the callback routine
  385. */
  386. struct dma_async_tx_descriptor *
  387. async_trigger_callback(enum async_tx_flags flags,
  388. struct dma_async_tx_descriptor *depend_tx,
  389. dma_async_tx_callback cb_fn, void *cb_param)
  390. {
  391. struct dma_chan *chan;
  392. struct dma_device *device;
  393. struct dma_async_tx_descriptor *tx;
  394. if (depend_tx) {
  395. chan = depend_tx->chan;
  396. device = chan->device;
  397. /* see if we can schedule an interrupt
  398. * otherwise poll for completion
  399. */
  400. if (device && !dma_has_cap(DMA_INTERRUPT, device->cap_mask))
  401. device = NULL;
  402. tx = device ? device->device_prep_dma_interrupt(chan) : NULL;
  403. } else
  404. tx = NULL;
  405. if (tx) {
  406. pr_debug("%s: (async)\n", __FUNCTION__);
  407. async_tx_submit(chan, tx, flags, depend_tx, cb_fn, cb_param);
  408. } else {
  409. pr_debug("%s: (sync)\n", __FUNCTION__);
  410. /* wait for any prerequisite operations */
  411. if (depend_tx) {
  412. /* if ack is already set then we cannot be sure
  413. * we are referring to the correct operation
  414. */
  415. BUG_ON(depend_tx->ack);
  416. if (dma_wait_for_async_tx(depend_tx) == DMA_ERROR)
  417. panic("%s: DMA_ERROR waiting for depend_tx\n",
  418. __FUNCTION__);
  419. }
  420. async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param);
  421. }
  422. return tx;
  423. }
  424. EXPORT_SYMBOL_GPL(async_trigger_callback);
  425. module_init(async_tx_init);
  426. module_exit(async_tx_exit);
  427. MODULE_AUTHOR("Intel Corporation");
  428. MODULE_DESCRIPTION("Asynchronous Bulk Memory Transactions API");
  429. MODULE_LICENSE("GPL");