]> git.proxmox.com Git - qemu.git/blame - hw/qdev.h
qom: add new dynamic property infrastructure based on Visitors (v2)
[qemu.git] / hw / qdev.h
CommitLineData
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"
44677ded 8#include "qapi/qapi-visit-core.h"
aae9460e 9
ee6847d1
GH
10typedef struct Property Property;
11
12typedef struct PropertyInfo PropertyInfo;
aae9460e 13
b6b61144
GH
14typedef struct CompatProperty CompatProperty;
15
ee6847d1 16typedef struct DeviceInfo DeviceInfo;
aae9460e 17
02e2da45 18typedef struct BusState BusState;
4d6ae674 19
10c4c98a
GH
20typedef struct BusInfo BusInfo;
21
131ec1bd
GH
22enum DevState {
23 DEV_STATE_CREATED = 1,
24 DEV_STATE_INITIALIZED,
25};
26
75422b0d
AS
27enum {
28 DEV_NVECTORS_UNSPECIFIED = -1,
29};
30
44677ded
AL
31/**
32 * @DevicePropertyAccessor - called when trying to get/set a property
33 *
34 * @dev the device that owns the property
35 * @v the visitor that contains the property data
36 * @opaque the device property opaque
37 * @name the name of the property
38 * @errp a pointer to an Error that is filled if getting/setting fails.
39 */
40typedef void (DevicePropertyAccessor)(DeviceState *dev,
41 Visitor *v,
42 void *opaque,
43 const char *name,
44 Error **errp);
45
46/**
47 * @DevicePropertyRelease - called when a property is removed from a device
48 *
49 * @dev the device that owns the property
50 * @name the name of the property
51 * @opaque the opaque registered with the property
52 */
53typedef void (DevicePropertyRelease)(DeviceState *dev,
54 const char *name,
55 void *opaque);
56
57typedef struct DeviceProperty
58{
59 gchar *name;
60 gchar *type;
61 DevicePropertyAccessor *get;
62 DevicePropertyAccessor *set;
63 DevicePropertyRelease *release;
64 void *opaque;
65
66 QTAILQ_ENTRY(DeviceProperty) node;
67} DeviceProperty;
68
aae9460e
PB
69/* This structure should not be accessed directly. We declare it here
70 so that it can be embedded in individual device state structures. */
02e2da45 71struct DeviceState {
f31d07d1 72 const char *id;
131ec1bd 73 enum DevState state;
ef80b466 74 QemuOpts *opts;
3418bd25 75 int hotplugged;
042f84d0 76 DeviceInfo *info;
02e2da45 77 BusState *parent_bus;
aae9460e
PB
78 int num_gpio_out;
79 qemu_irq *gpio_out;
80 int num_gpio_in;
81 qemu_irq *gpio_in;
72cf2d4f 82 QLIST_HEAD(, BusState) child_bus;
d271de9f 83 int num_child_bus;
d8bb00d6 84 QTAILQ_ENTRY(DeviceState) sibling;
4d2ffa08
JK
85 int instance_id_alias;
86 int alias_required_for_version;
85ed303b
AL
87
88 /**
89 * This tracks the number of references between devices. See @qdev_ref for
90 * more information.
91 */
92 uint32_t ref;
44677ded
AL
93
94 QTAILQ_HEAD(, DeviceProperty) properties;
02e2da45
PB
95};
96
10c4c98a 97typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
6772b936 98typedef char *(*bus_get_dev_path)(DeviceState *dev);
21150814
GN
99/*
100 * This callback is used to create Open Firmware device path in accordance with
101 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
102 * can be found here http://playground.sun.com/1275/bindings/.
103 */
104typedef char *(*bus_get_fw_dev_path)(DeviceState *dev);
b4694b7c 105typedef int (qbus_resetfn)(BusState *bus);
6772b936 106
10c4c98a
GH
107struct BusInfo {
108 const char *name;
109 size_t size;
110 bus_dev_printfn print_dev;
6772b936 111 bus_get_dev_path get_dev_path;
21150814 112 bus_get_fw_dev_path get_fw_dev_path;
b4694b7c 113 qbus_resetfn *reset;
ee6847d1 114 Property *props;
10c4c98a 115};
02e2da45
PB
116
117struct BusState {
118 DeviceState *parent;
10c4c98a 119 BusInfo *info;
02e2da45 120 const char *name;
3418bd25 121 int allow_hotplug;
cd739fb6 122 int qdev_allocated;
f48a7a6e 123 QTAILQ_HEAD(ChildrenHead, DeviceState) children;
72cf2d4f 124 QLIST_ENTRY(BusState) sibling;
aae9460e
PB
125};
126
ee6847d1
GH
127struct Property {
128 const char *name;
129 PropertyInfo *info;
130 int offset;
d2364ee4 131 int bitnr;
ee6847d1
GH
132 void *defval;
133};
134
135enum PropertyType {
136 PROP_TYPE_UNSPEC = 0,
c7cc172d 137 PROP_TYPE_UINT8,
ee6847d1
GH
138 PROP_TYPE_UINT16,
139 PROP_TYPE_UINT32,
316940b0 140 PROP_TYPE_INT32,
5a053d1f 141 PROP_TYPE_UINT64,
ee6847d1
GH
142 PROP_TYPE_TADDR,
143 PROP_TYPE_MACADDR,
14b41872 144 PROP_TYPE_DRIVE,
313feaab 145 PROP_TYPE_CHR,
59419663 146 PROP_TYPE_STRING,
2ef924b4 147 PROP_TYPE_NETDEV,
851bec09 148 PROP_TYPE_VLAN,
ee6847d1 149 PROP_TYPE_PTR,
d2364ee4 150 PROP_TYPE_BIT,
ee6847d1
GH
151};
152
153struct PropertyInfo {
154 const char *name;
155 size_t size;
156 enum PropertyType type;
157 int (*parse)(DeviceState *dev, Property *prop, const char *str);
158 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
d21357df 159 void (*free)(DeviceState *dev, Property *prop);
ee6847d1
GH
160};
161
458fb679 162typedef struct GlobalProperty {
b6b61144
GH
163 const char *driver;
164 const char *property;
165 const char *value;
458fb679
GH
166 QTAILQ_ENTRY(GlobalProperty) next;
167} GlobalProperty;
b6b61144 168
aae9460e
PB
169/*** Board API. This should go away once we have a machine config file. ***/
170
02e2da45 171DeviceState *qdev_create(BusState *bus, const char *name);
0bcdeda7 172DeviceState *qdev_try_create(BusState *bus, const char *name);
ff952ba2 173int qdev_device_help(QemuOpts *opts);
f31d07d1 174DeviceState *qdev_device_add(QemuOpts *opts);
747bbdf7 175int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
e23a1b33 176void qdev_init_nofail(DeviceState *dev);
4d2ffa08
JK
177void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
178 int required_for_version);
3418bd25 179int qdev_unplug(DeviceState *dev);
02e2da45 180void qdev_free(DeviceState *dev);
3418bd25
GH
181int qdev_simple_unplug_cb(DeviceState *dev);
182void qdev_machine_creation_done(void);
0ac8ef71 183bool qdev_machine_modified(void);
aae9460e 184
aae9460e
PB
185qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
186void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
187
02e2da45 188BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
4d6ae674 189
aae9460e
PB
190/*** Device API. ***/
191
81a322d4 192typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
3418bd25 193typedef int (*qdev_event)(DeviceState *dev);
7f23f812 194typedef void (*qdev_resetfn)(DeviceState *dev);
aae9460e 195
02e2da45 196struct DeviceInfo {
074f2fff 197 const char *name;
779206de 198 const char *fw_name;
3320e56e
GH
199 const char *alias;
200 const char *desc;
074f2fff 201 size_t size;
ee6847d1 202 Property *props;
3320e56e 203 int no_user;
074f2fff 204
959f733a 205 /* callbacks */
7f23f812 206 qdev_resetfn reset;
959f733a 207
391a079e
GH
208 /* device state */
209 const VMStateDescription *vmsd;
210
074f2fff 211 /* Private to qdev / bus. */
02e2da45 212 qdev_initfn init;
3418bd25
GH
213 qdev_event unplug;
214 qdev_event exit;
10c4c98a 215 BusInfo *bus_info;
042f84d0 216 struct DeviceInfo *next;
02e2da45 217};
0958b4cc 218extern DeviceInfo *device_info_list;
02e2da45 219
074f2fff 220void qdev_register(DeviceInfo *info);
aae9460e
PB
221
222/* Register device properties. */
067a3ddc 223/* GPIO inputs also double as IRQ sinks. */
aae9460e
PB
224void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
225void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
226
227CharDriverState *qdev_init_chardev(DeviceState *dev);
228
02e2da45 229BusState *qdev_get_parent_bus(DeviceState *dev);
aae9460e 230
02e2da45
PB
231/*** BUS API. ***/
232
a2ee6b4f
IY
233DeviceState *qdev_find_recursive(BusState *bus, const char *id);
234
81699d8a
AL
235/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
236typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
237typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
238
cd739fb6
GH
239void qbus_create_inplace(BusState *bus, BusInfo *info,
240 DeviceState *parent, const char *name);
10c4c98a 241BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
81699d8a
AL
242/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
243 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
244 * 0 otherwise. */
245int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
246 qbus_walkerfn *busfn, void *opaque);
247int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
248 qbus_walkerfn *busfn, void *opaque);
5af0a04b 249void qdev_reset_all(DeviceState *dev);
80376c3f
IY
250void qbus_reset_all_fn(void *opaque);
251
131ec1bd 252void qbus_free(BusState *bus);
02e2da45
PB
253
254#define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
255
ec990eb6
AL
256/* This should go away once we get rid of the NULL bus hack */
257BusState *sysbus_get_default(void);
258
cae4956e
GH
259/*** monitor commands ***/
260
261void do_info_qtree(Monitor *mon);
f6c64e0e 262void do_info_qdm(Monitor *mon);
8bc27249 263int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
17a38eaa 264int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
cae4956e 265
ee6847d1
GH
266/*** qdev-properties.c ***/
267
d2364ee4 268extern PropertyInfo qdev_prop_bit;
c7cc172d 269extern PropertyInfo qdev_prop_uint8;
ee6847d1
GH
270extern PropertyInfo qdev_prop_uint16;
271extern PropertyInfo qdev_prop_uint32;
316940b0 272extern PropertyInfo qdev_prop_int32;
5a053d1f 273extern PropertyInfo qdev_prop_uint64;
6835678c 274extern PropertyInfo qdev_prop_hex8;
ee6847d1 275extern PropertyInfo qdev_prop_hex32;
5a053d1f 276extern PropertyInfo qdev_prop_hex64;
59419663 277extern PropertyInfo qdev_prop_string;
313feaab 278extern PropertyInfo qdev_prop_chr;
ee6847d1
GH
279extern PropertyInfo qdev_prop_ptr;
280extern PropertyInfo qdev_prop_macaddr;
14b41872 281extern PropertyInfo qdev_prop_drive;
851bec09
GH
282extern PropertyInfo qdev_prop_netdev;
283extern PropertyInfo qdev_prop_vlan;
05cb5fe4 284extern PropertyInfo qdev_prop_pci_devfn;
ee6847d1 285
cf12b95b
GH
286#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
287 .name = (_name), \
288 .info = &(_prop), \
289 .offset = offsetof(_state, _field) \
290 + type_check(_type,typeof_field(_state, _field)), \
291 }
292#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
293 .name = (_name), \
294 .info = &(_prop), \
295 .offset = offsetof(_state, _field) \
296 + type_check(_type,typeof_field(_state, _field)), \
297 .defval = (_type[]) { _defval }, \
298 }
d2364ee4
MT
299#define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
300 .name = (_name), \
301 .info = &(qdev_prop_bit), \
302 .bitnr = (_bit), \
303 .offset = offsetof(_state, _field) \
304 + type_check(uint32_t,typeof_field(_state, _field)), \
305 .defval = (bool[]) { (_defval) }, \
306 }
cf12b95b 307
c7cc172d
JQ
308#define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
309 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
cf12b95b
GH
310#define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
311 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
312#define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
313 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
316940b0
GH
314#define DEFINE_PROP_INT32(_n, _s, _f, _d) \
315 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
cf12b95b
GH
316#define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
317 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
6835678c
JK
318#define DEFINE_PROP_HEX8(_n, _s, _f, _d) \
319 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
cf12b95b
GH
320#define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
321 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
322#define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
323 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
324#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
325 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
326
327#define DEFINE_PROP_PTR(_n, _s, _f) \
328 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
313feaab
GH
329#define DEFINE_PROP_CHR(_n, _s, _f) \
330 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
59419663
GH
331#define DEFINE_PROP_STRING(_n, _s, _f) \
332 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
2ef924b4
GH
333#define DEFINE_PROP_NETDEV(_n, _s, _f) \
334 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
851bec09
GH
335#define DEFINE_PROP_VLAN(_n, _s, _f) \
336 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
f8b6cc00
MA
337#define DEFINE_PROP_DRIVE(_n, _s, _f) \
338 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
cf12b95b 339#define DEFINE_PROP_MACADDR(_n, _s, _f) \
1503fff3 340 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
cf12b95b
GH
341
342#define DEFINE_PROP_END_OF_LIST() \
343 {}
344
ee6847d1
GH
345/* Set properties between creation and init. */
346void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
d8ed79ae 347int qdev_prop_exists(DeviceState *dev, const char *name);
ee6847d1
GH
348int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
349void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
f4594a3b 350void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
c7cc172d 351void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
ee6847d1
GH
352void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
353void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
316940b0 354void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
5a053d1f 355void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
cc984673 356void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
313feaab 357void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
2ef924b4 358void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
851bec09 359void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
18846dee
MA
360int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
361void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
1503fff3 362void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
ee6847d1
GH
363/* FIXME: Remove opaque pointer properties. */
364void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
365void qdev_prop_set_defaults(DeviceState *dev, Property *props);
366
458fb679
GH
367void qdev_prop_register_global_list(GlobalProperty *props);
368void qdev_prop_set_globals(DeviceState *dev);
b6b61144 369
779206de
GN
370static inline const char *qdev_fw_name(DeviceState *dev)
371{
372 return dev->info->fw_name ? : dev->info->alias ? : dev->info->name;
373}
374
1ca4d09a 375char *qdev_get_fw_dev_path(DeviceState *dev);
a9ff9df1
BS
376/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
377extern struct BusInfo system_bus_info;
378
85ed303b
AL
379/**
380 * @qdev_ref
381 *
382 * Increase the reference count of a device. A device cannot be freed as long
383 * as its reference count is greater than zero.
384 *
385 * @dev - the device
386 */
387void qdev_ref(DeviceState *dev);
388
389/**
390 * @qdef_unref
391 *
392 * Decrease the reference count of a device. A device cannot be freed as long
393 * as its reference count is greater than zero.
394 *
395 * @dev - the device
396 */
397void qdev_unref(DeviceState *dev);
398
44677ded
AL
399/**
400 * @qdev_property_add - add a new property to a device
401 *
402 * @dev - the device to add a property to
403 *
404 * @name - the name of the property. This can contain any character except for
405 * a forward slash. In general, you should use hyphens '-' instead of
406 * underscores '_' when naming properties.
407 *
408 * @type - the type name of the property. This namespace is pretty loosely
409 * defined. Sub namespaces are constructed by using a prefix and then
410 * to angle brackets. For instance, the type 'virtio-net-pci' in the
411 * 'link' namespace would be 'link<virtio-net-pci>'.
412 *
413 * @get - the getter to be called to read a property. If this is NULL, then
414 * the property cannot be read.
415 *
416 * @set - the setter to be called to write a property. If this is NULL,
417 * then the property cannot be written.
418 *
419 * @release - called when the property is removed from the device. This is
420 * meant to allow a property to free its opaque upon device
421 * destruction. This may be NULL.
422 *
423 * @opaque - an opaque pointer to pass to the callbacks for the property
424 *
425 * @errp - returns an error if this function fails
426 */
427void qdev_property_add(DeviceState *dev, const char *name, const char *type,
428 DevicePropertyAccessor *get, DevicePropertyAccessor *set,
429 DevicePropertyRelease *release,
430 void *opaque, Error **errp);
431
432/**
433 * @qdev_property_get - reads a property from a device
434 *
435 * @dev - the device
436 *
437 * @v - the visitor that will receive the property value. This should be an
438 * Output visitor and the data will be written with @name as the name.
439 *
440 * @name - the name of the property
441 *
442 * @errp - returns an error if this function fails
443 */
444void qdev_property_get(DeviceState *dev, Visitor *v, const char *name,
445 Error **errp);
446
447/**
448 * @qdev_property_set - writes a property to a device
449 *
450 * @dev - the device
451 *
452 * @v - the visitor that will be used to write the property value. This should
453 * be an Input visitor and the data will be first read with @name as the
454 * name and then written as the property value.
455 *
456 * @name - the name of the property
457 *
458 * @errp - returns an error if this function fails
459 */
460void qdev_property_set(DeviceState *dev, Visitor *v, const char *name,
461 Error **errp);
462
463/**
464 * @qdev_property_get_type - returns the type of a property
465 *
466 * @dev - the device
467 *
468 * @name - the name of the property
469 *
470 * @errp - returns an error if this function fails
471 *
472 * Returns:
473 * The type name of the property.
474 */
475const char *qdev_property_get_type(DeviceState *dev, const char *name,
476 Error **errp);
477
aae9460e 478#endif