[PATCH v1 16/31] serial: 8250_mxpcie: optimize Tx using memory-mapped FIFO access

Crescent Hsieh posted 31 patches 1 day, 11 hours ago
[PATCH v1 16/31] serial: 8250_mxpcie: optimize Tx using memory-mapped FIFO access
Posted by Crescent Hsieh 1 day, 11 hours ago
Add mxpcie8250_tx_chars() to transmit data via memory-mapped FIFO
access, using the UART-specific Tx FIFO counter and buffer region.

This replaces serial8250_tx_chars() to reduce per-byte I/O operations
and improve transmit throughput on Moxa PCIe UART devices.

Signed-off-by: Crescent Hsieh <crescentcy.hsieh@moxa.com>
---
 drivers/tty/serial/8250/8250_mxpcie.c | 35 ++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/8250/8250_mxpcie.c b/drivers/tty/serial/8250/8250_mxpcie.c
index cf519960d5d1..90ce522fe40f 100644
--- a/drivers/tty/serial/8250/8250_mxpcie.c
+++ b/drivers/tty/serial/8250/8250_mxpcie.c
@@ -73,8 +73,10 @@
 #define MOXA_PUART_FCL		0x12	/* Flow Control Low Trigger Level */
 #define MOXA_PUART_FCH		0x13	/* Flow Control High Trigger Level */
 #define MOXA_PUART_RX_FIFO_CNT	0x15	/* Rx FIFO Data Counter */
+#define MOXA_PUART_TX_FIFO_CNT	0x16	/* Tx FIFO Data Counter */
 
 #define MOXA_PUART_RX_FIFO_MEM	0x100	/* Memory Space to Rx FIFO Data Register */
+#define MOXA_PUART_TX_FIFO_MEM	0x100	/* Memory Space to Tx FIFO Data Register */
 
 #define MOXA_GPIO_DIRECTION	0x09
 #define MOXA_GPIO_OUTPUT	0x0A
@@ -282,6 +284,37 @@ static void mxpcie8250_rx_chars(struct uart_8250_port *up)
 	}
 }
 
+static void mxpcie8250_tx_chars(struct uart_8250_port *up)
+{
+	struct uart_port *port = &up->port;
+	struct tty_port *tport = &port->state->port;
+	unsigned int count, i;
+	unsigned char c;
+
+	if (port->x_char) {
+		uart_xchar_out(port, UART_TX);
+		return;
+	}
+	if (uart_tx_stopped(port) || kfifo_is_empty(&tport->xmit_fifo)) {
+		port->ops->stop_tx(port);
+		return;
+	}
+	count = kfifo_len(&tport->xmit_fifo);
+	count = min(count, port->fifosize - serial_in(up, MOXA_PUART_TX_FIFO_CNT));
+
+	for (i = 0; i < count; ++i) {
+		if (!uart_fifo_get(port, &c))
+			break;
+
+		*(port->membase + MOXA_PUART_TX_FIFO_MEM + i) = c;
+	}
+	if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
+		uart_write_wakeup(port);
+
+	if (kfifo_is_empty(&tport->xmit_fifo) && !(up->capabilities & UART_CAP_RPM))
+		port->ops->stop_tx(port);
+}
+
 static int mxpcie8250_handle_irq(struct uart_port *port)
 {
 	struct uart_8250_port *up = up_to_u8250p(port);
@@ -314,7 +347,7 @@ static int mxpcie8250_handle_irq(struct uart_port *port)
 	serial8250_modem_status(up);
 
 	if ((lsr & UART_LSR_THRE) && (up->ier & UART_IER_THRI))
-		serial8250_tx_chars(up);
+		mxpcie8250_tx_chars(up);
 
 	uart_unlock_and_check_sysrq_irqrestore(port, flags);
 
-- 
2.45.2
Re: [PATCH v1 16/31] serial: 8250_mxpcie: optimize Tx using memory-mapped FIFO access
Posted by Andy Shevchenko 20 hours ago
On Sun, Nov 30, 2025 at 12:44 PM Crescent Hsieh
<crescentcy.hsieh@moxa.com> wrote:
>
> Add mxpcie8250_tx_chars() to transmit data via memory-mapped FIFO
> access, using the UART-specific Tx FIFO counter and buffer region.
>
> This replaces serial8250_tx_chars() to reduce per-byte I/O operations
> and improve transmit throughput on Moxa PCIe UART devices.

...

> +static void mxpcie8250_tx_chars(struct uart_8250_port *up)
> +{
> +       struct uart_port *port = &up->port;
> +       struct tty_port *tport = &port->state->port;
> +       unsigned int count, i;
> +       unsigned char c;
> +
> +       if (port->x_char) {
> +               uart_xchar_out(port, UART_TX);
> +               return;
> +       }
> +       if (uart_tx_stopped(port) || kfifo_is_empty(&tport->xmit_fifo)) {
> +               port->ops->stop_tx(port);
> +               return;
> +       }

> +       count = kfifo_len(&tport->xmit_fifo);
> +       count = min(count, port->fifosize - serial_in(up, MOXA_PUART_TX_FIFO_CNT));

Perhaps

  unsigned int i, count, txsize;
  ...
  txsize = serial_in(up, MOXA_PUART_TX_FIFO_CNT);
  count = min(kfifo_len(...), fifosize - txsize);

?

> +       for (i = 0; i < count; ++i) {
> +               if (!uart_fifo_get(port, &c))
> +                       break;

> +               *(port->membase + MOXA_PUART_TX_FIFO_MEM + i) = c;

This is interesting... Why can't the proper IO accessors be used?

> +       }
> +       if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
> +               uart_write_wakeup(port);
> +
> +       if (kfifo_is_empty(&tport->xmit_fifo) && !(up->capabilities & UART_CAP_RPM))
> +               port->ops->stop_tx(port);
> +}

-- 
With Best Regards,
Andy Shevchenko