qdev: Fold state enum into bool realized
[qemu/agraf.git] / hw / qdev.c
blob37a083d3db12eb1d8a3c2baab54214edb08fdedd
1 /*
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
26 this API directly. */
28 #include "qdev.h"
29 #include "sysemu/sysemu.h"
30 #include "qapi/error.h"
31 #include "qapi/visitor.h"
33 int qdev_hotplug = 0;
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);
40 return dc->vmsd;
43 const char *qdev_fw_name(DeviceState *dev)
45 DeviceClass *dc = DEVICE_GET_CLASS(dev);
47 if (dc->fw_name) {
48 return dc->fw_name;
51 return object_get_typename(OBJECT(dev));
54 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
55 Error **errp);
57 static void bus_remove_child(BusState *bus, DeviceState *child)
59 BusChild *kid;
61 QTAILQ_FOREACH(kid, &bus->children, sibling) {
62 if (kid->child == child) {
63 char name[32];
65 snprintf(name, sizeof(name), "child[%d]", kid->index);
66 QTAILQ_REMOVE(&bus->children, kid, sibling);
67 object_property_del(OBJECT(bus), name, NULL);
68 g_free(kid);
69 return;
74 static void bus_add_child(BusState *bus, DeviceState *child)
76 char name[32];
77 BusChild *kid = g_malloc0(sizeof(*kid));
79 if (qdev_hotplug) {
80 assert(bus->allow_hotplug);
83 kid->index = bus->max_index++;
84 kid->child = child;
86 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
88 snprintf(name, sizeof(name), "child[%d]", kid->index);
89 object_property_add_link(OBJECT(bus), name,
90 object_get_typename(OBJECT(child)),
91 (Object **)&kid->child,
92 NULL);
95 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
97 dev->parent_bus = bus;
98 bus_add_child(bus, dev);
101 /* Create a new device. This only initializes the device state structure
102 and allows properties to be set. qdev_init should be called to
103 initialize the actual device emulation. */
104 DeviceState *qdev_create(BusState *bus, const char *name)
106 DeviceState *dev;
108 dev = qdev_try_create(bus, name);
109 if (!dev) {
110 if (bus) {
111 error_report("Unknown device '%s' for bus '%s'\n", name,
112 object_get_typename(OBJECT(bus)));
113 abort();
114 } else {
115 error_report("Unknown device '%s' for default sysbus\n", name);
116 abort();
120 return dev;
123 DeviceState *qdev_try_create(BusState *bus, const char *type)
125 DeviceState *dev;
127 if (object_class_by_name(type) == NULL) {
128 return NULL;
130 dev = DEVICE(object_new(type));
131 if (!dev) {
132 return NULL;
135 if (!bus) {
136 bus = sysbus_get_default();
139 qdev_set_parent_bus(dev, bus);
141 return dev;
144 /* Initialize a device. Device properties should be set before calling
145 this function. IRQs and MMIO regions should be connected/mapped after
146 calling this function.
147 On failure, destroy the device and return negative value.
148 Return 0 on success. */
149 int qdev_init(DeviceState *dev)
151 DeviceClass *dc = DEVICE_GET_CLASS(dev);
152 int rc;
154 assert(!dev->realized);
156 rc = dc->init(dev);
157 if (rc < 0) {
158 qdev_free(dev);
159 return rc;
162 if (!OBJECT(dev)->parent) {
163 static int unattached_count = 0;
164 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
166 object_property_add_child(container_get(qdev_get_machine(),
167 "/unattached"),
168 name, OBJECT(dev), NULL);
169 g_free(name);
172 if (qdev_get_vmsd(dev)) {
173 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
174 dev->instance_id_alias,
175 dev->alias_required_for_version);
177 dev->realized = true;
178 if (dev->hotplugged) {
179 device_reset(dev);
181 return 0;
184 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
185 int required_for_version)
187 assert(!dev->realized);
188 dev->instance_id_alias = alias_id;
189 dev->alias_required_for_version = required_for_version;
192 void qdev_unplug(DeviceState *dev, Error **errp)
194 DeviceClass *dc = DEVICE_GET_CLASS(dev);
196 if (!dev->parent_bus->allow_hotplug) {
197 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
198 return;
200 assert(dc->unplug != NULL);
202 qdev_hot_removed = true;
204 if (dc->unplug(dev) < 0) {
205 error_set(errp, QERR_UNDEFINED_ERROR);
206 return;
210 static int qdev_reset_one(DeviceState *dev, void *opaque)
212 device_reset(dev);
214 return 0;
217 static int qbus_reset_one(BusState *bus, void *opaque)
219 BusClass *bc = BUS_GET_CLASS(bus);
220 if (bc->reset) {
221 return bc->reset(bus);
223 return 0;
226 void qdev_reset_all(DeviceState *dev)
228 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
231 void qbus_reset_all(BusState *bus)
233 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
236 void qbus_reset_all_fn(void *opaque)
238 BusState *bus = opaque;
239 qbus_reset_all(bus);
242 /* can be used as ->unplug() callback for the simple cases */
243 int qdev_simple_unplug_cb(DeviceState *dev)
245 /* just zap it */
246 qdev_free(dev);
247 return 0;
251 /* Like qdev_init(), but terminate program via error_report() instead of
252 returning an error value. This is okay during machine creation.
253 Don't use for hotplug, because there callers need to recover from
254 failure. Exception: if you know the device's init() callback can't
255 fail, then qdev_init_nofail() can't fail either, and is therefore
256 usable even then. But relying on the device implementation that
257 way is somewhat unclean, and best avoided. */
258 void qdev_init_nofail(DeviceState *dev)
260 const char *typename = object_get_typename(OBJECT(dev));
262 if (qdev_init(dev) < 0) {
263 error_report("Initialization of device %s failed", typename);
264 exit(1);
268 /* Unlink device from bus and free the structure. */
269 void qdev_free(DeviceState *dev)
271 object_delete(OBJECT(dev));
274 void qdev_machine_creation_done(void)
277 * ok, initial machine setup is done, starting from now we can
278 * only create hotpluggable devices
280 qdev_hotplug = 1;
283 bool qdev_machine_modified(void)
285 return qdev_hot_added || qdev_hot_removed;
288 BusState *qdev_get_parent_bus(DeviceState *dev)
290 return dev->parent_bus;
293 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
295 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
296 dev, n);
297 dev->num_gpio_in += n;
300 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
302 assert(dev->num_gpio_out == 0);
303 dev->num_gpio_out = n;
304 dev->gpio_out = pins;
307 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
309 assert(n >= 0 && n < dev->num_gpio_in);
310 return dev->gpio_in[n];
313 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
315 assert(n >= 0 && n < dev->num_gpio_out);
316 dev->gpio_out[n] = pin;
319 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
321 BusState *bus;
323 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
324 if (strcmp(name, bus->name) == 0) {
325 return bus;
328 return NULL;
331 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
332 qbus_walkerfn *busfn, void *opaque)
334 BusChild *kid;
335 int err;
337 if (busfn) {
338 err = busfn(bus, opaque);
339 if (err) {
340 return err;
344 QTAILQ_FOREACH(kid, &bus->children, sibling) {
345 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
346 if (err < 0) {
347 return err;
351 return 0;
354 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
355 qbus_walkerfn *busfn, void *opaque)
357 BusState *bus;
358 int err;
360 if (devfn) {
361 err = devfn(dev, opaque);
362 if (err) {
363 return err;
367 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
368 err = qbus_walk_children(bus, devfn, busfn, opaque);
369 if (err < 0) {
370 return err;
374 return 0;
377 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
379 BusChild *kid;
380 DeviceState *ret;
381 BusState *child;
383 QTAILQ_FOREACH(kid, &bus->children, sibling) {
384 DeviceState *dev = kid->child;
386 if (dev->id && strcmp(dev->id, id) == 0) {
387 return dev;
390 QLIST_FOREACH(child, &dev->child_bus, sibling) {
391 ret = qdev_find_recursive(child, id);
392 if (ret) {
393 return ret;
397 return NULL;
400 static void qbus_realize(BusState *bus)
402 const char *typename = object_get_typename(OBJECT(bus));
403 char *buf;
404 int i,len;
406 if (bus->name) {
407 /* use supplied 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;
411 buf = g_malloc(len);
412 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
413 bus->name = buf;
414 } else {
415 /* no id -> use lowercase bus type for bus name */
416 len = strlen(typename) + 16;
417 buf = g_malloc(len);
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]);
422 bus->name = buf;
425 if (bus->parent) {
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 void qbus_create_inplace(BusState *bus, const char *typename,
437 DeviceState *parent, const char *name)
439 object_initialize(bus, typename);
441 bus->parent = parent;
442 bus->name = name ? g_strdup(name) : NULL;
443 qbus_realize(bus);
446 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
448 BusState *bus;
450 bus = BUS(object_new(typename));
452 bus->parent = parent;
453 bus->name = name ? g_strdup(name) : NULL;
454 qbus_realize(bus);
456 return bus;
459 void qbus_free(BusState *bus)
461 object_delete(OBJECT(bus));
464 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
466 BusClass *bc = BUS_GET_CLASS(bus);
468 if (bc->get_fw_dev_path) {
469 return bc->get_fw_dev_path(dev);
472 return NULL;
475 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
477 int l = 0;
479 if (dev && dev->parent_bus) {
480 char *d;
481 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
482 d = bus_get_fw_dev_path(dev->parent_bus, dev);
483 if (d) {
484 l += snprintf(p + l, size - l, "%s", d);
485 g_free(d);
486 } else {
487 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
490 l += snprintf(p + l , size - l, "/");
492 return l;
495 char* qdev_get_fw_dev_path(DeviceState *dev)
497 char path[128];
498 int l;
500 l = qdev_get_fw_dev_path_helper(dev, path, 128);
502 path[l-1] = '\0';
504 return g_strdup(path);
507 char *qdev_get_dev_path(DeviceState *dev)
509 BusClass *bc;
511 if (!dev || !dev->parent_bus) {
512 return NULL;
515 bc = BUS_GET_CLASS(dev->parent_bus);
516 if (bc->get_dev_path) {
517 return bc->get_dev_path(dev);
520 return NULL;
524 * Legacy property handling
527 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
528 const char *name, Error **errp)
530 DeviceState *dev = DEVICE(obj);
531 Property *prop = opaque;
533 char buffer[1024];
534 char *ptr = buffer;
536 prop->info->print(dev, prop, buffer, sizeof(buffer));
537 visit_type_str(v, &ptr, name, errp);
540 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
541 const char *name, Error **errp)
543 DeviceState *dev = DEVICE(obj);
544 Property *prop = opaque;
545 Error *local_err = NULL;
546 char *ptr = NULL;
547 int ret;
549 if (dev->realized) {
550 error_set(errp, QERR_PERMISSION_DENIED);
551 return;
554 visit_type_str(v, &ptr, name, &local_err);
555 if (local_err) {
556 error_propagate(errp, local_err);
557 return;
560 ret = prop->info->parse(dev, prop, ptr);
561 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
562 g_free(ptr);
566 * @qdev_add_legacy_property - adds a legacy property
568 * Do not use this is new code! Properties added through this interface will
569 * be given names and types in the "legacy" namespace.
571 * Legacy properties are string versions of other OOM properties. The format
572 * of the string depends on the property type.
574 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
575 Error **errp)
577 gchar *name, *type;
579 /* Register pointer properties as legacy properties */
580 if (!prop->info->print && !prop->info->parse &&
581 (prop->info->set || prop->info->get)) {
582 return;
585 name = g_strdup_printf("legacy-%s", prop->name);
586 type = g_strdup_printf("legacy<%s>",
587 prop->info->legacy_name ?: prop->info->name);
589 object_property_add(OBJECT(dev), name, type,
590 prop->info->print ? qdev_get_legacy_property : prop->info->get,
591 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
592 NULL,
593 prop, errp);
595 g_free(type);
596 g_free(name);
600 * @qdev_property_add_static - add a @Property to a device.
602 * Static properties access data in a struct. The actual type of the
603 * property and the field depends on the property type.
605 void qdev_property_add_static(DeviceState *dev, Property *prop,
606 Error **errp)
608 Error *local_err = NULL;
609 Object *obj = OBJECT(dev);
612 * TODO qdev_prop_ptr does not have getters or setters. It must
613 * go now that it can be replaced with links. The test should be
614 * removed along with it: all static properties are read/write.
616 if (!prop->info->get && !prop->info->set) {
617 return;
620 object_property_add(obj, prop->name, prop->info->name,
621 prop->info->get, prop->info->set,
622 prop->info->release,
623 prop, &local_err);
625 if (local_err) {
626 error_propagate(errp, local_err);
627 return;
629 if (prop->qtype == QTYPE_NONE) {
630 return;
633 if (prop->qtype == QTYPE_QBOOL) {
634 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
635 } else if (prop->info->enum_table) {
636 object_property_set_str(obj, prop->info->enum_table[prop->defval],
637 prop->name, &local_err);
638 } else if (prop->qtype == QTYPE_QINT) {
639 object_property_set_int(obj, prop->defval, prop->name, &local_err);
641 assert_no_error(local_err);
644 static void device_initfn(Object *obj)
646 DeviceState *dev = DEVICE(obj);
647 ObjectClass *class;
648 Property *prop;
650 if (qdev_hotplug) {
651 dev->hotplugged = 1;
652 qdev_hot_added = true;
655 dev->instance_id_alias = -1;
656 dev->realized = false;
658 class = object_get_class(OBJECT(dev));
659 do {
660 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
661 qdev_property_add_legacy(dev, prop, NULL);
662 qdev_property_add_static(dev, prop, NULL);
664 class = object_class_get_parent(class);
665 } while (class != object_class_by_name(TYPE_DEVICE));
666 qdev_prop_set_globals(dev);
668 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
669 (Object **)&dev->parent_bus, NULL);
672 /* Unlink device from bus and free the structure. */
673 static void device_finalize(Object *obj)
675 DeviceState *dev = DEVICE(obj);
676 BusState *bus;
677 DeviceClass *dc = DEVICE_GET_CLASS(dev);
679 if (dev->realized) {
680 while (dev->num_child_bus) {
681 bus = QLIST_FIRST(&dev->child_bus);
682 qbus_free(bus);
684 if (qdev_get_vmsd(dev)) {
685 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
687 if (dc->exit) {
688 dc->exit(dev);
690 if (dev->opts) {
691 qemu_opts_del(dev->opts);
696 static void device_class_base_init(ObjectClass *class, void *data)
698 DeviceClass *klass = DEVICE_CLASS(class);
700 /* We explicitly look up properties in the superclasses,
701 * so do not propagate them to the subclasses.
703 klass->props = NULL;
706 static void device_unparent(Object *obj)
708 DeviceState *dev = DEVICE(obj);
710 if (dev->parent_bus != NULL) {
711 bus_remove_child(dev->parent_bus, dev);
715 static void device_class_init(ObjectClass *class, void *data)
717 class->unparent = device_unparent;
720 void device_reset(DeviceState *dev)
722 DeviceClass *klass = DEVICE_GET_CLASS(dev);
724 if (klass->reset) {
725 klass->reset(dev);
729 Object *qdev_get_machine(void)
731 static Object *dev;
733 if (dev == NULL) {
734 dev = container_get(object_get_root(), "/machine");
737 return dev;
740 static const TypeInfo device_type_info = {
741 .name = TYPE_DEVICE,
742 .parent = TYPE_OBJECT,
743 .instance_size = sizeof(DeviceState),
744 .instance_init = device_initfn,
745 .instance_finalize = device_finalize,
746 .class_base_init = device_class_base_init,
747 .class_init = device_class_init,
748 .abstract = true,
749 .class_size = sizeof(DeviceClass),
752 static void qbus_initfn(Object *obj)
754 BusState *bus = BUS(obj);
756 QTAILQ_INIT(&bus->children);
759 static void qbus_finalize(Object *obj)
761 BusState *bus = BUS(obj);
762 BusChild *kid;
764 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
765 DeviceState *dev = kid->child;
766 qdev_free(dev);
768 if (bus->parent) {
769 QLIST_REMOVE(bus, sibling);
770 bus->parent->num_child_bus--;
771 } else {
772 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
773 qemu_unregister_reset(qbus_reset_all_fn, bus);
775 g_free((char *)bus->name);
778 static const TypeInfo bus_info = {
779 .name = TYPE_BUS,
780 .parent = TYPE_OBJECT,
781 .instance_size = sizeof(BusState),
782 .abstract = true,
783 .class_size = sizeof(BusClass),
784 .instance_init = qbus_initfn,
785 .instance_finalize = qbus_finalize,
788 static void qdev_register_types(void)
790 type_register_static(&bus_info);
791 type_register_static(&device_type_info);
794 type_init(qdev_register_types)