dmaengine.c 29 KB

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