xref: /openbmc/qemu/include/hw/qdev-properties.h (revision 9af2398977a78d37bf184d6ff6bd04c72bfbf006)
1 #ifndef QEMU_QDEV_PROPERTIES_H
2 #define QEMU_QDEV_PROPERTIES_H
3 
4 #include "qapi-types.h"
5 #include "hw/qdev-core.h"
6 
7 /*** qdev-properties.c ***/
8 
9 extern const PropertyInfo qdev_prop_bit;
10 extern const PropertyInfo qdev_prop_bit64;
11 extern const PropertyInfo qdev_prop_bool;
12 extern const PropertyInfo qdev_prop_uint8;
13 extern const PropertyInfo qdev_prop_uint16;
14 extern const PropertyInfo qdev_prop_uint32;
15 extern const PropertyInfo qdev_prop_int32;
16 extern const PropertyInfo qdev_prop_uint64;
17 extern const PropertyInfo qdev_prop_int64;
18 extern const PropertyInfo qdev_prop_size;
19 extern const PropertyInfo qdev_prop_string;
20 extern const PropertyInfo qdev_prop_chr;
21 extern const PropertyInfo qdev_prop_tpm;
22 extern const PropertyInfo qdev_prop_ptr;
23 extern const PropertyInfo qdev_prop_macaddr;
24 extern const PropertyInfo qdev_prop_on_off_auto;
25 extern const PropertyInfo qdev_prop_losttickpolicy;
26 extern const PropertyInfo qdev_prop_blockdev_on_error;
27 extern const PropertyInfo qdev_prop_bios_chs_trans;
28 extern const PropertyInfo qdev_prop_fdc_drive_type;
29 extern const PropertyInfo qdev_prop_drive;
30 extern const PropertyInfo qdev_prop_netdev;
31 extern const PropertyInfo qdev_prop_vlan;
32 extern const PropertyInfo qdev_prop_pci_devfn;
33 extern const PropertyInfo qdev_prop_blocksize;
34 extern const PropertyInfo qdev_prop_pci_host_devaddr;
35 extern const PropertyInfo qdev_prop_uuid;
36 extern const PropertyInfo qdev_prop_arraylen;
37 extern const PropertyInfo qdev_prop_link;
38 extern const PropertyInfo qdev_prop_off_auto_pcibar;
39 
40 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
41         .name      = (_name),                                    \
42         .info      = &(_prop),                                   \
43         .offset    = offsetof(_state, _field)                    \
44             + type_check(_type, typeof_field(_state, _field)),   \
45         }
46 
47 #define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) { \
48         .name      = (_name),                                           \
49         .info      = &(_prop),                                          \
50         .offset    = offsetof(_state, _field)                           \
51             + type_check(_type,typeof_field(_state, _field)),           \
52         .set_default = true,                                            \
53         .defval.i  = (_type)_defval,                                    \
54         }
55 
56 #define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
57         .name      = (_name),                                           \
58         .info      = &(_prop),                                          \
59         .offset    = offsetof(_state, _field)                           \
60             + type_check(_type, typeof_field(_state, _field)),          \
61         }
62 
63 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
64         .name      = (_name),                                    \
65         .info      = &(qdev_prop_bit),                           \
66         .bitnr    = (_bit),                                      \
67         .offset    = offsetof(_state, _field)                    \
68             + type_check(uint32_t,typeof_field(_state, _field)), \
69         .set_default = true,                                     \
70         .defval.u  = (bool)_defval,                              \
71         }
72 
73 #define DEFINE_PROP_UNSIGNED(_name, _state, _field, _defval, _prop, _type) { \
74         .name      = (_name),                                           \
75         .info      = &(_prop),                                          \
76         .offset    = offsetof(_state, _field)                           \
77             + type_check(_type, typeof_field(_state, _field)),          \
78         .set_default = true,                                            \
79         .defval.u  = (_type)_defval,                                    \
80         }
81 
82 #define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
83         .name      = (_name),                                           \
84         .info      = &(_prop),                                          \
85         .offset    = offsetof(_state, _field)                           \
86             + type_check(_type, typeof_field(_state, _field)),          \
87         }
88 
89 #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) {       \
90         .name      = (_name),                                           \
91         .info      = &(qdev_prop_bit64),                                \
92         .bitnr    = (_bit),                                             \
93         .offset    = offsetof(_state, _field)                           \
94             + type_check(uint64_t, typeof_field(_state, _field)),       \
95         .set_default = true,                                            \
96         .defval.u  = (bool)_defval,                                     \
97         }
98 
99 #define DEFINE_PROP_BOOL(_name, _state, _field, _defval) {       \
100         .name      = (_name),                                    \
101         .info      = &(qdev_prop_bool),                          \
102         .offset    = offsetof(_state, _field)                    \
103             + type_check(bool, typeof_field(_state, _field)),    \
104         .set_default = true,                                     \
105         .defval.u    = (bool)_defval,                            \
106         }
107 
108 #define PROP_ARRAY_LEN_PREFIX "len-"
109 
110 /**
111  * DEFINE_PROP_ARRAY:
112  * @_name: name of the array
113  * @_state: name of the device state structure type
114  * @_field: uint32_t field in @_state to hold the array length
115  * @_arrayfield: field in @_state (of type '@_arraytype *') which
116  *               will point to the array
117  * @_arrayprop: PropertyInfo defining what property the array elements have
118  * @_arraytype: C type of the array elements
119  *
120  * Define device properties for a variable-length array _name.  A
121  * static property "len-arrayname" is defined. When the device creator
122  * sets this property to the desired length of array, further dynamic
123  * properties "arrayname[0]", "arrayname[1]", ...  are defined so the
124  * device creator can set the array element values. Setting the
125  * "len-arrayname" property more than once is an error.
126  *
127  * When the array length is set, the @_field member of the device
128  * struct is set to the array length, and @_arrayfield is set to point
129  * to (zero-initialised) memory allocated for the array.  For a zero
130  * length array, @_field will be set to 0 and @_arrayfield to NULL.
131  * It is the responsibility of the device deinit code to free the
132  * @_arrayfield memory.
133  */
134 #define DEFINE_PROP_ARRAY(_name, _state, _field,                        \
135                           _arrayfield, _arrayprop, _arraytype) {        \
136         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
137         .info = &(qdev_prop_arraylen),                                  \
138         .set_default = true,                                            \
139         .defval.u = 0,                                                  \
140         .offset = offsetof(_state, _field)                              \
141             + type_check(uint32_t, typeof_field(_state, _field)),       \
142         .arrayinfo = &(_arrayprop),                                     \
143         .arrayfieldsize = sizeof(_arraytype),                           \
144         .arrayoffset = offsetof(_state, _arrayfield),                   \
145         }
146 
147 #define DEFINE_PROP_LINK(_name, _state, _field, _type, _ptr_type) {     \
148         .name = (_name),                                                \
149         .info = &(qdev_prop_link),                                      \
150         .offset = offsetof(_state, _field)                              \
151             + type_check(_ptr_type, typeof_field(_state, _field)),      \
152         .link_type  = _type,                                            \
153         }
154 
155 #define DEFINE_PROP_UINT8(_n, _s, _f, _d)                       \
156     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
157 #define DEFINE_PROP_UINT16(_n, _s, _f, _d)                      \
158     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
159 #define DEFINE_PROP_UINT32(_n, _s, _f, _d)                      \
160     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
161 #define DEFINE_PROP_INT32(_n, _s, _f, _d)                      \
162     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int32, int32_t)
163 #define DEFINE_PROP_UINT64(_n, _s, _f, _d)                      \
164     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
165 #define DEFINE_PROP_INT64(_n, _s, _f, _d)                      \
166     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int64, int64_t)
167 #define DEFINE_PROP_SIZE(_n, _s, _f, _d)                       \
168     DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
169 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d)                   \
170     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
171 
172 /*
173  * Please avoid pointer properties.  If you must use them, you must
174  * cover them in their device's class init function as follows:
175  *
176  * - If the property must be set, the device cannot be used with
177  *   device_add, so add code like this:
178  *   |* Reason: pointer property "NAME-OF-YOUR-PROP" *|
179  *   DeviceClass *dc = DEVICE_CLASS(class);
180  *   dc->user_creatable = false;
181  *
182  * - If the property may safely remain null, document it like this:
183  *   |*
184  *    * Note: pointer property "interrupt_vector" may remain null, thus
185  *    * no need for dc->user_creatable = false;
186  *    *|
187  */
188 #define DEFINE_PROP_PTR(_n, _s, _f)             \
189     DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
190 
191 #define DEFINE_PROP_CHR(_n, _s, _f)             \
192     DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharBackend)
193 #define DEFINE_PROP_STRING(_n, _s, _f)             \
194     DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
195 #define DEFINE_PROP_NETDEV(_n, _s, _f)             \
196     DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, NICPeers)
197 #define DEFINE_PROP_VLAN(_n, _s, _f)             \
198     DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, NICPeers)
199 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
200     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockBackend *)
201 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
202     DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
203 #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
204     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
205 #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
206     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
207                         LostTickPolicy)
208 #define DEFINE_PROP_BLOCKDEV_ON_ERROR(_n, _s, _f, _d) \
209     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_blockdev_on_error, \
210                         BlockdevOnError)
211 #define DEFINE_PROP_BIOS_CHS_TRANS(_n, _s, _f, _d) \
212     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_bios_chs_trans, int)
213 #define DEFINE_PROP_BLOCKSIZE(_n, _s, _f) \
214     DEFINE_PROP_UNSIGNED(_n, _s, _f, 0, qdev_prop_blocksize, uint16_t)
215 #define DEFINE_PROP_PCI_HOST_DEVADDR(_n, _s, _f) \
216     DEFINE_PROP(_n, _s, _f, qdev_prop_pci_host_devaddr, PCIHostDeviceAddress)
217 #define DEFINE_PROP_MEMORY_REGION(_n, _s, _f)             \
218     DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, MemoryRegion *)
219 #define DEFINE_PROP_OFF_AUTO_PCIBAR(_n, _s, _f, _d) \
220     DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_off_auto_pcibar, \
221                         OffAutoPCIBAR)
222 
223 #define DEFINE_PROP_UUID(_name, _state, _field) {                  \
224         .name      = (_name),                                      \
225         .info      = &qdev_prop_uuid,                              \
226         .offset    = offsetof(_state, _field)                      \
227             + type_check(QemuUUID, typeof_field(_state, _field)),  \
228         .set_default = true,                                       \
229         }
230 
231 #define DEFINE_PROP_END_OF_LIST()               \
232     {}
233 
234 /* Set properties between creation and init.  */
235 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
236 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
237 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
238 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
239 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
240 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
241 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
242 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
243 void qdev_prop_set_chr(DeviceState *dev, const char *name, Chardev *value);
244 void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
245 void qdev_prop_set_drive(DeviceState *dev, const char *name,
246                          BlockBackend *value, Error **errp);
247 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
248                            const uint8_t *value);
249 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
250 /* FIXME: Remove opaque pointer properties.  */
251 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
252 
253 void qdev_prop_register_global(GlobalProperty *prop);
254 void qdev_prop_register_global_list(GlobalProperty *props);
255 int qdev_prop_check_globals(void);
256 void qdev_prop_set_globals(DeviceState *dev);
257 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
258                                     Property *prop, const char *value);
259 
260 /**
261  * register_compat_prop:
262  *
263  * Register internal (not user-provided) global property, changing the
264  * default value of a given property in a device type.  This can be used
265  * for enabling machine-type compatibility or for enabling
266  * accelerator-specific defaults in devices.
267  *
268  * The property values set using this function must be always valid and
269  * never report setter errors, as the property will have
270  * GlobalProperty::errp set to &error_abort.
271  *
272  * User-provided global properties should override internal global
273  * properties, so callers of this function should ensure that it is
274  * called before user-provided global properties are registered.
275  *
276  * @driver: Device type to be affected
277  * @property: Property whose default value is going to be changed
278  * @value: New default value for the property
279  */
280 void register_compat_prop(const char *driver, const char *property,
281                           const char *value);
282 /*
283  * register_compat_props_array(): using register_compat_prop(), which
284  * only registers internal global properties (which has lower priority
285  * than user-provided global properties)
286  */
287 void register_compat_props_array(GlobalProperty *prop);
288 
289 /**
290  * qdev_property_add_static:
291  * @dev: Device to add the property to.
292  * @prop: The qdev property definition.
293  * @errp: location to store error information.
294  *
295  * Add a static QOM property to @dev for qdev property @prop.
296  * On error, store error in @errp.  Static properties access data in a struct.
297  * The type of the QOM property is derived from prop->info.
298  */
299 void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp);
300 
301 void qdev_alias_all_properties(DeviceState *target, Object *source);
302 
303 /**
304  * @qdev_prop_set_after_realize:
305  * @dev: device
306  * @name: name of property
307  * @errp: indirect pointer to Error to be set
308  * Set the Error object to report that an attempt was made to set a property
309  * on a device after it has already been realized. This is a utility function
310  * which allows property-setter functions to easily report the error in
311  * a friendly format identifying both the device and the property.
312  */
313 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
314                                  Error **errp);
315 
316 /**
317  * qdev_prop_allow_set_link_before_realize:
318  *
319  * Set the #Error object if an attempt is made to set the link after realize.
320  * This function should be used as the check() argument to
321  * object_property_add_link().
322  */
323 void qdev_prop_allow_set_link_before_realize(const Object *obj,
324                                              const char *name,
325                                              Object *val, Error **errp);
326 
327 #endif
328