4 #include "qemu/queue.h"
5 #include "qemu/bitmap.h"
6 #include "qom/object.h"
7 #include "hw/hotplug.h"
10 DEV_NVECTORS_UNSPECIFIED
= -1,
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)
18 typedef enum DeviceCategory
{
19 DEVICE_CATEGORY_BRIDGE
,
21 DEVICE_CATEGORY_STORAGE
,
22 DEVICE_CATEGORY_NETWORK
,
23 DEVICE_CATEGORY_INPUT
,
24 DEVICE_CATEGORY_DISPLAY
,
25 DEVICE_CATEGORY_SOUND
,
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
);
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
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.
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.
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.
74 * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
75 * derived directly from it need not call their parent's @realize and
77 * For other types consult the documentation and implementation of the
78 * respective parent types.
83 * To hide a device, a DeviceListener function should_be_hidden() needs to
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.
95 typedef struct DeviceClass
{
97 ObjectClass parent_class
;
100 DECLARE_BITMAP(categories
, DEVICE_CATEGORY_MAX
);
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
114 * TODO remove once we're there
121 DeviceRealize realize
;
122 DeviceUnrealize unrealize
;
125 const VMStateDescription
*vmsd
;
127 /* Private to qdev / bus. */
128 const char *bus_type
;
131 typedef struct NamedGPIOList NamedGPIOList
;
133 struct NamedGPIOList
{
138 QLIST_ENTRY(NamedGPIOList
) node
;
143 * @realized: Indicates whether the device has been fully constructed.
145 * This structure should not be accessed directly. We declare it here
146 * so that it can be embedded in individual device state structures.
154 char *canonical_path
;
156 bool pending_deleted_event
;
159 bool allow_unplug_during_migration
;
160 BusState
*parent_bus
;
161 QLIST_HEAD(, NamedGPIOList
) gpios
;
162 QLIST_HEAD(, BusState
) child_bus
;
164 int instance_id_alias
;
165 int alias_required_for_version
;
168 struct DeviceListener
{
169 void (*realize
)(DeviceListener
*listener
, DeviceState
*dev
);
170 void (*unrealize
)(DeviceListener
*listener
, DeviceState
*dev
);
172 * This callback is called upon init of the DeviceState and allows to
173 * inform qdev that a device should be hidden, depending on the device
174 * opts, for example, to hide a standby device.
176 int (*should_be_hidden
)(DeviceListener
*listener
, QemuOpts
*device_opts
);
177 QTAILQ_ENTRY(DeviceListener
) link
;
180 #define TYPE_BUS "bus"
181 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
182 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
183 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
186 ObjectClass parent_class
;
188 /* FIXME first arg should be BusState */
189 void (*print_dev
)(Monitor
*mon
, DeviceState
*dev
, int indent
);
190 char *(*get_dev_path
)(DeviceState
*dev
);
192 * This callback is used to create Open Firmware device path in accordance
193 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
194 * bindings can be found at http://playground.sun.com/1275/bindings/.
196 char *(*get_fw_dev_path
)(DeviceState
*dev
);
197 void (*reset
)(BusState
*bus
);
199 BusUnrealize unrealize
;
201 /* maximum devices allowed on the bus, 0: no limit. */
203 /* number of automatically allocated bus ids (e.g. ide.0) */
207 typedef struct BusChild
{
210 QTAILQ_ENTRY(BusChild
) sibling
;
213 #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
217 * @hotplug_handler: link to a hotplug handler associated with bus.
223 HotplugHandler
*hotplug_handler
;
227 QTAILQ_HEAD(, BusChild
) children
;
228 QLIST_ENTRY(BusState
) sibling
;
233 * @set_default: true if the default value should be set from @defval,
234 * in which case @info->set_default_value must not be NULL
235 * (if false then no default value is set by the property system
236 * and the field retains whatever value it was given by instance_init).
237 * @defval: default value for the property. This is used only if @set_default
242 const PropertyInfo
*info
;
251 const PropertyInfo
*arrayinfo
;
253 const char *link_type
;
256 struct PropertyInfo
{
258 const char *description
;
259 const QEnumLookup
*enum_table
;
260 int (*print
)(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
);
261 void (*set_default_value
)(Object
*obj
, const Property
*prop
);
262 void (*create
)(Object
*obj
, Property
*prop
, Error
**errp
);
263 ObjectPropertyAccessor
*get
;
264 ObjectPropertyAccessor
*set
;
265 ObjectPropertyRelease
*release
;
270 * @used: Set to true if property was used when initializing a device.
271 * @optional: If set to true, GlobalProperty will be skipped without errors
272 * if the property doesn't exist.
274 * An error is fatal for non-hotplugged devices, when the global is applied.
276 typedef struct GlobalProperty
{
278 const char *property
;
285 compat_props_add(GPtrArray
*arr
,
286 GlobalProperty props
[], size_t nelem
)
289 for (i
= 0; i
< nelem
; i
++) {
290 g_ptr_array_add(arr
, (void *)&props
[i
]);
294 /*** Board API. This should go away once we have a machine config file. ***/
296 DeviceState
*qdev_create(BusState
*bus
, const char *name
);
297 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
);
298 void qdev_init_nofail(DeviceState
*dev
);
299 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
300 int required_for_version
);
301 HotplugHandler
*qdev_get_bus_hotplug_handler(DeviceState
*dev
);
302 HotplugHandler
*qdev_get_machine_hotplug_handler(DeviceState
*dev
);
303 bool qdev_hotplug_allowed(DeviceState
*dev
, Error
**errp
);
305 * qdev_get_hotplug_handler: Get handler responsible for device wiring
307 * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
309 * Note: in case @dev has a parent bus, it will be returned as handler unless
310 * machine handler overrides it.
312 * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
313 * or NULL if there aren't any.
315 HotplugHandler
*qdev_get_hotplug_handler(DeviceState
*dev
);
316 void qdev_unplug(DeviceState
*dev
, Error
**errp
);
317 void qdev_simple_device_unplug_cb(HotplugHandler
*hotplug_dev
,
318 DeviceState
*dev
, Error
**errp
);
319 void qdev_machine_creation_done(void);
320 bool qdev_machine_modified(void);
322 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
);
323 qemu_irq
qdev_get_gpio_in_named(DeviceState
*dev
, const char *name
, int n
);
325 void qdev_connect_gpio_out(DeviceState
*dev
, int n
, qemu_irq pin
);
326 void qdev_connect_gpio_out_named(DeviceState
*dev
, const char *name
, int n
,
328 qemu_irq
qdev_get_gpio_out_connector(DeviceState
*dev
, const char *name
, int n
);
329 qemu_irq
qdev_intercept_gpio_out(DeviceState
*dev
, qemu_irq icpt
,
330 const char *name
, int n
);
332 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
);
334 /*** Device API. ***/
336 /* Register device properties. */
337 /* GPIO inputs also double as IRQ sinks. */
338 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
);
339 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
);
340 void qdev_init_gpio_out_named(DeviceState
*dev
, qemu_irq
*pins
,
341 const char *name
, int n
);
343 * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
344 * for the specified device
346 * @dev: Device to create input GPIOs for
347 * @handler: Function to call when GPIO line value is set
348 * @opaque: Opaque data pointer to pass to @handler
349 * @name: Name of the GPIO input (must be unique for this device)
350 * @n: Number of GPIO lines in this input set
352 void qdev_init_gpio_in_named_with_opaque(DeviceState
*dev
,
353 qemu_irq_handler handler
,
355 const char *name
, int n
);
358 * qdev_init_gpio_in_named: create an array of input GPIO lines
359 * for the specified device
361 * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
362 * passed to the handler is @dev (which is the most commonly desired behaviour).
364 static inline void qdev_init_gpio_in_named(DeviceState
*dev
,
365 qemu_irq_handler handler
,
366 const char *name
, int n
)
368 qdev_init_gpio_in_named_with_opaque(dev
, handler
, dev
, name
, n
);
371 void qdev_pass_gpios(DeviceState
*dev
, DeviceState
*container
,
374 BusState
*qdev_get_parent_bus(DeviceState
*dev
);
378 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
);
380 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
381 typedef int (qbus_walkerfn
)(BusState
*bus
, void *opaque
);
382 typedef int (qdev_walkerfn
)(DeviceState
*dev
, void *opaque
);
384 void qbus_create_inplace(void *bus
, size_t size
, const char *typename
,
385 DeviceState
*parent
, const char *name
);
386 BusState
*qbus_create(const char *typename
, DeviceState
*parent
, const char *name
);
387 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
388 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
390 int qbus_walk_children(BusState
*bus
,
391 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
392 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
394 int qdev_walk_children(DeviceState
*dev
,
395 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
396 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
399 void qdev_reset_all(DeviceState
*dev
);
400 void qdev_reset_all_fn(void *opaque
);
404 * @bus: Bus to be reset.
406 * Reset @bus and perform a bus-level ("hard") reset of all devices connected
407 * to it, including recursive processing of all buses below @bus itself. A
408 * hard reset means that qbus_reset_all will reset all state of the device.
409 * For PCI devices, for example, this will include the base address registers
410 * or configuration space.
412 void qbus_reset_all(BusState
*bus
);
413 void qbus_reset_all_fn(void *opaque
);
415 /* This should go away once we get rid of the NULL bus hack */
416 BusState
*sysbus_get_default(void);
418 char *qdev_get_fw_dev_path(DeviceState
*dev
);
419 char *qdev_get_own_fw_dev_path_from_handler(BusState
*bus
, DeviceState
*dev
);
424 * Initialize platform devices before machine init. This is a hack until full
425 * support for composition is added.
427 void qdev_machine_init(void);
432 * Reset a single device (by calling the reset method).
434 void device_reset(DeviceState
*dev
);
436 void device_class_set_parent_reset(DeviceClass
*dc
,
437 DeviceReset dev_reset
,
438 DeviceReset
*parent_reset
);
439 void device_class_set_parent_realize(DeviceClass
*dc
,
440 DeviceRealize dev_realize
,
441 DeviceRealize
*parent_realize
);
442 void device_class_set_parent_unrealize(DeviceClass
*dc
,
443 DeviceUnrealize dev_unrealize
,
444 DeviceUnrealize
*parent_unrealize
);
446 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
);
448 const char *qdev_fw_name(DeviceState
*dev
);
450 Object
*qdev_get_machine(void);
452 /* FIXME: make this a link<> */
453 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
);
455 extern bool qdev_hotplug
;
456 extern bool qdev_hot_removed
;
458 char *qdev_get_dev_path(DeviceState
*dev
);
460 GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
);
462 void qbus_set_hotplug_handler(BusState
*bus
, Object
*handler
, Error
**errp
);
464 void qbus_set_bus_hotplug_handler(BusState
*bus
, Error
**errp
);
466 static inline bool qbus_is_hotpluggable(BusState
*bus
)
468 return bus
->hotplug_handler
;
471 void device_listener_register(DeviceListener
*listener
);
472 void device_listener_unregister(DeviceListener
*listener
);
475 * @qdev_should_hide_device:
476 * @opts: QemuOpts as passed on cmdline.
478 * Check if a device should be added.
479 * When a device is added via qdev_device_add() this will be called,
480 * and return if the device should be added now or not.
482 bool qdev_should_hide_device(QemuOpts
*opts
);