qdev: Drop legacy hotplug fields/methods
[qemu/ar7.git] / hw / core / qdev.c
blob3e58dd030953e61518007358c0a0001b53d873b1
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 "hw/fw-path-provider.h"
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "hw/hotplug.h"
36 #include "hw/boards.h"
37 #include "qapi-event.h"
39 int qdev_hotplug = 0;
40 static bool qdev_hot_added = false;
41 static bool qdev_hot_removed = false;
43 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
45 DeviceClass *dc = DEVICE_GET_CLASS(dev);
46 return dc->vmsd;
49 const char *qdev_fw_name(DeviceState *dev)
51 DeviceClass *dc = DEVICE_GET_CLASS(dev);
53 if (dc->fw_name) {
54 return dc->fw_name;
57 return object_get_typename(OBJECT(dev));
60 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
61 Error **errp);
63 static void bus_remove_child(BusState *bus, DeviceState *child)
65 BusChild *kid;
67 QTAILQ_FOREACH(kid, &bus->children, sibling) {
68 if (kid->child == child) {
69 char name[32];
71 snprintf(name, sizeof(name), "child[%d]", kid->index);
72 QTAILQ_REMOVE(&bus->children, kid, sibling);
74 /* This gives back ownership of kid->child back to us. */
75 object_property_del(OBJECT(bus), name, NULL);
76 object_unref(OBJECT(kid->child));
77 g_free(kid);
78 return;
83 static void bus_add_child(BusState *bus, DeviceState *child)
85 char name[32];
86 BusChild *kid = g_malloc0(sizeof(*kid));
88 kid->index = bus->max_index++;
89 kid->child = child;
90 object_ref(OBJECT(kid->child));
92 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
94 /* This transfers ownership of kid->child to the property. */
95 snprintf(name, sizeof(name), "child[%d]", kid->index);
96 object_property_add_link(OBJECT(bus), name,
97 object_get_typename(OBJECT(child)),
98 (Object **)&kid->child,
99 NULL, /* read-only property */
100 0, /* return ownership on prop deletion */
101 NULL);
104 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
106 dev->parent_bus = bus;
107 object_ref(OBJECT(bus));
108 bus_add_child(bus, dev);
111 static void qbus_set_hotplug_handler_internal(BusState *bus, Object *handler,
112 Error **errp)
115 object_property_set_link(OBJECT(bus), OBJECT(handler),
116 QDEV_HOTPLUG_HANDLER_PROPERTY, errp);
119 void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, Error **errp)
121 qbus_set_hotplug_handler_internal(bus, OBJECT(handler), errp);
124 void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp)
126 qbus_set_hotplug_handler_internal(bus, OBJECT(bus), errp);
129 /* Create a new device. This only initializes the device state structure
130 and allows properties to be set. qdev_init should be called to
131 initialize the actual device emulation. */
132 DeviceState *qdev_create(BusState *bus, const char *name)
134 DeviceState *dev;
136 dev = qdev_try_create(bus, name);
137 if (!dev) {
138 if (bus) {
139 error_report("Unknown device '%s' for bus '%s'", name,
140 object_get_typename(OBJECT(bus)));
141 } else {
142 error_report("Unknown device '%s' for default sysbus", name);
144 abort();
147 return dev;
150 DeviceState *qdev_try_create(BusState *bus, const char *type)
152 DeviceState *dev;
154 if (object_class_by_name(type) == NULL) {
155 return NULL;
157 dev = DEVICE(object_new(type));
158 if (!dev) {
159 return NULL;
162 if (!bus) {
163 bus = sysbus_get_default();
166 qdev_set_parent_bus(dev, bus);
167 object_unref(OBJECT(dev));
168 return dev;
171 /* Initialize a device. Device properties should be set before calling
172 this function. IRQs and MMIO regions should be connected/mapped after
173 calling this function.
174 On failure, destroy the device and return negative value.
175 Return 0 on success. */
176 int qdev_init(DeviceState *dev)
178 Error *local_err = NULL;
180 assert(!dev->realized);
182 object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
183 if (local_err != NULL) {
184 qerror_report_err(local_err);
185 error_free(local_err);
186 object_unparent(OBJECT(dev));
187 return -1;
189 return 0;
192 static void device_realize(DeviceState *dev, Error **errp)
194 DeviceClass *dc = DEVICE_GET_CLASS(dev);
196 if (dc->init) {
197 int rc = dc->init(dev);
198 if (rc < 0) {
199 error_setg(errp, "Device initialization failed.");
200 return;
205 static void device_unrealize(DeviceState *dev, Error **errp)
207 DeviceClass *dc = DEVICE_GET_CLASS(dev);
209 if (dc->exit) {
210 int rc = dc->exit(dev);
211 if (rc < 0) {
212 error_setg(errp, "Device exit failed.");
213 return;
218 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
219 int required_for_version)
221 assert(!dev->realized);
222 dev->instance_id_alias = alias_id;
223 dev->alias_required_for_version = required_for_version;
226 void qdev_unplug(DeviceState *dev, Error **errp)
228 DeviceClass *dc = DEVICE_GET_CLASS(dev);
230 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
231 error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
232 return;
235 if (!dc->hotpluggable) {
236 error_set(errp, QERR_DEVICE_NO_HOTPLUG,
237 object_get_typename(OBJECT(dev)));
238 return;
241 qdev_hot_removed = true;
243 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
244 HotplugHandlerClass *hdc;
246 /* If device supports async unplug just request it to be done,
247 * otherwise just remove it synchronously */
248 hdc = HOTPLUG_HANDLER_GET_CLASS(dev->parent_bus->hotplug_handler);
249 if (hdc->unplug_request) {
250 hotplug_handler_unplug_request(dev->parent_bus->hotplug_handler,
251 dev, errp);
252 } else {
253 hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
255 } else {
256 assert(0);
260 static int qdev_reset_one(DeviceState *dev, void *opaque)
262 device_reset(dev);
264 return 0;
267 static int qbus_reset_one(BusState *bus, void *opaque)
269 BusClass *bc = BUS_GET_CLASS(bus);
270 if (bc->reset) {
271 bc->reset(bus);
273 return 0;
276 void qdev_reset_all(DeviceState *dev)
278 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
281 void qbus_reset_all(BusState *bus)
283 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
286 void qbus_reset_all_fn(void *opaque)
288 BusState *bus = opaque;
289 qbus_reset_all(bus);
292 /* can be used as ->unplug() callback for the simple cases */
293 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
294 DeviceState *dev, Error **errp)
296 /* just zap it */
297 object_unparent(OBJECT(dev));
300 /* Like qdev_init(), but terminate program via error_report() instead of
301 returning an error value. This is okay during machine creation.
302 Don't use for hotplug, because there callers need to recover from
303 failure. Exception: if you know the device's init() callback can't
304 fail, then qdev_init_nofail() can't fail either, and is therefore
305 usable even then. But relying on the device implementation that
306 way is somewhat unclean, and best avoided. */
307 void qdev_init_nofail(DeviceState *dev)
309 const char *typename = object_get_typename(OBJECT(dev));
311 if (qdev_init(dev) < 0) {
312 error_report("Initialization of device %s failed", typename);
313 exit(1);
317 void qdev_machine_creation_done(void)
320 * ok, initial machine setup is done, starting from now we can
321 * only create hotpluggable devices
323 qdev_hotplug = 1;
326 bool qdev_machine_modified(void)
328 return qdev_hot_added || qdev_hot_removed;
331 BusState *qdev_get_parent_bus(DeviceState *dev)
333 return dev->parent_bus;
336 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
337 const char *name)
339 NamedGPIOList *ngl;
341 QLIST_FOREACH(ngl, &dev->gpios, node) {
342 /* NULL is a valid and matchable name, otherwise do a normal
343 * strcmp match.
345 if ((!ngl->name && !name) ||
346 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
347 return ngl;
351 ngl = g_malloc0(sizeof(*ngl));
352 ngl->name = g_strdup(name);
353 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
354 return ngl;
357 void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
358 const char *name, int n)
360 int i;
361 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
362 char *propname = g_strdup_printf("%s[*]", name ? name : "unnamed-gpio-in");
364 assert(gpio_list->num_out == 0 || !name);
365 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
366 dev, n);
368 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
369 object_property_add_child(OBJECT(dev), propname,
370 OBJECT(gpio_list->in[i]), &error_abort);
372 g_free(propname);
374 gpio_list->num_in += n;
377 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
379 qdev_init_gpio_in_named(dev, handler, NULL, n);
382 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
383 const char *name, int n)
385 int i;
386 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
387 char *propname = g_strdup_printf("%s[*]", name ? name : "unnamed-gpio-out");
389 assert(gpio_list->num_in == 0 || !name);
390 assert(gpio_list->num_out == 0);
391 gpio_list->num_out = n;
392 gpio_list->out = pins;
394 for (i = 0; i < n; ++i) {
395 memset(&pins[i], 0, sizeof(*pins));
396 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
397 (Object **)&pins[i],
398 object_property_allow_set_link,
399 OBJ_PROP_LINK_UNREF_ON_RELEASE,
400 &error_abort);
402 g_free(propname);
405 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
407 qdev_init_gpio_out_named(dev, pins, NULL, n);
410 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
412 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
414 assert(n >= 0 && n < gpio_list->num_in);
415 return gpio_list->in[n];
418 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
420 return qdev_get_gpio_in_named(dev, NULL, n);
423 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
424 qemu_irq pin)
426 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
428 assert(n >= 0 && n < gpio_list->num_out);
429 gpio_list->out[n] = pin;
432 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
434 qdev_connect_gpio_out_named(dev, NULL, n, pin);
437 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
439 BusState *bus;
441 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
442 if (strcmp(name, bus->name) == 0) {
443 return bus;
446 return NULL;
449 int qbus_walk_children(BusState *bus,
450 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
451 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
452 void *opaque)
454 BusChild *kid;
455 int err;
457 if (pre_busfn) {
458 err = pre_busfn(bus, opaque);
459 if (err) {
460 return err;
464 QTAILQ_FOREACH(kid, &bus->children, sibling) {
465 err = qdev_walk_children(kid->child,
466 pre_devfn, pre_busfn,
467 post_devfn, post_busfn, opaque);
468 if (err < 0) {
469 return err;
473 if (post_busfn) {
474 err = post_busfn(bus, opaque);
475 if (err) {
476 return err;
480 return 0;
483 int qdev_walk_children(DeviceState *dev,
484 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
485 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
486 void *opaque)
488 BusState *bus;
489 int err;
491 if (pre_devfn) {
492 err = pre_devfn(dev, opaque);
493 if (err) {
494 return err;
498 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
499 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
500 post_devfn, post_busfn, opaque);
501 if (err < 0) {
502 return err;
506 if (post_devfn) {
507 err = post_devfn(dev, opaque);
508 if (err) {
509 return err;
513 return 0;
516 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
518 BusChild *kid;
519 DeviceState *ret;
520 BusState *child;
522 QTAILQ_FOREACH(kid, &bus->children, sibling) {
523 DeviceState *dev = kid->child;
525 if (dev->id && strcmp(dev->id, id) == 0) {
526 return dev;
529 QLIST_FOREACH(child, &dev->child_bus, sibling) {
530 ret = qdev_find_recursive(child, id);
531 if (ret) {
532 return ret;
536 return NULL;
539 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
541 const char *typename = object_get_typename(OBJECT(bus));
542 BusClass *bc;
543 char *buf;
544 int i, len, bus_id;
546 bus->parent = parent;
548 if (name) {
549 bus->name = g_strdup(name);
550 } else if (bus->parent && bus->parent->id) {
551 /* parent device has id -> use it plus parent-bus-id for bus name */
552 bus_id = bus->parent->num_child_bus;
554 len = strlen(bus->parent->id) + 16;
555 buf = g_malloc(len);
556 snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
557 bus->name = buf;
558 } else {
559 /* no id -> use lowercase bus type plus global bus-id for bus name */
560 bc = BUS_GET_CLASS(bus);
561 bus_id = bc->automatic_ids++;
563 len = strlen(typename) + 16;
564 buf = g_malloc(len);
565 len = snprintf(buf, len, "%s.%d", typename, bus_id);
566 for (i = 0; i < len; i++) {
567 buf[i] = qemu_tolower(buf[i]);
569 bus->name = buf;
572 if (bus->parent) {
573 QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
574 bus->parent->num_child_bus++;
575 object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
576 object_unref(OBJECT(bus));
577 } else if (bus != sysbus_get_default()) {
578 /* TODO: once all bus devices are qdevified,
579 only reset handler for main_system_bus should be registered here. */
580 qemu_register_reset(qbus_reset_all_fn, bus);
584 static void bus_unparent(Object *obj)
586 BusState *bus = BUS(obj);
587 BusChild *kid;
589 while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
590 DeviceState *dev = kid->child;
591 object_unparent(OBJECT(dev));
593 if (bus->parent) {
594 QLIST_REMOVE(bus, sibling);
595 bus->parent->num_child_bus--;
596 bus->parent = NULL;
597 } else {
598 assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
599 qemu_unregister_reset(qbus_reset_all_fn, bus);
603 static bool bus_get_realized(Object *obj, Error **errp)
605 BusState *bus = BUS(obj);
607 return bus->realized;
610 static void bus_set_realized(Object *obj, bool value, Error **errp)
612 BusState *bus = BUS(obj);
613 BusClass *bc = BUS_GET_CLASS(bus);
614 BusChild *kid;
615 Error *local_err = NULL;
617 if (value && !bus->realized) {
618 if (bc->realize) {
619 bc->realize(bus, &local_err);
622 /* TODO: recursive realization */
623 } else if (!value && bus->realized) {
624 QTAILQ_FOREACH(kid, &bus->children, sibling) {
625 DeviceState *dev = kid->child;
626 object_property_set_bool(OBJECT(dev), false, "realized",
627 &local_err);
628 if (local_err != NULL) {
629 break;
632 if (bc->unrealize && local_err == NULL) {
633 bc->unrealize(bus, &local_err);
637 if (local_err != NULL) {
638 error_propagate(errp, local_err);
639 return;
642 bus->realized = value;
645 void qbus_create_inplace(void *bus, size_t size, const char *typename,
646 DeviceState *parent, const char *name)
648 object_initialize(bus, size, typename);
649 qbus_realize(bus, parent, name);
652 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
654 BusState *bus;
656 bus = BUS(object_new(typename));
657 qbus_realize(bus, parent, name);
659 return bus;
662 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
664 BusClass *bc = BUS_GET_CLASS(bus);
666 if (bc->get_fw_dev_path) {
667 return bc->get_fw_dev_path(dev);
670 return NULL;
673 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
675 Object *obj = OBJECT(dev);
676 char *d = NULL;
678 while (!d && obj->parent) {
679 obj = obj->parent;
680 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
682 return d;
685 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
687 int l = 0;
689 if (dev && dev->parent_bus) {
690 char *d;
691 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
692 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
693 if (!d) {
694 d = bus_get_fw_dev_path(dev->parent_bus, dev);
696 if (d) {
697 l += snprintf(p + l, size - l, "%s", d);
698 g_free(d);
699 } else {
700 return l;
703 l += snprintf(p + l , size - l, "/");
705 return l;
708 char* qdev_get_fw_dev_path(DeviceState *dev)
710 char path[128];
711 int l;
713 l = qdev_get_fw_dev_path_helper(dev, path, 128);
715 path[l-1] = '\0';
717 return g_strdup(path);
720 char *qdev_get_dev_path(DeviceState *dev)
722 BusClass *bc;
724 if (!dev || !dev->parent_bus) {
725 return NULL;
728 bc = BUS_GET_CLASS(dev->parent_bus);
729 if (bc->get_dev_path) {
730 return bc->get_dev_path(dev);
733 return NULL;
737 * Legacy property handling
740 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
741 const char *name, Error **errp)
743 DeviceState *dev = DEVICE(obj);
744 Property *prop = opaque;
746 char buffer[1024];
747 char *ptr = buffer;
749 prop->info->print(dev, prop, buffer, sizeof(buffer));
750 visit_type_str(v, &ptr, name, errp);
754 * @qdev_add_legacy_property - adds a legacy property
756 * Do not use this is new code! Properties added through this interface will
757 * be given names and types in the "legacy" namespace.
759 * Legacy properties are string versions of other OOM properties. The format
760 * of the string depends on the property type.
762 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
763 Error **errp)
765 gchar *name;
767 /* Register pointer properties as legacy properties */
768 if (!prop->info->print && prop->info->get) {
769 return;
772 name = g_strdup_printf("legacy-%s", prop->name);
773 object_property_add(OBJECT(dev), name, "str",
774 prop->info->print ? qdev_get_legacy_property : prop->info->get,
775 NULL,
776 NULL,
777 prop, errp);
779 g_free(name);
783 * @qdev_property_add_static - add a @Property to a device.
785 * Static properties access data in a struct. The actual type of the
786 * property and the field depends on the property type.
788 void qdev_property_add_static(DeviceState *dev, Property *prop,
789 Error **errp)
791 Error *local_err = NULL;
792 Object *obj = OBJECT(dev);
795 * TODO qdev_prop_ptr does not have getters or setters. It must
796 * go now that it can be replaced with links. The test should be
797 * removed along with it: all static properties are read/write.
799 if (!prop->info->get && !prop->info->set) {
800 return;
803 object_property_add(obj, prop->name, prop->info->name,
804 prop->info->get, prop->info->set,
805 prop->info->release,
806 prop, &local_err);
808 if (local_err) {
809 error_propagate(errp, local_err);
810 return;
812 if (prop->qtype == QTYPE_NONE) {
813 return;
816 if (prop->qtype == QTYPE_QBOOL) {
817 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
818 } else if (prop->info->enum_table) {
819 object_property_set_str(obj, prop->info->enum_table[prop->defval],
820 prop->name, &error_abort);
821 } else if (prop->qtype == QTYPE_QINT) {
822 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
826 /* @qdev_alias_all_properties - Add alias properties to the source object for
827 * all qdev properties on the target DeviceState.
829 void qdev_alias_all_properties(DeviceState *target, Object *source)
831 ObjectClass *class;
832 Property *prop;
834 class = object_get_class(OBJECT(target));
835 do {
836 DeviceClass *dc = DEVICE_CLASS(class);
838 for (prop = dc->props; prop && prop->name; prop++) {
839 object_property_add_alias(source, prop->name,
840 OBJECT(target), prop->name,
841 &error_abort);
843 class = object_class_get_parent(class);
844 } while (class != object_class_by_name(TYPE_DEVICE));
847 static bool device_get_realized(Object *obj, Error **errp)
849 DeviceState *dev = DEVICE(obj);
850 return dev->realized;
853 static void device_set_realized(Object *obj, bool value, Error **errp)
855 DeviceState *dev = DEVICE(obj);
856 DeviceClass *dc = DEVICE_GET_CLASS(dev);
857 BusState *bus;
858 Error *local_err = NULL;
860 if (dev->hotplugged && !dc->hotpluggable) {
861 error_set(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
862 return;
865 if (value && !dev->realized) {
866 if (!obj->parent) {
867 static int unattached_count;
868 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
870 object_property_add_child(container_get(qdev_get_machine(),
871 "/unattached"),
872 name, obj, &error_abort);
873 g_free(name);
876 if (dc->realize) {
877 dc->realize(dev, &local_err);
880 if (local_err != NULL) {
881 goto fail;
884 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
885 hotplug_handler_plug(dev->parent_bus->hotplug_handler,
886 dev, &local_err);
887 } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
888 HotplugHandler *hotplug_ctrl;
889 MachineState *machine = MACHINE(qdev_get_machine());
890 MachineClass *mc = MACHINE_GET_CLASS(machine);
892 if (mc->get_hotplug_handler) {
893 hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
894 if (hotplug_ctrl) {
895 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
900 if (local_err != NULL) {
901 goto post_realize_fail;
904 if (qdev_get_vmsd(dev)) {
905 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
906 dev->instance_id_alias,
907 dev->alias_required_for_version);
910 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
911 object_property_set_bool(OBJECT(bus), true, "realized",
912 &local_err);
913 if (local_err != NULL) {
914 goto child_realize_fail;
917 if (dev->hotplugged) {
918 device_reset(dev);
920 dev->pending_deleted_event = false;
921 } else if (!value && dev->realized) {
922 Error **local_errp = NULL;
923 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
924 local_errp = local_err ? NULL : &local_err;
925 object_property_set_bool(OBJECT(bus), false, "realized",
926 local_errp);
928 if (qdev_get_vmsd(dev)) {
929 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
931 if (dc->unrealize) {
932 local_errp = local_err ? NULL : &local_err;
933 dc->unrealize(dev, local_errp);
935 dev->pending_deleted_event = true;
938 if (local_err != NULL) {
939 goto fail;
942 dev->realized = value;
943 return;
945 child_realize_fail:
946 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
947 object_property_set_bool(OBJECT(bus), false, "realized",
948 NULL);
951 if (qdev_get_vmsd(dev)) {
952 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
955 post_realize_fail:
956 if (dc->unrealize) {
957 dc->unrealize(dev, NULL);
960 fail:
961 error_propagate(errp, local_err);
962 return;
965 static bool device_get_hotpluggable(Object *obj, Error **errp)
967 DeviceClass *dc = DEVICE_GET_CLASS(obj);
968 DeviceState *dev = DEVICE(obj);
970 return dc->hotpluggable && (dev->parent_bus == NULL ||
971 qbus_is_hotpluggable(dev->parent_bus));
974 static bool device_get_hotplugged(Object *obj, Error **err)
976 DeviceState *dev = DEVICE(obj);
978 return dev->hotplugged;
981 static void device_set_hotplugged(Object *obj, bool value, Error **err)
983 DeviceState *dev = DEVICE(obj);
985 dev->hotplugged = value;
988 static void device_initfn(Object *obj)
990 DeviceState *dev = DEVICE(obj);
991 ObjectClass *class;
992 Property *prop;
994 if (qdev_hotplug) {
995 dev->hotplugged = 1;
996 qdev_hot_added = true;
999 dev->instance_id_alias = -1;
1000 dev->realized = false;
1002 object_property_add_bool(obj, "realized",
1003 device_get_realized, device_set_realized, NULL);
1004 object_property_add_bool(obj, "hotpluggable",
1005 device_get_hotpluggable, NULL, NULL);
1006 object_property_add_bool(obj, "hotplugged",
1007 device_get_hotplugged, device_set_hotplugged,
1008 &error_abort);
1010 class = object_get_class(OBJECT(dev));
1011 do {
1012 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
1013 qdev_property_add_legacy(dev, prop, &error_abort);
1014 qdev_property_add_static(dev, prop, &error_abort);
1016 class = object_class_get_parent(class);
1017 } while (class != object_class_by_name(TYPE_DEVICE));
1019 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
1020 (Object **)&dev->parent_bus, NULL, 0,
1021 &error_abort);
1022 QLIST_INIT(&dev->gpios);
1025 static void device_post_init(Object *obj)
1027 Error *err = NULL;
1028 qdev_prop_set_globals(DEVICE(obj), &err);
1029 if (err) {
1030 qerror_report_err(err);
1031 error_free(err);
1032 exit(EXIT_FAILURE);
1036 /* Unlink device from bus and free the structure. */
1037 static void device_finalize(Object *obj)
1039 NamedGPIOList *ngl, *next;
1041 DeviceState *dev = DEVICE(obj);
1042 if (dev->opts) {
1043 qemu_opts_del(dev->opts);
1046 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1047 QLIST_REMOVE(ngl, node);
1048 qemu_free_irqs(ngl->in, ngl->num_in);
1049 g_free(ngl->name);
1050 g_free(ngl);
1051 /* ngl->out irqs are owned by the other end and should not be freed
1052 * here
1057 static void device_class_base_init(ObjectClass *class, void *data)
1059 DeviceClass *klass = DEVICE_CLASS(class);
1061 /* We explicitly look up properties in the superclasses,
1062 * so do not propagate them to the subclasses.
1064 klass->props = NULL;
1067 static void device_unparent(Object *obj)
1069 DeviceState *dev = DEVICE(obj);
1070 BusState *bus;
1072 if (dev->realized) {
1073 object_property_set_bool(obj, false, "realized", NULL);
1075 while (dev->num_child_bus) {
1076 bus = QLIST_FIRST(&dev->child_bus);
1077 object_unparent(OBJECT(bus));
1079 if (dev->parent_bus) {
1080 bus_remove_child(dev->parent_bus, dev);
1081 object_unref(OBJECT(dev->parent_bus));
1082 dev->parent_bus = NULL;
1085 /* Only send event if the device had been completely realized */
1086 if (dev->pending_deleted_event) {
1087 gchar *path = object_get_canonical_path(OBJECT(dev));
1089 qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
1090 g_free(path);
1094 static void device_class_init(ObjectClass *class, void *data)
1096 DeviceClass *dc = DEVICE_CLASS(class);
1098 class->unparent = device_unparent;
1099 dc->realize = device_realize;
1100 dc->unrealize = device_unrealize;
1102 /* by default all devices were considered as hotpluggable,
1103 * so with intent to check it in generic qdev_unplug() /
1104 * device_set_realized() functions make every device
1105 * hotpluggable. Devices that shouldn't be hotpluggable,
1106 * should override it in their class_init()
1108 dc->hotpluggable = true;
1111 void device_reset(DeviceState *dev)
1113 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1115 if (klass->reset) {
1116 klass->reset(dev);
1120 Object *qdev_get_machine(void)
1122 static Object *dev;
1124 if (dev == NULL) {
1125 dev = container_get(object_get_root(), "/machine");
1128 return dev;
1131 static const TypeInfo device_type_info = {
1132 .name = TYPE_DEVICE,
1133 .parent = TYPE_OBJECT,
1134 .instance_size = sizeof(DeviceState),
1135 .instance_init = device_initfn,
1136 .instance_post_init = device_post_init,
1137 .instance_finalize = device_finalize,
1138 .class_base_init = device_class_base_init,
1139 .class_init = device_class_init,
1140 .abstract = true,
1141 .class_size = sizeof(DeviceClass),
1144 static void qbus_initfn(Object *obj)
1146 BusState *bus = BUS(obj);
1148 QTAILQ_INIT(&bus->children);
1149 object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
1150 TYPE_HOTPLUG_HANDLER,
1151 (Object **)&bus->hotplug_handler,
1152 object_property_allow_set_link,
1153 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1154 NULL);
1155 object_property_add_bool(obj, "realized",
1156 bus_get_realized, bus_set_realized, NULL);
1159 static char *default_bus_get_fw_dev_path(DeviceState *dev)
1161 return g_strdup(object_get_typename(OBJECT(dev)));
1164 static void bus_class_init(ObjectClass *class, void *data)
1166 BusClass *bc = BUS_CLASS(class);
1168 class->unparent = bus_unparent;
1169 bc->get_fw_dev_path = default_bus_get_fw_dev_path;
1172 static void qbus_finalize(Object *obj)
1174 BusState *bus = BUS(obj);
1176 g_free((char *)bus->name);
1179 static const TypeInfo bus_info = {
1180 .name = TYPE_BUS,
1181 .parent = TYPE_OBJECT,
1182 .instance_size = sizeof(BusState),
1183 .abstract = true,
1184 .class_size = sizeof(BusClass),
1185 .instance_init = qbus_initfn,
1186 .instance_finalize = qbus_finalize,
1187 .class_init = bus_class_init,
1190 static void qdev_register_types(void)
1192 type_register_static(&bus_info);
1193 type_register_static(&device_type_info);
1196 type_init(qdev_register_types)