1 #ifndef QDEV_CORE_H 2 #define QDEV_CORE_H 3 4 #include "qemu/queue.h" 5 #include "qemu/option.h" 6 #include "qemu/typedefs.h" 7 #include "qemu/bitmap.h" 8 #include "qom/object.h" 9 #include "hw/irq.h" 10 #include "qapi/error.h" 11 12 enum { 13 DEV_NVECTORS_UNSPECIFIED = -1, 14 }; 15 16 #define TYPE_DEVICE "device" 17 #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE) 18 #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE) 19 #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE) 20 21 typedef enum DeviceCategory { 22 DEVICE_CATEGORY_BRIDGE, 23 DEVICE_CATEGORY_USB, 24 DEVICE_CATEGORY_STORAGE, 25 DEVICE_CATEGORY_NETWORK, 26 DEVICE_CATEGORY_INPUT, 27 DEVICE_CATEGORY_DISPLAY, 28 DEVICE_CATEGORY_SOUND, 29 DEVICE_CATEGORY_MISC, 30 DEVICE_CATEGORY_MAX 31 } DeviceCategory; 32 33 typedef int (*qdev_initfn)(DeviceState *dev); 34 typedef int (*qdev_event)(DeviceState *dev); 35 typedef void (*qdev_resetfn)(DeviceState *dev); 36 typedef void (*DeviceRealize)(DeviceState *dev, Error **errp); 37 typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp); 38 39 struct VMStateDescription; 40 41 /** 42 * DeviceClass: 43 * @props: Properties accessing state fields. 44 * @realize: Callback function invoked when the #DeviceState:realized 45 * property is changed to %true. The default invokes @init if not %NULL. 46 * @unrealize: Callback function invoked when the #DeviceState:realized 47 * property is changed to %false. 48 * @init: Callback function invoked when the #DeviceState::realized property 49 * is changed to %true. Deprecated, new types inheriting directly from 50 * TYPE_DEVICE should use @realize instead, new leaf types should consult 51 * their respective parent type. 52 * 53 * # Realization # 54 * Devices are constructed in two stages, 55 * 1) object instantiation via object_initialize() and 56 * 2) device realization via #DeviceState:realized property. 57 * The former may not fail (it might assert or exit), the latter may return 58 * error information to the caller and must be re-entrant. 59 * Trivial field initializations should go into #TypeInfo.instance_init. 60 * Operations depending on @props static properties should go into @realize. 61 * After successful realization, setting static properties will fail. 62 * 63 * As an interim step, the #DeviceState:realized property is set by deprecated 64 * functions qdev_init() and qdev_init_nofail(). 65 * In the future, devices will propagate this state change to their children 66 * and along busses they expose. 67 * The point in time will be deferred to machine creation, so that values 68 * set in @realize will not be introspectable beforehand. Therefore devices 69 * must not create children during @realize; they should initialize them via 70 * object_initialize() in their own #TypeInfo.instance_init and forward the 71 * realization events appropriately. 72 * 73 * The @init callback is considered private to a particular bus implementation 74 * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an 75 * "init" callback on their parent class instead. 76 * 77 * Any type may override the @realize and/or @unrealize callbacks but needs 78 * to call the parent type's implementation if keeping their functionality 79 * is desired. Refer to QOM documentation for further discussion and examples. 80 * 81 * <note> 82 * <para> 83 * If a type derived directly from TYPE_DEVICE implements @realize, it does 84 * not need to implement @init and therefore does not need to store and call 85 * #DeviceClass' default @realize callback. 86 * For other types consult the documentation and implementation of the 87 * respective parent types. 88 * </para> 89 * </note> 90 */ 91 typedef struct DeviceClass { 92 /*< private >*/ 93 ObjectClass parent_class; 94 /*< public >*/ 95 96 DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX); 97 const char *fw_name; 98 const char *desc; 99 Property *props; 100 int no_user; 101 102 /* callbacks */ 103 void (*reset)(DeviceState *dev); 104 DeviceRealize realize; 105 DeviceUnrealize unrealize; 106 107 /* device state */ 108 const struct VMStateDescription *vmsd; 109 110 /* Private to qdev / bus. */ 111 qdev_initfn init; /* TODO remove, once users are converted to realize */ 112 qdev_event unplug; 113 qdev_event exit; /* TODO remove, once users are converted to unrealize */ 114 const char *bus_type; 115 } DeviceClass; 116 117 /** 118 * DeviceState: 119 * @realized: Indicates whether the device has been fully constructed. 120 * 121 * This structure should not be accessed directly. We declare it here 122 * so that it can be embedded in individual device state structures. 123 */ 124 struct DeviceState { 125 /*< private >*/ 126 Object parent_obj; 127 /*< public >*/ 128 129 const char *id; 130 bool realized; 131 QemuOpts *opts; 132 int hotplugged; 133 BusState *parent_bus; 134 int num_gpio_out; 135 qemu_irq *gpio_out; 136 int num_gpio_in; 137 qemu_irq *gpio_in; 138 QLIST_HEAD(, BusState) child_bus; 139 int num_child_bus; 140 int instance_id_alias; 141 int alias_required_for_version; 142 }; 143 144 #define TYPE_BUS "bus" 145 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS) 146 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS) 147 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS) 148 149 struct BusClass { 150 ObjectClass parent_class; 151 152 /* FIXME first arg should be BusState */ 153 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent); 154 char *(*get_dev_path)(DeviceState *dev); 155 /* 156 * This callback is used to create Open Firmware device path in accordance 157 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus 158 * bindings can be found at http://playground.sun.com/1275/bindings/. 159 */ 160 char *(*get_fw_dev_path)(DeviceState *dev); 161 int (*reset)(BusState *bus); 162 /* maximum devices allowed on the bus, 0: no limit. */ 163 int max_dev; 164 }; 165 166 typedef struct BusChild { 167 DeviceState *child; 168 int index; 169 QTAILQ_ENTRY(BusChild) sibling; 170 } BusChild; 171 172 /** 173 * BusState: 174 */ 175 struct BusState { 176 Object obj; 177 DeviceState *parent; 178 const char *name; 179 int allow_hotplug; 180 int max_index; 181 QTAILQ_HEAD(ChildrenHead, BusChild) children; 182 QLIST_ENTRY(BusState) sibling; 183 }; 184 185 struct Property { 186 const char *name; 187 PropertyInfo *info; 188 int offset; 189 uint8_t bitnr; 190 uint8_t qtype; 191 int64_t defval; 192 int arrayoffset; 193 PropertyInfo *arrayinfo; 194 int arrayfieldsize; 195 }; 196 197 struct PropertyInfo { 198 const char *name; 199 const char *legacy_name; 200 const char **enum_table; 201 int (*parse)(DeviceState *dev, Property *prop, const char *str); 202 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len); 203 ObjectPropertyAccessor *get; 204 ObjectPropertyAccessor *set; 205 ObjectPropertyRelease *release; 206 }; 207 208 typedef struct GlobalProperty { 209 const char *driver; 210 const char *property; 211 const char *value; 212 QTAILQ_ENTRY(GlobalProperty) next; 213 } GlobalProperty; 214 215 /*** Board API. This should go away once we have a machine config file. ***/ 216 217 DeviceState *qdev_create(BusState *bus, const char *name); 218 DeviceState *qdev_try_create(BusState *bus, const char *name); 219 int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT; 220 void qdev_init_nofail(DeviceState *dev); 221 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, 222 int required_for_version); 223 void qdev_unplug(DeviceState *dev, Error **errp); 224 int qdev_simple_unplug_cb(DeviceState *dev); 225 void qdev_machine_creation_done(void); 226 bool qdev_machine_modified(void); 227 228 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n); 229 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin); 230 231 BusState *qdev_get_child_bus(DeviceState *dev, const char *name); 232 233 /*** Device API. ***/ 234 235 /* Register device properties. */ 236 /* GPIO inputs also double as IRQ sinks. */ 237 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n); 238 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n); 239 240 BusState *qdev_get_parent_bus(DeviceState *dev); 241 242 /*** BUS API. ***/ 243 244 DeviceState *qdev_find_recursive(BusState *bus, const char *id); 245 246 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */ 247 typedef int (qbus_walkerfn)(BusState *bus, void *opaque); 248 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque); 249 250 void qbus_create_inplace(void *bus, size_t size, const char *typename, 251 DeviceState *parent, const char *name); 252 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name); 253 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion, 254 * < 0 if either devfn or busfn terminate walk somewhere in cursion, 255 * 0 otherwise. */ 256 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, 257 qbus_walkerfn *busfn, void *opaque); 258 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, 259 qbus_walkerfn *busfn, void *opaque); 260 void qdev_reset_all(DeviceState *dev); 261 262 /** 263 * @qbus_reset_all: 264 * @bus: Bus to be reset. 265 * 266 * Reset @bus and perform a bus-level ("hard") reset of all devices connected 267 * to it, including recursive processing of all buses below @bus itself. A 268 * hard reset means that qbus_reset_all will reset all state of the device. 269 * For PCI devices, for example, this will include the base address registers 270 * or configuration space. 271 */ 272 void qbus_reset_all(BusState *bus); 273 void qbus_reset_all_fn(void *opaque); 274 275 void qbus_free(BusState *bus); 276 277 /* This should go away once we get rid of the NULL bus hack */ 278 BusState *sysbus_get_default(void); 279 280 char *qdev_get_fw_dev_path(DeviceState *dev); 281 282 /** 283 * @qdev_machine_init 284 * 285 * Initialize platform devices before machine init. This is a hack until full 286 * support for composition is added. 287 */ 288 void qdev_machine_init(void); 289 290 /** 291 * @device_reset 292 * 293 * Reset a single device (by calling the reset method). 294 */ 295 void device_reset(DeviceState *dev); 296 297 const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev); 298 299 const char *qdev_fw_name(DeviceState *dev); 300 301 Object *qdev_get_machine(void); 302 303 /* FIXME: make this a link<> */ 304 void qdev_set_parent_bus(DeviceState *dev, BusState *bus); 305 306 extern int qdev_hotplug; 307 308 char *qdev_get_dev_path(DeviceState *dev); 309 310 #endif 311