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