]>
Commit | Line | Data |
---|---|---|
aae9460e PB |
1 | #ifndef QDEV_H |
2 | #define QDEV_H | |
3 | ||
4 | #include "hw.h" | |
72cf2d4f | 5 | #include "qemu-queue.h" |
313feaab | 6 | #include "qemu-char.h" |
f31d07d1 | 7 | #include "qemu-option.h" |
aae9460e | 8 | |
ee6847d1 GH |
9 | typedef struct Property Property; |
10 | ||
11 | typedef struct PropertyInfo PropertyInfo; | |
aae9460e | 12 | |
b6b61144 GH |
13 | typedef struct CompatProperty CompatProperty; |
14 | ||
ee6847d1 | 15 | typedef struct DeviceInfo DeviceInfo; |
aae9460e | 16 | |
02e2da45 | 17 | typedef struct BusState BusState; |
4d6ae674 | 18 | |
10c4c98a GH |
19 | typedef struct BusInfo BusInfo; |
20 | ||
131ec1bd GH |
21 | enum DevState { |
22 | DEV_STATE_CREATED = 1, | |
23 | DEV_STATE_INITIALIZED, | |
24 | }; | |
25 | ||
75422b0d AS |
26 | enum { |
27 | DEV_NVECTORS_UNSPECIFIED = -1, | |
28 | }; | |
29 | ||
aae9460e PB |
30 | /* This structure should not be accessed directly. We declare it here |
31 | so that it can be embedded in individual device state structures. */ | |
02e2da45 | 32 | struct DeviceState { |
f31d07d1 | 33 | const char *id; |
131ec1bd | 34 | enum DevState state; |
ef80b466 | 35 | QemuOpts *opts; |
3418bd25 | 36 | int hotplugged; |
042f84d0 | 37 | DeviceInfo *info; |
02e2da45 | 38 | BusState *parent_bus; |
aae9460e PB |
39 | int num_gpio_out; |
40 | qemu_irq *gpio_out; | |
41 | int num_gpio_in; | |
42 | qemu_irq *gpio_in; | |
72cf2d4f | 43 | QLIST_HEAD(, BusState) child_bus; |
d271de9f | 44 | int num_child_bus; |
d8bb00d6 | 45 | QTAILQ_ENTRY(DeviceState) sibling; |
4d2ffa08 JK |
46 | int instance_id_alias; |
47 | int alias_required_for_version; | |
85ed303b AL |
48 | |
49 | /** | |
50 | * This tracks the number of references between devices. See @qdev_ref for | |
51 | * more information. | |
52 | */ | |
53 | uint32_t ref; | |
02e2da45 PB |
54 | }; |
55 | ||
10c4c98a | 56 | typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent); |
6772b936 | 57 | typedef char *(*bus_get_dev_path)(DeviceState *dev); |
21150814 GN |
58 | /* |
59 | * This callback is used to create Open Firmware device path in accordance with | |
60 | * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings | |
61 | * can be found here http://playground.sun.com/1275/bindings/. | |
62 | */ | |
63 | typedef char *(*bus_get_fw_dev_path)(DeviceState *dev); | |
b4694b7c | 64 | typedef int (qbus_resetfn)(BusState *bus); |
6772b936 | 65 | |
10c4c98a GH |
66 | struct BusInfo { |
67 | const char *name; | |
68 | size_t size; | |
69 | bus_dev_printfn print_dev; | |
6772b936 | 70 | bus_get_dev_path get_dev_path; |
21150814 | 71 | bus_get_fw_dev_path get_fw_dev_path; |
b4694b7c | 72 | qbus_resetfn *reset; |
ee6847d1 | 73 | Property *props; |
10c4c98a | 74 | }; |
02e2da45 PB |
75 | |
76 | struct BusState { | |
77 | DeviceState *parent; | |
10c4c98a | 78 | BusInfo *info; |
02e2da45 | 79 | const char *name; |
3418bd25 | 80 | int allow_hotplug; |
cd739fb6 | 81 | int qdev_allocated; |
f48a7a6e | 82 | QTAILQ_HEAD(ChildrenHead, DeviceState) children; |
72cf2d4f | 83 | QLIST_ENTRY(BusState) sibling; |
aae9460e PB |
84 | }; |
85 | ||
ee6847d1 GH |
86 | struct Property { |
87 | const char *name; | |
88 | PropertyInfo *info; | |
89 | int offset; | |
d2364ee4 | 90 | int bitnr; |
ee6847d1 GH |
91 | void *defval; |
92 | }; | |
93 | ||
94 | enum PropertyType { | |
95 | PROP_TYPE_UNSPEC = 0, | |
c7cc172d | 96 | PROP_TYPE_UINT8, |
ee6847d1 GH |
97 | PROP_TYPE_UINT16, |
98 | PROP_TYPE_UINT32, | |
316940b0 | 99 | PROP_TYPE_INT32, |
5a053d1f | 100 | PROP_TYPE_UINT64, |
ee6847d1 GH |
101 | PROP_TYPE_TADDR, |
102 | PROP_TYPE_MACADDR, | |
14b41872 | 103 | PROP_TYPE_DRIVE, |
313feaab | 104 | PROP_TYPE_CHR, |
59419663 | 105 | PROP_TYPE_STRING, |
2ef924b4 | 106 | PROP_TYPE_NETDEV, |
851bec09 | 107 | PROP_TYPE_VLAN, |
ee6847d1 | 108 | PROP_TYPE_PTR, |
d2364ee4 | 109 | PROP_TYPE_BIT, |
ee6847d1 GH |
110 | }; |
111 | ||
112 | struct PropertyInfo { | |
113 | const char *name; | |
114 | size_t size; | |
115 | enum PropertyType type; | |
116 | int (*parse)(DeviceState *dev, Property *prop, const char *str); | |
117 | int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len); | |
d21357df | 118 | void (*free)(DeviceState *dev, Property *prop); |
ee6847d1 GH |
119 | }; |
120 | ||
458fb679 | 121 | typedef struct GlobalProperty { |
b6b61144 GH |
122 | const char *driver; |
123 | const char *property; | |
124 | const char *value; | |
458fb679 GH |
125 | QTAILQ_ENTRY(GlobalProperty) next; |
126 | } GlobalProperty; | |
b6b61144 | 127 | |
aae9460e PB |
128 | /*** Board API. This should go away once we have a machine config file. ***/ |
129 | ||
02e2da45 | 130 | DeviceState *qdev_create(BusState *bus, const char *name); |
0bcdeda7 | 131 | DeviceState *qdev_try_create(BusState *bus, const char *name); |
ff952ba2 | 132 | int qdev_device_help(QemuOpts *opts); |
f31d07d1 | 133 | DeviceState *qdev_device_add(QemuOpts *opts); |
747bbdf7 | 134 | int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT; |
e23a1b33 | 135 | void qdev_init_nofail(DeviceState *dev); |
4d2ffa08 JK |
136 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
137 | int required_for_version); | |
3418bd25 | 138 | int qdev_unplug(DeviceState *dev); |
02e2da45 | 139 | void qdev_free(DeviceState *dev); |
3418bd25 GH |
140 | int qdev_simple_unplug_cb(DeviceState *dev); |
141 | void qdev_machine_creation_done(void); | |
0ac8ef71 | 142 | bool qdev_machine_modified(void); |
aae9460e | 143 | |
aae9460e PB |
144 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n); |
145 | void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin); | |
146 | ||
02e2da45 | 147 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name); |
4d6ae674 | 148 | |
aae9460e PB |
149 | /*** Device API. ***/ |
150 | ||
81a322d4 | 151 | typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info); |
3418bd25 | 152 | typedef int (*qdev_event)(DeviceState *dev); |
7f23f812 | 153 | typedef void (*qdev_resetfn)(DeviceState *dev); |
aae9460e | 154 | |
02e2da45 | 155 | struct DeviceInfo { |
074f2fff | 156 | const char *name; |
779206de | 157 | const char *fw_name; |
3320e56e GH |
158 | const char *alias; |
159 | const char *desc; | |
074f2fff | 160 | size_t size; |
ee6847d1 | 161 | Property *props; |
3320e56e | 162 | int no_user; |
074f2fff | 163 | |
959f733a | 164 | /* callbacks */ |
7f23f812 | 165 | qdev_resetfn reset; |
959f733a | 166 | |
391a079e GH |
167 | /* device state */ |
168 | const VMStateDescription *vmsd; | |
169 | ||
074f2fff | 170 | /* Private to qdev / bus. */ |
02e2da45 | 171 | qdev_initfn init; |
3418bd25 GH |
172 | qdev_event unplug; |
173 | qdev_event exit; | |
10c4c98a | 174 | BusInfo *bus_info; |
042f84d0 | 175 | struct DeviceInfo *next; |
02e2da45 | 176 | }; |
0958b4cc | 177 | extern DeviceInfo *device_info_list; |
02e2da45 | 178 | |
074f2fff | 179 | void qdev_register(DeviceInfo *info); |
aae9460e PB |
180 | |
181 | /* Register device properties. */ | |
067a3ddc | 182 | /* GPIO inputs also double as IRQ sinks. */ |
aae9460e PB |
183 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n); |
184 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n); | |
185 | ||
186 | CharDriverState *qdev_init_chardev(DeviceState *dev); | |
187 | ||
02e2da45 | 188 | BusState *qdev_get_parent_bus(DeviceState *dev); |
aae9460e | 189 | |
02e2da45 PB |
190 | /*** BUS API. ***/ |
191 | ||
a2ee6b4f IY |
192 | DeviceState *qdev_find_recursive(BusState *bus, const char *id); |
193 | ||
81699d8a AL |
194 | /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */ |
195 | typedef int (qbus_walkerfn)(BusState *bus, void *opaque); | |
196 | typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque); | |
197 | ||
cd739fb6 GH |
198 | void qbus_create_inplace(BusState *bus, BusInfo *info, |
199 | DeviceState *parent, const char *name); | |
10c4c98a | 200 | BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name); |
81699d8a AL |
201 | /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion, |
202 | * < 0 if either devfn or busfn terminate walk somewhere in cursion, | |
203 | * 0 otherwise. */ | |
204 | int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, | |
205 | qbus_walkerfn *busfn, void *opaque); | |
206 | int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, | |
207 | qbus_walkerfn *busfn, void *opaque); | |
5af0a04b | 208 | void qdev_reset_all(DeviceState *dev); |
80376c3f IY |
209 | void qbus_reset_all_fn(void *opaque); |
210 | ||
131ec1bd | 211 | void qbus_free(BusState *bus); |
02e2da45 PB |
212 | |
213 | #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev) | |
214 | ||
ec990eb6 AL |
215 | /* This should go away once we get rid of the NULL bus hack */ |
216 | BusState *sysbus_get_default(void); | |
217 | ||
cae4956e GH |
218 | /*** monitor commands ***/ |
219 | ||
220 | void do_info_qtree(Monitor *mon); | |
f6c64e0e | 221 | void do_info_qdm(Monitor *mon); |
8bc27249 | 222 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data); |
17a38eaa | 223 | int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data); |
cae4956e | 224 | |
ee6847d1 GH |
225 | /*** qdev-properties.c ***/ |
226 | ||
d2364ee4 | 227 | extern PropertyInfo qdev_prop_bit; |
c7cc172d | 228 | extern PropertyInfo qdev_prop_uint8; |
ee6847d1 GH |
229 | extern PropertyInfo qdev_prop_uint16; |
230 | extern PropertyInfo qdev_prop_uint32; | |
316940b0 | 231 | extern PropertyInfo qdev_prop_int32; |
5a053d1f | 232 | extern PropertyInfo qdev_prop_uint64; |
6835678c | 233 | extern PropertyInfo qdev_prop_hex8; |
ee6847d1 | 234 | extern PropertyInfo qdev_prop_hex32; |
5a053d1f | 235 | extern PropertyInfo qdev_prop_hex64; |
59419663 | 236 | extern PropertyInfo qdev_prop_string; |
313feaab | 237 | extern PropertyInfo qdev_prop_chr; |
ee6847d1 GH |
238 | extern PropertyInfo qdev_prop_ptr; |
239 | extern PropertyInfo qdev_prop_macaddr; | |
14b41872 | 240 | extern PropertyInfo qdev_prop_drive; |
851bec09 GH |
241 | extern PropertyInfo qdev_prop_netdev; |
242 | extern PropertyInfo qdev_prop_vlan; | |
05cb5fe4 | 243 | extern PropertyInfo qdev_prop_pci_devfn; |
ee6847d1 | 244 | |
cf12b95b GH |
245 | #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \ |
246 | .name = (_name), \ | |
247 | .info = &(_prop), \ | |
248 | .offset = offsetof(_state, _field) \ | |
249 | + type_check(_type,typeof_field(_state, _field)), \ | |
250 | } | |
251 | #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \ | |
252 | .name = (_name), \ | |
253 | .info = &(_prop), \ | |
254 | .offset = offsetof(_state, _field) \ | |
255 | + type_check(_type,typeof_field(_state, _field)), \ | |
256 | .defval = (_type[]) { _defval }, \ | |
257 | } | |
d2364ee4 MT |
258 | #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \ |
259 | .name = (_name), \ | |
260 | .info = &(qdev_prop_bit), \ | |
261 | .bitnr = (_bit), \ | |
262 | .offset = offsetof(_state, _field) \ | |
263 | + type_check(uint32_t,typeof_field(_state, _field)), \ | |
264 | .defval = (bool[]) { (_defval) }, \ | |
265 | } | |
cf12b95b | 266 | |
c7cc172d JQ |
267 | #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \ |
268 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t) | |
cf12b95b GH |
269 | #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \ |
270 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t) | |
271 | #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \ | |
272 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t) | |
316940b0 GH |
273 | #define DEFINE_PROP_INT32(_n, _s, _f, _d) \ |
274 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t) | |
cf12b95b GH |
275 | #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \ |
276 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t) | |
6835678c JK |
277 | #define DEFINE_PROP_HEX8(_n, _s, _f, _d) \ |
278 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t) | |
cf12b95b GH |
279 | #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \ |
280 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t) | |
281 | #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \ | |
282 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t) | |
283 | #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \ | |
284 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t) | |
285 | ||
286 | #define DEFINE_PROP_PTR(_n, _s, _f) \ | |
287 | DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*) | |
313feaab GH |
288 | #define DEFINE_PROP_CHR(_n, _s, _f) \ |
289 | DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*) | |
59419663 GH |
290 | #define DEFINE_PROP_STRING(_n, _s, _f) \ |
291 | DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*) | |
2ef924b4 GH |
292 | #define DEFINE_PROP_NETDEV(_n, _s, _f) \ |
293 | DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*) | |
851bec09 GH |
294 | #define DEFINE_PROP_VLAN(_n, _s, _f) \ |
295 | DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*) | |
f8b6cc00 MA |
296 | #define DEFINE_PROP_DRIVE(_n, _s, _f) \ |
297 | DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *) | |
cf12b95b | 298 | #define DEFINE_PROP_MACADDR(_n, _s, _f) \ |
1503fff3 | 299 | DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr) |
cf12b95b GH |
300 | |
301 | #define DEFINE_PROP_END_OF_LIST() \ | |
302 | {} | |
303 | ||
ee6847d1 GH |
304 | /* Set properties between creation and init. */ |
305 | void *qdev_get_prop_ptr(DeviceState *dev, Property *prop); | |
d8ed79ae | 306 | int qdev_prop_exists(DeviceState *dev, const char *name); |
ee6847d1 GH |
307 | int qdev_prop_parse(DeviceState *dev, const char *name, const char *value); |
308 | void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type); | |
f4594a3b | 309 | void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value); |
c7cc172d | 310 | void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value); |
ee6847d1 GH |
311 | void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value); |
312 | void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value); | |
316940b0 | 313 | void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value); |
5a053d1f | 314 | void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value); |
cc984673 | 315 | void qdev_prop_set_string(DeviceState *dev, const char *name, char *value); |
313feaab | 316 | void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value); |
2ef924b4 | 317 | void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value); |
851bec09 | 318 | void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value); |
18846dee MA |
319 | int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT; |
320 | void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value); | |
1503fff3 | 321 | void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value); |
ee6847d1 GH |
322 | /* FIXME: Remove opaque pointer properties. */ |
323 | void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value); | |
324 | void qdev_prop_set_defaults(DeviceState *dev, Property *props); | |
325 | ||
458fb679 GH |
326 | void qdev_prop_register_global_list(GlobalProperty *props); |
327 | void qdev_prop_set_globals(DeviceState *dev); | |
b6b61144 | 328 | |
779206de GN |
329 | static inline const char *qdev_fw_name(DeviceState *dev) |
330 | { | |
331 | return dev->info->fw_name ? : dev->info->alias ? : dev->info->name; | |
332 | } | |
333 | ||
1ca4d09a | 334 | char *qdev_get_fw_dev_path(DeviceState *dev); |
a9ff9df1 BS |
335 | /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ |
336 | extern struct BusInfo system_bus_info; | |
337 | ||
85ed303b AL |
338 | /** |
339 | * @qdev_ref | |
340 | * | |
341 | * Increase the reference count of a device. A device cannot be freed as long | |
342 | * as its reference count is greater than zero. | |
343 | * | |
344 | * @dev - the device | |
345 | */ | |
346 | void qdev_ref(DeviceState *dev); | |
347 | ||
348 | /** | |
349 | * @qdef_unref | |
350 | * | |
351 | * Decrease the reference count of a device. A device cannot be freed as long | |
352 | * as its reference count is greater than zero. | |
353 | * | |
354 | * @dev - the device | |
355 | */ | |
356 | void qdev_unref(DeviceState *dev); | |
357 | ||
aae9460e | 358 | #endif |