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