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 #include "qapi/error.h" 16 17 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev) 18 { 19 MachineState *machine; 20 MachineClass *mc; 21 Object *m_obj = qdev_get_machine(); 22 23 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) { 24 machine = MACHINE(m_obj); 25 mc = MACHINE_GET_CLASS(machine); 26 if (mc->get_hotplug_handler) { 27 return mc->get_hotplug_handler(machine, dev); 28 } 29 } 30 31 return NULL; 32 } 33 34 static bool qdev_hotplug_unplug_allowed_common(DeviceState *dev, BusState *bus, 35 Error **errp) 36 { 37 DeviceClass *dc = DEVICE_GET_CLASS(dev); 38 39 if (!dc->hotpluggable) { 40 error_setg(errp, "Device '%s' does not support hotplugging", 41 object_get_typename(OBJECT(dev))); 42 return false; 43 } 44 45 if (bus) { 46 if (!qbus_is_hotpluggable(bus)) { 47 error_setg(errp, "Bus '%s' does not support hotplugging", 48 bus->name); 49 return false; 50 } 51 } 52 53 return true; 54 } 55 56 bool qdev_hotplug_allowed(DeviceState *dev, BusState *bus, Error **errp) 57 { 58 MachineState *machine; 59 MachineClass *mc; 60 Object *m_obj = qdev_get_machine(); 61 62 if (!qdev_hotplug_unplug_allowed_common(dev, bus, errp)) { 63 return false; 64 } 65 66 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) { 67 machine = MACHINE(m_obj); 68 mc = MACHINE_GET_CLASS(machine); 69 if (mc->hotplug_allowed) { 70 return mc->hotplug_allowed(machine, dev, errp); 71 } 72 } 73 74 return true; 75 } 76 77 bool qdev_hotunplug_allowed(DeviceState *dev, Error **errp) 78 { 79 return !qdev_unplug_blocked(dev, errp) && 80 qdev_hotplug_unplug_allowed_common(dev, dev->parent_bus, errp); 81 } 82 83 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev) 84 { 85 if (dev->parent_bus) { 86 return dev->parent_bus->hotplug_handler; 87 } 88 return NULL; 89 } 90 91 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev) 92 { 93 HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev); 94 95 if (hotplug_ctrl == NULL && dev->parent_bus) { 96 hotplug_ctrl = qdev_get_bus_hotplug_handler(dev); 97 } 98 return hotplug_ctrl; 99 } 100 101 /* can be used as ->unplug() callback for the simple cases */ 102 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev, 103 DeviceState *dev, Error **errp) 104 { 105 qdev_unrealize(dev); 106 } 107