5 #include "qemu-queue.h"
7 #include "qemu-option.h"
8 #include "qapi/qapi-visit-core.h"
9 #include "qemu/object.h"
11 typedef struct Property Property
;
13 typedef struct PropertyInfo PropertyInfo
;
15 typedef struct CompatProperty CompatProperty
;
17 typedef struct DeviceInfo DeviceInfo
;
19 typedef struct BusState BusState
;
21 typedef struct BusInfo BusInfo
;
24 DEV_STATE_CREATED
= 1,
25 DEV_STATE_INITIALIZED
,
29 DEV_NVECTORS_UNSPECIFIED
= -1,
33 * @DevicePropertyAccessor - called when trying to get/set a property
35 * @dev the device that owns the property
36 * @v the visitor that contains the property data
37 * @opaque the device property opaque
38 * @name the name of the property
39 * @errp a pointer to an Error that is filled if getting/setting fails.
41 typedef void (DevicePropertyAccessor
)(DeviceState
*dev
,
48 * @DevicePropertyRelease - called when a property is removed from a device
50 * @dev the device that owns the property
51 * @name the name of the property
52 * @opaque the opaque registered with the property
54 typedef void (DevicePropertyRelease
)(DeviceState
*dev
,
58 typedef struct DeviceProperty
62 DevicePropertyAccessor
*get
;
63 DevicePropertyAccessor
*set
;
64 DevicePropertyRelease
*release
;
67 QTAILQ_ENTRY(DeviceProperty
) node
;
70 #define TYPE_DEVICE "device"
71 #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
72 #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
73 #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
75 typedef struct DeviceClass
{
76 ObjectClass parent_class
;
78 void (*reset
)(DeviceState
*dev
);
81 /* This structure should not be accessed directly. We declare it here
82 so that it can be embedded in individual device state structures. */
95 QLIST_HEAD(, BusState
) child_bus
;
97 QTAILQ_ENTRY(DeviceState
) sibling
;
98 int instance_id_alias
;
99 int alias_required_for_version
;
102 * This tracks the number of references between devices. See @qdev_ref for
107 QTAILQ_HEAD(, DeviceProperty
) properties
;
109 /* Do not, under any circumstance, use this parent link below anywhere
110 * outside of qdev.c. You have been warned. */
114 typedef void (*bus_dev_printfn
)(Monitor
*mon
, DeviceState
*dev
, int indent
);
115 typedef char *(*bus_get_dev_path
)(DeviceState
*dev
);
117 * This callback is used to create Open Firmware device path in accordance with
118 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
119 * can be found here http://playground.sun.com/1275/bindings/.
121 typedef char *(*bus_get_fw_dev_path
)(DeviceState
*dev
);
122 typedef int (qbus_resetfn
)(BusState
*bus
);
127 bus_dev_printfn print_dev
;
128 bus_get_dev_path get_dev_path
;
129 bus_get_fw_dev_path get_fw_dev_path
;
140 QTAILQ_HEAD(ChildrenHead
, DeviceState
) children
;
141 QLIST_ENTRY(BusState
) sibling
;
153 PROP_TYPE_UNSPEC
= 0,
161 PROP_TYPE_LOSTTICKPOLICY
,
171 struct PropertyInfo
{
173 const char *legacy_name
;
175 enum PropertyType type
;
178 int (*parse
)(DeviceState
*dev
, Property
*prop
, const char *str
);
179 int (*print
)(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
);
180 void (*free
)(DeviceState
*dev
, Property
*prop
);
181 DevicePropertyAccessor
*get
;
182 DevicePropertyAccessor
*set
;
185 typedef struct GlobalProperty
{
187 const char *property
;
189 QTAILQ_ENTRY(GlobalProperty
) next
;
192 /*** Board API. This should go away once we have a machine config file. ***/
194 DeviceState
*qdev_create(BusState
*bus
, const char *name
);
195 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
);
196 bool qdev_exists(const char *name
);
197 int qdev_device_help(QemuOpts
*opts
);
198 DeviceState
*qdev_device_add(QemuOpts
*opts
);
199 int qdev_init(DeviceState
*dev
) QEMU_WARN_UNUSED_RESULT
;
200 void qdev_init_nofail(DeviceState
*dev
);
201 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
202 int required_for_version
);
203 int qdev_unplug(DeviceState
*dev
);
204 void qdev_free(DeviceState
*dev
);
205 int qdev_simple_unplug_cb(DeviceState
*dev
);
206 void qdev_machine_creation_done(void);
207 bool qdev_machine_modified(void);
209 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
);
210 void qdev_connect_gpio_out(DeviceState
*dev
, int n
, qemu_irq pin
);
212 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
);
214 /*** Device API. ***/
216 typedef int (*qdev_initfn
)(DeviceState
*dev
, DeviceInfo
*info
);
217 typedef int (*qdev_event
)(DeviceState
*dev
);
218 typedef void (*qdev_resetfn
)(DeviceState
*dev
);
233 const VMStateDescription
*vmsd
;
236 * See #TypeInfo::class_init()
238 void (*class_init
)(ObjectClass
*klass
, void *data
);
240 /* Private to qdev / bus. */
245 struct DeviceInfo
*next
;
247 extern DeviceInfo
*device_info_list
;
249 void qdev_register(DeviceInfo
*info
);
250 void qdev_register_subclass(DeviceInfo
*info
, const char *parent
);
252 /* Register device properties. */
253 /* GPIO inputs also double as IRQ sinks. */
254 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
);
255 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
);
257 CharDriverState
*qdev_init_chardev(DeviceState
*dev
);
259 BusState
*qdev_get_parent_bus(DeviceState
*dev
);
263 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
);
265 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
266 typedef int (qbus_walkerfn
)(BusState
*bus
, void *opaque
);
267 typedef int (qdev_walkerfn
)(DeviceState
*dev
, void *opaque
);
269 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
270 DeviceState
*parent
, const char *name
);
271 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
);
272 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
273 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
275 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
276 qbus_walkerfn
*busfn
, void *opaque
);
277 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
278 qbus_walkerfn
*busfn
, void *opaque
);
279 void qdev_reset_all(DeviceState
*dev
);
280 void qbus_reset_all_fn(void *opaque
);
282 void qbus_free(BusState
*bus
);
284 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
286 /* This should go away once we get rid of the NULL bus hack */
287 BusState
*sysbus_get_default(void);
289 /*** monitor commands ***/
291 void do_info_qtree(Monitor
*mon
);
292 void do_info_qdm(Monitor
*mon
);
293 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
);
294 int do_device_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
);
296 /*** qdev-properties.c ***/
298 extern PropertyInfo qdev_prop_bit
;
299 extern PropertyInfo qdev_prop_uint8
;
300 extern PropertyInfo qdev_prop_uint16
;
301 extern PropertyInfo qdev_prop_uint32
;
302 extern PropertyInfo qdev_prop_int32
;
303 extern PropertyInfo qdev_prop_uint64
;
304 extern PropertyInfo qdev_prop_hex8
;
305 extern PropertyInfo qdev_prop_hex32
;
306 extern PropertyInfo qdev_prop_hex64
;
307 extern PropertyInfo qdev_prop_string
;
308 extern PropertyInfo qdev_prop_chr
;
309 extern PropertyInfo qdev_prop_ptr
;
310 extern PropertyInfo qdev_prop_macaddr
;
311 extern PropertyInfo qdev_prop_losttickpolicy
;
312 extern PropertyInfo qdev_prop_drive
;
313 extern PropertyInfo qdev_prop_netdev
;
314 extern PropertyInfo qdev_prop_vlan
;
315 extern PropertyInfo qdev_prop_pci_devfn
;
317 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
320 .offset = offsetof(_state, _field) \
321 + type_check(_type,typeof_field(_state, _field)), \
323 #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
326 .offset = offsetof(_state, _field) \
327 + type_check(_type,typeof_field(_state, _field)), \
328 .defval = (_type[]) { _defval }, \
330 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
332 .info = &(qdev_prop_bit), \
334 .offset = offsetof(_state, _field) \
335 + type_check(uint32_t,typeof_field(_state, _field)), \
336 .defval = (bool[]) { (_defval) }, \
339 #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
340 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
341 #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
342 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
343 #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
344 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
345 #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
346 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
347 #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
348 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
349 #define DEFINE_PROP_HEX8(_n, _s, _f, _d) \
350 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
351 #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
352 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
353 #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
354 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
355 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
356 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
358 #define DEFINE_PROP_PTR(_n, _s, _f) \
359 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
360 #define DEFINE_PROP_CHR(_n, _s, _f) \
361 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
362 #define DEFINE_PROP_STRING(_n, _s, _f) \
363 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
364 #define DEFINE_PROP_NETDEV(_n, _s, _f) \
365 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
366 #define DEFINE_PROP_VLAN(_n, _s, _f) \
367 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
368 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
369 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
370 #define DEFINE_PROP_MACADDR(_n, _s, _f) \
371 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
372 #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
373 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
376 #define DEFINE_PROP_END_OF_LIST() \
379 /* Set properties between creation and init. */
380 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
);
381 int qdev_prop_exists(DeviceState
*dev
, const char *name
);
382 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
);
383 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
);
384 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
);
385 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
);
386 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
);
387 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
);
388 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
);
389 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
);
390 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, char *value
);
391 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
);
392 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, VLANClientState
*value
);
393 void qdev_prop_set_vlan(DeviceState
*dev
, const char *name
, VLANState
*value
);
394 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
, BlockDriverState
*value
) QEMU_WARN_UNUSED_RESULT
;
395 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
, BlockDriverState
*value
);
396 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
);
397 void qdev_prop_set_losttickpolicy(DeviceState
*dev
, const char *name
,
398 LostTickPolicy
*value
);
399 /* FIXME: Remove opaque pointer properties. */
400 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
);
401 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
);
403 void qdev_prop_register_global_list(GlobalProperty
*props
);
404 void qdev_prop_set_globals(DeviceState
*dev
);
405 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
406 Property
*prop
, const char *value
);
408 DeviceInfo
*qdev_get_info(DeviceState
*dev
);
410 static inline const char *qdev_fw_name(DeviceState
*dev
)
412 DeviceInfo
*info
= qdev_get_info(dev
);
415 return info
->fw_name
;
416 } else if (info
->alias
) {
420 return object_get_typename(OBJECT(dev
));
423 char *qdev_get_fw_dev_path(DeviceState
*dev
);
424 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
425 extern struct BusInfo system_bus_info
;
430 * Increase the reference count of a device. A device cannot be freed as long
431 * as its reference count is greater than zero.
435 void qdev_ref(DeviceState
*dev
);
440 * Decrease the reference count of a device. A device cannot be freed as long
441 * as its reference count is greater than zero.
445 void qdev_unref(DeviceState
*dev
);
448 * @qdev_property_add - add a new property to a device
450 * @dev - the device to add a property to
452 * @name - the name of the property. This can contain any character except for
453 * a forward slash. In general, you should use hyphens '-' instead of
454 * underscores '_' when naming properties.
456 * @type - the type name of the property. This namespace is pretty loosely
457 * defined. Sub namespaces are constructed by using a prefix and then
458 * to angle brackets. For instance, the type 'virtio-net-pci' in the
459 * 'link' namespace would be 'link<virtio-net-pci>'.
461 * @get - the getter to be called to read a property. If this is NULL, then
462 * the property cannot be read.
464 * @set - the setter to be called to write a property. If this is NULL,
465 * then the property cannot be written.
467 * @release - called when the property is removed from the device. This is
468 * meant to allow a property to free its opaque upon device
469 * destruction. This may be NULL.
471 * @opaque - an opaque pointer to pass to the callbacks for the property
473 * @errp - returns an error if this function fails
475 void qdev_property_add(DeviceState
*dev
, const char *name
, const char *type
,
476 DevicePropertyAccessor
*get
, DevicePropertyAccessor
*set
,
477 DevicePropertyRelease
*release
,
478 void *opaque
, Error
**errp
);
481 * @qdev_property_get - reads a property from a device
485 * @v - the visitor that will receive the property value. This should be an
486 * Output visitor and the data will be written with @name as the name.
488 * @name - the name of the property
490 * @errp - returns an error if this function fails
492 void qdev_property_get(DeviceState
*dev
, Visitor
*v
, const char *name
,
496 * @qdev_property_set - writes a property to a device
500 * @v - the visitor that will be used to write the property value. This should
501 * be an Input visitor and the data will be first read with @name as the
502 * name and then written as the property value.
504 * @name - the name of the property
506 * @errp - returns an error if this function fails
508 void qdev_property_set(DeviceState
*dev
, Visitor
*v
, const char *name
,
512 * @qdev_property_get_type - returns the type of a property
516 * @name - the name of the property
518 * @errp - returns an error if this function fails
521 * The type name of the property.
523 const char *qdev_property_get_type(DeviceState
*dev
, const char *name
,
527 * @qdev_property_add_static - add a @Property to a device referencing a
530 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
, Error
**errp
);
533 * @qdev_get_root - returns the root device of the composition tree
536 * The root of the composition tree.
538 DeviceState
*qdev_get_root(void);
541 * @qdev_get_canonical_path - returns the canonical path for a device. This
542 * is the path within the composition tree starting from the root.
545 * The canonical path in the composition tree.
547 gchar
*qdev_get_canonical_path(DeviceState
*dev
);
550 * @qdev_resolve_path - resolves a path returning a device
552 * There are two types of supported paths--absolute paths and partial paths.
554 * Absolute paths are derived from the root device and can follow child<> or
555 * link<> properties. Since they can follow link<> properties, they can be
556 * arbitrarily long. Absolute paths look like absolute filenames and are
557 * prefixed with a leading slash.
559 * Partial paths look like relative filenames. They do not begin with a
560 * prefix. The matching rules for partial paths are subtle but designed to make
561 * specifying devices easy. At each level of the composition tree, the partial
562 * path is matched as an absolute path. The first match is not returned. At
563 * least two matches are searched for. A successful result is only returned if
564 * only one match is founded. If more than one match is found, a flag is
565 * return to indicate that the match was ambiguous.
567 * @path - the path to resolve
569 * @ambiguous - returns true if the path resolution failed because of an
573 * The matched device or NULL on path lookup failure.
575 DeviceState
*qdev_resolve_path(const char *path
, bool *ambiguous
);
578 * @qdev_property_add_child - Add a child property to a device
580 * Child properties form the composition tree. All devices need to be a child
581 * of another device. Devices can only be a child of one device.
583 * There is no way for a child to determine what its parent is. It is not
584 * a bidirectional relationship. This is by design.
586 * @dev - the device to add a property to
588 * @name - the name of the property
590 * @child - the child device
592 * @errp - if an error occurs, a pointer to an area to store the area
594 void qdev_property_add_child(DeviceState
*dev
, const char *name
,
595 DeviceState
*child
, Error
**errp
);
598 * @qdev_property_add_link - Add a link property to a device
600 * Links establish relationships between devices. Links are unidirectional
601 * although two links can be combined to form a bidirectional relationship
604 * Links form the graph in the device model.
606 * @dev - the device to add a property to
608 * @name - the name of the property
610 * @type - the qdev type of the link
612 * @child - a pointer to where the link device reference is stored
614 * @errp - if an error occurs, a pointer to an area to store the area
616 void qdev_property_add_link(DeviceState
*dev
, const char *name
,
617 const char *type
, DeviceState
**child
,
621 * @qdev_property_add_str
623 * Add a string property using getters/setters. This function will add a
624 * property of type 'string'.
626 * @dev - the device to add a property to
628 * @name - the name of the property
630 * @get - the getter or NULL if the property is write-only. This function must
631 * return a string to be freed by @g_free().
633 * @set - the setter or NULL if the property is read-only
635 * @errp - if an error occurs, a pointer to an area to store the error
637 void qdev_property_add_str(DeviceState
*dev
, const char *name
,
638 char *(*get
)(DeviceState
*, Error
**),
639 void (*set
)(DeviceState
*, const char *, Error
**),
645 * Returns the string representation of the type of this object.
649 * @errp - if an error occurs, a pointer to an area to store the error
651 * Returns: a string representing the type. This must be freed by the caller
654 char *qdev_get_type(DeviceState
*dev
, Error
**errp
);
659 * Initialize platform devices before machine init. This is a hack until full
660 * support for composition is added.
662 void qdev_machine_init(void);
667 * Reset a single device (by calling the reset method).
669 void device_reset(DeviceState
*dev
);