Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-2.9-20170303' into staging
[qemu/ar7.git] / hw / core / qdev.c
blob1e7fb3324617b1980010126e007d9e9c0b4d11d8
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 "hw/sysbus.h"
39 #include "qapi-event.h"
40 #include "migration/migration.h"
42 int qdev_hotplug = 0;
43 static bool qdev_hot_added = false;
44 static bool qdev_hot_removed = false;
46 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
48 DeviceClass *dc = DEVICE_GET_CLASS(dev);
49 return dc->vmsd;
52 const char *qdev_fw_name(DeviceState *dev)
54 DeviceClass *dc = DEVICE_GET_CLASS(dev);
56 if (dc->fw_name) {
57 return dc->fw_name;
60 return object_get_typename(OBJECT(dev));
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 bool replugging = dev->parent_bus != NULL;
108 if (replugging) {
109 /* Keep a reference to the device while it's not plugged into
110 * any bus, to avoid it potentially evaporating when it is
111 * dereffed in bus_remove_child().
113 object_ref(OBJECT(dev));
114 bus_remove_child(dev->parent_bus, dev);
115 object_unref(OBJECT(dev->parent_bus));
117 dev->parent_bus = bus;
118 object_ref(OBJECT(bus));
119 bus_add_child(bus, dev);
120 if (replugging) {
121 object_unref(OBJECT(dev));
125 /* Create a new device. This only initializes the device state
126 structure and allows properties to be set. The device still needs
127 to be realized. See qdev-core.h. */
128 DeviceState *qdev_create(BusState *bus, const char *name)
130 DeviceState *dev;
132 dev = qdev_try_create(bus, name);
133 if (!dev) {
134 if (bus) {
135 error_report("Unknown device '%s' for bus '%s'", name,
136 object_get_typename(OBJECT(bus)));
137 } else {
138 error_report("Unknown device '%s' for default sysbus", name);
140 abort();
143 return dev;
146 DeviceState *qdev_try_create(BusState *bus, const char *type)
148 DeviceState *dev;
150 if (object_class_by_name(type) == NULL) {
151 return NULL;
153 dev = DEVICE(object_new(type));
154 if (!dev) {
155 return NULL;
158 if (!bus) {
159 /* Assert that the device really is a SysBusDevice before
160 * we put it onto the sysbus. Non-sysbus devices which aren't
161 * being put onto a bus should be created with object_new(TYPE_FOO),
162 * not qdev_create(NULL, TYPE_FOO).
164 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
165 bus = sysbus_get_default();
168 qdev_set_parent_bus(dev, bus);
169 object_unref(OBJECT(dev));
170 return dev;
173 static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
174 = QTAILQ_HEAD_INITIALIZER(device_listeners);
176 enum ListenerDirection { Forward, Reverse };
178 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
179 do { \
180 DeviceListener *_listener; \
182 switch (_direction) { \
183 case Forward: \
184 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
185 if (_listener->_callback) { \
186 _listener->_callback(_listener, ##_args); \
189 break; \
190 case Reverse: \
191 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
192 device_listeners, link) { \
193 if (_listener->_callback) { \
194 _listener->_callback(_listener, ##_args); \
197 break; \
198 default: \
199 abort(); \
201 } while (0)
203 static int device_listener_add(DeviceState *dev, void *opaque)
205 DEVICE_LISTENER_CALL(realize, Forward, dev);
207 return 0;
210 void device_listener_register(DeviceListener *listener)
212 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
214 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
215 NULL, NULL);
218 void device_listener_unregister(DeviceListener *listener)
220 QTAILQ_REMOVE(&device_listeners, listener, link);
223 static void device_realize(DeviceState *dev, Error **errp)
225 DeviceClass *dc = DEVICE_GET_CLASS(dev);
227 if (dc->init) {
228 int rc = dc->init(dev);
229 if (rc < 0) {
230 error_setg(errp, "Device initialization failed.");
231 return;
236 static void device_unrealize(DeviceState *dev, Error **errp)
238 DeviceClass *dc = DEVICE_GET_CLASS(dev);
240 if (dc->exit) {
241 int rc = dc->exit(dev);
242 if (rc < 0) {
243 error_setg(errp, "Device exit failed.");
244 return;
249 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
250 int required_for_version)
252 assert(!dev->realized);
253 dev->instance_id_alias = alias_id;
254 dev->alias_required_for_version = required_for_version;
257 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
259 HotplugHandler *hotplug_ctrl = NULL;
261 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
262 hotplug_ctrl = dev->parent_bus->hotplug_handler;
263 } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
264 MachineState *machine = MACHINE(qdev_get_machine());
265 MachineClass *mc = MACHINE_GET_CLASS(machine);
267 if (mc->get_hotplug_handler) {
268 hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
271 return hotplug_ctrl;
274 void qdev_unplug(DeviceState *dev, Error **errp)
276 DeviceClass *dc = DEVICE_GET_CLASS(dev);
277 HotplugHandler *hotplug_ctrl;
278 HotplugHandlerClass *hdc;
280 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
281 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
282 return;
285 if (!dc->hotpluggable) {
286 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
287 object_get_typename(OBJECT(dev)));
288 return;
291 qdev_hot_removed = true;
293 hotplug_ctrl = qdev_get_hotplug_handler(dev);
294 /* hotpluggable device MUST have HotplugHandler, if it doesn't
295 * then something is very wrong with it */
296 g_assert(hotplug_ctrl);
298 /* If device supports async unplug just request it to be done,
299 * otherwise just remove it synchronously */
300 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
301 if (hdc->unplug_request) {
302 hotplug_handler_unplug_request(hotplug_ctrl, dev, errp);
303 } else {
304 hotplug_handler_unplug(hotplug_ctrl, dev, errp);
308 static int qdev_reset_one(DeviceState *dev, void *opaque)
310 device_reset(dev);
312 return 0;
315 static int qbus_reset_one(BusState *bus, void *opaque)
317 BusClass *bc = BUS_GET_CLASS(bus);
318 if (bc->reset) {
319 bc->reset(bus);
321 return 0;
324 void qdev_reset_all(DeviceState *dev)
326 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
329 void qdev_reset_all_fn(void *opaque)
331 qdev_reset_all(DEVICE(opaque));
334 void qbus_reset_all(BusState *bus)
336 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
339 void qbus_reset_all_fn(void *opaque)
341 BusState *bus = opaque;
342 qbus_reset_all(bus);
345 /* can be used as ->unplug() callback for the simple cases */
346 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
347 DeviceState *dev, Error **errp)
349 /* just zap it */
350 object_unparent(OBJECT(dev));
354 * Realize @dev.
355 * Device properties should be set before calling this function. IRQs
356 * and MMIO regions should be connected/mapped after calling this
357 * function.
358 * On failure, report an error with error_report() and terminate the
359 * program. This is okay during machine creation. Don't use for
360 * hotplug, because there callers need to recover from failure.
361 * Exception: if you know the device's init() callback can't fail,
362 * then qdev_init_nofail() can't fail either, and is therefore usable
363 * even then. But relying on the device implementation that way is
364 * somewhat unclean, and best avoided.
366 void qdev_init_nofail(DeviceState *dev)
368 Error *err = NULL;
370 assert(!dev->realized);
372 object_ref(OBJECT(dev));
373 object_property_set_bool(OBJECT(dev), true, "realized", &err);
374 if (err) {
375 error_reportf_err(err, "Initialization of device %s failed: ",
376 object_get_typename(OBJECT(dev)));
377 exit(1);
379 object_unref(OBJECT(dev));
382 void qdev_machine_creation_done(void)
385 * ok, initial machine setup is done, starting from now we can
386 * only create hotpluggable devices
388 qdev_hotplug = 1;
391 bool qdev_machine_modified(void)
393 return qdev_hot_added || qdev_hot_removed;
396 BusState *qdev_get_parent_bus(DeviceState *dev)
398 return dev->parent_bus;
401 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
402 const char *name)
404 NamedGPIOList *ngl;
406 QLIST_FOREACH(ngl, &dev->gpios, node) {
407 /* NULL is a valid and matchable name, otherwise do a normal
408 * strcmp match.
410 if ((!ngl->name && !name) ||
411 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
412 return ngl;
416 ngl = g_malloc0(sizeof(*ngl));
417 ngl->name = g_strdup(name);
418 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
419 return ngl;
422 void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
423 const char *name, int n)
425 int i;
426 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
428 assert(gpio_list->num_out == 0 || !name);
429 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
430 dev, n);
432 if (!name) {
433 name = "unnamed-gpio-in";
435 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
436 gchar *propname = g_strdup_printf("%s[%u]", name, i);
438 object_property_add_child(OBJECT(dev), propname,
439 OBJECT(gpio_list->in[i]), &error_abort);
440 g_free(propname);
443 gpio_list->num_in += n;
446 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
448 qdev_init_gpio_in_named(dev, handler, NULL, n);
451 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
452 const char *name, int n)
454 int i;
455 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
457 assert(gpio_list->num_in == 0 || !name);
459 if (!name) {
460 name = "unnamed-gpio-out";
462 memset(pins, 0, sizeof(*pins) * n);
463 for (i = 0; i < n; ++i) {
464 gchar *propname = g_strdup_printf("%s[%u]", name,
465 gpio_list->num_out + i);
467 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
468 (Object **)&pins[i],
469 object_property_allow_set_link,
470 OBJ_PROP_LINK_UNREF_ON_RELEASE,
471 &error_abort);
472 g_free(propname);
474 gpio_list->num_out += n;
477 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
479 qdev_init_gpio_out_named(dev, pins, NULL, n);
482 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
484 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
486 assert(n >= 0 && n < gpio_list->num_in);
487 return gpio_list->in[n];
490 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
492 return qdev_get_gpio_in_named(dev, NULL, n);
495 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
496 qemu_irq pin)
498 char *propname = g_strdup_printf("%s[%d]",
499 name ? name : "unnamed-gpio-out", n);
500 if (pin) {
501 /* We need a name for object_property_set_link to work. If the
502 * object has a parent, object_property_add_child will come back
503 * with an error without doing anything. If it has none, it will
504 * never fail. So we can just call it with a NULL Error pointer.
506 object_property_add_child(container_get(qdev_get_machine(),
507 "/unattached"),
508 "non-qdev-gpio[*]", OBJECT(pin), NULL);
510 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
511 g_free(propname);
514 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
516 char *propname = g_strdup_printf("%s[%d]",
517 name ? name : "unnamed-gpio-out", n);
519 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
520 NULL);
522 return ret;
525 /* disconnect a GPIO output, returning the disconnected input (if any) */
527 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
528 const char *name, int n)
530 char *propname = g_strdup_printf("%s[%d]",
531 name ? name : "unnamed-gpio-out", n);
533 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
534 NULL);
535 if (ret) {
536 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
538 g_free(propname);
539 return ret;
542 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
543 const char *name, int n)
545 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
546 qdev_connect_gpio_out_named(dev, name, n, icpt);
547 return disconnected;
550 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
552 qdev_connect_gpio_out_named(dev, NULL, n, pin);
555 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
556 const char *name)
558 int i;
559 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
561 for (i = 0; i < ngl->num_in; i++) {
562 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
563 char *propname = g_strdup_printf("%s[%d]", nm, i);
565 object_property_add_alias(OBJECT(container), propname,
566 OBJECT(dev), propname,
567 &error_abort);
568 g_free(propname);
570 for (i = 0; i < ngl->num_out; i++) {
571 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
572 char *propname = g_strdup_printf("%s[%d]", nm, i);
574 object_property_add_alias(OBJECT(container), propname,
575 OBJECT(dev), propname,
576 &error_abort);
577 g_free(propname);
579 QLIST_REMOVE(ngl, node);
580 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
583 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
585 BusState *bus;
586 Object *child = object_resolve_path_component(OBJECT(dev), name);
588 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
589 if (bus) {
590 return bus;
593 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
594 if (strcmp(name, bus->name) == 0) {
595 return bus;
598 return NULL;
601 int qdev_walk_children(DeviceState *dev,
602 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
603 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
604 void *opaque)
606 BusState *bus;
607 int err;
609 if (pre_devfn) {
610 err = pre_devfn(dev, opaque);
611 if (err) {
612 return err;
616 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
617 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
618 post_devfn, post_busfn, opaque);
619 if (err < 0) {
620 return err;
624 if (post_devfn) {
625 err = post_devfn(dev, opaque);
626 if (err) {
627 return err;
631 return 0;
634 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
636 BusChild *kid;
637 DeviceState *ret;
638 BusState *child;
640 QTAILQ_FOREACH(kid, &bus->children, sibling) {
641 DeviceState *dev = kid->child;
643 if (dev->id && strcmp(dev->id, id) == 0) {
644 return dev;
647 QLIST_FOREACH(child, &dev->child_bus, sibling) {
648 ret = qdev_find_recursive(child, id);
649 if (ret) {
650 return ret;
654 return NULL;
657 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
659 BusClass *bc = BUS_GET_CLASS(bus);
661 if (bc->get_fw_dev_path) {
662 return bc->get_fw_dev_path(dev);
665 return NULL;
668 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
670 Object *obj = OBJECT(dev);
671 char *d = NULL;
673 while (!d && obj->parent) {
674 obj = obj->parent;
675 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
677 return d;
680 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
682 Object *obj = OBJECT(dev);
684 return fw_path_provider_try_get_dev_path(obj, bus, dev);
687 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
689 int l = 0;
691 if (dev && dev->parent_bus) {
692 char *d;
693 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
694 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
695 if (!d) {
696 d = bus_get_fw_dev_path(dev->parent_bus, dev);
698 if (d) {
699 l += snprintf(p + l, size - l, "%s", d);
700 g_free(d);
701 } else {
702 return l;
705 l += snprintf(p + l , size - l, "/");
707 return l;
710 char* qdev_get_fw_dev_path(DeviceState *dev)
712 char path[128];
713 int l;
715 l = qdev_get_fw_dev_path_helper(dev, path, 128);
717 path[l-1] = '\0';
719 return g_strdup(path);
722 char *qdev_get_dev_path(DeviceState *dev)
724 BusClass *bc;
726 if (!dev || !dev->parent_bus) {
727 return NULL;
730 bc = BUS_GET_CLASS(dev->parent_bus);
731 if (bc->get_dev_path) {
732 return bc->get_dev_path(dev);
735 return NULL;
739 * Legacy property handling
742 static void qdev_get_legacy_property(Object *obj, Visitor *v,
743 const char *name, void *opaque,
744 Error **errp)
746 DeviceState *dev = DEVICE(obj);
747 Property *prop = opaque;
749 char buffer[1024];
750 char *ptr = buffer;
752 prop->info->print(dev, prop, buffer, sizeof(buffer));
753 visit_type_str(v, name, &ptr, errp);
757 * qdev_property_add_legacy:
758 * @dev: Device to add the property to.
759 * @prop: The qdev property definition.
760 * @errp: location to store error information.
762 * Add a legacy QOM property to @dev for qdev property @prop.
763 * On error, store error in @errp.
765 * Legacy properties are string versions of QOM properties. The format of
766 * the string depends on the property type. Legacy properties are only
767 * needed for "info qtree".
769 * Do not use this is new code! QOM Properties added through this interface
770 * will be given names in the "legacy" namespace.
772 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
773 Error **errp)
775 gchar *name;
777 /* Register pointer properties as legacy properties */
778 if (!prop->info->print && prop->info->get) {
779 return;
782 name = g_strdup_printf("legacy-%s", prop->name);
783 object_property_add(OBJECT(dev), name, "str",
784 prop->info->print ? qdev_get_legacy_property : prop->info->get,
785 NULL,
786 NULL,
787 prop, errp);
789 g_free(name);
793 * qdev_property_add_static:
794 * @dev: Device to add the property to.
795 * @prop: The qdev property definition.
796 * @errp: location to store error information.
798 * Add a static QOM property to @dev for qdev property @prop.
799 * On error, store error in @errp. Static properties access data in a struct.
800 * The type of the QOM property is derived from prop->info.
802 void qdev_property_add_static(DeviceState *dev, Property *prop,
803 Error **errp)
805 Error *local_err = NULL;
806 Object *obj = OBJECT(dev);
809 * TODO qdev_prop_ptr does not have getters or setters. It must
810 * go now that it can be replaced with links. The test should be
811 * removed along with it: all static properties are read/write.
813 if (!prop->info->get && !prop->info->set) {
814 return;
817 object_property_add(obj, prop->name, prop->info->name,
818 prop->info->get, prop->info->set,
819 prop->info->release,
820 prop, &local_err);
822 if (local_err) {
823 error_propagate(errp, local_err);
824 return;
827 object_property_set_description(obj, prop->name,
828 prop->info->description,
829 &error_abort);
831 if (prop->qtype == QTYPE_NONE) {
832 return;
835 if (prop->qtype == QTYPE_QBOOL) {
836 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
837 } else if (prop->info->enum_table) {
838 object_property_set_str(obj, prop->info->enum_table[prop->defval],
839 prop->name, &error_abort);
840 } else if (prop->qtype == QTYPE_QINT) {
841 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
845 /* @qdev_alias_all_properties - Add alias properties to the source object for
846 * all qdev properties on the target DeviceState.
848 void qdev_alias_all_properties(DeviceState *target, Object *source)
850 ObjectClass *class;
851 Property *prop;
853 class = object_get_class(OBJECT(target));
854 do {
855 DeviceClass *dc = DEVICE_CLASS(class);
857 for (prop = dc->props; prop && prop->name; prop++) {
858 object_property_add_alias(source, prop->name,
859 OBJECT(target), prop->name,
860 &error_abort);
862 class = object_class_get_parent(class);
863 } while (class != object_class_by_name(TYPE_DEVICE));
866 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
868 GSList **list = opaque;
869 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
870 TYPE_DEVICE);
872 if (dev == NULL) {
873 return 0;
876 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
877 *list = g_slist_append(*list, dev);
880 return 0;
883 GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
885 GSList *list = NULL;
887 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
889 return list;
892 static bool device_get_realized(Object *obj, Error **errp)
894 DeviceState *dev = DEVICE(obj);
895 return dev->realized;
898 static void device_set_realized(Object *obj, bool value, Error **errp)
900 DeviceState *dev = DEVICE(obj);
901 DeviceClass *dc = DEVICE_GET_CLASS(dev);
902 HotplugHandler *hotplug_ctrl;
903 BusState *bus;
904 Error *local_err = NULL;
905 bool unattached_parent = false;
906 static int unattached_count;
907 int ret;
909 if (dev->hotplugged && !dc->hotpluggable) {
910 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
911 return;
914 if (value && !dev->realized) {
915 ret = check_migratable(obj, &local_err);
916 if (ret < 0) {
917 goto fail;
920 if (!obj->parent) {
921 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
923 object_property_add_child(container_get(qdev_get_machine(),
924 "/unattached"),
925 name, obj, &error_abort);
926 unattached_parent = true;
927 g_free(name);
930 hotplug_ctrl = qdev_get_hotplug_handler(dev);
931 if (hotplug_ctrl) {
932 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
933 if (local_err != NULL) {
934 goto fail;
938 if (dc->realize) {
939 dc->realize(dev, &local_err);
942 if (local_err != NULL) {
943 goto fail;
946 DEVICE_LISTENER_CALL(realize, Forward, dev);
948 if (hotplug_ctrl) {
949 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
952 if (local_err != NULL) {
953 goto post_realize_fail;
956 if (qdev_get_vmsd(dev)) {
957 if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
958 dev->instance_id_alias,
959 dev->alias_required_for_version,
960 &local_err) < 0) {
961 goto post_realize_fail;
965 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
966 object_property_set_bool(OBJECT(bus), true, "realized",
967 &local_err);
968 if (local_err != NULL) {
969 goto child_realize_fail;
972 if (dev->hotplugged) {
973 device_reset(dev);
975 dev->pending_deleted_event = false;
976 } else if (!value && dev->realized) {
977 Error **local_errp = NULL;
978 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
979 local_errp = local_err ? NULL : &local_err;
980 object_property_set_bool(OBJECT(bus), false, "realized",
981 local_errp);
983 if (qdev_get_vmsd(dev)) {
984 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
986 if (dc->unrealize) {
987 local_errp = local_err ? NULL : &local_err;
988 dc->unrealize(dev, local_errp);
990 dev->pending_deleted_event = true;
991 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
994 if (local_err != NULL) {
995 goto fail;
998 dev->realized = value;
999 return;
1001 child_realize_fail:
1002 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
1003 object_property_set_bool(OBJECT(bus), false, "realized",
1004 NULL);
1007 if (qdev_get_vmsd(dev)) {
1008 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
1011 post_realize_fail:
1012 if (dc->unrealize) {
1013 dc->unrealize(dev, NULL);
1016 fail:
1017 error_propagate(errp, local_err);
1018 if (unattached_parent) {
1019 object_unparent(OBJECT(dev));
1020 unattached_count--;
1024 static bool device_get_hotpluggable(Object *obj, Error **errp)
1026 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1027 DeviceState *dev = DEVICE(obj);
1029 return dc->hotpluggable && (dev->parent_bus == NULL ||
1030 qbus_is_hotpluggable(dev->parent_bus));
1033 static bool device_get_hotplugged(Object *obj, Error **err)
1035 DeviceState *dev = DEVICE(obj);
1037 return dev->hotplugged;
1040 static void device_set_hotplugged(Object *obj, bool value, Error **err)
1042 DeviceState *dev = DEVICE(obj);
1044 dev->hotplugged = value;
1047 static void device_initfn(Object *obj)
1049 DeviceState *dev = DEVICE(obj);
1050 ObjectClass *class;
1051 Property *prop;
1053 if (qdev_hotplug) {
1054 dev->hotplugged = 1;
1055 qdev_hot_added = true;
1058 dev->instance_id_alias = -1;
1059 dev->realized = false;
1061 object_property_add_bool(obj, "realized",
1062 device_get_realized, device_set_realized, NULL);
1063 object_property_add_bool(obj, "hotpluggable",
1064 device_get_hotpluggable, NULL, NULL);
1065 object_property_add_bool(obj, "hotplugged",
1066 device_get_hotplugged, device_set_hotplugged,
1067 &error_abort);
1069 class = object_get_class(OBJECT(dev));
1070 do {
1071 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
1072 qdev_property_add_legacy(dev, prop, &error_abort);
1073 qdev_property_add_static(dev, prop, &error_abort);
1075 class = object_class_get_parent(class);
1076 } while (class != object_class_by_name(TYPE_DEVICE));
1078 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
1079 (Object **)&dev->parent_bus, NULL, 0,
1080 &error_abort);
1081 QLIST_INIT(&dev->gpios);
1084 static void device_post_init(Object *obj)
1086 qdev_prop_set_globals(DEVICE(obj));
1089 /* Unlink device from bus and free the structure. */
1090 static void device_finalize(Object *obj)
1092 NamedGPIOList *ngl, *next;
1094 DeviceState *dev = DEVICE(obj);
1096 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1097 QLIST_REMOVE(ngl, node);
1098 qemu_free_irqs(ngl->in, ngl->num_in);
1099 g_free(ngl->name);
1100 g_free(ngl);
1101 /* ngl->out irqs are owned by the other end and should not be freed
1102 * here
1107 static void device_class_base_init(ObjectClass *class, void *data)
1109 DeviceClass *klass = DEVICE_CLASS(class);
1111 /* We explicitly look up properties in the superclasses,
1112 * so do not propagate them to the subclasses.
1114 klass->props = NULL;
1117 static void device_unparent(Object *obj)
1119 DeviceState *dev = DEVICE(obj);
1120 BusState *bus;
1122 if (dev->realized) {
1123 object_property_set_bool(obj, false, "realized", NULL);
1125 while (dev->num_child_bus) {
1126 bus = QLIST_FIRST(&dev->child_bus);
1127 object_unparent(OBJECT(bus));
1129 if (dev->parent_bus) {
1130 bus_remove_child(dev->parent_bus, dev);
1131 object_unref(OBJECT(dev->parent_bus));
1132 dev->parent_bus = NULL;
1135 /* Only send event if the device had been completely realized */
1136 if (dev->pending_deleted_event) {
1137 gchar *path = object_get_canonical_path(OBJECT(dev));
1139 qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
1140 g_free(path);
1143 qemu_opts_del(dev->opts);
1144 dev->opts = NULL;
1147 static void device_class_init(ObjectClass *class, void *data)
1149 DeviceClass *dc = DEVICE_CLASS(class);
1151 class->unparent = device_unparent;
1152 dc->realize = device_realize;
1153 dc->unrealize = device_unrealize;
1155 /* by default all devices were considered as hotpluggable,
1156 * so with intent to check it in generic qdev_unplug() /
1157 * device_set_realized() functions make every device
1158 * hotpluggable. Devices that shouldn't be hotpluggable,
1159 * should override it in their class_init()
1161 dc->hotpluggable = true;
1164 void device_reset(DeviceState *dev)
1166 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1168 if (klass->reset) {
1169 klass->reset(dev);
1173 Object *qdev_get_machine(void)
1175 static Object *dev;
1177 if (dev == NULL) {
1178 dev = container_get(object_get_root(), "/machine");
1181 return dev;
1184 static const TypeInfo device_type_info = {
1185 .name = TYPE_DEVICE,
1186 .parent = TYPE_OBJECT,
1187 .instance_size = sizeof(DeviceState),
1188 .instance_init = device_initfn,
1189 .instance_post_init = device_post_init,
1190 .instance_finalize = device_finalize,
1191 .class_base_init = device_class_base_init,
1192 .class_init = device_class_init,
1193 .abstract = true,
1194 .class_size = sizeof(DeviceClass),
1197 static void qdev_register_types(void)
1199 type_register_static(&device_type_info);
1202 type_init(qdev_register_types)