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
;
80 /* This structure should not be accessed directly. We declare it here
81 so that it can be embedded in individual device state structures. */
94 QLIST_HEAD(, BusState
) child_bus
;
96 QTAILQ_ENTRY(DeviceState
) sibling
;
97 int instance_id_alias
;
98 int alias_required_for_version
;
101 * This tracks the number of references between devices. See @qdev_ref for
106 QTAILQ_HEAD(, DeviceProperty
) properties
;
108 /* Do not, under any circumstance, use this parent link below anywhere
109 * outside of qdev.c. You have been warned. */
113 typedef void (*bus_dev_printfn
)(Monitor
*mon
, DeviceState
*dev
, int indent
);
114 typedef char *(*bus_get_dev_path
)(DeviceState
*dev
);
116 * This callback is used to create Open Firmware device path in accordance with
117 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
118 * can be found here http://playground.sun.com/1275/bindings/.
120 typedef char *(*bus_get_fw_dev_path
)(DeviceState
*dev
);
121 typedef int (qbus_resetfn
)(BusState
*bus
);
126 bus_dev_printfn print_dev
;
127 bus_get_dev_path get_dev_path
;
128 bus_get_fw_dev_path get_fw_dev_path
;
139 QTAILQ_HEAD(ChildrenHead
, DeviceState
) children
;
140 QLIST_ENTRY(BusState
) sibling
;
152 PROP_TYPE_UNSPEC
= 0,
169 struct PropertyInfo
{
171 const char *legacy_name
;
173 enum PropertyType type
;
176 int (*parse
)(DeviceState
*dev
, Property
*prop
, const char *str
);
177 int (*print
)(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
);
178 void (*free
)(DeviceState
*dev
, Property
*prop
);
179 DevicePropertyAccessor
*get
;
180 DevicePropertyAccessor
*set
;
183 typedef struct GlobalProperty
{
185 const char *property
;
187 QTAILQ_ENTRY(GlobalProperty
) next
;
190 /*** Board API. This should go away once we have a machine config file. ***/
192 DeviceState
*qdev_create(BusState
*bus
, const char *name
);
193 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
);
194 bool qdev_exists(const char *name
);
195 int qdev_device_help(QemuOpts
*opts
);
196 DeviceState
*qdev_device_add(QemuOpts
*opts
);
197 int qdev_init(DeviceState
*dev
) QEMU_WARN_UNUSED_RESULT
;
198 void qdev_init_nofail(DeviceState
*dev
);
199 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
200 int required_for_version
);
201 int qdev_unplug(DeviceState
*dev
);
202 void qdev_free(DeviceState
*dev
);
203 int qdev_simple_unplug_cb(DeviceState
*dev
);
204 void qdev_machine_creation_done(void);
205 bool qdev_machine_modified(void);
207 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
);
208 void qdev_connect_gpio_out(DeviceState
*dev
, int n
, qemu_irq pin
);
210 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
);
212 /*** Device API. ***/
214 typedef int (*qdev_initfn
)(DeviceState
*dev
, DeviceInfo
*info
);
215 typedef int (*qdev_event
)(DeviceState
*dev
);
216 typedef void (*qdev_resetfn
)(DeviceState
*dev
);
231 const VMStateDescription
*vmsd
;
233 /* Private to qdev / bus. */
238 struct DeviceInfo
*next
;
240 extern DeviceInfo
*device_info_list
;
242 void qdev_register(DeviceInfo
*info
);
244 /* Register device properties. */
245 /* GPIO inputs also double as IRQ sinks. */
246 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
);
247 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
);
249 CharDriverState
*qdev_init_chardev(DeviceState
*dev
);
251 BusState
*qdev_get_parent_bus(DeviceState
*dev
);
255 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
);
257 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
258 typedef int (qbus_walkerfn
)(BusState
*bus
, void *opaque
);
259 typedef int (qdev_walkerfn
)(DeviceState
*dev
, void *opaque
);
261 void qbus_create_inplace(BusState
*bus
, BusInfo
*info
,
262 DeviceState
*parent
, const char *name
);
263 BusState
*qbus_create(BusInfo
*info
, DeviceState
*parent
, const char *name
);
264 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
265 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
267 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
268 qbus_walkerfn
*busfn
, void *opaque
);
269 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
270 qbus_walkerfn
*busfn
, void *opaque
);
271 void qdev_reset_all(DeviceState
*dev
);
272 void qbus_reset_all_fn(void *opaque
);
274 void qbus_free(BusState
*bus
);
276 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
278 /* This should go away once we get rid of the NULL bus hack */
279 BusState
*sysbus_get_default(void);
281 /*** monitor commands ***/
283 void do_info_qtree(Monitor
*mon
);
284 void do_info_qdm(Monitor
*mon
);
285 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
);
286 int do_device_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
);
288 /*** qdev-properties.c ***/
290 extern PropertyInfo qdev_prop_bit
;
291 extern PropertyInfo qdev_prop_uint8
;
292 extern PropertyInfo qdev_prop_uint16
;
293 extern PropertyInfo qdev_prop_uint32
;
294 extern PropertyInfo qdev_prop_int32
;
295 extern PropertyInfo qdev_prop_uint64
;
296 extern PropertyInfo qdev_prop_hex8
;
297 extern PropertyInfo qdev_prop_hex32
;
298 extern PropertyInfo qdev_prop_hex64
;
299 extern PropertyInfo qdev_prop_string
;
300 extern PropertyInfo qdev_prop_chr
;
301 extern PropertyInfo qdev_prop_ptr
;
302 extern PropertyInfo qdev_prop_macaddr
;
303 extern PropertyInfo qdev_prop_drive
;
304 extern PropertyInfo qdev_prop_netdev
;
305 extern PropertyInfo qdev_prop_vlan
;
306 extern PropertyInfo qdev_prop_pci_devfn
;
308 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
311 .offset = offsetof(_state, _field) \
312 + type_check(_type,typeof_field(_state, _field)), \
314 #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
317 .offset = offsetof(_state, _field) \
318 + type_check(_type,typeof_field(_state, _field)), \
319 .defval = (_type[]) { _defval }, \
321 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
323 .info = &(qdev_prop_bit), \
325 .offset = offsetof(_state, _field) \
326 + type_check(uint32_t,typeof_field(_state, _field)), \
327 .defval = (bool[]) { (_defval) }, \
330 #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
331 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
332 #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
333 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
334 #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
335 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
336 #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
337 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
338 #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
339 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
340 #define DEFINE_PROP_HEX8(_n, _s, _f, _d) \
341 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
342 #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
343 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
344 #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
345 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
346 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
347 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
349 #define DEFINE_PROP_PTR(_n, _s, _f) \
350 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
351 #define DEFINE_PROP_CHR(_n, _s, _f) \
352 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
353 #define DEFINE_PROP_STRING(_n, _s, _f) \
354 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
355 #define DEFINE_PROP_NETDEV(_n, _s, _f) \
356 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
357 #define DEFINE_PROP_VLAN(_n, _s, _f) \
358 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
359 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
360 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
361 #define DEFINE_PROP_MACADDR(_n, _s, _f) \
362 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
364 #define DEFINE_PROP_END_OF_LIST() \
367 /* Set properties between creation and init. */
368 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
);
369 int qdev_prop_exists(DeviceState
*dev
, const char *name
);
370 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
);
371 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
);
372 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
);
373 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
);
374 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
);
375 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
);
376 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
);
377 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
);
378 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, char *value
);
379 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
);
380 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, VLANClientState
*value
);
381 void qdev_prop_set_vlan(DeviceState
*dev
, const char *name
, VLANState
*value
);
382 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
, BlockDriverState
*value
) QEMU_WARN_UNUSED_RESULT
;
383 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
, BlockDriverState
*value
);
384 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
);
385 /* FIXME: Remove opaque pointer properties. */
386 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
);
387 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
);
389 void qdev_prop_register_global_list(GlobalProperty
*props
);
390 void qdev_prop_set_globals(DeviceState
*dev
);
391 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
392 Property
*prop
, const char *value
);
394 DeviceInfo
*qdev_get_info(DeviceState
*dev
);
396 static inline const char *qdev_fw_name(DeviceState
*dev
)
398 DeviceInfo
*info
= qdev_get_info(dev
);
401 return info
->fw_name
;
402 } else if (info
->alias
) {
409 char *qdev_get_fw_dev_path(DeviceState
*dev
);
410 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
411 extern struct BusInfo system_bus_info
;
416 * Increase the reference count of a device. A device cannot be freed as long
417 * as its reference count is greater than zero.
421 void qdev_ref(DeviceState
*dev
);
426 * Decrease the reference count of a device. A device cannot be freed as long
427 * as its reference count is greater than zero.
431 void qdev_unref(DeviceState
*dev
);
434 * @qdev_property_add - add a new property to a device
436 * @dev - the device to add a property to
438 * @name - the name of the property. This can contain any character except for
439 * a forward slash. In general, you should use hyphens '-' instead of
440 * underscores '_' when naming properties.
442 * @type - the type name of the property. This namespace is pretty loosely
443 * defined. Sub namespaces are constructed by using a prefix and then
444 * to angle brackets. For instance, the type 'virtio-net-pci' in the
445 * 'link' namespace would be 'link<virtio-net-pci>'.
447 * @get - the getter to be called to read a property. If this is NULL, then
448 * the property cannot be read.
450 * @set - the setter to be called to write a property. If this is NULL,
451 * then the property cannot be written.
453 * @release - called when the property is removed from the device. This is
454 * meant to allow a property to free its opaque upon device
455 * destruction. This may be NULL.
457 * @opaque - an opaque pointer to pass to the callbacks for the property
459 * @errp - returns an error if this function fails
461 void qdev_property_add(DeviceState
*dev
, const char *name
, const char *type
,
462 DevicePropertyAccessor
*get
, DevicePropertyAccessor
*set
,
463 DevicePropertyRelease
*release
,
464 void *opaque
, Error
**errp
);
467 * @qdev_property_get - reads a property from a device
471 * @v - the visitor that will receive the property value. This should be an
472 * Output visitor and the data will be written with @name as the name.
474 * @name - the name of the property
476 * @errp - returns an error if this function fails
478 void qdev_property_get(DeviceState
*dev
, Visitor
*v
, const char *name
,
482 * @qdev_property_set - writes a property to a device
486 * @v - the visitor that will be used to write the property value. This should
487 * be an Input visitor and the data will be first read with @name as the
488 * name and then written as the property value.
490 * @name - the name of the property
492 * @errp - returns an error if this function fails
494 void qdev_property_set(DeviceState
*dev
, Visitor
*v
, const char *name
,
498 * @qdev_property_get_type - returns the type of a property
502 * @name - the name of the property
504 * @errp - returns an error if this function fails
507 * The type name of the property.
509 const char *qdev_property_get_type(DeviceState
*dev
, const char *name
,
513 * @qdev_property_add_static - add a @Property to a device referencing a
516 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
, Error
**errp
);
519 * @qdev_get_root - returns the root device of the composition tree
522 * The root of the composition tree.
524 DeviceState
*qdev_get_root(void);
527 * @qdev_get_canonical_path - returns the canonical path for a device. This
528 * is the path within the composition tree starting from the root.
531 * The canonical path in the composition tree.
533 gchar
*qdev_get_canonical_path(DeviceState
*dev
);
536 * @qdev_resolve_path - resolves a path returning a device
538 * There are two types of supported paths--absolute paths and partial paths.
540 * Absolute paths are derived from the root device and can follow child<> or
541 * link<> properties. Since they can follow link<> properties, they can be
542 * arbitrarily long. Absolute paths look like absolute filenames and are
543 * prefixed with a leading slash.
545 * Partial paths look like relative filenames. They do not begin with a
546 * prefix. The matching rules for partial paths are subtle but designed to make
547 * specifying devices easy. At each level of the composition tree, the partial
548 * path is matched as an absolute path. The first match is not returned. At
549 * least two matches are searched for. A successful result is only returned if
550 * only one match is founded. If more than one match is found, a flag is
551 * return to indicate that the match was ambiguous.
553 * @path - the path to resolve
555 * @ambiguous - returns true if the path resolution failed because of an
559 * The matched device or NULL on path lookup failure.
561 DeviceState
*qdev_resolve_path(const char *path
, bool *ambiguous
);
564 * @qdev_property_add_child - Add a child property to a device
566 * Child properties form the composition tree. All devices need to be a child
567 * of another device. Devices can only be a child of one device.
569 * There is no way for a child to determine what its parent is. It is not
570 * a bidirectional relationship. This is by design.
572 * @dev - the device to add a property to
574 * @name - the name of the property
576 * @child - the child device
578 * @errp - if an error occurs, a pointer to an area to store the area
580 void qdev_property_add_child(DeviceState
*dev
, const char *name
,
581 DeviceState
*child
, Error
**errp
);
584 * @qdev_property_add_link - Add a link property to a device
586 * Links establish relationships between devices. Links are unidirectional
587 * although two links can be combined to form a bidirectional relationship
590 * Links form the graph in the device model.
592 * @dev - the device to add a property to
594 * @name - the name of the property
596 * @type - the qdev type of the link
598 * @child - a pointer to where the link device reference is stored
600 * @errp - if an error occurs, a pointer to an area to store the area
602 void qdev_property_add_link(DeviceState
*dev
, const char *name
,
603 const char *type
, DeviceState
**child
,
607 * @qdev_property_add_str
609 * Add a string property using getters/setters. This function will add a
610 * property of type 'string'.
612 * @dev - the device to add a property to
614 * @name - the name of the property
616 * @get - the getter or NULL if the property is write-only. This function must
617 * return a string to be freed by @g_free().
619 * @set - the setter or NULL if the property is read-only
621 * @errp - if an error occurs, a pointer to an area to store the error
623 void qdev_property_add_str(DeviceState
*dev
, const char *name
,
624 char *(*get
)(DeviceState
*, Error
**),
625 void (*set
)(DeviceState
*, const char *, Error
**),
631 * Returns the string representation of the type of this object.
635 * @errp - if an error occurs, a pointer to an area to store the error
637 * Returns: a string representing the type. This must be freed by the caller
640 char *qdev_get_type(DeviceState
*dev
, Error
**errp
);
645 * Initialize platform devices before machine init. This is a hack until full
646 * support for composition is added.
648 void qdev_machine_init(void);