Skip to contentSkip to conversation

Allow int props with no default, use them in ARM

PATCH In review Peter Maydell 3 participants 19 messages Jul 11, 2017 – Jul 13, 2017

lore
Share & tools
lore
#0 lore raw

cover Allow int props with no default, use them in ARM

This patchset follows up on a suggestion by Markus for allowing devices to implement integer properties which don't have a default value specified in their Property struct.

My use case for this is ARM, where we have a property pmsav7-dregion whose correct default value depends on which exact ARM CPU this is. That means we can't have a single constant default value specified in the DEFINE_PROP_UINT32(), but on the other hand if we just prevent the qdev prop system from setting a default itself we can naturally get the right default value by having the structure field be correctly set to the default for the CPU in the first place.

Patch 1 cleans up a corner case where we had just one Property struct which was using the default value (because it has a PropertyInfo with a non-NULL set_default_value field) but not explicitly setting .defval.[ui].

Patch 2 adds .set_default to Property, uses it as the gate on calling .set_default_value rather than just whether .set_default_value is non-NULL, and adds some macros so that devices can use the new no-default-value int props.

Patch 3 uses this so M3 and M4 have the right default for the number of PMSA regions rather than getting the different R7 default.

thanks
-- PMM

Peter Maydell (3):
qdev-properties.h: Explicitly set the default value for arraylen
properties
qdev: support properties which don't set a default value target/arm: Make Cortex-M3 and M4 default to 8 PMSA regions

 include/hw/qdev-core.h       |  1 +
 include/hw/qdev-properties.h | 21 +++++++++++++++++++++
 hw/core/qdev.c               |  2 +-
 target/arm/cpu.c             | 12 +++++++++++-
 4 files changed, 34 insertions(+), 2 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/3 qdev-properties.h: Explicitly set the default value for arraylen properties

In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen) which has a .set_default_value member we will set the field to a default value. That default value will be zero, by the C rule that struct initialization sets unmentioned members to zero if at least one member is initialized. However it's clearer to state it explicitly.

---
If my eyeball of the sources is correct, this is the only case we have of a Property struct that uses a PropertyInfo with a non-NULL .set_default_value but which doesn't explicitly set .defval.u.

---
 include/hw/qdev-properties.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/qdev-properties.h +1 -0
index 0604c33..36d040c 100644
@@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
         .arrayinfo = &(_arrayprop),                                     \
Signature · 1 line
2.7.4
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] qdev-properties.h: Explicitly set the default value for arraylen properties

----- Original Message -----

Quoted 7 lines

In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen) which has a .set_default_value member we will set the field to a default value. That default value will be zero, by the C rule that struct initialization sets unmentioned members to zero if at least one member is initialized. However it's clearer to state it explicitly.

Quoted 24 lines

---
If my eyeball of the sources is correct, this is the only case we have of a Property struct that uses a PropertyInfo with a non-NULL .set_default_value but which doesn't explicitly set .defval.u.

---
 include/hw/qdev-properties.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/qdev-properties.h +1 -1
index 0604c33..36d040c 100644
@@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
         .arrayinfo = &(_arrayprop),                                     \
--

2.7.4

#3 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/3] qdev-properties.h: Explicitly set the default value for arraylen properties

In reply to Peter Maydell
Quoted 20 lines · Peter Maydell

In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen) which has a .set_default_value member we will set the field to a default value. That default value will be zero, by the C rule that struct initialization sets unmentioned members to zero if at least one member is initialized. However it's clearer to state it explicitly.

---
If my eyeball of the sources is correct, this is the only case we have of a Property struct that uses a PropertyInfo with a non-NULL .set_default_value but which doesn't explicitly set .defval.u.

---
 include/hw/qdev-properties.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/qdev-properties.h +0 -0
index 0604c33..36d040c 100644

include/hw/qdev-properties.h extern PropertyInfo qdev_prop_arraylen; · line 116

Quoted 7 lines
include/hw/qdev-properties.h +1 -0
@@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
         .arrayinfo = &(_arrayprop),                                     \

Hmm.

.info and .defval apply to the array *length*. .info.set_default_value() is set_default_uint(), which uses .defval.u. Setting it here is correct.

The array *elements* are described by .arrayinfo. There is no .arraydefval, but that's okay, because .arrayinfo.set_default_value() doesn't get called. Correct?

#4 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/3] qdev-properties.h: Explicitly set the default value for arraylen properties

Quoted 32 lines · Markus Armbruster
Quoted 20 lines · Peter Maydell

In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen) which has a .set_default_value member we will set the field to a default value. That default value will be zero, by the C rule that struct initialization sets unmentioned members to zero if at least one member is initialized. However it's clearer to state it explicitly.

---
If my eyeball of the sources is correct, this is the only case we have of a Property struct that uses a PropertyInfo with a non-NULL .set_default_value but which doesn't explicitly set .defval.u.

---
 include/hw/qdev-properties.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/qdev-properties.h +0 -0
index 0604c33..36d040c 100644

include/hw/qdev-properties.h extern PropertyInfo qdev_prop_arraylen; · line 116

Quoted 7 lines
include/hw/qdev-properties.h +1 -0
@@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
         .arrayinfo = &(_arrayprop),                                     \

Hmm.

Quoted 7 lines

.info and .defval apply to the array *length*. .info.set_default_value() is set_default_uint(), which uses .defval.u. Setting it here is correct.

The array *elements* are described by .arrayinfo. There is no .arraydefval, but that's okay, because .arrayinfo.set_default_value() doesn't get called. Correct?

Correct. (There's no way to specify a default value for array properties.)

thanks
-- PMM

Reviewed-by
#5 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/3] qdev-properties.h: Explicitly set the default value for arraylen properties

Quoted 37 lines · Peter Maydell
Quoted 36 lines · Markus Armbruster
Quoted 20 lines · Peter Maydell

In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen) which has a .set_default_value member we will set the field to a default value. That default value will be zero, by the C rule that struct initialization sets unmentioned members to zero if at least one member is initialized. However it's clearer to state it explicitly.

---
If my eyeball of the sources is correct, this is the only case we have of a Property struct that uses a PropertyInfo with a non-NULL .set_default_value but which doesn't explicitly set .defval.u.

---
 include/hw/qdev-properties.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/qdev-properties.h +0 -0
index 0604c33..36d040c 100644

include/hw/qdev-properties.h extern PropertyInfo qdev_prop_arraylen; · line 116

Quoted 7 lines
include/hw/qdev-properties.h +1 -0
@@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
         .arrayinfo = &(_arrayprop),                                     \

Hmm.

.info and .defval apply to the array *length*. .info.set_default_value() is set_default_uint(), which uses .defval.u. Setting it here is correct.

Quoted 5 lines
Quoted 3 lines

The array *elements* are described by .arrayinfo. There is no .arraydefval, but that's okay, because .arrayinfo.set_default_value() doesn't get called. Correct?

Correct. (There's no way to specify a default value for array properties.)

Thanks!

#6 lore raw

PATCH 2/3 qdev: support properties which don't set a default value

In reply to Peter Maydell

In some situations it's useful to have a qdev property which doesn't automatically set its default value when qdev_property_add_static is called (for instance when the default value is not constant).

Support this by adding a flag to the Property struct indicating whether to set the default value. This replaces the existing test for whether the PorpertyInfo set_default_value function pointer is NULL, and we set the .set_default field to true for all those cases of struct Property which use a PropertyInfo with a non-NULL set_default_value, so behaviour remains the same as before.

We define two new macros DEFINE_PROP_SIGNED_NODEFAULT and DEFINE_PROP_UNSIGNED_NODEFAULT, to cover the most plausible use cases of wanting to set an integer property with no default value.

---
Since the previous patch fixed the only case that was using a default value but not explicitly setting .defval.[ui], this patch is a bit easier to review: .set_default = true is added in exactly the places that initialize .defval.[ui].

---
 include/hw/qdev-core.h       |  1 +
 include/hw/qdev-properties.h | 20 ++++++++++++++++++++
 hw/core/qdev.c               |  2 +-
 3 files changed, 22 insertions(+), 1 deletion(-)
include/hw/qdev-core.h +1 -0
index 9d7c1c0..d110163 100644
@@ -226,6 +226,7 @@ struct Property {
     PropertyInfo *info;
     ptrdiff_t    offset;
     uint8_t      bitnr;
+    bool         set_default;
     union {
         int64_t i;
         uint64_t u;
include/hw/qdev-properties.h +20 -0
index 36d040c..66816a5 100644
@@ -43,15 +43,24 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(_prop),                                          \
         .offset    = offsetof(_state, _field)                           \
             + type_check(_type,typeof_field(_state, _field)),           \
+        .set_default = true,                                            \
         .defval.i  = (_type)_defval,                                    \
         }
 
+#define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
+        .name      = (_name),                                           \
+        .info      = &(_prop),                                          \
+        .offset    = offsetof(_state, _field)                           \
+            + type_check(_type, typeof_field(_state, _field)),          \
+        }
+
 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
         .name      = (_name),                                    \
         .info      = &(qdev_prop_bit),                           \
         .bitnr    = (_bit),                                      \
         .offset    = offsetof(_state, _field)                    \
             + type_check(uint32_t,typeof_field(_state, _field)), \
+        .set_default = true,                                     \
         .defval.u  = (bool)_defval,                              \
         }
 
@@ -60,15 +69,24 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(_prop),                                          \
         .offset    = offsetof(_state, _field)                           \
             + type_check(_type, typeof_field(_state, _field)),          \
+        .set_default = true,                                            \
         .defval.u  = (_type)_defval,                                    \
         }
 
+#define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
+        .name      = (_name),                                           \
+        .info      = &(_prop),                                          \
+        .offset    = offsetof(_state, _field)                           \
+            + type_check(_type, typeof_field(_state, _field)),          \
+        }
+
 #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) {       \
         .name      = (_name),                                           \
         .info      = &(qdev_prop_bit64),                                \
         .bitnr    = (_bit),                                             \
         .offset    = offsetof(_state, _field)                           \
             + type_check(uint64_t, typeof_field(_state, _field)),       \
+        .set_default = true,                                            \
         .defval.u  = (bool)_defval,                                     \
         }
 
@@ -77,6 +95,7 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(qdev_prop_bool),                          \
         .offset    = offsetof(_state, _field)                    \
             + type_check(bool, typeof_field(_state, _field)),    \
+        .set_default = true,                                     \
         .defval.u    = (bool)_defval,                            \
         }
 
@@ -110,6 +129,7 @@ extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .set_default = true,                                            \
         .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
hw/core/qdev.c +1 -1
index 849952a..96965a7 100644
@@ -793,7 +793,7 @@ void qdev_property_add_static(DeviceState *dev, Property *prop,
                                     prop->info->description,
                                     &error_abort);
 
-    if (prop->info->set_default_value) {
+    if (prop->set_default) {
         prop->info->set_default_value(obj, prop);
     }
 }
Signature · 1 line
2.7.4
Reviewed-by on v1 2/3
#7 lore raw

Re: [Qemu-devel] [PATCH 2/3] qdev: support properties which don't set a default value

Hi

----- Original Message -----

Quoted 11 lines

In some situations it's useful to have a qdev property which doesn't automatically set its default value when qdev_property_add_static is called (for instance when the default value is not constant).

Support this by adding a flag to the Property struct indicating whether to set the default value. This replaces the existing test for whether the PorpertyInfo set_default_value function pointer is NULL, and we set the .set_default field to true for all those cases of struct Property which use a PropertyInfo with a non-NULL set_default_value, so behaviour remains the same as before.

Quoted 6 lines

We define two new macros DEFINE_PROP_SIGNED_NODEFAULT and DEFINE_PROP_UNSIGNED_NODEFAULT, to cover the most plausible use cases of wanting to set an integer property with no default value.

minor comment below

Quoted 100 lines

---
Since the previous patch fixed the only case that was using a default value but not explicitly setting .defval.[ui], this patch is a bit easier to review: .set_default = true is added in exactly the places that initialize .defval.[ui].

---
 include/hw/qdev-core.h       |  1 +
 include/hw/qdev-properties.h | 20 ++++++++++++++++++++
 hw/core/qdev.c               |  2 +-
 3 files changed, 22 insertions(+), 1 deletion(-)
include/hw/qdev-core.h +1 -0
index 9d7c1c0..d110163 100644
@@ -226,6 +226,7 @@ struct Property {
     PropertyInfo *info;
     ptrdiff_t    offset;
     uint8_t      bitnr;
+    bool         set_default;
     union {
         int64_t i;
         uint64_t u;
include/hw/qdev-properties.h +20 -0
index 36d040c..66816a5 100644
@@ -43,15 +43,24 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(_prop),                                          \
         .offset    = offsetof(_state, _field)                           \
             + type_check(_type,typeof_field(_state, _field)),           \
+        .set_default = true,                                            \
         .defval.i  = (_type)_defval,                                    \
         }
 
+#define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) {
 \
+        .name      = (_name),                                           \
+        .info      = &(_prop),                                          \
+        .offset    = offsetof(_state, _field)                           \
+            + type_check(_type, typeof_field(_state, _field)),          \
+        }
+
 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
         .name      = (_name),                                    \
         .info      = &(qdev_prop_bit),                           \
         .bitnr    = (_bit),                                      \
         .offset    = offsetof(_state, _field)                    \
             + type_check(uint32_t,typeof_field(_state, _field)), \
+        .set_default = true,                                     \
         .defval.u  = (bool)_defval,                              \
         }
@@ -60,15 +69,24 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(_prop),                                          \
         .offset    = offsetof(_state, _field)                           \
             + type_check(_type, typeof_field(_state, _field)),          \
+        .set_default = true,                                            \
         .defval.u  = (_type)_defval,                                    \
         }
 
+#define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type)
 { \
+        .name      = (_name),                                           \
+        .info      = &(_prop),                                          \
+        .offset    = offsetof(_state, _field)                           \
+            + type_check(_type, typeof_field(_state, _field)),          \
+        }
+
 #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) {       \
         .name      = (_name),                                           \
         .info      = &(qdev_prop_bit64),                                \
         .bitnr    = (_bit),                                             \
         .offset    = offsetof(_state, _field)                           \
             + type_check(uint64_t, typeof_field(_state, _field)),       \
+        .set_default = true,                                            \
         .defval.u  = (bool)_defval,                                     \
         }
@@ -77,6 +95,7 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(qdev_prop_bool),                          \
         .offset    = offsetof(_state, _field)                    \
             + type_check(bool, typeof_field(_state, _field)),    \
+        .set_default = true,                                     \
         .defval.u    = (bool)_defval,                            \
         }
 
@@ -110,6 +129,7 @@ extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .set_default = true,                                            \
         .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
hw/core/qdev.c +0 -0
index 849952a..96965a7 100644

hw/core/qdev.c void qdev_property_add_static(DeviceState *dev, Property · line 797

Quoted 6 lines
hw/core/qdev.c +1 -1
@@ -793,7 +793,7 @@ void qdev_property_add_static(DeviceState *dev, Property
 *prop,
                                     prop->info->description,
                                     &error_abort);
 
-    if (prop->info->set_default_value) {
+    if (prop->set_default) {

Not sure if it's worth to have an assert(prop->info->set_default_value), probably not necessary.

Quoted 7 lines
hw/core/qdev.c +0 -0
         prop->info->set_default_value(obj, prop);
     }

}
--
2.7.4

#8 lore raw

Re: [Qemu-arm] [PATCH 2/3] qdev: support properties which don't set a default value

Quoted 11 lines · Marc-André Lureau

hw/core/qdev.c void qdev_property_add_static(DeviceState *dev, Property · line 797

Quoted 7 lines
hw/core/qdev.c +1 -1
@@ -793,7 +793,7 @@ void qdev_property_add_static(DeviceState *dev, Property
 *prop,
                                     prop->info->description,
                                     &error_abort);
 
-    if (prop->info->set_default_value) {
+    if (prop->set_default) {

Not sure if it's worth to have an assert(prop->info->set_default_value), probably not necessary.

Quoted 1 line
hw/core/qdev.c +0 -0
         prop->info->set_default_value(obj, prop);

Yes, we'll just segfault on the NULL pointer immediately anyway. I tend to the view that assertions are for turning obscure and delayed failures into clearer and more immediate failures, and in this case the failure is already pretty clear and immediate.

thanks
-- PMM

Reviewed-by on v1 2/3
#9 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 2/3] qdev: support properties which don't set a default value

In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

In some situations it's useful to have a qdev property which doesn't automatically set its default value when qdev_property_add_static is called (for instance when the default value is not constant).

Support this by adding a flag to the Property struct indicating whether to set the default value. This replaces the existing test for whether the PorpertyInfo set_default_value function pointer is

PropertyInfo

Quoted 7 lines

NULL, and we set the .set_default field to true for all those cases of struct Property which use a PropertyInfo with a non-NULL set_default_value, so behaviour remains the same as before.

We define two new macros DEFINE_PROP_SIGNED_NODEFAULT and DEFINE_PROP_UNSIGNED_NODEFAULT, to cover the most plausible use cases of wanting to set an integer property with no default value.

Your text makes me wonder what is supposed to set the default value then. Glancing ahead to PATCH 3, it looks like method instance_init() is. Can you think of a scenario where something else sets it?

Quoted 18 lines

---
Since the previous patch fixed the only case that was using a default value but not explicitly setting .defval.[ui], this patch is a bit easier to review: .set_default = true is added in exactly the places that initialize .defval.[ui].

---
 include/hw/qdev-core.h       |  1 +
 include/hw/qdev-properties.h | 20 ++++++++++++++++++++
 hw/core/qdev.c               |  2 +-
 3 files changed, 22 insertions(+), 1 deletion(-)
include/hw/qdev-core.h +0 -0
index 9d7c1c0..d110163 100644

include/hw/qdev-core.h struct Property { · line 229

Quoted 4 lines
include/hw/qdev-core.h +1 -0
@@ -226,6 +226,7 @@ struct Property {
     PropertyInfo *info;
     ptrdiff_t    offset;
     uint8_t      bitnr;
+    bool         set_default;

Your chance to add the very first comment to struct Property (its existing undocumentedness is an embarrassment, but not this patch's problem). Let's write down that this flag governs where (integer) default values come from, either from member devfal or from method instance_init() or whatever.

Quoted 8 lines
include/hw/qdev-core.h +0 -0
     union {
         int64_t i;
         uint64_t u;
include/hw/qdev-properties.h +0 -0
index 36d040c..66816a5 100644

include/hw/qdev-properties.h extern PropertyInfo qdev_prop_arraylen; · line 48

Quoted 6 lines
include/hw/qdev-properties.h +1 -0
@@ -43,15 +43,24 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(_prop),                                          \
         .offset    = offsetof(_state, _field)                           \
             + type_check(_type,typeof_field(_state, _field)),           \
+        .set_default = true,                                            \
         .defval.i  = (_type)_defval,                                    \
         }

If you flip the sense of the flag, you don't need to mess with the PropertyInfo, and don't need the note referring reviewers to the previous patch. However, the case "use .defval" already requires an initializer, so adding one for .set_default seems fair. Okay.

Quoted 64 lines
include/hw/qdev-properties.h +19 -0
+#define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
+        .name      = (_name),                                           \
+        .info      = &(_prop),                                          \
+        .offset    = offsetof(_state, _field)                           \
+            + type_check(_type, typeof_field(_state, _field)),          \
+        }
+
 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
         .name      = (_name),                                    \
         .info      = &(qdev_prop_bit),                           \
         .bitnr    = (_bit),                                      \
         .offset    = offsetof(_state, _field)                    \
             + type_check(uint32_t,typeof_field(_state, _field)), \
+        .set_default = true,                                     \
         .defval.u  = (bool)_defval,                              \
         }
 
@@ -60,15 +69,24 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(_prop),                                          \
         .offset    = offsetof(_state, _field)                           \
             + type_check(_type, typeof_field(_state, _field)),          \
+        .set_default = true,                                            \
         .defval.u  = (_type)_defval,                                    \
         }
 
+#define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
+        .name      = (_name),                                           \
+        .info      = &(_prop),                                          \
+        .offset    = offsetof(_state, _field)                           \
+            + type_check(_type, typeof_field(_state, _field)),          \
+        }
+
 #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) {       \
         .name      = (_name),                                           \
         .info      = &(qdev_prop_bit64),                                \
         .bitnr    = (_bit),                                             \
         .offset    = offsetof(_state, _field)                           \
             + type_check(uint64_t, typeof_field(_state, _field)),       \
+        .set_default = true,                                            \
         .defval.u  = (bool)_defval,                                     \
         }
 
@@ -77,6 +95,7 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(qdev_prop_bool),                          \
         .offset    = offsetof(_state, _field)                    \
             + type_check(bool, typeof_field(_state, _field)),    \
+        .set_default = true,                                     \
         .defval.u    = (bool)_defval,                            \
         }
 
@@ -110,6 +129,7 @@ extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .set_default = true,                                            \
         .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
hw/core/qdev.c +0 -0
index 849952a..96965a7 100644

hw/core/qdev.c void qdev_property_add_static(DeviceState *dev, Property *prop, · line 799

Quoted 8 lines
hw/core/qdev.c +1 -1
@@ -793,7 +793,7 @@ void qdev_property_add_static(DeviceState *dev, Property *prop,
                                     prop->info->description,
                                     &error_abort);
 
-    if (prop->info->set_default_value) {
+    if (prop->set_default) {
         prop->info->set_default_value(obj, prop);
     }
 }

Preferably with a suitable comment on member set_default and an improved commit message:

#10 lore raw

Re: [Qemu-devel] [PATCH 2/3] qdev: support properties which don't set a default value

Quoted 16 lines · Markus Armbruster
Quoted 7 lines · Peter Maydell

In some situations it's useful to have a qdev property which doesn't automatically set its default value when qdev_property_add_static is called (for instance when the default value is not constant).

Support this by adding a flag to the Property struct indicating whether to set the default value. This replaces the existing test for whether the PorpertyInfo set_default_value function pointer is

PropertyInfo

Quoted 4 lines

NULL, and we set the .set_default field to true for all those cases of struct Property which use a PropertyInfo with a non-NULL set_default_value, so behaviour remains the same as before.

Quoted 7 lines
Quoted 3 lines

We define two new macros DEFINE_PROP_SIGNED_NODEFAULT and DEFINE_PROP_UNSIGNED_NODEFAULT, to cover the most plausible use cases of wanting to set an integer property with no default value.

Your text makes me wonder what is supposed to set the default value then. Glancing ahead to PATCH 3, it looks like method instance_init() is. Can you think of a scenario where something else sets it?

Yes, instance_init is the obvious place (though in fact pretty much anything that runs before the user of the device sets properties will do).

Quoted 11 lines
Quoted 11 lines
     union {
         int64_t i;
         uint64_t u;
include/hw/qdev-properties.h +0 -0
index 36d040c..66816a5 100644
@@ -43,15 +43,24 @@ extern PropertyInfo qdev_prop_arraylen;
         .info      = &(_prop),                                          \
         .offset    = offsetof(_state, _field)                           \
             + type_check(_type,typeof_field(_state, _field)),           \
Quoted 8 lines

include/hw/qdev-properties.h extern PropertyInfo qdev_prop_arraylen; · line 48

Quoted 3 lines
include/hw/qdev-properties.h +1 -0
+        .set_default = true,                                            \
         .defval.i  = (_type)_defval,                                    \
         }

If you flip the sense of the flag, you don't need to mess with the PropertyInfo, and don't need the note referring reviewers to the previous patch. However, the case "use .defval" already requires an initializer, so adding one for .set_default seems fair. Okay.

Having the flag this way round was your idea :-) Putting it the other way round might have been a smaller change in some sense (you'd have had to retain the check on set_default_value being NULL), but I do think it's nicer in the end...

Quoted 3 lines

Preferably with a suitable comment on member set_default and an improved commit message:

thanks
-- PMM

#11 lore raw

Re: [Qemu-devel] [PATCH 2/3] qdev: support properties which don't set a default value

In reply to Markus Armbruster
Quoted 16 lines · Markus Armbruster
Quoted 7 lines · Peter Maydell

In some situations it's useful to have a qdev property which doesn't automatically set its default value when qdev_property_add_static is called (for instance when the default value is not constant).

Support this by adding a flag to the Property struct indicating whether to set the default value. This replaces the existing test for whether the PorpertyInfo set_default_value function pointer is

PropertyInfo

Quoted 4 lines

NULL, and we set the .set_default field to true for all those cases of struct Property which use a PropertyInfo with a non-NULL set_default_value, so behaviour remains the same as before.

Quoted 7 lines
Quoted 3 lines

We define two new macros DEFINE_PROP_SIGNED_NODEFAULT and DEFINE_PROP_UNSIGNED_NODEFAULT, to cover the most plausible use cases of wanting to set an integer property with no default value.

Your text makes me wonder what is supposed to set the default value then. Glancing ahead to PATCH 3, it looks like method instance_init() is. Can you think of a scenario where something else sets it?

OK, proposed extra paragraph for commit message:

This gives us the semantics of:
* if .set_default is true and .info->set_default_value is not NULL, then .defval is used as the the default value of the property
* otherwise, the property system does not set any default, and the field will retain whatever initial value it was given by the device's .instance_init method

(to go just before the "We define two new macros..." para.)

Quoted 5 lines
Quoted 5 lines
include/hw/qdev-core.h +0 -0
@@ -226,6 +226,7 @@ struct Property {
     PropertyInfo *info;
     ptrdiff_t    offset;
Quoted 8 lines

include/hw/qdev-core.h struct Property { · line 229

Quoted 2 lines
include/hw/qdev-core.h +1 -0
     uint8_t      bitnr;
+    bool         set_default;

Your chance to add the very first comment to struct Property (its existing undocumentedness is an embarrassment, but not this patch's problem). Let's write down that this flag governs where (integer) default values come from, either from member devfal or from method instance_init() or whatever.

OK, how about
/**
* Property:
* @set_default: true if the default value should be set from @defval
* (if false then no default value is set by the property system
* and the field retains whatever value it was given by instance_init).
* @defval: default value for the property. This is used only if @set_default
* is true and @info->set_default_value is not NULL.
*/

?

thanks
-- PMM

#12 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 2/3] qdev: support properties which don't set a default value

Quoted 25 lines · Peter Maydell
Quoted 16 lines · Markus Armbruster
Quoted 7 lines · Peter Maydell

In some situations it's useful to have a qdev property which doesn't automatically set its default value when qdev_property_add_static is called (for instance when the default value is not constant).

Support this by adding a flag to the Property struct indicating whether to set the default value. This replaces the existing test for whether the PorpertyInfo set_default_value function pointer is

PropertyInfo

Quoted 4 lines

NULL, and we set the .set_default field to true for all those cases of struct Property which use a PropertyInfo with a non-NULL set_default_value, so behaviour remains the same as before.

Quoted 7 lines
Quoted 3 lines

We define two new macros DEFINE_PROP_SIGNED_NODEFAULT and DEFINE_PROP_UNSIGNED_NODEFAULT, to cover the most plausible use cases of wanting to set an integer property with no default value.

Your text makes me wonder what is supposed to set the default value then. Glancing ahead to PATCH 3, it looks like method instance_init() is. Can you think of a scenario where something else sets it?

Quoted 5 lines

OK, proposed extra paragraph for commit message:

This gives us the semantics of:
* if .set_default is true and .info->set_default_value is not NULL, then .defval is used as the the default value of the property

If I read your patch correctly, then this should be "if .set_default is true, then .info->set_default_value() must not be null, and .defval is used ..."

Quoted 20 lines

* otherwise, the property system does not set any default, and the field will retain whatever initial value it was given by the device's .instance_init method

(to go just before the "We define two new macros..." para.)

Quoted 5 lines
Quoted 5 lines
include/hw/qdev-core.h +0 -0
@@ -226,6 +226,7 @@ struct Property {
     PropertyInfo *info;
     ptrdiff_t    offset;
Quoted 8 lines

include/hw/qdev-core.h struct Property { · line 229

Quoted 2 lines
include/hw/qdev-core.h +1 -0
     uint8_t      bitnr;
+    bool         set_default;

Your chance to add the very first comment to struct Property (its existing undocumentedness is an embarrassment, but not this patch's problem). Let's write down that this flag governs where (integer) default values come from, either from member devfal or from method instance_init() or whatever.

Quoted 9 lines

OK, how about
/**
* Property:
* @set_default: true if the default value should be set from @defval
* (if false then no default value is set by the property system
* and the field retains whatever value it was given by instance_init).
* @defval: default value for the property. This is used only if @set_default
* is true and @info->set_default_value is not NULL.
*/

Again, I believe ->set_default_value() must not be null when ->set_default is true.

Quoted 5 lines

?

thanks
-- PMM

#13 lore raw

Re: [Qemu-devel] [PATCH 2/3] qdev: support properties which don't set a default value

Quoted 29 lines · Markus Armbruster
Quoted 27 lines · Peter Maydell
Quoted 16 lines · Markus Armbruster
Quoted 7 lines · Peter Maydell

In some situations it's useful to have a qdev property which doesn't automatically set its default value when qdev_property_add_static is called (for instance when the default value is not constant).

Support this by adding a flag to the Property struct indicating whether to set the default value. This replaces the existing test for whether the PorpertyInfo set_default_value function pointer is

PropertyInfo

Quoted 4 lines

NULL, and we set the .set_default field to true for all those cases of struct Property which use a PropertyInfo with a non-NULL set_default_value, so behaviour remains the same as before.

Quoted 7 lines
Quoted 3 lines

We define two new macros DEFINE_PROP_SIGNED_NODEFAULT and DEFINE_PROP_UNSIGNED_NODEFAULT, to cover the most plausible use cases of wanting to set an integer property with no default value.

Your text makes me wonder what is supposed to set the default value then. Glancing ahead to PATCH 3, it looks like method instance_init() is. Can you think of a scenario where something else sets it?

OK, proposed extra paragraph for commit message:

Quoted 7 lines
Quoted 3 lines

This gives us the semantics of:
* if .set_default is true and .info->set_default_value is not NULL, then .defval is used as the the default value of the property

If I read your patch correctly, then this should be "if .set_default is true, then .info->set_default_value() must not be null, and .defval is used ..."

Yes.

thanks
-- PMM

Reviewed-by
#14 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 2/3] qdev: support properties which don't set a default value

Quoted 34 lines · Peter Maydell
Quoted 33 lines · Markus Armbruster
Quoted 30 lines · Peter Maydell
Quoted 16 lines · Markus Armbruster
Quoted 7 lines · Peter Maydell

In some situations it's useful to have a qdev property which doesn't automatically set its default value when qdev_property_add_static is called (for instance when the default value is not constant).

Support this by adding a flag to the Property struct indicating whether to set the default value. This replaces the existing test for whether the PorpertyInfo set_default_value function pointer is

PropertyInfo

Quoted 4 lines

NULL, and we set the .set_default field to true for all those cases of struct Property which use a PropertyInfo with a non-NULL set_default_value, so behaviour remains the same as before.

Quoted 7 lines
Quoted 3 lines

We define two new macros DEFINE_PROP_SIGNED_NODEFAULT and DEFINE_PROP_UNSIGNED_NODEFAULT, to cover the most plausible use cases of wanting to set an integer property with no default value.

Your text makes me wonder what is supposed to set the default value then. Glancing ahead to PATCH 3, it looks like method instance_init() is. Can you think of a scenario where something else sets it?

OK, proposed extra paragraph for commit message:

This gives us the semantics of:
* if .set_default is true and .info->set_default_value is not NULL, then .defval is used as the the default value of the property

Quoted 5 lines
Quoted 3 lines

If I read your patch correctly, then this should be "if .set_default is true, then .info->set_default_value() must not be null, and .defval is used ..."

Yes.

Update your comment and commit message accordingly, and you may add

#15 lore raw

PATCH 3/3 target/arm: Make Cortex-M3 and M4 default to 8 PMSA regions

In reply to Peter Maydell

The Cortex-M3 and M4 CPUs always have 8 PMSA MPU regions (this isn't a configurable option for the hardware). Make the default value of the pmsav7-dregion property be set per-cpu, so we don't need to have every user of these CPUs set it manually. (The existing default of 16 is correct for the other PMSAv7 core, the Cortex-R5.)

---
 target/arm/cpu.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
target/arm/cpu.c +11 -1
index 28a9141..96d1f84 100644
@@ -543,8 +543,15 @@ static Property arm_cpu_has_pmu_property =
 static Property arm_cpu_has_mpu_property =
             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
 
+/* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
+ * because the CPU initfn will have already set cpu->pmsav7_dregion to
+ * the right value for that particular CPU type, and we don't want
+ * to override that with an incorrect constant value.
+ */
 static Property arm_cpu_pmsav7_dregion_property =
-            DEFINE_PROP_UINT32("pmsav7-dregion", ARMCPU, pmsav7_dregion, 16);
+            DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
+                                           pmsav7_dregion,
+                                           qdev_prop_uint32, uint32_t);
 
 static void arm_cpu_post_init(Object *obj)
 {
@@ -1054,6 +1061,7 @@ static void cortex_m3_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_V7);
     set_feature(&cpu->env, ARM_FEATURE_M);
     cpu->midr = 0x410fc231;
+    cpu->pmsav7_dregion = 8;
 }
 
 static void cortex_m4_initfn(Object *obj)
@@ -1064,6 +1072,7 @@ static void cortex_m4_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_M);
     set_feature(&cpu->env, ARM_FEATURE_THUMB_DSP);
     cpu->midr = 0x410fc240; /* r0p0 */
+    cpu->pmsav7_dregion = 8;
 }
 static void arm_v7m_class_init(ObjectClass *oc, void *data)
 {
@@ -1112,6 +1121,7 @@ static void cortex_r5_initfn(Object *obj)
     cpu->id_isar4 = 0x0010142;
     cpu->id_isar5 = 0x0;
     cpu->mp_is_up = true;
+    cpu->pmsav7_dregion = 16;
     define_arm_cp_regs(cpu, cortexr5_cp_reginfo);
 }
 
Signature · 1 line
2.7.4
#16 lore raw

Re: [Qemu-devel] [PATCH 3/3] target/arm: Make Cortex-M3 and M4 default to 8 PMSA regions

Hi

----- Original Message -----

Quoted 7 lines

The Cortex-M3 and M4 CPUs always have 8 PMSA MPU regions (this isn't a configurable option for the hardware). Make the default value of the pmsav7-dregion property be set per-cpu, so we don't need to have every user of these CPUs set it manually. (The existing default of 16 is correct for the other PMSAv7 core, the Cortex-R5.)

So until now that value was wrong for m3/m4 if I understand correctly.

Quoted 54 lines
---
 target/arm/cpu.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
target/arm/cpu.c +8 -1
index 28a9141..96d1f84 100644
@@ -543,8 +543,15 @@ static Property arm_cpu_has_pmu_property =
 static Property arm_cpu_has_mpu_property =
             DEFINE_PROP_BOOL("has-mpu", ARMCPU, has_mpu, true);
 
+/* This is like DEFINE_PROP_UINT32 but it doesn't set the default value,
+ * because the CPU initfn will have already set cpu->pmsav7_dregion to
+ * the right value for that particular CPU type, and we don't want
+ * to override that with an incorrect constant value.
+ */
 static Property arm_cpu_pmsav7_dregion_property =
-            DEFINE_PROP_UINT32("pmsav7-dregion", ARMCPU, pmsav7_dregion,
 16);
+            DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
+                                           pmsav7_dregion,
+                                           qdev_prop_uint32, uint32_t);
 
 static void arm_cpu_post_init(Object *obj)

{

target/arm/cpu.c +3 -1
@@ -1054,6 +1061,7 @@ static void cortex_m3_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_V7);
     set_feature(&cpu->env, ARM_FEATURE_M);
     cpu->midr = 0x410fc231;
+    cpu->pmsav7_dregion = 8;
 }
 
 static void cortex_m4_initfn(Object *obj)
@@ -1064,6 +1072,7 @@ static void cortex_m4_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_M);
     set_feature(&cpu->env, ARM_FEATURE_THUMB_DSP);
     cpu->midr = 0x410fc240; /* r0p0 */
+    cpu->pmsav7_dregion = 8;
 }
 static void arm_v7m_class_init(ObjectClass *oc, void *data)
 {
@@ -1112,6 +1121,7 @@ static void cortex_r5_initfn(Object *obj)
     cpu->id_isar4 = 0x0010142;
     cpu->id_isar5 = 0x0;
     cpu->mp_is_up = true;
+    cpu->pmsav7_dregion = 16;
     define_arm_cp_regs(cpu, cortexr5_cp_reginfo);
 }
 
--

2.7.4

#17 lore raw

Re: [Qemu-arm] [PATCH 3/3] target/arm: Make Cortex-M3 and M4 default to 8 PMSA regions

Quoted 10 lines · Marc-André Lureau

----- Original Message -----

Quoted 7 lines

The Cortex-M3 and M4 CPUs always have 8 PMSA MPU regions (this isn't a configurable option for the hardware). Make the default value of the pmsav7-dregion property be set per-cpu, so we don't need to have every user of these CPUs set it manually. (The existing default of 16 is correct for the other PMSAv7 core, the Cortex-R5.)

So until now that value was wrong for m3/m4 if I understand correctly.

Correct. (It was too high, so didn't cause a problem for guests typically -- a guest assuming 8 memory regions would just not use the registers associated with the extra unexpected ones.)

thanks
-- PMM

Reviewed-by
#18 lore raw

Re: [Qemu-devel] [PATCH 3/3] target/arm: Make Cortex-M3 and M4 default to 8 PMSA regions

hi

----- Original Message -----

Quoted 9 lines
Quoted 7 lines · Marc-André Lureau

----- Original Message -----

Quoted 6 lines

The Cortex-M3 and M4 CPUs always have 8 PMSA MPU regions (this isn't a configurable option for the hardware). Make the default value of the pmsav7-dregion property be set per-cpu, so we don't need to have every user of these CPUs set it manually. (The existing default of 16 is correct for the other PMSAv7 core, the Cortex-R5.)

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

So until now that value was wrong for m3/m4 if I understand correctly.

Correct. (It was too high, so didn't cause a problem for guests typically -- a guest assuming 8 memory regions would just not use the registers associated with the extra unexpected ones.)

Would be nice to add that to the commit message

in any case: