tsi721_dma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * DMA Engine support for Tsi721 PCIExpress-to-SRIO bridge
  3. *
  4. * Copyright (c) 2011-2014 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. * The full GNU General Public License is included in this distribution in the
  18. * file called COPYING.
  19. */
  20. #include <linux/io.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/ioport.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/rio.h>
  28. #include <linux/rio_drv.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/kfifo.h>
  32. #include <linux/delay.h>
  33. #include "../../dma/dmaengine.h"
  34. #include "tsi721.h"
  35. #define TSI721_DMA_TX_QUEUE_SZ 16 /* number of transaction descriptors */
  36. #ifdef CONFIG_PCI_MSI
  37. static irqreturn_t tsi721_bdma_msix(int irq, void *ptr);
  38. #endif
  39. static int tsi721_submit_sg(struct tsi721_tx_desc *desc);
  40. static unsigned int dma_desc_per_channel = 128;
  41. module_param(dma_desc_per_channel, uint, S_IWUSR | S_IRUGO);
  42. MODULE_PARM_DESC(dma_desc_per_channel,
  43. "Number of DMA descriptors per channel (default: 128)");
  44. static inline struct tsi721_bdma_chan *to_tsi721_chan(struct dma_chan *chan)
  45. {
  46. return container_of(chan, struct tsi721_bdma_chan, dchan);
  47. }
  48. static inline struct tsi721_device *to_tsi721(struct dma_device *ddev)
  49. {
  50. return container_of(ddev, struct rio_mport, dma)->priv;
  51. }
  52. static inline
  53. struct tsi721_tx_desc *to_tsi721_desc(struct dma_async_tx_descriptor *txd)
  54. {
  55. return container_of(txd, struct tsi721_tx_desc, txd);
  56. }
  57. static inline
  58. struct tsi721_tx_desc *tsi721_dma_first_active(
  59. struct tsi721_bdma_chan *bdma_chan)
  60. {
  61. return list_first_entry(&bdma_chan->active_list,
  62. struct tsi721_tx_desc, desc_node);
  63. }
  64. static int tsi721_bdma_ch_init(struct tsi721_bdma_chan *bdma_chan, int bd_num)
  65. {
  66. struct tsi721_dma_desc *bd_ptr;
  67. struct device *dev = bdma_chan->dchan.device->dev;
  68. u64 *sts_ptr;
  69. dma_addr_t bd_phys;
  70. dma_addr_t sts_phys;
  71. int sts_size;
  72. #ifdef CONFIG_PCI_MSI
  73. struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
  74. #endif
  75. dev_dbg(dev, "Init Block DMA Engine, CH%d\n", bdma_chan->id);
  76. /*
  77. * Allocate space for DMA descriptors
  78. * (add an extra element for link descriptor)
  79. */
  80. bd_ptr = dma_zalloc_coherent(dev,
  81. (bd_num + 1) * sizeof(struct tsi721_dma_desc),
  82. &bd_phys, GFP_KERNEL);
  83. if (!bd_ptr)
  84. return -ENOMEM;
  85. bdma_chan->bd_num = bd_num;
  86. bdma_chan->bd_phys = bd_phys;
  87. bdma_chan->bd_base = bd_ptr;
  88. dev_dbg(dev, "DMA descriptors @ %p (phys = %llx)\n",
  89. bd_ptr, (unsigned long long)bd_phys);
  90. /* Allocate space for descriptor status FIFO */
  91. sts_size = ((bd_num + 1) >= TSI721_DMA_MINSTSSZ) ?
  92. (bd_num + 1) : TSI721_DMA_MINSTSSZ;
  93. sts_size = roundup_pow_of_two(sts_size);
  94. sts_ptr = dma_zalloc_coherent(dev,
  95. sts_size * sizeof(struct tsi721_dma_sts),
  96. &sts_phys, GFP_KERNEL);
  97. if (!sts_ptr) {
  98. /* Free space allocated for DMA descriptors */
  99. dma_free_coherent(dev,
  100. (bd_num + 1) * sizeof(struct tsi721_dma_desc),
  101. bd_ptr, bd_phys);
  102. bdma_chan->bd_base = NULL;
  103. return -ENOMEM;
  104. }
  105. bdma_chan->sts_phys = sts_phys;
  106. bdma_chan->sts_base = sts_ptr;
  107. bdma_chan->sts_size = sts_size;
  108. dev_dbg(dev,
  109. "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
  110. sts_ptr, (unsigned long long)sts_phys, sts_size);
  111. /* Initialize DMA descriptors ring using added link descriptor */
  112. bd_ptr[bd_num].type_id = cpu_to_le32(DTYPE3 << 29);
  113. bd_ptr[bd_num].next_lo = cpu_to_le32((u64)bd_phys &
  114. TSI721_DMAC_DPTRL_MASK);
  115. bd_ptr[bd_num].next_hi = cpu_to_le32((u64)bd_phys >> 32);
  116. /* Setup DMA descriptor pointers */
  117. iowrite32(((u64)bd_phys >> 32),
  118. bdma_chan->regs + TSI721_DMAC_DPTRH);
  119. iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
  120. bdma_chan->regs + TSI721_DMAC_DPTRL);
  121. /* Setup descriptor status FIFO */
  122. iowrite32(((u64)sts_phys >> 32),
  123. bdma_chan->regs + TSI721_DMAC_DSBH);
  124. iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
  125. bdma_chan->regs + TSI721_DMAC_DSBL);
  126. iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
  127. bdma_chan->regs + TSI721_DMAC_DSSZ);
  128. /* Clear interrupt bits */
  129. iowrite32(TSI721_DMAC_INT_ALL,
  130. bdma_chan->regs + TSI721_DMAC_INT);
  131. ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  132. #ifdef CONFIG_PCI_MSI
  133. /* Request interrupt service if we are in MSI-X mode */
  134. if (priv->flags & TSI721_USING_MSIX) {
  135. int rc, idx;
  136. idx = TSI721_VECT_DMA0_DONE + bdma_chan->id;
  137. rc = request_irq(priv->msix[idx].vector, tsi721_bdma_msix, 0,
  138. priv->msix[idx].irq_name, (void *)bdma_chan);
  139. if (rc) {
  140. dev_dbg(dev, "Unable to get MSI-X for BDMA%d-DONE\n",
  141. bdma_chan->id);
  142. goto err_out;
  143. }
  144. idx = TSI721_VECT_DMA0_INT + bdma_chan->id;
  145. rc = request_irq(priv->msix[idx].vector, tsi721_bdma_msix, 0,
  146. priv->msix[idx].irq_name, (void *)bdma_chan);
  147. if (rc) {
  148. dev_dbg(dev, "Unable to get MSI-X for BDMA%d-INT\n",
  149. bdma_chan->id);
  150. free_irq(
  151. priv->msix[TSI721_VECT_DMA0_DONE +
  152. bdma_chan->id].vector,
  153. (void *)bdma_chan);
  154. }
  155. err_out:
  156. if (rc) {
  157. /* Free space allocated for DMA descriptors */
  158. dma_free_coherent(dev,
  159. (bd_num + 1) * sizeof(struct tsi721_dma_desc),
  160. bd_ptr, bd_phys);
  161. bdma_chan->bd_base = NULL;
  162. /* Free space allocated for status descriptors */
  163. dma_free_coherent(dev,
  164. sts_size * sizeof(struct tsi721_dma_sts),
  165. sts_ptr, sts_phys);
  166. bdma_chan->sts_base = NULL;
  167. return -EIO;
  168. }
  169. }
  170. #endif /* CONFIG_PCI_MSI */
  171. /* Toggle DMA channel initialization */
  172. iowrite32(TSI721_DMAC_CTL_INIT, bdma_chan->regs + TSI721_DMAC_CTL);
  173. ioread32(bdma_chan->regs + TSI721_DMAC_CTL);
  174. bdma_chan->wr_count = bdma_chan->wr_count_next = 0;
  175. bdma_chan->sts_rdptr = 0;
  176. udelay(10);
  177. return 0;
  178. }
  179. static int tsi721_bdma_ch_free(struct tsi721_bdma_chan *bdma_chan)
  180. {
  181. u32 ch_stat;
  182. #ifdef CONFIG_PCI_MSI
  183. struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
  184. #endif
  185. if (bdma_chan->bd_base == NULL)
  186. return 0;
  187. /* Check if DMA channel still running */
  188. ch_stat = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  189. if (ch_stat & TSI721_DMAC_STS_RUN)
  190. return -EFAULT;
  191. /* Put DMA channel into init state */
  192. iowrite32(TSI721_DMAC_CTL_INIT, bdma_chan->regs + TSI721_DMAC_CTL);
  193. #ifdef CONFIG_PCI_MSI
  194. if (priv->flags & TSI721_USING_MSIX) {
  195. free_irq(priv->msix[TSI721_VECT_DMA0_DONE +
  196. bdma_chan->id].vector, (void *)bdma_chan);
  197. free_irq(priv->msix[TSI721_VECT_DMA0_INT +
  198. bdma_chan->id].vector, (void *)bdma_chan);
  199. }
  200. #endif /* CONFIG_PCI_MSI */
  201. /* Free space allocated for DMA descriptors */
  202. dma_free_coherent(bdma_chan->dchan.device->dev,
  203. (bdma_chan->bd_num + 1) * sizeof(struct tsi721_dma_desc),
  204. bdma_chan->bd_base, bdma_chan->bd_phys);
  205. bdma_chan->bd_base = NULL;
  206. /* Free space allocated for status FIFO */
  207. dma_free_coherent(bdma_chan->dchan.device->dev,
  208. bdma_chan->sts_size * sizeof(struct tsi721_dma_sts),
  209. bdma_chan->sts_base, bdma_chan->sts_phys);
  210. bdma_chan->sts_base = NULL;
  211. return 0;
  212. }
  213. static void
  214. tsi721_bdma_interrupt_enable(struct tsi721_bdma_chan *bdma_chan, int enable)
  215. {
  216. if (enable) {
  217. /* Clear pending BDMA channel interrupts */
  218. iowrite32(TSI721_DMAC_INT_ALL,
  219. bdma_chan->regs + TSI721_DMAC_INT);
  220. ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  221. /* Enable BDMA channel interrupts */
  222. iowrite32(TSI721_DMAC_INT_ALL,
  223. bdma_chan->regs + TSI721_DMAC_INTE);
  224. } else {
  225. /* Disable BDMA channel interrupts */
  226. iowrite32(0, bdma_chan->regs + TSI721_DMAC_INTE);
  227. /* Clear pending BDMA channel interrupts */
  228. iowrite32(TSI721_DMAC_INT_ALL,
  229. bdma_chan->regs + TSI721_DMAC_INT);
  230. }
  231. }
  232. static bool tsi721_dma_is_idle(struct tsi721_bdma_chan *bdma_chan)
  233. {
  234. u32 sts;
  235. sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  236. return ((sts & TSI721_DMAC_STS_RUN) == 0);
  237. }
  238. void tsi721_bdma_handler(struct tsi721_bdma_chan *bdma_chan)
  239. {
  240. /* Disable BDMA channel interrupts */
  241. iowrite32(0, bdma_chan->regs + TSI721_DMAC_INTE);
  242. if (bdma_chan->active)
  243. tasklet_schedule(&bdma_chan->tasklet);
  244. }
  245. #ifdef CONFIG_PCI_MSI
  246. /**
  247. * tsi721_omsg_msix - MSI-X interrupt handler for BDMA channels
  248. * @irq: Linux interrupt number
  249. * @ptr: Pointer to interrupt-specific data (BDMA channel structure)
  250. *
  251. * Handles BDMA channel interrupts signaled using MSI-X.
  252. */
  253. static irqreturn_t tsi721_bdma_msix(int irq, void *ptr)
  254. {
  255. struct tsi721_bdma_chan *bdma_chan = ptr;
  256. tsi721_bdma_handler(bdma_chan);
  257. return IRQ_HANDLED;
  258. }
  259. #endif /* CONFIG_PCI_MSI */
  260. /* Must be called with the spinlock held */
  261. static void tsi721_start_dma(struct tsi721_bdma_chan *bdma_chan)
  262. {
  263. if (!tsi721_dma_is_idle(bdma_chan)) {
  264. dev_err(bdma_chan->dchan.device->dev,
  265. "BUG: Attempt to start non-idle channel\n");
  266. return;
  267. }
  268. if (bdma_chan->wr_count == bdma_chan->wr_count_next) {
  269. dev_err(bdma_chan->dchan.device->dev,
  270. "BUG: Attempt to start DMA with no BDs ready\n");
  271. return;
  272. }
  273. dev_dbg(bdma_chan->dchan.device->dev,
  274. "%s: chan_%d (wrc=%d)\n", __func__, bdma_chan->id,
  275. bdma_chan->wr_count_next);
  276. iowrite32(bdma_chan->wr_count_next,
  277. bdma_chan->regs + TSI721_DMAC_DWRCNT);
  278. ioread32(bdma_chan->regs + TSI721_DMAC_DWRCNT);
  279. bdma_chan->wr_count = bdma_chan->wr_count_next;
  280. }
  281. static int
  282. tsi721_desc_fill_init(struct tsi721_tx_desc *desc,
  283. struct tsi721_dma_desc *bd_ptr,
  284. struct scatterlist *sg, u32 sys_size)
  285. {
  286. u64 rio_addr;
  287. if (bd_ptr == NULL)
  288. return -EINVAL;
  289. /* Initialize DMA descriptor */
  290. bd_ptr->type_id = cpu_to_le32((DTYPE1 << 29) |
  291. (desc->rtype << 19) | desc->destid);
  292. bd_ptr->bcount = cpu_to_le32(((desc->rio_addr & 0x3) << 30) |
  293. (sys_size << 26));
  294. rio_addr = (desc->rio_addr >> 2) |
  295. ((u64)(desc->rio_addr_u & 0x3) << 62);
  296. bd_ptr->raddr_lo = cpu_to_le32(rio_addr & 0xffffffff);
  297. bd_ptr->raddr_hi = cpu_to_le32(rio_addr >> 32);
  298. bd_ptr->t1.bufptr_lo = cpu_to_le32(
  299. (u64)sg_dma_address(sg) & 0xffffffff);
  300. bd_ptr->t1.bufptr_hi = cpu_to_le32((u64)sg_dma_address(sg) >> 32);
  301. bd_ptr->t1.s_dist = 0;
  302. bd_ptr->t1.s_size = 0;
  303. return 0;
  304. }
  305. static int
  306. tsi721_desc_fill_end(struct tsi721_dma_desc *bd_ptr, u32 bcount, bool interrupt)
  307. {
  308. if (bd_ptr == NULL)
  309. return -EINVAL;
  310. /* Update DMA descriptor */
  311. if (interrupt)
  312. bd_ptr->type_id |= cpu_to_le32(TSI721_DMAD_IOF);
  313. bd_ptr->bcount |= cpu_to_le32(bcount & TSI721_DMAD_BCOUNT1);
  314. return 0;
  315. }
  316. static void tsi721_dma_tx_err(struct tsi721_bdma_chan *bdma_chan,
  317. struct tsi721_tx_desc *desc)
  318. {
  319. struct dma_async_tx_descriptor *txd = &desc->txd;
  320. dma_async_tx_callback callback = txd->callback;
  321. void *param = txd->callback_param;
  322. list_move(&desc->desc_node, &bdma_chan->free_list);
  323. if (callback)
  324. callback(param);
  325. }
  326. static void tsi721_clr_stat(struct tsi721_bdma_chan *bdma_chan)
  327. {
  328. u32 srd_ptr;
  329. u64 *sts_ptr;
  330. int i, j;
  331. /* Check and clear descriptor status FIFO entries */
  332. srd_ptr = bdma_chan->sts_rdptr;
  333. sts_ptr = bdma_chan->sts_base;
  334. j = srd_ptr * 8;
  335. while (sts_ptr[j]) {
  336. for (i = 0; i < 8 && sts_ptr[j]; i++, j++)
  337. sts_ptr[j] = 0;
  338. ++srd_ptr;
  339. srd_ptr %= bdma_chan->sts_size;
  340. j = srd_ptr * 8;
  341. }
  342. iowrite32(srd_ptr, bdma_chan->regs + TSI721_DMAC_DSRP);
  343. bdma_chan->sts_rdptr = srd_ptr;
  344. }
  345. /* Must be called with the channel spinlock held */
  346. static int tsi721_submit_sg(struct tsi721_tx_desc *desc)
  347. {
  348. struct dma_chan *dchan = desc->txd.chan;
  349. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  350. u32 sys_size;
  351. u64 rio_addr;
  352. dma_addr_t next_addr;
  353. u32 bcount;
  354. struct scatterlist *sg;
  355. unsigned int i;
  356. int err = 0;
  357. struct tsi721_dma_desc *bd_ptr = NULL;
  358. u32 idx, rd_idx;
  359. u32 add_count = 0;
  360. if (!tsi721_dma_is_idle(bdma_chan)) {
  361. dev_err(bdma_chan->dchan.device->dev,
  362. "BUG: Attempt to use non-idle channel\n");
  363. return -EIO;
  364. }
  365. /*
  366. * Fill DMA channel's hardware buffer descriptors.
  367. * (NOTE: RapidIO destination address is limited to 64 bits for now)
  368. */
  369. rio_addr = desc->rio_addr;
  370. next_addr = -1;
  371. bcount = 0;
  372. sys_size = dma_to_mport(bdma_chan->dchan.device)->sys_size;
  373. rd_idx = ioread32(bdma_chan->regs + TSI721_DMAC_DRDCNT);
  374. rd_idx %= (bdma_chan->bd_num + 1);
  375. idx = bdma_chan->wr_count_next % (bdma_chan->bd_num + 1);
  376. if (idx == bdma_chan->bd_num) {
  377. /* wrap around link descriptor */
  378. idx = 0;
  379. add_count++;
  380. }
  381. dev_dbg(dchan->device->dev, "%s: BD ring status: rdi=%d wri=%d\n",
  382. __func__, rd_idx, idx);
  383. for_each_sg(desc->sg, sg, desc->sg_len, i) {
  384. dev_dbg(dchan->device->dev, "sg%d/%d addr: 0x%llx len: %d\n",
  385. i, desc->sg_len,
  386. (unsigned long long)sg_dma_address(sg), sg_dma_len(sg));
  387. if (sg_dma_len(sg) > TSI721_BDMA_MAX_BCOUNT) {
  388. dev_err(dchan->device->dev,
  389. "%s: SG entry %d is too large\n", __func__, i);
  390. err = -EINVAL;
  391. break;
  392. }
  393. /*
  394. * If this sg entry forms contiguous block with previous one,
  395. * try to merge it into existing DMA descriptor
  396. */
  397. if (next_addr == sg_dma_address(sg) &&
  398. bcount + sg_dma_len(sg) <= TSI721_BDMA_MAX_BCOUNT) {
  399. /* Adjust byte count of the descriptor */
  400. bcount += sg_dma_len(sg);
  401. goto entry_done;
  402. } else if (next_addr != -1) {
  403. /* Finalize descriptor using total byte count value */
  404. tsi721_desc_fill_end(bd_ptr, bcount, 0);
  405. dev_dbg(dchan->device->dev,
  406. "%s: prev desc final len: %d\n",
  407. __func__, bcount);
  408. }
  409. desc->rio_addr = rio_addr;
  410. if (i && idx == rd_idx) {
  411. dev_dbg(dchan->device->dev,
  412. "%s: HW descriptor ring is full @ %d\n",
  413. __func__, i);
  414. desc->sg = sg;
  415. desc->sg_len -= i;
  416. break;
  417. }
  418. bd_ptr = &((struct tsi721_dma_desc *)bdma_chan->bd_base)[idx];
  419. err = tsi721_desc_fill_init(desc, bd_ptr, sg, sys_size);
  420. if (err) {
  421. dev_err(dchan->device->dev,
  422. "Failed to build desc: err=%d\n", err);
  423. break;
  424. }
  425. dev_dbg(dchan->device->dev, "bd_ptr = %p did=%d raddr=0x%llx\n",
  426. bd_ptr, desc->destid, desc->rio_addr);
  427. next_addr = sg_dma_address(sg);
  428. bcount = sg_dma_len(sg);
  429. add_count++;
  430. if (++idx == bdma_chan->bd_num) {
  431. /* wrap around link descriptor */
  432. idx = 0;
  433. add_count++;
  434. }
  435. entry_done:
  436. if (sg_is_last(sg)) {
  437. tsi721_desc_fill_end(bd_ptr, bcount, 0);
  438. dev_dbg(dchan->device->dev, "%s: last desc final len: %d\n",
  439. __func__, bcount);
  440. desc->sg_len = 0;
  441. } else {
  442. rio_addr += sg_dma_len(sg);
  443. next_addr += sg_dma_len(sg);
  444. }
  445. }
  446. if (!err)
  447. bdma_chan->wr_count_next += add_count;
  448. return err;
  449. }
  450. static void tsi721_advance_work(struct tsi721_bdma_chan *bdma_chan)
  451. {
  452. struct tsi721_tx_desc *desc;
  453. int err;
  454. dev_dbg(bdma_chan->dchan.device->dev, "%s: Enter\n", __func__);
  455. /*
  456. * If there are any new transactions in the queue add them
  457. * into the processing list
  458. */
  459. if (!list_empty(&bdma_chan->queue))
  460. list_splice_init(&bdma_chan->queue, &bdma_chan->active_list);
  461. /* Start new transaction (if available) */
  462. if (!list_empty(&bdma_chan->active_list)) {
  463. desc = tsi721_dma_first_active(bdma_chan);
  464. err = tsi721_submit_sg(desc);
  465. if (!err)
  466. tsi721_start_dma(bdma_chan);
  467. else {
  468. tsi721_dma_tx_err(bdma_chan, desc);
  469. dev_dbg(bdma_chan->dchan.device->dev,
  470. "ERR: tsi721_submit_sg failed with err=%d\n",
  471. err);
  472. }
  473. }
  474. dev_dbg(bdma_chan->dchan.device->dev, "%s: Exit\n", __func__);
  475. }
  476. static void tsi721_dma_tasklet(unsigned long data)
  477. {
  478. struct tsi721_bdma_chan *bdma_chan = (struct tsi721_bdma_chan *)data;
  479. u32 dmac_int, dmac_sts;
  480. dmac_int = ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  481. dev_dbg(bdma_chan->dchan.device->dev, "%s: DMAC%d_INT = 0x%x\n",
  482. __func__, bdma_chan->id, dmac_int);
  483. /* Clear channel interrupts */
  484. iowrite32(dmac_int, bdma_chan->regs + TSI721_DMAC_INT);
  485. if (dmac_int & TSI721_DMAC_INT_ERR) {
  486. dmac_sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  487. dev_err(bdma_chan->dchan.device->dev,
  488. "%s: DMA ERROR - DMAC%d_STS = 0x%x\n",
  489. __func__, bdma_chan->id, dmac_sts);
  490. }
  491. if (dmac_int & TSI721_DMAC_INT_STFULL) {
  492. dev_err(bdma_chan->dchan.device->dev,
  493. "%s: DMAC%d descriptor status FIFO is full\n",
  494. __func__, bdma_chan->id);
  495. }
  496. if (dmac_int & (TSI721_DMAC_INT_DONE | TSI721_DMAC_INT_IOFDONE)) {
  497. struct tsi721_tx_desc *desc;
  498. tsi721_clr_stat(bdma_chan);
  499. spin_lock(&bdma_chan->lock);
  500. desc = tsi721_dma_first_active(bdma_chan);
  501. if (desc->sg_len == 0) {
  502. dma_async_tx_callback callback = NULL;
  503. void *param = NULL;
  504. desc->status = DMA_COMPLETE;
  505. dma_cookie_complete(&desc->txd);
  506. if (desc->txd.flags & DMA_PREP_INTERRUPT) {
  507. callback = desc->txd.callback;
  508. param = desc->txd.callback_param;
  509. }
  510. list_move(&desc->desc_node, &bdma_chan->free_list);
  511. spin_unlock(&bdma_chan->lock);
  512. if (callback)
  513. callback(param);
  514. spin_lock(&bdma_chan->lock);
  515. }
  516. tsi721_advance_work(bdma_chan);
  517. spin_unlock(&bdma_chan->lock);
  518. }
  519. /* Re-Enable BDMA channel interrupts */
  520. iowrite32(TSI721_DMAC_INT_ALL, bdma_chan->regs + TSI721_DMAC_INTE);
  521. }
  522. static dma_cookie_t tsi721_tx_submit(struct dma_async_tx_descriptor *txd)
  523. {
  524. struct tsi721_tx_desc *desc = to_tsi721_desc(txd);
  525. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(txd->chan);
  526. dma_cookie_t cookie;
  527. /* Check if the descriptor is detached from any lists */
  528. if (!list_empty(&desc->desc_node)) {
  529. dev_err(bdma_chan->dchan.device->dev,
  530. "%s: wrong state of descriptor %p\n", __func__, txd);
  531. return -EIO;
  532. }
  533. spin_lock_bh(&bdma_chan->lock);
  534. if (!bdma_chan->active) {
  535. spin_unlock_bh(&bdma_chan->lock);
  536. return -ENODEV;
  537. }
  538. cookie = dma_cookie_assign(txd);
  539. desc->status = DMA_IN_PROGRESS;
  540. list_add_tail(&desc->desc_node, &bdma_chan->queue);
  541. spin_unlock_bh(&bdma_chan->lock);
  542. return cookie;
  543. }
  544. static int tsi721_alloc_chan_resources(struct dma_chan *dchan)
  545. {
  546. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  547. struct tsi721_tx_desc *desc = NULL;
  548. int i;
  549. dev_dbg(dchan->device->dev, "%s: for channel %d\n",
  550. __func__, bdma_chan->id);
  551. if (bdma_chan->bd_base)
  552. return TSI721_DMA_TX_QUEUE_SZ;
  553. /* Initialize BDMA channel */
  554. if (tsi721_bdma_ch_init(bdma_chan, dma_desc_per_channel)) {
  555. dev_err(dchan->device->dev, "Unable to initialize data DMA"
  556. " channel %d, aborting\n", bdma_chan->id);
  557. return -ENODEV;
  558. }
  559. /* Allocate queue of transaction descriptors */
  560. desc = kcalloc(TSI721_DMA_TX_QUEUE_SZ, sizeof(struct tsi721_tx_desc),
  561. GFP_KERNEL);
  562. if (!desc) {
  563. dev_err(dchan->device->dev,
  564. "Failed to allocate logical descriptors\n");
  565. tsi721_bdma_ch_free(bdma_chan);
  566. return -ENOMEM;
  567. }
  568. bdma_chan->tx_desc = desc;
  569. for (i = 0; i < TSI721_DMA_TX_QUEUE_SZ; i++) {
  570. dma_async_tx_descriptor_init(&desc[i].txd, dchan);
  571. desc[i].txd.tx_submit = tsi721_tx_submit;
  572. desc[i].txd.flags = DMA_CTRL_ACK;
  573. list_add(&desc[i].desc_node, &bdma_chan->free_list);
  574. }
  575. dma_cookie_init(dchan);
  576. bdma_chan->active = true;
  577. tsi721_bdma_interrupt_enable(bdma_chan, 1);
  578. return TSI721_DMA_TX_QUEUE_SZ;
  579. }
  580. static void tsi721_sync_dma_irq(struct tsi721_bdma_chan *bdma_chan)
  581. {
  582. struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
  583. #ifdef CONFIG_PCI_MSI
  584. if (priv->flags & TSI721_USING_MSIX) {
  585. synchronize_irq(priv->msix[TSI721_VECT_DMA0_DONE +
  586. bdma_chan->id].vector);
  587. synchronize_irq(priv->msix[TSI721_VECT_DMA0_INT +
  588. bdma_chan->id].vector);
  589. } else
  590. #endif
  591. synchronize_irq(priv->pdev->irq);
  592. }
  593. static void tsi721_free_chan_resources(struct dma_chan *dchan)
  594. {
  595. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  596. dev_dbg(dchan->device->dev, "%s: for channel %d\n",
  597. __func__, bdma_chan->id);
  598. if (bdma_chan->bd_base == NULL)
  599. return;
  600. BUG_ON(!list_empty(&bdma_chan->active_list));
  601. BUG_ON(!list_empty(&bdma_chan->queue));
  602. tsi721_bdma_interrupt_enable(bdma_chan, 0);
  603. bdma_chan->active = false;
  604. tsi721_sync_dma_irq(bdma_chan);
  605. tasklet_kill(&bdma_chan->tasklet);
  606. INIT_LIST_HEAD(&bdma_chan->free_list);
  607. kfree(bdma_chan->tx_desc);
  608. tsi721_bdma_ch_free(bdma_chan);
  609. }
  610. static
  611. enum dma_status tsi721_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
  612. struct dma_tx_state *txstate)
  613. {
  614. return dma_cookie_status(dchan, cookie, txstate);
  615. }
  616. static void tsi721_issue_pending(struct dma_chan *dchan)
  617. {
  618. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  619. dev_dbg(dchan->device->dev, "%s: Enter\n", __func__);
  620. if (tsi721_dma_is_idle(bdma_chan) && bdma_chan->active) {
  621. spin_lock_bh(&bdma_chan->lock);
  622. tsi721_advance_work(bdma_chan);
  623. spin_unlock_bh(&bdma_chan->lock);
  624. }
  625. }
  626. static
  627. struct dma_async_tx_descriptor *tsi721_prep_rio_sg(struct dma_chan *dchan,
  628. struct scatterlist *sgl, unsigned int sg_len,
  629. enum dma_transfer_direction dir, unsigned long flags,
  630. void *tinfo)
  631. {
  632. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  633. struct tsi721_tx_desc *desc, *_d;
  634. struct rio_dma_ext *rext = tinfo;
  635. enum dma_rtype rtype;
  636. struct dma_async_tx_descriptor *txd = NULL;
  637. if (!sgl || !sg_len) {
  638. dev_err(dchan->device->dev, "%s: No SG list\n", __func__);
  639. return NULL;
  640. }
  641. dev_dbg(dchan->device->dev, "%s: %s\n", __func__,
  642. (dir == DMA_DEV_TO_MEM)?"READ":"WRITE");
  643. if (dir == DMA_DEV_TO_MEM)
  644. rtype = NREAD;
  645. else if (dir == DMA_MEM_TO_DEV) {
  646. switch (rext->wr_type) {
  647. case RDW_ALL_NWRITE:
  648. rtype = ALL_NWRITE;
  649. break;
  650. case RDW_ALL_NWRITE_R:
  651. rtype = ALL_NWRITE_R;
  652. break;
  653. case RDW_LAST_NWRITE_R:
  654. default:
  655. rtype = LAST_NWRITE_R;
  656. break;
  657. }
  658. } else {
  659. dev_err(dchan->device->dev,
  660. "%s: Unsupported DMA direction option\n", __func__);
  661. return NULL;
  662. }
  663. spin_lock_bh(&bdma_chan->lock);
  664. list_for_each_entry_safe(desc, _d, &bdma_chan->free_list, desc_node) {
  665. if (async_tx_test_ack(&desc->txd)) {
  666. list_del_init(&desc->desc_node);
  667. desc->destid = rext->destid;
  668. desc->rio_addr = rext->rio_addr;
  669. desc->rio_addr_u = 0;
  670. desc->rtype = rtype;
  671. desc->sg_len = sg_len;
  672. desc->sg = sgl;
  673. txd = &desc->txd;
  674. txd->flags = flags;
  675. break;
  676. }
  677. }
  678. spin_unlock_bh(&bdma_chan->lock);
  679. return txd;
  680. }
  681. static int tsi721_device_control(struct dma_chan *dchan, enum dma_ctrl_cmd cmd,
  682. unsigned long arg)
  683. {
  684. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  685. struct tsi721_tx_desc *desc, *_d;
  686. u32 dmac_int;
  687. LIST_HEAD(list);
  688. dev_dbg(dchan->device->dev, "%s: Entry\n", __func__);
  689. if (cmd != DMA_TERMINATE_ALL)
  690. return -ENOSYS;
  691. spin_lock_bh(&bdma_chan->lock);
  692. bdma_chan->active = false;
  693. if (!tsi721_dma_is_idle(bdma_chan)) {
  694. /* make sure to stop the transfer */
  695. iowrite32(TSI721_DMAC_CTL_SUSP,
  696. bdma_chan->regs + TSI721_DMAC_CTL);
  697. /* Wait until DMA channel stops */
  698. do {
  699. dmac_int = ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  700. } while ((dmac_int & TSI721_DMAC_INT_SUSP) == 0);
  701. }
  702. list_splice_init(&bdma_chan->active_list, &list);
  703. list_splice_init(&bdma_chan->queue, &list);
  704. list_for_each_entry_safe(desc, _d, &list, desc_node)
  705. tsi721_dma_tx_err(bdma_chan, desc);
  706. spin_unlock_bh(&bdma_chan->lock);
  707. return 0;
  708. }
  709. int tsi721_register_dma(struct tsi721_device *priv)
  710. {
  711. int i;
  712. int nr_channels = 0;
  713. int err;
  714. struct rio_mport *mport = priv->mport;
  715. INIT_LIST_HEAD(&mport->dma.channels);
  716. for (i = 0; i < TSI721_DMA_MAXCH; i++) {
  717. struct tsi721_bdma_chan *bdma_chan = &priv->bdma[i];
  718. if (i == TSI721_DMACH_MAINT)
  719. continue;
  720. bdma_chan->regs = priv->regs + TSI721_DMAC_BASE(i);
  721. bdma_chan->dchan.device = &mport->dma;
  722. bdma_chan->dchan.cookie = 1;
  723. bdma_chan->dchan.chan_id = i;
  724. bdma_chan->id = i;
  725. bdma_chan->active = false;
  726. spin_lock_init(&bdma_chan->lock);
  727. INIT_LIST_HEAD(&bdma_chan->active_list);
  728. INIT_LIST_HEAD(&bdma_chan->queue);
  729. INIT_LIST_HEAD(&bdma_chan->free_list);
  730. tasklet_init(&bdma_chan->tasklet, tsi721_dma_tasklet,
  731. (unsigned long)bdma_chan);
  732. list_add_tail(&bdma_chan->dchan.device_node,
  733. &mport->dma.channels);
  734. nr_channels++;
  735. }
  736. mport->dma.chancnt = nr_channels;
  737. dma_cap_zero(mport->dma.cap_mask);
  738. dma_cap_set(DMA_PRIVATE, mport->dma.cap_mask);
  739. dma_cap_set(DMA_SLAVE, mport->dma.cap_mask);
  740. mport->dma.dev = &priv->pdev->dev;
  741. mport->dma.device_alloc_chan_resources = tsi721_alloc_chan_resources;
  742. mport->dma.device_free_chan_resources = tsi721_free_chan_resources;
  743. mport->dma.device_tx_status = tsi721_tx_status;
  744. mport->dma.device_issue_pending = tsi721_issue_pending;
  745. mport->dma.device_prep_slave_sg = tsi721_prep_rio_sg;
  746. mport->dma.device_control = tsi721_device_control;
  747. err = dma_async_device_register(&mport->dma);
  748. if (err)
  749. dev_err(&priv->pdev->dev, "Failed to register DMA device\n");
  750. return err;
  751. }