dmaengine.c 33 KB

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