qdev: Have qdev_set_parent_bus() handle devices already on a bus
[qemu/ar7.git] / hw / core / qdev.c
blob923e626333d78c674cab2fd856adf4292c380718
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"
41 int qdev_hotplug = 0;
42 static bool qdev_hot_added = false;
43 static bool qdev_hot_removed = false;
45 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
47 DeviceClass *dc = DEVICE_GET_CLASS(dev);
48 return dc->vmsd;
51 const char *qdev_fw_name(DeviceState *dev)
53 DeviceClass *dc = DEVICE_GET_CLASS(dev);
55 if (dc->fw_name) {
56 return dc->fw_name;
59 return object_get_typename(OBJECT(dev));
62 static void bus_remove_child(BusState *bus, DeviceState *child)
64 BusChild *kid;
66 QTAILQ_FOREACH(kid, &bus->children, sibling) {
67 if (kid->child == child) {
68 char name[32];
70 snprintf(name, sizeof(name), "child[%d]", kid->index);
71 QTAILQ_REMOVE(&bus->children, kid, sibling);
73 /* This gives back ownership of kid->child back to us. */
74 object_property_del(OBJECT(bus), name, NULL);
75 object_unref(OBJECT(kid->child));
76 g_free(kid);
77 return;
82 static void bus_add_child(BusState *bus, DeviceState *child)
84 char name[32];
85 BusChild *kid = g_malloc0(sizeof(*kid));
87 kid->index = bus->max_index++;
88 kid->child = child;
89 object_ref(OBJECT(kid->child));
91 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
93 /* This transfers ownership of kid->child to the property. */
94 snprintf(name, sizeof(name), "child[%d]", kid->index);
95 object_property_add_link(OBJECT(bus), name,
96 object_get_typename(OBJECT(child)),
97 (Object **)&kid->child,
98 NULL, /* read-only property */
99 0, /* return ownership on prop deletion */
100 NULL);
103 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
105 bool replugging = dev->parent_bus != NULL;
107 if (replugging) {
108 /* Keep a reference to the device while it's not plugged into
109 * any bus, to avoid it potentially evaporating when it is
110 * dereffed in bus_remove_child().
112 object_ref(OBJECT(dev));
113 bus_remove_child(dev->parent_bus, dev);
114 object_unref(OBJECT(dev->parent_bus));
116 dev->parent_bus = bus;
117 object_ref(OBJECT(bus));
118 bus_add_child(bus, dev);
119 if (replugging) {
120 object_unref(OBJECT(dev));
124 /* Create a new device. This only initializes the device state
125 structure and allows properties to be set. The device still needs
126 to be realized. See qdev-core.h. */
127 DeviceState *qdev_create(BusState *bus, const char *name)
129 DeviceState *dev;
131 dev = qdev_try_create(bus, name);
132 if (!dev) {
133 if (bus) {
134 error_report("Unknown device '%s' for bus '%s'", name,
135 object_get_typename(OBJECT(bus)));
136 } else {
137 error_report("Unknown device '%s' for default sysbus", name);
139 abort();
142 return dev;
145 DeviceState *qdev_try_create(BusState *bus, const char *type)
147 DeviceState *dev;
149 if (object_class_by_name(type) == NULL) {
150 return NULL;
152 dev = DEVICE(object_new(type));
153 if (!dev) {
154 return NULL;
157 if (!bus) {
158 /* Assert that the device really is a SysBusDevice before
159 * we put it onto the sysbus. Non-sysbus devices which aren't
160 * being put onto a bus should be created with object_new(TYPE_FOO),
161 * not qdev_create(NULL, TYPE_FOO).
163 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
164 bus = sysbus_get_default();
167 qdev_set_parent_bus(dev, bus);
168 object_unref(OBJECT(dev));
169 return dev;
172 static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
173 = QTAILQ_HEAD_INITIALIZER(device_listeners);
175 enum ListenerDirection { Forward, Reverse };
177 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \
178 do { \
179 DeviceListener *_listener; \
181 switch (_direction) { \
182 case Forward: \
183 QTAILQ_FOREACH(_listener, &device_listeners, link) { \
184 if (_listener->_callback) { \
185 _listener->_callback(_listener, ##_args); \
188 break; \
189 case Reverse: \
190 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \
191 device_listeners, link) { \
192 if (_listener->_callback) { \
193 _listener->_callback(_listener, ##_args); \
196 break; \
197 default: \
198 abort(); \
200 } while (0)
202 static int device_listener_add(DeviceState *dev, void *opaque)
204 DEVICE_LISTENER_CALL(realize, Forward, dev);
206 return 0;
209 void device_listener_register(DeviceListener *listener)
211 QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
213 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
214 NULL, NULL);
217 void device_listener_unregister(DeviceListener *listener)
219 QTAILQ_REMOVE(&device_listeners, listener, link);
222 static void device_realize(DeviceState *dev, Error **errp)
224 DeviceClass *dc = DEVICE_GET_CLASS(dev);
226 if (dc->init) {
227 int rc = dc->init(dev);
228 if (rc < 0) {
229 error_setg(errp, "Device initialization failed.");
230 return;
235 static void device_unrealize(DeviceState *dev, Error **errp)
237 DeviceClass *dc = DEVICE_GET_CLASS(dev);
239 if (dc->exit) {
240 int rc = dc->exit(dev);
241 if (rc < 0) {
242 error_setg(errp, "Device exit failed.");
243 return;
248 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
249 int required_for_version)
251 assert(!dev->realized);
252 dev->instance_id_alias = alias_id;
253 dev->alias_required_for_version = required_for_version;
256 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
258 HotplugHandler *hotplug_ctrl = NULL;
260 if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
261 hotplug_ctrl = dev->parent_bus->hotplug_handler;
262 } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
263 MachineState *machine = MACHINE(qdev_get_machine());
264 MachineClass *mc = MACHINE_GET_CLASS(machine);
266 if (mc->get_hotplug_handler) {
267 hotplug_ctrl = mc->get_hotplug_handler(machine, dev);
270 return hotplug_ctrl;
273 void qdev_unplug(DeviceState *dev, Error **errp)
275 DeviceClass *dc = DEVICE_GET_CLASS(dev);
276 HotplugHandler *hotplug_ctrl;
277 HotplugHandlerClass *hdc;
279 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
280 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
281 return;
284 if (!dc->hotpluggable) {
285 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
286 object_get_typename(OBJECT(dev)));
287 return;
290 qdev_hot_removed = true;
292 hotplug_ctrl = qdev_get_hotplug_handler(dev);
293 /* hotpluggable device MUST have HotplugHandler, if it doesn't
294 * then something is very wrong with it */
295 g_assert(hotplug_ctrl);
297 /* If device supports async unplug just request it to be done,
298 * otherwise just remove it synchronously */
299 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
300 if (hdc->unplug_request) {
301 hotplug_handler_unplug_request(hotplug_ctrl, dev, errp);
302 } else {
303 hotplug_handler_unplug(hotplug_ctrl, dev, errp);
307 static int qdev_reset_one(DeviceState *dev, void *opaque)
309 device_reset(dev);
311 return 0;
314 static int qbus_reset_one(BusState *bus, void *opaque)
316 BusClass *bc = BUS_GET_CLASS(bus);
317 if (bc->reset) {
318 bc->reset(bus);
320 return 0;
323 void qdev_reset_all(DeviceState *dev)
325 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
328 void qdev_reset_all_fn(void *opaque)
330 qdev_reset_all(DEVICE(opaque));
333 void qbus_reset_all(BusState *bus)
335 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
338 void qbus_reset_all_fn(void *opaque)
340 BusState *bus = opaque;
341 qbus_reset_all(bus);
344 /* can be used as ->unplug() callback for the simple cases */
345 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
346 DeviceState *dev, Error **errp)
348 /* just zap it */
349 object_unparent(OBJECT(dev));
353 * Realize @dev.
354 * Device properties should be set before calling this function. IRQs
355 * and MMIO regions should be connected/mapped after calling this
356 * function.
357 * On failure, report an error with error_report() and terminate the
358 * program. This is okay during machine creation. Don't use for
359 * hotplug, because there callers need to recover from failure.
360 * Exception: if you know the device's init() callback can't fail,
361 * then qdev_init_nofail() can't fail either, and is therefore usable
362 * even then. But relying on the device implementation that way is
363 * somewhat unclean, and best avoided.
365 void qdev_init_nofail(DeviceState *dev)
367 Error *err = NULL;
369 assert(!dev->realized);
371 object_ref(OBJECT(dev));
372 object_property_set_bool(OBJECT(dev), true, "realized", &err);
373 if (err) {
374 error_reportf_err(err, "Initialization of device %s failed: ",
375 object_get_typename(OBJECT(dev)));
376 exit(1);
378 object_unref(OBJECT(dev));
381 void qdev_machine_creation_done(void)
384 * ok, initial machine setup is done, starting from now we can
385 * only create hotpluggable devices
387 qdev_hotplug = 1;
390 bool qdev_machine_modified(void)
392 return qdev_hot_added || qdev_hot_removed;
395 BusState *qdev_get_parent_bus(DeviceState *dev)
397 return dev->parent_bus;
400 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
401 const char *name)
403 NamedGPIOList *ngl;
405 QLIST_FOREACH(ngl, &dev->gpios, node) {
406 /* NULL is a valid and matchable name, otherwise do a normal
407 * strcmp match.
409 if ((!ngl->name && !name) ||
410 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
411 return ngl;
415 ngl = g_malloc0(sizeof(*ngl));
416 ngl->name = g_strdup(name);
417 QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
418 return ngl;
421 void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
422 const char *name, int n)
424 int i;
425 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
427 assert(gpio_list->num_out == 0 || !name);
428 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
429 dev, n);
431 if (!name) {
432 name = "unnamed-gpio-in";
434 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
435 gchar *propname = g_strdup_printf("%s[%u]", name, i);
437 object_property_add_child(OBJECT(dev), propname,
438 OBJECT(gpio_list->in[i]), &error_abort);
439 g_free(propname);
442 gpio_list->num_in += n;
445 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
447 qdev_init_gpio_in_named(dev, handler, NULL, n);
450 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
451 const char *name, int n)
453 int i;
454 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
456 assert(gpio_list->num_in == 0 || !name);
458 if (!name) {
459 name = "unnamed-gpio-out";
461 memset(pins, 0, sizeof(*pins) * n);
462 for (i = 0; i < n; ++i) {
463 gchar *propname = g_strdup_printf("%s[%u]", name,
464 gpio_list->num_out + i);
466 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
467 (Object **)&pins[i],
468 object_property_allow_set_link,
469 OBJ_PROP_LINK_UNREF_ON_RELEASE,
470 &error_abort);
471 g_free(propname);
473 gpio_list->num_out += n;
476 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
478 qdev_init_gpio_out_named(dev, pins, NULL, n);
481 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
483 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
485 assert(n >= 0 && n < gpio_list->num_in);
486 return gpio_list->in[n];
489 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
491 return qdev_get_gpio_in_named(dev, NULL, n);
494 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
495 qemu_irq pin)
497 char *propname = g_strdup_printf("%s[%d]",
498 name ? name : "unnamed-gpio-out", n);
499 if (pin) {
500 /* We need a name for object_property_set_link to work. If the
501 * object has a parent, object_property_add_child will come back
502 * with an error without doing anything. If it has none, it will
503 * never fail. So we can just call it with a NULL Error pointer.
505 object_property_add_child(container_get(qdev_get_machine(),
506 "/unattached"),
507 "non-qdev-gpio[*]", OBJECT(pin), NULL);
509 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
510 g_free(propname);
513 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
515 char *propname = g_strdup_printf("%s[%d]",
516 name ? name : "unnamed-gpio-out", n);
518 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
519 NULL);
521 return ret;
524 /* disconnect a GPIO output, returning the disconnected input (if any) */
526 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
527 const char *name, int n)
529 char *propname = g_strdup_printf("%s[%d]",
530 name ? name : "unnamed-gpio-out", n);
532 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
533 NULL);
534 if (ret) {
535 object_property_set_link(OBJECT(dev), NULL, propname, NULL);
537 g_free(propname);
538 return ret;
541 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
542 const char *name, int n)
544 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
545 qdev_connect_gpio_out_named(dev, name, n, icpt);
546 return disconnected;
549 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
551 qdev_connect_gpio_out_named(dev, NULL, n, pin);
554 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
555 const char *name)
557 int i;
558 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
560 for (i = 0; i < ngl->num_in; i++) {
561 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
562 char *propname = g_strdup_printf("%s[%d]", nm, i);
564 object_property_add_alias(OBJECT(container), propname,
565 OBJECT(dev), propname,
566 &error_abort);
567 g_free(propname);
569 for (i = 0; i < ngl->num_out; i++) {
570 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
571 char *propname = g_strdup_printf("%s[%d]", nm, i);
573 object_property_add_alias(OBJECT(container), propname,
574 OBJECT(dev), propname,
575 &error_abort);
576 g_free(propname);
578 QLIST_REMOVE(ngl, node);
579 QLIST_INSERT_HEAD(&container->gpios, ngl, node);
582 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
584 BusState *bus;
585 Object *child = object_resolve_path_component(OBJECT(dev), name);
587 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
588 if (bus) {
589 return bus;
592 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
593 if (strcmp(name, bus->name) == 0) {
594 return bus;
597 return NULL;
600 int qdev_walk_children(DeviceState *dev,
601 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
602 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
603 void *opaque)
605 BusState *bus;
606 int err;
608 if (pre_devfn) {
609 err = pre_devfn(dev, opaque);
610 if (err) {
611 return err;
615 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
616 err = qbus_walk_children(bus, pre_devfn, pre_busfn,
617 post_devfn, post_busfn, opaque);
618 if (err < 0) {
619 return err;
623 if (post_devfn) {
624 err = post_devfn(dev, opaque);
625 if (err) {
626 return err;
630 return 0;
633 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
635 BusChild *kid;
636 DeviceState *ret;
637 BusState *child;
639 QTAILQ_FOREACH(kid, &bus->children, sibling) {
640 DeviceState *dev = kid->child;
642 if (dev->id && strcmp(dev->id, id) == 0) {
643 return dev;
646 QLIST_FOREACH(child, &dev->child_bus, sibling) {
647 ret = qdev_find_recursive(child, id);
648 if (ret) {
649 return ret;
653 return NULL;
656 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
658 BusClass *bc = BUS_GET_CLASS(bus);
660 if (bc->get_fw_dev_path) {
661 return bc->get_fw_dev_path(dev);
664 return NULL;
667 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
669 Object *obj = OBJECT(dev);
670 char *d = NULL;
672 while (!d && obj->parent) {
673 obj = obj->parent;
674 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
676 return d;
679 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
681 Object *obj = OBJECT(dev);
683 return fw_path_provider_try_get_dev_path(obj, bus, dev);
686 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
688 int l = 0;
690 if (dev && dev->parent_bus) {
691 char *d;
692 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
693 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
694 if (!d) {
695 d = bus_get_fw_dev_path(dev->parent_bus, dev);
697 if (d) {
698 l += snprintf(p + l, size - l, "%s", d);
699 g_free(d);
700 } else {
701 return l;
704 l += snprintf(p + l , size - l, "/");
706 return l;
709 char* qdev_get_fw_dev_path(DeviceState *dev)
711 char path[128];
712 int l;
714 l = qdev_get_fw_dev_path_helper(dev, path, 128);
716 path[l-1] = '\0';
718 return g_strdup(path);
721 char *qdev_get_dev_path(DeviceState *dev)
723 BusClass *bc;
725 if (!dev || !dev->parent_bus) {
726 return NULL;
729 bc = BUS_GET_CLASS(dev->parent_bus);
730 if (bc->get_dev_path) {
731 return bc->get_dev_path(dev);
734 return NULL;
738 * Legacy property handling
741 static void qdev_get_legacy_property(Object *obj, Visitor *v,
742 const char *name, void *opaque,
743 Error **errp)
745 DeviceState *dev = DEVICE(obj);
746 Property *prop = opaque;
748 char buffer[1024];
749 char *ptr = buffer;
751 prop->info->print(dev, prop, buffer, sizeof(buffer));
752 visit_type_str(v, name, &ptr, errp);
756 * qdev_property_add_legacy:
757 * @dev: Device to add the property to.
758 * @prop: The qdev property definition.
759 * @errp: location to store error information.
761 * Add a legacy QOM property to @dev for qdev property @prop.
762 * On error, store error in @errp.
764 * Legacy properties are string versions of QOM properties. The format of
765 * the string depends on the property type. Legacy properties are only
766 * needed for "info qtree".
768 * Do not use this is new code! QOM Properties added through this interface
769 * will be given names in the "legacy" namespace.
771 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
772 Error **errp)
774 gchar *name;
776 /* Register pointer properties as legacy properties */
777 if (!prop->info->print && prop->info->get) {
778 return;
781 name = g_strdup_printf("legacy-%s", prop->name);
782 object_property_add(OBJECT(dev), name, "str",
783 prop->info->print ? qdev_get_legacy_property : prop->info->get,
784 NULL,
785 NULL,
786 prop, errp);
788 g_free(name);
792 * qdev_property_add_static:
793 * @dev: Device to add the property to.
794 * @prop: The qdev property definition.
795 * @errp: location to store error information.
797 * Add a static QOM property to @dev for qdev property @prop.
798 * On error, store error in @errp. Static properties access data in a struct.
799 * The type of the QOM property is derived from prop->info.
801 void qdev_property_add_static(DeviceState *dev, Property *prop,
802 Error **errp)
804 Error *local_err = NULL;
805 Object *obj = OBJECT(dev);
808 * TODO qdev_prop_ptr does not have getters or setters. It must
809 * go now that it can be replaced with links. The test should be
810 * removed along with it: all static properties are read/write.
812 if (!prop->info->get && !prop->info->set) {
813 return;
816 object_property_add(obj, prop->name, prop->info->name,
817 prop->info->get, prop->info->set,
818 prop->info->release,
819 prop, &local_err);
821 if (local_err) {
822 error_propagate(errp, local_err);
823 return;
826 object_property_set_description(obj, prop->name,
827 prop->info->description,
828 &error_abort);
830 if (prop->qtype == QTYPE_NONE) {
831 return;
834 if (prop->qtype == QTYPE_QBOOL) {
835 object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
836 } else if (prop->info->enum_table) {
837 object_property_set_str(obj, prop->info->enum_table[prop->defval],
838 prop->name, &error_abort);
839 } else if (prop->qtype == QTYPE_QINT) {
840 object_property_set_int(obj, prop->defval, prop->name, &error_abort);
844 /* @qdev_alias_all_properties - Add alias properties to the source object for
845 * all qdev properties on the target DeviceState.
847 void qdev_alias_all_properties(DeviceState *target, Object *source)
849 ObjectClass *class;
850 Property *prop;
852 class = object_get_class(OBJECT(target));
853 do {
854 DeviceClass *dc = DEVICE_CLASS(class);
856 for (prop = dc->props; prop && prop->name; prop++) {
857 object_property_add_alias(source, prop->name,
858 OBJECT(target), prop->name,
859 &error_abort);
861 class = object_class_get_parent(class);
862 } while (class != object_class_by_name(TYPE_DEVICE));
865 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
867 GSList **list = opaque;
868 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
869 TYPE_DEVICE);
871 if (dev == NULL) {
872 return 0;
875 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
876 *list = g_slist_append(*list, dev);
879 return 0;
882 GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
884 GSList *list = NULL;
886 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
888 return list;
891 static bool device_get_realized(Object *obj, Error **errp)
893 DeviceState *dev = DEVICE(obj);
894 return dev->realized;
897 static void device_set_realized(Object *obj, bool value, Error **errp)
899 DeviceState *dev = DEVICE(obj);
900 DeviceClass *dc = DEVICE_GET_CLASS(dev);
901 HotplugHandler *hotplug_ctrl;
902 BusState *bus;
903 Error *local_err = NULL;
904 bool unattached_parent = false;
905 static int unattached_count;
907 if (dev->hotplugged && !dc->hotpluggable) {
908 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
909 return;
912 if (value && !dev->realized) {
913 if (!obj->parent) {
914 gchar *name = g_strdup_printf("device[%d]", unattached_count++);
916 object_property_add_child(container_get(qdev_get_machine(),
917 "/unattached"),
918 name, obj, &error_abort);
919 unattached_parent = true;
920 g_free(name);
923 hotplug_ctrl = qdev_get_hotplug_handler(dev);
924 if (hotplug_ctrl) {
925 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
926 if (local_err != NULL) {
927 goto fail;
931 if (dc->realize) {
932 dc->realize(dev, &local_err);
935 if (local_err != NULL) {
936 goto fail;
939 DEVICE_LISTENER_CALL(realize, Forward, dev);
941 if (hotplug_ctrl) {
942 hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
945 if (local_err != NULL) {
946 goto post_realize_fail;
949 if (qdev_get_vmsd(dev)) {
950 if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
951 dev->instance_id_alias,
952 dev->alias_required_for_version,
953 &local_err) < 0) {
954 goto post_realize_fail;
958 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
959 object_property_set_bool(OBJECT(bus), true, "realized",
960 &local_err);
961 if (local_err != NULL) {
962 goto child_realize_fail;
965 if (dev->hotplugged) {
966 device_reset(dev);
968 dev->pending_deleted_event = false;
969 } else if (!value && dev->realized) {
970 Error **local_errp = NULL;
971 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
972 local_errp = local_err ? NULL : &local_err;
973 object_property_set_bool(OBJECT(bus), false, "realized",
974 local_errp);
976 if (qdev_get_vmsd(dev)) {
977 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
979 if (dc->unrealize) {
980 local_errp = local_err ? NULL : &local_err;
981 dc->unrealize(dev, local_errp);
983 dev->pending_deleted_event = true;
984 DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
987 if (local_err != NULL) {
988 goto fail;
991 dev->realized = value;
992 return;
994 child_realize_fail:
995 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
996 object_property_set_bool(OBJECT(bus), false, "realized",
997 NULL);
1000 if (qdev_get_vmsd(dev)) {
1001 vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
1004 post_realize_fail:
1005 if (dc->unrealize) {
1006 dc->unrealize(dev, NULL);
1009 fail:
1010 error_propagate(errp, local_err);
1011 if (unattached_parent) {
1012 object_unparent(OBJECT(dev));
1013 unattached_count--;
1017 static bool device_get_hotpluggable(Object *obj, Error **errp)
1019 DeviceClass *dc = DEVICE_GET_CLASS(obj);
1020 DeviceState *dev = DEVICE(obj);
1022 return dc->hotpluggable && (dev->parent_bus == NULL ||
1023 qbus_is_hotpluggable(dev->parent_bus));
1026 static bool device_get_hotplugged(Object *obj, Error **err)
1028 DeviceState *dev = DEVICE(obj);
1030 return dev->hotplugged;
1033 static void device_set_hotplugged(Object *obj, bool value, Error **err)
1035 DeviceState *dev = DEVICE(obj);
1037 dev->hotplugged = value;
1040 static void device_initfn(Object *obj)
1042 DeviceState *dev = DEVICE(obj);
1043 ObjectClass *class;
1044 Property *prop;
1046 if (qdev_hotplug) {
1047 dev->hotplugged = 1;
1048 qdev_hot_added = true;
1051 dev->instance_id_alias = -1;
1052 dev->realized = false;
1054 object_property_add_bool(obj, "realized",
1055 device_get_realized, device_set_realized, NULL);
1056 object_property_add_bool(obj, "hotpluggable",
1057 device_get_hotpluggable, NULL, NULL);
1058 object_property_add_bool(obj, "hotplugged",
1059 device_get_hotplugged, device_set_hotplugged,
1060 &error_abort);
1062 class = object_get_class(OBJECT(dev));
1063 do {
1064 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
1065 qdev_property_add_legacy(dev, prop, &error_abort);
1066 qdev_property_add_static(dev, prop, &error_abort);
1068 class = object_class_get_parent(class);
1069 } while (class != object_class_by_name(TYPE_DEVICE));
1071 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
1072 (Object **)&dev->parent_bus, NULL, 0,
1073 &error_abort);
1074 QLIST_INIT(&dev->gpios);
1077 static void device_post_init(Object *obj)
1079 qdev_prop_set_globals(DEVICE(obj));
1082 /* Unlink device from bus and free the structure. */
1083 static void device_finalize(Object *obj)
1085 NamedGPIOList *ngl, *next;
1087 DeviceState *dev = DEVICE(obj);
1089 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1090 QLIST_REMOVE(ngl, node);
1091 qemu_free_irqs(ngl->in, ngl->num_in);
1092 g_free(ngl->name);
1093 g_free(ngl);
1094 /* ngl->out irqs are owned by the other end and should not be freed
1095 * here
1100 static void device_class_base_init(ObjectClass *class, void *data)
1102 DeviceClass *klass = DEVICE_CLASS(class);
1104 /* We explicitly look up properties in the superclasses,
1105 * so do not propagate them to the subclasses.
1107 klass->props = NULL;
1110 static void device_unparent(Object *obj)
1112 DeviceState *dev = DEVICE(obj);
1113 BusState *bus;
1115 if (dev->realized) {
1116 object_property_set_bool(obj, false, "realized", NULL);
1118 while (dev->num_child_bus) {
1119 bus = QLIST_FIRST(&dev->child_bus);
1120 object_unparent(OBJECT(bus));
1122 if (dev->parent_bus) {
1123 bus_remove_child(dev->parent_bus, dev);
1124 object_unref(OBJECT(dev->parent_bus));
1125 dev->parent_bus = NULL;
1128 /* Only send event if the device had been completely realized */
1129 if (dev->pending_deleted_event) {
1130 gchar *path = object_get_canonical_path(OBJECT(dev));
1132 qapi_event_send_device_deleted(!!dev->id, dev->id, path, &error_abort);
1133 g_free(path);
1136 qemu_opts_del(dev->opts);
1137 dev->opts = NULL;
1140 static void device_class_init(ObjectClass *class, void *data)
1142 DeviceClass *dc = DEVICE_CLASS(class);
1144 class->unparent = device_unparent;
1145 dc->realize = device_realize;
1146 dc->unrealize = device_unrealize;
1148 /* by default all devices were considered as hotpluggable,
1149 * so with intent to check it in generic qdev_unplug() /
1150 * device_set_realized() functions make every device
1151 * hotpluggable. Devices that shouldn't be hotpluggable,
1152 * should override it in their class_init()
1154 dc->hotpluggable = true;
1157 void device_reset(DeviceState *dev)
1159 DeviceClass *klass = DEVICE_GET_CLASS(dev);
1161 if (klass->reset) {
1162 klass->reset(dev);
1166 Object *qdev_get_machine(void)
1168 static Object *dev;
1170 if (dev == NULL) {
1171 dev = container_get(object_get_root(), "/machine");
1174 return dev;
1177 static const TypeInfo device_type_info = {
1178 .name = TYPE_DEVICE,
1179 .parent = TYPE_OBJECT,
1180 .instance_size = sizeof(DeviceState),
1181 .instance_init = device_initfn,
1182 .instance_post_init = device_post_init,
1183 .instance_finalize = device_finalize,
1184 .class_base_init = device_class_base_init,
1185 .class_init = device_class_init,
1186 .abstract = true,
1187 .class_size = sizeof(DeviceClass),
1190 static void qdev_register_types(void)
1192 type_register_static(&device_type_info);
1195 type_init(qdev_register_types)