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