Skip to contentSkip to conversation

TYPE_SERIAL cleanup

PATCH Superseded Bernhard Beschow 3 participants 28 messages Mar 2 – Mar 3 Arm/Aspeed machine support

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover TYPE_SERIAL cleanup

This series consists of cosmetic changes around TYPE_SERIAL. The biggest change is having it derive from TYPE_SYS_BUS_DEVICE which 1) eliminates usage of qemu_{un,}register_reset() and 2) stops all its users from accessing private data. The latter allows TYPE_SERIAL to be ported to Rust in the (near) future.

Testing done:
* Make check
* Boot mpc8544ds machine
* Boot q35 machine with Arch Linux and interact with it on the serial console

Bernhard Beschow (12):
hw/arm/Kconfig: Fix serial selection for NPCM8XX hw/char/serial: Remove explicit cast from void pointer hw/char/serial: Prefer fifo8 methods over open-coding hw/char/serial: Reuse fifo8_num_used()
hw/char/serial: Remove unhelpful comment hw/char/serial: Add constants for Line Control Register hw/char/serial: Remove redundant reset
hw/char/serial: Avoid implicit conversion when tracing util/fifo8: Make all read-only methods const-correct hw/arm/aspeed_ast27x0-{ssp,tsp}: Do not access SerialMM internals
directly
hw/char: Have SerialState inherit from SysBusDevice hw/char/serial: Plug into reset framework

 include/hw/char/serial.h    |  4 +--
 include/qemu/fifo8.h        | 10 +++---
 hw/arm/aspeed_ast27x0-ssp.c |  7 +++--
 hw/arm/aspeed_ast27x0-tsp.c |  7 +++--
 hw/char/diva-gsp.c          | 22 ++++++-------
 hw/char/serial-isa.c        | 19 +++++++-----
 hw/char/serial-mm.c         | 10 +++---
 hw/char/serial-pci-multi.c  | 22 ++++++-------
 hw/char/serial-pci.c        | 13 ++++----
 hw/char/serial.c            | 61 +++++++++++++++++++------------------
 util/fifo8.c                | 10 +++---
 hw/arm/Kconfig              |  2 +-
 hw/char/trace-events        |  4 +--
 13 files changed, 100 insertions(+), 91 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/12 hw/arm/Kconfig: Fix serial selection for NPCM8XX

CONFIG_SERIAL selects the internal TYPE_SERIAL device which is akin to an "IP block" that needs to be integrated with glue logic. In case of NPCM8XX this glue logic is TYPE_SERIAL_MM which the code uses already. Fix Kconfig to select CONFIG_SERIAL_MM which matches TYPE_SERIAL_MM.

Fixes ae0c4d1a1290 ("hw/arm: Add NPCM8XX SoC")
---
 hw/arm/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/Kconfig +1 -1
index c66c452737..20e99e52b1 100644
@@ -493,7 +493,7 @@ config NPCM8XX
     select SMBUS
     select PL310  # cache controller
     select NPCM7XX
-    select SERIAL
+    select SERIAL_MM
     select SSI
     select UNIMP
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/12
#2 lore raw

Re: [PATCH 01/12] hw/arm/Kconfig: Fix serial selection for NPCM8XX

Quoted 17 lines · Bernhard Beschow

CONFIG_SERIAL selects the internal TYPE_SERIAL device which is akin to an "IP block" that needs to be integrated with glue logic. In case of NPCM8XX this glue logic is TYPE_SERIAL_MM which the code uses already. Fix Kconfig to select CONFIG_SERIAL_MM which matches TYPE_SERIAL_MM.

Fixes ae0c4d1a1290 ("hw/arm: Add NPCM8XX SoC")
---
 hw/arm/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/arm/Kconfig +0 -0
index c66c452737..20e99e52b1 100644

hw/arm/Kconfig config NPCM8XX · line 498

Quoted 7 lines
hw/arm/Kconfig +1 -1
@@ -493,7 +493,7 @@ config NPCM8XX
     select SMBUS
     select PL310  # cache controller
     select NPCM7XX
-    select SERIAL
+    select SERIAL_MM
     select SSI
     select UNIMP

thanks
-- PMM

#3 lore raw

PATCH 2/12 hw/char/serial: Remove explicit cast from void pointer

In reply to Bernhard Beschow

A void pointer asks for being casted, so C allows for omitting the explicit cast. Take advantage of that.

---
 hw/char/serial.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
hw/char/serial.c +6 -6
index adbd1d1d4a..0f2e79dfba 100644
@@ -715,7 +715,7 @@ static const VMStateDescription vmstate_serial_thr_ipending = {
 
 static bool serial_tsr_needed(void *opaque)
 {
-    SerialState *s = (SerialState *)opaque;
+    SerialState *s = opaque;
     return s->tsr_retry != 0;
 }
 
@@ -734,7 +734,7 @@ static const VMStateDescription vmstate_serial_tsr = {
 
 static bool serial_recv_fifo_needed(void *opaque)
 {
-    SerialState *s = (SerialState *)opaque;
+    SerialState *s = opaque;
     return !fifo8_is_empty(&s->recv_fifo);
 
 }
@@ -752,7 +752,7 @@ static const VMStateDescription vmstate_serial_recv_fifo = {
 
 static bool serial_xmit_fifo_needed(void *opaque)
 {
-    SerialState *s = (SerialState *)opaque;
+    SerialState *s = opaque;
     return !fifo8_is_empty(&s->xmit_fifo);
 }
 
@@ -769,7 +769,7 @@ static const VMStateDescription vmstate_serial_xmit_fifo = {
 
 static bool serial_fifo_timeout_timer_needed(void *opaque)
 {
-    SerialState *s = (SerialState *)opaque;
+    SerialState *s = opaque;
     return timer_pending(s->fifo_timeout_timer);
 }
 
@@ -786,7 +786,7 @@ static const VMStateDescription vmstate_serial_fifo_timeout_timer = {
 
 static bool serial_timeout_ipending_needed(void *opaque)
 {
-    SerialState *s = (SerialState *)opaque;
+    SerialState *s = opaque;
     return s->timeout_ipending != 0;
 }
 
@@ -803,7 +803,7 @@ static const VMStateDescription vmstate_serial_timeout_ipending = {
 
 static bool serial_poll_needed(void *opaque)
 {
-    SerialState *s = (SerialState *)opaque;
+    SerialState *s = opaque;
     return s->poll_msl >= 0;
 }
 
Signature · 1 line
2.53.0
#5 lore raw

PATCH 3/12 hw/char/serial: Prefer fifo8 methods over open-coding

In reply to Bernhard Beschow

Use fifo8_is_empty() and fifo8_is_full() to improve readability of the code.

---
 hw/char/serial.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/char/serial.c +4 -4
index 0f2e79dfba..20f68fd2f8 100644
@@ -239,7 +239,7 @@ static void serial_xmit(SerialState *s)
             if (s->fcr & UART_FCR_FE) {
                 assert(!fifo8_is_empty(&s->xmit_fifo));
                 s->tsr = fifo8_pop(&s->xmit_fifo);
-                if (!s->xmit_fifo.num) {
+                if (fifo8_is_empty(&s->xmit_fifo)) {
                     s->lsr |= UART_LSR_THRE;
                 }
             } else {
@@ -481,7 +481,7 @@ static uint64_t serial_ioport_read(void *opaque, hwaddr addr, unsigned size)
             if(s->fcr & UART_FCR_FE) {
                 ret = fifo8_is_empty(&s->recv_fifo) ?
                             0 : fifo8_pop(&s->recv_fifo);
-                if (s->recv_fifo.num == 0) {
+                if (fifo8_is_empty(&s->recv_fifo)) {
                     s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
                 } else {
                     timer_mod(s->fifo_timeout_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time * 4);
@@ -555,7 +555,7 @@ static uint64_t serial_ioport_read(void *opaque, hwaddr addr, unsigned size)
 static int serial_can_receive(SerialState *s)
 {
     if(s->fcr & UART_FCR_FE) {
-        if (s->recv_fifo.num < UART_FIFO_LENGTH) {
+        if (!fifo8_is_full(&s->recv_fifo)) {
             /*
              * Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1
              * if above. If UART_FIFO_LENGTH - fifo.count is advertised the
@@ -585,7 +585,7 @@ static void serial_receive_break(SerialState *s)
 /* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
 static void fifo_timeout_int (void *opaque) {
     SerialState *s = opaque;
-    if (s->recv_fifo.num) {
+    if (!fifo8_is_empty(&s->recv_fifo)) {
         s->timeout_ipending = 1;
         serial_update_irq(s);
     }
Signature · 1 line
2.53.0
#7 lore raw

PATCH 4/12 hw/char/serial: Reuse fifo8_num_used()

In reply to Bernhard Beschow

Avoids accessing private fields of struct Fifo8. Now, TYPE_SERIAL only accesses struct Fifo8 through its methods.

---
 hw/char/serial.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/char/serial.c +3 -3
index 20f68fd2f8..2c558cb9fc 100644
@@ -128,7 +128,7 @@ static void serial_update_irq(SerialState *s)
         tmp_iir = UART_IIR_CTI;
     } else if ((s->ier & UART_IER_RDI) && (s->lsr & UART_LSR_DR) &&
                (!(s->fcr & UART_FCR_FE) ||
-                s->recv_fifo.num >= s->recv_fifo_itl)) {
+                fifo8_num_used(&s->recv_fifo) >= s->recv_fifo_itl)) {
         tmp_iir = UART_IIR_RDI;
     } else if ((s->ier & UART_IER_THRI) && s->thr_ipending) {
         tmp_iir = UART_IIR_THRI;
@@ -563,8 +563,8 @@ static int serial_can_receive(SerialState *s)
              * the guest has a chance to respond, effectively overriding the ITL
              * that the guest has set.
              */
-            return (s->recv_fifo.num <= s->recv_fifo_itl) ?
-                        s->recv_fifo_itl - s->recv_fifo.num : 1;
+            return (fifo8_num_used(&s->recv_fifo) <= s->recv_fifo_itl) ?
+                        s->recv_fifo_itl - fifo8_num_used(&s->recv_fifo) : 1;
         } else {
             return 0;
         }
Signature · 1 line
2.53.0
Reviewed-by on v1 4/12
#8 lore raw

Re: [PATCH 04/12] hw/char/serial: Reuse fifo8_num_used()

Quoted 9 lines · Bernhard Beschow

Avoids accessing private fields of struct Fifo8. Now, TYPE_SERIAL only accesses struct Fifo8 through its methods.

---
 hw/char/serial.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

thanks
-- PMM

#9 lore raw

PATCH 5/12 hw/char/serial: Remove unhelpful comment

In reply to Bernhard Beschow

There is no "is_load" flag and one can tell from the method name what the method does. Remove this unhelpful comment.

---
 hw/char/serial.c | 3 ---
 1 file changed, 3 deletions(-)
hw/char/serial.c +0 -3
index 2c558cb9fc..f73de1ae4f 100644
@@ -281,9 +281,6 @@ static void serial_xmit(SerialState *s)
     s->lsr |= UART_LSR_TEMT;
 }
 
-/* Setter for FCR.
-   is_load flag means, that value is set while loading VM state
-   and interrupt should not be invoked */
 static void serial_write_fcr(SerialState *s, uint8_t val)
 {
     /* Set fcr - val only has the bits that are supposed to "stick" */
Signature · 1 line
2.53.0
Reviewed-by on v1 5/12
#10 lore raw

Re: [PATCH 05/12] hw/char/serial: Remove unhelpful comment

Quoted 14 lines · Bernhard Beschow

There is no "is_load" flag and one can tell from the method name what the method does. Remove this unhelpful comment.

---
 hw/char/serial.c | 3 ---
 1 file changed, 3 deletions(-)
hw/char/serial.c +0 -0
index 2c558cb9fc..f73de1ae4f 100644

hw/char/serial.c static void serial_xmit(SerialState *s) · line 286

Quoted 9 lines
hw/char/serial.c +0 -3
@@ -281,9 +281,6 @@ static void serial_xmit(SerialState *s)
     s->lsr |= UART_LSR_TEMT;
 }
 
-/* Setter for FCR.
-   is_load flag means, that value is set while loading VM state
-   and interrupt should not be invoked */
 static void serial_write_fcr(SerialState *s, uint8_t val)
 {
     /* Set fcr - val only has the bits that are supposed to "stick" */

This must be an ancient stale comment from before the device was converted to vmstate.

thanks
-- PMM

#11 lore raw

Re: [PATCH 05/12] hw/char/serial: Remove unhelpful comment

Am 3. März 2026 11:27:18 UTC schrieb Peter Maydell <[email protected]>:

Quoted 18 lines · Bernhard Beschow
Quoted 17 lines

There is no "is_load" flag and one can tell from the method name what the method does. Remove this unhelpful comment.

---
 hw/char/serial.c | 3 ---
 1 file changed, 3 deletions(-)
hw/char/serial.c +0 -0
index 2c558cb9fc..f73de1ae4f 100644
@@ -281,9 +281,6 @@ static void serial_xmit(SerialState *s)
     s->lsr |= UART_LSR_TEMT;
 }
 
Quoted 9 lines

hw/char/serial.c static void serial_xmit(SerialState *s) · line 286

Quoted 6 lines
hw/char/serial.c +0 -3
-/* Setter for FCR.
-   is_load flag means, that value is set while loading VM state
-   and interrupt should not be invoked */
 static void serial_write_fcr(SerialState *s, uint8_t val)
 {
     /* Set fcr - val only has the bits that are supposed to "stick" */

This must be an ancient stale comment from before the device was converted to vmstate.

Yeah, "stale" seems like a better fit. Will change in v2.

Best regards,
Bernhard

Quoted 5 lines

thanks
-- PMM

#12 lore raw

PATCH 6/12 hw/char/serial: Add constants for Line Control Register

In reply to Bernhard Beschow

Substitute some magic numbers by named constants for slightly improved readability.

---
 hw/char/serial.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
hw/char/serial.c +11 -6
index f73de1ae4f..485b98f03f 100644
@@ -39,6 +39,11 @@
 #include "hw/core/qdev-properties-system.h"
 
 #define UART_LCR_DLAB   0x80    /* Divisor latch access bit */
+#define UART_LCR_SB     0x40    /* Set break */
+#define UART_LCR_EPS    0x10    /* Even parity select */
+#define UART_LCR_PEN    0x08    /* Parity enable */
+#define UART_LCR_NSTB   0x04    /* Number of stop bits */
+#define UART_LCR_WLS    0x03    /* Word length select */
 
 #define UART_IER_MSI    0x08    /* Enable Modem status interrupt */
 #define UART_IER_RLSI   0x04    /* Enable receiver line status interrupt */
@@ -153,23 +158,23 @@ static void serial_update_parameters(SerialState *s)
 
     /* Start bit. */
     frame_size = 1;
-    if (s->lcr & 0x08) {
+    if (s->lcr & UART_LCR_PEN) {
         /* Parity bit. */
         frame_size++;
-        if (s->lcr & 0x10)
+        if (s->lcr & UART_LCR_EPS)
             parity = 'E';
         else
             parity = 'O';
     } else {
             parity = 'N';
     }
-    if (s->lcr & 0x04) {
+    if (s->lcr & UART_LCR_NSTB) {
         stop_bits = 2;
     } else {
         stop_bits = 1;
     }
 
-    data_bits = (s->lcr & 0x03) + 5;
+    data_bits = (s->lcr & UART_LCR_WLS) + 5;
     frame_size += data_bits + stop_bits;
     /* Zero divisor should give about 3500 baud */
     speed = (s->divider == 0) ? 3500 : (float) s->baudbase / s->divider;
@@ -430,7 +435,7 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,
             int break_enable;
             s->lcr = val;
             serial_update_parameters(s);
-            break_enable = (val >> 6) & 1;
+            break_enable = !!(val & UART_LCR_SB);
             if (break_enable != s->last_break_enable) {
                 s->last_break_enable = break_enable;
                 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
@@ -676,7 +681,7 @@ static int serial_post_load(void *opaque, int version_id)
         }
     }
 
-    s->last_break_enable = (s->lcr >> 6) & 1;
+    s->last_break_enable = !!(s->lcr & UART_LCR_SB);
     /* Initialize fcr via setter to perform essential side-effects */
     serial_write_fcr(s, s->fcr_vmstate);
     serial_update_parameters(s);
Signature · 1 line
2.53.0
#14 lore raw

PATCH 7/12 hw/char/serial: Remove redundant reset

In reply to Bernhard Beschow

There is no need to invoke the reset method in realize since the reset framework will do so anyway before the machine starts.

---
 hw/char/serial.c | 1 -
 1 file changed, 1 deletion(-)
hw/char/serial.c +0 -1
index 485b98f03f..0f3469a1e8 100644
@@ -934,7 +934,6 @@ static void serial_realize(DeviceState *dev, Error **errp)
                              serial_event, serial_be_change, s, NULL, true);
     fifo8_create(&s->recv_fifo, UART_FIFO_LENGTH);
     fifo8_create(&s->xmit_fifo, UART_FIFO_LENGTH);
-    serial_reset(s);
 }
 
 static void serial_unrealize(DeviceState *dev)
Signature · 1 line
2.53.0
Reviewed-by on v1 7/12
#15 lore raw

Re: [PATCH 07/12] hw/char/serial: Remove redundant reset

Quoted 14 lines · Bernhard Beschow

There is no need to invoke the reset method in realize since the reset framework will do so anyway before the machine starts.

---
 hw/char/serial.c | 1 -
 1 file changed, 1 deletion(-)
hw/char/serial.c +0 -0
index 485b98f03f..0f3469a1e8 100644

hw/char/serial.c static void serial_realize(DeviceState *dev, Error **errp) · line 937

Quoted 5 lines
hw/char/serial.c +0 -1
@@ -934,7 +934,6 @@ static void serial_realize(DeviceState *dev, Error **errp)
                              serial_event, serial_be_change, s, NULL, true);
     fifo8_create(&s->recv_fifo, UART_FIFO_LENGTH);
     fifo8_create(&s->xmit_fifo, UART_FIFO_LENGTH);
-    serial_reset(s);
 }

It would be nice to make this not do qemu_register_reset(), though right now because it's a direct subtype of TYPE_DEVICE that would imply having all the devices that directly instantiate TYPE_SERIAL arranging to reset it in their own reset methods.

thanks
-- PMM

#16 lore raw

PATCH 8/12 hw/char/serial: Avoid implicit conversion when tracing

In reply to Bernhard Beschow

On 64 bit targets, the MemoryRegion API passes an address and a value as uint64_t, so use that for tracing. Keep the uint8_t for reading since this is what the device model produces. On targets with less than 64 bits, uint64_t is wide enough to avoid narrowing.

---
 hw/char/trace-events | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/char/trace-events +2 -2
index 9e74be2c14..a3fcc77287 100644
@@ -5,8 +5,8 @@ parallel_ioport_read(const char *desc, uint16_t addr, uint8_t value) "read [%s]
 parallel_ioport_write(const char *desc, uint16_t addr, uint8_t value) "write [%s] addr 0x%02x val 0x%02x"
 
 # serial.c
-serial_read(uint16_t addr, uint8_t value) "read addr 0x%02x val 0x%02x"
-serial_write(uint16_t addr, uint8_t value) "write addr 0x%02x val 0x%02x"
+serial_read(uint64_t addr, uint8_t value) "[0x%02" PRIx64 "] -> 0x%02" PRIx8
+serial_write(uint64_t addr, uint64_t value) "[0x%02" PRIx64 "] <- 0x%02" PRIx64
 serial_update_parameters(uint64_t baudrate, char parity, int data_bits, int stop_bits) "baudrate=%"PRIu64" parity='%c' data=%d stop=%d"
 
 # virtio-serial-bus.c
Signature · 1 line
2.53.0
#17 lore raw

Re: [PATCH 08/12] hw/char/serial: Avoid implicit conversion when tracing

Quoted 16 lines · Bernhard Beschow

On 64 bit targets, the MemoryRegion API passes an address and a value as uint64_t, so use that for tracing. Keep the uint8_t for reading since this is what the device model produces. On targets with less than 64 bits, uint64_t is wide enough to avoid narrowing.

---
 hw/char/trace-events | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/char/trace-events +0 -0
index 9e74be2c14..a3fcc77287 100644

hw/char/trace-events parallel_ioport_read(const char *desc, uint16_t addr, uint8_t value) "read [%s] · line 10

Quoted 8 lines
hw/char/trace-events +2 -2
@@ -5,8 +5,8 @@ parallel_ioport_read(const char *desc, uint16_t addr, uint8_t value) "read [%s]
 parallel_ioport_write(const char *desc, uint16_t addr, uint8_t value) "write [%s] addr 0x%02x val 0x%02x"
 
 # serial.c
-serial_read(uint16_t addr, uint8_t value) "read addr 0x%02x val 0x%02x"
-serial_write(uint16_t addr, uint8_t value) "write addr 0x%02x val 0x%02x"
+serial_read(uint64_t addr, uint8_t value) "[0x%02" PRIx64 "] -> 0x%02" PRIx8
+serial_write(uint64_t addr, uint64_t value) "[0x%02" PRIx64 "] <- 0x%02" PRIx64
 serial_update_parameters(uint64_t baudrate, char parity, int data_bits, int stop_bits) "baudrate=%"PRIu64" parity='%c' data=%d stop=%d"

We know that the value being written fits in one byte, though, because we just asserted that size == 1 before trace_serial_write().

-- PMM

#18 lore raw

Re: [PATCH 08/12] hw/char/serial: Avoid implicit conversion when tracing

Am 3. März 2026 11:34:01 UTC schrieb Peter Maydell <[email protected]>:

Quoted 19 lines · Bernhard Beschow
Quoted 18 lines

On 64 bit targets, the MemoryRegion API passes an address and a value as uint64_t, so use that for tracing. Keep the uint8_t for reading since this is what the device model produces. On targets with less than 64 bits, uint64_t is wide enough to avoid narrowing.

---
 hw/char/trace-events | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/char/trace-events +0 -0
index 9e74be2c14..a3fcc77287 100644
@@ -5,8 +5,8 @@ parallel_ioport_read(const char *desc, uint16_t addr, uint8_t value) "read [%s]
 parallel_ioport_write(const char *desc, uint16_t addr, uint8_t value) "write [%s] addr 0x%02x val 0x%02x"
 
Quoted 9 lines

hw/char/trace-events parallel_ioport_read(const char *desc, uint16_t addr, uint8_t value) "read [%s] · line 10

Quoted 6 lines
hw/char/trace-events +2 -2
 # serial.c
-serial_read(uint16_t addr, uint8_t value) "read addr 0x%02x val 0x%02x"
-serial_write(uint16_t addr, uint8_t value) "write addr 0x%02x val 0x%02x"
+serial_read(uint64_t addr, uint8_t value) "[0x%02" PRIx64 "] -> 0x%02" PRIx8
+serial_write(uint64_t addr, uint64_t value) "[0x%02" PRIx64 "] <- 0x%02" PRIx64
 serial_update_parameters(uint64_t baudrate, char parity, int data_bits, int stop_bits) "baudrate=%"PRIu64" parity='%c' data=%d stop=%d"

We know that the value being written fits in one byte, though, because we just asserted that size == 1 before trace_serial_write().

Well, we as humans know it but the compiler doesn't, so it will perform implicit conversion. The format string will still print it as a byte. Any issues with that?

Best regards,
Bernhard

Quoted 2 lines

-- PMM

#19 lore raw

PATCH 9/12 util/fifo8: Make all read-only methods const-correct

In reply to Bernhard Beschow

Allows these methods to be used in const contexts, i.e. where the parent of the fifo itself is const. This is in particular useful for Rust code.

---
 include/qemu/fifo8.h | 10 +++++-----
 util/fifo8.c         | 10 +++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)
include/qemu/fifo8.h +5 -5
index 4f768d4ee3..6b476b404e 100644
@@ -71,7 +71,7 @@ uint8_t fifo8_pop(Fifo8 *fifo);
  *
  * Returns: The peeked data byte.
  */
-uint8_t fifo8_peek(Fifo8 *fifo);
+uint8_t fifo8_peek(const Fifo8 *fifo);
 
 /**
  * fifo8_pop_buf:
@@ -181,7 +181,7 @@ void fifo8_reset(Fifo8 *fifo);
  *
  * Returns: True if the fifo is empty, false otherwise.
  */
-bool fifo8_is_empty(Fifo8 *fifo);
+bool fifo8_is_empty(const Fifo8 *fifo);
 
 /**
  * fifo8_is_full:
@@ -191,7 +191,7 @@ bool fifo8_is_empty(Fifo8 *fifo);
  *
  * Returns: True if the fifo is full, false otherwise.
  */
-bool fifo8_is_full(Fifo8 *fifo);
+bool fifo8_is_full(const Fifo8 *fifo);
 
 /**
  * fifo8_num_free:
@@ -201,7 +201,7 @@ bool fifo8_is_full(Fifo8 *fifo);
  *
  * Returns: Number of free bytes.
  */
-uint32_t fifo8_num_free(Fifo8 *fifo);
+uint32_t fifo8_num_free(const Fifo8 *fifo);
 
 /**
  * fifo8_num_used:
@@ -211,7 +211,7 @@ uint32_t fifo8_num_free(Fifo8 *fifo);
  *
  * Returns: Number of used bytes.
  */
-uint32_t fifo8_num_used(Fifo8 *fifo);
+uint32_t fifo8_num_used(const Fifo8 *fifo);
 
 extern const VMStateDescription vmstate_fifo8;
 
util/fifo8.c +5 -5
index a26da66ad2..cc4f590b7a 100644
@@ -71,7 +71,7 @@ uint8_t fifo8_pop(Fifo8 *fifo)
     return ret;
 }
 
-uint8_t fifo8_peek(Fifo8 *fifo)
+uint8_t fifo8_peek(const Fifo8 *fifo)
 {
     assert(fifo->num > 0);
     return fifo->data[fifo->head];
@@ -157,22 +157,22 @@ void fifo8_drop(Fifo8 *fifo, uint32_t len)
     assert(len == 0);
 }
 
-bool fifo8_is_empty(Fifo8 *fifo)
+bool fifo8_is_empty(const Fifo8 *fifo)
 {
     return (fifo->num == 0);
 }
 
-bool fifo8_is_full(Fifo8 *fifo)
+bool fifo8_is_full(const Fifo8 *fifo)
 {
     return (fifo->num == fifo->capacity);
 }
 
-uint32_t fifo8_num_free(Fifo8 *fifo)
+uint32_t fifo8_num_free(const Fifo8 *fifo)
 {
     return fifo->capacity - fifo->num;
 }
 
-uint32_t fifo8_num_used(Fifo8 *fifo)
+uint32_t fifo8_num_used(const Fifo8 *fifo)
 {
     return fifo->num;
 }
Signature · 1 line
2.53.0
Reviewed-by on v1 9/12
#20 lore raw

Re: [PATCH 09/12] util/fifo8: Make all read-only methods const-correct

Quoted 10 lines · Bernhard Beschow

Allows these methods to be used in const contexts, i.e. where the parent of the fifo itself is const. This is in particular useful for Rust code.

---
 include/qemu/fifo8.h | 10 +++++-----
 util/fifo8.c         | 10 +++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)

thanks
-- PMM

#21 lore raw

PATCH 10/12 hw/arm/aspeed_ast27x0-{ssp, tsp}: Do not access SerialMM internals directly

In reply to Bernhard Beschow

SerialMM inherits from SysBusDevice and exposes the memory region by means of sysbus_mmio_get_region(). Use that in order to avoid accessing implementation details of SerialMM.

---
 hw/arm/aspeed_ast27x0-ssp.c | 7 ++++---
 hw/arm/aspeed_ast27x0-tsp.c | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)
hw/arm/aspeed_ast27x0-ssp.c +4 -3
index 9b12ba6743..8b84300e0f 100644
@@ -149,6 +149,7 @@ static void aspeed_soc_ast27x0ssp_realize(DeviceState *dev_soc, Error **errp)
     AspeedCoprocessorState *s = ASPEED_COPROCESSOR(dev_soc);
     AspeedCoprocessorClass *sc = ASPEED_COPROCESSOR_GET_CLASS(s);
     DeviceState *armv7m;
+    MemoryRegion *mr;
     g_autofree char *sdram_name = NULL;
     int i;
 
@@ -230,9 +231,9 @@ static void aspeed_soc_ast27x0ssp_realize(DeviceState *dev_soc, Error **errp)
     }
 
     /* UART */
-    memory_region_init_alias(&s->uart_alias, OBJECT(s), "uart.alias",
-                             &s->uart->serial.io, 0,
-                             memory_region_size(&s->uart->serial.io));
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(s->uart), 0);
+    memory_region_init_alias(&s->uart_alias, OBJECT(s), "uart.alias", mr, 0,
+                             memory_region_size(mr));
     memory_region_add_subregion(s->memory, sc->memmap[s->uart_dev],
                                 &s->uart_alias);
     /*
hw/arm/aspeed_ast27x0-tsp.c +4 -3
index e39d1dc171..e7c7b74491 100644
@@ -149,6 +149,7 @@ static void aspeed_soc_ast27x0tsp_realize(DeviceState *dev_soc, Error **errp)
     AspeedCoprocessorState *s = ASPEED_COPROCESSOR(dev_soc);
     AspeedCoprocessorClass *sc = ASPEED_COPROCESSOR_GET_CLASS(s);
     DeviceState *armv7m;
+    MemoryRegion *mr;
     g_autofree char *sdram_name = NULL;
     int i;
 
@@ -230,9 +231,9 @@ static void aspeed_soc_ast27x0tsp_realize(DeviceState *dev_soc, Error **errp)
     }
 
     /* UART */
-    memory_region_init_alias(&s->uart_alias, OBJECT(s), "uart.alias",
-                             &s->uart->serial.io, 0,
-                             memory_region_size(&s->uart->serial.io));
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(s->uart), 0);
+    memory_region_init_alias(&s->uart_alias, OBJECT(s), "uart.alias", mr, 0,
+                             memory_region_size(mr));
     memory_region_add_subregion(s->memory, sc->memmap[s->uart_dev],
                                 &s->uart_alias);
     /*
Signature · 1 line
2.53.0
Reviewed-by on v1 10/12
#22 lore raw

RE: [PATCH 10/12] hw/arm/aspeed_ast27x0-{ssp,tsp}: Do not access SerialMM internals directly

Quoted 69 lines

Subject: [PATCH 10/12] hw/arm/aspeed_ast27x0-{ssp,tsp}: Do not access SerialMM internals directly

SerialMM inherits from SysBusDevice and exposes the memory region by means of sysbus_mmio_get_region(). Use that in order to avoid accessing implementation details of SerialMM.

---
hw/arm/aspeed_ast27x0-ssp.c | 7 ++++--- hw/arm/aspeed_ast27x0-tsp.c |
7 ++++---

 2 files changed, 8 insertions(+), 6 deletions(-)
hw/arm/aspeed_ast27x0-ssp.c +4 -3
index 9b12ba6743..8b84300e0f 100644
@@ -149,6 +149,7 @@ static void aspeed_soc_ast27x0ssp_realize(DeviceState
 *dev_soc, Error **errp)
     AspeedCoprocessorState *s = ASPEED_COPROCESSOR(dev_soc);
     AspeedCoprocessorClass *sc = ASPEED_COPROCESSOR_GET_CLASS(s);
     DeviceState *armv7m;
+    MemoryRegion *mr;
     g_autofree char *sdram_name = NULL;
     int i;
@@ -230,9 +231,9 @@ static void aspeed_soc_ast27x0ssp_realize(DeviceState
 *dev_soc, Error **errp)
     }
 
     /* UART */
-    memory_region_init_alias(&s->uart_alias, OBJECT(s), "uart.alias",
-                             &s->uart->serial.io, 0,
-
 memory_region_size(&s->uart->serial.io));
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(s->uart), 0);
+    memory_region_init_alias(&s->uart_alias, OBJECT(s), "uart.alias", mr, 0,
+                             memory_region_size(mr));
     memory_region_add_subregion(s->memory, sc->memmap[s->uart_dev],
                                 &s->uart_alias);
     /*
hw/arm/aspeed_ast27x0-tsp.c +4 -3
index e39d1dc171..e7c7b74491 100644
@@ -149,6 +149,7 @@ static void aspeed_soc_ast27x0tsp_realize(DeviceState
 *dev_soc, Error **errp)
     AspeedCoprocessorState *s = ASPEED_COPROCESSOR(dev_soc);
     AspeedCoprocessorClass *sc = ASPEED_COPROCESSOR_GET_CLASS(s);
     DeviceState *armv7m;
+    MemoryRegion *mr;
     g_autofree char *sdram_name = NULL;
     int i;
@@ -230,9 +231,9 @@ static void aspeed_soc_ast27x0tsp_realize(DeviceState
 *dev_soc, Error **errp)
     }
 
     /* UART */
-    memory_region_init_alias(&s->uart_alias, OBJECT(s), "uart.alias",
-                             &s->uart->serial.io, 0,
-
 memory_region_size(&s->uart->serial.io));
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(s->uart), 0);
+    memory_region_init_alias(&s->uart_alias, OBJECT(s), "uart.alias", mr, 0,
+                             memory_region_size(mr));
     memory_region_add_subregion(s->memory, sc->memmap[s->uart_dev],
                                 &s->uart_alias);
     /*
Quoted 2 lines

--
2.53.0

Thanks,
Jamin

Reviewed-by on v1 10/12
#23 lore raw

Re: [PATCH 10/12] hw/arm/aspeed_ast27x0-{ssp,tsp}: Do not access SerialMM internals directly

In reply to Bernhard Beschow
Quoted 7 lines · Bernhard Beschow

SerialMM inherits from SysBusDevice and exposes the memory region by means of sysbus_mmio_get_region(). Use that in order to avoid accessing implementation details of SerialMM.

---

thanks
-- PMM

#24 lore raw

PATCH 11/12 hw/char: Have SerialState inherit from SysBusDevice

In reply to Bernhard Beschow

SerialState currently inherits just from DeviceState and serial devices use SerialState as an "IP block". Since DeviceState doesn't have an API to provide MMIO regions or IRQs, all serial devices access attributes internal to SerialState directly. Fix this by having SerialState inherit from SysBusDevice.

In addition, SerialState doesn't participate in the reset framework while SysBusDevice does. This allows for implementing reset functionality more idiomatically.

---
 include/hw/char/serial.h   |  4 ++--
 hw/char/diva-gsp.c         | 22 +++++++++++-----------
 hw/char/serial-isa.c       | 19 ++++++++++++-------
 hw/char/serial-mm.c        | 10 +++++-----
 hw/char/serial-pci-multi.c | 22 +++++++++++-----------
 hw/char/serial-pci.c       | 13 +++++++------
 hw/char/serial.c           |  5 ++++-
 7 files changed, 52 insertions(+), 43 deletions(-)
include/hw/char/serial.h +2 -2
index ea82ffac47..650fc27693 100644
@@ -26,7 +26,7 @@
 #ifndef HW_SERIAL_H
 #define HW_SERIAL_H
 
-#include "hw/core/qdev.h"
+#include "hw/core/sysbus.h"
 #include "chardev/char-fe.h"
 #include "system/memory.h"
 #include "qemu/fifo8.h"
@@ -35,7 +35,7 @@
 #define UART_FIFO_LENGTH    16      /* 16550A Fifo Length */
 
 struct SerialState {
-    DeviceState parent;
+    SysBusDevice parent;
 
     uint16_t divider;
     uint8_t rbr; /* receive register */
hw/char/diva-gsp.c +11 -11
index 280d0413c6..60ddc78752 100644
@@ -57,13 +57,13 @@ typedef struct PCIDivaSerialState {
 static void diva_pci_exit(PCIDevice *dev)
 {
     PCIDivaSerialState *pci = DO_UPCAST(PCIDivaSerialState, dev, dev);
-    SerialState *s;
     int i;
 
     for (i = 0; i < pci->ports; i++) {
-        s = pci->state + i;
-        qdev_unrealize(DEVICE(s));
-        memory_region_del_subregion(&pci->membar, &s->io);
+        SysBusDevice *sbd = SYS_BUS_DEVICE(pci->state + i);
+        qdev_unrealize(DEVICE(sbd));
+        memory_region_del_subregion(&pci->membar,
+                                    sysbus_mmio_get_region(sbd, 0));
         g_free(pci->name[i]);
     }
     qemu_free_irqs(pci->irqs, pci->ports);
@@ -118,7 +118,6 @@ static void diva_pci_realize(PCIDevice *dev, Error **errp)
 {
     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
     PCIDivaSerialState *pci = DO_UPCAST(PCIDivaSerialState, dev, dev);
-    SerialState *s;
     struct diva_info di = diva_get_diva_info(pc);
     size_t i, offset = 0;
     size_t portmask = di.omask;
@@ -130,22 +129,23 @@ static void diva_pci_realize(PCIDevice *dev, Error **errp)
     pci->irqs = qemu_allocate_irqs(multi_serial_irq_mux, pci, di.nports);
 
     for (i = 0; i < di.nports; i++) {
-        s = pci->state + i;
-        if (!qdev_realize(DEVICE(s), NULL, errp)) {
+        SysBusDevice *sbd = SYS_BUS_DEVICE(pci->state + i);
+        if (!sysbus_realize(sbd, errp)) {
             diva_pci_exit(dev);
             return;
         }
-        s->irq = pci->irqs[i];
+        sysbus_connect_irq(sbd, 0, pci->irqs[i]);
         pci->name[i] = g_strdup_printf("uart #%zu", i + 1);
-        memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s,
-                              pci->name[i], 8);
+        memory_region_init_io(sysbus_mmio_get_region(sbd, 0), OBJECT(pci),
+                              &serial_io_ops, sbd, pci->name[i], 8);
 
         /* calculate offset of given port based on bitmask */
         while ((portmask & BIT(0)) == 0) {
             offset += 8;
             portmask >>= 1;
         }
-        memory_region_add_subregion(&pci->membar, offset, &s->io);
+        memory_region_add_subregion(&pci->membar, offset,
+                                    sysbus_mmio_get_region(sbd, 0));
         offset += 8;
         portmask >>= 1;
         pci->ports++;
hw/char/serial-isa.c +12 -7
index a4be0492c5..d3a401f56c 100644
@@ -58,7 +58,7 @@ static void serial_isa_realizefn(DeviceState *dev, Error **errp)
     static int index;
     ISADevice *isadev = ISA_DEVICE(dev);
     ISASerialState *isa = ISA_SERIAL(dev);
-    SerialState *s = &isa->state;
+    SysBusDevice *sbd = SYS_BUS_DEVICE(&isa->state);
 
     if (isa->index == -1) {
         isa->index = index;
@@ -76,12 +76,13 @@ static void serial_isa_realizefn(DeviceState *dev, Error **errp)
     }
     index++;
 
-    s->irq = isa_get_irq(isadev, isa->isairq);
-    qdev_realize(DEVICE(s), NULL, errp);
+    sysbus_realize(sbd, errp);
+    sysbus_connect_irq(sbd, 0, isa_get_irq(isadev, isa->isairq));
     qdev_set_legacy_instance_id(dev, isa->iobase, 3);
 
-    memory_region_init_io(&s->io, OBJECT(isa), &serial_io_ops, s, "serial", 8);
-    isa_register_ioport(isadev, &s->io, isa->iobase);
+    memory_region_init_io(sysbus_mmio_get_region(sbd, 0), OBJECT(isa),
+                          &serial_io_ops, sbd, "serial", 8);
+    isa_register_ioport(isadev, sysbus_mmio_get_region(sbd, 0), isa->iobase);
 }
 
 static void serial_isa_build_aml(AcpiDevAmlIf *adev, Aml *scope)
@@ -188,13 +189,17 @@ void serial_hds_isa_init(ISABus *bus, int from, int to)
 void isa_serial_set_iobase(ISADevice *serial, hwaddr iobase)
 {
     ISASerialState *s = ISA_SERIAL(serial);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(&s->state);
 
     serial->ioport_id = iobase;
     s->iobase = iobase;
-    memory_region_set_address(&s->state.io, s->iobase);
+    memory_region_set_address(sysbus_mmio_get_region(sbd, 0), s->iobase);
 }
 
 void isa_serial_set_enabled(ISADevice *serial, bool enabled)
 {
-    memory_region_set_enabled(&ISA_SERIAL(serial)->state.io, enabled);
+    ISASerialState *s = ISA_SERIAL(serial);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(&s->state);
+
+    memory_region_set_enabled(sysbus_mmio_get_region(sbd, 0), enabled);
 }
hw/char/serial-mm.c +5 -5
index 0e0be26fa9..de11498278 100644
@@ -71,17 +71,17 @@ static const MemoryRegionOps serial_mm_ops[] = {
 static void serial_mm_realize(DeviceState *dev, Error **errp)
 {
     SerialMM *smm = SERIAL_MM(dev);
-    SerialState *s = &smm->serial;
+    SysBusDevice *sbd = SYS_BUS_DEVICE(&smm->serial);
 
-    if (!qdev_realize(DEVICE(s), NULL, errp)) {
+    if (!sysbus_realize(sbd, errp)) {
         return;
     }
 
-    memory_region_init_io(&s->io, OBJECT(dev),
+    memory_region_init_io(sysbus_mmio_get_region(sbd, 0), OBJECT(dev),
                           &serial_mm_ops[smm->endianness], smm, "serial",
                           8 << smm->regshift);
-    sysbus_init_mmio(SYS_BUS_DEVICE(smm), &s->io);
-    sysbus_init_irq(SYS_BUS_DEVICE(smm), &smm->serial.irq);
+    sysbus_init_mmio(SYS_BUS_DEVICE(smm), sysbus_mmio_get_region(sbd, 0));
+    sysbus_pass_irq(SYS_BUS_DEVICE(smm), SYS_BUS_DEVICE(sbd));
 }
 
 static const VMStateDescription vmstate_serial_mm = {
hw/char/serial-pci-multi.c +11 -11
index 17796b93dd..7545ab7425 100644
@@ -51,13 +51,13 @@ typedef struct PCIMultiSerialState {
 static void multi_serial_pci_exit(PCIDevice *dev)
 {
     PCIMultiSerialState *pci = DO_UPCAST(PCIMultiSerialState, dev, dev);
-    SerialState *s;
     int i;
 
     for (i = 0; i < pci->ports; i++) {
-        s = pci->state + i;
-        qdev_unrealize(DEVICE(s));
-        memory_region_del_subregion(&pci->iobar, &s->io);
+        SysBusDevice *sbd = SYS_BUS_DEVICE(pci->state + i);
+        qdev_unrealize(DEVICE(sbd));
+        memory_region_del_subregion(&pci->iobar,
+                                    sysbus_mmio_get_region(sbd, 0));
         g_free(pci->name[i]);
     }
 }
@@ -93,7 +93,6 @@ static void multi_serial_pci_realize(PCIDevice *dev, Error **errp)
 {
     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
     PCIMultiSerialState *pci = DO_UPCAST(PCIMultiSerialState, dev, dev);
-    SerialState *s;
     size_t i, nports = multi_serial_get_port_count(pc);
 
     pci->dev.config[PCI_CLASS_PROG] = 2; /* 16550 compatible */
@@ -102,16 +101,17 @@ static void multi_serial_pci_realize(PCIDevice *dev, Error **errp)
     pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &pci->iobar);
 
     for (i = 0; i < nports; i++) {
-        s = pci->state + i;
-        if (!qdev_realize(DEVICE(s), NULL, errp)) {
+        SysBusDevice *sbd = SYS_BUS_DEVICE(pci->state + i);
+        if (!sysbus_realize(sbd, errp)) {
             multi_serial_pci_exit(dev);
             return;
         }
-        s->irq = &pci->irqs[i];
+        sysbus_connect_irq(sbd, 0, &pci->irqs[i]);
         pci->name[i] = g_strdup_printf("uart #%zu", i + 1);
-        memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s,
-                              pci->name[i], 8);
-        memory_region_add_subregion(&pci->iobar, 8 * i, &s->io);
+        memory_region_init_io(sysbus_mmio_get_region(sbd, 0), OBJECT(pci),
+                              &serial_io_ops, sbd, pci->name[i], 8);
+        memory_region_add_subregion(&pci->iobar, 8 * i,
+                                    sysbus_mmio_get_region(sbd, 0));
         pci->ports++;
     }
 }
hw/char/serial-pci.c +7 -6
index d8cacc9085..10a99592b6 100644
@@ -46,18 +46,20 @@ OBJECT_DECLARE_SIMPLE_TYPE(PCISerialState, PCI_SERIAL)
 static void serial_pci_realize(PCIDevice *dev, Error **errp)
 {
     PCISerialState *pci = DO_UPCAST(PCISerialState, dev, dev);
-    SerialState *s = &pci->state;
+    SysBusDevice *sbd = SYS_BUS_DEVICE(&pci->state);
 
-    if (!qdev_realize(DEVICE(s), NULL, errp)) {
+    if (!sysbus_realize(sbd, errp)) {
         return;
     }
 
     pci->dev.config[PCI_CLASS_PROG] = 2; /* 16550 compatible */
     pci->dev.config[PCI_INTERRUPT_PIN] = 1;
-    s->irq = pci_allocate_irq(&pci->dev);
+    sysbus_connect_irq(sbd, 0, pci_allocate_irq(&pci->dev));
 
-    memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s, "serial", 8);
-    pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
+    memory_region_init_io(sysbus_mmio_get_region(sbd, 0), OBJECT(pci),
+                          &serial_io_ops, sbd, "serial", 8);
+    pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
+                     sysbus_mmio_get_region(sbd, 0));
 }
 
 static void serial_pci_exit(PCIDevice *dev)
@@ -66,7 +68,6 @@ static void serial_pci_exit(PCIDevice *dev)
     SerialState *s = &pci->state;
 
     qdev_unrealize(DEVICE(s));
-    qemu_free_irq(s->irq);
 }
 
 static const VMStateDescription vmstate_pci_serial = {
hw/char/serial.c +4 -1
index 0f3469a1e8..c58337bd20 100644
@@ -934,6 +934,9 @@ static void serial_realize(DeviceState *dev, Error **errp)
                              serial_event, serial_be_change, s, NULL, true);
     fifo8_create(&s->recv_fifo, UART_FIFO_LENGTH);
     fifo8_create(&s->xmit_fifo, UART_FIFO_LENGTH);
+
+    sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->io);
+    sysbus_init_irq(SYS_BUS_DEVICE(s), &s->irq);
 }
 
 static void serial_unrealize(DeviceState *dev)
@@ -984,7 +987,7 @@ static void serial_class_init(ObjectClass *klass, const void *data)
 
 static const TypeInfo serial_info = {
     .name = TYPE_SERIAL,
-    .parent = TYPE_DEVICE,
+    .parent = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(SerialState),
     .class_init = serial_class_init,
 };
Signature · 1 line
2.53.0
#25 lore raw

Re: [PATCH 11/12] hw/char: Have SerialState inherit from SysBusDevice

Quoted 19 lines · Bernhard Beschow

SerialState currently inherits just from DeviceState and serial devices use SerialState as an "IP block". Since DeviceState doesn't have an API to provide MMIO regions or IRQs, all serial devices access attributes internal to SerialState directly. Fix this by having SerialState inherit from SysBusDevice.

In addition, SerialState doesn't participate in the reset framework while SysBusDevice does. This allows for implementing reset functionality more idiomatically.

hw/char/serial-isa.c +0 -0
@@ -76,12 +76,13 @@ static void serial_isa_realizefn(DeviceState *dev, Error **errp)
     }
     index++;
 

hw/char/serial-isa.c static void serial_isa_realizefn(DeviceState *dev, Error **errp) · line 84

Quoted 10 lines
hw/char/serial-isa.c +4 -4
-    s->irq = isa_get_irq(isadev, isa->isairq);
-    qdev_realize(DEVICE(s), NULL, errp);
+    sysbus_realize(sbd, errp);
+    sysbus_connect_irq(sbd, 0, isa_get_irq(isadev, isa->isairq));
     qdev_set_legacy_instance_id(dev, isa->iobase, 3);
 
-    memory_region_init_io(&s->io, OBJECT(isa), &serial_io_ops, s, "serial", 8);
-    isa_register_ioport(isadev, &s->io, isa->iobase);
+    memory_region_init_io(sysbus_mmio_get_region(sbd, 0), OBJECT(isa),
+                          &serial_io_ops, sbd, "serial", 8);

This looks very odd, because sysbus_mmio_get_region() is supposed to be "the sysbus device provides an MR it has initialized, and this is how you get hold of it". But here we're taking an uninitialized thing and initializing it by calling memory_region_init_io(). It works because it's just returning a pointer into the internal memory, but it's still making assumptions about the internals of the device just as much as directly accessing s->io.

Maybe this is worthwhile for the benefit of getting reset handling automatically into the reset tree.

thanks
-- PMM

#26 lore raw

Re: [PATCH 11/12] hw/char: Have SerialState inherit from SysBusDevice

Am 3. März 2026 11:41:52 UTC schrieb Peter Maydell <[email protected]>:

Quoted 39 lines · Bernhard Beschow
Quoted 19 lines

SerialState currently inherits just from DeviceState and serial devices use SerialState as an "IP block". Since DeviceState doesn't have an API to provide MMIO regions or IRQs, all serial devices access attributes internal to SerialState directly. Fix this by having SerialState inherit from SysBusDevice.

In addition, SerialState doesn't participate in the reset framework while SysBusDevice does. This allows for implementing reset functionality more idiomatically.

hw/char/serial-isa.c +0 -0
@@ -76,12 +76,13 @@ static void serial_isa_realizefn(DeviceState *dev, Error **errp)
     }
     index++;
 

hw/char/serial-isa.c static void serial_isa_realizefn(DeviceState *dev, Error **errp) · line 84

Quoted 10 lines
hw/char/serial-isa.c +4 -4
-    s->irq = isa_get_irq(isadev, isa->isairq);
-    qdev_realize(DEVICE(s), NULL, errp);
+    sysbus_realize(sbd, errp);
+    sysbus_connect_irq(sbd, 0, isa_get_irq(isadev, isa->isairq));
     qdev_set_legacy_instance_id(dev, isa->iobase, 3);
 
-    memory_region_init_io(&s->io, OBJECT(isa), &serial_io_ops, s, "serial", 8);
-    isa_register_ioport(isadev, &s->io, isa->iobase);
+    memory_region_init_io(sysbus_mmio_get_region(sbd, 0), OBJECT(isa),
+                          &serial_io_ops, sbd, "serial", 8);

This looks very odd, because sysbus_mmio_get_region() is supposed to be "the sysbus device provides an MR it has initialized, and this is how you get hold of it". But here we're taking an uninitialized thing and initializing it by calling memory_region_init_io(). It works because it's just returning a pointer into the internal memory, but it's still making assumptions about the internals of the device just as much as directly accessing s->io.

Quoted 2 lines

Maybe this is worthwhile for the benefit of getting reset handling automatically into the reset tree.

I think the logic can be folded into TYPE_SERIAL. I'll send a v2.

Best regards,
Bernhard

Quoted 3 lines

thanks
-- PMM

#27 lore raw

PATCH 12/12 hw/char/serial: Plug into reset framework

In reply to Bernhard Beschow

Now that SerialState inherits from SysBusDevice we can take advantage of having the reset method called by the bus hierarchy rather than having to plug and unplug it manually.

---
 hw/char/serial.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
hw/char/serial.c +3 -6
index c58337bd20..c4537e5764 100644
@@ -31,7 +31,6 @@
 #include "chardev/char-serial.h"
 #include "qapi/error.h"
 #include "qemu/timer.h"
-#include "system/reset.h"
 #include "system/runstate.h"
 #include "qemu/error-report.h"
 #include "trace.h"
@@ -853,9 +852,9 @@ const VMStateDescription vmstate_serial = {
     }
 };
 
-static void serial_reset(void *opaque)
+static void serial_reset(DeviceState *dev)
 {
-    SerialState *s = opaque;
+    SerialState *s = SERIAL(dev);
 
     if (s->watch_tag > 0) {
         g_source_remove(s->watch_tag);
@@ -928,7 +927,6 @@ static void serial_realize(DeviceState *dev, Error **errp)
     s->modem_status_poll = timer_new_ns(QEMU_CLOCK_VIRTUAL, (QEMUTimerCB *) serial_update_msl, s);
 
     s->fifo_timeout_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, (QEMUTimerCB *) fifo_timeout_int, s);
-    qemu_register_reset(serial_reset, s);
 
     qemu_chr_fe_set_handlers(&s->chr, serial_can_receive1, serial_receive1,
                              serial_event, serial_be_change, s, NULL, true);
@@ -951,8 +949,6 @@ static void serial_unrealize(DeviceState *dev)
 
     fifo8_destroy(&s->recv_fifo);
     fifo8_destroy(&s->xmit_fifo);
-
-    qemu_unregister_reset(serial_reset, s);
 }
 
 const MemoryRegionOps serial_io_ops = {
@@ -982,6 +978,7 @@ static void serial_class_init(ObjectClass *klass, const void *data)
     dc->user_creatable = false;
     dc->realize = serial_realize;
     dc->unrealize = serial_unrealize;
+    device_class_set_legacy_reset(dc, serial_reset);
     device_class_set_props(dc, serial_properties);
 }
 
Signature · 1 line
2.53.0