Skip to contentSkip to conversation

hw/qdev: Split 'wiring' phase from 'realize'

RFC PATCH In review Philippe Mathieu-Daudé 3 participants 10 messages Feb 9, 2024

lore
Share & tools
lore
#0 lore raw

cover hw/qdev: Split 'wiring' phase from 'realize'

Hi,

Various issues related to implementing dynamic machines have been documented in [1].

We are trying to understand what means "a qdev is realized". One explanation was "the device is guest visible"; however many devices are realized before being mapped, thus are not "guest visible". Some devices map / wire their IRQs before being realized (such ISA devices). There is a need for devices to be "automatically" mapped/wired (see [2]) such CLI-created devices, but this apply generically to dynamic machines.

Currently the device creation steps are expected to roughly be:

(external use) (QDev core) (Device Impl)
~~~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~~

                               INIT enter
                   ----->
                         +----------------------+
                         |    Allocate state    |
                         +----------------------+
                                                 ----->
                                                        +---------------------+
                                                        | INIT children       |
                                                        |                     |
                                                        | Alias children properties
                                                        |                     |
                                                        | Expose properties   |
                                INIT exit               +---------------------+
                   <-----------------------------------
 +----------------+
 | set properties |
 |                |
 | set ClkIn      |
 +----------------+          REALIZE enter
                   ---------------------------------->
                                                       +----------------------+
                                                       | Use config properties|
                                                       |                      |
                                                       | Realize children     |
                                                       |                      |
                                                       | Init GPIOs/IRQs      |
                                                       |                      |
                                                       | Init MemoryRegions   |
                                                       +----------------------+
                               REALIZE exit
                   <-----------------------------------                        ----  "realized" / "guest visible"
+-----------------+
| Explicit wiring:|
|   IRQs          |
|   I/O / Mem     |
|   ClkOut        |
+-----------------+             RESET enter
                    --------------------------------->
                                                       +----------------------+
                                                       | Reset default values |
                                                       +----------------------+

But as mentioned, various devices "wire" parts before they exit the "realize" step.
In order to clarify, I'm trying to enforce what can be done *before* and *after* realization.

*after* a device is expected to be stable (no more configurable) and fully usable.

To be able to use internal/auto wiring (such ISA devices) and keep the current external/explicit wiring, I propose to add an extra "internal wiring" step, happening after the REALIZE step as:

(external use) (QDev core) (Device Impl)
~~~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~~

                               INIT enter
                   ----->
                         +----------------------+
                         |    Allocate state    |
                         +----------------------+
                                                 ----->
                                                        +---------------------+
                                                        | INIT children       |
                                                        |                     |
                                                        | Alias children properties
                                                        |                     |
                                                        | Expose properties   |
                                INIT exit               +---------------------+
                   <-----------------------------------
 +----------------+
 | set properties |
 |                |
 | set ClkIn      |
 +----------------+          REALIZE enter
                   ---------------------------------->
                                                       +----------------------+
                                                       | Use config properties|
                                                       |                      |
                                                       | Realize children     |
                                                       |                      |
                                                       | Init GPIOs/IRQs      |
                                                       |                      |
                                                       | Init MemoryRegions   |
                                                       +----------------------+
                               REALIZE exit       <---
                         +----------------------+
                         | Internal auto wiring |
                         |   IRQs               |  (i.e. ISA bus)
                         |   I/O / Mem          |
                         |   ClkOut             |
                         +----------------------+
                    <---                                                       ----  "realized"
+-----------------+
| External wiring:|
|   IRQs          |
|   I/O / Mem     |
|   ClkOut        |
+-----------------+             RESET enter                                    ----  "guest visible"
                    --------------------------------->
                                                       +----------------------+
                                                       | Reset default values |
                                                       +----------------------+

The "realized" point is not changed. "guest visible" concept only occurs *after* wiring, just before the reset phase.

This series introduces the DeviceClass::wire handler within qdev core realization code, and convert devices using the implicit wiring to using that explicit handler.

QDev API assertions patches will be posted later as another series.

Thoughts?

Regards,

Phil.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Cc Eduardo Habkost <[email protected]>
Cc Markus Armbruster <[email protected]>' --cc '
Cc Edgar E. Iglesias <[email protected]>'
Cc Richard Henderson <[email protected]>
Cc Mark Cave-Ayland <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Bernhard Beschow <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Cédric Le Goater <[email protected]>
Cc Luc Michel <[email protected]>
Cc Zhao Liu <[email protected]>
Cc Gustavo Romero <[email protected]>

Philippe Mathieu-Daudé (7):
hw/qdev: Introduce DeviceClass::[un]wire() handlers hw/input/pckbd: Connect i8042 GPIOs once mouse/keyboard are realized hw/ide/cmd646: Configure IDE bus IRQs after realization hw/ide/sii3112: Configure IDE bus IRQs after realization hw/ide/via: Configure IDE bus IRQs after realization hw/intc/mips_gic: Initialize IRQ array once device is realized hw/misc/mac_via: Have VIA1 child access parent IRQ once realized

 include/hw/qdev-core.h |  8 +++++++-
 hw/core/qdev.c         | 21 ++++++++++++++++++++-
 hw/ide/cmd646.c        | 12 +++++++++++-
 hw/ide/sii3112.c       | 10 ++++++++++
 hw/ide/via.c           | 10 ++++++++++
 hw/input/pckbd.c       | 38 +++++++++++++++++++++++++++-----------
 hw/intc/mips_gic.c     | 11 +++++++++--
 hw/misc/mac_via.c      |  9 ++++++++-
 8 files changed, 102 insertions(+), 17 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

RFC PATCH 1/7 hw/qdev: Introduce DeviceClass::[un]wire() handlers

---
 include/hw/qdev-core.h |  8 +++++++-
 hw/core/qdev.c         | 21 ++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)
include/hw/qdev-core.h +7 -1
index d47536eadb..2ca33aea3b 100644
@@ -101,8 +101,12 @@ typedef void (*BusUnrealize)(BusState *bus);
  * @props: Properties accessing state fields.
  * @realize: Callback function invoked when the #DeviceState:realized
  * property is changed to %true.
+ * @wire: Callback function called after @realize to connect IRQs,
+ * clocks and map memories. Can not fail.
+ * @unwire: Callback function to undo @wire. Called before @unrealize.
+ * Can not fail.
  * @unrealize: Callback function invoked when the #DeviceState:realized
- * property is changed to %false.
+ * property is changed to %false. Can not fail.
  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
  * as readonly "hotpluggable" property of #DeviceState instance
  *
@@ -161,6 +165,8 @@ struct DeviceClass {
      */
     DeviceReset reset;
     DeviceRealize realize;
+    void (*wire)(DeviceState *dev);
+    void (*unwire)(DeviceState *dev);
     DeviceUnrealize unrealize;
 
     /**
hw/core/qdev.c +20 -1
index c68d0f7c51..1d399aae71 100644
@@ -303,6 +303,16 @@ bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp)
 
 void qdev_unrealize(DeviceState *dev)
 {
+    DeviceClass *dc = DEVICE_GET_CLASS(dev);
+
+    if (dc->unwire) {
+        if (!dc->wire) {
+            error_report("disconnect() without connect() for type '%s'",
+                         object_get_typename(OBJECT(dev)));
+            abort();
+        }
+        dc->unwire(dev);
+    }
     object_property_set_bool(OBJECT(dev), "realized", false, &error_abort);
 }
 
@@ -601,8 +611,17 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
         dev->pending_deleted_event = true;
         DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
     }
-
     assert(local_err == NULL);
+
+    if (dc->wire) {
+        if (!dc->unwire) {
+            warn_report_once("wire() without unwire() for type '%s'",
+                             object_get_typename(OBJECT(dev)));
+        }
+        dc->wire(dev);
+    }
+
+    /* At this point the device is "guest visible". */
     return;
 
 child_realize_fail:
Signature · 1 line
2.41.0
#2 lore raw

RFC PATCH 2/7 hw/input/pckbd: Connect i8042 GPIOs once mouse/keyboard are realized

In reply to Philippe Mathieu-Daudé

While a device is not realized, we shouldn't call:
- qdev_get_gpio_in_named()
- qdev_connect_gpio_out()
- isa_connect_gpio_out()

---
Probably better would be to declare the ISA wiring handlers in ISADeviceClass, but such class doesn't exist yet.

---
 hw/input/pckbd.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
hw/input/pckbd.c +27 -11
index 90a4d9eb40..76175f6fe8 100644
@@ -707,6 +707,12 @@ static void i8042_mmio_realize(DeviceState *dev, Error **errp)
     if (!sysbus_realize(SYS_BUS_DEVICE(&ks->ps2mouse), errp)) {
         return;
     }
+}
+
+static void i8042_mmio_wire(DeviceState *dev)
+{
+    MMIOKBDState *s = I8042_MMIO(dev);
+    KBDState *ks = &s->kbd;
 
     qdev_connect_gpio_out(DEVICE(&ks->ps2kbd), PS2_DEVICE_IRQ,
                           qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq",
@@ -756,6 +762,7 @@ static void i8042_mmio_class_init(ObjectClass *klass, void *data)
     DeviceClass *dc = DEVICE_CLASS(klass);
 
     dc->realize = i8042_mmio_realize;
+    dc->wire = i8042_mmio_wire;
     dc->reset = i8042_mmio_reset;
     dc->vmsd = &vmstate_kbd_mmio;
     device_class_set_props(dc, i8042_mmio_properties);
@@ -878,9 +885,6 @@ static void i8042_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    isa_connect_gpio_out(isadev, I8042_KBD_IRQ, isa_s->kbd_irq);
-    isa_connect_gpio_out(isadev, I8042_MOUSE_IRQ, isa_s->mouse_irq);
-
     isa_register_ioport(isadev, isa_s->io + 0, 0x60);
     isa_register_ioport(isadev, isa_s->io + 1, 0x64);
 
@@ -888,18 +892,10 @@ static void i8042_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qdev_connect_gpio_out(DEVICE(&s->ps2kbd), PS2_DEVICE_IRQ,
-                          qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq",
-                                                 0));
-
     if (!sysbus_realize(SYS_BUS_DEVICE(&s->ps2mouse), errp)) {
         return;
     }
 
-    qdev_connect_gpio_out(DEVICE(&s->ps2mouse), PS2_DEVICE_IRQ,
-                          qdev_get_gpio_in_named(dev, "ps2-mouse-input-irq",
-                                                 0));
-
     if (isa_s->kbd_throttle && !isa_s->kbd.extended_state) {
         warn_report(TYPE_I8042 ": can't enable kbd-throttle without"
                     " extended-state, disabling kbd-throttle");
@@ -909,6 +905,25 @@ static void i8042_realizefn(DeviceState *dev, Error **errp)
     }
 }
 
+static void i8042_wire(DeviceState *dev)
+{
+    ISADevice *isadev = ISA_DEVICE(dev);
+    ISAKBDState *i8042 = I8042(dev);
+    KBDState *s = &i8042->kbd;
+
+    isa_connect_gpio_out(isadev, I8042_KBD_IRQ, i8042->kbd_irq);
+    isa_connect_gpio_out(isadev, I8042_MOUSE_IRQ, i8042->mouse_irq);
+
+
+    qdev_connect_gpio_out(DEVICE(&s->ps2kbd), PS2_DEVICE_IRQ,
+                          qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq",
+                                                 0));
+
+    qdev_connect_gpio_out(DEVICE(&s->ps2mouse), PS2_DEVICE_IRQ,
+                          qdev_get_gpio_in_named(dev, "ps2-mouse-input-irq",
+                                                 0));
+}
+
 static void i8042_build_aml(AcpiDevAmlIf *adev, Aml *scope)
 {
     ISAKBDState *isa_s = I8042(adev);
@@ -954,6 +969,7 @@ static void i8042_class_initfn(ObjectClass *klass, void *data)
     device_class_set_props(dc, i8042_properties);
     dc->reset = i8042_reset;
     dc->realize = i8042_realizefn;
+    dc->wire = i8042_wire;
     dc->vmsd = &vmstate_kbd_isa;
     adevc->build_dev_aml = i8042_build_aml;
     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Signature · 1 line
2.41.0
#3 lore raw

RFC PATCH 3/7 hw/ide/cmd646: Configure IDE bus IRQs after realization

In reply to Philippe Mathieu-Daudé

We shouldn't call qdev_get_gpio_in() on unrealized devices.

---
Ideally we should rework the current IDE bus model to really use QOM and not globals. Left for later.

---
 hw/ide/cmd646.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/ide/cmd646.c +11 -1
index c0bcfa4414..92e1e7a4fc 100644
@@ -291,9 +291,18 @@ static void pci_cmd646_ide_realize(PCIDevice *dev, Error **errp)
     /* TODO: RST# value should be 0 */
     pci_conf[PCI_INTERRUPT_PIN] = 0x01; // interrupt on pin 1
 
-    qdev_init_gpio_in(ds, cmd646_set_irq, 2);
     for (i = 0; i < 2; i++) {
         ide_bus_init(&d->bus[i], sizeof(d->bus[i]), ds, i, 2);
+    }
+}
+
+static void pci_cmd646_ide_wire(DeviceState *dev)
+{
+    PCIIDEState *d = PCI_IDE(dev);
+    DeviceState *ds = DEVICE(dev);
+
+    qdev_init_gpio_in(ds, cmd646_set_irq, 2);
+    for (unsigned i = 0; i < 2; i++) {
         ide_bus_init_output_irq(&d->bus[i], qdev_get_gpio_in(ds, i));
 
         bmdma_init(&d->bus[i], &d->bmdma[i], d);
@@ -324,6 +333,7 @@ static void cmd646_ide_class_init(ObjectClass *klass, void *data)
 
     dc->reset = cmd646_reset;
     dc->vmsd = &vmstate_ide_pci;
+    dc->wire = pci_cmd646_ide_wire;
     k->realize = pci_cmd646_ide_realize;
     k->exit = pci_cmd646_ide_exitfn;
     k->vendor_id = PCI_VENDOR_ID_CMD;
Signature · 1 line
2.41.0
#4 lore raw

RFC PATCH 4/7 hw/ide/sii3112: Configure IDE bus IRQs after realization

In reply to Philippe Mathieu-Daudé

We shouldn't call qdev_get_gpio_in() on unrealized devices.

---
Ideally we should rework the current IDE bus model to really use QOM and not globals. Left for later.

---
 hw/ide/sii3112.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/ide/sii3112.c +10 -0
index 63dc4a0494..8b6e931f27 100644
@@ -282,6 +282,15 @@ static void sii3112_pci_realize(PCIDevice *dev, Error **errp)
     qdev_init_gpio_in(ds, sii3112_set_irq, 2);
     for (i = 0; i < 2; i++) {
         ide_bus_init(&s->bus[i], sizeof(s->bus[i]), ds, i, 1);
+    }
+}
+
+static void sii3112_pci_wire(DeviceState *dev)
+{
+    PCIIDEState *s = PCI_IDE(dev);
+    DeviceState *ds = DEVICE(dev);
+
+    for (unsigned i = 0; i < 2; i++) {
         ide_bus_init_output_irq(&s->bus[i], qdev_get_gpio_in(ds, i));
 
         bmdma_init(&s->bus[i], &s->bmdma[i], s);
@@ -299,6 +308,7 @@ static void sii3112_pci_class_init(ObjectClass *klass, void *data)
     pd->class_id = PCI_CLASS_STORAGE_RAID;
     pd->revision = 1;
     pd->realize = sii3112_pci_realize;
+    dc->wire = sii3112_pci_wire;
     dc->reset = sii3112_reset;
     dc->desc = "SiI3112A SATA controller";
     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
Signature · 1 line
2.41.0
#5 lore raw

RFC PATCH 5/7 hw/ide/via: Configure IDE bus IRQs after realization

In reply to Philippe Mathieu-Daudé

We shouldn't call qdev_get_gpio_in() on unrealized devices.

---
Ideally we should rework the current IDE bus model to really use QOM and not globals. Left for later.

---
 hw/ide/via.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/ide/via.c +10 -0
index 3f3c484253..caf1b69bd4 100644
@@ -226,6 +226,15 @@ static void via_ide_realize(PCIDevice *dev, Error **errp)
     qdev_init_gpio_in(ds, via_ide_set_irq, ARRAY_SIZE(d->bus));
     for (i = 0; i < ARRAY_SIZE(d->bus); i++) {
         ide_bus_init(&d->bus[i], sizeof(d->bus[i]), ds, i, MAX_IDE_DEVS);
+    }
+}
+
+static void via_ide_wire(DeviceState *dev)
+{
+    PCIIDEState *d = PCI_IDE(dev);
+    DeviceState *ds = DEVICE(dev);
+
+    for (unsigned i = 0; i < ARRAY_SIZE(d->bus); i++) {
         ide_bus_init_output_irq(&d->bus[i], qdev_get_gpio_in(ds, i));
 
         bmdma_init(&d->bus[i], &d->bmdma[i], d);
@@ -249,6 +258,7 @@ static void via_ide_class_init(ObjectClass *klass, void *data)
     DeviceClass *dc = DEVICE_CLASS(klass);
     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 
+    dc->wire = via_ide_wire;
     dc->reset = via_ide_reset;
     dc->vmsd = &vmstate_ide_pci;
     /* Reason: only works as function of VIA southbridge */
Signature · 1 line
2.41.0
#6 lore raw

RFC PATCH 6/7 hw/intc/mips_gic: Initialize IRQ array once device is realized

In reply to Philippe Mathieu-Daudé

We shouldn't call qdev_get_gpio_in() on unrealized devices.

---
 hw/intc/mips_gic.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/intc/mips_gic.c +9 -2
index 77ba7348a3..05c8b8f4c4 100644
@@ -419,7 +419,6 @@ static void mips_gic_realize(DeviceState *dev, Error **errp)
         return;
     }
     s->vps = g_new(MIPSGICVPState, s->num_vps);
-    s->irq_state = g_new(MIPSGICIRQState, s->num_irq);
     /* Register the env for all VPs with the GIC */
     for (i = 0; i < s->num_vps; i++) {
         if (cs != NULL) {
@@ -433,7 +432,14 @@ static void mips_gic_realize(DeviceState *dev, Error **errp)
     }
     s->gic_timer = mips_gictimer_init(s, s->num_vps, gic_timer_expire_cb);
     qdev_init_gpio_in(dev, gic_set_irq, s->num_irq);
-    for (i = 0; i < s->num_irq; i++) {
+}
+
+static void mips_gic_wire(DeviceState *dev)
+{
+    MIPSGICState *s = MIPS_GIC(dev);
+
+    s->irq_state = g_new(MIPSGICIRQState, s->num_irq);
+    for (unsigned i = 0; i < s->num_irq; i++) {
         s->irq_state[i].irq = qdev_get_gpio_in(dev, i);
     }
 }
@@ -450,6 +456,7 @@ static void mips_gic_class_init(ObjectClass *klass, void *data)
 
     device_class_set_props(dc, mips_gic_properties);
     dc->realize = mips_gic_realize;
+    dc->wire = mips_gic_wire;
 }
 
 static const TypeInfo mips_gic_info = {
Signature · 1 line
2.41.0
#7 lore raw

RFC PATCH 7/7 hw/misc/mac_via: Have VIA1 child access parent IRQ once realized

In reply to Philippe Mathieu-Daudé

We shouldn't call qdev_get_gpio_in() on unrealized devices.

---
 hw/misc/mac_via.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/misc/mac_via.c +8 -1
index db6142b5f4..67e51430bf 100644
@@ -1246,7 +1246,6 @@ static void mos6522_q800_via1_realize(DeviceState *dev, Error **errp)
     v1s->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
 
     adb_register_autopoll_callback(adb_bus, adb_via_poll, v1s);
-    v1s->adb_data_ready = qdev_get_gpio_in(dev, VIA1_IRQ_ADB_READY_BIT);
 
     if (v1s->blk) {
         int64_t len = blk_getlength(v1s->blk);
@@ -1270,6 +1269,13 @@ static void mos6522_q800_via1_realize(DeviceState *dev, Error **errp)
     }
 }
 
+static void mos6522_q800_via1_wire(DeviceState *dev)
+{
+    MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(dev);
+
+    v1s->adb_data_ready = qdev_get_gpio_in(dev, VIA1_IRQ_ADB_READY_BIT);
+}
+
 static void mos6522_q800_via1_init(Object *obj)
 {
     MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(obj);
@@ -1336,6 +1342,7 @@ static void mos6522_q800_via1_class_init(ObjectClass *oc, void *data)
     MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
 
     dc->realize = mos6522_q800_via1_realize;
+    dc->wire = mos6522_q800_via1_wire;
     resettable_class_set_parent_phases(rc, NULL, mos6522_q800_via1_reset_hold,
                                        NULL, &mdc->parent_phases);
     dc->vmsd = &vmstate_q800_via1;
Signature · 1 line
2.41.0
#8 lore raw
In reply to Philippe Mathieu-Daudé

Hello,

Instead of adding a new method to devices, why not move wiring to the code that created/realized the device? Either it's the job of the code that created the device and wants to use it or if it's an internal object the device itself is created then it should be OK to wire it in the device realize method. I probably don't understand the problem fully, what I want to say is that device creation is complex enough now so trying to avoid adding more complexity to it would be a good thing.

Regards,
BALATON Zoltan

#9 lore raw
In reply to Philippe Mathieu-Daudé

Am 9. Februar 2024 12:32:18 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 127 lines

Hi,

Various issues related to implementing dynamic machines have been documented in [1].

We are trying to understand what means "a qdev is realized". One explanation was "the device is guest visible"; however many devices are realized before being mapped, thus are not "guest visible". Some devices map / wire their IRQs before being realized (such ISA devices). There is a need for devices to be "automatically" mapped/wired (see [2]) such CLI-created devices, but this apply generically to dynamic machines.

Currently the device creation steps are expected to roughly be:

(external use) (QDev core) (Device Impl)
~~~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~~

                              INIT enter
                  ----->
                        +----------------------+
                        |    Allocate state    |
                        +----------------------+
                                                ----->
                                                       +---------------------+
                                                       | INIT children       |
                                                       |                     |
                                                       | Alias children properties
                                                       |                     |
                                                       | Expose properties   |
                               INIT exit               +---------------------+
                  <-----------------------------------
+----------------+
| set properties |
|                |
| set ClkIn      |
+----------------+          REALIZE enter
                  ---------------------------------->
                                                      +----------------------+
                                                      | Use config properties|
                                                      |                      |
                                                      | Realize children     |
                                                      |                      |
                                                      | Init GPIOs/IRQs      |
                                                      |                      |
                                                      | Init MemoryRegions   |
                                                      +----------------------+
                              REALIZE exit
                  <-----------------------------------                        ----  "realized" / "guest visible"
+-----------------+
| Explicit wiring:|
|   IRQs          |
|   I/O / Mem     |
|   ClkOut        |
+-----------------+             RESET enter
                   --------------------------------->
                                                      +----------------------+
                                                      | Reset default values |
                                                      +----------------------+

But as mentioned, various devices "wire" parts before they exit the "realize" step.
In order to clarify, I'm trying to enforce what can be done *before* and *after* realization.

*after* a device is expected to be stable (no more configurable) and fully usable.

To be able to use internal/auto wiring (such ISA devices) and keep the current external/explicit wiring, I propose to add an extra "internal wiring" step, happening after the REALIZE step as:

(external use) (QDev core) (Device Impl)
~~~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~~

                              INIT enter
                  ----->
                        +----------------------+
                        |    Allocate state    |
                        +----------------------+
                                                ----->
                                                       +---------------------+
                                                       | INIT children       |
                                                       |                     |
                                                       | Alias children properties
                                                       |                     |
                                                       | Expose properties   |
                               INIT exit               +---------------------+
                  <-----------------------------------
+----------------+
| set properties |
|                |
| set ClkIn      |
+----------------+          REALIZE enter
                  ---------------------------------->
                                                      +----------------------+
                                                      | Use config properties|
                                                      |                      |
                                                      | Realize children     |
                                                      |                      |
                                                      | Init GPIOs/IRQs      |
                                                      |                      |
                                                      | Init MemoryRegions   |
                                                      +----------------------+
                              REALIZE exit       <---
                        +----------------------+
                        | Internal auto wiring |
                        |   IRQs               |  (i.e. ISA bus)
                        |   I/O / Mem          |
                        |   ClkOut             |
                        +----------------------+
                   <---                                                       ----  "realized"
+-----------------+
| External wiring:|
|   IRQs          |
|   I/O / Mem     |
|   ClkOut        |
+-----------------+             RESET enter                                    ----  "guest visible"
                   --------------------------------->
                                                      +----------------------+
                                                      | Reset default values |
                                                      +----------------------+

The "realized" point is not changed. "guest visible" concept only occurs *after* wiring, just before the reset phase.

Quoted 7 lines

This series introduces the DeviceClass::wire handler within qdev core realization code, and convert devices using the implicit wiring to using that explicit handler.

QDev API assertions patches will be posted later as another series.

Thoughts?

Hi Phil,

What exactly are you trying to achieve? I assume that the goal is to make devices user-creatable which currently can't due to current limitations. For this I'm presenting an idea:

I'd probably not force the new discipline onto devices which aren't user-creatable -- that seems like too much gymnastics to me for no gain. Instead, I'd act on the suggestion of having an interface TYPE_USER_CREATABLE and put the wire method there.

This wire method would be responsible for performing *all* duties between the states "realized" and "guest-visible". Based on the parameters it is given by the user, it would find all resources it needs to wire up the device. It finds these resources by e.g. scanning the QOM tree. A good practice would be to pass the root node for the scanning as parameter, e.g. the current machine.

It could happen that a user-crrated device depends on another user-created device. To make this work, the part of QEMU that deals with user-created devices would perform a fixed point iteration over all not yet finished user-created devices. When there is no progress within one iteration, QEMU aborts with an error message. This error message could come from the wire invocation of any -- or even better -- all not yet finished devices.

To demonstrate the approach, one could probably pick just one device, add the interface and hack up something in the core handling of user-created devices.

Does this idea sound reasonable? Is this what you're ultimately after?

Best regards,
Bernhatd

Quoted 43 lines

Regards,

Phil.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Cc Eduardo Habkost <[email protected]>
Cc Markus Armbruster <[email protected]>' --cc '
Cc Edgar E. Iglesias <[email protected]>'
Cc Richard Henderson <[email protected]>
Cc Mark Cave-Ayland <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Bernhard Beschow <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Cédric Le Goater <[email protected]>
Cc Luc Michel <[email protected]>
Cc Zhao Liu <[email protected]>
Cc Gustavo Romero <[email protected]>

Philippe Mathieu-Daudé (7):
hw/qdev: Introduce DeviceClass::[un]wire() handlers hw/input/pckbd: Connect i8042 GPIOs once mouse/keyboard are realized hw/ide/cmd646: Configure IDE bus IRQs after realization hw/ide/sii3112: Configure IDE bus IRQs after realization hw/ide/via: Configure IDE bus IRQs after realization hw/intc/mips_gic: Initialize IRQ array once device is realized hw/misc/mac_via: Have VIA1 child access parent IRQ once realized

include/hw/qdev-core.h |  8 +++++++-
hw/core/qdev.c         | 21 ++++++++++++++++++++-
hw/ide/cmd646.c        | 12 +++++++++++-
hw/ide/sii3112.c       | 10 ++++++++++
hw/ide/via.c           | 10 ++++++++++
hw/input/pckbd.c       | 38 +++++++++++++++++++++++++++-----------
hw/intc/mips_gic.c     | 11 +++++++++--
hw/misc/mac_via.c      |  9 ++++++++-
8 files changed, 102 insertions(+), 17 deletions(-)