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