xref: /openbmc/qemu/hw/core/qdev-hotplug.c (revision 7d87775f)
1 /*
2  * QDev Hotplug handlers
3  *
4  * Copyright (c) Red Hat
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  */
11 
12 #include "qemu/osdep.h"
13 #include "hw/qdev-core.h"
14 #include "hw/boards.h"
15 
16 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
17 {
18     MachineState *machine;
19     MachineClass *mc;
20     Object *m_obj = qdev_get_machine();
21 
22     if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
23         machine = MACHINE(m_obj);
24         mc = MACHINE_GET_CLASS(machine);
25         if (mc->get_hotplug_handler) {
26             return mc->get_hotplug_handler(machine, dev);
27         }
28     }
29 
30     return NULL;
31 }
32 
33 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
34 {
35     MachineState *machine;
36     MachineClass *mc;
37     Object *m_obj = qdev_get_machine();
38 
39     if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
40         machine = MACHINE(m_obj);
41         mc = MACHINE_GET_CLASS(machine);
42         if (mc->hotplug_allowed) {
43             return mc->hotplug_allowed(machine, dev, errp);
44         }
45     }
46 
47     return true;
48 }
49 
50 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
51 {
52     if (dev->parent_bus) {
53         return dev->parent_bus->hotplug_handler;
54     }
55     return NULL;
56 }
57 
58 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
59 {
60     HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
61 
62     if (hotplug_ctrl == NULL && dev->parent_bus) {
63         hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
64     }
65     return hotplug_ctrl;
66 }
67 
68 /* can be used as ->unplug() callback for the simple cases */
69 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
70                                   DeviceState *dev, Error **errp)
71 {
72     qdev_unrealize(dev);
73 }
74