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