xref: /qemu/include/hw/qdev-properties.h (revision eb9b25c6565d8c49a0db40f65a8a1f7932e81ff5)
1 #ifndef QEMU_QDEV_PROPERTIES_H
2 #define QEMU_QDEV_PROPERTIES_H
3 
4 #include "hw/qdev-core.h"
5 
6 /**
7  * Property:
8  * @set_default: true if the default value should be set from @defval,
9  *    in which case @info->set_default_value must not be NULL
10  *    (if false then no default value is set by the property system
11  *     and the field retains whatever value it was given by instance_init).
12  * @defval: default value for the property. This is used only if @set_default
13  *     is true.
14  */
15 struct Property {
16     const char   *name;
17     const PropertyInfo *info;
18     ptrdiff_t    offset;
19     const char   *link_type;
20     uint64_t     bitmask;
21     union {
22         int64_t i;
23         uint64_t u;
24     } defval;
25     const PropertyInfo *arrayinfo;
26     int          arrayoffset;
27     int          arrayfieldsize;
28     uint8_t      bitnr;
29     bool         set_default;
30 };
31 
32 struct PropertyInfo {
33     const char *type;
34     const char *description;
35     const QEnumLookup *enum_table;
36     bool realized_set_allowed; /* allow setting property on realized device */
37     int (*print)(Object *obj, const Property *prop, char *dest, size_t len);
38     void (*set_default_value)(ObjectProperty *op, const Property *prop);
39     ObjectProperty *(*create)(ObjectClass *oc, const char *name,
40                               const Property *prop);
41     ObjectPropertyAccessor *get;
42     ObjectPropertyAccessor *set;
43     ObjectPropertyRelease *release;
44 };
45 
46 
47 /*** qdev-properties.c ***/
48 
49 extern const PropertyInfo qdev_prop_bit;
50 extern const PropertyInfo qdev_prop_bit64;
51 extern const PropertyInfo qdev_prop_bool;
52 extern const PropertyInfo qdev_prop_uint8;
53 extern const PropertyInfo qdev_prop_uint16;
54 extern const PropertyInfo qdev_prop_uint32;
55 extern const PropertyInfo qdev_prop_int32;
56 extern const PropertyInfo qdev_prop_uint64;
57 extern const PropertyInfo qdev_prop_uint64_checkmask;
58 extern const PropertyInfo qdev_prop_int64;
59 extern const PropertyInfo qdev_prop_size;
60 extern const PropertyInfo qdev_prop_string;
61 extern const PropertyInfo qdev_prop_on_off_auto;
62 extern const PropertyInfo qdev_prop_size32;
63 extern const PropertyInfo qdev_prop_array;
64 extern const PropertyInfo qdev_prop_link;
65 
66 #define DEFINE_PROP(_name, _state, _field, _prop, _type, ...) {  \
67         .name      = (_name),                                    \
68         .info      = &(_prop),                                   \
69         .offset    = offsetof(_state, _field)                    \
70             + type_check(_type, typeof_field(_state, _field)),   \
71         __VA_ARGS__                                              \
72         }
73 
74 #define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) \
75     DEFINE_PROP(_name, _state, _field, _prop, _type,                     \
76                 .set_default = true,                                     \
77                 .defval.i    = (_type)_defval)
78 
79 #define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) \
80     DEFINE_PROP(_name, _state, _field, _prop, _type)
81 
82 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval)   \
83     DEFINE_PROP(_name, _state, _field, qdev_prop_bit, uint32_t, \
84                 .bitnr       = (_bit),                          \
85                 .set_default = true,                            \
86                 .defval.u    = (bool)_defval)
87 
88 #define DEFINE_PROP_UNSIGNED(_name, _state, _field, _defval, _prop, _type) \
89     DEFINE_PROP(_name, _state, _field, _prop, _type,                       \
90                 .set_default = true,                                       \
91                 .defval.u  = (_type)_defval)
92 
93 #define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) \
94     DEFINE_PROP(_name, _state, _field, _prop, _type)
95 
96 #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval)   \
97     DEFINE_PROP(_name, _state, _field, qdev_prop_bit64, uint64_t, \
98                 .bitnr    = (_bit),                               \
99                 .set_default = true,                              \
100                 .defval.u  = (bool)_defval)
101 
102 #define DEFINE_PROP_BOOL(_name, _state, _field, _defval)     \
103     DEFINE_PROP(_name, _state, _field, qdev_prop_bool, bool, \
104                 .set_default = true,                         \
105                 .defval.u    = (bool)_defval)
106 
107 /**
108  * The DEFINE_PROP_UINT64_CHECKMASK macro checks a user-supplied value
109  * against corresponding bitmask, rejects the value if it violates.
110  * The default value is set in instance_init().
111  */
112 #define DEFINE_PROP_UINT64_CHECKMASK(_name, _state, _field, _bitmask)   \
113     DEFINE_PROP(_name, _state, _field, qdev_prop_uint64_checkmask, uint64_t, \
114                 .bitmask    = (_bitmask),                     \
115                 .set_default = false)
116 
117 /**
118  * DEFINE_PROP_ARRAY:
119  * @_name: name of the array
120  * @_state: name of the device state structure type
121  * @_field: uint32_t field in @_state to hold the array length
122  * @_arrayfield: field in @_state (of type '@_arraytype *') which
123  *               will point to the array
124  * @_arrayprop: PropertyInfo defining what property the array elements have
125  * @_arraytype: C type of the array elements
126  *
127  * Define device properties for a variable-length array _name.  The array is
128  * represented as a list in the visitor interface.
129  *
130  * @_arraytype is required to be movable with memcpy().
131  *
132  * When the array property is set, the @_field member of the device
133  * struct is set to the array length, and @_arrayfield is set to point
134  * to the memory allocated for the array.
135  *
136  * It is the responsibility of the device deinit code to free the
137  * @_arrayfield memory.
138  */
139 #define DEFINE_PROP_ARRAY(_name, _state, _field,                        \
140                           _arrayfield, _arrayprop, _arraytype)          \
141     DEFINE_PROP(_name, _state, _field, qdev_prop_array, uint32_t,       \
142                 .set_default = true,                                    \
143                 .defval.u = 0,                                          \
144                 .arrayinfo = &(_arrayprop),                             \
145                 .arrayfieldsize = sizeof(_arraytype),                   \
146                 .arrayoffset = offsetof(_state, _arrayfield))
147 
148 #define DEFINE_PROP_LINK(_name, _state, _field, _type, _ptr_type)     \
149     DEFINE_PROP(_name, _state, _field, qdev_prop_link, _ptr_type,     \
150                 .link_type  = _type)
151 
152 #define DEFINE_PROP_UINT8(_n, _s, _f, _d)                       \
153     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
154 #define DEFINE_PROP_UINT16(_n, _s, _f, _d)                      \
155     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
156 #define DEFINE_PROP_UINT32(_n, _s, _f, _d)                      \
157     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
158 #define DEFINE_PROP_INT32(_n, _s, _f, _d)                      \
159     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int32, int32_t)
160 #define DEFINE_PROP_UINT64(_n, _s, _f, _d)                      \
161     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
162 #define DEFINE_PROP_INT64(_n, _s, _f, _d)                      \
163     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int64, int64_t)
164 #define DEFINE_PROP_SIZE(_n, _s, _f, _d)                       \
165     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
166 #define DEFINE_PROP_STRING(_n, _s, _f)             \
167     DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
168 #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
169     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
170 #define DEFINE_PROP_SIZE32(_n, _s, _f, _d)                       \
171     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size32, uint32_t)
172 
173 /*
174  * Set properties between creation and realization.
175  *
176  * Returns: %true on success, %false on error.
177  */
178 bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
179                              BlockBackend *value, Error **errp);
180 
181 /*
182  * Set properties between creation and realization.
183  * @value must be valid.  Each property may be set at most once.
184  */
185 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
186 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
187 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
188 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
189 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
190 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
191 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
192 void qdev_prop_set_chr(DeviceState *dev, const char *name, Chardev *value);
193 void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
194 void qdev_prop_set_drive(DeviceState *dev, const char *name,
195                          BlockBackend *value);
196 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
197                            const uint8_t *value);
198 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
199 
200 /* Takes ownership of @values */
201 void qdev_prop_set_array(DeviceState *dev, const char *name, QList *values);
202 
203 void *object_field_prop_ptr(Object *obj, const Property *prop);
204 
205 void qdev_prop_register_global(GlobalProperty *prop);
206 const GlobalProperty *qdev_find_global_prop(Object *obj,
207                                             const char *name);
208 int qdev_prop_check_globals(void);
209 void qdev_prop_set_globals(DeviceState *dev);
210 void error_set_from_qdev_prop_error(Error **errp, int ret, Object *obj,
211                                     const char *name, const char *value);
212 
213 /**
214  * qdev_property_add_static:
215  * @dev: Device to add the property to.
216  * @prop: The qdev property definition.
217  *
218  * Add a static QOM property to @dev for qdev property @prop.
219  * On error, store error in @errp.  Static properties access data in a struct.
220  * The type of the QOM property is derived from prop->info.
221  */
222 void qdev_property_add_static(DeviceState *dev, const Property *prop);
223 
224 /**
225  * qdev_alias_all_properties: Create aliases on source for all target properties
226  * @target: Device which has properties to be aliased
227  * @source: Object to add alias properties to
228  *
229  * Add alias properties to the @source object for all properties on the @target
230  * DeviceState.
231  *
232  * This is useful when @target is an internal implementation object
233  * owned by @source, and you want to expose all the properties of that
234  * implementation object as properties on the @source object so that users
235  * of @source can set them.
236  */
237 void qdev_alias_all_properties(DeviceState *target, Object *source);
238 
239 /**
240  * @qdev_prop_set_after_realize:
241  * @dev: device
242  * @name: name of property
243  * @errp: indirect pointer to Error to be set
244  * Set the Error object to report that an attempt was made to set a property
245  * on a device after it has already been realized. This is a utility function
246  * which allows property-setter functions to easily report the error in
247  * a friendly format identifying both the device and the property.
248  */
249 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
250                                  Error **errp);
251 
252 /**
253  * qdev_prop_allow_set_link_before_realize:
254  *
255  * Set the #Error object if an attempt is made to set the link after realize.
256  * This function should be used as the check() argument to
257  * object_property_add_link().
258  */
259 void qdev_prop_allow_set_link_before_realize(const Object *obj,
260                                              const char *name,
261                                              Object *val, Error **errp);
262 
263 #endif
264