Make serial IO a proper sysbus device, similar to serial MM.

Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com>
---
 hw/char/serial.c         | 59 ++++++++++++++++++++++++++++++++--------
 include/hw/char/serial.h | 13 +++++++--
 2 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/hw/char/serial.c b/hw/char/serial.c
index e0f1a99e8b..164146ede8 100644
--- a/hw/char/serial.c
+++ b/hw/char/serial.c
@@ -985,22 +985,58 @@ const MemoryRegionOps serial_io_ops = {
     .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
-SerialState *serial_init(int base, qemu_irq irq, int baudbase,
-                         Chardev *chr, MemoryRegion *system_io)
+static void serial_io_realize(DeviceState *dev, Error **errp)
 {
-    DeviceState *dev = DEVICE(object_new(TYPE_SERIAL));
-    SerialState *s = SERIAL(dev);
+    SerialIO *sio = SERIAL_IO(dev);
+    SerialState *s = &sio->serial;
 
-    s->irq = irq;
-    qdev_prop_set_uint32(dev, "baudbase", baudbase);
-    qdev_prop_set_chr(dev, "chardev", chr);
-    qdev_set_legacy_instance_id(dev, base, 2);
-    qdev_init_nofail(dev);
+    qdev_init_nofail(DEVICE(s));
 
     memory_region_init_io(&s->io, NULL, &serial_io_ops, s, "serial", 8);
-    memory_region_add_subregion(system_io, base, &s->io);
+    sysbus_init_irq(SYS_BUS_DEVICE(sio), &s->irq);
+}
+
+static void serial_io_class_init(ObjectClass *klass, void* data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = serial_io_realize;
+    /* No dc->vmsd: class has no migratable state */
+}
+
+static void serial_io_instance_init(Object *o)
+{
+    SerialIO *sio = SERIAL_IO(o);
+
+    object_initialize_child(o, "serial", &sio->serial, sizeof(sio->serial),
+                            TYPE_SERIAL, &error_abort, NULL);
+
+    qdev_alias_all_properties(DEVICE(&sio->serial), o);
+}
+
+
+static const TypeInfo serial_io_info = {
+    .name = TYPE_SERIAL_IO,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(SerialIO),
+    .instance_init = serial_io_instance_init,
+    .class_init = serial_io_class_init,
+};
+
+SerialIO *serial_init(int base, qemu_irq irq, int baudbase,
+                         Chardev *chr, MemoryRegion *system_io)
+{
+    SerialIO *sio = SERIAL_IO(qdev_create(NULL, TYPE_SERIAL_IO));
+
+    qdev_prop_set_uint32(DEVICE(sio), "baudbase", baudbase);
+    qdev_prop_set_chr(DEVICE(sio), "chardev", chr);
+    qdev_set_legacy_instance_id(DEVICE(sio), base, 2);
+    qdev_init_nofail(DEVICE(sio));
+
+    sysbus_connect_irq(SYS_BUS_DEVICE(sio), 0, irq);
+    memory_region_add_subregion(system_io, base, &sio->serial.io);
 
-    return s;
+    return sio;
 }
 
 static Property serial_properties[] = {
@@ -1142,6 +1178,7 @@ static const TypeInfo serial_mm_info = {
 static void serial_register_types(void)
 {
     type_register_static(&serial_info);
+    type_register_static(&serial_io_info);
     type_register_static(&serial_mm_info);
 }
 
diff --git a/include/hw/char/serial.h b/include/hw/char/serial.h
index d586924937..d356ba838c 100644
--- a/include/hw/char/serial.h
+++ b/include/hw/char/serial.h
@@ -88,6 +88,12 @@ typedef struct SerialMM {
     uint8_t endianness;
 } SerialMM;
 
+typedef struct SerialIO {
+    SysBusDevice parent;
+
+    SerialState serial;
+} SerialIO;
+
 extern const VMStateDescription vmstate_serial;
 extern const MemoryRegionOps serial_io_ops;
 
@@ -99,8 +105,11 @@ void serial_set_frequency(SerialState *s, uint32_t 
frequency);
 #define TYPE_SERIAL_MM "serial-mm"
 #define SERIAL_MM(s) OBJECT_CHECK(SerialMM, (s), TYPE_SERIAL_MM)
 
-SerialState *serial_init(int base, qemu_irq irq, int baudbase,
-                         Chardev *chr, MemoryRegion *system_io);
+#define TYPE_SERIAL_IO "serial-io"
+#define SERIAL_IO(s) OBJECT_CHECK(SerialIO, (s), TYPE_SERIAL_IO)
+
+SerialIO *serial_init(int base, qemu_irq irq, int baudbase,
+                      Chardev *chr, MemoryRegion *system_io);
 SerialMM *serial_mm_init(MemoryRegion *address_space,
                          hwaddr base, int regshift,
                          qemu_irq irq, int baudbase,
-- 
2.24.0


Reply via email to