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 "qapi/qmp/qerror.h" 32 #include "qapi/visitor.h" 33 #include "qemu/error-report.h" 34 #include "qemu/option.h" 35 #include "hw/hotplug.h" 36 #include "hw/irq.h" 37 #include "hw/qdev-properties.h" 38 #include "hw/boards.h" 39 #include "hw/sysbus.h" 40 #include "hw/qdev-clock.h" 41 #include "migration/vmstate.h" 42 #include "trace.h" 43 44 bool qdev_hotplug = false; 45 static bool qdev_hot_added = false; 46 bool qdev_hot_removed = false; 47 48 const VMStateDescription *qdev_get_vmsd(DeviceState *dev) 49 { 50 DeviceClass *dc = DEVICE_GET_CLASS(dev); 51 return dc->vmsd; 52 } 53 54 static void bus_free_bus_child(BusChild *kid) 55 { 56 object_unref(OBJECT(kid->child)); 57 g_free(kid); 58 } 59 60 static void bus_remove_child(BusState *bus, DeviceState *child) 61 { 62 BusChild *kid; 63 64 QTAILQ_FOREACH(kid, &bus->children, sibling) { 65 if (kid->child == child) { 66 char name[32]; 67 68 snprintf(name, sizeof(name), "child[%d]", kid->index); 69 QTAILQ_REMOVE_RCU(&bus->children, kid, sibling); 70 71 bus->num_children--; 72 73 /* This gives back ownership of kid->child back to us. */ 74 object_property_del(OBJECT(bus), name); 75 76 /* free the bus kid, when it is safe to do so*/ 77 call_rcu(kid, bus_free_bus_child, rcu); 78 break; 79 } 80 } 81 } 82 83 static void bus_add_child(BusState *bus, DeviceState *child) 84 { 85 char name[32]; 86 BusChild *kid = g_malloc0(sizeof(*kid)); 87 88 bus->num_children++; 89 kid->index = bus->max_index++; 90 kid->child = child; 91 object_ref(OBJECT(kid->child)); 92 93 QTAILQ_INSERT_HEAD_RCU(&bus->children, kid, sibling); 94 95 /* This transfers ownership of kid->child to the property. */ 96 snprintf(name, sizeof(name), "child[%d]", kid->index); 97 object_property_add_link(OBJECT(bus), name, 98 object_get_typename(OBJECT(child)), 99 (Object **)&kid->child, 100 NULL, /* read-only property */ 101 0); 102 } 103 104 static bool bus_check_address(BusState *bus, DeviceState *child, Error **errp) 105 { 106 BusClass *bc = BUS_GET_CLASS(bus); 107 return !bc->check_address || bc->check_address(bus, child, errp); 108 } 109 110 bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp) 111 { 112 BusState *old_parent_bus = dev->parent_bus; 113 DeviceClass *dc = DEVICE_GET_CLASS(dev); 114 115 assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type)); 116 117 if (!bus_check_address(bus, dev, errp)) { 118 return false; 119 } 120 121 if (old_parent_bus) { 122 trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)), 123 old_parent_bus, object_get_typename(OBJECT(old_parent_bus)), 124 OBJECT(bus), object_get_typename(OBJECT(bus))); 125 /* 126 * Keep a reference to the device while it's not plugged into 127 * any bus, to avoid it potentially evaporating when it is 128 * dereffed in bus_remove_child(). 129 * Also keep the ref of the parent bus until the end, so that 130 * we can safely call resettable_change_parent() below. 131 */ 132 object_ref(OBJECT(dev)); 133 bus_remove_child(dev->parent_bus, dev); 134 } 135 dev->parent_bus = bus; 136 object_ref(OBJECT(bus)); 137 bus_add_child(bus, dev); 138 if (dev->realized) { 139 resettable_change_parent(OBJECT(dev), OBJECT(bus), 140 OBJECT(old_parent_bus)); 141 } 142 if (old_parent_bus) { 143 object_unref(OBJECT(old_parent_bus)); 144 object_unref(OBJECT(dev)); 145 } 146 return true; 147 } 148 149 DeviceState *qdev_new(const char *name) 150 { 151 if (!object_class_by_name(name)) { 152 module_load_qom_one(name); 153 } 154 return DEVICE(object_new(name)); 155 } 156 157 DeviceState *qdev_try_new(const char *name) 158 { 159 if (!module_object_class_by_name(name)) { 160 return NULL; 161 } 162 return DEVICE(object_new(name)); 163 } 164 165 static QTAILQ_HEAD(, DeviceListener) device_listeners 166 = QTAILQ_HEAD_INITIALIZER(device_listeners); 167 168 enum ListenerDirection { Forward, Reverse }; 169 170 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \ 171 do { \ 172 DeviceListener *_listener; \ 173 \ 174 switch (_direction) { \ 175 case Forward: \ 176 QTAILQ_FOREACH(_listener, &device_listeners, link) { \ 177 if (_listener->_callback) { \ 178 _listener->_callback(_listener, ##_args); \ 179 } \ 180 } \ 181 break; \ 182 case Reverse: \ 183 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \ 184 link) { \ 185 if (_listener->_callback) { \ 186 _listener->_callback(_listener, ##_args); \ 187 } \ 188 } \ 189 break; \ 190 default: \ 191 abort(); \ 192 } \ 193 } while (0) 194 195 static int device_listener_add(DeviceState *dev, void *opaque) 196 { 197 DEVICE_LISTENER_CALL(realize, Forward, dev); 198 199 return 0; 200 } 201 202 void device_listener_register(DeviceListener *listener) 203 { 204 QTAILQ_INSERT_TAIL(&device_listeners, listener, link); 205 206 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add, 207 NULL, NULL); 208 } 209 210 void device_listener_unregister(DeviceListener *listener) 211 { 212 QTAILQ_REMOVE(&device_listeners, listener, link); 213 } 214 215 bool qdev_should_hide_device(QemuOpts *opts) 216 { 217 int rc = -1; 218 DeviceListener *listener; 219 220 QTAILQ_FOREACH(listener, &device_listeners, link) { 221 if (listener->should_be_hidden) { 222 /* 223 * should_be_hidden_will return 224 * 1 if device matches opts and it should be hidden 225 * 0 if device matches opts and should not be hidden 226 * -1 if device doesn't match ops 227 */ 228 rc = listener->should_be_hidden(listener, opts); 229 } 230 231 if (rc > 0) { 232 break; 233 } 234 } 235 236 return rc > 0; 237 } 238 239 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, 240 int required_for_version) 241 { 242 assert(!dev->realized); 243 dev->instance_id_alias = alias_id; 244 dev->alias_required_for_version = required_for_version; 245 } 246 247 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev) 248 { 249 MachineState *machine; 250 MachineClass *mc; 251 Object *m_obj = qdev_get_machine(); 252 253 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) { 254 machine = MACHINE(m_obj); 255 mc = MACHINE_GET_CLASS(machine); 256 if (mc->get_hotplug_handler) { 257 return mc->get_hotplug_handler(machine, dev); 258 } 259 } 260 261 return NULL; 262 } 263 264 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp) 265 { 266 MachineState *machine; 267 MachineClass *mc; 268 Object *m_obj = qdev_get_machine(); 269 270 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) { 271 machine = MACHINE(m_obj); 272 mc = MACHINE_GET_CLASS(machine); 273 if (mc->hotplug_allowed) { 274 return mc->hotplug_allowed(machine, dev, errp); 275 } 276 } 277 278 return true; 279 } 280 281 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev) 282 { 283 if (dev->parent_bus) { 284 return dev->parent_bus->hotplug_handler; 285 } 286 return NULL; 287 } 288 289 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev) 290 { 291 HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev); 292 293 if (hotplug_ctrl == NULL && dev->parent_bus) { 294 hotplug_ctrl = qdev_get_bus_hotplug_handler(dev); 295 } 296 return hotplug_ctrl; 297 } 298 299 static int qdev_prereset(DeviceState *dev, void *opaque) 300 { 301 trace_qdev_reset_tree(dev, object_get_typename(OBJECT(dev))); 302 return 0; 303 } 304 305 static int qbus_prereset(BusState *bus, void *opaque) 306 { 307 trace_qbus_reset_tree(bus, object_get_typename(OBJECT(bus))); 308 return 0; 309 } 310 311 static int qdev_reset_one(DeviceState *dev, void *opaque) 312 { 313 device_legacy_reset(dev); 314 315 return 0; 316 } 317 318 static int qbus_reset_one(BusState *bus, void *opaque) 319 { 320 BusClass *bc = BUS_GET_CLASS(bus); 321 trace_qbus_reset(bus, object_get_typename(OBJECT(bus))); 322 if (bc->reset) { 323 bc->reset(bus); 324 } 325 return 0; 326 } 327 328 void qdev_reset_all(DeviceState *dev) 329 { 330 trace_qdev_reset_all(dev, object_get_typename(OBJECT(dev))); 331 qdev_walk_children(dev, qdev_prereset, qbus_prereset, 332 qdev_reset_one, qbus_reset_one, NULL); 333 } 334 335 void qdev_reset_all_fn(void *opaque) 336 { 337 qdev_reset_all(DEVICE(opaque)); 338 } 339 340 void qbus_reset_all(BusState *bus) 341 { 342 trace_qbus_reset_all(bus, object_get_typename(OBJECT(bus))); 343 qbus_walk_children(bus, qdev_prereset, qbus_prereset, 344 qdev_reset_one, qbus_reset_one, NULL); 345 } 346 347 void qbus_reset_all_fn(void *opaque) 348 { 349 BusState *bus = opaque; 350 qbus_reset_all(bus); 351 } 352 353 void device_cold_reset(DeviceState *dev) 354 { 355 resettable_reset(OBJECT(dev), RESET_TYPE_COLD); 356 } 357 358 bool device_is_in_reset(DeviceState *dev) 359 { 360 return resettable_is_in_reset(OBJECT(dev)); 361 } 362 363 static ResettableState *device_get_reset_state(Object *obj) 364 { 365 DeviceState *dev = DEVICE(obj); 366 return &dev->reset; 367 } 368 369 static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb, 370 void *opaque, ResetType type) 371 { 372 DeviceState *dev = DEVICE(obj); 373 BusState *bus; 374 375 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 376 cb(OBJECT(bus), opaque, type); 377 } 378 } 379 380 /* can be used as ->unplug() callback for the simple cases */ 381 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev, 382 DeviceState *dev, Error **errp) 383 { 384 qdev_unrealize(dev); 385 } 386 387 bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp) 388 { 389 assert(!dev->realized && !dev->parent_bus); 390 391 if (bus) { 392 if (!qdev_set_parent_bus(dev, bus, errp)) { 393 return false; 394 } 395 } else { 396 assert(!DEVICE_GET_CLASS(dev)->bus_type); 397 } 398 399 return object_property_set_bool(OBJECT(dev), "realized", true, errp); 400 } 401 402 bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp) 403 { 404 bool ret; 405 406 ret = qdev_realize(dev, bus, errp); 407 object_unref(OBJECT(dev)); 408 return ret; 409 } 410 411 void qdev_unrealize(DeviceState *dev) 412 { 413 object_property_set_bool(OBJECT(dev), "realized", false, &error_abort); 414 } 415 416 static int qdev_assert_realized_properly(Object *obj, void *opaque) 417 { 418 DeviceState *dev = DEVICE(object_dynamic_cast(obj, TYPE_DEVICE)); 419 DeviceClass *dc; 420 421 if (dev) { 422 dc = DEVICE_GET_CLASS(dev); 423 assert(dev->realized); 424 assert(dev->parent_bus || !dc->bus_type); 425 } 426 return 0; 427 } 428 429 void qdev_machine_creation_done(void) 430 { 431 /* 432 * ok, initial machine setup is done, starting from now we can 433 * only create hotpluggable devices 434 */ 435 qdev_hotplug = true; 436 437 object_child_foreach_recursive(object_get_root(), 438 qdev_assert_realized_properly, NULL); 439 } 440 441 bool qdev_machine_modified(void) 442 { 443 return qdev_hot_added || qdev_hot_removed; 444 } 445 446 BusState *qdev_get_parent_bus(DeviceState *dev) 447 { 448 return dev->parent_bus; 449 } 450 451 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev, 452 const char *name) 453 { 454 NamedGPIOList *ngl; 455 456 QLIST_FOREACH(ngl, &dev->gpios, node) { 457 /* NULL is a valid and matchable name. */ 458 if (g_strcmp0(name, ngl->name) == 0) { 459 return ngl; 460 } 461 } 462 463 ngl = g_malloc0(sizeof(*ngl)); 464 ngl->name = g_strdup(name); 465 QLIST_INSERT_HEAD(&dev->gpios, ngl, node); 466 return ngl; 467 } 468 469 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev, 470 qemu_irq_handler handler, 471 void *opaque, 472 const char *name, int n) 473 { 474 int i; 475 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name); 476 477 assert(gpio_list->num_out == 0 || !name); 478 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler, 479 opaque, n); 480 481 if (!name) { 482 name = "unnamed-gpio-in"; 483 } 484 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) { 485 gchar *propname = g_strdup_printf("%s[%u]", name, i); 486 487 object_property_add_child(OBJECT(dev), propname, 488 OBJECT(gpio_list->in[i])); 489 g_free(propname); 490 } 491 492 gpio_list->num_in += n; 493 } 494 495 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) 496 { 497 qdev_init_gpio_in_named(dev, handler, NULL, n); 498 } 499 500 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins, 501 const char *name, int n) 502 { 503 int i; 504 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name); 505 506 assert(gpio_list->num_in == 0 || !name); 507 508 if (!name) { 509 name = "unnamed-gpio-out"; 510 } 511 memset(pins, 0, sizeof(*pins) * n); 512 for (i = 0; i < n; ++i) { 513 gchar *propname = g_strdup_printf("%s[%u]", name, 514 gpio_list->num_out + i); 515 516 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ, 517 (Object **)&pins[i], 518 object_property_allow_set_link, 519 OBJ_PROP_LINK_STRONG); 520 g_free(propname); 521 } 522 gpio_list->num_out += n; 523 } 524 525 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) 526 { 527 qdev_init_gpio_out_named(dev, pins, NULL, n); 528 } 529 530 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n) 531 { 532 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name); 533 534 assert(n >= 0 && n < gpio_list->num_in); 535 return gpio_list->in[n]; 536 } 537 538 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) 539 { 540 return qdev_get_gpio_in_named(dev, NULL, n); 541 } 542 543 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n, 544 qemu_irq pin) 545 { 546 char *propname = g_strdup_printf("%s[%d]", 547 name ? name : "unnamed-gpio-out", n); 548 if (pin && !OBJECT(pin)->parent) { 549 /* We need a name for object_property_set_link to work */ 550 object_property_add_child(container_get(qdev_get_machine(), 551 "/unattached"), 552 "non-qdev-gpio[*]", OBJECT(pin)); 553 } 554 object_property_set_link(OBJECT(dev), propname, OBJECT(pin), &error_abort); 555 g_free(propname); 556 } 557 558 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n) 559 { 560 g_autofree char *propname = g_strdup_printf("%s[%d]", 561 name ? name : "unnamed-gpio-out", n); 562 563 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname, 564 NULL); 565 566 return ret; 567 } 568 569 /* disconnect a GPIO output, returning the disconnected input (if any) */ 570 571 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev, 572 const char *name, int n) 573 { 574 char *propname = g_strdup_printf("%s[%d]", 575 name ? name : "unnamed-gpio-out", n); 576 577 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname, 578 NULL); 579 if (ret) { 580 object_property_set_link(OBJECT(dev), propname, NULL, NULL); 581 } 582 g_free(propname); 583 return ret; 584 } 585 586 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt, 587 const char *name, int n) 588 { 589 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n); 590 qdev_connect_gpio_out_named(dev, name, n, icpt); 591 return disconnected; 592 } 593 594 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) 595 { 596 qdev_connect_gpio_out_named(dev, NULL, n, pin); 597 } 598 599 void qdev_pass_gpios(DeviceState *dev, DeviceState *container, 600 const char *name) 601 { 602 int i; 603 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name); 604 605 for (i = 0; i < ngl->num_in; i++) { 606 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in"; 607 char *propname = g_strdup_printf("%s[%d]", nm, i); 608 609 object_property_add_alias(OBJECT(container), propname, 610 OBJECT(dev), propname); 611 g_free(propname); 612 } 613 for (i = 0; i < ngl->num_out; i++) { 614 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out"; 615 char *propname = g_strdup_printf("%s[%d]", nm, i); 616 617 object_property_add_alias(OBJECT(container), propname, 618 OBJECT(dev), propname); 619 g_free(propname); 620 } 621 QLIST_REMOVE(ngl, node); 622 QLIST_INSERT_HEAD(&container->gpios, ngl, node); 623 } 624 625 BusState *qdev_get_child_bus(DeviceState *dev, const char *name) 626 { 627 BusState *bus; 628 Object *child = object_resolve_path_component(OBJECT(dev), name); 629 630 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS); 631 if (bus) { 632 return bus; 633 } 634 635 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 636 if (strcmp(name, bus->name) == 0) { 637 return bus; 638 } 639 } 640 return NULL; 641 } 642 643 int qdev_walk_children(DeviceState *dev, 644 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, 645 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, 646 void *opaque) 647 { 648 BusState *bus; 649 int err; 650 651 if (pre_devfn) { 652 err = pre_devfn(dev, opaque); 653 if (err) { 654 return err; 655 } 656 } 657 658 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 659 err = qbus_walk_children(bus, pre_devfn, pre_busfn, 660 post_devfn, post_busfn, opaque); 661 if (err < 0) { 662 return err; 663 } 664 } 665 666 if (post_devfn) { 667 err = post_devfn(dev, opaque); 668 if (err) { 669 return err; 670 } 671 } 672 673 return 0; 674 } 675 676 DeviceState *qdev_find_recursive(BusState *bus, const char *id) 677 { 678 BusChild *kid; 679 DeviceState *ret; 680 BusState *child; 681 682 WITH_RCU_READ_LOCK_GUARD() { 683 QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) { 684 DeviceState *dev = kid->child; 685 686 if (dev->id && strcmp(dev->id, id) == 0) { 687 return dev; 688 } 689 690 QLIST_FOREACH(child, &dev->child_bus, sibling) { 691 ret = qdev_find_recursive(child, id); 692 if (ret) { 693 return ret; 694 } 695 } 696 } 697 } 698 return NULL; 699 } 700 701 char *qdev_get_dev_path(DeviceState *dev) 702 { 703 BusClass *bc; 704 705 if (!dev || !dev->parent_bus) { 706 return NULL; 707 } 708 709 bc = BUS_GET_CLASS(dev->parent_bus); 710 if (bc->get_dev_path) { 711 return bc->get_dev_path(dev); 712 } 713 714 return NULL; 715 } 716 717 /** 718 * Legacy property handling 719 */ 720 721 static void qdev_get_legacy_property(Object *obj, Visitor *v, 722 const char *name, void *opaque, 723 Error **errp) 724 { 725 DeviceState *dev = DEVICE(obj); 726 Property *prop = opaque; 727 728 char buffer[1024]; 729 char *ptr = buffer; 730 731 prop->info->print(dev, prop, buffer, sizeof(buffer)); 732 visit_type_str(v, name, &ptr, errp); 733 } 734 735 /** 736 * qdev_class_add_legacy_property: 737 * @dev: Device to add the property to. 738 * @prop: The qdev property definition. 739 * 740 * Add a legacy QOM property to @dev for qdev property @prop. 741 * 742 * Legacy properties are string versions of QOM properties. The format of 743 * the string depends on the property type. Legacy properties are only 744 * needed for "info qtree". 745 * 746 * Do not use this in new code! QOM Properties added through this interface 747 * will be given names in the "legacy" namespace. 748 */ 749 static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop) 750 { 751 g_autofree char *name = NULL; 752 753 /* Register pointer properties as legacy properties */ 754 if (!prop->info->print && prop->info->get) { 755 return; 756 } 757 758 name = g_strdup_printf("legacy-%s", prop->name); 759 object_class_property_add(OBJECT_CLASS(dc), name, "str", 760 prop->info->print ? qdev_get_legacy_property : prop->info->get, 761 NULL, NULL, prop); 762 } 763 764 void qdev_property_add_static(DeviceState *dev, Property *prop) 765 { 766 Object *obj = OBJECT(dev); 767 ObjectProperty *op; 768 769 assert(!prop->info->create); 770 771 op = object_property_add(obj, prop->name, prop->info->name, 772 prop->info->get, prop->info->set, 773 prop->info->release, 774 prop); 775 776 object_property_set_description(obj, prop->name, 777 prop->info->description); 778 779 if (prop->set_default) { 780 prop->info->set_default_value(op, prop); 781 if (op->init) { 782 op->init(obj, op); 783 } 784 } 785 } 786 787 static void qdev_class_add_property(DeviceClass *klass, Property *prop) 788 { 789 ObjectClass *oc = OBJECT_CLASS(klass); 790 791 if (prop->info->create) { 792 prop->info->create(oc, prop); 793 } else { 794 ObjectProperty *op; 795 796 op = object_class_property_add(oc, 797 prop->name, prop->info->name, 798 prop->info->get, prop->info->set, 799 prop->info->release, 800 prop); 801 if (prop->set_default) { 802 prop->info->set_default_value(op, prop); 803 } 804 } 805 object_class_property_set_description(oc, prop->name, 806 prop->info->description); 807 } 808 809 void qdev_alias_all_properties(DeviceState *target, Object *source) 810 { 811 ObjectClass *class; 812 Property *prop; 813 814 class = object_get_class(OBJECT(target)); 815 do { 816 DeviceClass *dc = DEVICE_CLASS(class); 817 818 for (prop = dc->props_; prop && prop->name; prop++) { 819 object_property_add_alias(source, prop->name, 820 OBJECT(target), prop->name); 821 } 822 class = object_class_get_parent(class); 823 } while (class != object_class_by_name(TYPE_DEVICE)); 824 } 825 826 static bool device_get_realized(Object *obj, Error **errp) 827 { 828 DeviceState *dev = DEVICE(obj); 829 return dev->realized; 830 } 831 832 static bool check_only_migratable(Object *obj, Error **errp) 833 { 834 DeviceClass *dc = DEVICE_GET_CLASS(obj); 835 836 if (!vmstate_check_only_migratable(dc->vmsd)) { 837 error_setg(errp, "Device %s is not migratable, but " 838 "--only-migratable was specified", 839 object_get_typename(obj)); 840 return false; 841 } 842 843 return true; 844 } 845 846 static void device_set_realized(Object *obj, bool value, Error **errp) 847 { 848 DeviceState *dev = DEVICE(obj); 849 DeviceClass *dc = DEVICE_GET_CLASS(dev); 850 HotplugHandler *hotplug_ctrl; 851 BusState *bus; 852 NamedClockList *ncl; 853 Error *local_err = NULL; 854 bool unattached_parent = false; 855 static int unattached_count; 856 857 if (dev->hotplugged && !dc->hotpluggable) { 858 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj)); 859 return; 860 } 861 862 if (value && !dev->realized) { 863 if (!check_only_migratable(obj, errp)) { 864 goto fail; 865 } 866 867 if (!obj->parent) { 868 gchar *name = g_strdup_printf("device[%d]", unattached_count++); 869 870 object_property_add_child(container_get(qdev_get_machine(), 871 "/unattached"), 872 name, obj); 873 unattached_parent = true; 874 g_free(name); 875 } 876 877 hotplug_ctrl = qdev_get_hotplug_handler(dev); 878 if (hotplug_ctrl) { 879 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err); 880 if (local_err != NULL) { 881 goto fail; 882 } 883 } 884 885 if (dc->realize) { 886 dc->realize(dev, &local_err); 887 if (local_err != NULL) { 888 goto fail; 889 } 890 } 891 892 DEVICE_LISTENER_CALL(realize, Forward, dev); 893 894 /* 895 * always free/re-initialize here since the value cannot be cleaned up 896 * in device_unrealize due to its usage later on in the unplug path 897 */ 898 g_free(dev->canonical_path); 899 dev->canonical_path = object_get_canonical_path(OBJECT(dev)); 900 QLIST_FOREACH(ncl, &dev->clocks, node) { 901 if (ncl->alias) { 902 continue; 903 } else { 904 clock_setup_canonical_path(ncl->clock); 905 } 906 } 907 908 if (qdev_get_vmsd(dev)) { 909 if (vmstate_register_with_alias_id(VMSTATE_IF(dev), 910 VMSTATE_INSTANCE_ID_ANY, 911 qdev_get_vmsd(dev), dev, 912 dev->instance_id_alias, 913 dev->alias_required_for_version, 914 &local_err) < 0) { 915 goto post_realize_fail; 916 } 917 } 918 919 /* 920 * Clear the reset state, in case the object was previously unrealized 921 * with a dirty state. 922 */ 923 resettable_state_clear(&dev->reset); 924 925 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 926 if (!qbus_realize(bus, errp)) { 927 goto child_realize_fail; 928 } 929 } 930 if (dev->hotplugged) { 931 /* 932 * Reset the device, as well as its subtree which, at this point, 933 * should be realized too. 934 */ 935 resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD); 936 resettable_change_parent(OBJECT(dev), OBJECT(dev->parent_bus), 937 NULL); 938 resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD); 939 } 940 dev->pending_deleted_event = false; 941 942 if (hotplug_ctrl) { 943 hotplug_handler_plug(hotplug_ctrl, dev, &local_err); 944 if (local_err != NULL) { 945 goto child_realize_fail; 946 } 947 } 948 949 qatomic_store_release(&dev->realized, value); 950 951 } else if (!value && dev->realized) { 952 953 /* 954 * Change the value so that any concurrent users are aware 955 * that the device is going to be unrealized 956 * 957 * TODO: change .realized property to enum that states 958 * each phase of the device realization/unrealization 959 */ 960 961 qatomic_set(&dev->realized, value); 962 /* 963 * Ensure that concurrent users see this update prior to 964 * any other changes done by unrealize. 965 */ 966 smp_wmb(); 967 968 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 969 qbus_unrealize(bus); 970 } 971 if (qdev_get_vmsd(dev)) { 972 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); 973 } 974 if (dc->unrealize) { 975 dc->unrealize(dev); 976 } 977 dev->pending_deleted_event = true; 978 DEVICE_LISTENER_CALL(unrealize, Reverse, dev); 979 } 980 981 assert(local_err == NULL); 982 return; 983 984 child_realize_fail: 985 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 986 qbus_unrealize(bus); 987 } 988 989 if (qdev_get_vmsd(dev)) { 990 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev); 991 } 992 993 post_realize_fail: 994 g_free(dev->canonical_path); 995 dev->canonical_path = NULL; 996 if (dc->unrealize) { 997 dc->unrealize(dev); 998 } 999 1000 fail: 1001 error_propagate(errp, local_err); 1002 if (unattached_parent) { 1003 /* 1004 * Beware, this doesn't just revert 1005 * object_property_add_child(), it also runs bus_remove()! 1006 */ 1007 object_unparent(OBJECT(dev)); 1008 unattached_count--; 1009 } 1010 } 1011 1012 static bool device_get_hotpluggable(Object *obj, Error **errp) 1013 { 1014 DeviceClass *dc = DEVICE_GET_CLASS(obj); 1015 DeviceState *dev = DEVICE(obj); 1016 1017 return dc->hotpluggable && (dev->parent_bus == NULL || 1018 qbus_is_hotpluggable(dev->parent_bus)); 1019 } 1020 1021 static bool device_get_hotplugged(Object *obj, Error **errp) 1022 { 1023 DeviceState *dev = DEVICE(obj); 1024 1025 return dev->hotplugged; 1026 } 1027 1028 static void device_initfn(Object *obj) 1029 { 1030 DeviceState *dev = DEVICE(obj); 1031 1032 if (qdev_hotplug) { 1033 dev->hotplugged = 1; 1034 qdev_hot_added = true; 1035 } 1036 1037 dev->instance_id_alias = -1; 1038 dev->realized = false; 1039 dev->allow_unplug_during_migration = false; 1040 1041 QLIST_INIT(&dev->gpios); 1042 QLIST_INIT(&dev->clocks); 1043 } 1044 1045 static void device_post_init(Object *obj) 1046 { 1047 /* 1048 * Note: ordered so that the user's global properties take 1049 * precedence. 1050 */ 1051 object_apply_compat_props(obj); 1052 qdev_prop_set_globals(DEVICE(obj)); 1053 } 1054 1055 /* Unlink device from bus and free the structure. */ 1056 static void device_finalize(Object *obj) 1057 { 1058 NamedGPIOList *ngl, *next; 1059 1060 DeviceState *dev = DEVICE(obj); 1061 1062 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) { 1063 QLIST_REMOVE(ngl, node); 1064 qemu_free_irqs(ngl->in, ngl->num_in); 1065 g_free(ngl->name); 1066 g_free(ngl); 1067 /* ngl->out irqs are owned by the other end and should not be freed 1068 * here 1069 */ 1070 } 1071 1072 qdev_finalize_clocklist(dev); 1073 1074 /* Only send event if the device had been completely realized */ 1075 if (dev->pending_deleted_event) { 1076 g_assert(dev->canonical_path); 1077 1078 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path); 1079 g_free(dev->canonical_path); 1080 dev->canonical_path = NULL; 1081 } 1082 1083 qemu_opts_del(dev->opts); 1084 } 1085 1086 static void device_class_base_init(ObjectClass *class, void *data) 1087 { 1088 DeviceClass *klass = DEVICE_CLASS(class); 1089 1090 /* We explicitly look up properties in the superclasses, 1091 * so do not propagate them to the subclasses. 1092 */ 1093 klass->props_ = NULL; 1094 } 1095 1096 static void device_unparent(Object *obj) 1097 { 1098 DeviceState *dev = DEVICE(obj); 1099 BusState *bus; 1100 1101 if (dev->realized) { 1102 qdev_unrealize(dev); 1103 } 1104 while (dev->num_child_bus) { 1105 bus = QLIST_FIRST(&dev->child_bus); 1106 object_unparent(OBJECT(bus)); 1107 } 1108 if (dev->parent_bus) { 1109 bus_remove_child(dev->parent_bus, dev); 1110 object_unref(OBJECT(dev->parent_bus)); 1111 dev->parent_bus = NULL; 1112 } 1113 } 1114 1115 static char * 1116 device_vmstate_if_get_id(VMStateIf *obj) 1117 { 1118 DeviceState *dev = DEVICE(obj); 1119 1120 return qdev_get_dev_path(dev); 1121 } 1122 1123 /** 1124 * device_phases_reset: 1125 * Transition reset method for devices to allow moving 1126 * smoothly from legacy reset method to multi-phases 1127 */ 1128 static void device_phases_reset(DeviceState *dev) 1129 { 1130 ResettableClass *rc = RESETTABLE_GET_CLASS(dev); 1131 1132 if (rc->phases.enter) { 1133 rc->phases.enter(OBJECT(dev), RESET_TYPE_COLD); 1134 } 1135 if (rc->phases.hold) { 1136 rc->phases.hold(OBJECT(dev)); 1137 } 1138 if (rc->phases.exit) { 1139 rc->phases.exit(OBJECT(dev)); 1140 } 1141 } 1142 1143 static void device_transitional_reset(Object *obj) 1144 { 1145 DeviceClass *dc = DEVICE_GET_CLASS(obj); 1146 1147 /* 1148 * This will call either @device_phases_reset (for multi-phases transitioned 1149 * devices) or a device's specific method for not-yet transitioned devices. 1150 * In both case, it does not reset children. 1151 */ 1152 if (dc->reset) { 1153 dc->reset(DEVICE(obj)); 1154 } 1155 } 1156 1157 /** 1158 * device_get_transitional_reset: 1159 * check if the device's class is ready for multi-phase 1160 */ 1161 static ResettableTrFunction device_get_transitional_reset(Object *obj) 1162 { 1163 DeviceClass *dc = DEVICE_GET_CLASS(obj); 1164 if (dc->reset != device_phases_reset) { 1165 /* 1166 * dc->reset has been overridden by a subclass, 1167 * the device is not ready for multi phase yet. 1168 */ 1169 return device_transitional_reset; 1170 } 1171 return NULL; 1172 } 1173 1174 static void device_class_init(ObjectClass *class, void *data) 1175 { 1176 DeviceClass *dc = DEVICE_CLASS(class); 1177 VMStateIfClass *vc = VMSTATE_IF_CLASS(class); 1178 ResettableClass *rc = RESETTABLE_CLASS(class); 1179 1180 class->unparent = device_unparent; 1181 1182 /* by default all devices were considered as hotpluggable, 1183 * so with intent to check it in generic qdev_unplug() / 1184 * device_set_realized() functions make every device 1185 * hotpluggable. Devices that shouldn't be hotpluggable, 1186 * should override it in their class_init() 1187 */ 1188 dc->hotpluggable = true; 1189 dc->user_creatable = true; 1190 vc->get_id = device_vmstate_if_get_id; 1191 rc->get_state = device_get_reset_state; 1192 rc->child_foreach = device_reset_child_foreach; 1193 1194 /* 1195 * @device_phases_reset is put as the default reset method below, allowing 1196 * to do the multi-phase transition from base classes to leaf classes. It 1197 * allows a legacy-reset Device class to extend a multi-phases-reset 1198 * Device class for the following reason: 1199 * + If a base class B has been moved to multi-phase, then it does not 1200 * override this default reset method and may have defined phase methods. 1201 * + A child class C (extending class B) which uses 1202 * device_class_set_parent_reset() (or similar means) to override the 1203 * reset method will still work as expected. @device_phases_reset function 1204 * will be registered as the parent reset method and effectively call 1205 * parent reset phases. 1206 */ 1207 dc->reset = device_phases_reset; 1208 rc->get_transitional_function = device_get_transitional_reset; 1209 1210 object_class_property_add_bool(class, "realized", 1211 device_get_realized, device_set_realized); 1212 object_class_property_add_bool(class, "hotpluggable", 1213 device_get_hotpluggable, NULL); 1214 object_class_property_add_bool(class, "hotplugged", 1215 device_get_hotplugged, NULL); 1216 object_class_property_add_link(class, "parent_bus", TYPE_BUS, 1217 offsetof(DeviceState, parent_bus), NULL, 0); 1218 } 1219 1220 void device_class_set_props(DeviceClass *dc, Property *props) 1221 { 1222 Property *prop; 1223 1224 dc->props_ = props; 1225 for (prop = props; prop && prop->name; prop++) { 1226 qdev_class_add_legacy_property(dc, prop); 1227 qdev_class_add_property(dc, prop); 1228 } 1229 } 1230 1231 void device_class_set_parent_reset(DeviceClass *dc, 1232 DeviceReset dev_reset, 1233 DeviceReset *parent_reset) 1234 { 1235 *parent_reset = dc->reset; 1236 dc->reset = dev_reset; 1237 } 1238 1239 void device_class_set_parent_realize(DeviceClass *dc, 1240 DeviceRealize dev_realize, 1241 DeviceRealize *parent_realize) 1242 { 1243 *parent_realize = dc->realize; 1244 dc->realize = dev_realize; 1245 } 1246 1247 void device_class_set_parent_unrealize(DeviceClass *dc, 1248 DeviceUnrealize dev_unrealize, 1249 DeviceUnrealize *parent_unrealize) 1250 { 1251 *parent_unrealize = dc->unrealize; 1252 dc->unrealize = dev_unrealize; 1253 } 1254 1255 void device_legacy_reset(DeviceState *dev) 1256 { 1257 DeviceClass *klass = DEVICE_GET_CLASS(dev); 1258 1259 trace_qdev_reset(dev, object_get_typename(OBJECT(dev))); 1260 if (klass->reset) { 1261 klass->reset(dev); 1262 } 1263 } 1264 1265 Object *qdev_get_machine(void) 1266 { 1267 static Object *dev; 1268 1269 if (dev == NULL) { 1270 dev = container_get(object_get_root(), "/machine"); 1271 } 1272 1273 return dev; 1274 } 1275 1276 static const TypeInfo device_type_info = { 1277 .name = TYPE_DEVICE, 1278 .parent = TYPE_OBJECT, 1279 .instance_size = sizeof(DeviceState), 1280 .instance_init = device_initfn, 1281 .instance_post_init = device_post_init, 1282 .instance_finalize = device_finalize, 1283 .class_base_init = device_class_base_init, 1284 .class_init = device_class_init, 1285 .abstract = true, 1286 .class_size = sizeof(DeviceClass), 1287 .interfaces = (InterfaceInfo[]) { 1288 { TYPE_VMSTATE_IF }, 1289 { TYPE_RESETTABLE_INTERFACE }, 1290 { } 1291 } 1292 }; 1293 1294 static void qdev_register_types(void) 1295 { 1296 type_register_static(&device_type_info); 1297 } 1298 1299 type_init(qdev_register_types) 1300