qemu/hw/qdev.h
<<
>>
Prefs
   1#ifndef QDEV_H
   2#define QDEV_H
   3
   4#include "hw.h"
   5#include "qemu-queue.h"
   6#include "qemu-char.h"
   7#include "qemu-option.h"
   8#include "qapi/qapi-visit-core.h"
   9#include "qemu/object.h"
  10#include "error.h"
  11
  12typedef struct Property Property;
  13
  14typedef struct PropertyInfo PropertyInfo;
  15
  16typedef struct CompatProperty CompatProperty;
  17
  18typedef struct BusState BusState;
  19
  20typedef struct BusInfo BusInfo;
  21
  22enum DevState {
  23    DEV_STATE_CREATED = 1,
  24    DEV_STATE_INITIALIZED,
  25};
  26
  27enum {
  28    DEV_NVECTORS_UNSPECIFIED = -1,
  29};
  30
  31#define TYPE_DEVICE "device"
  32#define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
  33#define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
  34#define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
  35
  36typedef int (*qdev_initfn)(DeviceState *dev);
  37typedef int (*qdev_event)(DeviceState *dev);
  38typedef void (*qdev_resetfn)(DeviceState *dev);
  39
  40typedef struct DeviceClass {
  41    ObjectClass parent_class;
  42
  43    const char *fw_name;
  44    const char *desc;
  45    Property *props;
  46    int no_user;
  47
  48    /* callbacks */
  49    void (*reset)(DeviceState *dev);
  50
  51    /* device state */
  52    const VMStateDescription *vmsd;
  53
  54    /* Private to qdev / bus.  */
  55    qdev_initfn init;
  56    qdev_event unplug;
  57    qdev_event exit;
  58    BusInfo *bus_info;
  59} DeviceClass;
  60
  61/* This structure should not be accessed directly.  We declare it here
  62   so that it can be embedded in individual device state structures.  */
  63struct DeviceState {
  64    Object parent_obj;
  65
  66    const char *id;
  67    enum DevState state;
  68    QemuOpts *opts;
  69    int hotplugged;
  70    BusState *parent_bus;
  71    int num_gpio_out;
  72    qemu_irq *gpio_out;
  73    int num_gpio_in;
  74    qemu_irq *gpio_in;
  75    QLIST_HEAD(, BusState) child_bus;
  76    int num_child_bus;
  77    QTAILQ_ENTRY(DeviceState) sibling;
  78    int instance_id_alias;
  79    int alias_required_for_version;
  80};
  81
  82typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
  83typedef char *(*bus_get_dev_path)(DeviceState *dev);
  84/*
  85 * This callback is used to create Open Firmware device path in accordance with
  86 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
  87 * can be found here http://playground.sun.com/1275/bindings/.
  88 */
  89typedef char *(*bus_get_fw_dev_path)(DeviceState *dev);
  90typedef int (qbus_resetfn)(BusState *bus);
  91
  92struct BusInfo {
  93    const char *name;
  94    size_t size;
  95    bus_dev_printfn print_dev;
  96    bus_get_dev_path get_dev_path;
  97    bus_get_fw_dev_path get_fw_dev_path;
  98    qbus_resetfn *reset;
  99    Property *props;
 100};
 101
 102struct BusState {
 103    DeviceState *parent;
 104    BusInfo *info;
 105    const char *name;
 106    int allow_hotplug;
 107    int qdev_allocated;
 108    QTAILQ_HEAD(ChildrenHead, DeviceState) children;
 109    QLIST_ENTRY(BusState) sibling;
 110};
 111
 112struct Property {
 113    const char   *name;
 114    PropertyInfo *info;
 115    int          offset;
 116    uint8_t      bitnr;
 117    uint8_t      qtype;
 118    int64_t      defval;
 119};
 120
 121struct PropertyInfo {
 122    const char *name;
 123    const char *legacy_name;
 124    const char **enum_table;
 125    int64_t min;
 126    int64_t max;
 127    int (*parse)(DeviceState *dev, Property *prop, const char *str);
 128    int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
 129    ObjectPropertyAccessor *get;
 130    ObjectPropertyAccessor *set;
 131    ObjectPropertyRelease *release;
 132};
 133
 134typedef struct GlobalProperty {
 135    const char *driver;
 136    const char *property;
 137    const char *value;
 138    QTAILQ_ENTRY(GlobalProperty) next;
 139} GlobalProperty;
 140
 141/*** Board API.  This should go away once we have a machine config file.  ***/
 142
 143DeviceState *qdev_create(BusState *bus, const char *name);
 144DeviceState *qdev_try_create(BusState *bus, const char *name);
 145bool qdev_exists(const char *name);
 146int qdev_device_help(QemuOpts *opts);
 147DeviceState *qdev_device_add(QemuOpts *opts);
 148int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
 149void qdev_init_nofail(DeviceState *dev);
 150void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
 151                                 int required_for_version);
 152void qdev_unplug(DeviceState *dev, Error **errp);
 153void qdev_free(DeviceState *dev);
 154int qdev_simple_unplug_cb(DeviceState *dev);
 155void qdev_machine_creation_done(void);
 156bool qdev_machine_modified(void);
 157
 158qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
 159void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
 160
 161BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
 162
 163/*** Device API.  ***/
 164
 165/* Register device properties.  */
 166/* GPIO inputs also double as IRQ sinks.  */
 167void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
 168void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
 169
 170BusState *qdev_get_parent_bus(DeviceState *dev);
 171
 172/*** BUS API. ***/
 173
 174DeviceState *qdev_find_recursive(BusState *bus, const char *id);
 175
 176/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
 177typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
 178typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
 179
 180void qbus_create_inplace(BusState *bus, BusInfo *info,
 181                         DeviceState *parent, const char *name);
 182BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
 183/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
 184 *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
 185 *           0 otherwise. */
 186int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
 187                       qbus_walkerfn *busfn, void *opaque);
 188int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
 189                       qbus_walkerfn *busfn, void *opaque);
 190void qdev_reset_all(DeviceState *dev);
 191void qbus_reset_all_fn(void *opaque);
 192
 193void qbus_free(BusState *bus);
 194
 195#define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
 196
 197/* This should go away once we get rid of the NULL bus hack */
 198BusState *sysbus_get_default(void);
 199
 200/*** monitor commands ***/
 201
 202void do_info_qtree(Monitor *mon);
 203void do_info_qdm(Monitor *mon);
 204int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
 205int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
 206
 207/*** qdev-properties.c ***/
 208
 209extern PropertyInfo qdev_prop_bit;
 210extern PropertyInfo qdev_prop_uint8;
 211extern PropertyInfo qdev_prop_uint16;
 212extern PropertyInfo qdev_prop_uint32;
 213extern PropertyInfo qdev_prop_int32;
 214extern PropertyInfo qdev_prop_uint64;
 215extern PropertyInfo qdev_prop_hex8;
 216extern PropertyInfo qdev_prop_hex32;
 217extern PropertyInfo qdev_prop_hex64;
 218extern PropertyInfo qdev_prop_string;
 219extern PropertyInfo qdev_prop_chr;
 220extern PropertyInfo qdev_prop_ptr;
 221extern PropertyInfo qdev_prop_macaddr;
 222extern PropertyInfo qdev_prop_losttickpolicy;
 223extern PropertyInfo qdev_prop_drive;
 224extern PropertyInfo qdev_prop_netdev;
 225extern PropertyInfo qdev_prop_vlan;
 226extern PropertyInfo qdev_prop_pci_devfn;
 227extern PropertyInfo qdev_prop_blocksize;
 228
 229#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
 230        .name      = (_name),                                    \
 231        .info      = &(_prop),                                   \
 232        .offset    = offsetof(_state, _field)                    \
 233            + type_check(_type,typeof_field(_state, _field)),    \
 234        }
 235#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
 236        .name      = (_name),                                           \
 237        .info      = &(_prop),                                          \
 238        .offset    = offsetof(_state, _field)                           \
 239            + type_check(_type,typeof_field(_state, _field)),           \
 240        .qtype     = QTYPE_QINT,                                        \
 241        .defval    = (_type)_defval,                                    \
 242        }
 243#define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
 244        .name      = (_name),                                    \
 245        .info      = &(qdev_prop_bit),                           \
 246        .bitnr    = (_bit),                                      \
 247        .offset    = offsetof(_state, _field)                    \
 248            + type_check(uint32_t,typeof_field(_state, _field)), \
 249        .qtype     = QTYPE_QBOOL,                                \
 250        .defval    = (bool)_defval,                              \
 251        }
 252
 253#define DEFINE_PROP_UINT8(_n, _s, _f, _d)                       \
 254    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
 255#define DEFINE_PROP_UINT16(_n, _s, _f, _d)                      \
 256    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
 257#define DEFINE_PROP_UINT32(_n, _s, _f, _d)                      \
 258    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
 259#define DEFINE_PROP_INT32(_n, _s, _f, _d)                      \
 260    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
 261#define DEFINE_PROP_UINT64(_n, _s, _f, _d)                      \
 262    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
 263#define DEFINE_PROP_HEX8(_n, _s, _f, _d)                       \
 264    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
 265#define DEFINE_PROP_HEX32(_n, _s, _f, _d)                       \
 266    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
 267#define DEFINE_PROP_HEX64(_n, _s, _f, _d)                       \
 268    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
 269#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d)                   \
 270    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
 271
 272#define DEFINE_PROP_PTR(_n, _s, _f)             \
 273    DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
 274#define DEFINE_PROP_CHR(_n, _s, _f)             \
 275    DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
 276#define DEFINE_PROP_STRING(_n, _s, _f)             \
 277    DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
 278#define DEFINE_PROP_NETDEV(_n, _s, _f)             \
 279    DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
 280#define DEFINE_PROP_VLAN(_n, _s, _f)             \
 281    DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
 282#define DEFINE_PROP_DRIVE(_n, _s, _f) \
 283    DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
 284#define DEFINE_PROP_MACADDR(_n, _s, _f)         \
 285    DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
 286#define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
 287    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
 288                        LostTickPolicy)
 289#define DEFINE_PROP_BLOCKSIZE(_n, _s, _f, _d) \
 290    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_blocksize, uint16_t)
 291
 292#define DEFINE_PROP_END_OF_LIST()               \
 293    {}
 294
 295/* Set properties between creation and init.  */
 296void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
 297int qdev_prop_exists(DeviceState *dev, const char *name);
 298int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
 299void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
 300void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
 301void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
 302void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
 303void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
 304void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
 305void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
 306void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
 307void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
 308void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
 309int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
 310void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
 311void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
 312void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
 313/* FIXME: Remove opaque pointer properties.  */
 314void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
 315void qdev_prop_set_defaults(DeviceState *dev, Property *props);
 316
 317void qdev_prop_register_global_list(GlobalProperty *props);
 318void qdev_prop_set_globals(DeviceState *dev);
 319void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
 320                                    Property *prop, const char *value);
 321
 322char *qdev_get_fw_dev_path(DeviceState *dev);
 323
 324/* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
 325extern struct BusInfo system_bus_info;
 326
 327/**
 328 * @qdev_property_add_static - add a @Property to a device referencing a
 329 * field in a struct.
 330 */
 331void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp);
 332
 333/**
 334 * @qdev_machine_init
 335 *
 336 * Initialize platform devices before machine init.  This is a hack until full
 337 * support for composition is added.
 338 */
 339void qdev_machine_init(void);
 340
 341/**
 342 * @device_reset
 343 *
 344 * Reset a single device (by calling the reset method).
 345 */
 346void device_reset(DeviceState *dev);
 347
 348const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
 349
 350const char *qdev_fw_name(DeviceState *dev);
 351
 352BusInfo *qdev_get_bus_info(DeviceState *dev);
 353
 354Property *qdev_get_props(DeviceState *dev);
 355
 356Object *qdev_get_machine(void);
 357
 358/* FIXME: make this a link<> */
 359void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
 360
 361extern int qdev_hotplug;
 362
 363#endif
 364