virtio-pci: drop unused wmb macro
[qemu/qmp-unstable.git] / hw / core / qdev.c
blob6985ad870c2f7b666c26b374670d952b81eedc55
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 "hw/qdev.h"
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"
36 int qdev_hotplug = 0;
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);
43 return dc->vmsd;
46 const char *qdev_fw_name(DeviceState *dev)
48 DeviceClass *dc = DEVICE_GET_CLASS(dev);
50 if (dc->fw_name) {
51 return dc->fw_name;
54 return object_get_typename(OBJECT(dev));
57 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
58 Error **errp);
60 static void bus_remove_child(BusState *bus, DeviceState *child)
62 BusChild *kid;
64 QTAILQ_FOREACH(kid, &bus->children, sibling) {
65 if (kid->child == child) {
66 char name[32];
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));
74 g_free(kid);
75 return;
80 static void bus_add_child(BusState *bus, DeviceState *child)
82 char name[32];
83 BusChild *kid = g_malloc0(sizeof(*kid));
85 if (qdev_hotplug) {
86 assert(bus->allow_hotplug);
89 kid->index = bus->max_index++;
90 kid->child = child;
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,
100 NULL);
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)
115 DeviceState *dev;
117 dev = qdev_try_create(bus, name);
118 if (!dev) {
119 if (bus) {
120 error_report("Unknown device '%s' for bus '%s'", name,
121 object_get_typename(OBJECT(bus)));
122 } else {
123 error_report("Unknown device '%s' for default sysbus", name);
125 abort();
128 return dev;
131 DeviceState *qdev_try_create(BusState *bus, const char *type)
133 DeviceState *dev;
135 if (object_class_by_name(type) == NULL) {
136 return NULL;
138 dev = DEVICE(object_new(type));
139 if (!dev) {
140 return NULL;
143 if (!bus) {
144 bus = sysbus_get_default();
147 qdev_set_parent_bus(dev, bus);
148 object_unref(OBJECT(dev));
149 return 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 qerror_report_err(local_err);
166 error_free(local_err);
167 qdev_free(dev);
168 return -1;
170 return 0;
173 static void device_realize(DeviceState *dev, Error **err)
175 DeviceClass *dc = DEVICE_GET_CLASS(dev);
177 if (dc->init) {
178 int rc = dc->init(dev);
179 if (rc < 0) {
180 error_setg(err, "Device initialization failed.");
181 return;
186 static void device_unrealize(DeviceState *dev, Error **errp)
188 DeviceClass *dc = DEVICE_GET_CLASS(dev);
190 if (dc->exit) {
191 int rc = dc->exit(dev);
192 if (rc < 0) {
193 error_setg(errp, "Device exit failed.");
194 return;
199 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
200 int required_for_version)
202 assert(!dev->realized);
203 dev->instance_id_alias = alias_id;
204 dev->alias_required_for_version = required_for_version;
207 void qdev_unplug(DeviceState *dev, Error **errp)
209 DeviceClass *dc = DEVICE_GET_CLASS(dev);
211 if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
212 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
213 return;
215 assert(dc->unplug != NULL);
217 qdev_hot_removed = true;
219 if (dc->unplug(dev) < 0) {
220 error_set(errp, QERR_UNDEFINED_ERROR);
221 return;
225 static int qdev_reset_one(DeviceState *dev, void *opaque)
227 device_reset(dev);
229 return 0;
232 static int qbus_reset_one(BusState *bus, void *opaque)
234 BusClass *bc = BUS_GET_CLASS(bus);
235 if (bc->reset) {
236 return bc->reset(bus);
238 return 0;
241 void qdev_reset_all(DeviceState *dev)
243 qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
246 void qbus_reset_all(BusState *bus)
248 qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
251 void qbus_reset_all_fn(void *opaque)
253 BusState *bus = opaque;
254 qbus_reset_all(bus);
257 /* can be used as ->unplug() callback for the simple cases */
258 int qdev_simple_unplug_cb(DeviceState *dev)
260 /* just zap it */
261 qdev_free(dev);
262 return 0;
266 /* Like qdev_init(), but terminate program via error_report() instead of
267 returning an error value. This is okay during machine creation.
268 Don't use for hotplug, because there callers need to recover from
269 failure. Exception: if you know the device's init() callback can't
270 fail, then qdev_init_nofail() can't fail either, and is therefore
271 usable even then. But relying on the device implementation that
272 way is somewhat unclean, and best avoided. */
273 void qdev_init_nofail(DeviceState *dev)
275 const char *typename = object_get_typename(OBJECT(dev));
277 if (qdev_init(dev) < 0) {
278 error_report("Initialization of device %s failed", typename);
279 exit(1);
283 /* Unlink device from bus and free the structure. */
284 void qdev_free(DeviceState *dev)
286 object_unparent(OBJECT(dev));
289 void qdev_machine_creation_done(void)
292 * ok, initial machine setup is done, starting from now we can
293 * only create hotpluggable devices
295 qdev_hotplug = 1;
298 bool qdev_machine_modified(void)
300 return qdev_hot_added || qdev_hot_removed;
303 BusState *qdev_get_parent_bus(DeviceState *dev)
305 return dev->parent_bus;
308 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
310 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
311 dev, n);
312 dev->num_gpio_in += n;
315 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
317 assert(dev->num_gpio_out == 0);
318 dev->num_gpio_out = n;
319 dev->gpio_out = pins;
322 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
324 assert(n >= 0 && n < dev->num_gpio_in);
325 return dev->gpio_in[n];
328 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
330 assert(n >= 0 && n < dev->num_gpio_out);
331 dev->gpio_out[n] = pin;
334 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
336 BusState *bus;
338 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
339 if (strcmp(name, bus->name) == 0) {
340 return bus;
343 return NULL;
346 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
347 qbus_walkerfn *busfn, void *opaque)
349 BusChild *kid;
350 int err;
352 if (busfn) {
353 err = busfn(bus, opaque);
354 if (err) {
355 return err;
359 QTAILQ_FOREACH(kid, &bus->children, sibling) {
360 err = qdev_walk_children(kid->child, devfn, busfn, opaque);
361 if (err < 0) {
362 return err;
366 return 0;
369 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
370 qbus_walkerfn *busfn, void *opaque)
372 BusState *bus;
373 int err;
375 if (devfn) {
376 err = devfn(dev, opaque);
377 if (err) {
378 return err;
382 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
383 err = qbus_walk_children(bus, devfn, busfn, opaque);
384 if (err < 0) {
385 return err;
389 return 0;
392 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
394 BusChild *kid;
395 DeviceState *ret;
396 BusState *child;
398 QTAILQ_FOREACH(kid, &bus->children, sibling) {
399 DeviceState *dev = kid->child;
401 if (dev->id && strcmp(dev->id, id) == 0) {
402 return dev;
405 QLIST_FOREACH(child, &dev->child_bus, sibling) {
406 ret = qdev_find_recursive(child, id);
407 if (ret) {
408 return ret;
412 return NULL;
415 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
417 const char *typename = object_get_typename(OBJECT(bus));
418 char *buf;
419 int i,len;
421 bus->parent = parent;
423 if (name) {
424 bus->name = g_strdup(name);
425 } else if (bus->parent && bus->parent->id) {
426 /* parent device has id -> use it for bus name */
427 len = strlen(bus->parent->id) + 16;
428 buf = g_malloc(len);
429 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
430 bus->name = buf;
431 } else {
432 /* no id -> use lowercase bus type for bus name */
433 len = strlen(typename) + 16;
434 buf = g_malloc(len);
435 len = snprintf(buf, len, "%s.%d", typename,
436 bus->parent ? bus->parent->num_child_bus : 0);
437 for (i = 0; i < len; i++)
438 buf[i] = qemu_tolower(buf[i]);
439 bus->name = buf;
442 if (bus->parent) {
443 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
444 bus->parent->num_child_bus++;
445 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
446 object_unref(OBJECT(bus));
447 } else if (bus != sysbus_get_default()) {
448 /* TODO: once all bus devices are qdevified,
449 only reset handler for main_system_bus should be registered here. */
450 qemu_register_reset(qbus_reset_all_fn, bus);
454 static void bus_unparent(Object *obj)
456 BusState *bus = BUS(obj);
457 BusChild *kid;
459 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
460 DeviceState *dev = kid->child;
461 qdev_free(dev);
463 if (bus->parent) {
464 QLIST_REMOVE(bus, sibling);
465 bus->parent->num_child_bus--;
466 bus->parent = NULL;
467 } else {
468 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
469 qemu_unregister_reset(qbus_reset_all_fn, bus);
473 void qbus_create_inplace(void *bus, const char *typename,
474 DeviceState *parent, const char *name)
476 object_initialize(bus, typename);
477 qbus_realize(bus, parent, name);
480 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
482 BusState *bus;
484 bus = BUS(object_new(typename));
485 qbus_realize(bus, parent, name);
487 return bus;
490 void qbus_free(BusState *bus)
492 object_unparent(OBJECT(bus));
495 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
497 BusClass *bc = BUS_GET_CLASS(bus);
499 if (bc->get_fw_dev_path) {
500 return bc->get_fw_dev_path(dev);
503 return NULL;
506 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
508 int l = 0;
510 if (dev && dev->parent_bus) {
511 char *d;
512 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
513 d = bus_get_fw_dev_path(dev->parent_bus, dev);
514 if (d) {
515 l += snprintf(p + l, size - l, "%s", d);
516 g_free(d);
517 } else {
518 l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
521 l += snprintf(p + l , size - l, "/");
523 return l;
526 char* qdev_get_fw_dev_path(DeviceState *dev)
528 char path[128];
529 int l;
531 l = qdev_get_fw_dev_path_helper(dev, path, 128);
533 path[l-1] = '\0';
535 return g_strdup(path);
538 char *qdev_get_dev_path(DeviceState *dev)
540 BusClass *bc;
542 if (!dev || !dev->parent_bus) {
543 return NULL;
546 bc = BUS_GET_CLASS(dev->parent_bus);
547 if (bc->get_dev_path) {
548 return bc->get_dev_path(dev);
551 return NULL;
555 * Legacy property handling
558 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
559 const char *name, Error **errp)
561 DeviceState *dev = DEVICE(obj);
562 Property *prop = opaque;
564 char buffer[1024];
565 char *ptr = buffer;
567 prop->info->print(dev, prop, buffer, sizeof(buffer));
568 visit_type_str(v, &ptr, name, errp);
571 static void qdev_set_legacy_property(Object *obj, Visitor *v, void *opaque,
572 const char *name, Error **errp)
574 DeviceState *dev = DEVICE(obj);
575 Property *prop = opaque;
576 Error *local_err = NULL;
577 char *ptr = NULL;
578 int ret;
580 if (dev->realized) {
581 qdev_prop_set_after_realize(dev, name, errp);
582 return;
585 visit_type_str(v, &ptr, name, &local_err);
586 if (local_err) {
587 error_propagate(errp, local_err);
588 return;
591 ret = prop->info->parse(dev, prop, ptr);
592 error_set_from_qdev_prop_error(errp, ret, dev, prop, ptr);
593 g_free(ptr);
597 * @qdev_add_legacy_property - adds a legacy property
599 * Do not use this is new code! Properties added through this interface will
600 * be given names and types in the "legacy" namespace.
602 * Legacy properties are string versions of other OOM properties. The format
603 * of the string depends on the property type.
605 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
606 Error **errp)
608 gchar *name, *type;
610 /* Register pointer properties as legacy properties */
611 if (!prop->info->print && !prop->info->parse &&
612 (prop->info->set || prop->info->get)) {
613 return;
616 name = g_strdup_printf("legacy-%s", prop->name);
617 type = g_strdup_printf("legacy<%s>",
618 prop->info->legacy_name ?: prop->info->name);
620 object_property_add(OBJECT(dev), name, type,
621 prop->info->print ? qdev_get_legacy_property : prop->info->get,
622 prop->info->parse ? qdev_set_legacy_property : prop->info->set,
623 NULL,
624 prop, errp);
626 g_free(type);
627 g_free(name);
631 * @qdev_property_add_static - add a @Property to a device.
633 * Static properties access data in a struct. The actual type of the
634 * property and the field depends on the property type.
636 void qdev_property_add_static(DeviceState *dev, Property *prop,
637 Error **errp)
639 Error *local_err = NULL;
640 Object *obj = OBJECT(dev);
643 * TODO qdev_prop_ptr does not have getters or setters. It must
644 * go now that it can be replaced with links. The test should be
645 * removed along with it: all static properties are read/write.
647 if (!prop->info->get && !prop->info->set) {
648 return;
651 object_property_add(obj, prop->name, prop->info->name,
652 prop->info->get, prop->info->set,
653 prop->info->release,
654 prop, &local_err);
656 if (local_err) {
657 error_propagate(errp, local_err);
658 return;
660 if (prop->qtype == QTYPE_NONE) {
661 return;
664 if (prop->qtype == QTYPE_QBOOL) {
665 object_property_set_bool(obj, prop->defval, prop->name, &local_err);
666 } else if (prop->info->enum_table) {
667 object_property_set_str(obj, prop->info->enum_table[prop->defval],
668 prop->name, &local_err);
669 } else if (prop->qtype == QTYPE_QINT) {
670 object_property_set_int(obj, prop->defval, prop->name, &local_err);
672 assert_no_error(local_err);
675 static bool device_get_realized(Object *obj, Error **err)
677 DeviceState *dev = DEVICE(obj);
678 return dev->realized;
681 static void device_set_realized(Object *obj, bool value, Error **err)
683 DeviceState *dev = DEVICE(obj);
684 DeviceClass *dc = DEVICE_GET_CLASS(dev);
685 Error *local_err = NULL;
687 if (value && !dev->realized) {
688 if (!obj->parent && local_err == NULL) {
689 static int unattached_count;
690 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
692 object_property_add_child(container_get(qdev_get_machine(),
693 "/unattached"),
694 name, obj, &local_err);
695 g_free(name);
698 if (dc->realize) {
699 dc->realize(dev, &local_err);
702 if (qdev_get_vmsd(dev) && local_err == NULL) {
703 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
704 dev->instance_id_alias,
705 dev->alias_required_for_version);
707 if (dev->hotplugged && local_err == NULL) {
708 device_reset(dev);
710 } else if (!value && dev->realized) {
711 if (qdev_get_vmsd(dev)) {
712 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
714 if (dc->unrealize) {
715 dc->unrealize(dev, &local_err);
719 if (local_err != NULL) {
720 error_propagate(err, local_err);
721 return;
724 dev->realized = value;
727 static void device_initfn(Object *obj)
729 DeviceState *dev = DEVICE(obj);
730 ObjectClass *class;
731 Property *prop;
732 Error *err = NULL;
734 if (qdev_hotplug) {
735 dev->hotplugged = 1;
736 qdev_hot_added = true;
739 dev->instance_id_alias = -1;
740 dev->realized = false;
742 object_property_add_bool(obj, "realized",
743 device_get_realized, device_set_realized, NULL);
745 class = object_get_class(OBJECT(dev));
746 do {
747 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
748 qdev_property_add_legacy(dev, prop, &err);
749 assert_no_error(err);
750 qdev_property_add_static(dev, prop, &err);
751 assert_no_error(err);
753 class = object_class_get_parent(class);
754 } while (class != object_class_by_name(TYPE_DEVICE));
755 qdev_prop_set_globals(dev, &err);
756 if (err != NULL) {
757 qerror_report_err(err);
758 error_free(err);
759 exit(1);
762 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
763 (Object **)&dev->parent_bus, &err);
764 assert_no_error(err);
767 /* Unlink device from bus and free the structure. */
768 static void device_finalize(Object *obj)
770 DeviceState *dev = DEVICE(obj);
771 if (dev->opts) {
772 qemu_opts_del(dev->opts);
776 static void device_class_base_init(ObjectClass *class, void *data)
778 DeviceClass *klass = DEVICE_CLASS(class);
780 /* We explicitly look up properties in the superclasses,
781 * so do not propagate them to the subclasses.
783 klass->props = NULL;
786 static void device_unparent(Object *obj)
788 DeviceState *dev = DEVICE(obj);
789 BusState *bus;
790 QObject *event_data;
791 bool have_realized = dev->realized;
793 while (dev->num_child_bus) {
794 bus = QLIST_FIRST(&dev->child_bus);
795 qbus_free(bus);
797 if (dev->realized) {
798 object_property_set_bool(obj, false, "realized", NULL);
800 if (dev->parent_bus) {
801 bus_remove_child(dev->parent_bus, dev);
802 object_unref(OBJECT(dev->parent_bus));
803 dev->parent_bus = NULL;
806 /* Only send event if the device had been completely realized */
807 if (have_realized) {
808 gchar *path = object_get_canonical_path(OBJECT(dev));
810 if (dev->id) {
811 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
812 dev->id, path);
813 } else {
814 event_data = qobject_from_jsonf("{ 'path': %s }", path);
816 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
817 qobject_decref(event_data);
818 g_free(path);
822 static void device_class_init(ObjectClass *class, void *data)
824 DeviceClass *dc = DEVICE_CLASS(class);
826 class->unparent = device_unparent;
827 dc->realize = device_realize;
828 dc->unrealize = device_unrealize;
831 void device_reset(DeviceState *dev)
833 DeviceClass *klass = DEVICE_GET_CLASS(dev);
835 if (klass->reset) {
836 klass->reset(dev);
840 Object *qdev_get_machine(void)
842 static Object *dev;
844 if (dev == NULL) {
845 dev = container_get(object_get_root(), "/machine");
848 return dev;
851 static const TypeInfo device_type_info = {
852 .name = TYPE_DEVICE,
853 .parent = TYPE_OBJECT,
854 .instance_size = sizeof(DeviceState),
855 .instance_init = device_initfn,
856 .instance_finalize = device_finalize,
857 .class_base_init = device_class_base_init,
858 .class_init = device_class_init,
859 .abstract = true,
860 .class_size = sizeof(DeviceClass),
863 static void qbus_initfn(Object *obj)
865 BusState *bus = BUS(obj);
867 QTAILQ_INIT(&bus->children);
870 static void bus_class_init(ObjectClass *class, void *data)
872 class->unparent = bus_unparent;
875 static void qbus_finalize(Object *obj)
877 BusState *bus = BUS(obj);
879 g_free((char *)bus->name);
882 static const TypeInfo bus_info = {
883 .name = TYPE_BUS,
884 .parent = TYPE_OBJECT,
885 .instance_size = sizeof(BusState),
886 .abstract = true,
887 .class_size = sizeof(BusClass),
888 .instance_init = qbus_initfn,
889 .instance_finalize = qbus_finalize,
890 .class_init = bus_class_init,
893 static void qdev_register_types(void)
895 type_register_static(&bus_info);
896 type_register_static(&device_type_info);
899 type_init(qdev_register_types)