xref: /openbmc/qemu/include/hw/qdev-properties.h (revision c63ca4ff)
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_chr;
59 extern const PropertyInfo qdev_prop_tpm;
60 extern const PropertyInfo qdev_prop_macaddr;
61 extern const PropertyInfo qdev_prop_reserved_region;
62 extern const PropertyInfo qdev_prop_on_off_auto;
63 extern const PropertyInfo qdev_prop_multifd_compression;
64 extern const PropertyInfo qdev_prop_losttickpolicy;
65 extern const PropertyInfo qdev_prop_blockdev_on_error;
66 extern const PropertyInfo qdev_prop_bios_chs_trans;
67 extern const PropertyInfo qdev_prop_fdc_drive_type;
68 extern const PropertyInfo qdev_prop_drive;
69 extern const PropertyInfo qdev_prop_drive_iothread;
70 extern const PropertyInfo qdev_prop_netdev;
71 extern const PropertyInfo qdev_prop_pci_devfn;
72 extern const PropertyInfo qdev_prop_size32;
73 extern const PropertyInfo qdev_prop_blocksize;
74 extern const PropertyInfo qdev_prop_pci_host_devaddr;
75 extern const PropertyInfo qdev_prop_uuid;
76 extern const PropertyInfo qdev_prop_arraylen;
77 extern const PropertyInfo qdev_prop_audiodev;
78 extern const PropertyInfo qdev_prop_link;
79 extern const PropertyInfo qdev_prop_off_auto_pcibar;
80 extern const PropertyInfo qdev_prop_pcie_link_speed;
81 extern const PropertyInfo qdev_prop_pcie_link_width;
82 
83 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
84         .name      = (_name),                                    \
85         .info      = &(_prop),                                   \
86         .offset    = offsetof(_state, _field)                    \
87             + type_check(_type, typeof_field(_state, _field)),   \
88         }
89 
90 #define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) { \
91         .name      = (_name),                                           \
92         .info      = &(_prop),                                          \
93         .offset    = offsetof(_state, _field)                           \
94             + type_check(_type,typeof_field(_state, _field)),           \
95         .set_default = true,                                            \
96         .defval.i  = (_type)_defval,                                    \
97         }
98 
99 #define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
100         .name      = (_name),                                           \
101         .info      = &(_prop),                                          \
102         .offset    = offsetof(_state, _field)                           \
103             + type_check(_type, typeof_field(_state, _field)),          \
104         }
105 
106 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
107         .name      = (_name),                                    \
108         .info      = &(qdev_prop_bit),                           \
109         .bitnr    = (_bit),                                      \
110         .offset    = offsetof(_state, _field)                    \
111             + type_check(uint32_t,typeof_field(_state, _field)), \
112         .set_default = true,                                     \
113         .defval.u  = (bool)_defval,                              \
114         }
115 
116 #define DEFINE_PROP_UNSIGNED(_name, _state, _field, _defval, _prop, _type) { \
117         .name      = (_name),                                           \
118         .info      = &(_prop),                                          \
119         .offset    = offsetof(_state, _field)                           \
120             + type_check(_type, typeof_field(_state, _field)),          \
121         .set_default = true,                                            \
122         .defval.u  = (_type)_defval,                                    \
123         }
124 
125 #define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
126         .name      = (_name),                                           \
127         .info      = &(_prop),                                          \
128         .offset    = offsetof(_state, _field)                           \
129             + type_check(_type, typeof_field(_state, _field)),          \
130         }
131 
132 #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) {       \
133         .name      = (_name),                                           \
134         .info      = &(qdev_prop_bit64),                                \
135         .bitnr    = (_bit),                                             \
136         .offset    = offsetof(_state, _field)                           \
137             + type_check(uint64_t, typeof_field(_state, _field)),       \
138         .set_default = true,                                            \
139         .defval.u  = (bool)_defval,                                     \
140         }
141 
142 #define DEFINE_PROP_BOOL(_name, _state, _field, _defval) {       \
143         .name      = (_name),                                    \
144         .info      = &(qdev_prop_bool),                          \
145         .offset    = offsetof(_state, _field)                    \
146             + type_check(bool, typeof_field(_state, _field)),    \
147         .set_default = true,                                     \
148         .defval.u    = (bool)_defval,                            \
149         }
150 
151 #define PROP_ARRAY_LEN_PREFIX "len-"
152 
153 /**
154  * DEFINE_PROP_ARRAY:
155  * @_name: name of the array
156  * @_state: name of the device state structure type
157  * @_field: uint32_t field in @_state to hold the array length
158  * @_arrayfield: field in @_state (of type '@_arraytype *') which
159  *               will point to the array
160  * @_arrayprop: PropertyInfo defining what property the array elements have
161  * @_arraytype: C type of the array elements
162  *
163  * Define device properties for a variable-length array _name.  A
164  * static property "len-arrayname" is defined. When the device creator
165  * sets this property to the desired length of array, further dynamic
166  * properties "arrayname[0]", "arrayname[1]", ...  are defined so the
167  * device creator can set the array element values. Setting the
168  * "len-arrayname" property more than once is an error.
169  *
170  * When the array length is set, the @_field member of the device
171  * struct is set to the array length, and @_arrayfield is set to point
172  * to (zero-initialised) memory allocated for the array.  For a zero
173  * length array, @_field will be set to 0 and @_arrayfield to NULL.
174  * It is the responsibility of the device deinit code to free the
175  * @_arrayfield memory.
176  */
177 #define DEFINE_PROP_ARRAY(_name, _state, _field,                        \
178                           _arrayfield, _arrayprop, _arraytype) {        \
179         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
180         .info = &(qdev_prop_arraylen),                                  \
181         .set_default = true,                                            \
182         .defval.u = 0,                                                  \
183         .offset = offsetof(_state, _field)                              \
184             + type_check(uint32_t, typeof_field(_state, _field)),       \
185         .arrayinfo = &(_arrayprop),                                     \
186         .arrayfieldsize = sizeof(_arraytype),                           \
187         .arrayoffset = offsetof(_state, _arrayfield),                   \
188         }
189 
190 #define DEFINE_PROP_LINK(_name, _state, _field, _type, _ptr_type) {     \
191         .name = (_name),                                                \
192         .info = &(qdev_prop_link),                                      \
193         .offset = offsetof(_state, _field)                              \
194             + type_check(_ptr_type, typeof_field(_state, _field)),      \
195         .link_type  = _type,                                            \
196         }
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_PCI_DEVFN(_n, _s, _f, _d)                   \
213     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
214 
215 #define DEFINE_PROP_CHR(_n, _s, _f)             \
216     DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharBackend)
217 #define DEFINE_PROP_STRING(_n, _s, _f)             \
218     DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
219 #define DEFINE_PROP_NETDEV(_n, _s, _f)             \
220     DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, NICPeers)
221 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
222     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockBackend *)
223 #define DEFINE_PROP_DRIVE_IOTHREAD(_n, _s, _f) \
224     DEFINE_PROP(_n, _s, _f, qdev_prop_drive_iothread, BlockBackend *)
225 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
226     DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
227 #define DEFINE_PROP_RESERVED_REGION(_n, _s, _f)         \
228     DEFINE_PROP(_n, _s, _f, qdev_prop_reserved_region, ReservedRegion)
229 #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
230     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
231 #define DEFINE_PROP_MULTIFD_COMPRESSION(_n, _s, _f, _d) \
232     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_multifd_compression, \
233                        MultiFDCompression)
234 #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
235     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
236                         LostTickPolicy)
237 #define DEFINE_PROP_BLOCKDEV_ON_ERROR(_n, _s, _f, _d) \
238     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_blockdev_on_error, \
239                         BlockdevOnError)
240 #define DEFINE_PROP_BIOS_CHS_TRANS(_n, _s, _f, _d) \
241     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_bios_chs_trans, int)
242 #define DEFINE_PROP_SIZE32(_n, _s, _f, _d)                       \
243     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size32, uint32_t)
244 #define DEFINE_PROP_BLOCKSIZE(_n, _s, _f) \
245     DEFINE_PROP_UNSIGNED(_n, _s, _f, 0, qdev_prop_blocksize, uint32_t)
246 #define DEFINE_PROP_PCI_HOST_DEVADDR(_n, _s, _f) \
247     DEFINE_PROP(_n, _s, _f, qdev_prop_pci_host_devaddr, PCIHostDeviceAddress)
248 #define DEFINE_PROP_OFF_AUTO_PCIBAR(_n, _s, _f, _d) \
249     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_off_auto_pcibar, \
250                         OffAutoPCIBAR)
251 #define DEFINE_PROP_PCIE_LINK_SPEED(_n, _s, _f, _d) \
252     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pcie_link_speed, \
253                         PCIExpLinkSpeed)
254 #define DEFINE_PROP_PCIE_LINK_WIDTH(_n, _s, _f, _d) \
255     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pcie_link_width, \
256                         PCIExpLinkWidth)
257 
258 #define DEFINE_PROP_UUID(_name, _state, _field) {                  \
259         .name      = (_name),                                      \
260         .info      = &qdev_prop_uuid,                              \
261         .offset    = offsetof(_state, _field)                      \
262             + type_check(QemuUUID, typeof_field(_state, _field)),  \
263         .set_default = true,                                       \
264         }
265 #define DEFINE_PROP_AUDIODEV(_n, _s, _f) \
266     DEFINE_PROP(_n, _s, _f, qdev_prop_audiodev, QEMUSoundCard)
267 
268 #define DEFINE_PROP_UUID_NODEFAULT(_name, _state, _field) {        \
269         .name      = (_name),                                      \
270         .info      = &qdev_prop_uuid,                              \
271         .offset    = offsetof(_state, _field)                      \
272             + type_check(QemuUUID, typeof_field(_state, _field)),  \
273         }
274 
275 #define DEFINE_PROP_END_OF_LIST()               \
276     {}
277 
278 /*
279  * Set properties between creation and realization.
280  *
281  * Returns: %true on success, %false on error.
282  */
283 bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
284                              BlockBackend *value, Error **errp);
285 
286 /*
287  * Set properties between creation and realization.
288  * @value must be valid.  Each property may be set at most once.
289  */
290 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
291 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
292 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
293 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
294 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
295 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
296 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
297 void qdev_prop_set_chr(DeviceState *dev, const char *name, Chardev *value);
298 void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
299 void qdev_prop_set_drive(DeviceState *dev, const char *name,
300                          BlockBackend *value);
301 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
302                            const uint8_t *value);
303 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
304 
305 void *qdev_get_prop_ptr(Object *obj, Property *prop);
306 
307 void qdev_prop_register_global(GlobalProperty *prop);
308 const GlobalProperty *qdev_find_global_prop(Object *obj,
309                                             const char *name);
310 int qdev_prop_check_globals(void);
311 void qdev_prop_set_globals(DeviceState *dev);
312 void error_set_from_qdev_prop_error(Error **errp, int ret, Object *obj,
313                                     Property *prop, const char *value);
314 
315 /**
316  * qdev_property_add_static:
317  * @dev: Device to add the property to.
318  * @prop: The qdev property definition.
319  *
320  * Add a static QOM property to @dev for qdev property @prop.
321  * On error, store error in @errp.  Static properties access data in a struct.
322  * The type of the QOM property is derived from prop->info.
323  */
324 void qdev_property_add_static(DeviceState *dev, Property *prop);
325 
326 /**
327  * qdev_alias_all_properties: Create aliases on source for all target properties
328  * @target: Device which has properties to be aliased
329  * @source: Object to add alias properties to
330  *
331  * Add alias properties to the @source object for all qdev properties on
332  * the @target DeviceState.
333  *
334  * This is useful when @target is an internal implementation object
335  * owned by @source, and you want to expose all the properties of that
336  * implementation object as properties on the @source object so that users
337  * of @source can set them.
338  */
339 void qdev_alias_all_properties(DeviceState *target, Object *source);
340 
341 /**
342  * @qdev_prop_set_after_realize:
343  * @dev: device
344  * @name: name of property
345  * @errp: indirect pointer to Error to be set
346  * Set the Error object to report that an attempt was made to set a property
347  * on a device after it has already been realized. This is a utility function
348  * which allows property-setter functions to easily report the error in
349  * a friendly format identifying both the device and the property.
350  */
351 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
352                                  Error **errp);
353 
354 /**
355  * qdev_prop_allow_set_link_before_realize:
356  *
357  * Set the #Error object if an attempt is made to set the link after realize.
358  * This function should be used as the check() argument to
359  * object_property_add_link().
360  */
361 void qdev_prop_allow_set_link_before_realize(const Object *obj,
362                                              const char *name,
363                                              Object *val, Error **errp);
364 
365 #endif
366