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/visitor.h"
34 static bool qdev_hot_added = false;
35 static bool qdev_hot_removed = false;
37 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
39 DeviceClass *dc = DEVICE_GET_CLASS(dev);
43 const char *qdev_fw_name(DeviceState *dev)
45 DeviceClass *dc = DEVICE_GET_CLASS(dev);
51 return object_get_typename(OBJECT(dev));
54 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
57 static void bus_remove_child(BusState *bus, DeviceState *child)
61 QTAILQ_FOREACH(kid, &bus->children, sibling) {
62 if (kid->child == child) {
65 snprintf(name, sizeof(name), "child[%d]", kid->index);
66 QTAILQ_REMOVE(&bus->children, kid, sibling);
68 /* This gives back ownership of kid->child back to us. */
69 object_property_del(OBJECT(bus), name, NULL);
70 object_unref(OBJECT(kid->child));
77 static void bus_add_child(BusState *bus, DeviceState *child)
80 BusChild *kid = g_malloc0(sizeof(*kid));
83 assert(bus->allow_hotplug);
86 kid->index = bus->max_index++;
88 object_ref(OBJECT(kid->child));
90 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
92 /* This transfers ownership of kid->child to the property. */
93 snprintf(name, sizeof(name), "child[%d]", kid->index);
94 object_property_add_link(OBJECT(bus), name,
95 object_get_typename(OBJECT(child)),
96 (Object **)&kid->child,
100 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
102 dev->parent_bus = bus;
103 bus_add_child(bus, dev);
106 /* Create a new device. This only initializes the device state structure
107 and allows properties to be set. qdev_init should be called to
108 initialize the actual device emulation. */
109 DeviceState *qdev_create(BusState *bus, const char *name)
113 dev = qdev_try_create(bus, name);
116 error_report("Unknown device '%s' for bus '%s'\n", name,
117 object_get_typename(OBJECT(bus)));
120 error_report("Unknown device '%s' for default sysbus\n", name);
128 DeviceState *qdev_try_create(BusState *bus, const char *type)
132 if (object_class_by_name(type) == NULL) {
135 dev = DEVICE(object_new(type));
141 bus = sysbus_get_default();
144 qdev_set_parent_bus(dev, bus);
149 /* Initialize a device. Device properties should be set before calling
150 this function. IRQs and MMIO regions should be connected/mapped after
151 calling this function.
152 On failure, destroy the device and return negative value.
153 Return 0 on success. */
154 int qdev_init(DeviceState *dev)
156 Error *local_err = NULL;
158 assert(!dev->realized);
160 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
161 if (local_err != NULL) {
162 error_free(local_err);
169 static void device_realize(DeviceState *dev, Error **err)
171 DeviceClass *dc = DEVICE_GET_CLASS(dev);
174 int rc = dc->init(dev);
176 error_setg(err, "Device initialization failed.");
182 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
183 int required_for_version)
185 assert(!dev->realized);
186 dev->instance_id_alias = alias_id;
187 dev->alias_required_for_version = required_for_version;
190 void qdev_unplug(DeviceState *dev, Error **errp)
192 DeviceClass *dc = DEVICE_GET_CLASS(dev);
194 if (!dev->parent_bus->allow_hotplug) {
195 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
198 assert(dc->unplug != NULL);
200 qdev_hot_removed = true;
202 if (dc->unplug(dev) < 0) {
203 error_set(errp, QERR_UNDEFINED_ERROR);
208 static int qdev_reset_one(DeviceState *dev, void *opaque)
215 static int qbus_reset_one(BusState *bus, void *opaque)
217 BusClass *bc = BUS_GET_CLASS(bus);
219 return bc->reset(bus);
224 void qdev_reset_all(DeviceState *dev)
226 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
229 void qbus_reset_all(BusState *bus)
231 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
234 void qbus_reset_all_fn(void *opaque)
236 BusState *bus = opaque;
240 /* can be used as ->unplug() callback for the simple cases */
241 int qdev_simple_unplug_cb(DeviceState *dev)
249 /* Like qdev_init(), but terminate program via error_report() instead of
250 returning an error value. This is okay during machine creation.
251 Don't use for hotplug, because there callers need to recover from
252 failure. Exception: if you know the device's init() callback can't
253 fail, then qdev_init_nofail() can't fail either, and is therefore
254 usable even then. But relying on the device implementation that
255 way is somewhat unclean, and best avoided. */
256 void qdev_init_nofail(DeviceState *dev)
258 const char *typename = object_get_typename(OBJECT(dev));
260 if (qdev_init(dev) < 0) {
261 error_report("Initialization of device %s failed", typename);
266 /* Unlink device from bus and free the structure. */
267 void qdev_free(DeviceState *dev)
269 object_delete(OBJECT(dev));
272 void qdev_machine_creation_done(void)
275 * ok, initial machine setup is done, starting from now we can
276 * only create hotpluggable devices
281 bool qdev_machine_modified(void)
283 return qdev_hot_added || qdev_hot_removed;
286 BusState *qdev_get_parent_bus(DeviceState *dev)
288 return dev->parent_bus;
291 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
293 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
295 dev->num_gpio_in += n;
298 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
300 assert(dev->num_gpio_out == 0);
301 dev->num_gpio_out = n;
302 dev->gpio_out = pins;
305 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
307 assert(n >= 0 && n < dev->num_gpio_in);
308 return dev->gpio_in[n];
311 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
313 assert(n >= 0 && n < dev->num_gpio_out);
314 dev->gpio_out[n] = pin;
317 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
321 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
322 if (strcmp(name, bus->name) == 0) {
329 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
330 qbus_walkerfn *busfn, void *opaque)
336 err = busfn(bus, opaque);
342 QTAILQ_FOREACH(kid, &bus->children, sibling) {
343 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
352 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
353 qbus_walkerfn *busfn, void *opaque)
359 err = devfn(dev, opaque);
365 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
366 err = qbus_walk_children(bus, devfn, busfn, opaque);
375 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
381 QTAILQ_FOREACH(kid, &bus->children, sibling) {
382 DeviceState *dev = kid->child;
384 if (dev->id && strcmp(dev->id, id) == 0) {
388 QLIST_FOREACH(child, &dev->child_bus, sibling) {
389 ret = qdev_find_recursive(child, id);
398 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
400 const char *typename = object_get_typename(OBJECT(bus));
404 bus->parent = parent;
407 bus->name = g_strdup(name);
408 } else if (bus->parent && bus->parent->id) {
409 /* parent device has id -> use it for bus name */
410 len = strlen(bus->parent->id) + 16;
412 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
415 /* no id -> use lowercase bus type for bus name */
416 len = strlen(typename) + 16;
418 len = snprintf(buf, len, "%s.%d", typename,
419 bus->parent ? bus->parent->num_child_bus : 0);
420 for (i = 0; i < len; i++)
421 buf[i] = qemu_tolower(buf[i]);
426 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
427 bus->parent->num_child_bus++;
428 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
429 } else if (bus != sysbus_get_default()) {
430 /* TODO: once all bus devices are qdevified,
431 only reset handler for main_system_bus should be registered here. */
432 qemu_register_reset(qbus_reset_all_fn, bus);
436 static void bus_unparent(Object *obj)
438 BusState *bus = BUS(obj);
441 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
442 DeviceState *dev = kid->child;
446 QLIST_REMOVE(bus, sibling);
447 bus->parent->num_child_bus--;
450 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
451 qemu_unregister_reset(qbus_reset_all_fn, bus);
455 void qbus_create_inplace(void *bus, const char *typename,
456 DeviceState *parent, const char *name)
458 object_initialize(bus, typename);
459 qbus_realize(bus, parent, name);
462 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
466 bus = BUS(object_new(typename));
467 qbus_realize(bus, parent, name);
472 void qbus_free(BusState *bus)
474 object_delete(OBJECT(bus));
477 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
479 BusClass *bc = BUS_GET_CLASS(bus);
481 if (bc->get_fw_dev_path) {
482 return bc->get_fw_dev_path(dev);
488 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
492 if (dev && dev->parent_bus) {
494 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
495 d = bus_get_fw_dev_path(dev->parent_bus, dev);
497 l += snprintf(p + l, size - l, "%s", d);
500 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
503 l += snprintf(p + l , size - l, "/");
508 char* qdev_get_fw_dev_path(DeviceState *dev)
513 l = qdev_get_fw_dev_path_helper(dev, path, 128);
517 return g_strdup(path);
520 char *qdev_get_dev_path(DeviceState *dev)
524 if (!dev || !dev->parent_bus) {
528 bc = BUS_GET_CLASS(dev->parent_bus);
529 if (bc->get_dev_path) {
530 return bc->get_dev_path(dev);
537 * Legacy property handling
540 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
541 const char *name, Error **errp)
543 DeviceState *dev = DEVICE(obj);
544 Property *prop = opaque;
549 prop->info->print(dev, prop, buffer, sizeof(buffer));
550 visit_type_str(v, &ptr, name, errp);
553 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
554 const char *name, Error **errp)
556 DeviceState *dev = DEVICE(obj);
557 Property *prop = opaque;
558 Error *local_err = NULL;
563 error_set(errp, QERR_PERMISSION_DENIED);
567 visit_type_str(v, &ptr, name, &local_err);
569 error_propagate(errp, local_err);
573 ret = prop->info->parse(dev, prop, ptr);
574 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
579 * @qdev_add_legacy_property - adds a legacy property
581 * Do not use this is new code! Properties added through this interface will
582 * be given names and types in the "legacy" namespace.
584 * Legacy properties are string versions of other OOM properties. The format
585 * of the string depends on the property type.
587 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
592 /* Register pointer properties as legacy properties */
593 if (!prop->info->print && !prop->info->parse &&
594 (prop->info->set || prop->info->get)) {
598 name = g_strdup_printf("legacy-%s", prop->name);
599 type = g_strdup_printf("legacy<%s>",
600 prop->info->legacy_name ?: prop->info->name);
602 object_property_add(OBJECT(dev), name, type,
603 prop->info->print ? qdev_get_legacy_property : prop->info->get,
604 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
613 * @qdev_property_add_static - add a @Property to a device.
615 * Static properties access data in a struct. The actual type of the
616 * property and the field depends on the property type.
618 void qdev_property_add_static(DeviceState *dev, Property *prop,
621 Error *local_err = NULL;
622 Object *obj = OBJECT(dev);
625 * TODO qdev_prop_ptr does not have getters or setters. It must
626 * go now that it can be replaced with links. The test should be
627 * removed along with it: all static properties are read/write.
629 if (!prop->info->get && !prop->info->set) {
633 object_property_add(obj, prop->name, prop->info->name,
634 prop->info->get, prop->info->set,
639 error_propagate(errp, local_err);
642 if (prop->qtype == QTYPE_NONE) {
646 if (prop->qtype == QTYPE_QBOOL) {
647 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
648 } else if (prop->info->enum_table) {
649 object_property_set_str(obj, prop->info->enum_table[prop->defval],
650 prop->name, &local_err);
651 } else if (prop->qtype == QTYPE_QINT) {
652 object_property_set_int(obj, prop->defval, prop->name, &local_err);
654 assert_no_error(local_err);
657 static bool device_get_realized(Object *obj, Error **err)
659 DeviceState *dev = DEVICE(obj);
660 return dev->realized;
663 static void device_set_realized(Object *obj, bool value, Error **err)
665 DeviceState *dev = DEVICE(obj);
666 DeviceClass *dc = DEVICE_GET_CLASS(dev);
667 Error *local_err = NULL;
669 if (value && !dev->realized) {
671 dc->realize(dev, &local_err);
674 if (!obj->parent && local_err == NULL) {
675 static int unattached_count;
676 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
678 object_property_add_child(container_get(qdev_get_machine(),
680 name, obj, &local_err);
684 if (qdev_get_vmsd(dev) && local_err == NULL) {
685 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
686 dev->instance_id_alias,
687 dev->alias_required_for_version);
689 if (dev->hotplugged && local_err == NULL) {
692 } else if (!value && dev->realized) {
694 dc->unrealize(dev, &local_err);
698 if (local_err != NULL) {
699 error_propagate(err, local_err);
703 dev->realized = value;
706 static void device_initfn(Object *obj)
708 DeviceState *dev = DEVICE(obj);
714 qdev_hot_added = true;
717 dev->instance_id_alias = -1;
718 dev->realized = false;
720 object_property_add_bool(obj, "realized",
721 device_get_realized, device_set_realized, NULL);
723 class = object_get_class(OBJECT(dev));
725 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
726 qdev_property_add_legacy(dev, prop, NULL);
727 qdev_property_add_static(dev, prop, NULL);
729 class = object_class_get_parent(class);
730 } while (class != object_class_by_name(TYPE_DEVICE));
731 qdev_prop_set_globals(dev);
733 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
734 (Object **)&dev->parent_bus, NULL);
737 /* Unlink device from bus and free the structure. */
738 static void device_finalize(Object *obj)
740 DeviceState *dev = DEVICE(obj);
742 qemu_opts_del(dev->opts);
746 static void device_class_base_init(ObjectClass *class, void *data)
748 DeviceClass *klass = DEVICE_CLASS(class);
750 /* We explicitly look up properties in the superclasses,
751 * so do not propagate them to the subclasses.
756 static void device_unparent(Object *obj)
758 DeviceState *dev = DEVICE(obj);
759 DeviceClass *dc = DEVICE_GET_CLASS(dev);
762 while (dev->num_child_bus) {
763 bus = QLIST_FIRST(&dev->child_bus);
767 if (qdev_get_vmsd(dev)) {
768 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
774 if (dev->parent_bus) {
775 bus_remove_child(dev->parent_bus, dev);
779 static void device_class_init(ObjectClass *class, void *data)
781 DeviceClass *dc = DEVICE_CLASS(class);
783 class->unparent = device_unparent;
784 dc->realize = device_realize;
787 void device_reset(DeviceState *dev)
789 DeviceClass *klass = DEVICE_GET_CLASS(dev);
796 Object *qdev_get_machine(void)
801 dev = container_get(object_get_root(), "/machine");
807 static const TypeInfo device_type_info = {
809 .parent = TYPE_OBJECT,
810 .instance_size = sizeof(DeviceState),
811 .instance_init = device_initfn,
812 .instance_finalize = device_finalize,
813 .class_base_init = device_class_base_init,
814 .class_init = device_class_init,
816 .class_size = sizeof(DeviceClass),
819 static void qbus_initfn(Object *obj)
821 BusState *bus = BUS(obj);
823 QTAILQ_INIT(&bus->children);
826 static void bus_class_init(ObjectClass *class, void *data)
828 class->unparent = bus_unparent;
831 static void qbus_finalize(Object *obj)
833 BusState *bus = BUS(obj);
835 g_free((char *)bus->name);
838 static const TypeInfo bus_info = {
840 .parent = TYPE_OBJECT,
841 .instance_size = sizeof(BusState),
843 .class_size = sizeof(BusClass),
844 .instance_init = qbus_initfn,
845 .instance_finalize = qbus_finalize,
846 .class_init = bus_class_init,
849 static void qdev_register_types(void)
851 type_register_static(&bus_info);
852 type_register_static(&device_type_info);
855 type_init(qdev_register_types)