shdma-base.c 25 KB

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