2
0

qdev-hotplug.c 3.0 KB

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