exec: Fix qemu_ram_block_from_host for Xen
[qemu.git] / hw / core / qdev.c
blob0a05a5295c77a347c5aaf8f87d0827bcd27f2f84
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 "qemu/osdep.h"
29 #include "hw/qdev.h"
30 #include "hw/fw-path-provider.h"
31 #include "sysemu/sysemu.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/visitor.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu/error-report.h"
36 #include "hw/hotplug.h"
37 #include "hw/boards.h"
38 #include "qapi-event.h"
40 int qdev_hotplug = 0;
41 static bool qdev_hot_added = false;
42 static bool qdev_hot_removed = false;
44 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
46 DeviceClass *dc = DEVICE_GET_CLASS(dev);
47 return dc->vmsd;
50 const char *qdev_fw_name(DeviceState *dev)
52 DeviceClass *dc = DEVICE_GET_CLASS(dev);
54 if (dc->fw_name) {
55 return dc->fw_name;
58 return object_get_typename(OBJECT(dev));
61 static void bus_remove_child(BusState *bus, DeviceState *child)
63 BusChild *kid;
65 QTAILQ_FOREACH(kid, &bus->children, sibling) {
66 if (kid->child == child) {
67 char name[32];
69 snprintf(name, sizeof(name), "child[%d]", kid->index);
70 QTAILQ_REMOVE(&bus->children, kid, sibling);
72 /* This gives back ownership of kid->child back to us. */
73 object_property_del(OBJECT(bus), name, NULL);
74 object_unref(OBJECT(kid->child));
75 g_free(kid);
76 return;
81 static void bus_add_child(BusState *bus, DeviceState *child)
83 char name[32];
84 BusChild *kid = g_malloc0(sizeof(*kid));
86 kid->index = bus->max_index++;
87 kid->child = child;
88 object_ref(OBJECT(kid->child));
90 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
92 /* This transfers ownership of kid->child to the property. */
93 snprintf(name, sizeof(name), "child[%d]", kid->index);
94 object_property_add_link(OBJECT(bus), name,
95 object_get_typename(OBJECT(child)),
96 (Object **)&kid->child,
97 NULL, /* read-only property */
98 0, /* return ownership on prop deletion */
99 NULL);
102 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
104 dev->parent_bus = bus;
105 object_ref(OBJECT(bus));
106 bus_add_child(bus, dev);
109 /* Create a new device. This only initializes the device state
110 structure and allows properties to be set. The device still needs
111 to be realized. See qdev-core.h. */
112 DeviceState *qdev_create(BusState *bus, const char *name)
114 DeviceState *dev;
116 dev = qdev_try_create(bus, name);
117 if (!dev) {
118 if (bus) {
119 error_report("Unknown device '%s' for bus '%s'", name,
120 object_get_typename(OBJECT(bus)));
121 } else {
122 error_report("Unknown device '%s' for default sysbus", name);
124 abort();
127 return dev;
130 DeviceState *qdev_try_create(BusState *bus, const char *type)
132 DeviceState *dev;
134 if (object_class_by_name(type) == NULL) {
135 return NULL;
137 dev = DEVICE(object_new(type));
138 if (!dev) {
139 return NULL;
142 if (!bus) {
143 bus = sysbus_get_default();
146 qdev_set_parent_bus(dev, bus);
147 object_unref(OBJECT(dev));
148 return dev;
151 static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
152 = QTAILQ_HEAD_INITIALIZER(device_listeners);
154 enum ListenerDirection { Forward, Reverse };
156 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
157 do { \
158 DeviceListener *_listener; \
160 switch (_direction) { \
161 case Forward: \
162 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
163 if (_listener->_callback) { \
164 _listener->_callback(_listener, ##_args); \
167 break; \
168 case Reverse: \
169 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
170 device_listeners, link) { \
171 if (_listener->_callback) { \
172 _listener->_callback(_listener, ##_args); \
175 break; \
176 default: \
177 abort(); \
179 } while (0)
181 static int device_listener_add(DeviceState *dev, void *opaque)
183 DEVICE_LISTENER_CALL(realize, Forward, dev);
185 return 0;
188 void device_listener_register(DeviceListener *listener)
190 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
192 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
193 NULL, NULL);
196 void device_listener_unregister(DeviceListener *listener)
198 QTAILQ_REMOVE(&device_listeners, listener, link);
201 static void device_realize(DeviceState *dev, Error **errp)
203 DeviceClass *dc = DEVICE_GET_CLASS(dev);
205 if (dc->init) {
206 int rc = dc->init(dev);
207 if (rc < 0) {
208 error_setg(errp, "Device initialization failed.");
209 return;
214 static void device_unrealize(DeviceState *dev, Error **errp)
216 DeviceClass *dc = DEVICE_GET_CLASS(dev);
218 if (dc->exit) {
219 int rc = dc->exit(dev);
220 if (rc < 0) {
221 error_setg(errp, "Device exit failed.");
222 return;
227 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
228 int required_for_version)
230 assert(!dev->realized);
231 dev->instance_id_alias = alias_id;
232 dev->alias_required_for_version = required_for_version;
235 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
237 HotplugHandler *hotplug_ctrl = NULL;
239 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
240 hotplug_ctrl = dev->parent_bus->hotplug_handler;
241 } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
242 MachineState *machine = MACHINE(qdev_get_machine());
243 MachineClass *mc = MACHINE_GET_CLASS(machine);
245 if (mc->get_hotplug_handler) {
246 hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
249 return hotplug_ctrl;
252 void qdev_unplug(DeviceState *dev, Error **errp)
254 DeviceClass *dc = DEVICE_GET_CLASS(dev);
255 HotplugHandler *hotplug_ctrl;
256 HotplugHandlerClass *hdc;
258 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
259 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
260 return;
263 if (!dc->hotpluggable) {
264 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
265 object_get_typename(OBJECT(dev)));
266 return;
269 qdev_hot_removed = true;
271 hotplug_ctrl = qdev_get_hotplug_handler(dev);
272 /* hotpluggable device MUST have HotplugHandler, if it doesn't
273 * then something is very wrong with it */
274 g_assert(hotplug_ctrl);
276 /* If device supports async unplug just request it to be done,
277 * otherwise just remove it synchronously */
278 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
279 if (hdc->unplug_request) {
280 hotplug_handler_unplug_request(hotplug_ctrl, dev, errp);
281 } else {
282 hotplug_handler_unplug(hotplug_ctrl, dev, errp);
286 static int qdev_reset_one(DeviceState *dev, void *opaque)
288 device_reset(dev);
290 return 0;
293 static int qbus_reset_one(BusState *bus, void *opaque)
295 BusClass *bc = BUS_GET_CLASS(bus);
296 if (bc->reset) {
297 bc->reset(bus);
299 return 0;
302 void qdev_reset_all(DeviceState *dev)
304 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
307 void qdev_reset_all_fn(void *opaque)
309 qdev_reset_all(DEVICE(opaque));
312 void qbus_reset_all(BusState *bus)
314 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
317 void qbus_reset_all_fn(void *opaque)
319 BusState *bus = opaque;
320 qbus_reset_all(bus);
323 /* can be used as ->unplug() callback for the simple cases */
324 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
325 DeviceState *dev, Error **errp)
327 /* just zap it */
328 object_unparent(OBJECT(dev));
332 * Realize @dev.
333 * Device properties should be set before calling this function. IRQs
334 * and MMIO regions should be connected/mapped after calling this
335 * function.
336 * On failure, report an error with error_report() and terminate the
337 * program. This is okay during machine creation. Don't use for
338 * hotplug, because there callers need to recover from failure.
339 * Exception: if you know the device's init() callback can't fail,
340 * then qdev_init_nofail() can't fail either, and is therefore usable
341 * even then. But relying on the device implementation that way is
342 * somewhat unclean, and best avoided.
344 void qdev_init_nofail(DeviceState *dev)
346 Error *err = NULL;
348 assert(!dev->realized);
350 object_property_set_bool(OBJECT(dev), true, "realized", &err);
351 if (err) {
352 error_reportf_err(err, "Initialization of device %s failed: ",
353 object_get_typename(OBJECT(dev)));
354 exit(1);
358 void qdev_machine_creation_done(void)
361 * ok, initial machine setup is done, starting from now we can
362 * only create hotpluggable devices
364 qdev_hotplug = 1;
367 bool qdev_machine_modified(void)
369 return qdev_hot_added || qdev_hot_removed;
372 BusState *qdev_get_parent_bus(DeviceState *dev)
374 return dev->parent_bus;
377 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
378 const char *name)
380 NamedGPIOList *ngl;
382 QLIST_FOREACH(ngl, &dev->gpios, node) {
383 /* NULL is a valid and matchable name, otherwise do a normal
384 * strcmp match.
386 if ((!ngl->name && !name) ||
387 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
388 return ngl;
392 ngl = g_malloc0(sizeof(*ngl));
393 ngl->name = g_strdup(name);
394 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
395 return ngl;
398 void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
399 const char *name, int n)
401 int i;
402 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
404 assert(gpio_list->num_out == 0 || !name);
405 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
406 dev, n);
408 if (!name) {
409 name = "unnamed-gpio-in";
411 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
412 gchar *propname = g_strdup_printf("%s[%u]", name, i);
414 object_property_add_child(OBJECT(dev), propname,
415 OBJECT(gpio_list->in[i]), &error_abort);
416 g_free(propname);
419 gpio_list->num_in += n;
422 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
424 qdev_init_gpio_in_named(dev, handler, NULL, n);
427 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
428 const char *name, int n)
430 int i;
431 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
433 assert(gpio_list->num_in == 0 || !name);
435 if (!name) {
436 name = "unnamed-gpio-out";
438 memset(pins, 0, sizeof(*pins) * n);
439 for (i = 0; i < n; ++i) {
440 gchar *propname = g_strdup_printf("%s[%u]", name,
441 gpio_list->num_out + i);
443 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
444 (Object **)&pins[i],
445 object_property_allow_set_link,
446 OBJ_PROP_LINK_UNREF_ON_RELEASE,
447 &error_abort);
448 g_free(propname);
450 gpio_list->num_out += n;
453 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
455 qdev_init_gpio_out_named(dev, pins, NULL, n);
458 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
460 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
462 assert(n >= 0 && n < gpio_list->num_in);
463 return gpio_list->in[n];
466 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
468 return qdev_get_gpio_in_named(dev, NULL, n);
471 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
472 qemu_irq pin)
474 char *propname = g_strdup_printf("%s[%d]",
475 name ? name : "unnamed-gpio-out", n);
476 if (pin) {
477 /* We need a name for object_property_set_link to work. If the
478 * object has a parent, object_property_add_child will come back
479 * with an error without doing anything. If it has none, it will
480 * never fail. So we can just call it with a NULL Error pointer.
482 object_property_add_child(container_get(qdev_get_machine(),
483 "/unattached"),
484 "non-qdev-gpio[*]", OBJECT(pin), NULL);
486 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
487 g_free(propname);
490 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
492 char *propname = g_strdup_printf("%s[%d]",
493 name ? name : "unnamed-gpio-out", n);
495 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
496 NULL);
498 return ret;
501 /* disconnect a GPIO output, returning the disconnected input (if any) */
503 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
504 const char *name, int n)
506 char *propname = g_strdup_printf("%s[%d]",
507 name ? name : "unnamed-gpio-out", n);
509 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
510 NULL);
511 if (ret) {
512 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
514 g_free(propname);
515 return ret;
518 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
519 const char *name, int n)
521 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
522 qdev_connect_gpio_out_named(dev, name, n, icpt);
523 return disconnected;
526 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
528 qdev_connect_gpio_out_named(dev, NULL, n, pin);
531 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
532 const char *name)
534 int i;
535 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
537 for (i = 0; i < ngl->num_in; i++) {
538 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
539 char *propname = g_strdup_printf("%s[%d]", nm, i);
541 object_property_add_alias(OBJECT(container), propname,
542 OBJECT(dev), propname,
543 &error_abort);
544 g_free(propname);
546 for (i = 0; i < ngl->num_out; i++) {
547 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
548 char *propname = g_strdup_printf("%s[%d]", nm, i);
550 object_property_add_alias(OBJECT(container), propname,
551 OBJECT(dev), propname,
552 &error_abort);
553 g_free(propname);
555 QLIST_REMOVE(ngl, node);
556 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
559 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
561 BusState *bus;
562 Object *child = object_resolve_path_component(OBJECT(dev), name);
564 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
565 if (bus) {
566 return bus;
569 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
570 if (strcmp(name, bus->name) == 0) {
571 return bus;
574 return NULL;
577 int qdev_walk_children(DeviceState *dev,
578 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
579 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
580 void *opaque)
582 BusState *bus;
583 int err;
585 if (pre_devfn) {
586 err = pre_devfn(dev, opaque);
587 if (err) {
588 return err;
592 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
593 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
594 post_devfn, post_busfn, opaque);
595 if (err < 0) {
596 return err;
600 if (post_devfn) {
601 err = post_devfn(dev, opaque);
602 if (err) {
603 return err;
607 return 0;
610 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
612 BusChild *kid;
613 DeviceState *ret;
614 BusState *child;
616 QTAILQ_FOREACH(kid, &bus->children, sibling) {
617 DeviceState *dev = kid->child;
619 if (dev->id && strcmp(dev->id, id) == 0) {
620 return dev;
623 QLIST_FOREACH(child, &dev->child_bus, sibling) {
624 ret = qdev_find_recursive(child, id);
625 if (ret) {
626 return ret;
630 return NULL;
633 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
635 BusClass *bc = BUS_GET_CLASS(bus);
637 if (bc->get_fw_dev_path) {
638 return bc->get_fw_dev_path(dev);
641 return NULL;
644 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
646 Object *obj = OBJECT(dev);
647 char *d = NULL;
649 while (!d && obj->parent) {
650 obj = obj->parent;
651 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
653 return d;
656 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
658 Object *obj = OBJECT(dev);
660 return fw_path_provider_try_get_dev_path(obj, bus, dev);
663 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
665 int l = 0;
667 if (dev && dev->parent_bus) {
668 char *d;
669 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
670 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
671 if (!d) {
672 d = bus_get_fw_dev_path(dev->parent_bus, dev);
674 if (d) {
675 l += snprintf(p + l, size - l, "%s", d);
676 g_free(d);
677 } else {
678 return l;
681 l += snprintf(p + l , size - l, "/");
683 return l;
686 char* qdev_get_fw_dev_path(DeviceState *dev)
688 char path[128];
689 int l;
691 l = qdev_get_fw_dev_path_helper(dev, path, 128);
693 path[l-1] = '\0';
695 return g_strdup(path);
698 char *qdev_get_dev_path(DeviceState *dev)
700 BusClass *bc;
702 if (!dev || !dev->parent_bus) {
703 return NULL;
706 bc = BUS_GET_CLASS(dev->parent_bus);
707 if (bc->get_dev_path) {
708 return bc->get_dev_path(dev);
711 return NULL;
715 * Legacy property handling
718 static void qdev_get_legacy_property(Object *obj, Visitor *v,
719 const char *name, void *opaque,
720 Error **errp)
722 DeviceState *dev = DEVICE(obj);
723 Property *prop = opaque;
725 char buffer[1024];
726 char *ptr = buffer;
728 prop->info->print(dev, prop, buffer, sizeof(buffer));
729 visit_type_str(v, name, &ptr, errp);
733 * qdev_property_add_legacy:
734 * @dev: Device to add the property to.
735 * @prop: The qdev property definition.
736 * @errp: location to store error information.
738 * Add a legacy QOM property to @dev for qdev property @prop.
739 * On error, store error in @errp.
741 * Legacy properties are string versions of QOM properties. The format of
742 * the string depends on the property type. Legacy properties are only
743 * needed for "info qtree".
745 * Do not use this is new code! QOM Properties added through this interface
746 * will be given names in the "legacy" namespace.
748 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
749 Error **errp)
751 gchar *name;
753 /* Register pointer properties as legacy properties */
754 if (!prop->info->print && prop->info->get) {
755 return;
758 name = g_strdup_printf("legacy-%s", prop->name);
759 object_property_add(OBJECT(dev), name, "str",
760 prop->info->print ? qdev_get_legacy_property : prop->info->get,
761 NULL,
762 NULL,
763 prop, errp);
765 g_free(name);
769 * qdev_property_add_static:
770 * @dev: Device to add the property to.
771 * @prop: The qdev property definition.
772 * @errp: location to store error information.
774 * Add a static QOM property to @dev for qdev property @prop.
775 * On error, store error in @errp. Static properties access data in a struct.
776 * The type of the QOM property is derived from prop->info.
778 void qdev_property_add_static(DeviceState *dev, Property *prop,
779 Error **errp)
781 Error *local_err = NULL;
782 Object *obj = OBJECT(dev);
785 * TODO qdev_prop_ptr does not have getters or setters. It must
786 * go now that it can be replaced with links. The test should be
787 * removed along with it: all static properties are read/write.
789 if (!prop->info->get && !prop->info->set) {
790 return;
793 object_property_add(obj, prop->name, prop->info->name,
794 prop->info->get, prop->info->set,
795 prop->info->release,
796 prop, &local_err);
798 if (local_err) {
799 error_propagate(errp, local_err);
800 return;
803 object_property_set_description(obj, prop->name,
804 prop->info->description,
805 &error_abort);
807 if (prop->qtype == QTYPE_NONE) {
808 return;
811 if (prop->qtype == QTYPE_QBOOL) {
812 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
813 } else if (prop->info->enum_table) {
814 object_property_set_str(obj, prop->info->enum_table[prop->defval],
815 prop->name, &error_abort);
816 } else if (prop->qtype == QTYPE_QINT) {
817 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
821 /* @qdev_alias_all_properties - Add alias properties to the source object for
822 * all qdev properties on the target DeviceState.
824 void qdev_alias_all_properties(DeviceState *target, Object *source)
826 ObjectClass *class;
827 Property *prop;
829 class = object_get_class(OBJECT(target));
830 do {
831 DeviceClass *dc = DEVICE_CLASS(class);
833 for (prop = dc->props; prop && prop->name; prop++) {
834 object_property_add_alias(source, prop->name,
835 OBJECT(target), prop->name,
836 &error_abort);
838 class = object_class_get_parent(class);
839 } while (class != object_class_by_name(TYPE_DEVICE));
842 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
844 GSList **list = opaque;
845 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
846 TYPE_DEVICE);
848 if (dev == NULL) {
849 return 0;
852 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
853 *list = g_slist_append(*list, dev);
856 return 0;
859 GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
861 GSList *list = NULL;
863 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
865 return list;
868 static bool device_get_realized(Object *obj, Error **errp)
870 DeviceState *dev = DEVICE(obj);
871 return dev->realized;
874 static void device_set_realized(Object *obj, bool value, Error **errp)
876 DeviceState *dev = DEVICE(obj);
877 DeviceClass *dc = DEVICE_GET_CLASS(dev);
878 HotplugHandler *hotplug_ctrl;
879 BusState *bus;
880 Error *local_err = NULL;
882 if (dev->hotplugged && !dc->hotpluggable) {
883 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
884 return;
887 if (value && !dev->realized) {
888 if (!obj->parent) {
889 static int unattached_count;
890 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
892 object_property_add_child(container_get(qdev_get_machine(),
893 "/unattached"),
894 name, obj, &error_abort);
895 g_free(name);
898 if (dc->realize) {
899 dc->realize(dev, &local_err);
902 if (local_err != NULL) {
903 goto fail;
906 DEVICE_LISTENER_CALL(realize, Forward, dev);
908 hotplug_ctrl = qdev_get_hotplug_handler(dev);
909 if (hotplug_ctrl) {
910 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
913 if (local_err != NULL) {
914 goto post_realize_fail;
917 if (qdev_get_vmsd(dev)) {
918 vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
919 dev->instance_id_alias,
920 dev->alias_required_for_version);
923 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
924 object_property_set_bool(OBJECT(bus), true, "realized",
925 &local_err);
926 if (local_err != NULL) {
927 goto child_realize_fail;
930 if (dev->hotplugged) {
931 device_reset(dev);
933 dev->pending_deleted_event = false;
934 } else if (!value && dev->realized) {
935 Error **local_errp = NULL;
936 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
937 local_errp = local_err ? NULL : &local_err;
938 object_property_set_bool(OBJECT(bus), false, "realized",
939 local_errp);
941 if (qdev_get_vmsd(dev)) {
942 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
944 if (dc->unrealize) {
945 local_errp = local_err ? NULL : &local_err;
946 dc->unrealize(dev, local_errp);
948 dev->pending_deleted_event = true;
949 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
952 if (local_err != NULL) {
953 goto fail;
956 dev->realized = value;
957 return;
959 child_realize_fail:
960 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
961 object_property_set_bool(OBJECT(bus), false, "realized",
962 NULL);
965 if (qdev_get_vmsd(dev)) {
966 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
969 post_realize_fail:
970 if (dc->unrealize) {
971 dc->unrealize(dev, NULL);
974 fail:
975 error_propagate(errp, local_err);
978 static bool device_get_hotpluggable(Object *obj, Error **errp)
980 DeviceClass *dc = DEVICE_GET_CLASS(obj);
981 DeviceState *dev = DEVICE(obj);
983 return dc->hotpluggable && (dev->parent_bus == NULL ||
984 qbus_is_hotpluggable(dev->parent_bus));
987 static bool device_get_hotplugged(Object *obj, Error **err)
989 DeviceState *dev = DEVICE(obj);
991 return dev->hotplugged;
994 static void device_set_hotplugged(Object *obj, bool value, Error **err)
996 DeviceState *dev = DEVICE(obj);
998 dev->hotplugged = value;
1001 static void device_initfn(Object *obj)
1003 DeviceState *dev = DEVICE(obj);
1004 ObjectClass *class;
1005 Property *prop;
1007 if (qdev_hotplug) {
1008 dev->hotplugged = 1;
1009 qdev_hot_added = true;
1012 dev->instance_id_alias = -1;
1013 dev->realized = false;
1015 object_property_add_bool(obj, "realized",
1016 device_get_realized, device_set_realized, NULL);
1017 object_property_add_bool(obj, "hotpluggable",
1018 device_get_hotpluggable, NULL, NULL);
1019 object_property_add_bool(obj, "hotplugged",
1020 device_get_hotplugged, device_set_hotplugged,
1021 &error_abort);
1023 class = object_get_class(OBJECT(dev));
1024 do {
1025 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
1026 qdev_property_add_legacy(dev, prop, &error_abort);
1027 qdev_property_add_static(dev, prop, &error_abort);
1029 class = object_class_get_parent(class);
1030 } while (class != object_class_by_name(TYPE_DEVICE));
1032 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
1033 (Object **)&dev->parent_bus, NULL, 0,
1034 &error_abort);
1035 QLIST_INIT(&dev->gpios);
1038 static void device_post_init(Object *obj)
1040 qdev_prop_set_globals(DEVICE(obj));
1043 /* Unlink device from bus and free the structure. */
1044 static void device_finalize(Object *obj)
1046 NamedGPIOList *ngl, *next;
1048 DeviceState *dev = DEVICE(obj);
1050 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1051 QLIST_REMOVE(ngl, node);
1052 qemu_free_irqs(ngl->in, ngl->num_in);
1053 g_free(ngl->name);
1054 g_free(ngl);
1055 /* ngl->out irqs are owned by the other end and should not be freed
1056 * here
1061 static void device_class_base_init(ObjectClass *class, void *data)
1063 DeviceClass *klass = DEVICE_CLASS(class);
1065 /* We explicitly look up properties in the superclasses,
1066 * so do not propagate them to the subclasses.
1068 klass->props = NULL;
1071 static void device_unparent(Object *obj)
1073 DeviceState *dev = DEVICE(obj);
1074 BusState *bus;
1076 if (dev->realized) {
1077 object_property_set_bool(obj, false, "realized", NULL);
1079 while (dev->num_child_bus) {
1080 bus = QLIST_FIRST(&dev->child_bus);
1081 object_unparent(OBJECT(bus));
1083 if (dev->parent_bus) {
1084 bus_remove_child(dev->parent_bus, dev);
1085 object_unref(OBJECT(dev->parent_bus));
1086 dev->parent_bus = NULL;
1089 /* Only send event if the device had been completely realized */
1090 if (dev->pending_deleted_event) {
1091 gchar *path = object_get_canonical_path(OBJECT(dev));
1093 qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
1094 g_free(path);
1097 qemu_opts_del(dev->opts);
1098 dev->opts = NULL;
1101 static void device_class_init(ObjectClass *class, void *data)
1103 DeviceClass *dc = DEVICE_CLASS(class);
1105 class->unparent = device_unparent;
1106 dc->realize = device_realize;
1107 dc->unrealize = device_unrealize;
1109 /* by default all devices were considered as hotpluggable,
1110 * so with intent to check it in generic qdev_unplug() /
1111 * device_set_realized() functions make every device
1112 * hotpluggable. Devices that shouldn't be hotpluggable,
1113 * should override it in their class_init()
1115 dc->hotpluggable = true;
1118 void device_reset(DeviceState *dev)
1120 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1122 if (klass->reset) {
1123 klass->reset(dev);
1127 Object *qdev_get_machine(void)
1129 static Object *dev;
1131 if (dev == NULL) {
1132 dev = container_get(object_get_root(), "/machine");
1135 return dev;
1138 static const TypeInfo device_type_info = {
1139 .name = TYPE_DEVICE,
1140 .parent = TYPE_OBJECT,
1141 .instance_size = sizeof(DeviceState),
1142 .instance_init = device_initfn,
1143 .instance_post_init = device_post_init,
1144 .instance_finalize = device_finalize,
1145 .class_base_init = device_class_base_init,
1146 .class_init = device_class_init,
1147 .abstract = true,
1148 .class_size = sizeof(DeviceClass),
1151 static void qdev_register_types(void)
1153 type_register_static(&device_type_info);
1156 type_init(qdev_register_types)