qdev: Assert onboard devices all get realized properly
[qemu.git] / hw / core / qdev.c
bloba68ba674db9433f485e1625632b9b271f5658f5a
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 "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"
36 #include "hw/irq.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"
42 #include "trace.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);
51 return dc->vmsd;
54 static void bus_remove_child(BusState *bus, DeviceState *child)
56 BusChild *kid;
58 QTAILQ_FOREACH(kid, &bus->children, sibling) {
59 if (kid->child == child) {
60 char name[32];
62 snprintf(name, sizeof(name), "child[%d]", kid->index);
63 QTAILQ_REMOVE(&bus->children, kid, sibling);
65 bus->num_children--;
67 /* This gives back ownership of kid->child back to us. */
68 object_property_del(OBJECT(bus), name);
69 object_unref(OBJECT(kid->child));
70 g_free(kid);
71 return;
76 static void bus_add_child(BusState *bus, DeviceState *child)
78 char name[32];
79 BusChild *kid = g_malloc0(sizeof(*kid));
81 bus->num_children++;
82 kid->index = bus->max_index++;
83 kid->child = child;
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);
97 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
99 BusState *old_parent_bus = dev->parent_bus;
100 DeviceClass *dc = DEVICE_GET_CLASS(dev);
102 assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type));
104 if (old_parent_bus) {
105 trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)),
106 old_parent_bus, object_get_typename(OBJECT(old_parent_bus)),
107 OBJECT(bus), object_get_typename(OBJECT(bus)));
109 * Keep a reference to the device while it's not plugged into
110 * any bus, to avoid it potentially evaporating when it is
111 * dereffed in bus_remove_child().
112 * Also keep the ref of the parent bus until the end, so that
113 * we can safely call resettable_change_parent() below.
115 object_ref(OBJECT(dev));
116 bus_remove_child(dev->parent_bus, dev);
118 dev->parent_bus = bus;
119 object_ref(OBJECT(bus));
120 bus_add_child(bus, dev);
121 if (dev->realized) {
122 resettable_change_parent(OBJECT(dev), OBJECT(bus),
123 OBJECT(old_parent_bus));
125 if (old_parent_bus) {
126 object_unref(OBJECT(old_parent_bus));
127 object_unref(OBJECT(dev));
131 /* Create a new device. This only initializes the device state
132 structure and allows properties to be set. The device still needs
133 to be realized. See qdev-core.h. */
134 DeviceState *qdev_create(BusState *bus, const char *name)
136 DeviceState *dev;
138 dev = qdev_try_create(bus, name);
139 if (!dev) {
140 if (bus) {
141 error_report("Unknown device '%s' for bus '%s'", name,
142 object_get_typename(OBJECT(bus)));
143 } else {
144 error_report("Unknown device '%s' for default sysbus", name);
146 abort();
149 return dev;
152 DeviceState *qdev_try_create(BusState *bus, const char *type)
154 DeviceState *dev;
156 if (object_class_by_name(type) == NULL) {
157 return NULL;
159 dev = DEVICE(object_new(type));
160 if (!dev) {
161 return NULL;
164 if (!bus) {
165 /* Assert that the device really is a SysBusDevice before
166 * we put it onto the sysbus. Non-sysbus devices which aren't
167 * being put onto a bus should be created with object_new(TYPE_FOO),
168 * not qdev_create(NULL, TYPE_FOO).
170 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
171 bus = sysbus_get_default();
174 qdev_set_parent_bus(dev, bus);
175 object_unref(OBJECT(dev));
176 return dev;
179 static QTAILQ_HEAD(, DeviceListener) device_listeners
180 = QTAILQ_HEAD_INITIALIZER(device_listeners);
182 enum ListenerDirection { Forward, Reverse };
184 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
185 do { \
186 DeviceListener *_listener; \
188 switch (_direction) { \
189 case Forward: \
190 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
191 if (_listener->_callback) { \
192 _listener->_callback(_listener, ##_args); \
195 break; \
196 case Reverse: \
197 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
198 link) { \
199 if (_listener->_callback) { \
200 _listener->_callback(_listener, ##_args); \
203 break; \
204 default: \
205 abort(); \
207 } while (0)
209 static int device_listener_add(DeviceState *dev, void *opaque)
211 DEVICE_LISTENER_CALL(realize, Forward, dev);
213 return 0;
216 void device_listener_register(DeviceListener *listener)
218 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
220 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
221 NULL, NULL);
224 void device_listener_unregister(DeviceListener *listener)
226 QTAILQ_REMOVE(&device_listeners, listener, link);
229 bool qdev_should_hide_device(QemuOpts *opts)
231 int rc = -1;
232 DeviceListener *listener;
234 QTAILQ_FOREACH(listener, &device_listeners, link) {
235 if (listener->should_be_hidden) {
237 * should_be_hidden_will return
238 * 1 if device matches opts and it should be hidden
239 * 0 if device matches opts and should not be hidden
240 * -1 if device doesn't match ops
242 rc = listener->should_be_hidden(listener, opts);
245 if (rc > 0) {
246 break;
250 return rc > 0;
253 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
254 int required_for_version)
256 assert(!dev->realized);
257 dev->instance_id_alias = alias_id;
258 dev->alias_required_for_version = required_for_version;
261 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
263 MachineState *machine;
264 MachineClass *mc;
265 Object *m_obj = qdev_get_machine();
267 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
268 machine = MACHINE(m_obj);
269 mc = MACHINE_GET_CLASS(machine);
270 if (mc->get_hotplug_handler) {
271 return mc->get_hotplug_handler(machine, dev);
275 return NULL;
278 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
280 MachineState *machine;
281 MachineClass *mc;
282 Object *m_obj = qdev_get_machine();
284 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
285 machine = MACHINE(m_obj);
286 mc = MACHINE_GET_CLASS(machine);
287 if (mc->hotplug_allowed) {
288 return mc->hotplug_allowed(machine, dev, errp);
292 return true;
295 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
297 if (dev->parent_bus) {
298 return dev->parent_bus->hotplug_handler;
300 return NULL;
303 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
305 HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
307 if (hotplug_ctrl == NULL && dev->parent_bus) {
308 hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
310 return hotplug_ctrl;
313 static int qdev_prereset(DeviceState *dev, void *opaque)
315 trace_qdev_reset_tree(dev, object_get_typename(OBJECT(dev)));
316 return 0;
319 static int qbus_prereset(BusState *bus, void *opaque)
321 trace_qbus_reset_tree(bus, object_get_typename(OBJECT(bus)));
322 return 0;
325 static int qdev_reset_one(DeviceState *dev, void *opaque)
327 device_legacy_reset(dev);
329 return 0;
332 static int qbus_reset_one(BusState *bus, void *opaque)
334 BusClass *bc = BUS_GET_CLASS(bus);
335 trace_qbus_reset(bus, object_get_typename(OBJECT(bus)));
336 if (bc->reset) {
337 bc->reset(bus);
339 return 0;
342 void qdev_reset_all(DeviceState *dev)
344 trace_qdev_reset_all(dev, object_get_typename(OBJECT(dev)));
345 qdev_walk_children(dev, qdev_prereset, qbus_prereset,
346 qdev_reset_one, qbus_reset_one, NULL);
349 void qdev_reset_all_fn(void *opaque)
351 qdev_reset_all(DEVICE(opaque));
354 void qbus_reset_all(BusState *bus)
356 trace_qbus_reset_all(bus, object_get_typename(OBJECT(bus)));
357 qbus_walk_children(bus, qdev_prereset, qbus_prereset,
358 qdev_reset_one, qbus_reset_one, NULL);
361 void qbus_reset_all_fn(void *opaque)
363 BusState *bus = opaque;
364 qbus_reset_all(bus);
367 void device_cold_reset(DeviceState *dev)
369 resettable_reset(OBJECT(dev), RESET_TYPE_COLD);
372 bool device_is_in_reset(DeviceState *dev)
374 return resettable_is_in_reset(OBJECT(dev));
377 static ResettableState *device_get_reset_state(Object *obj)
379 DeviceState *dev = DEVICE(obj);
380 return &dev->reset;
383 static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
384 void *opaque, ResetType type)
386 DeviceState *dev = DEVICE(obj);
387 BusState *bus;
389 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
390 cb(OBJECT(bus), opaque, type);
394 /* can be used as ->unplug() callback for the simple cases */
395 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
396 DeviceState *dev, Error **errp)
398 object_property_set_bool(OBJECT(dev), false, "realized", &error_abort);
402 * Realize @dev.
403 * Device properties should be set before calling this function. IRQs
404 * and MMIO regions should be connected/mapped after calling this
405 * function.
406 * On failure, report an error with error_report() and terminate the
407 * program. This is okay during machine creation. Don't use for
408 * hotplug, because there callers need to recover from failure.
409 * Exception: if you know the device's init() callback can't fail,
410 * then qdev_init_nofail() can't fail either, and is therefore usable
411 * even then. But relying on the device implementation that way is
412 * somewhat unclean, and best avoided.
414 void qdev_init_nofail(DeviceState *dev)
416 Error *err = NULL;
418 assert(!dev->realized);
420 object_ref(OBJECT(dev));
421 object_property_set_bool(OBJECT(dev), true, "realized", &err);
422 if (err) {
423 error_reportf_err(err, "Initialization of device %s failed: ",
424 object_get_typename(OBJECT(dev)));
425 exit(1);
427 object_unref(OBJECT(dev));
430 static int qdev_assert_realized_properly(Object *obj, void *opaque)
432 DeviceState *dev = DEVICE(object_dynamic_cast(obj, TYPE_DEVICE));
433 DeviceClass *dc;
435 if (dev) {
436 dc = DEVICE_GET_CLASS(dev);
437 assert(dev->realized);
438 assert(dev->parent_bus || !dc->bus_type);
440 return 0;
443 void qdev_machine_creation_done(void)
446 * ok, initial machine setup is done, starting from now we can
447 * only create hotpluggable devices
449 qdev_hotplug = true;
451 object_child_foreach_recursive(object_get_root(),
452 qdev_assert_realized_properly, NULL);
455 bool qdev_machine_modified(void)
457 return qdev_hot_added || qdev_hot_removed;
460 BusState *qdev_get_parent_bus(DeviceState *dev)
462 return dev->parent_bus;
465 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
466 const char *name)
468 NamedGPIOList *ngl;
470 QLIST_FOREACH(ngl, &dev->gpios, node) {
471 /* NULL is a valid and matchable name. */
472 if (g_strcmp0(name, ngl->name) == 0) {
473 return ngl;
477 ngl = g_malloc0(sizeof(*ngl));
478 ngl->name = g_strdup(name);
479 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
480 return ngl;
483 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
484 qemu_irq_handler handler,
485 void *opaque,
486 const char *name, int n)
488 int i;
489 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
491 assert(gpio_list->num_out == 0 || !name);
492 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
493 opaque, n);
495 if (!name) {
496 name = "unnamed-gpio-in";
498 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
499 gchar *propname = g_strdup_printf("%s[%u]", name, i);
501 object_property_add_child(OBJECT(dev), propname,
502 OBJECT(gpio_list->in[i]));
503 g_free(propname);
506 gpio_list->num_in += n;
509 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
511 qdev_init_gpio_in_named(dev, handler, NULL, n);
514 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
515 const char *name, int n)
517 int i;
518 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
520 assert(gpio_list->num_in == 0 || !name);
522 if (!name) {
523 name = "unnamed-gpio-out";
525 memset(pins, 0, sizeof(*pins) * n);
526 for (i = 0; i < n; ++i) {
527 gchar *propname = g_strdup_printf("%s[%u]", name,
528 gpio_list->num_out + i);
530 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
531 (Object **)&pins[i],
532 object_property_allow_set_link,
533 OBJ_PROP_LINK_STRONG);
534 g_free(propname);
536 gpio_list->num_out += n;
539 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
541 qdev_init_gpio_out_named(dev, pins, NULL, n);
544 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
546 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
548 assert(n >= 0 && n < gpio_list->num_in);
549 return gpio_list->in[n];
552 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
554 return qdev_get_gpio_in_named(dev, NULL, n);
557 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
558 qemu_irq pin)
560 char *propname = g_strdup_printf("%s[%d]",
561 name ? name : "unnamed-gpio-out", n);
562 if (pin && !OBJECT(pin)->parent) {
563 /* We need a name for object_property_set_link to work */
564 object_property_add_child(container_get(qdev_get_machine(),
565 "/unattached"),
566 "non-qdev-gpio[*]", OBJECT(pin));
568 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
569 g_free(propname);
572 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
574 g_autofree char *propname = g_strdup_printf("%s[%d]",
575 name ? name : "unnamed-gpio-out", n);
577 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
578 NULL);
580 return ret;
583 /* disconnect a GPIO output, returning the disconnected input (if any) */
585 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
586 const char *name, int n)
588 char *propname = g_strdup_printf("%s[%d]",
589 name ? name : "unnamed-gpio-out", n);
591 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
592 NULL);
593 if (ret) {
594 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
596 g_free(propname);
597 return ret;
600 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
601 const char *name, int n)
603 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
604 qdev_connect_gpio_out_named(dev, name, n, icpt);
605 return disconnected;
608 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
610 qdev_connect_gpio_out_named(dev, NULL, n, pin);
613 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
614 const char *name)
616 int i;
617 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
619 for (i = 0; i < ngl->num_in; i++) {
620 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
621 char *propname = g_strdup_printf("%s[%d]", nm, i);
623 object_property_add_alias(OBJECT(container), propname,
624 OBJECT(dev), propname);
625 g_free(propname);
627 for (i = 0; i < ngl->num_out; i++) {
628 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
629 char *propname = g_strdup_printf("%s[%d]", nm, i);
631 object_property_add_alias(OBJECT(container), propname,
632 OBJECT(dev), propname);
633 g_free(propname);
635 QLIST_REMOVE(ngl, node);
636 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
639 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
641 BusState *bus;
642 Object *child = object_resolve_path_component(OBJECT(dev), name);
644 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
645 if (bus) {
646 return bus;
649 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
650 if (strcmp(name, bus->name) == 0) {
651 return bus;
654 return NULL;
657 int qdev_walk_children(DeviceState *dev,
658 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
659 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
660 void *opaque)
662 BusState *bus;
663 int err;
665 if (pre_devfn) {
666 err = pre_devfn(dev, opaque);
667 if (err) {
668 return err;
672 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
673 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
674 post_devfn, post_busfn, opaque);
675 if (err < 0) {
676 return err;
680 if (post_devfn) {
681 err = post_devfn(dev, opaque);
682 if (err) {
683 return err;
687 return 0;
690 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
692 BusChild *kid;
693 DeviceState *ret;
694 BusState *child;
696 QTAILQ_FOREACH(kid, &bus->children, sibling) {
697 DeviceState *dev = kid->child;
699 if (dev->id && strcmp(dev->id, id) == 0) {
700 return dev;
703 QLIST_FOREACH(child, &dev->child_bus, sibling) {
704 ret = qdev_find_recursive(child, id);
705 if (ret) {
706 return ret;
710 return NULL;
713 char *qdev_get_dev_path(DeviceState *dev)
715 BusClass *bc;
717 if (!dev || !dev->parent_bus) {
718 return NULL;
721 bc = BUS_GET_CLASS(dev->parent_bus);
722 if (bc->get_dev_path) {
723 return bc->get_dev_path(dev);
726 return NULL;
730 * Legacy property handling
733 static void qdev_get_legacy_property(Object *obj, Visitor *v,
734 const char *name, void *opaque,
735 Error **errp)
737 DeviceState *dev = DEVICE(obj);
738 Property *prop = opaque;
740 char buffer[1024];
741 char *ptr = buffer;
743 prop->info->print(dev, prop, buffer, sizeof(buffer));
744 visit_type_str(v, name, &ptr, errp);
748 * qdev_class_add_legacy_property:
749 * @dev: Device to add the property to.
750 * @prop: The qdev property definition.
752 * Add a legacy QOM property to @dev for qdev property @prop.
754 * Legacy properties are string versions of QOM properties. The format of
755 * the string depends on the property type. Legacy properties are only
756 * needed for "info qtree".
758 * Do not use this in new code! QOM Properties added through this interface
759 * will be given names in the "legacy" namespace.
761 static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop)
763 g_autofree char *name = NULL;
765 /* Register pointer properties as legacy properties */
766 if (!prop->info->print && prop->info->get) {
767 return;
770 name = g_strdup_printf("legacy-%s", prop->name);
771 object_class_property_add(OBJECT_CLASS(dc), name, "str",
772 prop->info->print ? qdev_get_legacy_property : prop->info->get,
773 NULL, NULL, prop);
776 void qdev_property_add_static(DeviceState *dev, Property *prop)
778 Object *obj = OBJECT(dev);
779 ObjectProperty *op;
781 assert(!prop->info->create);
783 op = object_property_add(obj, prop->name, prop->info->name,
784 prop->info->get, prop->info->set,
785 prop->info->release,
786 prop);
788 object_property_set_description(obj, prop->name,
789 prop->info->description);
791 if (prop->set_default) {
792 prop->info->set_default_value(op, prop);
793 if (op->init) {
794 op->init(obj, op);
799 static void qdev_class_add_property(DeviceClass *klass, Property *prop)
801 ObjectClass *oc = OBJECT_CLASS(klass);
803 if (prop->info->create) {
804 prop->info->create(oc, prop);
805 } else {
806 ObjectProperty *op;
808 op = object_class_property_add(oc,
809 prop->name, prop->info->name,
810 prop->info->get, prop->info->set,
811 prop->info->release,
812 prop);
813 if (prop->set_default) {
814 prop->info->set_default_value(op, prop);
817 object_class_property_set_description(oc, prop->name,
818 prop->info->description);
821 /* @qdev_alias_all_properties - Add alias properties to the source object for
822 * all qdev properties on the target DeviceState.
824 void qdev_alias_all_properties(DeviceState *target, Object *source)
826 ObjectClass *class;
827 Property *prop;
829 class = object_get_class(OBJECT(target));
830 do {
831 DeviceClass *dc = DEVICE_CLASS(class);
833 for (prop = dc->props_; prop && prop->name; prop++) {
834 object_property_add_alias(source, prop->name,
835 OBJECT(target), prop->name);
837 class = object_class_get_parent(class);
838 } while (class != object_class_by_name(TYPE_DEVICE));
841 static bool device_get_realized(Object *obj, Error **errp)
843 DeviceState *dev = DEVICE(obj);
844 return dev->realized;
847 static bool check_only_migratable(Object *obj, Error **errp)
849 DeviceClass *dc = DEVICE_GET_CLASS(obj);
851 if (!vmstate_check_only_migratable(dc->vmsd)) {
852 error_setg(errp, "Device %s is not migratable, but "
853 "--only-migratable was specified",
854 object_get_typename(obj));
855 return false;
858 return true;
861 static void device_set_realized(Object *obj, bool value, Error **errp)
863 DeviceState *dev = DEVICE(obj);
864 DeviceClass *dc = DEVICE_GET_CLASS(dev);
865 HotplugHandler *hotplug_ctrl;
866 BusState *bus;
867 NamedClockList *ncl;
868 Error *local_err = NULL;
869 bool unattached_parent = false;
870 static int unattached_count;
872 if (dev->hotplugged && !dc->hotpluggable) {
873 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
874 return;
877 if (value && !dev->realized) {
878 if (!check_only_migratable(obj, &local_err)) {
879 goto fail;
882 if (!obj->parent) {
883 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
885 object_property_add_child(container_get(qdev_get_machine(),
886 "/unattached"),
887 name, obj);
888 unattached_parent = true;
889 g_free(name);
892 hotplug_ctrl = qdev_get_hotplug_handler(dev);
893 if (hotplug_ctrl) {
894 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
895 if (local_err != NULL) {
896 goto fail;
900 if (dc->realize) {
901 dc->realize(dev, &local_err);
902 if (local_err != NULL) {
903 goto fail;
907 DEVICE_LISTENER_CALL(realize, Forward, dev);
910 * always free/re-initialize here since the value cannot be cleaned up
911 * in device_unrealize due to its usage later on in the unplug path
913 g_free(dev->canonical_path);
914 dev->canonical_path = object_get_canonical_path(OBJECT(dev));
915 QLIST_FOREACH(ncl, &dev->clocks, node) {
916 if (ncl->alias) {
917 continue;
918 } else {
919 clock_setup_canonical_path(ncl->clock);
923 if (qdev_get_vmsd(dev)) {
924 if (vmstate_register_with_alias_id(VMSTATE_IF(dev),
925 VMSTATE_INSTANCE_ID_ANY,
926 qdev_get_vmsd(dev), dev,
927 dev->instance_id_alias,
928 dev->alias_required_for_version,
929 &local_err) < 0) {
930 goto post_realize_fail;
935 * Clear the reset state, in case the object was previously unrealized
936 * with a dirty state.
938 resettable_state_clear(&dev->reset);
940 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
941 object_property_set_bool(OBJECT(bus), true, "realized",
942 &local_err);
943 if (local_err != NULL) {
944 goto child_realize_fail;
947 if (dev->hotplugged) {
949 * Reset the device, as well as its subtree which, at this point,
950 * should be realized too.
952 resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD);
953 resettable_change_parent(OBJECT(dev), OBJECT(dev->parent_bus),
954 NULL);
955 resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD);
957 dev->pending_deleted_event = false;
959 if (hotplug_ctrl) {
960 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
961 if (local_err != NULL) {
962 goto child_realize_fail;
966 } else if (!value && dev->realized) {
967 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
968 object_property_set_bool(OBJECT(bus), false, "realized",
969 &error_abort);
971 if (qdev_get_vmsd(dev)) {
972 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
974 if (dc->unrealize) {
975 dc->unrealize(dev);
977 dev->pending_deleted_event = true;
978 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
981 assert(local_err == NULL);
982 dev->realized = value;
983 return;
985 child_realize_fail:
986 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
987 object_property_set_bool(OBJECT(bus), false, "realized",
988 &error_abort);
991 if (qdev_get_vmsd(dev)) {
992 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
995 post_realize_fail:
996 g_free(dev->canonical_path);
997 dev->canonical_path = NULL;
998 if (dc->unrealize) {
999 dc->unrealize(dev);
1002 fail:
1003 error_propagate(errp, local_err);
1004 if (unattached_parent) {
1005 object_unparent(OBJECT(dev));
1006 unattached_count--;
1010 static bool device_get_hotpluggable(Object *obj, Error **errp)
1012 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1013 DeviceState *dev = DEVICE(obj);
1015 return dc->hotpluggable && (dev->parent_bus == NULL ||
1016 qbus_is_hotpluggable(dev->parent_bus));
1019 static bool device_get_hotplugged(Object *obj, Error **errp)
1021 DeviceState *dev = DEVICE(obj);
1023 return dev->hotplugged;
1026 static void device_initfn(Object *obj)
1028 DeviceState *dev = DEVICE(obj);
1030 if (qdev_hotplug) {
1031 dev->hotplugged = 1;
1032 qdev_hot_added = true;
1035 dev->instance_id_alias = -1;
1036 dev->realized = false;
1037 dev->allow_unplug_during_migration = false;
1039 QLIST_INIT(&dev->gpios);
1040 QLIST_INIT(&dev->clocks);
1043 static void device_post_init(Object *obj)
1046 * Note: ordered so that the user's global properties take
1047 * precedence.
1049 object_apply_compat_props(obj);
1050 qdev_prop_set_globals(DEVICE(obj));
1053 /* Unlink device from bus and free the structure. */
1054 static void device_finalize(Object *obj)
1056 NamedGPIOList *ngl, *next;
1058 DeviceState *dev = DEVICE(obj);
1060 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1061 QLIST_REMOVE(ngl, node);
1062 qemu_free_irqs(ngl->in, ngl->num_in);
1063 g_free(ngl->name);
1064 g_free(ngl);
1065 /* ngl->out irqs are owned by the other end and should not be freed
1066 * here
1070 qdev_finalize_clocklist(dev);
1072 /* Only send event if the device had been completely realized */
1073 if (dev->pending_deleted_event) {
1074 g_assert(dev->canonical_path);
1076 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
1077 g_free(dev->canonical_path);
1078 dev->canonical_path = NULL;
1081 qemu_opts_del(dev->opts);
1084 static void device_class_base_init(ObjectClass *class, void *data)
1086 DeviceClass *klass = DEVICE_CLASS(class);
1088 /* We explicitly look up properties in the superclasses,
1089 * so do not propagate them to the subclasses.
1091 klass->props_ = NULL;
1094 static void device_unparent(Object *obj)
1096 DeviceState *dev = DEVICE(obj);
1097 BusState *bus;
1099 if (dev->realized) {
1100 object_property_set_bool(obj, false, "realized", &error_abort);
1102 while (dev->num_child_bus) {
1103 bus = QLIST_FIRST(&dev->child_bus);
1104 object_unparent(OBJECT(bus));
1106 if (dev->parent_bus) {
1107 bus_remove_child(dev->parent_bus, dev);
1108 object_unref(OBJECT(dev->parent_bus));
1109 dev->parent_bus = NULL;
1113 static char *
1114 device_vmstate_if_get_id(VMStateIf *obj)
1116 DeviceState *dev = DEVICE(obj);
1118 return qdev_get_dev_path(dev);
1122 * device_phases_reset:
1123 * Transition reset method for devices to allow moving
1124 * smoothly from legacy reset method to multi-phases
1126 static void device_phases_reset(DeviceState *dev)
1128 ResettableClass *rc = RESETTABLE_GET_CLASS(dev);
1130 if (rc->phases.enter) {
1131 rc->phases.enter(OBJECT(dev), RESET_TYPE_COLD);
1133 if (rc->phases.hold) {
1134 rc->phases.hold(OBJECT(dev));
1136 if (rc->phases.exit) {
1137 rc->phases.exit(OBJECT(dev));
1141 static void device_transitional_reset(Object *obj)
1143 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1146 * This will call either @device_phases_reset (for multi-phases transitioned
1147 * devices) or a device's specific method for not-yet transitioned devices.
1148 * In both case, it does not reset children.
1150 if (dc->reset) {
1151 dc->reset(DEVICE(obj));
1156 * device_get_transitional_reset:
1157 * check if the device's class is ready for multi-phase
1159 static ResettableTrFunction device_get_transitional_reset(Object *obj)
1161 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1162 if (dc->reset != device_phases_reset) {
1164 * dc->reset has been overridden by a subclass,
1165 * the device is not ready for multi phase yet.
1167 return device_transitional_reset;
1169 return NULL;
1172 static void device_class_init(ObjectClass *class, void *data)
1174 DeviceClass *dc = DEVICE_CLASS(class);
1175 VMStateIfClass *vc = VMSTATE_IF_CLASS(class);
1176 ResettableClass *rc = RESETTABLE_CLASS(class);
1178 class->unparent = device_unparent;
1180 /* by default all devices were considered as hotpluggable,
1181 * so with intent to check it in generic qdev_unplug() /
1182 * device_set_realized() functions make every device
1183 * hotpluggable. Devices that shouldn't be hotpluggable,
1184 * should override it in their class_init()
1186 dc->hotpluggable = true;
1187 dc->user_creatable = true;
1188 vc->get_id = device_vmstate_if_get_id;
1189 rc->get_state = device_get_reset_state;
1190 rc->child_foreach = device_reset_child_foreach;
1193 * @device_phases_reset is put as the default reset method below, allowing
1194 * to do the multi-phase transition from base classes to leaf classes. It
1195 * allows a legacy-reset Device class to extend a multi-phases-reset
1196 * Device class for the following reason:
1197 * + If a base class B has been moved to multi-phase, then it does not
1198 * override this default reset method and may have defined phase methods.
1199 * + A child class C (extending class B) which uses
1200 * device_class_set_parent_reset() (or similar means) to override the
1201 * reset method will still work as expected. @device_phases_reset function
1202 * will be registered as the parent reset method and effectively call
1203 * parent reset phases.
1205 dc->reset = device_phases_reset;
1206 rc->get_transitional_function = device_get_transitional_reset;
1208 object_class_property_add_bool(class, "realized",
1209 device_get_realized, device_set_realized);
1210 object_class_property_add_bool(class, "hotpluggable",
1211 device_get_hotpluggable, NULL);
1212 object_class_property_add_bool(class, "hotplugged",
1213 device_get_hotplugged, NULL);
1214 object_class_property_add_link(class, "parent_bus", TYPE_BUS,
1215 offsetof(DeviceState, parent_bus), NULL, 0);
1218 void device_class_set_props(DeviceClass *dc, Property *props)
1220 Property *prop;
1222 dc->props_ = props;
1223 for (prop = props; prop && prop->name; prop++) {
1224 qdev_class_add_legacy_property(dc, prop);
1225 qdev_class_add_property(dc, prop);
1229 void device_class_set_parent_reset(DeviceClass *dc,
1230 DeviceReset dev_reset,
1231 DeviceReset *parent_reset)
1233 *parent_reset = dc->reset;
1234 dc->reset = dev_reset;
1237 void device_class_set_parent_realize(DeviceClass *dc,
1238 DeviceRealize dev_realize,
1239 DeviceRealize *parent_realize)
1241 *parent_realize = dc->realize;
1242 dc->realize = dev_realize;
1245 void device_class_set_parent_unrealize(DeviceClass *dc,
1246 DeviceUnrealize dev_unrealize,
1247 DeviceUnrealize *parent_unrealize)
1249 *parent_unrealize = dc->unrealize;
1250 dc->unrealize = dev_unrealize;
1253 void device_legacy_reset(DeviceState *dev)
1255 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1257 trace_qdev_reset(dev, object_get_typename(OBJECT(dev)));
1258 if (klass->reset) {
1259 klass->reset(dev);
1263 Object *qdev_get_machine(void)
1265 static Object *dev;
1267 if (dev == NULL) {
1268 dev = container_get(object_get_root(), "/machine");
1271 return dev;
1274 static const TypeInfo device_type_info = {
1275 .name = TYPE_DEVICE,
1276 .parent = TYPE_OBJECT,
1277 .instance_size = sizeof(DeviceState),
1278 .instance_init = device_initfn,
1279 .instance_post_init = device_post_init,
1280 .instance_finalize = device_finalize,
1281 .class_base_init = device_class_base_init,
1282 .class_init = device_class_init,
1283 .abstract = true,
1284 .class_size = sizeof(DeviceClass),
1285 .interfaces = (InterfaceInfo[]) {
1286 { TYPE_VMSTATE_IF },
1287 { TYPE_RESETTABLE_INTERFACE },
1292 static void qdev_register_types(void)
1294 type_register_static(&device_type_info);
1297 type_init(qdev_register_types)