qdev-hotplug.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "qemu/osdep.h"
  12. #include "hw/qdev-core.h"
  13. #include "hw/boards.h"
  14. HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
  15. {
  16. MachineState *machine;
  17. MachineClass *mc;
  18. Object *m_obj = qdev_get_machine();
  19. if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
  20. machine = MACHINE(m_obj);
  21. mc = MACHINE_GET_CLASS(machine);
  22. if (mc->get_hotplug_handler) {
  23. return mc->get_hotplug_handler(machine, dev);
  24. }
  25. }
  26. return NULL;
  27. }
  28. bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
  29. {
  30. MachineState *machine;
  31. MachineClass *mc;
  32. Object *m_obj = qdev_get_machine();
  33. if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
  34. machine = MACHINE(m_obj);
  35. mc = MACHINE_GET_CLASS(machine);
  36. if (mc->hotplug_allowed) {
  37. return mc->hotplug_allowed(machine, dev, errp);
  38. }
  39. }
  40. return true;
  41. }
  42. HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
  43. {
  44. if (dev->parent_bus) {
  45. return dev->parent_bus->hotplug_handler;
  46. }
  47. return NULL;
  48. }
  49. HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
  50. {
  51. HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
  52. if (hotplug_ctrl == NULL && dev->parent_bus) {
  53. hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
  54. }
  55. return hotplug_ctrl;
  56. }
  57. /* can be used as ->unplug() callback for the simple cases */
  58. void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
  59. DeviceState *dev, Error **errp)
  60. {
  61. qdev_unrealize(dev);
  62. }