shdma-base.c 27 KB

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