From 356d8b942b514c1bca72eabcc17548d88906d5f3 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 3 Nov 2026 04:44:21 +0000 Subject: [PATCH] [ot] hw/opentitan: gpio: report host pin status on query Previously, the OpenTitan GPIO monitor sent an unsolicited query command (`Q:`) upon socket connection in `ot_gpio_eg_chr_event_hander` and `ot_gpio_dj_chr_event_hander`. This required external host tools to cache GPIO state across connections to answer QEMU's query. This behavior makes it difficult to interact with QEMU across multiple opentitantool invocations and differs from other platforms using the HyperDebug board. Update the GPIO monitor to report host pin status on demand: - Remove unsolicited `Q:` frame transmission on `CHR_EVENT_OPENED`. - In `ot_gpio_eg_chr_receive` and `ot_gpio_dj_chr_receive`, handle `cmd == 'Q'` by replying with the current host pin configuration frames: - `M:%08x\r\n` (host output disable mask `s->data_bi`) - `I:%08x\r\n` (host driven input data `s->data_ib`) - `H:%08x\r\n` (host pull-up mask `s->host_pull` in `ot_gpio_eg`, `0` in `ot_gpio_dj`) This allows host tools to actively query and synchronize the current host pin status across connections. Signed-off-by: Yi-Hsuan Deng Change-Id: I8b5cd78ce6a62afafa1c70b379d1845ba4dd1527 --- hw/opentitan/ot_gpio_dj.c | 13 ++++++------- hw/opentitan/ot_gpio_eg.c | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/hw/opentitan/ot_gpio_dj.c b/hw/opentitan/ot_gpio_dj.c index eb14e6c83dda4..08893b5f3f63d 100644 --- a/hw/opentitan/ot_gpio_dj.c +++ b/hw/opentitan/ot_gpio_dj.c @@ -642,6 +642,12 @@ static void ot_gpio_dj_chr_receive(void *opaque, const uint8_t *buf, int size) } else if (cmd == 'I') { s->data_ib = data_in; ot_gpio_dj_update_data_in(s); + } else if (cmd == 'Q') { + char qbuf[64u]; + int qlen = snprintf(qbuf, sizeof(qbuf), + "M:%08x\r\nI:%08x\r\nH:%08x\r\n", + s->data_bi, s->data_ib, 0u); + qemu_chr_fe_write(&s->chr, (const uint8_t *)qbuf, qlen); } else if (cmd == 'R') { ot_gpio_dj_update_backend(s, true); } else { @@ -742,13 +748,6 @@ static void ot_gpio_dj_chr_event_hander(void *opaque, QEMUChrEvent event) } ot_gpio_dj_update_backend(s, true); - - /* query backend for current input status */ - char buf[16u]; - int len = snprintf(buf, sizeof(buf), "Q:%08x\r\n", s->data_oe); - qemu_chr_fe_write(&s->chr, (const uint8_t *)buf, len); - buf[MIN((unsigned)len - 1, sizeof(buf) - 1u)] = '\0'; - trace_ot_gpio_backend_send(s->ot_id, buf); } } diff --git a/hw/opentitan/ot_gpio_eg.c b/hw/opentitan/ot_gpio_eg.c index 1bb9b4d8e4656..03ce36015d7d4 100644 --- a/hw/opentitan/ot_gpio_eg.c +++ b/hw/opentitan/ot_gpio_eg.c @@ -1294,6 +1294,12 @@ static void ot_gpio_eg_chr_receive(void *opaque, const uint8_t *buf, int size) ot_gpio_eg_update_data_in(s); ot_gpio_eg_send_input_ack(s); ot_gpio_eg_update_backend(s, false); + } else if (cmd == 'Q') { + char qbuf[64u]; + int qlen = snprintf(qbuf, sizeof(qbuf), + "M:%08x\r\nI:%08x\r\nH:%08x\r\n", + s->data_bi, s->data_ib, s->host_pull); + qemu_chr_fe_write(&s->chr, (const uint8_t *)qbuf, qlen); } else if (cmd == 'R') { s->sync_seq = data_in; timer_mod(s->sync_timer, @@ -1521,13 +1527,6 @@ static void ot_gpio_eg_chr_event_hander(void *opaque, QEMUChrEvent event) } ot_gpio_eg_update_backend(s, true); - - /* query backend for current input status */ - char buf[16u]; - int len = snprintf(buf, sizeof(buf), "Q:%08x\r\n", s->data_oe); - qemu_chr_fe_write(&s->chr, (const uint8_t *)buf, len); - buf[MIN((unsigned)len - 1, sizeof(buf) - 1u)] = '\0'; - trace_ot_gpio_backend_send(s->ot_id, buf); } }