qdev-properties.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. uint64_t bitmask;
  19. bool set_default;
  20. union {
  21. int64_t i;
  22. uint64_t u;
  23. } defval;
  24. int arrayoffset;
  25. const PropertyInfo *arrayinfo;
  26. int arrayfieldsize;
  27. const char *link_type;
  28. };
  29. struct PropertyInfo {
  30. const char *name;
  31. const char *description;
  32. const QEnumLookup *enum_table;
  33. bool realized_set_allowed; /* allow setting property on realized device */
  34. int (*print)(Object *obj, Property *prop, char *dest, size_t len);
  35. void (*set_default_value)(ObjectProperty *op, const Property *prop);
  36. ObjectProperty *(*create)(ObjectClass *oc, const char *name,
  37. Property *prop);
  38. ObjectPropertyAccessor *get;
  39. ObjectPropertyAccessor *set;
  40. ObjectPropertyRelease *release;
  41. };
  42. /*** qdev-properties.c ***/
  43. extern const PropertyInfo qdev_prop_bit;
  44. extern const PropertyInfo qdev_prop_bit64;
  45. extern const PropertyInfo qdev_prop_bool;
  46. extern const PropertyInfo qdev_prop_enum;
  47. extern const PropertyInfo qdev_prop_uint8;
  48. extern const PropertyInfo qdev_prop_uint16;
  49. extern const PropertyInfo qdev_prop_uint32;
  50. extern const PropertyInfo qdev_prop_int32;
  51. extern const PropertyInfo qdev_prop_uint64;
  52. extern const PropertyInfo qdev_prop_uint64_checkmask;
  53. extern const PropertyInfo qdev_prop_int64;
  54. extern const PropertyInfo qdev_prop_size;
  55. extern const PropertyInfo qdev_prop_string;
  56. extern const PropertyInfo qdev_prop_on_off_auto;
  57. extern const PropertyInfo qdev_prop_size32;
  58. extern const PropertyInfo qdev_prop_arraylen;
  59. extern const PropertyInfo qdev_prop_link;
  60. #define DEFINE_PROP(_name, _state, _field, _prop, _type, ...) { \
  61. .name = (_name), \
  62. .info = &(_prop), \
  63. .offset = offsetof(_state, _field) \
  64. + type_check(_type, typeof_field(_state, _field)), \
  65. __VA_ARGS__ \
  66. }
  67. #define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) \
  68. DEFINE_PROP(_name, _state, _field, _prop, _type, \
  69. .set_default = true, \
  70. .defval.i = (_type)_defval)
  71. #define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) \
  72. DEFINE_PROP(_name, _state, _field, _prop, _type)
  73. #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) \
  74. DEFINE_PROP(_name, _state, _field, qdev_prop_bit, uint32_t, \
  75. .bitnr = (_bit), \
  76. .set_default = true, \
  77. .defval.u = (bool)_defval)
  78. #define DEFINE_PROP_UNSIGNED(_name, _state, _field, _defval, _prop, _type) \
  79. DEFINE_PROP(_name, _state, _field, _prop, _type, \
  80. .set_default = true, \
  81. .defval.u = (_type)_defval)
  82. #define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) \
  83. DEFINE_PROP(_name, _state, _field, _prop, _type)
  84. #define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) \
  85. DEFINE_PROP(_name, _state, _field, qdev_prop_bit64, uint64_t, \
  86. .bitnr = (_bit), \
  87. .set_default = true, \
  88. .defval.u = (bool)_defval)
  89. #define DEFINE_PROP_BOOL(_name, _state, _field, _defval) \
  90. DEFINE_PROP(_name, _state, _field, qdev_prop_bool, bool, \
  91. .set_default = true, \
  92. .defval.u = (bool)_defval)
  93. /**
  94. * The DEFINE_PROP_UINT64_CHECKMASK macro checks a user-supplied value
  95. * against corresponding bitmask, rejects the value if it violates.
  96. * The default value is set in instance_init().
  97. */
  98. #define DEFINE_PROP_UINT64_CHECKMASK(_name, _state, _field, _bitmask) \
  99. DEFINE_PROP(_name, _state, _field, qdev_prop_uint64_checkmask, uint64_t, \
  100. .bitmask = (_bitmask), \
  101. .set_default = false)
  102. #define PROP_ARRAY_LEN_PREFIX "len-"
  103. /**
  104. * DEFINE_PROP_ARRAY:
  105. * @_name: name of the array
  106. * @_state: name of the device state structure type
  107. * @_field: uint32_t field in @_state to hold the array length
  108. * @_arrayfield: field in @_state (of type '@_arraytype *') which
  109. * will point to the array
  110. * @_arrayprop: PropertyInfo defining what property the array elements have
  111. * @_arraytype: C type of the array elements
  112. *
  113. * Define device properties for a variable-length array _name. A
  114. * static property "len-arrayname" is defined. When the device creator
  115. * sets this property to the desired length of array, further dynamic
  116. * properties "arrayname[0]", "arrayname[1]", ... are defined so the
  117. * device creator can set the array element values. Setting the
  118. * "len-arrayname" property more than once is an error.
  119. *
  120. * When the array length is set, the @_field member of the device
  121. * struct is set to the array length, and @_arrayfield is set to point
  122. * to (zero-initialised) memory allocated for the array. For a zero
  123. * length array, @_field will be set to 0 and @_arrayfield to NULL.
  124. * It is the responsibility of the device deinit code to free the
  125. * @_arrayfield memory.
  126. */
  127. #define DEFINE_PROP_ARRAY(_name, _state, _field, \
  128. _arrayfield, _arrayprop, _arraytype) \
  129. DEFINE_PROP((PROP_ARRAY_LEN_PREFIX _name), \
  130. _state, _field, qdev_prop_arraylen, uint32_t, \
  131. .set_default = true, \
  132. .defval.u = 0, \
  133. .arrayinfo = &(_arrayprop), \
  134. .arrayfieldsize = sizeof(_arraytype), \
  135. .arrayoffset = offsetof(_state, _arrayfield))
  136. #define DEFINE_PROP_LINK(_name, _state, _field, _type, _ptr_type) \
  137. DEFINE_PROP(_name, _state, _field, qdev_prop_link, _ptr_type, \
  138. .link_type = _type)
  139. #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
  140. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
  141. #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
  142. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
  143. #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
  144. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
  145. #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
  146. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int32, int32_t)
  147. #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
  148. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
  149. #define DEFINE_PROP_INT64(_n, _s, _f, _d) \
  150. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int64, int64_t)
  151. #define DEFINE_PROP_SIZE(_n, _s, _f, _d) \
  152. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
  153. #define DEFINE_PROP_STRING(_n, _s, _f) \
  154. DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
  155. #define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
  156. DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
  157. #define DEFINE_PROP_SIZE32(_n, _s, _f, _d) \
  158. DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size32, uint32_t)
  159. #define DEFINE_PROP_END_OF_LIST() \
  160. {}
  161. /*
  162. * Set properties between creation and realization.
  163. *
  164. * Returns: %true on success, %false on error.
  165. */
  166. bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
  167. BlockBackend *value, Error **errp);
  168. /*
  169. * Set properties between creation and realization.
  170. * @value must be valid. Each property may be set at most once.
  171. */
  172. void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
  173. void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
  174. void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
  175. void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
  176. void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
  177. void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
  178. void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
  179. void qdev_prop_set_chr(DeviceState *dev, const char *name, Chardev *value);
  180. void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
  181. void qdev_prop_set_drive(DeviceState *dev, const char *name,
  182. BlockBackend *value);
  183. void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
  184. const uint8_t *value);
  185. void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
  186. void *object_field_prop_ptr(Object *obj, Property *prop);
  187. void qdev_prop_register_global(GlobalProperty *prop);
  188. const GlobalProperty *qdev_find_global_prop(Object *obj,
  189. const char *name);
  190. int qdev_prop_check_globals(void);
  191. void qdev_prop_set_globals(DeviceState *dev);
  192. void error_set_from_qdev_prop_error(Error **errp, int ret, Object *obj,
  193. const char *name, const char *value);
  194. /**
  195. * qdev_property_add_static:
  196. * @dev: Device to add the property to.
  197. * @prop: The qdev property definition.
  198. *
  199. * Add a static QOM property to @dev for qdev property @prop.
  200. * On error, store error in @errp. Static properties access data in a struct.
  201. * The type of the QOM property is derived from prop->info.
  202. */
  203. void qdev_property_add_static(DeviceState *dev, Property *prop);
  204. /**
  205. * qdev_alias_all_properties: Create aliases on source for all target properties
  206. * @target: Device which has properties to be aliased
  207. * @source: Object to add alias properties to
  208. *
  209. * Add alias properties to the @source object for all qdev properties on
  210. * the @target DeviceState.
  211. *
  212. * This is useful when @target is an internal implementation object
  213. * owned by @source, and you want to expose all the properties of that
  214. * implementation object as properties on the @source object so that users
  215. * of @source can set them.
  216. */
  217. void qdev_alias_all_properties(DeviceState *target, Object *source);
  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. * qdev_prop_allow_set_link_before_realize:
  232. *
  233. * Set the #Error object if an attempt is made to set the link after realize.
  234. * This function should be used as the check() argument to
  235. * object_property_add_link().
  236. */
  237. void qdev_prop_allow_set_link_before_realize(const Object *obj,
  238. const char *name,
  239. Object *val, Error **errp);
  240. #endif