dmaengine.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  2. * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59
  16. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called COPYING.
  20. */
  21. /*
  22. * This code implements the DMA subsystem. It provides a HW-neutral interface
  23. * for other kernel code to use asynchronous memory copy capabilities,
  24. * if present, and allows different HW DMA drivers to register as providing
  25. * this capability.
  26. *
  27. * Due to the fact we are accelerating what is already a relatively fast
  28. * operation, the code goes to great lengths to avoid additional overhead,
  29. * such as locking.
  30. *
  31. * LOCKING:
  32. *
  33. * The subsystem keeps a global list of dma_device structs it is protected by a
  34. * mutex, dma_list_mutex.
  35. *
  36. * A subsystem can get access to a channel by calling dmaengine_get() followed
  37. * by dma_find_channel(), or if it has need for an exclusive channel it can call
  38. * dma_request_channel(). Once a channel is allocated a reference is taken
  39. * against its corresponding driver to disable removal.
  40. *
  41. * Each device has a channels list, which runs unlocked but is never modified
  42. * once the device is registered, it's just setup by the driver.
  43. *
  44. * See Documentation/dmaengine.txt for more details
  45. */
  46. #include <linux/init.h>
  47. #include <linux/module.h>
  48. #include <linux/mm.h>
  49. #include <linux/device.h>
  50. #include <linux/dmaengine.h>
  51. #include <linux/hardirq.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/percpu.h>
  54. #include <linux/rcupdate.h>
  55. #include <linux/mutex.h>
  56. #include <linux/jiffies.h>
  57. #include <linux/rculist.h>
  58. #include <linux/idr.h>
  59. #include <linux/slab.h>
  60. static DEFINE_MUTEX(dma_list_mutex);
  61. static LIST_HEAD(dma_device_list);
  62. static long dmaengine_ref_count;
  63. static struct idr dma_idr;
  64. /* --- sysfs implementation --- */
  65. /**
  66. * dev_to_dma_chan - convert a device pointer to the its sysfs container object
  67. * @dev - device node
  68. *
  69. * Must be called under dma_list_mutex
  70. */
  71. static struct dma_chan *dev_to_dma_chan(struct device *dev)
  72. {
  73. struct dma_chan_dev *chan_dev;
  74. chan_dev = container_of(dev, typeof(*chan_dev), device);
  75. return chan_dev->chan;
  76. }
  77. static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf)
  78. {
  79. struct dma_chan *chan;
  80. unsigned long count = 0;
  81. int i;
  82. int err;
  83. mutex_lock(&dma_list_mutex);
  84. chan = dev_to_dma_chan(dev);
  85. if (chan) {
  86. for_each_possible_cpu(i)
  87. count += per_cpu_ptr(chan->local, i)->memcpy_count;
  88. err = sprintf(buf, "%lu\n", count);
  89. } else
  90. err = -ENODEV;
  91. mutex_unlock(&dma_list_mutex);
  92. return err;
  93. }
  94. static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr,
  95. char *buf)
  96. {
  97. struct dma_chan *chan;
  98. unsigned long count = 0;
  99. int i;
  100. int err;
  101. mutex_lock(&dma_list_mutex);
  102. chan = dev_to_dma_chan(dev);
  103. if (chan) {
  104. for_each_possible_cpu(i)
  105. count += per_cpu_ptr(chan->local, i)->bytes_transferred;
  106. err = sprintf(buf, "%lu\n", count);
  107. } else
  108. err = -ENODEV;
  109. mutex_unlock(&dma_list_mutex);
  110. return err;
  111. }
  112. static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf)
  113. {
  114. struct dma_chan *chan;
  115. int err;
  116. mutex_lock(&dma_list_mutex);
  117. chan = dev_to_dma_chan(dev);
  118. if (chan)
  119. err = sprintf(buf, "%d\n", chan->client_count);
  120. else
  121. err = -ENODEV;
  122. mutex_unlock(&dma_list_mutex);
  123. return err;
  124. }
  125. static struct device_attribute dma_attrs[] = {
  126. __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL),
  127. __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL),
  128. __ATTR(in_use, S_IRUGO, show_in_use, NULL),
  129. __ATTR_NULL
  130. };
  131. static void chan_dev_release(struct device *dev)
  132. {
  133. struct dma_chan_dev *chan_dev;
  134. chan_dev = container_of(dev, typeof(*chan_dev), device);
  135. if (atomic_dec_and_test(chan_dev->idr_ref)) {
  136. mutex_lock(&dma_list_mutex);
  137. idr_remove(&dma_idr, chan_dev->dev_id);
  138. mutex_unlock(&dma_list_mutex);
  139. kfree(chan_dev->idr_ref);
  140. }
  141. kfree(chan_dev);
  142. }
  143. static struct class dma_devclass = {
  144. .name = "dma",
  145. .dev_attrs = dma_attrs,
  146. .dev_release = chan_dev_release,
  147. };
  148. /* --- client and device registration --- */
  149. #define dma_device_satisfies_mask(device, mask) \
  150. __dma_device_satisfies_mask((device), &(mask))
  151. static int
  152. __dma_device_satisfies_mask(struct dma_device *device, dma_cap_mask_t *want)
  153. {
  154. dma_cap_mask_t has;
  155. bitmap_and(has.bits, want->bits, device->cap_mask.bits,
  156. DMA_TX_TYPE_END);
  157. return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
  158. }
  159. static struct module *dma_chan_to_owner(struct dma_chan *chan)
  160. {
  161. return chan->device->dev->driver->owner;
  162. }
  163. /**
  164. * balance_ref_count - catch up the channel reference count
  165. * @chan - channel to balance ->client_count versus dmaengine_ref_count
  166. *
  167. * balance_ref_count must be called under dma_list_mutex
  168. */
  169. static void balance_ref_count(struct dma_chan *chan)
  170. {
  171. struct module *owner = dma_chan_to_owner(chan);
  172. while (chan->client_count < dmaengine_ref_count) {
  173. __module_get(owner);
  174. chan->client_count++;
  175. }
  176. }
  177. /**
  178. * dma_chan_get - try to grab a dma channel's parent driver module
  179. * @chan - channel to grab
  180. *
  181. * Must be called under dma_list_mutex
  182. */
  183. static int dma_chan_get(struct dma_chan *chan)
  184. {
  185. int err = -ENODEV;
  186. struct module *owner = dma_chan_to_owner(chan);
  187. if (chan->client_count) {
  188. __module_get(owner);
  189. err = 0;
  190. } else if (try_module_get(owner))
  191. err = 0;
  192. if (err == 0)
  193. chan->client_count++;
  194. /* allocate upon first client reference */
  195. if (chan->client_count == 1 && err == 0) {
  196. int desc_cnt = chan->device->device_alloc_chan_resources(chan);
  197. if (desc_cnt < 0) {
  198. err = desc_cnt;
  199. chan->client_count = 0;
  200. module_put(owner);
  201. } else if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
  202. balance_ref_count(chan);
  203. }
  204. return err;
  205. }
  206. /**
  207. * dma_chan_put - drop a reference to a dma channel's parent driver module
  208. * @chan - channel to release
  209. *
  210. * Must be called under dma_list_mutex
  211. */
  212. static void dma_chan_put(struct dma_chan *chan)
  213. {
  214. if (!chan->client_count)
  215. return; /* this channel failed alloc_chan_resources */
  216. chan->client_count--;
  217. module_put(dma_chan_to_owner(chan));
  218. if (chan->client_count == 0)
  219. chan->device->device_free_chan_resources(chan);
  220. }
  221. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  222. {
  223. enum dma_status status;
  224. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  225. dma_async_issue_pending(chan);
  226. do {
  227. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  228. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  229. printk(KERN_ERR "dma_sync_wait_timeout!\n");
  230. return DMA_ERROR;
  231. }
  232. } while (status == DMA_IN_PROGRESS);
  233. return status;
  234. }
  235. EXPORT_SYMBOL(dma_sync_wait);
  236. /**
  237. * dma_cap_mask_all - enable iteration over all operation types
  238. */
  239. static dma_cap_mask_t dma_cap_mask_all;
  240. /**
  241. * dma_chan_tbl_ent - tracks channel allocations per core/operation
  242. * @chan - associated channel for this entry
  243. */
  244. struct dma_chan_tbl_ent {
  245. struct dma_chan *chan;
  246. };
  247. /**
  248. * channel_table - percpu lookup table for memory-to-memory offload providers
  249. */
  250. static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
  251. static int __init dma_channel_table_init(void)
  252. {
  253. enum dma_transaction_type cap;
  254. int err = 0;
  255. bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
  256. /* 'interrupt', 'private', and 'slave' are channel capabilities,
  257. * but are not associated with an operation so they do not need
  258. * an entry in the channel_table
  259. */
  260. clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
  261. clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
  262. clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
  263. for_each_dma_cap_mask(cap, dma_cap_mask_all) {
  264. channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
  265. if (!channel_table[cap]) {
  266. err = -ENOMEM;
  267. break;
  268. }
  269. }
  270. if (err) {
  271. pr_err("dmaengine: initialization failure\n");
  272. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  273. if (channel_table[cap])
  274. free_percpu(channel_table[cap]);
  275. }
  276. return err;
  277. }
  278. arch_initcall(dma_channel_table_init);
  279. /**
  280. * dma_find_channel - find a channel to carry out the operation
  281. * @tx_type: transaction type
  282. */
  283. struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
  284. {
  285. return this_cpu_read(channel_table[tx_type]->chan);
  286. }
  287. EXPORT_SYMBOL(dma_find_channel);
  288. /**
  289. * dma_issue_pending_all - flush all pending operations across all channels
  290. */
  291. void dma_issue_pending_all(void)
  292. {
  293. struct dma_device *device;
  294. struct dma_chan *chan;
  295. rcu_read_lock();
  296. list_for_each_entry_rcu(device, &dma_device_list, global_node) {
  297. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  298. continue;
  299. list_for_each_entry(chan, &device->channels, device_node)
  300. if (chan->client_count)
  301. device->device_issue_pending(chan);
  302. }
  303. rcu_read_unlock();
  304. }
  305. EXPORT_SYMBOL(dma_issue_pending_all);
  306. /**
  307. * nth_chan - returns the nth channel of the given capability
  308. * @cap: capability to match
  309. * @n: nth channel desired
  310. *
  311. * Defaults to returning the channel with the desired capability and the
  312. * lowest reference count when 'n' cannot be satisfied. Must be called
  313. * under dma_list_mutex.
  314. */
  315. static struct dma_chan *nth_chan(enum dma_transaction_type cap, int n)
  316. {
  317. struct dma_device *device;
  318. struct dma_chan *chan;
  319. struct dma_chan *ret = NULL;
  320. struct dma_chan *min = NULL;
  321. list_for_each_entry(device, &dma_device_list, global_node) {
  322. if (!dma_has_cap(cap, device->cap_mask) ||
  323. dma_has_cap(DMA_PRIVATE, device->cap_mask))
  324. continue;
  325. list_for_each_entry(chan, &device->channels, device_node) {
  326. if (!chan->client_count)
  327. continue;
  328. if (!min)
  329. min = chan;
  330. else if (chan->table_count < min->table_count)
  331. min = chan;
  332. if (n-- == 0) {
  333. ret = chan;
  334. break; /* done */
  335. }
  336. }
  337. if (ret)
  338. break; /* done */
  339. }
  340. if (!ret)
  341. ret = min;
  342. if (ret)
  343. ret->table_count++;
  344. return ret;
  345. }
  346. /**
  347. * dma_channel_rebalance - redistribute the available channels
  348. *
  349. * Optimize for cpu isolation (each cpu gets a dedicated channel for an
  350. * operation type) in the SMP case, and operation isolation (avoid
  351. * multi-tasking channels) in the non-SMP case. Must be called under
  352. * dma_list_mutex.
  353. */
  354. static void dma_channel_rebalance(void)
  355. {
  356. struct dma_chan *chan;
  357. struct dma_device *device;
  358. int cpu;
  359. int cap;
  360. int n;
  361. /* undo the last distribution */
  362. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  363. for_each_possible_cpu(cpu)
  364. per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
  365. list_for_each_entry(device, &dma_device_list, global_node) {
  366. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  367. continue;
  368. list_for_each_entry(chan, &device->channels, device_node)
  369. chan->table_count = 0;
  370. }
  371. /* don't populate the channel_table if no clients are available */
  372. if (!dmaengine_ref_count)
  373. return;
  374. /* redistribute available channels */
  375. n = 0;
  376. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  377. for_each_online_cpu(cpu) {
  378. if (num_possible_cpus() > 1)
  379. chan = nth_chan(cap, n++);
  380. else
  381. chan = nth_chan(cap, -1);
  382. per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
  383. }
  384. }
  385. static struct dma_chan *private_candidate(dma_cap_mask_t *mask, struct dma_device *dev,
  386. dma_filter_fn fn, void *fn_param)
  387. {
  388. struct dma_chan *chan;
  389. if (!__dma_device_satisfies_mask(dev, mask)) {
  390. pr_debug("%s: wrong capabilities\n", __func__);
  391. return NULL;
  392. }
  393. /* devices with multiple channels need special handling as we need to
  394. * ensure that all channels are either private or public.
  395. */
  396. if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
  397. list_for_each_entry(chan, &dev->channels, device_node) {
  398. /* some channels are already publicly allocated */
  399. if (chan->client_count)
  400. return NULL;
  401. }
  402. list_for_each_entry(chan, &dev->channels, device_node) {
  403. if (chan->client_count) {
  404. pr_debug("%s: %s busy\n",
  405. __func__, dma_chan_name(chan));
  406. continue;
  407. }
  408. if (fn && !fn(chan, fn_param)) {
  409. pr_debug("%s: %s filter said false\n",
  410. __func__, dma_chan_name(chan));
  411. continue;
  412. }
  413. return chan;
  414. }
  415. return NULL;
  416. }
  417. /**
  418. * dma_request_channel - try to allocate an exclusive channel
  419. * @mask: capabilities that the channel must satisfy
  420. * @fn: optional callback to disposition available channels
  421. * @fn_param: opaque parameter to pass to dma_filter_fn
  422. */
  423. struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param)
  424. {
  425. struct dma_device *device, *_d;
  426. struct dma_chan *chan = NULL;
  427. int err;
  428. /* Find a channel */
  429. mutex_lock(&dma_list_mutex);
  430. list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
  431. chan = private_candidate(mask, device, fn, fn_param);
  432. if (chan) {
  433. /* Found a suitable channel, try to grab, prep, and
  434. * return it. We first set DMA_PRIVATE to disable
  435. * balance_ref_count as this channel will not be
  436. * published in the general-purpose allocator
  437. */
  438. dma_cap_set(DMA_PRIVATE, device->cap_mask);
  439. device->privatecnt++;
  440. err = dma_chan_get(chan);
  441. if (err == -ENODEV) {
  442. pr_debug("%s: %s module removed\n", __func__,
  443. dma_chan_name(chan));
  444. list_del_rcu(&device->global_node);
  445. } else if (err)
  446. pr_err("dmaengine: failed to get %s: (%d)\n",
  447. dma_chan_name(chan), err);
  448. else
  449. break;
  450. if (--device->privatecnt == 0)
  451. dma_cap_clear(DMA_PRIVATE, device->cap_mask);
  452. chan = NULL;
  453. }
  454. }
  455. mutex_unlock(&dma_list_mutex);
  456. pr_debug("%s: %s (%s)\n", __func__, chan ? "success" : "fail",
  457. chan ? dma_chan_name(chan) : NULL);
  458. return chan;
  459. }
  460. EXPORT_SYMBOL_GPL(__dma_request_channel);
  461. void dma_release_channel(struct dma_chan *chan)
  462. {
  463. mutex_lock(&dma_list_mutex);
  464. WARN_ONCE(chan->client_count != 1,
  465. "chan reference count %d != 1\n", chan->client_count);
  466. dma_chan_put(chan);
  467. /* drop PRIVATE cap enabled by __dma_request_channel() */
  468. if (--chan->device->privatecnt == 0)
  469. dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
  470. mutex_unlock(&dma_list_mutex);
  471. }
  472. EXPORT_SYMBOL_GPL(dma_release_channel);
  473. /**
  474. * dmaengine_get - register interest in dma_channels
  475. */
  476. void dmaengine_get(void)
  477. {
  478. struct dma_device *device, *_d;
  479. struct dma_chan *chan;
  480. int err;
  481. mutex_lock(&dma_list_mutex);
  482. dmaengine_ref_count++;
  483. /* try to grab channels */
  484. list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
  485. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  486. continue;
  487. list_for_each_entry(chan, &device->channels, device_node) {
  488. err = dma_chan_get(chan);
  489. if (err == -ENODEV) {
  490. /* module removed before we could use it */
  491. list_del_rcu(&device->global_node);
  492. break;
  493. } else if (err)
  494. pr_err("dmaengine: failed to get %s: (%d)\n",
  495. dma_chan_name(chan), err);
  496. }
  497. }
  498. /* if this is the first reference and there were channels
  499. * waiting we need to rebalance to get those channels
  500. * incorporated into the channel table
  501. */
  502. if (dmaengine_ref_count == 1)
  503. dma_channel_rebalance();
  504. mutex_unlock(&dma_list_mutex);
  505. }
  506. EXPORT_SYMBOL(dmaengine_get);
  507. /**
  508. * dmaengine_put - let dma drivers be removed when ref_count == 0
  509. */
  510. void dmaengine_put(void)
  511. {
  512. struct dma_device *device;
  513. struct dma_chan *chan;
  514. mutex_lock(&dma_list_mutex);
  515. dmaengine_ref_count--;
  516. BUG_ON(dmaengine_ref_count < 0);
  517. /* drop channel references */
  518. list_for_each_entry(device, &dma_device_list, global_node) {
  519. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  520. continue;
  521. list_for_each_entry(chan, &device->channels, device_node)
  522. dma_chan_put(chan);
  523. }
  524. mutex_unlock(&dma_list_mutex);
  525. }
  526. EXPORT_SYMBOL(dmaengine_put);
  527. static bool device_has_all_tx_types(struct dma_device *device)
  528. {
  529. /* A device that satisfies this test has channels that will never cause
  530. * an async_tx channel switch event as all possible operation types can
  531. * be handled.
  532. */
  533. #ifdef CONFIG_ASYNC_TX_DMA
  534. if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
  535. return false;
  536. #endif
  537. #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE)
  538. if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
  539. return false;
  540. #endif
  541. #if defined(CONFIG_ASYNC_MEMSET) || defined(CONFIG_ASYNC_MEMSET_MODULE)
  542. if (!dma_has_cap(DMA_MEMSET, device->cap_mask))
  543. return false;
  544. #endif
  545. #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE)
  546. if (!dma_has_cap(DMA_XOR, device->cap_mask))
  547. return false;
  548. #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  549. if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
  550. return false;
  551. #endif
  552. #endif
  553. #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE)
  554. if (!dma_has_cap(DMA_PQ, device->cap_mask))
  555. return false;
  556. #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  557. if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
  558. return false;
  559. #endif
  560. #endif
  561. return true;
  562. }
  563. static int get_dma_id(struct dma_device *device)
  564. {
  565. int rc;
  566. idr_retry:
  567. if (!idr_pre_get(&dma_idr, GFP_KERNEL))
  568. return -ENOMEM;
  569. mutex_lock(&dma_list_mutex);
  570. rc = idr_get_new(&dma_idr, NULL, &device->dev_id);
  571. mutex_unlock(&dma_list_mutex);
  572. if (rc == -EAGAIN)
  573. goto idr_retry;
  574. else if (rc != 0)
  575. return rc;
  576. return 0;
  577. }
  578. /**
  579. * dma_async_device_register - registers DMA devices found
  580. * @device: &dma_device
  581. */
  582. int dma_async_device_register(struct dma_device *device)
  583. {
  584. int chancnt = 0, rc;
  585. struct dma_chan* chan;
  586. atomic_t *idr_ref;
  587. if (!device)
  588. return -ENODEV;
  589. /* validate device routines */
  590. BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
  591. !device->device_prep_dma_memcpy);
  592. BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
  593. !device->device_prep_dma_xor);
  594. BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) &&
  595. !device->device_prep_dma_xor_val);
  596. BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) &&
  597. !device->device_prep_dma_pq);
  598. BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) &&
  599. !device->device_prep_dma_pq_val);
  600. BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
  601. !device->device_prep_dma_memset);
  602. BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
  603. !device->device_prep_dma_interrupt);
  604. BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) &&
  605. !device->device_prep_dma_sg);
  606. BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
  607. !device->device_prep_slave_sg);
  608. BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) &&
  609. !device->device_prep_dma_cyclic);
  610. BUG_ON(dma_has_cap(DMA_SLAVE, device->cap_mask) &&
  611. !device->device_control);
  612. BUG_ON(!device->device_alloc_chan_resources);
  613. BUG_ON(!device->device_free_chan_resources);
  614. BUG_ON(!device->device_tx_status);
  615. BUG_ON(!device->device_issue_pending);
  616. BUG_ON(!device->dev);
  617. /* note: this only matters in the
  618. * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
  619. */
  620. if (device_has_all_tx_types(device))
  621. dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
  622. idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
  623. if (!idr_ref)
  624. return -ENOMEM;
  625. rc = get_dma_id(device);
  626. if (rc != 0) {
  627. kfree(idr_ref);
  628. return rc;
  629. }
  630. atomic_set(idr_ref, 0);
  631. /* represent channels in sysfs. Probably want devs too */
  632. list_for_each_entry(chan, &device->channels, device_node) {
  633. rc = -ENOMEM;
  634. chan->local = alloc_percpu(typeof(*chan->local));
  635. if (chan->local == NULL)
  636. goto err_out;
  637. chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
  638. if (chan->dev == NULL) {
  639. free_percpu(chan->local);
  640. chan->local = NULL;
  641. goto err_out;
  642. }
  643. chan->chan_id = chancnt++;
  644. chan->dev->device.class = &dma_devclass;
  645. chan->dev->device.parent = device->dev;
  646. chan->dev->chan = chan;
  647. chan->dev->idr_ref = idr_ref;
  648. chan->dev->dev_id = device->dev_id;
  649. atomic_inc(idr_ref);
  650. dev_set_name(&chan->dev->device, "dma%dchan%d",
  651. device->dev_id, chan->chan_id);
  652. rc = device_register(&chan->dev->device);
  653. if (rc) {
  654. free_percpu(chan->local);
  655. chan->local = NULL;
  656. kfree(chan->dev);
  657. atomic_dec(idr_ref);
  658. goto err_out;
  659. }
  660. chan->client_count = 0;
  661. }
  662. device->chancnt = chancnt;
  663. mutex_lock(&dma_list_mutex);
  664. /* take references on public channels */
  665. if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
  666. list_for_each_entry(chan, &device->channels, device_node) {
  667. /* if clients are already waiting for channels we need
  668. * to take references on their behalf
  669. */
  670. if (dma_chan_get(chan) == -ENODEV) {
  671. /* note we can only get here for the first
  672. * channel as the remaining channels are
  673. * guaranteed to get a reference
  674. */
  675. rc = -ENODEV;
  676. mutex_unlock(&dma_list_mutex);
  677. goto err_out;
  678. }
  679. }
  680. list_add_tail_rcu(&device->global_node, &dma_device_list);
  681. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  682. device->privatecnt++; /* Always private */
  683. dma_channel_rebalance();
  684. mutex_unlock(&dma_list_mutex);
  685. return 0;
  686. err_out:
  687. /* if we never registered a channel just release the idr */
  688. if (atomic_read(idr_ref) == 0) {
  689. mutex_lock(&dma_list_mutex);
  690. idr_remove(&dma_idr, device->dev_id);
  691. mutex_unlock(&dma_list_mutex);
  692. kfree(idr_ref);
  693. return rc;
  694. }
  695. list_for_each_entry(chan, &device->channels, device_node) {
  696. if (chan->local == NULL)
  697. continue;
  698. mutex_lock(&dma_list_mutex);
  699. chan->dev->chan = NULL;
  700. mutex_unlock(&dma_list_mutex);
  701. device_unregister(&chan->dev->device);
  702. free_percpu(chan->local);
  703. }
  704. return rc;
  705. }
  706. EXPORT_SYMBOL(dma_async_device_register);
  707. /**
  708. * dma_async_device_unregister - unregister a DMA device
  709. * @device: &dma_device
  710. *
  711. * This routine is called by dma driver exit routines, dmaengine holds module
  712. * references to prevent it being called while channels are in use.
  713. */
  714. void dma_async_device_unregister(struct dma_device *device)
  715. {
  716. struct dma_chan *chan;
  717. mutex_lock(&dma_list_mutex);
  718. list_del_rcu(&device->global_node);
  719. dma_channel_rebalance();
  720. mutex_unlock(&dma_list_mutex);
  721. list_for_each_entry(chan, &device->channels, device_node) {
  722. WARN_ONCE(chan->client_count,
  723. "%s called while %d clients hold a reference\n",
  724. __func__, chan->client_count);
  725. mutex_lock(&dma_list_mutex);
  726. chan->dev->chan = NULL;
  727. mutex_unlock(&dma_list_mutex);
  728. device_unregister(&chan->dev->device);
  729. free_percpu(chan->local);
  730. }
  731. }
  732. EXPORT_SYMBOL(dma_async_device_unregister);
  733. /**
  734. * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
  735. * @chan: DMA channel to offload copy to
  736. * @dest: destination address (virtual)
  737. * @src: source address (virtual)
  738. * @len: length
  739. *
  740. * Both @dest and @src must be mappable to a bus address according to the
  741. * DMA mapping API rules for streaming mappings.
  742. * Both @dest and @src must stay memory resident (kernel memory or locked
  743. * user space pages).
  744. */
  745. dma_cookie_t
  746. dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest,
  747. void *src, size_t len)
  748. {
  749. struct dma_device *dev = chan->device;
  750. struct dma_async_tx_descriptor *tx;
  751. dma_addr_t dma_dest, dma_src;
  752. dma_cookie_t cookie;
  753. unsigned long flags;
  754. dma_src = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE);
  755. dma_dest = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE);
  756. flags = DMA_CTRL_ACK |
  757. DMA_COMPL_SRC_UNMAP_SINGLE |
  758. DMA_COMPL_DEST_UNMAP_SINGLE;
  759. tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
  760. if (!tx) {
  761. dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
  762. dma_unmap_single(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
  763. return -ENOMEM;
  764. }
  765. tx->callback = NULL;
  766. cookie = tx->tx_submit(tx);
  767. preempt_disable();
  768. __this_cpu_add(chan->local->bytes_transferred, len);
  769. __this_cpu_inc(chan->local->memcpy_count);
  770. preempt_enable();
  771. return cookie;
  772. }
  773. EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf);
  774. /**
  775. * dma_async_memcpy_buf_to_pg - offloaded copy from address to page
  776. * @chan: DMA channel to offload copy to
  777. * @page: destination page
  778. * @offset: offset in page to copy to
  779. * @kdata: source address (virtual)
  780. * @len: length
  781. *
  782. * Both @page/@offset and @kdata must be mappable to a bus address according
  783. * to the DMA mapping API rules for streaming mappings.
  784. * Both @page/@offset and @kdata must stay memory resident (kernel memory or
  785. * locked user space pages)
  786. */
  787. dma_cookie_t
  788. dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page,
  789. unsigned int offset, void *kdata, size_t len)
  790. {
  791. struct dma_device *dev = chan->device;
  792. struct dma_async_tx_descriptor *tx;
  793. dma_addr_t dma_dest, dma_src;
  794. dma_cookie_t cookie;
  795. unsigned long flags;
  796. dma_src = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE);
  797. dma_dest = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE);
  798. flags = DMA_CTRL_ACK | DMA_COMPL_SRC_UNMAP_SINGLE;
  799. tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
  800. if (!tx) {
  801. dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
  802. dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
  803. return -ENOMEM;
  804. }
  805. tx->callback = NULL;
  806. cookie = tx->tx_submit(tx);
  807. preempt_disable();
  808. __this_cpu_add(chan->local->bytes_transferred, len);
  809. __this_cpu_inc(chan->local->memcpy_count);
  810. preempt_enable();
  811. return cookie;
  812. }
  813. EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg);
  814. /**
  815. * dma_async_memcpy_pg_to_pg - offloaded copy from page to page
  816. * @chan: DMA channel to offload copy to
  817. * @dest_pg: destination page
  818. * @dest_off: offset in page to copy to
  819. * @src_pg: source page
  820. * @src_off: offset in page to copy from
  821. * @len: length
  822. *
  823. * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
  824. * address according to the DMA mapping API rules for streaming mappings.
  825. * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
  826. * (kernel memory or locked user space pages).
  827. */
  828. dma_cookie_t
  829. dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg,
  830. unsigned int dest_off, struct page *src_pg, unsigned int src_off,
  831. size_t len)
  832. {
  833. struct dma_device *dev = chan->device;
  834. struct dma_async_tx_descriptor *tx;
  835. dma_addr_t dma_dest, dma_src;
  836. dma_cookie_t cookie;
  837. unsigned long flags;
  838. dma_src = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE);
  839. dma_dest = dma_map_page(dev->dev, dest_pg, dest_off, len,
  840. DMA_FROM_DEVICE);
  841. flags = DMA_CTRL_ACK;
  842. tx = dev->device_prep_dma_memcpy(chan, dma_dest, dma_src, len, flags);
  843. if (!tx) {
  844. dma_unmap_page(dev->dev, dma_src, len, DMA_TO_DEVICE);
  845. dma_unmap_page(dev->dev, dma_dest, len, DMA_FROM_DEVICE);
  846. return -ENOMEM;
  847. }
  848. tx->callback = NULL;
  849. cookie = tx->tx_submit(tx);
  850. preempt_disable();
  851. __this_cpu_add(chan->local->bytes_transferred, len);
  852. __this_cpu_inc(chan->local->memcpy_count);
  853. preempt_enable();
  854. return cookie;
  855. }
  856. EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg);
  857. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  858. struct dma_chan *chan)
  859. {
  860. tx->chan = chan;
  861. #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  862. spin_lock_init(&tx->lock);
  863. #endif
  864. }
  865. EXPORT_SYMBOL(dma_async_tx_descriptor_init);
  866. /* dma_wait_for_async_tx - spin wait for a transaction to complete
  867. * @tx: in-flight transaction to wait on
  868. */
  869. enum dma_status
  870. dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  871. {
  872. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  873. if (!tx)
  874. return DMA_SUCCESS;
  875. while (tx->cookie == -EBUSY) {
  876. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  877. pr_err("%s timeout waiting for descriptor submission\n",
  878. __func__);
  879. return DMA_ERROR;
  880. }
  881. cpu_relax();
  882. }
  883. return dma_sync_wait(tx->chan, tx->cookie);
  884. }
  885. EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
  886. /* dma_run_dependencies - helper routine for dma drivers to process
  887. * (start) dependent operations on their target channel
  888. * @tx: transaction with dependencies
  889. */
  890. void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
  891. {
  892. struct dma_async_tx_descriptor *dep = txd_next(tx);
  893. struct dma_async_tx_descriptor *dep_next;
  894. struct dma_chan *chan;
  895. if (!dep)
  896. return;
  897. /* we'll submit tx->next now, so clear the link */
  898. txd_clear_next(tx);
  899. chan = dep->chan;
  900. /* keep submitting up until a channel switch is detected
  901. * in that case we will be called again as a result of
  902. * processing the interrupt from async_tx_channel_switch
  903. */
  904. for (; dep; dep = dep_next) {
  905. txd_lock(dep);
  906. txd_clear_parent(dep);
  907. dep_next = txd_next(dep);
  908. if (dep_next && dep_next->chan == chan)
  909. txd_clear_next(dep); /* ->next will be submitted */
  910. else
  911. dep_next = NULL; /* submit current dep and terminate */
  912. txd_unlock(dep);
  913. dep->tx_submit(dep);
  914. }
  915. chan->device->device_issue_pending(chan);
  916. }
  917. EXPORT_SYMBOL_GPL(dma_run_dependencies);
  918. static int __init dma_bus_init(void)
  919. {
  920. idr_init(&dma_idr);
  921. mutex_init(&dma_list_mutex);
  922. return class_register(&dma_devclass);
  923. }
  924. arch_initcall(dma_bus_init);