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 "hw/qdev.h" 30 #include "sysemu/sysemu.h" 31 #include "qapi/error.h" 32 #include "qapi/qapi-events-misc.h" 33 #include "qapi/qmp/qerror.h" 34 #include "qapi/visitor.h" 35 #include "qemu/error-report.h" 36 #include "qemu/option.h" 37 #include "hw/hotplug.h" 38 #include "hw/boards.h" 39 #include "hw/sysbus.h" 40 41 bool qdev_hotplug = false; 42 static bool qdev_hot_added = false; 43 bool qdev_hot_removed = false; 44 45 const VMStateDescription *qdev_get_vmsd(DeviceState *dev) 46 { 47 DeviceClass *dc = DEVICE_GET_CLASS(dev); 48 return dc->vmsd; 49 } 50 51 static void bus_remove_child(BusState *bus, DeviceState *child) 52 { 53 BusChild *kid; 54 55 QTAILQ_FOREACH(kid, &bus->children, sibling) { 56 if (kid->child == child) { 57 char name[32]; 58 59 snprintf(name, sizeof(name), "child[%d]", kid->index); 60 QTAILQ_REMOVE(&bus->children, kid, sibling); 61 62 bus->num_children--; 63 64 /* This gives back ownership of kid->child back to us. */ 65 object_property_del(OBJECT(bus), name, NULL); 66 object_unref(OBJECT(kid->child)); 67 g_free(kid); 68 return; 69 } 70 } 71 } 72 73 static void bus_add_child(BusState *bus, DeviceState *child) 74 { 75 char name[32]; 76 BusChild *kid = g_malloc0(sizeof(*kid)); 77 78 bus->num_children++; 79 kid->index = bus->max_index++; 80 kid->child = child; 81 object_ref(OBJECT(kid->child)); 82 83 QTAILQ_INSERT_HEAD(&bus->children, kid, sibling); 84 85 /* This transfers ownership of kid->child to the property. */ 86 snprintf(name, sizeof(name), "child[%d]", kid->index); 87 object_property_add_link(OBJECT(bus), name, 88 object_get_typename(OBJECT(child)), 89 (Object **)&kid->child, 90 NULL, /* read-only property */ 91 0, /* return ownership on prop deletion */ 92 NULL); 93 } 94 95 void qdev_set_parent_bus(DeviceState *dev, BusState *bus) 96 { 97 bool replugging = dev->parent_bus != NULL; 98 99 if (replugging) { 100 /* Keep a reference to the device while it's not plugged into 101 * any bus, to avoid it potentially evaporating when it is 102 * dereffed in bus_remove_child(). 103 */ 104 object_ref(OBJECT(dev)); 105 bus_remove_child(dev->parent_bus, dev); 106 object_unref(OBJECT(dev->parent_bus)); 107 } 108 dev->parent_bus = bus; 109 object_ref(OBJECT(bus)); 110 bus_add_child(bus, dev); 111 if (replugging) { 112 object_unref(OBJECT(dev)); 113 } 114 } 115 116 /* Create a new device. This only initializes the device state 117 structure and allows properties to be set. The device still needs 118 to be realized. See qdev-core.h. */ 119 DeviceState *qdev_create(BusState *bus, const char *name) 120 { 121 DeviceState *dev; 122 123 dev = qdev_try_create(bus, name); 124 if (!dev) { 125 if (bus) { 126 error_report("Unknown device '%s' for bus '%s'", name, 127 object_get_typename(OBJECT(bus))); 128 } else { 129 error_report("Unknown device '%s' for default sysbus", name); 130 } 131 abort(); 132 } 133 134 return dev; 135 } 136 137 DeviceState *qdev_try_create(BusState *bus, const char *type) 138 { 139 DeviceState *dev; 140 141 if (object_class_by_name(type) == NULL) { 142 return NULL; 143 } 144 dev = DEVICE(object_new(type)); 145 if (!dev) { 146 return NULL; 147 } 148 149 if (!bus) { 150 /* Assert that the device really is a SysBusDevice before 151 * we put it onto the sysbus. Non-sysbus devices which aren't 152 * being put onto a bus should be created with object_new(TYPE_FOO), 153 * not qdev_create(NULL, TYPE_FOO). 154 */ 155 g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)); 156 bus = sysbus_get_default(); 157 } 158 159 qdev_set_parent_bus(dev, bus); 160 object_unref(OBJECT(dev)); 161 return dev; 162 } 163 164 static QTAILQ_HEAD(, DeviceListener) device_listeners 165 = QTAILQ_HEAD_INITIALIZER(device_listeners); 166 167 enum ListenerDirection { Forward, Reverse }; 168 169 #define DEVICE_LISTENER_CALL(_callback, _direction, _args...) \ 170 do { \ 171 DeviceListener *_listener; \ 172 \ 173 switch (_direction) { \ 174 case Forward: \ 175 QTAILQ_FOREACH(_listener, &device_listeners, link) { \ 176 if (_listener->_callback) { \ 177 _listener->_callback(_listener, ##_args); \ 178 } \ 179 } \ 180 break; \ 181 case Reverse: \ 182 QTAILQ_FOREACH_REVERSE(_listener, &device_listeners, \ 183 link) { \ 184 if (_listener->_callback) { \ 185 _listener->_callback(_listener, ##_args); \ 186 } \ 187 } \ 188 break; \ 189 default: \ 190 abort(); \ 191 } \ 192 } while (0) 193 194 static int device_listener_add(DeviceState *dev, void *opaque) 195 { 196 DEVICE_LISTENER_CALL(realize, Forward, dev); 197 198 return 0; 199 } 200 201 void device_listener_register(DeviceListener *listener) 202 { 203 QTAILQ_INSERT_TAIL(&device_listeners, listener, link); 204 205 qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add, 206 NULL, NULL); 207 } 208 209 void device_listener_unregister(DeviceListener *listener) 210 { 211 QTAILQ_REMOVE(&device_listeners, listener, link); 212 } 213 214 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, 215 int required_for_version) 216 { 217 assert(!dev->realized); 218 dev->instance_id_alias = alias_id; 219 dev->alias_required_for_version = required_for_version; 220 } 221 222 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev) 223 { 224 MachineState *machine; 225 MachineClass *mc; 226 Object *m_obj = qdev_get_machine(); 227 228 if (object_dynamic_cast(m_obj, TYPE_MACHINE)) { 229 machine = MACHINE(m_obj); 230 mc = MACHINE_GET_CLASS(machine); 231 if (mc->get_hotplug_handler) { 232 return mc->get_hotplug_handler(machine, dev); 233 } 234 } 235 236 return NULL; 237 } 238 239 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev) 240 { 241 HotplugHandler *hotplug_ctrl; 242 243 if (dev->parent_bus && dev->parent_bus->hotplug_handler) { 244 hotplug_ctrl = dev->parent_bus->hotplug_handler; 245 } else { 246 hotplug_ctrl = qdev_get_machine_hotplug_handler(dev); 247 } 248 return hotplug_ctrl; 249 } 250 251 static int qdev_reset_one(DeviceState *dev, void *opaque) 252 { 253 device_reset(dev); 254 255 return 0; 256 } 257 258 static int qbus_reset_one(BusState *bus, void *opaque) 259 { 260 BusClass *bc = BUS_GET_CLASS(bus); 261 if (bc->reset) { 262 bc->reset(bus); 263 } 264 return 0; 265 } 266 267 void qdev_reset_all(DeviceState *dev) 268 { 269 qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL); 270 } 271 272 void qdev_reset_all_fn(void *opaque) 273 { 274 qdev_reset_all(DEVICE(opaque)); 275 } 276 277 void qbus_reset_all(BusState *bus) 278 { 279 qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL); 280 } 281 282 void qbus_reset_all_fn(void *opaque) 283 { 284 BusState *bus = opaque; 285 qbus_reset_all(bus); 286 } 287 288 /* can be used as ->unplug() callback for the simple cases */ 289 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev, 290 DeviceState *dev, Error **errp) 291 { 292 /* just zap it */ 293 object_unparent(OBJECT(dev)); 294 } 295 296 /* 297 * Realize @dev. 298 * Device properties should be set before calling this function. IRQs 299 * and MMIO regions should be connected/mapped after calling this 300 * function. 301 * On failure, report an error with error_report() and terminate the 302 * program. This is okay during machine creation. Don't use for 303 * hotplug, because there callers need to recover from failure. 304 * Exception: if you know the device's init() callback can't fail, 305 * then qdev_init_nofail() can't fail either, and is therefore usable 306 * even then. But relying on the device implementation that way is 307 * somewhat unclean, and best avoided. 308 */ 309 void qdev_init_nofail(DeviceState *dev) 310 { 311 Error *err = NULL; 312 313 assert(!dev->realized); 314 315 object_ref(OBJECT(dev)); 316 object_property_set_bool(OBJECT(dev), true, "realized", &err); 317 if (err) { 318 error_reportf_err(err, "Initialization of device %s failed: ", 319 object_get_typename(OBJECT(dev))); 320 exit(1); 321 } 322 object_unref(OBJECT(dev)); 323 } 324 325 void qdev_machine_creation_done(void) 326 { 327 /* 328 * ok, initial machine setup is done, starting from now we can 329 * only create hotpluggable devices 330 */ 331 qdev_hotplug = true; 332 } 333 334 bool qdev_machine_modified(void) 335 { 336 return qdev_hot_added || qdev_hot_removed; 337 } 338 339 BusState *qdev_get_parent_bus(DeviceState *dev) 340 { 341 return dev->parent_bus; 342 } 343 344 static NamedGPIOList *qdev_get_named_gpio_list(DeviceState *dev, 345 const char *name) 346 { 347 NamedGPIOList *ngl; 348 349 QLIST_FOREACH(ngl, &dev->gpios, node) { 350 /* NULL is a valid and matchable name, otherwise do a normal 351 * strcmp match. 352 */ 353 if ((!ngl->name && !name) || 354 (name && ngl->name && strcmp(name, ngl->name) == 0)) { 355 return ngl; 356 } 357 } 358 359 ngl = g_malloc0(sizeof(*ngl)); 360 ngl->name = g_strdup(name); 361 QLIST_INSERT_HEAD(&dev->gpios, ngl, node); 362 return ngl; 363 } 364 365 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev, 366 qemu_irq_handler handler, 367 void *opaque, 368 const char *name, int n) 369 { 370 int i; 371 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name); 372 373 assert(gpio_list->num_out == 0 || !name); 374 gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler, 375 opaque, n); 376 377 if (!name) { 378 name = "unnamed-gpio-in"; 379 } 380 for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) { 381 gchar *propname = g_strdup_printf("%s[%u]", name, i); 382 383 object_property_add_child(OBJECT(dev), propname, 384 OBJECT(gpio_list->in[i]), &error_abort); 385 g_free(propname); 386 } 387 388 gpio_list->num_in += n; 389 } 390 391 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n) 392 { 393 qdev_init_gpio_in_named(dev, handler, NULL, n); 394 } 395 396 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins, 397 const char *name, int n) 398 { 399 int i; 400 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name); 401 402 assert(gpio_list->num_in == 0 || !name); 403 404 if (!name) { 405 name = "unnamed-gpio-out"; 406 } 407 memset(pins, 0, sizeof(*pins) * n); 408 for (i = 0; i < n; ++i) { 409 gchar *propname = g_strdup_printf("%s[%u]", name, 410 gpio_list->num_out + i); 411 412 object_property_add_link(OBJECT(dev), propname, TYPE_IRQ, 413 (Object **)&pins[i], 414 object_property_allow_set_link, 415 OBJ_PROP_LINK_STRONG, 416 &error_abort); 417 g_free(propname); 418 } 419 gpio_list->num_out += n; 420 } 421 422 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n) 423 { 424 qdev_init_gpio_out_named(dev, pins, NULL, n); 425 } 426 427 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n) 428 { 429 NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name); 430 431 assert(n >= 0 && n < gpio_list->num_in); 432 return gpio_list->in[n]; 433 } 434 435 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n) 436 { 437 return qdev_get_gpio_in_named(dev, NULL, n); 438 } 439 440 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n, 441 qemu_irq pin) 442 { 443 char *propname = g_strdup_printf("%s[%d]", 444 name ? name : "unnamed-gpio-out", n); 445 if (pin) { 446 /* We need a name for object_property_set_link to work. If the 447 * object has a parent, object_property_add_child will come back 448 * with an error without doing anything. If it has none, it will 449 * never fail. So we can just call it with a NULL Error pointer. 450 */ 451 object_property_add_child(container_get(qdev_get_machine(), 452 "/unattached"), 453 "non-qdev-gpio[*]", OBJECT(pin), NULL); 454 } 455 object_property_set_link(OBJECT(dev), OBJECT(pin), propname, &error_abort); 456 g_free(propname); 457 } 458 459 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n) 460 { 461 char *propname = g_strdup_printf("%s[%d]", 462 name ? name : "unnamed-gpio-out", n); 463 464 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname, 465 NULL); 466 467 return ret; 468 } 469 470 /* disconnect a GPIO output, returning the disconnected input (if any) */ 471 472 static qemu_irq qdev_disconnect_gpio_out_named(DeviceState *dev, 473 const char *name, int n) 474 { 475 char *propname = g_strdup_printf("%s[%d]", 476 name ? name : "unnamed-gpio-out", n); 477 478 qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), propname, 479 NULL); 480 if (ret) { 481 object_property_set_link(OBJECT(dev), NULL, propname, NULL); 482 } 483 g_free(propname); 484 return ret; 485 } 486 487 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt, 488 const char *name, int n) 489 { 490 qemu_irq disconnected = qdev_disconnect_gpio_out_named(dev, name, n); 491 qdev_connect_gpio_out_named(dev, name, n, icpt); 492 return disconnected; 493 } 494 495 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin) 496 { 497 qdev_connect_gpio_out_named(dev, NULL, n, pin); 498 } 499 500 void qdev_pass_gpios(DeviceState *dev, DeviceState *container, 501 const char *name) 502 { 503 int i; 504 NamedGPIOList *ngl = qdev_get_named_gpio_list(dev, name); 505 506 for (i = 0; i < ngl->num_in; i++) { 507 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-in"; 508 char *propname = g_strdup_printf("%s[%d]", nm, i); 509 510 object_property_add_alias(OBJECT(container), propname, 511 OBJECT(dev), propname, 512 &error_abort); 513 g_free(propname); 514 } 515 for (i = 0; i < ngl->num_out; i++) { 516 const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out"; 517 char *propname = g_strdup_printf("%s[%d]", nm, i); 518 519 object_property_add_alias(OBJECT(container), propname, 520 OBJECT(dev), propname, 521 &error_abort); 522 g_free(propname); 523 } 524 QLIST_REMOVE(ngl, node); 525 QLIST_INSERT_HEAD(&container->gpios, ngl, node); 526 } 527 528 BusState *qdev_get_child_bus(DeviceState *dev, const char *name) 529 { 530 BusState *bus; 531 Object *child = object_resolve_path_component(OBJECT(dev), name); 532 533 bus = (BusState *)object_dynamic_cast(child, TYPE_BUS); 534 if (bus) { 535 return bus; 536 } 537 538 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 539 if (strcmp(name, bus->name) == 0) { 540 return bus; 541 } 542 } 543 return NULL; 544 } 545 546 int qdev_walk_children(DeviceState *dev, 547 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, 548 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, 549 void *opaque) 550 { 551 BusState *bus; 552 int err; 553 554 if (pre_devfn) { 555 err = pre_devfn(dev, opaque); 556 if (err) { 557 return err; 558 } 559 } 560 561 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 562 err = qbus_walk_children(bus, pre_devfn, pre_busfn, 563 post_devfn, post_busfn, opaque); 564 if (err < 0) { 565 return err; 566 } 567 } 568 569 if (post_devfn) { 570 err = post_devfn(dev, opaque); 571 if (err) { 572 return err; 573 } 574 } 575 576 return 0; 577 } 578 579 DeviceState *qdev_find_recursive(BusState *bus, const char *id) 580 { 581 BusChild *kid; 582 DeviceState *ret; 583 BusState *child; 584 585 QTAILQ_FOREACH(kid, &bus->children, sibling) { 586 DeviceState *dev = kid->child; 587 588 if (dev->id && strcmp(dev->id, id) == 0) { 589 return dev; 590 } 591 592 QLIST_FOREACH(child, &dev->child_bus, sibling) { 593 ret = qdev_find_recursive(child, id); 594 if (ret) { 595 return ret; 596 } 597 } 598 } 599 return NULL; 600 } 601 602 char *qdev_get_dev_path(DeviceState *dev) 603 { 604 BusClass *bc; 605 606 if (!dev || !dev->parent_bus) { 607 return NULL; 608 } 609 610 bc = BUS_GET_CLASS(dev->parent_bus); 611 if (bc->get_dev_path) { 612 return bc->get_dev_path(dev); 613 } 614 615 return NULL; 616 } 617 618 /** 619 * Legacy property handling 620 */ 621 622 static void qdev_get_legacy_property(Object *obj, Visitor *v, 623 const char *name, void *opaque, 624 Error **errp) 625 { 626 DeviceState *dev = DEVICE(obj); 627 Property *prop = opaque; 628 629 char buffer[1024]; 630 char *ptr = buffer; 631 632 prop->info->print(dev, prop, buffer, sizeof(buffer)); 633 visit_type_str(v, name, &ptr, errp); 634 } 635 636 /** 637 * qdev_property_add_legacy: 638 * @dev: Device to add the property to. 639 * @prop: The qdev property definition. 640 * @errp: location to store error information. 641 * 642 * Add a legacy QOM property to @dev for qdev property @prop. 643 * On error, store error in @errp. 644 * 645 * Legacy properties are string versions of QOM properties. The format of 646 * the string depends on the property type. Legacy properties are only 647 * needed for "info qtree". 648 * 649 * Do not use this in new code! QOM Properties added through this interface 650 * will be given names in the "legacy" namespace. 651 */ 652 static void qdev_property_add_legacy(DeviceState *dev, Property *prop, 653 Error **errp) 654 { 655 gchar *name; 656 657 /* Register pointer properties as legacy properties */ 658 if (!prop->info->print && prop->info->get) { 659 return; 660 } 661 662 if (prop->info->create) { 663 return; 664 } 665 666 name = g_strdup_printf("legacy-%s", prop->name); 667 object_property_add(OBJECT(dev), name, "str", 668 prop->info->print ? qdev_get_legacy_property : prop->info->get, 669 NULL, 670 NULL, 671 prop, errp); 672 673 g_free(name); 674 } 675 676 /** 677 * qdev_property_add_static: 678 * @dev: Device to add the property to. 679 * @prop: The qdev property definition. 680 * @errp: location to store error information. 681 * 682 * Add a static QOM property to @dev for qdev property @prop. 683 * On error, store error in @errp. Static properties access data in a struct. 684 * The type of the QOM property is derived from prop->info. 685 */ 686 void qdev_property_add_static(DeviceState *dev, Property *prop, 687 Error **errp) 688 { 689 Error *local_err = NULL; 690 Object *obj = OBJECT(dev); 691 692 if (prop->info->create) { 693 prop->info->create(obj, prop, &local_err); 694 } else { 695 /* 696 * TODO qdev_prop_ptr does not have getters or setters. It must 697 * go now that it can be replaced with links. The test should be 698 * removed along with it: all static properties are read/write. 699 */ 700 if (!prop->info->get && !prop->info->set) { 701 return; 702 } 703 object_property_add(obj, prop->name, prop->info->name, 704 prop->info->get, prop->info->set, 705 prop->info->release, 706 prop, &local_err); 707 } 708 709 if (local_err) { 710 error_propagate(errp, local_err); 711 return; 712 } 713 714 object_property_set_description(obj, prop->name, 715 prop->info->description, 716 &error_abort); 717 718 if (prop->set_default) { 719 prop->info->set_default_value(obj, prop); 720 } 721 } 722 723 /* @qdev_alias_all_properties - Add alias properties to the source object for 724 * all qdev properties on the target DeviceState. 725 */ 726 void qdev_alias_all_properties(DeviceState *target, Object *source) 727 { 728 ObjectClass *class; 729 Property *prop; 730 731 class = object_get_class(OBJECT(target)); 732 do { 733 DeviceClass *dc = DEVICE_CLASS(class); 734 735 for (prop = dc->props; prop && prop->name; prop++) { 736 object_property_add_alias(source, prop->name, 737 OBJECT(target), prop->name, 738 &error_abort); 739 } 740 class = object_class_get_parent(class); 741 } while (class != object_class_by_name(TYPE_DEVICE)); 742 } 743 744 static int qdev_add_hotpluggable_device(Object *obj, void *opaque) 745 { 746 GSList **list = opaque; 747 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj), 748 TYPE_DEVICE); 749 750 if (dev == NULL) { 751 return 0; 752 } 753 754 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) { 755 *list = g_slist_append(*list, dev); 756 } 757 758 return 0; 759 } 760 761 GSList *qdev_build_hotpluggable_device_list(Object *peripheral) 762 { 763 GSList *list = NULL; 764 765 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list); 766 767 return list; 768 } 769 770 static bool device_get_realized(Object *obj, Error **errp) 771 { 772 DeviceState *dev = DEVICE(obj); 773 return dev->realized; 774 } 775 776 static bool check_only_migratable(Object *obj, Error **err) 777 { 778 DeviceClass *dc = DEVICE_GET_CLASS(obj); 779 780 if (!vmstate_check_only_migratable(dc->vmsd)) { 781 error_setg(err, "Device %s is not migratable, but " 782 "--only-migratable was specified", 783 object_get_typename(obj)); 784 return false; 785 } 786 787 return true; 788 } 789 790 static void device_set_realized(Object *obj, bool value, Error **errp) 791 { 792 DeviceState *dev = DEVICE(obj); 793 DeviceClass *dc = DEVICE_GET_CLASS(dev); 794 HotplugHandler *hotplug_ctrl; 795 BusState *bus; 796 Error *local_err = NULL; 797 bool unattached_parent = false; 798 static int unattached_count; 799 800 if (dev->hotplugged && !dc->hotpluggable) { 801 error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj)); 802 return; 803 } 804 805 if (value && !dev->realized) { 806 if (!check_only_migratable(obj, &local_err)) { 807 goto fail; 808 } 809 810 if (!obj->parent) { 811 gchar *name = g_strdup_printf("device[%d]", unattached_count++); 812 813 object_property_add_child(container_get(qdev_get_machine(), 814 "/unattached"), 815 name, obj, &error_abort); 816 unattached_parent = true; 817 g_free(name); 818 } 819 820 hotplug_ctrl = qdev_get_hotplug_handler(dev); 821 if (hotplug_ctrl) { 822 hotplug_handler_pre_plug(hotplug_ctrl, dev, &local_err); 823 if (local_err != NULL) { 824 goto fail; 825 } 826 } 827 828 if (dc->realize) { 829 dc->realize(dev, &local_err); 830 } 831 832 if (local_err != NULL) { 833 goto fail; 834 } 835 836 DEVICE_LISTENER_CALL(realize, Forward, dev); 837 838 /* 839 * always free/re-initialize here since the value cannot be cleaned up 840 * in device_unrealize due to its usage later on in the unplug path 841 */ 842 g_free(dev->canonical_path); 843 dev->canonical_path = object_get_canonical_path(OBJECT(dev)); 844 845 if (qdev_get_vmsd(dev)) { 846 if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, 847 dev->instance_id_alias, 848 dev->alias_required_for_version, 849 &local_err) < 0) { 850 goto post_realize_fail; 851 } 852 } 853 854 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 855 object_property_set_bool(OBJECT(bus), true, "realized", 856 &local_err); 857 if (local_err != NULL) { 858 goto child_realize_fail; 859 } 860 } 861 if (dev->hotplugged) { 862 device_reset(dev); 863 } 864 dev->pending_deleted_event = false; 865 866 if (hotplug_ctrl) { 867 hotplug_handler_plug(hotplug_ctrl, dev, &local_err); 868 if (local_err != NULL) { 869 goto child_realize_fail; 870 } 871 } 872 873 } else if (!value && dev->realized) { 874 Error **local_errp = NULL; 875 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 876 local_errp = local_err ? NULL : &local_err; 877 object_property_set_bool(OBJECT(bus), false, "realized", 878 local_errp); 879 } 880 if (qdev_get_vmsd(dev)) { 881 vmstate_unregister(dev, qdev_get_vmsd(dev), dev); 882 } 883 if (dc->unrealize) { 884 local_errp = local_err ? NULL : &local_err; 885 dc->unrealize(dev, local_errp); 886 } 887 dev->pending_deleted_event = true; 888 DEVICE_LISTENER_CALL(unrealize, Reverse, dev); 889 } 890 891 if (local_err != NULL) { 892 goto fail; 893 } 894 895 dev->realized = value; 896 return; 897 898 child_realize_fail: 899 QLIST_FOREACH(bus, &dev->child_bus, sibling) { 900 object_property_set_bool(OBJECT(bus), false, "realized", 901 NULL); 902 } 903 904 if (qdev_get_vmsd(dev)) { 905 vmstate_unregister(dev, qdev_get_vmsd(dev), dev); 906 } 907 908 post_realize_fail: 909 g_free(dev->canonical_path); 910 dev->canonical_path = NULL; 911 if (dc->unrealize) { 912 dc->unrealize(dev, NULL); 913 } 914 915 fail: 916 error_propagate(errp, local_err); 917 if (unattached_parent) { 918 object_unparent(OBJECT(dev)); 919 unattached_count--; 920 } 921 } 922 923 static bool device_get_hotpluggable(Object *obj, Error **errp) 924 { 925 DeviceClass *dc = DEVICE_GET_CLASS(obj); 926 DeviceState *dev = DEVICE(obj); 927 928 return dc->hotpluggable && (dev->parent_bus == NULL || 929 qbus_is_hotpluggable(dev->parent_bus)); 930 } 931 932 static bool device_get_hotplugged(Object *obj, Error **err) 933 { 934 DeviceState *dev = DEVICE(obj); 935 936 return dev->hotplugged; 937 } 938 939 static void device_initfn(Object *obj) 940 { 941 DeviceState *dev = DEVICE(obj); 942 ObjectClass *class; 943 Property *prop; 944 945 if (qdev_hotplug) { 946 dev->hotplugged = 1; 947 qdev_hot_added = true; 948 } 949 950 dev->instance_id_alias = -1; 951 dev->realized = false; 952 953 object_property_add_bool(obj, "realized", 954 device_get_realized, device_set_realized, NULL); 955 object_property_add_bool(obj, "hotpluggable", 956 device_get_hotpluggable, NULL, NULL); 957 object_property_add_bool(obj, "hotplugged", 958 device_get_hotplugged, NULL, 959 &error_abort); 960 961 class = object_get_class(OBJECT(dev)); 962 do { 963 for (prop = DEVICE_CLASS(class)->props; prop && prop->name; prop++) { 964 qdev_property_add_legacy(dev, prop, &error_abort); 965 qdev_property_add_static(dev, prop, &error_abort); 966 } 967 class = object_class_get_parent(class); 968 } while (class != object_class_by_name(TYPE_DEVICE)); 969 970 object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS, 971 (Object **)&dev->parent_bus, NULL, 0, 972 &error_abort); 973 QLIST_INIT(&dev->gpios); 974 } 975 976 void object_apply_compat_props(Object *obj) 977 { 978 if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) { 979 MachineState *m = MACHINE(qdev_get_machine()); 980 MachineClass *mc = MACHINE_GET_CLASS(m); 981 982 if (m->accelerator) { 983 AccelClass *ac = ACCEL_GET_CLASS(m->accelerator); 984 985 if (ac->compat_props) { 986 object_apply_global_props(obj, ac->compat_props, &error_abort); 987 } 988 } 989 object_apply_global_props(obj, mc->compat_props, &error_abort); 990 } 991 } 992 993 static void device_post_init(Object *obj) 994 { 995 object_apply_compat_props(obj); 996 qdev_prop_set_globals(DEVICE(obj)); 997 } 998 999 /* Unlink device from bus and free the structure. */ 1000 static void device_finalize(Object *obj) 1001 { 1002 NamedGPIOList *ngl, *next; 1003 1004 DeviceState *dev = DEVICE(obj); 1005 1006 QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) { 1007 QLIST_REMOVE(ngl, node); 1008 qemu_free_irqs(ngl->in, ngl->num_in); 1009 g_free(ngl->name); 1010 g_free(ngl); 1011 /* ngl->out irqs are owned by the other end and should not be freed 1012 * here 1013 */ 1014 } 1015 1016 /* Only send event if the device had been completely realized */ 1017 if (dev->pending_deleted_event) { 1018 g_assert(dev->canonical_path); 1019 1020 qapi_event_send_device_deleted(!!dev->id, dev->id, dev->canonical_path); 1021 g_free(dev->canonical_path); 1022 dev->canonical_path = NULL; 1023 } 1024 1025 qemu_opts_del(dev->opts); 1026 } 1027 1028 static void device_class_base_init(ObjectClass *class, void *data) 1029 { 1030 DeviceClass *klass = DEVICE_CLASS(class); 1031 1032 /* We explicitly look up properties in the superclasses, 1033 * so do not propagate them to the subclasses. 1034 */ 1035 klass->props = NULL; 1036 } 1037 1038 static void device_unparent(Object *obj) 1039 { 1040 DeviceState *dev = DEVICE(obj); 1041 BusState *bus; 1042 1043 if (dev->realized) { 1044 object_property_set_bool(obj, false, "realized", NULL); 1045 } 1046 while (dev->num_child_bus) { 1047 bus = QLIST_FIRST(&dev->child_bus); 1048 object_unparent(OBJECT(bus)); 1049 } 1050 if (dev->parent_bus) { 1051 bus_remove_child(dev->parent_bus, dev); 1052 object_unref(OBJECT(dev->parent_bus)); 1053 dev->parent_bus = NULL; 1054 } 1055 } 1056 1057 static void device_class_init(ObjectClass *class, void *data) 1058 { 1059 DeviceClass *dc = DEVICE_CLASS(class); 1060 1061 class->unparent = device_unparent; 1062 1063 /* by default all devices were considered as hotpluggable, 1064 * so with intent to check it in generic qdev_unplug() / 1065 * device_set_realized() functions make every device 1066 * hotpluggable. Devices that shouldn't be hotpluggable, 1067 * should override it in their class_init() 1068 */ 1069 dc->hotpluggable = true; 1070 dc->user_creatable = true; 1071 } 1072 1073 void device_class_set_parent_reset(DeviceClass *dc, 1074 DeviceReset dev_reset, 1075 DeviceReset *parent_reset) 1076 { 1077 *parent_reset = dc->reset; 1078 dc->reset = dev_reset; 1079 } 1080 1081 void device_class_set_parent_realize(DeviceClass *dc, 1082 DeviceRealize dev_realize, 1083 DeviceRealize *parent_realize) 1084 { 1085 *parent_realize = dc->realize; 1086 dc->realize = dev_realize; 1087 } 1088 1089 void device_class_set_parent_unrealize(DeviceClass *dc, 1090 DeviceUnrealize dev_unrealize, 1091 DeviceUnrealize *parent_unrealize) 1092 { 1093 *parent_unrealize = dc->unrealize; 1094 dc->unrealize = dev_unrealize; 1095 } 1096 1097 void device_reset(DeviceState *dev) 1098 { 1099 DeviceClass *klass = DEVICE_GET_CLASS(dev); 1100 1101 if (klass->reset) { 1102 klass->reset(dev); 1103 } 1104 } 1105 1106 Object *qdev_get_machine(void) 1107 { 1108 static Object *dev; 1109 1110 if (dev == NULL) { 1111 dev = container_get(object_get_root(), "/machine"); 1112 } 1113 1114 return dev; 1115 } 1116 1117 static const TypeInfo device_type_info = { 1118 .name = TYPE_DEVICE, 1119 .parent = TYPE_OBJECT, 1120 .instance_size = sizeof(DeviceState), 1121 .instance_init = device_initfn, 1122 .instance_post_init = device_post_init, 1123 .instance_finalize = device_finalize, 1124 .class_base_init = device_class_base_init, 1125 .class_init = device_class_init, 1126 .abstract = true, 1127 .class_size = sizeof(DeviceClass), 1128 }; 1129 1130 static void qdev_register_types(void) 1131 { 1132 type_register_static(&device_type_info); 1133 } 1134 1135 type_init(qdev_register_types) 1136