Fix SiFive UART spurious IRQ issue and misc updates
cover Fix SiFive UART spurious IRQ issue and misc updates
From: Frank Chang <[email protected]>
This patch series fixes the spurious IRQ issue in the SiFive UART on Linux introduced after commit [1], which changed character transmission from synchronous to asynchronous.
Currently, the txwm interrupt pending status is only updated when the asynchronous transmit handler runs. This can cause the txwm interrupt state to become unsynchronized between the SiFive UART and the interrupt controller.
This issue has been observed on resource-constrained systems, where Linux reports spurious IRQ errors. In these cases, the asynchronous transmit handler is unable to drain the TX FIFO quickly enough to update the txwm pending status before software reads the IP register, which derives the txwm pending state directly from the actual number of characters in the TX FIFO.
The remaining patches contain miscellaneous updates, including implementing txctrl.txen and rxctrl.rxen, update IRQ when rxctrl is written and remove the unused ip variable.
[1] 53c1557b230986ab6320a58e1b2c26216ecd86d5
Frank Chang (4):
hw/char: sifive_uart: Implement txctrl.txen and rxctrl.rxen hw/char: sifive_uart: Sync txwm interrupt pending status after TX FIFO
enqueue
hw/char: sifive_uart: Update IRQ when rxctrl is written hw/char: sifive_uart: Remove ip variable
hw/char/sifive_uart.c | 46 ++++++++++++++++++++++++++++------- include/hw/char/sifive_uart.h | 3 ++- 2 files changed, 39 insertions(+), 10 deletions(-)
--
2.43.0
PATCH v2 1/4 hw/char: sifive_uart: Implement txctrl.txen and rxctrl.rxen
From: Frank Chang <[email protected]>
Implement txctrl.txen and rxctrl.rxen as follows:
* txctrl.txen
The txen bit controls whether the Tx channel is active. When cleared, transmission of Tx FIFO contents is suppressed, and the txd pin is driven high.
* rxctrl.rxen:
The rxen bit controls whether the Rx channel is active. When cleared, the state of the rxd pin is ignored, and no characters will be enqueued into the Rx FIFO.
Therefore, the Tx FIFO should not be dequeued when txctrl.txen is cleared, and the Rx FIFO should not be enqueued when rxctrl.rxen is cleared.
--- hw/char/sifive_uart.c | 27 ++++++++++++++++++++------- include/hw/char/sifive_uart.h | 2 ++ 2 files changed, 22 insertions(+), 7 deletions(-)
hw/char/sifive_uart.c +20 -7
@@ -78,6 +78,11 @@ static gboolean sifive_uart_xmit(void *do_not_use, GIOCondition cond,return G_SOURCE_REMOVE ; } +/* Don't pop the FIFO if transmit is disabled. */ +if (!SIFIVE_UART_TXEN (s->txctrl)) { +return G_SOURCE_REMOVE ; + } +/* Don't pop the FIFO in case the write fails */ characters =fifo8_peek_bufptr (&s->tx_fifo,fifo8_num_used (&s->tx_fifo), &numptr);
@@ -106,11 +111,19 @@ static gboolean sifive_uart_xmit(void *do_not_use, GIOCondition cond,return G_SOURCE_REMOVE ; } -static void sifive_uart_write_tx_fifo (SiFiveUARTState *s,const uint8_t *buf, -int size) +static void sifive_uart_trigger_tx_fifo (SiFiveUARTState *s) {uint64_t current_time =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); +if (!timer_pending (s->fifo_trigger_handle)) { +timer_mod (s->fifo_trigger_handle, current_time + +TX_INTERRUPT_TRIGGER_DELAY_NS ); + } +} + +static void sifive_uart_write_tx_fifo (SiFiveUARTState *s,const uint8_t *buf, +int size) +{if (size >fifo8_num_free (&s->tx_fifo)) { size =fifo8_num_free (&s->tx_fifo);qemu_log_mask (LOG_GUEST_ERROR ,"sifive_uart: TX FIFO overflow.\n" );
@@ -124,10 +137,7 @@ static void sifive_uart_write_tx_fifo(SiFiveUARTState *s, const uint8_t *buf, s->txfifo |=SIFIVE_UART_TXFIFO_FULL ; } -if (!timer_pending (s->fifo_trigger_handle)) { -timer_mod (s->fifo_trigger_handle, current_time + -TX_INTERRUPT_TRIGGER_DELAY_NS ); - } +sifive_uart_trigger_tx_fifo (s); } static uint64_t
@@ -184,6 +194,9 @@ sifive_uart_write(void *opaque, hwaddr addr,return ;case SIFIVE_UART_TXCTRL : s->txctrl = val64; +if (SIFIVE_UART_TXEN (s->txctrl) && !fifo8_is_empty (&s->tx_fifo)) { +sifive_uart_trigger_tx_fifo (s); + }return ;case SIFIVE_UART_RXCTRL : s->rxctrl = val64;
@@ -231,7 +244,7 @@ static int sifive_uart_can_rx(void *opaque) {SiFiveUARTState *s = opaque; -return s->rx_fifo_len <sizeof (s->rx_fifo); +return SIFIVE_UART_RXEN (s->rxctrl) && (s->rx_fifo_len <sizeof (s->rx_fifo)); }static void sifive_uart_event (void *opaque,QEMUChrEvent event)
include/hw/char/sifive_uart.h +2 -0
@@ -51,6 +51,8 @@ enum {#define SIFIVE_UART_TXFIFO_FULL 0x80000000 +#define SIFIVE_UART_TXEN (txctrl) (txctrl & 0x1) +#define SIFIVE_UART_RXEN (rxctrl) (rxctrl & 0x1)#define SIFIVE_UART_GET_TXCNT (txctrl) ((txctrl >> 16) & 0x7)#define SIFIVE_UART_GET_RXCNT (rxctrl) ((rxctrl >> 16) & 0x7)
Signature · 1 line
2.43.0
PATCH v2 2/4 hw/char: sifive_uart: Sync txwm interrupt pending status after TX FIFO enqueue
In reply to [email protected]From: Frank Chang <[email protected]>
Currently, the txwm interrupt pending status is only updated when the asynchronous transmit handler runs. This can cause the txwm interrupt state to become unsynchronized between the SiFive UART and the interrupt controller.
For example, when a txwm interrupt is raised, the corresponding APLIC pending bit is also set. However, if software later enqueues additional characters into the TX FIFO exceeding the transmit watermark, the APLIC pending bit may remain set because the txwm interrupt pending status is not updated at enqueue time.
This issue has been observed on resource-constrained machines, where Linux reports spurious IRQ errors. In these cases, the asynchronous transmit handler is unable to drain the TX FIFO quickly enough to update the txwm pending status before software reads the ip register, which derives the txwm pending state directly from the actual number of characters in the TX FIFO.
This commit fixes the issue by updating the txwm interrupt pending status immediately after enqueuing data into the TX FIFO, ensuring that the interrupt pending status between the SiFive UART and the interrupt controller remains synchronized.
--- hw/char/sifive_uart.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
hw/char/sifive_uart.c +16 -0
@@ -124,12 +124,20 @@ static void sifive_uart_trigger_tx_fifo(SiFiveUARTState *s)static void sifive_uart_write_tx_fifo (SiFiveUARTState *s,const uint8_t *buf,int size) { +uint32_t txcnt =SIFIVE_UART_GET_TXCNT (s->txctrl); +bool update_irq = false; +if (size >fifo8_num_free (&s->tx_fifo)) { size =fifo8_num_free (&s->tx_fifo);qemu_log_mask (LOG_GUEST_ERROR ,"sifive_uart: TX FIFO overflow.\n" ); }if (size >0 ) { +if (fifo8_num_used (&s->tx_fifo) < txcnt && + (fifo8_num_used (&s->tx_fifo) + size) >= txcnt) { + update_irq = true; + } +fifo8_push_all (&s->tx_fifo, buf, size); }
@@ -137,6 +145,14 @@ static void sifive_uart_write_tx_fifo(SiFiveUARTState *s, const uint8_t *buf, s->txfifo |=SIFIVE_UART_TXFIFO_FULL ; } +/* +* Update txwm interrupt pending status when the number of entries +* in the transmit FIFO crosses or reaches the watermark. +*/ +if (update_irq) { +sifive_uart_update_irq (s); + } +sifive_uart_trigger_tx_fifo (s); }
Signature · 1 line
2.43.0
PATCH v2 3/4 hw/char: sifive_uart: Update IRQ when rxctrl is written
In reply to [email protected]From: Frank Chang <[email protected]>
When rxctl is updated, we also need to check whether the IRQ should be raised, as the user may activate the Rx channel or change the Rx FIFO watermark level.
--- hw/char/sifive_uart.c | 1 + 1 file changed, 1 insertion(+)
hw/char/sifive_uart.c +1 -0
@@ -216,6 +216,7 @@ sifive_uart_write(void *opaque, hwaddr addr,return ;case SIFIVE_UART_RXCTRL : s->rxctrl = val64; +sifive_uart_update_irq (s);return ;case SIFIVE_UART_DIV : s->div = val64;
Signature · 1 line
2.43.0
PATCH v2 4/4 hw/char: sifive_uart: Remove ip variable
In reply to [email protected]From: Frank Chang <[email protected]>
The ip variable is no longer used in the code. Remove it from the codebase.
--- hw/char/sifive_uart.c | 6 ++---- include/hw/char/sifive_uart.h | 1 - 2 files changed, 2 insertions(+), 5 deletions(-)
hw/char/sifive_uart.c +2 -4
@@ -285,7 +285,6 @@ static void sifive_uart_reset_enter(Object *obj, ResetType type) s->txfifo =0 ; s->ie =0 ; - s->ip =0 ; s->txctrl =0 ; s->rxctrl =0 ; s->div =0 ;
@@ -343,14 +342,13 @@ static void sifive_uart_reset_hold(Object *obj, ResetType type)static const VMStateDescription vmstate_sifive_uart = { .name =TYPE_SIFIVE_UART , - .version_id =2 , - .minimum_version_id =2 , + .version_id =3 , + .minimum_version_id =3 , .fields = (const VMStateField []) {VMSTATE_UINT8_ARRAY (rx_fifo, SiFiveUARTState,SIFIVE_UART_RX_FIFO_SIZE ),VMSTATE_UINT8 (rx_fifo_len, SiFiveUARTState),VMSTATE_UINT32 (ie, SiFiveUARTState), -VMSTATE_UINT32 (ip, SiFiveUARTState),VMSTATE_UINT32 (txctrl, SiFiveUARTState),VMSTATE_UINT32 (rxctrl, SiFiveUARTState),VMSTATE_UINT32 (div, SiFiveUARTState),
include/hw/char/sifive_uart.h +0 -1
@@ -73,7 +73,6 @@ struct SiFiveUARTState {uint32_t txfifo;uint32_t ie; -uint32_t ip;uint32_t txctrl;uint32_t rxctrl;uint32_t div;
Signature · 1 line
2.43.0
Sorry, the title should be: "[PATCH v2 0/4] Fix SiFive UART spurious IRQ issue and misc updates" instead.
ping
Regards,
Frank Chang
Quoted 39 lines · On Wed, Dec 10, 2025 at 12:01 AM <[email protected]> wrote:
From: Frank Chang <[email protected]>
This patch series fixes the spurious IRQ issue in the SiFive UART on Linux introduced after commit [1], which changed character transmission from synchronous to asynchronous.
Currently, the txwm interrupt pending status is only updated when the asynchronous transmit handler runs. This can cause the txwm interrupt state to become unsynchronized between the SiFive UART and the interrupt controller.
This issue has been observed on resource-constrained systems, where Linux reports spurious IRQ errors. In these cases, the asynchronous transmit handler is unable to drain the TX FIFO quickly enough to update the txwm pending status before software reads the IP register, which derives the txwm pending state directly from the actual number of characters in the TX FIFO.
The remaining patches contain miscellaneous updates, including implementing txctrl.txen and rxctrl.rxen, update IRQ when rxctrl is written and remove the unused ip variable.
[1]
53c1557b230986ab6320a58e1b2c26216ecd86d5Frank Chang (4):
hw/char:sifive_uart: Implement txctrl.txen and rxctrl.rxen hw/char:sifive_uart: Sync txwm interrupt pending status after TX FIFO
enqueue
hw/char:sifive_uart: Update IRQ when rxctrl is written hw/char:sifive_uart: Remove ip variablehw/char/sifive_uart.c | 46 ++++++++++++++++++++++++++++------- include/hw/char/sifive_uart.h | 3 ++- 2 files changed, 39 insertions(+), 10 deletions(-)--
2.43.0
Gentle ping.
Thanks~
Quoted 49 lines · Frank Chang
Sorry, the title should be: "[PATCH v2 0/4] Fix SiFive UART spurious IRQ issue and misc updates" instead.
ping
Regards,
Frank ChangQuoted 39 lines · On Wed, Dec 10, 2025 at 12:01 AM <[email protected]> wrote:
From: Frank Chang <[email protected]>
This patch series fixes the spurious IRQ issue in the SiFive UART on Linux introduced after commit [1], which changed character transmission from synchronous to asynchronous.
Currently, the txwm interrupt pending status is only updated when the asynchronous transmit handler runs. This can cause the txwm interrupt state to become unsynchronized between the SiFive UART and the interrupt controller.
This issue has been observed on resource-constrained systems, where Linux reports spurious IRQ errors. In these cases, the asynchronous transmit handler is unable to drain the TX FIFO quickly enough to update the txwm pending status before software reads the IP register, which derives the txwm pending state directly from the actual number of characters in the TX FIFO.
The remaining patches contain miscellaneous updates, including implementing txctrl.txen and rxctrl.rxen, update IRQ when rxctrl is written and remove the unused ip variable.
[1]
53c1557b230986ab6320a58e1b2c26216ecd86d5Frank Chang (4):
hw/char:sifive_uart: Implement txctrl.txen and rxctrl.rxen hw/char:sifive_uart: Sync txwm interrupt pending status after TX FIFO
enqueue
hw/char:sifive_uart: Update IRQ when rxctrl is written hw/char:sifive_uart: Remove ip variablehw/char/sifive_uart.c | 46 ++++++++++++++++++++++++++++------- include/hw/char/sifive_uart.h | 3 ++- 2 files changed, 39 insertions(+), 10 deletions(-)--
2.43.0
Quoted 2 lines · Frank Chang
Gentle ping.
Hey,
Sorry about this but I lost these patches and the ones from the other series you sent that I haven't responded to. Do you mind sending a new version?
Alistair
Quoted 52 lines
Thanks~
Quoted 48 lines · Frank Chang
Sorry, the title should be: "[PATCH v2 0/4] Fix SiFive UART spurious IRQ issue and misc updates" instead.
ping
Regards,
Frank ChangQuoted 39 lines · On Wed, Dec 10, 2025 at 12:01 AM <[email protected]> wrote:
From: Frank Chang <[email protected]>
This patch series fixes the spurious IRQ issue in the SiFive UART on Linux introduced after commit [1], which changed character transmission from synchronous to asynchronous.
Currently, the txwm interrupt pending status is only updated when the asynchronous transmit handler runs. This can cause the txwm interrupt state to become unsynchronized between the SiFive UART and the interrupt controller.
This issue has been observed on resource-constrained systems, where Linux reports spurious IRQ errors. In these cases, the asynchronous transmit handler is unable to drain the TX FIFO quickly enough to update the txwm pending status before software reads the IP register, which derives the txwm pending state directly from the actual number of characters in the TX FIFO.
The remaining patches contain miscellaneous updates, including implementing txctrl.txen and rxctrl.rxen, update IRQ when rxctrl is written and remove the unused ip variable.
[1]
53c1557b230986ab6320a58e1b2c26216ecd86d5Frank Chang (4):
hw/char:sifive_uart: Implement txctrl.txen and rxctrl.rxen hw/char:sifive_uart: Sync txwm interrupt pending status after TX FIFO
enqueue
hw/char:sifive_uart: Update IRQ when rxctrl is written hw/char:sifive_uart: Remove ip variablehw/char/sifive_uart.c | 46 ++++++++++++++++++++++++++++------- include/hw/char/sifive_uart.h | 3 ++- 2 files changed, 39 insertions(+), 10 deletions(-)--
2.43.0
Quoted 11 lines · Alistair Francis
Quoted 2 lines · Frank Chang
Gentle ping.
Hey,
Sorry about this but I lost these patches and the ones from the other series you sent that I haven't responded to. Do you mind sending a new version?
No problem. Thanks for reviewing.
I've sent out the v3 patchset:
https://patchew.org/QEMU/[email protected]/
Regards,
Frank Chang
Quoted 62 lines
Alistair
Quoted 4 lines
Thanks~
On Mon, Jan 19, 2026 at 4:13 PM Frank Chang <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
Sorry, the title should be: "[PATCH v2 0/4] Fix SiFive UART spurious
IRQ issue and misc updates" instead.
Quoted 11 lines
Quoted 11 lines
ping
Regards,
Frank ChangQuoted 4 lines · On Wed, Dec 10, 2025 at 12:01 AM <[email protected]> wrote:
From: Frank Chang <[email protected]>
This patch series fixes the spurious IRQ issue in the SiFive UART on
Linux
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
introduced after commit [1], which changed character transmission from synchronous to asynchronous.
Currently, the txwm interrupt pending status is only updated when the asynchronous transmit handler runs. This can cause the txwm interrupt state to become unsynchronized between the SiFive UART and the
interrupt
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
controller.
This issue has been observed on resource-constrained systems, where Linux reports spurious IRQ errors. In these cases, the asynchronous transmit handler is unable to drain the TX FIFO quickly enough to
update
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
the txwm pending status before software reads the IP register, which derives the txwm pending state directly from the actual number of characters in the TX FIFO.
The remaining patches contain miscellaneous updates, including implementing txctrl.txen and rxctrl.rxen, update IRQ when rxctrl is written and remove the unused ip variable.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
[1]
53c1557b230986ab6320a58e1b2c26216ecd86d5Frank Chang (4):
hw/char:sifive_uart: Implement txctrl.txen and rxctrl.rxen hw/char:sifive_uart: Sync txwm interrupt pending status after TXFIFO
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
enqueue
hw/char:sifive_uart: Update IRQ when rxctrl is written hw/char:sifive_uart: Remove ip variablehw/char/sifive_uart.c | 46 ++++++++++++++++++++++++++++------- include/hw/char/sifive_uart.h | 3 ++- 2 files changed, 39 insertions(+), 10 deletions(-)--
2.43.0