async_tx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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/rculist.h>
  27. #include <linux/kernel.h>
  28. #include <linux/async_tx.h>
  29. #ifdef CONFIG_DMA_ENGINE
  30. static enum dma_state_client
  31. dma_channel_add_remove(struct dma_client *client,
  32. struct dma_chan *chan, enum dma_state state);
  33. static struct dma_client async_tx_dma = {
  34. .event_callback = dma_channel_add_remove,
  35. /* .cap_mask == 0 defaults to all channels */
  36. };
  37. /**
  38. * dma_cap_mask_all - enable iteration over all operation types
  39. */
  40. static dma_cap_mask_t dma_cap_mask_all;
  41. /**
  42. * chan_ref_percpu - tracks channel allocations per core/opertion
  43. */
  44. struct chan_ref_percpu {
  45. struct dma_chan_ref *ref;
  46. };
  47. static int channel_table_initialized;
  48. static struct chan_ref_percpu *channel_table[DMA_TX_TYPE_END];
  49. /**
  50. * async_tx_lock - protect modification of async_tx_master_list and serialize
  51. * rebalance operations
  52. */
  53. static spinlock_t async_tx_lock;
  54. static LIST_HEAD(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. do {
  81. parent = iter->parent;
  82. if (!parent)
  83. break;
  84. else
  85. iter = parent;
  86. } while (parent);
  87. /* there is a small window for ->parent == NULL and
  88. * ->cookie == -EBUSY
  89. */
  90. while (iter->cookie == -EBUSY)
  91. cpu_relax();
  92. status = dma_sync_wait(iter->chan, iter->cookie);
  93. } while (status == DMA_IN_PROGRESS || (iter != tx));
  94. return status;
  95. }
  96. EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
  97. /* async_tx_run_dependencies - helper routine for dma drivers to process
  98. * (start) dependent operations on their target channel
  99. * @tx: transaction with dependencies
  100. */
  101. void
  102. async_tx_run_dependencies(struct dma_async_tx_descriptor *tx)
  103. {
  104. struct dma_async_tx_descriptor *next = tx->next;
  105. struct dma_chan *chan;
  106. if (!next)
  107. return;
  108. tx->next = NULL;
  109. chan = next->chan;
  110. /* keep submitting up until a channel switch is detected
  111. * in that case we will be called again as a result of
  112. * processing the interrupt from async_tx_channel_switch
  113. */
  114. while (next && next->chan == chan) {
  115. struct dma_async_tx_descriptor *_next;
  116. spin_lock_bh(&next->lock);
  117. next->parent = NULL;
  118. _next = next->next;
  119. if (_next && _next->chan == chan)
  120. next->next = NULL;
  121. spin_unlock_bh(&next->lock);
  122. next->tx_submit(next);
  123. next = _next;
  124. }
  125. chan->device->device_issue_pending(chan);
  126. }
  127. EXPORT_SYMBOL_GPL(async_tx_run_dependencies);
  128. static void
  129. free_dma_chan_ref(struct rcu_head *rcu)
  130. {
  131. struct dma_chan_ref *ref;
  132. ref = container_of(rcu, struct dma_chan_ref, rcu);
  133. kfree(ref);
  134. }
  135. static void
  136. init_dma_chan_ref(struct dma_chan_ref *ref, struct dma_chan *chan)
  137. {
  138. INIT_LIST_HEAD(&ref->node);
  139. INIT_RCU_HEAD(&ref->rcu);
  140. ref->chan = chan;
  141. atomic_set(&ref->count, 0);
  142. }
  143. /**
  144. * get_chan_ref_by_cap - returns the nth channel of the given capability
  145. * defaults to returning the channel with the desired capability and the
  146. * lowest reference count if the index can not be satisfied
  147. * @cap: capability to match
  148. * @index: nth channel desired, passing -1 has the effect of forcing the
  149. * default return value
  150. */
  151. static struct dma_chan_ref *
  152. get_chan_ref_by_cap(enum dma_transaction_type cap, int index)
  153. {
  154. struct dma_chan_ref *ret_ref = NULL, *min_ref = NULL, *ref;
  155. rcu_read_lock();
  156. list_for_each_entry_rcu(ref, &async_tx_master_list, node)
  157. if (dma_has_cap(cap, ref->chan->device->cap_mask)) {
  158. if (!min_ref)
  159. min_ref = ref;
  160. else if (atomic_read(&ref->count) <
  161. atomic_read(&min_ref->count))
  162. min_ref = ref;
  163. if (index-- == 0) {
  164. ret_ref = ref;
  165. break;
  166. }
  167. }
  168. rcu_read_unlock();
  169. if (!ret_ref)
  170. ret_ref = min_ref;
  171. if (ret_ref)
  172. atomic_inc(&ret_ref->count);
  173. return ret_ref;
  174. }
  175. /**
  176. * async_tx_rebalance - redistribute the available channels, optimize
  177. * for cpu isolation in the SMP case, and opertaion isolation in the
  178. * uniprocessor case
  179. */
  180. static void async_tx_rebalance(void)
  181. {
  182. int cpu, cap, cpu_idx = 0;
  183. unsigned long flags;
  184. if (!channel_table_initialized)
  185. return;
  186. spin_lock_irqsave(&async_tx_lock, flags);
  187. /* undo the last distribution */
  188. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  189. for_each_possible_cpu(cpu) {
  190. struct dma_chan_ref *ref =
  191. per_cpu_ptr(channel_table[cap], cpu)->ref;
  192. if (ref) {
  193. atomic_set(&ref->count, 0);
  194. per_cpu_ptr(channel_table[cap], cpu)->ref =
  195. NULL;
  196. }
  197. }
  198. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  199. for_each_online_cpu(cpu) {
  200. struct dma_chan_ref *new;
  201. if (NR_CPUS > 1)
  202. new = get_chan_ref_by_cap(cap, cpu_idx++);
  203. else
  204. new = get_chan_ref_by_cap(cap, -1);
  205. per_cpu_ptr(channel_table[cap], cpu)->ref = new;
  206. }
  207. spin_unlock_irqrestore(&async_tx_lock, flags);
  208. }
  209. static enum dma_state_client
  210. dma_channel_add_remove(struct dma_client *client,
  211. struct dma_chan *chan, enum dma_state state)
  212. {
  213. unsigned long found, flags;
  214. struct dma_chan_ref *master_ref, *ref;
  215. enum dma_state_client ack = DMA_DUP; /* default: take no action */
  216. switch (state) {
  217. case DMA_RESOURCE_AVAILABLE:
  218. found = 0;
  219. rcu_read_lock();
  220. list_for_each_entry_rcu(ref, &async_tx_master_list, node)
  221. if (ref->chan == chan) {
  222. found = 1;
  223. break;
  224. }
  225. rcu_read_unlock();
  226. pr_debug("async_tx: dma resource available [%s]\n",
  227. found ? "old" : "new");
  228. if (!found)
  229. ack = DMA_ACK;
  230. else
  231. break;
  232. /* add the channel to the generic management list */
  233. master_ref = kmalloc(sizeof(*master_ref), GFP_KERNEL);
  234. if (master_ref) {
  235. /* keep a reference until async_tx is unloaded */
  236. dma_chan_get(chan);
  237. init_dma_chan_ref(master_ref, chan);
  238. spin_lock_irqsave(&async_tx_lock, flags);
  239. list_add_tail_rcu(&master_ref->node,
  240. &async_tx_master_list);
  241. spin_unlock_irqrestore(&async_tx_lock,
  242. flags);
  243. } else {
  244. printk(KERN_WARNING "async_tx: unable to create"
  245. " new master entry in response to"
  246. " a DMA_RESOURCE_ADDED event"
  247. " (-ENOMEM)\n");
  248. return 0;
  249. }
  250. async_tx_rebalance();
  251. break;
  252. case DMA_RESOURCE_REMOVED:
  253. found = 0;
  254. spin_lock_irqsave(&async_tx_lock, flags);
  255. list_for_each_entry(ref, &async_tx_master_list, node)
  256. if (ref->chan == chan) {
  257. /* permit backing devices to go away */
  258. dma_chan_put(ref->chan);
  259. list_del_rcu(&ref->node);
  260. call_rcu(&ref->rcu, free_dma_chan_ref);
  261. found = 1;
  262. break;
  263. }
  264. spin_unlock_irqrestore(&async_tx_lock, flags);
  265. pr_debug("async_tx: dma resource removed [%s]\n",
  266. found ? "ours" : "not ours");
  267. if (found)
  268. ack = DMA_ACK;
  269. else
  270. break;
  271. async_tx_rebalance();
  272. break;
  273. case DMA_RESOURCE_SUSPEND:
  274. case DMA_RESOURCE_RESUME:
  275. printk(KERN_WARNING "async_tx: does not support dma channel"
  276. " suspend/resume\n");
  277. break;
  278. default:
  279. BUG();
  280. }
  281. return ack;
  282. }
  283. static int __init
  284. async_tx_init(void)
  285. {
  286. enum dma_transaction_type cap;
  287. spin_lock_init(&async_tx_lock);
  288. bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
  289. /* an interrupt will never be an explicit operation type.
  290. * clearing this bit prevents allocation to a slot in 'channel_table'
  291. */
  292. clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
  293. for_each_dma_cap_mask(cap, dma_cap_mask_all) {
  294. channel_table[cap] = alloc_percpu(struct chan_ref_percpu);
  295. if (!channel_table[cap])
  296. goto err;
  297. }
  298. channel_table_initialized = 1;
  299. dma_async_client_register(&async_tx_dma);
  300. dma_async_client_chan_request(&async_tx_dma);
  301. printk(KERN_INFO "async_tx: api initialized (async)\n");
  302. return 0;
  303. err:
  304. printk(KERN_ERR "async_tx: initialization failure\n");
  305. while (--cap >= 0)
  306. free_percpu(channel_table[cap]);
  307. return 1;
  308. }
  309. static void __exit async_tx_exit(void)
  310. {
  311. enum dma_transaction_type cap;
  312. channel_table_initialized = 0;
  313. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  314. if (channel_table[cap])
  315. free_percpu(channel_table[cap]);
  316. dma_async_client_unregister(&async_tx_dma);
  317. }
  318. /**
  319. * __async_tx_find_channel - find a channel to carry out the operation or let
  320. * the transaction execute synchronously
  321. * @depend_tx: transaction dependency
  322. * @tx_type: transaction type
  323. */
  324. struct dma_chan *
  325. __async_tx_find_channel(struct dma_async_tx_descriptor *depend_tx,
  326. enum dma_transaction_type tx_type)
  327. {
  328. /* see if we can keep the chain on one channel */
  329. if (depend_tx &&
  330. dma_has_cap(tx_type, depend_tx->chan->device->cap_mask))
  331. return depend_tx->chan;
  332. else if (likely(channel_table_initialized)) {
  333. struct dma_chan_ref *ref;
  334. int cpu = get_cpu();
  335. ref = per_cpu_ptr(channel_table[tx_type], cpu)->ref;
  336. put_cpu();
  337. return ref ? ref->chan : NULL;
  338. } else
  339. return NULL;
  340. }
  341. EXPORT_SYMBOL_GPL(__async_tx_find_channel);
  342. #else
  343. static int __init async_tx_init(void)
  344. {
  345. printk(KERN_INFO "async_tx: api initialized (sync-only)\n");
  346. return 0;
  347. }
  348. static void __exit async_tx_exit(void)
  349. {
  350. do { } while (0);
  351. }
  352. #endif
  353. /**
  354. * async_tx_channel_switch - queue an interrupt descriptor with a dependency
  355. * pre-attached.
  356. * @depend_tx: the operation that must finish before the new operation runs
  357. * @tx: the new operation
  358. */
  359. static void
  360. async_tx_channel_switch(struct dma_async_tx_descriptor *depend_tx,
  361. struct dma_async_tx_descriptor *tx)
  362. {
  363. struct dma_chan *chan;
  364. struct dma_device *device;
  365. struct dma_async_tx_descriptor *intr_tx = (void *) ~0;
  366. /* first check to see if we can still append to depend_tx */
  367. spin_lock_bh(&depend_tx->lock);
  368. if (depend_tx->parent && depend_tx->chan == tx->chan) {
  369. tx->parent = depend_tx;
  370. depend_tx->next = tx;
  371. intr_tx = NULL;
  372. }
  373. spin_unlock_bh(&depend_tx->lock);
  374. if (!intr_tx)
  375. return;
  376. chan = depend_tx->chan;
  377. device = chan->device;
  378. /* see if we can schedule an interrupt
  379. * otherwise poll for completion
  380. */
  381. if (dma_has_cap(DMA_INTERRUPT, device->cap_mask))
  382. intr_tx = device->device_prep_dma_interrupt(chan, 0);
  383. else
  384. intr_tx = NULL;
  385. if (intr_tx) {
  386. intr_tx->callback = NULL;
  387. intr_tx->callback_param = NULL;
  388. tx->parent = intr_tx;
  389. /* safe to set ->next outside the lock since we know we are
  390. * not submitted yet
  391. */
  392. intr_tx->next = tx;
  393. /* check if we need to append */
  394. spin_lock_bh(&depend_tx->lock);
  395. if (depend_tx->parent) {
  396. intr_tx->parent = depend_tx;
  397. depend_tx->next = intr_tx;
  398. async_tx_ack(intr_tx);
  399. intr_tx = NULL;
  400. }
  401. spin_unlock_bh(&depend_tx->lock);
  402. if (intr_tx) {
  403. intr_tx->parent = NULL;
  404. intr_tx->tx_submit(intr_tx);
  405. async_tx_ack(intr_tx);
  406. }
  407. } else {
  408. if (dma_wait_for_async_tx(depend_tx) == DMA_ERROR)
  409. panic("%s: DMA_ERROR waiting for depend_tx\n",
  410. __func__);
  411. tx->tx_submit(tx);
  412. }
  413. }
  414. /**
  415. * submit_disposition - while holding depend_tx->lock we must avoid submitting
  416. * new operations to prevent a circular locking dependency with
  417. * drivers that already hold a channel lock when calling
  418. * async_tx_run_dependencies.
  419. * @ASYNC_TX_SUBMITTED: we were able to append the new operation under the lock
  420. * @ASYNC_TX_CHANNEL_SWITCH: when the lock is dropped schedule a channel switch
  421. * @ASYNC_TX_DIRECT_SUBMIT: when the lock is dropped submit directly
  422. */
  423. enum submit_disposition {
  424. ASYNC_TX_SUBMITTED,
  425. ASYNC_TX_CHANNEL_SWITCH,
  426. ASYNC_TX_DIRECT_SUBMIT,
  427. };
  428. void
  429. async_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx,
  430. enum async_tx_flags flags, struct dma_async_tx_descriptor *depend_tx,
  431. dma_async_tx_callback cb_fn, void *cb_param)
  432. {
  433. tx->callback = cb_fn;
  434. tx->callback_param = cb_param;
  435. if (depend_tx) {
  436. enum submit_disposition s;
  437. /* sanity check the dependency chain:
  438. * 1/ if ack is already set then we cannot be sure
  439. * we are referring to the correct operation
  440. * 2/ dependencies are 1:1 i.e. two transactions can
  441. * not depend on the same parent
  442. */
  443. BUG_ON(async_tx_test_ack(depend_tx) || depend_tx->next ||
  444. tx->parent);
  445. /* the lock prevents async_tx_run_dependencies from missing
  446. * the setting of ->next when ->parent != NULL
  447. */
  448. spin_lock_bh(&depend_tx->lock);
  449. if (depend_tx->parent) {
  450. /* we have a parent so we can not submit directly
  451. * if we are staying on the same channel: append
  452. * else: channel switch
  453. */
  454. if (depend_tx->chan == chan) {
  455. tx->parent = depend_tx;
  456. depend_tx->next = tx;
  457. s = ASYNC_TX_SUBMITTED;
  458. } else
  459. s = ASYNC_TX_CHANNEL_SWITCH;
  460. } else {
  461. /* we do not have a parent so we may be able to submit
  462. * directly if we are staying on the same channel
  463. */
  464. if (depend_tx->chan == chan)
  465. s = ASYNC_TX_DIRECT_SUBMIT;
  466. else
  467. s = ASYNC_TX_CHANNEL_SWITCH;
  468. }
  469. spin_unlock_bh(&depend_tx->lock);
  470. switch (s) {
  471. case ASYNC_TX_SUBMITTED:
  472. break;
  473. case ASYNC_TX_CHANNEL_SWITCH:
  474. async_tx_channel_switch(depend_tx, tx);
  475. break;
  476. case ASYNC_TX_DIRECT_SUBMIT:
  477. tx->parent = NULL;
  478. tx->tx_submit(tx);
  479. break;
  480. }
  481. } else {
  482. tx->parent = NULL;
  483. tx->tx_submit(tx);
  484. }
  485. if (flags & ASYNC_TX_ACK)
  486. async_tx_ack(tx);
  487. if (depend_tx && (flags & ASYNC_TX_DEP_ACK))
  488. async_tx_ack(depend_tx);
  489. }
  490. EXPORT_SYMBOL_GPL(async_tx_submit);
  491. /**
  492. * async_trigger_callback - schedules the callback function to be run after
  493. * any dependent operations have been completed.
  494. * @flags: ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
  495. * @depend_tx: 'callback' requires the completion of this transaction
  496. * @cb_fn: function to call after depend_tx completes
  497. * @cb_param: parameter to pass to the callback routine
  498. */
  499. struct dma_async_tx_descriptor *
  500. async_trigger_callback(enum async_tx_flags flags,
  501. struct dma_async_tx_descriptor *depend_tx,
  502. dma_async_tx_callback cb_fn, void *cb_param)
  503. {
  504. struct dma_chan *chan;
  505. struct dma_device *device;
  506. struct dma_async_tx_descriptor *tx;
  507. if (depend_tx) {
  508. chan = depend_tx->chan;
  509. device = chan->device;
  510. /* see if we can schedule an interrupt
  511. * otherwise poll for completion
  512. */
  513. if (device && !dma_has_cap(DMA_INTERRUPT, device->cap_mask))
  514. device = NULL;
  515. tx = device ? device->device_prep_dma_interrupt(chan, 0) : NULL;
  516. } else
  517. tx = NULL;
  518. if (tx) {
  519. pr_debug("%s: (async)\n", __func__);
  520. async_tx_submit(chan, tx, flags, depend_tx, cb_fn, cb_param);
  521. } else {
  522. pr_debug("%s: (sync)\n", __func__);
  523. /* wait for any prerequisite operations */
  524. async_tx_quiesce(&depend_tx);
  525. async_tx_sync_epilog(cb_fn, cb_param);
  526. }
  527. return tx;
  528. }
  529. EXPORT_SYMBOL_GPL(async_trigger_callback);
  530. /**
  531. * async_tx_quiesce - ensure tx is complete and freeable upon return
  532. * @tx - transaction to quiesce
  533. */
  534. void async_tx_quiesce(struct dma_async_tx_descriptor **tx)
  535. {
  536. if (*tx) {
  537. /* if ack is already set then we cannot be sure
  538. * we are referring to the correct operation
  539. */
  540. BUG_ON(async_tx_test_ack(*tx));
  541. if (dma_wait_for_async_tx(*tx) == DMA_ERROR)
  542. panic("DMA_ERROR waiting for transaction\n");
  543. async_tx_ack(*tx);
  544. *tx = NULL;
  545. }
  546. }
  547. EXPORT_SYMBOL_GPL(async_tx_quiesce);
  548. module_init(async_tx_init);
  549. module_exit(async_tx_exit);
  550. MODULE_AUTHOR("Intel Corporation");
  551. MODULE_DESCRIPTION("Asynchronous Bulk Memory Transactions API");
  552. MODULE_LICENSE("GPL");