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
;
101 if (old_parent_bus
) {
102 trace_qdev_update_parent_bus(dev
, object_get_typename(OBJECT(dev
)),
103 old_parent_bus
, object_get_typename(OBJECT(old_parent_bus
)),
104 OBJECT(bus
), object_get_typename(OBJECT(bus
)));
106 * Keep a reference to the device while it's not plugged into
107 * any bus, to avoid it potentially evaporating when it is
108 * dereffed in bus_remove_child().
109 * Also keep the ref of the parent bus until the end, so that
110 * we can safely call resettable_change_parent() below.
112 object_ref(OBJECT(dev
));
113 bus_remove_child(dev
->parent_bus
, dev
);
115 dev
->parent_bus
= bus
;
116 object_ref(OBJECT(bus
));
117 bus_add_child(bus
, dev
);
119 resettable_change_parent(OBJECT(dev
), OBJECT(bus
),
120 OBJECT(old_parent_bus
));
122 if (old_parent_bus
) {
123 object_unref(OBJECT(old_parent_bus
));
124 object_unref(OBJECT(dev
));
128 /* Create a new device. This only initializes the device state
129 structure and allows properties to be set. The device still needs
130 to be realized. See qdev-core.h. */
131 DeviceState
*qdev_create(BusState
*bus
, const char *name
)
135 dev
= qdev_try_create(bus
, name
);
138 error_report("Unknown device '%s' for bus '%s'", name
,
139 object_get_typename(OBJECT(bus
)));
141 error_report("Unknown device '%s' for default sysbus", name
);
149 DeviceState
*qdev_try_create(BusState
*bus
, const char *type
)
153 if (object_class_by_name(type
) == NULL
) {
156 dev
= DEVICE(object_new(type
));
162 /* Assert that the device really is a SysBusDevice before
163 * we put it onto the sysbus. Non-sysbus devices which aren't
164 * being put onto a bus should be created with object_new(TYPE_FOO),
165 * not qdev_create(NULL, TYPE_FOO).
167 g_assert(object_dynamic_cast(OBJECT(dev
), TYPE_SYS_BUS_DEVICE
));
168 bus
= sysbus_get_default();
171 qdev_set_parent_bus(dev
, bus
);
172 object_unref(OBJECT(dev
));
176 static QTAILQ_HEAD(, DeviceListener
) device_listeners
177 = QTAILQ_HEAD_INITIALIZER(device_listeners
);
179 enum ListenerDirection
{ Forward
, Reverse
};
181 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
183 DeviceListener *_listener; \
185 switch (_direction) { \
187 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
188 if (_listener->_callback) { \
189 _listener->_callback(_listener, ##_args); \
194 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
196 if (_listener->_callback) { \
197 _listener->_callback(_listener, ##_args); \
206 static int device_listener_add(DeviceState
*dev
, void *opaque
)
208 DEVICE_LISTENER_CALL(realize
, Forward
, dev
);
213 void device_listener_register(DeviceListener
*listener
)
215 QTAILQ_INSERT_TAIL(&device_listeners
, listener
, link
);
217 qbus_walk_children(sysbus_get_default(), NULL
, NULL
, device_listener_add
,
221 void device_listener_unregister(DeviceListener
*listener
)
223 QTAILQ_REMOVE(&device_listeners
, listener
, link
);
226 bool qdev_should_hide_device(QemuOpts
*opts
)
229 DeviceListener
*listener
;
231 QTAILQ_FOREACH(listener
, &device_listeners
, link
) {
232 if (listener
->should_be_hidden
) {
234 * should_be_hidden_will return
235 * 1 if device matches opts and it should be hidden
236 * 0 if device matches opts and should not be hidden
237 * -1 if device doesn't match ops
239 rc
= listener
->should_be_hidden(listener
, opts
);
250 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
251 int required_for_version
)
253 assert(!dev
->realized
);
254 dev
->instance_id_alias
= alias_id
;
255 dev
->alias_required_for_version
= required_for_version
;
258 HotplugHandler
*qdev_get_machine_hotplug_handler(DeviceState
*dev
)
260 MachineState
*machine
;
262 Object
*m_obj
= qdev_get_machine();
264 if (object_dynamic_cast(m_obj
, TYPE_MACHINE
)) {
265 machine
= MACHINE(m_obj
);
266 mc
= MACHINE_GET_CLASS(machine
);
267 if (mc
->get_hotplug_handler
) {
268 return mc
->get_hotplug_handler(machine
, dev
);
275 bool qdev_hotplug_allowed(DeviceState
*dev
, Error
**errp
)
277 MachineState
*machine
;
279 Object
*m_obj
= qdev_get_machine();
281 if (object_dynamic_cast(m_obj
, TYPE_MACHINE
)) {
282 machine
= MACHINE(m_obj
);
283 mc
= MACHINE_GET_CLASS(machine
);
284 if (mc
->hotplug_allowed
) {
285 return mc
->hotplug_allowed(machine
, dev
, errp
);
292 HotplugHandler
*qdev_get_bus_hotplug_handler(DeviceState
*dev
)
294 if (dev
->parent_bus
) {
295 return dev
->parent_bus
->hotplug_handler
;
300 HotplugHandler
*qdev_get_hotplug_handler(DeviceState
*dev
)
302 HotplugHandler
*hotplug_ctrl
= qdev_get_machine_hotplug_handler(dev
);
304 if (hotplug_ctrl
== NULL
&& dev
->parent_bus
) {
305 hotplug_ctrl
= qdev_get_bus_hotplug_handler(dev
);
310 static int qdev_prereset(DeviceState
*dev
, void *opaque
)
312 trace_qdev_reset_tree(dev
, object_get_typename(OBJECT(dev
)));
316 static int qbus_prereset(BusState
*bus
, void *opaque
)
318 trace_qbus_reset_tree(bus
, object_get_typename(OBJECT(bus
)));
322 static int qdev_reset_one(DeviceState
*dev
, void *opaque
)
324 device_legacy_reset(dev
);
329 static int qbus_reset_one(BusState
*bus
, void *opaque
)
331 BusClass
*bc
= BUS_GET_CLASS(bus
);
332 trace_qbus_reset(bus
, object_get_typename(OBJECT(bus
)));
339 void qdev_reset_all(DeviceState
*dev
)
341 trace_qdev_reset_all(dev
, object_get_typename(OBJECT(dev
)));
342 qdev_walk_children(dev
, qdev_prereset
, qbus_prereset
,
343 qdev_reset_one
, qbus_reset_one
, NULL
);
346 void qdev_reset_all_fn(void *opaque
)
348 qdev_reset_all(DEVICE(opaque
));
351 void qbus_reset_all(BusState
*bus
)
353 trace_qbus_reset_all(bus
, object_get_typename(OBJECT(bus
)));
354 qbus_walk_children(bus
, qdev_prereset
, qbus_prereset
,
355 qdev_reset_one
, qbus_reset_one
, NULL
);
358 void qbus_reset_all_fn(void *opaque
)
360 BusState
*bus
= opaque
;
364 void device_cold_reset(DeviceState
*dev
)
366 resettable_reset(OBJECT(dev
), RESET_TYPE_COLD
);
369 bool device_is_in_reset(DeviceState
*dev
)
371 return resettable_is_in_reset(OBJECT(dev
));
374 static ResettableState
*device_get_reset_state(Object
*obj
)
376 DeviceState
*dev
= DEVICE(obj
);
380 static void device_reset_child_foreach(Object
*obj
, ResettableChildCallback cb
,
381 void *opaque
, ResetType type
)
383 DeviceState
*dev
= DEVICE(obj
);
386 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
387 cb(OBJECT(bus
), opaque
, type
);
391 /* can be used as ->unplug() callback for the simple cases */
392 void qdev_simple_device_unplug_cb(HotplugHandler
*hotplug_dev
,
393 DeviceState
*dev
, Error
**errp
)
395 object_property_set_bool(OBJECT(dev
), false, "realized", &error_abort
);
400 * Device properties should be set before calling this function. IRQs
401 * and MMIO regions should be connected/mapped after calling this
403 * On failure, report an error with error_report() and terminate the
404 * program. This is okay during machine creation. Don't use for
405 * hotplug, because there callers need to recover from failure.
406 * Exception: if you know the device's init() callback can't fail,
407 * then qdev_init_nofail() can't fail either, and is therefore usable
408 * even then. But relying on the device implementation that way is
409 * somewhat unclean, and best avoided.
411 void qdev_init_nofail(DeviceState
*dev
)
415 assert(!dev
->realized
);
417 object_ref(OBJECT(dev
));
418 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
420 error_reportf_err(err
, "Initialization of device %s failed: ",
421 object_get_typename(OBJECT(dev
)));
424 object_unref(OBJECT(dev
));
427 void qdev_machine_creation_done(void)
430 * ok, initial machine setup is done, starting from now we can
431 * only create hotpluggable devices
436 bool qdev_machine_modified(void)
438 return qdev_hot_added
|| qdev_hot_removed
;
441 BusState
*qdev_get_parent_bus(DeviceState
*dev
)
443 return dev
->parent_bus
;
446 static NamedGPIOList
*qdev_get_named_gpio_list(DeviceState
*dev
,
451 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
452 /* NULL is a valid and matchable name. */
453 if (g_strcmp0(name
, ngl
->name
) == 0) {
458 ngl
= g_malloc0(sizeof(*ngl
));
459 ngl
->name
= g_strdup(name
);
460 QLIST_INSERT_HEAD(&dev
->gpios
, ngl
, node
);
464 void qdev_init_gpio_in_named_with_opaque(DeviceState
*dev
,
465 qemu_irq_handler handler
,
467 const char *name
, int n
)
470 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
472 assert(gpio_list
->num_out
== 0 || !name
);
473 gpio_list
->in
= qemu_extend_irqs(gpio_list
->in
, gpio_list
->num_in
, handler
,
477 name
= "unnamed-gpio-in";
479 for (i
= gpio_list
->num_in
; i
< gpio_list
->num_in
+ n
; i
++) {
480 gchar
*propname
= g_strdup_printf("%s[%u]", name
, i
);
482 object_property_add_child(OBJECT(dev
), propname
,
483 OBJECT(gpio_list
->in
[i
]));
487 gpio_list
->num_in
+= n
;
490 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
)
492 qdev_init_gpio_in_named(dev
, handler
, NULL
, n
);
495 void qdev_init_gpio_out_named(DeviceState
*dev
, qemu_irq
*pins
,
496 const char *name
, int n
)
499 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
501 assert(gpio_list
->num_in
== 0 || !name
);
504 name
= "unnamed-gpio-out";
506 memset(pins
, 0, sizeof(*pins
) * n
);
507 for (i
= 0; i
< n
; ++i
) {
508 gchar
*propname
= g_strdup_printf("%s[%u]", name
,
509 gpio_list
->num_out
+ i
);
511 object_property_add_link(OBJECT(dev
), propname
, TYPE_IRQ
,
513 object_property_allow_set_link
,
514 OBJ_PROP_LINK_STRONG
);
517 gpio_list
->num_out
+= n
;
520 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
)
522 qdev_init_gpio_out_named(dev
, pins
, NULL
, n
);
525 qemu_irq
qdev_get_gpio_in_named(DeviceState
*dev
, const char *name
, int n
)
527 NamedGPIOList
*gpio_list
= qdev_get_named_gpio_list(dev
, name
);
529 assert(n
>= 0 && n
< gpio_list
->num_in
);
530 return gpio_list
->in
[n
];
533 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
)
535 return qdev_get_gpio_in_named(dev
, NULL
, n
);
538 void qdev_connect_gpio_out_named(DeviceState
*dev
, const char *name
, int n
,
541 char *propname
= g_strdup_printf("%s[%d]",
542 name
? name
: "unnamed-gpio-out", n
);
543 if (pin
&& !OBJECT(pin
)->parent
) {
544 /* We need a name for object_property_set_link to work */
545 object_property_add_child(container_get(qdev_get_machine(),
547 "non-qdev-gpio[*]", OBJECT(pin
));
549 object_property_set_link(OBJECT(dev
), OBJECT(pin
), propname
, &error_abort
);
553 qemu_irq
qdev_get_gpio_out_connector(DeviceState
*dev
, const char *name
, int n
)
555 g_autofree
char *propname
= g_strdup_printf("%s[%d]",
556 name
? name
: "unnamed-gpio-out", n
);
558 qemu_irq ret
= (qemu_irq
)object_property_get_link(OBJECT(dev
), propname
,
564 /* disconnect a GPIO output, returning the disconnected input (if any) */
566 static qemu_irq
qdev_disconnect_gpio_out_named(DeviceState
*dev
,
567 const char *name
, int n
)
569 char *propname
= g_strdup_printf("%s[%d]",
570 name
? name
: "unnamed-gpio-out", n
);
572 qemu_irq ret
= (qemu_irq
)object_property_get_link(OBJECT(dev
), propname
,
575 object_property_set_link(OBJECT(dev
), NULL
, propname
, NULL
);
581 qemu_irq
qdev_intercept_gpio_out(DeviceState
*dev
, qemu_irq icpt
,
582 const char *name
, int n
)
584 qemu_irq disconnected
= qdev_disconnect_gpio_out_named(dev
, name
, n
);
585 qdev_connect_gpio_out_named(dev
, name
, n
, icpt
);
589 void qdev_connect_gpio_out(DeviceState
* dev
, int n
, qemu_irq pin
)
591 qdev_connect_gpio_out_named(dev
, NULL
, n
, pin
);
594 void qdev_pass_gpios(DeviceState
*dev
, DeviceState
*container
,
598 NamedGPIOList
*ngl
= qdev_get_named_gpio_list(dev
, name
);
600 for (i
= 0; i
< ngl
->num_in
; i
++) {
601 const char *nm
= ngl
->name
? ngl
->name
: "unnamed-gpio-in";
602 char *propname
= g_strdup_printf("%s[%d]", nm
, i
);
604 object_property_add_alias(OBJECT(container
), propname
,
605 OBJECT(dev
), propname
);
608 for (i
= 0; i
< ngl
->num_out
; i
++) {
609 const char *nm
= ngl
->name
? ngl
->name
: "unnamed-gpio-out";
610 char *propname
= g_strdup_printf("%s[%d]", nm
, i
);
612 object_property_add_alias(OBJECT(container
), propname
,
613 OBJECT(dev
), propname
);
616 QLIST_REMOVE(ngl
, node
);
617 QLIST_INSERT_HEAD(&container
->gpios
, ngl
, node
);
620 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
)
623 Object
*child
= object_resolve_path_component(OBJECT(dev
), name
);
625 bus
= (BusState
*)object_dynamic_cast(child
, TYPE_BUS
);
630 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
631 if (strcmp(name
, bus
->name
) == 0) {
638 int qdev_walk_children(DeviceState
*dev
,
639 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
640 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
647 err
= pre_devfn(dev
, opaque
);
653 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
654 err
= qbus_walk_children(bus
, pre_devfn
, pre_busfn
,
655 post_devfn
, post_busfn
, opaque
);
662 err
= post_devfn(dev
, opaque
);
671 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
)
677 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
678 DeviceState
*dev
= kid
->child
;
680 if (dev
->id
&& strcmp(dev
->id
, id
) == 0) {
684 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
685 ret
= qdev_find_recursive(child
, id
);
694 char *qdev_get_dev_path(DeviceState
*dev
)
698 if (!dev
|| !dev
->parent_bus
) {
702 bc
= BUS_GET_CLASS(dev
->parent_bus
);
703 if (bc
->get_dev_path
) {
704 return bc
->get_dev_path(dev
);
711 * Legacy property handling
714 static void qdev_get_legacy_property(Object
*obj
, Visitor
*v
,
715 const char *name
, void *opaque
,
718 DeviceState
*dev
= DEVICE(obj
);
719 Property
*prop
= opaque
;
724 prop
->info
->print(dev
, prop
, buffer
, sizeof(buffer
));
725 visit_type_str(v
, name
, &ptr
, errp
);
729 * qdev_class_add_legacy_property:
730 * @dev: Device to add the property to.
731 * @prop: The qdev property definition.
733 * Add a legacy QOM property to @dev for qdev property @prop.
735 * Legacy properties are string versions of QOM properties. The format of
736 * the string depends on the property type. Legacy properties are only
737 * needed for "info qtree".
739 * Do not use this in new code! QOM Properties added through this interface
740 * will be given names in the "legacy" namespace.
742 static void qdev_class_add_legacy_property(DeviceClass
*dc
, Property
*prop
)
744 g_autofree
char *name
= NULL
;
746 /* Register pointer properties as legacy properties */
747 if (!prop
->info
->print
&& prop
->info
->get
) {
751 name
= g_strdup_printf("legacy-%s", prop
->name
);
752 object_class_property_add(OBJECT_CLASS(dc
), name
, "str",
753 prop
->info
->print
? qdev_get_legacy_property
: prop
->info
->get
,
757 void qdev_property_add_static(DeviceState
*dev
, Property
*prop
)
759 Object
*obj
= OBJECT(dev
);
762 assert(!prop
->info
->create
);
764 op
= object_property_add(obj
, prop
->name
, prop
->info
->name
,
765 prop
->info
->get
, prop
->info
->set
,
769 object_property_set_description(obj
, prop
->name
,
770 prop
->info
->description
);
772 if (prop
->set_default
) {
773 prop
->info
->set_default_value(op
, prop
);
780 static void qdev_class_add_property(DeviceClass
*klass
, Property
*prop
)
782 ObjectClass
*oc
= OBJECT_CLASS(klass
);
784 if (prop
->info
->create
) {
785 prop
->info
->create(oc
, prop
);
789 op
= object_class_property_add(oc
,
790 prop
->name
, prop
->info
->name
,
791 prop
->info
->get
, prop
->info
->set
,
794 if (prop
->set_default
) {
795 prop
->info
->set_default_value(op
, prop
);
798 object_class_property_set_description(oc
, prop
->name
,
799 prop
->info
->description
);
802 /* @qdev_alias_all_properties - Add alias properties to the source object for
803 * all qdev properties on the target DeviceState.
805 void qdev_alias_all_properties(DeviceState
*target
, Object
*source
)
810 class = object_get_class(OBJECT(target
));
812 DeviceClass
*dc
= DEVICE_CLASS(class);
814 for (prop
= dc
->props_
; prop
&& prop
->name
; prop
++) {
815 object_property_add_alias(source
, prop
->name
,
816 OBJECT(target
), prop
->name
);
818 class = object_class_get_parent(class);
819 } while (class != object_class_by_name(TYPE_DEVICE
));
822 static bool device_get_realized(Object
*obj
, Error
**errp
)
824 DeviceState
*dev
= DEVICE(obj
);
825 return dev
->realized
;
828 static bool check_only_migratable(Object
*obj
, Error
**errp
)
830 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
832 if (!vmstate_check_only_migratable(dc
->vmsd
)) {
833 error_setg(errp
, "Device %s is not migratable, but "
834 "--only-migratable was specified",
835 object_get_typename(obj
));
842 static void device_set_realized(Object
*obj
, bool value
, Error
**errp
)
844 DeviceState
*dev
= DEVICE(obj
);
845 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
846 HotplugHandler
*hotplug_ctrl
;
849 Error
*local_err
= NULL
;
850 bool unattached_parent
= false;
851 static int unattached_count
;
853 if (dev
->hotplugged
&& !dc
->hotpluggable
) {
854 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
, object_get_typename(obj
));
858 if (value
&& !dev
->realized
) {
859 if (!check_only_migratable(obj
, &local_err
)) {
864 gchar
*name
= g_strdup_printf("device[%d]", unattached_count
++);
866 object_property_add_child(container_get(qdev_get_machine(),
869 unattached_parent
= true;
873 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
875 hotplug_handler_pre_plug(hotplug_ctrl
, dev
, &local_err
);
876 if (local_err
!= NULL
) {
882 dc
->realize(dev
, &local_err
);
883 if (local_err
!= NULL
) {
888 DEVICE_LISTENER_CALL(realize
, Forward
, dev
);
891 * always free/re-initialize here since the value cannot be cleaned up
892 * in device_unrealize due to its usage later on in the unplug path
894 g_free(dev
->canonical_path
);
895 dev
->canonical_path
= object_get_canonical_path(OBJECT(dev
));
896 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
900 clock_setup_canonical_path(ncl
->clock
);
904 if (qdev_get_vmsd(dev
)) {
905 if (vmstate_register_with_alias_id(VMSTATE_IF(dev
),
906 VMSTATE_INSTANCE_ID_ANY
,
907 qdev_get_vmsd(dev
), dev
,
908 dev
->instance_id_alias
,
909 dev
->alias_required_for_version
,
911 goto post_realize_fail
;
916 * Clear the reset state, in case the object was previously unrealized
917 * with a dirty state.
919 resettable_state_clear(&dev
->reset
);
921 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
922 object_property_set_bool(OBJECT(bus
), true, "realized",
924 if (local_err
!= NULL
) {
925 goto child_realize_fail
;
928 if (dev
->hotplugged
) {
930 * Reset the device, as well as its subtree which, at this point,
931 * should be realized too.
933 resettable_assert_reset(OBJECT(dev
), RESET_TYPE_COLD
);
934 resettable_change_parent(OBJECT(dev
), OBJECT(dev
->parent_bus
),
936 resettable_release_reset(OBJECT(dev
), RESET_TYPE_COLD
);
938 dev
->pending_deleted_event
= false;
941 hotplug_handler_plug(hotplug_ctrl
, dev
, &local_err
);
942 if (local_err
!= NULL
) {
943 goto child_realize_fail
;
947 } else if (!value
&& dev
->realized
) {
948 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
949 object_property_set_bool(OBJECT(bus
), false, "realized",
952 if (qdev_get_vmsd(dev
)) {
953 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
958 dev
->pending_deleted_event
= true;
959 DEVICE_LISTENER_CALL(unrealize
, Reverse
, dev
);
962 assert(local_err
== NULL
);
963 dev
->realized
= value
;
967 QLIST_FOREACH(bus
, &dev
->child_bus
, sibling
) {
968 object_property_set_bool(OBJECT(bus
), false, "realized",
972 if (qdev_get_vmsd(dev
)) {
973 vmstate_unregister(VMSTATE_IF(dev
), qdev_get_vmsd(dev
), dev
);
977 g_free(dev
->canonical_path
);
978 dev
->canonical_path
= NULL
;
984 error_propagate(errp
, local_err
);
985 if (unattached_parent
) {
986 object_unparent(OBJECT(dev
));
991 static bool device_get_hotpluggable(Object
*obj
, Error
**errp
)
993 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
994 DeviceState
*dev
= DEVICE(obj
);
996 return dc
->hotpluggable
&& (dev
->parent_bus
== NULL
||
997 qbus_is_hotpluggable(dev
->parent_bus
));
1000 static bool device_get_hotplugged(Object
*obj
, Error
**errp
)
1002 DeviceState
*dev
= DEVICE(obj
);
1004 return dev
->hotplugged
;
1007 static void device_initfn(Object
*obj
)
1009 DeviceState
*dev
= DEVICE(obj
);
1012 dev
->hotplugged
= 1;
1013 qdev_hot_added
= true;
1016 dev
->instance_id_alias
= -1;
1017 dev
->realized
= false;
1018 dev
->allow_unplug_during_migration
= false;
1020 QLIST_INIT(&dev
->gpios
);
1021 QLIST_INIT(&dev
->clocks
);
1024 static void device_post_init(Object
*obj
)
1027 * Note: ordered so that the user's global properties take
1030 object_apply_compat_props(obj
);
1031 qdev_prop_set_globals(DEVICE(obj
));
1034 /* Unlink device from bus and free the structure. */
1035 static void device_finalize(Object
*obj
)
1037 NamedGPIOList
*ngl
, *next
;
1039 DeviceState
*dev
= DEVICE(obj
);
1041 QLIST_FOREACH_SAFE(ngl
, &dev
->gpios
, node
, next
) {
1042 QLIST_REMOVE(ngl
, node
);
1043 qemu_free_irqs(ngl
->in
, ngl
->num_in
);
1046 /* ngl->out irqs are owned by the other end and should not be freed
1051 qdev_finalize_clocklist(dev
);
1053 /* Only send event if the device had been completely realized */
1054 if (dev
->pending_deleted_event
) {
1055 g_assert(dev
->canonical_path
);
1057 qapi_event_send_device_deleted(!!dev
->id
, dev
->id
, dev
->canonical_path
);
1058 g_free(dev
->canonical_path
);
1059 dev
->canonical_path
= NULL
;
1062 qemu_opts_del(dev
->opts
);
1065 static void device_class_base_init(ObjectClass
*class, void *data
)
1067 DeviceClass
*klass
= DEVICE_CLASS(class);
1069 /* We explicitly look up properties in the superclasses,
1070 * so do not propagate them to the subclasses.
1072 klass
->props_
= NULL
;
1075 static void device_unparent(Object
*obj
)
1077 DeviceState
*dev
= DEVICE(obj
);
1080 if (dev
->realized
) {
1081 object_property_set_bool(obj
, false, "realized", &error_abort
);
1083 while (dev
->num_child_bus
) {
1084 bus
= QLIST_FIRST(&dev
->child_bus
);
1085 object_unparent(OBJECT(bus
));
1087 if (dev
->parent_bus
) {
1088 bus_remove_child(dev
->parent_bus
, dev
);
1089 object_unref(OBJECT(dev
->parent_bus
));
1090 dev
->parent_bus
= NULL
;
1095 device_vmstate_if_get_id(VMStateIf
*obj
)
1097 DeviceState
*dev
= DEVICE(obj
);
1099 return qdev_get_dev_path(dev
);
1103 * device_phases_reset:
1104 * Transition reset method for devices to allow moving
1105 * smoothly from legacy reset method to multi-phases
1107 static void device_phases_reset(DeviceState
*dev
)
1109 ResettableClass
*rc
= RESETTABLE_GET_CLASS(dev
);
1111 if (rc
->phases
.enter
) {
1112 rc
->phases
.enter(OBJECT(dev
), RESET_TYPE_COLD
);
1114 if (rc
->phases
.hold
) {
1115 rc
->phases
.hold(OBJECT(dev
));
1117 if (rc
->phases
.exit
) {
1118 rc
->phases
.exit(OBJECT(dev
));
1122 static void device_transitional_reset(Object
*obj
)
1124 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
1127 * This will call either @device_phases_reset (for multi-phases transitioned
1128 * devices) or a device's specific method for not-yet transitioned devices.
1129 * In both case, it does not reset children.
1132 dc
->reset(DEVICE(obj
));
1137 * device_get_transitional_reset:
1138 * check if the device's class is ready for multi-phase
1140 static ResettableTrFunction
device_get_transitional_reset(Object
*obj
)
1142 DeviceClass
*dc
= DEVICE_GET_CLASS(obj
);
1143 if (dc
->reset
!= device_phases_reset
) {
1145 * dc->reset has been overridden by a subclass,
1146 * the device is not ready for multi phase yet.
1148 return device_transitional_reset
;
1153 static void device_class_init(ObjectClass
*class, void *data
)
1155 DeviceClass
*dc
= DEVICE_CLASS(class);
1156 VMStateIfClass
*vc
= VMSTATE_IF_CLASS(class);
1157 ResettableClass
*rc
= RESETTABLE_CLASS(class);
1159 class->unparent
= device_unparent
;
1161 /* by default all devices were considered as hotpluggable,
1162 * so with intent to check it in generic qdev_unplug() /
1163 * device_set_realized() functions make every device
1164 * hotpluggable. Devices that shouldn't be hotpluggable,
1165 * should override it in their class_init()
1167 dc
->hotpluggable
= true;
1168 dc
->user_creatable
= true;
1169 vc
->get_id
= device_vmstate_if_get_id
;
1170 rc
->get_state
= device_get_reset_state
;
1171 rc
->child_foreach
= device_reset_child_foreach
;
1174 * @device_phases_reset is put as the default reset method below, allowing
1175 * to do the multi-phase transition from base classes to leaf classes. It
1176 * allows a legacy-reset Device class to extend a multi-phases-reset
1177 * Device class for the following reason:
1178 * + If a base class B has been moved to multi-phase, then it does not
1179 * override this default reset method and may have defined phase methods.
1180 * + A child class C (extending class B) which uses
1181 * device_class_set_parent_reset() (or similar means) to override the
1182 * reset method will still work as expected. @device_phases_reset function
1183 * will be registered as the parent reset method and effectively call
1184 * parent reset phases.
1186 dc
->reset
= device_phases_reset
;
1187 rc
->get_transitional_function
= device_get_transitional_reset
;
1189 object_class_property_add_bool(class, "realized",
1190 device_get_realized
, device_set_realized
);
1191 object_class_property_add_bool(class, "hotpluggable",
1192 device_get_hotpluggable
, NULL
);
1193 object_class_property_add_bool(class, "hotplugged",
1194 device_get_hotplugged
, NULL
);
1195 object_class_property_add_link(class, "parent_bus", TYPE_BUS
,
1196 offsetof(DeviceState
, parent_bus
), NULL
, 0);
1199 void device_class_set_props(DeviceClass
*dc
, Property
*props
)
1204 for (prop
= props
; prop
&& prop
->name
; prop
++) {
1205 qdev_class_add_legacy_property(dc
, prop
);
1206 qdev_class_add_property(dc
, prop
);
1210 void device_class_set_parent_reset(DeviceClass
*dc
,
1211 DeviceReset dev_reset
,
1212 DeviceReset
*parent_reset
)
1214 *parent_reset
= dc
->reset
;
1215 dc
->reset
= dev_reset
;
1218 void device_class_set_parent_realize(DeviceClass
*dc
,
1219 DeviceRealize dev_realize
,
1220 DeviceRealize
*parent_realize
)
1222 *parent_realize
= dc
->realize
;
1223 dc
->realize
= dev_realize
;
1226 void device_class_set_parent_unrealize(DeviceClass
*dc
,
1227 DeviceUnrealize dev_unrealize
,
1228 DeviceUnrealize
*parent_unrealize
)
1230 *parent_unrealize
= dc
->unrealize
;
1231 dc
->unrealize
= dev_unrealize
;
1234 void device_legacy_reset(DeviceState
*dev
)
1236 DeviceClass
*klass
= DEVICE_GET_CLASS(dev
);
1238 trace_qdev_reset(dev
, object_get_typename(OBJECT(dev
)));
1244 Object
*qdev_get_machine(void)
1249 dev
= container_get(object_get_root(), "/machine");
1255 static const TypeInfo device_type_info
= {
1256 .name
= TYPE_DEVICE
,
1257 .parent
= TYPE_OBJECT
,
1258 .instance_size
= sizeof(DeviceState
),
1259 .instance_init
= device_initfn
,
1260 .instance_post_init
= device_post_init
,
1261 .instance_finalize
= device_finalize
,
1262 .class_base_init
= device_class_base_init
,
1263 .class_init
= device_class_init
,
1265 .class_size
= sizeof(DeviceClass
),
1266 .interfaces
= (InterfaceInfo
[]) {
1267 { TYPE_VMSTATE_IF
},
1268 { TYPE_RESETTABLE_INTERFACE
},
1273 static void qdev_register_types(void)
1275 type_register_static(&device_type_info
);
1278 type_init(qdev_register_types
)