xref: /openbmc/qemu/hw/core/qdev.c (revision 02e7f85dac3c639b70460ce557cb6c29963db97a)
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 "hw/qdev.h"
29 #include "sysemu/sysemu.h"
30 #include "qapi/error.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/visitor.h"
33 #include "qapi/qmp/qjson.h"
34 #include "monitor/monitor.h"
35 #include "hw/hotplug.h"
36 
37 int qdev_hotplug = 0;
38 static bool qdev_hot_added = false;
39 static bool qdev_hot_removed = false;
40 
41 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
42 {
43     DeviceClass *dc = DEVICE_GET_CLASS(dev);
44     return dc->vmsd;
45 }
46 
47 const char *qdev_fw_name(DeviceState *dev)
48 {
49     DeviceClass *dc = DEVICE_GET_CLASS(dev);
50 
51     if (dc->fw_name) {
52         return dc->fw_name;
53     }
54 
55     return object_get_typename(OBJECT(dev));
56 }
57 
58 static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
59                                      Error **errp);
60 
61 static void bus_remove_child(BusState *bus, DeviceState *child)
62 {
63     BusChild *kid;
64 
65     QTAILQ_FOREACH(kid, &bus->children, sibling) {
66         if (kid->child == child) {
67             char name[32];
68 
69             snprintf(name, sizeof(name), "child[%d]", kid->index);
70             QTAILQ_REMOVE(&bus->children, kid, sibling);
71 
72             /* This gives back ownership of kid->child back to us.  */
73             object_property_del(OBJECT(bus), name, NULL);
74             object_unref(OBJECT(kid->child));
75             g_free(kid);
76             return;
77         }
78     }
79 }
80 
81 static void bus_add_child(BusState *bus, DeviceState *child)
82 {
83     char name[32];
84     BusChild *kid = g_malloc0(sizeof(*kid));
85 
86     if (qdev_hotplug) {
87         assert(bus->allow_hotplug);
88     }
89 
90     kid->index = bus->max_index++;
91     kid->child = child;
92     object_ref(OBJECT(kid->child));
93 
94     QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
95 
96     /* This transfers ownership of kid->child to the property.  */
97     snprintf(name, sizeof(name), "child[%d]", kid->index);
98     object_property_add_link(OBJECT(bus), name,
99                              object_get_typename(OBJECT(child)),
100                              (Object **)&kid->child,
101                              NULL);
102 }
103 
104 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
105 {
106     dev->parent_bus = bus;
107     object_ref(OBJECT(bus));
108     bus_add_child(bus, dev);
109 }
110 
111 /* Create a new device.  This only initializes the device state structure
112    and allows properties to be set.  qdev_init should be called to
113    initialize the actual device emulation.  */
114 DeviceState *qdev_create(BusState *bus, const char *name)
115 {
116     DeviceState *dev;
117 
118     dev = qdev_try_create(bus, name);
119     if (!dev) {
120         if (bus) {
121             error_report("Unknown device '%s' for bus '%s'", name,
122                          object_get_typename(OBJECT(bus)));
123         } else {
124             error_report("Unknown device '%s' for default sysbus", name);
125         }
126         abort();
127     }
128 
129     return dev;
130 }
131 
132 DeviceState *qdev_try_create(BusState *bus, const char *type)
133 {
134     DeviceState *dev;
135 
136     if (object_class_by_name(type) == NULL) {
137         return NULL;
138     }
139     dev = DEVICE(object_new(type));
140     if (!dev) {
141         return NULL;
142     }
143 
144     if (!bus) {
145         bus = sysbus_get_default();
146     }
147 
148     qdev_set_parent_bus(dev, bus);
149     object_unref(OBJECT(dev));
150     return dev;
151 }
152 
153 /* Initialize a device.  Device properties should be set before calling
154    this function.  IRQs and MMIO regions should be connected/mapped after
155    calling this function.
156    On failure, destroy the device and return negative value.
157    Return 0 on success.  */
158 int qdev_init(DeviceState *dev)
159 {
160     Error *local_err = NULL;
161 
162     assert(!dev->realized);
163 
164     object_property_set_bool(OBJECT(dev), true, "realized", &local_err);
165     if (local_err != NULL) {
166         qerror_report_err(local_err);
167         error_free(local_err);
168         object_unparent(OBJECT(dev));
169         return -1;
170     }
171     return 0;
172 }
173 
174 static void device_realize(DeviceState *dev, Error **err)
175 {
176     DeviceClass *dc = DEVICE_GET_CLASS(dev);
177 
178     if (dc->init) {
179         int rc = dc->init(dev);
180         if (rc < 0) {
181             error_setg(err, "Device initialization failed.");
182             return;
183         }
184     }
185 }
186 
187 static void device_unrealize(DeviceState *dev, Error **errp)
188 {
189     DeviceClass *dc = DEVICE_GET_CLASS(dev);
190 
191     if (dc->exit) {
192         int rc = dc->exit(dev);
193         if (rc < 0) {
194             error_setg(errp, "Device exit failed.");
195             return;
196         }
197     }
198 }
199 
200 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
201                                  int required_for_version)
202 {
203     assert(!dev->realized);
204     dev->instance_id_alias = alias_id;
205     dev->alias_required_for_version = required_for_version;
206 }
207 
208 void qdev_unplug(DeviceState *dev, Error **errp)
209 {
210     DeviceClass *dc = DEVICE_GET_CLASS(dev);
211 
212     if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
213         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
214         return;
215     }
216 
217     if (!dc->hotpluggable) {
218         error_set(errp, QERR_DEVICE_NO_HOTPLUG,
219                   object_get_typename(OBJECT(dev)));
220         return;
221     }
222 
223     qdev_hot_removed = true;
224 
225     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
226         hotplug_handler_unplug(dev->parent_bus->hotplug_handler, dev, errp);
227     } else {
228         assert(dc->unplug != NULL);
229         if (dc->unplug(dev) < 0) { /* legacy handler */
230             error_set(errp, QERR_UNDEFINED_ERROR);
231         }
232     }
233 }
234 
235 static int qdev_reset_one(DeviceState *dev, void *opaque)
236 {
237     device_reset(dev);
238 
239     return 0;
240 }
241 
242 static int qbus_reset_one(BusState *bus, void *opaque)
243 {
244     BusClass *bc = BUS_GET_CLASS(bus);
245     if (bc->reset) {
246         bc->reset(bus);
247     }
248     return 0;
249 }
250 
251 void qdev_reset_all(DeviceState *dev)
252 {
253     qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
254 }
255 
256 void qbus_reset_all(BusState *bus)
257 {
258     qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
259 }
260 
261 void qbus_reset_all_fn(void *opaque)
262 {
263     BusState *bus = opaque;
264     qbus_reset_all(bus);
265 }
266 
267 /* can be used as ->unplug() callback for the simple cases */
268 int qdev_simple_unplug_cb(DeviceState *dev)
269 {
270     /* just zap it */
271     object_unparent(OBJECT(dev));
272     return 0;
273 }
274 
275 
276 /* Like qdev_init(), but terminate program via error_report() instead of
277    returning an error value.  This is okay during machine creation.
278    Don't use for hotplug, because there callers need to recover from
279    failure.  Exception: if you know the device's init() callback can't
280    fail, then qdev_init_nofail() can't fail either, and is therefore
281    usable even then.  But relying on the device implementation that
282    way is somewhat unclean, and best avoided.  */
283 void qdev_init_nofail(DeviceState *dev)
284 {
285     const char *typename = object_get_typename(OBJECT(dev));
286 
287     if (qdev_init(dev) < 0) {
288         error_report("Initialization of device %s failed", typename);
289         exit(1);
290     }
291 }
292 
293 void qdev_machine_creation_done(void)
294 {
295     /*
296      * ok, initial machine setup is done, starting from now we can
297      * only create hotpluggable devices
298      */
299     qdev_hotplug = 1;
300 }
301 
302 bool qdev_machine_modified(void)
303 {
304     return qdev_hot_added || qdev_hot_removed;
305 }
306 
307 BusState *qdev_get_parent_bus(DeviceState *dev)
308 {
309     return dev->parent_bus;
310 }
311 
312 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
313 {
314     dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
315                                         dev, n);
316     dev->num_gpio_in += n;
317 }
318 
319 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
320 {
321     assert(dev->num_gpio_out == 0);
322     dev->num_gpio_out = n;
323     dev->gpio_out = pins;
324 }
325 
326 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
327 {
328     assert(n >= 0 && n < dev->num_gpio_in);
329     return dev->gpio_in[n];
330 }
331 
332 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
333 {
334     assert(n >= 0 && n < dev->num_gpio_out);
335     dev->gpio_out[n] = pin;
336 }
337 
338 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
339 {
340     BusState *bus;
341 
342     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
343         if (strcmp(name, bus->name) == 0) {
344             return bus;
345         }
346     }
347     return NULL;
348 }
349 
350 int qbus_walk_children(BusState *bus,
351                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
352                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
353                        void *opaque)
354 {
355     BusChild *kid;
356     int err;
357 
358     if (pre_busfn) {
359         err = pre_busfn(bus, opaque);
360         if (err) {
361             return err;
362         }
363     }
364 
365     QTAILQ_FOREACH(kid, &bus->children, sibling) {
366         err = qdev_walk_children(kid->child,
367                                  pre_devfn, pre_busfn,
368                                  post_devfn, post_busfn, opaque);
369         if (err < 0) {
370             return err;
371         }
372     }
373 
374     if (post_busfn) {
375         err = post_busfn(bus, opaque);
376         if (err) {
377             return err;
378         }
379     }
380 
381     return 0;
382 }
383 
384 int qdev_walk_children(DeviceState *dev,
385                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
386                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
387                        void *opaque)
388 {
389     BusState *bus;
390     int err;
391 
392     if (pre_devfn) {
393         err = pre_devfn(dev, opaque);
394         if (err) {
395             return err;
396         }
397     }
398 
399     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
400         err = qbus_walk_children(bus, pre_devfn, pre_busfn,
401                                  post_devfn, post_busfn, opaque);
402         if (err < 0) {
403             return err;
404         }
405     }
406 
407     if (post_devfn) {
408         err = post_devfn(dev, opaque);
409         if (err) {
410             return err;
411         }
412     }
413 
414     return 0;
415 }
416 
417 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
418 {
419     BusChild *kid;
420     DeviceState *ret;
421     BusState *child;
422 
423     QTAILQ_FOREACH(kid, &bus->children, sibling) {
424         DeviceState *dev = kid->child;
425 
426         if (dev->id && strcmp(dev->id, id) == 0) {
427             return dev;
428         }
429 
430         QLIST_FOREACH(child, &dev->child_bus, sibling) {
431             ret = qdev_find_recursive(child, id);
432             if (ret) {
433                 return ret;
434             }
435         }
436     }
437     return NULL;
438 }
439 
440 static void qbus_realize(BusState *bus, DeviceState *parent, const char *name)
441 {
442     const char *typename = object_get_typename(OBJECT(bus));
443     BusClass *bc;
444     char *buf;
445     int i, len, bus_id;
446 
447     bus->parent = parent;
448 
449     if (name) {
450         bus->name = g_strdup(name);
451     } else if (bus->parent && bus->parent->id) {
452         /* parent device has id -> use it plus parent-bus-id for bus name */
453         bus_id = bus->parent->num_child_bus;
454 
455         len = strlen(bus->parent->id) + 16;
456         buf = g_malloc(len);
457         snprintf(buf, len, "%s.%d", bus->parent->id, bus_id);
458         bus->name = buf;
459     } else {
460         /* no id -> use lowercase bus type plus global bus-id for bus name */
461         bc = BUS_GET_CLASS(bus);
462         bus_id = bc->automatic_ids++;
463 
464         len = strlen(typename) + 16;
465         buf = g_malloc(len);
466         len = snprintf(buf, len, "%s.%d", typename, bus_id);
467         for (i = 0; i < len; i++) {
468             buf[i] = qemu_tolower(buf[i]);
469         }
470         bus->name = buf;
471     }
472 
473     if (bus->parent) {
474         QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
475         bus->parent->num_child_bus++;
476         object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus), NULL);
477         object_unref(OBJECT(bus));
478     } else if (bus != sysbus_get_default()) {
479         /* TODO: once all bus devices are qdevified,
480            only reset handler for main_system_bus should be registered here. */
481         qemu_register_reset(qbus_reset_all_fn, bus);
482     }
483 }
484 
485 static void bus_unparent(Object *obj)
486 {
487     BusState *bus = BUS(obj);
488     BusChild *kid;
489 
490     while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
491         DeviceState *dev = kid->child;
492         object_unparent(OBJECT(dev));
493     }
494     if (bus->parent) {
495         QLIST_REMOVE(bus, sibling);
496         bus->parent->num_child_bus--;
497         bus->parent = NULL;
498     } else {
499         assert(bus != sysbus_get_default()); /* main_system_bus is never freed */
500         qemu_unregister_reset(qbus_reset_all_fn, bus);
501     }
502 }
503 
504 static bool bus_get_realized(Object *obj, Error **err)
505 {
506     BusState *bus = BUS(obj);
507 
508     return bus->realized;
509 }
510 
511 static void bus_set_realized(Object *obj, bool value, Error **err)
512 {
513     BusState *bus = BUS(obj);
514     BusClass *bc = BUS_GET_CLASS(bus);
515     Error *local_err = NULL;
516 
517     if (value && !bus->realized) {
518         if (bc->realize) {
519             bc->realize(bus, &local_err);
520 
521             if (local_err != NULL) {
522                 goto error;
523             }
524 
525         }
526     } else if (!value && bus->realized) {
527         if (bc->unrealize) {
528             bc->unrealize(bus, &local_err);
529 
530             if (local_err != NULL) {
531                 goto error;
532             }
533         }
534     }
535 
536     bus->realized = value;
537     return;
538 
539 error:
540     error_propagate(err, local_err);
541 }
542 
543 void qbus_create_inplace(void *bus, size_t size, const char *typename,
544                          DeviceState *parent, const char *name)
545 {
546     object_initialize(bus, size, typename);
547     qbus_realize(bus, parent, name);
548 }
549 
550 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
551 {
552     BusState *bus;
553 
554     bus = BUS(object_new(typename));
555     qbus_realize(bus, parent, name);
556 
557     return bus;
558 }
559 
560 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
561 {
562     BusClass *bc = BUS_GET_CLASS(bus);
563 
564     if (bc->get_fw_dev_path) {
565         return bc->get_fw_dev_path(dev);
566     }
567 
568     return NULL;
569 }
570 
571 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
572 {
573     int l = 0;
574 
575     if (dev && dev->parent_bus) {
576         char *d;
577         l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
578         d = bus_get_fw_dev_path(dev->parent_bus, dev);
579         if (d) {
580             l += snprintf(p + l, size - l, "%s", d);
581             g_free(d);
582         } else {
583             return l;
584         }
585     }
586     l += snprintf(p + l , size - l, "/");
587 
588     return l;
589 }
590 
591 char* qdev_get_fw_dev_path(DeviceState *dev)
592 {
593     char path[128];
594     int l;
595 
596     l = qdev_get_fw_dev_path_helper(dev, path, 128);
597 
598     path[l-1] = '\0';
599 
600     return g_strdup(path);
601 }
602 
603 char *qdev_get_dev_path(DeviceState *dev)
604 {
605     BusClass *bc;
606 
607     if (!dev || !dev->parent_bus) {
608         return NULL;
609     }
610 
611     bc = BUS_GET_CLASS(dev->parent_bus);
612     if (bc->get_dev_path) {
613         return bc->get_dev_path(dev);
614     }
615 
616     return NULL;
617 }
618 
619 /**
620  * Legacy property handling
621  */
622 
623 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
624                                      const char *name, Error **errp)
625 {
626     DeviceState *dev = DEVICE(obj);
627     Property *prop = opaque;
628 
629     char buffer[1024];
630     char *ptr = buffer;
631 
632     prop->info->print(dev, prop, buffer, sizeof(buffer));
633     visit_type_str(v, &ptr, name, errp);
634 }
635 
636 /**
637  * @qdev_add_legacy_property - adds a legacy property
638  *
639  * Do not use this is new code!  Properties added through this interface will
640  * be given names and types in the "legacy" namespace.
641  *
642  * Legacy properties are string versions of other OOM properties.  The format
643  * of the string depends on the property type.
644  */
645 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
646                               Error **errp)
647 {
648     gchar *name;
649 
650     /* Register pointer properties as legacy properties */
651     if (!prop->info->print && prop->info->get) {
652         return;
653     }
654 
655     name = g_strdup_printf("legacy-%s", prop->name);
656     object_property_add(OBJECT(dev), name, "str",
657                         prop->info->print ? qdev_get_legacy_property : prop->info->get,
658                         NULL,
659                         NULL,
660                         prop, errp);
661 
662     g_free(name);
663 }
664 
665 /**
666  * @qdev_property_add_static - add a @Property to a device.
667  *
668  * Static properties access data in a struct.  The actual type of the
669  * property and the field depends on the property type.
670  */
671 void qdev_property_add_static(DeviceState *dev, Property *prop,
672                               Error **errp)
673 {
674     Error *local_err = NULL;
675     Object *obj = OBJECT(dev);
676 
677     /*
678      * TODO qdev_prop_ptr does not have getters or setters.  It must
679      * go now that it can be replaced with links.  The test should be
680      * removed along with it: all static properties are read/write.
681      */
682     if (!prop->info->get && !prop->info->set) {
683         return;
684     }
685 
686     object_property_add(obj, prop->name, prop->info->name,
687                         prop->info->get, prop->info->set,
688                         prop->info->release,
689                         prop, &local_err);
690 
691     if (local_err) {
692         error_propagate(errp, local_err);
693         return;
694     }
695     if (prop->qtype == QTYPE_NONE) {
696         return;
697     }
698 
699     if (prop->qtype == QTYPE_QBOOL) {
700         object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
701     } else if (prop->info->enum_table) {
702         object_property_set_str(obj, prop->info->enum_table[prop->defval],
703                                 prop->name, &error_abort);
704     } else if (prop->qtype == QTYPE_QINT) {
705         object_property_set_int(obj, prop->defval, prop->name, &error_abort);
706     }
707 }
708 
709 static bool device_get_realized(Object *obj, Error **err)
710 {
711     DeviceState *dev = DEVICE(obj);
712     return dev->realized;
713 }
714 
715 static void device_set_realized(Object *obj, bool value, Error **err)
716 {
717     DeviceState *dev = DEVICE(obj);
718     DeviceClass *dc = DEVICE_GET_CLASS(dev);
719     Error *local_err = NULL;
720 
721     if (dev->hotplugged && !dc->hotpluggable) {
722         error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
723         return;
724     }
725 
726     if (value && !dev->realized) {
727         if (!obj->parent && local_err == NULL) {
728             static int unattached_count;
729             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
730 
731             object_property_add_child(container_get(qdev_get_machine(),
732                                                     "/unattached"),
733                                       name, obj, &local_err);
734             g_free(name);
735         }
736 
737         if (dc->realize) {
738             dc->realize(dev, &local_err);
739         }
740 
741         if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
742             local_err == NULL) {
743             hotplug_handler_plug(dev->parent_bus->hotplug_handler,
744                                  dev, &local_err);
745         }
746 
747         if (qdev_get_vmsd(dev) && local_err == NULL) {
748             vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
749                                            dev->instance_id_alias,
750                                            dev->alias_required_for_version);
751         }
752         if (dev->hotplugged && local_err == NULL) {
753             device_reset(dev);
754         }
755     } else if (!value && dev->realized) {
756         if (qdev_get_vmsd(dev)) {
757             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
758         }
759         if (dc->unrealize) {
760             dc->unrealize(dev, &local_err);
761         }
762     }
763 
764     if (local_err != NULL) {
765         error_propagate(err, local_err);
766         return;
767     }
768 
769     dev->realized = value;
770 }
771 
772 static bool device_get_hotpluggable(Object *obj, Error **err)
773 {
774     DeviceClass *dc = DEVICE_GET_CLASS(obj);
775     DeviceState *dev = DEVICE(obj);
776 
777     return dc->hotpluggable && (dev->parent_bus == NULL ||
778                                 dev->parent_bus->allow_hotplug);
779 }
780 
781 static void device_initfn(Object *obj)
782 {
783     DeviceState *dev = DEVICE(obj);
784     ObjectClass *class;
785     Property *prop;
786 
787     if (qdev_hotplug) {
788         dev->hotplugged = 1;
789         qdev_hot_added = true;
790     }
791 
792     dev->instance_id_alias = -1;
793     dev->realized = false;
794 
795     object_property_add_bool(obj, "realized",
796                              device_get_realized, device_set_realized, NULL);
797     object_property_add_bool(obj, "hotpluggable",
798                              device_get_hotpluggable, NULL, NULL);
799 
800     class = object_get_class(OBJECT(dev));
801     do {
802         for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
803             qdev_property_add_legacy(dev, prop, &error_abort);
804             qdev_property_add_static(dev, prop, &error_abort);
805         }
806         class = object_class_get_parent(class);
807     } while (class != object_class_by_name(TYPE_DEVICE));
808 
809     object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
810                              (Object **)&dev->parent_bus, &error_abort);
811 }
812 
813 static void device_post_init(Object *obj)
814 {
815     qdev_prop_set_globals(DEVICE(obj), &error_abort);
816 }
817 
818 /* Unlink device from bus and free the structure.  */
819 static void device_finalize(Object *obj)
820 {
821     DeviceState *dev = DEVICE(obj);
822     if (dev->opts) {
823         qemu_opts_del(dev->opts);
824     }
825 }
826 
827 static void device_class_base_init(ObjectClass *class, void *data)
828 {
829     DeviceClass *klass = DEVICE_CLASS(class);
830 
831     /* We explicitly look up properties in the superclasses,
832      * so do not propagate them to the subclasses.
833      */
834     klass->props = NULL;
835 }
836 
837 static void device_unparent(Object *obj)
838 {
839     DeviceState *dev = DEVICE(obj);
840     BusState *bus;
841     QObject *event_data;
842     bool have_realized = dev->realized;
843 
844     while (dev->num_child_bus) {
845         bus = QLIST_FIRST(&dev->child_bus);
846         object_unparent(OBJECT(bus));
847     }
848     if (dev->realized) {
849         object_property_set_bool(obj, false, "realized", NULL);
850     }
851     if (dev->parent_bus) {
852         bus_remove_child(dev->parent_bus, dev);
853         object_unref(OBJECT(dev->parent_bus));
854         dev->parent_bus = NULL;
855     }
856 
857     /* Only send event if the device had been completely realized */
858     if (have_realized) {
859         gchar *path = object_get_canonical_path(OBJECT(dev));
860 
861         if (dev->id) {
862             event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
863                                             dev->id, path);
864         } else {
865             event_data = qobject_from_jsonf("{ 'path': %s }", path);
866         }
867         monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
868         qobject_decref(event_data);
869         g_free(path);
870     }
871 }
872 
873 static void device_class_init(ObjectClass *class, void *data)
874 {
875     DeviceClass *dc = DEVICE_CLASS(class);
876 
877     class->unparent = device_unparent;
878     dc->realize = device_realize;
879     dc->unrealize = device_unrealize;
880 
881     /* by default all devices were considered as hotpluggable,
882      * so with intent to check it in generic qdev_unplug() /
883      * device_set_realized() functions make every device
884      * hotpluggable. Devices that shouldn't be hotpluggable,
885      * should override it in their class_init()
886      */
887     dc->hotpluggable = true;
888 }
889 
890 void device_reset(DeviceState *dev)
891 {
892     DeviceClass *klass = DEVICE_GET_CLASS(dev);
893 
894     if (klass->reset) {
895         klass->reset(dev);
896     }
897 }
898 
899 Object *qdev_get_machine(void)
900 {
901     static Object *dev;
902 
903     if (dev == NULL) {
904         dev = container_get(object_get_root(), "/machine");
905     }
906 
907     return dev;
908 }
909 
910 static const TypeInfo device_type_info = {
911     .name = TYPE_DEVICE,
912     .parent = TYPE_OBJECT,
913     .instance_size = sizeof(DeviceState),
914     .instance_init = device_initfn,
915     .instance_post_init = device_post_init,
916     .instance_finalize = device_finalize,
917     .class_base_init = device_class_base_init,
918     .class_init = device_class_init,
919     .abstract = true,
920     .class_size = sizeof(DeviceClass),
921 };
922 
923 static void qbus_initfn(Object *obj)
924 {
925     BusState *bus = BUS(obj);
926 
927     QTAILQ_INIT(&bus->children);
928     object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
929                              TYPE_HOTPLUG_HANDLER,
930                              (Object **)&bus->hotplug_handler, NULL);
931     object_property_add_bool(obj, "realized",
932                              bus_get_realized, bus_set_realized, NULL);
933 }
934 
935 static char *default_bus_get_fw_dev_path(DeviceState *dev)
936 {
937     return g_strdup(object_get_typename(OBJECT(dev)));
938 }
939 
940 static void bus_class_init(ObjectClass *class, void *data)
941 {
942     BusClass *bc = BUS_CLASS(class);
943 
944     class->unparent = bus_unparent;
945     bc->get_fw_dev_path = default_bus_get_fw_dev_path;
946 }
947 
948 static void qbus_finalize(Object *obj)
949 {
950     BusState *bus = BUS(obj);
951 
952     g_free((char *)bus->name);
953 }
954 
955 static const TypeInfo bus_info = {
956     .name = TYPE_BUS,
957     .parent = TYPE_OBJECT,
958     .instance_size = sizeof(BusState),
959     .abstract = true,
960     .class_size = sizeof(BusClass),
961     .instance_init = qbus_initfn,
962     .instance_finalize = qbus_finalize,
963     .class_init = bus_class_init,
964 };
965 
966 static void qdev_register_types(void)
967 {
968     type_register_static(&bus_info);
969     type_register_static(&device_type_info);
970 }
971 
972 type_init(qdev_register_types)
973