2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 /* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
28 #include "qemu/osdep.h"
30 #include "hw/fw-path-provider.h"
31 #include "sysemu/sysemu.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu/error-report.h"
36 #include "hw/hotplug.h"
37 #include "hw/boards.h"
38 #include "hw/sysbus.h"
39 #include "qapi-event.h"
42 static bool qdev_hot_added = false;
43 static bool qdev_hot_removed = false;
45 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
47 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51 const char *qdev_fw_name(DeviceState *dev)
53 DeviceClass *dc = DEVICE_GET_CLASS(dev);
59 return object_get_typename(OBJECT(dev));
62 static void bus_remove_child(BusState *bus, DeviceState *child)
66 QTAILQ_FOREACH(kid, &bus->children, sibling) {
67 if (kid->child == child) {
70 snprintf(name, sizeof(name), "child[%d]", kid->index);
71 QTAILQ_REMOVE(&bus->children, kid, sibling);
73 /* This gives back ownership of kid->child back to us. */
74 object_property_del(OBJECT(bus), name, NULL);
75 object_unref(OBJECT(kid->child));
82 static void bus_add_child(BusState *bus, DeviceState *child)
85 BusChild *kid = g_malloc0(sizeof(*kid));
87 kid->index = bus->max_index++;
89 object_ref(OBJECT(kid->child));
91 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
93 /* This transfers ownership of kid->child to the property. */
94 snprintf(name, sizeof(name), "child[%d]", kid->index);
95 object_property_add_link(OBJECT(bus), name,
96 object_get_typename(OBJECT(child)),
97 (Object **)&kid->child,
98 NULL, /* read-only property */
99 0, /* return ownership on prop deletion */
103 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
105 bool replugging = dev->parent_bus != NULL;
108 /* Keep a reference to the device while it's not plugged into
109 * any bus, to avoid it potentially evaporating when it is
110 * dereffed in bus_remove_child().
112 object_ref(OBJECT(dev));
113 bus_remove_child(dev->parent_bus, dev);
114 object_unref(OBJECT(dev->parent_bus));
116 dev->parent_bus = bus;
117 object_ref(OBJECT(bus));
118 bus_add_child(bus, dev);
120 object_unref(OBJECT(dev));
124 /* Create a new device. This only initializes the device state
125 structure and allows properties to be set. The device still needs
126 to be realized. See qdev-core.h. */
127 DeviceState *qdev_create(BusState *bus, const char *name)
131 dev = qdev_try_create(bus, name);
134 error_report("Unknown device '%s' for bus '%s'", name,
135 object_get_typename(OBJECT(bus)));
137 error_report("Unknown device '%s' for default sysbus", name);
145 DeviceState *qdev_try_create(BusState *bus, const char *type)
149 if (object_class_by_name(type) == NULL) {
152 dev = DEVICE(object_new(type));
158 /* Assert that the device really is a SysBusDevice before
159 * we put it onto the sysbus. Non-sysbus devices which aren't
160 * being put onto a bus should be created with object_new(TYPE_FOO),
161 * not qdev_create(NULL, TYPE_FOO).
163 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
164 bus = sysbus_get_default();
167 qdev_set_parent_bus(dev, bus);
168 object_unref(OBJECT(dev));
172 static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
173 = QTAILQ_HEAD_INITIALIZER(device_listeners);
175 enum ListenerDirection { Forward, Reverse };
177 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
179 DeviceListener *_listener; \
181 switch (_direction) { \
183 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
184 if (_listener->_callback) { \
185 _listener->_callback(_listener, ##_args); \
190 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
191 device_listeners, link) { \
192 if (_listener->_callback) { \
193 _listener->_callback(_listener, ##_args); \
202 static int device_listener_add(DeviceState *dev, void *opaque)
204 DEVICE_LISTENER_CALL(realize, Forward, dev);
209 void device_listener_register(DeviceListener *listener)
211 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
213 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
217 void device_listener_unregister(DeviceListener *listener)
219 QTAILQ_REMOVE(&device_listeners, listener, link);
222 static void device_realize(DeviceState *dev, Error **errp)
224 DeviceClass *dc = DEVICE_GET_CLASS(dev);
227 int rc = dc->init(dev);
229 error_setg(errp, "Device initialization failed.");
235 static void device_unrealize(DeviceState *dev, Error **errp)
237 DeviceClass *dc = DEVICE_GET_CLASS(dev);
240 int rc = dc->exit(dev);
242 error_setg(errp, "Device exit failed.");
248 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
249 int required_for_version)
251 assert(!dev->realized);
252 dev->instance_id_alias = alias_id;
253 dev->alias_required_for_version = required_for_version;
256 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
258 HotplugHandler *hotplug_ctrl = NULL;
260 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
261 hotplug_ctrl = dev->parent_bus->hotplug_handler;
262 } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
263 MachineState *machine = MACHINE(qdev_get_machine());
264 MachineClass *mc = MACHINE_GET_CLASS(machine);
266 if (mc->get_hotplug_handler) {
267 hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
273 void qdev_unplug(DeviceState *dev, Error **errp)
275 DeviceClass *dc = DEVICE_GET_CLASS(dev);
276 HotplugHandler *hotplug_ctrl;
277 HotplugHandlerClass *hdc;
279 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
280 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
284 if (!dc->hotpluggable) {
285 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
286 object_get_typename(OBJECT(dev)));
290 qdev_hot_removed = true;
292 hotplug_ctrl = qdev_get_hotplug_handler(dev);
293 /* hotpluggable device MUST have HotplugHandler, if it doesn't
294 * then something is very wrong with it */
295 g_assert(hotplug_ctrl);
297 /* If device supports async unplug just request it to be done,
298 * otherwise just remove it synchronously */
299 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
300 if (hdc->unplug_request) {
301 hotplug_handler_unplug_request(hotplug_ctrl, dev, errp);
303 hotplug_handler_unplug(hotplug_ctrl, dev, errp);
307 static int qdev_reset_one(DeviceState *dev, void *opaque)
314 static int qbus_reset_one(BusState *bus, void *opaque)
316 BusClass *bc = BUS_GET_CLASS(bus);
323 void qdev_reset_all(DeviceState *dev)
325 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
328 void qdev_reset_all_fn(void *opaque)
330 qdev_reset_all(DEVICE(opaque));
333 void qbus_reset_all(BusState *bus)
335 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
338 void qbus_reset_all_fn(void *opaque)
340 BusState *bus = opaque;
344 /* can be used as ->unplug() callback for the simple cases */
345 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
346 DeviceState *dev, Error **errp)
349 object_unparent(OBJECT(dev));
354 * Device properties should be set before calling this function. IRQs
355 * and MMIO regions should be connected/mapped after calling this
357 * On failure, report an error with error_report() and terminate the
358 * program. This is okay during machine creation. Don't use for
359 * hotplug, because there callers need to recover from failure.
360 * Exception: if you know the device's init() callback can't fail,
361 * then qdev_init_nofail() can't fail either, and is therefore usable
362 * even then. But relying on the device implementation that way is
363 * somewhat unclean, and best avoided.
365 void qdev_init_nofail(DeviceState *dev)
369 assert(!dev->realized);
371 object_ref(OBJECT(dev));
372 object_property_set_bool(OBJECT(dev), true, "realized", &err);
374 error_reportf_err(err, "Initialization of device %s failed: ",
375 object_get_typename(OBJECT(dev)));
378 object_unref(OBJECT(dev));
381 void qdev_machine_creation_done(void)
384 * ok, initial machine setup is done, starting from now we can
385 * only create hotpluggable devices
390 bool qdev_machine_modified(void)
392 return qdev_hot_added || qdev_hot_removed;
395 BusState *qdev_get_parent_bus(DeviceState *dev)
397 return dev->parent_bus;
400 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
405 QLIST_FOREACH(ngl, &dev->gpios, node) {
406 /* NULL is a valid and matchable name, otherwise do a normal
409 if ((!ngl->name && !name) ||
410 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
415 ngl = g_malloc0(sizeof(*ngl));
416 ngl->name = g_strdup(name);
417 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
421 void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
422 const char *name, int n)
425 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
427 assert(gpio_list->num_out == 0 || !name);
428 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
432 name = "unnamed-gpio-in";
434 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
435 gchar *propname = g_strdup_printf("%s[%u]", name, i);
437 object_property_add_child(OBJECT(dev), propname,
438 OBJECT(gpio_list->in[i]), &error_abort);
442 gpio_list->num_in += n;
445 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
447 qdev_init_gpio_in_named(dev, handler, NULL, n);
450 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
451 const char *name, int n)
454 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
456 assert(gpio_list->num_in == 0 || !name);
459 name = "unnamed-gpio-out";
461 memset(pins, 0, sizeof(*pins) * n);
462 for (i = 0; i < n; ++i) {
463 gchar *propname = g_strdup_printf("%s[%u]", name,
464 gpio_list->num_out + i);
466 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
468 object_property_allow_set_link,
469 OBJ_PROP_LINK_UNREF_ON_RELEASE,
473 gpio_list->num_out += n;
476 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
478 qdev_init_gpio_out_named(dev, pins, NULL, n);
481 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
483 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
485 assert(n >= 0 && n < gpio_list->num_in);
486 return gpio_list->in[n];
489 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
491 return qdev_get_gpio_in_named(dev, NULL, n);
494 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
497 char *propname = g_strdup_printf("%s[%d]",
498 name ? name : "unnamed-gpio-out", n);
500 /* We need a name for object_property_set_link to work. If the
501 * object has a parent, object_property_add_child will come back
502 * with an error without doing anything. If it has none, it will
503 * never fail. So we can just call it with a NULL Error pointer.
505 object_property_add_child(container_get(qdev_get_machine(),
507 "non-qdev-gpio[*]", OBJECT(pin), NULL);
509 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
513 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
515 char *propname = g_strdup_printf("%s[%d]",
516 name ? name : "unnamed-gpio-out", n);
518 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
524 /* disconnect a GPIO output, returning the disconnected input (if any) */
526 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
527 const char *name, int n)
529 char *propname = g_strdup_printf("%s[%d]",
530 name ? name : "unnamed-gpio-out", n);
532 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
535 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
541 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
542 const char *name, int n)
544 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
545 qdev_connect_gpio_out_named(dev, name, n, icpt);
549 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
551 qdev_connect_gpio_out_named(dev, NULL, n, pin);
554 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
558 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
560 for (i = 0; i < ngl->num_in; i++) {
561 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
562 char *propname = g_strdup_printf("%s[%d]", nm, i);
564 object_property_add_alias(OBJECT(container), propname,
565 OBJECT(dev), propname,
569 for (i = 0; i < ngl->num_out; i++) {
570 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
571 char *propname = g_strdup_printf("%s[%d]", nm, i);
573 object_property_add_alias(OBJECT(container), propname,
574 OBJECT(dev), propname,
578 QLIST_REMOVE(ngl, node);
579 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
582 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
585 Object *child = object_resolve_path_component(OBJECT(dev), name);
587 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
592 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
593 if (strcmp(name, bus->name) == 0) {
600 int qdev_walk_children(DeviceState *dev,
601 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
602 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
609 err = pre_devfn(dev, opaque);
615 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
616 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
617 post_devfn, post_busfn, opaque);
624 err = post_devfn(dev, opaque);
633 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
639 QTAILQ_FOREACH(kid, &bus->children, sibling) {
640 DeviceState *dev = kid->child;
642 if (dev->id && strcmp(dev->id, id) == 0) {
646 QLIST_FOREACH(child, &dev->child_bus, sibling) {
647 ret = qdev_find_recursive(child, id);
656 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
658 BusClass *bc = BUS_GET_CLASS(bus);
660 if (bc->get_fw_dev_path) {
661 return bc->get_fw_dev_path(dev);
667 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
669 Object *obj = OBJECT(dev);
672 while (!d && obj->parent) {
674 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
679 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
681 Object *obj = OBJECT(dev);
683 return fw_path_provider_try_get_dev_path(obj, bus, dev);
686 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
690 if (dev && dev->parent_bus) {
692 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
693 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
695 d = bus_get_fw_dev_path(dev->parent_bus, dev);
698 l += snprintf(p + l, size - l, "%s", d);
704 l += snprintf(p + l , size - l, "/");
709 char* qdev_get_fw_dev_path(DeviceState *dev)
714 l = qdev_get_fw_dev_path_helper(dev, path, 128);
718 return g_strdup(path);
721 char *qdev_get_dev_path(DeviceState *dev)
725 if (!dev || !dev->parent_bus) {
729 bc = BUS_GET_CLASS(dev->parent_bus);
730 if (bc->get_dev_path) {
731 return bc->get_dev_path(dev);
738 * Legacy property handling
741 static void qdev_get_legacy_property(Object *obj, Visitor *v,
742 const char *name, void *opaque,
745 DeviceState *dev = DEVICE(obj);
746 Property *prop = opaque;
751 prop->info->print(dev, prop, buffer, sizeof(buffer));
752 visit_type_str(v, name, &ptr, errp);
756 * qdev_property_add_legacy:
757 * @dev: Device to add the property to.
758 * @prop: The qdev property definition.
759 * @errp: location to store error information.
761 * Add a legacy QOM property to @dev for qdev property @prop.
762 * On error, store error in @errp.
764 * Legacy properties are string versions of QOM properties. The format of
765 * the string depends on the property type. Legacy properties are only
766 * needed for "info qtree".
768 * Do not use this is new code! QOM Properties added through this interface
769 * will be given names in the "legacy" namespace.
771 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
776 /* Register pointer properties as legacy properties */
777 if (!prop->info->print && prop->info->get) {
781 name = g_strdup_printf("legacy-%s", prop->name);
782 object_property_add(OBJECT(dev), name, "str",
783 prop->info->print ? qdev_get_legacy_property : prop->info->get,
792 * qdev_property_add_static:
793 * @dev: Device to add the property to.
794 * @prop: The qdev property definition.
795 * @errp: location to store error information.
797 * Add a static QOM property to @dev for qdev property @prop.
798 * On error, store error in @errp. Static properties access data in a struct.
799 * The type of the QOM property is derived from prop->info.
801 void qdev_property_add_static(DeviceState *dev, Property *prop,
804 Error *local_err = NULL;
805 Object *obj = OBJECT(dev);
808 * TODO qdev_prop_ptr does not have getters or setters. It must
809 * go now that it can be replaced with links. The test should be
810 * removed along with it: all static properties are read/write.
812 if (!prop->info->get && !prop->info->set) {
816 object_property_add(obj, prop->name, prop->info->name,
817 prop->info->get, prop->info->set,
822 error_propagate(errp, local_err);
826 object_property_set_description(obj, prop->name,
827 prop->info->description,
830 if (prop->qtype == QTYPE_NONE) {
834 if (prop->qtype == QTYPE_QBOOL) {
835 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
836 } else if (prop->info->enum_table) {
837 object_property_set_str(obj, prop->info->enum_table[prop->defval],
838 prop->name, &error_abort);
839 } else if (prop->qtype == QTYPE_QINT) {
840 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
844 /* @qdev_alias_all_properties - Add alias properties to the source object for
845 * all qdev properties on the target DeviceState.
847 void qdev_alias_all_properties(DeviceState *target, Object *source)
852 class = object_get_class(OBJECT(target));
854 DeviceClass *dc = DEVICE_CLASS(class);
856 for (prop = dc->props; prop && prop->name; prop++) {
857 object_property_add_alias(source, prop->name,
858 OBJECT(target), prop->name,
861 class = object_class_get_parent(class);
862 } while (class != object_class_by_name(TYPE_DEVICE));
865 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
867 GSList **list = opaque;
868 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
875 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
876 *list = g_slist_append(*list, dev);
882 GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
886 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
891 static bool device_get_realized(Object *obj, Error **errp)
893 DeviceState *dev = DEVICE(obj);
894 return dev->realized;
897 static void device_set_realized(Object *obj, bool value, Error **errp)
899 DeviceState *dev = DEVICE(obj);
900 DeviceClass *dc = DEVICE_GET_CLASS(dev);
901 HotplugHandler *hotplug_ctrl;
903 Error *local_err = NULL;
904 bool unattached_parent = false;
905 static int unattached_count;
907 if (dev->hotplugged && !dc->hotpluggable) {
908 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
912 if (value && !dev->realized) {
914 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
916 object_property_add_child(container_get(qdev_get_machine(),
918 name, obj, &error_abort);
919 unattached_parent = true;
923 hotplug_ctrl = qdev_get_hotplug_handler(dev);
925 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
926 if (local_err != NULL) {
932 dc->realize(dev, &local_err);
935 if (local_err != NULL) {
939 DEVICE_LISTENER_CALL(realize, Forward, dev);
942 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
945 if (local_err != NULL) {
946 goto post_realize_fail;
949 if (qdev_get_vmsd(dev)) {
950 if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
951 dev->instance_id_alias,
952 dev->alias_required_for_version,
954 goto post_realize_fail;
958 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
959 object_property_set_bool(OBJECT(bus), true, "realized",
961 if (local_err != NULL) {
962 goto child_realize_fail;
965 if (dev->hotplugged) {
968 dev->pending_deleted_event = false;
969 } else if (!value && dev->realized) {
970 Error **local_errp = NULL;
971 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
972 local_errp = local_err ? NULL : &local_err;
973 object_property_set_bool(OBJECT(bus), false, "realized",
976 if (qdev_get_vmsd(dev)) {
977 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
980 local_errp = local_err ? NULL : &local_err;
981 dc->unrealize(dev, local_errp);
983 dev->pending_deleted_event = true;
984 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
987 if (local_err != NULL) {
991 dev->realized = value;
995 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
996 object_property_set_bool(OBJECT(bus), false, "realized",
1000 if (qdev_get_vmsd(dev)) {
1001 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
1005 if (dc->unrealize) {
1006 dc->unrealize(dev, NULL);
1010 error_propagate(errp, local_err);
1011 if (unattached_parent) {
1012 object_unparent(OBJECT(dev));
1017 static bool device_get_hotpluggable(Object *obj, Error **errp)
1019 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1020 DeviceState *dev = DEVICE(obj);
1022 return dc->hotpluggable && (dev->parent_bus == NULL ||
1023 qbus_is_hotpluggable(dev->parent_bus));
1026 static bool device_get_hotplugged(Object *obj, Error **err)
1028 DeviceState *dev = DEVICE(obj);
1030 return dev->hotplugged;
1033 static void device_set_hotplugged(Object *obj, bool value, Error **err)
1035 DeviceState *dev = DEVICE(obj);
1037 dev->hotplugged = value;
1040 static void device_initfn(Object *obj)
1042 DeviceState *dev = DEVICE(obj);
1047 dev->hotplugged = 1;
1048 qdev_hot_added = true;
1051 dev->instance_id_alias = -1;
1052 dev->realized = false;
1054 object_property_add_bool(obj, "realized",
1055 device_get_realized, device_set_realized, NULL);
1056 object_property_add_bool(obj, "hotpluggable",
1057 device_get_hotpluggable, NULL, NULL);
1058 object_property_add_bool(obj, "hotplugged",
1059 device_get_hotplugged, device_set_hotplugged,
1062 class = object_get_class(OBJECT(dev));
1064 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
1065 qdev_property_add_legacy(dev, prop, &error_abort);
1066 qdev_property_add_static(dev, prop, &error_abort);
1068 class = object_class_get_parent(class);
1069 } while (class != object_class_by_name(TYPE_DEVICE));
1071 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
1072 (Object **)&dev->parent_bus, NULL, 0,
1074 QLIST_INIT(&dev->gpios);
1077 static void device_post_init(Object *obj)
1079 qdev_prop_set_globals(DEVICE(obj));
1082 /* Unlink device from bus and free the structure. */
1083 static void device_finalize(Object *obj)
1085 NamedGPIOList *ngl, *next;
1087 DeviceState *dev = DEVICE(obj);
1089 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1090 QLIST_REMOVE(ngl, node);
1091 qemu_free_irqs(ngl->in, ngl->num_in);
1094 /* ngl->out irqs are owned by the other end and should not be freed
1100 static void device_class_base_init(ObjectClass *class, void *data)
1102 DeviceClass *klass = DEVICE_CLASS(class);
1104 /* We explicitly look up properties in the superclasses,
1105 * so do not propagate them to the subclasses.
1107 klass->props = NULL;
1110 static void device_unparent(Object *obj)
1112 DeviceState *dev = DEVICE(obj);
1115 if (dev->realized) {
1116 object_property_set_bool(obj, false, "realized", NULL);
1118 while (dev->num_child_bus) {
1119 bus = QLIST_FIRST(&dev->child_bus);
1120 object_unparent(OBJECT(bus));
1122 if (dev->parent_bus) {
1123 bus_remove_child(dev->parent_bus, dev);
1124 object_unref(OBJECT(dev->parent_bus));
1125 dev->parent_bus = NULL;
1128 /* Only send event if the device had been completely realized */
1129 if (dev->pending_deleted_event) {
1130 gchar *path = object_get_canonical_path(OBJECT(dev));
1132 qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
1136 qemu_opts_del(dev->opts);
1140 static void device_class_init(ObjectClass *class, void *data)
1142 DeviceClass *dc = DEVICE_CLASS(class);
1144 class->unparent = device_unparent;
1145 dc->realize = device_realize;
1146 dc->unrealize = device_unrealize;
1148 /* by default all devices were considered as hotpluggable,
1149 * so with intent to check it in generic qdev_unplug() /
1150 * device_set_realized() functions make every device
1151 * hotpluggable. Devices that shouldn't be hotpluggable,
1152 * should override it in their class_init()
1154 dc->hotpluggable = true;
1157 void device_reset(DeviceState *dev)
1159 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1166 Object *qdev_get_machine(void)
1171 dev = container_get(object_get_root(), "/machine");
1177 static const TypeInfo device_type_info = {
1178 .name = TYPE_DEVICE,
1179 .parent = TYPE_OBJECT,
1180 .instance_size = sizeof(DeviceState),
1181 .instance_init = device_initfn,
1182 .instance_post_init = device_post_init,
1183 .instance_finalize = device_finalize,
1184 .class_base_init = device_class_base_init,
1185 .class_init = device_class_init,
1187 .class_size = sizeof(DeviceClass),
1190 static void qdev_register_types(void)
1192 type_register_static(&device_type_info);
1195 type_init(qdev_register_types)