shdma-base.c 27 KB

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