async_tx.c 16 KB

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