qdev: Legacy properties are now read-only
[qemu/ar7.git] / hw / core / qdev.c
blob7c1b7325caffbbb3bb57e290155216ea836e8b4b
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 object_unparent(OBJECT(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 bc->reset(bus);
238 return 0;
241 void qdev_reset_all(DeviceState *dev)
243 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
246 void qbus_reset_all(BusState *bus)
248 qbus_walk_children(bus, NULL, NULL, 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 object_unparent(OBJECT(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 void qdev_machine_creation_done(void)
286 * ok, initial machine setup is done, starting from now we can
287 * only create hotpluggable devices
289 qdev_hotplug = 1;
292 bool qdev_machine_modified(void)
294 return qdev_hot_added || qdev_hot_removed;
297 BusState *qdev_get_parent_bus(DeviceState *dev)
299 return dev->parent_bus;
302 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
304 dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
305 dev, n);
306 dev->num_gpio_in += n;
309 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
311 assert(dev->num_gpio_out == 0);
312 dev->num_gpio_out = n;
313 dev->gpio_out = pins;
316 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
318 assert(n >= 0 && n < dev->num_gpio_in);
319 return dev->gpio_in[n];
322 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
324 assert(n >= 0 && n < dev->num_gpio_out);
325 dev->gpio_out[n] = pin;
328 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
330 BusState *bus;
332 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
333 if (strcmp(name, bus->name) == 0) {
334 return bus;
337 return NULL;
340 int qbus_walk_children(BusState *bus,
341 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
342 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
343 void *opaque)
345 BusChild *kid;
346 int err;
348 if (pre_busfn) {
349 err = pre_busfn(bus, opaque);
350 if (err) {
351 return err;
355 QTAILQ_FOREACH(kid, &bus->children, sibling) {
356 err = qdev_walk_children(kid->child,
357 pre_devfn, pre_busfn,
358 post_devfn, post_busfn, opaque);
359 if (err < 0) {
360 return err;
364 if (post_busfn) {
365 err = post_busfn(bus, opaque);
366 if (err) {
367 return err;
371 return 0;
374 int qdev_walk_children(DeviceState *dev,
375 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
376 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
377 void *opaque)
379 BusState *bus;
380 int err;
382 if (pre_devfn) {
383 err = pre_devfn(dev, opaque);
384 if (err) {
385 return err;
389 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
390 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
391 post_devfn, post_busfn, opaque);
392 if (err < 0) {
393 return err;
397 if (post_devfn) {
398 err = post_devfn(dev, opaque);
399 if (err) {
400 return err;
404 return 0;
407 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
409 BusChild *kid;
410 DeviceState *ret;
411 BusState *child;
413 QTAILQ_FOREACH(kid, &bus->children, sibling) {
414 DeviceState *dev = kid->child;
416 if (dev->id && strcmp(dev->id, id) == 0) {
417 return dev;
420 QLIST_FOREACH(child, &dev->child_bus, sibling) {
421 ret = qdev_find_recursive(child, id);
422 if (ret) {
423 return ret;
427 return NULL;
430 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
432 const char *typename = object_get_typename(OBJECT(bus));
433 char *buf;
434 int i,len;
436 bus->parent = parent;
438 if (name) {
439 bus->name = g_strdup(name);
440 } else if (bus->parent && bus->parent->id) {
441 /* parent device has id -> use it for bus name */
442 len = strlen(bus->parent->id) + 16;
443 buf = g_malloc(len);
444 snprintf(buf, len, "%s.%d", bus->parent->id, bus->parent->num_child_bus);
445 bus->name = buf;
446 } else {
447 /* no id -> use lowercase bus type for bus name */
448 len = strlen(typename) + 16;
449 buf = g_malloc(len);
450 len = snprintf(buf, len, "%s.%d", typename,
451 bus->parent ? bus->parent->num_child_bus : 0);
452 for (i = 0; i < len; i++)
453 buf[i] = qemu_tolower(buf[i]);
454 bus->name = buf;
457 if (bus->parent) {
458 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
459 bus->parent->num_child_bus++;
460 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
461 object_unref(OBJECT(bus));
462 } else if (bus != sysbus_get_default()) {
463 /* TODO: once all bus devices are qdevified,
464 only reset handler for main_system_bus should be registered here. */
465 qemu_register_reset(qbus_reset_all_fn, bus);
469 static void bus_unparent(Object *obj)
471 BusState *bus = BUS(obj);
472 BusChild *kid;
474 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
475 DeviceState *dev = kid->child;
476 object_unparent(OBJECT(dev));
478 if (bus->parent) {
479 QLIST_REMOVE(bus, sibling);
480 bus->parent->num_child_bus--;
481 bus->parent = NULL;
482 } else {
483 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
484 qemu_unregister_reset(qbus_reset_all_fn, bus);
488 void qbus_create_inplace(void *bus, size_t size, const char *typename,
489 DeviceState *parent, const char *name)
491 object_initialize(bus, size, typename);
492 qbus_realize(bus, parent, name);
495 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
497 BusState *bus;
499 bus = BUS(object_new(typename));
500 qbus_realize(bus, parent, name);
502 return bus;
505 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
507 BusClass *bc = BUS_GET_CLASS(bus);
509 if (bc->get_fw_dev_path) {
510 return bc->get_fw_dev_path(dev);
513 return NULL;
516 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
518 int l = 0;
520 if (dev && dev->parent_bus) {
521 char *d;
522 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
523 d = bus_get_fw_dev_path(dev->parent_bus, dev);
524 if (d) {
525 l += snprintf(p + l, size - l, "%s", d);
526 g_free(d);
527 } else {
528 return l;
531 l += snprintf(p + l , size - l, "/");
533 return l;
536 char* qdev_get_fw_dev_path(DeviceState *dev)
538 char path[128];
539 int l;
541 l = qdev_get_fw_dev_path_helper(dev, path, 128);
543 path[l-1] = '\0';
545 return g_strdup(path);
548 char *qdev_get_dev_path(DeviceState *dev)
550 BusClass *bc;
552 if (!dev || !dev->parent_bus) {
553 return NULL;
556 bc = BUS_GET_CLASS(dev->parent_bus);
557 if (bc->get_dev_path) {
558 return bc->get_dev_path(dev);
561 return NULL;
565 * Legacy property handling
568 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
569 const char *name, Error **errp)
571 DeviceState *dev = DEVICE(obj);
572 Property *prop = opaque;
574 char buffer[1024];
575 char *ptr = buffer;
577 prop->info->print(dev, prop, buffer, sizeof(buffer));
578 visit_type_str(v, &ptr, name, errp);
582 * @qdev_add_legacy_property - adds a legacy property
584 * Do not use this is new code! Properties added through this interface will
585 * be given names and types in the "legacy" namespace.
587 * Legacy properties are string versions of other OOM properties. The format
588 * of the string depends on the property type.
590 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
591 Error **errp)
593 gchar *name, *type;
595 /* Register pointer properties as legacy properties */
596 if (!prop->info->print && prop->info->get) {
597 return;
600 name = g_strdup_printf("legacy-%s", prop->name);
601 type = g_strdup_printf("legacy<%s>",
602 prop->info->legacy_name ?: prop->info->name);
604 object_property_add(OBJECT(dev), name, type,
605 prop->info->print ? qdev_get_legacy_property : prop->info->get,
606 NULL,
607 NULL,
608 prop, errp);
610 g_free(type);
611 g_free(name);
615 * @qdev_property_add_static - add a @Property to a device.
617 * Static properties access data in a struct. The actual type of the
618 * property and the field depends on the property type.
620 void qdev_property_add_static(DeviceState *dev, Property *prop,
621 Error **errp)
623 Error *local_err = NULL;
624 Object *obj = OBJECT(dev);
627 * TODO qdev_prop_ptr does not have getters or setters. It must
628 * go now that it can be replaced with links. The test should be
629 * removed along with it: all static properties are read/write.
631 if (!prop->info->get && !prop->info->set) {
632 return;
635 object_property_add(obj, prop->name, prop->info->name,
636 prop->info->get, prop->info->set,
637 prop->info->release,
638 prop, &local_err);
640 if (local_err) {
641 error_propagate(errp, local_err);
642 return;
644 if (prop->qtype == QTYPE_NONE) {
645 return;
648 if (prop->qtype == QTYPE_QBOOL) {
649 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
650 } else if (prop->info->enum_table) {
651 object_property_set_str(obj, prop->info->enum_table[prop->defval],
652 prop->name, &error_abort);
653 } else if (prop->qtype == QTYPE_QINT) {
654 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
658 static bool device_get_realized(Object *obj, Error **err)
660 DeviceState *dev = DEVICE(obj);
661 return dev->realized;
664 static void device_set_realized(Object *obj, bool value, Error **err)
666 DeviceState *dev = DEVICE(obj);
667 DeviceClass *dc = DEVICE_GET_CLASS(dev);
668 Error *local_err = NULL;
670 if (value && !dev->realized) {
671 if (!obj->parent && local_err == NULL) {
672 static int unattached_count;
673 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
675 object_property_add_child(container_get(qdev_get_machine(),
676 "/unattached"),
677 name, obj, &local_err);
678 g_free(name);
681 if (dc->realize) {
682 dc->realize(dev, &local_err);
685 if (qdev_get_vmsd(dev) && local_err == NULL) {
686 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
687 dev->instance_id_alias,
688 dev->alias_required_for_version);
690 if (dev->hotplugged && local_err == NULL) {
691 device_reset(dev);
693 } else if (!value && dev->realized) {
694 if (qdev_get_vmsd(dev)) {
695 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
697 if (dc->unrealize) {
698 dc->unrealize(dev, &local_err);
702 if (local_err != NULL) {
703 error_propagate(err, local_err);
704 return;
707 dev->realized = value;
710 static void device_initfn(Object *obj)
712 DeviceState *dev = DEVICE(obj);
713 ObjectClass *class;
714 Property *prop;
716 if (qdev_hotplug) {
717 dev->hotplugged = 1;
718 qdev_hot_added = true;
721 dev->instance_id_alias = -1;
722 dev->realized = false;
724 object_property_add_bool(obj, "realized",
725 device_get_realized, device_set_realized, NULL);
727 class = object_get_class(OBJECT(dev));
728 do {
729 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
730 qdev_property_add_legacy(dev, prop, &error_abort);
731 qdev_property_add_static(dev, prop, &error_abort);
733 class = object_class_get_parent(class);
734 } while (class != object_class_by_name(TYPE_DEVICE));
736 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
737 (Object **)&dev->parent_bus, &error_abort);
740 static void device_post_init(Object *obj)
742 qdev_prop_set_globals(DEVICE(obj), &error_abort);
745 /* Unlink device from bus and free the structure. */
746 static void device_finalize(Object *obj)
748 DeviceState *dev = DEVICE(obj);
749 if (dev->opts) {
750 qemu_opts_del(dev->opts);
754 static void device_class_base_init(ObjectClass *class, void *data)
756 DeviceClass *klass = DEVICE_CLASS(class);
758 /* We explicitly look up properties in the superclasses,
759 * so do not propagate them to the subclasses.
761 klass->props = NULL;
764 static void device_unparent(Object *obj)
766 DeviceState *dev = DEVICE(obj);
767 BusState *bus;
768 QObject *event_data;
769 bool have_realized = dev->realized;
771 while (dev->num_child_bus) {
772 bus = QLIST_FIRST(&dev->child_bus);
773 object_unparent(OBJECT(bus));
775 if (dev->realized) {
776 object_property_set_bool(obj, false, "realized", NULL);
778 if (dev->parent_bus) {
779 bus_remove_child(dev->parent_bus, dev);
780 object_unref(OBJECT(dev->parent_bus));
781 dev->parent_bus = NULL;
784 /* Only send event if the device had been completely realized */
785 if (have_realized) {
786 gchar *path = object_get_canonical_path(OBJECT(dev));
788 if (dev->id) {
789 event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
790 dev->id, path);
791 } else {
792 event_data = qobject_from_jsonf("{ 'path': %s }", path);
794 monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
795 qobject_decref(event_data);
796 g_free(path);
800 static void device_class_init(ObjectClass *class, void *data)
802 DeviceClass *dc = DEVICE_CLASS(class);
804 class->unparent = device_unparent;
805 dc->realize = device_realize;
806 dc->unrealize = device_unrealize;
809 void device_reset(DeviceState *dev)
811 DeviceClass *klass = DEVICE_GET_CLASS(dev);
813 if (klass->reset) {
814 klass->reset(dev);
818 Object *qdev_get_machine(void)
820 static Object *dev;
822 if (dev == NULL) {
823 dev = container_get(object_get_root(), "/machine");
826 return dev;
829 static const TypeInfo device_type_info = {
830 .name = TYPE_DEVICE,
831 .parent = TYPE_OBJECT,
832 .instance_size = sizeof(DeviceState),
833 .instance_init = device_initfn,
834 .instance_post_init = device_post_init,
835 .instance_finalize = device_finalize,
836 .class_base_init = device_class_base_init,
837 .class_init = device_class_init,
838 .abstract = true,
839 .class_size = sizeof(DeviceClass),
842 static void qbus_initfn(Object *obj)
844 BusState *bus = BUS(obj);
846 QTAILQ_INIT(&bus->children);
849 static char *default_bus_get_fw_dev_path(DeviceState *dev)
851 return g_strdup(object_get_typename(OBJECT(dev)));
854 static void bus_class_init(ObjectClass *class, void *data)
856 BusClass *bc = BUS_CLASS(class);
858 class->unparent = bus_unparent;
859 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
862 static void qbus_finalize(Object *obj)
864 BusState *bus = BUS(obj);
866 g_free((char *)bus->name);
869 static const TypeInfo bus_info = {
870 .name = TYPE_BUS,
871 .parent = TYPE_OBJECT,
872 .instance_size = sizeof(BusState),
873 .abstract = true,
874 .class_size = sizeof(BusClass),
875 .instance_init = qbus_initfn,
876 .instance_finalize = qbus_finalize,
877 .class_init = bus_class_init,
880 static void qdev_register_types(void)
882 type_register_static(&bus_info);
883 type_register_static(&device_type_info);
886 type_init(qdev_register_types)