xref: /openbmc/qemu/hw/core/qdev-hotplug.c (revision 206d602e9b73d9079449b44899d572624d57390a)
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, BusState *bus, 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 bool qdev_hotunplug_allowed(DeviceState *dev, Error **errp)
51 {
52     return !qdev_unplug_blocked(dev, errp);
53 }
54 
55 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
56 {
57     if (dev->parent_bus) {
58         return dev->parent_bus->hotplug_handler;
59     }
60     return NULL;
61 }
62 
63 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
64 {
65     HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
66 
67     if (hotplug_ctrl == NULL && dev->parent_bus) {
68         hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
69     }
70     return hotplug_ctrl;
71 }
72 
73 /* can be used as ->unplug() callback for the simple cases */
74 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
75                                   DeviceState *dev, Error **errp)
76 {
77     qdev_unrealize(dev);
78 }
79