dmaengine.c 32 KB

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