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