dmaengine.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. test_bit(DMA_CYCLIC, device->cap_mask.bits)))
  419. return -ENXIO;
  420. /*
  421. * Check whether it reports it uses the generic slave
  422. * capabilities, if not, that means it doesn't support any
  423. * kind of slave capabilities reporting.
  424. */
  425. if (!device->directions)
  426. return -ENXIO;
  427. caps->src_addr_widths = device->src_addr_widths;
  428. caps->dst_addr_widths = device->dst_addr_widths;
  429. caps->directions = device->directions;
  430. caps->max_burst = device->max_burst;
  431. caps->residue_granularity = device->residue_granularity;
  432. caps->descriptor_reuse = device->descriptor_reuse;
  433. /*
  434. * Some devices implement only pause (e.g. to get residuum) but no
  435. * resume. However cmd_pause is advertised as pause AND resume.
  436. */
  437. caps->cmd_pause = !!(device->device_pause && device->device_resume);
  438. caps->cmd_terminate = !!device->device_terminate_all;
  439. return 0;
  440. }
  441. EXPORT_SYMBOL_GPL(dma_get_slave_caps);
  442. static struct dma_chan *private_candidate(const dma_cap_mask_t *mask,
  443. struct dma_device *dev,
  444. dma_filter_fn fn, void *fn_param)
  445. {
  446. struct dma_chan *chan;
  447. if (mask && !__dma_device_satisfies_mask(dev, mask)) {
  448. dev_dbg(dev->dev, "%s: wrong capabilities\n", __func__);
  449. return NULL;
  450. }
  451. /* devices with multiple channels need special handling as we need to
  452. * ensure that all channels are either private or public.
  453. */
  454. if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask))
  455. list_for_each_entry(chan, &dev->channels, device_node) {
  456. /* some channels are already publicly allocated */
  457. if (chan->client_count)
  458. return NULL;
  459. }
  460. list_for_each_entry(chan, &dev->channels, device_node) {
  461. if (chan->client_count) {
  462. dev_dbg(dev->dev, "%s: %s busy\n",
  463. __func__, dma_chan_name(chan));
  464. continue;
  465. }
  466. if (fn && !fn(chan, fn_param)) {
  467. dev_dbg(dev->dev, "%s: %s filter said false\n",
  468. __func__, dma_chan_name(chan));
  469. continue;
  470. }
  471. return chan;
  472. }
  473. return NULL;
  474. }
  475. static struct dma_chan *find_candidate(struct dma_device *device,
  476. const dma_cap_mask_t *mask,
  477. dma_filter_fn fn, void *fn_param)
  478. {
  479. struct dma_chan *chan = private_candidate(mask, device, fn, fn_param);
  480. int err;
  481. if (chan) {
  482. /* Found a suitable channel, try to grab, prep, and return it.
  483. * We first set DMA_PRIVATE to disable balance_ref_count as this
  484. * channel will not be published in the general-purpose
  485. * allocator
  486. */
  487. dma_cap_set(DMA_PRIVATE, device->cap_mask);
  488. device->privatecnt++;
  489. err = dma_chan_get(chan);
  490. if (err) {
  491. if (err == -ENODEV) {
  492. dev_dbg(device->dev, "%s: %s module removed\n",
  493. __func__, dma_chan_name(chan));
  494. list_del_rcu(&device->global_node);
  495. } else
  496. dev_dbg(device->dev,
  497. "%s: failed to get %s: (%d)\n",
  498. __func__, dma_chan_name(chan), err);
  499. if (--device->privatecnt == 0)
  500. dma_cap_clear(DMA_PRIVATE, device->cap_mask);
  501. chan = ERR_PTR(err);
  502. }
  503. }
  504. return chan ? chan : ERR_PTR(-EPROBE_DEFER);
  505. }
  506. /**
  507. * dma_get_slave_channel - try to get specific channel exclusively
  508. * @chan: target channel
  509. */
  510. struct dma_chan *dma_get_slave_channel(struct dma_chan *chan)
  511. {
  512. int err = -EBUSY;
  513. /* lock against __dma_request_channel */
  514. mutex_lock(&dma_list_mutex);
  515. if (chan->client_count == 0) {
  516. struct dma_device *device = chan->device;
  517. dma_cap_set(DMA_PRIVATE, device->cap_mask);
  518. device->privatecnt++;
  519. err = dma_chan_get(chan);
  520. if (err) {
  521. dev_dbg(chan->device->dev,
  522. "%s: failed to get %s: (%d)\n",
  523. __func__, dma_chan_name(chan), err);
  524. chan = NULL;
  525. if (--device->privatecnt == 0)
  526. dma_cap_clear(DMA_PRIVATE, device->cap_mask);
  527. }
  528. } else
  529. chan = NULL;
  530. mutex_unlock(&dma_list_mutex);
  531. return chan;
  532. }
  533. EXPORT_SYMBOL_GPL(dma_get_slave_channel);
  534. struct dma_chan *dma_get_any_slave_channel(struct dma_device *device)
  535. {
  536. dma_cap_mask_t mask;
  537. struct dma_chan *chan;
  538. dma_cap_zero(mask);
  539. dma_cap_set(DMA_SLAVE, mask);
  540. /* lock against __dma_request_channel */
  541. mutex_lock(&dma_list_mutex);
  542. chan = find_candidate(device, &mask, NULL, NULL);
  543. mutex_unlock(&dma_list_mutex);
  544. return IS_ERR(chan) ? NULL : chan;
  545. }
  546. EXPORT_SYMBOL_GPL(dma_get_any_slave_channel);
  547. /**
  548. * __dma_request_channel - try to allocate an exclusive channel
  549. * @mask: capabilities that the channel must satisfy
  550. * @fn: optional callback to disposition available channels
  551. * @fn_param: opaque parameter to pass to dma_filter_fn
  552. *
  553. * Returns pointer to appropriate DMA channel on success or NULL.
  554. */
  555. struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  556. dma_filter_fn fn, void *fn_param)
  557. {
  558. struct dma_device *device, *_d;
  559. struct dma_chan *chan = NULL;
  560. /* Find a channel */
  561. mutex_lock(&dma_list_mutex);
  562. list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
  563. chan = find_candidate(device, mask, fn, fn_param);
  564. if (!IS_ERR(chan))
  565. break;
  566. chan = NULL;
  567. }
  568. mutex_unlock(&dma_list_mutex);
  569. pr_debug("%s: %s (%s)\n",
  570. __func__,
  571. chan ? "success" : "fail",
  572. chan ? dma_chan_name(chan) : NULL);
  573. return chan;
  574. }
  575. EXPORT_SYMBOL_GPL(__dma_request_channel);
  576. static const struct dma_slave_map *dma_filter_match(struct dma_device *device,
  577. const char *name,
  578. struct device *dev)
  579. {
  580. int i;
  581. if (!device->filter.mapcnt)
  582. return NULL;
  583. for (i = 0; i < device->filter.mapcnt; i++) {
  584. const struct dma_slave_map *map = &device->filter.map[i];
  585. if (!strcmp(map->devname, dev_name(dev)) &&
  586. !strcmp(map->slave, name))
  587. return map;
  588. }
  589. return NULL;
  590. }
  591. /**
  592. * dma_request_chan - try to allocate an exclusive slave channel
  593. * @dev: pointer to client device structure
  594. * @name: slave channel name
  595. *
  596. * Returns pointer to appropriate DMA channel on success or an error pointer.
  597. */
  598. struct dma_chan *dma_request_chan(struct device *dev, const char *name)
  599. {
  600. struct dma_device *d, *_d;
  601. struct dma_chan *chan = NULL;
  602. /* If device-tree is present get slave info from here */
  603. if (dev->of_node)
  604. chan = of_dma_request_slave_channel(dev->of_node, name);
  605. /* If device was enumerated by ACPI get slave info from here */
  606. if (has_acpi_companion(dev) && !chan)
  607. chan = acpi_dma_request_slave_chan_by_name(dev, name);
  608. if (chan) {
  609. /* Valid channel found or requester need to be deferred */
  610. if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
  611. return chan;
  612. }
  613. /* Try to find the channel via the DMA filter map(s) */
  614. mutex_lock(&dma_list_mutex);
  615. list_for_each_entry_safe(d, _d, &dma_device_list, global_node) {
  616. dma_cap_mask_t mask;
  617. const struct dma_slave_map *map = dma_filter_match(d, name, dev);
  618. if (!map)
  619. continue;
  620. dma_cap_zero(mask);
  621. dma_cap_set(DMA_SLAVE, mask);
  622. chan = find_candidate(d, &mask, d->filter.fn, map->param);
  623. if (!IS_ERR(chan))
  624. break;
  625. }
  626. mutex_unlock(&dma_list_mutex);
  627. return chan ? chan : ERR_PTR(-EPROBE_DEFER);
  628. }
  629. EXPORT_SYMBOL_GPL(dma_request_chan);
  630. /**
  631. * dma_request_slave_channel - try to allocate an exclusive slave channel
  632. * @dev: pointer to client device structure
  633. * @name: slave channel name
  634. *
  635. * Returns pointer to appropriate DMA channel on success or NULL.
  636. */
  637. struct dma_chan *dma_request_slave_channel(struct device *dev,
  638. const char *name)
  639. {
  640. struct dma_chan *ch = dma_request_chan(dev, name);
  641. if (IS_ERR(ch))
  642. return NULL;
  643. return ch;
  644. }
  645. EXPORT_SYMBOL_GPL(dma_request_slave_channel);
  646. /**
  647. * dma_request_chan_by_mask - allocate a channel satisfying certain capabilities
  648. * @mask: capabilities that the channel must satisfy
  649. *
  650. * Returns pointer to appropriate DMA channel on success or an error pointer.
  651. */
  652. struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask)
  653. {
  654. struct dma_chan *chan;
  655. if (!mask)
  656. return ERR_PTR(-ENODEV);
  657. chan = __dma_request_channel(mask, NULL, NULL);
  658. if (!chan)
  659. chan = ERR_PTR(-ENODEV);
  660. return chan;
  661. }
  662. EXPORT_SYMBOL_GPL(dma_request_chan_by_mask);
  663. void dma_release_channel(struct dma_chan *chan)
  664. {
  665. mutex_lock(&dma_list_mutex);
  666. WARN_ONCE(chan->client_count != 1,
  667. "chan reference count %d != 1\n", chan->client_count);
  668. dma_chan_put(chan);
  669. /* drop PRIVATE cap enabled by __dma_request_channel() */
  670. if (--chan->device->privatecnt == 0)
  671. dma_cap_clear(DMA_PRIVATE, chan->device->cap_mask);
  672. mutex_unlock(&dma_list_mutex);
  673. }
  674. EXPORT_SYMBOL_GPL(dma_release_channel);
  675. /**
  676. * dmaengine_get - register interest in dma_channels
  677. */
  678. void dmaengine_get(void)
  679. {
  680. struct dma_device *device, *_d;
  681. struct dma_chan *chan;
  682. int err;
  683. mutex_lock(&dma_list_mutex);
  684. dmaengine_ref_count++;
  685. /* try to grab channels */
  686. list_for_each_entry_safe(device, _d, &dma_device_list, global_node) {
  687. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  688. continue;
  689. list_for_each_entry(chan, &device->channels, device_node) {
  690. err = dma_chan_get(chan);
  691. if (err == -ENODEV) {
  692. /* module removed before we could use it */
  693. list_del_rcu(&device->global_node);
  694. break;
  695. } else if (err)
  696. dev_dbg(chan->device->dev,
  697. "%s: failed to get %s: (%d)\n",
  698. __func__, dma_chan_name(chan), err);
  699. }
  700. }
  701. /* if this is the first reference and there were channels
  702. * waiting we need to rebalance to get those channels
  703. * incorporated into the channel table
  704. */
  705. if (dmaengine_ref_count == 1)
  706. dma_channel_rebalance();
  707. mutex_unlock(&dma_list_mutex);
  708. }
  709. EXPORT_SYMBOL(dmaengine_get);
  710. /**
  711. * dmaengine_put - let dma drivers be removed when ref_count == 0
  712. */
  713. void dmaengine_put(void)
  714. {
  715. struct dma_device *device;
  716. struct dma_chan *chan;
  717. mutex_lock(&dma_list_mutex);
  718. dmaengine_ref_count--;
  719. BUG_ON(dmaengine_ref_count < 0);
  720. /* drop channel references */
  721. list_for_each_entry(device, &dma_device_list, global_node) {
  722. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  723. continue;
  724. list_for_each_entry(chan, &device->channels, device_node)
  725. dma_chan_put(chan);
  726. }
  727. mutex_unlock(&dma_list_mutex);
  728. }
  729. EXPORT_SYMBOL(dmaengine_put);
  730. static bool device_has_all_tx_types(struct dma_device *device)
  731. {
  732. /* A device that satisfies this test has channels that will never cause
  733. * an async_tx channel switch event as all possible operation types can
  734. * be handled.
  735. */
  736. #ifdef CONFIG_ASYNC_TX_DMA
  737. if (!dma_has_cap(DMA_INTERRUPT, device->cap_mask))
  738. return false;
  739. #endif
  740. #if IS_ENABLED(CONFIG_ASYNC_MEMCPY)
  741. if (!dma_has_cap(DMA_MEMCPY, device->cap_mask))
  742. return false;
  743. #endif
  744. #if IS_ENABLED(CONFIG_ASYNC_XOR)
  745. if (!dma_has_cap(DMA_XOR, device->cap_mask))
  746. return false;
  747. #ifndef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  748. if (!dma_has_cap(DMA_XOR_VAL, device->cap_mask))
  749. return false;
  750. #endif
  751. #endif
  752. #if IS_ENABLED(CONFIG_ASYNC_PQ)
  753. if (!dma_has_cap(DMA_PQ, device->cap_mask))
  754. return false;
  755. #ifndef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  756. if (!dma_has_cap(DMA_PQ_VAL, device->cap_mask))
  757. return false;
  758. #endif
  759. #endif
  760. return true;
  761. }
  762. static int get_dma_id(struct dma_device *device)
  763. {
  764. int rc;
  765. mutex_lock(&dma_list_mutex);
  766. rc = idr_alloc(&dma_idr, NULL, 0, 0, GFP_KERNEL);
  767. if (rc >= 0)
  768. device->dev_id = rc;
  769. mutex_unlock(&dma_list_mutex);
  770. return rc < 0 ? rc : 0;
  771. }
  772. /**
  773. * dma_async_device_register - registers DMA devices found
  774. * @device: &dma_device
  775. */
  776. int dma_async_device_register(struct dma_device *device)
  777. {
  778. int chancnt = 0, rc;
  779. struct dma_chan* chan;
  780. atomic_t *idr_ref;
  781. if (!device)
  782. return -ENODEV;
  783. /* validate device routines */
  784. BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) &&
  785. !device->device_prep_dma_memcpy);
  786. BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) &&
  787. !device->device_prep_dma_xor);
  788. BUG_ON(dma_has_cap(DMA_XOR_VAL, device->cap_mask) &&
  789. !device->device_prep_dma_xor_val);
  790. BUG_ON(dma_has_cap(DMA_PQ, device->cap_mask) &&
  791. !device->device_prep_dma_pq);
  792. BUG_ON(dma_has_cap(DMA_PQ_VAL, device->cap_mask) &&
  793. !device->device_prep_dma_pq_val);
  794. BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) &&
  795. !device->device_prep_dma_memset);
  796. BUG_ON(dma_has_cap(DMA_INTERRUPT, device->cap_mask) &&
  797. !device->device_prep_dma_interrupt);
  798. BUG_ON(dma_has_cap(DMA_SG, device->cap_mask) &&
  799. !device->device_prep_dma_sg);
  800. BUG_ON(dma_has_cap(DMA_CYCLIC, device->cap_mask) &&
  801. !device->device_prep_dma_cyclic);
  802. BUG_ON(dma_has_cap(DMA_INTERLEAVE, device->cap_mask) &&
  803. !device->device_prep_interleaved_dma);
  804. BUG_ON(!device->device_tx_status);
  805. BUG_ON(!device->device_issue_pending);
  806. BUG_ON(!device->dev);
  807. /* note: this only matters in the
  808. * CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH=n case
  809. */
  810. if (device_has_all_tx_types(device))
  811. dma_cap_set(DMA_ASYNC_TX, device->cap_mask);
  812. idr_ref = kmalloc(sizeof(*idr_ref), GFP_KERNEL);
  813. if (!idr_ref)
  814. return -ENOMEM;
  815. rc = get_dma_id(device);
  816. if (rc != 0) {
  817. kfree(idr_ref);
  818. return rc;
  819. }
  820. atomic_set(idr_ref, 0);
  821. /* represent channels in sysfs. Probably want devs too */
  822. list_for_each_entry(chan, &device->channels, device_node) {
  823. rc = -ENOMEM;
  824. chan->local = alloc_percpu(typeof(*chan->local));
  825. if (chan->local == NULL)
  826. goto err_out;
  827. chan->dev = kzalloc(sizeof(*chan->dev), GFP_KERNEL);
  828. if (chan->dev == NULL) {
  829. free_percpu(chan->local);
  830. chan->local = NULL;
  831. goto err_out;
  832. }
  833. chan->chan_id = chancnt++;
  834. chan->dev->device.class = &dma_devclass;
  835. chan->dev->device.parent = device->dev;
  836. chan->dev->chan = chan;
  837. chan->dev->idr_ref = idr_ref;
  838. chan->dev->dev_id = device->dev_id;
  839. atomic_inc(idr_ref);
  840. dev_set_name(&chan->dev->device, "dma%dchan%d",
  841. device->dev_id, chan->chan_id);
  842. rc = device_register(&chan->dev->device);
  843. if (rc) {
  844. free_percpu(chan->local);
  845. chan->local = NULL;
  846. kfree(chan->dev);
  847. atomic_dec(idr_ref);
  848. goto err_out;
  849. }
  850. chan->client_count = 0;
  851. }
  852. if (!chancnt) {
  853. dev_err(device->dev, "%s: device has no channels!\n", __func__);
  854. rc = -ENODEV;
  855. goto err_out;
  856. }
  857. device->chancnt = chancnt;
  858. mutex_lock(&dma_list_mutex);
  859. /* take references on public channels */
  860. if (dmaengine_ref_count && !dma_has_cap(DMA_PRIVATE, device->cap_mask))
  861. list_for_each_entry(chan, &device->channels, device_node) {
  862. /* if clients are already waiting for channels we need
  863. * to take references on their behalf
  864. */
  865. if (dma_chan_get(chan) == -ENODEV) {
  866. /* note we can only get here for the first
  867. * channel as the remaining channels are
  868. * guaranteed to get a reference
  869. */
  870. rc = -ENODEV;
  871. mutex_unlock(&dma_list_mutex);
  872. goto err_out;
  873. }
  874. }
  875. list_add_tail_rcu(&device->global_node, &dma_device_list);
  876. if (dma_has_cap(DMA_PRIVATE, device->cap_mask))
  877. device->privatecnt++; /* Always private */
  878. dma_channel_rebalance();
  879. mutex_unlock(&dma_list_mutex);
  880. return 0;
  881. err_out:
  882. /* if we never registered a channel just release the idr */
  883. if (atomic_read(idr_ref) == 0) {
  884. mutex_lock(&dma_list_mutex);
  885. idr_remove(&dma_idr, device->dev_id);
  886. mutex_unlock(&dma_list_mutex);
  887. kfree(idr_ref);
  888. return rc;
  889. }
  890. list_for_each_entry(chan, &device->channels, device_node) {
  891. if (chan->local == NULL)
  892. continue;
  893. mutex_lock(&dma_list_mutex);
  894. chan->dev->chan = NULL;
  895. mutex_unlock(&dma_list_mutex);
  896. device_unregister(&chan->dev->device);
  897. free_percpu(chan->local);
  898. }
  899. return rc;
  900. }
  901. EXPORT_SYMBOL(dma_async_device_register);
  902. /**
  903. * dma_async_device_unregister - unregister a DMA device
  904. * @device: &dma_device
  905. *
  906. * This routine is called by dma driver exit routines, dmaengine holds module
  907. * references to prevent it being called while channels are in use.
  908. */
  909. void dma_async_device_unregister(struct dma_device *device)
  910. {
  911. struct dma_chan *chan;
  912. mutex_lock(&dma_list_mutex);
  913. list_del_rcu(&device->global_node);
  914. dma_channel_rebalance();
  915. mutex_unlock(&dma_list_mutex);
  916. list_for_each_entry(chan, &device->channels, device_node) {
  917. WARN_ONCE(chan->client_count,
  918. "%s called while %d clients hold a reference\n",
  919. __func__, chan->client_count);
  920. mutex_lock(&dma_list_mutex);
  921. chan->dev->chan = NULL;
  922. mutex_unlock(&dma_list_mutex);
  923. device_unregister(&chan->dev->device);
  924. free_percpu(chan->local);
  925. }
  926. }
  927. EXPORT_SYMBOL(dma_async_device_unregister);
  928. struct dmaengine_unmap_pool {
  929. struct kmem_cache *cache;
  930. const char *name;
  931. mempool_t *pool;
  932. size_t size;
  933. };
  934. #define __UNMAP_POOL(x) { .size = x, .name = "dmaengine-unmap-" __stringify(x) }
  935. static struct dmaengine_unmap_pool unmap_pool[] = {
  936. __UNMAP_POOL(2),
  937. #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
  938. __UNMAP_POOL(16),
  939. __UNMAP_POOL(128),
  940. __UNMAP_POOL(256),
  941. #endif
  942. };
  943. static struct dmaengine_unmap_pool *__get_unmap_pool(int nr)
  944. {
  945. int order = get_count_order(nr);
  946. switch (order) {
  947. case 0 ... 1:
  948. return &unmap_pool[0];
  949. #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
  950. case 2 ... 4:
  951. return &unmap_pool[1];
  952. case 5 ... 7:
  953. return &unmap_pool[2];
  954. case 8:
  955. return &unmap_pool[3];
  956. #endif
  957. default:
  958. BUG();
  959. return NULL;
  960. }
  961. }
  962. static void dmaengine_unmap(struct kref *kref)
  963. {
  964. struct dmaengine_unmap_data *unmap = container_of(kref, typeof(*unmap), kref);
  965. struct device *dev = unmap->dev;
  966. int cnt, i;
  967. cnt = unmap->to_cnt;
  968. for (i = 0; i < cnt; i++)
  969. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  970. DMA_TO_DEVICE);
  971. cnt += unmap->from_cnt;
  972. for (; i < cnt; i++)
  973. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  974. DMA_FROM_DEVICE);
  975. cnt += unmap->bidi_cnt;
  976. for (; i < cnt; i++) {
  977. if (unmap->addr[i] == 0)
  978. continue;
  979. dma_unmap_page(dev, unmap->addr[i], unmap->len,
  980. DMA_BIDIRECTIONAL);
  981. }
  982. cnt = unmap->map_cnt;
  983. mempool_free(unmap, __get_unmap_pool(cnt)->pool);
  984. }
  985. void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
  986. {
  987. if (unmap)
  988. kref_put(&unmap->kref, dmaengine_unmap);
  989. }
  990. EXPORT_SYMBOL_GPL(dmaengine_unmap_put);
  991. static void dmaengine_destroy_unmap_pool(void)
  992. {
  993. int i;
  994. for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
  995. struct dmaengine_unmap_pool *p = &unmap_pool[i];
  996. mempool_destroy(p->pool);
  997. p->pool = NULL;
  998. kmem_cache_destroy(p->cache);
  999. p->cache = NULL;
  1000. }
  1001. }
  1002. static int __init dmaengine_init_unmap_pool(void)
  1003. {
  1004. int i;
  1005. for (i = 0; i < ARRAY_SIZE(unmap_pool); i++) {
  1006. struct dmaengine_unmap_pool *p = &unmap_pool[i];
  1007. size_t size;
  1008. size = sizeof(struct dmaengine_unmap_data) +
  1009. sizeof(dma_addr_t) * p->size;
  1010. p->cache = kmem_cache_create(p->name, size, 0,
  1011. SLAB_HWCACHE_ALIGN, NULL);
  1012. if (!p->cache)
  1013. break;
  1014. p->pool = mempool_create_slab_pool(1, p->cache);
  1015. if (!p->pool)
  1016. break;
  1017. }
  1018. if (i == ARRAY_SIZE(unmap_pool))
  1019. return 0;
  1020. dmaengine_destroy_unmap_pool();
  1021. return -ENOMEM;
  1022. }
  1023. struct dmaengine_unmap_data *
  1024. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
  1025. {
  1026. struct dmaengine_unmap_data *unmap;
  1027. unmap = mempool_alloc(__get_unmap_pool(nr)->pool, flags);
  1028. if (!unmap)
  1029. return NULL;
  1030. memset(unmap, 0, sizeof(*unmap));
  1031. kref_init(&unmap->kref);
  1032. unmap->dev = dev;
  1033. unmap->map_cnt = nr;
  1034. return unmap;
  1035. }
  1036. EXPORT_SYMBOL(dmaengine_get_unmap_data);
  1037. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  1038. struct dma_chan *chan)
  1039. {
  1040. tx->chan = chan;
  1041. #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  1042. spin_lock_init(&tx->lock);
  1043. #endif
  1044. }
  1045. EXPORT_SYMBOL(dma_async_tx_descriptor_init);
  1046. /* dma_wait_for_async_tx - spin wait for a transaction to complete
  1047. * @tx: in-flight transaction to wait on
  1048. */
  1049. enum dma_status
  1050. dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  1051. {
  1052. unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000);
  1053. if (!tx)
  1054. return DMA_COMPLETE;
  1055. while (tx->cookie == -EBUSY) {
  1056. if (time_after_eq(jiffies, dma_sync_wait_timeout)) {
  1057. dev_err(tx->chan->device->dev,
  1058. "%s timeout waiting for descriptor submission\n",
  1059. __func__);
  1060. return DMA_ERROR;
  1061. }
  1062. cpu_relax();
  1063. }
  1064. return dma_sync_wait(tx->chan, tx->cookie);
  1065. }
  1066. EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
  1067. /* dma_run_dependencies - helper routine for dma drivers to process
  1068. * (start) dependent operations on their target channel
  1069. * @tx: transaction with dependencies
  1070. */
  1071. void dma_run_dependencies(struct dma_async_tx_descriptor *tx)
  1072. {
  1073. struct dma_async_tx_descriptor *dep = txd_next(tx);
  1074. struct dma_async_tx_descriptor *dep_next;
  1075. struct dma_chan *chan;
  1076. if (!dep)
  1077. return;
  1078. /* we'll submit tx->next now, so clear the link */
  1079. txd_clear_next(tx);
  1080. chan = dep->chan;
  1081. /* keep submitting up until a channel switch is detected
  1082. * in that case we will be called again as a result of
  1083. * processing the interrupt from async_tx_channel_switch
  1084. */
  1085. for (; dep; dep = dep_next) {
  1086. txd_lock(dep);
  1087. txd_clear_parent(dep);
  1088. dep_next = txd_next(dep);
  1089. if (dep_next && dep_next->chan == chan)
  1090. txd_clear_next(dep); /* ->next will be submitted */
  1091. else
  1092. dep_next = NULL; /* submit current dep and terminate */
  1093. txd_unlock(dep);
  1094. dep->tx_submit(dep);
  1095. }
  1096. chan->device->device_issue_pending(chan);
  1097. }
  1098. EXPORT_SYMBOL_GPL(dma_run_dependencies);
  1099. static int __init dma_bus_init(void)
  1100. {
  1101. int err = dmaengine_init_unmap_pool();
  1102. if (err)
  1103. return err;
  1104. return class_register(&dma_devclass);
  1105. }
  1106. arch_initcall(dma_bus_init);