Swatantra-66 opened a new pull request, #20894:
URL: https://github.com/apache/nuttx/pull/20894

   ## 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`:
   ```c
     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:
   ```c
     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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to