dmaengine.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. * The full GNU General Public License is included in this distribution in the
  15. * file called COPYING.
  16. */
  17. /*
  18. * This code implements the DMA subsystem. It provides a HW-neutral interface
  19. * for other kernel code to use asynchronous memory copy capabilities,
  20. * if present, and allows different HW DMA drivers to register as providing
  21. * this capability.
  22. *
  23. * Due to the fact we are accelerating what is already a relatively fast
  24. * operation, the code goes to great lengths to avoid additional overhead,
  25. * such as locking.
  26. *
  27. * LOCKING:
  28. *
  29. * The subsystem keeps a global list of dma_device structs it is protected by a
  30. * mutex, dma_list_mutex.
  31. *
  32. * A subsystem can get access to a channel by calling dmaengine_get() followed
  33. * by dma_find_channel(), or if it has need for an exclusive channel it can call
  34. * dma_request_channel(). Once a channel is allocated a reference is taken
  35. * against its corresponding driver to disable removal.
  36. *
  37. * Each device has a channels list, which runs unlocked but is never modified
  38. * once the device is registered, it's just setup by the driver.
  39. *
  40. * See Documentation/dmaengine.txt for more details
  41. */
  42. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  43. #include <linux/dma-mapping.h>
  44. #include <linux/init.h>
  45. #include <linux/module.h>
  46. #include <linux/mm.h>
  47. #include <linux/device.h>
  48. #include <linux/dmaengine.h>
  49. #include <linux/hardirq.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/percpu.h>
  52. #include <linux/rcupdate.h>
  53. #include <linux/mutex.h>
  54. #include <linux/jiffies.h>
  55. #include <linux/rculist.h>
  56. #include <linux/idr.h>
  57. #include <linux/slab.h>
  58. #include <linux/acpi.h>
  59. #include <linux/acpi_dma.h>
  60. #include <linux/of_dma.h>
  61. #include <linux/mempool.h>
  62. static DEFINE_MUTEX(dma_list_mutex);
  63. static DEFINE_IDR(dma_idr);
  64. static LIST_HEAD(dma_device_list);
  65. static long dmaengine_ref_count;
  66. /* --- sysfs implementation --- */
  67. /**
  68. * dev_to_dma_chan - convert a device pointer to the its sysfs container object
  69. * @dev - device node
  70. *
  71. * Must be called under dma_list_mutex
  72. */
  73. static struct dma_chan *dev_to_dma_chan(struct device *dev)
  74. {
  75. struct dma_chan_dev *chan_dev;
  76. chan_dev = container_of(dev, typeof(*chan_dev), device);
  77. return chan_dev->chan;
  78. }
  79. static ssize_t memcpy_count_show(struct device *dev,
  80. struct device_attribute *attr, char *buf)
  81. {
  82. struct dma_chan *chan;
  83. unsigned long count = 0;
  84. int i;
  85. int err;
  86. mutex_lock(&dma_list_mutex);
  87. chan = dev_to_dma_chan(dev);
  88. if (chan) {
  89. for_each_possible_cpu(i)
  90. count += per_cpu_ptr(chan->local, i)->memcpy_count;
  91. err = sprintf(buf, "%lu\n", count);
  92. } else
  93. err = -ENODEV;
  94. mutex_unlock(&dma_list_mutex);
  95. return err;
  96. }
  97. static DEVICE_ATTR_RO(memcpy_count);
  98. static ssize_t bytes_transferred_show(struct device *dev,
  99. struct device_attribute *attr, char *buf)
  100. {
  101. struct dma_chan *chan;
  102. unsigned long count = 0;
  103. int i;
  104. int err;
  105. mutex_lock(&dma_list_mutex);
  106. chan = dev_to_dma_chan(dev);
  107. if (chan) {
  108. for_each_possible_cpu(i)
  109. count += per_cpu_ptr(chan->local, i)->bytes_transferred;
  110. err = sprintf(buf, "%lu\n", count);
  111. } else
  112. err = -ENODEV;
  113. mutex_unlock(&dma_list_mutex);
  114. return err;
  115. }
  116. static DEVICE_ATTR_RO(bytes_transferred);
  117. static ssize_t in_use_show(struct device *dev, struct device_attribute *attr,
  118. char *buf)
  119. {
  120. struct dma_chan *chan;
  121. int err;
  122. mutex_lock(&dma_list_mutex);
  123. chan = dev_to_dma_chan(dev);
  124. if (chan)
  125. err = sprintf(buf, "%d\n", chan->client_count);
  126. else
  127. err = -ENODEV;
  128. mutex_unlock(&dma_list_mutex);
  129. return err;
  130. }
  131. static DEVICE_ATTR_RO(in_use);
  132. static struct attribute *dma_dev_attrs[] = {
  133. &dev_attr_memcpy_count.attr,
  134. &dev_attr_bytes_transferred.attr,
  135. &dev_attr_in_use.attr,
  136. NULL,
  137. };
  138. ATTRIBUTE_GROUPS(dma_dev);
  139. static void chan_dev_release(struct device *dev)
  140. {
  141. struct dma_chan_dev *chan_dev;
  142. chan_dev = container_of(dev, typeof(*chan_dev), device);
  143. if (atomic_dec_and_test(chan_dev->idr_ref)) {
  144. mutex_lock(&dma_list_mutex);
  145. idr_remove(&dma_idr, chan_dev->dev_id);
  146. mutex_unlock(&dma_list_mutex);
  147. kfree(chan_dev->idr_ref);
  148. }
  149. kfree(chan_dev);
  150. }
  151. static struct class dma_devclass = {
  152. .name = "dma",
  153. .dev_groups = dma_dev_groups,
  154. .dev_release = chan_dev_release,
  155. };
  156. /* --- client and device registration --- */
  157. #define dma_device_satisfies_mask(device, mask) \
  158. __dma_device_satisfies_mask((device), &(mask))
  159. static int
  160. __dma_device_satisfies_mask(struct dma_device *device,
  161. const dma_cap_mask_t *want)
  162. {
  163. dma_cap_mask_t has;
  164. bitmap_and(has.bits, want->bits, device->cap_mask.bits,
  165. DMA_TX_TYPE_END);
  166. return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END);
  167. }
  168. static struct module *dma_chan_to_owner(struct dma_chan *chan)
  169. {
  170. return chan->device->dev->driver->owner;
  171. }
  172. /**
  173. * balance_ref_count - catch up the channel reference count
  174. * @chan - channel to balance ->client_count versus dmaengine_ref_count
  175. *
  176. * balance_ref_count must be called under dma_list_mutex
  177. */
  178. static void balance_ref_count(struct dma_chan *chan)
  179. {
  180. struct module *owner = dma_chan_to_owner(chan);
  181. while (chan->client_count < dmaengine_ref_count) {
  182. __module_get(owner);
  183. chan->client_count++;
  184. }
  185. }
  186. /**
  187. * dma_chan_get - try to grab a dma channel's parent driver module
  188. * @chan - channel to grab
  189. *
  190. * Must be called under dma_list_mutex
  191. */
  192. static int dma_chan_get(struct dma_chan *chan)
  193. {
  194. struct module *owner = dma_chan_to_owner(chan);
  195. int ret;
  196. /* The channel is already in use, update client count */
  197. if (chan->client_count) {
  198. __module_get(owner);
  199. goto out;
  200. }
  201. if (!try_module_get(owner))
  202. return -ENODEV;
  203. /* allocate upon first client reference */
  204. if (chan->device->device_alloc_chan_resources) {
  205. ret = chan->device->device_alloc_chan_resources(chan);
  206. if (ret < 0)
  207. goto err_out;
  208. }
  209. if (!dma_has_cap(DMA_PRIVATE, chan->device->cap_mask))
  210. balance_ref_count(chan);
  211. out:
  212. chan->client_count++;
  213. return 0;
  214. err_out:
  215. module_put(owner);
  216. return ret;
  217. }
  218. /**
  219. * dma_chan_put - drop a reference to a dma channel's parent driver module
  220. * @chan - channel to release
  221. *
  222. * Must be called under dma_list_mutex
  223. */
  224. static void dma_chan_put(struct dma_chan *chan)
  225. {
  226. /* This channel is not in use, bail out */
  227. if (!chan->client_count)
  228. return;
  229. chan->client_count--;
  230. module_put(dma_chan_to_owner(chan));
  231. /* This channel is not in use anymore, free it */
  232. if (!chan->client_count && chan->device->device_free_chan_resources)
  233. chan->device->device_free_chan_resources(chan);
  234. }
  235. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  236. {
  237. enum dma_status status;
  238. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  239. dma_async_issue_pending(chan);
  240. do {
  241. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  242. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  243. pr_err("%s: timeout!\n", __func__);
  244. return DMA_ERROR;
  245. }
  246. if (status != DMA_IN_PROGRESS)
  247. break;
  248. cpu_relax();
  249. } while (1);
  250. return status;
  251. }
  252. EXPORT_SYMBOL(dma_sync_wait);
  253. /**
  254. * dma_cap_mask_all - enable iteration over all operation types
  255. */
  256. static dma_cap_mask_t dma_cap_mask_all;
  257. /**
  258. * dma_chan_tbl_ent - tracks channel allocations per core/operation
  259. * @chan - associated channel for this entry
  260. */
  261. struct dma_chan_tbl_ent {
  262. struct dma_chan *chan;
  263. };
  264. /**
  265. * channel_table - percpu lookup table for memory-to-memory offload providers
  266. */
  267. static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
  268. static int __init dma_channel_table_init(void)
  269. {
  270. enum dma_transaction_type cap;
  271. int err = 0;
  272. bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
  273. /* 'interrupt', 'private', and 'slave' are channel capabilities,
  274. * but are not associated with an operation so they do not need
  275. * an entry in the channel_table
  276. */
  277. clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
  278. clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
  279. clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
  280. for_each_dma_cap_mask(cap, dma_cap_mask_all) {
  281. channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
  282. if (!channel_table[cap]) {
  283. err = -ENOMEM;
  284. break;
  285. }
  286. }
  287. if (err) {
  288. pr_err("initialization failure\n");
  289. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  290. free_percpu(channel_table[cap]);
  291. }
  292. return err;
  293. }
  294. arch_initcall(dma_channel_table_init);
  295. /**
  296. * dma_find_channel - find a channel to carry out the operation
  297. * @tx_type: transaction type
  298. */
  299. struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
  300. {
  301. return this_cpu_read(channel_table[tx_type]->chan);
  302. }
  303. EXPORT_SYMBOL(dma_find_channel);
  304. /**
  305. * dma_issue_pending_all - flush all pending operations across all channels
  306. */
  307. void dma_issue_pending_all(void)
  308. {
  309. struct dma_device *device;
  310. struct dma_chan *chan;
  311. rcu_read_lock();
  312. list_for_each_entry_rcu(device, &dma_device_list, global_node) {
  313. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  314. continue;
  315. list_for_each_entry(chan, &device->channels, device_node)
  316. if (chan->client_count)
  317. device->device_issue_pending(chan);
  318. }
  319. rcu_read_unlock();
  320. }
  321. EXPORT_SYMBOL(dma_issue_pending_all);
  322. /**
  323. * dma_chan_is_local - returns true if the channel is in the same numa-node as the cpu
  324. */
  325. static bool dma_chan_is_local(struct dma_chan *chan, int cpu)
  326. {
  327. int node = dev_to_node(chan->device->dev);
  328. return node == -1 || cpumask_test_cpu(cpu, cpumask_of_node(node));
  329. }
  330. /**
  331. * min_chan - returns the channel with min count and in the same numa-node as the cpu
  332. * @cap: capability to match
  333. * @cpu: cpu index which the channel should be close to
  334. *
  335. * If some channels are close to the given cpu, the one with the lowest
  336. * reference count is returned. Otherwise, cpu is ignored and only the
  337. * reference count is taken into account.
  338. * Must be called under dma_list_mutex.
  339. */
  340. static struct dma_chan *min_chan(enum dma_transaction_type cap, int cpu)
  341. {
  342. struct dma_device *device;
  343. struct dma_chan *chan;
  344. struct dma_chan *min = NULL;
  345. struct dma_chan *localmin = NULL;
  346. list_for_each_entry(device, &dma_device_list, global_node) {
  347. if (!dma_has_cap(cap, device->cap_mask) ||
  348. dma_has_cap(DMA_PRIVATE, device->cap_mask))
  349. continue;
  350. list_for_each_entry(chan, &device->channels, device_node) {
  351. if (!chan->client_count)
  352. continue;
  353. if (!min || chan->table_count < min->table_count)
  354. min = chan;
  355. if (dma_chan_is_local(chan, cpu))
  356. if (!localmin ||
  357. chan->table_count < localmin->table_count)
  358. localmin = chan;
  359. }
  360. }
  361. chan = localmin ? localmin : min;
  362. if (chan)
  363. chan->table_count++;
  364. return chan;
  365. }
  366. /**
  367. * dma_channel_rebalance - redistribute the available channels
  368. *
  369. * Optimize for cpu isolation (each cpu gets a dedicated channel for an
  370. * operation type) in the SMP case, and operation isolation (avoid
  371. * multi-tasking channels) in the non-SMP case. Must be called under
  372. * dma_list_mutex.
  373. */
  374. static void dma_channel_rebalance(void)
  375. {
  376. struct dma_chan *chan;
  377. struct dma_device *device;
  378. int cpu;
  379. int cap;
  380. /* undo the last distribution */
  381. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  382. for_each_possible_cpu(cpu)
  383. per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
  384. list_for_each_entry(device, &dma_device_list, global_node) {
  385. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  386. continue;
  387. list_for_each_entry(chan, &device->channels, device_node)
  388. chan->table_count = 0;
  389. }
  390. /* don't populate the channel_table if no clients are available */
  391. if (!dmaengine_ref_count)
  392. return;
  393. /* redistribute available channels */
  394. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  395. for_each_online_cpu(cpu) {
  396. chan = min_chan(cap, cpu);
  397. per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
  398. }
  399. }
  400. int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps)
  401. {
  402. struct dma_device *device;
  403. if (!chan || !caps)
  404. return -EINVAL;
  405. device = chan->device;
  406. /* check if the channel supports slave transactions */
  407. if (!test_bit(DMA_SLAVE, device->cap_mask.bits))
  408. return -ENXIO;
  409. /*
  410. * Check whether it reports it uses the generic slave
  411. * capabilities, if not, that means it doesn't support any
  412. * kind of slave capabilities reporting.
  413. */
  414. if (!device->directions)
  415. return -ENXIO;
  416. caps->src_addr_widths = device->src_addr_widths;
  417. caps->dst_addr_widths = device->dst_addr_widths;
  418. caps->directions = device->directions;
  419. caps->residue_granularity = device->residue_granularity;
  420. caps->cmd_pause = !!device->device_pause;
  421. caps->cmd_terminate = !!device->device_terminate_all;
  422. return 0;
  423. }
  424. EXPORT_SYMBOL_GPL(dma_get_slave_caps);
  425. static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
  426. struct dma_device *dev,
  427. dma_filter_fn fn, void *fn_param)
  428. {
  429. struct dma_chan *chan;
  430. if (!__dma_device_satisfies_mask(dev, mask)) {
  431. pr_debug("%s: wrong capabilities\n", __func__);
  432. return NULL;
  433. }
  434. /* devices with multiple channels need special handling as we need to
  435. * ensure that all channels are either private or public.
  436. */
  437. if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
  438. list_for_each_entry(chan, &dev->channels, device_node) {
  439. /* some channels are already publicly allocated */
  440. if (chan->client_count)
  441. return NULL;
  442. }
  443. list_for_each_entry(chan, &dev->channels, device_node) {
  444. if (chan->client_count) {
  445. pr_debug("%s: %s busy\n",
  446. __func__, dma_chan_name(chan));
  447. continue;
  448. }
  449. if (fn && !fn(chan, fn_param)) {
  450. pr_debug("%s: %s filter said false\n",
  451. __func__, dma_chan_name(chan));
  452. continue;
  453. }
  454. return chan;
  455. }
  456. return NULL;
  457. }
  458. /**
  459. * dma_get_slave_channel - try to get specific channel exclusively
  460. * @chan: target channel
  461. */
  462. struct dma_chan *dma_get_slave_channel(struct dma_chan *chan)
  463. {
  464. int err = -EBUSY;
  465. /* lock against __dma_request_channel */
  466. mutex_lock(&dma_list_mutex);
  467. if (chan->client_count == 0) {
  468. err = dma_chan_get(chan);
  469. if (err)
  470. pr_debug("%s: failed to get %s: (%d)\n",
  471. __func__, dma_chan_name(chan), err);
  472. } else
  473. chan = NULL;
  474. mutex_unlock(&dma_list_mutex);
  475. return chan;
  476. }
  477. EXPORT_SYMBOL_GPL(dma_get_slave_channel);
  478. struct dma_chan *dma_get_any_slave_channel(struct dma_device *device)
  479. {
  480. dma_cap_mask_t mask;
  481. struct dma_chan *chan;
  482. int err;
  483. dma_cap_zero(mask);
  484. dma_cap_set(DMA_SLAVE, mask);
  485. /* lock against __dma_request_channel */
  486. mutex_lock(&dma_list_mutex);
  487. chan = private_candidate(&mask, device, NULL, NULL);
  488. if (chan) {
  489. dma_cap_set(DMA_PRIVATE, device->cap_mask);
  490. device->privatecnt++;
  491. err = dma_chan_get(chan);
  492. if (err) {
  493. pr_debug("%s: failed to get %s: (%d)\n",
  494. __func__, dma_chan_name(chan), err);
  495. chan = NULL;
  496. if (--device->privatecnt == 0)
  497. dma_cap_clear(DMA_PRIVATE, device->cap_mask);
  498. }
  499. }
  500. mutex_unlock(&dma_list_mutex);
  501. return chan;
  502. }
  503. EXPORT_SYMBOL_GPL(dma_get_any_slave_channel);
  504. /**
  505. * __dma_request_channel - try to allocate an exclusive channel
  506. * @mask: capabilities that the channel must satisfy
  507. * @fn: optional callback to disposition available channels
  508. * @fn_param: opaque parameter to pass to dma_filter_fn
  509. *
  510. * Returns pointer to appropriate DMA channel on success or NULL.
  511. */
  512. struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  513. dma_filter_fn fn, void *fn_param)
  514. {
  515. struct dma_device *device, *_d;
  516. struct dma_chan *chan = NULL;
  517. int err;
  518. /* Find a channel */
  519. mutex_lock(&dma_list_mutex);
  520. list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
  521. chan = private_candidate(mask, device, fn, fn_param);
  522. if (chan) {
  523. /* Found a suitable channel, try to grab, prep, and
  524. * return it. We first set DMA_PRIVATE to disable
  525. * balance_ref_count as this channel will not be
  526. * published in the general-purpose allocator
  527. */
  528. dma_cap_set(DMA_PRIVATE, device->cap_mask);
  529. device->privatecnt++;
  530. err = dma_chan_get(chan);
  531. if (err == -ENODEV) {
  532. pr_debug("%s: %s module removed\n",
  533. __func__, dma_chan_name(chan));
  534. list_del_rcu(&device->global_node);
  535. } else if (err)
  536. pr_debug("%s: failed to get %s: (%d)\n",
  537. __func__, dma_chan_name(chan), err);
  538. else
  539. break;
  540. if (--device->privatecnt == 0)
  541. dma_cap_clear(DMA_PRIVATE, device->cap_mask);
  542. chan = NULL;
  543. }
  544. }
  545. mutex_unlock(&dma_list_mutex);
  546. pr_debug("%s: %s (%s)\n",
  547. __func__,
  548. chan ? "success" : "fail",
  549. chan ? dma_chan_name(chan) : NULL);
  550. return chan;
  551. }
  552. EXPORT_SYMBOL_GPL(__dma_request_channel);
  553. /**
  554. * dma_request_slave_channel_reason - try to allocate an exclusive slave channel
  555. * @dev: pointer to client device structure
  556. * @name: slave channel name
  557. *
  558. * Returns pointer to appropriate DMA channel on success or an error pointer.
  559. */
  560. struct dma_chan *dma_request_slave_channel_reason(struct device *dev,
  561. const char *name)
  562. {
  563. /* If device-tree is present get slave info from here */
  564. if (dev->of_node)
  565. return of_dma_request_slave_channel(dev->of_node, name);
  566. /* If device was enumerated by ACPI get slave info from here */
  567. if (ACPI_HANDLE(dev))
  568. return acpi_dma_request_slave_chan_by_name(dev, name);
  569. return ERR_PTR(-ENODEV);
  570. }
  571. EXPORT_SYMBOL_GPL(dma_request_slave_channel_reason);
  572. /**
  573. * dma_request_slave_channel - try to allocate an exclusive slave channel
  574. * @dev: pointer to client device structure
  575. * @name: slave channel name
  576. *
  577. * Returns pointer to appropriate DMA channel on success or NULL.
  578. */
  579. struct dma_chan *dma_request_slave_channel(struct device *dev,
  580. const char *name)
  581. {
  582. struct dma_chan *ch = dma_request_slave_channel_reason(dev, name);
  583. if (IS_ERR(ch))
  584. return NULL;
  585. return ch;
  586. }
  587. EXPORT_SYMBOL_GPL(dma_request_slave_channel);
  588. void dma_release_channel(struct dma_chan *chan)
  589. {
  590. mutex_lock(&dma_list_mutex);
  591. WARN_ONCE(chan->client_count != 1,
  592. "chan reference count %d != 1\n", chan->client_count);
  593. dma_chan_put(chan);
  594. /* drop PRIVATE cap enabled by __dma_request_channel() */
  595. if (--chan->device->privatecnt == 0)
  596. dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
  597. mutex_unlock(&dma_list_mutex);
  598. }
  599. EXPORT_SYMBOL_GPL(dma_release_channel);
  600. /**
  601. * dmaengine_get - register interest in dma_channels
  602. */
  603. void dmaengine_get(void)
  604. {
  605. struct dma_device *device, *_d;
  606. struct dma_chan *chan;
  607. int err;
  608. mutex_lock(&dma_list_mutex);
  609. dmaengine_ref_count++;
  610. /* try to grab channels */
  611. list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
  612. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  613. continue;
  614. list_for_each_entry(chan, &device->channels, device_node) {
  615. err = dma_chan_get(chan);
  616. if (err == -ENODEV) {
  617. /* module removed before we could use it */
  618. list_del_rcu(&device->global_node);
  619. break;
  620. } else if (err)
  621. pr_debug("%s: failed to get %s: (%d)\n",
  622. __func__, dma_chan_name(chan), err);
  623. }
  624. }
  625. /* if this is the first reference and there were channels
  626. * waiting we need to rebalance to get those channels
  627. * incorporated into the channel table
  628. */
  629. if (dmaengine_ref_count == 1)
  630. dma_channel_rebalance();
  631. mutex_unlock(&dma_list_mutex);
  632. }
  633. EXPORT_SYMBOL(dmaengine_get);
  634. /**
  635. * dmaengine_put - let dma drivers be removed when ref_count == 0
  636. */
  637. void dmaengine_put(void)
  638. {
  639. struct dma_device *device;
  640. struct dma_chan *chan;
  641. mutex_lock(&dma_list_mutex);
  642. dmaengine_ref_count--;
  643. BUG_ON(dmaengine_ref_count < 0);
  644. /* drop channel references */
  645. list_for_each_entry(device, &dma_device_list, global_node) {
  646. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  647. continue;
  648. list_for_each_entry(chan, &device->channels, device_node)
  649. dma_chan_put(chan);
  650. }
  651. mutex_unlock(&dma_list_mutex);
  652. }
  653. EXPORT_SYMBOL(dmaengine_put);
  654. static bool device_has_all_tx_types(struct dma_device *device)
  655. {
  656. /* A device that satisfies this test has channels that will never cause
  657. * an async_tx channel switch event as all possible operation types can
  658. * be handled.
  659. */
  660. #ifdef CONFIG_ASYNC_TX_DMA
  661. if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
  662. return false;
  663. #endif
  664. #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE)
  665. if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
  666. return false;
  667. #endif
  668. #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE)
  669. if (!dma_has_cap(DMA_XOR, device->cap_mask))
  670. return false;
  671. #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  672. if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
  673. return false;
  674. #endif
  675. #endif
  676. #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE)
  677. if (!dma_has_cap(DMA_PQ, device->cap_mask))
  678. return false;
  679. #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  680. if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
  681. return false;
  682. #endif
  683. #endif
  684. return true;
  685. }
  686. static int get_dma_id(struct dma_device *device)
  687. {
  688. int rc;
  689. mutex_lock(&dma_list_mutex);
  690. rc = idr_alloc(&dma_idr, NULL, 0, 0, GFP_KERNEL);
  691. if (rc >= 0)
  692. device->dev_id = rc;
  693. mutex_unlock(&dma_list_mutex);
  694. return rc < 0 ? rc : 0;
  695. }
  696. /**
  697. * dma_async_device_register - registers DMA devices found
  698. * @device: &dma_device
  699. */
  700. int dma_async_device_register(struct dma_device *device)
  701. {
  702. int chancnt = 0, rc;
  703. struct dma_chan* chan;
  704. atomic_t *idr_ref;
  705. if (!device)
  706. return -ENODEV;
  707. /* validate device routines */
  708. BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
  709. !device->device_prep_dma_memcpy);
  710. BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
  711. !device->device_prep_dma_xor);
  712. BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) &&
  713. !device->device_prep_dma_xor_val);
  714. BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) &&
  715. !device->device_prep_dma_pq);
  716. BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) &&
  717. !device->device_prep_dma_pq_val);
  718. BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
  719. !device->device_prep_dma_memset);
  720. BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
  721. !device->device_prep_dma_interrupt);
  722. BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) &&
  723. !device->device_prep_dma_sg);
  724. BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) &&
  725. !device->device_prep_dma_cyclic);
  726. BUG_ON(dma_has_cap(DMA_INTERLEAVE, device->cap_mask) &&
  727. !device->device_prep_interleaved_dma);
  728. BUG_ON(!device->device_tx_status);
  729. BUG_ON(!device->device_issue_pending);
  730. BUG_ON(!device->dev);
  731. /* note: this only matters in the
  732. * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
  733. */
  734. if (device_has_all_tx_types(device))
  735. dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
  736. idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
  737. if (!idr_ref)
  738. return -ENOMEM;
  739. rc = get_dma_id(device);
  740. if (rc != 0) {
  741. kfree(idr_ref);
  742. return rc;
  743. }
  744. atomic_set(idr_ref, 0);
  745. /* represent channels in sysfs. Probably want devs too */
  746. list_for_each_entry(chan, &device->channels, device_node) {
  747. rc = -ENOMEM;
  748. chan->local = alloc_percpu(typeof(*chan->local));
  749. if (chan->local == NULL)
  750. goto err_out;
  751. chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
  752. if (chan->dev == NULL) {
  753. free_percpu(chan->local);
  754. chan->local = NULL;
  755. goto err_out;
  756. }
  757. chan->chan_id = chancnt++;
  758. chan->dev->device.class = &dma_devclass;
  759. chan->dev->device.parent = device->dev;
  760. chan->dev->chan = chan;
  761. chan->dev->idr_ref = idr_ref;
  762. chan->dev->dev_id = device->dev_id;
  763. atomic_inc(idr_ref);
  764. dev_set_name(&chan->dev->device, "dma%dchan%d",
  765. device->dev_id, chan->chan_id);
  766. rc = device_register(&chan->dev->device);
  767. if (rc) {
  768. free_percpu(chan->local);
  769. chan->local = NULL;
  770. kfree(chan->dev);
  771. atomic_dec(idr_ref);
  772. goto err_out;
  773. }
  774. chan->client_count = 0;
  775. }
  776. device->chancnt = chancnt;
  777. mutex_lock(&dma_list_mutex);
  778. /* take references on public channels */
  779. if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
  780. list_for_each_entry(chan, &device->channels, device_node) {
  781. /* if clients are already waiting for channels we need
  782. * to take references on their behalf
  783. */
  784. if (dma_chan_get(chan) == -ENODEV) {
  785. /* note we can only get here for the first
  786. * channel as the remaining channels are
  787. * guaranteed to get a reference
  788. */
  789. rc = -ENODEV;
  790. mutex_unlock(&dma_list_mutex);
  791. goto err_out;
  792. }
  793. }
  794. list_add_tail_rcu(&device->global_node, &dma_device_list);
  795. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  796. device->privatecnt++; /* Always private */
  797. dma_channel_rebalance();
  798. mutex_unlock(&dma_list_mutex);
  799. return 0;
  800. err_out:
  801. /* if we never registered a channel just release the idr */
  802. if (atomic_read(idr_ref) == 0) {
  803. mutex_lock(&dma_list_mutex);
  804. idr_remove(&dma_idr, device->dev_id);
  805. mutex_unlock(&dma_list_mutex);
  806. kfree(idr_ref);
  807. return rc;
  808. }
  809. list_for_each_entry(chan, &device->channels, device_node) {
  810. if (chan->local == NULL)
  811. continue;
  812. mutex_lock(&dma_list_mutex);
  813. chan->dev->chan = NULL;
  814. mutex_unlock(&dma_list_mutex);
  815. device_unregister(&chan->dev->device);
  816. free_percpu(chan->local);
  817. }
  818. return rc;
  819. }
  820. EXPORT_SYMBOL(dma_async_device_register);
  821. /**
  822. * dma_async_device_unregister - unregister a DMA device
  823. * @device: &dma_device
  824. *
  825. * This routine is called by dma driver exit routines, dmaengine holds module
  826. * references to prevent it being called while channels are in use.
  827. */
  828. void dma_async_device_unregister(struct dma_device *device)
  829. {
  830. struct dma_chan *chan;
  831. mutex_lock(&dma_list_mutex);
  832. list_del_rcu(&device->global_node);
  833. dma_channel_rebalance();
  834. mutex_unlock(&dma_list_mutex);
  835. list_for_each_entry(chan, &device->channels, device_node) {
  836. WARN_ONCE(chan->client_count,
  837. "%s called while %d clients hold a reference\n",
  838. __func__, chan->client_count);
  839. mutex_lock(&dma_list_mutex);
  840. chan->dev->chan = NULL;
  841. mutex_unlock(&dma_list_mutex);
  842. device_unregister(&chan->dev->device);
  843. free_percpu(chan->local);
  844. }
  845. }
  846. EXPORT_SYMBOL(dma_async_device_unregister);
  847. struct dmaengine_unmap_pool {
  848. struct kmem_cache *cache;
  849. const char *name;
  850. mempool_t *pool;
  851. size_t size;
  852. };
  853. #define __UNMAP_POOL(x) { .size = x, .name = "dmaengine-unmap-" __stringify(x) }
  854. static struct dmaengine_unmap_pool unmap_pool[] = {
  855. __UNMAP_POOL(2),
  856. #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
  857. __UNMAP_POOL(16),
  858. __UNMAP_POOL(128),
  859. __UNMAP_POOL(256),
  860. #endif
  861. };
  862. static struct dmaengine_unmap_pool *__get_unmap_pool(int nr)
  863. {
  864. int order = get_count_order(nr);
  865. switch (order) {
  866. case 0 ... 1:
  867. return &unmap_pool[0];
  868. case 2 ... 4:
  869. return &unmap_pool[1];
  870. case 5 ... 7:
  871. return &unmap_pool[2];
  872. case 8:
  873. return &unmap_pool[3];
  874. default:
  875. BUG();
  876. return NULL;
  877. }
  878. }
  879. static void dmaengine_unmap(struct kref *kref)
  880. {
  881. struct dmaengine_unmap_data *unmap = container_of(kref, typeof(*unmap), kref);
  882. struct device *dev = unmap->dev;
  883. int cnt, i;
  884. cnt = unmap->to_cnt;
  885. for (i = 0; i < cnt; i++)
  886. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  887. DMA_TO_DEVICE);
  888. cnt += unmap->from_cnt;
  889. for (; i < cnt; i++)
  890. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  891. DMA_FROM_DEVICE);
  892. cnt += unmap->bidi_cnt;
  893. for (; i < cnt; i++) {
  894. if (unmap->addr[i] == 0)
  895. continue;
  896. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  897. DMA_BIDIRECTIONAL);
  898. }
  899. cnt = unmap->map_cnt;
  900. mempool_free(unmap, __get_unmap_pool(cnt)->pool);
  901. }
  902. void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
  903. {
  904. if (unmap)
  905. kref_put(&unmap->kref, dmaengine_unmap);
  906. }
  907. EXPORT_SYMBOL_GPL(dmaengine_unmap_put);
  908. static void dmaengine_destroy_unmap_pool(void)
  909. {
  910. int i;
  911. for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
  912. struct dmaengine_unmap_pool *p = &unmap_pool[i];
  913. if (p->pool)
  914. mempool_destroy(p->pool);
  915. p->pool = NULL;
  916. if (p->cache)
  917. kmem_cache_destroy(p->cache);
  918. p->cache = NULL;
  919. }
  920. }
  921. static int __init dmaengine_init_unmap_pool(void)
  922. {
  923. int i;
  924. for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
  925. struct dmaengine_unmap_pool *p = &unmap_pool[i];
  926. size_t size;
  927. size = sizeof(struct dmaengine_unmap_data) +
  928. sizeof(dma_addr_t) * p->size;
  929. p->cache = kmem_cache_create(p->name, size, 0,
  930. SLAB_HWCACHE_ALIGN, NULL);
  931. if (!p->cache)
  932. break;
  933. p->pool = mempool_create_slab_pool(1, p->cache);
  934. if (!p->pool)
  935. break;
  936. }
  937. if (i == ARRAY_SIZE(unmap_pool))
  938. return 0;
  939. dmaengine_destroy_unmap_pool();
  940. return -ENOMEM;
  941. }
  942. struct dmaengine_unmap_data *
  943. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
  944. {
  945. struct dmaengine_unmap_data *unmap;
  946. unmap = mempool_alloc(__get_unmap_pool(nr)->pool, flags);
  947. if (!unmap)
  948. return NULL;
  949. memset(unmap, 0, sizeof(*unmap));
  950. kref_init(&unmap->kref);
  951. unmap->dev = dev;
  952. unmap->map_cnt = nr;
  953. return unmap;
  954. }
  955. EXPORT_SYMBOL(dmaengine_get_unmap_data);
  956. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  957. struct dma_chan *chan)
  958. {
  959. tx->chan = chan;
  960. #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  961. spin_lock_init(&tx->lock);
  962. #endif
  963. }
  964. EXPORT_SYMBOL(dma_async_tx_descriptor_init);
  965. /* dma_wait_for_async_tx - spin wait for a transaction to complete
  966. * @tx: in-flight transaction to wait on
  967. */
  968. enum dma_status
  969. dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  970. {
  971. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  972. if (!tx)
  973. return DMA_COMPLETE;
  974. while (tx->cookie == -EBUSY) {
  975. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  976. pr_err("%s timeout waiting for descriptor submission\n",
  977. __func__);
  978. return DMA_ERROR;
  979. }
  980. cpu_relax();
  981. }
  982. return dma_sync_wait(tx->chan, tx->cookie);
  983. }
  984. EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
  985. /* dma_run_dependencies - helper routine for dma drivers to process
  986. * (start) dependent operations on their target channel
  987. * @tx: transaction with dependencies
  988. */
  989. void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
  990. {
  991. struct dma_async_tx_descriptor *dep = txd_next(tx);
  992. struct dma_async_tx_descriptor *dep_next;
  993. struct dma_chan *chan;
  994. if (!dep)
  995. return;
  996. /* we'll submit tx->next now, so clear the link */
  997. txd_clear_next(tx);
  998. chan = dep->chan;
  999. /* keep submitting up until a channel switch is detected
  1000. * in that case we will be called again as a result of
  1001. * processing the interrupt from async_tx_channel_switch
  1002. */
  1003. for (; dep; dep = dep_next) {
  1004. txd_lock(dep);
  1005. txd_clear_parent(dep);
  1006. dep_next = txd_next(dep);
  1007. if (dep_next && dep_next->chan == chan)
  1008. txd_clear_next(dep); /* ->next will be submitted */
  1009. else
  1010. dep_next = NULL; /* submit current dep and terminate */
  1011. txd_unlock(dep);
  1012. dep->tx_submit(dep);
  1013. }
  1014. chan->device->device_issue_pending(chan);
  1015. }
  1016. EXPORT_SYMBOL_GPL(dma_run_dependencies);
  1017. static int __init dma_bus_init(void)
  1018. {
  1019. int err = dmaengine_init_unmap_pool();
  1020. if (err)
  1021. return err;
  1022. return class_register(&dma_devclass);
  1023. }
  1024. arch_initcall(dma_bus_init);