shdma-base.c 28 KB

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