xref: /openbmc/qemu/hw/core/qdev.c (revision 07578b0ad6a6792fbe23bcd8477f3331b78f6027)
1 /*
2  *  Dynamic device configuration and creation.
3  *
4  *  Copyright (c) 2009 CodeSourcery
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
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.  */
27 
28 #include "qemu/osdep.h"
29 #include "hw/qdev.h"
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/qapi-events-misc.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/visitor.h"
35 #include "qemu/error-report.h"
36 #include "qemu/option.h"
37 #include "hw/hotplug.h"
38 #include "hw/boards.h"
39 #include "hw/sysbus.h"
40 
41 bool qdev_hotplug = false;
42 static bool qdev_hot_added = false;
43 bool qdev_hot_removed = false;
44 
45 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
46 {
47     DeviceClass *dc = DEVICE_GET_CLASS(dev);
48     return dc->vmsd;
49 }
50 
51 static void bus_remove_child(BusState *bus, DeviceState *child)
52 {
53     BusChild *kid;
54 
55     QTAILQ_FOREACH(kid, &bus->children, sibling) {
56         if (kid->child == child) {
57             char name[32];
58 
59             snprintf(name, sizeof(name), "child[%d]", kid->index);
60             QTAILQ_REMOVE(&bus->children, kid, sibling);
61 
62             bus->num_children--;
63 
64             /* This gives back ownership of kid->child back to us.  */
65             object_property_del(OBJECT(bus), name, NULL);
66             object_unref(OBJECT(kid->child));
67             g_free(kid);
68             return;
69         }
70     }
71 }
72 
73 static void bus_add_child(BusState *bus, DeviceState *child)
74 {
75     char name[32];
76     BusChild *kid = g_malloc0(sizeof(*kid));
77 
78     bus->num_children++;
79     kid->index = bus->max_index++;
80     kid->child = child;
81     object_ref(OBJECT(kid->child));
82 
83     QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
84 
85     /* This transfers ownership of kid->child to the property.  */
86     snprintf(name, sizeof(name), "child[%d]", kid->index);
87     object_property_add_link(OBJECT(bus), name,
88                              object_get_typename(OBJECT(child)),
89                              (Object **)&kid->child,
90                              NULL, /* read-only property */
91                              0, /* return ownership on prop deletion */
92                              NULL);
93 }
94 
95 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
96 {
97     bool replugging = dev->parent_bus != NULL;
98 
99     if (replugging) {
100         /* Keep a reference to the device while it's not plugged into
101          * any bus, to avoid it potentially evaporating when it is
102          * dereffed in bus_remove_child().
103          */
104         object_ref(OBJECT(dev));
105         bus_remove_child(dev->parent_bus, dev);
106         object_unref(OBJECT(dev->parent_bus));
107     }
108     dev->parent_bus = bus;
109     object_ref(OBJECT(bus));
110     bus_add_child(bus, dev);
111     if (replugging) {
112         object_unref(OBJECT(dev));
113     }
114 }
115 
116 /* Create a new device.  This only initializes the device state
117    structure and allows properties to be set.  The device still needs
118    to be realized.  See qdev-core.h.  */
119 DeviceState *qdev_create(BusState *bus, const char *name)
120 {
121     DeviceState *dev;
122 
123     dev = qdev_try_create(bus, name);
124     if (!dev) {
125         if (bus) {
126             error_report("Unknown device '%s' for bus '%s'", name,
127                          object_get_typename(OBJECT(bus)));
128         } else {
129             error_report("Unknown device '%s' for default sysbus", name);
130         }
131         abort();
132     }
133 
134     return dev;
135 }
136 
137 DeviceState *qdev_try_create(BusState *bus, const char *type)
138 {
139     DeviceState *dev;
140 
141     if (object_class_by_name(type) == NULL) {
142         return NULL;
143     }
144     dev = DEVICE(object_new(type));
145     if (!dev) {
146         return NULL;
147     }
148 
149     if (!bus) {
150         /* Assert that the device really is a SysBusDevice before
151          * we put it onto the sysbus. Non-sysbus devices which aren't
152          * being put onto a bus should be created with object_new(TYPE_FOO),
153          * not qdev_create(NULL, TYPE_FOO).
154          */
155         g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
156         bus = sysbus_get_default();
157     }
158 
159     qdev_set_parent_bus(dev, bus);
160     object_unref(OBJECT(dev));
161     return dev;
162 }
163 
164 static QTAILQ_HEAD(, DeviceListener) device_listeners
165     = QTAILQ_HEAD_INITIALIZER(device_listeners);
166 
167 enum ListenerDirection { Forward, Reverse };
168 
169 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...)     \
170     do {                                                          \
171         DeviceListener *_listener;                                \
172                                                                   \
173         switch (_direction) {                                     \
174         case Forward:                                             \
175             QTAILQ_FOREACH(_listener, &device_listeners, link) {  \
176                 if (_listener->_callback) {                       \
177                     _listener->_callback(_listener, ##_args);     \
178                 }                                                 \
179             }                                                     \
180             break;                                                \
181         case Reverse:                                             \
182             QTAILQ_FOREACH_REVERSE(_listener, &device_listeners,  \
183                                    link) {                        \
184                 if (_listener->_callback) {                       \
185                     _listener->_callback(_listener, ##_args);     \
186                 }                                                 \
187             }                                                     \
188             break;                                                \
189         default:                                                  \
190             abort();                                              \
191         }                                                         \
192     } while (0)
193 
194 static int device_listener_add(DeviceState *dev, void *opaque)
195 {
196     DEVICE_LISTENER_CALL(realize, Forward, dev);
197 
198     return 0;
199 }
200 
201 void device_listener_register(DeviceListener *listener)
202 {
203     QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
204 
205     qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
206                        NULL, NULL);
207 }
208 
209 void device_listener_unregister(DeviceListener *listener)
210 {
211     QTAILQ_REMOVE(&device_listeners, listener, link);
212 }
213 
214 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
215                                  int required_for_version)
216 {
217     assert(!dev->realized);
218     dev->instance_id_alias = alias_id;
219     dev->alias_required_for_version = required_for_version;
220 }
221 
222 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
223 {
224     MachineState *machine;
225     MachineClass *mc;
226     Object *m_obj = qdev_get_machine();
227 
228     if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
229         machine = MACHINE(m_obj);
230         mc = MACHINE_GET_CLASS(machine);
231         if (mc->get_hotplug_handler) {
232             return mc->get_hotplug_handler(machine, dev);
233         }
234     }
235 
236     return NULL;
237 }
238 
239 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
240 {
241     HotplugHandler *hotplug_ctrl;
242 
243     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
244         hotplug_ctrl = dev->parent_bus->hotplug_handler;
245     } else {
246         hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
247     }
248     return hotplug_ctrl;
249 }
250 
251 static int qdev_reset_one(DeviceState *dev, void *opaque)
252 {
253     device_reset(dev);
254 
255     return 0;
256 }
257 
258 static int qbus_reset_one(BusState *bus, void *opaque)
259 {
260     BusClass *bc = BUS_GET_CLASS(bus);
261     if (bc->reset) {
262         bc->reset(bus);
263     }
264     return 0;
265 }
266 
267 void qdev_reset_all(DeviceState *dev)
268 {
269     qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
270 }
271 
272 void qdev_reset_all_fn(void *opaque)
273 {
274     qdev_reset_all(DEVICE(opaque));
275 }
276 
277 void qbus_reset_all(BusState *bus)
278 {
279     qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
280 }
281 
282 void qbus_reset_all_fn(void *opaque)
283 {
284     BusState *bus = opaque;
285     qbus_reset_all(bus);
286 }
287 
288 /* can be used as ->unplug() callback for the simple cases */
289 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
290                                   DeviceState *dev, Error **errp)
291 {
292     object_property_set_bool(OBJECT(dev), false, "realized", NULL);
293 }
294 
295 /*
296  * Realize @dev.
297  * Device properties should be set before calling this function.  IRQs
298  * and MMIO regions should be connected/mapped after calling this
299  * function.
300  * On failure, report an error with error_report() and terminate the
301  * program.  This is okay during machine creation.  Don't use for
302  * hotplug, because there callers need to recover from failure.
303  * Exception: if you know the device's init() callback can't fail,
304  * then qdev_init_nofail() can't fail either, and is therefore usable
305  * even then.  But relying on the device implementation that way is
306  * somewhat unclean, and best avoided.
307  */
308 void qdev_init_nofail(DeviceState *dev)
309 {
310     Error *err = NULL;
311 
312     assert(!dev->realized);
313 
314     object_ref(OBJECT(dev));
315     object_property_set_bool(OBJECT(dev), true, "realized", &err);
316     if (err) {
317         error_reportf_err(err, "Initialization of device %s failed: ",
318                           object_get_typename(OBJECT(dev)));
319         exit(1);
320     }
321     object_unref(OBJECT(dev));
322 }
323 
324 void qdev_machine_creation_done(void)
325 {
326     /*
327      * ok, initial machine setup is done, starting from now we can
328      * only create hotpluggable devices
329      */
330     qdev_hotplug = true;
331 }
332 
333 bool qdev_machine_modified(void)
334 {
335     return qdev_hot_added || qdev_hot_removed;
336 }
337 
338 BusState *qdev_get_parent_bus(DeviceState *dev)
339 {
340     return dev->parent_bus;
341 }
342 
343 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev,
344                                                const char *name)
345 {
346     NamedGPIOList *ngl;
347 
348     QLIST_FOREACH(ngl, &dev->gpios, node) {
349         /* NULL is a valid and matchable name, otherwise do a normal
350          * strcmp match.
351          */
352         if ((!ngl->name && !name) ||
353                 (name && ngl->name && strcmp(name, ngl->name) == 0)) {
354             return ngl;
355         }
356     }
357 
358     ngl = g_malloc0(sizeof(*ngl));
359     ngl->name = g_strdup(name);
360     QLIST_INSERT_HEAD(&dev->gpios, ngl, node);
361     return ngl;
362 }
363 
364 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
365                                          qemu_irq_handler handler,
366                                          void *opaque,
367                                          const char *name, int n)
368 {
369     int i;
370     NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
371 
372     assert(gpio_list->num_out == 0 || !name);
373     gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
374                                      opaque, n);
375 
376     if (!name) {
377         name = "unnamed-gpio-in";
378     }
379     for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
380         gchar *propname = g_strdup_printf("%s[%u]", name, i);
381 
382         object_property_add_child(OBJECT(dev), propname,
383                                   OBJECT(gpio_list->in[i]), &error_abort);
384         g_free(propname);
385     }
386 
387     gpio_list->num_in += n;
388 }
389 
390 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
391 {
392     qdev_init_gpio_in_named(dev, handler, NULL, n);
393 }
394 
395 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
396                               const char *name, int n)
397 {
398     int i;
399     NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
400 
401     assert(gpio_list->num_in == 0 || !name);
402 
403     if (!name) {
404         name = "unnamed-gpio-out";
405     }
406     memset(pins, 0, sizeof(*pins) * n);
407     for (i = 0; i < n; ++i) {
408         gchar *propname = g_strdup_printf("%s[%u]", name,
409                                           gpio_list->num_out + i);
410 
411         object_property_add_link(OBJECT(dev), propname, TYPE_IRQ,
412                                  (Object **)&pins[i],
413                                  object_property_allow_set_link,
414                                  OBJ_PROP_LINK_STRONG,
415                                  &error_abort);
416         g_free(propname);
417     }
418     gpio_list->num_out += n;
419 }
420 
421 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
422 {
423     qdev_init_gpio_out_named(dev, pins, NULL, n);
424 }
425 
426 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n)
427 {
428     NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
429 
430     assert(n >= 0 && n < gpio_list->num_in);
431     return gpio_list->in[n];
432 }
433 
434 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
435 {
436     return qdev_get_gpio_in_named(dev, NULL, n);
437 }
438 
439 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
440                                  qemu_irq pin)
441 {
442     char *propname = g_strdup_printf("%s[%d]",
443                                      name ? name : "unnamed-gpio-out", n);
444     if (pin) {
445         /* We need a name for object_property_set_link to work.  If the
446          * object has a parent, object_property_add_child will come back
447          * with an error without doing anything.  If it has none, it will
448          * never fail.  So we can just call it with a NULL Error pointer.
449          */
450         object_property_add_child(container_get(qdev_get_machine(),
451                                                 "/unattached"),
452                                   "non-qdev-gpio[*]", OBJECT(pin), NULL);
453     }
454     object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort);
455     g_free(propname);
456 }
457 
458 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n)
459 {
460     char *propname = g_strdup_printf("%s[%d]",
461                                      name ? name : "unnamed-gpio-out", n);
462 
463     qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
464                                                       NULL);
465 
466     return ret;
467 }
468 
469 /* disconnect a GPIO output, returning the disconnected input (if any) */
470 
471 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev,
472                                                const char *name, int n)
473 {
474     char *propname = g_strdup_printf("%s[%d]",
475                                      name ? name : "unnamed-gpio-out", n);
476 
477     qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname,
478                                                       NULL);
479     if (ret) {
480         object_property_set_link(OBJECT(dev), NULL, propname, NULL);
481     }
482     g_free(propname);
483     return ret;
484 }
485 
486 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
487                                  const char *name, int n)
488 {
489     qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n);
490     qdev_connect_gpio_out_named(dev, name, n, icpt);
491     return disconnected;
492 }
493 
494 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
495 {
496     qdev_connect_gpio_out_named(dev, NULL, n, pin);
497 }
498 
499 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
500                      const char *name)
501 {
502     int i;
503     NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name);
504 
505     for (i = 0; i < ngl->num_in; i++) {
506         const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in";
507         char *propname = g_strdup_printf("%s[%d]", nm, i);
508 
509         object_property_add_alias(OBJECT(container), propname,
510                                   OBJECT(dev), propname,
511                                   &error_abort);
512         g_free(propname);
513     }
514     for (i = 0; i < ngl->num_out; i++) {
515         const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
516         char *propname = g_strdup_printf("%s[%d]", nm, i);
517 
518         object_property_add_alias(OBJECT(container), propname,
519                                   OBJECT(dev), propname,
520                                   &error_abort);
521         g_free(propname);
522     }
523     QLIST_REMOVE(ngl, node);
524     QLIST_INSERT_HEAD(&container->gpios, ngl, node);
525 }
526 
527 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
528 {
529     BusState *bus;
530     Object *child = object_resolve_path_component(OBJECT(dev), name);
531 
532     bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
533     if (bus) {
534         return bus;
535     }
536 
537     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
538         if (strcmp(name, bus->name) == 0) {
539             return bus;
540         }
541     }
542     return NULL;
543 }
544 
545 int qdev_walk_children(DeviceState *dev,
546                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
547                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
548                        void *opaque)
549 {
550     BusState *bus;
551     int err;
552 
553     if (pre_devfn) {
554         err = pre_devfn(dev, opaque);
555         if (err) {
556             return err;
557         }
558     }
559 
560     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
561         err = qbus_walk_children(bus, pre_devfn, pre_busfn,
562                                  post_devfn, post_busfn, opaque);
563         if (err < 0) {
564             return err;
565         }
566     }
567 
568     if (post_devfn) {
569         err = post_devfn(dev, opaque);
570         if (err) {
571             return err;
572         }
573     }
574 
575     return 0;
576 }
577 
578 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
579 {
580     BusChild *kid;
581     DeviceState *ret;
582     BusState *child;
583 
584     QTAILQ_FOREACH(kid, &bus->children, sibling) {
585         DeviceState *dev = kid->child;
586 
587         if (dev->id && strcmp(dev->id, id) == 0) {
588             return dev;
589         }
590 
591         QLIST_FOREACH(child, &dev->child_bus, sibling) {
592             ret = qdev_find_recursive(child, id);
593             if (ret) {
594                 return ret;
595             }
596         }
597     }
598     return NULL;
599 }
600 
601 char *qdev_get_dev_path(DeviceState *dev)
602 {
603     BusClass *bc;
604 
605     if (!dev || !dev->parent_bus) {
606         return NULL;
607     }
608 
609     bc = BUS_GET_CLASS(dev->parent_bus);
610     if (bc->get_dev_path) {
611         return bc->get_dev_path(dev);
612     }
613 
614     return NULL;
615 }
616 
617 /**
618  * Legacy property handling
619  */
620 
621 static void qdev_get_legacy_property(Object *obj, Visitor *v,
622                                      const char *name, void *opaque,
623                                      Error **errp)
624 {
625     DeviceState *dev = DEVICE(obj);
626     Property *prop = opaque;
627 
628     char buffer[1024];
629     char *ptr = buffer;
630 
631     prop->info->print(dev, prop, buffer, sizeof(buffer));
632     visit_type_str(v, name, &ptr, errp);
633 }
634 
635 /**
636  * qdev_property_add_legacy:
637  * @dev: Device to add the property to.
638  * @prop: The qdev property definition.
639  * @errp: location to store error information.
640  *
641  * Add a legacy QOM property to @dev for qdev property @prop.
642  * On error, store error in @errp.
643  *
644  * Legacy properties are string versions of QOM properties.  The format of
645  * the string depends on the property type.  Legacy properties are only
646  * needed for "info qtree".
647  *
648  * Do not use this in new code!  QOM Properties added through this interface
649  * will be given names in the "legacy" namespace.
650  */
651 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
652                                      Error **errp)
653 {
654     gchar *name;
655 
656     /* Register pointer properties as legacy properties */
657     if (!prop->info->print && prop->info->get) {
658         return;
659     }
660 
661     if (prop->info->create) {
662         return;
663     }
664 
665     name = g_strdup_printf("legacy-%s", prop->name);
666     object_property_add(OBJECT(dev), name, "str",
667                         prop->info->print ? qdev_get_legacy_property : prop->info->get,
668                         NULL,
669                         NULL,
670                         prop, errp);
671 
672     g_free(name);
673 }
674 
675 /**
676  * qdev_property_add_static:
677  * @dev: Device to add the property to.
678  * @prop: The qdev property definition.
679  * @errp: location to store error information.
680  *
681  * Add a static QOM property to @dev for qdev property @prop.
682  * On error, store error in @errp.  Static properties access data in a struct.
683  * The type of the QOM property is derived from prop->info.
684  */
685 void qdev_property_add_static(DeviceState *dev, Property *prop,
686                               Error **errp)
687 {
688     Error *local_err = NULL;
689     Object *obj = OBJECT(dev);
690 
691     if (prop->info->create) {
692         prop->info->create(obj, prop, &local_err);
693     } else {
694         /*
695          * TODO qdev_prop_ptr does not have getters or setters.  It must
696          * go now that it can be replaced with links.  The test should be
697          * removed along with it: all static properties are read/write.
698          */
699         if (!prop->info->get && !prop->info->set) {
700             return;
701         }
702         object_property_add(obj, prop->name, prop->info->name,
703                             prop->info->get, prop->info->set,
704                             prop->info->release,
705                             prop, &local_err);
706     }
707 
708     if (local_err) {
709         error_propagate(errp, local_err);
710         return;
711     }
712 
713     object_property_set_description(obj, prop->name,
714                                     prop->info->description,
715                                     &error_abort);
716 
717     if (prop->set_default) {
718         prop->info->set_default_value(obj, prop);
719     }
720 }
721 
722 /* @qdev_alias_all_properties - Add alias properties to the source object for
723  * all qdev properties on the target DeviceState.
724  */
725 void qdev_alias_all_properties(DeviceState *target, Object *source)
726 {
727     ObjectClass *class;
728     Property *prop;
729 
730     class = object_get_class(OBJECT(target));
731     do {
732         DeviceClass *dc = DEVICE_CLASS(class);
733 
734         for (prop = dc->props; prop && prop->name; prop++) {
735             object_property_add_alias(source, prop->name,
736                                       OBJECT(target), prop->name,
737                                       &error_abort);
738         }
739         class = object_class_get_parent(class);
740     } while (class != object_class_by_name(TYPE_DEVICE));
741 }
742 
743 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
744 {
745     GSList **list = opaque;
746     DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
747                                                           TYPE_DEVICE);
748 
749     if (dev == NULL) {
750         return 0;
751     }
752 
753     if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
754         *list = g_slist_append(*list, dev);
755     }
756 
757     return 0;
758 }
759 
760 GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
761 {
762     GSList *list = NULL;
763 
764     object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
765 
766     return list;
767 }
768 
769 static bool device_get_realized(Object *obj, Error **errp)
770 {
771     DeviceState *dev = DEVICE(obj);
772     return dev->realized;
773 }
774 
775 static bool check_only_migratable(Object *obj, Error **err)
776 {
777     DeviceClass *dc = DEVICE_GET_CLASS(obj);
778 
779     if (!vmstate_check_only_migratable(dc->vmsd)) {
780         error_setg(err, "Device %s is not migratable, but "
781                    "--only-migratable was specified",
782                    object_get_typename(obj));
783         return false;
784     }
785 
786     return true;
787 }
788 
789 static void device_set_realized(Object *obj, bool value, Error **errp)
790 {
791     DeviceState *dev = DEVICE(obj);
792     DeviceClass *dc = DEVICE_GET_CLASS(dev);
793     HotplugHandler *hotplug_ctrl;
794     BusState *bus;
795     Error *local_err = NULL;
796     bool unattached_parent = false;
797     static int unattached_count;
798 
799     if (dev->hotplugged && !dc->hotpluggable) {
800         error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
801         return;
802     }
803 
804     if (value && !dev->realized) {
805         if (!check_only_migratable(obj, &local_err)) {
806             goto fail;
807         }
808 
809         if (!obj->parent) {
810             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
811 
812             object_property_add_child(container_get(qdev_get_machine(),
813                                                     "/unattached"),
814                                       name, obj, &error_abort);
815             unattached_parent = true;
816             g_free(name);
817         }
818 
819         hotplug_ctrl = qdev_get_hotplug_handler(dev);
820         if (hotplug_ctrl) {
821             hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
822             if (local_err != NULL) {
823                 goto fail;
824             }
825         }
826 
827         if (dc->realize) {
828             dc->realize(dev, &local_err);
829         }
830 
831         if (local_err != NULL) {
832             goto fail;
833         }
834 
835         DEVICE_LISTENER_CALL(realize, Forward, dev);
836 
837         /*
838          * always free/re-initialize here since the value cannot be cleaned up
839          * in device_unrealize due to its usage later on in the unplug path
840          */
841         g_free(dev->canonical_path);
842         dev->canonical_path = object_get_canonical_path(OBJECT(dev));
843 
844         if (qdev_get_vmsd(dev)) {
845             if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
846                                                dev->instance_id_alias,
847                                                dev->alias_required_for_version,
848                                                &local_err) < 0) {
849                 goto post_realize_fail;
850             }
851         }
852 
853         QLIST_FOREACH(bus, &dev->child_bus, sibling) {
854             object_property_set_bool(OBJECT(bus), true, "realized",
855                                          &local_err);
856             if (local_err != NULL) {
857                 goto child_realize_fail;
858             }
859         }
860         if (dev->hotplugged) {
861             device_reset(dev);
862         }
863         dev->pending_deleted_event = false;
864 
865         if (hotplug_ctrl) {
866             hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
867             if (local_err != NULL) {
868                 goto child_realize_fail;
869             }
870        }
871 
872     } else if (!value && dev->realized) {
873         Error **local_errp = NULL;
874         QLIST_FOREACH(bus, &dev->child_bus, sibling) {
875             local_errp = local_err ? NULL : &local_err;
876             object_property_set_bool(OBJECT(bus), false, "realized",
877                                      local_errp);
878         }
879         if (qdev_get_vmsd(dev)) {
880             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
881         }
882         if (dc->unrealize) {
883             local_errp = local_err ? NULL : &local_err;
884             dc->unrealize(dev, local_errp);
885         }
886         dev->pending_deleted_event = true;
887         DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
888     }
889 
890     if (local_err != NULL) {
891         goto fail;
892     }
893 
894     dev->realized = value;
895     return;
896 
897 child_realize_fail:
898     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
899         object_property_set_bool(OBJECT(bus), false, "realized",
900                                  NULL);
901     }
902 
903     if (qdev_get_vmsd(dev)) {
904         vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
905     }
906 
907 post_realize_fail:
908     g_free(dev->canonical_path);
909     dev->canonical_path = NULL;
910     if (dc->unrealize) {
911         dc->unrealize(dev, NULL);
912     }
913 
914 fail:
915     error_propagate(errp, local_err);
916     if (unattached_parent) {
917         object_unparent(OBJECT(dev));
918         unattached_count--;
919     }
920 }
921 
922 static bool device_get_hotpluggable(Object *obj, Error **errp)
923 {
924     DeviceClass *dc = DEVICE_GET_CLASS(obj);
925     DeviceState *dev = DEVICE(obj);
926 
927     return dc->hotpluggable && (dev->parent_bus == NULL ||
928                                 qbus_is_hotpluggable(dev->parent_bus));
929 }
930 
931 static bool device_get_hotplugged(Object *obj, Error **err)
932 {
933     DeviceState *dev = DEVICE(obj);
934 
935     return dev->hotplugged;
936 }
937 
938 static void device_initfn(Object *obj)
939 {
940     DeviceState *dev = DEVICE(obj);
941     ObjectClass *class;
942     Property *prop;
943 
944     if (qdev_hotplug) {
945         dev->hotplugged = 1;
946         qdev_hot_added = true;
947     }
948 
949     dev->instance_id_alias = -1;
950     dev->realized = false;
951 
952     object_property_add_bool(obj, "realized",
953                              device_get_realized, device_set_realized, NULL);
954     object_property_add_bool(obj, "hotpluggable",
955                              device_get_hotpluggable, NULL, NULL);
956     object_property_add_bool(obj, "hotplugged",
957                              device_get_hotplugged, NULL,
958                              &error_abort);
959 
960     class = object_get_class(OBJECT(dev));
961     do {
962         for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
963             qdev_property_add_legacy(dev, prop, &error_abort);
964             qdev_property_add_static(dev, prop, &error_abort);
965         }
966         class = object_class_get_parent(class);
967     } while (class != object_class_by_name(TYPE_DEVICE));
968 
969     object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
970                              (Object **)&dev->parent_bus, NULL, 0,
971                              &error_abort);
972     QLIST_INIT(&dev->gpios);
973 }
974 
975 void object_apply_compat_props(Object *obj)
976 {
977     if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
978         MachineState *m = MACHINE(qdev_get_machine());
979         MachineClass *mc = MACHINE_GET_CLASS(m);
980 
981         if (m->accelerator) {
982             AccelClass *ac = ACCEL_GET_CLASS(m->accelerator);
983 
984             if (ac->compat_props) {
985                 object_apply_global_props(obj, ac->compat_props, &error_abort);
986             }
987         }
988         object_apply_global_props(obj, mc->compat_props, &error_abort);
989     }
990 }
991 
992 static void device_post_init(Object *obj)
993 {
994     object_apply_compat_props(obj);
995     qdev_prop_set_globals(DEVICE(obj));
996 }
997 
998 /* Unlink device from bus and free the structure.  */
999 static void device_finalize(Object *obj)
1000 {
1001     NamedGPIOList *ngl, *next;
1002 
1003     DeviceState *dev = DEVICE(obj);
1004 
1005     QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
1006         QLIST_REMOVE(ngl, node);
1007         qemu_free_irqs(ngl->in, ngl->num_in);
1008         g_free(ngl->name);
1009         g_free(ngl);
1010         /* ngl->out irqs are owned by the other end and should not be freed
1011          * here
1012          */
1013     }
1014 
1015     /* Only send event if the device had been completely realized */
1016     if (dev->pending_deleted_event) {
1017         g_assert(dev->canonical_path);
1018 
1019         qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path);
1020         g_free(dev->canonical_path);
1021         dev->canonical_path = NULL;
1022     }
1023 
1024     qemu_opts_del(dev->opts);
1025 }
1026 
1027 static void device_class_base_init(ObjectClass *class, void *data)
1028 {
1029     DeviceClass *klass = DEVICE_CLASS(class);
1030 
1031     /* We explicitly look up properties in the superclasses,
1032      * so do not propagate them to the subclasses.
1033      */
1034     klass->props = NULL;
1035 }
1036 
1037 static void device_unparent(Object *obj)
1038 {
1039     DeviceState *dev = DEVICE(obj);
1040     BusState *bus;
1041 
1042     if (dev->realized) {
1043         object_property_set_bool(obj, false, "realized", NULL);
1044     }
1045     while (dev->num_child_bus) {
1046         bus = QLIST_FIRST(&dev->child_bus);
1047         object_unparent(OBJECT(bus));
1048     }
1049     if (dev->parent_bus) {
1050         bus_remove_child(dev->parent_bus, dev);
1051         object_unref(OBJECT(dev->parent_bus));
1052         dev->parent_bus = NULL;
1053     }
1054 }
1055 
1056 static void device_class_init(ObjectClass *class, void *data)
1057 {
1058     DeviceClass *dc = DEVICE_CLASS(class);
1059 
1060     class->unparent = device_unparent;
1061 
1062     /* by default all devices were considered as hotpluggable,
1063      * so with intent to check it in generic qdev_unplug() /
1064      * device_set_realized() functions make every device
1065      * hotpluggable. Devices that shouldn't be hotpluggable,
1066      * should override it in their class_init()
1067      */
1068     dc->hotpluggable = true;
1069     dc->user_creatable = true;
1070 }
1071 
1072 void device_class_set_parent_reset(DeviceClass *dc,
1073                                    DeviceReset dev_reset,
1074                                    DeviceReset *parent_reset)
1075 {
1076     *parent_reset = dc->reset;
1077     dc->reset = dev_reset;
1078 }
1079 
1080 void device_class_set_parent_realize(DeviceClass *dc,
1081                                      DeviceRealize dev_realize,
1082                                      DeviceRealize *parent_realize)
1083 {
1084     *parent_realize = dc->realize;
1085     dc->realize = dev_realize;
1086 }
1087 
1088 void device_class_set_parent_unrealize(DeviceClass *dc,
1089                                        DeviceUnrealize dev_unrealize,
1090                                        DeviceUnrealize *parent_unrealize)
1091 {
1092     *parent_unrealize = dc->unrealize;
1093     dc->unrealize = dev_unrealize;
1094 }
1095 
1096 void device_reset(DeviceState *dev)
1097 {
1098     DeviceClass *klass = DEVICE_GET_CLASS(dev);
1099 
1100     if (klass->reset) {
1101         klass->reset(dev);
1102     }
1103 }
1104 
1105 Object *qdev_get_machine(void)
1106 {
1107     static Object *dev;
1108 
1109     if (dev == NULL) {
1110         dev = container_get(object_get_root(), "/machine");
1111     }
1112 
1113     return dev;
1114 }
1115 
1116 static const TypeInfo device_type_info = {
1117     .name = TYPE_DEVICE,
1118     .parent = TYPE_OBJECT,
1119     .instance_size = sizeof(DeviceState),
1120     .instance_init = device_initfn,
1121     .instance_post_init = device_post_init,
1122     .instance_finalize = device_finalize,
1123     .class_base_init = device_class_base_init,
1124     .class_init = device_class_init,
1125     .abstract = true,
1126     .class_size = sizeof(DeviceClass),
1127 };
1128 
1129 static void qdev_register_types(void)
1130 {
1131     type_register_static(&device_type_info);
1132 }
1133 
1134 type_init(qdev_register_types)
1135