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"
37 static bool qdev_hot_added = false;
38 static bool qdev_hot_removed = false;
40 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
42 DeviceClass *dc = DEVICE_GET_CLASS(dev);
46 const char *qdev_fw_name(DeviceState *dev)
48 DeviceClass *dc = DEVICE_GET_CLASS(dev);
54 return object_get_typename(OBJECT(dev));
57 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
60 static void bus_remove_child(BusState *bus, DeviceState *child)
64 QTAILQ_FOREACH(kid, &bus->children, sibling) {
65 if (kid->child == child) {
68 snprintf(name, sizeof(name), "child[%d]", kid->index);
69 QTAILQ_REMOVE(&bus->children, kid, sibling);
71 /* This gives back ownership of kid->child back to us. */
72 object_property_del(OBJECT(bus), name, NULL);
73 object_unref(OBJECT(kid->child));
80 static void bus_add_child(BusState *bus, DeviceState *child)
83 BusChild *kid = g_malloc0(sizeof(*kid));
86 assert(bus->allow_hotplug);
89 kid->index = bus->max_index++;
91 object_ref(OBJECT(kid->child));
93 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
95 /* This transfers ownership of kid->child to the property. */
96 snprintf(name, sizeof(name), "child[%d]", kid->index);
97 object_property_add_link(OBJECT(bus), name,
98 object_get_typename(OBJECT(child)),
99 (Object **)&kid->child,
103 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
105 dev->parent_bus = bus;
106 object_ref(OBJECT(bus));
107 bus_add_child(bus, dev);
110 /* Create a new device. This only initializes the device state structure
111 and allows properties to be set. qdev_init should be called to
112 initialize the actual device emulation. */
113 DeviceState *qdev_create(BusState *bus, const char *name)
117 dev = qdev_try_create(bus, name);
120 error_report("Unknown device '%s' for bus '%s'", name,
121 object_get_typename(OBJECT(bus)));
123 error_report("Unknown device '%s' for default sysbus", name);
131 DeviceState *qdev_try_create(BusState *bus, const char *type)
135 if (object_class_by_name(type) == NULL) {
138 dev = DEVICE(object_new(type));
144 bus = sysbus_get_default();
147 qdev_set_parent_bus(dev, bus);
148 object_unref(OBJECT(dev));
152 /* Initialize a device. Device properties should be set before calling
153 this function. IRQs and MMIO regions should be connected/mapped after
154 calling this function.
155 On failure, destroy the device and return negative value.
156 Return 0 on success. */
157 int qdev_init(DeviceState *dev)
159 Error *local_err = NULL;
161 assert(!dev->realized);
163 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
164 if (local_err != NULL) {
165 error_free(local_err);
172 static void device_realize(DeviceState *dev, Error **err)
174 DeviceClass *dc = DEVICE_GET_CLASS(dev);
177 int rc = dc->init(dev);
179 error_setg(err, "Device initialization failed.");
185 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
186 int required_for_version)
188 assert(!dev->realized);
189 dev->instance_id_alias = alias_id;
190 dev->alias_required_for_version = required_for_version;
193 void qdev_unplug(DeviceState *dev, Error **errp)
195 DeviceClass *dc = DEVICE_GET_CLASS(dev);
197 if (!dev->parent_bus->allow_hotplug) {
198 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
201 assert(dc->unplug != NULL);
203 qdev_hot_removed = true;
205 if (dc->unplug(dev) < 0) {
206 error_set(errp, QERR_UNDEFINED_ERROR);
211 static int qdev_reset_one(DeviceState *dev, void *opaque)
218 static int qbus_reset_one(BusState *bus, void *opaque)
220 BusClass *bc = BUS_GET_CLASS(bus);
222 return bc->reset(bus);
227 void qdev_reset_all(DeviceState *dev)
229 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
232 void qbus_reset_all(BusState *bus)
234 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
237 void qbus_reset_all_fn(void *opaque)
239 BusState *bus = opaque;
243 /* can be used as ->unplug() callback for the simple cases */
244 int qdev_simple_unplug_cb(DeviceState *dev)
252 /* Like qdev_init(), but terminate program via error_report() instead of
253 returning an error value. This is okay during machine creation.
254 Don't use for hotplug, because there callers need to recover from
255 failure. Exception: if you know the device's init() callback can't
256 fail, then qdev_init_nofail() can't fail either, and is therefore
257 usable even then. But relying on the device implementation that
258 way is somewhat unclean, and best avoided. */
259 void qdev_init_nofail(DeviceState *dev)
261 const char *typename = object_get_typename(OBJECT(dev));
263 if (qdev_init(dev) < 0) {
264 error_report("Initialization of device %s failed", typename);
269 /* Unlink device from bus and free the structure. */
270 void qdev_free(DeviceState *dev)
272 object_unparent(OBJECT(dev));
275 void qdev_machine_creation_done(void)
278 * ok, initial machine setup is done, starting from now we can
279 * only create hotpluggable devices
284 bool qdev_machine_modified(void)
286 return qdev_hot_added || qdev_hot_removed;
289 BusState *qdev_get_parent_bus(DeviceState *dev)
291 return dev->parent_bus;
294 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
296 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
298 dev->num_gpio_in += n;
301 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
303 assert(dev->num_gpio_out == 0);
304 dev->num_gpio_out = n;
305 dev->gpio_out = pins;
308 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
310 assert(n >= 0 && n < dev->num_gpio_in);
311 return dev->gpio_in[n];
314 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
316 assert(n >= 0 && n < dev->num_gpio_out);
317 dev->gpio_out[n] = pin;
320 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
324 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
325 if (strcmp(name, bus->name) == 0) {
332 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
333 qbus_walkerfn *busfn, void *opaque)
339 err = busfn(bus, opaque);
345 QTAILQ_FOREACH(kid, &bus->children, sibling) {
346 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
355 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
356 qbus_walkerfn *busfn, void *opaque)
362 err = devfn(dev, opaque);
368 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
369 err = qbus_walk_children(bus, devfn, busfn, opaque);
378 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
384 QTAILQ_FOREACH(kid, &bus->children, sibling) {
385 DeviceState *dev = kid->child;
387 if (dev->id && strcmp(dev->id, id) == 0) {
391 QLIST_FOREACH(child, &dev->child_bus, sibling) {
392 ret = qdev_find_recursive(child, id);
401 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
403 const char *typename = object_get_typename(OBJECT(bus));
407 bus->parent = parent;
410 bus->name = g_strdup(name);
411 } else if (bus->parent && bus->parent->id) {
412 /* parent device has id -> use it for bus name */
413 len = strlen(bus->parent->id) + 16;
415 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
418 /* no id -> use lowercase bus type for bus name */
419 len = strlen(typename) + 16;
421 len = snprintf(buf, len, "%s.%d", typename,
422 bus->parent ? bus->parent->num_child_bus : 0);
423 for (i = 0; i < len; i++)
424 buf[i] = qemu_tolower(buf[i]);
429 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
430 bus->parent->num_child_bus++;
431 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
432 object_unref(OBJECT(bus));
433 } else if (bus != sysbus_get_default()) {
434 /* TODO: once all bus devices are qdevified,
435 only reset handler for main_system_bus should be registered here. */
436 qemu_register_reset(qbus_reset_all_fn, bus);
440 static void bus_unparent(Object *obj)
442 BusState *bus = BUS(obj);
445 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
446 DeviceState *dev = kid->child;
450 QLIST_REMOVE(bus, sibling);
451 bus->parent->num_child_bus--;
454 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
455 qemu_unregister_reset(qbus_reset_all_fn, bus);
459 void qbus_create_inplace(void *bus, const char *typename,
460 DeviceState *parent, const char *name)
462 object_initialize(bus, typename);
463 qbus_realize(bus, parent, name);
466 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
470 bus = BUS(object_new(typename));
471 qbus_realize(bus, parent, name);
476 void qbus_free(BusState *bus)
478 object_unparent(OBJECT(bus));
481 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
483 BusClass *bc = BUS_GET_CLASS(bus);
485 if (bc->get_fw_dev_path) {
486 return bc->get_fw_dev_path(dev);
492 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
496 if (dev && dev->parent_bus) {
498 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
499 d = bus_get_fw_dev_path(dev->parent_bus, dev);
501 l += snprintf(p + l, size - l, "%s", d);
504 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
507 l += snprintf(p + l , size - l, "/");
512 char* qdev_get_fw_dev_path(DeviceState *dev)
517 l = qdev_get_fw_dev_path_helper(dev, path, 128);
521 return g_strdup(path);
524 char *qdev_get_dev_path(DeviceState *dev)
528 if (!dev || !dev->parent_bus) {
532 bc = BUS_GET_CLASS(dev->parent_bus);
533 if (bc->get_dev_path) {
534 return bc->get_dev_path(dev);
541 * Legacy property handling
544 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
545 const char *name, Error **errp)
547 DeviceState *dev = DEVICE(obj);
548 Property *prop = opaque;
553 prop->info->print(dev, prop, buffer, sizeof(buffer));
554 visit_type_str(v, &ptr, name, errp);
557 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
558 const char *name, Error **errp)
560 DeviceState *dev = DEVICE(obj);
561 Property *prop = opaque;
562 Error *local_err = NULL;
567 qdev_prop_set_after_realize(dev, name, errp);
571 visit_type_str(v, &ptr, name, &local_err);
573 error_propagate(errp, local_err);
577 ret = prop->info->parse(dev, prop, ptr);
578 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
583 * @qdev_add_legacy_property - adds a legacy property
585 * Do not use this is new code! Properties added through this interface will
586 * be given names and types in the "legacy" namespace.
588 * Legacy properties are string versions of other OOM properties. The format
589 * of the string depends on the property type.
591 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
596 /* Register pointer properties as legacy properties */
597 if (!prop->info->print && !prop->info->parse &&
598 (prop->info->set || prop->info->get)) {
602 name = g_strdup_printf("legacy-%s", prop->name);
603 type = g_strdup_printf("legacy<%s>",
604 prop->info->legacy_name ?: prop->info->name);
606 object_property_add(OBJECT(dev), name, type,
607 prop->info->print ? qdev_get_legacy_property : prop->info->get,
608 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
617 * @qdev_property_add_static - add a @Property to a device.
619 * Static properties access data in a struct. The actual type of the
620 * property and the field depends on the property type.
622 void qdev_property_add_static(DeviceState *dev, Property *prop,
625 Error *local_err = NULL;
626 Object *obj = OBJECT(dev);
629 * TODO qdev_prop_ptr does not have getters or setters. It must
630 * go now that it can be replaced with links. The test should be
631 * removed along with it: all static properties are read/write.
633 if (!prop->info->get && !prop->info->set) {
637 object_property_add(obj, prop->name, prop->info->name,
638 prop->info->get, prop->info->set,
643 error_propagate(errp, local_err);
646 if (prop->qtype == QTYPE_NONE) {
650 if (prop->qtype == QTYPE_QBOOL) {
651 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
652 } else if (prop->info->enum_table) {
653 object_property_set_str(obj, prop->info->enum_table[prop->defval],
654 prop->name, &local_err);
655 } else if (prop->qtype == QTYPE_QINT) {
656 object_property_set_int(obj, prop->defval, prop->name, &local_err);
658 assert_no_error(local_err);
661 static bool device_get_realized(Object *obj, Error **err)
663 DeviceState *dev = DEVICE(obj);
664 return dev->realized;
667 static void device_set_realized(Object *obj, bool value, Error **err)
669 DeviceState *dev = DEVICE(obj);
670 DeviceClass *dc = DEVICE_GET_CLASS(dev);
671 Error *local_err = NULL;
673 if (value && !dev->realized) {
675 dc->realize(dev, &local_err);
678 if (!obj->parent && local_err == NULL) {
679 static int unattached_count;
680 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
682 object_property_add_child(container_get(qdev_get_machine(),
684 name, obj, &local_err);
688 if (qdev_get_vmsd(dev) && local_err == NULL) {
689 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
690 dev->instance_id_alias,
691 dev->alias_required_for_version);
693 if (dev->hotplugged && local_err == NULL) {
696 } else if (!value && dev->realized) {
698 dc->unrealize(dev, &local_err);
702 if (local_err != NULL) {
703 error_propagate(err, local_err);
707 dev->realized = value;
710 static void device_initfn(Object *obj)
712 DeviceState *dev = DEVICE(obj);
719 qdev_hot_added = true;
722 dev->instance_id_alias = -1;
723 dev->realized = false;
725 object_property_add_bool(obj, "realized",
726 device_get_realized, device_set_realized, NULL);
728 class = object_get_class(OBJECT(dev));
730 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
731 qdev_property_add_legacy(dev, prop, &err);
732 assert_no_error(err);
733 qdev_property_add_static(dev, prop, &err);
734 assert_no_error(err);
736 class = object_class_get_parent(class);
737 } while (class != object_class_by_name(TYPE_DEVICE));
738 qdev_prop_set_globals(dev);
740 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
741 (Object **)&dev->parent_bus, &err);
742 assert_no_error(err);
745 /* Unlink device from bus and free the structure. */
746 static void device_finalize(Object *obj)
748 DeviceState *dev = DEVICE(obj);
750 qemu_opts_del(dev->opts);
754 static void device_class_base_init(ObjectClass *class, void *data)
756 DeviceClass *klass = DEVICE_CLASS(class);
758 /* We explicitly look up properties in the superclasses,
759 * so do not propagate them to the subclasses.
764 static void device_unparent(Object *obj)
766 DeviceState *dev = DEVICE(obj);
767 DeviceClass *dc = DEVICE_GET_CLASS(dev);
770 bool have_realized = dev->realized;
772 while (dev->num_child_bus) {
773 bus = QLIST_FIRST(&dev->child_bus);
777 if (qdev_get_vmsd(dev)) {
778 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
784 if (dev->parent_bus) {
785 bus_remove_child(dev->parent_bus, dev);
786 object_unref(OBJECT(dev->parent_bus));
787 dev->parent_bus = NULL;
790 /* Only send event if the device had been completely realized */
792 gchar *path = object_get_canonical_path(OBJECT(dev));
795 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
798 event_data = qobject_from_jsonf("{ 'path': %s }", path);
800 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
801 qobject_decref(event_data);
806 static void device_class_init(ObjectClass *class, void *data)
808 DeviceClass *dc = DEVICE_CLASS(class);
810 class->unparent = device_unparent;
811 dc->realize = device_realize;
814 void device_reset(DeviceState *dev)
816 DeviceClass *klass = DEVICE_GET_CLASS(dev);
823 Object *qdev_get_machine(void)
828 dev = container_get(object_get_root(), "/machine");
834 static const TypeInfo device_type_info = {
836 .parent = TYPE_OBJECT,
837 .instance_size = sizeof(DeviceState),
838 .instance_init = device_initfn,
839 .instance_finalize = device_finalize,
840 .class_base_init = device_class_base_init,
841 .class_init = device_class_init,
843 .class_size = sizeof(DeviceClass),
846 static void qbus_initfn(Object *obj)
848 BusState *bus = BUS(obj);
850 QTAILQ_INIT(&bus->children);
853 static void bus_class_init(ObjectClass *class, void *data)
855 class->unparent = bus_unparent;
858 static void qbus_finalize(Object *obj)
860 BusState *bus = BUS(obj);
862 g_free((char *)bus->name);
865 static const TypeInfo bus_info = {
867 .parent = TYPE_OBJECT,
868 .instance_size = sizeof(BusState),
870 .class_size = sizeof(BusClass),
871 .instance_init = qbus_initfn,
872 .instance_finalize = qbus_finalize,
873 .class_init = bus_class_init,
876 static void qdev_register_types(void)
878 type_register_static(&bus_info);
879 type_register_static(&device_type_info);
882 type_init(qdev_register_types)