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