shdma-base.c 25 KB

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