[RFC PATCH v2 018/137] hw/sh4: Give onboard devices a QOM parent

Alexander Graf <[email protected]>
Newsgroups org.nongnu.qemu-arm,org.nongnu.qemu-devel,org.nongnu.qemu-riscv
Message-ID <[email protected]>
Convert the *_orphan() device-creation calls in hw/sh4 to the new
parented API introduced earlier in this series, so every onboard
device gets a stable path in the composition tree instead of landing
in /machine/unattached with an unstable device[N] name.

The parent for each device is the object that owns its lifetime: the
machine for board-created devices, the containing device for
composite children.  Names follow existing QOM conventions.

Per-site rationale (reviewers: dispute the modeling here):

hw/sh4/r2d.c:254 | cpu_create | OBJECT(machine) | "cpu" | board init: single CPU owned by machine
hw/sh4/r2d.c:269 | qdev_new | OBJECT(machine) | "pci-host" | board init: SH PCI host bridge owned by machine
hw/sh4/r2d.c:280 | qdev_new | OBJECT(machine) | "sm501" | board init: on-board SM501 companion chip owned by machine
hw/sh4/r2d.c:292 | qdev_new | OBJECT(machine) | "ide" | board init: on-board CF/IDE controller owned by machine
hw/sh4/r2d.c:323 | usb_create_simple | OBJECT(BUS(usb_bus)->parent) | "usb-kbd" | keyboard hangs off the SM501 OHCI controller that owns the USB bus
hw/sh4/sh7750.c:765 | qdev_new | parent | "sci" | SoC helper: threaded Object *parent from board; on-chip SCI UART
hw/sh4/sh7750.c:782 | qdev_new | parent | "scif" | SoC helper: threaded Object *parent from board; on-chip SCIF UART

Link: https://lore.kernel.org/qemu-devel/[email protected]/
AI-used-for: code (refactoring)
Signed-off-by: Alexander Graf <[email protected]>
---
 hw/sh4/r2d.c        | 20 +++++++++++---------
 hw/sh4/sh7750.c     | 11 ++++++-----
 include/hw/sh4/sh.h |  3 ++-
 3 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/hw/sh4/r2d.c b/hw/sh4/r2d.c
index 9f313a77cb..c0f8cad335 100644
--- a/hw/sh4/r2d.c
+++ b/hw/sh4/r2d.c
@@ -251,7 +251,8 @@ static void r2d_init(MachineState *machine)
     USBBus *usb_bus;
     r2d_fpga_t *fpga;
 
-    cpu = SUPERH_CPU(cpu_create_orphan(machine->cpu_type));
+    cpu = SUPERH_CPU(cpu_create(OBJECT(machine), "cpu",
+                                machine->cpu_type));
     env = &cpu->env;
 
     reset_info = g_new0(ResetData, 1);
@@ -263,12 +264,12 @@ static void r2d_init(MachineState *machine)
     memory_region_init_ram(sdram, NULL, "r2d.sdram", SDRAM_SIZE, &error_fatal);
     memory_region_add_subregion(address_space_mem, SDRAM_BASE, sdram);
     /* Register peripherals */
-    s = sh7750_init(cpu, address_space_mem);
+    s = sh7750_init(OBJECT(machine), cpu, address_space_mem);
     fpga = r2d_fpga_init(address_space_mem, 0x04000000, sh7750_irl(s));
 
-    dev = qdev_new_orphan("sh_pci");
+    dev = qdev_new(OBJECT(machine), "pci-host", "sh_pci");
     busdev = SYS_BUS_DEVICE(dev);
-    sysbus_realize_and_unref(busdev, &error_fatal);
+    sysbus_realize(busdev, &error_fatal);
     pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci"));
     sysbus_mmio_map(busdev, 0, P4ADDR(0x1e200000));
     sysbus_mmio_map(busdev, 1, A7ADDR(0x1e200000));
@@ -277,23 +278,23 @@ static void r2d_init(MachineState *machine)
     sysbus_connect_irq(busdev, 2, &fpga->irq[PCI_INTC]);
     sysbus_connect_irq(busdev, 3, &fpga->irq[PCI_INTD]);
 
-    dev = qdev_new_orphan("sysbus-sm501");
+    dev = qdev_new(OBJECT(machine), "sm501", "sysbus-sm501");
     busdev = SYS_BUS_DEVICE(dev);
     qdev_prop_set_uint32(dev, "vram-size", SM501_VRAM_SIZE);
     qdev_prop_set_uint64(dev, "dma-offset", 0x10000000);
     qdev_prop_set_chr(dev, "chardev", serial_hd(2));
-    sysbus_realize_and_unref(busdev, &error_fatal);
+    sysbus_realize(busdev, &error_fatal);
     sysbus_mmio_map(busdev, 0, 0x10000000);
     sysbus_mmio_map(busdev, 1, 0x13e00000);
     sysbus_connect_irq(busdev, 0, &fpga->irq[SM501]);
 
     /* onboard CF (True IDE mode, Master only). */
     dinfo = drive_get(IF_IDE, 0, 0);
-    dev = qdev_new_orphan("mmio-ide");
+    dev = qdev_new(OBJECT(machine), "ide", "mmio-ide");
     busdev = SYS_BUS_DEVICE(dev);
     sysbus_connect_irq(busdev, 0, &fpga->irq[CF_IDE]);
     qdev_prop_set_uint32(dev, "shift", 1);
-    sysbus_realize_and_unref(busdev, &error_fatal);
+    sysbus_realize(busdev, &error_fatal);
     sysbus_mmio_map(busdev, 0, 0x14001000);
     sysbus_mmio_map(busdev, 1, 0x1400080c);
     mmio_ide_init_drives(dev, dinfo, NULL);
@@ -320,7 +321,8 @@ static void r2d_init(MachineState *machine)
     /* USB keyboard */
     usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
                                                       &error_abort));
-    usb_create_simple_orphan(usb_bus, "usb-kbd");
+    usb_create_simple(OBJECT(BUS(usb_bus)->parent), "usb-kbd",
+                      usb_bus, "usb-kbd");
 
     /* Todo: register on board registers */
     memset(&boot_params, 0, sizeof(boot_params));
diff --git a/hw/sh4/sh7750.c b/hw/sh4/sh7750.c
index 3eb47bc660..ff9724fadf 100644
--- a/hw/sh4/sh7750.c
+++ b/hw/sh4/sh7750.c
@@ -710,7 +710,8 @@ static const MemoryRegionOps sh7750_mmct_ops = {
     .endianness = DEVICE_NATIVE_ENDIAN,
 };
 
-SH7750State *sh7750_init(SuperHCPU *cpu, MemoryRegion *sysmem)
+SH7750State *sh7750_init(Object *parent, SuperHCPU *cpu,
+                         MemoryRegion *sysmem)
 {
     SH7750State *s;
     DeviceState *dev;
@@ -762,11 +763,11 @@ SH7750State *sh7750_init(SuperHCPU *cpu, MemoryRegion *sysmem)
     cpu->env.intc_handle = &s->intc;
 
     /* SCI */
-    dev = qdev_new_orphan(TYPE_SH_SERIAL);
+    dev = qdev_new(parent, "sci", TYPE_SH_SERIAL);
     dev->id = g_strdup("sci");
     qdev_prop_set_chr(dev, "chardev", serial_hd(0));
     sb = SYS_BUS_DEVICE(dev);
-    sysbus_realize_and_unref(sb, &error_fatal);
+    sysbus_realize(sb, &error_fatal);
     sysbus_mmio_map(sb, 0, 0xffe00000);
     alias = g_malloc(sizeof(*alias));
     mr = sysbus_mmio_get_region(sb, 0);
@@ -779,12 +780,12 @@ SH7750State *sh7750_init(SuperHCPU *cpu, MemoryRegion *sysmem)
     qdev_connect_gpio_out_named(dev, "tei", 0, s->intc.irqs[SCI1_TEI]);
 
     /* SCIF */
-    dev = qdev_new_orphan(TYPE_SH_SERIAL);
+    dev = qdev_new(parent, "scif", TYPE_SH_SERIAL);
     dev->id = g_strdup("scif");
     qdev_prop_set_chr(dev, "chardev", serial_hd(1));
     qdev_prop_set_uint8(dev, "features", SH_SERIAL_FEAT_SCIF);
     sb = SYS_BUS_DEVICE(dev);
-    sysbus_realize_and_unref(sb, &error_fatal);
+    sysbus_realize(sb, &error_fatal);
     sysbus_mmio_map(sb, 0, 0xffe80000);
     alias = g_malloc(sizeof(*alias));
     mr = sysbus_mmio_get_region(sb, 0);
diff --git a/include/hw/sh4/sh.h b/include/hw/sh4/sh.h
index c82feef8d0..b7ec234212 100644
--- a/include/hw/sh4/sh.h
+++ b/include/hw/sh4/sh.h
@@ -36,7 +36,8 @@
 /* sh7750.c */
 struct SH7750State;
 
-struct SH7750State *sh7750_init(SuperHCPU *cpu, MemoryRegion *sysmem);
+struct SH7750State *sh7750_init(Object *parent, SuperHCPU *cpu,
+                                MemoryRegion *sysmem);
 
 #define TYPE_SH_SERIAL "sh-serial"
 #define SH_SERIAL_FEAT_SCIF (1 << 0)
-- 
2.47.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.