xref: /openbmc/qemu/include/hw/qdev-properties.h (revision ce35e229)
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     uint8_t      bitnr;
20     bool         set_default;
21     union {
22         int64_t i;
23         uint64_t u;
24     } defval;
25     int          arrayoffset;
26     const PropertyInfo *arrayinfo;
27     int          arrayfieldsize;
28     const char   *link_type;
29 };
30 
31 struct PropertyInfo {
32     const char *name;
33     const char *description;
34     const QEnumLookup *enum_table;
35     int (*print)(Object *obj, Property *prop, char *dest, size_t len);
36     void (*set_default_value)(ObjectProperty *op, const Property *prop);
37     void (*create)(ObjectClass *oc, Property *prop);
38     ObjectPropertyAccessor *get;
39     ObjectPropertyAccessor *set;
40     ObjectPropertyRelease *release;
41 };
42 
43 
44 /*** qdev-properties.c ***/
45 
46 extern const PropertyInfo qdev_prop_bit;
47 extern const PropertyInfo qdev_prop_bit64;
48 extern const PropertyInfo qdev_prop_bool;
49 extern const PropertyInfo qdev_prop_enum;
50 extern const PropertyInfo qdev_prop_uint8;
51 extern const PropertyInfo qdev_prop_uint16;
52 extern const PropertyInfo qdev_prop_uint32;
53 extern const PropertyInfo qdev_prop_int32;
54 extern const PropertyInfo qdev_prop_uint64;
55 extern const PropertyInfo qdev_prop_int64;
56 extern const PropertyInfo qdev_prop_size;
57 extern const PropertyInfo qdev_prop_string;
58 extern const PropertyInfo qdev_prop_tpm;
59 extern const PropertyInfo qdev_prop_on_off_auto;
60 extern const PropertyInfo qdev_prop_size32;
61 extern const PropertyInfo qdev_prop_arraylen;
62 extern const PropertyInfo qdev_prop_link;
63 
64 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
65         .name      = (_name),                                    \
66         .info      = &(_prop),                                   \
67         .offset    = offsetof(_state, _field)                    \
68             + type_check(_type, typeof_field(_state, _field)),   \
69         }
70 
71 #define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) { \
72         .name      = (_name),                                           \
73         .info      = &(_prop),                                          \
74         .offset    = offsetof(_state, _field)                           \
75             + type_check(_type,typeof_field(_state, _field)),           \
76         .set_default = true,                                            \
77         .defval.i  = (_type)_defval,                                    \
78         }
79 
80 #define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
81         .name      = (_name),                                           \
82         .info      = &(_prop),                                          \
83         .offset    = offsetof(_state, _field)                           \
84             + type_check(_type, typeof_field(_state, _field)),          \
85         }
86 
87 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
88         .name      = (_name),                                    \
89         .info      = &(qdev_prop_bit),                           \
90         .bitnr    = (_bit),                                      \
91         .offset    = offsetof(_state, _field)                    \
92             + type_check(uint32_t,typeof_field(_state, _field)), \
93         .set_default = true,                                     \
94         .defval.u  = (bool)_defval,                              \
95         }
96 
97 #define DEFINE_PROP_UNSIGNED(_name, _state, _field, _defval, _prop, _type) { \
98         .name      = (_name),                                           \
99         .info      = &(_prop),                                          \
100         .offset    = offsetof(_state, _field)                           \
101             + type_check(_type, typeof_field(_state, _field)),          \
102         .set_default = true,                                            \
103         .defval.u  = (_type)_defval,                                    \
104         }
105 
106 #define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
107         .name      = (_name),                                           \
108         .info      = &(_prop),                                          \
109         .offset    = offsetof(_state, _field)                           \
110             + type_check(_type, typeof_field(_state, _field)),          \
111         }
112 
113 #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) {       \
114         .name      = (_name),                                           \
115         .info      = &(qdev_prop_bit64),                                \
116         .bitnr    = (_bit),                                             \
117         .offset    = offsetof(_state, _field)                           \
118             + type_check(uint64_t, typeof_field(_state, _field)),       \
119         .set_default = true,                                            \
120         .defval.u  = (bool)_defval,                                     \
121         }
122 
123 #define DEFINE_PROP_BOOL(_name, _state, _field, _defval) {       \
124         .name      = (_name),                                    \
125         .info      = &(qdev_prop_bool),                          \
126         .offset    = offsetof(_state, _field)                    \
127             + type_check(bool, typeof_field(_state, _field)),    \
128         .set_default = true,                                     \
129         .defval.u    = (bool)_defval,                            \
130         }
131 
132 #define PROP_ARRAY_LEN_PREFIX "len-"
133 
134 /**
135  * DEFINE_PROP_ARRAY:
136  * @_name: name of the array
137  * @_state: name of the device state structure type
138  * @_field: uint32_t field in @_state to hold the array length
139  * @_arrayfield: field in @_state (of type '@_arraytype *') which
140  *               will point to the array
141  * @_arrayprop: PropertyInfo defining what property the array elements have
142  * @_arraytype: C type of the array elements
143  *
144  * Define device properties for a variable-length array _name.  A
145  * static property "len-arrayname" is defined. When the device creator
146  * sets this property to the desired length of array, further dynamic
147  * properties "arrayname[0]", "arrayname[1]", ...  are defined so the
148  * device creator can set the array element values. Setting the
149  * "len-arrayname" property more than once is an error.
150  *
151  * When the array length is set, the @_field member of the device
152  * struct is set to the array length, and @_arrayfield is set to point
153  * to (zero-initialised) memory allocated for the array.  For a zero
154  * length array, @_field will be set to 0 and @_arrayfield to NULL.
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         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
161         .info = &(qdev_prop_arraylen),                                  \
162         .set_default = true,                                            \
163         .defval.u = 0,                                                  \
164         .offset = offsetof(_state, _field)                              \
165             + type_check(uint32_t, typeof_field(_state, _field)),       \
166         .arrayinfo = &(_arrayprop),                                     \
167         .arrayfieldsize = sizeof(_arraytype),                           \
168         .arrayoffset = offsetof(_state, _arrayfield),                   \
169         }
170 
171 #define DEFINE_PROP_LINK(_name, _state, _field, _type, _ptr_type) {     \
172         .name = (_name),                                                \
173         .info = &(qdev_prop_link),                                      \
174         .offset = offsetof(_state, _field)                              \
175             + type_check(_ptr_type, typeof_field(_state, _field)),      \
176         .link_type  = _type,                                            \
177         }
178 
179 #define DEFINE_PROP_UINT8(_n, _s, _f, _d)                       \
180     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
181 #define DEFINE_PROP_UINT16(_n, _s, _f, _d)                      \
182     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
183 #define DEFINE_PROP_UINT32(_n, _s, _f, _d)                      \
184     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
185 #define DEFINE_PROP_INT32(_n, _s, _f, _d)                      \
186     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int32, int32_t)
187 #define DEFINE_PROP_UINT64(_n, _s, _f, _d)                      \
188     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
189 #define DEFINE_PROP_INT64(_n, _s, _f, _d)                      \
190     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int64, int64_t)
191 #define DEFINE_PROP_SIZE(_n, _s, _f, _d)                       \
192     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
193 #define DEFINE_PROP_STRING(_n, _s, _f)             \
194     DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
195 #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
196     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
197 #define DEFINE_PROP_SIZE32(_n, _s, _f, _d)                       \
198     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size32, uint32_t)
199 
200 #define DEFINE_PROP_END_OF_LIST()               \
201     {}
202 
203 /*
204  * Set properties between creation and realization.
205  *
206  * Returns: %true on success, %false on error.
207  */
208 bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
209                              BlockBackend *value, Error **errp);
210 
211 /*
212  * Set properties between creation and realization.
213  * @value must be valid.  Each property may be set at most once.
214  */
215 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
216 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
217 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
218 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
219 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
220 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
221 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
222 void qdev_prop_set_chr(DeviceState *dev, const char *name, Chardev *value);
223 void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
224 void qdev_prop_set_drive(DeviceState *dev, const char *name,
225                          BlockBackend *value);
226 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
227                            const uint8_t *value);
228 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
229 
230 void *qdev_get_prop_ptr(Object *obj, Property *prop);
231 
232 void qdev_prop_register_global(GlobalProperty *prop);
233 const GlobalProperty *qdev_find_global_prop(Object *obj,
234                                             const char *name);
235 int qdev_prop_check_globals(void);
236 void qdev_prop_set_globals(DeviceState *dev);
237 void error_set_from_qdev_prop_error(Error **errp, int ret, Object *obj,
238                                     Property *prop, const char *value);
239 
240 /**
241  * qdev_property_add_static:
242  * @dev: Device to add the property to.
243  * @prop: The qdev property definition.
244  *
245  * Add a static QOM property to @dev for qdev property @prop.
246  * On error, store error in @errp.  Static properties access data in a struct.
247  * The type of the QOM property is derived from prop->info.
248  */
249 void qdev_property_add_static(DeviceState *dev, Property *prop);
250 
251 /**
252  * qdev_alias_all_properties: Create aliases on source for all target properties
253  * @target: Device which has properties to be aliased
254  * @source: Object to add alias properties to
255  *
256  * Add alias properties to the @source object for all qdev properties on
257  * the @target DeviceState.
258  *
259  * This is useful when @target is an internal implementation object
260  * owned by @source, and you want to expose all the properties of that
261  * implementation object as properties on the @source object so that users
262  * of @source can set them.
263  */
264 void qdev_alias_all_properties(DeviceState *target, Object *source);
265 
266 /**
267  * @qdev_prop_set_after_realize:
268  * @dev: device
269  * @name: name of property
270  * @errp: indirect pointer to Error to be set
271  * Set the Error object to report that an attempt was made to set a property
272  * on a device after it has already been realized. This is a utility function
273  * which allows property-setter functions to easily report the error in
274  * a friendly format identifying both the device and the property.
275  */
276 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
277                                  Error **errp);
278 
279 /**
280  * qdev_prop_allow_set_link_before_realize:
281  *
282  * Set the #Error object if an attempt is made to set the link after realize.
283  * This function should be used as the check() argument to
284  * object_property_add_link().
285  */
286 void qdev_prop_allow_set_link_before_realize(const Object *obj,
287                                              const char *name,
288                                              Object *val, Error **errp);
289 
290 #endif
291