Skip to content

drivers/serial: Fix zero-length second TX DMA transfer descriptor on buffer wrap - #20894

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
Swatantra-66:fix/serial-dma-zero-length-wrap
Oct 26, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
Swatantra-66:fix/serial-dma-zero-length-wrap

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Fixes a critical bug where serial TX DMA sets up a zero-length second transfer descriptor on buffer wrap, triggering illegal hardware configuration errors on eDMA engines.

Root Cause

In drivers/serial/serial_dma.c (uart_xmitchars_dma()), when the transmit circular buffer wraps with head == 0 and tail > 0:

  else
    {
      xfer->buffer  = &dev->xmit.buffer[dev->xmit.tail];
      xfer->length  = dev->xmit.size - dev->xmit.tail;
      xfer->nbuffer = dev->xmit.buffer;
      xfer->nlength = dev->xmit.head;
    }

xfer->nbuffer was assigned dev->xmit.buffer even though xfer->nlength == 0.

Across chip drivers (imxrt_serial.c, nxxx_lpuart.c, imx9_lpuart.c, s32k1xx_serial.c, s32k3xx_serial.c), the code checks:

  if (dev->dmatx.nbuffer)

Because nbuffer != NULL, the driver configures a second DMA descriptor with iter = nlength = 0 (or CITER = BITER = 0). On NXP eDMA controllers (i.MX RT, S32K, MCX-N, i.MX9), CITER = 0 is an illegal descriptor configuration. The eDMA hardware detects this, asserts ES[NCE] (Non-zero Configuration Error), and sets CR[HALT] = 1, which halts the entire eDMA engine chip-wide and causes SPI, I2C, and UART peripherals to lock up.

Fix

  1. drivers/serial/serial_dma.c:
    • In uart_xmitchars_dma(): Only set xfer->nbuffer and xfer->nlength if dev->xmit.head > 0. If head == 0, explicitly set xfer->nbuffer = NULL; xfer->nlength = 0;.
    • In uart_xmitchars_done(): Clean up xfer->buffer = xfer->nbuffer = NULL; on completion.
  2. arch/arm and arch/arm64:
    • Defend against zero-length descriptor setup in imxrt, mcx-nxxx, imx9, s32k1xx, and s32k3xx serial drivers by checking if (dev->dmatx.nbuffer != NULL && dev->dmatx.nlength > 0).

Fixes: #20881

Impact

  • Prevents eDMA ES[NCE] configuration errors and chip-wide DMA halt on i.MX RT, S32K, MCX, and i.MX9 during serial TX DMA operations.
  • Eliminates zero-length DMA descriptor submissions across all architectures using CONFIG_SERIAL_TXDMA.

Testing

  • Code style verified with tools/checkpatch.sh -u: all checks pass.

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Oct 22, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@github-actions github-actions Bot added Size: L The size of the change in this PR is large and removed Size: S The size of the change in this PR is small labels Oct 23, 2026
Comment thread arch/arm/src/mcx-nxxx/nxxx_lpuart.c Resolved
@Swatantra-66
Swatantra-66 force-pushed the fix/serial-dma-zero-length-wrap branch from 15ef5ba to bd227c3 Compare October 23, 2026 06:43
@Swatantra-66
Swatantra-66 deleted the fix/serial-dma-zero-length-wrap branch October 24, 2026 10:40
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@Swatantra-66 why close this pr?

@Swatantra-66
Swatantra-66 restored the fix/serial-dma-zero-length-wrap branch October 24, 2026 12:03
@Swatantra-66 Swatantra-66 reopened this Oct 24, 2026
@Swatantra-66

Swatantra-66 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Reopened, I thought it might be redundant after #20893 merged and the issue was closed.

acassis
acassis previously approved these changes Oct 24, 2026
@acassis

acassis commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

@Swatantra-66 please check if it is redundant and if not, please fix the conflicting files

@Swatantra-66
Swatantra-66 dismissed stale reviews from acassis and xiaoxiang781216 via cc99993 October 24, 2026 13:34
@Swatantra-66
Swatantra-66 force-pushed the fix/serial-dma-zero-length-wrap branch from bd227c3 to cc99993 Compare October 24, 2026 13:34
@github-actions github-actions Bot added Size: XS The size of the change in this PR is very small and removed Area: Drivers Drivers issues Size: L The size of the change in this PR is large labels Oct 24, 2026
Check that both dev->dmatx.nbuffer != NULL and dev->dmatx.nlength > 0
before configuring the second transfer descriptor. This defends against
configuring an illegal descriptor with iter = 0 (CITER = 0 on eDMA) in
case a zero-length second segment is passed.

Also fix nxstyle indentation of switch case labels in imx9_ioctl.

Signed-off-by: Swatantra Yadav <[email protected]>
Check that both dev->dmatx.nbuffer != NULL and dev->dmatx.nlength > 0
before configuring the second transfer descriptor.

Also fix nxstyle indentation of switch case labels in imx9_ioctl.

Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/serial-dma-zero-length-wrap branch from cc99993 to 3c9c010 Compare October 24, 2026 14:15
@github-actions github-actions Bot added Size: L The size of the change in this PR is large and removed Size: XS The size of the change in this PR is very small labels Oct 24, 2026
@Swatantra-66

Swatantra-66 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@acassis , conflicts resolved

@Swatantra-66

Copy link
Copy Markdown
Contributor Author

The failure in Build / Linux (arm-nxp) is an upstream issue caused by the recent pollevent_t change merged into nuttx-apps master (waiting on #20796 in nuttx). It is unrelated to this PR's imx9_lpuart changes.

@xiaoxiang781216

xiaoxiang781216 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

let's ignore ci error instead wasting ci resource by retrigger.

@xiaoxiang781216
xiaoxiang781216 merged commit 4c346d2 into apache:master Oct 26, 2026
34 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] imxrt: LPUART TX DMA links a TCD with CITER = 0 on buffer wrap, causing eDMA NCE errors

4 participants