shdma-base.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. *last = desc;
  67. struct shdma_chan *schan = to_shdma_chan(tx->chan);
  68. struct shdma_slave *slave = tx->chan->private;
  69. dma_async_tx_callback callback = tx->callback;
  70. dma_cookie_t cookie;
  71. bool power_up;
  72. spin_lock_irq(&schan->chan_lock);
  73. power_up = list_empty(&schan->ld_queue);
  74. cookie = dma_cookie_assign(tx);
  75. /* Mark all chunks of this descriptor as submitted, move to the queue */
  76. list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
  77. /*
  78. * All chunks are on the global ld_free, so, we have to find
  79. * the end of the chain ourselves
  80. */
  81. if (chunk != desc && (chunk->mark == DESC_IDLE ||
  82. chunk->async_tx.cookie > 0 ||
  83. chunk->async_tx.cookie == -EBUSY ||
  84. &chunk->node == &schan->ld_free))
  85. break;
  86. chunk->mark = DESC_SUBMITTED;
  87. /* Callback goes to the last chunk */
  88. chunk->async_tx.callback = NULL;
  89. chunk->cookie = cookie;
  90. list_move_tail(&chunk->node, &schan->ld_queue);
  91. last = chunk;
  92. dev_dbg(schan->dev, "submit #%d@%p on %d\n",
  93. tx->cookie, &last->async_tx, schan->id);
  94. }
  95. last->async_tx.callback = callback;
  96. last->async_tx.callback_param = tx->callback_param;
  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, slave);
  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_alloc_chan_resources(struct dma_chan *chan)
  146. {
  147. struct shdma_chan *schan = to_shdma_chan(chan);
  148. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  149. const struct shdma_ops *ops = sdev->ops;
  150. struct shdma_desc *desc;
  151. struct shdma_slave *slave = chan->private;
  152. int ret, i;
  153. /*
  154. * This relies on the guarantee from dmaengine that alloc_chan_resources
  155. * never runs concurrently with itself or free_chan_resources.
  156. */
  157. if (slave) {
  158. if (slave->slave_id >= slave_num) {
  159. ret = -EINVAL;
  160. goto evalid;
  161. }
  162. if (test_and_set_bit(slave->slave_id, shdma_slave_used)) {
  163. ret = -EBUSY;
  164. goto etestused;
  165. }
  166. ret = ops->set_slave(schan, slave);
  167. if (ret < 0)
  168. goto esetslave;
  169. }
  170. schan->desc = kcalloc(NR_DESCS_PER_CHANNEL,
  171. sdev->desc_size, GFP_KERNEL);
  172. if (!schan->desc) {
  173. ret = -ENOMEM;
  174. goto edescalloc;
  175. }
  176. schan->desc_num = NR_DESCS_PER_CHANNEL;
  177. for (i = 0; i < NR_DESCS_PER_CHANNEL; i++) {
  178. desc = ops->embedded_desc(schan->desc, i);
  179. dma_async_tx_descriptor_init(&desc->async_tx,
  180. &schan->dma_chan);
  181. desc->async_tx.tx_submit = shdma_tx_submit;
  182. desc->mark = DESC_IDLE;
  183. list_add(&desc->node, &schan->ld_free);
  184. }
  185. return NR_DESCS_PER_CHANNEL;
  186. edescalloc:
  187. if (slave)
  188. esetslave:
  189. clear_bit(slave->slave_id, shdma_slave_used);
  190. etestused:
  191. evalid:
  192. chan->private = NULL;
  193. return ret;
  194. }
  195. static dma_async_tx_callback __ld_cleanup(struct shdma_chan *schan, bool all)
  196. {
  197. struct shdma_desc *desc, *_desc;
  198. /* Is the "exposed" head of a chain acked? */
  199. bool head_acked = false;
  200. dma_cookie_t cookie = 0;
  201. dma_async_tx_callback callback = NULL;
  202. void *param = NULL;
  203. unsigned long flags;
  204. spin_lock_irqsave(&schan->chan_lock, flags);
  205. list_for_each_entry_safe(desc, _desc, &schan->ld_queue, node) {
  206. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  207. BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
  208. BUG_ON(desc->mark != DESC_SUBMITTED &&
  209. desc->mark != DESC_COMPLETED &&
  210. desc->mark != DESC_WAITING);
  211. /*
  212. * queue is ordered, and we use this loop to (1) clean up all
  213. * completed descriptors, and to (2) update descriptor flags of
  214. * any chunks in a (partially) completed chain
  215. */
  216. if (!all && desc->mark == DESC_SUBMITTED &&
  217. desc->cookie != cookie)
  218. break;
  219. if (tx->cookie > 0)
  220. cookie = tx->cookie;
  221. if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
  222. if (schan->dma_chan.completed_cookie != desc->cookie - 1)
  223. dev_dbg(schan->dev,
  224. "Completing cookie %d, expected %d\n",
  225. desc->cookie,
  226. schan->dma_chan.completed_cookie + 1);
  227. schan->dma_chan.completed_cookie = desc->cookie;
  228. }
  229. /* Call callback on the last chunk */
  230. if (desc->mark == DESC_COMPLETED && tx->callback) {
  231. desc->mark = DESC_WAITING;
  232. callback = tx->callback;
  233. param = tx->callback_param;
  234. dev_dbg(schan->dev, "descriptor #%d@%p on %d callback\n",
  235. tx->cookie, tx, schan->id);
  236. BUG_ON(desc->chunks != 1);
  237. break;
  238. }
  239. if (tx->cookie > 0 || tx->cookie == -EBUSY) {
  240. if (desc->mark == DESC_COMPLETED) {
  241. BUG_ON(tx->cookie < 0);
  242. desc->mark = DESC_WAITING;
  243. }
  244. head_acked = async_tx_test_ack(tx);
  245. } else {
  246. switch (desc->mark) {
  247. case DESC_COMPLETED:
  248. desc->mark = DESC_WAITING;
  249. /* Fall through */
  250. case DESC_WAITING:
  251. if (head_acked)
  252. async_tx_ack(&desc->async_tx);
  253. }
  254. }
  255. dev_dbg(schan->dev, "descriptor %p #%d completed.\n",
  256. tx, tx->cookie);
  257. if (((desc->mark == DESC_COMPLETED ||
  258. desc->mark == DESC_WAITING) &&
  259. async_tx_test_ack(&desc->async_tx)) || all) {
  260. /* Remove from ld_queue list */
  261. desc->mark = DESC_IDLE;
  262. list_move(&desc->node, &schan->ld_free);
  263. if (list_empty(&schan->ld_queue)) {
  264. dev_dbg(schan->dev, "Bring down channel %d\n", schan->id);
  265. pm_runtime_put(schan->dev);
  266. schan->pm_state = SHDMA_PM_ESTABLISHED;
  267. }
  268. }
  269. }
  270. if (all && !callback)
  271. /*
  272. * Terminating and the loop completed normally: forgive
  273. * uncompleted cookies
  274. */
  275. schan->dma_chan.completed_cookie = schan->dma_chan.cookie;
  276. spin_unlock_irqrestore(&schan->chan_lock, flags);
  277. if (callback)
  278. callback(param);
  279. return callback;
  280. }
  281. /*
  282. * shdma_chan_ld_cleanup - Clean up link descriptors
  283. *
  284. * Clean up the ld_queue of DMA channel.
  285. */
  286. static void shdma_chan_ld_cleanup(struct shdma_chan *schan, bool all)
  287. {
  288. while (__ld_cleanup(schan, all))
  289. ;
  290. }
  291. /*
  292. * shdma_free_chan_resources - Free all resources of the channel.
  293. */
  294. static void shdma_free_chan_resources(struct dma_chan *chan)
  295. {
  296. struct shdma_chan *schan = to_shdma_chan(chan);
  297. struct shdma_dev *sdev = to_shdma_dev(chan->device);
  298. const struct shdma_ops *ops = sdev->ops;
  299. LIST_HEAD(list);
  300. /* Protect against ISR */
  301. spin_lock_irq(&schan->chan_lock);
  302. ops->halt_channel(schan);
  303. spin_unlock_irq(&schan->chan_lock);
  304. /* Now no new interrupts will occur */
  305. /* Prepared and not submitted descriptors can still be on the queue */
  306. if (!list_empty(&schan->ld_queue))
  307. shdma_chan_ld_cleanup(schan, true);
  308. if (chan->private) {
  309. /* The caller is holding dma_list_mutex */
  310. struct shdma_slave *slave = chan->private;
  311. clear_bit(slave->slave_id, shdma_slave_used);
  312. chan->private = NULL;
  313. }
  314. spin_lock_irq(&schan->chan_lock);
  315. list_splice_init(&schan->ld_free, &list);
  316. schan->desc_num = 0;
  317. spin_unlock_irq(&schan->chan_lock);
  318. kfree(schan->desc);
  319. }
  320. /**
  321. * shdma_add_desc - get, set up and return one transfer descriptor
  322. * @schan: DMA channel
  323. * @flags: DMA transfer flags
  324. * @dst: destination DMA address, incremented when direction equals
  325. * DMA_DEV_TO_MEM or DMA_MEM_TO_MEM
  326. * @src: source DMA address, incremented when direction equals
  327. * DMA_MEM_TO_DEV or DMA_MEM_TO_MEM
  328. * @len: DMA transfer length
  329. * @first: if NULL, set to the current descriptor and cookie set to -EBUSY
  330. * @direction: needed for slave DMA to decide which address to keep constant,
  331. * equals DMA_MEM_TO_MEM for MEMCPY
  332. * Returns 0 or an error
  333. * Locks: called with desc_lock held
  334. */
  335. static struct shdma_desc *shdma_add_desc(struct shdma_chan *schan,
  336. unsigned long flags, dma_addr_t *dst, dma_addr_t *src, size_t *len,
  337. struct shdma_desc **first, enum dma_transfer_direction direction)
  338. {
  339. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  340. const struct shdma_ops *ops = sdev->ops;
  341. struct shdma_desc *new;
  342. size_t copy_size = *len;
  343. if (!copy_size)
  344. return NULL;
  345. /* Allocate the link descriptor from the free list */
  346. new = shdma_get_desc(schan);
  347. if (!new) {
  348. dev_err(schan->dev, "No free link descriptor available\n");
  349. return NULL;
  350. }
  351. ops->desc_setup(schan, new, *src, *dst, &copy_size);
  352. if (!*first) {
  353. /* First desc */
  354. new->async_tx.cookie = -EBUSY;
  355. *first = new;
  356. } else {
  357. /* Other desc - invisible to the user */
  358. new->async_tx.cookie = -EINVAL;
  359. }
  360. dev_dbg(schan->dev,
  361. "chaining (%u/%u)@%x -> %x with %p, cookie %d\n",
  362. copy_size, *len, *src, *dst, &new->async_tx,
  363. new->async_tx.cookie);
  364. new->mark = DESC_PREPARED;
  365. new->async_tx.flags = flags;
  366. new->direction = direction;
  367. *len -= copy_size;
  368. if (direction == DMA_MEM_TO_MEM || direction == DMA_MEM_TO_DEV)
  369. *src += copy_size;
  370. if (direction == DMA_MEM_TO_MEM || direction == DMA_DEV_TO_MEM)
  371. *dst += copy_size;
  372. return new;
  373. }
  374. /*
  375. * shdma_prep_sg - prepare transfer descriptors from an SG list
  376. *
  377. * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
  378. * converted to scatter-gather to guarantee consistent locking and a correct
  379. * list manipulation. For slave DMA direction carries the usual meaning, and,
  380. * logically, the SG list is RAM and the addr variable contains slave address,
  381. * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
  382. * and the SG list contains only one element and points at the source buffer.
  383. */
  384. static struct dma_async_tx_descriptor *shdma_prep_sg(struct shdma_chan *schan,
  385. struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr,
  386. enum dma_transfer_direction direction, unsigned long flags)
  387. {
  388. struct scatterlist *sg;
  389. struct shdma_desc *first = NULL, *new = NULL /* compiler... */;
  390. LIST_HEAD(tx_list);
  391. int chunks = 0;
  392. unsigned long irq_flags;
  393. int i;
  394. for_each_sg(sgl, sg, sg_len, i)
  395. chunks += DIV_ROUND_UP(sg_dma_len(sg), schan->max_xfer_len);
  396. /* Have to lock the whole loop to protect against concurrent release */
  397. spin_lock_irqsave(&schan->chan_lock, irq_flags);
  398. /*
  399. * Chaining:
  400. * first descriptor is what user is dealing with in all API calls, its
  401. * cookie is at first set to -EBUSY, at tx-submit to a positive
  402. * number
  403. * if more than one chunk is needed further chunks have cookie = -EINVAL
  404. * the last chunk, if not equal to the first, has cookie = -ENOSPC
  405. * all chunks are linked onto the tx_list head with their .node heads
  406. * only during this function, then they are immediately spliced
  407. * back onto the free list in form of a chain
  408. */
  409. for_each_sg(sgl, sg, sg_len, i) {
  410. dma_addr_t sg_addr = sg_dma_address(sg);
  411. size_t len = sg_dma_len(sg);
  412. if (!len)
  413. goto err_get_desc;
  414. do {
  415. dev_dbg(schan->dev, "Add SG #%d@%p[%d], dma %llx\n",
  416. i, sg, len, (unsigned long long)sg_addr);
  417. if (direction == DMA_DEV_TO_MEM)
  418. new = shdma_add_desc(schan, flags,
  419. &sg_addr, addr, &len, &first,
  420. direction);
  421. else
  422. new = shdma_add_desc(schan, flags,
  423. addr, &sg_addr, &len, &first,
  424. direction);
  425. if (!new)
  426. goto err_get_desc;
  427. new->chunks = chunks--;
  428. list_add_tail(&new->node, &tx_list);
  429. } while (len);
  430. }
  431. if (new != first)
  432. new->async_tx.cookie = -ENOSPC;
  433. /* Put them back on the free list, so, they don't get lost */
  434. list_splice_tail(&tx_list, &schan->ld_free);
  435. spin_unlock_irqrestore(&schan->chan_lock, irq_flags);
  436. return &first->async_tx;
  437. err_get_desc:
  438. list_for_each_entry(new, &tx_list, node)
  439. new->mark = DESC_IDLE;
  440. list_splice(&tx_list, &schan->ld_free);
  441. spin_unlock_irqrestore(&schan->chan_lock, irq_flags);
  442. return NULL;
  443. }
  444. static struct dma_async_tx_descriptor *shdma_prep_memcpy(
  445. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  446. size_t len, unsigned long flags)
  447. {
  448. struct shdma_chan *schan = to_shdma_chan(chan);
  449. struct scatterlist sg;
  450. if (!chan || !len)
  451. return NULL;
  452. BUG_ON(!schan->desc_num);
  453. sg_init_table(&sg, 1);
  454. sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len,
  455. offset_in_page(dma_src));
  456. sg_dma_address(&sg) = dma_src;
  457. sg_dma_len(&sg) = len;
  458. return shdma_prep_sg(schan, &sg, 1, &dma_dest, DMA_MEM_TO_MEM, flags);
  459. }
  460. static struct dma_async_tx_descriptor *shdma_prep_slave_sg(
  461. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  462. enum dma_transfer_direction direction, unsigned long flags, void *context)
  463. {
  464. struct shdma_chan *schan = to_shdma_chan(chan);
  465. struct shdma_dev *sdev = to_shdma_dev(schan->dma_chan.device);
  466. const struct shdma_ops *ops = sdev->ops;
  467. struct shdma_slave *slave = chan->private;
  468. dma_addr_t slave_addr;
  469. if (!chan)
  470. return NULL;
  471. BUG_ON(!schan->desc_num);
  472. /* Someone calling slave DMA on a generic channel? */
  473. if (!slave || !sg_len) {
  474. dev_warn(schan->dev, "%s: bad parameter: %p, %d, %d\n",
  475. __func__, slave, sg_len, slave ? slave->slave_id : -1);
  476. return NULL;
  477. }
  478. slave_addr = ops->slave_addr(schan);
  479. return shdma_prep_sg(schan, sgl, sg_len, &slave_addr,
  480. direction, flags);
  481. }
  482. static int shdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  483. unsigned long arg)
  484. {
  485. struct shdma_chan *schan = to_shdma_chan(chan);
  486. struct shdma_dev *sdev = to_shdma_dev(chan->device);
  487. const struct shdma_ops *ops = sdev->ops;
  488. unsigned long flags;
  489. /* Only supports DMA_TERMINATE_ALL */
  490. if (cmd != DMA_TERMINATE_ALL)
  491. return -ENXIO;
  492. if (!chan)
  493. return -EINVAL;
  494. spin_lock_irqsave(&schan->chan_lock, flags);
  495. ops->halt_channel(schan);
  496. spin_unlock_irqrestore(&schan->chan_lock, flags);
  497. shdma_chan_ld_cleanup(schan, true);
  498. return 0;
  499. }
  500. static void shdma_issue_pending(struct dma_chan *chan)
  501. {
  502. struct shdma_chan *schan = to_shdma_chan(chan);
  503. spin_lock_irq(&schan->chan_lock);
  504. if (schan->pm_state == SHDMA_PM_ESTABLISHED)
  505. shdma_chan_xfer_ld_queue(schan);
  506. else
  507. schan->pm_state = SHDMA_PM_PENDING;
  508. spin_unlock_irq(&schan->chan_lock);
  509. }
  510. static enum dma_status shdma_tx_status(struct dma_chan *chan,
  511. dma_cookie_t cookie,
  512. struct dma_tx_state *txstate)
  513. {
  514. struct shdma_chan *schan = to_shdma_chan(chan);
  515. enum dma_status status;
  516. unsigned long flags;
  517. shdma_chan_ld_cleanup(schan, false);
  518. spin_lock_irqsave(&schan->chan_lock, flags);
  519. status = dma_cookie_status(chan, cookie, txstate);
  520. /*
  521. * If we don't find cookie on the queue, it has been aborted and we have
  522. * to report error
  523. */
  524. if (status != DMA_SUCCESS) {
  525. struct shdma_desc *sdesc;
  526. status = DMA_ERROR;
  527. list_for_each_entry(sdesc, &schan->ld_queue, node)
  528. if (sdesc->cookie == cookie) {
  529. status = DMA_IN_PROGRESS;
  530. break;
  531. }
  532. }
  533. spin_unlock_irqrestore(&schan->chan_lock, flags);
  534. return status;
  535. }
  536. /* Called from error IRQ or NMI */
  537. bool shdma_reset(struct shdma_dev *sdev)
  538. {
  539. const struct shdma_ops *ops = sdev->ops;
  540. struct shdma_chan *schan;
  541. unsigned int handled = 0;
  542. int i;
  543. /* Reset all channels */
  544. shdma_for_each_chan(schan, sdev, i) {
  545. struct shdma_desc *sdesc;
  546. LIST_HEAD(dl);
  547. if (!schan)
  548. continue;
  549. spin_lock(&schan->chan_lock);
  550. /* Stop the channel */
  551. ops->halt_channel(schan);
  552. list_splice_init(&schan->ld_queue, &dl);
  553. if (!list_empty(&dl)) {
  554. dev_dbg(schan->dev, "Bring down channel %d\n", schan->id);
  555. pm_runtime_put(schan->dev);
  556. }
  557. schan->pm_state = SHDMA_PM_ESTABLISHED;
  558. spin_unlock(&schan->chan_lock);
  559. /* Complete all */
  560. list_for_each_entry(sdesc, &dl, node) {
  561. struct dma_async_tx_descriptor *tx = &sdesc->async_tx;
  562. sdesc->mark = DESC_IDLE;
  563. if (tx->callback)
  564. tx->callback(tx->callback_param);
  565. }
  566. spin_lock(&schan->chan_lock);
  567. list_splice(&dl, &schan->ld_free);
  568. spin_unlock(&schan->chan_lock);
  569. handled++;
  570. }
  571. return !!handled;
  572. }
  573. EXPORT_SYMBOL(shdma_reset);
  574. static irqreturn_t chan_irq(int irq, void *dev)
  575. {
  576. struct shdma_chan *schan = dev;
  577. const struct shdma_ops *ops =
  578. to_shdma_dev(schan->dma_chan.device)->ops;
  579. irqreturn_t ret;
  580. spin_lock(&schan->chan_lock);
  581. ret = ops->chan_irq(schan, irq) ? IRQ_WAKE_THREAD : IRQ_NONE;
  582. spin_unlock(&schan->chan_lock);
  583. return ret;
  584. }
  585. static irqreturn_t chan_irqt(int irq, void *dev)
  586. {
  587. struct shdma_chan *schan = dev;
  588. const struct shdma_ops *ops =
  589. to_shdma_dev(schan->dma_chan.device)->ops;
  590. struct shdma_desc *sdesc;
  591. spin_lock_irq(&schan->chan_lock);
  592. list_for_each_entry(sdesc, &schan->ld_queue, node) {
  593. if (sdesc->mark == DESC_SUBMITTED &&
  594. ops->desc_completed(schan, sdesc)) {
  595. dev_dbg(schan->dev, "done #%d@%p\n",
  596. sdesc->async_tx.cookie, &sdesc->async_tx);
  597. sdesc->mark = DESC_COMPLETED;
  598. break;
  599. }
  600. }
  601. /* Next desc */
  602. shdma_chan_xfer_ld_queue(schan);
  603. spin_unlock_irq(&schan->chan_lock);
  604. shdma_chan_ld_cleanup(schan, false);
  605. return IRQ_HANDLED;
  606. }
  607. int shdma_request_irq(struct shdma_chan *schan, int irq,
  608. unsigned long flags, const char *name)
  609. {
  610. int ret = request_threaded_irq(irq, chan_irq, chan_irqt,
  611. flags, name, schan);
  612. schan->irq = ret < 0 ? ret : irq;
  613. return ret;
  614. }
  615. EXPORT_SYMBOL(shdma_request_irq);
  616. void shdma_free_irq(struct shdma_chan *schan)
  617. {
  618. if (schan->irq >= 0)
  619. free_irq(schan->irq, schan);
  620. }
  621. EXPORT_SYMBOL(shdma_free_irq);
  622. void shdma_chan_probe(struct shdma_dev *sdev,
  623. struct shdma_chan *schan, int id)
  624. {
  625. schan->pm_state = SHDMA_PM_ESTABLISHED;
  626. /* reference struct dma_device */
  627. schan->dma_chan.device = &sdev->dma_dev;
  628. dma_cookie_init(&schan->dma_chan);
  629. schan->dev = sdev->dma_dev.dev;
  630. schan->id = id;
  631. if (!schan->max_xfer_len)
  632. schan->max_xfer_len = PAGE_SIZE;
  633. spin_lock_init(&schan->chan_lock);
  634. /* Init descripter manage list */
  635. INIT_LIST_HEAD(&schan->ld_queue);
  636. INIT_LIST_HEAD(&schan->ld_free);
  637. /* Add the channel to DMA device channel list */
  638. list_add_tail(&schan->dma_chan.device_node,
  639. &sdev->dma_dev.channels);
  640. sdev->schan[sdev->dma_dev.chancnt++] = schan;
  641. }
  642. EXPORT_SYMBOL(shdma_chan_probe);
  643. void shdma_chan_remove(struct shdma_chan *schan)
  644. {
  645. list_del(&schan->dma_chan.device_node);
  646. }
  647. EXPORT_SYMBOL(shdma_chan_remove);
  648. int shdma_init(struct device *dev, struct shdma_dev *sdev,
  649. int chan_num)
  650. {
  651. struct dma_device *dma_dev = &sdev->dma_dev;
  652. /*
  653. * Require all call-backs for now, they can trivially be made optional
  654. * later as required
  655. */
  656. if (!sdev->ops ||
  657. !sdev->desc_size ||
  658. !sdev->ops->embedded_desc ||
  659. !sdev->ops->start_xfer ||
  660. !sdev->ops->setup_xfer ||
  661. !sdev->ops->set_slave ||
  662. !sdev->ops->desc_setup ||
  663. !sdev->ops->slave_addr ||
  664. !sdev->ops->channel_busy ||
  665. !sdev->ops->halt_channel ||
  666. !sdev->ops->desc_completed)
  667. return -EINVAL;
  668. sdev->schan = kcalloc(chan_num, sizeof(*sdev->schan), GFP_KERNEL);
  669. if (!sdev->schan)
  670. return -ENOMEM;
  671. INIT_LIST_HEAD(&dma_dev->channels);
  672. /* Common and MEMCPY operations */
  673. dma_dev->device_alloc_chan_resources
  674. = shdma_alloc_chan_resources;
  675. dma_dev->device_free_chan_resources = shdma_free_chan_resources;
  676. dma_dev->device_prep_dma_memcpy = shdma_prep_memcpy;
  677. dma_dev->device_tx_status = shdma_tx_status;
  678. dma_dev->device_issue_pending = shdma_issue_pending;
  679. /* Compulsory for DMA_SLAVE fields */
  680. dma_dev->device_prep_slave_sg = shdma_prep_slave_sg;
  681. dma_dev->device_control = shdma_control;
  682. dma_dev->dev = dev;
  683. return 0;
  684. }
  685. EXPORT_SYMBOL(shdma_init);
  686. void shdma_cleanup(struct shdma_dev *sdev)
  687. {
  688. kfree(sdev->schan);
  689. }
  690. EXPORT_SYMBOL(shdma_cleanup);
  691. static int __init shdma_enter(void)
  692. {
  693. shdma_slave_used = kzalloc(DIV_ROUND_UP(slave_num, BITS_PER_LONG) *
  694. sizeof(long), GFP_KERNEL);
  695. if (!shdma_slave_used)
  696. return -ENOMEM;
  697. return 0;
  698. }
  699. module_init(shdma_enter);
  700. static void __exit shdma_exit(void)
  701. {
  702. kfree(shdma_slave_used);
  703. }
  704. module_exit(shdma_exit);
  705. MODULE_LICENSE("GPL v2");
  706. MODULE_DESCRIPTION("SH-DMA driver base library");
  707. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");