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