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