| 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 | |
| 18 | #include "qemu/osdep.h" |
| 19 | #include "hw/core/qdev.h" |
| 20 | #include "system/runstate.h" |
| 21 | |
| 22 | static int qdev_get_dev_tree_depth(DeviceState *dev) |
| 23 | { |
| 24 | int depth; |
| 25 | |
| 26 | for (depth = 0; dev; depth++) { |
| 27 | BusState *bus = dev->parent_bus; |
| 28 | |
| 29 | if (!bus) { |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | dev = bus->parent; |
| 34 | } |
| 35 | |
| 36 | return depth; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * qdev_add_vm_change_state_handler: |
| 41 | * @dev: the device that owns this handler |
| 42 | * @cb: the callback function to be invoked |
| 43 | * @cb_ret: the callback function with return value to be invoked |
| 44 | * @opaque: user data passed to the callback function |
| 45 | * |
| 46 | * This function works like qemu_add_vm_change_state_handler() except callbacks |
| 47 | * are invoked in qdev tree depth order. Ordering is desirable when callbacks |
| 48 | * of children depend on their parent's callback having completed first. |
| 49 | * |
| 50 | * For example, when qdev_add_vm_change_state_handler() is used, a host |
| 51 | * controller's callback is invoked before the children on its bus when the VM |
| 52 | * starts running. The order is reversed when the VM stops running. |
| 53 | * |
| 54 | * Note that the parameter `cb` and `cb_ret` are mutually exclusive. |
| 55 | * |
| 56 | * Returns: an entry to be freed with qemu_del_vm_change_state_handler() |
| 57 | */ |
| 58 | VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, |
| 59 | VMChangeStateHandler *cb, |
| 60 | VMChangeStateHandlerWithRet *cb_ret, |
| 61 | void *opaque) |
| 62 | { |
| 63 | assert(!cb || !cb_ret); |
| 64 | return qdev_add_vm_change_state_handler_full(dev, cb, NULL, cb_ret, opaque); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Exactly like qdev_add_vm_change_state_handler() but passes a prepare_cb |
| 69 | * and the cb_ret arguments too. |
| 70 | */ |
| 71 | VMChangeStateEntry *qdev_add_vm_change_state_handler_full( |
| 72 | DeviceState *dev, VMChangeStateHandler *cb, VMChangeStateHandler *prepare_cb, |
| 73 | VMChangeStateHandlerWithRet *cb_ret, void *opaque) |
| 74 | { |
| 75 | int depth = qdev_get_dev_tree_depth(dev); |
| 76 | |
| 77 | assert(!cb || !cb_ret); |
| 78 | return qemu_add_vm_change_state_handler_prio_full(cb, prepare_cb, cb_ret, |
| 79 | opaque, depth); |
| 80 | } |