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