shdma-base.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * Dmaengine driver base library for DMA controllers, found on SH-based SoCs
  3. *
  4. * extracted from shdma.c
  5. *
  6. * Copyright (C) 2011-2012 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  7. * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
  8. * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
  9. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/shdma-base.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include "../dmaengine.h"
  25. /* DMA descriptor control */
  26. enum shdma_desc_status {
  27. DESC_IDLE,
  28. DESC_PREPARED,
  29. DESC_SUBMITTED,
  30. DESC_COMPLETED, /* completed, have to call callback */
  31. DESC_WAITING, /* callback called, waiting for ack / re-submit */
  32. };
  33. #define NR_DESCS_PER_CHANNEL 32
  34. #define to_shdma_chan(c) container_of(c, struct shdma_chan, dma_chan)
  35. #define to_shdma_dev(d) container_of(d, struct shdma_dev, dma_dev)
  36. /*
  37. * For slave DMA we assume, that there is a finite number of DMA slaves in the
  38. * system, and that each such slave can only use a finite number of channels.
  39. * We use slave channel IDs to make sure, that no such slave channel ID is
  40. * allocated more than once.
  41. */
  42. static unsigned int slave_num = 256;
  43. module_param(slave_num, uint, 0444);
  44. /* A bitmask with slave_num bits */
  45. static unsigned long *shdma_slave_used;
  46. /* Called under spin_lock_irq(&schan->chan_lock") */
  47. static void shdma_chan_xfer_ld_queue(struct shdma_chan *schan)
  48. {
  49. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  50. const struct shdma_ops *ops = sdev->ops;
  51. struct shdma_desc *sdesc;
  52. /* DMA work check */
  53. if (ops->channel_busy(schan))
  54. return;
  55. /* Find the first not transferred descriptor */
  56. list_for_each_entry(sdesc, &schan->ld_queue, node)
  57. if (sdesc->mark == DESC_SUBMITTED) {
  58. ops->start_xfer(schan, sdesc);
  59. break;
  60. }
  61. }
  62. static dma_cookie_t shdma_tx_submit(struct dma_async_tx_descriptor *tx)
  63. {
  64. struct shdma_desc *chunk, *c, *desc =
  65. container_of(tx, struct shdma_desc, async_tx);
  66. struct shdma_chan *schan = to_shdma_chan(tx->chan);
  67. dma_async_tx_callback callback = tx->callback;
  68. dma_cookie_t cookie;
  69. bool power_up;
  70. spin_lock_irq(&schan->chan_lock);
  71. power_up = list_empty(&schan->ld_queue);
  72. cookie = dma_cookie_assign(tx);
  73. /* Mark all chunks of this descriptor as submitted, move to the queue */
  74. list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
  75. /*
  76. * All chunks are on the global ld_free, so, we have to find
  77. * the end of the chain ourselves
  78. */
  79. if (chunk != desc && (chunk->mark == DESC_IDLE ||
  80. chunk->async_tx.cookie > 0 ||
  81. chunk->async_tx.cookie == -EBUSY ||
  82. &chunk->node == &schan->ld_free))
  83. break;
  84. chunk->mark = DESC_SUBMITTED;
  85. if (chunk->chunks == 1) {
  86. chunk->async_tx.callback = callback;
  87. chunk->async_tx.callback_param = tx->callback_param;
  88. } else {
  89. /* Callback goes to the last chunk */
  90. chunk->async_tx.callback = NULL;
  91. }
  92. chunk->cookie = cookie;
  93. list_move_tail(&chunk->node, &schan->ld_queue);
  94. dev_dbg(schan->dev, "submit #%d@%p on %d\n",
  95. tx->cookie, &chunk->async_tx, schan->id);
  96. }
  97. if (power_up) {
  98. int ret;
  99. schan->pm_state = SHDMA_PM_BUSY;
  100. ret = pm_runtime_get(schan->dev);
  101. spin_unlock_irq(&schan->chan_lock);
  102. if (ret < 0)
  103. dev_err(schan->dev, "%s(): GET = %d\n", __func__, ret);
  104. pm_runtime_barrier(schan->dev);
  105. spin_lock_irq(&schan->chan_lock);
  106. /* Have we been reset, while waiting? */
  107. if (schan->pm_state != SHDMA_PM_ESTABLISHED) {
  108. struct shdma_dev *sdev =
  109. to_shdma_dev(schan->dma_chan.device);
  110. const struct shdma_ops *ops = sdev->ops;
  111. dev_dbg(schan->dev, "Bring up channel %d\n",
  112. schan->id);
  113. /*
  114. * TODO: .xfer_setup() might fail on some platforms.
  115. * Make it int then, on error remove chunks from the
  116. * queue again
  117. */
  118. ops->setup_xfer(schan, schan->slave_id);
  119. if (schan->pm_state == SHDMA_PM_PENDING)
  120. shdma_chan_xfer_ld_queue(schan);
  121. schan->pm_state = SHDMA_PM_ESTABLISHED;
  122. }
  123. } else {
  124. /*
  125. * Tell .device_issue_pending() not to run the queue, interrupts
  126. * will do it anyway
  127. */
  128. schan->pm_state = SHDMA_PM_PENDING;
  129. }
  130. spin_unlock_irq(&schan->chan_lock);
  131. return cookie;
  132. }
  133. /* Called with desc_lock held */
  134. static struct shdma_desc *shdma_get_desc(struct shdma_chan *schan)
  135. {
  136. struct shdma_desc *sdesc;
  137. list_for_each_entry(sdesc, &schan->ld_free, node)
  138. if (sdesc->mark != DESC_PREPARED) {
  139. BUG_ON(sdesc->mark != DESC_IDLE);
  140. list_del(&sdesc->node);
  141. return sdesc;
  142. }
  143. return NULL;
  144. }
  145. static int shdma_setup_slave(struct shdma_chan *schan, dma_addr_t slave_addr)
  146. {
  147. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  148. const struct shdma_ops *ops = sdev->ops;
  149. int ret, match;
  150. if (schan->dev->of_node) {
  151. match = schan->hw_req;
  152. ret = ops->set_slave(schan, match, slave_addr, true);
  153. if (ret < 0)
  154. return ret;
  155. } else {
  156. match = schan->real_slave_id;
  157. }
  158. if (schan->real_slave_id < 0 || schan->real_slave_id >= slave_num)
  159. return -EINVAL;
  160. if (test_and_set_bit(schan->real_slave_id, shdma_slave_used))
  161. return -EBUSY;
  162. ret = ops->set_slave(schan, match, slave_addr, false);
  163. if (ret < 0) {
  164. clear_bit(schan->real_slave_id, shdma_slave_used);
  165. return ret;
  166. }
  167. schan->slave_id = schan->real_slave_id;
  168. return 0;
  169. }
  170. static int shdma_alloc_chan_resources(struct dma_chan *chan)
  171. {
  172. struct shdma_chan *schan = to_shdma_chan(chan);
  173. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  174. const struct shdma_ops *ops = sdev->ops;
  175. struct shdma_desc *desc;
  176. struct shdma_slave *slave = chan->private;
  177. int ret, i;
  178. /*
  179. * This relies on the guarantee from dmaengine that alloc_chan_resources
  180. * never runs concurrently with itself or free_chan_resources.
  181. */
  182. if (slave) {
  183. /* Legacy mode: .private is set in filter */
  184. schan->real_slave_id = slave->slave_id;
  185. ret = shdma_setup_slave(schan, 0);
  186. if (ret < 0)
  187. goto esetslave;
  188. } else {
  189. /* Normal mode: real_slave_id was set by filter */
  190. schan->slave_id = -EINVAL;
  191. }
  192. schan->desc = kcalloc(NR_DESCS_PER_CHANNEL,
  193. sdev->desc_size, GFP_KERNEL);
  194. if (!schan->desc) {
  195. ret = -ENOMEM;
  196. goto edescalloc;
  197. }
  198. schan->desc_num = NR_DESCS_PER_CHANNEL;
  199. for (i = 0; i < NR_DESCS_PER_CHANNEL; i++) {
  200. desc = ops->embedded_desc(schan->desc, i);
  201. dma_async_tx_descriptor_init(&desc->async_tx,
  202. &schan->dma_chan);
  203. desc->async_tx.tx_submit = shdma_tx_submit;
  204. desc->mark = DESC_IDLE;
  205. list_add(&desc->node, &schan->ld_free);
  206. }
  207. return NR_DESCS_PER_CHANNEL;
  208. edescalloc:
  209. if (slave)
  210. esetslave:
  211. clear_bit(slave->slave_id, shdma_slave_used);
  212. chan->private = NULL;
  213. return ret;
  214. }
  215. /*
  216. * This is the standard shdma filter function to be used as a replacement to the
  217. * "old" method, using the .private pointer.
  218. * You always have to pass a valid slave id as the argument, old drivers that
  219. * pass ERR_PTR(-EINVAL) as a filter parameter and set it up in dma_slave_config
  220. * need to be updated so we can remove the slave_id field from dma_slave_config.
  221. * parameter. If this filter is used, the slave driver, after calling
  222. * dma_request_channel(), will also have to call dmaengine_slave_config() with
  223. * .direction, and either .src_addr or .dst_addr set.
  224. *
  225. * NOTE: this filter doesn't support multiple DMAC drivers with the DMA_SLAVE
  226. * capability! If this becomes a requirement, hardware glue drivers, using this
  227. * services would have to provide their own filters, which first would check
  228. * the device driver, similar to how other DMAC drivers, e.g., sa11x0-dma.c, do
  229. * this, and only then, in case of a match, call this common filter.
  230. * NOTE 2: This filter function is also used in the DT case by shdma_of_xlate().
  231. * In that case the MID-RID value is used for slave channel filtering and is
  232. * passed to this function in the "arg" parameter.
  233. */
  234. bool shdma_chan_filter(struct dma_chan *chan, void *arg)
  235. {
  236. struct shdma_chan *schan;
  237. struct shdma_dev *sdev;
  238. int slave_id = (long)arg;
  239. int ret;
  240. /* Only support channels handled by this driver. */
  241. if (chan->device->device_alloc_chan_resources !=
  242. shdma_alloc_chan_resources)
  243. return false;
  244. schan = to_shdma_chan(chan);
  245. sdev = to_shdma_dev(chan->device);
  246. /*
  247. * For DT, the schan->slave_id field is generated by the
  248. * set_slave function from the slave ID that is passed in
  249. * from xlate. For the non-DT case, the slave ID is
  250. * directly passed into the filter function by the driver
  251. */
  252. if (schan->dev->of_node) {
  253. ret = sdev->ops->set_slave(schan, slave_id, 0, true);
  254. if (ret < 0)
  255. return false;
  256. schan->real_slave_id = schan->slave_id;
  257. return true;
  258. }
  259. if (slave_id < 0) {
  260. /* No slave requested - arbitrary channel */
  261. dev_warn(sdev->dma_dev.dev, "invalid slave ID passed to dma_request_slave\n");
  262. return true;
  263. }
  264. if (slave_id >= slave_num)
  265. return false;
  266. ret = sdev->ops->set_slave(schan, slave_id, 0, true);
  267. if (ret < 0)
  268. return false;
  269. schan->real_slave_id = slave_id;
  270. return true;
  271. }
  272. EXPORT_SYMBOL(shdma_chan_filter);
  273. static dma_async_tx_callback __ld_cleanup(struct shdma_chan *schan, bool all)
  274. {
  275. struct shdma_desc *desc, *_desc;
  276. /* Is the "exposed" head of a chain acked? */
  277. bool head_acked = false;
  278. dma_cookie_t cookie = 0;
  279. dma_async_tx_callback callback = NULL;
  280. struct dmaengine_desc_callback cb;
  281. unsigned long flags;
  282. LIST_HEAD(cyclic_list);
  283. memset(&cb, 0, sizeof(cb));
  284. spin_lock_irqsave(&schan->chan_lock, flags);
  285. list_for_each_entry_safe(desc, _desc, &schan->ld_queue, node) {
  286. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  287. BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
  288. BUG_ON(desc->mark != DESC_SUBMITTED &&
  289. desc->mark != DESC_COMPLETED &&
  290. desc->mark != DESC_WAITING);
  291. /*
  292. * queue is ordered, and we use this loop to (1) clean up all
  293. * completed descriptors, and to (2) update descriptor flags of
  294. * any chunks in a (partially) completed chain
  295. */
  296. if (!all && desc->mark == DESC_SUBMITTED &&
  297. desc->cookie != cookie)
  298. break;
  299. if (tx->cookie > 0)
  300. cookie = tx->cookie;
  301. if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
  302. if (schan->dma_chan.completed_cookie != desc->cookie - 1)
  303. dev_dbg(schan->dev,
  304. "Completing cookie %d, expected %d\n",
  305. desc->cookie,
  306. schan->dma_chan.completed_cookie + 1);
  307. schan->dma_chan.completed_cookie = desc->cookie;
  308. }
  309. /* Call callback on the last chunk */
  310. if (desc->mark == DESC_COMPLETED && tx->callback) {
  311. desc->mark = DESC_WAITING;
  312. dmaengine_desc_get_callback(tx, &cb);
  313. callback = tx->callback;
  314. dev_dbg(schan->dev, "descriptor #%d@%p on %d callback\n",
  315. tx->cookie, tx, schan->id);
  316. BUG_ON(desc->chunks != 1);
  317. break;
  318. }
  319. if (tx->cookie > 0 || tx->cookie == -EBUSY) {
  320. if (desc->mark == DESC_COMPLETED) {
  321. BUG_ON(tx->cookie < 0);
  322. desc->mark = DESC_WAITING;
  323. }
  324. head_acked = async_tx_test_ack(tx);
  325. } else {
  326. switch (desc->mark) {
  327. case DESC_COMPLETED:
  328. desc->mark = DESC_WAITING;
  329. /* Fall through */
  330. case DESC_WAITING:
  331. if (head_acked)
  332. async_tx_ack(&desc->async_tx);
  333. }
  334. }
  335. dev_dbg(schan->dev, "descriptor %p #%d completed.\n",
  336. tx, tx->cookie);
  337. if (((desc->mark == DESC_COMPLETED ||
  338. desc->mark == DESC_WAITING) &&
  339. async_tx_test_ack(&desc->async_tx)) || all) {
  340. if (all || !desc->cyclic) {
  341. /* Remove from ld_queue list */
  342. desc->mark = DESC_IDLE;
  343. list_move(&desc->node, &schan->ld_free);
  344. } else {
  345. /* reuse as cyclic */
  346. desc->mark = DESC_SUBMITTED;
  347. list_move_tail(&desc->node, &cyclic_list);
  348. }
  349. if (list_empty(&schan->ld_queue)) {
  350. dev_dbg(schan->dev, "Bring down channel %d\n", schan->id);
  351. pm_runtime_put(schan->dev);
  352. schan->pm_state = SHDMA_PM_ESTABLISHED;
  353. } else if (schan->pm_state == SHDMA_PM_PENDING) {
  354. shdma_chan_xfer_ld_queue(schan);
  355. }
  356. }
  357. }
  358. if (all && !callback)
  359. /*
  360. * Terminating and the loop completed normally: forgive
  361. * uncompleted cookies
  362. */
  363. schan->dma_chan.completed_cookie = schan->dma_chan.cookie;
  364. list_splice_tail(&cyclic_list, &schan->ld_queue);
  365. spin_unlock_irqrestore(&schan->chan_lock, flags);
  366. dmaengine_desc_callback_invoke(&cb, NULL);
  367. return callback;
  368. }
  369. /*
  370. * shdma_chan_ld_cleanup - Clean up link descriptors
  371. *
  372. * Clean up the ld_queue of DMA channel.
  373. */
  374. static void shdma_chan_ld_cleanup(struct shdma_chan *schan, bool all)
  375. {
  376. while (__ld_cleanup(schan, all))
  377. ;
  378. }
  379. /*
  380. * shdma_free_chan_resources - Free all resources of the channel.
  381. */
  382. static void shdma_free_chan_resources(struct dma_chan *chan)
  383. {
  384. struct shdma_chan *schan = to_shdma_chan(chan);
  385. struct shdma_dev *sdev = to_shdma_dev(chan->device);
  386. const struct shdma_ops *ops = sdev->ops;
  387. LIST_HEAD(list);
  388. /* Protect against ISR */
  389. spin_lock_irq(&schan->chan_lock);
  390. ops->halt_channel(schan);
  391. spin_unlock_irq(&schan->chan_lock);
  392. /* Now no new interrupts will occur */
  393. /* Prepared and not submitted descriptors can still be on the queue */
  394. if (!list_empty(&schan->ld_queue))
  395. shdma_chan_ld_cleanup(schan, true);
  396. if (schan->slave_id >= 0) {
  397. /* The caller is holding dma_list_mutex */
  398. clear_bit(schan->slave_id, shdma_slave_used);
  399. chan->private = NULL;
  400. }
  401. schan->real_slave_id = 0;
  402. spin_lock_irq(&schan->chan_lock);
  403. list_splice_init(&schan->ld_free, &list);
  404. schan->desc_num = 0;
  405. spin_unlock_irq(&schan->chan_lock);
  406. kfree(schan->desc);
  407. }
  408. /**
  409. * shdma_add_desc - get, set up and return one transfer descriptor
  410. * @schan: DMA channel
  411. * @flags: DMA transfer flags
  412. * @dst: destination DMA address, incremented when direction equals
  413. * DMA_DEV_TO_MEM or DMA_MEM_TO_MEM
  414. * @src: source DMA address, incremented when direction equals
  415. * DMA_MEM_TO_DEV or DMA_MEM_TO_MEM
  416. * @len: DMA transfer length
  417. * @first: if NULL, set to the current descriptor and cookie set to -EBUSY
  418. * @direction: needed for slave DMA to decide which address to keep constant,
  419. * equals DMA_MEM_TO_MEM for MEMCPY
  420. * Returns 0 or an error
  421. * Locks: called with desc_lock held
  422. */
  423. static struct shdma_desc *shdma_add_desc(struct shdma_chan *schan,
  424. unsigned long flags, dma_addr_t *dst, dma_addr_t *src, size_t *len,
  425. struct shdma_desc **first, enum dma_transfer_direction direction)
  426. {
  427. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  428. const struct shdma_ops *ops = sdev->ops;
  429. struct shdma_desc *new;
  430. size_t copy_size = *len;
  431. if (!copy_size)
  432. return NULL;
  433. /* Allocate the link descriptor from the free list */
  434. new = shdma_get_desc(schan);
  435. if (!new) {
  436. dev_err(schan->dev, "No free link descriptor available\n");
  437. return NULL;
  438. }
  439. ops->desc_setup(schan, new, *src, *dst, &copy_size);
  440. if (!*first) {
  441. /* First desc */
  442. new->async_tx.cookie = -EBUSY;
  443. *first = new;
  444. } else {
  445. /* Other desc - invisible to the user */
  446. new->async_tx.cookie = -EINVAL;
  447. }
  448. dev_dbg(schan->dev,
  449. "chaining (%zu/%zu)@%pad -> %pad with %p, cookie %d\n",
  450. copy_size, *len, src, dst, &new->async_tx,
  451. new->async_tx.cookie);
  452. new->mark = DESC_PREPARED;
  453. new->async_tx.flags = flags;
  454. new->direction = direction;
  455. new->partial = 0;
  456. *len -= copy_size;
  457. if (direction == DMA_MEM_TO_MEM || direction == DMA_MEM_TO_DEV)
  458. *src += copy_size;
  459. if (direction == DMA_MEM_TO_MEM || direction == DMA_DEV_TO_MEM)
  460. *dst += copy_size;
  461. return new;
  462. }
  463. /*
  464. * shdma_prep_sg - prepare transfer descriptors from an SG list
  465. *
  466. * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
  467. * converted to scatter-gather to guarantee consistent locking and a correct
  468. * list manipulation. For slave DMA direction carries the usual meaning, and,
  469. * logically, the SG list is RAM and the addr variable contains slave address,
  470. * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
  471. * and the SG list contains only one element and points at the source buffer.
  472. */
  473. static struct dma_async_tx_descriptor *shdma_prep_sg(struct shdma_chan *schan,
  474. struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr,
  475. enum dma_transfer_direction direction, unsigned long flags, bool cyclic)
  476. {
  477. struct scatterlist *sg;
  478. struct shdma_desc *first = NULL, *new = NULL /* compiler... */;
  479. LIST_HEAD(tx_list);
  480. int chunks = 0;
  481. unsigned long irq_flags;
  482. int i;
  483. for_each_sg(sgl, sg, sg_len, i)
  484. chunks += DIV_ROUND_UP(sg_dma_len(sg), schan->max_xfer_len);
  485. /* Have to lock the whole loop to protect against concurrent release */
  486. spin_lock_irqsave(&schan->chan_lock, irq_flags);
  487. /*
  488. * Chaining:
  489. * first descriptor is what user is dealing with in all API calls, its
  490. * cookie is at first set to -EBUSY, at tx-submit to a positive
  491. * number
  492. * if more than one chunk is needed further chunks have cookie = -EINVAL
  493. * the last chunk, if not equal to the first, has cookie = -ENOSPC
  494. * all chunks are linked onto the tx_list head with their .node heads
  495. * only during this function, then they are immediately spliced
  496. * back onto the free list in form of a chain
  497. */
  498. for_each_sg(sgl, sg, sg_len, i) {
  499. dma_addr_t sg_addr = sg_dma_address(sg);
  500. size_t len = sg_dma_len(sg);
  501. if (!len)
  502. goto err_get_desc;
  503. do {
  504. dev_dbg(schan->dev, "Add SG #%d@%p[%zu], dma %pad\n",
  505. i, sg, len, &sg_addr);
  506. if (direction == DMA_DEV_TO_MEM)
  507. new = shdma_add_desc(schan, flags,
  508. &sg_addr, addr, &len, &first,
  509. direction);
  510. else
  511. new = shdma_add_desc(schan, flags,
  512. addr, &sg_addr, &len, &first,
  513. direction);
  514. if (!new)
  515. goto err_get_desc;
  516. new->cyclic = cyclic;
  517. if (cyclic)
  518. new->chunks = 1;
  519. else
  520. new->chunks = chunks--;
  521. list_add_tail(&new->node, &tx_list);
  522. } while (len);
  523. }
  524. if (new != first)
  525. new->async_tx.cookie = -ENOSPC;
  526. /* Put them back on the free list, so, they don't get lost */
  527. list_splice_tail(&tx_list, &schan->ld_free);
  528. spin_unlock_irqrestore(&schan->chan_lock, irq_flags);
  529. return &first->async_tx;
  530. err_get_desc:
  531. list_for_each_entry(new, &tx_list, node)
  532. new->mark = DESC_IDLE;
  533. list_splice(&tx_list, &schan->ld_free);
  534. spin_unlock_irqrestore(&schan->chan_lock, irq_flags);
  535. return NULL;
  536. }
  537. static struct dma_async_tx_descriptor *shdma_prep_memcpy(
  538. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  539. size_t len, unsigned long flags)
  540. {
  541. struct shdma_chan *schan = to_shdma_chan(chan);
  542. struct scatterlist sg;
  543. if (!chan || !len)
  544. return NULL;
  545. BUG_ON(!schan->desc_num);
  546. sg_init_table(&sg, 1);
  547. sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len,
  548. offset_in_page(dma_src));
  549. sg_dma_address(&sg) = dma_src;
  550. sg_dma_len(&sg) = len;
  551. return shdma_prep_sg(schan, &sg, 1, &dma_dest, DMA_MEM_TO_MEM,
  552. flags, false);
  553. }
  554. static struct dma_async_tx_descriptor *shdma_prep_slave_sg(
  555. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  556. enum dma_transfer_direction direction, unsigned long flags, void *context)
  557. {
  558. struct shdma_chan *schan = to_shdma_chan(chan);
  559. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  560. const struct shdma_ops *ops = sdev->ops;
  561. int slave_id = schan->slave_id;
  562. dma_addr_t slave_addr;
  563. if (!chan)
  564. return NULL;
  565. BUG_ON(!schan->desc_num);
  566. /* Someone calling slave DMA on a generic channel? */
  567. if (slave_id < 0 || !sg_len) {
  568. dev_warn(schan->dev, "%s: bad parameter: len=%d, id=%d\n",
  569. __func__, sg_len, slave_id);
  570. return NULL;
  571. }
  572. slave_addr = ops->slave_addr(schan);
  573. return shdma_prep_sg(schan, sgl, sg_len, &slave_addr,
  574. direction, flags, false);
  575. }
  576. #define SHDMA_MAX_SG_LEN 32
  577. static struct dma_async_tx_descriptor *shdma_prep_dma_cyclic(
  578. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  579. size_t period_len, enum dma_transfer_direction direction,
  580. unsigned long flags)
  581. {
  582. struct shdma_chan *schan = to_shdma_chan(chan);
  583. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  584. struct dma_async_tx_descriptor *desc;
  585. const struct shdma_ops *ops = sdev->ops;
  586. unsigned int sg_len = buf_len / period_len;
  587. int slave_id = schan->slave_id;
  588. dma_addr_t slave_addr;
  589. struct scatterlist *sgl;
  590. int i;
  591. if (!chan)
  592. return NULL;
  593. BUG_ON(!schan->desc_num);
  594. if (sg_len > SHDMA_MAX_SG_LEN) {
  595. dev_err(schan->dev, "sg length %d exceds limit %d",
  596. sg_len, SHDMA_MAX_SG_LEN);
  597. return NULL;
  598. }
  599. /* Someone calling slave DMA on a generic channel? */
  600. if (slave_id < 0 || (buf_len < period_len)) {
  601. dev_warn(schan->dev,
  602. "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n",
  603. __func__, buf_len, period_len, slave_id);
  604. return NULL;
  605. }
  606. slave_addr = ops->slave_addr(schan);
  607. /*
  608. * Allocate the sg list dynamically as it would consumer too much stack
  609. * space.
  610. */
  611. sgl = kcalloc(sg_len, sizeof(*sgl), GFP_KERNEL);
  612. if (!sgl)
  613. return NULL;
  614. sg_init_table(sgl, sg_len);
  615. for (i = 0; i < sg_len; i++) {
  616. dma_addr_t src = buf_addr + (period_len * i);
  617. sg_set_page(&sgl[i], pfn_to_page(PFN_DOWN(src)), period_len,
  618. offset_in_page(src));
  619. sg_dma_address(&sgl[i]) = src;
  620. sg_dma_len(&sgl[i]) = period_len;
  621. }
  622. desc = shdma_prep_sg(schan, sgl, sg_len, &slave_addr,
  623. direction, flags, true);
  624. kfree(sgl);
  625. return desc;
  626. }
  627. static int shdma_terminate_all(struct dma_chan *chan)
  628. {
  629. struct shdma_chan *schan = to_shdma_chan(chan);
  630. struct shdma_dev *sdev = to_shdma_dev(chan->device);
  631. const struct shdma_ops *ops = sdev->ops;
  632. unsigned long flags;
  633. spin_lock_irqsave(&schan->chan_lock, flags);
  634. ops->halt_channel(schan);
  635. if (ops->get_partial && !list_empty(&schan->ld_queue)) {
  636. /* Record partial transfer */
  637. struct shdma_desc *desc = list_first_entry(&schan->ld_queue,
  638. struct shdma_desc, node);
  639. desc->partial = ops->get_partial(schan, desc);
  640. }
  641. spin_unlock_irqrestore(&schan->chan_lock, flags);
  642. shdma_chan_ld_cleanup(schan, true);
  643. return 0;
  644. }
  645. static int shdma_config(struct dma_chan *chan,
  646. struct dma_slave_config *config)
  647. {
  648. struct shdma_chan *schan = to_shdma_chan(chan);
  649. /*
  650. * So far only .slave_id is used, but the slave drivers are
  651. * encouraged to also set a transfer direction and an address.
  652. */
  653. if (!config)
  654. return -EINVAL;
  655. /*
  656. * overriding the slave_id through dma_slave_config is deprecated,
  657. * but possibly some out-of-tree drivers still do it.
  658. */
  659. if (WARN_ON_ONCE(config->slave_id &&
  660. config->slave_id != schan->real_slave_id))
  661. schan->real_slave_id = config->slave_id;
  662. /*
  663. * We could lock this, but you shouldn't be configuring the
  664. * channel, while using it...
  665. */
  666. return shdma_setup_slave(schan,
  667. config->direction == DMA_DEV_TO_MEM ?
  668. config->src_addr : config->dst_addr);
  669. }
  670. static void shdma_issue_pending(struct dma_chan *chan)
  671. {
  672. struct shdma_chan *schan = to_shdma_chan(chan);
  673. spin_lock_irq(&schan->chan_lock);
  674. if (schan->pm_state == SHDMA_PM_ESTABLISHED)
  675. shdma_chan_xfer_ld_queue(schan);
  676. else
  677. schan->pm_state = SHDMA_PM_PENDING;
  678. spin_unlock_irq(&schan->chan_lock);
  679. }
  680. static enum dma_status shdma_tx_status(struct dma_chan *chan,
  681. dma_cookie_t cookie,
  682. struct dma_tx_state *txstate)
  683. {
  684. struct shdma_chan *schan = to_shdma_chan(chan);
  685. enum dma_status status;
  686. unsigned long flags;
  687. shdma_chan_ld_cleanup(schan, false);
  688. spin_lock_irqsave(&schan->chan_lock, flags);
  689. status = dma_cookie_status(chan, cookie, txstate);
  690. /*
  691. * If we don't find cookie on the queue, it has been aborted and we have
  692. * to report error
  693. */
  694. if (status != DMA_COMPLETE) {
  695. struct shdma_desc *sdesc;
  696. status = DMA_ERROR;
  697. list_for_each_entry(sdesc, &schan->ld_queue, node)
  698. if (sdesc->cookie == cookie) {
  699. status = DMA_IN_PROGRESS;
  700. break;
  701. }
  702. }
  703. spin_unlock_irqrestore(&schan->chan_lock, flags);
  704. return status;
  705. }
  706. /* Called from error IRQ or NMI */
  707. bool shdma_reset(struct shdma_dev *sdev)
  708. {
  709. const struct shdma_ops *ops = sdev->ops;
  710. struct shdma_chan *schan;
  711. unsigned int handled = 0;
  712. int i;
  713. /* Reset all channels */
  714. shdma_for_each_chan(schan, sdev, i) {
  715. struct shdma_desc *sdesc;
  716. LIST_HEAD(dl);
  717. if (!schan)
  718. continue;
  719. spin_lock(&schan->chan_lock);
  720. /* Stop the channel */
  721. ops->halt_channel(schan);
  722. list_splice_init(&schan->ld_queue, &dl);
  723. if (!list_empty(&dl)) {
  724. dev_dbg(schan->dev, "Bring down channel %d\n", schan->id);
  725. pm_runtime_put(schan->dev);
  726. }
  727. schan->pm_state = SHDMA_PM_ESTABLISHED;
  728. spin_unlock(&schan->chan_lock);
  729. /* Complete all */
  730. list_for_each_entry(sdesc, &dl, node) {
  731. struct dma_async_tx_descriptor *tx = &sdesc->async_tx;
  732. sdesc->mark = DESC_IDLE;
  733. dmaengine_desc_get_callback_invoke(tx, NULL);
  734. }
  735. spin_lock(&schan->chan_lock);
  736. list_splice(&dl, &schan->ld_free);
  737. spin_unlock(&schan->chan_lock);
  738. handled++;
  739. }
  740. return !!handled;
  741. }
  742. EXPORT_SYMBOL(shdma_reset);
  743. static irqreturn_t chan_irq(int irq, void *dev)
  744. {
  745. struct shdma_chan *schan = dev;
  746. const struct shdma_ops *ops =
  747. to_shdma_dev(schan->dma_chan.device)->ops;
  748. irqreturn_t ret;
  749. spin_lock(&schan->chan_lock);
  750. ret = ops->chan_irq(schan, irq) ? IRQ_WAKE_THREAD : IRQ_NONE;
  751. spin_unlock(&schan->chan_lock);
  752. return ret;
  753. }
  754. static irqreturn_t chan_irqt(int irq, void *dev)
  755. {
  756. struct shdma_chan *schan = dev;
  757. const struct shdma_ops *ops =
  758. to_shdma_dev(schan->dma_chan.device)->ops;
  759. struct shdma_desc *sdesc;
  760. spin_lock_irq(&schan->chan_lock);
  761. list_for_each_entry(sdesc, &schan->ld_queue, node) {
  762. if (sdesc->mark == DESC_SUBMITTED &&
  763. ops->desc_completed(schan, sdesc)) {
  764. dev_dbg(schan->dev, "done #%d@%p\n",
  765. sdesc->async_tx.cookie, &sdesc->async_tx);
  766. sdesc->mark = DESC_COMPLETED;
  767. break;
  768. }
  769. }
  770. /* Next desc */
  771. shdma_chan_xfer_ld_queue(schan);
  772. spin_unlock_irq(&schan->chan_lock);
  773. shdma_chan_ld_cleanup(schan, false);
  774. return IRQ_HANDLED;
  775. }
  776. int shdma_request_irq(struct shdma_chan *schan, int irq,
  777. unsigned long flags, const char *name)
  778. {
  779. int ret = devm_request_threaded_irq(schan->dev, irq, chan_irq,
  780. chan_irqt, flags, name, schan);
  781. schan->irq = ret < 0 ? ret : irq;
  782. return ret;
  783. }
  784. EXPORT_SYMBOL(shdma_request_irq);
  785. void shdma_chan_probe(struct shdma_dev *sdev,
  786. struct shdma_chan *schan, int id)
  787. {
  788. schan->pm_state = SHDMA_PM_ESTABLISHED;
  789. /* reference struct dma_device */
  790. schan->dma_chan.device = &sdev->dma_dev;
  791. dma_cookie_init(&schan->dma_chan);
  792. schan->dev = sdev->dma_dev.dev;
  793. schan->id = id;
  794. if (!schan->max_xfer_len)
  795. schan->max_xfer_len = PAGE_SIZE;
  796. spin_lock_init(&schan->chan_lock);
  797. /* Init descripter manage list */
  798. INIT_LIST_HEAD(&schan->ld_queue);
  799. INIT_LIST_HEAD(&schan->ld_free);
  800. /* Add the channel to DMA device channel list */
  801. list_add_tail(&schan->dma_chan.device_node,
  802. &sdev->dma_dev.channels);
  803. sdev->schan[id] = schan;
  804. }
  805. EXPORT_SYMBOL(shdma_chan_probe);
  806. void shdma_chan_remove(struct shdma_chan *schan)
  807. {
  808. list_del(&schan->dma_chan.device_node);
  809. }
  810. EXPORT_SYMBOL(shdma_chan_remove);
  811. int shdma_init(struct device *dev, struct shdma_dev *sdev,
  812. int chan_num)
  813. {
  814. struct dma_device *dma_dev = &sdev->dma_dev;
  815. /*
  816. * Require all call-backs for now, they can trivially be made optional
  817. * later as required
  818. */
  819. if (!sdev->ops ||
  820. !sdev->desc_size ||
  821. !sdev->ops->embedded_desc ||
  822. !sdev->ops->start_xfer ||
  823. !sdev->ops->setup_xfer ||
  824. !sdev->ops->set_slave ||
  825. !sdev->ops->desc_setup ||
  826. !sdev->ops->slave_addr ||
  827. !sdev->ops->channel_busy ||
  828. !sdev->ops->halt_channel ||
  829. !sdev->ops->desc_completed)
  830. return -EINVAL;
  831. sdev->schan = kcalloc(chan_num, sizeof(*sdev->schan), GFP_KERNEL);
  832. if (!sdev->schan)
  833. return -ENOMEM;
  834. INIT_LIST_HEAD(&dma_dev->channels);
  835. /* Common and MEMCPY operations */
  836. dma_dev->device_alloc_chan_resources
  837. = shdma_alloc_chan_resources;
  838. dma_dev->device_free_chan_resources = shdma_free_chan_resources;
  839. dma_dev->device_prep_dma_memcpy = shdma_prep_memcpy;
  840. dma_dev->device_tx_status = shdma_tx_status;
  841. dma_dev->device_issue_pending = shdma_issue_pending;
  842. /* Compulsory for DMA_SLAVE fields */
  843. dma_dev->device_prep_slave_sg = shdma_prep_slave_sg;
  844. dma_dev->device_prep_dma_cyclic = shdma_prep_dma_cyclic;
  845. dma_dev->device_config = shdma_config;
  846. dma_dev->device_terminate_all = shdma_terminate_all;
  847. dma_dev->dev = dev;
  848. return 0;
  849. }
  850. EXPORT_SYMBOL(shdma_init);
  851. void shdma_cleanup(struct shdma_dev *sdev)
  852. {
  853. kfree(sdev->schan);
  854. }
  855. EXPORT_SYMBOL(shdma_cleanup);
  856. static int __init shdma_enter(void)
  857. {
  858. shdma_slave_used = kcalloc(DIV_ROUND_UP(slave_num, BITS_PER_LONG),
  859. sizeof(long),
  860. GFP_KERNEL);
  861. if (!shdma_slave_used)
  862. return -ENOMEM;
  863. return 0;
  864. }
  865. module_init(shdma_enter);
  866. static void __exit shdma_exit(void)
  867. {
  868. kfree(shdma_slave_used);
  869. }
  870. module_exit(shdma_exit);
  871. MODULE_LICENSE("GPL v2");
  872. MODULE_DESCRIPTION("SH-DMA driver base library");
  873. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");