xref: /qemu/include/hw/qdev-properties.h (revision f231b88d)
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 PropertyInfo qdev_prop_bit;
9 extern PropertyInfo qdev_prop_bool;
10 extern PropertyInfo qdev_prop_uint8;
11 extern PropertyInfo qdev_prop_uint16;
12 extern PropertyInfo qdev_prop_uint32;
13 extern PropertyInfo qdev_prop_int32;
14 extern PropertyInfo qdev_prop_uint64;
15 extern PropertyInfo qdev_prop_size;
16 extern PropertyInfo qdev_prop_string;
17 extern PropertyInfo qdev_prop_chr;
18 extern PropertyInfo qdev_prop_ptr;
19 extern PropertyInfo qdev_prop_macaddr;
20 extern PropertyInfo qdev_prop_losttickpolicy;
21 extern PropertyInfo qdev_prop_bios_chs_trans;
22 extern PropertyInfo qdev_prop_drive;
23 extern PropertyInfo qdev_prop_netdev;
24 extern PropertyInfo qdev_prop_vlan;
25 extern PropertyInfo qdev_prop_iothread;
26 extern PropertyInfo qdev_prop_pci_devfn;
27 extern PropertyInfo qdev_prop_blocksize;
28 extern PropertyInfo qdev_prop_pci_host_devaddr;
29 extern PropertyInfo qdev_prop_arraylen;
30 
31 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
32         .name      = (_name),                                    \
33         .info      = &(_prop),                                   \
34         .offset    = offsetof(_state, _field)                    \
35             + type_check(_type, typeof_field(_state, _field)),   \
36         }
37 #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
38         .name      = (_name),                                           \
39         .info      = &(_prop),                                          \
40         .offset    = offsetof(_state, _field)                           \
41             + type_check(_type,typeof_field(_state, _field)),           \
42         .qtype     = QTYPE_QINT,                                        \
43         .defval    = (_type)_defval,                                    \
44         }
45 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
46         .name      = (_name),                                    \
47         .info      = &(qdev_prop_bit),                           \
48         .bitnr    = (_bit),                                      \
49         .offset    = offsetof(_state, _field)                    \
50             + type_check(uint32_t,typeof_field(_state, _field)), \
51         .qtype     = QTYPE_QBOOL,                                \
52         .defval    = (bool)_defval,                              \
53         }
54 
55 #define DEFINE_PROP_BOOL(_name, _state, _field, _defval) {       \
56         .name      = (_name),                                    \
57         .info      = &(qdev_prop_bool),                          \
58         .offset    = offsetof(_state, _field)                    \
59             + type_check(bool, typeof_field(_state, _field)),    \
60         .qtype     = QTYPE_QBOOL,                                \
61         .defval    = (bool)_defval,                              \
62         }
63 
64 #define PROP_ARRAY_LEN_PREFIX "len-"
65 
66 /**
67  * DEFINE_PROP_ARRAY:
68  * @_name: name of the array
69  * @_state: name of the device state structure type
70  * @_field: uint32_t field in @_state to hold the array length
71  * @_arrayfield: field in @_state (of type '@_arraytype *') which
72  *               will point to the array
73  * @_arrayprop: PropertyInfo defining what property the array elements have
74  * @_arraytype: C type of the array elements
75  *
76  * Define device properties for a variable-length array _name.  A
77  * static property "len-arrayname" is defined. When the device creator
78  * sets this property to the desired length of array, further dynamic
79  * properties "arrayname[0]", "arrayname[1]", ...  are defined so the
80  * device creator can set the array element values. Setting the
81  * "len-arrayname" property more than once is an error.
82  *
83  * When the array length is set, the @_field member of the device
84  * struct is set to the array length, and @_arrayfield is set to point
85  * to (zero-initialised) memory allocated for the array.  For a zero
86  * length array, @_field will be set to 0 and @_arrayfield to NULL.
87  * It is the responsibility of the device deinit code to free the
88  * @_arrayfield memory.
89  */
90 #define DEFINE_PROP_ARRAY(_name, _state, _field,                        \
91                           _arrayfield, _arrayprop, _arraytype) {        \
92         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
93         .info = &(qdev_prop_arraylen),                                  \
94         .offset = offsetof(_state, _field)                              \
95             + type_check(uint32_t, typeof_field(_state, _field)),       \
96         .qtype = QTYPE_QINT,                                            \
97         .arrayinfo = &(_arrayprop),                                     \
98         .arrayfieldsize = sizeof(_arraytype),                           \
99         .arrayoffset = offsetof(_state, _arrayfield),                   \
100         }
101 
102 #define DEFINE_PROP_UINT8(_n, _s, _f, _d)                       \
103     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
104 #define DEFINE_PROP_UINT16(_n, _s, _f, _d)                      \
105     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
106 #define DEFINE_PROP_UINT32(_n, _s, _f, _d)                      \
107     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
108 #define DEFINE_PROP_INT32(_n, _s, _f, _d)                      \
109     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
110 #define DEFINE_PROP_UINT64(_n, _s, _f, _d)                      \
111     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
112 #define DEFINE_PROP_SIZE(_n, _s, _f, _d)                       \
113     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_size, uint64_t)
114 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d)                   \
115     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
116 
117 /*
118  * Please avoid pointer properties.  If you must use them, you must
119  * cover them in their device's class init function as follows:
120  *
121  * - If the property must be set, the device cannot be used with
122  *   device_add, so add code like this:
123  *   |* Reason: pointer property "NAME-OF-YOUR-PROP" *|
124  *   DeviceClass *dc = DEVICE_CLASS(class);
125  *   dc->cannot_instantiate_with_device_add_yet = true;
126  *
127  * - If the property may safely remain null, document it like this:
128  *   |*
129  *    * Note: pointer property "interrupt_vector" may remain null, thus
130  *    * no need for dc->cannot_instantiate_with_device_add_yet = true;
131  *    *|
132  */
133 #define DEFINE_PROP_PTR(_n, _s, _f)             \
134     DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
135 
136 #define DEFINE_PROP_CHR(_n, _s, _f)             \
137     DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
138 #define DEFINE_PROP_STRING(_n, _s, _f)             \
139     DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
140 #define DEFINE_PROP_NETDEV(_n, _s, _f)             \
141     DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, NICPeers)
142 #define DEFINE_PROP_VLAN(_n, _s, _f)             \
143     DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, NICPeers)
144 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
145     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
146 #define DEFINE_PROP_IOTHREAD(_n, _s, _f)             \
147     DEFINE_PROP(_n, _s, _f, qdev_prop_iothread, IOThread *)
148 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
149     DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
150 #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
151     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
152                         LostTickPolicy)
153 #define DEFINE_PROP_BIOS_CHS_TRANS(_n, _s, _f, _d) \
154     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_bios_chs_trans, int)
155 #define DEFINE_PROP_BLOCKSIZE(_n, _s, _f, _d) \
156     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_blocksize, uint16_t)
157 #define DEFINE_PROP_PCI_HOST_DEVADDR(_n, _s, _f) \
158     DEFINE_PROP(_n, _s, _f, qdev_prop_pci_host_devaddr, PCIHostDeviceAddress)
159 
160 #define DEFINE_PROP_END_OF_LIST()               \
161     {}
162 
163 /* Set properties between creation and init.  */
164 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
165 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
166 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
167 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
168 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
169 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
170 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
171 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
172 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
173 void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
174 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
175 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
176 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
177 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
178 /* FIXME: Remove opaque pointer properties.  */
179 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
180 
181 void qdev_prop_register_global(GlobalProperty *prop);
182 void qdev_prop_register_global_list(GlobalProperty *props);
183 void qdev_prop_set_globals(DeviceState *dev, Error **errp);
184 void qdev_prop_set_globals_for_type(DeviceState *dev, const char *typename,
185                                     Error **errp);
186 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
187                                     Property *prop, const char *value);
188 
189 /**
190  * @qdev_property_add_static - add a @Property to a device referencing a
191  * field in a struct.
192  */
193 void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp);
194 
195 /**
196  * @qdev_prop_set_after_realize:
197  * @dev: device
198  * @name: name of property
199  * @errp: indirect pointer to Error to be set
200  * Set the Error object to report that an attempt was made to set a property
201  * on a device after it has already been realized. This is a utility function
202  * which allows property-setter functions to easily report the error in
203  * a friendly format identifying both the device and the property.
204  */
205 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
206                                  Error **errp);
207 
208 /**
209  * qdev_prop_allow_set_link_before_realize:
210  *
211  * Set the #Error object if an attempt is made to set the link after realize.
212  * This function should be used as the check() argument to
213  * object_property_add_link().
214  */
215 void qdev_prop_allow_set_link_before_realize(Object *obj, const char *name,
216                                              Object *val, Error **errp);
217 
218 #endif
219