dmaengine.c 29 KB

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