tsi721_dma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. tasklet_schedule(&bdma_chan->tasklet);
  178. }
  179. #ifdef CONFIG_PCI_MSI
  180. /**
  181. * tsi721_omsg_msix - MSI-X interrupt handler for BDMA channels
  182. * @irq: Linux interrupt number
  183. * @ptr: Pointer to interrupt-specific data (BDMA channel structure)
  184. *
  185. * Handles BDMA channel interrupts signaled using MSI-X.
  186. */
  187. static irqreturn_t tsi721_bdma_msix(int irq, void *ptr)
  188. {
  189. struct tsi721_bdma_chan *bdma_chan = ptr;
  190. tsi721_bdma_handler(bdma_chan);
  191. return IRQ_HANDLED;
  192. }
  193. #endif /* CONFIG_PCI_MSI */
  194. /* Must be called with the spinlock held */
  195. static void tsi721_start_dma(struct tsi721_bdma_chan *bdma_chan)
  196. {
  197. if (!tsi721_dma_is_idle(bdma_chan)) {
  198. dev_err(bdma_chan->dchan.device->dev,
  199. "BUG: Attempt to start non-idle channel\n");
  200. return;
  201. }
  202. if (bdma_chan->wr_count == bdma_chan->wr_count_next) {
  203. dev_err(bdma_chan->dchan.device->dev,
  204. "BUG: Attempt to start DMA with no BDs ready\n");
  205. return;
  206. }
  207. dev_dbg(bdma_chan->dchan.device->dev,
  208. "tx_chan: %p, chan: %d, regs: %p\n",
  209. bdma_chan, bdma_chan->dchan.chan_id, bdma_chan->regs);
  210. iowrite32(bdma_chan->wr_count_next,
  211. bdma_chan->regs + TSI721_DMAC_DWRCNT);
  212. ioread32(bdma_chan->regs + TSI721_DMAC_DWRCNT);
  213. bdma_chan->wr_count = bdma_chan->wr_count_next;
  214. }
  215. static void tsi721_desc_put(struct tsi721_bdma_chan *bdma_chan,
  216. struct tsi721_tx_desc *desc)
  217. {
  218. dev_dbg(bdma_chan->dchan.device->dev,
  219. "Put desc: %p into free list\n", desc);
  220. if (desc) {
  221. spin_lock_bh(&bdma_chan->lock);
  222. list_splice_init(&desc->tx_list, &bdma_chan->free_list);
  223. list_add(&desc->desc_node, &bdma_chan->free_list);
  224. bdma_chan->wr_count_next = bdma_chan->wr_count;
  225. spin_unlock_bh(&bdma_chan->lock);
  226. }
  227. }
  228. static
  229. struct tsi721_tx_desc *tsi721_desc_get(struct tsi721_bdma_chan *bdma_chan)
  230. {
  231. struct tsi721_tx_desc *tx_desc, *_tx_desc;
  232. struct tsi721_tx_desc *ret = NULL;
  233. int i;
  234. spin_lock_bh(&bdma_chan->lock);
  235. list_for_each_entry_safe(tx_desc, _tx_desc,
  236. &bdma_chan->free_list, desc_node) {
  237. if (async_tx_test_ack(&tx_desc->txd)) {
  238. list_del(&tx_desc->desc_node);
  239. ret = tx_desc;
  240. break;
  241. }
  242. dev_dbg(bdma_chan->dchan.device->dev,
  243. "desc %p not ACKed\n", tx_desc);
  244. }
  245. i = bdma_chan->wr_count_next % bdma_chan->bd_num;
  246. if (i == bdma_chan->bd_num - 1) {
  247. i = 0;
  248. bdma_chan->wr_count_next++; /* skip link descriptor */
  249. }
  250. bdma_chan->wr_count_next++;
  251. tx_desc->txd.phys = bdma_chan->bd_phys +
  252. i * sizeof(struct tsi721_dma_desc);
  253. tx_desc->hw_desc = &((struct tsi721_dma_desc *)bdma_chan->bd_base)[i];
  254. spin_unlock_bh(&bdma_chan->lock);
  255. return ret;
  256. }
  257. static int
  258. tsi721_fill_desc(struct tsi721_bdma_chan *bdma_chan,
  259. struct tsi721_tx_desc *desc, struct scatterlist *sg,
  260. enum dma_rtype rtype, u32 sys_size)
  261. {
  262. struct tsi721_dma_desc *bd_ptr = desc->hw_desc;
  263. u64 rio_addr;
  264. if (sg_dma_len(sg) > TSI721_DMAD_BCOUNT1 + 1) {
  265. dev_err(bdma_chan->dchan.device->dev,
  266. "SG element is too large\n");
  267. return -EINVAL;
  268. }
  269. dev_dbg(bdma_chan->dchan.device->dev,
  270. "desc: 0x%llx, addr: 0x%llx len: 0x%x\n",
  271. (u64)desc->txd.phys, (unsigned long long)sg_dma_address(sg),
  272. sg_dma_len(sg));
  273. dev_dbg(bdma_chan->dchan.device->dev,
  274. "bd_ptr = %p did=%d raddr=0x%llx\n",
  275. bd_ptr, desc->destid, desc->rio_addr);
  276. /* Initialize DMA descriptor */
  277. bd_ptr->type_id = cpu_to_le32((DTYPE1 << 29) |
  278. (rtype << 19) | desc->destid);
  279. if (desc->interrupt)
  280. bd_ptr->type_id |= cpu_to_le32(TSI721_DMAD_IOF);
  281. bd_ptr->bcount = cpu_to_le32(((desc->rio_addr & 0x3) << 30) |
  282. (sys_size << 26) | sg_dma_len(sg));
  283. rio_addr = (desc->rio_addr >> 2) |
  284. ((u64)(desc->rio_addr_u & 0x3) << 62);
  285. bd_ptr->raddr_lo = cpu_to_le32(rio_addr & 0xffffffff);
  286. bd_ptr->raddr_hi = cpu_to_le32(rio_addr >> 32);
  287. bd_ptr->t1.bufptr_lo = cpu_to_le32(
  288. (u64)sg_dma_address(sg) & 0xffffffff);
  289. bd_ptr->t1.bufptr_hi = cpu_to_le32((u64)sg_dma_address(sg) >> 32);
  290. bd_ptr->t1.s_dist = 0;
  291. bd_ptr->t1.s_size = 0;
  292. return 0;
  293. }
  294. static void tsi721_dma_chain_complete(struct tsi721_bdma_chan *bdma_chan,
  295. struct tsi721_tx_desc *desc)
  296. {
  297. struct dma_async_tx_descriptor *txd = &desc->txd;
  298. dma_async_tx_callback callback = txd->callback;
  299. void *param = txd->callback_param;
  300. list_splice_init(&desc->tx_list, &bdma_chan->free_list);
  301. list_move(&desc->desc_node, &bdma_chan->free_list);
  302. bdma_chan->completed_cookie = txd->cookie;
  303. if (callback)
  304. callback(param);
  305. }
  306. static void tsi721_dma_complete_all(struct tsi721_bdma_chan *bdma_chan)
  307. {
  308. struct tsi721_tx_desc *desc, *_d;
  309. LIST_HEAD(list);
  310. BUG_ON(!tsi721_dma_is_idle(bdma_chan));
  311. if (!list_empty(&bdma_chan->queue))
  312. tsi721_start_dma(bdma_chan);
  313. list_splice_init(&bdma_chan->active_list, &list);
  314. list_splice_init(&bdma_chan->queue, &bdma_chan->active_list);
  315. list_for_each_entry_safe(desc, _d, &list, desc_node)
  316. tsi721_dma_chain_complete(bdma_chan, desc);
  317. }
  318. static void tsi721_clr_stat(struct tsi721_bdma_chan *bdma_chan)
  319. {
  320. u32 srd_ptr;
  321. u64 *sts_ptr;
  322. int i, j;
  323. /* Check and clear descriptor status FIFO entries */
  324. srd_ptr = bdma_chan->sts_rdptr;
  325. sts_ptr = bdma_chan->sts_base;
  326. j = srd_ptr * 8;
  327. while (sts_ptr[j]) {
  328. for (i = 0; i < 8 && sts_ptr[j]; i++, j++)
  329. sts_ptr[j] = 0;
  330. ++srd_ptr;
  331. srd_ptr %= bdma_chan->sts_size;
  332. j = srd_ptr * 8;
  333. }
  334. iowrite32(srd_ptr, bdma_chan->regs + TSI721_DMAC_DSRP);
  335. bdma_chan->sts_rdptr = srd_ptr;
  336. }
  337. static void tsi721_advance_work(struct tsi721_bdma_chan *bdma_chan)
  338. {
  339. if (list_empty(&bdma_chan->active_list) ||
  340. list_is_singular(&bdma_chan->active_list)) {
  341. dev_dbg(bdma_chan->dchan.device->dev,
  342. "%s: Active_list empty\n", __func__);
  343. tsi721_dma_complete_all(bdma_chan);
  344. } else {
  345. dev_dbg(bdma_chan->dchan.device->dev,
  346. "%s: Active_list NOT empty\n", __func__);
  347. tsi721_dma_chain_complete(bdma_chan,
  348. tsi721_dma_first_active(bdma_chan));
  349. tsi721_start_dma(bdma_chan);
  350. }
  351. }
  352. static void tsi721_dma_tasklet(unsigned long data)
  353. {
  354. struct tsi721_bdma_chan *bdma_chan = (struct tsi721_bdma_chan *)data;
  355. u32 dmac_int, dmac_sts;
  356. dmac_int = ioread32(bdma_chan->regs + TSI721_DMAC_INT);
  357. dev_dbg(bdma_chan->dchan.device->dev, "%s: DMAC%d_INT = 0x%x\n",
  358. __func__, bdma_chan->id, dmac_int);
  359. /* Clear channel interrupts */
  360. iowrite32(dmac_int, bdma_chan->regs + TSI721_DMAC_INT);
  361. if (dmac_int & TSI721_DMAC_INT_ERR) {
  362. dmac_sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
  363. dev_err(bdma_chan->dchan.device->dev,
  364. "%s: DMA ERROR - DMAC%d_STS = 0x%x\n",
  365. __func__, bdma_chan->id, dmac_sts);
  366. }
  367. if (dmac_int & TSI721_DMAC_INT_STFULL) {
  368. dev_err(bdma_chan->dchan.device->dev,
  369. "%s: DMAC%d descriptor status FIFO is full\n",
  370. __func__, bdma_chan->id);
  371. }
  372. if (dmac_int & (TSI721_DMAC_INT_DONE | TSI721_DMAC_INT_IOFDONE)) {
  373. tsi721_clr_stat(bdma_chan);
  374. spin_lock(&bdma_chan->lock);
  375. tsi721_advance_work(bdma_chan);
  376. spin_unlock(&bdma_chan->lock);
  377. }
  378. /* Re-Enable BDMA channel interrupts */
  379. iowrite32(TSI721_DMAC_INT_ALL, bdma_chan->regs + TSI721_DMAC_INTE);
  380. }
  381. static dma_cookie_t tsi721_tx_submit(struct dma_async_tx_descriptor *txd)
  382. {
  383. struct tsi721_tx_desc *desc = to_tsi721_desc(txd);
  384. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(txd->chan);
  385. dma_cookie_t cookie;
  386. spin_lock_bh(&bdma_chan->lock);
  387. cookie = txd->chan->cookie;
  388. if (++cookie < 0)
  389. cookie = 1;
  390. txd->chan->cookie = cookie;
  391. txd->cookie = cookie;
  392. if (list_empty(&bdma_chan->active_list)) {
  393. list_add_tail(&desc->desc_node, &bdma_chan->active_list);
  394. tsi721_start_dma(bdma_chan);
  395. } else {
  396. list_add_tail(&desc->desc_node, &bdma_chan->queue);
  397. }
  398. spin_unlock_bh(&bdma_chan->lock);
  399. return cookie;
  400. }
  401. static int tsi721_alloc_chan_resources(struct dma_chan *dchan)
  402. {
  403. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  404. #ifdef CONFIG_PCI_MSI
  405. struct tsi721_device *priv = to_tsi721(dchan->device);
  406. #endif
  407. struct tsi721_tx_desc *desc = NULL;
  408. LIST_HEAD(tmp_list);
  409. int i;
  410. int rc;
  411. if (bdma_chan->bd_base)
  412. return bdma_chan->bd_num - 1;
  413. /* Initialize BDMA channel */
  414. if (tsi721_bdma_ch_init(bdma_chan)) {
  415. dev_err(dchan->device->dev, "Unable to initialize data DMA"
  416. " channel %d, aborting\n", bdma_chan->id);
  417. return -ENOMEM;
  418. }
  419. /* Alocate matching number of logical descriptors */
  420. desc = kcalloc((bdma_chan->bd_num - 1), sizeof(struct tsi721_tx_desc),
  421. GFP_KERNEL);
  422. if (!desc) {
  423. dev_err(dchan->device->dev,
  424. "Failed to allocate logical descriptors\n");
  425. rc = -ENOMEM;
  426. goto err_out;
  427. }
  428. bdma_chan->tx_desc = desc;
  429. for (i = 0; i < bdma_chan->bd_num - 1; i++) {
  430. dma_async_tx_descriptor_init(&desc[i].txd, dchan);
  431. desc[i].txd.tx_submit = tsi721_tx_submit;
  432. desc[i].txd.flags = DMA_CTRL_ACK;
  433. INIT_LIST_HEAD(&desc[i].tx_list);
  434. list_add_tail(&desc[i].desc_node, &tmp_list);
  435. }
  436. spin_lock_bh(&bdma_chan->lock);
  437. list_splice(&tmp_list, &bdma_chan->free_list);
  438. bdma_chan->completed_cookie = dchan->cookie = 1;
  439. spin_unlock_bh(&bdma_chan->lock);
  440. #ifdef CONFIG_PCI_MSI
  441. if (priv->flags & TSI721_USING_MSIX) {
  442. /* Request interrupt service if we are in MSI-X mode */
  443. rc = request_irq(
  444. priv->msix[TSI721_VECT_DMA0_DONE +
  445. bdma_chan->id].vector,
  446. tsi721_bdma_msix, 0,
  447. priv->msix[TSI721_VECT_DMA0_DONE +
  448. bdma_chan->id].irq_name,
  449. (void *)bdma_chan);
  450. if (rc) {
  451. dev_dbg(dchan->device->dev,
  452. "Unable to allocate MSI-X interrupt for "
  453. "BDMA%d-DONE\n", bdma_chan->id);
  454. goto err_out;
  455. }
  456. rc = request_irq(priv->msix[TSI721_VECT_DMA0_INT +
  457. bdma_chan->id].vector,
  458. tsi721_bdma_msix, 0,
  459. priv->msix[TSI721_VECT_DMA0_INT +
  460. bdma_chan->id].irq_name,
  461. (void *)bdma_chan);
  462. if (rc) {
  463. dev_dbg(dchan->device->dev,
  464. "Unable to allocate MSI-X interrupt for "
  465. "BDMA%d-INT\n", bdma_chan->id);
  466. free_irq(
  467. priv->msix[TSI721_VECT_DMA0_DONE +
  468. bdma_chan->id].vector,
  469. (void *)bdma_chan);
  470. rc = -EIO;
  471. goto err_out;
  472. }
  473. }
  474. #endif /* CONFIG_PCI_MSI */
  475. tasklet_enable(&bdma_chan->tasklet);
  476. tsi721_bdma_interrupt_enable(bdma_chan, 1);
  477. return bdma_chan->bd_num - 1;
  478. err_out:
  479. kfree(desc);
  480. tsi721_bdma_ch_free(bdma_chan);
  481. return rc;
  482. }
  483. static void tsi721_free_chan_resources(struct dma_chan *dchan)
  484. {
  485. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  486. #ifdef CONFIG_PCI_MSI
  487. struct tsi721_device *priv = to_tsi721(dchan->device);
  488. #endif
  489. LIST_HEAD(list);
  490. dev_dbg(dchan->device->dev, "%s: Entry\n", __func__);
  491. if (bdma_chan->bd_base == NULL)
  492. return;
  493. BUG_ON(!list_empty(&bdma_chan->active_list));
  494. BUG_ON(!list_empty(&bdma_chan->queue));
  495. tasklet_disable(&bdma_chan->tasklet);
  496. spin_lock_bh(&bdma_chan->lock);
  497. list_splice_init(&bdma_chan->free_list, &list);
  498. spin_unlock_bh(&bdma_chan->lock);
  499. tsi721_bdma_interrupt_enable(bdma_chan, 0);
  500. #ifdef CONFIG_PCI_MSI
  501. if (priv->flags & TSI721_USING_MSIX) {
  502. free_irq(priv->msix[TSI721_VECT_DMA0_DONE +
  503. bdma_chan->id].vector, (void *)bdma_chan);
  504. free_irq(priv->msix[TSI721_VECT_DMA0_INT +
  505. bdma_chan->id].vector, (void *)bdma_chan);
  506. }
  507. #endif /* CONFIG_PCI_MSI */
  508. tsi721_bdma_ch_free(bdma_chan);
  509. kfree(bdma_chan->tx_desc);
  510. }
  511. static
  512. enum dma_status tsi721_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
  513. struct dma_tx_state *txstate)
  514. {
  515. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  516. dma_cookie_t last_used;
  517. dma_cookie_t last_completed;
  518. int ret;
  519. spin_lock_bh(&bdma_chan->lock);
  520. last_completed = bdma_chan->completed_cookie;
  521. last_used = dchan->cookie;
  522. spin_unlock_bh(&bdma_chan->lock);
  523. ret = dma_async_is_complete(cookie, last_completed, last_used);
  524. dma_set_tx_state(txstate, last_completed, last_used, 0);
  525. dev_dbg(dchan->device->dev,
  526. "%s: exit, ret: %d, last_completed: %d, last_used: %d\n",
  527. __func__, ret, last_completed, last_used);
  528. return ret;
  529. }
  530. static void tsi721_issue_pending(struct dma_chan *dchan)
  531. {
  532. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  533. dev_dbg(dchan->device->dev, "%s: Entry\n", __func__);
  534. if (tsi721_dma_is_idle(bdma_chan)) {
  535. spin_lock_bh(&bdma_chan->lock);
  536. tsi721_advance_work(bdma_chan);
  537. spin_unlock_bh(&bdma_chan->lock);
  538. } else
  539. dev_dbg(dchan->device->dev,
  540. "%s: DMA channel still busy\n", __func__);
  541. }
  542. static
  543. struct dma_async_tx_descriptor *tsi721_prep_rio_sg(struct dma_chan *dchan,
  544. struct scatterlist *sgl, unsigned int sg_len,
  545. enum dma_transfer_direction dir, unsigned long flags,
  546. void *tinfo)
  547. {
  548. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  549. struct tsi721_tx_desc *desc = NULL;
  550. struct tsi721_tx_desc *first = NULL;
  551. struct scatterlist *sg;
  552. struct rio_dma_ext *rext = tinfo;
  553. u64 rio_addr = rext->rio_addr; /* limited to 64-bit rio_addr for now */
  554. unsigned int i;
  555. u32 sys_size = dma_to_mport(dchan->device)->sys_size;
  556. enum dma_rtype rtype;
  557. if (!sgl || !sg_len) {
  558. dev_err(dchan->device->dev, "%s: No SG list\n", __func__);
  559. return NULL;
  560. }
  561. if (dir == DMA_DEV_TO_MEM)
  562. rtype = NREAD;
  563. else if (dir == DMA_MEM_TO_DEV) {
  564. switch (rext->wr_type) {
  565. case RDW_ALL_NWRITE:
  566. rtype = ALL_NWRITE;
  567. break;
  568. case RDW_ALL_NWRITE_R:
  569. rtype = ALL_NWRITE_R;
  570. break;
  571. case RDW_LAST_NWRITE_R:
  572. default:
  573. rtype = LAST_NWRITE_R;
  574. break;
  575. }
  576. } else {
  577. dev_err(dchan->device->dev,
  578. "%s: Unsupported DMA direction option\n", __func__);
  579. return NULL;
  580. }
  581. for_each_sg(sgl, sg, sg_len, i) {
  582. int err;
  583. dev_dbg(dchan->device->dev, "%s: sg #%d\n", __func__, i);
  584. desc = tsi721_desc_get(bdma_chan);
  585. if (!desc) {
  586. dev_err(dchan->device->dev,
  587. "Not enough descriptors available\n");
  588. goto err_desc_get;
  589. }
  590. if (sg_is_last(sg))
  591. desc->interrupt = (flags & DMA_PREP_INTERRUPT) != 0;
  592. else
  593. desc->interrupt = false;
  594. desc->destid = rext->destid;
  595. desc->rio_addr = rio_addr;
  596. desc->rio_addr_u = 0;
  597. err = tsi721_fill_desc(bdma_chan, desc, sg, rtype, sys_size);
  598. if (err) {
  599. dev_err(dchan->device->dev,
  600. "Failed to build desc: %d\n", err);
  601. goto err_desc_get;
  602. }
  603. rio_addr += sg_dma_len(sg);
  604. if (!first)
  605. first = desc;
  606. else
  607. list_add_tail(&desc->desc_node, &first->tx_list);
  608. }
  609. first->txd.cookie = -EBUSY;
  610. desc->txd.flags = flags;
  611. return &first->txd;
  612. err_desc_get:
  613. tsi721_desc_put(bdma_chan, first);
  614. return NULL;
  615. }
  616. static int tsi721_device_control(struct dma_chan *dchan, enum dma_ctrl_cmd cmd,
  617. unsigned long arg)
  618. {
  619. struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
  620. struct tsi721_tx_desc *desc, *_d;
  621. LIST_HEAD(list);
  622. dev_dbg(dchan->device->dev, "%s: Entry\n", __func__);
  623. if (cmd != DMA_TERMINATE_ALL)
  624. return -ENXIO;
  625. spin_lock_bh(&bdma_chan->lock);
  626. /* make sure to stop the transfer */
  627. iowrite32(TSI721_DMAC_CTL_SUSP, bdma_chan->regs + TSI721_DMAC_CTL);
  628. list_splice_init(&bdma_chan->active_list, &list);
  629. list_splice_init(&bdma_chan->queue, &list);
  630. list_for_each_entry_safe(desc, _d, &list, desc_node)
  631. tsi721_dma_chain_complete(bdma_chan, desc);
  632. spin_unlock_bh(&bdma_chan->lock);
  633. return 0;
  634. }
  635. int tsi721_register_dma(struct tsi721_device *priv)
  636. {
  637. int i;
  638. int nr_channels = TSI721_DMA_MAXCH;
  639. int err;
  640. struct rio_mport *mport = priv->mport;
  641. mport->dma.dev = &priv->pdev->dev;
  642. mport->dma.chancnt = nr_channels;
  643. INIT_LIST_HEAD(&mport->dma.channels);
  644. for (i = 0; i < nr_channels; i++) {
  645. struct tsi721_bdma_chan *bdma_chan = &priv->bdma[i];
  646. if (i == TSI721_DMACH_MAINT)
  647. continue;
  648. bdma_chan->bd_num = 64;
  649. bdma_chan->regs = priv->regs + TSI721_DMAC_BASE(i);
  650. bdma_chan->dchan.device = &mport->dma;
  651. bdma_chan->dchan.cookie = 1;
  652. bdma_chan->dchan.chan_id = i;
  653. bdma_chan->id = i;
  654. spin_lock_init(&bdma_chan->lock);
  655. INIT_LIST_HEAD(&bdma_chan->active_list);
  656. INIT_LIST_HEAD(&bdma_chan->queue);
  657. INIT_LIST_HEAD(&bdma_chan->free_list);
  658. tasklet_init(&bdma_chan->tasklet, tsi721_dma_tasklet,
  659. (unsigned long)bdma_chan);
  660. tasklet_disable(&bdma_chan->tasklet);
  661. list_add_tail(&bdma_chan->dchan.device_node,
  662. &mport->dma.channels);
  663. }
  664. dma_cap_zero(mport->dma.cap_mask);
  665. dma_cap_set(DMA_PRIVATE, mport->dma.cap_mask);
  666. dma_cap_set(DMA_SLAVE, mport->dma.cap_mask);
  667. mport->dma.device_alloc_chan_resources = tsi721_alloc_chan_resources;
  668. mport->dma.device_free_chan_resources = tsi721_free_chan_resources;
  669. mport->dma.device_tx_status = tsi721_tx_status;
  670. mport->dma.device_issue_pending = tsi721_issue_pending;
  671. mport->dma.device_prep_slave_sg = tsi721_prep_rio_sg;
  672. mport->dma.device_control = tsi721_device_control;
  673. err = dma_async_device_register(&mport->dma);
  674. if (err)
  675. dev_err(&priv->pdev->dev, "Failed to register DMA device\n");
  676. return err;
  677. }