1 #ifndef QDEV_CORE_H 2 #define QDEV_CORE_H 3 4 #include "qemu/queue.h" 5 #include "qemu/bitmap.h" 6 #include "qom/object.h" 7 #include "hw/hotplug.h" 8 9 enum { 10 DEV_NVECTORS_UNSPECIFIED = -1, 11 }; 12 13 #define TYPE_DEVICE "device" 14 #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE) 15 #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE) 16 #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE) 17 18 typedef enum DeviceCategory { 19 DEVICE_CATEGORY_BRIDGE, 20 DEVICE_CATEGORY_USB, 21 DEVICE_CATEGORY_STORAGE, 22 DEVICE_CATEGORY_NETWORK, 23 DEVICE_CATEGORY_INPUT, 24 DEVICE_CATEGORY_DISPLAY, 25 DEVICE_CATEGORY_SOUND, 26 DEVICE_CATEGORY_MISC, 27 DEVICE_CATEGORY_CPU, 28 DEVICE_CATEGORY_MAX 29 } DeviceCategory; 30 31 typedef void (*DeviceRealize)(DeviceState *dev, Error **errp); 32 typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp); 33 typedef void (*DeviceReset)(DeviceState *dev); 34 typedef void (*BusRealize)(BusState *bus, Error **errp); 35 typedef void (*BusUnrealize)(BusState *bus, Error **errp); 36 37 /** 38 * DeviceClass: 39 * @props: Properties accessing state fields. 40 * @realize: Callback function invoked when the #DeviceState:realized 41 * property is changed to %true. 42 * @unrealize: Callback function invoked when the #DeviceState:realized 43 * property is changed to %false. 44 * @hotpluggable: indicates if #DeviceClass is hotpluggable, available 45 * as readonly "hotpluggable" property of #DeviceState instance 46 * 47 * # Realization # 48 * Devices are constructed in two stages, 49 * 1) object instantiation via object_initialize() and 50 * 2) device realization via #DeviceState:realized property. 51 * The former may not fail (and must not abort or exit, since it is called 52 * during device introspection already), and the latter may return error 53 * information to the caller and must be re-entrant. 54 * Trivial field initializations should go into #TypeInfo.instance_init. 55 * Operations depending on @props static properties should go into @realize. 56 * After successful realization, setting static properties will fail. 57 * 58 * As an interim step, the #DeviceState:realized property can also be 59 * set with qdev_init_nofail(). 60 * In the future, devices will propagate this state change to their children 61 * and along busses they expose. 62 * The point in time will be deferred to machine creation, so that values 63 * set in @realize will not be introspectable beforehand. Therefore devices 64 * must not create children during @realize; they should initialize them via 65 * object_initialize() in their own #TypeInfo.instance_init and forward the 66 * realization events appropriately. 67 * 68 * Any type may override the @realize and/or @unrealize callbacks but needs 69 * to call the parent type's implementation if keeping their functionality 70 * is desired. Refer to QOM documentation for further discussion and examples. 71 * 72 * <note> 73 * <para> 74 * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types 75 * derived directly from it need not call their parent's @realize and 76 * @unrealize. 77 * For other types consult the documentation and implementation of the 78 * respective parent types. 79 * </para> 80 * </note> 81 * 82 * # Hiding a device # 83 * To hide a device, a DeviceListener function should_be_hidden() needs to 84 * be registered. 85 * It can be used to defer adding a device and therefore hide it from the 86 * guest. The handler registering to this DeviceListener can save the QOpts 87 * passed to it for re-using it later and must return that it wants the device 88 * to be/remain hidden or not. When the handler function decides the device 89 * shall not be hidden it will be added in qdev_device_add() and 90 * realized as any other device. Otherwise qdev_device_add() will return early 91 * without adding the device. The guest will not see a "hidden" device 92 * until it was marked don't hide and qdev_device_add called again. 93 * 94 */ 95 typedef struct DeviceClass { 96 /*< private >*/ 97 ObjectClass parent_class; 98 /*< public >*/ 99 100 DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX); 101 const char *fw_name; 102 const char *desc; 103 Property *props; 104 105 /* 106 * Can this device be instantiated with -device / device_add? 107 * All devices should support instantiation with device_add, and 108 * this flag should not exist. But we're not there, yet. Some 109 * devices fail to instantiate with cryptic error messages. 110 * Others instantiate, but don't work. Exposing users to such 111 * behavior would be cruel; clearing this flag will protect them. 112 * It should never be cleared without a comment explaining why it 113 * is cleared. 114 * TODO remove once we're there 115 */ 116 bool user_creatable; 117 bool hotpluggable; 118 119 /* callbacks */ 120 DeviceReset reset; 121 DeviceRealize realize; 122 DeviceUnrealize unrealize; 123 124 /* device state */ 125 const VMStateDescription *vmsd; 126 127 /* Private to qdev / bus. */ 128 const char *bus_type; 129 } DeviceClass; 130 131 typedef struct NamedGPIOList NamedGPIOList; 132 133 struct NamedGPIOList { 134 char *name; 135 qemu_irq *in; 136 int num_in; 137 int num_out; 138 QLIST_ENTRY(NamedGPIOList) node; 139 }; 140 141 /** 142 * DeviceState: 143 * @realized: Indicates whether the device has been fully constructed. 144 * 145 * This structure should not be accessed directly. We declare it here 146 * so that it can be embedded in individual device state structures. 147 */ 148 struct DeviceState { 149 /*< private >*/ 150 Object parent_obj; 151 /*< public >*/ 152 153 const char *id; 154 char *canonical_path; 155 bool realized; 156 bool pending_deleted_event; 157 QemuOpts *opts; 158 int hotplugged; 159 BusState *parent_bus; 160 QLIST_HEAD(, NamedGPIOList) gpios; 161 QLIST_HEAD(, BusState) child_bus; 162 int num_child_bus; 163 int instance_id_alias; 164 int alias_required_for_version; 165 }; 166 167 struct DeviceListener { 168 void (*realize)(DeviceListener *listener, DeviceState *dev); 169 void (*unrealize)(DeviceListener *listener, DeviceState *dev); 170 /* 171 * This callback is called upon init of the DeviceState and allows to 172 * inform qdev that a device should be hidden, depending on the device 173 * opts, for example, to hide a standby device. 174 */ 175 int (*should_be_hidden)(DeviceListener *listener, QemuOpts *device_opts); 176 QTAILQ_ENTRY(DeviceListener) link; 177 }; 178 179 #define TYPE_BUS "bus" 180 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS) 181 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS) 182 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS) 183 184 struct BusClass { 185 ObjectClass parent_class; 186 187 /* FIXME first arg should be BusState */ 188 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent); 189 char *(*get_dev_path)(DeviceState *dev); 190 /* 191 * This callback is used to create Open Firmware device path in accordance 192 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus 193 * bindings can be found at http://playground.sun.com/1275/bindings/. 194 */ 195 char *(*get_fw_dev_path)(DeviceState *dev); 196 void (*reset)(BusState *bus); 197 BusRealize realize; 198 BusUnrealize unrealize; 199 200 /* maximum devices allowed on the bus, 0: no limit. */ 201 int max_dev; 202 /* number of automatically allocated bus ids (e.g. ide.0) */ 203 int automatic_ids; 204 }; 205 206 typedef struct BusChild { 207 DeviceState *child; 208 int index; 209 QTAILQ_ENTRY(BusChild) sibling; 210 } BusChild; 211 212 #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler" 213 214 /** 215 * BusState: 216 * @hotplug_handler: link to a hotplug handler associated with bus. 217 */ 218 struct BusState { 219 Object obj; 220 DeviceState *parent; 221 char *name; 222 HotplugHandler *hotplug_handler; 223 int max_index; 224 bool realized; 225 int num_children; 226 QTAILQ_HEAD(, BusChild) children; 227 QLIST_ENTRY(BusState) sibling; 228 }; 229 230 /** 231 * Property: 232 * @set_default: true if the default value should be set from @defval, 233 * in which case @info->set_default_value must not be NULL 234 * (if false then no default value is set by the property system 235 * and the field retains whatever value it was given by instance_init). 236 * @defval: default value for the property. This is used only if @set_default 237 * is true. 238 */ 239 struct Property { 240 const char *name; 241 const PropertyInfo *info; 242 ptrdiff_t offset; 243 uint8_t bitnr; 244 bool set_default; 245 union { 246 int64_t i; 247 uint64_t u; 248 } defval; 249 int arrayoffset; 250 const PropertyInfo *arrayinfo; 251 int arrayfieldsize; 252 const char *link_type; 253 }; 254 255 struct PropertyInfo { 256 const char *name; 257 const char *description; 258 const QEnumLookup *enum_table; 259 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len); 260 void (*set_default_value)(Object *obj, const Property *prop); 261 void (*create)(Object *obj, Property *prop, Error **errp); 262 ObjectPropertyAccessor *get; 263 ObjectPropertyAccessor *set; 264 ObjectPropertyRelease *release; 265 }; 266 267 /** 268 * GlobalProperty: 269 * @used: Set to true if property was used when initializing a device. 270 * @optional: If set to true, GlobalProperty will be skipped without errors 271 * if the property doesn't exist. 272 * 273 * An error is fatal for non-hotplugged devices, when the global is applied. 274 */ 275 typedef struct GlobalProperty { 276 const char *driver; 277 const char *property; 278 const char *value; 279 bool used; 280 bool optional; 281 } GlobalProperty; 282 283 static inline void 284 compat_props_add(GPtrArray *arr, 285 GlobalProperty props[], size_t nelem) 286 { 287 int i; 288 for (i = 0; i < nelem; i++) { 289 g_ptr_array_add(arr, (void *)&props[i]); 290 } 291 } 292 293 /*** Board API. This should go away once we have a machine config file. ***/ 294 295 DeviceState *qdev_create(BusState *bus, const char *name); 296 DeviceState *qdev_try_create(BusState *bus, const char *name); 297 void qdev_init_nofail(DeviceState *dev); 298 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, 299 int required_for_version); 300 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev); 301 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev); 302 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp); 303 /** 304 * qdev_get_hotplug_handler: Get handler responsible for device wiring 305 * 306 * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it. 307 * 308 * Note: in case @dev has a parent bus, it will be returned as handler unless 309 * machine handler overrides it. 310 * 311 * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface 312 * or NULL if there aren't any. 313 */ 314 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev); 315 void qdev_unplug(DeviceState *dev, Error **errp); 316 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev, 317 DeviceState *dev, Error **errp); 318 void qdev_machine_creation_done(void); 319 bool qdev_machine_modified(void); 320 321 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n); 322 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n); 323 324 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin); 325 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n, 326 qemu_irq pin); 327 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n); 328 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt, 329 const char *name, int n); 330 331 BusState *qdev_get_child_bus(DeviceState *dev, const char *name); 332 333 /*** Device API. ***/ 334 335 /* Register device properties. */ 336 /* GPIO inputs also double as IRQ sinks. */ 337 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n); 338 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n); 339 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins, 340 const char *name, int n); 341 /** 342 * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines 343 * for the specified device 344 * 345 * @dev: Device to create input GPIOs for 346 * @handler: Function to call when GPIO line value is set 347 * @opaque: Opaque data pointer to pass to @handler 348 * @name: Name of the GPIO input (must be unique for this device) 349 * @n: Number of GPIO lines in this input set 350 */ 351 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev, 352 qemu_irq_handler handler, 353 void *opaque, 354 const char *name, int n); 355 356 /** 357 * qdev_init_gpio_in_named: create an array of input GPIO lines 358 * for the specified device 359 * 360 * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer 361 * passed to the handler is @dev (which is the most commonly desired behaviour). 362 */ 363 static inline void qdev_init_gpio_in_named(DeviceState *dev, 364 qemu_irq_handler handler, 365 const char *name, int n) 366 { 367 qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n); 368 } 369 370 void qdev_pass_gpios(DeviceState *dev, DeviceState *container, 371 const char *name); 372 373 BusState *qdev_get_parent_bus(DeviceState *dev); 374 375 /*** BUS API. ***/ 376 377 DeviceState *qdev_find_recursive(BusState *bus, const char *id); 378 379 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */ 380 typedef int (qbus_walkerfn)(BusState *bus, void *opaque); 381 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque); 382 383 void qbus_create_inplace(void *bus, size_t size, const char *typename, 384 DeviceState *parent, const char *name); 385 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name); 386 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion, 387 * < 0 if either devfn or busfn terminate walk somewhere in cursion, 388 * 0 otherwise. */ 389 int qbus_walk_children(BusState *bus, 390 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, 391 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, 392 void *opaque); 393 int qdev_walk_children(DeviceState *dev, 394 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, 395 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, 396 void *opaque); 397 398 void qdev_reset_all(DeviceState *dev); 399 void qdev_reset_all_fn(void *opaque); 400 401 /** 402 * @qbus_reset_all: 403 * @bus: Bus to be reset. 404 * 405 * Reset @bus and perform a bus-level ("hard") reset of all devices connected 406 * to it, including recursive processing of all buses below @bus itself. A 407 * hard reset means that qbus_reset_all will reset all state of the device. 408 * For PCI devices, for example, this will include the base address registers 409 * or configuration space. 410 */ 411 void qbus_reset_all(BusState *bus); 412 void qbus_reset_all_fn(void *opaque); 413 414 /* This should go away once we get rid of the NULL bus hack */ 415 BusState *sysbus_get_default(void); 416 417 char *qdev_get_fw_dev_path(DeviceState *dev); 418 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev); 419 420 /** 421 * @qdev_machine_init 422 * 423 * Initialize platform devices before machine init. This is a hack until full 424 * support for composition is added. 425 */ 426 void qdev_machine_init(void); 427 428 /** 429 * @device_reset 430 * 431 * Reset a single device (by calling the reset method). 432 */ 433 void device_reset(DeviceState *dev); 434 435 void device_class_set_parent_reset(DeviceClass *dc, 436 DeviceReset dev_reset, 437 DeviceReset *parent_reset); 438 void device_class_set_parent_realize(DeviceClass *dc, 439 DeviceRealize dev_realize, 440 DeviceRealize *parent_realize); 441 void device_class_set_parent_unrealize(DeviceClass *dc, 442 DeviceUnrealize dev_unrealize, 443 DeviceUnrealize *parent_unrealize); 444 445 const VMStateDescription *qdev_get_vmsd(DeviceState *dev); 446 447 const char *qdev_fw_name(DeviceState *dev); 448 449 Object *qdev_get_machine(void); 450 451 /* FIXME: make this a link<> */ 452 void qdev_set_parent_bus(DeviceState *dev, BusState *bus); 453 454 extern bool qdev_hotplug; 455 extern bool qdev_hot_removed; 456 457 char *qdev_get_dev_path(DeviceState *dev); 458 459 GSList *qdev_build_hotpluggable_device_list(Object *peripheral); 460 461 void qbus_set_hotplug_handler(BusState *bus, Object *handler, Error **errp); 462 463 void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp); 464 465 static inline bool qbus_is_hotpluggable(BusState *bus) 466 { 467 return bus->hotplug_handler; 468 } 469 470 void device_listener_register(DeviceListener *listener); 471 void device_listener_unregister(DeviceListener *listener); 472 473 /** 474 * @qdev_should_hide_device: 475 * @opts: QemuOpts as passed on cmdline. 476 * 477 * Check if a device should be added. 478 * When a device is added via qdev_device_add() this will be called, 479 * and return if the device should be added now or not. 480 */ 481 bool qdev_should_hide_device(QemuOpts *opts); 482 483 #endif 484