| 1 | /* |
| 2 | * Dynamic device configuration and creation. |
| 3 | * |
| 4 | * Copyright (c) 2009 CodeSourcery |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | /* The theory here is that it should be possible to create a machine without |
| 21 | knowledge of specific devices. Historically board init routines have |
| 22 | passed a bunch of arguments to each device, requiring the board know |
| 23 | exactly which device it is dealing with. This file provides an abstract |
| 24 | API for device configuration and initialization. Devices will generally |
| 25 | inherit from a particular bus (e.g. PCI or I2C) rather than |
| 26 | this API directly. */ |
| 27 | |
| 28 | #include "qemu/osdep.h" |
| 29 | #include "qapi/error.h" |
| 30 | #include "qapi/qapi-events-qdev.h" |
| 31 | #include "qobject/qdict.h" |
| 32 | #include "qapi/visitor.h" |
| 33 | #include "qemu/error-report.h" |
| 34 | #include "qemu/option.h" |
| 35 | #include "qom/compat-properties.h" |
| 36 | #include "hw/core/irq.h" |
| 37 | #include "hw/core/qdev-properties.h" |
| 38 | #include "hw/core/boards.h" |
| 39 | #include "hw/core/sysbus.h" |
| 40 | #include "hw/core/qdev-clock.h" |
| 41 | #include "migration/vmstate.h" |
| 42 | #include "trace.h" |
| 43 | |
| 44 | static bool qdev_hot_added = false; |
| 45 | bool qdev_hot_removed = false; |
| 46 | |
| 47 | const VMStateDescription *qdev_get_vmsd(DeviceState *dev) |
| 48 | { |
| 49 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 50 | return dc->vmsd; |
| 51 | } |
| 52 | |
| 53 | static void bus_free_bus_child(BusChild *kid) |
| 54 | { |
| 55 | object_unref(OBJECT(kid->child)); |
| 56 | g_free(kid); |
| 57 | } |
| 58 | |
| 59 | static void bus_remove_child(BusState *bus, DeviceState *child) |
| 60 | { |
| 61 | BusChild *kid; |
| 62 | |
| 63 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
| 64 | if (kid->child == child) { |
| 65 | char name[32]; |
| 66 | |
| 67 | snprintf(name, sizeof(name), "child[%d]", kid->index); |
| 68 | QTAILQ_REMOVE_RCU(&bus->children, kid, sibling); |
| 69 | |
| 70 | bus->num_children--; |
| 71 | |
| 72 | /* This gives back ownership of kid->child back to us. */ |
| 73 | object_property_del(OBJECT(bus), name); |
| 74 | |
| 75 | /* free the bus kid, when it is safe to do so*/ |
| 76 | call_rcu(kid, bus_free_bus_child, rcu); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static void bus_add_child(BusState *bus, DeviceState *child) |
| 83 | { |
| 84 | char name[32]; |
| 85 | BusChild *kid = g_malloc0(sizeof(*kid)); |
| 86 | |
| 87 | bus->num_children++; |
| 88 | kid->index = bus->max_index++; |
| 89 | kid->child = child; |
| 90 | object_ref(OBJECT(kid->child)); |
| 91 | |
| 92 | QTAILQ_INSERT_HEAD_RCU(&bus->children, kid, sibling); |
| 93 | |
| 94 | /* This transfers ownership of kid->child to the property. */ |
| 95 | snprintf(name, sizeof(name), "child[%d]", kid->index); |
| 96 | object_property_add_link(OBJECT(bus), name, |
| 97 | object_get_typename(OBJECT(child)), |
| 98 | (Object **)&kid->child, |
| 99 | NULL, /* read-only property */ |
| 100 | 0); |
| 101 | } |
| 102 | |
| 103 | static bool bus_check_address(BusState *bus, DeviceState *child, Error **errp) |
| 104 | { |
| 105 | BusClass *bc = BUS_GET_CLASS(bus); |
| 106 | return !bc->check_address || bc->check_address(bus, child, errp); |
| 107 | } |
| 108 | |
| 109 | bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp) |
| 110 | { |
| 111 | BusState *old_parent_bus = dev->parent_bus; |
| 112 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 113 | |
| 114 | assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type)); |
| 115 | |
| 116 | if (!bus_check_address(bus, dev, errp)) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (old_parent_bus) { |
| 121 | trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)), |
| 122 | old_parent_bus, object_get_typename(OBJECT(old_parent_bus)), |
| 123 | OBJECT(bus), object_get_typename(OBJECT(bus))); |
| 124 | /* |
| 125 | * Keep a reference to the device while it's not plugged into |
| 126 | * any bus, to avoid it potentially evaporating when it is |
| 127 | * dereffed in bus_remove_child(). |
| 128 | * Also keep the ref of the parent bus until the end, so that |
| 129 | * we can safely call resettable_change_parent() below. |
| 130 | */ |
| 131 | object_ref(OBJECT(dev)); |
| 132 | bus_remove_child(dev->parent_bus, dev); |
| 133 | } |
| 134 | dev->parent_bus = bus; |
| 135 | object_ref(OBJECT(bus)); |
| 136 | bus_add_child(bus, dev); |
| 137 | if (dev->realized) { |
| 138 | resettable_change_parent(OBJECT(dev), OBJECT(bus), |
| 139 | OBJECT(old_parent_bus)); |
| 140 | } |
| 141 | if (old_parent_bus) { |
| 142 | object_unref(OBJECT(old_parent_bus)); |
| 143 | object_unref(OBJECT(dev)); |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | DeviceState *qdev_new(const char *name) |
| 149 | { |
| 150 | return DEVICE(object_new(name)); |
| 151 | } |
| 152 | |
| 153 | DeviceState *qdev_try_new(const char *name) |
| 154 | { |
| 155 | ObjectClass *oc = module_object_class_by_name(name); |
| 156 | if (!oc) { |
| 157 | return NULL; |
| 158 | } |
| 159 | return DEVICE(object_new_with_class(oc)); |
| 160 | } |
| 161 | |
| 162 | static QTAILQ_HEAD(, DeviceListener) device_listeners |
| 163 | = QTAILQ_HEAD_INITIALIZER(device_listeners); |
| 164 | |
| 165 | enum ListenerDirection { Forward, Reverse }; |
| 166 | |
| 167 | #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \ |
| 168 | do { \ |
| 169 | DeviceListener *_listener; \ |
| 170 | \ |
| 171 | switch (_direction) { \ |
| 172 | case Forward: \ |
| 173 | QTAILQ_FOREACH(_listener, &device_listeners, link) { \ |
| 174 | if (_listener->_callback) { \ |
| 175 | _listener->_callback(_listener, ##_args); \ |
| 176 | } \ |
| 177 | } \ |
| 178 | break; \ |
| 179 | case Reverse: \ |
| 180 | QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \ |
| 181 | link) { \ |
| 182 | if (_listener->_callback) { \ |
| 183 | _listener->_callback(_listener, ##_args); \ |
| 184 | } \ |
| 185 | } \ |
| 186 | break; \ |
| 187 | default: \ |
| 188 | abort(); \ |
| 189 | } \ |
| 190 | } while (0) |
| 191 | |
| 192 | static int device_listener_add(DeviceState *dev, void *opaque) |
| 193 | { |
| 194 | DEVICE_LISTENER_CALL(realize, Forward, dev); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | void device_listener_register(DeviceListener *listener) |
| 200 | { |
| 201 | QTAILQ_INSERT_TAIL(&device_listeners, listener, link); |
| 202 | |
| 203 | qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add, |
| 204 | NULL, NULL); |
| 205 | } |
| 206 | |
| 207 | void device_listener_unregister(DeviceListener *listener) |
| 208 | { |
| 209 | QTAILQ_REMOVE(&device_listeners, listener, link); |
| 210 | } |
| 211 | |
| 212 | bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp) |
| 213 | { |
| 214 | ERRP_GUARD(); |
| 215 | DeviceListener *listener; |
| 216 | |
| 217 | QTAILQ_FOREACH(listener, &device_listeners, link) { |
| 218 | if (listener->hide_device) { |
| 219 | if (listener->hide_device(listener, opts, from_json, errp)) { |
| 220 | return true; |
| 221 | } else if (*errp) { |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
| 231 | int required_for_version) |
| 232 | { |
| 233 | assert(!dev->realized); |
| 234 | dev->instance_id_alias = alias_id; |
| 235 | dev->alias_required_for_version = required_for_version; |
| 236 | } |
| 237 | |
| 238 | void device_cold_reset(DeviceState *dev) |
| 239 | { |
| 240 | resettable_reset(OBJECT(dev), RESET_TYPE_COLD); |
| 241 | } |
| 242 | |
| 243 | bool device_is_in_reset(DeviceState *dev) |
| 244 | { |
| 245 | return resettable_is_in_reset(OBJECT(dev)); |
| 246 | } |
| 247 | |
| 248 | static ResettableState *device_get_reset_state(Object *obj) |
| 249 | { |
| 250 | DeviceState *dev = DEVICE(obj); |
| 251 | return &dev->reset; |
| 252 | } |
| 253 | |
| 254 | static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb, |
| 255 | void *opaque, ResetType type) |
| 256 | { |
| 257 | DeviceState *dev = DEVICE(obj); |
| 258 | BusState *bus; |
| 259 | |
| 260 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 261 | cb(OBJECT(bus), opaque, type); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp) |
| 266 | { |
| 267 | static int unattached_count; |
| 268 | bool unattached_parent = false; |
| 269 | |
| 270 | assert(!dev->parent_bus); |
| 271 | |
| 272 | if (!OBJECT(dev)->parent) { |
| 273 | gchar *name = g_strdup_printf("device[%d]", unattached_count++); |
| 274 | |
| 275 | object_property_add_child(machine_get_container("unattached"), |
| 276 | name, OBJECT(dev)); |
| 277 | unattached_parent = true; |
| 278 | g_free(name); |
| 279 | } |
| 280 | |
| 281 | if (bus) { |
| 282 | if (!qdev_set_parent_bus(dev, bus, errp)) { |
| 283 | goto fail; |
| 284 | } |
| 285 | } else { |
| 286 | assert(!DEVICE_GET_CLASS(dev)->bus_type); |
| 287 | } |
| 288 | |
| 289 | if (object_property_set_bool(OBJECT(dev), "realized", true, errp)) { |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | fail: |
| 294 | if (unattached_parent) { |
| 295 | /* |
| 296 | * Beware, this doesn't just revert |
| 297 | * object_property_add_child(), it also runs bus_remove()! |
| 298 | */ |
| 299 | object_unparent(OBJECT(dev)); |
| 300 | unattached_count--; |
| 301 | } |
| 302 | |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp) |
| 307 | { |
| 308 | bool ret; |
| 309 | |
| 310 | ret = qdev_realize(dev, bus, errp); |
| 311 | object_unref(OBJECT(dev)); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | void qdev_unrealize(DeviceState *dev) |
| 316 | { |
| 317 | object_property_set_bool(OBJECT(dev), "realized", false, &error_abort); |
| 318 | } |
| 319 | |
| 320 | static int qdev_assert_realized_properly_cb(Object *obj, void *opaque) |
| 321 | { |
| 322 | DeviceState *dev = DEVICE(object_dynamic_cast(obj, TYPE_DEVICE)); |
| 323 | DeviceClass *dc; |
| 324 | |
| 325 | if (dev) { |
| 326 | dc = DEVICE_GET_CLASS(dev); |
| 327 | assert(dev->realized); |
| 328 | assert(dev->parent_bus || !dc->bus_type); |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | void qdev_assert_realized_properly(void) |
| 334 | { |
| 335 | object_child_foreach_recursive(object_get_root(), |
| 336 | qdev_assert_realized_properly_cb, NULL); |
| 337 | } |
| 338 | |
| 339 | bool qdev_machine_modified(void) |
| 340 | { |
| 341 | return qdev_hot_added || qdev_hot_removed; |
| 342 | } |
| 343 | |
| 344 | BusState *qdev_get_parent_bus(const DeviceState *dev) |
| 345 | { |
| 346 | return dev->parent_bus; |
| 347 | } |
| 348 | |
| 349 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name) |
| 350 | { |
| 351 | BusState *bus; |
| 352 | Object *child = object_resolve_path_component(OBJECT(dev), name); |
| 353 | |
| 354 | bus = (BusState *)object_dynamic_cast(child, TYPE_BUS); |
| 355 | if (bus) { |
| 356 | return bus; |
| 357 | } |
| 358 | |
| 359 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 360 | if (strcmp(name, bus->name) == 0) { |
| 361 | return bus; |
| 362 | } |
| 363 | } |
| 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | int qdev_walk_children(DeviceState *dev, |
| 368 | qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, |
| 369 | qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, |
| 370 | void *opaque) |
| 371 | { |
| 372 | BusState *bus; |
| 373 | int err; |
| 374 | |
| 375 | if (pre_devfn) { |
| 376 | err = pre_devfn(dev, opaque); |
| 377 | if (err) { |
| 378 | return err; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 383 | err = qbus_walk_children(bus, pre_devfn, pre_busfn, |
| 384 | post_devfn, post_busfn, opaque); |
| 385 | if (err < 0) { |
| 386 | return err; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (post_devfn) { |
| 391 | err = post_devfn(dev, opaque); |
| 392 | if (err) { |
| 393 | return err; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | DeviceState *qdev_find_recursive(BusState *bus, const char *id) |
| 401 | { |
| 402 | BusChild *kid; |
| 403 | DeviceState *ret; |
| 404 | BusState *child; |
| 405 | |
| 406 | WITH_RCU_READ_LOCK_GUARD() { |
| 407 | QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) { |
| 408 | DeviceState *dev = kid->child; |
| 409 | |
| 410 | if (dev->id && strcmp(dev->id, id) == 0) { |
| 411 | return dev; |
| 412 | } |
| 413 | |
| 414 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
| 415 | ret = qdev_find_recursive(child, id); |
| 416 | if (ret) { |
| 417 | return ret; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | char *qdev_get_dev_path(DeviceState *dev) |
| 426 | { |
| 427 | BusClass *bc; |
| 428 | |
| 429 | if (!dev || !dev->parent_bus) { |
| 430 | return NULL; |
| 431 | } |
| 432 | |
| 433 | bc = BUS_GET_CLASS(dev->parent_bus); |
| 434 | if (bc->get_dev_path) { |
| 435 | return bc->get_dev_path(dev); |
| 436 | } |
| 437 | |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | char *qdev_get_human_name(DeviceState *dev) |
| 442 | { |
| 443 | if (dev->id) { |
| 444 | return g_strdup(dev->id); |
| 445 | } |
| 446 | /* |
| 447 | * Fall back to a bus-specific device path, if the bus |
| 448 | * provides one (e.g. "PCI device 0000:00:04.0"). |
| 449 | */ |
| 450 | g_autofree char *path = qdev_get_dev_path(dev); |
| 451 | if (path) { |
| 452 | const char *bus_type = object_get_typename(OBJECT(dev->parent_bus)); |
| 453 | char *name = g_strdup_printf("%s device %s", bus_type, path); |
| 454 | return name; |
| 455 | } |
| 456 | |
| 457 | return object_get_canonical_path(OBJECT(dev)); |
| 458 | } |
| 459 | |
| 460 | void qdev_add_unplug_blocker(DeviceState *dev, Error *reason) |
| 461 | { |
| 462 | dev->unplug_blockers = g_slist_prepend(dev->unplug_blockers, reason); |
| 463 | } |
| 464 | |
| 465 | void qdev_del_unplug_blocker(DeviceState *dev, Error *reason) |
| 466 | { |
| 467 | dev->unplug_blockers = g_slist_remove(dev->unplug_blockers, reason); |
| 468 | } |
| 469 | |
| 470 | bool qdev_unplug_blocked(DeviceState *dev, Error **errp) |
| 471 | { |
| 472 | if (dev->unplug_blockers) { |
| 473 | error_propagate(errp, error_copy(dev->unplug_blockers->data)); |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | static bool device_get_realized(Object *obj, Error **errp) |
| 481 | { |
| 482 | DeviceState *dev = DEVICE(obj); |
| 483 | return dev->realized; |
| 484 | } |
| 485 | |
| 486 | static bool check_only_migratable(Object *obj, Error **errp) |
| 487 | { |
| 488 | DeviceClass *dc = DEVICE_GET_CLASS(obj); |
| 489 | |
| 490 | if (!vmstate_check_only_migratable(dc->vmsd)) { |
| 491 | error_setg(errp, "Device %s is not migratable, but " |
| 492 | "--only-migratable was specified", |
| 493 | object_get_typename(obj)); |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | return true; |
| 498 | } |
| 499 | |
| 500 | static void device_set_realized(Object *obj, bool value, Error **errp) |
| 501 | { |
| 502 | DeviceState *dev = DEVICE(obj); |
| 503 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
| 504 | HotplugHandler *hotplug_ctrl; |
| 505 | BusState *bus; |
| 506 | NamedClockList *ncl; |
| 507 | Error *local_err = NULL; |
| 508 | |
| 509 | if (dev->hotplugged && !dc->hotpluggable) { |
| 510 | error_setg(errp, "Device '%s' does not support hotplugging", |
| 511 | object_get_typename(obj)); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | if (value && !dev->realized) { |
| 516 | if (!check_only_migratable(obj, errp)) { |
| 517 | goto fail; |
| 518 | } |
| 519 | |
| 520 | hotplug_ctrl = qdev_get_hotplug_handler(dev); |
| 521 | if (hotplug_ctrl) { |
| 522 | hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err); |
| 523 | if (local_err != NULL) { |
| 524 | goto fail; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (dc->realize) { |
| 529 | dc->realize(dev, &local_err); |
| 530 | if (local_err != NULL) { |
| 531 | goto fail; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | DEVICE_LISTENER_CALL(realize, Forward, dev); |
| 536 | |
| 537 | /* |
| 538 | * always free/re-initialize here since the value cannot be cleaned up |
| 539 | * in device_unrealize due to its usage later on in the unplug path |
| 540 | */ |
| 541 | g_free(dev->canonical_path); |
| 542 | dev->canonical_path = object_get_canonical_path(OBJECT(dev)); |
| 543 | QLIST_FOREACH(ncl, &dev->clocks, node) { |
| 544 | if (ncl->alias) { |
| 545 | continue; |
| 546 | } else { |
| 547 | clock_setup_canonical_path(ncl->clock); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (qdev_get_vmsd(dev)) { |
| 552 | if (vmstate_register_with_alias_id(VMSTATE_IF(dev), |
| 553 | VMSTATE_INSTANCE_ID_ANY, |
| 554 | qdev_get_vmsd(dev), dev, |
| 555 | dev->instance_id_alias, |
| 556 | dev->alias_required_for_version, |
| 557 | &local_err) < 0) { |
| 558 | goto post_realize_fail; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Clear the reset state, in case the object was previously unrealized |
| 564 | * with a dirty state. |
| 565 | */ |
| 566 | resettable_state_clear(&dev->reset); |
| 567 | |
| 568 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 569 | if (!qbus_realize(bus, errp)) { |
| 570 | goto child_realize_fail; |
| 571 | } |
| 572 | } |
| 573 | if (dev->hotplugged) { |
| 574 | /* |
| 575 | * Reset the device, as well as its subtree which, at this point, |
| 576 | * should be realized too. |
| 577 | */ |
| 578 | resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD); |
| 579 | resettable_change_parent(OBJECT(dev), OBJECT(dev->parent_bus), |
| 580 | NULL); |
| 581 | resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD); |
| 582 | } |
| 583 | dev->pending_deleted_event = false; |
| 584 | |
| 585 | if (hotplug_ctrl) { |
| 586 | hotplug_handler_plug(hotplug_ctrl, dev, &local_err); |
| 587 | if (local_err != NULL) { |
| 588 | goto child_realize_fail; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | qatomic_store_release(&dev->realized, value); |
| 593 | |
| 594 | } else if (!value && dev->realized) { |
| 595 | |
| 596 | /* |
| 597 | * Change the value so that any concurrent users are aware |
| 598 | * that the device is going to be unrealized |
| 599 | * |
| 600 | * TODO: change .realized property to enum that states |
| 601 | * each phase of the device realization/unrealization |
| 602 | */ |
| 603 | |
| 604 | qatomic_set(&dev->realized, value); |
| 605 | /* |
| 606 | * Ensure that concurrent users see this update prior to |
| 607 | * any other changes done by unrealize. |
| 608 | */ |
| 609 | smp_wmb(); |
| 610 | |
| 611 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 612 | qbus_unrealize(bus); |
| 613 | } |
| 614 | if (qdev_get_vmsd(dev)) { |
| 615 | vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); |
| 616 | } |
| 617 | if (dc->unrealize) { |
| 618 | dc->unrealize(dev); |
| 619 | } |
| 620 | dev->pending_deleted_event = true; |
| 621 | DEVICE_LISTENER_CALL(unrealize, Reverse, dev); |
| 622 | } |
| 623 | |
| 624 | assert(local_err == NULL); |
| 625 | return; |
| 626 | |
| 627 | child_realize_fail: |
| 628 | QLIST_FOREACH(bus, &dev->child_bus, sibling) { |
| 629 | qbus_unrealize(bus); |
| 630 | } |
| 631 | |
| 632 | if (qdev_get_vmsd(dev)) { |
| 633 | vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); |
| 634 | } |
| 635 | |
| 636 | post_realize_fail: |
| 637 | g_free(dev->canonical_path); |
| 638 | dev->canonical_path = NULL; |
| 639 | if (dc->unrealize) { |
| 640 | dc->unrealize(dev); |
| 641 | } |
| 642 | |
| 643 | fail: |
| 644 | error_propagate(errp, local_err); |
| 645 | } |
| 646 | |
| 647 | static bool device_get_hotpluggable(Object *obj, Error **errp) |
| 648 | { |
| 649 | DeviceClass *dc = DEVICE_GET_CLASS(obj); |
| 650 | DeviceState *dev = DEVICE(obj); |
| 651 | |
| 652 | return dc->hotpluggable && (dev->parent_bus == NULL || |
| 653 | qbus_is_hotpluggable(dev->parent_bus)); |
| 654 | } |
| 655 | |
| 656 | static bool device_get_hotplugged(Object *obj, Error **errp) |
| 657 | { |
| 658 | DeviceState *dev = DEVICE(obj); |
| 659 | |
| 660 | return dev->hotplugged; |
| 661 | } |
| 662 | |
| 663 | static void device_initfn(Object *obj) |
| 664 | { |
| 665 | DeviceState *dev = DEVICE(obj); |
| 666 | |
| 667 | if (phase_check(PHASE_MACHINE_READY)) { |
| 668 | dev->hotplugged = 1; |
| 669 | qdev_hot_added = true; |
| 670 | } |
| 671 | |
| 672 | dev->instance_id_alias = -1; |
| 673 | dev->realized = false; |
| 674 | dev->allow_unplug_during_migration = false; |
| 675 | |
| 676 | QLIST_INIT(&dev->gpios); |
| 677 | QLIST_INIT(&dev->clocks); |
| 678 | } |
| 679 | |
| 680 | static void device_post_init(Object *obj) |
| 681 | { |
| 682 | /* |
| 683 | * Note: ordered so that the user's global properties take |
| 684 | * precedence. |
| 685 | */ |
| 686 | object_apply_compat_props(obj); |
| 687 | qdev_prop_set_globals(DEVICE(obj)); |
| 688 | } |
| 689 | |
| 690 | /* Unlink device from bus and free the structure. */ |
| 691 | static void device_finalize(Object *obj) |
| 692 | { |
| 693 | NamedGPIOList *ngl, *next; |
| 694 | |
| 695 | DeviceState *dev = DEVICE(obj); |
| 696 | |
| 697 | g_assert(!dev->unplug_blockers); |
| 698 | |
| 699 | QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) { |
| 700 | QLIST_REMOVE(ngl, node); |
| 701 | qemu_free_irqs(ngl->in, ngl->num_in); |
| 702 | g_free(ngl->name); |
| 703 | g_free(ngl); |
| 704 | /* ngl->out irqs are owned by the other end and should not be freed |
| 705 | * here |
| 706 | */ |
| 707 | } |
| 708 | |
| 709 | qdev_finalize_clocklist(dev); |
| 710 | |
| 711 | /* Only send event if the device had been completely realized */ |
| 712 | if (dev->pending_deleted_event) { |
| 713 | g_assert(dev->canonical_path); |
| 714 | |
| 715 | qapi_event_send_device_deleted(dev->id, dev->canonical_path); |
| 716 | g_free(dev->canonical_path); |
| 717 | dev->canonical_path = NULL; |
| 718 | } |
| 719 | |
| 720 | g_free(dev->id); |
| 721 | } |
| 722 | |
| 723 | static void device_class_base_init(ObjectClass *class, const void *data) |
| 724 | { |
| 725 | DeviceClass *klass = DEVICE_CLASS(class); |
| 726 | |
| 727 | /* We explicitly look up properties in the superclasses, |
| 728 | * so do not propagate them to the subclasses. |
| 729 | */ |
| 730 | klass->props_ = NULL; |
| 731 | klass->props_count_ = 0; |
| 732 | } |
| 733 | |
| 734 | static void device_unparent(Object *obj) |
| 735 | { |
| 736 | DeviceState *dev = DEVICE(obj); |
| 737 | BusState *bus; |
| 738 | |
| 739 | if (dev->realized) { |
| 740 | qdev_unrealize(dev); |
| 741 | } |
| 742 | while (dev->num_child_bus) { |
| 743 | bus = QLIST_FIRST(&dev->child_bus); |
| 744 | object_unparent(OBJECT(bus)); |
| 745 | } |
| 746 | if (dev->parent_bus) { |
| 747 | bus_remove_child(dev->parent_bus, dev); |
| 748 | object_unref(OBJECT(dev->parent_bus)); |
| 749 | dev->parent_bus = NULL; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | static char * |
| 754 | device_vmstate_if_get_id(VMStateIf *obj) |
| 755 | { |
| 756 | DeviceState *dev = DEVICE(obj); |
| 757 | |
| 758 | return qdev_get_dev_path(dev); |
| 759 | } |
| 760 | |
| 761 | static void device_class_init(ObjectClass *class, const void *data) |
| 762 | { |
| 763 | DeviceClass *dc = DEVICE_CLASS(class); |
| 764 | VMStateIfClass *vc = VMSTATE_IF_CLASS(class); |
| 765 | ResettableClass *rc = RESETTABLE_CLASS(class); |
| 766 | |
| 767 | class->unparent = device_unparent; |
| 768 | |
| 769 | /* by default all devices were considered as hotpluggable, |
| 770 | * so with intent to check it in generic qdev_unplug() / |
| 771 | * device_set_realized() functions make every device |
| 772 | * hotpluggable. Devices that shouldn't be hotpluggable, |
| 773 | * should override it in their class_init() |
| 774 | */ |
| 775 | dc->hotpluggable = true; |
| 776 | dc->user_creatable = true; |
| 777 | vc->get_id = device_vmstate_if_get_id; |
| 778 | rc->get_state = device_get_reset_state; |
| 779 | rc->child_foreach = device_reset_child_foreach; |
| 780 | |
| 781 | /* |
| 782 | * A NULL legacy_reset implies a three-phase reset device. Devices can |
| 783 | * only be reset using three-phase aware mechanisms, but we still support |
| 784 | * for transitional purposes leaf classes which set the old legacy_reset |
| 785 | * method via device_class_set_legacy_reset(). |
| 786 | */ |
| 787 | dc->legacy_reset = NULL; |
| 788 | |
| 789 | object_class_property_add_bool(class, "realized", |
| 790 | device_get_realized, device_set_realized); |
| 791 | object_class_property_add_bool(class, "hotpluggable", |
| 792 | device_get_hotpluggable, NULL); |
| 793 | object_class_property_add_bool(class, "hotplugged", |
| 794 | device_get_hotplugged, NULL); |
| 795 | object_class_property_add_link(class, "parent_bus", TYPE_BUS, |
| 796 | offsetof(DeviceState, parent_bus), NULL, 0); |
| 797 | } |
| 798 | |
| 799 | static void do_legacy_reset(Object *obj, ResetType type) |
| 800 | { |
| 801 | DeviceClass *dc = DEVICE_GET_CLASS(obj); |
| 802 | |
| 803 | dc->legacy_reset(DEVICE(obj)); |
| 804 | } |
| 805 | |
| 806 | void device_class_set_legacy_reset(DeviceClass *dc, DeviceReset dev_reset) |
| 807 | { |
| 808 | /* |
| 809 | * A legacy DeviceClass::reset has identical semantics to the |
| 810 | * three-phase "hold" method, with no "enter" or "exit" |
| 811 | * behaviour. Classes that use this legacy function must be leaf |
| 812 | * classes that do not chain up to their parent class reset. |
| 813 | * There is no mechanism for resetting a device that does not |
| 814 | * use the three-phase APIs, so the only place which calls |
| 815 | * the legacy_reset hook is do_legacy_reset(). |
| 816 | */ |
| 817 | ResettableClass *rc = RESETTABLE_CLASS(dc); |
| 818 | |
| 819 | rc->phases.enter = NULL; |
| 820 | rc->phases.hold = do_legacy_reset; |
| 821 | rc->phases.exit = NULL; |
| 822 | dc->legacy_reset = dev_reset; |
| 823 | } |
| 824 | |
| 825 | void device_class_set_parent_realize(DeviceClass *dc, |
| 826 | DeviceRealize dev_realize, |
| 827 | DeviceRealize *parent_realize) |
| 828 | { |
| 829 | *parent_realize = dc->realize; |
| 830 | dc->realize = dev_realize; |
| 831 | } |
| 832 | |
| 833 | void device_class_set_parent_unrealize(DeviceClass *dc, |
| 834 | DeviceUnrealize dev_unrealize, |
| 835 | DeviceUnrealize *parent_unrealize) |
| 836 | { |
| 837 | *parent_unrealize = dc->unrealize; |
| 838 | dc->unrealize = dev_unrealize; |
| 839 | } |
| 840 | |
| 841 | Object *qdev_get_machine(void) |
| 842 | { |
| 843 | static Object *dev; |
| 844 | |
| 845 | if (dev == NULL) { |
| 846 | dev = object_resolve_path_component(object_get_root(), "machine"); |
| 847 | /* |
| 848 | * Any call to this function before machine is created is treated |
| 849 | * as a programming error as of now. |
| 850 | */ |
| 851 | assert(dev); |
| 852 | } |
| 853 | |
| 854 | return dev; |
| 855 | } |
| 856 | |
| 857 | Object *machine_get_container(const char *name) |
| 858 | { |
| 859 | Object *container, *machine; |
| 860 | |
| 861 | machine = qdev_get_machine(); |
| 862 | container = object_resolve_path_component(machine, name); |
| 863 | assert(object_dynamic_cast(container, TYPE_CONTAINER)); |
| 864 | |
| 865 | return container; |
| 866 | } |
| 867 | |
| 868 | static MachineInitPhase machine_phase; |
| 869 | |
| 870 | bool phase_check(MachineInitPhase phase) |
| 871 | { |
| 872 | return machine_phase >= phase; |
| 873 | } |
| 874 | |
| 875 | void phase_advance(MachineInitPhase phase) |
| 876 | { |
| 877 | assert(machine_phase == phase - 1); |
| 878 | machine_phase = phase; |
| 879 | } |
| 880 | |
| 881 | static const TypeInfo device_type_info = { |
| 882 | .name = TYPE_DEVICE, |
| 883 | .parent = TYPE_OBJECT, |
| 884 | .instance_size = sizeof(DeviceState), |
| 885 | .instance_init = device_initfn, |
| 886 | .instance_post_init = device_post_init, |
| 887 | .instance_finalize = device_finalize, |
| 888 | .class_base_init = device_class_base_init, |
| 889 | .class_init = device_class_init, |
| 890 | .abstract = true, |
| 891 | .class_size = sizeof(DeviceClass), |
| 892 | .interfaces = (const InterfaceInfo[]) { |
| 893 | { TYPE_VMSTATE_IF }, |
| 894 | { TYPE_RESETTABLE_INTERFACE }, |
| 895 | { } |
| 896 | } |
| 897 | }; |
| 898 | |
| 899 | static void qdev_register_types(void) |
| 900 | { |
| 901 | type_register_static(&device_type_info); |
| 902 | } |
| 903 | |
| 904 | type_init(qdev_register_types) |