blob: f7925f67d03b2110923a8524a1620f1ba79a8d44 [file] [log] [blame]
Anthony Liguori074a86f2012-08-10 12:00:43 -05001#ifndef QEMU_QDEV_PROPERTIES_H
2#define QEMU_QDEV_PROPERTIES_H
3
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +01004#include "hw/qdev-core.h"
Anthony Liguori074a86f2012-08-10 12:00:43 -05005
Eduardo Habkostd3fd6e72020-12-11 17:05:00 -05006/**
7 * Property:
8 * @set_default: true if the default value should be set from @defval,
9 * in which case @info->set_default_value must not be NULL
10 * (if false then no default value is set by the property system
11 * and the field retains whatever value it was given by instance_init).
12 * @defval: default value for the property. This is used only if @set_default
13 * is true.
14 */
15struct Property {
16 const char *name;
17 const PropertyInfo *info;
18 ptrdiff_t offset;
19 uint8_t bitnr;
20 bool set_default;
21 union {
22 int64_t i;
23 uint64_t u;
24 } defval;
25 int arrayoffset;
26 const PropertyInfo *arrayinfo;
27 int arrayfieldsize;
28 const char *link_type;
29};
30
31struct PropertyInfo {
32 const char *name;
33 const char *description;
34 const QEnumLookup *enum_table;
Vladimir Sementsov-Ogievskiydeb2bb12021-08-24 11:38:25 +030035 bool realized_set_allowed; /* allow setting property on realized device */
Eduardo Habkost40ea00b2020-12-11 17:05:04 -050036 int (*print)(Object *obj, Property *prop, char *dest, size_t len);
Eduardo Habkostd3fd6e72020-12-11 17:05:00 -050037 void (*set_default_value)(ObjectProperty *op, const Property *prop);
Eduardo Habkostf59c6d22020-12-11 17:05:21 -050038 ObjectProperty *(*create)(ObjectClass *oc, const char *name,
39 Property *prop);
Eduardo Habkostd3fd6e72020-12-11 17:05:00 -050040 ObjectPropertyAccessor *get;
41 ObjectPropertyAccessor *set;
42 ObjectPropertyRelease *release;
43};
44
45
Anthony Liguori074a86f2012-08-10 12:00:43 -050046/*** qdev-properties.c ***/
47
Fam Zheng1b6b7d12017-07-14 10:14:54 +080048extern const PropertyInfo qdev_prop_bit;
49extern const PropertyInfo qdev_prop_bit64;
50extern const PropertyInfo qdev_prop_bool;
Philippe Mathieu-Daudé79bdf292020-09-30 18:49:44 +020051extern const PropertyInfo qdev_prop_enum;
Fam Zheng1b6b7d12017-07-14 10:14:54 +080052extern const PropertyInfo qdev_prop_uint8;
53extern const PropertyInfo qdev_prop_uint16;
54extern const PropertyInfo qdev_prop_uint32;
55extern const PropertyInfo qdev_prop_int32;
56extern const PropertyInfo qdev_prop_uint64;
Peter Xu07d1d062017-07-18 11:39:01 +080057extern const PropertyInfo qdev_prop_int64;
Fam Zheng1b6b7d12017-07-14 10:14:54 +080058extern const PropertyInfo qdev_prop_size;
59extern const PropertyInfo qdev_prop_string;
Fam Zheng1b6b7d12017-07-14 10:14:54 +080060extern const PropertyInfo qdev_prop_on_off_auto;
Roman Kagan914e74c2020-05-29 01:55:12 +030061extern const PropertyInfo qdev_prop_size32;
Fam Zheng1b6b7d12017-07-14 10:14:54 +080062extern const PropertyInfo qdev_prop_arraylen;
63extern const PropertyInfo qdev_prop_link;
Anthony Liguori074a86f2012-08-10 12:00:43 -050064
Eduardo Habkost45efa072020-12-11 17:05:13 -050065#define DEFINE_PROP(_name, _state, _field, _prop, _type, ...) { \
Anthony Liguori074a86f2012-08-10 12:00:43 -050066 .name = (_name), \
67 .info = &(_prop), \
68 .offset = offsetof(_state, _field) \
Jason Wang1ceef9f2013-01-30 19:12:28 +080069 + type_check(_type, typeof_field(_state, _field)), \
Eduardo Habkost45efa072020-12-11 17:05:13 -050070 __VA_ARGS__ \
Anthony Liguori074a86f2012-08-10 12:00:43 -050071 }
Marc-André Lureau3fb21112017-06-07 20:36:09 +040072
Eduardo Habkost45efa072020-12-11 17:05:13 -050073#define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) \
74 DEFINE_PROP(_name, _state, _field, _prop, _type, \
75 .set_default = true, \
76 .defval.i = (_type)_defval)
Marc-André Lureau3fb21112017-06-07 20:36:09 +040077
Eduardo Habkost45efa072020-12-11 17:05:13 -050078#define DEFINE_PROP_SIGNED_NODEFAULT(_name, _state, _field, _prop, _type) \
79 DEFINE_PROP(_name, _state, _field, _prop, _type)
Peter Maydell5cc56cc2017-07-17 13:36:06 +010080
Eduardo Habkost45efa072020-12-11 17:05:13 -050081#define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) \
82 DEFINE_PROP(_name, _state, _field, qdev_prop_bit, uint32_t, \
83 .bitnr = (_bit), \
84 .set_default = true, \
85 .defval.u = (bool)_defval)
Marc-André Lureau3fb21112017-06-07 20:36:09 +040086
Eduardo Habkost45efa072020-12-11 17:05:13 -050087#define DEFINE_PROP_UNSIGNED(_name, _state, _field, _defval, _prop, _type) \
88 DEFINE_PROP(_name, _state, _field, _prop, _type, \
89 .set_default = true, \
90 .defval.u = (_type)_defval)
Marc-André Lureau3fb21112017-06-07 20:36:09 +040091
Eduardo Habkost45efa072020-12-11 17:05:13 -050092#define DEFINE_PROP_UNSIGNED_NODEFAULT(_name, _state, _field, _prop, _type) \
93 DEFINE_PROP(_name, _state, _field, _prop, _type)
Peter Maydell5cc56cc2017-07-17 13:36:06 +010094
Eduardo Habkost45efa072020-12-11 17:05:13 -050095#define DEFINE_PROP_BIT64(_name, _state, _field, _bit, _defval) \
96 DEFINE_PROP(_name, _state, _field, qdev_prop_bit64, uint64_t, \
97 .bitnr = (_bit), \
98 .set_default = true, \
99 .defval.u = (bool)_defval)
Anthony Liguori074a86f2012-08-10 12:00:43 -0500100
Eduardo Habkost45efa072020-12-11 17:05:13 -0500101#define DEFINE_PROP_BOOL(_name, _state, _field, _defval) \
102 DEFINE_PROP(_name, _state, _field, qdev_prop_bool, bool, \
103 .set_default = true, \
104 .defval.u = (bool)_defval)
Igor Mammedov72cc5132013-03-07 17:16:18 +0100105
Peter Maydell0be6bfa2013-03-15 16:41:57 +0000106#define PROP_ARRAY_LEN_PREFIX "len-"
107
108/**
109 * DEFINE_PROP_ARRAY:
110 * @_name: name of the array
111 * @_state: name of the device state structure type
112 * @_field: uint32_t field in @_state to hold the array length
113 * @_arrayfield: field in @_state (of type '@_arraytype *') which
114 * will point to the array
115 * @_arrayprop: PropertyInfo defining what property the array elements have
116 * @_arraytype: C type of the array elements
117 *
118 * Define device properties for a variable-length array _name. A
119 * static property "len-arrayname" is defined. When the device creator
120 * sets this property to the desired length of array, further dynamic
121 * properties "arrayname[0]", "arrayname[1]", ... are defined so the
122 * device creator can set the array element values. Setting the
123 * "len-arrayname" property more than once is an error.
124 *
125 * When the array length is set, the @_field member of the device
126 * struct is set to the array length, and @_arrayfield is set to point
127 * to (zero-initialised) memory allocated for the array. For a zero
128 * length array, @_field will be set to 0 and @_arrayfield to NULL.
129 * It is the responsibility of the device deinit code to free the
130 * @_arrayfield memory.
131 */
Eduardo Habkost45efa072020-12-11 17:05:13 -0500132#define DEFINE_PROP_ARRAY(_name, _state, _field, \
133 _arrayfield, _arrayprop, _arraytype) \
134 DEFINE_PROP((PROP_ARRAY_LEN_PREFIX _name), \
135 _state, _field, qdev_prop_arraylen, uint32_t, \
136 .set_default = true, \
137 .defval.u = 0, \
138 .arrayinfo = &(_arrayprop), \
139 .arrayfieldsize = sizeof(_arraytype), \
140 .arrayoffset = offsetof(_state, _arrayfield))
Peter Maydell0be6bfa2013-03-15 16:41:57 +0000141
Eduardo Habkost45efa072020-12-11 17:05:13 -0500142#define DEFINE_PROP_LINK(_name, _state, _field, _type, _ptr_type) \
143 DEFINE_PROP(_name, _state, _field, qdev_prop_link, _ptr_type, \
144 .link_type = _type)
Fam Zheng5b4ff3c2017-07-14 10:14:52 +0800145
Anthony Liguori074a86f2012-08-10 12:00:43 -0500146#define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
Marc-André Lureau3fb21112017-06-07 20:36:09 +0400147 DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
Anthony Liguori074a86f2012-08-10 12:00:43 -0500148#define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
Marc-André Lureau3fb21112017-06-07 20:36:09 +0400149 DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
Anthony Liguori074a86f2012-08-10 12:00:43 -0500150#define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
Marc-André Lureau3fb21112017-06-07 20:36:09 +0400151 DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
Anthony Liguori074a86f2012-08-10 12:00:43 -0500152#define DEFINE_PROP_INT32(_n, _s, _f, _d) \
Marc-André Lureau85bbd1e2017-06-07 20:36:07 +0400153 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int32, int32_t)
Anthony Liguori074a86f2012-08-10 12:00:43 -0500154#define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
Marc-André Lureau3fb21112017-06-07 20:36:09 +0400155 DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
Peter Xu07d1d062017-07-18 11:39:01 +0800156#define DEFINE_PROP_INT64(_n, _s, _f, _d) \
157 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_int64, int64_t)
Vasilis Liaskovitise8cd45c2013-07-29 16:47:56 +0200158#define DEFINE_PROP_SIZE(_n, _s, _f, _d) \
Marc-André Lureau3fb21112017-06-07 20:36:09 +0400159 DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size, uint64_t)
Anthony Liguori074a86f2012-08-10 12:00:43 -0500160#define DEFINE_PROP_STRING(_n, _s, _f) \
161 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
Markus Armbruster55e8a152016-03-15 19:34:49 +0100162#define DEFINE_PROP_ON_OFF_AUTO(_n, _s, _f, _d) \
Marc-André Lureau85bbd1e2017-06-07 20:36:07 +0400163 DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_on_off_auto, OnOffAuto)
Roman Kagan914e74c2020-05-29 01:55:12 +0300164#define DEFINE_PROP_SIZE32(_n, _s, _f, _d) \
165 DEFINE_PROP_UNSIGNED(_n, _s, _f, _d, qdev_prop_size32, uint32_t)
Corey Minyarda65f4d42018-11-07 16:33:09 -0600166
Anthony Liguori074a86f2012-08-10 12:00:43 -0500167#define DEFINE_PROP_END_OF_LIST() \
168 {}
169
Markus Armbruster934df912020-06-22 11:42:24 +0200170/*
171 * Set properties between creation and realization.
Philippe Mathieu-Daudé0aca03a2020-07-20 14:16:59 +0200172 *
173 * Returns: %true on success, %false on error.
Markus Armbruster934df912020-06-22 11:42:24 +0200174 */
Markus Armbruster73ac1aa2020-07-07 18:05:59 +0200175bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
176 BlockBackend *value, Error **errp);
Markus Armbruster934df912020-06-22 11:42:24 +0200177
178/*
179 * Set properties between creation and realization.
180 * @value must be valid. Each property may be set at most once.
181 */
Anthony Liguori074a86f2012-08-10 12:00:43 -0500182void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
183void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
184void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
185void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
186void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
187void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
188void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value);
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300189void qdev_prop_set_chr(DeviceState *dev, const char *name, Chardev *value);
Anthony Liguori074a86f2012-08-10 12:00:43 -0500190void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value);
Markus Armbruster9b3d1112015-03-09 19:17:26 +0100191void qdev_prop_set_drive(DeviceState *dev, const char *name,
Markus Armbruster934df912020-06-22 11:42:24 +0200192 BlockBackend *value);
Krzysztof Kozlowski606fd0e2017-03-10 22:05:49 +0200193void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
194 const uint8_t *value);
Anthony Liguori074a86f2012-08-10 12:00:43 -0500195void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
Anthony Liguori074a86f2012-08-10 12:00:43 -0500196
Eduardo Habkost1e198712020-12-11 17:05:27 -0500197void *object_field_prop_ptr(Object *obj, Property *prop);
Markus Armbruster934df912020-06-22 11:42:24 +0200198
Eduardo Habkosta404b612012-12-05 14:49:11 -0200199void qdev_prop_register_global(GlobalProperty *prop);
Eduardo Habkost39501272020-12-11 17:05:07 -0500200const GlobalProperty *qdev_find_global_prop(Object *obj,
Markus Armbruster1bc13332020-06-22 11:42:21 +0200201 const char *name);
Eduardo Habkostd828c432014-08-08 16:03:30 -0300202int qdev_prop_check_globals(void);
Markus Armbruster25f8dd92015-01-20 10:04:07 +0100203void qdev_prop_set_globals(DeviceState *dev);
Eduardo Habkostc7525b12020-12-11 17:05:09 -0500204void error_set_from_qdev_prop_error(Error **errp, int ret, Object *obj,
Eduardo Habkoste68c2cb2020-12-11 17:05:15 -0500205 const char *name, const char *value);
Anthony Liguori074a86f2012-08-10 12:00:43 -0500206
207/**
Cao jind9d8d452016-04-17 15:45:54 +0800208 * qdev_property_add_static:
209 * @dev: Device to add the property to.
210 * @prop: The qdev property definition.
Cao jind9d8d452016-04-17 15:45:54 +0800211 *
212 * Add a static QOM property to @dev for qdev property @prop.
213 * On error, store error in @errp. Static properties access data in a struct.
214 * The type of the QOM property is derived from prop->info.
Anthony Liguori074a86f2012-08-10 12:00:43 -0500215 */
Marc-André Lureau94d912d2020-01-10 19:30:16 +0400216void qdev_property_add_static(DeviceState *dev, Property *prop);
Anthony Liguori074a86f2012-08-10 12:00:43 -0500217
Peter Maydellb51238e2020-07-11 15:24:23 +0100218/**
219 * qdev_alias_all_properties: Create aliases on source for all target properties
220 * @target: Device which has properties to be aliased
221 * @source: Object to add alias properties to
222 *
223 * Add alias properties to the @source object for all qdev properties on
224 * the @target DeviceState.
225 *
226 * This is useful when @target is an internal implementation object
227 * owned by @source, and you want to expose all the properties of that
228 * implementation object as properties on the @source object so that users
229 * of @source can set them.
230 */
Stefan Hajnoczi67cc7e02014-06-18 17:58:32 +0800231void qdev_alias_all_properties(DeviceState *target, Object *source);
232
Peter Maydellb000dfb2013-03-25 13:40:44 +0000233/**
234 * @qdev_prop_set_after_realize:
235 * @dev: device
236 * @name: name of property
237 * @errp: indirect pointer to Error to be set
238 * Set the Error object to report that an attempt was made to set a property
239 * on a device after it has already been realized. This is a utility function
240 * which allows property-setter functions to easily report the error in
241 * a friendly format identifying both the device and the property.
242 */
243void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
244 Error **errp);
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +0100245
246/**
247 * qdev_prop_allow_set_link_before_realize:
248 *
249 * Set the #Error object if an attempt is made to set the link after realize.
250 * This function should be used as the check() argument to
251 * object_property_add_link().
252 */
Igor Mammedov8f5d58e2017-07-14 10:14:50 +0800253void qdev_prop_allow_set_link_before_realize(const Object *obj,
254 const char *name,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +0100255 Object *val, Error **errp);
256
Anthony Liguori074a86f2012-08-10 12:00:43 -0500257#endif