async_tx.c 16 KB

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