8250_dma.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * 8250_dma.c - DMA Engine API support for 8250.c
  4. *
  5. * Copyright (C) 2013 Intel Corporation
  6. */
  7. #include <linux/tty.h>
  8. #include <linux/tty_flip.h>
  9. #include <linux/serial_reg.h>
  10. #include <linux/dma-mapping.h>
  11. #include "8250.h"
  12. static void __dma_tx_complete(void *param)
  13. {
  14. struct uart_8250_port *p = param;
  15. struct uart_8250_dma *dma = p->dma;
  16. struct tty_port *tport = &p->port.state->port;
  17. unsigned long flags;
  18. int ret;
  19. dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
  20. UART_XMIT_SIZE, DMA_TO_DEVICE);
  21. uart_port_lock_irqsave(&p->port, &flags);
  22. dma->tx_running = 0;
  23. uart_xmit_advance(&p->port, dma->tx_size);
  24. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  25. uart_write_wakeup(&p->port);
  26. ret = serial8250_tx_dma(p);
  27. if (ret || !dma->tx_running)
  28. serial8250_set_THRI(p);
  29. uart_port_unlock_irqrestore(&p->port, flags);
  30. }
  31. static void __dma_rx_complete(struct uart_8250_port *p)
  32. {
  33. struct uart_8250_dma *dma = p->dma;
  34. struct tty_port *tty_port = &p->port.state->port;
  35. struct dma_tx_state state;
  36. enum dma_status dma_status;
  37. int count;
  38. /*
  39. * New DMA Rx can be started during the completion handler before it
  40. * could acquire port's lock and it might still be ongoing. Don't to
  41. * anything in such case.
  42. */
  43. dma_status = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
  44. if (dma_status == DMA_IN_PROGRESS)
  45. return;
  46. count = dma->rx_size - state.residue;
  47. tty_insert_flip_string(tty_port, dma->rx_buf, count);
  48. p->port.icount.rx += count;
  49. dma->rx_running = 0;
  50. tty_flip_buffer_push(tty_port);
  51. }
  52. static void dma_rx_complete(void *param)
  53. {
  54. struct uart_8250_port *p = param;
  55. struct uart_8250_dma *dma = p->dma;
  56. unsigned long flags;
  57. uart_port_lock_irqsave(&p->port, &flags);
  58. if (dma->rx_running)
  59. __dma_rx_complete(p);
  60. /*
  61. * Cannot be combined with the previous check because __dma_rx_complete()
  62. * changes dma->rx_running.
  63. */
  64. if (!dma->rx_running && (serial_lsr_in(p) & UART_LSR_DR))
  65. p->dma->rx_dma(p);
  66. uart_port_unlock_irqrestore(&p->port, flags);
  67. }
  68. int serial8250_tx_dma(struct uart_8250_port *p)
  69. {
  70. struct uart_8250_dma *dma = p->dma;
  71. struct tty_port *tport = &p->port.state->port;
  72. struct dma_async_tx_descriptor *desc;
  73. struct uart_port *up = &p->port;
  74. struct scatterlist *sg;
  75. struct scatterlist sgl[2];
  76. int i;
  77. int ret;
  78. if (dma->tx_running) {
  79. if (up->x_char) {
  80. dmaengine_pause(dma->txchan);
  81. uart_xchar_out(up, UART_TX);
  82. dmaengine_resume(dma->txchan);
  83. }
  84. return 0;
  85. } else if (up->x_char) {
  86. uart_xchar_out(up, UART_TX);
  87. }
  88. if (uart_tx_stopped(&p->port) || kfifo_is_empty(&tport->xmit_fifo)) {
  89. /* We have been called from __dma_tx_complete() */
  90. return 0;
  91. }
  92. serial8250_do_prepare_tx_dma(p);
  93. sg_init_table(sgl, ARRAY_SIZE(sgl));
  94. ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, sgl, ARRAY_SIZE(sgl),
  95. UART_XMIT_SIZE, dma->tx_addr);
  96. dma->tx_size = 0;
  97. for_each_sg(sgl, sg, ret, i)
  98. dma->tx_size += sg_dma_len(sg);
  99. desc = dmaengine_prep_slave_sg(dma->txchan, sgl, ret,
  100. DMA_MEM_TO_DEV,
  101. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  102. if (!desc) {
  103. ret = -EBUSY;
  104. goto err;
  105. }
  106. dma->tx_running = 1;
  107. desc->callback = __dma_tx_complete;
  108. desc->callback_param = p;
  109. dma->tx_cookie = dmaengine_submit(desc);
  110. dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
  111. UART_XMIT_SIZE, DMA_TO_DEVICE);
  112. dma_async_issue_pending(dma->txchan);
  113. serial8250_clear_THRI(p);
  114. dma->tx_err = 0;
  115. return 0;
  116. err:
  117. dma->tx_err = 1;
  118. return ret;
  119. }
  120. void serial8250_tx_dma_flush(struct uart_8250_port *p)
  121. {
  122. struct uart_8250_dma *dma = p->dma;
  123. if (!dma->tx_running)
  124. return;
  125. /*
  126. * kfifo_reset() has been called by the serial core, avoid
  127. * advancing and underflowing in __dma_tx_complete().
  128. */
  129. dma->tx_size = 0;
  130. /*
  131. * We can't use `dmaengine_terminate_sync` because `uart_flush_buffer` is
  132. * holding the uart port spinlock.
  133. */
  134. dmaengine_terminate_async(dma->txchan);
  135. /*
  136. * The callback might or might not run. If it doesn't run, we need to ensure
  137. * that `tx_running` is cleared so that we can schedule new transactions.
  138. * If it does run, then the zombie callback will clear `tx_running` again
  139. * and perform a no-op since `tx_size` was cleared above.
  140. *
  141. * In either case, we ASSUME the DMA transaction will terminate before we
  142. * issue a new `serial8250_tx_dma`.
  143. */
  144. dma->tx_running = 0;
  145. }
  146. int serial8250_rx_dma(struct uart_8250_port *p)
  147. {
  148. struct uart_8250_dma *dma = p->dma;
  149. struct dma_async_tx_descriptor *desc;
  150. if (dma->rx_running)
  151. return 0;
  152. serial8250_do_prepare_rx_dma(p);
  153. desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
  154. dma->rx_size, DMA_DEV_TO_MEM,
  155. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  156. if (!desc)
  157. return -EBUSY;
  158. dma->rx_running = 1;
  159. desc->callback = dma_rx_complete;
  160. desc->callback_param = p;
  161. dma->rx_cookie = dmaengine_submit(desc);
  162. dma_async_issue_pending(dma->rxchan);
  163. return 0;
  164. }
  165. void serial8250_rx_dma_flush(struct uart_8250_port *p)
  166. {
  167. struct uart_8250_dma *dma = p->dma;
  168. if (dma->rx_running) {
  169. dmaengine_pause(dma->rxchan);
  170. __dma_rx_complete(p);
  171. dmaengine_terminate_async(dma->rxchan);
  172. }
  173. }
  174. EXPORT_SYMBOL_GPL(serial8250_rx_dma_flush);
  175. int serial8250_request_dma(struct uart_8250_port *p)
  176. {
  177. struct uart_8250_dma *dma = p->dma;
  178. phys_addr_t rx_dma_addr = dma->rx_dma_addr ?
  179. dma->rx_dma_addr : p->port.mapbase;
  180. phys_addr_t tx_dma_addr = dma->tx_dma_addr ?
  181. dma->tx_dma_addr : p->port.mapbase;
  182. dma_cap_mask_t mask;
  183. struct dma_slave_caps caps;
  184. int ret;
  185. /* Default slave configuration parameters */
  186. dma->rxconf.direction = DMA_DEV_TO_MEM;
  187. dma->rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  188. dma->rxconf.src_addr = rx_dma_addr + UART_RX;
  189. dma->txconf.direction = DMA_MEM_TO_DEV;
  190. dma->txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  191. dma->txconf.dst_addr = tx_dma_addr + UART_TX;
  192. dma_cap_zero(mask);
  193. dma_cap_set(DMA_SLAVE, mask);
  194. /* Get a channel for RX */
  195. dma->rxchan = dma_request_slave_channel_compat(mask,
  196. dma->fn, dma->rx_param,
  197. p->port.dev, "rx");
  198. if (!dma->rxchan)
  199. return -ENODEV;
  200. /* 8250 rx dma requires dmaengine driver to support pause/terminate */
  201. ret = dma_get_slave_caps(dma->rxchan, &caps);
  202. if (ret)
  203. goto release_rx;
  204. if (!caps.cmd_pause || !caps.cmd_terminate ||
  205. caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
  206. ret = -EINVAL;
  207. goto release_rx;
  208. }
  209. dmaengine_slave_config(dma->rxchan, &dma->rxconf);
  210. /* Get a channel for TX */
  211. dma->txchan = dma_request_slave_channel_compat(mask,
  212. dma->fn, dma->tx_param,
  213. p->port.dev, "tx");
  214. if (!dma->txchan) {
  215. ret = -ENODEV;
  216. goto release_rx;
  217. }
  218. /* 8250 tx dma requires dmaengine driver to support terminate */
  219. ret = dma_get_slave_caps(dma->txchan, &caps);
  220. if (ret)
  221. goto err;
  222. if (!caps.cmd_terminate) {
  223. ret = -EINVAL;
  224. goto err;
  225. }
  226. dmaengine_slave_config(dma->txchan, &dma->txconf);
  227. /* RX buffer */
  228. if (!dma->rx_size)
  229. dma->rx_size = PAGE_SIZE;
  230. dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size,
  231. &dma->rx_addr, GFP_KERNEL);
  232. if (!dma->rx_buf) {
  233. ret = -ENOMEM;
  234. goto err;
  235. }
  236. /* TX buffer */
  237. dma->tx_addr = dma_map_single(dma->txchan->device->dev,
  238. p->port.state->port.xmit_buf,
  239. UART_XMIT_SIZE,
  240. DMA_TO_DEVICE);
  241. if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) {
  242. dma_free_coherent(dma->rxchan->device->dev, dma->rx_size,
  243. dma->rx_buf, dma->rx_addr);
  244. ret = -ENOMEM;
  245. goto err;
  246. }
  247. dev_dbg_ratelimited(p->port.dev, "got both dma channels\n");
  248. return 0;
  249. err:
  250. dma_release_channel(dma->txchan);
  251. release_rx:
  252. dma_release_channel(dma->rxchan);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL_GPL(serial8250_request_dma);
  256. void serial8250_release_dma(struct uart_8250_port *p)
  257. {
  258. struct uart_8250_dma *dma = p->dma;
  259. if (!dma)
  260. return;
  261. /* Release RX resources */
  262. dmaengine_terminate_sync(dma->rxchan);
  263. dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf,
  264. dma->rx_addr);
  265. dma_release_channel(dma->rxchan);
  266. dma->rxchan = NULL;
  267. /* Release TX resources */
  268. dmaengine_terminate_sync(dma->txchan);
  269. dma_unmap_single(dma->txchan->device->dev, dma->tx_addr,
  270. UART_XMIT_SIZE, DMA_TO_DEVICE);
  271. dma_release_channel(dma->txchan);
  272. dma->txchan = NULL;
  273. dma->tx_running = 0;
  274. dev_dbg_ratelimited(p->port.dev, "dma channels released\n");
  275. }
  276. EXPORT_SYMBOL_GPL(serial8250_release_dma);