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"
29 #include "qapi/error.h"
30 #include "qapi/qapi-events-qdev.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/visitor.h"
33 #include "qemu/error-report.h"
34 #include "qemu/option.h"
35 #include "hw/hotplug.h"
37 #include "hw/qdev-properties.h"
38 #include "hw/boards.h"
39 #include "hw/sysbus.h"
40 #include "hw/qdev-clock.h"
41 #include "migration/vmstate.h"
44 bool qdev_hotplug
= false;
45 static bool qdev_hot_added
= false;
46 bool qdev_hot_removed
= false;
48 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
)
50 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
54 static void bus_remove_child(BusState
*bus
, DeviceState
*child
)
58 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
59 if (kid
->child
== child
) {
62 snprintf(name
, sizeof(name
), "child[%d]", kid
->index
);
63 QTAILQ_REMOVE(&bus
->children
, kid
, sibling
);
67 /* This gives back ownership of kid->child back to us. */
68 object_property_del(OBJECT(bus
), name
);
69 object_unref(OBJECT(kid
->child
));
76 static void bus_add_child(BusState
*bus
, DeviceState
*child
)
79 BusChild
*kid
= g_malloc0(sizeof(*kid
));
82 kid
->index
= bus
->max_index
++;
84 object_ref(OBJECT(kid
->child
));
86 QTAILQ_INSERT_HEAD(&bus
->children
, kid
, sibling
);
88 /* This transfers ownership of kid->child to the property. */
89 snprintf(name
, sizeof(name
), "child[%d]", kid
->index
);
90 object_property_add_link(OBJECT(bus
), name
,
91 object_get_typename(OBJECT(child
)),
92 (Object
**)&kid
->child
,
93 NULL
, /* read-only property */
97 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
)
99 BusState
*old_parent_bus
= dev
->parent_bus
;
100 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
102 assert(dc
->bus_type
&& object_dynamic_cast(OBJECT(bus
), dc
->bus_type
));
104 if (old_parent_bus
) {
105 trace_qdev_update_parent_bus(dev
, object_get_typename(OBJECT(dev
)),
106 old_parent_bus
, object_get_typename(OBJECT(old_parent_bus
)),
107 OBJECT(bus
), object_get_typename(OBJECT(bus
)));
109 * Keep a reference to the device while it's not plugged into
110 * any bus, to avoid it potentially evaporating when it is
111 * dereffed in bus_remove_child().
112 * Also keep the ref of the parent bus until the end, so that
113 * we can safely call resettable_change_parent() below.
115 object_ref(OBJECT(dev
));
116 bus_remove_child(dev
->parent_bus
, dev
);
118 dev
->parent_bus
= bus
;
119 object_ref(OBJECT(bus
));
120 bus_add_child(bus
, dev
);
122 resettable_change_parent(OBJECT(dev
), OBJECT(bus
),
123 OBJECT(old_parent_bus
));
125 if (old_parent_bus
) {
126 object_unref(OBJECT(old_parent_bus
));
127 object_unref(OBJECT(dev
));
131 DeviceState
*qdev_new(const char *name
)
133 if (!object_class_by_name(name
)) {
134 module_load_qom_one(name
);
136 return DEVICE(object_new(name
));
139 DeviceState
*qdev_try_new(const char *name
)
141 if (!module_object_class_by_name(name
)) {
144 return DEVICE(object_new(name
));
147 static QTAILQ_HEAD(, DeviceListener
) device_listeners
148 = QTAILQ_HEAD_INITIALIZER(device_listeners
);
150 enum ListenerDirection
{ Forward
, Reverse
};
152 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
154 DeviceListener *_listener; \
156 switch (_direction) { \
158 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
159 if (_listener->_callback) { \
160 _listener->_callback(_listener, ##_args); \
165 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
167 if (_listener->_callback) { \
168 _listener->_callback(_listener, ##_args); \
177 static int device_listener_add(DeviceState
*dev
, void *opaque
)
179 DEVICE_LISTENER_CALL(realize
, Forward
, dev
);
184 void device_listener_register(DeviceListener
*listener
)
186 QTAILQ_INSERT_TAIL(&device_listeners
, listener
, link
);
188 qbus_walk_children(sysbus_get_default(), NULL
, NULL
, device_listener_add
,
192 void device_listener_unregister(DeviceListener
*listener
)
194 QTAILQ_REMOVE(&device_listeners
, listener
, link
);
197 bool qdev_should_hide_device(QemuOpts
*opts
)
200 DeviceListener
*listener
;
202 QTAILQ_FOREACH(listener
, &device_listeners
, link
) {
203 if (listener
->should_be_hidden
) {
205 * should_be_hidden_will return
206 * 1 if device matches opts and it should be hidden
207 * 0 if device matches opts and should not be hidden
208 * -1 if device doesn't match ops
210 rc
= listener
->should_be_hidden(listener
, opts
);
221 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
222 int required_for_version
)
224 assert(!dev
->realized
);
225 dev
->instance_id_alias
= alias_id
;
226 dev
->alias_required_for_version
= required_for_version
;
229 HotplugHandler
*qdev_get_machine_hotplug_handler(DeviceState
*dev
)
231 MachineState
*machine
;
233 Object
*m_obj
= qdev_get_machine();
235 if (object_dynamic_cast(m_obj
, TYPE_MACHINE
)) {
236 machine
= MACHINE(m_obj
);
237 mc
= MACHINE_GET_CLASS(machine
);
238 if (mc
->get_hotplug_handler
) {
239 return mc
->get_hotplug_handler(machine
, dev
);
246 bool qdev_hotplug_allowed(DeviceState
*dev
, Error
**errp
)
248 MachineState
*machine
;
250 Object
*m_obj
= qdev_get_machine();
252 if (object_dynamic_cast(m_obj
, TYPE_MACHINE
)) {
253 machine
= MACHINE(m_obj
);
254 mc
= MACHINE_GET_CLASS(machine
);
255 if (mc
->hotplug_allowed
) {
256 return mc
->hotplug_allowed(machine
, dev
, errp
);
263 HotplugHandler
*qdev_get_bus_hotplug_handler(DeviceState
*dev
)
265 if (dev
->parent_bus
) {
266 return dev
->parent_bus
->hotplug_handler
;
271 HotplugHandler
*qdev_get_hotplug_handler(DeviceState
*dev
)
273 HotplugHandler
*hotplug_ctrl
= qdev_get_machine_hotplug_handler(dev
);
275 if (hotplug_ctrl
== NULL
&& dev
->parent_bus
) {
276 hotplug_ctrl
= qdev_get_bus_hotplug_handler(dev
);
281 static int qdev_prereset(DeviceState
*dev
, void *opaque
)
283 trace_qdev_reset_tree(dev
, object_get_typename(OBJECT(dev
)));
287 static int qbus_prereset(BusState
*bus
, void *opaque
)
289 trace_qbus_reset_tree(bus
, object_get_typename(OBJECT(bus
)));
293 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
295 device_legacy_reset(dev
);
300 static int qbus_reset_one(BusState
*bus
, void *opaque
)
302 BusClass
*bc
= BUS_GET_CLASS(bus
);
303 trace_qbus_reset(bus
, object_get_typename(OBJECT(bus
)));
310 void qdev_reset_all(DeviceState
*dev
)
312 trace_qdev_reset_all(dev
, object_get_typename(OBJECT(dev
)));
313 qdev_walk_children(dev
, qdev_prereset
, qbus_prereset
,
314 qdev_reset_one
, qbus_reset_one
, NULL
);
317 void qdev_reset_all_fn(void *opaque
)
319 qdev_reset_all(DEVICE(opaque
));
322 void qbus_reset_all(BusState
*bus
)
324 trace_qbus_reset_all(bus
, object_get_typename(OBJECT(bus
)));
325 qbus_walk_children(bus
, qdev_prereset
, qbus_prereset
,
326 qdev_reset_one
, qbus_reset_one
, NULL
);
329 void qbus_reset_all_fn(void *opaque
)
331 BusState
*bus
= opaque
;
335 void device_cold_reset(DeviceState
*dev
)
337 resettable_reset(OBJECT(dev
), RESET_TYPE_COLD
);
340 bool device_is_in_reset(DeviceState
*dev
)
342 return resettable_is_in_reset(OBJECT(dev
));
345 static ResettableState
*device_get_reset_state(Object
*obj
)
347 DeviceState
*dev
= DEVICE(obj
);
351 static void device_reset_child_foreach(Object
*obj
, ResettableChildCallback cb
,
352 void *opaque
, ResetType type
)
354 DeviceState
*dev
= DEVICE(obj
);
357 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
358 cb(OBJECT(bus
), opaque
, type
);
362 /* can be used as ->unplug() callback for the simple cases */
363 void qdev_simple_device_unplug_cb(HotplugHandler
*hotplug_dev
,
364 DeviceState
*dev
, Error
**errp
)
369 bool qdev_realize(DeviceState
*dev
, BusState
*bus
, Error
**errp
)
371 assert(!dev
->realized
&& !dev
->parent_bus
);
374 qdev_set_parent_bus(dev
, bus
);
376 assert(!DEVICE_GET_CLASS(dev
)->bus_type
);
379 return object_property_set_bool(OBJECT(dev
), "realized", true, errp
);
382 bool qdev_realize_and_unref(DeviceState
*dev
, BusState
*bus
, Error
**errp
)
386 ret
= qdev_realize(dev
, bus
, errp
);
387 object_unref(OBJECT(dev
));
391 void qdev_unrealize(DeviceState
*dev
)
393 object_property_set_bool(OBJECT(dev
), "realized", false, &error_abort
);
396 static int qdev_assert_realized_properly(Object
*obj
, void *opaque
)
398 DeviceState
*dev
= DEVICE(object_dynamic_cast(obj
, TYPE_DEVICE
));
402 dc
= DEVICE_GET_CLASS(dev
);
403 assert(dev
->realized
);
404 assert(dev
->parent_bus
|| !dc
->bus_type
);
409 void qdev_machine_creation_done(void)
412 * ok, initial machine setup is done, starting from now we can
413 * only create hotpluggable devices
417 object_child_foreach_recursive(object_get_root(),
418 qdev_assert_realized_properly
, NULL
);
421 bool qdev_machine_modified(void)
423 return qdev_hot_added
|| qdev_hot_removed
;
426 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
428 return dev
->parent_bus
;
431 static NamedGPIOList
*qdev_get_named_gpio_list(DeviceState
*dev
,
436 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
437 /* NULL is a valid and matchable name. */
438 if (g_strcmp0(name
, ngl
->name
) == 0) {
443 ngl
= g_malloc0(sizeof(*ngl
));
444 ngl
->name
= g_strdup(name
);
445 QLIST_INSERT_HEAD(&dev
->gpios
, ngl
, node
);
449 void qdev_init_gpio_in_named_with_opaque(DeviceState
*dev
,
450 qemu_irq_handler handler
,
452 const char *name
, int n
)
455 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
457 assert(gpio_list
->num_out
== 0 || !name
);
458 gpio_list
->in
= qemu_extend_irqs(gpio_list
->in
, gpio_list
->num_in
, handler
,
462 name
= "unnamed-gpio-in";
464 for (i
= gpio_list
->num_in
; i
< gpio_list
->num_in
+ n
; i
++) {
465 gchar
*propname
= g_strdup_printf("%s[%u]", name
, i
);
467 object_property_add_child(OBJECT(dev
), propname
,
468 OBJECT(gpio_list
->in
[i
]));
472 gpio_list
->num_in
+= n
;
475 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
477 qdev_init_gpio_in_named(dev
, handler
, NULL
, n
);
480 void qdev_init_gpio_out_named(DeviceState
*dev
, qemu_irq
*pins
,
481 const char *name
, int n
)
484 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
486 assert(gpio_list
->num_in
== 0 || !name
);
489 name
= "unnamed-gpio-out";
491 memset(pins
, 0, sizeof(*pins
) * n
);
492 for (i
= 0; i
< n
; ++i
) {
493 gchar
*propname
= g_strdup_printf("%s[%u]", name
,
494 gpio_list
->num_out
+ i
);
496 object_property_add_link(OBJECT(dev
), propname
, TYPE_IRQ
,
498 object_property_allow_set_link
,
499 OBJ_PROP_LINK_STRONG
);
502 gpio_list
->num_out
+= n
;
505 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
507 qdev_init_gpio_out_named(dev
, pins
, NULL
, n
);
510 qemu_irq
qdev_get_gpio_in_named(DeviceState
*dev
, const char *name
, int n
)
512 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
514 assert(n
>= 0 && n
< gpio_list
->num_in
);
515 return gpio_list
->in
[n
];
518 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
520 return qdev_get_gpio_in_named(dev
, NULL
, n
);
523 void qdev_connect_gpio_out_named(DeviceState
*dev
, const char *name
, int n
,
526 char *propname
= g_strdup_printf("%s[%d]",
527 name
? name
: "unnamed-gpio-out", n
);
528 if (pin
&& !OBJECT(pin
)->parent
) {
529 /* We need a name for object_property_set_link to work */
530 object_property_add_child(container_get(qdev_get_machine(),
532 "non-qdev-gpio[*]", OBJECT(pin
));
534 object_property_set_link(OBJECT(dev
), propname
, OBJECT(pin
), &error_abort
);
538 qemu_irq
qdev_get_gpio_out_connector(DeviceState
*dev
, const char *name
, int n
)
540 g_autofree
char *propname
= g_strdup_printf("%s[%d]",
541 name
? name
: "unnamed-gpio-out", n
);
543 qemu_irq ret
= (qemu_irq
)object_property_get_link(OBJECT(dev
), propname
,
549 /* disconnect a GPIO output, returning the disconnected input (if any) */
551 static qemu_irq
qdev_disconnect_gpio_out_named(DeviceState
*dev
,
552 const char *name
, int n
)
554 char *propname
= g_strdup_printf("%s[%d]",
555 name
? name
: "unnamed-gpio-out", n
);
557 qemu_irq ret
= (qemu_irq
)object_property_get_link(OBJECT(dev
), propname
,
560 object_property_set_link(OBJECT(dev
), propname
, NULL
, NULL
);
566 qemu_irq
qdev_intercept_gpio_out(DeviceState
*dev
, qemu_irq icpt
,
567 const char *name
, int n
)
569 qemu_irq disconnected
= qdev_disconnect_gpio_out_named(dev
, name
, n
);
570 qdev_connect_gpio_out_named(dev
, name
, n
, icpt
);
574 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
576 qdev_connect_gpio_out_named(dev
, NULL
, n
, pin
);
579 void qdev_pass_gpios(DeviceState
*dev
, DeviceState
*container
,
583 NamedGPIOList
*ngl
= qdev_get_named_gpio_list(dev
, name
);
585 for (i
= 0; i
< ngl
->num_in
; i
++) {
586 const char *nm
= ngl
->name
? ngl
->name
: "unnamed-gpio-in";
587 char *propname
= g_strdup_printf("%s[%d]", nm
, i
);
589 object_property_add_alias(OBJECT(container
), propname
,
590 OBJECT(dev
), propname
);
593 for (i
= 0; i
< ngl
->num_out
; i
++) {
594 const char *nm
= ngl
->name
? ngl
->name
: "unnamed-gpio-out";
595 char *propname
= g_strdup_printf("%s[%d]", nm
, i
);
597 object_property_add_alias(OBJECT(container
), propname
,
598 OBJECT(dev
), propname
);
601 QLIST_REMOVE(ngl
, node
);
602 QLIST_INSERT_HEAD(&container
->gpios
, ngl
, node
);
605 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
608 Object
*child
= object_resolve_path_component(OBJECT(dev
), name
);
610 bus
= (BusState
*)object_dynamic_cast(child
, TYPE_BUS
);
615 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
616 if (strcmp(name
, bus
->name
) == 0) {
623 int qdev_walk_children(DeviceState
*dev
,
624 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
625 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
632 err
= pre_devfn(dev
, opaque
);
638 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
639 err
= qbus_walk_children(bus
, pre_devfn
, pre_busfn
,
640 post_devfn
, post_busfn
, opaque
);
647 err
= post_devfn(dev
, opaque
);
656 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
662 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
663 DeviceState
*dev
= kid
->child
;
665 if (dev
->id
&& strcmp(dev
->id
, id
) == 0) {
669 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
670 ret
= qdev_find_recursive(child
, id
);
679 char *qdev_get_dev_path(DeviceState
*dev
)
683 if (!dev
|| !dev
->parent_bus
) {
687 bc
= BUS_GET_CLASS(dev
->parent_bus
);
688 if (bc
->get_dev_path
) {
689 return bc
->get_dev_path(dev
);
696 * Legacy property handling
699 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
,
700 const char *name
, void *opaque
,
703 DeviceState
*dev
= DEVICE(obj
);
704 Property
*prop
= opaque
;
709 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
710 visit_type_str(v
, name
, &ptr
, errp
);
714 * qdev_class_add_legacy_property:
715 * @dev: Device to add the property to.
716 * @prop: The qdev property definition.
718 * Add a legacy QOM property to @dev for qdev property @prop.
720 * Legacy properties are string versions of QOM properties. The format of
721 * the string depends on the property type. Legacy properties are only
722 * needed for "info qtree".
724 * Do not use this in new code! QOM Properties added through this interface
725 * will be given names in the "legacy" namespace.
727 static void qdev_class_add_legacy_property(DeviceClass
*dc
, Property
*prop
)
729 g_autofree
char *name
= NULL
;
731 /* Register pointer properties as legacy properties */
732 if (!prop
->info
->print
&& prop
->info
->get
) {
736 name
= g_strdup_printf("legacy-%s", prop
->name
);
737 object_class_property_add(OBJECT_CLASS(dc
), name
, "str",
738 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
742 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
)
744 Object
*obj
= OBJECT(dev
);
747 assert(!prop
->info
->create
);
749 op
= object_property_add(obj
, prop
->name
, prop
->info
->name
,
750 prop
->info
->get
, prop
->info
->set
,
754 object_property_set_description(obj
, prop
->name
,
755 prop
->info
->description
);
757 if (prop
->set_default
) {
758 prop
->info
->set_default_value(op
, prop
);
765 static void qdev_class_add_property(DeviceClass
*klass
, Property
*prop
)
767 ObjectClass
*oc
= OBJECT_CLASS(klass
);
769 if (prop
->info
->create
) {
770 prop
->info
->create(oc
, prop
);
774 op
= object_class_property_add(oc
,
775 prop
->name
, prop
->info
->name
,
776 prop
->info
->get
, prop
->info
->set
,
779 if (prop
->set_default
) {
780 prop
->info
->set_default_value(op
, prop
);
783 object_class_property_set_description(oc
, prop
->name
,
784 prop
->info
->description
);
787 void qdev_alias_all_properties(DeviceState
*target
, Object
*source
)
792 class = object_get_class(OBJECT(target
));
794 DeviceClass
*dc
= DEVICE_CLASS(class);
796 for (prop
= dc
->props_
; prop
&& prop
->name
; prop
++) {
797 object_property_add_alias(source
, prop
->name
,
798 OBJECT(target
), prop
->name
);
800 class = object_class_get_parent(class);
801 } while (class != object_class_by_name(TYPE_DEVICE
));
804 static bool device_get_realized(Object
*obj
, Error
**errp
)
806 DeviceState
*dev
= DEVICE(obj
);
807 return dev
->realized
;
810 static bool check_only_migratable(Object
*obj
, Error
**errp
)
812 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
814 if (!vmstate_check_only_migratable(dc
->vmsd
)) {
815 error_setg(errp
, "Device %s is not migratable, but "
816 "--only-migratable was specified",
817 object_get_typename(obj
));
824 static void device_set_realized(Object
*obj
, bool value
, Error
**errp
)
826 DeviceState
*dev
= DEVICE(obj
);
827 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
828 HotplugHandler
*hotplug_ctrl
;
831 Error
*local_err
= NULL
;
832 bool unattached_parent
= false;
833 static int unattached_count
;
835 if (dev
->hotplugged
&& !dc
->hotpluggable
) {
836 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
, object_get_typename(obj
));
840 if (value
&& !dev
->realized
) {
841 if (!check_only_migratable(obj
, errp
)) {
846 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
848 object_property_add_child(container_get(qdev_get_machine(),
851 unattached_parent
= true;
855 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
857 hotplug_handler_pre_plug(hotplug_ctrl
, dev
, &local_err
);
858 if (local_err
!= NULL
) {
864 dc
->realize(dev
, &local_err
);
865 if (local_err
!= NULL
) {
870 DEVICE_LISTENER_CALL(realize
, Forward
, dev
);
873 * always free/re-initialize here since the value cannot be cleaned up
874 * in device_unrealize due to its usage later on in the unplug path
876 g_free(dev
->canonical_path
);
877 dev
->canonical_path
= object_get_canonical_path(OBJECT(dev
));
878 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
882 clock_setup_canonical_path(ncl
->clock
);
886 if (qdev_get_vmsd(dev
)) {
887 if (vmstate_register_with_alias_id(VMSTATE_IF(dev
),
888 VMSTATE_INSTANCE_ID_ANY
,
889 qdev_get_vmsd(dev
), dev
,
890 dev
->instance_id_alias
,
891 dev
->alias_required_for_version
,
893 goto post_realize_fail
;
898 * Clear the reset state, in case the object was previously unrealized
899 * with a dirty state.
901 resettable_state_clear(&dev
->reset
);
903 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
904 if (!qbus_realize(bus
, errp
)) {
905 goto child_realize_fail
;
908 if (dev
->hotplugged
) {
910 * Reset the device, as well as its subtree which, at this point,
911 * should be realized too.
913 resettable_assert_reset(OBJECT(dev
), RESET_TYPE_COLD
);
914 resettable_change_parent(OBJECT(dev
), OBJECT(dev
->parent_bus
),
916 resettable_release_reset(OBJECT(dev
), RESET_TYPE_COLD
);
918 dev
->pending_deleted_event
= false;
921 hotplug_handler_plug(hotplug_ctrl
, dev
, &local_err
);
922 if (local_err
!= NULL
) {
923 goto child_realize_fail
;
927 } else if (!value
&& dev
->realized
) {
928 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
931 if (qdev_get_vmsd(dev
)) {
932 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
937 dev
->pending_deleted_event
= true;
938 DEVICE_LISTENER_CALL(unrealize
, Reverse
, dev
);
941 assert(local_err
== NULL
);
942 dev
->realized
= value
;
946 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
950 if (qdev_get_vmsd(dev
)) {
951 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
955 g_free(dev
->canonical_path
);
956 dev
->canonical_path
= NULL
;
962 error_propagate(errp
, local_err
);
963 if (unattached_parent
) {
965 * Beware, this doesn't just revert
966 * object_property_add_child(), it also runs bus_remove()!
968 object_unparent(OBJECT(dev
));
973 static bool device_get_hotpluggable(Object
*obj
, Error
**errp
)
975 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
976 DeviceState
*dev
= DEVICE(obj
);
978 return dc
->hotpluggable
&& (dev
->parent_bus
== NULL
||
979 qbus_is_hotpluggable(dev
->parent_bus
));
982 static bool device_get_hotplugged(Object
*obj
, Error
**errp
)
984 DeviceState
*dev
= DEVICE(obj
);
986 return dev
->hotplugged
;
989 static void device_initfn(Object
*obj
)
991 DeviceState
*dev
= DEVICE(obj
);
995 qdev_hot_added
= true;
998 dev
->instance_id_alias
= -1;
999 dev
->realized
= false;
1000 dev
->allow_unplug_during_migration
= false;
1002 QLIST_INIT(&dev
->gpios
);
1003 QLIST_INIT(&dev
->clocks
);
1006 static void device_post_init(Object
*obj
)
1009 * Note: ordered so that the user's global properties take
1012 object_apply_compat_props(obj
);
1013 qdev_prop_set_globals(DEVICE(obj
));
1016 /* Unlink device from bus and free the structure. */
1017 static void device_finalize(Object
*obj
)
1019 NamedGPIOList
*ngl
, *next
;
1021 DeviceState
*dev
= DEVICE(obj
);
1023 QLIST_FOREACH_SAFE(ngl
, &dev
->gpios
, node
, next
) {
1024 QLIST_REMOVE(ngl
, node
);
1025 qemu_free_irqs(ngl
->in
, ngl
->num_in
);
1028 /* ngl->out irqs are owned by the other end and should not be freed
1033 qdev_finalize_clocklist(dev
);
1035 /* Only send event if the device had been completely realized */
1036 if (dev
->pending_deleted_event
) {
1037 g_assert(dev
->canonical_path
);
1039 qapi_event_send_device_deleted(!!dev
->id
, dev
->id
, dev
->canonical_path
);
1040 g_free(dev
->canonical_path
);
1041 dev
->canonical_path
= NULL
;
1044 qemu_opts_del(dev
->opts
);
1047 static void device_class_base_init(ObjectClass
*class, void *data
)
1049 DeviceClass
*klass
= DEVICE_CLASS(class);
1051 /* We explicitly look up properties in the superclasses,
1052 * so do not propagate them to the subclasses.
1054 klass
->props_
= NULL
;
1057 static void device_unparent(Object
*obj
)
1059 DeviceState
*dev
= DEVICE(obj
);
1062 if (dev
->realized
) {
1063 qdev_unrealize(dev
);
1065 while (dev
->num_child_bus
) {
1066 bus
= QLIST_FIRST(&dev
->child_bus
);
1067 object_unparent(OBJECT(bus
));
1069 if (dev
->parent_bus
) {
1070 bus_remove_child(dev
->parent_bus
, dev
);
1071 object_unref(OBJECT(dev
->parent_bus
));
1072 dev
->parent_bus
= NULL
;
1077 device_vmstate_if_get_id(VMStateIf
*obj
)
1079 DeviceState
*dev
= DEVICE(obj
);
1081 return qdev_get_dev_path(dev
);
1085 * device_phases_reset:
1086 * Transition reset method for devices to allow moving
1087 * smoothly from legacy reset method to multi-phases
1089 static void device_phases_reset(DeviceState
*dev
)
1091 ResettableClass
*rc
= RESETTABLE_GET_CLASS(dev
);
1093 if (rc
->phases
.enter
) {
1094 rc
->phases
.enter(OBJECT(dev
), RESET_TYPE_COLD
);
1096 if (rc
->phases
.hold
) {
1097 rc
->phases
.hold(OBJECT(dev
));
1099 if (rc
->phases
.exit
) {
1100 rc
->phases
.exit(OBJECT(dev
));
1104 static void device_transitional_reset(Object
*obj
)
1106 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
1109 * This will call either @device_phases_reset (for multi-phases transitioned
1110 * devices) or a device's specific method for not-yet transitioned devices.
1111 * In both case, it does not reset children.
1114 dc
->reset(DEVICE(obj
));
1119 * device_get_transitional_reset:
1120 * check if the device's class is ready for multi-phase
1122 static ResettableTrFunction
device_get_transitional_reset(Object
*obj
)
1124 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
1125 if (dc
->reset
!= device_phases_reset
) {
1127 * dc->reset has been overridden by a subclass,
1128 * the device is not ready for multi phase yet.
1130 return device_transitional_reset
;
1135 static void device_class_init(ObjectClass
*class, void *data
)
1137 DeviceClass
*dc
= DEVICE_CLASS(class);
1138 VMStateIfClass
*vc
= VMSTATE_IF_CLASS(class);
1139 ResettableClass
*rc
= RESETTABLE_CLASS(class);
1141 class->unparent
= device_unparent
;
1143 /* by default all devices were considered as hotpluggable,
1144 * so with intent to check it in generic qdev_unplug() /
1145 * device_set_realized() functions make every device
1146 * hotpluggable. Devices that shouldn't be hotpluggable,
1147 * should override it in their class_init()
1149 dc
->hotpluggable
= true;
1150 dc
->user_creatable
= true;
1151 vc
->get_id
= device_vmstate_if_get_id
;
1152 rc
->get_state
= device_get_reset_state
;
1153 rc
->child_foreach
= device_reset_child_foreach
;
1156 * @device_phases_reset is put as the default reset method below, allowing
1157 * to do the multi-phase transition from base classes to leaf classes. It
1158 * allows a legacy-reset Device class to extend a multi-phases-reset
1159 * Device class for the following reason:
1160 * + If a base class B has been moved to multi-phase, then it does not
1161 * override this default reset method and may have defined phase methods.
1162 * + A child class C (extending class B) which uses
1163 * device_class_set_parent_reset() (or similar means) to override the
1164 * reset method will still work as expected. @device_phases_reset function
1165 * will be registered as the parent reset method and effectively call
1166 * parent reset phases.
1168 dc
->reset
= device_phases_reset
;
1169 rc
->get_transitional_function
= device_get_transitional_reset
;
1171 object_class_property_add_bool(class, "realized",
1172 device_get_realized
, device_set_realized
);
1173 object_class_property_add_bool(class, "hotpluggable",
1174 device_get_hotpluggable
, NULL
);
1175 object_class_property_add_bool(class, "hotplugged",
1176 device_get_hotplugged
, NULL
);
1177 object_class_property_add_link(class, "parent_bus", TYPE_BUS
,
1178 offsetof(DeviceState
, parent_bus
), NULL
, 0);
1181 void device_class_set_props(DeviceClass
*dc
, Property
*props
)
1186 for (prop
= props
; prop
&& prop
->name
; prop
++) {
1187 qdev_class_add_legacy_property(dc
, prop
);
1188 qdev_class_add_property(dc
, prop
);
1192 void device_class_set_parent_reset(DeviceClass
*dc
,
1193 DeviceReset dev_reset
,
1194 DeviceReset
*parent_reset
)
1196 *parent_reset
= dc
->reset
;
1197 dc
->reset
= dev_reset
;
1200 void device_class_set_parent_realize(DeviceClass
*dc
,
1201 DeviceRealize dev_realize
,
1202 DeviceRealize
*parent_realize
)
1204 *parent_realize
= dc
->realize
;
1205 dc
->realize
= dev_realize
;
1208 void device_class_set_parent_unrealize(DeviceClass
*dc
,
1209 DeviceUnrealize dev_unrealize
,
1210 DeviceUnrealize
*parent_unrealize
)
1212 *parent_unrealize
= dc
->unrealize
;
1213 dc
->unrealize
= dev_unrealize
;
1216 void device_legacy_reset(DeviceState
*dev
)
1218 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
1220 trace_qdev_reset(dev
, object_get_typename(OBJECT(dev
)));
1226 Object
*qdev_get_machine(void)
1231 dev
= container_get(object_get_root(), "/machine");
1237 static const TypeInfo device_type_info
= {
1238 .name
= TYPE_DEVICE
,
1239 .parent
= TYPE_OBJECT
,
1240 .instance_size
= sizeof(DeviceState
),
1241 .instance_init
= device_initfn
,
1242 .instance_post_init
= device_post_init
,
1243 .instance_finalize
= device_finalize
,
1244 .class_base_init
= device_class_base_init
,
1245 .class_init
= device_class_init
,
1247 .class_size
= sizeof(DeviceClass
),
1248 .interfaces
= (InterfaceInfo
[]) {
1249 { TYPE_VMSTATE_IF
},
1250 { TYPE_RESETTABLE_INTERFACE
},
1255 static void qdev_register_types(void)
1257 type_register_static(&device_type_info
);
1260 type_init(qdev_register_types
)