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