Skip to contentSkip to conversation

hw/adc: convert object props to class props

PATCH Superseded Mark Cave-Ayland 2 participants 5 messages Jun 24 – Jun 25

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hw/adc: convert object props to class props

Since the use of object props is effectively deprecated, here is an attempt to convert all use of object props in hw/adc to class props. The eventual aim is to continue working through the codebase, removing all remaining uses of object props.

The series is lightly tested: it passes "make check", GitLab CI and some simple local tests. I'm mostly interested for feedback on the conversion strategy, and to get a feel for the best way to merge this series since once the basic conversion patterns are in place, the same patterns can be applied elsewhere and it would be good to minimise the merge window for such changes.

Mark Cave-Ayland (2):
qom: add support for array class properties hw/adc/npcm7xx_adc.c: convert NPCM7XX_ADC object props to class props

 hw/adc/npcm7xx_adc.c | 75 +++++++++++++++++++++++++++++++++++++++-----
 qom/object.c         | 27 ++++++++++++++++
 2 files changed, 95 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 qom: add support for array class properties

These will be required for the upcoming conversions of object properties to class properties.

---
 qom/object.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
qom/object.c +27 -0
index 0ac201de4c..204871cb53 100644
@@ -1411,6 +1411,33 @@ object_class_property_add(ObjectClass *klass,
                           void *opaque)
 {
     ObjectProperty *prop;
+    size_t name_len = strlen(name);
+
+    if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
+        int i;
+        ObjectProperty *ret = NULL;
+        char *name_no_array = g_strdup(name);
+
+        name_no_array[name_len - 3] = '\0';
+        for (i = 0; i < INT16_MAX; ++i) {
+            char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
+
+            if (object_class_property_find(klass, full_name)) {
+                g_free(full_name);
+                continue;
+            }
+
+            ret = object_class_property_add(klass, full_name, type, get, set,
+                                            release, opaque);
+            g_free(full_name);
+            if (ret) {
+                break;
+            }
+        }
+        g_free(name_no_array);
+        assert(ret);
+        return ret;
+    }
 
     assert(!object_class_property_find(klass, name));
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 hw/adc/npcm7xx_adc.c: convert NPCM7XX_ADC object props to class props

In reply to Mark Cave-Ayland
---
 hw/adc/npcm7xx_adc.c | 75 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 68 insertions(+), 7 deletions(-)
hw/adc/npcm7xx_adc.c +68 -7
index 3584c27c75..accaaf2620 100644
@@ -20,6 +20,8 @@
 #include "hw/core/qdev-properties.h"
 #include "hw/core/registerfields.h"
 #include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
 #include "qemu/timer.h"
@@ -229,7 +231,6 @@ static void npcm7xx_adc_init(Object *obj)
 {
     NPCM7xxADCState *s = NPCM7XX_ADC(obj);
     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
-    int i;
 
     sysbus_init_irq(sbd, &s->irq);
 
@@ -240,15 +241,63 @@ static void npcm7xx_adc_init(Object *obj)
     sysbus_init_mmio(sbd, &s->iomem);
     s->clock = qdev_init_clock_in(DEVICE(s), "clock", NULL, NULL, 0);
 
-    for (i = 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) {
-        object_property_add_uint32_ptr(obj, "adci[*]",
-                &s->adci[i], OBJ_PROP_FLAG_READWRITE);
-    }
-    object_property_add_uint32_ptr(obj, "vref",
-            &s->vref, OBJ_PROP_FLAG_WRITE);
     npcm7xx_adc_calibrate(s);
 }
 
+static void npcm7xx_adc_get_input(Object *obj, Visitor *v,
+                                  const char *name, void *opaque,
+                                  Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t idx, val;
+    int res;
+
+    res = sscanf(name, "adci[%u]", &idx);
+    if (res == EOF || res != 1) {
+        error_setg(errp, "unable to parse index from %s", name);
+        return;
+    }
+
+    val = s->adci[idx];
+    visit_type_uint32(v, name, &val, errp);
+}
+
+static void npcm7xx_adc_set_input(Object *obj, Visitor *v,
+                                  const char *name, void *opaque,
+                                  Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t idx, val;
+    int res;
+
+    res = sscanf(name, "adci[%u]", &idx);
+    if (res == EOF || res != 1) {
+        error_setg(errp, "unable to parse index from %s", name);
+        return;
+    }
+
+    val = s->adci[idx];
+    if (!visit_type_uint32(v, name, &val, errp)) {
+        return;
+    }
+
+    s->adci[idx] = val;
+}
+
+static void npcm7xx_adc_set_vref(Object *obj, Visitor *v,
+                                 const char *name, void *opaque,
+                                 Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t vref = s->vref;
+
+    if (!visit_type_uint32(v, name, &vref, errp)) {
+        return;
+    }
+
+    s->vref = vref;
+}
+
 static const VMStateDescription vmstate_npcm7xx_adc = {
     .name = "npcm7xx-adc",
     .version_id = 0,
@@ -275,6 +324,7 @@ static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data)
 {
     ResettableClass *rc = RESETTABLE_CLASS(klass);
     DeviceClass *dc = DEVICE_CLASS(klass);
+    int i;
 
     dc->desc = "NPCM7xx ADC Module";
     dc->vmsd = &vmstate_npcm7xx_adc;
@@ -282,6 +332,17 @@ static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data)
     rc->phases.hold = npcm7xx_adc_hold_reset;
 
     device_class_set_props(dc, npcm7xx_timer_properties);
+
+    for (i = 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) {
+        object_class_property_add(klass, "adci[*]", "uint32",
+                                  npcm7xx_adc_get_input,
+                                  npcm7xx_adc_set_input,
+                                  NULL, NULL);
+    }
+    object_class_property_add(klass, "vref", "uint32",
+                              NULL,
+                              npcm7xx_adc_set_vref,
+                              NULL, NULL);
 }
 
 static const TypeInfo npcm7xx_adc_info = {
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/2] hw/adc/npcm7xx_adc.c: convert NPCM7XX_ADC object props to class props

Quoted 105 lines · Mark Cave-Ayland
---
 hw/adc/npcm7xx_adc.c | 75 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 68 insertions(+), 7 deletions(-)
hw/adc/npcm7xx_adc.c +57 -7
index 3584c27c75..accaaf2620 100644
@@ -20,6 +20,8 @@
 #include "hw/core/qdev-properties.h"
 #include "hw/core/registerfields.h"
 #include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
 #include "qemu/timer.h"
@@ -229,7 +231,6 @@ static void npcm7xx_adc_init(Object *obj)
 {
     NPCM7xxADCState *s = NPCM7XX_ADC(obj);
     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
-    int i;
 
     sysbus_init_irq(sbd, &s->irq);
 
@@ -240,15 +241,63 @@ static void npcm7xx_adc_init(Object *obj)
     sysbus_init_mmio(sbd, &s->iomem);
     s->clock = qdev_init_clock_in(DEVICE(s), "clock", NULL, NULL, 0);
 
-    for (i = 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) {
-        object_property_add_uint32_ptr(obj, "adci[*]",
-                &s->adci[i], OBJ_PROP_FLAG_READWRITE);
-    }
-    object_property_add_uint32_ptr(obj, "vref",
-            &s->vref, OBJ_PROP_FLAG_WRITE);
     npcm7xx_adc_calibrate(s);
 }
 
+static void npcm7xx_adc_get_input(Object *obj, Visitor *v,
+                                  const char *name, void *opaque,
+                                  Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t idx, val;
+    int res;
+
+    res = sscanf(name, "adci[%u]", &idx);
+    if (res == EOF || res != 1) {
+        error_setg(errp, "unable to parse index from %s", name);
+        return;
+    }
+
+    val = s->adci[idx];
+    visit_type_uint32(v, name, &val, errp);
+}
+
+static void npcm7xx_adc_set_input(Object *obj, Visitor *v,
+                                  const char *name, void *opaque,
+                                  Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t idx, val;
+    int res;
+
+    res = sscanf(name, "adci[%u]", &idx);
+    if (res == EOF || res != 1) {
+        error_setg(errp, "unable to parse index from %s", name);
+        return;
+    }
+
+    val = s->adci[idx];
+    if (!visit_type_uint32(v, name, &val, errp)) {
+        return;
+    }
+
+    s->adci[idx] = val;
+}
+
+static void npcm7xx_adc_set_vref(Object *obj, Visitor *v,
+                                 const char *name, void *opaque,
+                                 Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t vref = s->vref;
+
+    if (!visit_type_uint32(v, name, &vref, errp)) {
+        return;
+    }
+
+    s->vref = vref;
+}
+
 static const VMStateDescription vmstate_npcm7xx_adc = {
     .name = "npcm7xx-adc",
     .version_id = 0,
@@ -275,6 +324,7 @@ static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data)
 {
     ResettableClass *rc = RESETTABLE_CLASS(klass);
     DeviceClass *dc = DEVICE_CLASS(klass);
+    int i;
 
     dc->desc = "NPCM7xx ADC Module";
     dc->vmsd = &vmstate_npcm7xx_adc;

hw/adc/npcm7xx_adc.c static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data) · line 341

Quoted 10 lines
hw/adc/npcm7xx_adc.c +7 -0
@@ -282,6 +332,17 @@ static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data)
     rc->phases.hold = npcm7xx_adc_hold_reset;
 
     device_class_set_props(dc, npcm7xx_timer_properties);
+
+    for (i = 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) {
+        object_class_property_add(klass, "adci[*]", "uint32",
+                                  npcm7xx_adc_get_input,
+                                  npcm7xx_adc_set_input,
+                                  NULL, NULL);
+    }

IMHO the logic around "[*]" expansion is mostly useful for child properties where you don't know ahead of time how many relationships might already exist.

In this case though, we know exactly how many props we want and we know we want to start from 0.

How about we do this explicitly:

   g_autofree char *adciprop = g_strdup_printf("adci[%u]", i);
   object_class_property_add(klass, adciprop, "uint32",
                             npcm7xx_adc_get_input,
                             npcm7xx_adc_set_input,
                             NULL, NULL);

which avoids the O(N^2) performance of handling '[*]' and thus makes the prior patch redundant.

I kind of wish we didn't have '[' in the property name but we're probably stuck with that for back compat.

Quoted 10 lines
hw/adc/npcm7xx_adc.c +4 -0
+    object_class_property_add(klass, "vref", "uint32",
+                              NULL,
+                              npcm7xx_adc_set_vref,
+                              NULL, NULL);
 }
 
 static const TypeInfo npcm7xx_adc_info = {
Signature · 1 line
2.43.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#4 lore raw

Re: [PATCH 2/2] hw/adc/npcm7xx_adc.c: convert NPCM7XX_ADC object props to class props

Quoted 132 lines · Daniel P. Berrangé
Quoted 105 lines · Mark Cave-Ayland
---
  hw/adc/npcm7xx_adc.c | 75 +++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 68 insertions(+), 7 deletions(-)
hw/adc/npcm7xx_adc.c +57 -7
index 3584c27c75..accaaf2620 100644
@@ -20,6 +20,8 @@
  #include "hw/core/qdev-properties.h"
  #include "hw/core/registerfields.h"
  #include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
  #include "qemu/log.h"
  #include "qemu/module.h"
  #include "qemu/timer.h"
@@ -229,7 +231,6 @@ static void npcm7xx_adc_init(Object *obj)
  {
      NPCM7xxADCState *s = NPCM7XX_ADC(obj);
      SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
-    int i;
 
      sysbus_init_irq(sbd, &s->irq);
 
@@ -240,15 +241,63 @@ static void npcm7xx_adc_init(Object *obj)
      sysbus_init_mmio(sbd, &s->iomem);
      s->clock = qdev_init_clock_in(DEVICE(s), "clock", NULL, NULL, 0);
 
-    for (i = 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) {
-        object_property_add_uint32_ptr(obj, "adci[*]",
-                &s->adci[i], OBJ_PROP_FLAG_READWRITE);
-    }
-    object_property_add_uint32_ptr(obj, "vref",
-            &s->vref, OBJ_PROP_FLAG_WRITE);
      npcm7xx_adc_calibrate(s);
  }
 
+static void npcm7xx_adc_get_input(Object *obj, Visitor *v,
+                                  const char *name, void *opaque,
+                                  Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t idx, val;
+    int res;
+
+    res = sscanf(name, "adci[%u]", &idx);
+    if (res == EOF || res != 1) {
+        error_setg(errp, "unable to parse index from %s", name);
+        return;
+    }
+
+    val = s->adci[idx];
+    visit_type_uint32(v, name, &val, errp);
+}
+
+static void npcm7xx_adc_set_input(Object *obj, Visitor *v,
+                                  const char *name, void *opaque,
+                                  Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t idx, val;
+    int res;
+
+    res = sscanf(name, "adci[%u]", &idx);
+    if (res == EOF || res != 1) {
+        error_setg(errp, "unable to parse index from %s", name);
+        return;
+    }
+
+    val = s->adci[idx];
+    if (!visit_type_uint32(v, name, &val, errp)) {
+        return;
+    }
+
+    s->adci[idx] = val;
+}
+
+static void npcm7xx_adc_set_vref(Object *obj, Visitor *v,
+                                 const char *name, void *opaque,
+                                 Error **errp)
+{
+    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
+    uint32_t vref = s->vref;
+
+    if (!visit_type_uint32(v, name, &vref, errp)) {
+        return;
+    }
+
+    s->vref = vref;
+}
+
  static const VMStateDescription vmstate_npcm7xx_adc = {
      .name = "npcm7xx-adc",
      .version_id = 0,
@@ -275,6 +324,7 @@ static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data)
  {
      ResettableClass *rc = RESETTABLE_CLASS(klass);
      DeviceClass *dc = DEVICE_CLASS(klass);
+    int i;
 
      dc->desc = "NPCM7xx ADC Module";
      dc->vmsd = &vmstate_npcm7xx_adc;

hw/adc/npcm7xx_adc.c static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data) · line 341

Quoted 10 lines
hw/adc/npcm7xx_adc.c +7 -0
@@ -282,6 +332,17 @@ static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data)
      rc->phases.hold = npcm7xx_adc_hold_reset;
 
      device_class_set_props(dc, npcm7xx_timer_properties);
+
+    for (i = 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) {
+        object_class_property_add(klass, "adci[*]", "uint32",
+                                  npcm7xx_adc_get_input,
+                                  npcm7xx_adc_set_input,
+                                  NULL, NULL);
+    }

IMHO the logic around "[*]" expansion is mostly useful for child properties where you don't know ahead of time how many relationships might already exist.

In this case though, we know exactly how many props we want and we know we want to start from 0.

How about we do this explicitly:

    g_autofree char *adciprop = g_strdup_printf("adci[%u]", i);
    object_class_property_add(klass, adciprop, "uint32",
                              npcm7xx_adc_get_input,
                              npcm7xx_adc_set_input,
                              NULL, NULL);
Quoted 5 lines

which avoids the O(N^2) performance of handling '[*]' and thus makes the prior patch redundant.

I kind of wish we didn't have '[' in the property name but we're probably stuck with that for back compat.

That's fine with me: I'll drop the object.c change for now and see how far I get.

I've just send a v2 with this change.

Quoted 9 lines
Quoted 9 lines
hw/adc/npcm7xx_adc.c +4 -0
+    object_class_property_add(klass, "vref", "uint32",
+                              NULL,
+                              npcm7xx_adc_set_vref,
+                              NULL, NULL);
  }
 
  static const TypeInfo npcm7xx_adc_info = {
Signature · 1 line
2.43.0

ATB,

Mark.