dmaengine.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. /* If the channel is used via a DMA request router, free the mapping */
  235. if (chan->router && chan->router->route_free) {
  236. chan->router->route_free(chan->router->dev, chan->route_data);
  237. chan->router = NULL;
  238. chan->route_data = NULL;
  239. }
  240. }
  241. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  242. {
  243. enum dma_status status;
  244. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  245. dma_async_issue_pending(chan);
  246. do {
  247. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  248. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  249. pr_err("%s: timeout!\n", __func__);
  250. return DMA_ERROR;
  251. }
  252. if (status != DMA_IN_PROGRESS)
  253. break;
  254. cpu_relax();
  255. } while (1);
  256. return status;
  257. }
  258. EXPORT_SYMBOL(dma_sync_wait);
  259. /**
  260. * dma_cap_mask_all - enable iteration over all operation types
  261. */
  262. static dma_cap_mask_t dma_cap_mask_all;
  263. /**
  264. * dma_chan_tbl_ent - tracks channel allocations per core/operation
  265. * @chan - associated channel for this entry
  266. */
  267. struct dma_chan_tbl_ent {
  268. struct dma_chan *chan;
  269. };
  270. /**
  271. * channel_table - percpu lookup table for memory-to-memory offload providers
  272. */
  273. static struct dma_chan_tbl_ent __percpu *channel_table[DMA_TX_TYPE_END];
  274. static int __init dma_channel_table_init(void)
  275. {
  276. enum dma_transaction_type cap;
  277. int err = 0;
  278. bitmap_fill(dma_cap_mask_all.bits, DMA_TX_TYPE_END);
  279. /* 'interrupt', 'private', and 'slave' are channel capabilities,
  280. * but are not associated with an operation so they do not need
  281. * an entry in the channel_table
  282. */
  283. clear_bit(DMA_INTERRUPT, dma_cap_mask_all.bits);
  284. clear_bit(DMA_PRIVATE, dma_cap_mask_all.bits);
  285. clear_bit(DMA_SLAVE, dma_cap_mask_all.bits);
  286. for_each_dma_cap_mask(cap, dma_cap_mask_all) {
  287. channel_table[cap] = alloc_percpu(struct dma_chan_tbl_ent);
  288. if (!channel_table[cap]) {
  289. err = -ENOMEM;
  290. break;
  291. }
  292. }
  293. if (err) {
  294. pr_err("initialization failure\n");
  295. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  296. free_percpu(channel_table[cap]);
  297. }
  298. return err;
  299. }
  300. arch_initcall(dma_channel_table_init);
  301. /**
  302. * dma_find_channel - find a channel to carry out the operation
  303. * @tx_type: transaction type
  304. */
  305. struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
  306. {
  307. return this_cpu_read(channel_table[tx_type]->chan);
  308. }
  309. EXPORT_SYMBOL(dma_find_channel);
  310. /**
  311. * dma_issue_pending_all - flush all pending operations across all channels
  312. */
  313. void dma_issue_pending_all(void)
  314. {
  315. struct dma_device *device;
  316. struct dma_chan *chan;
  317. rcu_read_lock();
  318. list_for_each_entry_rcu(device, &dma_device_list, global_node) {
  319. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  320. continue;
  321. list_for_each_entry(chan, &device->channels, device_node)
  322. if (chan->client_count)
  323. device->device_issue_pending(chan);
  324. }
  325. rcu_read_unlock();
  326. }
  327. EXPORT_SYMBOL(dma_issue_pending_all);
  328. /**
  329. * dma_chan_is_local - returns true if the channel is in the same numa-node as the cpu
  330. */
  331. static bool dma_chan_is_local(struct dma_chan *chan, int cpu)
  332. {
  333. int node = dev_to_node(chan->device->dev);
  334. return node == -1 || cpumask_test_cpu(cpu, cpumask_of_node(node));
  335. }
  336. /**
  337. * min_chan - returns the channel with min count and in the same numa-node as the cpu
  338. * @cap: capability to match
  339. * @cpu: cpu index which the channel should be close to
  340. *
  341. * If some channels are close to the given cpu, the one with the lowest
  342. * reference count is returned. Otherwise, cpu is ignored and only the
  343. * reference count is taken into account.
  344. * Must be called under dma_list_mutex.
  345. */
  346. static struct dma_chan *min_chan(enum dma_transaction_type cap, int cpu)
  347. {
  348. struct dma_device *device;
  349. struct dma_chan *chan;
  350. struct dma_chan *min = NULL;
  351. struct dma_chan *localmin = NULL;
  352. list_for_each_entry(device, &dma_device_list, global_node) {
  353. if (!dma_has_cap(cap, device->cap_mask) ||
  354. dma_has_cap(DMA_PRIVATE, device->cap_mask))
  355. continue;
  356. list_for_each_entry(chan, &device->channels, device_node) {
  357. if (!chan->client_count)
  358. continue;
  359. if (!min || chan->table_count < min->table_count)
  360. min = chan;
  361. if (dma_chan_is_local(chan, cpu))
  362. if (!localmin ||
  363. chan->table_count < localmin->table_count)
  364. localmin = chan;
  365. }
  366. }
  367. chan = localmin ? localmin : min;
  368. if (chan)
  369. chan->table_count++;
  370. return chan;
  371. }
  372. /**
  373. * dma_channel_rebalance - redistribute the available channels
  374. *
  375. * Optimize for cpu isolation (each cpu gets a dedicated channel for an
  376. * operation type) in the SMP case, and operation isolation (avoid
  377. * multi-tasking channels) in the non-SMP case. Must be called under
  378. * dma_list_mutex.
  379. */
  380. static void dma_channel_rebalance(void)
  381. {
  382. struct dma_chan *chan;
  383. struct dma_device *device;
  384. int cpu;
  385. int cap;
  386. /* undo the last distribution */
  387. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  388. for_each_possible_cpu(cpu)
  389. per_cpu_ptr(channel_table[cap], cpu)->chan = NULL;
  390. list_for_each_entry(device, &dma_device_list, global_node) {
  391. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  392. continue;
  393. list_for_each_entry(chan, &device->channels, device_node)
  394. chan->table_count = 0;
  395. }
  396. /* don't populate the channel_table if no clients are available */
  397. if (!dmaengine_ref_count)
  398. return;
  399. /* redistribute available channels */
  400. for_each_dma_cap_mask(cap, dma_cap_mask_all)
  401. for_each_online_cpu(cpu) {
  402. chan = min_chan(cap, cpu);
  403. per_cpu_ptr(channel_table[cap], cpu)->chan = chan;
  404. }
  405. }
  406. int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps)
  407. {
  408. struct dma_device *device;
  409. if (!chan || !caps)
  410. return -EINVAL;
  411. device = chan->device;
  412. /* check if the channel supports slave transactions */
  413. if (!test_bit(DMA_SLAVE, device->cap_mask.bits))
  414. return -ENXIO;
  415. /*
  416. * Check whether it reports it uses the generic slave
  417. * capabilities, if not, that means it doesn't support any
  418. * kind of slave capabilities reporting.
  419. */
  420. if (!device->directions)
  421. return -ENXIO;
  422. caps->src_addr_widths = device->src_addr_widths;
  423. caps->dst_addr_widths = device->dst_addr_widths;
  424. caps->directions = device->directions;
  425. caps->residue_granularity = device->residue_granularity;
  426. /*
  427. * Some devices implement only pause (e.g. to get residuum) but no
  428. * resume. However cmd_pause is advertised as pause AND resume.
  429. */
  430. caps->cmd_pause = !!(device->device_pause && device->device_resume);
  431. caps->cmd_terminate = !!device->device_terminate_all;
  432. return 0;
  433. }
  434. EXPORT_SYMBOL_GPL(dma_get_slave_caps);
  435. static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
  436. struct dma_device *dev,
  437. dma_filter_fn fn, void *fn_param)
  438. {
  439. struct dma_chan *chan;
  440. if (!__dma_device_satisfies_mask(dev, mask)) {
  441. pr_debug("%s: wrong capabilities\n", __func__);
  442. return NULL;
  443. }
  444. /* devices with multiple channels need special handling as we need to
  445. * ensure that all channels are either private or public.
  446. */
  447. if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
  448. list_for_each_entry(chan, &dev->channels, device_node) {
  449. /* some channels are already publicly allocated */
  450. if (chan->client_count)
  451. return NULL;
  452. }
  453. list_for_each_entry(chan, &dev->channels, device_node) {
  454. if (chan->client_count) {
  455. pr_debug("%s: %s busy\n",
  456. __func__, dma_chan_name(chan));
  457. continue;
  458. }
  459. if (fn && !fn(chan, fn_param)) {
  460. pr_debug("%s: %s filter said false\n",
  461. __func__, dma_chan_name(chan));
  462. continue;
  463. }
  464. return chan;
  465. }
  466. return NULL;
  467. }
  468. /**
  469. * dma_get_slave_channel - try to get specific channel exclusively
  470. * @chan: target channel
  471. */
  472. struct dma_chan *dma_get_slave_channel(struct dma_chan *chan)
  473. {
  474. int err = -EBUSY;
  475. /* lock against __dma_request_channel */
  476. mutex_lock(&dma_list_mutex);
  477. if (chan->client_count == 0) {
  478. struct dma_device *device = chan->device;
  479. dma_cap_set(DMA_PRIVATE, device->cap_mask);
  480. device->privatecnt++;
  481. err = dma_chan_get(chan);
  482. if (err) {
  483. pr_debug("%s: failed to get %s: (%d)\n",
  484. __func__, dma_chan_name(chan), err);
  485. chan = NULL;
  486. if (--device->privatecnt == 0)
  487. dma_cap_clear(DMA_PRIVATE, device->cap_mask);
  488. }
  489. } else
  490. chan = NULL;
  491. mutex_unlock(&dma_list_mutex);
  492. return chan;
  493. }
  494. EXPORT_SYMBOL_GPL(dma_get_slave_channel);
  495. struct dma_chan *dma_get_any_slave_channel(struct dma_device *device)
  496. {
  497. dma_cap_mask_t mask;
  498. struct dma_chan *chan;
  499. int err;
  500. dma_cap_zero(mask);
  501. dma_cap_set(DMA_SLAVE, mask);
  502. /* lock against __dma_request_channel */
  503. mutex_lock(&dma_list_mutex);
  504. chan = private_candidate(&mask, device, NULL, NULL);
  505. if (chan) {
  506. dma_cap_set(DMA_PRIVATE, device->cap_mask);
  507. device->privatecnt++;
  508. err = dma_chan_get(chan);
  509. if (err) {
  510. pr_debug("%s: failed to get %s: (%d)\n",
  511. __func__, dma_chan_name(chan), err);
  512. chan = NULL;
  513. if (--device->privatecnt == 0)
  514. dma_cap_clear(DMA_PRIVATE, device->cap_mask);
  515. }
  516. }
  517. mutex_unlock(&dma_list_mutex);
  518. return chan;
  519. }
  520. EXPORT_SYMBOL_GPL(dma_get_any_slave_channel);
  521. /**
  522. * __dma_request_channel - try to allocate an exclusive channel
  523. * @mask: capabilities that the channel must satisfy
  524. * @fn: optional callback to disposition available channels
  525. * @fn_param: opaque parameter to pass to dma_filter_fn
  526. *
  527. * Returns pointer to appropriate DMA channel on success or NULL.
  528. */
  529. struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  530. dma_filter_fn fn, void *fn_param)
  531. {
  532. struct dma_device *device, *_d;
  533. struct dma_chan *chan = NULL;
  534. int err;
  535. /* Find a channel */
  536. mutex_lock(&dma_list_mutex);
  537. list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
  538. chan = private_candidate(mask, device, fn, fn_param);
  539. if (chan) {
  540. /* Found a suitable channel, try to grab, prep, and
  541. * return it. We first set DMA_PRIVATE to disable
  542. * balance_ref_count as this channel will not be
  543. * published in the general-purpose allocator
  544. */
  545. dma_cap_set(DMA_PRIVATE, device->cap_mask);
  546. device->privatecnt++;
  547. err = dma_chan_get(chan);
  548. if (err == -ENODEV) {
  549. pr_debug("%s: %s module removed\n",
  550. __func__, dma_chan_name(chan));
  551. list_del_rcu(&device->global_node);
  552. } else if (err)
  553. pr_debug("%s: failed to get %s: (%d)\n",
  554. __func__, dma_chan_name(chan), err);
  555. else
  556. break;
  557. if (--device->privatecnt == 0)
  558. dma_cap_clear(DMA_PRIVATE, device->cap_mask);
  559. chan = NULL;
  560. }
  561. }
  562. mutex_unlock(&dma_list_mutex);
  563. pr_debug("%s: %s (%s)\n",
  564. __func__,
  565. chan ? "success" : "fail",
  566. chan ? dma_chan_name(chan) : NULL);
  567. return chan;
  568. }
  569. EXPORT_SYMBOL_GPL(__dma_request_channel);
  570. /**
  571. * dma_request_slave_channel_reason - try to allocate an exclusive slave channel
  572. * @dev: pointer to client device structure
  573. * @name: slave channel name
  574. *
  575. * Returns pointer to appropriate DMA channel on success or an error pointer.
  576. */
  577. struct dma_chan *dma_request_slave_channel_reason(struct device *dev,
  578. const char *name)
  579. {
  580. /* If device-tree is present get slave info from here */
  581. if (dev->of_node)
  582. return of_dma_request_slave_channel(dev->of_node, name);
  583. /* If device was enumerated by ACPI get slave info from here */
  584. if (ACPI_HANDLE(dev))
  585. return acpi_dma_request_slave_chan_by_name(dev, name);
  586. return ERR_PTR(-ENODEV);
  587. }
  588. EXPORT_SYMBOL_GPL(dma_request_slave_channel_reason);
  589. /**
  590. * dma_request_slave_channel - try to allocate an exclusive slave channel
  591. * @dev: pointer to client device structure
  592. * @name: slave channel name
  593. *
  594. * Returns pointer to appropriate DMA channel on success or NULL.
  595. */
  596. struct dma_chan *dma_request_slave_channel(struct device *dev,
  597. const char *name)
  598. {
  599. struct dma_chan *ch = dma_request_slave_channel_reason(dev, name);
  600. if (IS_ERR(ch))
  601. return NULL;
  602. dma_cap_set(DMA_PRIVATE, ch->device->cap_mask);
  603. ch->device->privatecnt++;
  604. return ch;
  605. }
  606. EXPORT_SYMBOL_GPL(dma_request_slave_channel);
  607. void dma_release_channel(struct dma_chan *chan)
  608. {
  609. mutex_lock(&dma_list_mutex);
  610. WARN_ONCE(chan->client_count != 1,
  611. "chan reference count %d != 1\n", chan->client_count);
  612. dma_chan_put(chan);
  613. /* drop PRIVATE cap enabled by __dma_request_channel() */
  614. if (--chan->device->privatecnt == 0)
  615. dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
  616. mutex_unlock(&dma_list_mutex);
  617. }
  618. EXPORT_SYMBOL_GPL(dma_release_channel);
  619. /**
  620. * dmaengine_get - register interest in dma_channels
  621. */
  622. void dmaengine_get(void)
  623. {
  624. struct dma_device *device, *_d;
  625. struct dma_chan *chan;
  626. int err;
  627. mutex_lock(&dma_list_mutex);
  628. dmaengine_ref_count++;
  629. /* try to grab channels */
  630. list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
  631. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  632. continue;
  633. list_for_each_entry(chan, &device->channels, device_node) {
  634. err = dma_chan_get(chan);
  635. if (err == -ENODEV) {
  636. /* module removed before we could use it */
  637. list_del_rcu(&device->global_node);
  638. break;
  639. } else if (err)
  640. pr_debug("%s: failed to get %s: (%d)\n",
  641. __func__, dma_chan_name(chan), err);
  642. }
  643. }
  644. /* if this is the first reference and there were channels
  645. * waiting we need to rebalance to get those channels
  646. * incorporated into the channel table
  647. */
  648. if (dmaengine_ref_count == 1)
  649. dma_channel_rebalance();
  650. mutex_unlock(&dma_list_mutex);
  651. }
  652. EXPORT_SYMBOL(dmaengine_get);
  653. /**
  654. * dmaengine_put - let dma drivers be removed when ref_count == 0
  655. */
  656. void dmaengine_put(void)
  657. {
  658. struct dma_device *device;
  659. struct dma_chan *chan;
  660. mutex_lock(&dma_list_mutex);
  661. dmaengine_ref_count--;
  662. BUG_ON(dmaengine_ref_count < 0);
  663. /* drop channel references */
  664. list_for_each_entry(device, &dma_device_list, global_node) {
  665. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  666. continue;
  667. list_for_each_entry(chan, &device->channels, device_node)
  668. dma_chan_put(chan);
  669. }
  670. mutex_unlock(&dma_list_mutex);
  671. }
  672. EXPORT_SYMBOL(dmaengine_put);
  673. static bool device_has_all_tx_types(struct dma_device *device)
  674. {
  675. /* A device that satisfies this test has channels that will never cause
  676. * an async_tx channel switch event as all possible operation types can
  677. * be handled.
  678. */
  679. #ifdef CONFIG_ASYNC_TX_DMA
  680. if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
  681. return false;
  682. #endif
  683. #if defined(CONFIG_ASYNC_MEMCPY) || defined(CONFIG_ASYNC_MEMCPY_MODULE)
  684. if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
  685. return false;
  686. #endif
  687. #if defined(CONFIG_ASYNC_XOR) || defined(CONFIG_ASYNC_XOR_MODULE)
  688. if (!dma_has_cap(DMA_XOR, device->cap_mask))
  689. return false;
  690. #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  691. if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
  692. return false;
  693. #endif
  694. #endif
  695. #if defined(CONFIG_ASYNC_PQ) || defined(CONFIG_ASYNC_PQ_MODULE)
  696. if (!dma_has_cap(DMA_PQ, device->cap_mask))
  697. return false;
  698. #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  699. if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
  700. return false;
  701. #endif
  702. #endif
  703. return true;
  704. }
  705. static int get_dma_id(struct dma_device *device)
  706. {
  707. int rc;
  708. mutex_lock(&dma_list_mutex);
  709. rc = idr_alloc(&dma_idr, NULL, 0, 0, GFP_KERNEL);
  710. if (rc >= 0)
  711. device->dev_id = rc;
  712. mutex_unlock(&dma_list_mutex);
  713. return rc < 0 ? rc : 0;
  714. }
  715. /**
  716. * dma_async_device_register - registers DMA devices found
  717. * @device: &dma_device
  718. */
  719. int dma_async_device_register(struct dma_device *device)
  720. {
  721. int chancnt = 0, rc;
  722. struct dma_chan* chan;
  723. atomic_t *idr_ref;
  724. if (!device)
  725. return -ENODEV;
  726. /* validate device routines */
  727. BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
  728. !device->device_prep_dma_memcpy);
  729. BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
  730. !device->device_prep_dma_xor);
  731. BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) &&
  732. !device->device_prep_dma_xor_val);
  733. BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) &&
  734. !device->device_prep_dma_pq);
  735. BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) &&
  736. !device->device_prep_dma_pq_val);
  737. BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
  738. !device->device_prep_dma_memset);
  739. BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
  740. !device->device_prep_dma_interrupt);
  741. BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) &&
  742. !device->device_prep_dma_sg);
  743. BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) &&
  744. !device->device_prep_dma_cyclic);
  745. BUG_ON(dma_has_cap(DMA_INTERLEAVE, device->cap_mask) &&
  746. !device->device_prep_interleaved_dma);
  747. BUG_ON(!device->device_tx_status);
  748. BUG_ON(!device->device_issue_pending);
  749. BUG_ON(!device->dev);
  750. /* note: this only matters in the
  751. * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
  752. */
  753. if (device_has_all_tx_types(device))
  754. dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
  755. idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
  756. if (!idr_ref)
  757. return -ENOMEM;
  758. rc = get_dma_id(device);
  759. if (rc != 0) {
  760. kfree(idr_ref);
  761. return rc;
  762. }
  763. atomic_set(idr_ref, 0);
  764. /* represent channels in sysfs. Probably want devs too */
  765. list_for_each_entry(chan, &device->channels, device_node) {
  766. rc = -ENOMEM;
  767. chan->local = alloc_percpu(typeof(*chan->local));
  768. if (chan->local == NULL)
  769. goto err_out;
  770. chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
  771. if (chan->dev == NULL) {
  772. free_percpu(chan->local);
  773. chan->local = NULL;
  774. goto err_out;
  775. }
  776. chan->chan_id = chancnt++;
  777. chan->dev->device.class = &dma_devclass;
  778. chan->dev->device.parent = device->dev;
  779. chan->dev->chan = chan;
  780. chan->dev->idr_ref = idr_ref;
  781. chan->dev->dev_id = device->dev_id;
  782. atomic_inc(idr_ref);
  783. dev_set_name(&chan->dev->device, "dma%dchan%d",
  784. device->dev_id, chan->chan_id);
  785. rc = device_register(&chan->dev->device);
  786. if (rc) {
  787. free_percpu(chan->local);
  788. chan->local = NULL;
  789. kfree(chan->dev);
  790. atomic_dec(idr_ref);
  791. goto err_out;
  792. }
  793. chan->client_count = 0;
  794. }
  795. device->chancnt = chancnt;
  796. mutex_lock(&dma_list_mutex);
  797. /* take references on public channels */
  798. if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
  799. list_for_each_entry(chan, &device->channels, device_node) {
  800. /* if clients are already waiting for channels we need
  801. * to take references on their behalf
  802. */
  803. if (dma_chan_get(chan) == -ENODEV) {
  804. /* note we can only get here for the first
  805. * channel as the remaining channels are
  806. * guaranteed to get a reference
  807. */
  808. rc = -ENODEV;
  809. mutex_unlock(&dma_list_mutex);
  810. goto err_out;
  811. }
  812. }
  813. list_add_tail_rcu(&device->global_node, &dma_device_list);
  814. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  815. device->privatecnt++; /* Always private */
  816. dma_channel_rebalance();
  817. mutex_unlock(&dma_list_mutex);
  818. return 0;
  819. err_out:
  820. /* if we never registered a channel just release the idr */
  821. if (atomic_read(idr_ref) == 0) {
  822. mutex_lock(&dma_list_mutex);
  823. idr_remove(&dma_idr, device->dev_id);
  824. mutex_unlock(&dma_list_mutex);
  825. kfree(idr_ref);
  826. return rc;
  827. }
  828. list_for_each_entry(chan, &device->channels, device_node) {
  829. if (chan->local == NULL)
  830. continue;
  831. mutex_lock(&dma_list_mutex);
  832. chan->dev->chan = NULL;
  833. mutex_unlock(&dma_list_mutex);
  834. device_unregister(&chan->dev->device);
  835. free_percpu(chan->local);
  836. }
  837. return rc;
  838. }
  839. EXPORT_SYMBOL(dma_async_device_register);
  840. /**
  841. * dma_async_device_unregister - unregister a DMA device
  842. * @device: &dma_device
  843. *
  844. * This routine is called by dma driver exit routines, dmaengine holds module
  845. * references to prevent it being called while channels are in use.
  846. */
  847. void dma_async_device_unregister(struct dma_device *device)
  848. {
  849. struct dma_chan *chan;
  850. mutex_lock(&dma_list_mutex);
  851. list_del_rcu(&device->global_node);
  852. dma_channel_rebalance();
  853. mutex_unlock(&dma_list_mutex);
  854. list_for_each_entry(chan, &device->channels, device_node) {
  855. WARN_ONCE(chan->client_count,
  856. "%s called while %d clients hold a reference\n",
  857. __func__, chan->client_count);
  858. mutex_lock(&dma_list_mutex);
  859. chan->dev->chan = NULL;
  860. mutex_unlock(&dma_list_mutex);
  861. device_unregister(&chan->dev->device);
  862. free_percpu(chan->local);
  863. }
  864. }
  865. EXPORT_SYMBOL(dma_async_device_unregister);
  866. struct dmaengine_unmap_pool {
  867. struct kmem_cache *cache;
  868. const char *name;
  869. mempool_t *pool;
  870. size_t size;
  871. };
  872. #define __UNMAP_POOL(x) { .size = x, .name = "dmaengine-unmap-" __stringify(x) }
  873. static struct dmaengine_unmap_pool unmap_pool[] = {
  874. __UNMAP_POOL(2),
  875. #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
  876. __UNMAP_POOL(16),
  877. __UNMAP_POOL(128),
  878. __UNMAP_POOL(256),
  879. #endif
  880. };
  881. static struct dmaengine_unmap_pool *__get_unmap_pool(int nr)
  882. {
  883. int order = get_count_order(nr);
  884. switch (order) {
  885. case 0 ... 1:
  886. return &unmap_pool[0];
  887. #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
  888. case 2 ... 4:
  889. return &unmap_pool[1];
  890. case 5 ... 7:
  891. return &unmap_pool[2];
  892. case 8:
  893. return &unmap_pool[3];
  894. #endif
  895. default:
  896. BUG();
  897. return NULL;
  898. }
  899. }
  900. static void dmaengine_unmap(struct kref *kref)
  901. {
  902. struct dmaengine_unmap_data *unmap = container_of(kref, typeof(*unmap), kref);
  903. struct device *dev = unmap->dev;
  904. int cnt, i;
  905. cnt = unmap->to_cnt;
  906. for (i = 0; i < cnt; i++)
  907. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  908. DMA_TO_DEVICE);
  909. cnt += unmap->from_cnt;
  910. for (; i < cnt; i++)
  911. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  912. DMA_FROM_DEVICE);
  913. cnt += unmap->bidi_cnt;
  914. for (; i < cnt; i++) {
  915. if (unmap->addr[i] == 0)
  916. continue;
  917. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  918. DMA_BIDIRECTIONAL);
  919. }
  920. cnt = unmap->map_cnt;
  921. mempool_free(unmap, __get_unmap_pool(cnt)->pool);
  922. }
  923. void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
  924. {
  925. if (unmap)
  926. kref_put(&unmap->kref, dmaengine_unmap);
  927. }
  928. EXPORT_SYMBOL_GPL(dmaengine_unmap_put);
  929. static void dmaengine_destroy_unmap_pool(void)
  930. {
  931. int i;
  932. for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
  933. struct dmaengine_unmap_pool *p = &unmap_pool[i];
  934. mempool_destroy(p->pool);
  935. p->pool = NULL;
  936. kmem_cache_destroy(p->cache);
  937. p->cache = NULL;
  938. }
  939. }
  940. static int __init dmaengine_init_unmap_pool(void)
  941. {
  942. int i;
  943. for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
  944. struct dmaengine_unmap_pool *p = &unmap_pool[i];
  945. size_t size;
  946. size = sizeof(struct dmaengine_unmap_data) +
  947. sizeof(dma_addr_t) * p->size;
  948. p->cache = kmem_cache_create(p->name, size, 0,
  949. SLAB_HWCACHE_ALIGN, NULL);
  950. if (!p->cache)
  951. break;
  952. p->pool = mempool_create_slab_pool(1, p->cache);
  953. if (!p->pool)
  954. break;
  955. }
  956. if (i == ARRAY_SIZE(unmap_pool))
  957. return 0;
  958. dmaengine_destroy_unmap_pool();
  959. return -ENOMEM;
  960. }
  961. struct dmaengine_unmap_data *
  962. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
  963. {
  964. struct dmaengine_unmap_data *unmap;
  965. unmap = mempool_alloc(__get_unmap_pool(nr)->pool, flags);
  966. if (!unmap)
  967. return NULL;
  968. memset(unmap, 0, sizeof(*unmap));
  969. kref_init(&unmap->kref);
  970. unmap->dev = dev;
  971. unmap->map_cnt = nr;
  972. return unmap;
  973. }
  974. EXPORT_SYMBOL(dmaengine_get_unmap_data);
  975. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  976. struct dma_chan *chan)
  977. {
  978. tx->chan = chan;
  979. #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  980. spin_lock_init(&tx->lock);
  981. #endif
  982. }
  983. EXPORT_SYMBOL(dma_async_tx_descriptor_init);
  984. /* dma_wait_for_async_tx - spin wait for a transaction to complete
  985. * @tx: in-flight transaction to wait on
  986. */
  987. enum dma_status
  988. dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  989. {
  990. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  991. if (!tx)
  992. return DMA_COMPLETE;
  993. while (tx->cookie == -EBUSY) {
  994. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  995. pr_err("%s timeout waiting for descriptor submission\n",
  996. __func__);
  997. return DMA_ERROR;
  998. }
  999. cpu_relax();
  1000. }
  1001. return dma_sync_wait(tx->chan, tx->cookie);
  1002. }
  1003. EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
  1004. /* dma_run_dependencies - helper routine for dma drivers to process
  1005. * (start) dependent operations on their target channel
  1006. * @tx: transaction with dependencies
  1007. */
  1008. void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
  1009. {
  1010. struct dma_async_tx_descriptor *dep = txd_next(tx);
  1011. struct dma_async_tx_descriptor *dep_next;
  1012. struct dma_chan *chan;
  1013. if (!dep)
  1014. return;
  1015. /* we'll submit tx->next now, so clear the link */
  1016. txd_clear_next(tx);
  1017. chan = dep->chan;
  1018. /* keep submitting up until a channel switch is detected
  1019. * in that case we will be called again as a result of
  1020. * processing the interrupt from async_tx_channel_switch
  1021. */
  1022. for (; dep; dep = dep_next) {
  1023. txd_lock(dep);
  1024. txd_clear_parent(dep);
  1025. dep_next = txd_next(dep);
  1026. if (dep_next && dep_next->chan == chan)
  1027. txd_clear_next(dep); /* ->next will be submitted */
  1028. else
  1029. dep_next = NULL; /* submit current dep and terminate */
  1030. txd_unlock(dep);
  1031. dep->tx_submit(dep);
  1032. }
  1033. chan->device->device_issue_pending(chan);
  1034. }
  1035. EXPORT_SYMBOL_GPL(dma_run_dependencies);
  1036. static int __init dma_bus_init(void)
  1037. {
  1038. int err = dmaengine_init_unmap_pool();
  1039. if (err)
  1040. return err;
  1041. return class_register(&dma_devclass);
  1042. }
  1043. arch_initcall(dma_bus_init);