qdev-properties.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #ifndef QEMU_QDEV_PROPERTIES_H
  2. #define QEMU_QDEV_PROPERTIES_H
  3. #include "hw/qdev-core.h"
  4. /**
  5. * Property:
  6. * @set_default: true if the default value should be set from @defval,
  7. * in which case @info->set_default_value must not be NULL
  8. * (if false then no default value is set by the property system
  9. * and the field retains whatever value it was given by instance_init).
  10. * @defval: default value for the property. This is used only if @set_default
  11. * is true.
  12. */
  13. struct Property {
  14. const char *name;
  15. const PropertyInfo *info;
  16. ptrdiff_t offset;
  17. uint8_t bitnr;
  18. bool set_default;
  19. union {
  20. int64_t i;
  21. uint64_t u;
  22. } defval;
  23. int arrayoffset;
  24. const PropertyInfo *arrayinfo;
  25. int arrayfieldsize;
  26. const char *link_type;
  27. };
  28. struct PropertyInfo {
  29. const char *name;
  30. const char *description;
  31. const QEnumLookup *enum_table;
  32. int (*print)(Object *obj, Property *prop, char *dest, size_t len);
  33. void (*set_default_value)(ObjectProperty *op, const Property *prop);
  34. void (*create)(ObjectClass *oc, Property *prop);
  35. ObjectPropertyAccessor *get;
  36. ObjectPropertyAccessor *set;
  37. ObjectPropertyRelease *release;
  38. };
  39. /*** qdev-properties.c ***/
  40. extern const PropertyInfo qdev_prop_bit;
  41. extern const PropertyInfo qdev_prop_bit64;
  42. extern const PropertyInfo qdev_prop_bool;
  43. extern const PropertyInfo qdev_prop_enum;
  44. extern const PropertyInfo qdev_prop_uint8;
  45. extern const PropertyInfo qdev_prop_uint16;
  46. extern const PropertyInfo qdev_prop_uint32;
  47. extern const PropertyInfo qdev_prop_int32;
  48. extern const PropertyInfo qdev_prop_uint64;
  49. extern const PropertyInfo qdev_prop_int64;
  50. extern const PropertyInfo qdev_prop_size;
  51. extern const PropertyInfo qdev_prop_string;
  52. extern const PropertyInfo qdev_prop_chr;
  53. extern const PropertyInfo qdev_prop_tpm;
  54. extern const PropertyInfo qdev_prop_macaddr;
  55. extern const PropertyInfo qdev_prop_reserved_region;
  56. extern const PropertyInfo qdev_prop_on_off_auto;
  57. extern const PropertyInfo qdev_prop_multifd_compression;
  58. extern const PropertyInfo qdev_prop_losttickpolicy;
  59. extern const PropertyInfo qdev_prop_blockdev_on_error;
  60. extern const PropertyInfo qdev_prop_bios_chs_trans;
  61. extern const PropertyInfo qdev_prop_fdc_drive_type;
  62. extern const PropertyInfo qdev_prop_drive;
  63. extern const PropertyInfo qdev_prop_drive_iothread;
  64. extern const PropertyInfo qdev_prop_netdev;
  65. extern const PropertyInfo qdev_prop_pci_devfn;
  66. extern const PropertyInfo qdev_prop_size32;
  67. extern const PropertyInfo qdev_prop_blocksize;
  68. extern const PropertyInfo qdev_prop_pci_host_devaddr;
  69. extern const PropertyInfo qdev_prop_uuid;
  70. extern const PropertyInfo qdev_prop_arraylen;
  71. extern const PropertyInfo qdev_prop_audiodev;
  72. extern const PropertyInfo qdev_prop_link;
  73. extern const PropertyInfo qdev_prop_off_auto_pcibar;
  74. extern const PropertyInfo qdev_prop_pcie_link_speed;
  75. extern const PropertyInfo qdev_prop_pcie_link_width;
  76. #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
  77. .name = (_name), \
  78. .info = &(_prop), \
  79. .offset = offsetof(_state, _field) \
  80. + type_check(_type, typeof_field(_state, _field)), \
  81. }
  82. #define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) { \
  83. .name = (_name), \
  84. .info = &(_prop), \
  85. .offset = offsetof(_state, _field) \
  86. + type_check(_type,typeof_field(_state, _field)), \
  87. .set_default = true, \
  88. .defval.i = (_type)_defval, \
  89. }
  90. #define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
  91. .name = (_name), \
  92. .info = &(_prop), \
  93. .offset = offsetof(_state, _field) \
  94. + type_check(_type, typeof_field(_state, _field)), \
  95. }
  96. #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
  97. .name = (_name), \
  98. .info = &(qdev_prop_bit), \
  99. .bitnr = (_bit), \
  100. .offset = offsetof(_state, _field) \
  101. + type_check(uint32_t,typeof_field(_state, _field)), \
  102. .set_default = true, \
  103. .defval.u = (bool)_defval, \
  104. }
  105. #define DEFINE_PROP_UNSIGNED(_name, _state, _field, _defval, _prop, _type) { \
  106. .name = (_name), \
  107. .info = &(_prop), \
  108. .offset = offsetof(_state, _field) \
  109. + type_check(_type, typeof_field(_state, _field)), \
  110. .set_default = true, \
  111. .defval.u = (_type)_defval, \
  112. }
  113. #define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) { \
  114. .name = (_name), \
  115. .info = &(_prop), \
  116. .offset = offsetof(_state, _field) \
  117. + type_check(_type, typeof_field(_state, _field)), \
  118. }
  119. #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) { \
  120. .name = (_name), \
  121. .info = &(qdev_prop_bit64), \
  122. .bitnr = (_bit), \
  123. .offset = offsetof(_state, _field) \
  124. + type_check(uint64_t, typeof_field(_state, _field)), \
  125. .set_default = true, \
  126. .defval.u = (bool)_defval, \
  127. }
  128. #define DEFINE_PROP_BOOL(_name, _state, _field, _defval) { \
  129. .name = (_name), \
  130. .info = &(qdev_prop_bool), \
  131. .offset = offsetof(_state, _field) \
  132. + type_check(bool, typeof_field(_state, _field)), \
  133. .set_default = true, \
  134. .defval.u = (bool)_defval, \
  135. }
  136. #define PROP_ARRAY_LEN_PREFIX "len-"
  137. /**
  138. * DEFINE_PROP_ARRAY:
  139. * @_name: name of the array
  140. * @_state: name of the device state structure type
  141. * @_field: uint32_t field in @_state to hold the array length
  142. * @_arrayfield: field in @_state (of type '@_arraytype *') which
  143. * will point to the array
  144. * @_arrayprop: PropertyInfo defining what property the array elements have
  145. * @_arraytype: C type of the array elements
  146. *
  147. * Define device properties for a variable-length array _name. A
  148. * static property "len-arrayname" is defined. When the device creator
  149. * sets this property to the desired length of array, further dynamic
  150. * properties "arrayname[0]", "arrayname[1]", ... are defined so the
  151. * device creator can set the array element values. Setting the
  152. * "len-arrayname" property more than once is an error.
  153. *
  154. * When the array length is set, the @_field member of the device
  155. * struct is set to the array length, and @_arrayfield is set to point
  156. * to (zero-initialised) memory allocated for the array. For a zero
  157. * length array, @_field will be set to 0 and @_arrayfield to NULL.
  158. * It is the responsibility of the device deinit code to free the
  159. * @_arrayfield memory.
  160. */
  161. #define DEFINE_PROP_ARRAY(_name, _state, _field, \
  162. _arrayfield, _arrayprop, _arraytype) { \
  163. .name = (PROP_ARRAY_LEN_PREFIX _name), \
  164. .info = &(qdev_prop_arraylen), \
  165. .set_default = true, \
  166. .defval.u = 0, \
  167. .offset = offsetof(_state, _field) \
  168. + type_check(uint32_t, typeof_field(_state, _field)), \
  169. .arrayinfo = &(_arrayprop), \
  170. .arrayfieldsize = sizeof(_arraytype), \
  171. .arrayoffset = offsetof(_state, _arrayfield), \
  172. }
  173. #define DEFINE_PROP_LINK(_name, _state, _field, _type, _ptr_type) { \
  174. .name = (_name), \
  175. .info = &(qdev_prop_link), \
  176. .offset = offsetof(_state, _field) \
  177. + type_check(_ptr_type, typeof_field(_state, _field)), \
  178. .link_type = _type, \
  179. }
  180. #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
  181. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
  182. #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
  183. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
  184. #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
  185. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
  186. #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
  187. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int32, int32_t)
  188. #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
  189. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
  190. #define DEFINE_PROP_INT64(_n, _s, _f, _d) \
  191. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int64, int64_t)
  192. #define DEFINE_PROP_SIZE(_n, _s, _f, _d) \
  193. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
  194. #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
  195. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
  196. #define DEFINE_PROP_CHR(_n, _s, _f) \
  197. DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharBackend)
  198. #define DEFINE_PROP_STRING(_n, _s, _f) \
  199. DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
  200. #define DEFINE_PROP_NETDEV(_n, _s, _f) \
  201. DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, NICPeers)
  202. #define DEFINE_PROP_DRIVE(_n, _s, _f) \
  203. DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockBackend *)
  204. #define DEFINE_PROP_DRIVE_IOTHREAD(_n, _s, _f) \
  205. DEFINE_PROP(_n, _s, _f, qdev_prop_drive_iothread, BlockBackend *)
  206. #define DEFINE_PROP_MACADDR(_n, _s, _f) \
  207. DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
  208. #define DEFINE_PROP_RESERVED_REGION(_n, _s, _f) \
  209. DEFINE_PROP(_n, _s, _f, qdev_prop_reserved_region, ReservedRegion)
  210. #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
  211. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
  212. #define DEFINE_PROP_MULTIFD_COMPRESSION(_n, _s, _f, _d) \
  213. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_multifd_compression, \
  214. MultiFDCompression)
  215. #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
  216. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
  217. LostTickPolicy)
  218. #define DEFINE_PROP_BLOCKDEV_ON_ERROR(_n, _s, _f, _d) \
  219. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_blockdev_on_error, \
  220. BlockdevOnError)
  221. #define DEFINE_PROP_BIOS_CHS_TRANS(_n, _s, _f, _d) \
  222. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_bios_chs_trans, int)
  223. #define DEFINE_PROP_SIZE32(_n, _s, _f, _d) \
  224. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size32, uint32_t)
  225. #define DEFINE_PROP_BLOCKSIZE(_n, _s, _f) \
  226. DEFINE_PROP_UNSIGNED(_n, _s, _f, 0, qdev_prop_blocksize, uint32_t)
  227. #define DEFINE_PROP_PCI_HOST_DEVADDR(_n, _s, _f) \
  228. DEFINE_PROP(_n, _s, _f, qdev_prop_pci_host_devaddr, PCIHostDeviceAddress)
  229. #define DEFINE_PROP_OFF_AUTO_PCIBAR(_n, _s, _f, _d) \
  230. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_off_auto_pcibar, \
  231. OffAutoPCIBAR)
  232. #define DEFINE_PROP_PCIE_LINK_SPEED(_n, _s, _f, _d) \
  233. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pcie_link_speed, \
  234. PCIExpLinkSpeed)
  235. #define DEFINE_PROP_PCIE_LINK_WIDTH(_n, _s, _f, _d) \
  236. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pcie_link_width, \
  237. PCIExpLinkWidth)
  238. #define DEFINE_PROP_UUID(_name, _state, _field) { \
  239. .name = (_name), \
  240. .info = &qdev_prop_uuid, \
  241. .offset = offsetof(_state, _field) \
  242. + type_check(QemuUUID, typeof_field(_state, _field)), \
  243. .set_default = true, \
  244. }
  245. #define DEFINE_PROP_AUDIODEV(_n, _s, _f) \
  246. DEFINE_PROP(_n, _s, _f, qdev_prop_audiodev, QEMUSoundCard)
  247. #define DEFINE_PROP_UUID_NODEFAULT(_name, _state, _field) { \
  248. .name = (_name), \
  249. .info = &qdev_prop_uuid, \
  250. .offset = offsetof(_state, _field) \
  251. + type_check(QemuUUID, typeof_field(_state, _field)), \
  252. }
  253. #define DEFINE_PROP_END_OF_LIST() \
  254. {}
  255. /*
  256. * Set properties between creation and realization.
  257. *
  258. * Returns: %true on success, %false on error.
  259. */
  260. bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
  261. BlockBackend *value, Error **errp);
  262. /*
  263. * Set properties between creation and realization.
  264. * @value must be valid. Each property may be set at most once.
  265. */
  266. void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
  267. void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
  268. void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
  269. void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
  270. void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
  271. void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
  272. void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
  273. void qdev_prop_set_chr(DeviceState *dev, const char *name, Chardev *value);
  274. void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
  275. void qdev_prop_set_drive(DeviceState *dev, const char *name,
  276. BlockBackend *value);
  277. void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
  278. const uint8_t *value);
  279. void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
  280. void *qdev_get_prop_ptr(Object *obj, Property *prop);
  281. void qdev_prop_register_global(GlobalProperty *prop);
  282. const GlobalProperty *qdev_find_global_prop(DeviceState *dev,
  283. const char *name);
  284. int qdev_prop_check_globals(void);
  285. void qdev_prop_set_globals(DeviceState *dev);
  286. void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
  287. Property *prop, const char *value);
  288. /**
  289. * qdev_property_add_static:
  290. * @dev: Device to add the property to.
  291. * @prop: The qdev property definition.
  292. *
  293. * Add a static QOM property to @dev for qdev property @prop.
  294. * On error, store error in @errp. Static properties access data in a struct.
  295. * The type of the QOM property is derived from prop->info.
  296. */
  297. void qdev_property_add_static(DeviceState *dev, Property *prop);
  298. /**
  299. * qdev_alias_all_properties: Create aliases on source for all target properties
  300. * @target: Device which has properties to be aliased
  301. * @source: Object to add alias properties to
  302. *
  303. * Add alias properties to the @source object for all qdev properties on
  304. * the @target DeviceState.
  305. *
  306. * This is useful when @target is an internal implementation object
  307. * owned by @source, and you want to expose all the properties of that
  308. * implementation object as properties on the @source object so that users
  309. * of @source can set them.
  310. */
  311. void qdev_alias_all_properties(DeviceState *target, Object *source);
  312. /**
  313. * @qdev_prop_set_after_realize:
  314. * @dev: device
  315. * @name: name of property
  316. * @errp: indirect pointer to Error to be set
  317. * Set the Error object to report that an attempt was made to set a property
  318. * on a device after it has already been realized. This is a utility function
  319. * which allows property-setter functions to easily report the error in
  320. * a friendly format identifying both the device and the property.
  321. */
  322. void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
  323. Error **errp);
  324. /**
  325. * qdev_prop_allow_set_link_before_realize:
  326. *
  327. * Set the #Error object if an attempt is made to set the link after realize.
  328. * This function should be used as the check() argument to
  329. * object_property_add_link().
  330. */
  331. void qdev_prop_allow_set_link_before_realize(const Object *obj,
  332. const char *name,
  333. Object *val, Error **errp);
  334. #endif