vm-change-state-handler.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * qdev vm change state handlers
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "hw/qdev-core.h"
  19. #include "system/runstate.h"
  20. static int qdev_get_dev_tree_depth(DeviceState *dev)
  21. {
  22. int depth;
  23. for (depth = 0; dev; depth++) {
  24. BusState *bus = dev->parent_bus;
  25. if (!bus) {
  26. break;
  27. }
  28. dev = bus->parent;
  29. }
  30. return depth;
  31. }
  32. /**
  33. * qdev_add_vm_change_state_handler:
  34. * @dev: the device that owns this handler
  35. * @cb: the callback function to be invoked
  36. * @opaque: user data passed to the callback function
  37. *
  38. * This function works like qemu_add_vm_change_state_handler() except callbacks
  39. * are invoked in qdev tree depth order. Ordering is desirable when callbacks
  40. * of children depend on their parent's callback having completed first.
  41. *
  42. * For example, when qdev_add_vm_change_state_handler() is used, a host
  43. * controller's callback is invoked before the children on its bus when the VM
  44. * starts running. The order is reversed when the VM stops running.
  45. *
  46. * Returns: an entry to be freed with qemu_del_vm_change_state_handler()
  47. */
  48. VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev,
  49. VMChangeStateHandler *cb,
  50. void *opaque)
  51. {
  52. return qdev_add_vm_change_state_handler_full(dev, cb, NULL, opaque);
  53. }
  54. /*
  55. * Exactly like qdev_add_vm_change_state_handler() but passes a prepare_cb
  56. * argument too.
  57. */
  58. VMChangeStateEntry *qdev_add_vm_change_state_handler_full(
  59. DeviceState *dev, VMChangeStateHandler *cb,
  60. VMChangeStateHandler *prepare_cb, void *opaque)
  61. {
  62. int depth = qdev_get_dev_tree_depth(dev);
  63. return qemu_add_vm_change_state_handler_prio_full(cb, prepare_cb, opaque,
  64. depth);
  65. }