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