xref: /openbmc/qemu/hw/core/qdev.c (revision 0399a381)
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 void qbus_create_inplace(void *bus, size_t size, const char *typename,
505                          DeviceState *parent, const char *name)
506 {
507     object_initialize(bus, size, typename);
508     qbus_realize(bus, parent, name);
509 }
510 
511 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
512 {
513     BusState *bus;
514 
515     bus = BUS(object_new(typename));
516     qbus_realize(bus, parent, name);
517 
518     return bus;
519 }
520 
521 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
522 {
523     BusClass *bc = BUS_GET_CLASS(bus);
524 
525     if (bc->get_fw_dev_path) {
526         return bc->get_fw_dev_path(dev);
527     }
528 
529     return NULL;
530 }
531 
532 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
533 {
534     int l = 0;
535 
536     if (dev && dev->parent_bus) {
537         char *d;
538         l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
539         d = bus_get_fw_dev_path(dev->parent_bus, dev);
540         if (d) {
541             l += snprintf(p + l, size - l, "%s", d);
542             g_free(d);
543         } else {
544             return l;
545         }
546     }
547     l += snprintf(p + l , size - l, "/");
548 
549     return l;
550 }
551 
552 char* qdev_get_fw_dev_path(DeviceState *dev)
553 {
554     char path[128];
555     int l;
556 
557     l = qdev_get_fw_dev_path_helper(dev, path, 128);
558 
559     path[l-1] = '\0';
560 
561     return g_strdup(path);
562 }
563 
564 char *qdev_get_dev_path(DeviceState *dev)
565 {
566     BusClass *bc;
567 
568     if (!dev || !dev->parent_bus) {
569         return NULL;
570     }
571 
572     bc = BUS_GET_CLASS(dev->parent_bus);
573     if (bc->get_dev_path) {
574         return bc->get_dev_path(dev);
575     }
576 
577     return NULL;
578 }
579 
580 /**
581  * Legacy property handling
582  */
583 
584 static void qdev_get_legacy_property(Object *obj, Visitor *v, void *opaque,
585                                      const char *name, Error **errp)
586 {
587     DeviceState *dev = DEVICE(obj);
588     Property *prop = opaque;
589 
590     char buffer[1024];
591     char *ptr = buffer;
592 
593     prop->info->print(dev, prop, buffer, sizeof(buffer));
594     visit_type_str(v, &ptr, name, errp);
595 }
596 
597 /**
598  * @qdev_add_legacy_property - adds a legacy property
599  *
600  * Do not use this is new code!  Properties added through this interface will
601  * be given names and types in the "legacy" namespace.
602  *
603  * Legacy properties are string versions of other OOM properties.  The format
604  * of the string depends on the property type.
605  */
606 void qdev_property_add_legacy(DeviceState *dev, Property *prop,
607                               Error **errp)
608 {
609     gchar *name;
610 
611     /* Register pointer properties as legacy properties */
612     if (!prop->info->print && prop->info->get) {
613         return;
614     }
615 
616     name = g_strdup_printf("legacy-%s", prop->name);
617     object_property_add(OBJECT(dev), name, "str",
618                         prop->info->print ? qdev_get_legacy_property : prop->info->get,
619                         NULL,
620                         NULL,
621                         prop, errp);
622 
623     g_free(name);
624 }
625 
626 /**
627  * @qdev_property_add_static - add a @Property to a device.
628  *
629  * Static properties access data in a struct.  The actual type of the
630  * property and the field depends on the property type.
631  */
632 void qdev_property_add_static(DeviceState *dev, Property *prop,
633                               Error **errp)
634 {
635     Error *local_err = NULL;
636     Object *obj = OBJECT(dev);
637 
638     /*
639      * TODO qdev_prop_ptr does not have getters or setters.  It must
640      * go now that it can be replaced with links.  The test should be
641      * removed along with it: all static properties are read/write.
642      */
643     if (!prop->info->get && !prop->info->set) {
644         return;
645     }
646 
647     object_property_add(obj, prop->name, prop->info->name,
648                         prop->info->get, prop->info->set,
649                         prop->info->release,
650                         prop, &local_err);
651 
652     if (local_err) {
653         error_propagate(errp, local_err);
654         return;
655     }
656     if (prop->qtype == QTYPE_NONE) {
657         return;
658     }
659 
660     if (prop->qtype == QTYPE_QBOOL) {
661         object_property_set_bool(obj, prop->defval, prop->name, &error_abort);
662     } else if (prop->info->enum_table) {
663         object_property_set_str(obj, prop->info->enum_table[prop->defval],
664                                 prop->name, &error_abort);
665     } else if (prop->qtype == QTYPE_QINT) {
666         object_property_set_int(obj, prop->defval, prop->name, &error_abort);
667     }
668 }
669 
670 static bool device_get_realized(Object *obj, Error **err)
671 {
672     DeviceState *dev = DEVICE(obj);
673     return dev->realized;
674 }
675 
676 static void device_set_realized(Object *obj, bool value, Error **err)
677 {
678     DeviceState *dev = DEVICE(obj);
679     DeviceClass *dc = DEVICE_GET_CLASS(dev);
680     Error *local_err = NULL;
681 
682     if (dev->hotplugged && !dc->hotpluggable) {
683         error_set(err, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj));
684         return;
685     }
686 
687     if (value && !dev->realized) {
688         if (!obj->parent && local_err == NULL) {
689             static int unattached_count;
690             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
691 
692             object_property_add_child(container_get(qdev_get_machine(),
693                                                     "/unattached"),
694                                       name, obj, &local_err);
695             g_free(name);
696         }
697 
698         if (dc->realize) {
699             dc->realize(dev, &local_err);
700         }
701 
702         if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
703             local_err == NULL) {
704             hotplug_handler_plug(dev->parent_bus->hotplug_handler,
705                                  dev, &local_err);
706         }
707 
708         if (qdev_get_vmsd(dev) && local_err == NULL) {
709             vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
710                                            dev->instance_id_alias,
711                                            dev->alias_required_for_version);
712         }
713         if (dev->hotplugged && local_err == NULL) {
714             device_reset(dev);
715         }
716     } else if (!value && dev->realized) {
717         if (qdev_get_vmsd(dev)) {
718             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
719         }
720         if (dc->unrealize) {
721             dc->unrealize(dev, &local_err);
722         }
723     }
724 
725     if (local_err != NULL) {
726         error_propagate(err, local_err);
727         return;
728     }
729 
730     dev->realized = value;
731 }
732 
733 static bool device_get_hotpluggable(Object *obj, Error **err)
734 {
735     DeviceClass *dc = DEVICE_GET_CLASS(obj);
736     DeviceState *dev = DEVICE(obj);
737 
738     return dc->hotpluggable && (dev->parent_bus == NULL ||
739                                 dev->parent_bus->allow_hotplug);
740 }
741 
742 static void device_initfn(Object *obj)
743 {
744     DeviceState *dev = DEVICE(obj);
745     ObjectClass *class;
746     Property *prop;
747 
748     if (qdev_hotplug) {
749         dev->hotplugged = 1;
750         qdev_hot_added = true;
751     }
752 
753     dev->instance_id_alias = -1;
754     dev->realized = false;
755 
756     object_property_add_bool(obj, "realized",
757                              device_get_realized, device_set_realized, NULL);
758     object_property_add_bool(obj, "hotpluggable",
759                              device_get_hotpluggable, NULL, NULL);
760 
761     class = object_get_class(OBJECT(dev));
762     do {
763         for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) {
764             qdev_property_add_legacy(dev, prop, &error_abort);
765             qdev_property_add_static(dev, prop, &error_abort);
766         }
767         class = object_class_get_parent(class);
768     } while (class != object_class_by_name(TYPE_DEVICE));
769 
770     object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
771                              (Object **)&dev->parent_bus, &error_abort);
772 }
773 
774 static void device_post_init(Object *obj)
775 {
776     qdev_prop_set_globals(DEVICE(obj), &error_abort);
777 }
778 
779 /* Unlink device from bus and free the structure.  */
780 static void device_finalize(Object *obj)
781 {
782     DeviceState *dev = DEVICE(obj);
783     if (dev->opts) {
784         qemu_opts_del(dev->opts);
785     }
786 }
787 
788 static void device_class_base_init(ObjectClass *class, void *data)
789 {
790     DeviceClass *klass = DEVICE_CLASS(class);
791 
792     /* We explicitly look up properties in the superclasses,
793      * so do not propagate them to the subclasses.
794      */
795     klass->props = NULL;
796 }
797 
798 static void device_unparent(Object *obj)
799 {
800     DeviceState *dev = DEVICE(obj);
801     BusState *bus;
802     QObject *event_data;
803     bool have_realized = dev->realized;
804 
805     while (dev->num_child_bus) {
806         bus = QLIST_FIRST(&dev->child_bus);
807         object_unparent(OBJECT(bus));
808     }
809     if (dev->realized) {
810         object_property_set_bool(obj, false, "realized", NULL);
811     }
812     if (dev->parent_bus) {
813         bus_remove_child(dev->parent_bus, dev);
814         object_unref(OBJECT(dev->parent_bus));
815         dev->parent_bus = NULL;
816     }
817 
818     /* Only send event if the device had been completely realized */
819     if (have_realized) {
820         gchar *path = object_get_canonical_path(OBJECT(dev));
821 
822         if (dev->id) {
823             event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }",
824                                             dev->id, path);
825         } else {
826             event_data = qobject_from_jsonf("{ 'path': %s }", path);
827         }
828         monitor_protocol_event(QEVENT_DEVICE_DELETED, event_data);
829         qobject_decref(event_data);
830         g_free(path);
831     }
832 }
833 
834 static void device_class_init(ObjectClass *class, void *data)
835 {
836     DeviceClass *dc = DEVICE_CLASS(class);
837 
838     class->unparent = device_unparent;
839     dc->realize = device_realize;
840     dc->unrealize = device_unrealize;
841 
842     /* by default all devices were considered as hotpluggable,
843      * so with intent to check it in generic qdev_unplug() /
844      * device_set_realized() functions make every device
845      * hotpluggable. Devices that shouldn't be hotpluggable,
846      * should override it in their class_init()
847      */
848     dc->hotpluggable = true;
849 }
850 
851 void device_reset(DeviceState *dev)
852 {
853     DeviceClass *klass = DEVICE_GET_CLASS(dev);
854 
855     if (klass->reset) {
856         klass->reset(dev);
857     }
858 }
859 
860 Object *qdev_get_machine(void)
861 {
862     static Object *dev;
863 
864     if (dev == NULL) {
865         dev = container_get(object_get_root(), "/machine");
866     }
867 
868     return dev;
869 }
870 
871 static const TypeInfo device_type_info = {
872     .name = TYPE_DEVICE,
873     .parent = TYPE_OBJECT,
874     .instance_size = sizeof(DeviceState),
875     .instance_init = device_initfn,
876     .instance_post_init = device_post_init,
877     .instance_finalize = device_finalize,
878     .class_base_init = device_class_base_init,
879     .class_init = device_class_init,
880     .abstract = true,
881     .class_size = sizeof(DeviceClass),
882 };
883 
884 static void qbus_initfn(Object *obj)
885 {
886     BusState *bus = BUS(obj);
887 
888     QTAILQ_INIT(&bus->children);
889     object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
890                              TYPE_HOTPLUG_HANDLER,
891                              (Object **)&bus->hotplug_handler, NULL);
892 }
893 
894 static char *default_bus_get_fw_dev_path(DeviceState *dev)
895 {
896     return g_strdup(object_get_typename(OBJECT(dev)));
897 }
898 
899 static void bus_class_init(ObjectClass *class, void *data)
900 {
901     BusClass *bc = BUS_CLASS(class);
902 
903     class->unparent = bus_unparent;
904     bc->get_fw_dev_path = default_bus_get_fw_dev_path;
905 }
906 
907 static void qbus_finalize(Object *obj)
908 {
909     BusState *bus = BUS(obj);
910 
911     g_free((char *)bus->name);
912 }
913 
914 static const TypeInfo bus_info = {
915     .name = TYPE_BUS,
916     .parent = TYPE_OBJECT,
917     .instance_size = sizeof(BusState),
918     .abstract = true,
919     .class_size = sizeof(BusClass),
920     .instance_init = qbus_initfn,
921     .instance_finalize = qbus_finalize,
922     .class_init = bus_class_init,
923 };
924 
925 static void qdev_register_types(void)
926 {
927     type_register_static(&bus_info);
928     type_register_static(&device_type_info);
929 }
930 
931 type_init(qdev_register_types)
932