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