2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 /* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
28 #include "qemu/osdep.h"
30 #include "hw/fw-path-provider.h"
31 #include "sysemu/sysemu.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu/error-report.h"
36 #include "hw/hotplug.h"
37 #include "hw/boards.h"
38 #include "qapi-event.h"
41 static bool qdev_hot_added
= false;
42 static bool qdev_hot_removed
= false;
44 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
)
46 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
50 const char *qdev_fw_name(DeviceState
*dev
)
52 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
58 return object_get_typename(OBJECT(dev
));
61 static void bus_remove_child(BusState
*bus
, DeviceState
*child
)
65 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
66 if (kid
->child
== child
) {
69 snprintf(name
, sizeof(name
), "child[%d]", kid
->index
);
70 QTAILQ_REMOVE(&bus
->children
, kid
, sibling
);
72 /* This gives back ownership of kid->child back to us. */
73 object_property_del(OBJECT(bus
), name
, NULL
);
74 object_unref(OBJECT(kid
->child
));
81 static void bus_add_child(BusState
*bus
, DeviceState
*child
)
84 BusChild
*kid
= g_malloc0(sizeof(*kid
));
86 kid
->index
= bus
->max_index
++;
88 object_ref(OBJECT(kid
->child
));
90 QTAILQ_INSERT_HEAD(&bus
->children
, kid
, sibling
);
92 /* This transfers ownership of kid->child to the property. */
93 snprintf(name
, sizeof(name
), "child[%d]", kid
->index
);
94 object_property_add_link(OBJECT(bus
), name
,
95 object_get_typename(OBJECT(child
)),
96 (Object
**)&kid
->child
,
97 NULL
, /* read-only property */
98 0, /* return ownership on prop deletion */
102 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
)
104 dev
->parent_bus
= bus
;
105 object_ref(OBJECT(bus
));
106 bus_add_child(bus
, dev
);
109 /* Create a new device. This only initializes the device state
110 structure and allows properties to be set. The device still needs
111 to be realized. See qdev-core.h. */
112 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
116 dev
= qdev_try_create(bus
, name
);
119 error_report("Unknown device '%s' for bus '%s'", name
,
120 object_get_typename(OBJECT(bus
)));
122 error_report("Unknown device '%s' for default sysbus", name
);
130 DeviceState
*qdev_try_create(BusState
*bus
, const char *type
)
134 if (object_class_by_name(type
) == NULL
) {
137 dev
= DEVICE(object_new(type
));
143 bus
= sysbus_get_default();
146 qdev_set_parent_bus(dev
, bus
);
147 object_unref(OBJECT(dev
));
151 static QTAILQ_HEAD(device_listeners
, DeviceListener
) device_listeners
152 = QTAILQ_HEAD_INITIALIZER(device_listeners
);
154 enum ListenerDirection
{ Forward
, Reverse
};
156 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
158 DeviceListener *_listener; \
160 switch (_direction) { \
162 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
163 if (_listener->_callback) { \
164 _listener->_callback(_listener, ##_args); \
169 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
170 device_listeners, link) { \
171 if (_listener->_callback) { \
172 _listener->_callback(_listener, ##_args); \
181 static int device_listener_add(DeviceState
*dev
, void *opaque
)
183 DEVICE_LISTENER_CALL(realize
, Forward
, dev
);
188 void device_listener_register(DeviceListener
*listener
)
190 QTAILQ_INSERT_TAIL(&device_listeners
, listener
, link
);
192 qbus_walk_children(sysbus_get_default(), NULL
, NULL
, device_listener_add
,
196 void device_listener_unregister(DeviceListener
*listener
)
198 QTAILQ_REMOVE(&device_listeners
, listener
, link
);
201 static void device_realize(DeviceState
*dev
, Error
**errp
)
203 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
206 int rc
= dc
->init(dev
);
208 error_setg(errp
, "Device initialization failed.");
214 static void device_unrealize(DeviceState
*dev
, Error
**errp
)
216 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
219 int rc
= dc
->exit(dev
);
221 error_setg(errp
, "Device exit failed.");
227 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
228 int required_for_version
)
230 assert(!dev
->realized
);
231 dev
->instance_id_alias
= alias_id
;
232 dev
->alias_required_for_version
= required_for_version
;
235 HotplugHandler
*qdev_get_hotplug_handler(DeviceState
*dev
)
237 HotplugHandler
*hotplug_ctrl
= NULL
;
239 if (dev
->parent_bus
&& dev
->parent_bus
->hotplug_handler
) {
240 hotplug_ctrl
= dev
->parent_bus
->hotplug_handler
;
241 } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE
)) {
242 MachineState
*machine
= MACHINE(qdev_get_machine());
243 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
245 if (mc
->get_hotplug_handler
) {
246 hotplug_ctrl
= mc
->get_hotplug_handler(machine
, dev
);
252 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
254 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
255 HotplugHandler
*hotplug_ctrl
;
256 HotplugHandlerClass
*hdc
;
258 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
259 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
263 if (!dc
->hotpluggable
) {
264 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
,
265 object_get_typename(OBJECT(dev
)));
269 qdev_hot_removed
= true;
271 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
272 /* hotpluggable device MUST have HotplugHandler, if it doesn't
273 * then something is very wrong with it */
274 g_assert(hotplug_ctrl
);
276 /* If device supports async unplug just request it to be done,
277 * otherwise just remove it synchronously */
278 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
279 if (hdc
->unplug_request
) {
280 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, errp
);
282 hotplug_handler_unplug(hotplug_ctrl
, dev
, errp
);
286 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
293 static int qbus_reset_one(BusState
*bus
, void *opaque
)
295 BusClass
*bc
= BUS_GET_CLASS(bus
);
302 void qdev_reset_all(DeviceState
*dev
)
304 qdev_walk_children(dev
, NULL
, NULL
, qdev_reset_one
, qbus_reset_one
, NULL
);
307 void qdev_reset_all_fn(void *opaque
)
309 qdev_reset_all(DEVICE(opaque
));
312 void qbus_reset_all(BusState
*bus
)
314 qbus_walk_children(bus
, NULL
, NULL
, qdev_reset_one
, qbus_reset_one
, NULL
);
317 void qbus_reset_all_fn(void *opaque
)
319 BusState
*bus
= opaque
;
323 /* can be used as ->unplug() callback for the simple cases */
324 void qdev_simple_device_unplug_cb(HotplugHandler
*hotplug_dev
,
325 DeviceState
*dev
, Error
**errp
)
328 object_unparent(OBJECT(dev
));
333 * Device properties should be set before calling this function. IRQs
334 * and MMIO regions should be connected/mapped after calling this
336 * On failure, report an error with error_report() and terminate the
337 * program. This is okay during machine creation. Don't use for
338 * hotplug, because there callers need to recover from failure.
339 * Exception: if you know the device's init() callback can't fail,
340 * then qdev_init_nofail() can't fail either, and is therefore usable
341 * even then. But relying on the device implementation that way is
342 * somewhat unclean, and best avoided.
344 void qdev_init_nofail(DeviceState
*dev
)
348 assert(!dev
->realized
);
350 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
352 error_reportf_err(err
, "Initialization of device %s failed: ",
353 object_get_typename(OBJECT(dev
)));
358 void qdev_machine_creation_done(void)
361 * ok, initial machine setup is done, starting from now we can
362 * only create hotpluggable devices
367 bool qdev_machine_modified(void)
369 return qdev_hot_added
|| qdev_hot_removed
;
372 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
374 return dev
->parent_bus
;
377 static NamedGPIOList
*qdev_get_named_gpio_list(DeviceState
*dev
,
382 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
383 /* NULL is a valid and matchable name, otherwise do a normal
386 if ((!ngl
->name
&& !name
) ||
387 (name
&& ngl
->name
&& strcmp(name
, ngl
->name
) == 0)) {
392 ngl
= g_malloc0(sizeof(*ngl
));
393 ngl
->name
= g_strdup(name
);
394 QLIST_INSERT_HEAD(&dev
->gpios
, ngl
, node
);
398 void qdev_init_gpio_in_named(DeviceState
*dev
, qemu_irq_handler handler
,
399 const char *name
, int n
)
402 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
404 assert(gpio_list
->num_out
== 0 || !name
);
405 gpio_list
->in
= qemu_extend_irqs(gpio_list
->in
, gpio_list
->num_in
, handler
,
409 name
= "unnamed-gpio-in";
411 for (i
= gpio_list
->num_in
; i
< gpio_list
->num_in
+ n
; i
++) {
412 gchar
*propname
= g_strdup_printf("%s[%u]", name
, i
);
414 object_property_add_child(OBJECT(dev
), propname
,
415 OBJECT(gpio_list
->in
[i
]), &error_abort
);
419 gpio_list
->num_in
+= n
;
422 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
424 qdev_init_gpio_in_named(dev
, handler
, NULL
, n
);
427 void qdev_init_gpio_out_named(DeviceState
*dev
, qemu_irq
*pins
,
428 const char *name
, int n
)
431 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
433 assert(gpio_list
->num_in
== 0 || !name
);
436 name
= "unnamed-gpio-out";
438 memset(pins
, 0, sizeof(*pins
) * n
);
439 for (i
= 0; i
< n
; ++i
) {
440 gchar
*propname
= g_strdup_printf("%s[%u]", name
,
441 gpio_list
->num_out
+ i
);
443 object_property_add_link(OBJECT(dev
), propname
, TYPE_IRQ
,
445 object_property_allow_set_link
,
446 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
450 gpio_list
->num_out
+= n
;
453 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
455 qdev_init_gpio_out_named(dev
, pins
, NULL
, n
);
458 qemu_irq
qdev_get_gpio_in_named(DeviceState
*dev
, const char *name
, int n
)
460 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
462 assert(n
>= 0 && n
< gpio_list
->num_in
);
463 return gpio_list
->in
[n
];
466 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
468 return qdev_get_gpio_in_named(dev
, NULL
, n
);
471 void qdev_connect_gpio_out_named(DeviceState
*dev
, const char *name
, int n
,
474 char *propname
= g_strdup_printf("%s[%d]",
475 name
? name
: "unnamed-gpio-out", n
);
477 /* We need a name for object_property_set_link to work. If the
478 * object has a parent, object_property_add_child will come back
479 * with an error without doing anything. If it has none, it will
480 * never fail. So we can just call it with a NULL Error pointer.
482 object_property_add_child(container_get(qdev_get_machine(),
484 "non-qdev-gpio[*]", OBJECT(pin
), NULL
);
486 object_property_set_link(OBJECT(dev
), OBJECT(pin
), propname
, &error_abort
);
490 qemu_irq
qdev_get_gpio_out_connector(DeviceState
*dev
, const char *name
, int n
)
492 char *propname
= g_strdup_printf("%s[%d]",
493 name
? name
: "unnamed-gpio-out", n
);
495 qemu_irq ret
= (qemu_irq
)object_property_get_link(OBJECT(dev
), propname
,
501 /* disconnect a GPIO output, returning the disconnected input (if any) */
503 static qemu_irq
qdev_disconnect_gpio_out_named(DeviceState
*dev
,
504 const char *name
, int n
)
506 char *propname
= g_strdup_printf("%s[%d]",
507 name
? name
: "unnamed-gpio-out", n
);
509 qemu_irq ret
= (qemu_irq
)object_property_get_link(OBJECT(dev
), propname
,
512 object_property_set_link(OBJECT(dev
), NULL
, propname
, NULL
);
518 qemu_irq
qdev_intercept_gpio_out(DeviceState
*dev
, qemu_irq icpt
,
519 const char *name
, int n
)
521 qemu_irq disconnected
= qdev_disconnect_gpio_out_named(dev
, name
, n
);
522 qdev_connect_gpio_out_named(dev
, name
, n
, icpt
);
526 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
528 qdev_connect_gpio_out_named(dev
, NULL
, n
, pin
);
531 void qdev_pass_gpios(DeviceState
*dev
, DeviceState
*container
,
535 NamedGPIOList
*ngl
= qdev_get_named_gpio_list(dev
, name
);
537 for (i
= 0; i
< ngl
->num_in
; i
++) {
538 const char *nm
= ngl
->name
? ngl
->name
: "unnamed-gpio-in";
539 char *propname
= g_strdup_printf("%s[%d]", nm
, i
);
541 object_property_add_alias(OBJECT(container
), propname
,
542 OBJECT(dev
), propname
,
546 for (i
= 0; i
< ngl
->num_out
; i
++) {
547 const char *nm
= ngl
->name
? ngl
->name
: "unnamed-gpio-out";
548 char *propname
= g_strdup_printf("%s[%d]", nm
, i
);
550 object_property_add_alias(OBJECT(container
), propname
,
551 OBJECT(dev
), propname
,
555 QLIST_REMOVE(ngl
, node
);
556 QLIST_INSERT_HEAD(&container
->gpios
, ngl
, node
);
559 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
562 Object
*child
= object_resolve_path_component(OBJECT(dev
), name
);
564 bus
= (BusState
*)object_dynamic_cast(child
, TYPE_BUS
);
569 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
570 if (strcmp(name
, bus
->name
) == 0) {
577 int qdev_walk_children(DeviceState
*dev
,
578 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
579 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
586 err
= pre_devfn(dev
, opaque
);
592 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
593 err
= qbus_walk_children(bus
, pre_devfn
, pre_busfn
,
594 post_devfn
, post_busfn
, opaque
);
601 err
= post_devfn(dev
, opaque
);
610 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
616 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
617 DeviceState
*dev
= kid
->child
;
619 if (dev
->id
&& strcmp(dev
->id
, id
) == 0) {
623 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
624 ret
= qdev_find_recursive(child
, id
);
633 static char *bus_get_fw_dev_path(BusState
*bus
, DeviceState
*dev
)
635 BusClass
*bc
= BUS_GET_CLASS(bus
);
637 if (bc
->get_fw_dev_path
) {
638 return bc
->get_fw_dev_path(dev
);
644 static char *qdev_get_fw_dev_path_from_handler(BusState
*bus
, DeviceState
*dev
)
646 Object
*obj
= OBJECT(dev
);
649 while (!d
&& obj
->parent
) {
651 d
= fw_path_provider_try_get_dev_path(obj
, bus
, dev
);
656 char *qdev_get_own_fw_dev_path_from_handler(BusState
*bus
, DeviceState
*dev
)
658 Object
*obj
= OBJECT(dev
);
660 return fw_path_provider_try_get_dev_path(obj
, bus
, dev
);
663 static int qdev_get_fw_dev_path_helper(DeviceState
*dev
, char *p
, int size
)
667 if (dev
&& dev
->parent_bus
) {
669 l
= qdev_get_fw_dev_path_helper(dev
->parent_bus
->parent
, p
, size
);
670 d
= qdev_get_fw_dev_path_from_handler(dev
->parent_bus
, dev
);
672 d
= bus_get_fw_dev_path(dev
->parent_bus
, dev
);
675 l
+= snprintf(p
+ l
, size
- l
, "%s", d
);
681 l
+= snprintf(p
+ l
, size
- l
, "/");
686 char* qdev_get_fw_dev_path(DeviceState
*dev
)
691 l
= qdev_get_fw_dev_path_helper(dev
, path
, 128);
695 return g_strdup(path
);
698 char *qdev_get_dev_path(DeviceState
*dev
)
702 if (!dev
|| !dev
->parent_bus
) {
706 bc
= BUS_GET_CLASS(dev
->parent_bus
);
707 if (bc
->get_dev_path
) {
708 return bc
->get_dev_path(dev
);
715 * Legacy property handling
718 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
,
719 const char *name
, void *opaque
,
722 DeviceState
*dev
= DEVICE(obj
);
723 Property
*prop
= opaque
;
728 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
729 visit_type_str(v
, name
, &ptr
, errp
);
733 * qdev_property_add_legacy:
734 * @dev: Device to add the property to.
735 * @prop: The qdev property definition.
736 * @errp: location to store error information.
738 * Add a legacy QOM property to @dev for qdev property @prop.
739 * On error, store error in @errp.
741 * Legacy properties are string versions of QOM properties. The format of
742 * the string depends on the property type. Legacy properties are only
743 * needed for "info qtree".
745 * Do not use this is new code! QOM Properties added through this interface
746 * will be given names in the "legacy" namespace.
748 static void qdev_property_add_legacy(DeviceState
*dev
, Property
*prop
,
753 /* Register pointer properties as legacy properties */
754 if (!prop
->info
->print
&& prop
->info
->get
) {
758 name
= g_strdup_printf("legacy-%s", prop
->name
);
759 object_property_add(OBJECT(dev
), name
, "str",
760 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
769 * qdev_property_add_static:
770 * @dev: Device to add the property to.
771 * @prop: The qdev property definition.
772 * @errp: location to store error information.
774 * Add a static QOM property to @dev for qdev property @prop.
775 * On error, store error in @errp. Static properties access data in a struct.
776 * The type of the QOM property is derived from prop->info.
778 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
,
781 Error
*local_err
= NULL
;
782 Object
*obj
= OBJECT(dev
);
785 * TODO qdev_prop_ptr does not have getters or setters. It must
786 * go now that it can be replaced with links. The test should be
787 * removed along with it: all static properties are read/write.
789 if (!prop
->info
->get
&& !prop
->info
->set
) {
793 object_property_add(obj
, prop
->name
, prop
->info
->name
,
794 prop
->info
->get
, prop
->info
->set
,
799 error_propagate(errp
, local_err
);
803 object_property_set_description(obj
, prop
->name
,
804 prop
->info
->description
,
807 if (prop
->qtype
== QTYPE_NONE
) {
811 if (prop
->qtype
== QTYPE_QBOOL
) {
812 object_property_set_bool(obj
, prop
->defval
, prop
->name
, &error_abort
);
813 } else if (prop
->info
->enum_table
) {
814 object_property_set_str(obj
, prop
->info
->enum_table
[prop
->defval
],
815 prop
->name
, &error_abort
);
816 } else if (prop
->qtype
== QTYPE_QINT
) {
817 object_property_set_int(obj
, prop
->defval
, prop
->name
, &error_abort
);
821 /* @qdev_alias_all_properties - Add alias properties to the source object for
822 * all qdev properties on the target DeviceState.
824 void qdev_alias_all_properties(DeviceState
*target
, Object
*source
)
829 class = object_get_class(OBJECT(target
));
831 DeviceClass
*dc
= DEVICE_CLASS(class);
833 for (prop
= dc
->props
; prop
&& prop
->name
; prop
++) {
834 object_property_add_alias(source
, prop
->name
,
835 OBJECT(target
), prop
->name
,
838 class = object_class_get_parent(class);
839 } while (class != object_class_by_name(TYPE_DEVICE
));
842 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
844 GSList
**list
= opaque
;
845 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(OBJECT(obj
),
852 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
853 *list
= g_slist_append(*list
, dev
);
859 GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
863 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
868 static bool device_get_realized(Object
*obj
, Error
**errp
)
870 DeviceState
*dev
= DEVICE(obj
);
871 return dev
->realized
;
874 static void device_set_realized(Object
*obj
, bool value
, Error
**errp
)
876 DeviceState
*dev
= DEVICE(obj
);
877 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
878 HotplugHandler
*hotplug_ctrl
;
880 Error
*local_err
= NULL
;
882 if (dev
->hotplugged
&& !dc
->hotpluggable
) {
883 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
, object_get_typename(obj
));
887 if (value
&& !dev
->realized
) {
889 static int unattached_count
;
890 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
892 object_property_add_child(container_get(qdev_get_machine(),
894 name
, obj
, &error_abort
);
899 dc
->realize(dev
, &local_err
);
902 if (local_err
!= NULL
) {
906 DEVICE_LISTENER_CALL(realize
, Forward
, dev
);
908 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
910 hotplug_handler_plug(hotplug_ctrl
, dev
, &local_err
);
913 if (local_err
!= NULL
) {
914 goto post_realize_fail
;
917 if (qdev_get_vmsd(dev
)) {
918 vmstate_register_with_alias_id(dev
, -1, qdev_get_vmsd(dev
), dev
,
919 dev
->instance_id_alias
,
920 dev
->alias_required_for_version
);
923 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
924 object_property_set_bool(OBJECT(bus
), true, "realized",
926 if (local_err
!= NULL
) {
927 goto child_realize_fail
;
930 if (dev
->hotplugged
) {
933 dev
->pending_deleted_event
= false;
934 } else if (!value
&& dev
->realized
) {
935 Error
**local_errp
= NULL
;
936 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
937 local_errp
= local_err
? NULL
: &local_err
;
938 object_property_set_bool(OBJECT(bus
), false, "realized",
941 if (qdev_get_vmsd(dev
)) {
942 vmstate_unregister(dev
, qdev_get_vmsd(dev
), dev
);
945 local_errp
= local_err
? NULL
: &local_err
;
946 dc
->unrealize(dev
, local_errp
);
948 dev
->pending_deleted_event
= true;
949 DEVICE_LISTENER_CALL(unrealize
, Reverse
, dev
);
952 if (local_err
!= NULL
) {
956 dev
->realized
= value
;
960 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
961 object_property_set_bool(OBJECT(bus
), false, "realized",
965 if (qdev_get_vmsd(dev
)) {
966 vmstate_unregister(dev
, qdev_get_vmsd(dev
), dev
);
971 dc
->unrealize(dev
, NULL
);
975 error_propagate(errp
, local_err
);
978 static bool device_get_hotpluggable(Object
*obj
, Error
**errp
)
980 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
981 DeviceState
*dev
= DEVICE(obj
);
983 return dc
->hotpluggable
&& (dev
->parent_bus
== NULL
||
984 qbus_is_hotpluggable(dev
->parent_bus
));
987 static bool device_get_hotplugged(Object
*obj
, Error
**err
)
989 DeviceState
*dev
= DEVICE(obj
);
991 return dev
->hotplugged
;
994 static void device_set_hotplugged(Object
*obj
, bool value
, Error
**err
)
996 DeviceState
*dev
= DEVICE(obj
);
998 dev
->hotplugged
= value
;
1001 static void device_initfn(Object
*obj
)
1003 DeviceState
*dev
= DEVICE(obj
);
1008 dev
->hotplugged
= 1;
1009 qdev_hot_added
= true;
1012 dev
->instance_id_alias
= -1;
1013 dev
->realized
= false;
1015 object_property_add_bool(obj
, "realized",
1016 device_get_realized
, device_set_realized
, NULL
);
1017 object_property_add_bool(obj
, "hotpluggable",
1018 device_get_hotpluggable
, NULL
, NULL
);
1019 object_property_add_bool(obj
, "hotplugged",
1020 device_get_hotplugged
, device_set_hotplugged
,
1023 class = object_get_class(OBJECT(dev
));
1025 for (prop
= DEVICE_CLASS(class)->props
; prop
&& prop
->name
; prop
++) {
1026 qdev_property_add_legacy(dev
, prop
, &error_abort
);
1027 qdev_property_add_static(dev
, prop
, &error_abort
);
1029 class = object_class_get_parent(class);
1030 } while (class != object_class_by_name(TYPE_DEVICE
));
1032 object_property_add_link(OBJECT(dev
), "parent_bus", TYPE_BUS
,
1033 (Object
**)&dev
->parent_bus
, NULL
, 0,
1035 QLIST_INIT(&dev
->gpios
);
1038 static void device_post_init(Object
*obj
)
1040 qdev_prop_set_globals(DEVICE(obj
));
1043 /* Unlink device from bus and free the structure. */
1044 static void device_finalize(Object
*obj
)
1046 NamedGPIOList
*ngl
, *next
;
1048 DeviceState
*dev
= DEVICE(obj
);
1050 QLIST_FOREACH_SAFE(ngl
, &dev
->gpios
, node
, next
) {
1051 QLIST_REMOVE(ngl
, node
);
1052 qemu_free_irqs(ngl
->in
, ngl
->num_in
);
1055 /* ngl->out irqs are owned by the other end and should not be freed
1061 static void device_class_base_init(ObjectClass
*class, void *data
)
1063 DeviceClass
*klass
= DEVICE_CLASS(class);
1065 /* We explicitly look up properties in the superclasses,
1066 * so do not propagate them to the subclasses.
1068 klass
->props
= NULL
;
1071 static void device_unparent(Object
*obj
)
1073 DeviceState
*dev
= DEVICE(obj
);
1076 if (dev
->realized
) {
1077 object_property_set_bool(obj
, false, "realized", NULL
);
1079 while (dev
->num_child_bus
) {
1080 bus
= QLIST_FIRST(&dev
->child_bus
);
1081 object_unparent(OBJECT(bus
));
1083 if (dev
->parent_bus
) {
1084 bus_remove_child(dev
->parent_bus
, dev
);
1085 object_unref(OBJECT(dev
->parent_bus
));
1086 dev
->parent_bus
= NULL
;
1089 /* Only send event if the device had been completely realized */
1090 if (dev
->pending_deleted_event
) {
1091 gchar
*path
= object_get_canonical_path(OBJECT(dev
));
1093 qapi_event_send_device_deleted(!!dev
->id
, dev
->id
, path
, &error_abort
);
1097 qemu_opts_del(dev
->opts
);
1101 static void device_class_init(ObjectClass
*class, void *data
)
1103 DeviceClass
*dc
= DEVICE_CLASS(class);
1105 class->unparent
= device_unparent
;
1106 dc
->realize
= device_realize
;
1107 dc
->unrealize
= device_unrealize
;
1109 /* by default all devices were considered as hotpluggable,
1110 * so with intent to check it in generic qdev_unplug() /
1111 * device_set_realized() functions make every device
1112 * hotpluggable. Devices that shouldn't be hotpluggable,
1113 * should override it in their class_init()
1115 dc
->hotpluggable
= true;
1118 void device_reset(DeviceState
*dev
)
1120 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
1127 Object
*qdev_get_machine(void)
1132 dev
= container_get(object_get_root(), "/machine");
1138 static const TypeInfo device_type_info
= {
1139 .name
= TYPE_DEVICE
,
1140 .parent
= TYPE_OBJECT
,
1141 .instance_size
= sizeof(DeviceState
),
1142 .instance_init
= device_initfn
,
1143 .instance_post_init
= device_post_init
,
1144 .instance_finalize
= device_finalize
,
1145 .class_base_init
= device_class_base_init
,
1146 .class_init
= device_class_init
,
1148 .class_size
= sizeof(DeviceClass
),
1151 static void qdev_register_types(void)
1153 type_register_static(&device_type_info
);
1156 type_init(qdev_register_types
)