qemu/include/hw/qdev-core.h
<<
>>
Prefs
   1#ifndef QDEV_CORE_H
   2#define QDEV_CORE_H
   3
   4#include "qemu/atomic.h"
   5#include "qemu/queue.h"
   6#include "qemu/bitmap.h"
   7#include "qemu/rcu.h"
   8#include "qemu/rcu_queue.h"
   9#include "qom/object.h"
  10#include "hw/hotplug.h"
  11#include "hw/resettable.h"
  12
  13/**
  14 * DOC: The QEMU Device API
  15 *
  16 * All modern devices should represented as a derived QOM class of
  17 * TYPE_DEVICE. The device API introduces the additional methods of
  18 * @realize and @unrealize to represent additional stages in a device
  19 * objects life cycle.
  20 *
  21 * Realization
  22 * -----------
  23 *
  24 * Devices are constructed in two stages:
  25 *
  26 * 1) object instantiation via object_initialize() and
  27 * 2) device realization via the #DeviceState.realized property
  28 *
  29 * The former may not fail (and must not abort or exit, since it is called
  30 * during device introspection already), and the latter may return error
  31 * information to the caller and must be re-entrant.
  32 * Trivial field initializations should go into #TypeInfo.instance_init.
  33 * Operations depending on @props static properties should go into @realize.
  34 * After successful realization, setting static properties will fail.
  35 *
  36 * As an interim step, the #DeviceState.realized property can also be
  37 * set with qdev_realize(). In the future, devices will propagate this
  38 * state change to their children and along busses they expose. The
  39 * point in time will be deferred to machine creation, so that values
  40 * set in @realize will not be introspectable beforehand. Therefore
  41 * devices must not create children during @realize; they should
  42 * initialize them via object_initialize() in their own
  43 * #TypeInfo.instance_init and forward the realization events
  44 * appropriately.
  45 *
  46 * Any type may override the @realize and/or @unrealize callbacks but needs
  47 * to call the parent type's implementation if keeping their functionality
  48 * is desired. Refer to QOM documentation for further discussion and examples.
  49 *
  50 * .. note::
  51 *   Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
  52 *   derived directly from it need not call their parent's @realize and
  53 *   @unrealize. For other types consult the documentation and
  54 *   implementation of the respective parent types.
  55 *
  56 * Hiding a device
  57 * ---------------
  58 *
  59 * To hide a device, a DeviceListener function hide_device() needs to
  60 * be registered. It can be used to defer adding a device and
  61 * therefore hide it from the guest. The handler registering to this
  62 * DeviceListener can save the QOpts passed to it for re-using it
  63 * later. It must return if it wants the device to be hidden or
  64 * visible. When the handler function decides the device shall be
  65 * visible it will be added with qdev_device_add() and realized as any
  66 * other device. Otherwise qdev_device_add() will return early without
  67 * adding the device. The guest will not see a "hidden" device until
  68 * it was marked visible and qdev_device_add called again.
  69 *
  70 */
  71
  72enum {
  73    DEV_NVECTORS_UNSPECIFIED = -1,
  74};
  75
  76#define TYPE_DEVICE "device"
  77OBJECT_DECLARE_TYPE(DeviceState, DeviceClass, DEVICE)
  78
  79typedef enum DeviceCategory {
  80    DEVICE_CATEGORY_BRIDGE,
  81    DEVICE_CATEGORY_USB,
  82    DEVICE_CATEGORY_STORAGE,
  83    DEVICE_CATEGORY_NETWORK,
  84    DEVICE_CATEGORY_INPUT,
  85    DEVICE_CATEGORY_DISPLAY,
  86    DEVICE_CATEGORY_SOUND,
  87    DEVICE_CATEGORY_MISC,
  88    DEVICE_CATEGORY_CPU,
  89    DEVICE_CATEGORY_WATCHDOG,
  90    DEVICE_CATEGORY_MAX
  91} DeviceCategory;
  92
  93typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
  94typedef void (*DeviceUnrealize)(DeviceState *dev);
  95typedef void (*DeviceReset)(DeviceState *dev);
  96typedef void (*BusRealize)(BusState *bus, Error **errp);
  97typedef void (*BusUnrealize)(BusState *bus);
  98typedef int (*DeviceSyncConfig)(DeviceState *dev, Error **errp);
  99
 100/**
 101 * struct DeviceClass - The base class for all devices.
 102 * @props: Properties accessing state fields.
 103 * @realize: Callback function invoked when the #DeviceState:realized
 104 * property is changed to %true.
 105 * @unrealize: Callback function invoked when the #DeviceState:realized
 106 * property is changed to %false.
 107 * @sync_config: Callback function invoked when QMP command device-sync-config
 108 * is called. Should synchronize device configuration from host to guest part
 109 * and notify the guest about the change.
 110 * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
 111 * as readonly "hotpluggable" property of #DeviceState instance
 112 *
 113 */
 114struct DeviceClass {
 115    /* private: */
 116    ObjectClass parent_class;
 117
 118    /* public: */
 119
 120    /**
 121     * @categories: device categories device belongs to
 122     */
 123    DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
 124    /**
 125     * @fw_name: name used to identify device to firmware interfaces
 126     */
 127    const char *fw_name;
 128    /**
 129     * @desc: human readable description of device
 130     */
 131    const char *desc;
 132
 133    /**
 134     * @props_: properties associated with device, should only be
 135     * assigned by using device_class_set_props(). The underscore
 136     * ensures a compile-time error if someone attempts to assign
 137     * dc->props directly.
 138     */
 139    const Property *props_;
 140
 141    /**
 142     * @props_count_: number of elements in @props_; should only be
 143     * assigned by using device_class_set_props().
 144     */
 145    uint16_t props_count_;
 146
 147    /**
 148     * @user_creatable: Can user instantiate with -device / device_add?
 149     *
 150     * All devices should support instantiation with device_add, and
 151     * this flag should not exist.  But we're not there, yet.  Some
 152     * devices fail to instantiate with cryptic error messages.
 153     * Others instantiate, but don't work.  Exposing users to such
 154     * behavior would be cruel; clearing this flag will protect them.
 155     * It should never be cleared without a comment explaining why it
 156     * is cleared.
 157     *
 158     * TODO remove once we're there
 159     */
 160    bool user_creatable;
 161    bool hotpluggable;
 162
 163    /* callbacks */
 164    /**
 165     * @legacy_reset: deprecated device reset method pointer
 166     *
 167     * Modern code should use the ResettableClass interface to
 168     * implement a multi-phase reset.
 169     *
 170     * TODO: remove once every reset callback is unused
 171     */
 172    DeviceReset legacy_reset;
 173    DeviceRealize realize;
 174    DeviceUnrealize unrealize;
 175    DeviceSyncConfig sync_config;
 176
 177    /**
 178     * @vmsd: device state serialisation description for
 179     * migration/save/restore
 180     */
 181    const VMStateDescription *vmsd;
 182
 183    /**
 184     * @bus_type: bus type
 185     * private: to qdev / bus.
 186     */
 187    const char *bus_type;
 188};
 189
 190typedef struct NamedGPIOList NamedGPIOList;
 191
 192struct NamedGPIOList {
 193    char *name;
 194    qemu_irq *in;
 195    int num_in;
 196    int num_out;
 197    QLIST_ENTRY(NamedGPIOList) node;
 198};
 199
 200typedef struct Clock Clock;
 201typedef struct NamedClockList NamedClockList;
 202
 203struct NamedClockList {
 204    char *name;
 205    Clock *clock;
 206    bool output;
 207    bool alias;
 208    QLIST_ENTRY(NamedClockList) node;
 209};
 210
 211typedef struct {
 212    bool engaged_in_io;
 213} MemReentrancyGuard;
 214
 215
 216typedef QLIST_HEAD(, NamedGPIOList) NamedGPIOListHead;
 217typedef QLIST_HEAD(, NamedClockList) NamedClockListHead;
 218typedef QLIST_HEAD(, BusState) BusStateHead;
 219
 220/**
 221 * struct DeviceState - common device state, accessed with qdev helpers
 222 *
 223 * This structure should not be accessed directly.  We declare it here
 224 * so that it can be embedded in individual device state structures.
 225 */
 226struct DeviceState {
 227    /* private: */
 228    Object parent_obj;
 229    /* public: */
 230
 231    /**
 232     * @id: global device id
 233     */
 234    char *id;
 235    /**
 236     * @canonical_path: canonical path of realized device in the QOM tree
 237     */
 238    char *canonical_path;
 239    /**
 240     * @realized: has device been realized?
 241     */
 242    bool realized;
 243    /**
 244     * @pending_deleted_event: track pending deletion events during unplug
 245     */
 246    bool pending_deleted_event;
 247    /**
 248     * @pending_deleted_expires_ms: optional timeout for deletion events
 249     */
 250    int64_t pending_deleted_expires_ms;
 251    /**
 252     * @hotplugged: was device added after PHASE_MACHINE_READY?
 253     */
 254    int hotplugged;
 255    /**
 256     * @allow_unplug_during_migration: can device be unplugged during migration
 257     */
 258    bool allow_unplug_during_migration;
 259    /**
 260     * @parent_bus: bus this device belongs to
 261     */
 262    BusState *parent_bus;
 263    /**
 264     * @gpios: QLIST of named GPIOs the device provides.
 265     */
 266    NamedGPIOListHead gpios;
 267    /**
 268     * @clocks: QLIST of named clocks the device provides.
 269     */
 270    NamedClockListHead clocks;
 271    /**
 272     * @child_bus: QLIST of child buses
 273     */
 274    BusStateHead child_bus;
 275    /**
 276     * @num_child_bus: number of @child_bus entries
 277     */
 278    int num_child_bus;
 279    /**
 280     * @instance_id_alias: device alias for handling legacy migration setups
 281     */
 282    int instance_id_alias;
 283    /**
 284     * @alias_required_for_version: indicates @instance_id_alias is
 285     * needed for migration
 286     */
 287    int alias_required_for_version;
 288    /**
 289     * @reset: ResettableState for the device; handled by Resettable interface.
 290     */
 291    ResettableState reset;
 292    /**
 293     * @unplug_blockers: list of reasons to block unplugging of device
 294     */
 295    GSList *unplug_blockers;
 296    /**
 297     * @mem_reentrancy_guard: Is the device currently in mmio/pio/dma?
 298     *
 299     * Used to prevent re-entrancy confusing things.
 300     */
 301    MemReentrancyGuard mem_reentrancy_guard;
 302};
 303
 304typedef struct DeviceListener DeviceListener;
 305struct DeviceListener {
 306    void (*realize)(DeviceListener *listener, DeviceState *dev);
 307    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
 308    /*
 309     * This callback is called upon init of the DeviceState and
 310     * informs qdev if a device should be visible or hidden.  We can
 311     * hide a failover device depending for example on the device
 312     * opts.
 313     *
 314     * On errors, it returns false and errp is set. Device creation
 315     * should fail in this case.
 316     */
 317    bool (*hide_device)(DeviceListener *listener, const QDict *device_opts,
 318                        bool from_json, Error **errp);
 319    QTAILQ_ENTRY(DeviceListener) link;
 320};
 321
 322#define TYPE_BUS "bus"
 323DECLARE_OBJ_CHECKERS(BusState, BusClass,
 324                     BUS, TYPE_BUS)
 325
 326struct BusClass {
 327    ObjectClass parent_class;
 328
 329    /* FIXME first arg should be BusState */
 330    void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
 331    char *(*get_dev_path)(DeviceState *dev);
 332
 333    /*
 334     * This callback is used to create Open Firmware device path in accordance
 335     * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
 336     * bindings can be found at http://playground.sun.com/1275/bindings/.
 337     */
 338    char *(*get_fw_dev_path)(DeviceState *dev);
 339
 340    /*
 341     * Return whether the device can be added to @bus,
 342     * based on the address that was set (via device properties)
 343     * before realize.  If not, on return @errp contains the
 344     * human-readable error message.
 345     */
 346    bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
 347
 348    BusRealize realize;
 349    BusUnrealize unrealize;
 350
 351    /* maximum devices allowed on the bus, 0: no limit. */
 352    int max_dev;
 353    /* number of automatically allocated bus ids (e.g. ide.0) */
 354    int automatic_ids;
 355};
 356
 357typedef struct BusChild {
 358    struct rcu_head rcu;
 359    DeviceState *child;
 360    int index;
 361    QTAILQ_ENTRY(BusChild) sibling;
 362} BusChild;
 363
 364#define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
 365
 366typedef QTAILQ_HEAD(, BusChild) BusChildHead;
 367typedef QLIST_ENTRY(BusState) BusStateEntry;
 368
 369/**
 370 * struct BusState:
 371 * @obj: parent object
 372 * @parent: parent Device
 373 * @name: name of bus
 374 * @hotplug_handler: link to a hotplug handler associated with bus.
 375 * @max_index: max number of child buses
 376 * @realized: is the bus itself realized?
 377 * @full: is the bus full?
 378 * @num_children: current number of child buses
 379 */
 380struct BusState {
 381    /* private: */
 382    Object obj;
 383    /* public: */
 384    DeviceState *parent;
 385    char *name;
 386    HotplugHandler *hotplug_handler;
 387    int max_index;
 388    bool realized;
 389    bool full;
 390    int num_children;
 391
 392    /**
 393     * @children: an RCU protected QTAILQ, thus readers must use RCU
 394     * to access it, and writers must hold the big qemu lock
 395     */
 396    BusChildHead children;
 397    /**
 398     * @sibling: next bus
 399     */
 400    BusStateEntry sibling;
 401    /**
 402     * @reset: ResettableState for the bus; handled by Resettable interface.
 403     */
 404    ResettableState reset;
 405};
 406
 407/**
 408 * typedef GlobalProperty - a global property type
 409 *
 410 * @used: Set to true if property was used when initializing a device.
 411 * @optional: If set to true, GlobalProperty will be skipped without errors
 412 *            if the property doesn't exist.
 413 *
 414 * An error is fatal for non-hotplugged devices, when the global is applied.
 415 */
 416typedef struct GlobalProperty {
 417    const char *driver;
 418    const char *property;
 419    const char *value;
 420    bool used;
 421    bool optional;
 422} GlobalProperty;
 423
 424static inline void
 425compat_props_add(GPtrArray *arr,
 426                 GlobalProperty props[], size_t nelem)
 427{
 428    int i;
 429    for (i = 0; i < nelem; i++) {
 430        g_ptr_array_add(arr, (void *)&props[i]);
 431    }
 432}
 433
 434/*** Board API.  This should go away once we have a machine config file.  ***/
 435
 436/**
 437 * qdev_new: Create a device on the heap
 438 * @name: device type to create (we assert() that this type exists)
 439 *
 440 * This only allocates the memory and initializes the device state
 441 * structure, ready for the caller to set properties if they wish.
 442 * The device still needs to be realized.
 443 *
 444 * Return: a derived DeviceState object with a reference count of 1.
 445 */
 446DeviceState *qdev_new(const char *name);
 447
 448/**
 449 * qdev_try_new: Try to create a device on the heap
 450 * @name: device type to create
 451 *
 452 * This is like qdev_new(), except it returns %NULL when type @name
 453 * does not exist, rather than asserting.
 454 *
 455 * Return: a derived DeviceState object with a reference count of 1 or
 456 * NULL if type @name does not exist.
 457 */
 458DeviceState *qdev_try_new(const char *name);
 459
 460/**
 461 * qdev_is_realized() - check if device is realized
 462 * @dev: The device to check.
 463 *
 464 * Context: May be called outside big qemu lock.
 465 * Return: true if the device has been fully constructed, false otherwise.
 466 */
 467static inline bool qdev_is_realized(DeviceState *dev)
 468{
 469    return qatomic_load_acquire(&dev->realized);
 470}
 471
 472/**
 473 * qdev_realize: Realize @dev.
 474 * @dev: device to realize
 475 * @bus: bus to plug it into (may be NULL)
 476 * @errp: pointer to error object
 477 *
 478 * "Realize" the device, i.e. perform the second phase of device
 479 * initialization.
 480 * @dev must not be plugged into a bus already.
 481 * If @bus, plug @dev into @bus.  This takes a reference to @dev.
 482 * If @dev has no QOM parent, make one up, taking another reference.
 483 *
 484 * If you created @dev using qdev_new(), you probably want to use
 485 * qdev_realize_and_unref() instead.
 486 *
 487 * Return: true on success, else false setting @errp with error
 488 */
 489bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
 490
 491/**
 492 * qdev_realize_and_unref: Realize @dev and drop a reference
 493 * @dev: device to realize
 494 * @bus: bus to plug it into (may be NULL)
 495 * @errp: pointer to error object
 496 *
 497 * Realize @dev and drop a reference.
 498 * This is like qdev_realize(), except the caller must hold a
 499 * (private) reference, which is dropped on return regardless of
 500 * success or failure.  Intended use::
 501 *
 502 *     dev = qdev_new();
 503 *     [...]
 504 *     qdev_realize_and_unref(dev, bus, errp);
 505 *
 506 * Now @dev can go away without further ado.
 507 *
 508 * If you are embedding the device into some other QOM device and
 509 * initialized it via some variant on object_initialize_child() then
 510 * do not use this function, because that family of functions arrange
 511 * for the only reference to the child device to be held by the parent
 512 * via the child<> property, and so the reference-count-drop done here
 513 * would be incorrect. For that use case you want qdev_realize().
 514 *
 515 * Return: true on success, else false setting @errp with error
 516 */
 517bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
 518
 519/**
 520 * qdev_unrealize: Unrealize a device
 521 * @dev: device to unrealize
 522 *
 523 * This function will "unrealize" a device, which is the first phase
 524 * of correctly destroying a device that has been realized. It will:
 525 *
 526 *  - unrealize any child buses by calling qbus_unrealize()
 527 *    (this will recursively unrealize any devices on those buses)
 528 *  - call the unrealize method of @dev
 529 *
 530 * The device can then be freed by causing its reference count to go
 531 * to zero.
 532 *
 533 * Warning: most devices in QEMU do not expect to be unrealized.  Only
 534 * devices which are hot-unpluggable should be unrealized (as part of
 535 * the unplugging process); all other devices are expected to last for
 536 * the life of the simulation and should not be unrealized and freed.
 537 */
 538void qdev_unrealize(DeviceState *dev);
 539void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
 540                                 int required_for_version);
 541HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
 542HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
 543bool qdev_hotplug_allowed(DeviceState *dev, BusState *bus, Error **errp);
 544bool qdev_hotunplug_allowed(DeviceState *dev, Error **errp);
 545
 546/**
 547 * qdev_get_hotplug_handler() - Get handler responsible for device wiring
 548 * @dev: the device we want the HOTPLUG_HANDLER for.
 549 *
 550 * Note: in case @dev has a parent bus, it will be returned as handler unless
 551 * machine handler overrides it.
 552 *
 553 * Return: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
 554 * or NULL if there aren't any.
 555 */
 556HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 557void qdev_unplug(DeviceState *dev, Error **errp);
 558int qdev_sync_config(DeviceState *dev, Error **errp);
 559void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
 560                                  DeviceState *dev, Error **errp);
 561void qdev_machine_creation_done(void);
 562bool qdev_machine_modified(void);
 563
 564/**
 565 * qdev_add_unplug_blocker: Add an unplug blocker to a device
 566 *
 567 * @dev: Device to be blocked from unplug
 568 * @reason: Reason for blocking
 569 */
 570void qdev_add_unplug_blocker(DeviceState *dev, Error *reason);
 571
 572/**
 573 * qdev_del_unplug_blocker: Remove an unplug blocker from a device
 574 *
 575 * @dev: Device to be unblocked
 576 * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
 577 *          Used as a handle to lookup the blocker for deletion.
 578 */
 579void qdev_del_unplug_blocker(DeviceState *dev, Error *reason);
 580
 581/**
 582 * qdev_unplug_blocked: Confirm if a device is blocked from unplug
 583 *
 584 * @dev: Device to be tested
 585 * @errp: The reasons why the device is blocked, if any
 586 *
 587 * Returns: true (also setting @errp) if device is blocked from unplug,
 588 * false otherwise
 589 */
 590bool qdev_unplug_blocked(DeviceState *dev, Error **errp);
 591
 592/**
 593 * typedef GpioPolarity - Polarity of a GPIO line
 594 *
 595 * GPIO lines use either positive (active-high) logic,
 596 * or negative (active-low) logic.
 597 *
 598 * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
 599 * active when the voltage on the pin is high (relative to ground);
 600 * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
 601 * is active when the voltage on the pin is low (or grounded).
 602 */
 603typedef enum {
 604    GPIO_POLARITY_ACTIVE_LOW,
 605    GPIO_POLARITY_ACTIVE_HIGH
 606} GpioPolarity;
 607
 608/**
 609 * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
 610 * @dev: Device whose GPIO we want
 611 * @n: Number of the anonymous GPIO line (which must be in range)
 612 *
 613 * Returns the qemu_irq corresponding to an anonymous input GPIO line
 614 * (which the device has set up with qdev_init_gpio_in()). The index
 615 * @n of the GPIO line must be valid (i.e. be at least 0 and less than
 616 * the total number of anonymous input GPIOs the device has); this
 617 * function will assert() if passed an invalid index.
 618 *
 619 * This function is intended to be used by board code or SoC "container"
 620 * device models to wire up the GPIO lines; usually the return value
 621 * will be passed to qdev_connect_gpio_out() or a similar function to
 622 * connect another device's output GPIO line to this input.
 623 *
 624 * For named input GPIO lines, use qdev_get_gpio_in_named().
 625 *
 626 * Return: qemu_irq corresponding to anonymous input GPIO line
 627 */
 628qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
 629
 630/**
 631 * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
 632 * @dev: Device whose GPIO we want
 633 * @name: Name of the input GPIO array
 634 * @n: Number of the GPIO line in that array (which must be in range)
 635 *
 636 * Returns the qemu_irq corresponding to a single input GPIO line
 637 * in a named array of input GPIO lines on a device (which the device
 638 * has set up with qdev_init_gpio_in_named()).
 639 * The @name string must correspond to an input GPIO array which exists on
 640 * the device, and the index @n of the GPIO line must be valid (i.e.
 641 * be at least 0 and less than the total number of input GPIOs in that
 642 * array); this function will assert() if passed an invalid name or index.
 643 *
 644 * For anonymous input GPIO lines, use qdev_get_gpio_in().
 645 *
 646 * Return: qemu_irq corresponding to named input GPIO line
 647 */
 648qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
 649
 650/**
 651 * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
 652 * @dev: Device whose GPIO to connect
 653 * @n: Number of the anonymous output GPIO line (which must be in range)
 654 * @pin: qemu_irq to connect the output line to
 655 *
 656 * This function connects an anonymous output GPIO line on a device
 657 * up to an arbitrary qemu_irq, so that when the device asserts that
 658 * output GPIO line, the qemu_irq's callback is invoked.
 659 * The index @n of the GPIO line must be valid (i.e. be at least 0 and
 660 * less than the total number of anonymous output GPIOs the device has
 661 * created with qdev_init_gpio_out()); otherwise this function will assert().
 662 *
 663 * Outbound GPIO lines can be connected to any qemu_irq, but the common
 664 * case is connecting them to another device's inbound GPIO line, using
 665 * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
 666 *
 667 * It is not valid to try to connect one outbound GPIO to multiple
 668 * qemu_irqs at once, or to connect multiple outbound GPIOs to the
 669 * same qemu_irq. (Warning: there is no assertion or other guard to
 670 * catch this error: the model will just not do the right thing.)
 671 * Instead, for fan-out you can use the TYPE_SPLIT_IRQ device: connect
 672 * a device's outbound GPIO to the splitter's input, and connect each
 673 * of the splitter's outputs to a different device.  For fan-in you
 674 * can use the TYPE_OR_IRQ device, which is a model of a logical OR
 675 * gate with multiple inputs and one output.
 676 *
 677 * For named output GPIO lines, use qdev_connect_gpio_out_named().
 678 */
 679void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
 680
 681/**
 682 * qdev_connect_gpio_out_named: Connect one of a device's named output
 683 *                              GPIO lines
 684 * @dev: Device whose GPIO to connect
 685 * @name: Name of the output GPIO array
 686 * @n: Number of the output GPIO line within that array (which must be in range)
 687 * @input_pin: qemu_irq to connect the output line to
 688 *
 689 * This function connects a single GPIO output in a named array of output
 690 * GPIO lines on a device up to an arbitrary qemu_irq, so that when the
 691 * device asserts that output GPIO line, the qemu_irq's callback is invoked.
 692 * The @name string must correspond to an output GPIO array which exists on
 693 * the device, and the index @n of the GPIO line must be valid (i.e.
 694 * be at least 0 and less than the total number of output GPIOs in that
 695 * array); this function will assert() if passed an invalid name or index.
 696 *
 697 * Outbound GPIO lines can be connected to any qemu_irq, but the common
 698 * case is connecting them to another device's inbound GPIO line, using
 699 * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
 700 *
 701 * It is not valid to try to connect one outbound GPIO to multiple
 702 * qemu_irqs at once, or to connect multiple outbound GPIOs to the
 703 * same qemu_irq; see qdev_connect_gpio_out() for details.
 704 *
 705 * For anonymous output GPIO lines, use qdev_connect_gpio_out().
 706 */
 707void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
 708                                 qemu_irq input_pin);
 709
 710/**
 711 * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
 712 * @dev: Device whose output GPIO we are interested in
 713 * @name: Name of the output GPIO array
 714 * @n: Number of the output GPIO line within that array
 715 *
 716 * Returns whatever qemu_irq is currently connected to the specified
 717 * output GPIO line of @dev. This will be NULL if the output GPIO line
 718 * has never been wired up to the anything.  Note that the qemu_irq
 719 * returned does not belong to @dev -- it will be the input GPIO or
 720 * IRQ of whichever device the board code has connected up to @dev's
 721 * output GPIO.
 722 *
 723 * You probably don't need to use this function -- it is used only
 724 * by the platform-bus subsystem.
 725 *
 726 * Return: qemu_irq associated with GPIO or NULL if un-wired.
 727 */
 728qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
 729
 730/**
 731 * qdev_intercept_gpio_out: Intercept an existing GPIO connection
 732 * @dev: Device to intercept the outbound GPIO line from
 733 * @icpt: New qemu_irq to connect instead
 734 * @name: Name of the output GPIO array
 735 * @n: Number of the GPIO line in the array
 736 *
 737 * .. note::
 738 *   This function is provided only for use by the qtest testing framework
 739 *   and is not suitable for use in non-testing parts of QEMU.
 740 *
 741 * This function breaks an existing connection of an outbound GPIO
 742 * line from @dev, and replaces it with the new qemu_irq @icpt, as if
 743 * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
 744 * The previously connected qemu_irq is returned, so it can be restored
 745 * by a second call to qdev_intercept_gpio_out() if desired.
 746 *
 747 * Return: old disconnected qemu_irq if one existed
 748 */
 749qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
 750                                 const char *name, int n);
 751
 752BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
 753
 754/*** Device API.  ***/
 755
 756/**
 757 * qdev_init_gpio_in: create an array of anonymous input GPIO lines
 758 * @dev: Device to create input GPIOs for
 759 * @handler: Function to call when GPIO line value is set
 760 * @n: Number of GPIO lines to create
 761 *
 762 * Devices should use functions in the qdev_init_gpio_in* family in
 763 * their instance_init or realize methods to create any input GPIO
 764 * lines they need. There is no functional difference between
 765 * anonymous and named GPIO lines. Stylistically, named GPIOs are
 766 * preferable (easier to understand at callsites) unless a device
 767 * has exactly one uniform kind of GPIO input whose purpose is obvious.
 768 * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
 769 *
 770 * See qdev_get_gpio_in() for how code that uses such a device can get
 771 * hold of an input GPIO line to manipulate it.
 772 */
 773void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
 774
 775/**
 776 * qdev_init_gpio_out: create an array of anonymous output GPIO lines
 777 * @dev: Device to create output GPIOs for
 778 * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
 779 * @n: Number of GPIO lines to create
 780 *
 781 * Devices should use functions in the qdev_init_gpio_out* family
 782 * in their instance_init or realize methods to create any output
 783 * GPIO lines they need. There is no functional difference between
 784 * anonymous and named GPIO lines. Stylistically, named GPIOs are
 785 * preferable (easier to understand at callsites) unless a device
 786 * has exactly one uniform kind of GPIO output whose purpose is obvious.
 787 *
 788 * The @pins argument should be a pointer to either a "qemu_irq"
 789 * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
 790 * state structure. The device implementation can then raise and
 791 * lower the GPIO line by calling qemu_set_irq(). (If anything is
 792 * connected to the other end of the GPIO this will cause the handler
 793 * function for that input GPIO to be called.)
 794 *
 795 * See qdev_connect_gpio_out() for how code that uses such a device
 796 * can connect to one of its output GPIO lines.
 797 *
 798 * There is no need to release the @pins allocated array because it
 799 * will be automatically released when @dev calls its instance_finalize()
 800 * handler.
 801 */
 802void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
 803
 804/**
 805 * qdev_init_gpio_out_named: create an array of named output GPIO lines
 806 * @dev: Device to create output GPIOs for
 807 * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
 808 * @name: Name to give this array of GPIO lines
 809 * @n: Number of GPIO lines to create in this array
 810 *
 811 * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
 812 * with a name. Code using the device can then connect these GPIO lines
 813 * using qdev_connect_gpio_out_named().
 814 */
 815void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
 816                              const char *name, int n);
 817
 818/**
 819 * qdev_init_gpio_in_named_with_opaque() - create an array of input GPIO lines
 820 * @dev: Device to create input GPIOs for
 821 * @handler: Function to call when GPIO line value is set
 822 * @opaque: Opaque data pointer to pass to @handler
 823 * @name: Name of the GPIO input (must be unique for this device)
 824 * @n: Number of GPIO lines in this input set
 825 */
 826void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
 827                                         qemu_irq_handler handler,
 828                                         void *opaque,
 829                                         const char *name, int n);
 830
 831/**
 832 * qdev_init_gpio_in_named() - create an array of input GPIO lines
 833 * @dev: device to add array to
 834 * @handler: a &typedef qemu_irq_handler function to call when GPIO is set
 835 * @name: Name of the GPIO input (must be unique for this device)
 836 * @n: Number of GPIO lines in this input set
 837 *
 838 * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
 839 * passed to the handler is @dev (which is the most commonly desired behaviour).
 840 */
 841static inline void qdev_init_gpio_in_named(DeviceState *dev,
 842                                           qemu_irq_handler handler,
 843                                           const char *name, int n)
 844{
 845    qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
 846}
 847
 848/**
 849 * qdev_pass_gpios: create GPIO lines on container which pass through to device
 850 * @dev: Device which has GPIO lines
 851 * @container: Container device which needs to expose them
 852 * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
 853 *
 854 * In QEMU, complicated devices like SoCs are often modelled with a
 855 * "container" QOM device which itself contains other QOM devices and
 856 * which wires them up appropriately. This function allows the container
 857 * to create GPIO arrays on itself which simply pass through to a GPIO
 858 * array of one of its internal devices.
 859 *
 860 * If @dev has both input and output GPIOs named @name then both will
 861 * be passed through. It is not possible to pass a subset of the array
 862 * with this function.
 863 *
 864 * To users of the container device, the GPIO array created on @container
 865 * behaves exactly like any other.
 866 */
 867void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
 868                     const char *name);
 869
 870BusState *qdev_get_parent_bus(const DeviceState *dev);
 871
 872/*** BUS API. ***/
 873
 874DeviceState *qdev_find_recursive(BusState *bus, const char *id);
 875
 876/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
 877typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
 878typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
 879
 880void qbus_init(void *bus, size_t size, const char *typename,
 881               DeviceState *parent, const char *name);
 882BusState *qbus_new(const char *typename, DeviceState *parent, const char *name);
 883bool qbus_realize(BusState *bus, Error **errp);
 884void qbus_unrealize(BusState *bus);
 885
 886/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
 887 *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
 888 *           0 otherwise. */
 889int qbus_walk_children(BusState *bus,
 890                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
 891                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
 892                       void *opaque);
 893int qdev_walk_children(DeviceState *dev,
 894                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
 895                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
 896                       void *opaque);
 897
 898/**
 899 * device_cold_reset() - perform a recursive cold reset on a device
 900 * @dev: device to reset.
 901 *
 902 * Reset device @dev and perform a recursive processing using the resettable
 903 * interface. It triggers a RESET_TYPE_COLD.
 904 */
 905void device_cold_reset(DeviceState *dev);
 906
 907/**
 908 * bus_cold_reset() - perform a recursive cold reset on a bus
 909 * @bus: bus to reset
 910 *
 911 * Reset bus @bus and perform a recursive processing using the resettable
 912 * interface. It triggers a RESET_TYPE_COLD.
 913 */
 914void bus_cold_reset(BusState *bus);
 915
 916/**
 917 * device_is_in_reset() - check device reset state
 918 * @dev: device to check
 919 *
 920 * Return: true if the device @dev is currently being reset.
 921 */
 922bool device_is_in_reset(DeviceState *dev);
 923
 924/**
 925 * bus_is_in_reset() - check bus reset state
 926 * @bus: bus to check
 927 *
 928 * Return: true if the bus @bus is currently being reset.
 929 */
 930bool bus_is_in_reset(BusState *bus);
 931
 932/* This should go away once we get rid of the NULL bus hack */
 933BusState *sysbus_get_default(void);
 934
 935char *qdev_get_fw_dev_path(DeviceState *dev);
 936char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
 937
 938/**
 939 * device_class_set_props(): add a set of properties to an device
 940 * @dc: the parent DeviceClass all devices inherit
 941 * @props: an array of properties
 942 *
 943 * This will add a set of properties to the object. It will fault if
 944 * you attempt to add an existing property defined by a parent class.
 945 * To modify an inherited property you need to use????
 946 *
 947 * Validate that @props has at least one Property.
 948 * Validate that @props is an array, not a pointer, via ARRAY_SIZE.
 949 * Validate that the array does not have a legacy terminator at compile-time;
 950 * requires -O2 and the array to be const.
 951 */
 952#define device_class_set_props(dc, props) \
 953    do {                                                                \
 954        QEMU_BUILD_BUG_ON(sizeof(props) == 0);                          \
 955        size_t props_count_ = ARRAY_SIZE(props);                        \
 956        if ((props)[props_count_ - 1].name == NULL) {                   \
 957            qemu_build_not_reached();                                   \
 958        }                                                               \
 959        device_class_set_props_n((dc), (props), props_count_);          \
 960    } while (0)
 961
 962/**
 963 * device_class_set_props_n(): add a set of properties to an device
 964 * @dc: the parent DeviceClass all devices inherit
 965 * @props: an array of properties
 966 * @n: ARRAY_SIZE(@props)
 967 *
 968 * This will add a set of properties to the object. It will fault if
 969 * you attempt to add an existing property defined by a parent class.
 970 * To modify an inherited property you need to use????
 971 */
 972void device_class_set_props_n(DeviceClass *dc, const Property *props, size_t n);
 973
 974/**
 975 * device_class_set_parent_realize() - set up for chaining realize fns
 976 * @dc: The device class
 977 * @dev_realize: the device realize function
 978 * @parent_realize: somewhere to save the parents realize function
 979 *
 980 * This is intended to be used when the new realize function will
 981 * eventually call its parent realization function during creation.
 982 * This requires storing the function call somewhere (usually in the
 983 * instance structure) so you can eventually call
 984 * dc->parent_realize(dev, errp)
 985 */
 986void device_class_set_parent_realize(DeviceClass *dc,
 987                                     DeviceRealize dev_realize,
 988                                     DeviceRealize *parent_realize);
 989
 990/**
 991 * device_class_set_legacy_reset(): set the DeviceClass::reset method
 992 * @dc: The device class
 993 * @dev_reset: the reset function
 994 *
 995 * This function sets the DeviceClass::reset method. This is widely
 996 * used in existing code, but new code should prefer to use the
 997 * Resettable API as documented in docs/devel/reset.rst.
 998 * In addition, devices which need to chain to their parent class's
 999 * reset methods or which need to be subclassed must use Resettable.
1000 */
1001void device_class_set_legacy_reset(DeviceClass *dc,
1002                                   DeviceReset dev_reset);
1003
1004/**
1005 * device_class_set_parent_unrealize() - set up for chaining unrealize fns
1006 * @dc: The device class
1007 * @dev_unrealize: the device realize function
1008 * @parent_unrealize: somewhere to save the parents unrealize function
1009 *
1010 * This is intended to be used when the new unrealize function will
1011 * eventually call its parent unrealization function during the
1012 * unrealize phase. This requires storing the function call somewhere
1013 * (usually in the instance structure) so you can eventually call
1014 * dc->parent_unrealize(dev);
1015 */
1016void device_class_set_parent_unrealize(DeviceClass *dc,
1017                                       DeviceUnrealize dev_unrealize,
1018                                       DeviceUnrealize *parent_unrealize);
1019
1020const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
1021
1022const char *qdev_fw_name(DeviceState *dev);
1023
1024void qdev_assert_realized_properly(void);
1025Object *qdev_get_machine(void);
1026
1027/**
1028 * qdev_create_fake_machine(): Create a fake machine container.
1029 *
1030 * .. note::
1031 *    This function is a kludge for user emulation (USER_ONLY)
1032 *    because when thread (TYPE_CPU) are realized, qdev_realize()
1033 *    access a machine container.
1034 */
1035void qdev_create_fake_machine(void);
1036
1037/**
1038 * machine_get_container:
1039 * @name: The name of container to lookup
1040 *
1041 * Get a container of the machine (QOM path "/machine/NAME").
1042 *
1043 * Returns: the machine container object.
1044 */
1045Object *machine_get_container(const char *name);
1046
1047/**
1048 * qdev_get_human_name() - Return a human-readable name for a device
1049 * @dev: The device. Must be a valid and non-NULL pointer.
1050 *
1051 * .. note::
1052 *    This function is intended for user friendly error messages.
1053 *
1054 * Returns: A newly allocated string containing the device id if not null,
1055 * else the object canonical path.
1056 *
1057 * Use g_free() to free it.
1058 */
1059char *qdev_get_human_name(DeviceState *dev);
1060
1061/* FIXME: make this a link<> */
1062bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
1063
1064extern bool qdev_hot_removed;
1065
1066char *qdev_get_dev_path(DeviceState *dev);
1067
1068void qbus_set_hotplug_handler(BusState *bus, Object *handler);
1069void qbus_set_bus_hotplug_handler(BusState *bus);
1070
1071static inline bool qbus_is_hotpluggable(BusState *bus)
1072{
1073    HotplugHandler *plug_handler = bus->hotplug_handler;
1074    bool ret = !!plug_handler;
1075
1076    if (plug_handler) {
1077        HotplugHandlerClass *hdc;
1078
1079        hdc = HOTPLUG_HANDLER_GET_CLASS(plug_handler);
1080        if (hdc->is_hotpluggable_bus) {
1081            ret = hdc->is_hotpluggable_bus(plug_handler, bus);
1082        }
1083    }
1084    return ret;
1085}
1086
1087/**
1088 * qbus_mark_full: Mark this bus as full, so no more devices can be attached
1089 * @bus: Bus to mark as full
1090 *
1091 * By default, QEMU will allow devices to be plugged into a bus up
1092 * to the bus class's device count limit. Calling this function
1093 * marks a particular bus as full, so that no more devices can be
1094 * plugged into it. In particular this means that the bus will not
1095 * be considered as a candidate for plugging in devices created by
1096 * the user on the commandline or via the monitor.
1097 * If a machine has multiple buses of a given type, such as I2C,
1098 * where some of those buses in the real hardware are used only for
1099 * internal devices and some are exposed via expansion ports, you
1100 * can use this function to mark the internal-only buses as full
1101 * after you have created all their internal devices. Then user
1102 * created devices will appear on the expansion-port bus where
1103 * guest software expects them.
1104 */
1105static inline void qbus_mark_full(BusState *bus)
1106{
1107    bus->full = true;
1108}
1109
1110void device_listener_register(DeviceListener *listener);
1111void device_listener_unregister(DeviceListener *listener);
1112
1113/**
1114 * qdev_should_hide_device() - check if device should be hidden
1115 *
1116 * @opts: options QDict
1117 * @from_json: true if @opts entries are typed, false for all strings
1118 * @errp: pointer to error object
1119 *
1120 * When a device is added via qdev_device_add() this will be called.
1121 *
1122 * Return: if the device should be added now or not.
1123 */
1124bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp);
1125
1126typedef enum MachineInitPhase {
1127    /* current_machine is NULL.  */
1128    PHASE_NO_MACHINE,
1129
1130    /* current_machine is not NULL, but current_machine->accel is NULL.  */
1131    PHASE_MACHINE_CREATED,
1132
1133    /*
1134     * current_machine->accel is not NULL, but the machine properties have
1135     * not been validated and machine_class->init has not yet been called.
1136     */
1137    PHASE_ACCEL_CREATED,
1138
1139    /*
1140     * Late backend objects have been created and initialized.
1141     */
1142    PHASE_LATE_BACKENDS_CREATED,
1143
1144    /*
1145     * machine_class->init has been called, thus creating any embedded
1146     * devices and validating machine properties.  Devices created at
1147     * this time are considered to be cold-plugged.
1148     */
1149    PHASE_MACHINE_INITIALIZED,
1150
1151    /*
1152     * QEMU is ready to start CPUs and devices created at this time
1153     * are considered to be hot-plugged.  The monitor is not restricted
1154     * to "preconfig" commands.
1155     */
1156    PHASE_MACHINE_READY,
1157} MachineInitPhase;
1158
1159bool phase_check(MachineInitPhase phase);
1160void phase_advance(MachineInitPhase phase);
1161
1162#endif
1163