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, NULL);
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 */
94 0, /* return ownership on prop deletion */
98 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
100 BusState *old_parent_bus = dev->parent_bus;
102 if (old_parent_bus) {
103 trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)),
104 old_parent_bus, object_get_typename(OBJECT(old_parent_bus)),
105 OBJECT(bus), object_get_typename(OBJECT(bus)));
107 * Keep a reference to the device while it's not plugged into
108 * any bus, to avoid it potentially evaporating when it is
109 * dereffed in bus_remove_child().
110 * Also keep the ref of the parent bus until the end, so that
111 * we can safely call resettable_change_parent() below.
113 object_ref(OBJECT(dev));
114 bus_remove_child(dev->parent_bus, dev);
116 dev->parent_bus = bus;
117 object_ref(OBJECT(bus));
118 bus_add_child(bus, dev);
120 resettable_change_parent(OBJECT(dev), OBJECT(bus),
121 OBJECT(old_parent_bus));
123 if (old_parent_bus) {
124 object_unref(OBJECT(old_parent_bus));
125 object_unref(OBJECT(dev));
129 /* Create a new device. This only initializes the device state
130 structure and allows properties to be set. The device still needs
131 to be realized. See qdev-core.h. */
132 DeviceState *qdev_create(BusState *bus, const char *name)
136 dev = qdev_try_create(bus, name);
139 error_report("Unknown device '%s' for bus '%s'", name,
140 object_get_typename(OBJECT(bus)));
142 error_report("Unknown device '%s' for default sysbus", name);
150 DeviceState *qdev_try_create(BusState *bus, const char *type)
154 if (object_class_by_name(type) == NULL) {
157 dev = DEVICE(object_new(type));
163 /* Assert that the device really is a SysBusDevice before
164 * we put it onto the sysbus. Non-sysbus devices which aren't
165 * being put onto a bus should be created with object_new(TYPE_FOO),
166 * not qdev_create(NULL, TYPE_FOO).
168 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
169 bus = sysbus_get_default();
172 qdev_set_parent_bus(dev, bus);
173 object_unref(OBJECT(dev));
177 static QTAILQ_HEAD(, DeviceListener) device_listeners
178 = QTAILQ_HEAD_INITIALIZER(device_listeners);
180 enum ListenerDirection { Forward, Reverse };
182 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
184 DeviceListener *_listener; \
186 switch (_direction) { \
188 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
189 if (_listener->_callback) { \
190 _listener->_callback(_listener, ##_args); \
195 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
197 if (_listener->_callback) { \
198 _listener->_callback(_listener, ##_args); \
207 static int device_listener_add(DeviceState *dev, void *opaque)
209 DEVICE_LISTENER_CALL(realize, Forward, dev);
214 void device_listener_register(DeviceListener *listener)
216 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
218 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
222 void device_listener_unregister(DeviceListener *listener)
224 QTAILQ_REMOVE(&device_listeners, listener, link);
227 bool qdev_should_hide_device(QemuOpts *opts)
230 DeviceListener *listener;
232 QTAILQ_FOREACH(listener, &device_listeners, link) {
233 if (listener->should_be_hidden) {
235 * should_be_hidden_will return
236 * 1 if device matches opts and it should be hidden
237 * 0 if device matches opts and should not be hidden
238 * -1 if device doesn't match ops
240 rc = listener->should_be_hidden(listener, opts);
251 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
252 int required_for_version)
254 assert(!dev->realized);
255 dev->instance_id_alias = alias_id;
256 dev->alias_required_for_version = required_for_version;
259 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
261 MachineState *machine;
263 Object *m_obj = qdev_get_machine();
265 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
266 machine = MACHINE(m_obj);
267 mc = MACHINE_GET_CLASS(machine);
268 if (mc->get_hotplug_handler) {
269 return mc->get_hotplug_handler(machine, dev);
276 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
278 MachineState *machine;
280 Object *m_obj = qdev_get_machine();
282 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
283 machine = MACHINE(m_obj);
284 mc = MACHINE_GET_CLASS(machine);
285 if (mc->hotplug_allowed) {
286 return mc->hotplug_allowed(machine, dev, errp);
293 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
295 if (dev->parent_bus) {
296 return dev->parent_bus->hotplug_handler;
301 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
303 HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
305 if (hotplug_ctrl == NULL && dev->parent_bus) {
306 hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
311 static int qdev_prereset(DeviceState *dev, void *opaque)
313 trace_qdev_reset_tree(dev, object_get_typename(OBJECT(dev)));
317 static int qbus_prereset(BusState *bus, void *opaque)
319 trace_qbus_reset_tree(bus, object_get_typename(OBJECT(bus)));
323 static int qdev_reset_one(DeviceState *dev, void *opaque)
325 device_legacy_reset(dev);
330 static int qbus_reset_one(BusState *bus, void *opaque)
332 BusClass *bc = BUS_GET_CLASS(bus);
333 trace_qbus_reset(bus, object_get_typename(OBJECT(bus)));
340 void qdev_reset_all(DeviceState *dev)
342 trace_qdev_reset_all(dev, object_get_typename(OBJECT(dev)));
343 qdev_walk_children(dev, qdev_prereset, qbus_prereset,
344 qdev_reset_one, qbus_reset_one, NULL);
347 void qdev_reset_all_fn(void *opaque)
349 qdev_reset_all(DEVICE(opaque));
352 void qbus_reset_all(BusState *bus)
354 trace_qbus_reset_all(bus, object_get_typename(OBJECT(bus)));
355 qbus_walk_children(bus, qdev_prereset, qbus_prereset,
356 qdev_reset_one, qbus_reset_one, NULL);
359 void qbus_reset_all_fn(void *opaque)
361 BusState *bus = opaque;
365 void device_cold_reset(DeviceState *dev)
367 resettable_reset(OBJECT(dev), RESET_TYPE_COLD);
370 bool device_is_in_reset(DeviceState *dev)
372 return resettable_is_in_reset(OBJECT(dev));
375 static ResettableState *device_get_reset_state(Object *obj)
377 DeviceState *dev = DEVICE(obj);
381 static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
382 void *opaque, ResetType type)
384 DeviceState *dev = DEVICE(obj);
387 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
388 cb(OBJECT(bus), opaque, type);
392 /* can be used as ->unplug() callback for the simple cases */
393 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
394 DeviceState *dev, Error **errp)
396 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
401 * Device properties should be set before calling this function. IRQs
402 * and MMIO regions should be connected/mapped after calling this
404 * On failure, report an error with error_report() and terminate the
405 * program. This is okay during machine creation. Don't use for
406 * hotplug, because there callers need to recover from failure.
407 * Exception: if you know the device's init() callback can't fail,
408 * then qdev_init_nofail() can't fail either, and is therefore usable
409 * even then. But relying on the device implementation that way is
410 * somewhat unclean, and best avoided.
412 void qdev_init_nofail(DeviceState *dev)
416 assert(!dev->realized);
418 object_ref(OBJECT(dev));
419 object_property_set_bool(OBJECT(dev), true, "realized", &err);
421 error_reportf_err(err, "Initialization of device %s failed: ",
422 object_get_typename(OBJECT(dev)));
425 object_unref(OBJECT(dev));
428 void qdev_machine_creation_done(void)
431 * ok, initial machine setup is done, starting from now we can
432 * only create hotpluggable devices
437 bool qdev_machine_modified(void)
439 return qdev_hot_added || qdev_hot_removed;
442 BusState *qdev_get_parent_bus(DeviceState *dev)
444 return dev->parent_bus;
447 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
452 QLIST_FOREACH(ngl, &dev->gpios, node) {
453 /* NULL is a valid and matchable name. */
454 if (g_strcmp0(name, ngl->name) == 0) {
459 ngl = g_malloc0(sizeof(*ngl));
460 ngl->name = g_strdup(name);
461 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
465 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
466 qemu_irq_handler handler,
468 const char *name, int n)
471 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
473 assert(gpio_list->num_out == 0 || !name);
474 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
478 name = "unnamed-gpio-in";
480 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
481 gchar *propname = g_strdup_printf("%s[%u]", name, i);
483 object_property_add_child(OBJECT(dev), propname,
484 OBJECT(gpio_list->in[i]), &error_abort);
488 gpio_list->num_in += n;
491 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
493 qdev_init_gpio_in_named(dev, handler, NULL, n);
496 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
497 const char *name, int n)
500 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
502 assert(gpio_list->num_in == 0 || !name);
505 name = "unnamed-gpio-out";
507 memset(pins, 0, sizeof(*pins) * n);
508 for (i = 0; i < n; ++i) {
509 gchar *propname = g_strdup_printf("%s[%u]", name,
510 gpio_list->num_out + i);
512 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
514 object_property_allow_set_link,
515 OBJ_PROP_LINK_STRONG,
519 gpio_list->num_out += n;
522 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
524 qdev_init_gpio_out_named(dev, pins, NULL, n);
527 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
529 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
531 assert(n >= 0 && n < gpio_list->num_in);
532 return gpio_list->in[n];
535 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
537 return qdev_get_gpio_in_named(dev, NULL, n);
540 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
543 char *propname = g_strdup_printf("%s[%d]",
544 name ? name : "unnamed-gpio-out", n);
546 /* We need a name for object_property_set_link to work. If the
547 * object has a parent, object_property_add_child will come back
548 * with an error without doing anything. If it has none, it will
549 * never fail. So we can just call it with a NULL Error pointer.
551 object_property_add_child(container_get(qdev_get_machine(),
553 "non-qdev-gpio[*]", OBJECT(pin), NULL);
555 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
559 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
561 g_autofree char *propname = g_strdup_printf("%s[%d]",
562 name ? name : "unnamed-gpio-out", n);
564 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
570 /* disconnect a GPIO output, returning the disconnected input (if any) */
572 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
573 const char *name, int n)
575 char *propname = g_strdup_printf("%s[%d]",
576 name ? name : "unnamed-gpio-out", n);
578 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
581 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
587 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
588 const char *name, int n)
590 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
591 qdev_connect_gpio_out_named(dev, name, n, icpt);
595 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
597 qdev_connect_gpio_out_named(dev, NULL, n, pin);
600 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
604 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
606 for (i = 0; i < ngl->num_in; i++) {
607 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
608 char *propname = g_strdup_printf("%s[%d]", nm, i);
610 object_property_add_alias(OBJECT(container), propname,
611 OBJECT(dev), propname,
615 for (i = 0; i < ngl->num_out; i++) {
616 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
617 char *propname = g_strdup_printf("%s[%d]", nm, i);
619 object_property_add_alias(OBJECT(container), propname,
620 OBJECT(dev), propname,
624 QLIST_REMOVE(ngl, node);
625 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
628 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
631 Object *child = object_resolve_path_component(OBJECT(dev), name);
633 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
638 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
639 if (strcmp(name, bus->name) == 0) {
646 int qdev_walk_children(DeviceState *dev,
647 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
648 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
655 err = pre_devfn(dev, opaque);
661 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
662 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
663 post_devfn, post_busfn, opaque);
670 err = post_devfn(dev, opaque);
679 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
685 QTAILQ_FOREACH(kid, &bus->children, sibling) {
686 DeviceState *dev = kid->child;
688 if (dev->id && strcmp(dev->id, id) == 0) {
692 QLIST_FOREACH(child, &dev->child_bus, sibling) {
693 ret = qdev_find_recursive(child, id);
702 char *qdev_get_dev_path(DeviceState *dev)
706 if (!dev || !dev->parent_bus) {
710 bc = BUS_GET_CLASS(dev->parent_bus);
711 if (bc->get_dev_path) {
712 return bc->get_dev_path(dev);
719 * Legacy property handling
722 static void qdev_get_legacy_property(Object *obj, Visitor *v,
723 const char *name, void *opaque,
726 DeviceState *dev = DEVICE(obj);
727 Property *prop = opaque;
732 prop->info->print(dev, prop, buffer, sizeof(buffer));
733 visit_type_str(v, name, &ptr, errp);
737 * qdev_class_add_legacy_property:
738 * @dev: Device to add the property to.
739 * @prop: The qdev property definition.
741 * Add a legacy QOM property to @dev for qdev property @prop.
743 * Legacy properties are string versions of QOM properties. The format of
744 * the string depends on the property type. Legacy properties are only
745 * needed for "info qtree".
747 * Do not use this in new code! QOM Properties added through this interface
748 * will be given names in the "legacy" namespace.
750 static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop)
752 g_autofree char *name = NULL;
754 /* Register pointer properties as legacy properties */
755 if (!prop->info->print && prop->info->get) {
759 name = g_strdup_printf("legacy-%s", prop->name);
760 object_class_property_add(OBJECT_CLASS(dc), name, "str",
761 prop->info->print ? qdev_get_legacy_property : prop->info->get,
762 NULL, NULL, prop, &error_abort);
765 void qdev_property_add_static(DeviceState *dev, Property *prop)
767 Object *obj = OBJECT(dev);
770 assert(!prop->info->create);
772 op = object_property_add(obj, prop->name, prop->info->name,
773 prop->info->get, prop->info->set,
777 object_property_set_description(obj, prop->name,
778 prop->info->description,
781 if (prop->set_default) {
782 prop->info->set_default_value(op, prop);
789 static void qdev_class_add_property(DeviceClass *klass, Property *prop)
791 ObjectClass *oc = OBJECT_CLASS(klass);
793 if (prop->info->create) {
794 prop->info->create(oc, prop, &error_abort);
798 op = object_class_property_add(oc,
799 prop->name, prop->info->name,
800 prop->info->get, prop->info->set,
803 if (prop->set_default) {
804 prop->info->set_default_value(op, prop);
807 object_class_property_set_description(oc, prop->name,
808 prop->info->description,
812 /* @qdev_alias_all_properties - Add alias properties to the source object for
813 * all qdev properties on the target DeviceState.
815 void qdev_alias_all_properties(DeviceState *target, Object *source)
820 class = object_get_class(OBJECT(target));
822 DeviceClass *dc = DEVICE_CLASS(class);
824 for (prop = dc->props_; prop && prop->name; prop++) {
825 object_property_add_alias(source, prop->name,
826 OBJECT(target), prop->name,
829 class = object_class_get_parent(class);
830 } while (class != object_class_by_name(TYPE_DEVICE));
833 static bool device_get_realized(Object *obj, Error **errp)
835 DeviceState *dev = DEVICE(obj);
836 return dev->realized;
839 static bool check_only_migratable(Object *obj, Error **errp)
841 DeviceClass *dc = DEVICE_GET_CLASS(obj);
843 if (!vmstate_check_only_migratable(dc->vmsd)) {
844 error_setg(errp, "Device %s is not migratable, but "
845 "--only-migratable was specified",
846 object_get_typename(obj));
853 static void device_set_realized(Object *obj, bool value, Error **errp)
855 DeviceState *dev = DEVICE(obj);
856 DeviceClass *dc = DEVICE_GET_CLASS(dev);
857 HotplugHandler *hotplug_ctrl;
860 Error *local_err = NULL;
861 bool unattached_parent = false;
862 static int unattached_count;
864 if (dev->hotplugged && !dc->hotpluggable) {
865 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
869 if (value && !dev->realized) {
870 if (!check_only_migratable(obj, &local_err)) {
875 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
877 object_property_add_child(container_get(qdev_get_machine(),
879 name, obj, &error_abort);
880 unattached_parent = true;
884 hotplug_ctrl = qdev_get_hotplug_handler(dev);
886 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
887 if (local_err != NULL) {
893 dc->realize(dev, &local_err);
894 if (local_err != NULL) {
899 DEVICE_LISTENER_CALL(realize, Forward, dev);
902 * always free/re-initialize here since the value cannot be cleaned up
903 * in device_unrealize due to its usage later on in the unplug path
905 g_free(dev->canonical_path);
906 dev->canonical_path = object_get_canonical_path(OBJECT(dev));
907 QLIST_FOREACH(ncl, &dev->clocks, node) {
911 clock_setup_canonical_path(ncl->clock);
915 if (qdev_get_vmsd(dev)) {
916 if (vmstate_register_with_alias_id(VMSTATE_IF(dev),
917 VMSTATE_INSTANCE_ID_ANY,
918 qdev_get_vmsd(dev), dev,
919 dev->instance_id_alias,
920 dev->alias_required_for_version,
922 goto post_realize_fail;
927 * Clear the reset state, in case the object was previously unrealized
928 * with a dirty state.
930 resettable_state_clear(&dev->reset);
932 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
933 object_property_set_bool(OBJECT(bus), true, "realized",
935 if (local_err != NULL) {
936 goto child_realize_fail;
939 if (dev->hotplugged) {
941 * Reset the device, as well as its subtree which, at this point,
942 * should be realized too.
944 resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD);
945 resettable_change_parent(OBJECT(dev), OBJECT(dev->parent_bus),
947 resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD);
949 dev->pending_deleted_event = false;
952 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
953 if (local_err != NULL) {
954 goto child_realize_fail;
958 } else if (!value && dev->realized) {
959 /* We want local_err to track only the first error */
960 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
961 object_property_set_bool(OBJECT(bus), false, "realized",
962 local_err ? NULL : &local_err);
964 if (qdev_get_vmsd(dev)) {
965 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
968 dc->unrealize(dev, local_err ? NULL : &local_err);
970 dev->pending_deleted_event = true;
971 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
973 if (local_err != NULL) {
978 assert(local_err == NULL);
979 dev->realized = value;
983 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
984 object_property_set_bool(OBJECT(bus), false, "realized",
988 if (qdev_get_vmsd(dev)) {
989 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
993 g_free(dev->canonical_path);
994 dev->canonical_path = NULL;
996 dc->unrealize(dev, NULL);
1000 error_propagate(errp, local_err);
1001 if (unattached_parent) {
1002 object_unparent(OBJECT(dev));
1007 static bool device_get_hotpluggable(Object *obj, Error **errp)
1009 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1010 DeviceState *dev = DEVICE(obj);
1012 return dc->hotpluggable && (dev->parent_bus == NULL ||
1013 qbus_is_hotpluggable(dev->parent_bus));
1016 static bool device_get_hotplugged(Object *obj, Error **errp)
1018 DeviceState *dev = DEVICE(obj);
1020 return dev->hotplugged;
1023 static void device_initfn(Object *obj)
1025 DeviceState *dev = DEVICE(obj);
1028 dev->hotplugged = 1;
1029 qdev_hot_added = true;
1032 dev->instance_id_alias = -1;
1033 dev->realized = false;
1034 dev->allow_unplug_during_migration = false;
1036 QLIST_INIT(&dev->gpios);
1037 QLIST_INIT(&dev->clocks);
1040 static void device_post_init(Object *obj)
1043 * Note: ordered so that the user's global properties take
1046 object_apply_compat_props(obj);
1047 qdev_prop_set_globals(DEVICE(obj));
1050 /* Unlink device from bus and free the structure. */
1051 static void device_finalize(Object *obj)
1053 NamedGPIOList *ngl, *next;
1055 DeviceState *dev = DEVICE(obj);
1057 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1058 QLIST_REMOVE(ngl, node);
1059 qemu_free_irqs(ngl->in, ngl->num_in);
1062 /* ngl->out irqs are owned by the other end and should not be freed
1067 qdev_finalize_clocklist(dev);
1069 /* Only send event if the device had been completely realized */
1070 if (dev->pending_deleted_event) {
1071 g_assert(dev->canonical_path);
1073 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
1074 g_free(dev->canonical_path);
1075 dev->canonical_path = NULL;
1078 qemu_opts_del(dev->opts);
1081 static void device_class_base_init(ObjectClass *class, void *data)
1083 DeviceClass *klass = DEVICE_CLASS(class);
1085 /* We explicitly look up properties in the superclasses,
1086 * so do not propagate them to the subclasses.
1088 klass->props_ = NULL;
1091 static void device_unparent(Object *obj)
1093 DeviceState *dev = DEVICE(obj);
1096 if (dev->realized) {
1097 object_property_set_bool(obj, false, "realized", NULL);
1099 while (dev->num_child_bus) {
1100 bus = QLIST_FIRST(&dev->child_bus);
1101 object_unparent(OBJECT(bus));
1103 if (dev->parent_bus) {
1104 bus_remove_child(dev->parent_bus, dev);
1105 object_unref(OBJECT(dev->parent_bus));
1106 dev->parent_bus = NULL;
1111 device_vmstate_if_get_id(VMStateIf *obj)
1113 DeviceState *dev = DEVICE(obj);
1115 return qdev_get_dev_path(dev);
1119 * device_phases_reset:
1120 * Transition reset method for devices to allow moving
1121 * smoothly from legacy reset method to multi-phases
1123 static void device_phases_reset(DeviceState *dev)
1125 ResettableClass *rc = RESETTABLE_GET_CLASS(dev);
1127 if (rc->phases.enter) {
1128 rc->phases.enter(OBJECT(dev), RESET_TYPE_COLD);
1130 if (rc->phases.hold) {
1131 rc->phases.hold(OBJECT(dev));
1133 if (rc->phases.exit) {
1134 rc->phases.exit(OBJECT(dev));
1138 static void device_transitional_reset(Object *obj)
1140 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1143 * This will call either @device_phases_reset (for multi-phases transitioned
1144 * devices) or a device's specific method for not-yet transitioned devices.
1145 * In both case, it does not reset children.
1148 dc->reset(DEVICE(obj));
1153 * device_get_transitional_reset:
1154 * check if the device's class is ready for multi-phase
1156 static ResettableTrFunction device_get_transitional_reset(Object *obj)
1158 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1159 if (dc->reset != device_phases_reset) {
1161 * dc->reset has been overridden by a subclass,
1162 * the device is not ready for multi phase yet.
1164 return device_transitional_reset;
1169 static void device_class_init(ObjectClass *class, void *data)
1171 DeviceClass *dc = DEVICE_CLASS(class);
1172 VMStateIfClass *vc = VMSTATE_IF_CLASS(class);
1173 ResettableClass *rc = RESETTABLE_CLASS(class);
1175 class->unparent = device_unparent;
1177 /* by default all devices were considered as hotpluggable,
1178 * so with intent to check it in generic qdev_unplug() /
1179 * device_set_realized() functions make every device
1180 * hotpluggable. Devices that shouldn't be hotpluggable,
1181 * should override it in their class_init()
1183 dc->hotpluggable = true;
1184 dc->user_creatable = true;
1185 vc->get_id = device_vmstate_if_get_id;
1186 rc->get_state = device_get_reset_state;
1187 rc->child_foreach = device_reset_child_foreach;
1190 * @device_phases_reset is put as the default reset method below, allowing
1191 * to do the multi-phase transition from base classes to leaf classes. It
1192 * allows a legacy-reset Device class to extend a multi-phases-reset
1193 * Device class for the following reason:
1194 * + If a base class B has been moved to multi-phase, then it does not
1195 * override this default reset method and may have defined phase methods.
1196 * + A child class C (extending class B) which uses
1197 * device_class_set_parent_reset() (or similar means) to override the
1198 * reset method will still work as expected. @device_phases_reset function
1199 * will be registered as the parent reset method and effectively call
1200 * parent reset phases.
1202 dc->reset = device_phases_reset;
1203 rc->get_transitional_function = device_get_transitional_reset;
1205 object_class_property_add_bool(class, "realized",
1206 device_get_realized, device_set_realized,
1208 object_class_property_add_bool(class, "hotpluggable",
1209 device_get_hotpluggable, NULL,
1211 object_class_property_add_bool(class, "hotplugged",
1212 device_get_hotplugged, NULL,
1214 object_class_property_add_link(class, "parent_bus", TYPE_BUS,
1215 offsetof(DeviceState, parent_bus), NULL, 0,
1219 void device_class_set_props(DeviceClass *dc, Property *props)
1224 for (prop = props; prop && prop->name; prop++) {
1225 qdev_class_add_legacy_property(dc, prop);
1226 qdev_class_add_property(dc, prop);
1230 void device_class_set_parent_reset(DeviceClass *dc,
1231 DeviceReset dev_reset,
1232 DeviceReset *parent_reset)
1234 *parent_reset = dc->reset;
1235 dc->reset = dev_reset;
1238 void device_class_set_parent_realize(DeviceClass *dc,
1239 DeviceRealize dev_realize,
1240 DeviceRealize *parent_realize)
1242 *parent_realize = dc->realize;
1243 dc->realize = dev_realize;
1246 void device_class_set_parent_unrealize(DeviceClass *dc,
1247 DeviceUnrealize dev_unrealize,
1248 DeviceUnrealize *parent_unrealize)
1250 *parent_unrealize = dc->unrealize;
1251 dc->unrealize = dev_unrealize;
1254 void device_legacy_reset(DeviceState *dev)
1256 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1258 trace_qdev_reset(dev, object_get_typename(OBJECT(dev)));
1264 Object *qdev_get_machine(void)
1269 dev = container_get(object_get_root(), "/machine");
1275 static const TypeInfo device_type_info = {
1276 .name = TYPE_DEVICE,
1277 .parent = TYPE_OBJECT,
1278 .instance_size = sizeof(DeviceState),
1279 .instance_init = device_initfn,
1280 .instance_post_init = device_post_init,
1281 .instance_finalize = device_finalize,
1282 .class_base_init = device_class_base_init,
1283 .class_init = device_class_init,
1285 .class_size = sizeof(DeviceClass),
1286 .interfaces = (InterfaceInfo[]) {
1287 { TYPE_VMSTATE_IF },
1288 { TYPE_RESETTABLE_INTERFACE },
1293 static void qdev_register_types(void)
1295 type_register_static(&device_type_info);
1298 type_init(qdev_register_types)