async_tx.c 14 KB

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