async_tx.c 13 KB

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