From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
qemu-arm@nongnu.org,
"Mark Cave-Ayland" <mark.cave-ayland@ilande.co.uk>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Mikko Rapeli" <mikko.rapeli@linaro.org>
Subject: [PATCH v6 7/7] hw/char/pl011: Implement TX FIFO
Date: Sat, 8 Feb 2025 17:39:11 +0100 [thread overview]
Message-ID: <20250208163911.54522-8-philmd@linaro.org> (raw)
In-Reply-To: <20250208163911.54522-1-philmd@linaro.org>
If the UART back-end chardev doesn't drain data as fast as stdout
does or blocks, buffer in the TX FIFO to try again later.
This avoids having the IO-thread busy waiting on chardev back-ends,
reported recently when testing the Trusted Reference Stack and
using the socket backend.
Implement registering a front-end 'watch' callback on back-end
events, so we can resume transmitting when the back-end is writable
again, not blocking the main loop.
Similarly to the RX FIFO path, FIFO level selection is not
implemented (interrupt is triggered when a single byte is available
in the FIFO).
Reported-by: Mikko Rapeli <mikko.rapeli@linaro.org>
Suggested-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/char/pl011.c | 39 +++++++++++++++++++++++++++++++--------
hw/char/trace-events | 1 +
2 files changed, 32 insertions(+), 8 deletions(-)
diff --git a/hw/char/pl011.c b/hw/char/pl011.c
index 3c4264869df..70eba224a9c 100644
--- a/hw/char/pl011.c
+++ b/hw/char/pl011.c
@@ -251,11 +251,15 @@ static gboolean pl011_xmit(void *do_not_use, GIOCondition cond, void *opaque)
{
PL011State *s = opaque;
int bytes_consumed;
- uint8_t data;
+ uint8_t buf[PL011_FIFO_DEPTH];
uint32_t count;
count = fifo8_num_used(&s->xmit_fifo);
trace_pl011_fifo_tx_xmit_used(count);
+ if (count < 1) {
+ /* FIFO empty */
+ return G_SOURCE_REMOVE;
+ }
if (!qemu_chr_fe_backend_connected(&s->chr)) {
/* Instant drain the fifo when there's no back-end. */
@@ -263,19 +267,29 @@ static gboolean pl011_xmit(void *do_not_use, GIOCondition cond, void *opaque)
return G_SOURCE_REMOVE;
}
- data = fifo8_pop(&s->xmit_fifo);
- bytes_consumed = 1;
+ count = fifo8_peek_buf(&s->xmit_fifo, buf, fifo8_num_used(&s->xmit_fifo));
+ trace_pl011_fifo_tx_xmit_peek(count);
- /*
- * XXX this blocks entire thread. Rewrite to use
- * qemu_chr_fe_write and background I/O callbacks
- */
- qemu_chr_fe_write_all(&s->chr, &data, bytes_consumed);
+ /* Transmit as much data as we can. */
+ bytes_consumed = qemu_chr_fe_write(&s->chr, buf, count);
trace_pl011_fifo_tx_xmit_consumed(bytes_consumed);
+ if (bytes_consumed < 0) {
+ /* Error in back-end: drain the fifo. */
+ pl011_drain_tx(s);
+ return G_SOURCE_REMOVE;
+ }
+
+ /* Pop the data we could transmit. */
+ fifo8_drop(&s->xmit_fifo, bytes_consumed);
s->int_level |= INT_TX;
pl011_update(s);
+ if (!fifo8_is_empty(&s->xmit_fifo)) {
+ /* Reschedule another transmission if we couldn't transmit all. */
+ return G_SOURCE_CONTINUE;
+ }
+
return G_SOURCE_REMOVE;
}
@@ -300,6 +314,10 @@ static void pl011_write_txdata(PL011State *s, uint8_t data)
trace_pl011_fifo_tx_put(data);
pl011_loopback_tx(s, data);
fifo8_push(&s->xmit_fifo, data);
+ if (pl011_is_tx_fifo_full(s)) {
+ s->flags |= PL011_FLAG_TXFF;
+ }
+
pl011_xmit(NULL, G_IO_OUT, s);
}
@@ -651,6 +669,11 @@ static int pl011_post_load(void *opaque, int version_id)
s->read_pos = 0;
}
+ if (!fifo8_is_empty(&s->xmit_fifo)) {
+ /* Reschedule another transmission */
+ qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP, pl011_xmit, s);
+ }
+
s->ibrd &= IBRD_MASK;
s->fbrd &= FBRD_MASK;
diff --git a/hw/char/trace-events b/hw/char/trace-events
index 7d1cba1b4f8..2d02c057483 100644
--- a/hw/char/trace-events
+++ b/hw/char/trace-events
@@ -69,6 +69,7 @@ pl011_fifo_rx_put(uint32_t c, int read_count) "new char 0x%02x read_count now %d
pl011_fifo_rx_full(void) "RX FIFO now full, RXFF set"
pl011_fifo_tx_is_full(const char *desc, bool full) "mode:%s full:%u"
pl011_fifo_tx_put(uint8_t byte) "TX FIFO push char [0x%02x]"
+pl011_fifo_tx_xmit_peek(unsigned sent) "TX FIFO peek %u chars"
pl011_fifo_tx_xmit_used(unsigned sent) "TX FIFO used %u chars"
pl011_fifo_tx_xmit_consumed(unsigned sent) "TX FIFO consumed %u chars"
pl011_fifo_tx_overrun(void) "TX FIFO overrun"
--
2.47.1
next prev parent reply other threads:[~2025-02-08 16:41 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-08 16:39 [PATCH v6 0/7] hw/char/pl011: Implement TX (async) FIFO to avoid blocking the main loop Philippe Mathieu-Daudé
2025-02-08 16:39 ` [PATCH v6 1/7] hw/char/pl011: Warn when using disabled receiver Philippe Mathieu-Daudé
2025-02-08 16:39 ` [PATCH v6 2/7] hw/char/pl011: Add transmit FIFO to PL011State Philippe Mathieu-Daudé
2025-02-08 16:39 ` [PATCH v6 3/7] hw/char/pl011: Introduce pl011_xmit() as GSource Philippe Mathieu-Daudé
2025-02-17 14:25 ` Peter Maydell
2025-02-08 16:39 ` [PATCH v6 4/7] hw/char/pl011: Trace FIFO enablement Philippe Mathieu-Daudé
2025-02-17 14:27 ` Peter Maydell
2025-02-17 14:39 ` Peter Maydell
2025-02-17 14:45 ` Philippe Mathieu-Daudé
2025-02-08 16:39 ` [PATCH v6 5/7] hw/char/pl011: Consider TX FIFO overrun error Philippe Mathieu-Daudé
2025-02-17 14:29 ` Peter Maydell
2025-02-17 14:52 ` Peter Maydell
2025-02-17 15:01 ` Philippe Mathieu-Daudé
2025-02-08 16:39 ` [PATCH v6 6/7] hw/char/pl011: Drain TX FIFO when no backend connected Philippe Mathieu-Daudé
2025-02-17 14:30 ` Peter Maydell
2025-02-08 16:39 ` Philippe Mathieu-Daudé [this message]
2025-02-17 14:37 ` [PATCH v6 7/7] hw/char/pl011: Implement TX FIFO Peter Maydell
2025-02-17 14:55 ` [PATCH v6 0/7] hw/char/pl011: Implement TX (async) FIFO to avoid blocking the main loop Peter Maydell
2025-02-18 13:54 ` Peter Maydell
2025-02-20 10:43 ` Peter Maydell
2025-02-20 10:52 ` Peter Maydell
2025-02-20 11:04 ` Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250208163911.54522-8-philmd@linaro.org \
--to=philmd@linaro.org \
--cc=alex.bennee@linaro.org \
--cc=marcandre.lureau@redhat.com \
--cc=mark.cave-ayland@ilande.co.uk \
--cc=mikko.rapeli@linaro.org \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).