Drop superfluous includes of qapi/qmp/qjson.h
[qemu/ar7.git] / hw / core / qdev.c
blob0f70d7e5fb32bf3a962130c489fd9fcdc199a387
1 /*
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
26 this API directly. */
28 #include "qemu/osdep.h"
29 #include "hw/qdev.h"
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qemu/error-report.h"
35 #include "hw/hotplug.h"
36 #include "hw/boards.h"
37 #include "hw/sysbus.h"
38 #include "qapi-event.h"
40 bool qdev_hotplug = false;
41 static bool qdev_hot_added = false;
42 bool qdev_hot_removed = false;
44 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
46 DeviceClass *dc = DEVICE_GET_CLASS(dev);
47 return dc->vmsd;
50 static void bus_remove_child(BusState *bus, DeviceState *child)
52 BusChild *kid;
54 QTAILQ_FOREACH(kid, &bus->children, sibling) {
55 if (kid->child == child) {
56 char name[32];
58 snprintf(name, sizeof(name), "child[%d]", kid->index);
59 QTAILQ_REMOVE(&bus->children, kid, sibling);
61 /* This gives back ownership of kid->child back to us. */
62 object_property_del(OBJECT(bus), name, NULL);
63 object_unref(OBJECT(kid->child));
64 g_free(kid);
65 return;
70 static void bus_add_child(BusState *bus, DeviceState *child)
72 char name[32];
73 BusChild *kid = g_malloc0(sizeof(*kid));
75 kid->index = bus->max_index++;
76 kid->child = child;
77 object_ref(OBJECT(kid->child));
79 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
81 /* This transfers ownership of kid->child to the property. */
82 snprintf(name, sizeof(name), "child[%d]", kid->index);
83 object_property_add_link(OBJECT(bus), name,
84 object_get_typename(OBJECT(child)),
85 (Object **)&kid->child,
86 NULL, /* read-only property */
87 0, /* return ownership on prop deletion */
88 NULL);
91 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
93 bool replugging = dev->parent_bus != NULL;
95 if (replugging) {
96 /* Keep a reference to the device while it's not plugged into
97 * any bus, to avoid it potentially evaporating when it is
98 * dereffed in bus_remove_child().
100 object_ref(OBJECT(dev));
101 bus_remove_child(dev->parent_bus, dev);
102 object_unref(OBJECT(dev->parent_bus));
104 dev->parent_bus = bus;
105 object_ref(OBJECT(bus));
106 bus_add_child(bus, dev);
107 if (replugging) {
108 object_unref(OBJECT(dev));
112 /* Create a new device. This only initializes the device state
113 structure and allows properties to be set. The device still needs
114 to be realized. See qdev-core.h. */
115 DeviceState *qdev_create(BusState *bus, const char *name)
117 DeviceState *dev;
119 dev = qdev_try_create(bus, name);
120 if (!dev) {
121 if (bus) {
122 error_report("Unknown device '%s' for bus '%s'", name,
123 object_get_typename(OBJECT(bus)));
124 } else {
125 error_report("Unknown device '%s' for default sysbus", name);
127 abort();
130 return dev;
133 DeviceState *qdev_try_create(BusState *bus, const char *type)
135 DeviceState *dev;
137 if (object_class_by_name(type) == NULL) {
138 return NULL;
140 dev = DEVICE(object_new(type));
141 if (!dev) {
142 return NULL;
145 if (!bus) {
146 /* Assert that the device really is a SysBusDevice before
147 * we put it onto the sysbus. Non-sysbus devices which aren't
148 * being put onto a bus should be created with object_new(TYPE_FOO),
149 * not qdev_create(NULL, TYPE_FOO).
151 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
152 bus = sysbus_get_default();
155 qdev_set_parent_bus(dev, bus);
156 object_unref(OBJECT(dev));
157 return dev;
160 static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
161 = QTAILQ_HEAD_INITIALIZER(device_listeners);
163 enum ListenerDirection { Forward, Reverse };
165 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
166 do { \
167 DeviceListener *_listener; \
169 switch (_direction) { \
170 case Forward: \
171 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
172 if (_listener->_callback) { \
173 _listener->_callback(_listener, ##_args); \
176 break; \
177 case Reverse: \
178 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
179 device_listeners, link) { \
180 if (_listener->_callback) { \
181 _listener->_callback(_listener, ##_args); \
184 break; \
185 default: \
186 abort(); \
188 } while (0)
190 static int device_listener_add(DeviceState *dev, void *opaque)
192 DEVICE_LISTENER_CALL(realize, Forward, dev);
194 return 0;
197 void device_listener_register(DeviceListener *listener)
199 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
201 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
202 NULL, NULL);
205 void device_listener_unregister(DeviceListener *listener)
207 QTAILQ_REMOVE(&device_listeners, listener, link);
210 static void device_realize(DeviceState *dev, Error **errp)
212 DeviceClass *dc = DEVICE_GET_CLASS(dev);
214 if (dc->init) {
215 int rc = dc->init(dev);
216 if (rc < 0) {
217 error_setg(errp, "Device initialization failed.");
218 return;
223 static void device_unrealize(DeviceState *dev, Error **errp)
225 DeviceClass *dc = DEVICE_GET_CLASS(dev);
227 if (dc->exit) {
228 int rc = dc->exit(dev);
229 if (rc < 0) {
230 error_setg(errp, "Device exit failed.");
231 return;
236 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
237 int required_for_version)
239 assert(!dev->realized);
240 dev->instance_id_alias = alias_id;
241 dev->alias_required_for_version = required_for_version;
244 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
246 MachineState *machine;
247 MachineClass *mc;
248 Object *m_obj = qdev_get_machine();
250 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
251 machine = MACHINE(m_obj);
252 mc = MACHINE_GET_CLASS(machine);
253 if (mc->get_hotplug_handler) {
254 return mc->get_hotplug_handler(machine, dev);
258 return NULL;
261 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
263 HotplugHandler *hotplug_ctrl;
265 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
266 hotplug_ctrl = dev->parent_bus->hotplug_handler;
267 } else {
268 hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
270 return hotplug_ctrl;
273 static int qdev_reset_one(DeviceState *dev, void *opaque)
275 device_reset(dev);
277 return 0;
280 static int qbus_reset_one(BusState *bus, void *opaque)
282 BusClass *bc = BUS_GET_CLASS(bus);
283 if (bc->reset) {
284 bc->reset(bus);
286 return 0;
289 void qdev_reset_all(DeviceState *dev)
291 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
294 void qdev_reset_all_fn(void *opaque)
296 qdev_reset_all(DEVICE(opaque));
299 void qbus_reset_all(BusState *bus)
301 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
304 void qbus_reset_all_fn(void *opaque)
306 BusState *bus = opaque;
307 qbus_reset_all(bus);
310 /* can be used as ->unplug() callback for the simple cases */
311 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
312 DeviceState *dev, Error **errp)
314 /* just zap it */
315 object_unparent(OBJECT(dev));
319 * Realize @dev.
320 * Device properties should be set before calling this function. IRQs
321 * and MMIO regions should be connected/mapped after calling this
322 * function.
323 * On failure, report an error with error_report() and terminate the
324 * program. This is okay during machine creation. Don't use for
325 * hotplug, because there callers need to recover from failure.
326 * Exception: if you know the device's init() callback can't fail,
327 * then qdev_init_nofail() can't fail either, and is therefore usable
328 * even then. But relying on the device implementation that way is
329 * somewhat unclean, and best avoided.
331 void qdev_init_nofail(DeviceState *dev)
333 Error *err = NULL;
335 assert(!dev->realized);
337 object_ref(OBJECT(dev));
338 object_property_set_bool(OBJECT(dev), true, "realized", &err);
339 if (err) {
340 error_reportf_err(err, "Initialization of device %s failed: ",
341 object_get_typename(OBJECT(dev)));
342 exit(1);
344 object_unref(OBJECT(dev));
347 void qdev_machine_creation_done(void)
350 * ok, initial machine setup is done, starting from now we can
351 * only create hotpluggable devices
353 qdev_hotplug = true;
356 bool qdev_machine_modified(void)
358 return qdev_hot_added || qdev_hot_removed;
361 BusState *qdev_get_parent_bus(DeviceState *dev)
363 return dev->parent_bus;
366 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
367 const char *name)
369 NamedGPIOList *ngl;
371 QLIST_FOREACH(ngl, &dev->gpios, node) {
372 /* NULL is a valid and matchable name, otherwise do a normal
373 * strcmp match.
375 if ((!ngl->name && !name) ||
376 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
377 return ngl;
381 ngl = g_malloc0(sizeof(*ngl));
382 ngl->name = g_strdup(name);
383 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
384 return ngl;
387 void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
388 const char *name, int n)
390 int i;
391 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
393 assert(gpio_list->num_out == 0 || !name);
394 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
395 dev, n);
397 if (!name) {
398 name = "unnamed-gpio-in";
400 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
401 gchar *propname = g_strdup_printf("%s[%u]", name, i);
403 object_property_add_child(OBJECT(dev), propname,
404 OBJECT(gpio_list->in[i]), &error_abort);
405 g_free(propname);
408 gpio_list->num_in += n;
411 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
413 qdev_init_gpio_in_named(dev, handler, NULL, n);
416 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
417 const char *name, int n)
419 int i;
420 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
422 assert(gpio_list->num_in == 0 || !name);
424 if (!name) {
425 name = "unnamed-gpio-out";
427 memset(pins, 0, sizeof(*pins) * n);
428 for (i = 0; i < n; ++i) {
429 gchar *propname = g_strdup_printf("%s[%u]", name,
430 gpio_list->num_out + i);
432 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
433 (Object **)&pins[i],
434 object_property_allow_set_link,
435 OBJ_PROP_LINK_UNREF_ON_RELEASE,
436 &error_abort);
437 g_free(propname);
439 gpio_list->num_out += n;
442 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
444 qdev_init_gpio_out_named(dev, pins, NULL, n);
447 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
449 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
451 assert(n >= 0 && n < gpio_list->num_in);
452 return gpio_list->in[n];
455 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
457 return qdev_get_gpio_in_named(dev, NULL, n);
460 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
461 qemu_irq pin)
463 char *propname = g_strdup_printf("%s[%d]",
464 name ? name : "unnamed-gpio-out", n);
465 if (pin) {
466 /* We need a name for object_property_set_link to work. If the
467 * object has a parent, object_property_add_child will come back
468 * with an error without doing anything. If it has none, it will
469 * never fail. So we can just call it with a NULL Error pointer.
471 object_property_add_child(container_get(qdev_get_machine(),
472 "/unattached"),
473 "non-qdev-gpio[*]", OBJECT(pin), NULL);
475 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
476 g_free(propname);
479 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
481 char *propname = g_strdup_printf("%s[%d]",
482 name ? name : "unnamed-gpio-out", n);
484 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
485 NULL);
487 return ret;
490 /* disconnect a GPIO output, returning the disconnected input (if any) */
492 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
493 const char *name, int n)
495 char *propname = g_strdup_printf("%s[%d]",
496 name ? name : "unnamed-gpio-out", n);
498 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
499 NULL);
500 if (ret) {
501 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
503 g_free(propname);
504 return ret;
507 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
508 const char *name, int n)
510 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
511 qdev_connect_gpio_out_named(dev, name, n, icpt);
512 return disconnected;
515 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
517 qdev_connect_gpio_out_named(dev, NULL, n, pin);
520 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
521 const char *name)
523 int i;
524 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
526 for (i = 0; i < ngl->num_in; i++) {
527 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
528 char *propname = g_strdup_printf("%s[%d]", nm, i);
530 object_property_add_alias(OBJECT(container), propname,
531 OBJECT(dev), propname,
532 &error_abort);
533 g_free(propname);
535 for (i = 0; i < ngl->num_out; i++) {
536 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
537 char *propname = g_strdup_printf("%s[%d]", nm, i);
539 object_property_add_alias(OBJECT(container), propname,
540 OBJECT(dev), propname,
541 &error_abort);
542 g_free(propname);
544 QLIST_REMOVE(ngl, node);
545 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
548 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
550 BusState *bus;
551 Object *child = object_resolve_path_component(OBJECT(dev), name);
553 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
554 if (bus) {
555 return bus;
558 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
559 if (strcmp(name, bus->name) == 0) {
560 return bus;
563 return NULL;
566 int qdev_walk_children(DeviceState *dev,
567 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
568 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
569 void *opaque)
571 BusState *bus;
572 int err;
574 if (pre_devfn) {
575 err = pre_devfn(dev, opaque);
576 if (err) {
577 return err;
581 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
582 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
583 post_devfn, post_busfn, opaque);
584 if (err < 0) {
585 return err;
589 if (post_devfn) {
590 err = post_devfn(dev, opaque);
591 if (err) {
592 return err;
596 return 0;
599 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
601 BusChild *kid;
602 DeviceState *ret;
603 BusState *child;
605 QTAILQ_FOREACH(kid, &bus->children, sibling) {
606 DeviceState *dev = kid->child;
608 if (dev->id && strcmp(dev->id, id) == 0) {
609 return dev;
612 QLIST_FOREACH(child, &dev->child_bus, sibling) {
613 ret = qdev_find_recursive(child, id);
614 if (ret) {
615 return ret;
619 return NULL;
622 char *qdev_get_dev_path(DeviceState *dev)
624 BusClass *bc;
626 if (!dev || !dev->parent_bus) {
627 return NULL;
630 bc = BUS_GET_CLASS(dev->parent_bus);
631 if (bc->get_dev_path) {
632 return bc->get_dev_path(dev);
635 return NULL;
639 * Legacy property handling
642 static void qdev_get_legacy_property(Object *obj, Visitor *v,
643 const char *name, void *opaque,
644 Error **errp)
646 DeviceState *dev = DEVICE(obj);
647 Property *prop = opaque;
649 char buffer[1024];
650 char *ptr = buffer;
652 prop->info->print(dev, prop, buffer, sizeof(buffer));
653 visit_type_str(v, name, &ptr, errp);
657 * qdev_property_add_legacy:
658 * @dev: Device to add the property to.
659 * @prop: The qdev property definition.
660 * @errp: location to store error information.
662 * Add a legacy QOM property to @dev for qdev property @prop.
663 * On error, store error in @errp.
665 * Legacy properties are string versions of QOM properties. The format of
666 * the string depends on the property type. Legacy properties are only
667 * needed for "info qtree".
669 * Do not use this is new code! QOM Properties added through this interface
670 * will be given names in the "legacy" namespace.
672 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
673 Error **errp)
675 gchar *name;
677 /* Register pointer properties as legacy properties */
678 if (!prop->info->print && prop->info->get) {
679 return;
682 if (prop->info->create) {
683 return;
686 name = g_strdup_printf("legacy-%s", prop->name);
687 object_property_add(OBJECT(dev), name, "str",
688 prop->info->print ? qdev_get_legacy_property : prop->info->get,
689 NULL,
690 NULL,
691 prop, errp);
693 g_free(name);
697 * qdev_property_add_static:
698 * @dev: Device to add the property to.
699 * @prop: The qdev property definition.
700 * @errp: location to store error information.
702 * Add a static QOM property to @dev for qdev property @prop.
703 * On error, store error in @errp. Static properties access data in a struct.
704 * The type of the QOM property is derived from prop->info.
706 void qdev_property_add_static(DeviceState *dev, Property *prop,
707 Error **errp)
709 Error *local_err = NULL;
710 Object *obj = OBJECT(dev);
712 if (prop->info->create) {
713 prop->info->create(obj, prop, &local_err);
714 } else {
716 * TODO qdev_prop_ptr does not have getters or setters. It must
717 * go now that it can be replaced with links. The test should be
718 * removed along with it: all static properties are read/write.
720 if (!prop->info->get && !prop->info->set) {
721 return;
723 object_property_add(obj, prop->name, prop->info->name,
724 prop->info->get, prop->info->set,
725 prop->info->release,
726 prop, &local_err);
729 if (local_err) {
730 error_propagate(errp, local_err);
731 return;
734 object_property_set_description(obj, prop->name,
735 prop->info->description,
736 &error_abort);
738 if (prop->set_default) {
739 prop->info->set_default_value(obj, prop);
743 /* @qdev_alias_all_properties - Add alias properties to the source object for
744 * all qdev properties on the target DeviceState.
746 void qdev_alias_all_properties(DeviceState *target, Object *source)
748 ObjectClass *class;
749 Property *prop;
751 class = object_get_class(OBJECT(target));
752 do {
753 DeviceClass *dc = DEVICE_CLASS(class);
755 for (prop = dc->props; prop && prop->name; prop++) {
756 object_property_add_alias(source, prop->name,
757 OBJECT(target), prop->name,
758 &error_abort);
760 class = object_class_get_parent(class);
761 } while (class != object_class_by_name(TYPE_DEVICE));
764 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
766 GSList **list = opaque;
767 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
768 TYPE_DEVICE);
770 if (dev == NULL) {
771 return 0;
774 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
775 *list = g_slist_append(*list, dev);
778 return 0;
781 GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
783 GSList *list = NULL;
785 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
787 return list;
790 static bool device_get_realized(Object *obj, Error **errp)
792 DeviceState *dev = DEVICE(obj);
793 return dev->realized;
796 static bool check_only_migratable(Object *obj, Error **err)
798 DeviceClass *dc = DEVICE_GET_CLASS(obj);
800 if (!vmstate_check_only_migratable(dc->vmsd)) {
801 error_setg(err, "Device %s is not migratable, but "
802 "--only-migratable was specified",
803 object_get_typename(obj));
804 return false;
807 return true;
810 static void device_set_realized(Object *obj, bool value, Error **errp)
812 DeviceState *dev = DEVICE(obj);
813 DeviceClass *dc = DEVICE_GET_CLASS(dev);
814 HotplugHandler *hotplug_ctrl;
815 BusState *bus;
816 Error *local_err = NULL;
817 bool unattached_parent = false;
818 static int unattached_count;
820 if (dev->hotplugged && !dc->hotpluggable) {
821 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
822 return;
825 if (value && !dev->realized) {
826 if (!check_only_migratable(obj, &local_err)) {
827 goto fail;
830 if (!obj->parent) {
831 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
833 object_property_add_child(container_get(qdev_get_machine(),
834 "/unattached"),
835 name, obj, &error_abort);
836 unattached_parent = true;
837 g_free(name);
840 hotplug_ctrl = qdev_get_hotplug_handler(dev);
841 if (hotplug_ctrl) {
842 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
843 if (local_err != NULL) {
844 goto fail;
848 if (dc->realize) {
849 dc->realize(dev, &local_err);
852 if (local_err != NULL) {
853 goto fail;
856 DEVICE_LISTENER_CALL(realize, Forward, dev);
858 if (hotplug_ctrl) {
859 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
862 if (local_err != NULL) {
863 goto post_realize_fail;
867 * always free/re-initialize here since the value cannot be cleaned up
868 * in device_unrealize due to its usage later on in the unplug path
870 g_free(dev->canonical_path);
871 dev->canonical_path = object_get_canonical_path(OBJECT(dev));
873 if (qdev_get_vmsd(dev)) {
874 if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
875 dev->instance_id_alias,
876 dev->alias_required_for_version,
877 &local_err) < 0) {
878 goto post_realize_fail;
882 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
883 object_property_set_bool(OBJECT(bus), true, "realized",
884 &local_err);
885 if (local_err != NULL) {
886 goto child_realize_fail;
889 if (dev->hotplugged) {
890 device_reset(dev);
892 dev->pending_deleted_event = false;
893 } else if (!value && dev->realized) {
894 Error **local_errp = NULL;
895 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
896 local_errp = local_err ? NULL : &local_err;
897 object_property_set_bool(OBJECT(bus), false, "realized",
898 local_errp);
900 if (qdev_get_vmsd(dev)) {
901 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
903 if (dc->unrealize) {
904 local_errp = local_err ? NULL : &local_err;
905 dc->unrealize(dev, local_errp);
907 dev->pending_deleted_event = true;
908 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
911 if (local_err != NULL) {
912 goto fail;
915 dev->realized = value;
916 return;
918 child_realize_fail:
919 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
920 object_property_set_bool(OBJECT(bus), false, "realized",
921 NULL);
924 if (qdev_get_vmsd(dev)) {
925 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
928 post_realize_fail:
929 g_free(dev->canonical_path);
930 dev->canonical_path = NULL;
931 if (dc->unrealize) {
932 dc->unrealize(dev, NULL);
935 fail:
936 error_propagate(errp, local_err);
937 if (unattached_parent) {
938 object_unparent(OBJECT(dev));
939 unattached_count--;
943 static bool device_get_hotpluggable(Object *obj, Error **errp)
945 DeviceClass *dc = DEVICE_GET_CLASS(obj);
946 DeviceState *dev = DEVICE(obj);
948 return dc->hotpluggable && (dev->parent_bus == NULL ||
949 qbus_is_hotpluggable(dev->parent_bus));
952 static bool device_get_hotplugged(Object *obj, Error **err)
954 DeviceState *dev = DEVICE(obj);
956 return dev->hotplugged;
959 static void device_initfn(Object *obj)
961 DeviceState *dev = DEVICE(obj);
962 ObjectClass *class;
963 Property *prop;
965 if (qdev_hotplug) {
966 dev->hotplugged = 1;
967 qdev_hot_added = true;
970 dev->instance_id_alias = -1;
971 dev->realized = false;
973 object_property_add_bool(obj, "realized",
974 device_get_realized, device_set_realized, NULL);
975 object_property_add_bool(obj, "hotpluggable",
976 device_get_hotpluggable, NULL, NULL);
977 object_property_add_bool(obj, "hotplugged",
978 device_get_hotplugged, NULL,
979 &error_abort);
981 class = object_get_class(OBJECT(dev));
982 do {
983 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
984 qdev_property_add_legacy(dev, prop, &error_abort);
985 qdev_property_add_static(dev, prop, &error_abort);
987 class = object_class_get_parent(class);
988 } while (class != object_class_by_name(TYPE_DEVICE));
990 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
991 (Object **)&dev->parent_bus, NULL, 0,
992 &error_abort);
993 QLIST_INIT(&dev->gpios);
996 static void device_post_init(Object *obj)
998 qdev_prop_set_globals(DEVICE(obj));
1001 /* Unlink device from bus and free the structure. */
1002 static void device_finalize(Object *obj)
1004 NamedGPIOList *ngl, *next;
1006 DeviceState *dev = DEVICE(obj);
1008 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1009 QLIST_REMOVE(ngl, node);
1010 qemu_free_irqs(ngl->in, ngl->num_in);
1011 g_free(ngl->name);
1012 g_free(ngl);
1013 /* ngl->out irqs are owned by the other end and should not be freed
1014 * here
1018 /* Only send event if the device had been completely realized */
1019 if (dev->pending_deleted_event) {
1020 g_assert(dev->canonical_path);
1022 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path,
1023 &error_abort);
1024 g_free(dev->canonical_path);
1025 dev->canonical_path = NULL;
1028 qemu_opts_del(dev->opts);
1031 static void device_class_base_init(ObjectClass *class, void *data)
1033 DeviceClass *klass = DEVICE_CLASS(class);
1035 /* We explicitly look up properties in the superclasses,
1036 * so do not propagate them to the subclasses.
1038 klass->props = NULL;
1041 static void device_unparent(Object *obj)
1043 DeviceState *dev = DEVICE(obj);
1044 BusState *bus;
1046 if (dev->realized) {
1047 object_property_set_bool(obj, false, "realized", NULL);
1049 while (dev->num_child_bus) {
1050 bus = QLIST_FIRST(&dev->child_bus);
1051 object_unparent(OBJECT(bus));
1053 if (dev->parent_bus) {
1054 bus_remove_child(dev->parent_bus, dev);
1055 object_unref(OBJECT(dev->parent_bus));
1056 dev->parent_bus = NULL;
1060 static void device_class_init(ObjectClass *class, void *data)
1062 DeviceClass *dc = DEVICE_CLASS(class);
1064 class->unparent = device_unparent;
1065 dc->realize = device_realize;
1066 dc->unrealize = device_unrealize;
1068 /* by default all devices were considered as hotpluggable,
1069 * so with intent to check it in generic qdev_unplug() /
1070 * device_set_realized() functions make every device
1071 * hotpluggable. Devices that shouldn't be hotpluggable,
1072 * should override it in their class_init()
1074 dc->hotpluggable = true;
1075 dc->user_creatable = true;
1078 void device_class_set_parent_reset(DeviceClass *dc,
1079 DeviceReset dev_reset,
1080 DeviceReset *parent_reset)
1082 *parent_reset = dc->reset;
1083 dc->reset = dev_reset;
1086 void device_class_set_parent_realize(DeviceClass *dc,
1087 DeviceRealize dev_realize,
1088 DeviceRealize *parent_realize)
1090 *parent_realize = dc->realize;
1091 dc->realize = dev_realize;
1094 void device_class_set_parent_unrealize(DeviceClass *dc,
1095 DeviceUnrealize dev_unrealize,
1096 DeviceUnrealize *parent_unrealize)
1098 *parent_unrealize = dc->unrealize;
1099 dc->unrealize = dev_unrealize;
1102 void device_reset(DeviceState *dev)
1104 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1106 if (klass->reset) {
1107 klass->reset(dev);
1111 Object *qdev_get_machine(void)
1113 static Object *dev;
1115 if (dev == NULL) {
1116 dev = container_get(object_get_root(), "/machine");
1119 return dev;
1122 static const TypeInfo device_type_info = {
1123 .name = TYPE_DEVICE,
1124 .parent = TYPE_OBJECT,
1125 .instance_size = sizeof(DeviceState),
1126 .instance_init = device_initfn,
1127 .instance_post_init = device_post_init,
1128 .instance_finalize = device_finalize,
1129 .class_base_init = device_class_base_init,
1130 .class_init = device_class_init,
1131 .abstract = true,
1132 .class_size = sizeof(DeviceClass),
1135 static void qdev_register_types(void)
1137 type_register_static(&device_type_info);
1140 type_init(qdev_register_types)