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.1 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 static bool qdev_hot_added
= false;
45 bool qdev_hot_removed
= false;
47 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
)
49 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
53 static void bus_free_bus_child(BusChild
*kid
)
55 object_unref(OBJECT(kid
->child
));
59 static void bus_remove_child(BusState
*bus
, DeviceState
*child
)
63 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
64 if (kid
->child
== child
) {
67 snprintf(name
, sizeof(name
), "child[%d]", kid
->index
);
68 QTAILQ_REMOVE_RCU(&bus
->children
, kid
, sibling
);
72 /* This gives back ownership of kid->child back to us. */
73 object_property_del(OBJECT(bus
), name
);
75 /* free the bus kid, when it is safe to do so*/
76 call_rcu(kid
, bus_free_bus_child
, rcu
);
82 static void bus_add_child(BusState
*bus
, DeviceState
*child
)
85 BusChild
*kid
= g_malloc0(sizeof(*kid
));
88 kid
->index
= bus
->max_index
++;
90 object_ref(OBJECT(kid
->child
));
92 QTAILQ_INSERT_HEAD_RCU(&bus
->children
, kid
, sibling
);
94 /* This transfers ownership of kid->child to the property. */
95 snprintf(name
, sizeof(name
), "child[%d]", kid
->index
);
96 object_property_add_link(OBJECT(bus
), name
,
97 object_get_typename(OBJECT(child
)),
98 (Object
**)&kid
->child
,
99 NULL
, /* read-only property */
103 static bool bus_check_address(BusState
*bus
, DeviceState
*child
, Error
**errp
)
105 BusClass
*bc
= BUS_GET_CLASS(bus
);
106 return !bc
->check_address
|| bc
->check_address(bus
, child
, errp
);
109 bool qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
, Error
**errp
)
111 BusState
*old_parent_bus
= dev
->parent_bus
;
112 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
114 assert(dc
->bus_type
&& object_dynamic_cast(OBJECT(bus
), dc
->bus_type
));
116 if (!bus_check_address(bus
, dev
, errp
)) {
120 if (old_parent_bus
) {
121 trace_qdev_update_parent_bus(dev
, object_get_typename(OBJECT(dev
)),
122 old_parent_bus
, object_get_typename(OBJECT(old_parent_bus
)),
123 OBJECT(bus
), object_get_typename(OBJECT(bus
)));
125 * Keep a reference to the device while it's not plugged into
126 * any bus, to avoid it potentially evaporating when it is
127 * dereffed in bus_remove_child().
128 * Also keep the ref of the parent bus until the end, so that
129 * we can safely call resettable_change_parent() below.
131 object_ref(OBJECT(dev
));
132 bus_remove_child(dev
->parent_bus
, dev
);
134 dev
->parent_bus
= bus
;
135 object_ref(OBJECT(bus
));
136 bus_add_child(bus
, dev
);
138 resettable_change_parent(OBJECT(dev
), OBJECT(bus
),
139 OBJECT(old_parent_bus
));
141 if (old_parent_bus
) {
142 object_unref(OBJECT(old_parent_bus
));
143 object_unref(OBJECT(dev
));
148 DeviceState
*qdev_new(const char *name
)
150 if (!object_class_by_name(name
)) {
151 module_load_qom_one(name
);
153 return DEVICE(object_new(name
));
156 DeviceState
*qdev_try_new(const char *name
)
158 if (!module_object_class_by_name(name
)) {
161 return DEVICE(object_new(name
));
164 static QTAILQ_HEAD(, DeviceListener
) device_listeners
165 = QTAILQ_HEAD_INITIALIZER(device_listeners
);
167 enum ListenerDirection
{ Forward
, Reverse
};
169 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
171 DeviceListener *_listener; \
173 switch (_direction) { \
175 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
176 if (_listener->_callback) { \
177 _listener->_callback(_listener, ##_args); \
182 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
184 if (_listener->_callback) { \
185 _listener->_callback(_listener, ##_args); \
194 static int device_listener_add(DeviceState
*dev
, void *opaque
)
196 DEVICE_LISTENER_CALL(realize
, Forward
, dev
);
201 void device_listener_register(DeviceListener
*listener
)
203 QTAILQ_INSERT_TAIL(&device_listeners
, listener
, link
);
205 qbus_walk_children(sysbus_get_default(), NULL
, NULL
, device_listener_add
,
209 void device_listener_unregister(DeviceListener
*listener
)
211 QTAILQ_REMOVE(&device_listeners
, listener
, link
);
214 bool qdev_should_hide_device(QemuOpts
*opts
)
216 DeviceListener
*listener
;
218 QTAILQ_FOREACH(listener
, &device_listeners
, link
) {
219 if (listener
->hide_device
) {
220 if (listener
->hide_device(listener
, opts
)) {
229 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
230 int required_for_version
)
232 assert(!dev
->realized
);
233 dev
->instance_id_alias
= alias_id
;
234 dev
->alias_required_for_version
= required_for_version
;
237 HotplugHandler
*qdev_get_machine_hotplug_handler(DeviceState
*dev
)
239 MachineState
*machine
;
241 Object
*m_obj
= qdev_get_machine();
243 if (object_dynamic_cast(m_obj
, TYPE_MACHINE
)) {
244 machine
= MACHINE(m_obj
);
245 mc
= MACHINE_GET_CLASS(machine
);
246 if (mc
->get_hotplug_handler
) {
247 return mc
->get_hotplug_handler(machine
, dev
);
254 bool qdev_hotplug_allowed(DeviceState
*dev
, Error
**errp
)
256 MachineState
*machine
;
258 Object
*m_obj
= qdev_get_machine();
260 if (object_dynamic_cast(m_obj
, TYPE_MACHINE
)) {
261 machine
= MACHINE(m_obj
);
262 mc
= MACHINE_GET_CLASS(machine
);
263 if (mc
->hotplug_allowed
) {
264 return mc
->hotplug_allowed(machine
, dev
, errp
);
271 HotplugHandler
*qdev_get_bus_hotplug_handler(DeviceState
*dev
)
273 if (dev
->parent_bus
) {
274 return dev
->parent_bus
->hotplug_handler
;
279 HotplugHandler
*qdev_get_hotplug_handler(DeviceState
*dev
)
281 HotplugHandler
*hotplug_ctrl
= qdev_get_machine_hotplug_handler(dev
);
283 if (hotplug_ctrl
== NULL
&& dev
->parent_bus
) {
284 hotplug_ctrl
= qdev_get_bus_hotplug_handler(dev
);
289 static int qdev_prereset(DeviceState
*dev
, void *opaque
)
291 trace_qdev_reset_tree(dev
, object_get_typename(OBJECT(dev
)));
295 static int qbus_prereset(BusState
*bus
, void *opaque
)
297 trace_qbus_reset_tree(bus
, object_get_typename(OBJECT(bus
)));
301 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
303 device_legacy_reset(dev
);
308 static int qbus_reset_one(BusState
*bus
, void *opaque
)
310 BusClass
*bc
= BUS_GET_CLASS(bus
);
311 trace_qbus_reset(bus
, object_get_typename(OBJECT(bus
)));
318 void qdev_reset_all(DeviceState
*dev
)
320 trace_qdev_reset_all(dev
, object_get_typename(OBJECT(dev
)));
321 qdev_walk_children(dev
, qdev_prereset
, qbus_prereset
,
322 qdev_reset_one
, qbus_reset_one
, NULL
);
325 void qdev_reset_all_fn(void *opaque
)
327 qdev_reset_all(DEVICE(opaque
));
330 void qbus_reset_all(BusState
*bus
)
332 trace_qbus_reset_all(bus
, object_get_typename(OBJECT(bus
)));
333 qbus_walk_children(bus
, qdev_prereset
, qbus_prereset
,
334 qdev_reset_one
, qbus_reset_one
, NULL
);
337 void qbus_reset_all_fn(void *opaque
)
339 BusState
*bus
= opaque
;
343 void device_cold_reset(DeviceState
*dev
)
345 resettable_reset(OBJECT(dev
), RESET_TYPE_COLD
);
348 bool device_is_in_reset(DeviceState
*dev
)
350 return resettable_is_in_reset(OBJECT(dev
));
353 static ResettableState
*device_get_reset_state(Object
*obj
)
355 DeviceState
*dev
= DEVICE(obj
);
359 static void device_reset_child_foreach(Object
*obj
, ResettableChildCallback cb
,
360 void *opaque
, ResetType type
)
362 DeviceState
*dev
= DEVICE(obj
);
365 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
366 cb(OBJECT(bus
), opaque
, type
);
370 /* can be used as ->unplug() callback for the simple cases */
371 void qdev_simple_device_unplug_cb(HotplugHandler
*hotplug_dev
,
372 DeviceState
*dev
, Error
**errp
)
377 bool qdev_realize(DeviceState
*dev
, BusState
*bus
, Error
**errp
)
379 assert(!dev
->realized
&& !dev
->parent_bus
);
382 if (!qdev_set_parent_bus(dev
, bus
, errp
)) {
386 assert(!DEVICE_GET_CLASS(dev
)->bus_type
);
389 return object_property_set_bool(OBJECT(dev
), "realized", true, errp
);
392 bool qdev_realize_and_unref(DeviceState
*dev
, BusState
*bus
, Error
**errp
)
396 ret
= qdev_realize(dev
, bus
, errp
);
397 object_unref(OBJECT(dev
));
401 void qdev_unrealize(DeviceState
*dev
)
403 object_property_set_bool(OBJECT(dev
), "realized", false, &error_abort
);
406 static int qdev_assert_realized_properly_cb(Object
*obj
, void *opaque
)
408 DeviceState
*dev
= DEVICE(object_dynamic_cast(obj
, TYPE_DEVICE
));
412 dc
= DEVICE_GET_CLASS(dev
);
413 assert(dev
->realized
);
414 assert(dev
->parent_bus
|| !dc
->bus_type
);
419 void qdev_assert_realized_properly(void)
421 object_child_foreach_recursive(object_get_root(),
422 qdev_assert_realized_properly_cb
, NULL
);
425 bool qdev_machine_modified(void)
427 return qdev_hot_added
|| qdev_hot_removed
;
430 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
432 return dev
->parent_bus
;
435 static NamedGPIOList
*qdev_get_named_gpio_list(DeviceState
*dev
,
440 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
441 /* NULL is a valid and matchable name. */
442 if (g_strcmp0(name
, ngl
->name
) == 0) {
447 ngl
= g_malloc0(sizeof(*ngl
));
448 ngl
->name
= g_strdup(name
);
449 QLIST_INSERT_HEAD(&dev
->gpios
, ngl
, node
);
453 void qdev_init_gpio_in_named_with_opaque(DeviceState
*dev
,
454 qemu_irq_handler handler
,
456 const char *name
, int n
)
459 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
461 assert(gpio_list
->num_out
== 0 || !name
);
462 gpio_list
->in
= qemu_extend_irqs(gpio_list
->in
, gpio_list
->num_in
, handler
,
466 name
= "unnamed-gpio-in";
468 for (i
= gpio_list
->num_in
; i
< gpio_list
->num_in
+ n
; i
++) {
469 gchar
*propname
= g_strdup_printf("%s[%u]", name
, i
);
471 object_property_add_child(OBJECT(dev
), propname
,
472 OBJECT(gpio_list
->in
[i
]));
476 gpio_list
->num_in
+= n
;
479 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
481 qdev_init_gpio_in_named(dev
, handler
, NULL
, n
);
484 void qdev_init_gpio_out_named(DeviceState
*dev
, qemu_irq
*pins
,
485 const char *name
, int n
)
488 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
490 assert(gpio_list
->num_in
== 0 || !name
);
493 name
= "unnamed-gpio-out";
495 memset(pins
, 0, sizeof(*pins
) * n
);
496 for (i
= 0; i
< n
; ++i
) {
497 gchar
*propname
= g_strdup_printf("%s[%u]", name
,
498 gpio_list
->num_out
+ i
);
500 object_property_add_link(OBJECT(dev
), propname
, TYPE_IRQ
,
502 object_property_allow_set_link
,
503 OBJ_PROP_LINK_STRONG
);
506 gpio_list
->num_out
+= n
;
509 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
511 qdev_init_gpio_out_named(dev
, pins
, NULL
, n
);
514 qemu_irq
qdev_get_gpio_in_named(DeviceState
*dev
, const char *name
, int n
)
516 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
518 assert(n
>= 0 && n
< gpio_list
->num_in
);
519 return gpio_list
->in
[n
];
522 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
524 return qdev_get_gpio_in_named(dev
, NULL
, n
);
527 void qdev_connect_gpio_out_named(DeviceState
*dev
, const char *name
, int n
,
530 char *propname
= g_strdup_printf("%s[%d]",
531 name
? name
: "unnamed-gpio-out", n
);
532 if (pin
&& !OBJECT(pin
)->parent
) {
533 /* We need a name for object_property_set_link to work */
534 object_property_add_child(container_get(qdev_get_machine(),
536 "non-qdev-gpio[*]", OBJECT(pin
));
538 object_property_set_link(OBJECT(dev
), propname
, OBJECT(pin
), &error_abort
);
542 qemu_irq
qdev_get_gpio_out_connector(DeviceState
*dev
, const char *name
, int n
)
544 g_autofree
char *propname
= g_strdup_printf("%s[%d]",
545 name
? name
: "unnamed-gpio-out", n
);
547 qemu_irq ret
= (qemu_irq
)object_property_get_link(OBJECT(dev
), propname
,
553 /* disconnect a GPIO output, returning the disconnected input (if any) */
555 static qemu_irq
qdev_disconnect_gpio_out_named(DeviceState
*dev
,
556 const char *name
, int n
)
558 char *propname
= g_strdup_printf("%s[%d]",
559 name
? name
: "unnamed-gpio-out", n
);
561 qemu_irq ret
= (qemu_irq
)object_property_get_link(OBJECT(dev
), propname
,
564 object_property_set_link(OBJECT(dev
), propname
, NULL
, NULL
);
570 qemu_irq
qdev_intercept_gpio_out(DeviceState
*dev
, qemu_irq icpt
,
571 const char *name
, int n
)
573 qemu_irq disconnected
= qdev_disconnect_gpio_out_named(dev
, name
, n
);
574 qdev_connect_gpio_out_named(dev
, name
, n
, icpt
);
578 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
580 qdev_connect_gpio_out_named(dev
, NULL
, n
, pin
);
583 void qdev_pass_gpios(DeviceState
*dev
, DeviceState
*container
,
587 NamedGPIOList
*ngl
= qdev_get_named_gpio_list(dev
, name
);
589 for (i
= 0; i
< ngl
->num_in
; i
++) {
590 const char *nm
= ngl
->name
? ngl
->name
: "unnamed-gpio-in";
591 char *propname
= g_strdup_printf("%s[%d]", nm
, i
);
593 object_property_add_alias(OBJECT(container
), propname
,
594 OBJECT(dev
), propname
);
597 for (i
= 0; i
< ngl
->num_out
; i
++) {
598 const char *nm
= ngl
->name
? ngl
->name
: "unnamed-gpio-out";
599 char *propname
= g_strdup_printf("%s[%d]", nm
, i
);
601 object_property_add_alias(OBJECT(container
), propname
,
602 OBJECT(dev
), propname
);
605 QLIST_REMOVE(ngl
, node
);
606 QLIST_INSERT_HEAD(&container
->gpios
, ngl
, node
);
609 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
612 Object
*child
= object_resolve_path_component(OBJECT(dev
), name
);
614 bus
= (BusState
*)object_dynamic_cast(child
, TYPE_BUS
);
619 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
620 if (strcmp(name
, bus
->name
) == 0) {
627 int qdev_walk_children(DeviceState
*dev
,
628 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
629 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
636 err
= pre_devfn(dev
, opaque
);
642 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
643 err
= qbus_walk_children(bus
, pre_devfn
, pre_busfn
,
644 post_devfn
, post_busfn
, opaque
);
651 err
= post_devfn(dev
, opaque
);
660 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
666 WITH_RCU_READ_LOCK_GUARD() {
667 QTAILQ_FOREACH_RCU(kid
, &bus
->children
, sibling
) {
668 DeviceState
*dev
= kid
->child
;
670 if (dev
->id
&& strcmp(dev
->id
, id
) == 0) {
674 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
675 ret
= qdev_find_recursive(child
, id
);
685 char *qdev_get_dev_path(DeviceState
*dev
)
689 if (!dev
|| !dev
->parent_bus
) {
693 bc
= BUS_GET_CLASS(dev
->parent_bus
);
694 if (bc
->get_dev_path
) {
695 return bc
->get_dev_path(dev
);
701 static bool device_get_realized(Object
*obj
, Error
**errp
)
703 DeviceState
*dev
= DEVICE(obj
);
704 return dev
->realized
;
707 static bool check_only_migratable(Object
*obj
, Error
**errp
)
709 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
711 if (!vmstate_check_only_migratable(dc
->vmsd
)) {
712 error_setg(errp
, "Device %s is not migratable, but "
713 "--only-migratable was specified",
714 object_get_typename(obj
));
721 static void device_set_realized(Object
*obj
, bool value
, Error
**errp
)
723 DeviceState
*dev
= DEVICE(obj
);
724 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
725 HotplugHandler
*hotplug_ctrl
;
728 Error
*local_err
= NULL
;
729 bool unattached_parent
= false;
730 static int unattached_count
;
732 if (dev
->hotplugged
&& !dc
->hotpluggable
) {
733 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
, object_get_typename(obj
));
737 if (value
&& !dev
->realized
) {
738 if (!check_only_migratable(obj
, errp
)) {
743 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
745 object_property_add_child(container_get(qdev_get_machine(),
748 unattached_parent
= true;
752 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
754 hotplug_handler_pre_plug(hotplug_ctrl
, dev
, &local_err
);
755 if (local_err
!= NULL
) {
761 dc
->realize(dev
, &local_err
);
762 if (local_err
!= NULL
) {
767 DEVICE_LISTENER_CALL(realize
, Forward
, dev
);
770 * always free/re-initialize here since the value cannot be cleaned up
771 * in device_unrealize due to its usage later on in the unplug path
773 g_free(dev
->canonical_path
);
774 dev
->canonical_path
= object_get_canonical_path(OBJECT(dev
));
775 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
779 clock_setup_canonical_path(ncl
->clock
);
783 if (qdev_get_vmsd(dev
)) {
784 if (vmstate_register_with_alias_id(VMSTATE_IF(dev
),
785 VMSTATE_INSTANCE_ID_ANY
,
786 qdev_get_vmsd(dev
), dev
,
787 dev
->instance_id_alias
,
788 dev
->alias_required_for_version
,
790 goto post_realize_fail
;
795 * Clear the reset state, in case the object was previously unrealized
796 * with a dirty state.
798 resettable_state_clear(&dev
->reset
);
800 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
801 if (!qbus_realize(bus
, errp
)) {
802 goto child_realize_fail
;
805 if (dev
->hotplugged
) {
807 * Reset the device, as well as its subtree which, at this point,
808 * should be realized too.
810 resettable_assert_reset(OBJECT(dev
), RESET_TYPE_COLD
);
811 resettable_change_parent(OBJECT(dev
), OBJECT(dev
->parent_bus
),
813 resettable_release_reset(OBJECT(dev
), RESET_TYPE_COLD
);
815 dev
->pending_deleted_event
= false;
818 hotplug_handler_plug(hotplug_ctrl
, dev
, &local_err
);
819 if (local_err
!= NULL
) {
820 goto child_realize_fail
;
824 qatomic_store_release(&dev
->realized
, value
);
826 } else if (!value
&& dev
->realized
) {
829 * Change the value so that any concurrent users are aware
830 * that the device is going to be unrealized
832 * TODO: change .realized property to enum that states
833 * each phase of the device realization/unrealization
836 qatomic_set(&dev
->realized
, value
);
838 * Ensure that concurrent users see this update prior to
839 * any other changes done by unrealize.
843 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
846 if (qdev_get_vmsd(dev
)) {
847 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
852 dev
->pending_deleted_event
= true;
853 DEVICE_LISTENER_CALL(unrealize
, Reverse
, dev
);
856 assert(local_err
== NULL
);
860 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
864 if (qdev_get_vmsd(dev
)) {
865 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
869 g_free(dev
->canonical_path
);
870 dev
->canonical_path
= NULL
;
876 error_propagate(errp
, local_err
);
877 if (unattached_parent
) {
879 * Beware, this doesn't just revert
880 * object_property_add_child(), it also runs bus_remove()!
882 object_unparent(OBJECT(dev
));
887 static bool device_get_hotpluggable(Object
*obj
, Error
**errp
)
889 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
890 DeviceState
*dev
= DEVICE(obj
);
892 return dc
->hotpluggable
&& (dev
->parent_bus
== NULL
||
893 qbus_is_hotpluggable(dev
->parent_bus
));
896 static bool device_get_hotplugged(Object
*obj
, Error
**errp
)
898 DeviceState
*dev
= DEVICE(obj
);
900 return dev
->hotplugged
;
903 static void device_initfn(Object
*obj
)
905 DeviceState
*dev
= DEVICE(obj
);
907 if (phase_check(PHASE_MACHINE_READY
)) {
909 qdev_hot_added
= true;
912 dev
->instance_id_alias
= -1;
913 dev
->realized
= false;
914 dev
->allow_unplug_during_migration
= false;
916 QLIST_INIT(&dev
->gpios
);
917 QLIST_INIT(&dev
->clocks
);
920 static void device_post_init(Object
*obj
)
923 * Note: ordered so that the user's global properties take
926 object_apply_compat_props(obj
);
927 qdev_prop_set_globals(DEVICE(obj
));
930 /* Unlink device from bus and free the structure. */
931 static void device_finalize(Object
*obj
)
933 NamedGPIOList
*ngl
, *next
;
935 DeviceState
*dev
= DEVICE(obj
);
937 QLIST_FOREACH_SAFE(ngl
, &dev
->gpios
, node
, next
) {
938 QLIST_REMOVE(ngl
, node
);
939 qemu_free_irqs(ngl
->in
, ngl
->num_in
);
942 /* ngl->out irqs are owned by the other end and should not be freed
947 qdev_finalize_clocklist(dev
);
949 /* Only send event if the device had been completely realized */
950 if (dev
->pending_deleted_event
) {
951 g_assert(dev
->canonical_path
);
953 qapi_event_send_device_deleted(!!dev
->id
, dev
->id
, dev
->canonical_path
);
954 g_free(dev
->canonical_path
);
955 dev
->canonical_path
= NULL
;
958 qemu_opts_del(dev
->opts
);
961 static void device_class_base_init(ObjectClass
*class, void *data
)
963 DeviceClass
*klass
= DEVICE_CLASS(class);
965 /* We explicitly look up properties in the superclasses,
966 * so do not propagate them to the subclasses.
968 klass
->props_
= NULL
;
971 static void device_unparent(Object
*obj
)
973 DeviceState
*dev
= DEVICE(obj
);
979 while (dev
->num_child_bus
) {
980 bus
= QLIST_FIRST(&dev
->child_bus
);
981 object_unparent(OBJECT(bus
));
983 if (dev
->parent_bus
) {
984 bus_remove_child(dev
->parent_bus
, dev
);
985 object_unref(OBJECT(dev
->parent_bus
));
986 dev
->parent_bus
= NULL
;
991 device_vmstate_if_get_id(VMStateIf
*obj
)
993 DeviceState
*dev
= DEVICE(obj
);
995 return qdev_get_dev_path(dev
);
999 * device_phases_reset:
1000 * Transition reset method for devices to allow moving
1001 * smoothly from legacy reset method to multi-phases
1003 static void device_phases_reset(DeviceState
*dev
)
1005 ResettableClass
*rc
= RESETTABLE_GET_CLASS(dev
);
1007 if (rc
->phases
.enter
) {
1008 rc
->phases
.enter(OBJECT(dev
), RESET_TYPE_COLD
);
1010 if (rc
->phases
.hold
) {
1011 rc
->phases
.hold(OBJECT(dev
));
1013 if (rc
->phases
.exit
) {
1014 rc
->phases
.exit(OBJECT(dev
));
1018 static void device_transitional_reset(Object
*obj
)
1020 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
1023 * This will call either @device_phases_reset (for multi-phases transitioned
1024 * devices) or a device's specific method for not-yet transitioned devices.
1025 * In both case, it does not reset children.
1028 dc
->reset(DEVICE(obj
));
1033 * device_get_transitional_reset:
1034 * check if the device's class is ready for multi-phase
1036 static ResettableTrFunction
device_get_transitional_reset(Object
*obj
)
1038 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
1039 if (dc
->reset
!= device_phases_reset
) {
1041 * dc->reset has been overridden by a subclass,
1042 * the device is not ready for multi phase yet.
1044 return device_transitional_reset
;
1049 static void device_class_init(ObjectClass
*class, void *data
)
1051 DeviceClass
*dc
= DEVICE_CLASS(class);
1052 VMStateIfClass
*vc
= VMSTATE_IF_CLASS(class);
1053 ResettableClass
*rc
= RESETTABLE_CLASS(class);
1055 class->unparent
= device_unparent
;
1057 /* by default all devices were considered as hotpluggable,
1058 * so with intent to check it in generic qdev_unplug() /
1059 * device_set_realized() functions make every device
1060 * hotpluggable. Devices that shouldn't be hotpluggable,
1061 * should override it in their class_init()
1063 dc
->hotpluggable
= true;
1064 dc
->user_creatable
= true;
1065 vc
->get_id
= device_vmstate_if_get_id
;
1066 rc
->get_state
= device_get_reset_state
;
1067 rc
->child_foreach
= device_reset_child_foreach
;
1070 * @device_phases_reset is put as the default reset method below, allowing
1071 * to do the multi-phase transition from base classes to leaf classes. It
1072 * allows a legacy-reset Device class to extend a multi-phases-reset
1073 * Device class for the following reason:
1074 * + If a base class B has been moved to multi-phase, then it does not
1075 * override this default reset method and may have defined phase methods.
1076 * + A child class C (extending class B) which uses
1077 * device_class_set_parent_reset() (or similar means) to override the
1078 * reset method will still work as expected. @device_phases_reset function
1079 * will be registered as the parent reset method and effectively call
1080 * parent reset phases.
1082 dc
->reset
= device_phases_reset
;
1083 rc
->get_transitional_function
= device_get_transitional_reset
;
1085 object_class_property_add_bool(class, "realized",
1086 device_get_realized
, device_set_realized
);
1087 object_class_property_add_bool(class, "hotpluggable",
1088 device_get_hotpluggable
, NULL
);
1089 object_class_property_add_bool(class, "hotplugged",
1090 device_get_hotplugged
, NULL
);
1091 object_class_property_add_link(class, "parent_bus", TYPE_BUS
,
1092 offsetof(DeviceState
, parent_bus
), NULL
, 0);
1095 void device_class_set_parent_reset(DeviceClass
*dc
,
1096 DeviceReset dev_reset
,
1097 DeviceReset
*parent_reset
)
1099 *parent_reset
= dc
->reset
;
1100 dc
->reset
= dev_reset
;
1103 void device_class_set_parent_realize(DeviceClass
*dc
,
1104 DeviceRealize dev_realize
,
1105 DeviceRealize
*parent_realize
)
1107 *parent_realize
= dc
->realize
;
1108 dc
->realize
= dev_realize
;
1111 void device_class_set_parent_unrealize(DeviceClass
*dc
,
1112 DeviceUnrealize dev_unrealize
,
1113 DeviceUnrealize
*parent_unrealize
)
1115 *parent_unrealize
= dc
->unrealize
;
1116 dc
->unrealize
= dev_unrealize
;
1119 void device_legacy_reset(DeviceState
*dev
)
1121 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
1123 trace_qdev_reset(dev
, object_get_typename(OBJECT(dev
)));
1129 Object
*qdev_get_machine(void)
1134 dev
= container_get(object_get_root(), "/machine");
1140 static MachineInitPhase machine_phase
;
1142 bool phase_check(MachineInitPhase phase
)
1144 return machine_phase
>= phase
;
1147 void phase_advance(MachineInitPhase phase
)
1149 assert(machine_phase
== phase
- 1);
1150 machine_phase
= phase
;
1153 static const TypeInfo device_type_info
= {
1154 .name
= TYPE_DEVICE
,
1155 .parent
= TYPE_OBJECT
,
1156 .instance_size
= sizeof(DeviceState
),
1157 .instance_init
= device_initfn
,
1158 .instance_post_init
= device_post_init
,
1159 .instance_finalize
= device_finalize
,
1160 .class_base_init
= device_class_base_init
,
1161 .class_init
= device_class_init
,
1163 .class_size
= sizeof(DeviceClass
),
1164 .interfaces
= (InterfaceInfo
[]) {
1165 { TYPE_VMSTATE_IF
},
1166 { TYPE_RESETTABLE_INTERFACE
},
1171 static void qdev_register_types(void)
1173 type_register_static(&device_type_info
);
1176 type_init(qdev_register_types
)