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