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
29 #include "sysemu/sysemu.h"
30 #include "qapi/error.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/visitor.h"
33 #include "qapi/qmp/qjson.h"
34 #include "monitor/monitor.h"
35 #include "hw/hotplug.h"
38 static bool qdev_hot_added = false;
39 static bool qdev_hot_removed = false;
41 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
43 DeviceClass *dc = DEVICE_GET_CLASS(dev);
47 const char *qdev_fw_name(DeviceState *dev)
49 DeviceClass *dc = DEVICE_GET_CLASS(dev);
55 return object_get_typename(OBJECT(dev));
58 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
61 static void bus_remove_child(BusState *bus, DeviceState *child)
65 QTAILQ_FOREACH(kid, &bus->children, sibling) {
66 if (kid->child == child) {
69 snprintf(name, sizeof(name), "child[%d]", kid->index);
70 QTAILQ_REMOVE(&bus->children, kid, sibling);
72 /* This gives back ownership of kid->child back to us. */
73 object_property_del(OBJECT(bus), name, NULL);
74 object_unref(OBJECT(kid->child));
81 static void bus_add_child(BusState *bus, DeviceState *child)
84 BusChild *kid = g_malloc0(sizeof(*kid));
87 assert(bus->allow_hotplug);
90 kid->index = bus->max_index++;
92 object_ref(OBJECT(kid->child));
94 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
96 /* This transfers ownership of kid->child to the property. */
97 snprintf(name, sizeof(name), "child[%d]", kid->index);
98 object_property_add_link(OBJECT(bus), name,
99 object_get_typename(OBJECT(child)),
100 (Object **)&kid->child,
104 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
106 dev->parent_bus = bus;
107 object_ref(OBJECT(bus));
108 bus_add_child(bus, dev);
111 /* Create a new device. This only initializes the device state structure
112 and allows properties to be set. qdev_init should be called to
113 initialize the actual device emulation. */
114 DeviceState *qdev_create(BusState *bus, const char *name)
118 dev = qdev_try_create(bus, name);
121 error_report("Unknown device '%s' for bus '%s'", name,
122 object_get_typename(OBJECT(bus)));
124 error_report("Unknown device '%s' for default sysbus", name);
132 DeviceState *qdev_try_create(BusState *bus, const char *type)
136 if (object_class_by_name(type) == NULL) {
139 dev = DEVICE(object_new(type));
145 bus = sysbus_get_default();
148 qdev_set_parent_bus(dev, bus);
149 object_unref(OBJECT(dev));
153 /* Initialize a device. Device properties should be set before calling
154 this function. IRQs and MMIO regions should be connected/mapped after
155 calling this function.
156 On failure, destroy the device and return negative value.
157 Return 0 on success. */
158 int qdev_init(DeviceState *dev)
160 Error *local_err = NULL;
162 assert(!dev->realized);
164 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
165 if (local_err != NULL) {
166 qerror_report_err(local_err);
167 error_free(local_err);
168 object_unparent(OBJECT(dev));
174 static void device_realize(DeviceState *dev, Error **err)
176 DeviceClass *dc = DEVICE_GET_CLASS(dev);
179 int rc = dc->init(dev);
181 error_setg(err, "Device initialization failed.");
187 static void device_unrealize(DeviceState *dev, Error **errp)
189 DeviceClass *dc = DEVICE_GET_CLASS(dev);
192 int rc = dc->exit(dev);
194 error_setg(errp, "Device exit failed.");
200 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
201 int required_for_version)
203 assert(!dev->realized);
204 dev->instance_id_alias = alias_id;
205 dev->alias_required_for_version = required_for_version;
208 void qdev_unplug(DeviceState *dev, Error **errp)
210 DeviceClass *dc = DEVICE_GET_CLASS(dev);
212 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
213 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
217 if (!dc->hotpluggable) {
218 error_set(errp, QERR_DEVICE_NO_HOTPLUG,
219 object_get_typename(OBJECT(dev)));
223 qdev_hot_removed = true;
225 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
226 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
228 assert(dc->unplug != NULL);
229 if (dc->unplug(dev) < 0) { /* legacy handler */
230 error_set(errp, QERR_UNDEFINED_ERROR);
235 static int qdev_reset_one(DeviceState *dev, void *opaque)
242 static int qbus_reset_one(BusState *bus, void *opaque)
244 BusClass *bc = BUS_GET_CLASS(bus);
251 void qdev_reset_all(DeviceState *dev)
253 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
256 void qbus_reset_all(BusState *bus)
258 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
261 void qbus_reset_all_fn(void *opaque)
263 BusState *bus = opaque;
267 /* can be used as ->unplug() callback for the simple cases */
268 int qdev_simple_unplug_cb(DeviceState *dev)
271 object_unparent(OBJECT(dev));
276 /* Like qdev_init(), but terminate program via error_report() instead of
277 returning an error value. This is okay during machine creation.
278 Don't use for hotplug, because there callers need to recover from
279 failure. Exception: if you know the device's init() callback can't
280 fail, then qdev_init_nofail() can't fail either, and is therefore
281 usable even then. But relying on the device implementation that
282 way is somewhat unclean, and best avoided. */
283 void qdev_init_nofail(DeviceState *dev)
285 const char *typename = object_get_typename(OBJECT(dev));
287 if (qdev_init(dev) < 0) {
288 error_report("Initialization of device %s failed", typename);
293 void qdev_machine_creation_done(void)
296 * ok, initial machine setup is done, starting from now we can
297 * only create hotpluggable devices
302 bool qdev_machine_modified(void)
304 return qdev_hot_added || qdev_hot_removed;
307 BusState *qdev_get_parent_bus(DeviceState *dev)
309 return dev->parent_bus;
312 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
314 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
316 dev->num_gpio_in += n;
319 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
321 assert(dev->num_gpio_out == 0);
322 dev->num_gpio_out = n;
323 dev->gpio_out = pins;
326 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
328 assert(n >= 0 && n < dev->num_gpio_in);
329 return dev->gpio_in[n];
332 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
334 assert(n >= 0 && n < dev->num_gpio_out);
335 dev->gpio_out[n] = pin;
338 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
342 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
343 if (strcmp(name, bus->name) == 0) {
350 int qbus_walk_children(BusState *bus,
351 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
352 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
359 err = pre_busfn(bus, opaque);
365 QTAILQ_FOREACH(kid, &bus->children, sibling) {
366 err = qdev_walk_children(kid->child,
367 pre_devfn, pre_busfn,
368 post_devfn, post_busfn, opaque);
375 err = post_busfn(bus, opaque);
384 int qdev_walk_children(DeviceState *dev,
385 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
386 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
393 err = pre_devfn(dev, opaque);
399 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
400 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
401 post_devfn, post_busfn, opaque);
408 err = post_devfn(dev, opaque);
417 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
423 QTAILQ_FOREACH(kid, &bus->children, sibling) {
424 DeviceState *dev = kid->child;
426 if (dev->id && strcmp(dev->id, id) == 0) {
430 QLIST_FOREACH(child, &dev->child_bus, sibling) {
431 ret = qdev_find_recursive(child, id);
440 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
442 const char *typename = object_get_typename(OBJECT(bus));
447 bus->parent = parent;
450 bus->name = g_strdup(name);
451 } else if (bus->parent && bus->parent->id) {
452 /* parent device has id -> use it plus parent-bus-id for bus name */
453 bus_id = bus->parent->num_child_bus;
455 len = strlen(bus->parent->id) + 16;
457 snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
460 /* no id -> use lowercase bus type plus global bus-id for bus name */
461 bc = BUS_GET_CLASS(bus);
462 bus_id = bc->automatic_ids++;
464 len = strlen(typename) + 16;
466 len = snprintf(buf, len, "%s.%d", typename, bus_id);
467 for (i = 0; i < len; i++) {
468 buf[i] = qemu_tolower(buf[i]);
474 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
475 bus->parent->num_child_bus++;
476 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
477 object_unref(OBJECT(bus));
478 } else if (bus != sysbus_get_default()) {
479 /* TODO: once all bus devices are qdevified,
480 only reset handler for main_system_bus should be registered here. */
481 qemu_register_reset(qbus_reset_all_fn, bus);
485 static void bus_unparent(Object *obj)
487 BusState *bus = BUS(obj);
490 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
491 DeviceState *dev = kid->child;
492 object_unparent(OBJECT(dev));
495 QLIST_REMOVE(bus, sibling);
496 bus->parent->num_child_bus--;
499 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
500 qemu_unregister_reset(qbus_reset_all_fn, bus);
504 static bool bus_get_realized(Object *obj, Error **err)
506 BusState *bus = BUS(obj);
508 return bus->realized;
511 static void bus_set_realized(Object *obj, bool value, Error **err)
513 BusState *bus = BUS(obj);
514 BusClass *bc = BUS_GET_CLASS(bus);
515 Error *local_err = NULL;
517 if (value && !bus->realized) {
519 bc->realize(bus, &local_err);
521 if (local_err != NULL) {
526 } else if (!value && bus->realized) {
528 bc->unrealize(bus, &local_err);
530 if (local_err != NULL) {
536 bus->realized = value;
540 error_propagate(err, local_err);
543 void qbus_create_inplace(void *bus, size_t size, const char *typename,
544 DeviceState *parent, const char *name)
546 object_initialize(bus, size, typename);
547 qbus_realize(bus, parent, name);
550 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
554 bus = BUS(object_new(typename));
555 qbus_realize(bus, parent, name);
560 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
562 BusClass *bc = BUS_GET_CLASS(bus);
564 if (bc->get_fw_dev_path) {
565 return bc->get_fw_dev_path(dev);
571 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
575 if (dev && dev->parent_bus) {
577 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
578 d = bus_get_fw_dev_path(dev->parent_bus, dev);
580 l += snprintf(p + l, size - l, "%s", d);
586 l += snprintf(p + l , size - l, "/");
591 char* qdev_get_fw_dev_path(DeviceState *dev)
596 l = qdev_get_fw_dev_path_helper(dev, path, 128);
600 return g_strdup(path);
603 char *qdev_get_dev_path(DeviceState *dev)
607 if (!dev || !dev->parent_bus) {
611 bc = BUS_GET_CLASS(dev->parent_bus);
612 if (bc->get_dev_path) {
613 return bc->get_dev_path(dev);
620 * Legacy property handling
623 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
624 const char *name, Error **errp)
626 DeviceState *dev = DEVICE(obj);
627 Property *prop = opaque;
632 prop->info->print(dev, prop, buffer, sizeof(buffer));
633 visit_type_str(v, &ptr, name, errp);
637 * @qdev_add_legacy_property - adds a legacy property
639 * Do not use this is new code! Properties added through this interface will
640 * be given names and types in the "legacy" namespace.
642 * Legacy properties are string versions of other OOM properties. The format
643 * of the string depends on the property type.
645 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
650 /* Register pointer properties as legacy properties */
651 if (!prop->info->print && prop->info->get) {
655 name = g_strdup_printf("legacy-%s", prop->name);
656 object_property_add(OBJECT(dev), name, "str",
657 prop->info->print ? qdev_get_legacy_property : prop->info->get,
666 * @qdev_property_add_static - add a @Property to a device.
668 * Static properties access data in a struct. The actual type of the
669 * property and the field depends on the property type.
671 void qdev_property_add_static(DeviceState *dev, Property *prop,
674 Error *local_err = NULL;
675 Object *obj = OBJECT(dev);
678 * TODO qdev_prop_ptr does not have getters or setters. It must
679 * go now that it can be replaced with links. The test should be
680 * removed along with it: all static properties are read/write.
682 if (!prop->info->get && !prop->info->set) {
686 object_property_add(obj, prop->name, prop->info->name,
687 prop->info->get, prop->info->set,
692 error_propagate(errp, local_err);
695 if (prop->qtype == QTYPE_NONE) {
699 if (prop->qtype == QTYPE_QBOOL) {
700 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
701 } else if (prop->info->enum_table) {
702 object_property_set_str(obj, prop->info->enum_table[prop->defval],
703 prop->name, &error_abort);
704 } else if (prop->qtype == QTYPE_QINT) {
705 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
709 static bool device_get_realized(Object *obj, Error **err)
711 DeviceState *dev = DEVICE(obj);
712 return dev->realized;
715 static void device_set_realized(Object *obj, bool value, Error **err)
717 DeviceState *dev = DEVICE(obj);
718 DeviceClass *dc = DEVICE_GET_CLASS(dev);
720 Error *local_err = NULL;
722 if (dev->hotplugged && !dc->hotpluggable) {
723 error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
727 if (value && !dev->realized) {
728 if (!obj->parent && local_err == NULL) {
729 static int unattached_count;
730 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
732 object_property_add_child(container_get(qdev_get_machine(),
734 name, obj, &local_err);
739 dc->realize(dev, &local_err);
742 if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
744 hotplug_handler_plug(dev->parent_bus->hotplug_handler,
748 if (qdev_get_vmsd(dev) && local_err == NULL) {
749 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
750 dev->instance_id_alias,
751 dev->alias_required_for_version);
753 if (local_err == NULL) {
754 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
755 object_property_set_bool(OBJECT(bus), true, "realized",
757 if (local_err != NULL) {
762 if (dev->hotplugged && local_err == NULL) {
765 } else if (!value && dev->realized) {
766 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
767 object_property_set_bool(OBJECT(bus), false, "realized",
769 if (local_err != NULL) {
773 if (qdev_get_vmsd(dev) && local_err == NULL) {
774 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
776 if (dc->unrealize && local_err == NULL) {
777 dc->unrealize(dev, &local_err);
781 if (local_err != NULL) {
782 error_propagate(err, local_err);
786 dev->realized = value;
789 static bool device_get_hotpluggable(Object *obj, Error **err)
791 DeviceClass *dc = DEVICE_GET_CLASS(obj);
792 DeviceState *dev = DEVICE(obj);
794 return dc->hotpluggable && (dev->parent_bus == NULL ||
795 dev->parent_bus->allow_hotplug);
798 static void device_initfn(Object *obj)
800 DeviceState *dev = DEVICE(obj);
806 qdev_hot_added = true;
809 dev->instance_id_alias = -1;
810 dev->realized = false;
812 object_property_add_bool(obj, "realized",
813 device_get_realized, device_set_realized, NULL);
814 object_property_add_bool(obj, "hotpluggable",
815 device_get_hotpluggable, NULL, NULL);
817 class = object_get_class(OBJECT(dev));
819 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
820 qdev_property_add_legacy(dev, prop, &error_abort);
821 qdev_property_add_static(dev, prop, &error_abort);
823 class = object_class_get_parent(class);
824 } while (class != object_class_by_name(TYPE_DEVICE));
826 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
827 (Object **)&dev->parent_bus, &error_abort);
830 static void device_post_init(Object *obj)
832 qdev_prop_set_globals(DEVICE(obj), &error_abort);
835 /* Unlink device from bus and free the structure. */
836 static void device_finalize(Object *obj)
838 DeviceState *dev = DEVICE(obj);
840 qemu_opts_del(dev->opts);
844 static void device_class_base_init(ObjectClass *class, void *data)
846 DeviceClass *klass = DEVICE_CLASS(class);
848 /* We explicitly look up properties in the superclasses,
849 * so do not propagate them to the subclasses.
854 static void device_unparent(Object *obj)
856 DeviceState *dev = DEVICE(obj);
859 bool have_realized = dev->realized;
862 object_property_set_bool(obj, false, "realized", NULL);
864 while (dev->num_child_bus) {
865 bus = QLIST_FIRST(&dev->child_bus);
866 object_unparent(OBJECT(bus));
868 if (dev->parent_bus) {
869 bus_remove_child(dev->parent_bus, dev);
870 object_unref(OBJECT(dev->parent_bus));
871 dev->parent_bus = NULL;
874 /* Only send event if the device had been completely realized */
876 gchar *path = object_get_canonical_path(OBJECT(dev));
879 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
882 event_data = qobject_from_jsonf("{ 'path': %s }", path);
884 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
885 qobject_decref(event_data);
890 static void device_class_init(ObjectClass *class, void *data)
892 DeviceClass *dc = DEVICE_CLASS(class);
894 class->unparent = device_unparent;
895 dc->realize = device_realize;
896 dc->unrealize = device_unrealize;
898 /* by default all devices were considered as hotpluggable,
899 * so with intent to check it in generic qdev_unplug() /
900 * device_set_realized() functions make every device
901 * hotpluggable. Devices that shouldn't be hotpluggable,
902 * should override it in their class_init()
904 dc->hotpluggable = true;
907 void device_reset(DeviceState *dev)
909 DeviceClass *klass = DEVICE_GET_CLASS(dev);
916 Object *qdev_get_machine(void)
921 dev = container_get(object_get_root(), "/machine");
927 static const TypeInfo device_type_info = {
929 .parent = TYPE_OBJECT,
930 .instance_size = sizeof(DeviceState),
931 .instance_init = device_initfn,
932 .instance_post_init = device_post_init,
933 .instance_finalize = device_finalize,
934 .class_base_init = device_class_base_init,
935 .class_init = device_class_init,
937 .class_size = sizeof(DeviceClass),
940 static void qbus_initfn(Object *obj)
942 BusState *bus = BUS(obj);
944 QTAILQ_INIT(&bus->children);
945 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
946 TYPE_HOTPLUG_HANDLER,
947 (Object **)&bus->hotplug_handler, NULL);
948 object_property_add_bool(obj, "realized",
949 bus_get_realized, bus_set_realized, NULL);
952 static char *default_bus_get_fw_dev_path(DeviceState *dev)
954 return g_strdup(object_get_typename(OBJECT(dev)));
957 static void bus_class_init(ObjectClass *class, void *data)
959 BusClass *bc = BUS_CLASS(class);
961 class->unparent = bus_unparent;
962 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
965 static void qbus_finalize(Object *obj)
967 BusState *bus = BUS(obj);
969 g_free((char *)bus->name);
972 static const TypeInfo bus_info = {
974 .parent = TYPE_OBJECT,
975 .instance_size = sizeof(BusState),
977 .class_size = sizeof(BusClass),
978 .instance_init = qbus_initfn,
979 .instance_finalize = qbus_finalize,
980 .class_init = bus_class_init,
983 static void qdev_register_types(void)
985 type_register_static(&bus_info);
986 type_register_static(&device_type_info);
989 type_init(qdev_register_types)