tsi721_dma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * DMA Engine support for Tsi721 PCIExpress-to-SRIO bridge
  3. *
  4. * Copyright 2011 Integrated Device Technology, Inc.
  5. * Alexandre Bounine <alexandre.bounine@idt.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc., 59
  19. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/io.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/ioport.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/rio.h>
  29. #include <linux/rio_drv.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/kfifo.h>
  33. #include <linux/delay.h>
  34. #include "tsi721.h"
  35. static inline struct tsi721_bdma_chan *to_tsi721_chan(struct dma_chan *chan)
  36. {
  37. return container_of(chan, struct tsi721_bdma_chan, dchan);
  38. }
  39. static inline struct tsi721_device *to_tsi721(struct dma_device *ddev)
  40. {
  41. return container_of(ddev, struct rio_mport, dma)->priv;
  42. }
  43. static inline
  44. struct tsi721_tx_desc *to_tsi721_desc(struct dma_async_tx_descriptor *txd)
  45. {
  46. return container_of(txd, struct tsi721_tx_desc, txd);
  47. }
  48. static inline
  49. struct tsi721_tx_desc *tsi721_dma_first_active(
  50. struct tsi721_bdma_chan *bdma_chan)
  51. {
  52. return list_first_entry(&bdma_chan->active_list,
  53. struct tsi721_tx_desc, desc_node);
  54. }
  55. static int tsi721_bdma_ch_init(struct tsi721_bdma_chan *bdma_chan)
  56. {
  57. struct tsi721_dma_desc *bd_ptr;
  58. struct device *dev = bdma_chan->dchan.device->dev;
  59. u64 *sts_ptr;
  60. dma_addr_t bd_phys;
  61. dma_addr_t sts_phys;
  62. int sts_size;
  63. int bd_num = bdma_chan->bd_num;
  64. dev_dbg(dev, "Init Block DMA Engine, CH%d\n", bdma_chan->id);
  65. /* Allocate space for DMA descriptors */
  66. bd_ptr = dma_zalloc_coherent(dev,
  67. bd_num * sizeof(struct tsi721_dma_desc),
  68. &bd_phys, GFP_KERNEL);
  69. if (!bd_ptr)
  70. return -ENOMEM;
  71. bdma_chan->bd_phys = bd_phys;
  72. bdma_chan->bd_base = bd_ptr;
  73. dev_dbg(dev, "DMA descriptors @ %p (phys = %llx)\n",
  74. bd_ptr, (unsigned long long)bd_phys);
  75. /* Allocate space for descriptor status FIFO */
  76. sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
  77. bd_num : TSI721_DMA_MINSTSSZ;
  78. sts_size = roundup_pow_of_two(sts_size);
  79. sts_ptr = dma_zalloc_coherent(dev,
  80. sts_size * sizeof(struct tsi721_dma_sts),
  81. &sts_phys, GFP_KERNEL);
  82. if (!sts_ptr) {
  83. /* Free space allocated for DMA descriptors */
  84. dma_free_coherent(dev,
  85. bd_num * sizeof(struct tsi721_dma_desc),
  86. bd_ptr, bd_phys);
  87. bdma_chan->bd_base = NULL;
  88. return -ENOMEM;
  89. }
  90. bdma_chan->sts_phys = sts_phys;
  91. bdma_chan->sts_base = sts_ptr;
  92. bdma_chan->sts_size = sts_size;
  93. dev_dbg(dev,
  94. "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
  95. sts_ptr, (unsigned long long)sts_phys, sts_size);
  96. /* Initialize DMA descriptors ring */
  97. bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
  98. bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
  99. TSI721_DMAC_DPTRL_MASK);
  100. bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
  101. /* Setup DMA descriptor pointers */
  102. iowrite32(((u64)bd_phys >> 32),
  103. bdma_chan->regs + TSI721_DMAC_DPTRH);
  104. iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
  105. bdma_chan->regs + TSI721_DMAC_DPTRL);
  106. /* Setup descriptor status FIFO */
  107. iowrite32(((u64)sts_phys >> 32),
  108. bdma_chan->regs + TSI721_DMAC_DSBH);
  109. iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
  110. bdma_chan->regs + TSI721_DMAC_DSBL);
  111. iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
  112. bdma_chan->regs + TSI721_DMAC_DSSZ);
  113. /* Clear interrupt bits */
  114. iowrite32(TSI721_DMAC_INT_ALL,
  115. bdma_chan->regs + TSI721_DMAC_INT);
  116. ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  117. /* Toggle DMA channel initialization */
  118. iowrite32(TSI721_DMAC_CTL_INIT, bdma_chan->regs + TSI721_DMAC_CTL);
  119. ioread32(bdma_chan->regs + TSI721_DMAC_CTL);
  120. bdma_chan->wr_count = bdma_chan->wr_count_next = 0;
  121. bdma_chan->sts_rdptr = 0;
  122. udelay(10);
  123. return 0;
  124. }
  125. static int tsi721_bdma_ch_free(struct tsi721_bdma_chan *bdma_chan)
  126. {
  127. u32 ch_stat;
  128. if (bdma_chan->bd_base == NULL)
  129. return 0;
  130. /* Check if DMA channel still running */
  131. ch_stat = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  132. if (ch_stat & TSI721_DMAC_STS_RUN)
  133. return -EFAULT;
  134. /* Put DMA channel into init state */
  135. iowrite32(TSI721_DMAC_CTL_INIT, bdma_chan->regs + TSI721_DMAC_CTL);
  136. /* Free space allocated for DMA descriptors */
  137. dma_free_coherent(bdma_chan->dchan.device->dev,
  138. bdma_chan->bd_num * sizeof(struct tsi721_dma_desc),
  139. bdma_chan->bd_base, bdma_chan->bd_phys);
  140. bdma_chan->bd_base = NULL;
  141. /* Free space allocated for status FIFO */
  142. dma_free_coherent(bdma_chan->dchan.device->dev,
  143. bdma_chan->sts_size * sizeof(struct tsi721_dma_sts),
  144. bdma_chan->sts_base, bdma_chan->sts_phys);
  145. bdma_chan->sts_base = NULL;
  146. return 0;
  147. }
  148. static void
  149. tsi721_bdma_interrupt_enable(struct tsi721_bdma_chan *bdma_chan, int enable)
  150. {
  151. if (enable) {
  152. /* Clear pending BDMA channel interrupts */
  153. iowrite32(TSI721_DMAC_INT_ALL,
  154. bdma_chan->regs + TSI721_DMAC_INT);
  155. ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  156. /* Enable BDMA channel interrupts */
  157. iowrite32(TSI721_DMAC_INT_ALL,
  158. bdma_chan->regs + TSI721_DMAC_INTE);
  159. } else {
  160. /* Disable BDMA channel interrupts */
  161. iowrite32(0, bdma_chan->regs + TSI721_DMAC_INTE);
  162. /* Clear pending BDMA channel interrupts */
  163. iowrite32(TSI721_DMAC_INT_ALL,
  164. bdma_chan->regs + TSI721_DMAC_INT);
  165. }
  166. }
  167. static bool tsi721_dma_is_idle(struct tsi721_bdma_chan *bdma_chan)
  168. {
  169. u32 sts;
  170. sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  171. return ((sts & TSI721_DMAC_STS_RUN) == 0);
  172. }
  173. void tsi721_bdma_handler(struct tsi721_bdma_chan *bdma_chan)
  174. {
  175. /* Disable BDMA channel interrupts */
  176. iowrite32(0, bdma_chan->regs + TSI721_DMAC_INTE);
  177. if (bdma_chan->active)
  178. tasklet_schedule(&bdma_chan->tasklet);
  179. }
  180. #ifdef CONFIG_PCI_MSI
  181. /**
  182. * tsi721_omsg_msix - MSI-X interrupt handler for BDMA channels
  183. * @irq: Linux interrupt number
  184. * @ptr: Pointer to interrupt-specific data (BDMA channel structure)
  185. *
  186. * Handles BDMA channel interrupts signaled using MSI-X.
  187. */
  188. static irqreturn_t tsi721_bdma_msix(int irq, void *ptr)
  189. {
  190. struct tsi721_bdma_chan *bdma_chan = ptr;
  191. tsi721_bdma_handler(bdma_chan);
  192. return IRQ_HANDLED;
  193. }
  194. #endif /* CONFIG_PCI_MSI */
  195. /* Must be called with the spinlock held */
  196. static void tsi721_start_dma(struct tsi721_bdma_chan *bdma_chan)
  197. {
  198. if (!tsi721_dma_is_idle(bdma_chan)) {
  199. dev_err(bdma_chan->dchan.device->dev,
  200. "BUG: Attempt to start non-idle channel\n");
  201. return;
  202. }
  203. if (bdma_chan->wr_count == bdma_chan->wr_count_next) {
  204. dev_err(bdma_chan->dchan.device->dev,
  205. "BUG: Attempt to start DMA with no BDs ready\n");
  206. return;
  207. }
  208. dev_dbg(bdma_chan->dchan.device->dev,
  209. "tx_chan: %p, chan: %d, regs: %p\n",
  210. bdma_chan, bdma_chan->dchan.chan_id, bdma_chan->regs);
  211. iowrite32(bdma_chan->wr_count_next,
  212. bdma_chan->regs + TSI721_DMAC_DWRCNT);
  213. ioread32(bdma_chan->regs + TSI721_DMAC_DWRCNT);
  214. bdma_chan->wr_count = bdma_chan->wr_count_next;
  215. }
  216. static void tsi721_desc_put(struct tsi721_bdma_chan *bdma_chan,
  217. struct tsi721_tx_desc *desc)
  218. {
  219. dev_dbg(bdma_chan->dchan.device->dev,
  220. "Put desc: %p into free list\n", desc);
  221. if (desc) {
  222. spin_lock_bh(&bdma_chan->lock);
  223. list_splice_init(&desc->tx_list, &bdma_chan->free_list);
  224. list_add(&desc->desc_node, &bdma_chan->free_list);
  225. bdma_chan->wr_count_next = bdma_chan->wr_count;
  226. spin_unlock_bh(&bdma_chan->lock);
  227. }
  228. }
  229. static
  230. struct tsi721_tx_desc *tsi721_desc_get(struct tsi721_bdma_chan *bdma_chan)
  231. {
  232. struct tsi721_tx_desc *tx_desc, *_tx_desc;
  233. struct tsi721_tx_desc *ret = NULL;
  234. int i;
  235. spin_lock_bh(&bdma_chan->lock);
  236. list_for_each_entry_safe(tx_desc, _tx_desc,
  237. &bdma_chan->free_list, desc_node) {
  238. if (async_tx_test_ack(&tx_desc->txd)) {
  239. list_del(&tx_desc->desc_node);
  240. ret = tx_desc;
  241. break;
  242. }
  243. dev_dbg(bdma_chan->dchan.device->dev,
  244. "desc %p not ACKed\n", tx_desc);
  245. }
  246. i = bdma_chan->wr_count_next % bdma_chan->bd_num;
  247. if (i == bdma_chan->bd_num - 1) {
  248. i = 0;
  249. bdma_chan->wr_count_next++; /* skip link descriptor */
  250. }
  251. bdma_chan->wr_count_next++;
  252. tx_desc->txd.phys = bdma_chan->bd_phys +
  253. i * sizeof(struct tsi721_dma_desc);
  254. tx_desc->hw_desc = &((struct tsi721_dma_desc *)bdma_chan->bd_base)[i];
  255. spin_unlock_bh(&bdma_chan->lock);
  256. return ret;
  257. }
  258. static int
  259. tsi721_fill_desc(struct tsi721_bdma_chan *bdma_chan,
  260. struct tsi721_tx_desc *desc, struct scatterlist *sg,
  261. enum dma_rtype rtype, u32 sys_size)
  262. {
  263. struct tsi721_dma_desc *bd_ptr = desc->hw_desc;
  264. u64 rio_addr;
  265. if (sg_dma_len(sg) > TSI721_DMAD_BCOUNT1 + 1) {
  266. dev_err(bdma_chan->dchan.device->dev,
  267. "SG element is too large\n");
  268. return -EINVAL;
  269. }
  270. dev_dbg(bdma_chan->dchan.device->dev,
  271. "desc: 0x%llx, addr: 0x%llx len: 0x%x\n",
  272. (u64)desc->txd.phys, (unsigned long long)sg_dma_address(sg),
  273. sg_dma_len(sg));
  274. dev_dbg(bdma_chan->dchan.device->dev,
  275. "bd_ptr = %p did=%d raddr=0x%llx\n",
  276. bd_ptr, desc->destid, desc->rio_addr);
  277. /* Initialize DMA descriptor */
  278. bd_ptr->type_id = cpu_to_le32((DTYPE1 << 29) |
  279. (rtype << 19) | desc->destid);
  280. if (desc->interrupt)
  281. bd_ptr->type_id |= cpu_to_le32(TSI721_DMAD_IOF);
  282. bd_ptr->bcount = cpu_to_le32(((desc->rio_addr & 0x3) << 30) |
  283. (sys_size << 26) | sg_dma_len(sg));
  284. rio_addr = (desc->rio_addr >> 2) |
  285. ((u64)(desc->rio_addr_u & 0x3) << 62);
  286. bd_ptr->raddr_lo = cpu_to_le32(rio_addr & 0xffffffff);
  287. bd_ptr->raddr_hi = cpu_to_le32(rio_addr >> 32);
  288. bd_ptr->t1.bufptr_lo = cpu_to_le32(
  289. (u64)sg_dma_address(sg) & 0xffffffff);
  290. bd_ptr->t1.bufptr_hi = cpu_to_le32((u64)sg_dma_address(sg) >> 32);
  291. bd_ptr->t1.s_dist = 0;
  292. bd_ptr->t1.s_size = 0;
  293. return 0;
  294. }
  295. static void tsi721_dma_chain_complete(struct tsi721_bdma_chan *bdma_chan,
  296. struct tsi721_tx_desc *desc)
  297. {
  298. struct dma_async_tx_descriptor *txd = &desc->txd;
  299. dma_async_tx_callback callback = txd->callback;
  300. void *param = txd->callback_param;
  301. list_splice_init(&desc->tx_list, &bdma_chan->free_list);
  302. list_move(&desc->desc_node, &bdma_chan->free_list);
  303. bdma_chan->completed_cookie = txd->cookie;
  304. if (callback)
  305. callback(param);
  306. }
  307. static void tsi721_dma_complete_all(struct tsi721_bdma_chan *bdma_chan)
  308. {
  309. struct tsi721_tx_desc *desc, *_d;
  310. LIST_HEAD(list);
  311. BUG_ON(!tsi721_dma_is_idle(bdma_chan));
  312. if (!list_empty(&bdma_chan->queue))
  313. tsi721_start_dma(bdma_chan);
  314. list_splice_init(&bdma_chan->active_list, &list);
  315. list_splice_init(&bdma_chan->queue, &bdma_chan->active_list);
  316. list_for_each_entry_safe(desc, _d, &list, desc_node)
  317. tsi721_dma_chain_complete(bdma_chan, desc);
  318. }
  319. static void tsi721_clr_stat(struct tsi721_bdma_chan *bdma_chan)
  320. {
  321. u32 srd_ptr;
  322. u64 *sts_ptr;
  323. int i, j;
  324. /* Check and clear descriptor status FIFO entries */
  325. srd_ptr = bdma_chan->sts_rdptr;
  326. sts_ptr = bdma_chan->sts_base;
  327. j = srd_ptr * 8;
  328. while (sts_ptr[j]) {
  329. for (i = 0; i < 8 && sts_ptr[j]; i++, j++)
  330. sts_ptr[j] = 0;
  331. ++srd_ptr;
  332. srd_ptr %= bdma_chan->sts_size;
  333. j = srd_ptr * 8;
  334. }
  335. iowrite32(srd_ptr, bdma_chan->regs + TSI721_DMAC_DSRP);
  336. bdma_chan->sts_rdptr = srd_ptr;
  337. }
  338. static void tsi721_advance_work(struct tsi721_bdma_chan *bdma_chan)
  339. {
  340. if (list_empty(&bdma_chan->active_list) ||
  341. list_is_singular(&bdma_chan->active_list)) {
  342. dev_dbg(bdma_chan->dchan.device->dev,
  343. "%s: Active_list empty\n", __func__);
  344. tsi721_dma_complete_all(bdma_chan);
  345. } else {
  346. dev_dbg(bdma_chan->dchan.device->dev,
  347. "%s: Active_list NOT empty\n", __func__);
  348. tsi721_dma_chain_complete(bdma_chan,
  349. tsi721_dma_first_active(bdma_chan));
  350. tsi721_start_dma(bdma_chan);
  351. }
  352. }
  353. static void tsi721_dma_tasklet(unsigned long data)
  354. {
  355. struct tsi721_bdma_chan *bdma_chan = (struct tsi721_bdma_chan *)data;
  356. u32 dmac_int, dmac_sts;
  357. dmac_int = ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  358. dev_dbg(bdma_chan->dchan.device->dev, "%s: DMAC%d_INT = 0x%x\n",
  359. __func__, bdma_chan->id, dmac_int);
  360. /* Clear channel interrupts */
  361. iowrite32(dmac_int, bdma_chan->regs + TSI721_DMAC_INT);
  362. if (dmac_int & TSI721_DMAC_INT_ERR) {
  363. dmac_sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  364. dev_err(bdma_chan->dchan.device->dev,
  365. "%s: DMA ERROR - DMAC%d_STS = 0x%x\n",
  366. __func__, bdma_chan->id, dmac_sts);
  367. }
  368. if (dmac_int & TSI721_DMAC_INT_STFULL) {
  369. dev_err(bdma_chan->dchan.device->dev,
  370. "%s: DMAC%d descriptor status FIFO is full\n",
  371. __func__, bdma_chan->id);
  372. }
  373. if (dmac_int & (TSI721_DMAC_INT_DONE | TSI721_DMAC_INT_IOFDONE)) {
  374. tsi721_clr_stat(bdma_chan);
  375. spin_lock(&bdma_chan->lock);
  376. tsi721_advance_work(bdma_chan);
  377. spin_unlock(&bdma_chan->lock);
  378. }
  379. /* Re-Enable BDMA channel interrupts */
  380. iowrite32(TSI721_DMAC_INT_ALL, bdma_chan->regs + TSI721_DMAC_INTE);
  381. }
  382. static dma_cookie_t tsi721_tx_submit(struct dma_async_tx_descriptor *txd)
  383. {
  384. struct tsi721_tx_desc *desc = to_tsi721_desc(txd);
  385. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(txd->chan);
  386. dma_cookie_t cookie;
  387. spin_lock_bh(&bdma_chan->lock);
  388. cookie = txd->chan->cookie;
  389. if (++cookie < 0)
  390. cookie = 1;
  391. txd->chan->cookie = cookie;
  392. txd->cookie = cookie;
  393. if (list_empty(&bdma_chan->active_list)) {
  394. list_add_tail(&desc->desc_node, &bdma_chan->active_list);
  395. tsi721_start_dma(bdma_chan);
  396. } else {
  397. list_add_tail(&desc->desc_node, &bdma_chan->queue);
  398. }
  399. spin_unlock_bh(&bdma_chan->lock);
  400. return cookie;
  401. }
  402. static int tsi721_alloc_chan_resources(struct dma_chan *dchan)
  403. {
  404. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  405. #ifdef CONFIG_PCI_MSI
  406. struct tsi721_device *priv = to_tsi721(dchan->device);
  407. #endif
  408. struct tsi721_tx_desc *desc = NULL;
  409. LIST_HEAD(tmp_list);
  410. int i;
  411. int rc;
  412. if (bdma_chan->bd_base)
  413. return bdma_chan->bd_num - 1;
  414. /* Initialize BDMA channel */
  415. if (tsi721_bdma_ch_init(bdma_chan)) {
  416. dev_err(dchan->device->dev, "Unable to initialize data DMA"
  417. " channel %d, aborting\n", bdma_chan->id);
  418. return -ENOMEM;
  419. }
  420. /* Alocate matching number of logical descriptors */
  421. desc = kcalloc((bdma_chan->bd_num - 1), sizeof(struct tsi721_tx_desc),
  422. GFP_KERNEL);
  423. if (!desc) {
  424. dev_err(dchan->device->dev,
  425. "Failed to allocate logical descriptors\n");
  426. rc = -ENOMEM;
  427. goto err_out;
  428. }
  429. bdma_chan->tx_desc = desc;
  430. for (i = 0; i < bdma_chan->bd_num - 1; i++) {
  431. dma_async_tx_descriptor_init(&desc[i].txd, dchan);
  432. desc[i].txd.tx_submit = tsi721_tx_submit;
  433. desc[i].txd.flags = DMA_CTRL_ACK;
  434. INIT_LIST_HEAD(&desc[i].tx_list);
  435. list_add_tail(&desc[i].desc_node, &tmp_list);
  436. }
  437. spin_lock_bh(&bdma_chan->lock);
  438. list_splice(&tmp_list, &bdma_chan->free_list);
  439. bdma_chan->completed_cookie = dchan->cookie = 1;
  440. spin_unlock_bh(&bdma_chan->lock);
  441. #ifdef CONFIG_PCI_MSI
  442. if (priv->flags & TSI721_USING_MSIX) {
  443. /* Request interrupt service if we are in MSI-X mode */
  444. rc = request_irq(
  445. priv->msix[TSI721_VECT_DMA0_DONE +
  446. bdma_chan->id].vector,
  447. tsi721_bdma_msix, 0,
  448. priv->msix[TSI721_VECT_DMA0_DONE +
  449. bdma_chan->id].irq_name,
  450. (void *)bdma_chan);
  451. if (rc) {
  452. dev_dbg(dchan->device->dev,
  453. "Unable to allocate MSI-X interrupt for "
  454. "BDMA%d-DONE\n", bdma_chan->id);
  455. goto err_out;
  456. }
  457. rc = request_irq(priv->msix[TSI721_VECT_DMA0_INT +
  458. bdma_chan->id].vector,
  459. tsi721_bdma_msix, 0,
  460. priv->msix[TSI721_VECT_DMA0_INT +
  461. bdma_chan->id].irq_name,
  462. (void *)bdma_chan);
  463. if (rc) {
  464. dev_dbg(dchan->device->dev,
  465. "Unable to allocate MSI-X interrupt for "
  466. "BDMA%d-INT\n", bdma_chan->id);
  467. free_irq(
  468. priv->msix[TSI721_VECT_DMA0_DONE +
  469. bdma_chan->id].vector,
  470. (void *)bdma_chan);
  471. rc = -EIO;
  472. goto err_out;
  473. }
  474. }
  475. #endif /* CONFIG_PCI_MSI */
  476. bdma_chan->active = true;
  477. tsi721_bdma_interrupt_enable(bdma_chan, 1);
  478. return bdma_chan->bd_num - 1;
  479. err_out:
  480. kfree(desc);
  481. tsi721_bdma_ch_free(bdma_chan);
  482. return rc;
  483. }
  484. static void tsi721_free_chan_resources(struct dma_chan *dchan)
  485. {
  486. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  487. struct tsi721_device *priv = to_tsi721(dchan->device);
  488. LIST_HEAD(list);
  489. dev_dbg(dchan->device->dev, "%s: Entry\n", __func__);
  490. if (bdma_chan->bd_base == NULL)
  491. return;
  492. BUG_ON(!list_empty(&bdma_chan->active_list));
  493. BUG_ON(!list_empty(&bdma_chan->queue));
  494. tsi721_bdma_interrupt_enable(bdma_chan, 0);
  495. bdma_chan->active = false;
  496. #ifdef CONFIG_PCI_MSI
  497. if (priv->flags & TSI721_USING_MSIX) {
  498. synchronize_irq(priv->msix[TSI721_VECT_DMA0_DONE +
  499. bdma_chan->id].vector);
  500. synchronize_irq(priv->msix[TSI721_VECT_DMA0_INT +
  501. bdma_chan->id].vector);
  502. } else
  503. #endif
  504. synchronize_irq(priv->pdev->irq);
  505. tasklet_kill(&bdma_chan->tasklet);
  506. spin_lock_bh(&bdma_chan->lock);
  507. list_splice_init(&bdma_chan->free_list, &list);
  508. spin_unlock_bh(&bdma_chan->lock);
  509. #ifdef CONFIG_PCI_MSI
  510. if (priv->flags & TSI721_USING_MSIX) {
  511. free_irq(priv->msix[TSI721_VECT_DMA0_DONE +
  512. bdma_chan->id].vector, (void *)bdma_chan);
  513. free_irq(priv->msix[TSI721_VECT_DMA0_INT +
  514. bdma_chan->id].vector, (void *)bdma_chan);
  515. }
  516. #endif /* CONFIG_PCI_MSI */
  517. tsi721_bdma_ch_free(bdma_chan);
  518. kfree(bdma_chan->tx_desc);
  519. }
  520. static
  521. enum dma_status tsi721_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
  522. struct dma_tx_state *txstate)
  523. {
  524. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  525. dma_cookie_t last_used;
  526. dma_cookie_t last_completed;
  527. int ret;
  528. spin_lock_bh(&bdma_chan->lock);
  529. last_completed = bdma_chan->completed_cookie;
  530. last_used = dchan->cookie;
  531. spin_unlock_bh(&bdma_chan->lock);
  532. ret = dma_async_is_complete(cookie, last_completed, last_used);
  533. dma_set_tx_state(txstate, last_completed, last_used, 0);
  534. dev_dbg(dchan->device->dev,
  535. "%s: exit, ret: %d, last_completed: %d, last_used: %d\n",
  536. __func__, ret, last_completed, last_used);
  537. return ret;
  538. }
  539. static void tsi721_issue_pending(struct dma_chan *dchan)
  540. {
  541. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  542. dev_dbg(dchan->device->dev, "%s: Entry\n", __func__);
  543. if (tsi721_dma_is_idle(bdma_chan)) {
  544. spin_lock_bh(&bdma_chan->lock);
  545. tsi721_advance_work(bdma_chan);
  546. spin_unlock_bh(&bdma_chan->lock);
  547. } else
  548. dev_dbg(dchan->device->dev,
  549. "%s: DMA channel still busy\n", __func__);
  550. }
  551. static
  552. struct dma_async_tx_descriptor *tsi721_prep_rio_sg(struct dma_chan *dchan,
  553. struct scatterlist *sgl, unsigned int sg_len,
  554. enum dma_transfer_direction dir, unsigned long flags,
  555. void *tinfo)
  556. {
  557. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  558. struct tsi721_tx_desc *desc = NULL;
  559. struct tsi721_tx_desc *first = NULL;
  560. struct scatterlist *sg;
  561. struct rio_dma_ext *rext = tinfo;
  562. u64 rio_addr = rext->rio_addr; /* limited to 64-bit rio_addr for now */
  563. unsigned int i;
  564. u32 sys_size = dma_to_mport(dchan->device)->sys_size;
  565. enum dma_rtype rtype;
  566. if (!sgl || !sg_len) {
  567. dev_err(dchan->device->dev, "%s: No SG list\n", __func__);
  568. return NULL;
  569. }
  570. if (dir == DMA_DEV_TO_MEM)
  571. rtype = NREAD;
  572. else if (dir == DMA_MEM_TO_DEV) {
  573. switch (rext->wr_type) {
  574. case RDW_ALL_NWRITE:
  575. rtype = ALL_NWRITE;
  576. break;
  577. case RDW_ALL_NWRITE_R:
  578. rtype = ALL_NWRITE_R;
  579. break;
  580. case RDW_LAST_NWRITE_R:
  581. default:
  582. rtype = LAST_NWRITE_R;
  583. break;
  584. }
  585. } else {
  586. dev_err(dchan->device->dev,
  587. "%s: Unsupported DMA direction option\n", __func__);
  588. return NULL;
  589. }
  590. for_each_sg(sgl, sg, sg_len, i) {
  591. int err;
  592. dev_dbg(dchan->device->dev, "%s: sg #%d\n", __func__, i);
  593. desc = tsi721_desc_get(bdma_chan);
  594. if (!desc) {
  595. dev_err(dchan->device->dev,
  596. "Not enough descriptors available\n");
  597. goto err_desc_get;
  598. }
  599. if (sg_is_last(sg))
  600. desc->interrupt = (flags & DMA_PREP_INTERRUPT) != 0;
  601. else
  602. desc->interrupt = false;
  603. desc->destid = rext->destid;
  604. desc->rio_addr = rio_addr;
  605. desc->rio_addr_u = 0;
  606. err = tsi721_fill_desc(bdma_chan, desc, sg, rtype, sys_size);
  607. if (err) {
  608. dev_err(dchan->device->dev,
  609. "Failed to build desc: %d\n", err);
  610. goto err_desc_get;
  611. }
  612. rio_addr += sg_dma_len(sg);
  613. if (!first)
  614. first = desc;
  615. else
  616. list_add_tail(&desc->desc_node, &first->tx_list);
  617. }
  618. first->txd.cookie = -EBUSY;
  619. desc->txd.flags = flags;
  620. return &first->txd;
  621. err_desc_get:
  622. tsi721_desc_put(bdma_chan, first);
  623. return NULL;
  624. }
  625. static int tsi721_device_control(struct dma_chan *dchan, enum dma_ctrl_cmd cmd,
  626. unsigned long arg)
  627. {
  628. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  629. struct tsi721_tx_desc *desc, *_d;
  630. LIST_HEAD(list);
  631. dev_dbg(dchan->device->dev, "%s: Entry\n", __func__);
  632. if (cmd != DMA_TERMINATE_ALL)
  633. return -ENXIO;
  634. spin_lock_bh(&bdma_chan->lock);
  635. /* make sure to stop the transfer */
  636. iowrite32(TSI721_DMAC_CTL_SUSP, bdma_chan->regs + TSI721_DMAC_CTL);
  637. list_splice_init(&bdma_chan->active_list, &list);
  638. list_splice_init(&bdma_chan->queue, &list);
  639. list_for_each_entry_safe(desc, _d, &list, desc_node)
  640. tsi721_dma_chain_complete(bdma_chan, desc);
  641. spin_unlock_bh(&bdma_chan->lock);
  642. return 0;
  643. }
  644. int tsi721_register_dma(struct tsi721_device *priv)
  645. {
  646. int i;
  647. int nr_channels = TSI721_DMA_MAXCH;
  648. int err;
  649. struct rio_mport *mport = priv->mport;
  650. mport->dma.dev = &priv->pdev->dev;
  651. mport->dma.chancnt = nr_channels;
  652. INIT_LIST_HEAD(&mport->dma.channels);
  653. for (i = 0; i < nr_channels; i++) {
  654. struct tsi721_bdma_chan *bdma_chan = &priv->bdma[i];
  655. if (i == TSI721_DMACH_MAINT)
  656. continue;
  657. bdma_chan->bd_num = 64;
  658. bdma_chan->regs = priv->regs + TSI721_DMAC_BASE(i);
  659. bdma_chan->dchan.device = &mport->dma;
  660. bdma_chan->dchan.cookie = 1;
  661. bdma_chan->dchan.chan_id = i;
  662. bdma_chan->id = i;
  663. bdma_chan->active = false;
  664. spin_lock_init(&bdma_chan->lock);
  665. INIT_LIST_HEAD(&bdma_chan->active_list);
  666. INIT_LIST_HEAD(&bdma_chan->queue);
  667. INIT_LIST_HEAD(&bdma_chan->free_list);
  668. tasklet_init(&bdma_chan->tasklet, tsi721_dma_tasklet,
  669. (unsigned long)bdma_chan);
  670. list_add_tail(&bdma_chan->dchan.device_node,
  671. &mport->dma.channels);
  672. }
  673. dma_cap_zero(mport->dma.cap_mask);
  674. dma_cap_set(DMA_PRIVATE, mport->dma.cap_mask);
  675. dma_cap_set(DMA_SLAVE, mport->dma.cap_mask);
  676. mport->dma.device_alloc_chan_resources = tsi721_alloc_chan_resources;
  677. mport->dma.device_free_chan_resources = tsi721_free_chan_resources;
  678. mport->dma.device_tx_status = tsi721_tx_status;
  679. mport->dma.device_issue_pending = tsi721_issue_pending;
  680. mport->dma.device_prep_slave_sg = tsi721_prep_rio_sg;
  681. mport->dma.device_control = tsi721_device_control;
  682. err = dma_async_device_register(&mport->dma);
  683. if (err)
  684. dev_err(&priv->pdev->dev, "Failed to register DMA device\n");
  685. return err;
  686. }