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 "hw/fw-path-provider.h"
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "monitor/monitor.h"
36 #include "hw/hotplug.h"
39 static bool qdev_hot_added = false;
40 static bool qdev_hot_removed = false;
42 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
44 DeviceClass *dc = DEVICE_GET_CLASS(dev);
48 const char *qdev_fw_name(DeviceState *dev)
50 DeviceClass *dc = DEVICE_GET_CLASS(dev);
56 return object_get_typename(OBJECT(dev));
59 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
62 static void bus_remove_child(BusState *bus, DeviceState *child)
66 QTAILQ_FOREACH(kid, &bus->children, sibling) {
67 if (kid->child == child) {
70 snprintf(name, sizeof(name), "child[%d]", kid->index);
71 QTAILQ_REMOVE(&bus->children, kid, sibling);
73 /* This gives back ownership of kid->child back to us. */
74 object_property_del(OBJECT(bus), name, NULL);
75 object_unref(OBJECT(kid->child));
82 static void bus_add_child(BusState *bus, DeviceState *child)
85 BusChild *kid = g_malloc0(sizeof(*kid));
88 assert(bus->allow_hotplug);
91 kid->index = bus->max_index++;
93 object_ref(OBJECT(kid->child));
95 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
97 /* This transfers ownership of kid->child to the property. */
98 snprintf(name, sizeof(name), "child[%d]", kid->index);
99 object_property_add_link(OBJECT(bus), name,
100 object_get_typename(OBJECT(child)),
101 (Object **)&kid->child,
102 NULL, /* read-only property */
103 0, /* return ownership on prop deletion */
107 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
109 dev->parent_bus = bus;
110 object_ref(OBJECT(bus));
111 bus_add_child(bus, dev);
114 /* Create a new device. This only initializes the device state structure
115 and allows properties to be set. qdev_init should be called to
116 initialize the actual device emulation. */
117 DeviceState *qdev_create(BusState *bus, const char *name)
121 dev = qdev_try_create(bus, name);
124 error_report("Unknown device '%s' for bus '%s'", name,
125 object_get_typename(OBJECT(bus)));
127 error_report("Unknown device '%s' for default sysbus", name);
135 DeviceState *qdev_try_create(BusState *bus, const char *type)
139 if (object_class_by_name(type) == NULL) {
142 dev = DEVICE(object_new(type));
148 bus = sysbus_get_default();
151 qdev_set_parent_bus(dev, bus);
152 object_unref(OBJECT(dev));
156 /* Initialize a device. Device properties should be set before calling
157 this function. IRQs and MMIO regions should be connected/mapped after
158 calling this function.
159 On failure, destroy the device and return negative value.
160 Return 0 on success. */
161 int qdev_init(DeviceState *dev)
163 Error *local_err = NULL;
165 assert(!dev->realized);
167 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
168 if (local_err != NULL) {
169 qerror_report_err(local_err);
170 error_free(local_err);
171 object_unparent(OBJECT(dev));
177 static void device_realize(DeviceState *dev, Error **errp)
179 DeviceClass *dc = DEVICE_GET_CLASS(dev);
182 int rc = dc->init(dev);
184 error_setg(errp, "Device initialization failed.");
190 static void device_unrealize(DeviceState *dev, Error **errp)
192 DeviceClass *dc = DEVICE_GET_CLASS(dev);
195 int rc = dc->exit(dev);
197 error_setg(errp, "Device exit failed.");
203 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
204 int required_for_version)
206 assert(!dev->realized);
207 dev->instance_id_alias = alias_id;
208 dev->alias_required_for_version = required_for_version;
211 void qdev_unplug(DeviceState *dev, Error **errp)
213 DeviceClass *dc = DEVICE_GET_CLASS(dev);
215 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
216 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
220 if (!dc->hotpluggable) {
221 error_set(errp, QERR_DEVICE_NO_HOTPLUG,
222 object_get_typename(OBJECT(dev)));
226 qdev_hot_removed = true;
228 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
229 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
231 assert(dc->unplug != NULL);
232 if (dc->unplug(dev) < 0) { /* legacy handler */
233 error_set(errp, QERR_UNDEFINED_ERROR);
238 static int qdev_reset_one(DeviceState *dev, void *opaque)
245 static int qbus_reset_one(BusState *bus, void *opaque)
247 BusClass *bc = BUS_GET_CLASS(bus);
254 void qdev_reset_all(DeviceState *dev)
256 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
259 void qbus_reset_all(BusState *bus)
261 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
264 void qbus_reset_all_fn(void *opaque)
266 BusState *bus = opaque;
270 /* can be used as ->unplug() callback for the simple cases */
271 int qdev_simple_unplug_cb(DeviceState *dev)
274 object_unparent(OBJECT(dev));
279 /* Like qdev_init(), but terminate program via error_report() instead of
280 returning an error value. This is okay during machine creation.
281 Don't use for hotplug, because there callers need to recover from
282 failure. Exception: if you know the device's init() callback can't
283 fail, then qdev_init_nofail() can't fail either, and is therefore
284 usable even then. But relying on the device implementation that
285 way is somewhat unclean, and best avoided. */
286 void qdev_init_nofail(DeviceState *dev)
288 const char *typename = object_get_typename(OBJECT(dev));
290 if (qdev_init(dev) < 0) {
291 error_report("Initialization of device %s failed", typename);
296 void qdev_machine_creation_done(void)
299 * ok, initial machine setup is done, starting from now we can
300 * only create hotpluggable devices
305 bool qdev_machine_modified(void)
307 return qdev_hot_added || qdev_hot_removed;
310 BusState *qdev_get_parent_bus(DeviceState *dev)
312 return dev->parent_bus;
315 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
317 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
319 dev->num_gpio_in += n;
322 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
324 assert(dev->num_gpio_out == 0);
325 dev->num_gpio_out = n;
326 dev->gpio_out = pins;
329 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
331 assert(n >= 0 && n < dev->num_gpio_in);
332 return dev->gpio_in[n];
335 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
337 assert(n >= 0 && n < dev->num_gpio_out);
338 dev->gpio_out[n] = pin;
341 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
345 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
346 if (strcmp(name, bus->name) == 0) {
353 int qbus_walk_children(BusState *bus,
354 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
355 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
362 err = pre_busfn(bus, opaque);
368 QTAILQ_FOREACH(kid, &bus->children, sibling) {
369 err = qdev_walk_children(kid->child,
370 pre_devfn, pre_busfn,
371 post_devfn, post_busfn, opaque);
378 err = post_busfn(bus, opaque);
387 int qdev_walk_children(DeviceState *dev,
388 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
389 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
396 err = pre_devfn(dev, opaque);
402 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
403 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
404 post_devfn, post_busfn, opaque);
411 err = post_devfn(dev, opaque);
420 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
426 QTAILQ_FOREACH(kid, &bus->children, sibling) {
427 DeviceState *dev = kid->child;
429 if (dev->id && strcmp(dev->id, id) == 0) {
433 QLIST_FOREACH(child, &dev->child_bus, sibling) {
434 ret = qdev_find_recursive(child, id);
443 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
445 const char *typename = object_get_typename(OBJECT(bus));
450 bus->parent = parent;
453 bus->name = g_strdup(name);
454 } else if (bus->parent && bus->parent->id) {
455 /* parent device has id -> use it plus parent-bus-id for bus name */
456 bus_id = bus->parent->num_child_bus;
458 len = strlen(bus->parent->id) + 16;
460 snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
463 /* no id -> use lowercase bus type plus global bus-id for bus name */
464 bc = BUS_GET_CLASS(bus);
465 bus_id = bc->automatic_ids++;
467 len = strlen(typename) + 16;
469 len = snprintf(buf, len, "%s.%d", typename, bus_id);
470 for (i = 0; i < len; i++) {
471 buf[i] = qemu_tolower(buf[i]);
477 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
478 bus->parent->num_child_bus++;
479 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
480 object_unref(OBJECT(bus));
481 } else if (bus != sysbus_get_default()) {
482 /* TODO: once all bus devices are qdevified,
483 only reset handler for main_system_bus should be registered here. */
484 qemu_register_reset(qbus_reset_all_fn, bus);
488 static void bus_unparent(Object *obj)
490 BusState *bus = BUS(obj);
493 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
494 DeviceState *dev = kid->child;
495 object_unparent(OBJECT(dev));
498 QLIST_REMOVE(bus, sibling);
499 bus->parent->num_child_bus--;
502 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
503 qemu_unregister_reset(qbus_reset_all_fn, bus);
507 static bool bus_get_realized(Object *obj, Error **errp)
509 BusState *bus = BUS(obj);
511 return bus->realized;
514 static void bus_set_realized(Object *obj, bool value, Error **errp)
516 BusState *bus = BUS(obj);
517 BusClass *bc = BUS_GET_CLASS(bus);
518 Error *local_err = NULL;
520 if (value && !bus->realized) {
522 bc->realize(bus, &local_err);
524 if (local_err != NULL) {
529 } else if (!value && bus->realized) {
531 bc->unrealize(bus, &local_err);
533 if (local_err != NULL) {
539 bus->realized = value;
543 error_propagate(errp, local_err);
546 void qbus_create_inplace(void *bus, size_t size, const char *typename,
547 DeviceState *parent, const char *name)
549 object_initialize(bus, size, typename);
550 qbus_realize(bus, parent, name);
553 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
557 bus = BUS(object_new(typename));
558 qbus_realize(bus, parent, name);
563 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
565 BusClass *bc = BUS_GET_CLASS(bus);
567 if (bc->get_fw_dev_path) {
568 return bc->get_fw_dev_path(dev);
574 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
576 Object *obj = OBJECT(dev);
579 while (!d && obj->parent) {
581 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
586 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
590 if (dev && dev->parent_bus) {
592 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
593 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
595 d = bus_get_fw_dev_path(dev->parent_bus, dev);
598 l += snprintf(p + l, size - l, "%s", d);
604 l += snprintf(p + l , size - l, "/");
609 char* qdev_get_fw_dev_path(DeviceState *dev)
614 l = qdev_get_fw_dev_path_helper(dev, path, 128);
618 return g_strdup(path);
621 char *qdev_get_dev_path(DeviceState *dev)
625 if (!dev || !dev->parent_bus) {
629 bc = BUS_GET_CLASS(dev->parent_bus);
630 if (bc->get_dev_path) {
631 return bc->get_dev_path(dev);
638 * Legacy property handling
641 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
642 const char *name, Error **errp)
644 DeviceState *dev = DEVICE(obj);
645 Property *prop = opaque;
650 prop->info->print(dev, prop, buffer, sizeof(buffer));
651 visit_type_str(v, &ptr, name, errp);
655 * @qdev_add_legacy_property - adds a legacy property
657 * Do not use this is new code! Properties added through this interface will
658 * be given names and types in the "legacy" namespace.
660 * Legacy properties are string versions of other OOM properties. The format
661 * of the string depends on the property type.
663 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
668 /* Register pointer properties as legacy properties */
669 if (!prop->info->print && prop->info->get) {
673 name = g_strdup_printf("legacy-%s", prop->name);
674 object_property_add(OBJECT(dev), name, "str",
675 prop->info->print ? qdev_get_legacy_property : prop->info->get,
684 * @qdev_property_add_static - add a @Property to a device.
686 * Static properties access data in a struct. The actual type of the
687 * property and the field depends on the property type.
689 void qdev_property_add_static(DeviceState *dev, Property *prop,
692 Error *local_err = NULL;
693 Object *obj = OBJECT(dev);
696 * TODO qdev_prop_ptr does not have getters or setters. It must
697 * go now that it can be replaced with links. The test should be
698 * removed along with it: all static properties are read/write.
700 if (!prop->info->get && !prop->info->set) {
704 object_property_add(obj, prop->name, prop->info->name,
705 prop->info->get, prop->info->set,
710 error_propagate(errp, local_err);
713 if (prop->qtype == QTYPE_NONE) {
717 if (prop->qtype == QTYPE_QBOOL) {
718 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
719 } else if (prop->info->enum_table) {
720 object_property_set_str(obj, prop->info->enum_table[prop->defval],
721 prop->name, &error_abort);
722 } else if (prop->qtype == QTYPE_QINT) {
723 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
727 static bool device_get_realized(Object *obj, Error **errp)
729 DeviceState *dev = DEVICE(obj);
730 return dev->realized;
733 static void device_set_realized(Object *obj, bool value, Error **errp)
735 DeviceState *dev = DEVICE(obj);
736 DeviceClass *dc = DEVICE_GET_CLASS(dev);
738 Error *local_err = NULL;
740 if (dev->hotplugged && !dc->hotpluggable) {
741 error_set(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
745 if (value && !dev->realized) {
746 if (!obj->parent && local_err == NULL) {
747 static int unattached_count;
748 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
750 object_property_add_child(container_get(qdev_get_machine(),
752 name, obj, &local_err);
757 dc->realize(dev, &local_err);
760 if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
762 hotplug_handler_plug(dev->parent_bus->hotplug_handler,
766 if (qdev_get_vmsd(dev) && local_err == NULL) {
767 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
768 dev->instance_id_alias,
769 dev->alias_required_for_version);
771 if (local_err == NULL) {
772 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
773 object_property_set_bool(OBJECT(bus), true, "realized",
775 if (local_err != NULL) {
780 if (dev->hotplugged && local_err == NULL) {
783 } else if (!value && dev->realized) {
784 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
785 object_property_set_bool(OBJECT(bus), false, "realized",
787 if (local_err != NULL) {
791 if (qdev_get_vmsd(dev) && local_err == NULL) {
792 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
794 if (dc->unrealize && local_err == NULL) {
795 dc->unrealize(dev, &local_err);
799 if (local_err != NULL) {
800 error_propagate(errp, local_err);
804 dev->realized = value;
807 static bool device_get_hotpluggable(Object *obj, Error **errp)
809 DeviceClass *dc = DEVICE_GET_CLASS(obj);
810 DeviceState *dev = DEVICE(obj);
812 return dc->hotpluggable && (dev->parent_bus == NULL ||
813 dev->parent_bus->allow_hotplug);
816 static void device_initfn(Object *obj)
818 DeviceState *dev = DEVICE(obj);
824 qdev_hot_added = true;
827 dev->instance_id_alias = -1;
828 dev->realized = false;
830 object_property_add_bool(obj, "realized",
831 device_get_realized, device_set_realized, NULL);
832 object_property_add_bool(obj, "hotpluggable",
833 device_get_hotpluggable, NULL, NULL);
835 class = object_get_class(OBJECT(dev));
837 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
838 qdev_property_add_legacy(dev, prop, &error_abort);
839 qdev_property_add_static(dev, prop, &error_abort);
841 class = object_class_get_parent(class);
842 } while (class != object_class_by_name(TYPE_DEVICE));
844 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
845 (Object **)&dev->parent_bus, NULL, 0,
849 static void device_post_init(Object *obj)
851 qdev_prop_set_globals(DEVICE(obj), &error_abort);
854 /* Unlink device from bus and free the structure. */
855 static void device_finalize(Object *obj)
857 DeviceState *dev = DEVICE(obj);
859 qemu_opts_del(dev->opts);
863 static void device_class_base_init(ObjectClass *class, void *data)
865 DeviceClass *klass = DEVICE_CLASS(class);
867 /* We explicitly look up properties in the superclasses,
868 * so do not propagate them to the subclasses.
873 static void device_unparent(Object *obj)
875 DeviceState *dev = DEVICE(obj);
878 bool have_realized = dev->realized;
881 object_property_set_bool(obj, false, "realized", NULL);
883 while (dev->num_child_bus) {
884 bus = QLIST_FIRST(&dev->child_bus);
885 object_unparent(OBJECT(bus));
887 if (dev->parent_bus) {
888 bus_remove_child(dev->parent_bus, dev);
889 object_unref(OBJECT(dev->parent_bus));
890 dev->parent_bus = NULL;
893 /* Only send event if the device had been completely realized */
895 gchar *path = object_get_canonical_path(OBJECT(dev));
898 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
901 event_data = qobject_from_jsonf("{ 'path': %s }", path);
903 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
904 qobject_decref(event_data);
909 static void device_class_init(ObjectClass *class, void *data)
911 DeviceClass *dc = DEVICE_CLASS(class);
913 class->unparent = device_unparent;
914 dc->realize = device_realize;
915 dc->unrealize = device_unrealize;
917 /* by default all devices were considered as hotpluggable,
918 * so with intent to check it in generic qdev_unplug() /
919 * device_set_realized() functions make every device
920 * hotpluggable. Devices that shouldn't be hotpluggable,
921 * should override it in their class_init()
923 dc->hotpluggable = true;
926 void device_reset(DeviceState *dev)
928 DeviceClass *klass = DEVICE_GET_CLASS(dev);
935 Object *qdev_get_machine(void)
940 dev = container_get(object_get_root(), "/machine");
946 static const TypeInfo device_type_info = {
948 .parent = TYPE_OBJECT,
949 .instance_size = sizeof(DeviceState),
950 .instance_init = device_initfn,
951 .instance_post_init = device_post_init,
952 .instance_finalize = device_finalize,
953 .class_base_init = device_class_base_init,
954 .class_init = device_class_init,
956 .class_size = sizeof(DeviceClass),
959 static void qbus_initfn(Object *obj)
961 BusState *bus = BUS(obj);
963 QTAILQ_INIT(&bus->children);
964 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
965 TYPE_HOTPLUG_HANDLER,
966 (Object **)&bus->hotplug_handler,
967 object_property_allow_set_link,
968 OBJ_PROP_LINK_UNREF_ON_RELEASE,
970 object_property_add_bool(obj, "realized",
971 bus_get_realized, bus_set_realized, NULL);
974 static char *default_bus_get_fw_dev_path(DeviceState *dev)
976 return g_strdup(object_get_typename(OBJECT(dev)));
979 static void bus_class_init(ObjectClass *class, void *data)
981 BusClass *bc = BUS_CLASS(class);
983 class->unparent = bus_unparent;
984 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
987 static void qbus_finalize(Object *obj)
989 BusState *bus = BUS(obj);
991 g_free((char *)bus->name);
994 static const TypeInfo bus_info = {
996 .parent = TYPE_OBJECT,
997 .instance_size = sizeof(BusState),
999 .class_size = sizeof(BusClass),
1000 .instance_init = qbus_initfn,
1001 .instance_finalize = qbus_finalize,
1002 .class_init = bus_class_init,
1005 static void qdev_register_types(void)
1007 type_register_static(&bus_info);
1008 type_register_static(&device_type_info);
1011 type_init(qdev_register_types)