diff mbox series

[v1,1/8] qdev-properties: Add read-only 64 bit property

Message ID 20210901090804.7139-2-maxim.davydov@virtuozzo.com (mailing list archive)
State New, archived
Headers show
Series Virtio features acknowledged by guest | expand

Commit Message

Maxim Davydov Sept. 1, 2021, 9:07 a.m. UTC
In some situations, we need a property that tracks the bit but
can't change it (for instance, guest features of virtio device).

Signed-off-by: Maxim Davydov <maxim.davydov@virtuozzo.com>
---
 hw/core/qdev-properties.c    | 32 ++++++++++++++++++++++++++++++++
 include/hw/qdev-properties.h |  5 +++++
 2 files changed, 37 insertions(+)
diff mbox series

Patch

diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index 50f4094..d7b0436 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -231,6 +231,38 @@  const PropertyInfo qdev_prop_bit64 = {
     .set_default_value = set_default_value_bool,
 };
 
+/* Read-only Bit64 */
+
+static void prop_set_read_only_bit64(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    return;
+}
+
+static uint64_t qdev_get_prop_read_only_mask64(Property *prop)
+{
+    assert(prop->info == &qdev_prop_read_only_bit64);
+    return 0x1ull << prop->bitnr;
+}
+
+static void prop_get_read_only_bit64(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+    uint64_t *p = object_field_prop_ptr(obj, prop);
+    bool value = (*p & qdev_get_prop_read_only_mask64(prop)) != 0;
+
+    visit_type_bool(v, name, &value, errp);
+}
+
+const PropertyInfo qdev_prop_read_only_bit64 = {
+    .name  = "bool",
+    .description = "on/off",
+    .get   = prop_get_read_only_bit64,
+    .set   = prop_set_read_only_bit64,
+    .set_default_value = set_default_value_bool,
+};
+
 /* --- bool --- */
 
 static void get_bool(Object *obj, Visitor *v, const char *name, void *opaque,
diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
index 0ef97d6..4c4bac7 100644
--- a/include/hw/qdev-properties.h
+++ b/include/hw/qdev-properties.h
@@ -46,6 +46,7 @@  struct PropertyInfo {
 
 extern const PropertyInfo qdev_prop_bit;
 extern const PropertyInfo qdev_prop_bit64;
+extern const PropertyInfo qdev_prop_read_only_bit64;
 extern const PropertyInfo qdev_prop_bool;
 extern const PropertyInfo qdev_prop_enum;
 extern const PropertyInfo qdev_prop_uint8;
@@ -102,6 +103,10 @@  extern const PropertyInfo qdev_prop_link;
                 .set_default = true,                         \
                 .defval.u    = (bool)_defval)
 
+#define DEFINE_PROP_READ_ONLY_BIT64(_name, _state, _field, _bit)            \
+    DEFINE_PROP(_name, _state, _field, qdev_prop_read_only_bit64, uint64_t, \
+                .bitnr = (_bit))
+
 #define PROP_ARRAY_LEN_PREFIX "len-"
 
 /**