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