xref: /openbmc/qemu/hw/core/qdev.c (revision b801e3cb2a7fd631a219222a8cbe9d554c906070)
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.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /* The theory here is that it should be possible to create a machine without
21    knowledge of specific devices.  Historically board init routines have
22    passed a bunch of arguments to each device, requiring the board know
23    exactly which device it is dealing with.  This file provides an abstract
24    API for device configuration and initialization.  Devices will generally
25    inherit from a particular bus (e.g. PCI or I2C) rather than
26    this API directly.  */
27 
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qapi/qapi-events-qdev.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/visitor.h"
33 #include "qemu/error-report.h"
34 #include "qemu/option.h"
35 #include "hw/irq.h"
36 #include "hw/qdev-properties.h"
37 #include "hw/boards.h"
38 #include "hw/sysbus.h"
39 #include "hw/qdev-clock.h"
40 #include "migration/vmstate.h"
41 #include "trace.h"
42 
43 static bool qdev_hot_added = false;
44 bool qdev_hot_removed = false;
45 
46 const VMStateDescription *qdev_get_vmsd(DeviceState *dev)
47 {
48     DeviceClass *dc = DEVICE_GET_CLASS(dev);
49     return dc->vmsd;
50 }
51 
52 static void bus_free_bus_child(BusChild *kid)
53 {
54     object_unref(OBJECT(kid->child));
55     g_free(kid);
56 }
57 
58 static void bus_remove_child(BusState *bus, DeviceState *child)
59 {
60     BusChild *kid;
61 
62     QTAILQ_FOREACH(kid, &bus->children, sibling) {
63         if (kid->child == child) {
64             char name[32];
65 
66             snprintf(name, sizeof(name), "child[%d]", kid->index);
67             QTAILQ_REMOVE_RCU(&bus->children, kid, sibling);
68 
69             bus->num_children--;
70 
71             /* This gives back ownership of kid->child back to us.  */
72             object_property_del(OBJECT(bus), name);
73 
74             /* free the bus kid, when it is safe to do so*/
75             call_rcu(kid, bus_free_bus_child, rcu);
76             break;
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     bus->num_children++;
87     kid->index = bus->max_index++;
88     kid->child = child;
89     object_ref(OBJECT(kid->child));
90 
91     QTAILQ_INSERT_HEAD_RCU(&bus->children, kid, sibling);
92 
93     /* This transfers ownership of kid->child to the property.  */
94     snprintf(name, sizeof(name), "child[%d]", kid->index);
95     object_property_add_link(OBJECT(bus), name,
96                              object_get_typename(OBJECT(child)),
97                              (Object **)&kid->child,
98                              NULL, /* read-only property */
99                              0);
100 }
101 
102 static bool bus_check_address(BusState *bus, DeviceState *child, Error **errp)
103 {
104     BusClass *bc = BUS_GET_CLASS(bus);
105     return !bc->check_address || bc->check_address(bus, child, errp);
106 }
107 
108 bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp)
109 {
110     BusState *old_parent_bus = dev->parent_bus;
111     DeviceClass *dc = DEVICE_GET_CLASS(dev);
112 
113     assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type));
114 
115     if (!bus_check_address(bus, dev, errp)) {
116         return false;
117     }
118 
119     if (old_parent_bus) {
120         trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)),
121             old_parent_bus, object_get_typename(OBJECT(old_parent_bus)),
122             OBJECT(bus), object_get_typename(OBJECT(bus)));
123         /*
124          * Keep a reference to the device while it's not plugged into
125          * any bus, to avoid it potentially evaporating when it is
126          * dereffed in bus_remove_child().
127          * Also keep the ref of the parent bus until the end, so that
128          * we can safely call resettable_change_parent() below.
129          */
130         object_ref(OBJECT(dev));
131         bus_remove_child(dev->parent_bus, dev);
132     }
133     dev->parent_bus = bus;
134     object_ref(OBJECT(bus));
135     bus_add_child(bus, dev);
136     if (dev->realized) {
137         resettable_change_parent(OBJECT(dev), OBJECT(bus),
138                                  OBJECT(old_parent_bus));
139     }
140     if (old_parent_bus) {
141         object_unref(OBJECT(old_parent_bus));
142         object_unref(OBJECT(dev));
143     }
144     return true;
145 }
146 
147 DeviceState *qdev_new(const char *name)
148 {
149     ObjectClass *oc = object_class_by_name(name);
150 #ifdef CONFIG_MODULES
151     if (!oc) {
152         int rv = module_load_qom(name, &error_fatal);
153         if (rv > 0) {
154             oc = object_class_by_name(name);
155         } else {
156             error_report("could not find a module for type '%s'", name);
157             exit(1);
158         }
159     }
160 #endif
161     if (!oc) {
162         error_report("unknown type '%s'", name);
163         abort();
164     }
165     return DEVICE(object_new(name));
166 }
167 
168 DeviceState *qdev_try_new(const char *name)
169 {
170     ObjectClass *oc = module_object_class_by_name(name);
171     if (!oc) {
172         return NULL;
173     }
174     return DEVICE(object_new_with_class(oc));
175 }
176 
177 static QTAILQ_HEAD(, DeviceListener) device_listeners
178     = QTAILQ_HEAD_INITIALIZER(device_listeners);
179 
180 enum ListenerDirection { Forward, Reverse };
181 
182 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...)     \
183     do {                                                          \
184         DeviceListener *_listener;                                \
185                                                                   \
186         switch (_direction) {                                     \
187         case Forward:                                             \
188             QTAILQ_FOREACH(_listener, &device_listeners, link) {  \
189                 if (_listener->_callback) {                       \
190                     _listener->_callback(_listener, ##_args);     \
191                 }                                                 \
192             }                                                     \
193             break;                                                \
194         case Reverse:                                             \
195             QTAILQ_FOREACH_REVERSE(_listener, &device_listeners,  \
196                                    link) {                        \
197                 if (_listener->_callback) {                       \
198                     _listener->_callback(_listener, ##_args);     \
199                 }                                                 \
200             }                                                     \
201             break;                                                \
202         default:                                                  \
203             abort();                                              \
204         }                                                         \
205     } while (0)
206 
207 static int device_listener_add(DeviceState *dev, void *opaque)
208 {
209     DEVICE_LISTENER_CALL(realize, Forward, dev);
210 
211     return 0;
212 }
213 
214 void device_listener_register(DeviceListener *listener)
215 {
216     QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
217 
218     qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
219                        NULL, NULL);
220 }
221 
222 void device_listener_unregister(DeviceListener *listener)
223 {
224     QTAILQ_REMOVE(&device_listeners, listener, link);
225 }
226 
227 bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp)
228 {
229     ERRP_GUARD();
230     DeviceListener *listener;
231 
232     QTAILQ_FOREACH(listener, &device_listeners, link) {
233         if (listener->hide_device) {
234             if (listener->hide_device(listener, opts, from_json, errp)) {
235                 return true;
236             } else if (*errp) {
237                 return false;
238             }
239         }
240     }
241 
242     return false;
243 }
244 
245 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
246                                  int required_for_version)
247 {
248     assert(!dev->realized);
249     dev->instance_id_alias = alias_id;
250     dev->alias_required_for_version = required_for_version;
251 }
252 
253 void device_cold_reset(DeviceState *dev)
254 {
255     resettable_reset(OBJECT(dev), RESET_TYPE_COLD);
256 }
257 
258 bool device_is_in_reset(DeviceState *dev)
259 {
260     return resettable_is_in_reset(OBJECT(dev));
261 }
262 
263 static ResettableState *device_get_reset_state(Object *obj)
264 {
265     DeviceState *dev = DEVICE(obj);
266     return &dev->reset;
267 }
268 
269 static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
270                                        void *opaque, ResetType type)
271 {
272     DeviceState *dev = DEVICE(obj);
273     BusState *bus;
274 
275     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
276         cb(OBJECT(bus), opaque, type);
277     }
278 }
279 
280 bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
281 {
282     assert(!dev->realized && !dev->parent_bus);
283 
284     if (bus) {
285         if (!qdev_set_parent_bus(dev, bus, errp)) {
286             return false;
287         }
288     } else {
289         assert(!DEVICE_GET_CLASS(dev)->bus_type);
290     }
291 
292     return object_property_set_bool(OBJECT(dev), "realized", true, errp);
293 }
294 
295 bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp)
296 {
297     bool ret;
298 
299     ret = qdev_realize(dev, bus, errp);
300     object_unref(OBJECT(dev));
301     return ret;
302 }
303 
304 void qdev_unrealize(DeviceState *dev)
305 {
306     object_property_set_bool(OBJECT(dev), "realized", false, &error_abort);
307 }
308 
309 static int qdev_assert_realized_properly_cb(Object *obj, void *opaque)
310 {
311     DeviceState *dev = DEVICE(object_dynamic_cast(obj, TYPE_DEVICE));
312     DeviceClass *dc;
313 
314     if (dev) {
315         dc = DEVICE_GET_CLASS(dev);
316         assert(dev->realized);
317         assert(dev->parent_bus || !dc->bus_type);
318     }
319     return 0;
320 }
321 
322 void qdev_assert_realized_properly(void)
323 {
324     object_child_foreach_recursive(object_get_root(),
325                                    qdev_assert_realized_properly_cb, NULL);
326 }
327 
328 bool qdev_machine_modified(void)
329 {
330     return qdev_hot_added || qdev_hot_removed;
331 }
332 
333 BusState *qdev_get_parent_bus(const DeviceState *dev)
334 {
335     return dev->parent_bus;
336 }
337 
338 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
339 {
340     BusState *bus;
341     Object *child = object_resolve_path_component(OBJECT(dev), name);
342 
343     bus = (BusState *)object_dynamic_cast(child, TYPE_BUS);
344     if (bus) {
345         return bus;
346     }
347 
348     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
349         if (strcmp(name, bus->name) == 0) {
350             return bus;
351         }
352     }
353     return NULL;
354 }
355 
356 int qdev_walk_children(DeviceState *dev,
357                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
358                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
359                        void *opaque)
360 {
361     BusState *bus;
362     int err;
363 
364     if (pre_devfn) {
365         err = pre_devfn(dev, opaque);
366         if (err) {
367             return err;
368         }
369     }
370 
371     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
372         err = qbus_walk_children(bus, pre_devfn, pre_busfn,
373                                  post_devfn, post_busfn, opaque);
374         if (err < 0) {
375             return err;
376         }
377     }
378 
379     if (post_devfn) {
380         err = post_devfn(dev, opaque);
381         if (err) {
382             return err;
383         }
384     }
385 
386     return 0;
387 }
388 
389 DeviceState *qdev_find_recursive(BusState *bus, const char *id)
390 {
391     BusChild *kid;
392     DeviceState *ret;
393     BusState *child;
394 
395     WITH_RCU_READ_LOCK_GUARD() {
396         QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
397             DeviceState *dev = kid->child;
398 
399             if (dev->id && strcmp(dev->id, id) == 0) {
400                 return dev;
401             }
402 
403             QLIST_FOREACH(child, &dev->child_bus, sibling) {
404                 ret = qdev_find_recursive(child, id);
405                 if (ret) {
406                     return ret;
407                 }
408             }
409         }
410     }
411     return NULL;
412 }
413 
414 char *qdev_get_dev_path(DeviceState *dev)
415 {
416     BusClass *bc;
417 
418     if (!dev || !dev->parent_bus) {
419         return NULL;
420     }
421 
422     bc = BUS_GET_CLASS(dev->parent_bus);
423     if (bc->get_dev_path) {
424         return bc->get_dev_path(dev);
425     }
426 
427     return NULL;
428 }
429 
430 void qdev_add_unplug_blocker(DeviceState *dev, Error *reason)
431 {
432     dev->unplug_blockers = g_slist_prepend(dev->unplug_blockers, reason);
433 }
434 
435 void qdev_del_unplug_blocker(DeviceState *dev, Error *reason)
436 {
437     dev->unplug_blockers = g_slist_remove(dev->unplug_blockers, reason);
438 }
439 
440 bool qdev_unplug_blocked(DeviceState *dev, Error **errp)
441 {
442     if (dev->unplug_blockers) {
443         error_propagate(errp, error_copy(dev->unplug_blockers->data));
444         return true;
445     }
446 
447     return false;
448 }
449 
450 static bool device_get_realized(Object *obj, Error **errp)
451 {
452     DeviceState *dev = DEVICE(obj);
453     return dev->realized;
454 }
455 
456 static bool check_only_migratable(Object *obj, Error **errp)
457 {
458     DeviceClass *dc = DEVICE_GET_CLASS(obj);
459 
460     if (!vmstate_check_only_migratable(dc->vmsd)) {
461         error_setg(errp, "Device %s is not migratable, but "
462                    "--only-migratable was specified",
463                    object_get_typename(obj));
464         return false;
465     }
466 
467     return true;
468 }
469 
470 static void device_set_realized(Object *obj, bool value, Error **errp)
471 {
472     DeviceState *dev = DEVICE(obj);
473     DeviceClass *dc = DEVICE_GET_CLASS(dev);
474     HotplugHandler *hotplug_ctrl;
475     BusState *bus;
476     NamedClockList *ncl;
477     Error *local_err = NULL;
478     bool unattached_parent = false;
479     static int unattached_count;
480 
481     if (dev->hotplugged && !dc->hotpluggable) {
482         error_setg(errp, "Device '%s' does not support hotplugging",
483                    object_get_typename(obj));
484         return;
485     }
486 
487     if (value && !dev->realized) {
488         if (!check_only_migratable(obj, errp)) {
489             goto fail;
490         }
491 
492         if (!obj->parent) {
493             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
494 
495             object_property_add_child(container_get(qdev_get_machine(),
496                                                     "/unattached"),
497                                       name, obj);
498             unattached_parent = true;
499             g_free(name);
500         }
501 
502         hotplug_ctrl = qdev_get_hotplug_handler(dev);
503         if (hotplug_ctrl) {
504             hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err);
505             if (local_err != NULL) {
506                 goto fail;
507             }
508         }
509 
510         if (dc->realize) {
511             dc->realize(dev, &local_err);
512             if (local_err != NULL) {
513                 goto fail;
514             }
515         }
516 
517         DEVICE_LISTENER_CALL(realize, Forward, dev);
518 
519         /*
520          * always free/re-initialize here since the value cannot be cleaned up
521          * in device_unrealize due to its usage later on in the unplug path
522          */
523         g_free(dev->canonical_path);
524         dev->canonical_path = object_get_canonical_path(OBJECT(dev));
525         QLIST_FOREACH(ncl, &dev->clocks, node) {
526             if (ncl->alias) {
527                 continue;
528             } else {
529                 clock_setup_canonical_path(ncl->clock);
530             }
531         }
532 
533         if (qdev_get_vmsd(dev)) {
534             if (vmstate_register_with_alias_id(VMSTATE_IF(dev),
535                                                VMSTATE_INSTANCE_ID_ANY,
536                                                qdev_get_vmsd(dev), dev,
537                                                dev->instance_id_alias,
538                                                dev->alias_required_for_version,
539                                                &local_err) < 0) {
540                 goto post_realize_fail;
541             }
542         }
543 
544         /*
545          * Clear the reset state, in case the object was previously unrealized
546          * with a dirty state.
547          */
548         resettable_state_clear(&dev->reset);
549 
550         QLIST_FOREACH(bus, &dev->child_bus, sibling) {
551             if (!qbus_realize(bus, errp)) {
552                 goto child_realize_fail;
553             }
554         }
555         if (dev->hotplugged) {
556             /*
557              * Reset the device, as well as its subtree which, at this point,
558              * should be realized too.
559              */
560             resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD);
561             resettable_change_parent(OBJECT(dev), OBJECT(dev->parent_bus),
562                                      NULL);
563             resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD);
564         }
565         dev->pending_deleted_event = false;
566 
567         if (hotplug_ctrl) {
568             hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
569             if (local_err != NULL) {
570                 goto child_realize_fail;
571             }
572        }
573 
574        qatomic_store_release(&dev->realized, value);
575 
576     } else if (!value && dev->realized) {
577 
578         /*
579          * Change the value so that any concurrent users are aware
580          * that the device is going to be unrealized
581          *
582          * TODO: change .realized property to enum that states
583          * each phase of the device realization/unrealization
584          */
585 
586         qatomic_set(&dev->realized, value);
587         /*
588          * Ensure that concurrent users see this update prior to
589          * any other changes done by unrealize.
590          */
591         smp_wmb();
592 
593         QLIST_FOREACH(bus, &dev->child_bus, sibling) {
594             qbus_unrealize(bus);
595         }
596         if (qdev_get_vmsd(dev)) {
597             vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
598         }
599         if (dc->unrealize) {
600             dc->unrealize(dev);
601         }
602         dev->pending_deleted_event = true;
603         DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
604     }
605 
606     assert(local_err == NULL);
607     return;
608 
609 child_realize_fail:
610     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
611         qbus_unrealize(bus);
612     }
613 
614     if (qdev_get_vmsd(dev)) {
615         vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
616     }
617 
618 post_realize_fail:
619     g_free(dev->canonical_path);
620     dev->canonical_path = NULL;
621     if (dc->unrealize) {
622         dc->unrealize(dev);
623     }
624 
625 fail:
626     error_propagate(errp, local_err);
627     if (unattached_parent) {
628         /*
629          * Beware, this doesn't just revert
630          * object_property_add_child(), it also runs bus_remove()!
631          */
632         object_unparent(OBJECT(dev));
633         unattached_count--;
634     }
635 }
636 
637 static bool device_get_hotpluggable(Object *obj, Error **errp)
638 {
639     DeviceClass *dc = DEVICE_GET_CLASS(obj);
640     DeviceState *dev = DEVICE(obj);
641 
642     return dc->hotpluggable && (dev->parent_bus == NULL ||
643                                 qbus_is_hotpluggable(dev->parent_bus));
644 }
645 
646 static bool device_get_hotplugged(Object *obj, Error **errp)
647 {
648     DeviceState *dev = DEVICE(obj);
649 
650     return dev->hotplugged;
651 }
652 
653 static void device_initfn(Object *obj)
654 {
655     DeviceState *dev = DEVICE(obj);
656 
657     if (phase_check(PHASE_MACHINE_READY)) {
658         dev->hotplugged = 1;
659         qdev_hot_added = true;
660     }
661 
662     dev->instance_id_alias = -1;
663     dev->realized = false;
664     dev->allow_unplug_during_migration = false;
665 
666     QLIST_INIT(&dev->gpios);
667     QLIST_INIT(&dev->clocks);
668 }
669 
670 static void device_post_init(Object *obj)
671 {
672     /*
673      * Note: ordered so that the user's global properties take
674      * precedence.
675      */
676     object_apply_compat_props(obj);
677     qdev_prop_set_globals(DEVICE(obj));
678 }
679 
680 /* Unlink device from bus and free the structure.  */
681 static void device_finalize(Object *obj)
682 {
683     NamedGPIOList *ngl, *next;
684 
685     DeviceState *dev = DEVICE(obj);
686 
687     g_assert(!dev->unplug_blockers);
688 
689     QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
690         QLIST_REMOVE(ngl, node);
691         qemu_free_irqs(ngl->in, ngl->num_in);
692         g_free(ngl->name);
693         g_free(ngl);
694         /* ngl->out irqs are owned by the other end and should not be freed
695          * here
696          */
697     }
698 
699     qdev_finalize_clocklist(dev);
700 
701     /* Only send event if the device had been completely realized */
702     if (dev->pending_deleted_event) {
703         g_assert(dev->canonical_path);
704 
705         qapi_event_send_device_deleted(dev->id, dev->canonical_path);
706         g_free(dev->canonical_path);
707         dev->canonical_path = NULL;
708     }
709 
710     qobject_unref(dev->opts);
711     g_free(dev->id);
712 }
713 
714 static void device_class_base_init(ObjectClass *class, void *data)
715 {
716     DeviceClass *klass = DEVICE_CLASS(class);
717 
718     /* We explicitly look up properties in the superclasses,
719      * so do not propagate them to the subclasses.
720      */
721     klass->props_ = NULL;
722 }
723 
724 static void device_unparent(Object *obj)
725 {
726     DeviceState *dev = DEVICE(obj);
727     BusState *bus;
728 
729     if (dev->realized) {
730         qdev_unrealize(dev);
731     }
732     while (dev->num_child_bus) {
733         bus = QLIST_FIRST(&dev->child_bus);
734         object_unparent(OBJECT(bus));
735     }
736     if (dev->parent_bus) {
737         bus_remove_child(dev->parent_bus, dev);
738         object_unref(OBJECT(dev->parent_bus));
739         dev->parent_bus = NULL;
740     }
741 }
742 
743 static char *
744 device_vmstate_if_get_id(VMStateIf *obj)
745 {
746     DeviceState *dev = DEVICE(obj);
747 
748     return qdev_get_dev_path(dev);
749 }
750 
751 static void device_class_init(ObjectClass *class, void *data)
752 {
753     DeviceClass *dc = DEVICE_CLASS(class);
754     VMStateIfClass *vc = VMSTATE_IF_CLASS(class);
755     ResettableClass *rc = RESETTABLE_CLASS(class);
756 
757     class->unparent = device_unparent;
758 
759     /* by default all devices were considered as hotpluggable,
760      * so with intent to check it in generic qdev_unplug() /
761      * device_set_realized() functions make every device
762      * hotpluggable. Devices that shouldn't be hotpluggable,
763      * should override it in their class_init()
764      */
765     dc->hotpluggable = true;
766     dc->user_creatable = true;
767     vc->get_id = device_vmstate_if_get_id;
768     rc->get_state = device_get_reset_state;
769     rc->child_foreach = device_reset_child_foreach;
770 
771     /*
772      * A NULL legacy_reset implies a three-phase reset device. Devices can
773      * only be reset using three-phase aware mechanisms, but we still support
774      * for transitional purposes leaf classes which set the old legacy_reset
775      * method via device_class_set_legacy_reset().
776      */
777     dc->legacy_reset = NULL;
778 
779     object_class_property_add_bool(class, "realized",
780                                    device_get_realized, device_set_realized);
781     object_class_property_add_bool(class, "hotpluggable",
782                                    device_get_hotpluggable, NULL);
783     object_class_property_add_bool(class, "hotplugged",
784                                    device_get_hotplugged, NULL);
785     object_class_property_add_link(class, "parent_bus", TYPE_BUS,
786                                    offsetof(DeviceState, parent_bus), NULL, 0);
787 }
788 
789 static void do_legacy_reset(Object *obj, ResetType type)
790 {
791     DeviceClass *dc = DEVICE_GET_CLASS(obj);
792 
793     dc->legacy_reset(DEVICE(obj));
794 }
795 
796 void device_class_set_legacy_reset(DeviceClass *dc, DeviceReset dev_reset)
797 {
798     /*
799      * A legacy DeviceClass::reset has identical semantics to the
800      * three-phase "hold" method, with no "enter" or "exit"
801      * behaviour. Classes that use this legacy function must be leaf
802      * classes that do not chain up to their parent class reset.
803      * There is no mechanism for resetting a device that does not
804      * use the three-phase APIs, so the only place which calls
805      * the legacy_reset hook is do_legacy_reset().
806      */
807     ResettableClass *rc = RESETTABLE_CLASS(dc);
808 
809     rc->phases.enter = NULL;
810     rc->phases.hold = do_legacy_reset;
811     rc->phases.exit = NULL;
812     dc->legacy_reset = dev_reset;
813 }
814 
815 void device_class_set_parent_realize(DeviceClass *dc,
816                                      DeviceRealize dev_realize,
817                                      DeviceRealize *parent_realize)
818 {
819     *parent_realize = dc->realize;
820     dc->realize = dev_realize;
821 }
822 
823 void device_class_set_parent_unrealize(DeviceClass *dc,
824                                        DeviceUnrealize dev_unrealize,
825                                        DeviceUnrealize *parent_unrealize)
826 {
827     *parent_unrealize = dc->unrealize;
828     dc->unrealize = dev_unrealize;
829 }
830 
831 Object *qdev_get_machine(void)
832 {
833     static Object *dev;
834 
835     if (dev == NULL) {
836         dev = container_get(object_get_root(), "/machine");
837     }
838 
839     return dev;
840 }
841 
842 char *qdev_get_human_name(DeviceState *dev)
843 {
844     g_assert(dev != NULL);
845 
846     return dev->id ?
847            g_strdup(dev->id) : object_get_canonical_path(OBJECT(dev));
848 }
849 
850 static MachineInitPhase machine_phase;
851 
852 bool phase_check(MachineInitPhase phase)
853 {
854     return machine_phase >= phase;
855 }
856 
857 void phase_advance(MachineInitPhase phase)
858 {
859     assert(machine_phase == phase - 1);
860     machine_phase = phase;
861 }
862 
863 static const TypeInfo device_type_info = {
864     .name = TYPE_DEVICE,
865     .parent = TYPE_OBJECT,
866     .instance_size = sizeof(DeviceState),
867     .instance_init = device_initfn,
868     .instance_post_init = device_post_init,
869     .instance_finalize = device_finalize,
870     .class_base_init = device_class_base_init,
871     .class_init = device_class_init,
872     .abstract = true,
873     .class_size = sizeof(DeviceClass),
874     .interfaces = (InterfaceInfo[]) {
875         { TYPE_VMSTATE_IF },
876         { TYPE_RESETTABLE_INTERFACE },
877         { }
878     }
879 };
880 
881 static void qdev_register_types(void)
882 {
883     type_register_static(&device_type_info);
884 }
885 
886 type_init(qdev_register_types)
887