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
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/visitor.h"
35 static bool qdev_hot_added = false;
36 static bool qdev_hot_removed = false;
38 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
40 DeviceClass *dc = DEVICE_GET_CLASS(dev);
44 const char *qdev_fw_name(DeviceState *dev)
46 DeviceClass *dc = DEVICE_GET_CLASS(dev);
52 return object_get_typename(OBJECT(dev));
55 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
58 static void bus_remove_child(BusState *bus, DeviceState *child)
62 QTAILQ_FOREACH(kid, &bus->children, sibling) {
63 if (kid->child == child) {
66 snprintf(name, sizeof(name), "child[%d]", kid->index);
67 QTAILQ_REMOVE(&bus->children, kid, sibling);
68 object_property_del(OBJECT(bus), name, NULL);
75 static void bus_add_child(BusState *bus, DeviceState *child)
78 BusChild *kid = g_malloc0(sizeof(*kid));
81 assert(bus->allow_hotplug);
84 kid->index = bus->max_index++;
87 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
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,
96 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
98 dev->parent_bus = bus;
99 bus_add_child(bus, dev);
102 /* Create a new device. This only initializes the device state structure
103 and allows properties to be set. qdev_init should be called to
104 initialize the actual device emulation. */
105 DeviceState *qdev_create(BusState *bus, const char *name)
109 dev = qdev_try_create(bus, name);
112 error_report("Unknown device '%s' for bus '%s'\n", name,
113 object_get_typename(OBJECT(bus)));
116 error_report("Unknown device '%s' for default sysbus\n", name);
124 DeviceState *qdev_try_create(BusState *bus, const char *type)
128 if (object_class_by_name(type) == NULL) {
131 dev = DEVICE(object_new(type));
137 bus = sysbus_get_default();
140 qdev_set_parent_bus(dev, bus);
145 /* Initialize a device. Device properties should be set before calling
146 this function. IRQs and MMIO regions should be connected/mapped after
147 calling this function.
148 On failure, destroy the device and return negative value.
149 Return 0 on success. */
150 int qdev_init(DeviceState *dev)
152 DeviceClass *dc = DEVICE_GET_CLASS(dev);
155 assert(dev->state == DEV_STATE_CREATED);
163 if (!OBJECT(dev)->parent) {
164 static int unattached_count = 0;
165 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
167 object_property_add_child(container_get(qdev_get_machine(),
169 name, OBJECT(dev), NULL);
173 if (qdev_get_vmsd(dev)) {
174 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
175 dev->instance_id_alias,
176 dev->alias_required_for_version);
178 dev->state = DEV_STATE_INITIALIZED;
179 if (dev->hotplugged) {
185 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
186 int required_for_version)
188 assert(dev->state == DEV_STATE_CREATED);
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_fn(void *opaque)
234 BusState *bus = opaque;
235 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
238 /* can be used as ->unplug() callback for the simple cases */
239 int qdev_simple_unplug_cb(DeviceState *dev)
247 /* Like qdev_init(), but terminate program via error_report() instead of
248 returning an error value. This is okay during machine creation.
249 Don't use for hotplug, because there callers need to recover from
250 failure. Exception: if you know the device's init() callback can't
251 fail, then qdev_init_nofail() can't fail either, and is therefore
252 usable even then. But relying on the device implementation that
253 way is somewhat unclean, and best avoided. */
254 void qdev_init_nofail(DeviceState *dev)
256 const char *typename = object_get_typename(OBJECT(dev));
258 if (qdev_init(dev) < 0) {
259 error_report("Initialization of device %s failed", typename);
264 /* Unlink device from bus and free the structure. */
265 void qdev_free(DeviceState *dev)
267 object_delete(OBJECT(dev));
270 void qdev_machine_creation_done(void)
273 * ok, initial machine setup is done, starting from now we can
274 * only create hotpluggable devices
279 bool qdev_machine_modified(void)
281 return qdev_hot_added || qdev_hot_removed;
284 BusState *qdev_get_parent_bus(DeviceState *dev)
286 return dev->parent_bus;
289 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
291 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
293 dev->num_gpio_in += n;
296 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
298 assert(dev->num_gpio_out == 0);
299 dev->num_gpio_out = n;
300 dev->gpio_out = pins;
303 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
305 assert(n >= 0 && n < dev->num_gpio_in);
306 return dev->gpio_in[n];
309 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
311 assert(n >= 0 && n < dev->num_gpio_out);
312 dev->gpio_out[n] = pin;
315 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
317 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
319 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
320 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
321 object_property_find(OBJECT(dev), "vectors", NULL)) {
322 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
324 nd->instantiated = 1;
327 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
331 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
332 if (strcmp(name, bus->name) == 0) {
339 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
340 qbus_walkerfn *busfn, void *opaque)
346 err = busfn(bus, opaque);
352 QTAILQ_FOREACH(kid, &bus->children, sibling) {
353 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
362 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
363 qbus_walkerfn *busfn, void *opaque)
369 err = devfn(dev, opaque);
375 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
376 err = qbus_walk_children(bus, devfn, busfn, opaque);
385 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
391 QTAILQ_FOREACH(kid, &bus->children, sibling) {
392 DeviceState *dev = kid->child;
394 if (dev->id && strcmp(dev->id, id) == 0) {
398 QLIST_FOREACH(child, &dev->child_bus, sibling) {
399 ret = qdev_find_recursive(child, id);
408 static void qbus_realize(BusState *bus)
410 const char *typename = object_get_typename(OBJECT(bus));
415 /* use supplied name */
416 } else if (bus->parent && bus->parent->id) {
417 /* parent device has id -> use it for bus name */
418 len = strlen(bus->parent->id) + 16;
420 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
423 /* no id -> use lowercase bus type for bus name */
424 len = strlen(typename) + 16;
426 len = snprintf(buf, len, "%s.%d", typename,
427 bus->parent ? bus->parent->num_child_bus : 0);
428 for (i = 0; i < len; i++)
429 buf[i] = qemu_tolower(buf[i]);
434 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
435 bus->parent->num_child_bus++;
436 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
437 } else if (bus != sysbus_get_default()) {
438 /* TODO: once all bus devices are qdevified,
439 only reset handler for main_system_bus should be registered here. */
440 qemu_register_reset(qbus_reset_all_fn, bus);
444 void qbus_create_inplace(BusState *bus, const char *typename,
445 DeviceState *parent, const char *name)
447 object_initialize(bus, typename);
449 bus->parent = parent;
450 bus->name = name ? g_strdup(name) : NULL;
454 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
458 bus = BUS(object_new(typename));
460 bus->parent = parent;
461 bus->name = name ? g_strdup(name) : NULL;
467 void qbus_free(BusState *bus)
469 object_delete(OBJECT(bus));
472 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
474 BusClass *bc = BUS_GET_CLASS(bus);
476 if (bc->get_fw_dev_path) {
477 return bc->get_fw_dev_path(dev);
483 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
487 if (dev && dev->parent_bus) {
489 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
490 d = bus_get_fw_dev_path(dev->parent_bus, dev);
492 l += snprintf(p + l, size - l, "%s", d);
495 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
498 l += snprintf(p + l , size - l, "/");
503 char* qdev_get_fw_dev_path(DeviceState *dev)
508 l = qdev_get_fw_dev_path_helper(dev, path, 128);
512 return g_strdup(path);
515 char *qdev_get_dev_path(DeviceState *dev)
519 if (!dev || !dev->parent_bus) {
523 bc = BUS_GET_CLASS(dev->parent_bus);
524 if (bc->get_dev_path) {
525 return bc->get_dev_path(dev);
532 * Legacy property handling
535 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
536 const char *name, Error **errp)
538 DeviceState *dev = DEVICE(obj);
539 Property *prop = opaque;
544 prop->info->print(dev, prop, buffer, sizeof(buffer));
545 visit_type_str(v, &ptr, name, errp);
548 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
549 const char *name, Error **errp)
551 DeviceState *dev = DEVICE(obj);
552 Property *prop = opaque;
553 Error *local_err = NULL;
557 if (dev->state != DEV_STATE_CREATED) {
558 error_set(errp, QERR_PERMISSION_DENIED);
562 visit_type_str(v, &ptr, name, &local_err);
564 error_propagate(errp, local_err);
568 ret = prop->info->parse(dev, prop, ptr);
569 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
574 * @qdev_add_legacy_property - adds a legacy property
576 * Do not use this is new code! Properties added through this interface will
577 * be given names and types in the "legacy" namespace.
579 * Legacy properties are string versions of other OOM properties. The format
580 * of the string depends on the property type.
582 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
587 /* Register pointer properties as legacy properties */
588 if (!prop->info->print && !prop->info->parse &&
589 (prop->info->set || prop->info->get)) {
593 name = g_strdup_printf("legacy-%s", prop->name);
594 type = g_strdup_printf("legacy<%s>",
595 prop->info->legacy_name ?: prop->info->name);
597 object_property_add(OBJECT(dev), name, type,
598 prop->info->print ? qdev_get_legacy_property : prop->info->get,
599 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
608 * @qdev_property_add_static - add a @Property to a device.
610 * Static properties access data in a struct. The actual type of the
611 * property and the field depends on the property type.
613 void qdev_property_add_static(DeviceState *dev, Property *prop,
616 Error *local_err = NULL;
617 Object *obj = OBJECT(dev);
620 * TODO qdev_prop_ptr does not have getters or setters. It must
621 * go now that it can be replaced with links. The test should be
622 * removed along with it: all static properties are read/write.
624 if (!prop->info->get && !prop->info->set) {
628 object_property_add(obj, prop->name, prop->info->name,
629 prop->info->get, prop->info->set,
634 error_propagate(errp, local_err);
637 if (prop->qtype == QTYPE_NONE) {
641 if (prop->qtype == QTYPE_QBOOL) {
642 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
643 } else if (prop->info->enum_table) {
644 object_property_set_str(obj, prop->info->enum_table[prop->defval],
645 prop->name, &local_err);
646 } else if (prop->qtype == QTYPE_QINT) {
647 object_property_set_int(obj, prop->defval, prop->name, &local_err);
649 assert_no_error(local_err);
652 static void device_initfn(Object *obj)
654 DeviceState *dev = DEVICE(obj);
660 qdev_hot_added = true;
663 dev->instance_id_alias = -1;
664 dev->state = DEV_STATE_CREATED;
666 class = object_get_class(OBJECT(dev));
668 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
669 qdev_property_add_legacy(dev, prop, NULL);
670 qdev_property_add_static(dev, prop, NULL);
672 class = object_class_get_parent(class);
673 } while (class != object_class_by_name(TYPE_DEVICE));
674 qdev_prop_set_globals(dev);
676 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
677 (Object **)&dev->parent_bus, NULL);
680 /* Unlink device from bus and free the structure. */
681 static void device_finalize(Object *obj)
683 DeviceState *dev = DEVICE(obj);
685 DeviceClass *dc = DEVICE_GET_CLASS(dev);
687 if (dev->state == DEV_STATE_INITIALIZED) {
688 while (dev->num_child_bus) {
689 bus = QLIST_FIRST(&dev->child_bus);
692 if (qdev_get_vmsd(dev)) {
693 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
699 qemu_opts_del(dev->opts);
704 static void device_class_base_init(ObjectClass *class, void *data)
706 DeviceClass *klass = DEVICE_CLASS(class);
708 /* We explicitly look up properties in the superclasses,
709 * so do not propagate them to the subclasses.
714 static void qdev_remove_from_bus(Object *obj)
716 DeviceState *dev = DEVICE(obj);
718 bus_remove_child(dev->parent_bus, dev);
721 static void device_class_init(ObjectClass *class, void *data)
723 class->unparent = qdev_remove_from_bus;
726 void device_reset(DeviceState *dev)
728 DeviceClass *klass = DEVICE_GET_CLASS(dev);
735 Object *qdev_get_machine(void)
740 dev = container_get(object_get_root(), "/machine");
746 static TypeInfo device_type_info = {
748 .parent = TYPE_OBJECT,
749 .instance_size = sizeof(DeviceState),
750 .instance_init = device_initfn,
751 .instance_finalize = device_finalize,
752 .class_base_init = device_class_base_init,
753 .class_init = device_class_init,
755 .class_size = sizeof(DeviceClass),
758 static void qbus_initfn(Object *obj)
760 BusState *bus = BUS(obj);
762 QTAILQ_INIT(&bus->children);
765 static void qbus_finalize(Object *obj)
767 BusState *bus = BUS(obj);
770 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
771 DeviceState *dev = kid->child;
775 QLIST_REMOVE(bus, sibling);
776 bus->parent->num_child_bus--;
778 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
779 qemu_unregister_reset(qbus_reset_all_fn, bus);
781 g_free((char *)bus->name);
784 static const TypeInfo bus_info = {
786 .parent = TYPE_OBJECT,
787 .instance_size = sizeof(BusState),
789 .class_size = sizeof(BusClass),
790 .instance_init = qbus_initfn,
791 .instance_finalize = qbus_finalize,
794 static void qdev_register_types(void)
796 type_register_static(&bus_info);
797 type_register_static(&device_type_info);
800 type_init(qdev_register_types)