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