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
32 #include "qapi/qapi-visit-core.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 hw_error("Unknown device '%s' for bus '%s'\n", name,
113 object_get_typename(OBJECT(bus)));
115 hw_error("Unknown device '%s' for default sysbus\n", name);
122 DeviceState *qdev_try_create(BusState *bus, const char *type)
126 if (object_class_by_name(type) == NULL) {
129 dev = DEVICE(object_new(type));
135 bus = sysbus_get_default();
138 qdev_set_parent_bus(dev, bus);
143 /* Initialize a device. Device properties should be set before calling
144 this function. IRQs and MMIO regions should be connected/mapped after
145 calling this function.
146 On failure, destroy the device and return negative value.
147 Return 0 on success. */
148 int qdev_init(DeviceState *dev)
150 DeviceClass *dc = DEVICE_GET_CLASS(dev);
153 assert(dev->state == DEV_STATE_CREATED);
161 if (!OBJECT(dev)->parent) {
162 static int unattached_count = 0;
163 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
165 object_property_add_child(container_get(qdev_get_machine(),
167 name, OBJECT(dev), NULL);
171 if (qdev_get_vmsd(dev)) {
172 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
173 dev->instance_id_alias,
174 dev->alias_required_for_version);
176 dev->state = DEV_STATE_INITIALIZED;
177 if (dev->hotplugged) {
183 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
184 int required_for_version)
186 assert(dev->state == DEV_STATE_CREATED);
187 dev->instance_id_alias = alias_id;
188 dev->alias_required_for_version = required_for_version;
191 void qdev_unplug(DeviceState *dev, Error **errp)
193 DeviceClass *dc = DEVICE_GET_CLASS(dev);
195 if (!dev->parent_bus->allow_hotplug) {
196 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
199 assert(dc->unplug != NULL);
201 qdev_hot_removed = true;
203 if (dc->unplug(dev) < 0) {
204 error_set(errp, QERR_UNDEFINED_ERROR);
209 static int qdev_reset_one(DeviceState *dev, void *opaque)
216 static int qbus_reset_one(BusState *bus, void *opaque)
218 BusClass *bc = BUS_GET_CLASS(bus);
220 return bc->reset(bus);
225 void qdev_reset_all(DeviceState *dev)
227 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
230 void qbus_reset_all_fn(void *opaque)
232 BusState *bus = opaque;
233 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
236 /* can be used as ->unplug() callback for the simple cases */
237 int qdev_simple_unplug_cb(DeviceState *dev)
245 /* Like qdev_init(), but terminate program via error_report() instead of
246 returning an error value. This is okay during machine creation.
247 Don't use for hotplug, because there callers need to recover from
248 failure. Exception: if you know the device's init() callback can't
249 fail, then qdev_init_nofail() can't fail either, and is therefore
250 usable even then. But relying on the device implementation that
251 way is somewhat unclean, and best avoided. */
252 void qdev_init_nofail(DeviceState *dev)
254 const char *typename = object_get_typename(OBJECT(dev));
256 if (qdev_init(dev) < 0) {
257 error_report("Initialization of device %s failed", typename);
262 /* Unlink device from bus and free the structure. */
263 void qdev_free(DeviceState *dev)
265 object_delete(OBJECT(dev));
268 void qdev_machine_creation_done(void)
271 * ok, initial machine setup is done, starting from now we can
272 * only create hotpluggable devices
277 bool qdev_machine_modified(void)
279 return qdev_hot_added || qdev_hot_removed;
282 BusState *qdev_get_parent_bus(DeviceState *dev)
284 return dev->parent_bus;
287 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
289 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
291 dev->num_gpio_in += n;
294 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
296 assert(dev->num_gpio_out == 0);
297 dev->num_gpio_out = n;
298 dev->gpio_out = pins;
301 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
303 assert(n >= 0 && n < dev->num_gpio_in);
304 return dev->gpio_in[n];
307 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
309 assert(n >= 0 && n < dev->num_gpio_out);
310 dev->gpio_out[n] = pin;
313 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
315 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
317 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
318 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
319 object_property_find(OBJECT(dev), "vectors", NULL)) {
320 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
322 nd->instantiated = 1;
325 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
329 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
330 if (strcmp(name, bus->name) == 0) {
337 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
338 qbus_walkerfn *busfn, void *opaque)
344 err = busfn(bus, opaque);
350 QTAILQ_FOREACH(kid, &bus->children, sibling) {
351 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
360 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
361 qbus_walkerfn *busfn, void *opaque)
367 err = devfn(dev, opaque);
373 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
374 err = qbus_walk_children(bus, devfn, busfn, opaque);
383 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
389 QTAILQ_FOREACH(kid, &bus->children, sibling) {
390 DeviceState *dev = kid->child;
392 if (dev->id && strcmp(dev->id, id) == 0) {
396 QLIST_FOREACH(child, &dev->child_bus, sibling) {
397 ret = qdev_find_recursive(child, id);
406 static void qbus_realize(BusState *bus)
408 const char *typename = object_get_typename(OBJECT(bus));
413 /* use supplied name */
414 } else if (bus->parent && bus->parent->id) {
415 /* parent device has id -> use it for bus name */
416 len = strlen(bus->parent->id) + 16;
418 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
421 /* no id -> use lowercase bus type for bus name */
422 len = strlen(typename) + 16;
424 len = snprintf(buf, len, "%s.%d", typename,
425 bus->parent ? bus->parent->num_child_bus : 0);
426 for (i = 0; i < len; i++)
427 buf[i] = qemu_tolower(buf[i]);
432 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
433 bus->parent->num_child_bus++;
434 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
435 } else if (bus != sysbus_get_default()) {
436 /* TODO: once all bus devices are qdevified,
437 only reset handler for main_system_bus should be registered here. */
438 qemu_register_reset(qbus_reset_all_fn, bus);
442 void qbus_create_inplace(BusState *bus, const char *typename,
443 DeviceState *parent, const char *name)
445 object_initialize(bus, typename);
447 bus->parent = parent;
448 bus->name = name ? g_strdup(name) : NULL;
452 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
456 bus = BUS(object_new(typename));
457 bus->qom_allocated = true;
459 bus->parent = parent;
460 bus->name = name ? g_strdup(name) : NULL;
466 void qbus_free(BusState *bus)
468 if (bus->qom_allocated) {
469 object_delete(OBJECT(bus));
471 object_finalize(OBJECT(bus));
472 if (bus->glib_allocated) {
478 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
480 BusClass *bc = BUS_GET_CLASS(bus);
482 if (bc->get_fw_dev_path) {
483 return bc->get_fw_dev_path(dev);
489 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
493 if (dev && dev->parent_bus) {
495 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
496 d = bus_get_fw_dev_path(dev->parent_bus, dev);
498 l += snprintf(p + l, size - l, "%s", d);
501 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
504 l += snprintf(p + l , size - l, "/");
509 char* qdev_get_fw_dev_path(DeviceState *dev)
514 l = qdev_get_fw_dev_path_helper(dev, path, 128);
518 return g_strdup(path);
521 char *qdev_get_dev_path(DeviceState *dev)
525 if (!dev || !dev->parent_bus) {
529 bc = BUS_GET_CLASS(dev->parent_bus);
530 if (bc->get_dev_path) {
531 return bc->get_dev_path(dev);
538 * Legacy property handling
541 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
542 const char *name, Error **errp)
544 DeviceState *dev = DEVICE(obj);
545 Property *prop = opaque;
550 prop->info->print(dev, prop, buffer, sizeof(buffer));
551 visit_type_str(v, &ptr, name, errp);
554 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
555 const char *name, Error **errp)
557 DeviceState *dev = DEVICE(obj);
558 Property *prop = opaque;
559 Error *local_err = NULL;
563 if (dev->state != DEV_STATE_CREATED) {
564 error_set(errp, QERR_PERMISSION_DENIED);
568 visit_type_str(v, &ptr, name, &local_err);
570 error_propagate(errp, local_err);
574 ret = prop->info->parse(dev, prop, ptr);
575 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
580 * @qdev_add_legacy_property - adds a legacy property
582 * Do not use this is new code! Properties added through this interface will
583 * be given names and types in the "legacy" namespace.
585 * Legacy properties are string versions of other OOM properties. The format
586 * of the string depends on the property type.
588 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
593 /* Register pointer properties as legacy properties */
594 if (!prop->info->print && !prop->info->parse &&
595 (prop->info->set || prop->info->get)) {
599 name = g_strdup_printf("legacy-%s", prop->name);
600 type = g_strdup_printf("legacy<%s>",
601 prop->info->legacy_name ?: prop->info->name);
603 object_property_add(OBJECT(dev), name, type,
604 prop->info->print ? qdev_get_legacy_property : prop->info->get,
605 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
614 * @qdev_property_add_static - add a @Property to a device.
616 * Static properties access data in a struct. The actual type of the
617 * property and the field depends on the property type.
619 void qdev_property_add_static(DeviceState *dev, Property *prop,
622 Error *local_err = NULL;
623 Object *obj = OBJECT(dev);
626 * TODO qdev_prop_ptr does not have getters or setters. It must
627 * go now that it can be replaced with links. The test should be
628 * removed along with it: all static properties are read/write.
630 if (!prop->info->get && !prop->info->set) {
634 object_property_add(obj, prop->name, prop->info->name,
635 prop->info->get, prop->info->set,
640 error_propagate(errp, local_err);
643 if (prop->qtype == QTYPE_NONE) {
647 if (prop->qtype == QTYPE_QBOOL) {
648 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
649 } else if (prop->info->enum_table) {
650 object_property_set_str(obj, prop->info->enum_table[prop->defval],
651 prop->name, &local_err);
652 } else if (prop->qtype == QTYPE_QINT) {
653 object_property_set_int(obj, prop->defval, prop->name, &local_err);
655 assert_no_error(local_err);
658 static void device_initfn(Object *obj)
660 DeviceState *dev = DEVICE(obj);
666 qdev_hot_added = true;
669 dev->instance_id_alias = -1;
670 dev->state = DEV_STATE_CREATED;
672 class = object_get_class(OBJECT(dev));
674 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
675 qdev_property_add_legacy(dev, prop, NULL);
676 qdev_property_add_static(dev, prop, NULL);
678 class = object_class_get_parent(class);
679 } while (class != object_class_by_name(TYPE_DEVICE));
680 qdev_prop_set_globals(dev);
682 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
683 (Object **)&dev->parent_bus, NULL);
686 /* Unlink device from bus and free the structure. */
687 static void device_finalize(Object *obj)
689 DeviceState *dev = DEVICE(obj);
691 DeviceClass *dc = DEVICE_GET_CLASS(dev);
693 if (dev->state == DEV_STATE_INITIALIZED) {
694 while (dev->num_child_bus) {
695 bus = QLIST_FIRST(&dev->child_bus);
698 if (qdev_get_vmsd(dev)) {
699 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
705 qemu_opts_del(dev->opts);
708 if (dev->parent_bus) {
709 bus_remove_child(dev->parent_bus, dev);
713 static void device_class_base_init(ObjectClass *class, void *data)
715 DeviceClass *klass = DEVICE_CLASS(class);
717 /* We explicitly look up properties in the superclasses,
718 * so do not propagate them to the subclasses.
723 void device_reset(DeviceState *dev)
725 DeviceClass *klass = DEVICE_GET_CLASS(dev);
732 Object *qdev_get_machine(void)
737 dev = container_get(object_get_root(), "/machine");
743 static TypeInfo device_type_info = {
745 .parent = TYPE_OBJECT,
746 .instance_size = sizeof(DeviceState),
747 .instance_init = device_initfn,
748 .instance_finalize = device_finalize,
749 .class_base_init = device_class_base_init,
751 .class_size = sizeof(DeviceClass),
754 static void qbus_initfn(Object *obj)
756 BusState *bus = BUS(obj);
758 QTAILQ_INIT(&bus->children);
761 static void qbus_finalize(Object *obj)
763 BusState *bus = BUS(obj);
766 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
767 DeviceState *dev = kid->child;
771 QLIST_REMOVE(bus, sibling);
772 bus->parent->num_child_bus--;
774 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
775 qemu_unregister_reset(qbus_reset_all_fn, bus);
777 g_free((char *)bus->name);
780 static const TypeInfo bus_info = {
782 .parent = TYPE_OBJECT,
783 .instance_size = sizeof(BusState),
785 .class_size = sizeof(BusClass),
786 .instance_init = qbus_initfn,
787 .instance_finalize = qbus_finalize,
790 static void qdev_register_types(void)
792 type_register_static(&bus_info);
793 type_register_static(&device_type_info);
796 type_init(qdev_register_types)