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