tty: serial: fsl_lpuart: optimize the timer based EOP logic
Subsystems (2)
PATCH v4 1/1 tty: serial: fsl_lpuart: optimize the timer based EOP logic
DMA transfer may end prematurely due to the DMA Rx timeout even during an active transfer because a constant timeout does not accurately simulate an EOP event. The timer should only complete a DMA transfer once the idle period satisfies a specified interval which is baud rate dependant. The problem has been observed with low baud rates but could occur also with high baud rates.
This patch uses a timer to simulate the hardware EOP (End Of Package) event. The idea is to make the DMA Rx timeout baud rate dependent and check the DMA residue count before copying data to the TTY buffer. If the residue count remains unchanged since the last interrupt, that indicates no new data was received. In this case, the DMA should complete as an EOP event. Otherwise, new data was received during the interval and the EOP condition is not met so restart the DMA Rx timeout
---
V4:
- improve the patch comments per Ilpo's suggestion.
- use nsecs_to_jiffies
V3:
- change the rx_watermark from 8 to 7 because the dma request is generated when the fifo level is greater than this value.
V2:
- this version is to address the review feedback from Ilpo.
- reset the last_residue when rx dma starts.
- simplified the character counting in the RX circular buffer.
- use max_t() and DIV_ROUND_UP()
drivers/tty/serial/fsl_lpuart.c | 50 ++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-)
drivers/tty/serial/fsl_lpuart.c +46 -4
@@ -238,6 +238,7 @@/* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */ #define DMA_RX_TIMEOUT (10) +#define DMA_RX_IDLE_CHARS (8)#define UART_AUTOSUSPEND_TIMEOUT 3000#define DRIVER_NAME "fsl-lpuart"
@@ -282,6 +283,7 @@ struct lpuart_port {struct scatterlist rx_sgl, tx_sgl[2 ];struct circ_buf rx_ring;int rx_dma_rng_buf_len; +int last_residue;unsigned int dma_tx_nents;wait_queue_head_t dma_wait;bool is_cs7;/* Set to true when character size is 7 */
@@ -331,7 +333,7 @@ static struct lpuart_soc_data imx8qxp_data = { .devtype = IMX8QXP_LPUART, .iotype = UPIO_MEM32, .reg_off = IMX_REG_OFF, - .rx_watermark = 31, + .rx_watermark = 7, /* A lower watermark is ideal for low baud rates. */ };static struct lpuart_soc_data imxrt1050_data = { .devtype =IMXRT1050_LPUART ,
@@ -1255,6 +1257,8 @@ static void lpuart_copy_rx_to_tty(struct lpuart_port *sport) sport->port.icount.rx += copied; } + sport->last_residue = state.residue; + exit:dma_sync_sg_for_device (chan->device->dev, &sport->rx_sgl,1 ,DMA_FROM_DEVICE );
@@ -1272,11 +1276,37 @@ static void lpuart_dma_rx_complete(void *arg)lpuart_copy_rx_to_tty (sport); } +/* +* Timer function to simulate the hardware EOP (End Of Package) event. +* The timer callback is to check for new RX data and copy to TTY buffer. +* If no new data are received since last interval, the EOP condition is +* met, complete the DMA transfer by copying the data. Otherwise, just +* restart timer. +*/ static void lpuart_timer_func (struct timer_list *t) {struct lpuart_port *sport =from_timer (sport, t, lpuart_timer); +struct dma_chan *chan = sport->dma_rx_chan; +struct circ_buf *ring = &sport->rx_ring; +struct dma_tx_state state; +unsigned long flags; +int count; -lpuart_copy_rx_to_tty (sport); +dmaengine_tx_status (chan, sport->dma_rx_cookie, &state); + ring->head = sport->rx_sgl.length - state.residue; + count =CIRC_CNT (ring->head, ring->tail, sport->rx_sgl.length); + +/* Check if new data received before copying */ +if ((count !=0 ) && (sport->last_residue == state.residue)) +lpuart_copy_rx_to_tty (sport); +else +mod_timer (&sport->lpuart_timer, + jiffies + sport->dma_rx_timeout); + +if (spin_trylock_irqsave (&sport->port.lock, flags)) { + sport->last_residue = state.residue; +spin_unlock_irqrestore (&sport->port.lock, flags); + } }static inline int lpuart_start_rx_dma (struct lpuart_port *sport)
@@ -1297,9 +1327,20 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)*/ sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud / bits /1000 ) *2 ; sport->rx_dma_rng_buf_len = (1 <<fls (sport->rx_dma_rng_buf_len)); + sport->rx_dma_rng_buf_len =max_t (int, + sport->rxfifo_size *2 , + sport->rx_dma_rng_buf_len); +/* +* Keep this condition check in case rxfifo_size is unavailable +* for some SoCs. +*/ if (sport->rx_dma_rng_buf_len <16 ) sport->rx_dma_rng_buf_len =16 ; + sport->last_residue =0 ; + sport->dma_rx_timeout =max (nsecs_to_jiffies ( + sport->port.frame_time *DMA_RX_IDLE_CHARS ),1UL ); + ring->buf =kzalloc (sport->rx_dma_rng_buf_len,GFP_ATOMIC );if (!ring->buf)return -ENOMEM ;
@@ -1687,12 +1728,13 @@ static void lpuart_rx_dma_startup(struct lpuart_port *sport)if (!sport->dma_rx_chan) goto err; +/* set default Rx DMA timeout */ + sport->dma_rx_timeout =msecs_to_jiffies (DMA_RX_TIMEOUT ); + ret =lpuart_start_rx_dma (sport);if (ret) goto err; -/* set Rx DMA timeout */ - sport->dma_rx_timeout =msecs_to_jiffies (DMA_RX_TIMEOUT );if (!sport->dma_rx_timeout) sport->dma_rx_timeout =1 ;
--
2.34.1
Quoted 8 lines · Shenwei Wang
DMA transfer may end prematurely due to the DMA Rx timeout even during an active transfer because a constant timeout does not accurately simulate an EOP event. The timer should only complete a DMA transfer once the idle period satisfies a specified interval which is baud rate dependant. The problem has been observed with low baud rates but could occur also with high baud rates.
This patch uses a timer to simulate the hardware EOP (End Of Package) event.
You could open EOP at the previous paragraph where it appears first.
Quoted 1 line
The idea is to make the DMA Rx timeout baud rate dependent and check the DMA
Just remove this "the idea is to" and just state what was done by starting with "Make" like I initially said.
Quoted 7 lines
residue count before copying data to the TTY buffer. If the residue count remains unchanged since the last interrupt, that indicates no new data was received. In this case, the DMA should complete as an EOP event. Otherwise, new data was received during the interval and the EOP condition is not met so restart the DMA Rx timeout
Signed-off-by Shenwei Wang <[email protected]>
However, please check a few additional comments below.
Quoted 23 lines
---
V4:
- improve the patch comments per Ilpo's suggestion.
- usensecs_to_jiffiesV3:
- change therx_watermarkfrom 8 to 7 because the dma request is generated when the fifo level is greater than this value.V2:
- this version is to address the review feedback from Ilpo.
- reset thelast_residuewhen rx dma starts.
- simplified the character counting in the RX circular buffer.
- usemax_t()andDIV_ROUND_UP()drivers/tty/serial/fsl_lpuart.c | 50 ++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-)drivers/tty/serial/fsl_lpuart.c +0
-0
drivers/tty/serial/fsl_lpuart.c line 241
Quoted 4 lines
drivers/tty/serial/fsl_lpuart.c +1
-0@@ -238,6 +238,7 @@/* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */ #define DMA_RX_TIMEOUT (10) +#define DMA_RX_IDLE_CHARS (8)
Extra parenthesis
Quoted 12 lines
drivers/tty/serial/fsl_lpuart.c +1
-0#define UART_AUTOSUSPEND_TIMEOUT 3000#define DRIVER_NAME "fsl-lpuart"@@ -282,6 +283,7 @@ struct lpuart_port {struct scatterlist rx_sgl, tx_sgl[2 ];struct circ_buf rx_ring;int rx_dma_rng_buf_len; +int last_residue;unsigned int dma_tx_nents;wait_queue_head_t dma_wait;bool is_cs7;/* Set to true when character size is 7 */
drivers/tty/serial/fsl_lpuart.c static struct lpuart_soc_data imx8qxp_data = { · line 336
Quoted 5 lines
drivers/tty/serial/fsl_lpuart.c +1
-1@@ -331,7 +333,7 @@ static struct lpuart_soc_data imx8qxp_data = { .devtype = IMX8QXP_LPUART, .iotype = UPIO_MEM32, .reg_off = IMX_REG_OFF, - .rx_watermark = 31, + .rx_watermark = 7, /* A lower watermark is ideal for low baud rates. */
It would be better to have this change in own patch. It might be something that causes discussion/problems later on so it would be better to have it as a separate patch. But I'm not sure how tightly it's related to DMA_RX_IDLE_CHARS which may make taking it out harder.
Quoted 33 lines
drivers/tty/serial/fsl_lpuart.c +14
-1};static struct lpuart_soc_data imxrt1050_data = { .devtype =IMXRT1050_LPUART ,@@ -1255,6 +1257,8 @@ static void lpuart_copy_rx_to_tty(struct lpuart_port *sport) sport->port.icount.rx += copied; } + sport->last_residue = state.residue; + exit:dma_sync_sg_for_device (chan->device->dev, &sport->rx_sgl,1 ,DMA_FROM_DEVICE );@@ -1272,11 +1276,37 @@ static void lpuart_dma_rx_complete(void *arg)lpuart_copy_rx_to_tty (sport); } +/* +* Timer function to simulate the hardware EOP (End Of Package) event. +* The timer callback is to check for new RX data and copy to TTY buffer. +* If no new data are received since last interval, the EOP condition is +* met, complete the DMA transfer by copying the data. Otherwise, just +* restart timer. +*/ static void lpuart_timer_func (struct timer_list *t) {struct lpuart_port *sport =from_timer (sport, t, lpuart_timer); +struct dma_chan *chan = sport->dma_rx_chan; +struct circ_buf *ring = &sport->rx_ring; +struct dma_tx_state state; +unsigned long flags; +int count; -lpuart_copy_rx_to_tty (sport);
drivers/tty/serial/fsl_lpuart.c static void lpuart_dma_rx_complete(void *arg) · line 1304
Quoted 10 lines
drivers/tty/serial/fsl_lpuart.c +10
-0+dmaengine_tx_status (chan, sport->dma_rx_cookie, &state); + ring->head = sport->rx_sgl.length - state.residue; + count =CIRC_CNT (ring->head, ring->tail, sport->rx_sgl.length); + +/* Check if new data received before copying */ +if ((count !=0 ) && (sport->last_residue == state.residue)) +lpuart_copy_rx_to_tty (sport); +else +mod_timer (&sport->lpuart_timer, + jiffies + sport->dma_rx_timeout);
Alignment doesn't match the previous line (start parameters from the same column).
Quoted 9 lines
drivers/tty/serial/fsl_lpuart.c +5
-0+ +if (spin_trylock_irqsave (&sport->port.lock, flags)) { + sport->last_residue = state.residue; +spin_unlock_irqrestore (&sport->port.lock, flags); + } }static inline int lpuart_start_rx_dma (struct lpuart_port *sport)
drivers/tty/serial/fsl_lpuart.c static inline int lpuart_start_rx_dma(struct lpuart_port *sport) · line 1332
Quoted 6 lines
drivers/tty/serial/fsl_lpuart.c +3
-0@@ -1297,9 +1327,20 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)*/ sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud / bits /1000 ) *2 ; sport->rx_dma_rng_buf_len = (1 <<fls (sport->rx_dma_rng_buf_len)); + sport->rx_dma_rng_buf_len =max_t (int, + sport->rxfifo_size *2 , + sport->rx_dma_rng_buf_len);
Alignment doesn't match the prev line.
Quoted 33 lines
drivers/tty/serial/fsl_lpuart.c +11
-3+/* +* Keep this condition check in case rxfifo_size is unavailable +* for some SoCs. +*/ if (sport->rx_dma_rng_buf_len <16 ) sport->rx_dma_rng_buf_len =16 ; + sport->last_residue =0 ; + sport->dma_rx_timeout =max (nsecs_to_jiffies ( + sport->port.frame_time *DMA_RX_IDLE_CHARS ),1UL ); + ring->buf =kzalloc (sport->rx_dma_rng_buf_len,GFP_ATOMIC );if (!ring->buf)return -ENOMEM ;@@ -1687,12 +1728,13 @@ static void lpuart_rx_dma_startup(struct lpuart_port *sport)if (!sport->dma_rx_chan) goto err; +/* set default Rx DMA timeout */ + sport->dma_rx_timeout =msecs_to_jiffies (DMA_RX_TIMEOUT ); + ret =lpuart_start_rx_dma (sport);if (ret) goto err; -/* set Rx DMA timeout */ - sport->dma_rx_timeout =msecs_to_jiffies (DMA_RX_TIMEOUT );if (!sport->dma_rx_timeout) sport->dma_rx_timeout =1 ; --2.34.1
Signature · 1 line
i.
Quoted 26 lines
-----Original Message-----
From: Ilpo Järvinen <[email protected]>
Sent: Wednesday, May 10, 2023 5:34 AM
To: Shenwei Wang <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>; Jiri Slaby <[email protected]>; linux-serial <[email protected]>; [email protected]; dl-linux-imx <[email protected]>
Subject: [EXT] Re: [PATCH v4 1/1] tty: serial:fsl_lpuart: optimize the timer based EOP logicCaution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button
Quoted 7 lines · Shenwei Wang
DMA transfer may end prematurely due to the DMA Rx timeout even during an active transfer because a constant timeout does not accurately simulate an EOP event. The timer should only complete a DMA transfer once the idle period satisfies a specified interval which is baud rate dependant. The problem has been observed with low baud rates but could occur also with high baud rates.
Quoted 4 lines
Quoted 1 line
This patch uses a timer to simulate the hardware EOP (End Of Package) event.
You could open EOP at the previous paragraph where it appears first.
That sounds more suitable.
Quoted 6 lines
Quoted 2 lines
The idea is to make the DMA Rx timeout baud rate dependent and check the DMA
Just remove this "the idea is to" and just state what was done by starting with "Make" like I initially said.
Agree.
Quoted 19 lines
Quoted 7 lines
residue count before copying data to the TTY buffer. If the residue count remains unchanged since the last interrupt, that indicates no new data was received. In this case, the DMA should complete as an EOP event. Otherwise, new data was received during the interval and the EOP condition is not met so restart the DMA Rx timeout
Signed-off-by Shenwei Wang <[email protected]>Reviewed-by Ilpo Järvinen <[email protected]>However, please check a few additional comments below.
Review
Quoted 6 lines
---
V4:
- improve the patch comments per Ilpo's suggestion.(quoted diff) +2
-1- .rx_watermark = 31, + .rx_watermark = 7, /* A lower watermark is ideal for low baud + rates. */
Quoted 5 lines
It would be better to have this change in own patch. It might be something that causes discussion/problems later on so it would be better to have it as a separate patch. But I'm not sure how tightly it's related to
DMA_RX_IDLE_CHARSwhich may make taking it out harder.
You are right. The rx_watermark here should no more than the DMA_RX_IDLE_CHARS.
Thanks,
Shenwei
Quoted 9 lines
Review static void lpuart_copy_rx_to_tty(struct lpuart_port
Quoted 4 lines
};
static struct lpuart_soc_data imxrt1050_data = { .devtype = IMXRT1050_LPUART,(quoted diff) +0
-0@@ -1255,6 +1257,8 @@ static void lpuart_copy_rx_to_tty(struct lpuart_port*sport)
Quoted 4 lines
drivers/tty/serial/fsl_lpuart.c +1
-0sport->port.icount.rx += copied; } + sport->last_residue = state.residue;