tsi721_dma.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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/sched.h>
  33. #include <linux/delay.h>
  34. #include "../../dma/dmaengine.h"
  35. #include "tsi721.h"
  36. #define TSI721_DMA_TX_QUEUE_SZ 16 /* number of transaction descriptors */
  37. #ifdef CONFIG_PCI_MSI
  38. static irqreturn_t tsi721_bdma_msix(int irq, void *ptr);
  39. #endif
  40. static int tsi721_submit_sg(struct tsi721_tx_desc *desc);
  41. static unsigned int dma_desc_per_channel = 128;
  42. module_param(dma_desc_per_channel, uint, S_IWUSR | S_IRUGO);
  43. MODULE_PARM_DESC(dma_desc_per_channel,
  44. "Number of DMA descriptors per channel (default: 128)");
  45. static inline struct tsi721_bdma_chan *to_tsi721_chan(struct dma_chan *chan)
  46. {
  47. return container_of(chan, struct tsi721_bdma_chan, dchan);
  48. }
  49. static inline struct tsi721_device *to_tsi721(struct dma_device *ddev)
  50. {
  51. return container_of(ddev, struct rio_mport, dma)->priv;
  52. }
  53. static inline
  54. struct tsi721_tx_desc *to_tsi721_desc(struct dma_async_tx_descriptor *txd)
  55. {
  56. return container_of(txd, struct tsi721_tx_desc, txd);
  57. }
  58. static int tsi721_bdma_ch_init(struct tsi721_bdma_chan *bdma_chan, int bd_num)
  59. {
  60. struct tsi721_dma_desc *bd_ptr;
  61. struct device *dev = bdma_chan->dchan.device->dev;
  62. u64 *sts_ptr;
  63. dma_addr_t bd_phys;
  64. dma_addr_t sts_phys;
  65. int sts_size;
  66. #ifdef CONFIG_PCI_MSI
  67. struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
  68. #endif
  69. tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d", bdma_chan->id);
  70. /*
  71. * Allocate space for DMA descriptors
  72. * (add an extra element for link descriptor)
  73. */
  74. bd_ptr = dma_zalloc_coherent(dev,
  75. (bd_num + 1) * sizeof(struct tsi721_dma_desc),
  76. &bd_phys, GFP_ATOMIC);
  77. if (!bd_ptr)
  78. return -ENOMEM;
  79. bdma_chan->bd_num = bd_num;
  80. bdma_chan->bd_phys = bd_phys;
  81. bdma_chan->bd_base = bd_ptr;
  82. tsi_debug(DMA, &bdma_chan->dchan.dev->device,
  83. "DMAC%d descriptors @ %p (phys = %pad)",
  84. bdma_chan->id, bd_ptr, &bd_phys);
  85. /* Allocate space for descriptor status FIFO */
  86. sts_size = ((bd_num + 1) >= TSI721_DMA_MINSTSSZ) ?
  87. (bd_num + 1) : TSI721_DMA_MINSTSSZ;
  88. sts_size = roundup_pow_of_two(sts_size);
  89. sts_ptr = dma_zalloc_coherent(dev,
  90. sts_size * sizeof(struct tsi721_dma_sts),
  91. &sts_phys, GFP_ATOMIC);
  92. if (!sts_ptr) {
  93. /* Free space allocated for DMA descriptors */
  94. dma_free_coherent(dev,
  95. (bd_num + 1) * sizeof(struct tsi721_dma_desc),
  96. bd_ptr, bd_phys);
  97. bdma_chan->bd_base = NULL;
  98. return -ENOMEM;
  99. }
  100. bdma_chan->sts_phys = sts_phys;
  101. bdma_chan->sts_base = sts_ptr;
  102. bdma_chan->sts_size = sts_size;
  103. tsi_debug(DMA, &bdma_chan->dchan.dev->device,
  104. "DMAC%d desc status FIFO @ %p (phys = %pad) size=0x%x",
  105. bdma_chan->id, sts_ptr, &sts_phys, sts_size);
  106. /* Initialize DMA descriptors ring using added link descriptor */
  107. bd_ptr[bd_num].type_id = cpu_to_le32(DTYPE3 << 29);
  108. bd_ptr[bd_num].next_lo = cpu_to_le32((u64)bd_phys &
  109. TSI721_DMAC_DPTRL_MASK);
  110. bd_ptr[bd_num].next_hi = cpu_to_le32((u64)bd_phys >> 32);
  111. /* Setup DMA descriptor pointers */
  112. iowrite32(((u64)bd_phys >> 32),
  113. bdma_chan->regs + TSI721_DMAC_DPTRH);
  114. iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
  115. bdma_chan->regs + TSI721_DMAC_DPTRL);
  116. /* Setup descriptor status FIFO */
  117. iowrite32(((u64)sts_phys >> 32),
  118. bdma_chan->regs + TSI721_DMAC_DSBH);
  119. iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
  120. bdma_chan->regs + TSI721_DMAC_DSBL);
  121. iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
  122. bdma_chan->regs + TSI721_DMAC_DSSZ);
  123. /* Clear interrupt bits */
  124. iowrite32(TSI721_DMAC_INT_ALL,
  125. bdma_chan->regs + TSI721_DMAC_INT);
  126. ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  127. #ifdef CONFIG_PCI_MSI
  128. /* Request interrupt service if we are in MSI-X mode */
  129. if (priv->flags & TSI721_USING_MSIX) {
  130. int rc, idx;
  131. idx = TSI721_VECT_DMA0_DONE + bdma_chan->id;
  132. rc = request_irq(priv->msix[idx].vector, tsi721_bdma_msix, 0,
  133. priv->msix[idx].irq_name, (void *)bdma_chan);
  134. if (rc) {
  135. tsi_debug(DMA, &bdma_chan->dchan.dev->device,
  136. "Unable to get MSI-X for DMAC%d-DONE",
  137. bdma_chan->id);
  138. goto err_out;
  139. }
  140. idx = TSI721_VECT_DMA0_INT + bdma_chan->id;
  141. rc = request_irq(priv->msix[idx].vector, tsi721_bdma_msix, 0,
  142. priv->msix[idx].irq_name, (void *)bdma_chan);
  143. if (rc) {
  144. tsi_debug(DMA, &bdma_chan->dchan.dev->device,
  145. "Unable to get MSI-X for DMAC%d-INT",
  146. bdma_chan->id);
  147. free_irq(
  148. priv->msix[TSI721_VECT_DMA0_DONE +
  149. bdma_chan->id].vector,
  150. (void *)bdma_chan);
  151. }
  152. err_out:
  153. if (rc) {
  154. /* Free space allocated for DMA descriptors */
  155. dma_free_coherent(dev,
  156. (bd_num + 1) * sizeof(struct tsi721_dma_desc),
  157. bd_ptr, bd_phys);
  158. bdma_chan->bd_base = NULL;
  159. /* Free space allocated for status descriptors */
  160. dma_free_coherent(dev,
  161. sts_size * sizeof(struct tsi721_dma_sts),
  162. sts_ptr, sts_phys);
  163. bdma_chan->sts_base = NULL;
  164. return -EIO;
  165. }
  166. }
  167. #endif /* CONFIG_PCI_MSI */
  168. /* Toggle DMA channel initialization */
  169. iowrite32(TSI721_DMAC_CTL_INIT, bdma_chan->regs + TSI721_DMAC_CTL);
  170. ioread32(bdma_chan->regs + TSI721_DMAC_CTL);
  171. bdma_chan->wr_count = bdma_chan->wr_count_next = 0;
  172. bdma_chan->sts_rdptr = 0;
  173. udelay(10);
  174. return 0;
  175. }
  176. static int tsi721_bdma_ch_free(struct tsi721_bdma_chan *bdma_chan)
  177. {
  178. u32 ch_stat;
  179. #ifdef CONFIG_PCI_MSI
  180. struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
  181. #endif
  182. if (bdma_chan->bd_base == NULL)
  183. return 0;
  184. /* Check if DMA channel still running */
  185. ch_stat = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  186. if (ch_stat & TSI721_DMAC_STS_RUN)
  187. return -EFAULT;
  188. /* Put DMA channel into init state */
  189. iowrite32(TSI721_DMAC_CTL_INIT, bdma_chan->regs + TSI721_DMAC_CTL);
  190. #ifdef CONFIG_PCI_MSI
  191. if (priv->flags & TSI721_USING_MSIX) {
  192. free_irq(priv->msix[TSI721_VECT_DMA0_DONE +
  193. bdma_chan->id].vector, (void *)bdma_chan);
  194. free_irq(priv->msix[TSI721_VECT_DMA0_INT +
  195. bdma_chan->id].vector, (void *)bdma_chan);
  196. }
  197. #endif /* CONFIG_PCI_MSI */
  198. /* Free space allocated for DMA descriptors */
  199. dma_free_coherent(bdma_chan->dchan.device->dev,
  200. (bdma_chan->bd_num + 1) * sizeof(struct tsi721_dma_desc),
  201. bdma_chan->bd_base, bdma_chan->bd_phys);
  202. bdma_chan->bd_base = NULL;
  203. /* Free space allocated for status FIFO */
  204. dma_free_coherent(bdma_chan->dchan.device->dev,
  205. bdma_chan->sts_size * sizeof(struct tsi721_dma_sts),
  206. bdma_chan->sts_base, bdma_chan->sts_phys);
  207. bdma_chan->sts_base = NULL;
  208. return 0;
  209. }
  210. static void
  211. tsi721_bdma_interrupt_enable(struct tsi721_bdma_chan *bdma_chan, int enable)
  212. {
  213. if (enable) {
  214. /* Clear pending BDMA channel interrupts */
  215. iowrite32(TSI721_DMAC_INT_ALL,
  216. bdma_chan->regs + TSI721_DMAC_INT);
  217. ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  218. /* Enable BDMA channel interrupts */
  219. iowrite32(TSI721_DMAC_INT_ALL,
  220. bdma_chan->regs + TSI721_DMAC_INTE);
  221. } else {
  222. /* Disable BDMA channel interrupts */
  223. iowrite32(0, bdma_chan->regs + TSI721_DMAC_INTE);
  224. /* Clear pending BDMA channel interrupts */
  225. iowrite32(TSI721_DMAC_INT_ALL,
  226. bdma_chan->regs + TSI721_DMAC_INT);
  227. }
  228. }
  229. static bool tsi721_dma_is_idle(struct tsi721_bdma_chan *bdma_chan)
  230. {
  231. u32 sts;
  232. sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  233. return ((sts & TSI721_DMAC_STS_RUN) == 0);
  234. }
  235. void tsi721_bdma_handler(struct tsi721_bdma_chan *bdma_chan)
  236. {
  237. /* Disable BDMA channel interrupts */
  238. iowrite32(0, bdma_chan->regs + TSI721_DMAC_INTE);
  239. if (bdma_chan->active)
  240. tasklet_hi_schedule(&bdma_chan->tasklet);
  241. }
  242. #ifdef CONFIG_PCI_MSI
  243. /**
  244. * tsi721_omsg_msix - MSI-X interrupt handler for BDMA channels
  245. * @irq: Linux interrupt number
  246. * @ptr: Pointer to interrupt-specific data (BDMA channel structure)
  247. *
  248. * Handles BDMA channel interrupts signaled using MSI-X.
  249. */
  250. static irqreturn_t tsi721_bdma_msix(int irq, void *ptr)
  251. {
  252. struct tsi721_bdma_chan *bdma_chan = ptr;
  253. if (bdma_chan->active)
  254. tasklet_hi_schedule(&bdma_chan->tasklet);
  255. return IRQ_HANDLED;
  256. }
  257. #endif /* CONFIG_PCI_MSI */
  258. /* Must be called with the spinlock held */
  259. static void tsi721_start_dma(struct tsi721_bdma_chan *bdma_chan)
  260. {
  261. if (!tsi721_dma_is_idle(bdma_chan)) {
  262. tsi_err(&bdma_chan->dchan.dev->device,
  263. "DMAC%d Attempt to start non-idle channel",
  264. bdma_chan->id);
  265. return;
  266. }
  267. if (bdma_chan->wr_count == bdma_chan->wr_count_next) {
  268. tsi_err(&bdma_chan->dchan.dev->device,
  269. "DMAC%d Attempt to start DMA with no BDs ready %d",
  270. bdma_chan->id, task_pid_nr(current));
  271. return;
  272. }
  273. tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d (wrc=%d) %d",
  274. bdma_chan->id, bdma_chan->wr_count_next,
  275. task_pid_nr(current));
  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. struct device *ch_dev = &dchan->dev->device;
  361. if (!tsi721_dma_is_idle(bdma_chan)) {
  362. tsi_err(ch_dev, "DMAC%d ERR: Attempt to use non-idle channel",
  363. bdma_chan->id);
  364. return -EIO;
  365. }
  366. /*
  367. * Fill DMA channel's hardware buffer descriptors.
  368. * (NOTE: RapidIO destination address is limited to 64 bits for now)
  369. */
  370. rio_addr = desc->rio_addr;
  371. next_addr = -1;
  372. bcount = 0;
  373. sys_size = dma_to_mport(dchan->device)->sys_size;
  374. rd_idx = ioread32(bdma_chan->regs + TSI721_DMAC_DRDCNT);
  375. rd_idx %= (bdma_chan->bd_num + 1);
  376. idx = bdma_chan->wr_count_next % (bdma_chan->bd_num + 1);
  377. if (idx == bdma_chan->bd_num) {
  378. /* wrap around link descriptor */
  379. idx = 0;
  380. add_count++;
  381. }
  382. tsi_debug(DMA, ch_dev, "DMAC%d BD ring status: rdi=%d wri=%d",
  383. bdma_chan->id, rd_idx, idx);
  384. for_each_sg(desc->sg, sg, desc->sg_len, i) {
  385. tsi_debug(DMAV, ch_dev, "DMAC%d sg%d/%d addr: 0x%llx len: %d",
  386. bdma_chan->id, i, desc->sg_len,
  387. (unsigned long long)sg_dma_address(sg), sg_dma_len(sg));
  388. if (sg_dma_len(sg) > TSI721_BDMA_MAX_BCOUNT) {
  389. tsi_err(ch_dev, "DMAC%d SG entry %d is too large",
  390. bdma_chan->id, i);
  391. err = -EINVAL;
  392. break;
  393. }
  394. /*
  395. * If this sg entry forms contiguous block with previous one,
  396. * try to merge it into existing DMA descriptor
  397. */
  398. if (next_addr == sg_dma_address(sg) &&
  399. bcount + sg_dma_len(sg) <= TSI721_BDMA_MAX_BCOUNT) {
  400. /* Adjust byte count of the descriptor */
  401. bcount += sg_dma_len(sg);
  402. goto entry_done;
  403. } else if (next_addr != -1) {
  404. /* Finalize descriptor using total byte count value */
  405. tsi721_desc_fill_end(bd_ptr, bcount, 0);
  406. tsi_debug(DMAV, ch_dev, "DMAC%d prev desc final len: %d",
  407. bdma_chan->id, bcount);
  408. }
  409. desc->rio_addr = rio_addr;
  410. if (i && idx == rd_idx) {
  411. tsi_debug(DMAV, ch_dev,
  412. "DMAC%d HW descriptor ring is full @ %d",
  413. bdma_chan->id, 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. tsi_err(ch_dev, "Failed to build desc: err=%d", err);
  422. break;
  423. }
  424. tsi_debug(DMAV, ch_dev, "DMAC%d bd_ptr = %p did=%d raddr=0x%llx",
  425. bdma_chan->id, bd_ptr, desc->destid, desc->rio_addr);
  426. next_addr = sg_dma_address(sg);
  427. bcount = sg_dma_len(sg);
  428. add_count++;
  429. if (++idx == bdma_chan->bd_num) {
  430. /* wrap around link descriptor */
  431. idx = 0;
  432. add_count++;
  433. }
  434. entry_done:
  435. if (sg_is_last(sg)) {
  436. tsi721_desc_fill_end(bd_ptr, bcount, 0);
  437. tsi_debug(DMAV, ch_dev,
  438. "DMAC%d last desc final len: %d",
  439. bdma_chan->id, 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. struct tsi721_tx_desc *desc)
  452. {
  453. int err;
  454. tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d", bdma_chan->id);
  455. if (!tsi721_dma_is_idle(bdma_chan))
  456. return;
  457. /*
  458. * If there is no data transfer in progress, fetch new descriptor from
  459. * the pending queue.
  460. */
  461. if (desc == NULL && bdma_chan->active_tx == NULL &&
  462. !list_empty(&bdma_chan->queue)) {
  463. desc = list_first_entry(&bdma_chan->queue,
  464. struct tsi721_tx_desc, desc_node);
  465. list_del_init((&desc->desc_node));
  466. bdma_chan->active_tx = desc;
  467. }
  468. if (desc) {
  469. err = tsi721_submit_sg(desc);
  470. if (!err)
  471. tsi721_start_dma(bdma_chan);
  472. else {
  473. tsi721_dma_tx_err(bdma_chan, desc);
  474. tsi_debug(DMA, &bdma_chan->dchan.dev->device,
  475. "DMAC%d ERR: tsi721_submit_sg failed with err=%d",
  476. bdma_chan->id, err);
  477. }
  478. }
  479. tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d Exit",
  480. bdma_chan->id);
  481. }
  482. static void tsi721_dma_tasklet(unsigned long data)
  483. {
  484. struct tsi721_bdma_chan *bdma_chan = (struct tsi721_bdma_chan *)data;
  485. u32 dmac_int, dmac_sts;
  486. dmac_int = ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  487. tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d_INT = 0x%x",
  488. bdma_chan->id, dmac_int);
  489. /* Clear channel interrupts */
  490. iowrite32(dmac_int, bdma_chan->regs + TSI721_DMAC_INT);
  491. if (dmac_int & TSI721_DMAC_INT_ERR) {
  492. int i = 10000;
  493. struct tsi721_tx_desc *desc;
  494. desc = bdma_chan->active_tx;
  495. dmac_sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  496. tsi_err(&bdma_chan->dchan.dev->device,
  497. "DMAC%d_STS = 0x%x did=%d raddr=0x%llx",
  498. bdma_chan->id, dmac_sts, desc->destid, desc->rio_addr);
  499. /* Re-initialize DMA channel if possible */
  500. if ((dmac_sts & TSI721_DMAC_STS_ABORT) == 0)
  501. goto err_out;
  502. tsi721_clr_stat(bdma_chan);
  503. spin_lock(&bdma_chan->lock);
  504. /* Put DMA channel into init state */
  505. iowrite32(TSI721_DMAC_CTL_INIT,
  506. bdma_chan->regs + TSI721_DMAC_CTL);
  507. do {
  508. udelay(1);
  509. dmac_sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  510. i--;
  511. } while ((dmac_sts & TSI721_DMAC_STS_ABORT) && i);
  512. if (dmac_sts & TSI721_DMAC_STS_ABORT) {
  513. tsi_err(&bdma_chan->dchan.dev->device,
  514. "Failed to re-initiate DMAC%d", bdma_chan->id);
  515. spin_unlock(&bdma_chan->lock);
  516. goto err_out;
  517. }
  518. /* Setup DMA descriptor pointers */
  519. iowrite32(((u64)bdma_chan->bd_phys >> 32),
  520. bdma_chan->regs + TSI721_DMAC_DPTRH);
  521. iowrite32(((u64)bdma_chan->bd_phys & TSI721_DMAC_DPTRL_MASK),
  522. bdma_chan->regs + TSI721_DMAC_DPTRL);
  523. /* Setup descriptor status FIFO */
  524. iowrite32(((u64)bdma_chan->sts_phys >> 32),
  525. bdma_chan->regs + TSI721_DMAC_DSBH);
  526. iowrite32(((u64)bdma_chan->sts_phys & TSI721_DMAC_DSBL_MASK),
  527. bdma_chan->regs + TSI721_DMAC_DSBL);
  528. iowrite32(TSI721_DMAC_DSSZ_SIZE(bdma_chan->sts_size),
  529. bdma_chan->regs + TSI721_DMAC_DSSZ);
  530. /* Clear interrupt bits */
  531. iowrite32(TSI721_DMAC_INT_ALL,
  532. bdma_chan->regs + TSI721_DMAC_INT);
  533. ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  534. bdma_chan->wr_count = bdma_chan->wr_count_next = 0;
  535. bdma_chan->sts_rdptr = 0;
  536. udelay(10);
  537. desc = bdma_chan->active_tx;
  538. desc->status = DMA_ERROR;
  539. dma_cookie_complete(&desc->txd);
  540. list_add(&desc->desc_node, &bdma_chan->free_list);
  541. bdma_chan->active_tx = NULL;
  542. if (bdma_chan->active)
  543. tsi721_advance_work(bdma_chan, NULL);
  544. spin_unlock(&bdma_chan->lock);
  545. }
  546. if (dmac_int & TSI721_DMAC_INT_STFULL) {
  547. tsi_err(&bdma_chan->dchan.dev->device,
  548. "DMAC%d descriptor status FIFO is full",
  549. bdma_chan->id);
  550. }
  551. if (dmac_int & (TSI721_DMAC_INT_DONE | TSI721_DMAC_INT_IOFDONE)) {
  552. struct tsi721_tx_desc *desc;
  553. tsi721_clr_stat(bdma_chan);
  554. spin_lock(&bdma_chan->lock);
  555. desc = bdma_chan->active_tx;
  556. if (desc->sg_len == 0) {
  557. dma_async_tx_callback callback = NULL;
  558. void *param = NULL;
  559. desc->status = DMA_COMPLETE;
  560. dma_cookie_complete(&desc->txd);
  561. if (desc->txd.flags & DMA_PREP_INTERRUPT) {
  562. callback = desc->txd.callback;
  563. param = desc->txd.callback_param;
  564. }
  565. list_add(&desc->desc_node, &bdma_chan->free_list);
  566. bdma_chan->active_tx = NULL;
  567. if (bdma_chan->active)
  568. tsi721_advance_work(bdma_chan, NULL);
  569. spin_unlock(&bdma_chan->lock);
  570. if (callback)
  571. callback(param);
  572. } else {
  573. if (bdma_chan->active)
  574. tsi721_advance_work(bdma_chan,
  575. bdma_chan->active_tx);
  576. spin_unlock(&bdma_chan->lock);
  577. }
  578. }
  579. err_out:
  580. /* Re-Enable BDMA channel interrupts */
  581. iowrite32(TSI721_DMAC_INT_ALL, bdma_chan->regs + TSI721_DMAC_INTE);
  582. }
  583. static dma_cookie_t tsi721_tx_submit(struct dma_async_tx_descriptor *txd)
  584. {
  585. struct tsi721_tx_desc *desc = to_tsi721_desc(txd);
  586. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(txd->chan);
  587. dma_cookie_t cookie;
  588. /* Check if the descriptor is detached from any lists */
  589. if (!list_empty(&desc->desc_node)) {
  590. tsi_err(&bdma_chan->dchan.dev->device,
  591. "DMAC%d wrong state of descriptor %p",
  592. bdma_chan->id, txd);
  593. return -EIO;
  594. }
  595. spin_lock_bh(&bdma_chan->lock);
  596. if (!bdma_chan->active) {
  597. spin_unlock_bh(&bdma_chan->lock);
  598. return -ENODEV;
  599. }
  600. cookie = dma_cookie_assign(txd);
  601. desc->status = DMA_IN_PROGRESS;
  602. list_add_tail(&desc->desc_node, &bdma_chan->queue);
  603. spin_unlock_bh(&bdma_chan->lock);
  604. return cookie;
  605. }
  606. static int tsi721_alloc_chan_resources(struct dma_chan *dchan)
  607. {
  608. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  609. struct tsi721_tx_desc *desc = NULL;
  610. int i;
  611. tsi_debug(DMA, &dchan->dev->device, "DMAC%d", bdma_chan->id);
  612. if (bdma_chan->bd_base)
  613. return TSI721_DMA_TX_QUEUE_SZ;
  614. /* Initialize BDMA channel */
  615. if (tsi721_bdma_ch_init(bdma_chan, dma_desc_per_channel)) {
  616. tsi_err(&dchan->dev->device, "Unable to initialize DMAC%d",
  617. bdma_chan->id);
  618. return -ENODEV;
  619. }
  620. /* Allocate queue of transaction descriptors */
  621. desc = kcalloc(TSI721_DMA_TX_QUEUE_SZ, sizeof(struct tsi721_tx_desc),
  622. GFP_ATOMIC);
  623. if (!desc) {
  624. tsi_err(&dchan->dev->device,
  625. "DMAC%d Failed to allocate logical descriptors",
  626. bdma_chan->id);
  627. tsi721_bdma_ch_free(bdma_chan);
  628. return -ENOMEM;
  629. }
  630. bdma_chan->tx_desc = desc;
  631. for (i = 0; i < TSI721_DMA_TX_QUEUE_SZ; i++) {
  632. dma_async_tx_descriptor_init(&desc[i].txd, dchan);
  633. desc[i].txd.tx_submit = tsi721_tx_submit;
  634. desc[i].txd.flags = DMA_CTRL_ACK;
  635. list_add(&desc[i].desc_node, &bdma_chan->free_list);
  636. }
  637. dma_cookie_init(dchan);
  638. bdma_chan->active = true;
  639. tsi721_bdma_interrupt_enable(bdma_chan, 1);
  640. return TSI721_DMA_TX_QUEUE_SZ;
  641. }
  642. static void tsi721_sync_dma_irq(struct tsi721_bdma_chan *bdma_chan)
  643. {
  644. struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
  645. #ifdef CONFIG_PCI_MSI
  646. if (priv->flags & TSI721_USING_MSIX) {
  647. synchronize_irq(priv->msix[TSI721_VECT_DMA0_DONE +
  648. bdma_chan->id].vector);
  649. synchronize_irq(priv->msix[TSI721_VECT_DMA0_INT +
  650. bdma_chan->id].vector);
  651. } else
  652. #endif
  653. synchronize_irq(priv->pdev->irq);
  654. }
  655. static void tsi721_free_chan_resources(struct dma_chan *dchan)
  656. {
  657. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  658. tsi_debug(DMA, &dchan->dev->device, "DMAC%d", bdma_chan->id);
  659. if (bdma_chan->bd_base == NULL)
  660. return;
  661. tsi721_bdma_interrupt_enable(bdma_chan, 0);
  662. bdma_chan->active = false;
  663. tsi721_sync_dma_irq(bdma_chan);
  664. tasklet_kill(&bdma_chan->tasklet);
  665. INIT_LIST_HEAD(&bdma_chan->free_list);
  666. kfree(bdma_chan->tx_desc);
  667. tsi721_bdma_ch_free(bdma_chan);
  668. }
  669. static
  670. enum dma_status tsi721_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
  671. struct dma_tx_state *txstate)
  672. {
  673. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  674. enum dma_status status;
  675. spin_lock_bh(&bdma_chan->lock);
  676. status = dma_cookie_status(dchan, cookie, txstate);
  677. spin_unlock_bh(&bdma_chan->lock);
  678. return status;
  679. }
  680. static void tsi721_issue_pending(struct dma_chan *dchan)
  681. {
  682. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  683. tsi_debug(DMA, &dchan->dev->device, "DMAC%d", bdma_chan->id);
  684. spin_lock_bh(&bdma_chan->lock);
  685. if (tsi721_dma_is_idle(bdma_chan) && bdma_chan->active) {
  686. tsi721_advance_work(bdma_chan, NULL);
  687. }
  688. spin_unlock_bh(&bdma_chan->lock);
  689. }
  690. static
  691. struct dma_async_tx_descriptor *tsi721_prep_rio_sg(struct dma_chan *dchan,
  692. struct scatterlist *sgl, unsigned int sg_len,
  693. enum dma_transfer_direction dir, unsigned long flags,
  694. void *tinfo)
  695. {
  696. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  697. struct tsi721_tx_desc *desc;
  698. struct rio_dma_ext *rext = tinfo;
  699. enum dma_rtype rtype;
  700. struct dma_async_tx_descriptor *txd = NULL;
  701. if (!sgl || !sg_len) {
  702. tsi_err(&dchan->dev->device, "DMAC%d No SG list",
  703. bdma_chan->id);
  704. return ERR_PTR(-EINVAL);
  705. }
  706. tsi_debug(DMA, &dchan->dev->device, "DMAC%d %s", bdma_chan->id,
  707. (dir == DMA_DEV_TO_MEM)?"READ":"WRITE");
  708. if (dir == DMA_DEV_TO_MEM)
  709. rtype = NREAD;
  710. else if (dir == DMA_MEM_TO_DEV) {
  711. switch (rext->wr_type) {
  712. case RDW_ALL_NWRITE:
  713. rtype = ALL_NWRITE;
  714. break;
  715. case RDW_ALL_NWRITE_R:
  716. rtype = ALL_NWRITE_R;
  717. break;
  718. case RDW_LAST_NWRITE_R:
  719. default:
  720. rtype = LAST_NWRITE_R;
  721. break;
  722. }
  723. } else {
  724. tsi_err(&dchan->dev->device,
  725. "DMAC%d Unsupported DMA direction option",
  726. bdma_chan->id);
  727. return ERR_PTR(-EINVAL);
  728. }
  729. spin_lock_bh(&bdma_chan->lock);
  730. if (!list_empty(&bdma_chan->free_list)) {
  731. desc = list_first_entry(&bdma_chan->free_list,
  732. struct tsi721_tx_desc, desc_node);
  733. list_del_init(&desc->desc_node);
  734. desc->destid = rext->destid;
  735. desc->rio_addr = rext->rio_addr;
  736. desc->rio_addr_u = 0;
  737. desc->rtype = rtype;
  738. desc->sg_len = sg_len;
  739. desc->sg = sgl;
  740. txd = &desc->txd;
  741. txd->flags = flags;
  742. }
  743. spin_unlock_bh(&bdma_chan->lock);
  744. if (!txd) {
  745. tsi_debug(DMA, &dchan->dev->device,
  746. "DMAC%d free TXD is not available", bdma_chan->id);
  747. return ERR_PTR(-EBUSY);
  748. }
  749. return txd;
  750. }
  751. static int tsi721_terminate_all(struct dma_chan *dchan)
  752. {
  753. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  754. struct tsi721_tx_desc *desc, *_d;
  755. LIST_HEAD(list);
  756. tsi_debug(DMA, &dchan->dev->device, "DMAC%d", bdma_chan->id);
  757. spin_lock_bh(&bdma_chan->lock);
  758. bdma_chan->active = false;
  759. while (!tsi721_dma_is_idle(bdma_chan)) {
  760. udelay(5);
  761. #if (0)
  762. /* make sure to stop the transfer */
  763. iowrite32(TSI721_DMAC_CTL_SUSP,
  764. bdma_chan->regs + TSI721_DMAC_CTL);
  765. /* Wait until DMA channel stops */
  766. do {
  767. dmac_int = ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  768. } while ((dmac_int & TSI721_DMAC_INT_SUSP) == 0);
  769. #endif
  770. }
  771. if (bdma_chan->active_tx)
  772. list_add(&bdma_chan->active_tx->desc_node, &list);
  773. list_splice_init(&bdma_chan->queue, &list);
  774. list_for_each_entry_safe(desc, _d, &list, desc_node)
  775. tsi721_dma_tx_err(bdma_chan, desc);
  776. spin_unlock_bh(&bdma_chan->lock);
  777. return 0;
  778. }
  779. static void tsi721_dma_stop(struct tsi721_bdma_chan *bdma_chan)
  780. {
  781. if (!bdma_chan->active)
  782. return;
  783. spin_lock_bh(&bdma_chan->lock);
  784. if (!tsi721_dma_is_idle(bdma_chan)) {
  785. int timeout = 100000;
  786. /* stop the transfer in progress */
  787. iowrite32(TSI721_DMAC_CTL_SUSP,
  788. bdma_chan->regs + TSI721_DMAC_CTL);
  789. /* Wait until DMA channel stops */
  790. while (!tsi721_dma_is_idle(bdma_chan) && --timeout)
  791. udelay(1);
  792. }
  793. spin_unlock_bh(&bdma_chan->lock);
  794. }
  795. void tsi721_dma_stop_all(struct tsi721_device *priv)
  796. {
  797. int i;
  798. for (i = 0; i < TSI721_DMA_MAXCH; i++) {
  799. if (i != TSI721_DMACH_MAINT)
  800. tsi721_dma_stop(&priv->bdma[i]);
  801. }
  802. }
  803. int tsi721_register_dma(struct tsi721_device *priv)
  804. {
  805. int i;
  806. int nr_channels = 0;
  807. int err;
  808. struct rio_mport *mport = &priv->mport;
  809. INIT_LIST_HEAD(&mport->dma.channels);
  810. for (i = 0; i < TSI721_DMA_MAXCH; i++) {
  811. struct tsi721_bdma_chan *bdma_chan = &priv->bdma[i];
  812. if (i == TSI721_DMACH_MAINT)
  813. continue;
  814. bdma_chan->regs = priv->regs + TSI721_DMAC_BASE(i);
  815. bdma_chan->dchan.device = &mport->dma;
  816. bdma_chan->dchan.cookie = 1;
  817. bdma_chan->dchan.chan_id = i;
  818. bdma_chan->id = i;
  819. bdma_chan->active = false;
  820. spin_lock_init(&bdma_chan->lock);
  821. bdma_chan->active_tx = NULL;
  822. INIT_LIST_HEAD(&bdma_chan->queue);
  823. INIT_LIST_HEAD(&bdma_chan->free_list);
  824. tasklet_init(&bdma_chan->tasklet, tsi721_dma_tasklet,
  825. (unsigned long)bdma_chan);
  826. list_add_tail(&bdma_chan->dchan.device_node,
  827. &mport->dma.channels);
  828. nr_channels++;
  829. }
  830. mport->dma.chancnt = nr_channels;
  831. dma_cap_zero(mport->dma.cap_mask);
  832. dma_cap_set(DMA_PRIVATE, mport->dma.cap_mask);
  833. dma_cap_set(DMA_SLAVE, mport->dma.cap_mask);
  834. mport->dma.dev = &priv->pdev->dev;
  835. mport->dma.device_alloc_chan_resources = tsi721_alloc_chan_resources;
  836. mport->dma.device_free_chan_resources = tsi721_free_chan_resources;
  837. mport->dma.device_tx_status = tsi721_tx_status;
  838. mport->dma.device_issue_pending = tsi721_issue_pending;
  839. mport->dma.device_prep_slave_sg = tsi721_prep_rio_sg;
  840. mport->dma.device_terminate_all = tsi721_terminate_all;
  841. err = dma_async_device_register(&mport->dma);
  842. if (err)
  843. tsi_err(&priv->pdev->dev, "Failed to register DMA device");
  844. return err;
  845. }
  846. void tsi721_unregister_dma(struct tsi721_device *priv)
  847. {
  848. struct rio_mport *mport = &priv->mport;
  849. struct dma_chan *chan, *_c;
  850. struct tsi721_bdma_chan *bdma_chan;
  851. tsi721_dma_stop_all(priv);
  852. dma_async_device_unregister(&mport->dma);
  853. list_for_each_entry_safe(chan, _c, &mport->dma.channels,
  854. device_node) {
  855. bdma_chan = to_tsi721_chan(chan);
  856. if (bdma_chan->active) {
  857. tsi721_bdma_interrupt_enable(bdma_chan, 0);
  858. bdma_chan->active = false;
  859. tsi721_sync_dma_irq(bdma_chan);
  860. tasklet_kill(&bdma_chan->tasklet);
  861. INIT_LIST_HEAD(&bdma_chan->free_list);
  862. kfree(bdma_chan->tx_desc);
  863. tsi721_bdma_ch_free(bdma_chan);
  864. }
  865. list_del(&chan->device_node);
  866. }
  867. }