qemu/include/hw/qdev-core.h
<<
>>
Prefs
   1#ifndef QDEV_CORE_H
   2#define QDEV_CORE_H
   3
   4#include "qemu/queue.h"
   5#include "qemu/option.h"
   6#include "qemu/bitmap.h"
   7#include "qom/object.h"
   8#include "hw/irq.h"
   9#include "hw/hotplug.h"
  10
  11enum {
  12    DEV_NVECTORS_UNSPECIFIED = -1,
  13};
  14
  15#define TYPE_DEVICE "device"
  16#define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
  17#define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
  18#define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
  19
  20typedef enum DeviceCategory {
  21    DEVICE_CATEGORY_BRIDGE,
  22    DEVICE_CATEGORY_USB,
  23    DEVICE_CATEGORY_STORAGE,
  24    DEVICE_CATEGORY_NETWORK,
  25    DEVICE_CATEGORY_INPUT,
  26    DEVICE_CATEGORY_DISPLAY,
  27    DEVICE_CATEGORY_SOUND,
  28    DEVICE_CATEGORY_MISC,
  29    DEVICE_CATEGORY_MAX
  30} DeviceCategory;
  31
  32typedef int (*qdev_initfn)(DeviceState *dev);
  33typedef int (*qdev_event)(DeviceState *dev);
  34typedef void (*qdev_resetfn)(DeviceState *dev);
  35typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
  36typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
  37typedef void (*BusRealize)(BusState *bus, Error **errp);
  38typedef void (*BusUnrealize)(BusState *bus, Error **errp);
  39
  40struct VMStateDescription;
  41
  42/**
  43 * DeviceClass:
  44 * @props: Properties accessing state fields.
  45 * @realize: Callback function invoked when the #DeviceState:realized
  46 * property is changed to %true. The default invokes @init if not %NULL.
  47 * @unrealize: Callback function invoked when the #DeviceState:realized
  48 * property is changed to %false.
  49 * @init: Callback function invoked when the #DeviceState::realized property
  50 * is changed to %true. Deprecated, new types inheriting directly from
  51 * TYPE_DEVICE should use @realize instead, new leaf types should consult
  52 * their respective parent type.
  53 * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
  54 * as readonly "hotpluggable" property of #DeviceState instance
  55 *
  56 * # Realization #
  57 * Devices are constructed in two stages,
  58 * 1) object instantiation via object_initialize() and
  59 * 2) device realization via #DeviceState:realized property.
  60 * The former may not fail (it might assert or exit), the latter may return
  61 * error information to the caller and must be re-entrant.
  62 * Trivial field initializations should go into #TypeInfo.instance_init.
  63 * Operations depending on @props static properties should go into @realize.
  64 * After successful realization, setting static properties will fail.
  65 *
  66 * As an interim step, the #DeviceState:realized property can also be
  67 * set with qdev_init_nofail().
  68 * In the future, devices will propagate this state change to their children
  69 * and along busses they expose.
  70 * The point in time will be deferred to machine creation, so that values
  71 * set in @realize will not be introspectable beforehand. Therefore devices
  72 * must not create children during @realize; they should initialize them via
  73 * object_initialize() in their own #TypeInfo.instance_init and forward the
  74 * realization events appropriately.
  75 *
  76 * The @init callback is considered private to a particular bus implementation
  77 * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an
  78 * "init" callback on their parent class instead.
  79 *
  80 * Any type may override the @realize and/or @unrealize callbacks but needs
  81 * to call the parent type's implementation if keeping their functionality
  82 * is desired. Refer to QOM documentation for further discussion and examples.
  83 *
  84 * <note>
  85 *   <para>
  86 * If a type derived directly from TYPE_DEVICE implements @realize, it does
  87 * not need to implement @init and therefore does not need to store and call
  88 * #DeviceClass' default @realize callback.
  89 * For other types consult the documentation and implementation of the
  90 * respective parent types.
  91 *   </para>
  92 * </note>
  93 */
  94typedef struct DeviceClass {
  95    /*< private >*/
  96    ObjectClass parent_class;
  97    /*< public >*/
  98
  99    DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
 100    const char *fw_name;
 101    const char *desc;
 102    Property *props;
 103
 104    /*
 105     * Shall we hide this device model from -device / device_add?
 106     * All devices should support instantiation with device_add, and
 107     * this flag should not exist.  But we're not there, yet.  Some
 108     * devices fail to instantiate with cryptic error messages.
 109     * Others instantiate, but don't work.  Exposing users to such
 110     * behavior would be cruel; this flag serves to protect them.  It
 111     * should never be set without a comment explaining why it is set.
 112     * TODO remove once we're there
 113     */
 114    bool cannot_instantiate_with_device_add_yet;
 115    /*
 116     * Does this device model survive object_unref(object_new(TNAME))?
 117     * All device models should, and this flag shouldn't exist.  Some
 118     * devices crash in object_new(), some crash or hang in
 119     * object_unref().  Makes introspecting properties with
 120     * qmp_device_list_properties() dangerous.  Bad, because it's used
 121     * by -device FOO,help.  This flag serves to protect that code.
 122     * It should never be set without a comment explaining why it is
 123     * set.
 124     * TODO remove once we're there
 125     */
 126    bool cannot_destroy_with_object_finalize_yet;
 127
 128    bool hotpluggable;
 129
 130    /* callbacks */
 131    void (*reset)(DeviceState *dev);
 132    void (*halt)(DeviceState *dev);
 133    void (*unhalt)(DeviceState *dev);
 134
 135    /* callbacks for setting of power state */
 136    void (*pwr_cntrl)(void *opaque, int n, int level);
 137    void (*hlt_cntrl)(void *opaque, int n, int level);
 138    /* reset control */
 139    void (*rst_cntrl)(void *opaque, int n, int level);
 140
 141    DeviceRealize realize;
 142    DeviceUnrealize unrealize;
 143
 144    /* device state */
 145    const struct VMStateDescription *vmsd;
 146
 147    /* Private to qdev / bus.  */
 148    qdev_initfn init; /* TODO remove, once users are converted to realize */
 149    qdev_event exit; /* TODO remove, once users are converted to unrealize */
 150    const char *bus_type;
 151} DeviceClass;
 152
 153typedef struct NamedGPIOList NamedGPIOList;
 154
 155struct NamedGPIOList {
 156    char *name;
 157    qemu_irq *in;
 158    int num_in;
 159    int num_out;
 160    QLIST_ENTRY(NamedGPIOList) node;
 161};
 162
 163typedef struct PowerState {
 164    bool power;
 165    bool halt;
 166    bool active;
 167} PowerState;
 168
 169/**
 170 * DeviceState:
 171 * @realized: Indicates whether the device has been fully constructed.
 172 *
 173 * This structure should not be accessed directly.  We declare it here
 174 * so that it can be embedded in individual device state structures.
 175 */
 176struct DeviceState {
 177    /*< private >*/
 178    Object parent_obj;
 179    /*< public >*/
 180
 181    char *id;
 182    bool realized;
 183    bool pending_deleted_event;
 184    QemuOpts *opts;
 185    int hotplugged;
 186    BusState *parent_bus;
 187    QLIST_HEAD(, NamedGPIOList) gpios;
 188    QLIST_HEAD(, BusState) child_bus;
 189    int num_child_bus;
 190    int instance_id_alias;
 191    int alias_required_for_version;
 192    PowerState ps;
 193};
 194
 195struct DeviceListener {
 196    void (*realize)(DeviceListener *listener, DeviceState *dev);
 197    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
 198    QTAILQ_ENTRY(DeviceListener) link;
 199};
 200
 201#define TYPE_BUS "bus"
 202#define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
 203#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
 204#define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
 205
 206struct BusClass {
 207    ObjectClass parent_class;
 208
 209    /* FIXME first arg should be BusState */
 210    void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
 211    char *(*get_dev_path)(DeviceState *dev);
 212    /*
 213     * This callback is used to create Open Firmware device path in accordance
 214     * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
 215     * bindings can be found at http://playground.sun.com/1275/bindings/.
 216     */
 217    char *(*get_fw_dev_path)(DeviceState *dev);
 218    void (*reset)(BusState *bus);
 219    BusRealize realize;
 220    BusUnrealize unrealize;
 221
 222    /* maximum devices allowed on the bus, 0: no limit. */
 223    int max_dev;
 224    /* number of automatically allocated bus ids (e.g. ide.0) */
 225    int automatic_ids;
 226};
 227
 228typedef struct BusChild {
 229    DeviceState *child;
 230    int index;
 231    QTAILQ_ENTRY(BusChild) sibling;
 232} BusChild;
 233
 234#define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
 235
 236/**
 237 * BusState:
 238 * @hotplug_device: link to a hotplug device associated with bus.
 239 */
 240struct BusState {
 241    Object obj;
 242    DeviceState *parent;
 243    const char *name;
 244    HotplugHandler *hotplug_handler;
 245    int max_index;
 246    bool realized;
 247    QTAILQ_HEAD(ChildrenHead, BusChild) children;
 248    QLIST_ENTRY(BusState) sibling;
 249};
 250
 251struct Property {
 252    const char   *name;
 253    PropertyInfo *info;
 254    ptrdiff_t    offset;
 255    uint8_t      bitnr;
 256    QType        qtype;
 257    int64_t      defval;
 258    int          arrayoffset;
 259    PropertyInfo *arrayinfo;
 260    int          arrayfieldsize;
 261};
 262
 263struct PropertyInfo {
 264    const char *name;
 265    const char *description;
 266    const char * const *enum_table;
 267    int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
 268    ObjectPropertyAccessor *get;
 269    ObjectPropertyAccessor *set;
 270    ObjectPropertyRelease *release;
 271};
 272
 273/**
 274 * GlobalProperty:
 275 * @user_provided: Set to true if property comes from user-provided config
 276 * (command-line or config file).
 277 * @used: Set to true if property was used when initializing a device.
 278 */
 279typedef struct GlobalProperty {
 280    const char *driver;
 281    const char *property;
 282    const char *value;
 283    bool user_provided;
 284    bool used;
 285    QTAILQ_ENTRY(GlobalProperty) next;
 286} GlobalProperty;
 287
 288/*** Board API.  This should go away once we have a machine config file.  ***/
 289
 290DeviceState *qdev_create(BusState *bus, const char *name);
 291DeviceState *qdev_try_create(BusState *bus, const char *name);
 292void qdev_init_nofail(DeviceState *dev);
 293void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
 294                                 int required_for_version);
 295HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
 296void qdev_unplug(DeviceState *dev, Error **errp);
 297void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
 298                                  DeviceState *dev, Error **errp);
 299void qdev_machine_creation_done(void);
 300bool qdev_machine_modified(void);
 301
 302qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
 303qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
 304qemu_irq qdev_get_gpio_out(DeviceState *dev, int n);
 305qemu_irq qdev_get_gpio_out_named(DeviceState *dev, const char *name, int n);
 306
 307void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
 308void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
 309                                 qemu_irq pin);
 310qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
 311qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
 312                                 const char *name, int n);
 313
 314BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
 315
 316/*** Device API.  ***/
 317
 318/* Register device properties.  */
 319/* GPIO inputs also double as IRQ sinks.  */
 320void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
 321void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
 322void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
 323                             const char *name, int n);
 324void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
 325                              const char *name, int n);
 326
 327void qdev_pass_all_gpios(DeviceState *dev, DeviceState *container);
 328void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
 329                     const char *name);
 330
 331BusState *qdev_get_parent_bus(DeviceState *dev);
 332
 333/*** BUS API. ***/
 334
 335DeviceState *qdev_find_recursive(BusState *bus, const char *id);
 336
 337/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
 338typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
 339typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
 340
 341void qbus_create_inplace(void *bus, size_t size, const char *typename,
 342                         DeviceState *parent, const char *name);
 343BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
 344/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
 345 *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
 346 *           0 otherwise. */
 347int qbus_walk_children(BusState *bus,
 348                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
 349                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
 350                       void *opaque);
 351int qdev_walk_children(DeviceState *dev,
 352                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
 353                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
 354                       void *opaque);
 355
 356void qdev_reset_all(DeviceState *dev);
 357void qdev_reset_all_fn(void *opaque);
 358
 359/**
 360 * @qbus_reset_all:
 361 * @bus: Bus to be reset.
 362 *
 363 * Reset @bus and perform a bus-level ("hard") reset of all devices connected
 364 * to it, including recursive processing of all buses below @bus itself.  A
 365 * hard reset means that qbus_reset_all will reset all state of the device.
 366 * For PCI devices, for example, this will include the base address registers
 367 * or configuration space.
 368 */
 369void qbus_reset_all(BusState *bus);
 370void qbus_reset_all_fn(void *opaque);
 371
 372/* This should go away once we get rid of the NULL bus hack */
 373BusState *sysbus_get_default(void);
 374
 375char *qdev_get_fw_dev_path(DeviceState *dev);
 376char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
 377
 378/**
 379 * @qdev_machine_init
 380 *
 381 * Initialize platform devices before machine init.  This is a hack until full
 382 * support for composition is added.
 383 */
 384void qdev_machine_init(void);
 385
 386/**
 387 * @device_reset
 388 *
 389 * Reset a single device (by calling the reset method).
 390 */
 391void device_reset(DeviceState *dev);
 392
 393/**
 394 * @device_halt
 395 *
 396 * Halt a single device (by calling the halt method).
 397 */
 398void device_halt(DeviceState *dev);
 399
 400/**
 401 * @device_unhalt
 402 *
 403 * Unhalt a single device (by calling the unhalt method).
 404 */
 405void device_unhalt(DeviceState *dev);
 406
 407const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
 408
 409const char *qdev_fw_name(DeviceState *dev);
 410
 411Object *qdev_get_machine(void);
 412
 413/* FIXME: make this a link<> */
 414void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
 415
 416extern int qdev_hotplug;
 417
 418char *qdev_get_dev_path(DeviceState *dev);
 419
 420GSList *qdev_build_hotpluggable_device_list(Object *peripheral);
 421
 422void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler,
 423                              Error **errp);
 424
 425void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp);
 426
 427static inline bool qbus_is_hotpluggable(BusState *bus)
 428{
 429   return bus->hotplug_handler;
 430}
 431
 432void device_listener_register(DeviceListener *listener);
 433void device_listener_unregister(DeviceListener *listener);
 434
 435#endif
 436