Skip to contentSkip to conversation

hw/char: sifive_uart: Avoid infinite delay of async xmit function

PATCH Applied Florian Lugou 2 participants 3 messages Jun 5, 2025 – Jun 9, 2025 SiFive drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/char: sifive_uart: Avoid infinite delay of async xmit function

The current handler for TXFIFO writes schedules an async callback to pop characters from the queue. When software writes to TXFIFO faster than the async callback delay (100ns), the timer may be pushed back while the previous character has not be dequeued yet. This happens in particular when using -icount with small shift values. This is especially worrysome when software repetitively issues amoor.w instructions (as suggested by SiFive specification) and the FIFO is full, leading to the callback being infinitly pushed back.

This commit fixes the issue by never pushing back the timer, only updating it if it is not already active.

---
 hw/char/sifive_uart.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/char/sifive_uart.c +4 -2
index 0fc89e76d1..9bc697a67b 100644
@@ -128,8 +128,10 @@ static void sifive_uart_write_tx_fifo(SiFiveUARTState *s, const uint8_t *buf,
         s->txfifo |= SIFIVE_UART_TXFIFO_FULL;
     }
 
-    timer_mod(s->fifo_trigger_handle, current_time +
-                  TX_INTERRUPT_TRIGGER_DELAY_NS);
+    if (!timer_pending(s->fifo_trigger_handle)) {
+        timer_mod(s->fifo_trigger_handle, current_time +
+                      TX_INTERRUPT_TRIGGER_DELAY_NS);
+    }
 }
 
 static uint64_t
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Florian Lugou

The current handler for TXFIFO writes schedules an async callback to pop characters from the queue. When software writes to TXFIFO faster than the async callback delay (100ns), the timer may be pushed back while the previous character has not be dequeued yet. This happens in particular when using -icount with small shift values. This is especially worrysome when software repetitively issues amoor.w instructions (as suggested by SiFive specification) and the FIFO is full, leading to the callback being infinitly pushed back.

You would hope the guest software doesn't write to the register when the FIFO is full

Quoted 3 lines

This commit fixes the issue by never pushing back the timer, only updating it if it is not already active.

The fix looks correct though

Quoted 2 lines

Alistair

Quoted 25 lines
---
 hw/char/sifive_uart.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/char/sifive_uart.c +4 -3
index 0fc89e76d1..9bc697a67b 100644
@@ -128,8 +128,10 @@ static void sifive_uart_write_tx_fifo(SiFiveUARTState *s, const uint8_t *buf,
         s->txfifo |= SIFIVE_UART_TXFIFO_FULL;
     }
 
-    timer_mod(s->fifo_trigger_handle, current_time +
-                  TX_INTERRUPT_TRIGGER_DELAY_NS);
+    if (!timer_pending(s->fifo_trigger_handle)) {
+        timer_mod(s->fifo_trigger_handle, current_time +
+                      TX_INTERRUPT_TRIGGER_DELAY_NS);
+    }
 }
 
 static uint64_t
--

2.43.0

#2 lore raw
In reply to Florian Lugou
Quoted 10 lines · Florian Lugou

The current handler for TXFIFO writes schedules an async callback to pop characters from the queue. When software writes to TXFIFO faster than the async callback delay (100ns), the timer may be pushed back while the previous character has not be dequeued yet. This happens in particular when using -icount with small shift values. This is especially worrysome when software repetitively issues amoor.w instructions (as suggested by SiFive specification) and the FIFO is full, leading to the callback being infinitly pushed back.

Quoted 4 lines

This commit fixes the issue by never pushing back the timer, only updating it if it is not already active.

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 25 lines
---
 hw/char/sifive_uart.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/char/sifive_uart.c +4 -3
index 0fc89e76d1..9bc697a67b 100644
@@ -128,8 +128,10 @@ static void sifive_uart_write_tx_fifo(SiFiveUARTState *s, const uint8_t *buf,
         s->txfifo |= SIFIVE_UART_TXFIFO_FULL;
     }
 
-    timer_mod(s->fifo_trigger_handle, current_time +
-                  TX_INTERRUPT_TRIGGER_DELAY_NS);
+    if (!timer_pending(s->fifo_trigger_handle)) {
+        timer_mod(s->fifo_trigger_handle, current_time +
+                      TX_INTERRUPT_TRIGGER_DELAY_NS);
+    }
 }
 
 static uint64_t
--

2.43.0