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 "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/qapi-events-qdev.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/visitor.h"
35 #include "qemu/error-report.h"
36 #include "qemu/option.h"
37 #include "hw/hotplug.h"
38 #include "hw/boards.h"
39 #include "hw/sysbus.h"
41 bool qdev_hotplug = false;
42 static bool qdev_hot_added = false;
43 bool qdev_hot_removed = false;
45 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
47 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51 static void bus_remove_child(BusState *bus, DeviceState *child)
55 QTAILQ_FOREACH(kid, &bus->children, sibling) {
56 if (kid->child == child) {
59 snprintf(name, sizeof(name), "child[%d]", kid->index);
60 QTAILQ_REMOVE(&bus->children, kid, sibling);
64 /* This gives back ownership of kid->child back to us. */
65 object_property_del(OBJECT(bus), name, NULL);
66 object_unref(OBJECT(kid->child));
73 static void bus_add_child(BusState *bus, DeviceState *child)
76 BusChild *kid = g_malloc0(sizeof(*kid));
79 kid->index = bus->max_index++;
81 object_ref(OBJECT(kid->child));
83 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
85 /* This transfers ownership of kid->child to the property. */
86 snprintf(name, sizeof(name), "child[%d]", kid->index);
87 object_property_add_link(OBJECT(bus), name,
88 object_get_typename(OBJECT(child)),
89 (Object **)&kid->child,
90 NULL, /* read-only property */
91 0, /* return ownership on prop deletion */
95 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
97 bool replugging = dev->parent_bus != NULL;
100 /* Keep a reference to the device while it's not plugged into
101 * any bus, to avoid it potentially evaporating when it is
102 * dereffed in bus_remove_child().
104 object_ref(OBJECT(dev));
105 bus_remove_child(dev->parent_bus, dev);
106 object_unref(OBJECT(dev->parent_bus));
108 dev->parent_bus = bus;
109 object_ref(OBJECT(bus));
110 bus_add_child(bus, dev);
112 object_unref(OBJECT(dev));
116 /* Create a new device. This only initializes the device state
117 structure and allows properties to be set. The device still needs
118 to be realized. See qdev-core.h. */
119 DeviceState *qdev_create(BusState *bus, const char *name)
123 dev = qdev_try_create(bus, name);
126 error_report("Unknown device '%s' for bus '%s'", name,
127 object_get_typename(OBJECT(bus)));
129 error_report("Unknown device '%s' for default sysbus", name);
137 DeviceState *qdev_try_create(BusState *bus, const char *type)
141 if (object_class_by_name(type) == NULL) {
144 dev = DEVICE(object_new(type));
150 /* Assert that the device really is a SysBusDevice before
151 * we put it onto the sysbus. Non-sysbus devices which aren't
152 * being put onto a bus should be created with object_new(TYPE_FOO),
153 * not qdev_create(NULL, TYPE_FOO).
155 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
156 bus = sysbus_get_default();
159 qdev_set_parent_bus(dev, bus);
160 object_unref(OBJECT(dev));
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 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
215 int required_for_version)
217 assert(!dev->realized);
218 dev->instance_id_alias = alias_id;
219 dev->alias_required_for_version = required_for_version;
222 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
224 MachineState *machine;
226 Object *m_obj = qdev_get_machine();
228 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
229 machine = MACHINE(m_obj);
230 mc = MACHINE_GET_CLASS(machine);
231 if (mc->get_hotplug_handler) {
232 return mc->get_hotplug_handler(machine, dev);
239 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
241 if (dev->parent_bus) {
242 return dev->parent_bus->hotplug_handler;
247 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
249 HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
251 if (hotplug_ctrl == NULL && dev->parent_bus) {
252 hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
257 static int qdev_reset_one(DeviceState *dev, void *opaque)
264 static int qbus_reset_one(BusState *bus, void *opaque)
266 BusClass *bc = BUS_GET_CLASS(bus);
273 void qdev_reset_all(DeviceState *dev)
275 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
278 void qdev_reset_all_fn(void *opaque)
280 qdev_reset_all(DEVICE(opaque));
283 void qbus_reset_all(BusState *bus)
285 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
288 void qbus_reset_all_fn(void *opaque)
290 BusState *bus = opaque;
294 /* can be used as ->unplug() callback for the simple cases */
295 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
296 DeviceState *dev, Error **errp)
298 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
303 * Device properties should be set before calling this function. IRQs
304 * and MMIO regions should be connected/mapped after calling this
306 * On failure, report an error with error_report() and terminate the
307 * program. This is okay during machine creation. Don't use for
308 * hotplug, because there callers need to recover from failure.
309 * Exception: if you know the device's init() callback can't fail,
310 * then qdev_init_nofail() can't fail either, and is therefore usable
311 * even then. But relying on the device implementation that way is
312 * somewhat unclean, and best avoided.
314 void qdev_init_nofail(DeviceState *dev)
318 assert(!dev->realized);
320 object_ref(OBJECT(dev));
321 object_property_set_bool(OBJECT(dev), true, "realized", &err);
323 error_reportf_err(err, "Initialization of device %s failed: ",
324 object_get_typename(OBJECT(dev)));
327 object_unref(OBJECT(dev));
330 void qdev_machine_creation_done(void)
333 * ok, initial machine setup is done, starting from now we can
334 * only create hotpluggable devices
339 bool qdev_machine_modified(void)
341 return qdev_hot_added || qdev_hot_removed;
344 BusState *qdev_get_parent_bus(DeviceState *dev)
346 return dev->parent_bus;
349 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
354 QLIST_FOREACH(ngl, &dev->gpios, node) {
355 /* NULL is a valid and matchable name, otherwise do a normal
358 if ((!ngl->name && !name) ||
359 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
364 ngl = g_malloc0(sizeof(*ngl));
365 ngl->name = g_strdup(name);
366 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
370 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
371 qemu_irq_handler handler,
373 const char *name, int n)
376 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
378 assert(gpio_list->num_out == 0 || !name);
379 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
383 name = "unnamed-gpio-in";
385 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
386 gchar *propname = g_strdup_printf("%s[%u]", name, i);
388 object_property_add_child(OBJECT(dev), propname,
389 OBJECT(gpio_list->in[i]), &error_abort);
393 gpio_list->num_in += n;
396 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
398 qdev_init_gpio_in_named(dev, handler, NULL, n);
401 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
402 const char *name, int n)
405 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
407 assert(gpio_list->num_in == 0 || !name);
410 name = "unnamed-gpio-out";
412 memset(pins, 0, sizeof(*pins) * n);
413 for (i = 0; i < n; ++i) {
414 gchar *propname = g_strdup_printf("%s[%u]", name,
415 gpio_list->num_out + i);
417 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
419 object_property_allow_set_link,
420 OBJ_PROP_LINK_STRONG,
424 gpio_list->num_out += n;
427 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
429 qdev_init_gpio_out_named(dev, pins, NULL, n);
432 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
434 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
436 assert(n >= 0 && n < gpio_list->num_in);
437 return gpio_list->in[n];
440 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
442 return qdev_get_gpio_in_named(dev, NULL, n);
445 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
448 char *propname = g_strdup_printf("%s[%d]",
449 name ? name : "unnamed-gpio-out", n);
451 /* We need a name for object_property_set_link to work. If the
452 * object has a parent, object_property_add_child will come back
453 * with an error without doing anything. If it has none, it will
454 * never fail. So we can just call it with a NULL Error pointer.
456 object_property_add_child(container_get(qdev_get_machine(),
458 "non-qdev-gpio[*]", OBJECT(pin), NULL);
460 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
464 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
466 char *propname = g_strdup_printf("%s[%d]",
467 name ? name : "unnamed-gpio-out", n);
469 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
475 /* disconnect a GPIO output, returning the disconnected input (if any) */
477 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
478 const char *name, int n)
480 char *propname = g_strdup_printf("%s[%d]",
481 name ? name : "unnamed-gpio-out", n);
483 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
486 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
492 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
493 const char *name, int n)
495 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
496 qdev_connect_gpio_out_named(dev, name, n, icpt);
500 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
502 qdev_connect_gpio_out_named(dev, NULL, n, pin);
505 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
509 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
511 for (i = 0; i < ngl->num_in; i++) {
512 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
513 char *propname = g_strdup_printf("%s[%d]", nm, i);
515 object_property_add_alias(OBJECT(container), propname,
516 OBJECT(dev), propname,
520 for (i = 0; i < ngl->num_out; i++) {
521 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
522 char *propname = g_strdup_printf("%s[%d]", nm, i);
524 object_property_add_alias(OBJECT(container), propname,
525 OBJECT(dev), propname,
529 QLIST_REMOVE(ngl, node);
530 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
533 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
536 Object *child = object_resolve_path_component(OBJECT(dev), name);
538 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
543 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
544 if (strcmp(name, bus->name) == 0) {
551 int qdev_walk_children(DeviceState *dev,
552 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
553 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
560 err = pre_devfn(dev, opaque);
566 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
567 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
568 post_devfn, post_busfn, opaque);
575 err = post_devfn(dev, opaque);
584 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
590 QTAILQ_FOREACH(kid, &bus->children, sibling) {
591 DeviceState *dev = kid->child;
593 if (dev->id && strcmp(dev->id, id) == 0) {
597 QLIST_FOREACH(child, &dev->child_bus, sibling) {
598 ret = qdev_find_recursive(child, id);
607 char *qdev_get_dev_path(DeviceState *dev)
611 if (!dev || !dev->parent_bus) {
615 bc = BUS_GET_CLASS(dev->parent_bus);
616 if (bc->get_dev_path) {
617 return bc->get_dev_path(dev);
624 * Legacy property handling
627 static void qdev_get_legacy_property(Object *obj, Visitor *v,
628 const char *name, void *opaque,
631 DeviceState *dev = DEVICE(obj);
632 Property *prop = opaque;
637 prop->info->print(dev, prop, buffer, sizeof(buffer));
638 visit_type_str(v, name, &ptr, errp);
642 * qdev_property_add_legacy:
643 * @dev: Device to add the property to.
644 * @prop: The qdev property definition.
645 * @errp: location to store error information.
647 * Add a legacy QOM property to @dev for qdev property @prop.
648 * On error, store error in @errp.
650 * Legacy properties are string versions of QOM properties. The format of
651 * the string depends on the property type. Legacy properties are only
652 * needed for "info qtree".
654 * Do not use this in new code! QOM Properties added through this interface
655 * will be given names in the "legacy" namespace.
657 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
662 /* Register pointer properties as legacy properties */
663 if (!prop->info->print && prop->info->get) {
667 if (prop->info->create) {
671 name = g_strdup_printf("legacy-%s", prop->name);
672 object_property_add(OBJECT(dev), name, "str",
673 prop->info->print ? qdev_get_legacy_property : prop->info->get,
682 * qdev_property_add_static:
683 * @dev: Device to add the property to.
684 * @prop: The qdev property definition.
685 * @errp: location to store error information.
687 * Add a static QOM property to @dev for qdev property @prop.
688 * On error, store error in @errp. Static properties access data in a struct.
689 * The type of the QOM property is derived from prop->info.
691 void qdev_property_add_static(DeviceState *dev, Property *prop,
694 Error *local_err = NULL;
695 Object *obj = OBJECT(dev);
697 if (prop->info->create) {
698 prop->info->create(obj, prop, &local_err);
701 * TODO qdev_prop_ptr does not have getters or setters. It must
702 * go now that it can be replaced with links. The test should be
703 * removed along with it: all static properties are read/write.
705 if (!prop->info->get && !prop->info->set) {
708 object_property_add(obj, prop->name, prop->info->name,
709 prop->info->get, prop->info->set,
715 error_propagate(errp, local_err);
719 object_property_set_description(obj, prop->name,
720 prop->info->description,
723 if (prop->set_default) {
724 prop->info->set_default_value(obj, prop);
728 /* @qdev_alias_all_properties - Add alias properties to the source object for
729 * all qdev properties on the target DeviceState.
731 void qdev_alias_all_properties(DeviceState *target, Object *source)
736 class = object_get_class(OBJECT(target));
738 DeviceClass *dc = DEVICE_CLASS(class);
740 for (prop = dc->props; prop && prop->name; prop++) {
741 object_property_add_alias(source, prop->name,
742 OBJECT(target), prop->name,
745 class = object_class_get_parent(class);
746 } while (class != object_class_by_name(TYPE_DEVICE));
749 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
751 GSList **list = opaque;
752 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
759 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
760 *list = g_slist_append(*list, dev);
766 GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
770 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
775 static bool device_get_realized(Object *obj, Error **errp)
777 DeviceState *dev = DEVICE(obj);
778 return dev->realized;
781 static bool check_only_migratable(Object *obj, Error **err)
783 DeviceClass *dc = DEVICE_GET_CLASS(obj);
785 if (!vmstate_check_only_migratable(dc->vmsd)) {
786 error_setg(err, "Device %s is not migratable, but "
787 "--only-migratable was specified",
788 object_get_typename(obj));
795 static void device_set_realized(Object *obj, bool value, Error **errp)
797 DeviceState *dev = DEVICE(obj);
798 DeviceClass *dc = DEVICE_GET_CLASS(dev);
799 HotplugHandler *hotplug_ctrl;
801 Error *local_err = NULL;
802 bool unattached_parent = false;
803 static int unattached_count;
805 if (dev->hotplugged && !dc->hotpluggable) {
806 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
810 if (value && !dev->realized) {
811 if (!check_only_migratable(obj, &local_err)) {
816 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
818 object_property_add_child(container_get(qdev_get_machine(),
820 name, obj, &error_abort);
821 unattached_parent = true;
825 hotplug_ctrl = qdev_get_hotplug_handler(dev);
827 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
828 if (local_err != NULL) {
834 dc->realize(dev, &local_err);
837 if (local_err != NULL) {
841 DEVICE_LISTENER_CALL(realize, Forward, dev);
844 * always free/re-initialize here since the value cannot be cleaned up
845 * in device_unrealize due to its usage later on in the unplug path
847 g_free(dev->canonical_path);
848 dev->canonical_path = object_get_canonical_path(OBJECT(dev));
850 if (qdev_get_vmsd(dev)) {
851 if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
852 dev->instance_id_alias,
853 dev->alias_required_for_version,
855 goto post_realize_fail;
859 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
860 object_property_set_bool(OBJECT(bus), true, "realized",
862 if (local_err != NULL) {
863 goto child_realize_fail;
866 if (dev->hotplugged) {
869 dev->pending_deleted_event = false;
872 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
873 if (local_err != NULL) {
874 goto child_realize_fail;
878 } else if (!value && dev->realized) {
879 Error **local_errp = NULL;
880 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
881 local_errp = local_err ? NULL : &local_err;
882 object_property_set_bool(OBJECT(bus), false, "realized",
885 if (qdev_get_vmsd(dev)) {
886 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
889 local_errp = local_err ? NULL : &local_err;
890 dc->unrealize(dev, local_errp);
892 dev->pending_deleted_event = true;
893 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
896 if (local_err != NULL) {
900 dev->realized = value;
904 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
905 object_property_set_bool(OBJECT(bus), false, "realized",
909 if (qdev_get_vmsd(dev)) {
910 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
914 g_free(dev->canonical_path);
915 dev->canonical_path = NULL;
917 dc->unrealize(dev, NULL);
921 error_propagate(errp, local_err);
922 if (unattached_parent) {
923 object_unparent(OBJECT(dev));
928 static bool device_get_hotpluggable(Object *obj, Error **errp)
930 DeviceClass *dc = DEVICE_GET_CLASS(obj);
931 DeviceState *dev = DEVICE(obj);
933 return dc->hotpluggable && (dev->parent_bus == NULL ||
934 qbus_is_hotpluggable(dev->parent_bus));
937 static bool device_get_hotplugged(Object *obj, Error **err)
939 DeviceState *dev = DEVICE(obj);
941 return dev->hotplugged;
944 static void device_initfn(Object *obj)
946 DeviceState *dev = DEVICE(obj);
952 qdev_hot_added = true;
955 dev->instance_id_alias = -1;
956 dev->realized = false;
958 object_property_add_bool(obj, "realized",
959 device_get_realized, device_set_realized, NULL);
960 object_property_add_bool(obj, "hotpluggable",
961 device_get_hotpluggable, NULL, NULL);
962 object_property_add_bool(obj, "hotplugged",
963 device_get_hotplugged, NULL,
966 class = object_get_class(OBJECT(dev));
968 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
969 qdev_property_add_legacy(dev, prop, &error_abort);
970 qdev_property_add_static(dev, prop, &error_abort);
972 class = object_class_get_parent(class);
973 } while (class != object_class_by_name(TYPE_DEVICE));
975 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
976 (Object **)&dev->parent_bus, NULL, 0,
978 QLIST_INIT(&dev->gpios);
981 static void device_post_init(Object *obj)
984 * Note: ordered so that the user's global properties take
987 object_apply_compat_props(obj);
988 qdev_prop_set_globals(DEVICE(obj));
991 /* Unlink device from bus and free the structure. */
992 static void device_finalize(Object *obj)
994 NamedGPIOList *ngl, *next;
996 DeviceState *dev = DEVICE(obj);
998 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
999 QLIST_REMOVE(ngl, node);
1000 qemu_free_irqs(ngl->in, ngl->num_in);
1003 /* ngl->out irqs are owned by the other end and should not be freed
1008 /* Only send event if the device had been completely realized */
1009 if (dev->pending_deleted_event) {
1010 g_assert(dev->canonical_path);
1012 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
1013 g_free(dev->canonical_path);
1014 dev->canonical_path = NULL;
1017 qemu_opts_del(dev->opts);
1020 static void device_class_base_init(ObjectClass *class, void *data)
1022 DeviceClass *klass = DEVICE_CLASS(class);
1024 /* We explicitly look up properties in the superclasses,
1025 * so do not propagate them to the subclasses.
1027 klass->props = NULL;
1030 static void device_unparent(Object *obj)
1032 DeviceState *dev = DEVICE(obj);
1035 if (dev->realized) {
1036 object_property_set_bool(obj, false, "realized", NULL);
1038 while (dev->num_child_bus) {
1039 bus = QLIST_FIRST(&dev->child_bus);
1040 object_unparent(OBJECT(bus));
1042 if (dev->parent_bus) {
1043 bus_remove_child(dev->parent_bus, dev);
1044 object_unref(OBJECT(dev->parent_bus));
1045 dev->parent_bus = NULL;
1049 static void device_class_init(ObjectClass *class, void *data)
1051 DeviceClass *dc = DEVICE_CLASS(class);
1053 class->unparent = device_unparent;
1055 /* by default all devices were considered as hotpluggable,
1056 * so with intent to check it in generic qdev_unplug() /
1057 * device_set_realized() functions make every device
1058 * hotpluggable. Devices that shouldn't be hotpluggable,
1059 * should override it in their class_init()
1061 dc->hotpluggable = true;
1062 dc->user_creatable = true;
1065 void device_class_set_parent_reset(DeviceClass *dc,
1066 DeviceReset dev_reset,
1067 DeviceReset *parent_reset)
1069 *parent_reset = dc->reset;
1070 dc->reset = dev_reset;
1073 void device_class_set_parent_realize(DeviceClass *dc,
1074 DeviceRealize dev_realize,
1075 DeviceRealize *parent_realize)
1077 *parent_realize = dc->realize;
1078 dc->realize = dev_realize;
1081 void device_class_set_parent_unrealize(DeviceClass *dc,
1082 DeviceUnrealize dev_unrealize,
1083 DeviceUnrealize *parent_unrealize)
1085 *parent_unrealize = dc->unrealize;
1086 dc->unrealize = dev_unrealize;
1089 void device_reset(DeviceState *dev)
1091 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1098 Object *qdev_get_machine(void)
1103 dev = container_get(object_get_root(), "/machine");
1109 static const TypeInfo device_type_info = {
1110 .name = TYPE_DEVICE,
1111 .parent = TYPE_OBJECT,
1112 .instance_size = sizeof(DeviceState),
1113 .instance_init = device_initfn,
1114 .instance_post_init = device_post_init,
1115 .instance_finalize = device_finalize,
1116 .class_base_init = device_class_base_init,
1117 .class_init = device_class_init,
1119 .class_size = sizeof(DeviceClass),
1122 static void qdev_register_types(void)
1124 type_register_static(&device_type_info);
1127 type_init(qdev_register_types)