4 #include "qemu/atomic.h"
5 #include "qemu/queue.h"
6 #include "qemu/bitmap.h"
8 #include "qemu/rcu_queue.h"
9 #include "qom/object.h"
10 #include "hw/hotplug.h"
11 #include "hw/resettable.h"
14 * DOC: The QEMU Device API
16 * All modern devices should represented as a derived QOM class of
17 * TYPE_DEVICE. The device API introduces the additional methods of
18 * @realize and @unrealize to represent additional stages in a device
24 * Devices are constructed in two stages:
26 * 1) object instantiation via object_initialize() and
27 * 2) device realization via the #DeviceState.realized property
29 * The former may not fail (and must not abort or exit, since it is called
30 * during device introspection already), and the latter may return error
31 * information to the caller and must be re-entrant.
32 * Trivial field initializations should go into #TypeInfo.instance_init.
33 * Operations depending on @props static properties should go into @realize.
34 * After successful realization, setting static properties will fail.
36 * As an interim step, the #DeviceState.realized property can also be
37 * set with qdev_realize(). In the future, devices will propagate this
38 * state change to their children and along busses they expose. The
39 * point in time will be deferred to machine creation, so that values
40 * set in @realize will not be introspectable beforehand. Therefore
41 * devices must not create children during @realize; they should
42 * initialize them via object_initialize() in their own
43 * #TypeInfo.instance_init and forward the realization events
46 * Any type may override the @realize and/or @unrealize callbacks but needs
47 * to call the parent type's implementation if keeping their functionality
48 * is desired. Refer to QOM documentation for further discussion and examples.
51 * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
52 * derived directly from it need not call their parent's @realize and
53 * @unrealize. For other types consult the documentation and
54 * implementation of the respective parent types.
59 * To hide a device, a DeviceListener function hide_device() needs to
60 * be registered. It can be used to defer adding a device and
61 * therefore hide it from the guest. The handler registering to this
62 * DeviceListener can save the QOpts passed to it for re-using it
63 * later. It must return if it wants the device to be hidden or
64 * visible. When the handler function decides the device shall be
65 * visible it will be added with qdev_device_add() and realized as any
66 * other device. Otherwise qdev_device_add() will return early without
67 * adding the device. The guest will not see a "hidden" device until
68 * it was marked visible and qdev_device_add called again.
73 DEV_NVECTORS_UNSPECIFIED
= -1,
76 #define TYPE_DEVICE "device"
77 OBJECT_DECLARE_TYPE(DeviceState
, DeviceClass
, DEVICE
)
79 typedef enum DeviceCategory
{
80 DEVICE_CATEGORY_BRIDGE
,
82 DEVICE_CATEGORY_STORAGE
,
83 DEVICE_CATEGORY_NETWORK
,
84 DEVICE_CATEGORY_INPUT
,
85 DEVICE_CATEGORY_DISPLAY
,
86 DEVICE_CATEGORY_SOUND
,
89 DEVICE_CATEGORY_WATCHDOG
,
93 typedef void (*DeviceRealize
)(DeviceState
*dev
, Error
**errp
);
94 typedef void (*DeviceUnrealize
)(DeviceState
*dev
);
95 typedef void (*DeviceReset
)(DeviceState
*dev
);
96 typedef void (*BusRealize
)(BusState
*bus
, Error
**errp
);
97 typedef void (*BusUnrealize
)(BusState
*bus
);
100 * struct DeviceClass - The base class for all devices.
101 * @props: Properties accessing state fields.
102 * @realize: Callback function invoked when the #DeviceState:realized
103 * property is changed to %true.
104 * @unrealize: Callback function invoked when the #DeviceState:realized
105 * property is changed to %false.
106 * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
107 * as readonly "hotpluggable" property of #DeviceState instance
112 ObjectClass parent_class
;
117 * @categories: device categories device belongs to
119 DECLARE_BITMAP(categories
, DEVICE_CATEGORY_MAX
);
121 * @fw_name: name used to identify device to firmware interfaces
125 * @desc: human readable description of device
130 * @props_: properties associated with device, should only be
131 * assigned by using device_class_set_props(). The underscore
132 * ensures a compile-time error if someone attempts to assign
133 * dc->props directly.
138 * @user_creatable: Can user instantiate with -device / device_add?
140 * All devices should support instantiation with device_add, and
141 * this flag should not exist. But we're not there, yet. Some
142 * devices fail to instantiate with cryptic error messages.
143 * Others instantiate, but don't work. Exposing users to such
144 * behavior would be cruel; clearing this flag will protect them.
145 * It should never be cleared without a comment explaining why it
148 * TODO remove once we're there
155 * @reset: deprecated device reset method pointer
157 * Modern code should use the ResettableClass interface to
158 * implement a multi-phase reset.
160 * TODO: remove once every reset callback is unused
163 DeviceRealize realize
;
164 DeviceUnrealize unrealize
;
167 * @vmsd: device state serialisation description for
168 * migration/save/restore
170 const VMStateDescription
*vmsd
;
173 * @bus_type: bus type
174 * private: to qdev / bus.
176 const char *bus_type
;
179 typedef struct NamedGPIOList NamedGPIOList
;
181 struct NamedGPIOList
{
186 QLIST_ENTRY(NamedGPIOList
) node
;
189 typedef struct Clock Clock
;
190 typedef struct NamedClockList NamedClockList
;
192 struct NamedClockList
{
197 QLIST_ENTRY(NamedClockList
) node
;
202 } MemReentrancyGuard
;
205 typedef QLIST_HEAD(, NamedGPIOList
) NamedGPIOListHead
;
206 typedef QLIST_HEAD(, NamedClockList
) NamedClockListHead
;
207 typedef QLIST_HEAD(, BusState
) BusStateHead
;
210 * struct DeviceState - common device state, accessed with qdev helpers
212 * This structure should not be accessed directly. We declare it here
213 * so that it can be embedded in individual device state structures.
221 * @id: global device id
225 * @canonical_path: canonical path of realized device in the QOM tree
227 char *canonical_path
;
229 * @realized: has device been realized?
233 * @pending_deleted_event: track pending deletion events during unplug
235 bool pending_deleted_event
;
237 * @pending_deleted_expires_ms: optional timeout for deletion events
239 int64_t pending_deleted_expires_ms
;
241 * @opts: QDict of options for the device
245 * @hotplugged: was device added after PHASE_MACHINE_READY?
249 * @allow_unplug_during_migration: can device be unplugged during migration
251 bool allow_unplug_during_migration
;
253 * @parent_bus: bus this device belongs to
255 BusState
*parent_bus
;
257 * @gpios: QLIST of named GPIOs the device provides.
259 NamedGPIOListHead gpios
;
261 * @clocks: QLIST of named clocks the device provides.
263 NamedClockListHead clocks
;
265 * @child_bus: QLIST of child buses
267 BusStateHead child_bus
;
269 * @num_child_bus: number of @child_bus entries
273 * @instance_id_alias: device alias for handling legacy migration setups
275 int instance_id_alias
;
277 * @alias_required_for_version: indicates @instance_id_alias is
278 * needed for migration
280 int alias_required_for_version
;
282 * @reset: ResettableState for the device; handled by Resettable interface.
284 ResettableState reset
;
286 * @unplug_blockers: list of reasons to block unplugging of device
288 GSList
*unplug_blockers
;
290 * @mem_reentrancy_guard: Is the device currently in mmio/pio/dma?
292 * Used to prevent re-entrancy confusing things.
294 MemReentrancyGuard mem_reentrancy_guard
;
297 struct DeviceListener
{
298 void (*realize
)(DeviceListener
*listener
, DeviceState
*dev
);
299 void (*unrealize
)(DeviceListener
*listener
, DeviceState
*dev
);
301 * This callback is called upon init of the DeviceState and
302 * informs qdev if a device should be visible or hidden. We can
303 * hide a failover device depending for example on the device
306 * On errors, it returns false and errp is set. Device creation
307 * should fail in this case.
309 bool (*hide_device
)(DeviceListener
*listener
, const QDict
*device_opts
,
310 bool from_json
, Error
**errp
);
311 QTAILQ_ENTRY(DeviceListener
) link
;
314 #define TYPE_BUS "bus"
315 DECLARE_OBJ_CHECKERS(BusState
, BusClass
,
319 ObjectClass parent_class
;
321 /* FIXME first arg should be BusState */
322 void (*print_dev
)(Monitor
*mon
, DeviceState
*dev
, int indent
);
323 char *(*get_dev_path
)(DeviceState
*dev
);
326 * This callback is used to create Open Firmware device path in accordance
327 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
328 * bindings can be found at http://playground.sun.com/1275/bindings/.
330 char *(*get_fw_dev_path
)(DeviceState
*dev
);
332 void (*reset
)(BusState
*bus
);
335 * Return whether the device can be added to @bus,
336 * based on the address that was set (via device properties)
337 * before realize. If not, on return @errp contains the
338 * human-readable error message.
340 bool (*check_address
)(BusState
*bus
, DeviceState
*dev
, Error
**errp
);
343 BusUnrealize unrealize
;
345 /* maximum devices allowed on the bus, 0: no limit. */
347 /* number of automatically allocated bus ids (e.g. ide.0) */
351 typedef struct BusChild
{
355 QTAILQ_ENTRY(BusChild
) sibling
;
358 #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
360 typedef QTAILQ_HEAD(, BusChild
) BusChildHead
;
361 typedef QLIST_ENTRY(BusState
) BusStateEntry
;
365 * @obj: parent object
366 * @parent: parent Device
368 * @hotplug_handler: link to a hotplug handler associated with bus.
369 * @max_index: max number of child buses
370 * @realized: is the bus itself realized?
371 * @full: is the bus full?
372 * @num_children: current number of child buses
380 HotplugHandler
*hotplug_handler
;
387 * @children: an RCU protected QTAILQ, thus readers must use RCU
388 * to access it, and writers must hold the big qemu lock
390 BusChildHead children
;
394 BusStateEntry sibling
;
396 * @reset: ResettableState for the bus; handled by Resettable interface.
398 ResettableState reset
;
402 * typedef GlobalProperty - a global property type
404 * @used: Set to true if property was used when initializing a device.
405 * @optional: If set to true, GlobalProperty will be skipped without errors
406 * if the property doesn't exist.
408 * An error is fatal for non-hotplugged devices, when the global is applied.
410 typedef struct GlobalProperty
{
412 const char *property
;
419 compat_props_add(GPtrArray
*arr
,
420 GlobalProperty props
[], size_t nelem
)
423 for (i
= 0; i
< nelem
; i
++) {
424 g_ptr_array_add(arr
, (void *)&props
[i
]);
428 /*** Board API. This should go away once we have a machine config file. ***/
431 * qdev_new: Create a device on the heap
432 * @name: device type to create (we assert() that this type exists)
434 * This only allocates the memory and initializes the device state
435 * structure, ready for the caller to set properties if they wish.
436 * The device still needs to be realized.
438 * Return: a derived DeviceState object with a reference count of 1.
440 DeviceState
*qdev_new(const char *name
);
443 * qdev_try_new: Try to create a device on the heap
444 * @name: device type to create
446 * This is like qdev_new(), except it returns %NULL when type @name
447 * does not exist, rather than asserting.
449 * Return: a derived DeviceState object with a reference count of 1 or
450 * NULL if type @name does not exist.
452 DeviceState
*qdev_try_new(const char *name
);
455 * qdev_is_realized() - check if device is realized
456 * @dev: The device to check.
458 * Context: May be called outside big qemu lock.
459 * Return: true if the device has been fully constructed, false otherwise.
461 static inline bool qdev_is_realized(DeviceState
*dev
)
463 return qatomic_load_acquire(&dev
->realized
);
467 * qdev_realize: Realize @dev.
468 * @dev: device to realize
469 * @bus: bus to plug it into (may be NULL)
470 * @errp: pointer to error object
472 * "Realize" the device, i.e. perform the second phase of device
474 * @dev must not be plugged into a bus already.
475 * If @bus, plug @dev into @bus. This takes a reference to @dev.
476 * If @dev has no QOM parent, make one up, taking another reference.
478 * If you created @dev using qdev_new(), you probably want to use
479 * qdev_realize_and_unref() instead.
481 * Return: true on success, else false setting @errp with error
483 bool qdev_realize(DeviceState
*dev
, BusState
*bus
, Error
**errp
);
486 * qdev_realize_and_unref: Realize @dev and drop a reference
487 * @dev: device to realize
488 * @bus: bus to plug it into (may be NULL)
489 * @errp: pointer to error object
491 * Realize @dev and drop a reference.
492 * This is like qdev_realize(), except the caller must hold a
493 * (private) reference, which is dropped on return regardless of
494 * success or failure. Intended use::
498 * qdev_realize_and_unref(dev, bus, errp);
500 * Now @dev can go away without further ado.
502 * If you are embedding the device into some other QOM device and
503 * initialized it via some variant on object_initialize_child() then
504 * do not use this function, because that family of functions arrange
505 * for the only reference to the child device to be held by the parent
506 * via the child<> property, and so the reference-count-drop done here
507 * would be incorrect. For that use case you want qdev_realize().
509 * Return: true on success, else false setting @errp with error
511 bool qdev_realize_and_unref(DeviceState
*dev
, BusState
*bus
, Error
**errp
);
514 * qdev_unrealize: Unrealize a device
515 * @dev: device to unrealize
517 * This function will "unrealize" a device, which is the first phase
518 * of correctly destroying a device that has been realized. It will:
520 * - unrealize any child buses by calling qbus_unrealize()
521 * (this will recursively unrealize any devices on those buses)
522 * - call the unrealize method of @dev
524 * The device can then be freed by causing its reference count to go
527 * Warning: most devices in QEMU do not expect to be unrealized. Only
528 * devices which are hot-unpluggable should be unrealized (as part of
529 * the unplugging process); all other devices are expected to last for
530 * the life of the simulation and should not be unrealized and freed.
532 void qdev_unrealize(DeviceState
*dev
);
533 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
534 int required_for_version
);
535 HotplugHandler
*qdev_get_bus_hotplug_handler(DeviceState
*dev
);
536 HotplugHandler
*qdev_get_machine_hotplug_handler(DeviceState
*dev
);
537 bool qdev_hotplug_allowed(DeviceState
*dev
, Error
**errp
);
540 * qdev_get_hotplug_handler() - Get handler responsible for device wiring
541 * @dev: the device we want the HOTPLUG_HANDLER for.
543 * Note: in case @dev has a parent bus, it will be returned as handler unless
544 * machine handler overrides it.
546 * Return: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
547 * or NULL if there aren't any.
549 HotplugHandler
*qdev_get_hotplug_handler(DeviceState
*dev
);
550 void qdev_unplug(DeviceState
*dev
, Error
**errp
);
551 void qdev_simple_device_unplug_cb(HotplugHandler
*hotplug_dev
,
552 DeviceState
*dev
, Error
**errp
);
553 void qdev_machine_creation_done(void);
554 bool qdev_machine_modified(void);
557 * qdev_add_unplug_blocker: Add an unplug blocker to a device
559 * @dev: Device to be blocked from unplug
560 * @reason: Reason for blocking
562 void qdev_add_unplug_blocker(DeviceState
*dev
, Error
*reason
);
565 * qdev_del_unplug_blocker: Remove an unplug blocker from a device
567 * @dev: Device to be unblocked
568 * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
569 * Used as a handle to lookup the blocker for deletion.
571 void qdev_del_unplug_blocker(DeviceState
*dev
, Error
*reason
);
574 * qdev_unplug_blocked: Confirm if a device is blocked from unplug
576 * @dev: Device to be tested
577 * @errp: The reasons why the device is blocked, if any
579 * Returns: true (also setting @errp) if device is blocked from unplug,
582 bool qdev_unplug_blocked(DeviceState
*dev
, Error
**errp
);
585 * typedef GpioPolarity - Polarity of a GPIO line
587 * GPIO lines use either positive (active-high) logic,
588 * or negative (active-low) logic.
590 * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
591 * active when the voltage on the pin is high (relative to ground);
592 * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
593 * is active when the voltage on the pin is low (or grounded).
596 GPIO_POLARITY_ACTIVE_LOW
,
597 GPIO_POLARITY_ACTIVE_HIGH
601 * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
602 * @dev: Device whose GPIO we want
603 * @n: Number of the anonymous GPIO line (which must be in range)
605 * Returns the qemu_irq corresponding to an anonymous input GPIO line
606 * (which the device has set up with qdev_init_gpio_in()). The index
607 * @n of the GPIO line must be valid (i.e. be at least 0 and less than
608 * the total number of anonymous input GPIOs the device has); this
609 * function will assert() if passed an invalid index.
611 * This function is intended to be used by board code or SoC "container"
612 * device models to wire up the GPIO lines; usually the return value
613 * will be passed to qdev_connect_gpio_out() or a similar function to
614 * connect another device's output GPIO line to this input.
616 * For named input GPIO lines, use qdev_get_gpio_in_named().
618 * Return: qemu_irq corresponding to anonymous input GPIO line
620 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
);
623 * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
624 * @dev: Device whose GPIO we want
625 * @name: Name of the input GPIO array
626 * @n: Number of the GPIO line in that array (which must be in range)
628 * Returns the qemu_irq corresponding to a named input GPIO line
629 * (which the device has set up with qdev_init_gpio_in_named()).
630 * The @name string must correspond to an input GPIO array which exists on
631 * the device, and the index @n of the GPIO line must be valid (i.e.
632 * be at least 0 and less than the total number of input GPIOs in that
633 * array); this function will assert() if passed an invalid name or index.
635 * For anonymous input GPIO lines, use qdev_get_gpio_in().
637 * Return: qemu_irq corresponding to named input GPIO line
639 qemu_irq
qdev_get_gpio_in_named(DeviceState
*dev
, const char *name
, int n
);
642 * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
643 * @dev: Device whose GPIO to connect
644 * @n: Number of the anonymous output GPIO line (which must be in range)
645 * @pin: qemu_irq to connect the output line to
647 * This function connects an anonymous output GPIO line on a device
648 * up to an arbitrary qemu_irq, so that when the device asserts that
649 * output GPIO line, the qemu_irq's callback is invoked.
650 * The index @n of the GPIO line must be valid (i.e. be at least 0 and
651 * less than the total number of anonymous output GPIOs the device has
652 * created with qdev_init_gpio_out()); otherwise this function will assert().
654 * Outbound GPIO lines can be connected to any qemu_irq, but the common
655 * case is connecting them to another device's inbound GPIO line, using
656 * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
658 * It is not valid to try to connect one outbound GPIO to multiple
659 * qemu_irqs at once, or to connect multiple outbound GPIOs to the
660 * same qemu_irq. (Warning: there is no assertion or other guard to
661 * catch this error: the model will just not do the right thing.)
662 * Instead, for fan-out you can use the TYPE_SPLIT_IRQ device: connect
663 * a device's outbound GPIO to the splitter's input, and connect each
664 * of the splitter's outputs to a different device. For fan-in you
665 * can use the TYPE_OR_IRQ device, which is a model of a logical OR
666 * gate with multiple inputs and one output.
668 * For named output GPIO lines, use qdev_connect_gpio_out_named().
670 void qdev_connect_gpio_out(DeviceState
*dev
, int n
, qemu_irq pin
);
673 * qdev_connect_gpio_out_named: Connect one of a device's named output
675 * @dev: Device whose GPIO to connect
676 * @name: Name of the output GPIO array
677 * @n: Number of the anonymous output GPIO line (which must be in range)
678 * @input_pin: qemu_irq to connect the output line to
680 * This function connects an anonymous output GPIO line on a device
681 * up to an arbitrary qemu_irq, so that when the device asserts that
682 * output GPIO line, the qemu_irq's callback is invoked.
683 * The @name string must correspond to an output GPIO array which exists on
684 * the device, and the index @n of the GPIO line must be valid (i.e.
685 * be at least 0 and less than the total number of input GPIOs in that
686 * array); this function will assert() if passed an invalid name or index.
688 * Outbound GPIO lines can be connected to any qemu_irq, but the common
689 * case is connecting them to another device's inbound GPIO line, using
690 * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
692 * It is not valid to try to connect one outbound GPIO to multiple
693 * qemu_irqs at once, or to connect multiple outbound GPIOs to the
694 * same qemu_irq; see qdev_connect_gpio_out() for details.
696 * For anonymous output GPIO lines, use qdev_connect_gpio_out().
698 void qdev_connect_gpio_out_named(DeviceState
*dev
, const char *name
, int n
,
702 * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
703 * @dev: Device whose output GPIO we are interested in
704 * @name: Name of the output GPIO array
705 * @n: Number of the output GPIO line within that array
707 * Returns whatever qemu_irq is currently connected to the specified
708 * output GPIO line of @dev. This will be NULL if the output GPIO line
709 * has never been wired up to the anything. Note that the qemu_irq
710 * returned does not belong to @dev -- it will be the input GPIO or
711 * IRQ of whichever device the board code has connected up to @dev's
714 * You probably don't need to use this function -- it is used only
715 * by the platform-bus subsystem.
717 * Return: qemu_irq associated with GPIO or NULL if un-wired.
719 qemu_irq
qdev_get_gpio_out_connector(DeviceState
*dev
, const char *name
, int n
);
722 * qdev_intercept_gpio_out: Intercept an existing GPIO connection
723 * @dev: Device to intercept the outbound GPIO line from
724 * @icpt: New qemu_irq to connect instead
725 * @name: Name of the output GPIO array
726 * @n: Number of the GPIO line in the array
729 * This function is provided only for use by the qtest testing framework
730 * and is not suitable for use in non-testing parts of QEMU.
732 * This function breaks an existing connection of an outbound GPIO
733 * line from @dev, and replaces it with the new qemu_irq @icpt, as if
734 * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
735 * The previously connected qemu_irq is returned, so it can be restored
736 * by a second call to qdev_intercept_gpio_out() if desired.
738 * Return: old disconnected qemu_irq if one existed
740 qemu_irq
qdev_intercept_gpio_out(DeviceState
*dev
, qemu_irq icpt
,
741 const char *name
, int n
);
743 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
);
745 /*** Device API. ***/
748 * qdev_init_gpio_in: create an array of anonymous input GPIO lines
749 * @dev: Device to create input GPIOs for
750 * @handler: Function to call when GPIO line value is set
751 * @n: Number of GPIO lines to create
753 * Devices should use functions in the qdev_init_gpio_in* family in
754 * their instance_init or realize methods to create any input GPIO
755 * lines they need. There is no functional difference between
756 * anonymous and named GPIO lines. Stylistically, named GPIOs are
757 * preferable (easier to understand at callsites) unless a device
758 * has exactly one uniform kind of GPIO input whose purpose is obvious.
759 * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
761 * See qdev_get_gpio_in() for how code that uses such a device can get
762 * hold of an input GPIO line to manipulate it.
764 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
);
767 * qdev_init_gpio_out: create an array of anonymous output GPIO lines
768 * @dev: Device to create output GPIOs for
769 * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
770 * @n: Number of GPIO lines to create
772 * Devices should use functions in the qdev_init_gpio_out* family
773 * in their instance_init or realize methods to create any output
774 * GPIO lines they need. There is no functional difference between
775 * anonymous and named GPIO lines. Stylistically, named GPIOs are
776 * preferable (easier to understand at callsites) unless a device
777 * has exactly one uniform kind of GPIO output whose purpose is obvious.
779 * The @pins argument should be a pointer to either a "qemu_irq"
780 * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
781 * state structure. The device implementation can then raise and
782 * lower the GPIO line by calling qemu_set_irq(). (If anything is
783 * connected to the other end of the GPIO this will cause the handler
784 * function for that input GPIO to be called.)
786 * See qdev_connect_gpio_out() for how code that uses such a device
787 * can connect to one of its output GPIO lines.
789 * There is no need to release the @pins allocated array because it
790 * will be automatically released when @dev calls its instance_finalize()
793 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
);
796 * qdev_init_gpio_out_named: create an array of named output GPIO lines
797 * @dev: Device to create output GPIOs for
798 * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
799 * @name: Name to give this array of GPIO lines
800 * @n: Number of GPIO lines to create
802 * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
803 * with a name. Code using the device can then connect these GPIO lines
804 * using qdev_connect_gpio_out_named().
806 void qdev_init_gpio_out_named(DeviceState
*dev
, qemu_irq
*pins
,
807 const char *name
, int n
);
810 * qdev_init_gpio_in_named_with_opaque() - create an array of input GPIO lines
811 * @dev: Device to create input GPIOs for
812 * @handler: Function to call when GPIO line value is set
813 * @opaque: Opaque data pointer to pass to @handler
814 * @name: Name of the GPIO input (must be unique for this device)
815 * @n: Number of GPIO lines in this input set
817 void qdev_init_gpio_in_named_with_opaque(DeviceState
*dev
,
818 qemu_irq_handler handler
,
820 const char *name
, int n
);
823 * qdev_init_gpio_in_named() - create an array of input GPIO lines
824 * @dev: device to add array to
825 * @handler: a &typedef qemu_irq_handler function to call when GPIO is set
826 * @name: Name of the GPIO input (must be unique for this device)
827 * @n: Number of GPIO lines in this input set
829 * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
830 * passed to the handler is @dev (which is the most commonly desired behaviour).
832 static inline void qdev_init_gpio_in_named(DeviceState
*dev
,
833 qemu_irq_handler handler
,
834 const char *name
, int n
)
836 qdev_init_gpio_in_named_with_opaque(dev
, handler
, dev
, name
, n
);
840 * qdev_pass_gpios: create GPIO lines on container which pass through to device
841 * @dev: Device which has GPIO lines
842 * @container: Container device which needs to expose them
843 * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
845 * In QEMU, complicated devices like SoCs are often modelled with a
846 * "container" QOM device which itself contains other QOM devices and
847 * which wires them up appropriately. This function allows the container
848 * to create GPIO arrays on itself which simply pass through to a GPIO
849 * array of one of its internal devices.
851 * If @dev has both input and output GPIOs named @name then both will
852 * be passed through. It is not possible to pass a subset of the array
853 * with this function.
855 * To users of the container device, the GPIO array created on @container
856 * behaves exactly like any other.
858 void qdev_pass_gpios(DeviceState
*dev
, DeviceState
*container
,
861 BusState
*qdev_get_parent_bus(const DeviceState
*dev
);
865 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
);
867 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
868 typedef int (qbus_walkerfn
)(BusState
*bus
, void *opaque
);
869 typedef int (qdev_walkerfn
)(DeviceState
*dev
, void *opaque
);
871 void qbus_init(void *bus
, size_t size
, const char *typename
,
872 DeviceState
*parent
, const char *name
);
873 BusState
*qbus_new(const char *typename
, DeviceState
*parent
, const char *name
);
874 bool qbus_realize(BusState
*bus
, Error
**errp
);
875 void qbus_unrealize(BusState
*bus
);
877 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
878 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
880 int qbus_walk_children(BusState
*bus
,
881 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
882 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
884 int qdev_walk_children(DeviceState
*dev
,
885 qdev_walkerfn
*pre_devfn
, qbus_walkerfn
*pre_busfn
,
886 qdev_walkerfn
*post_devfn
, qbus_walkerfn
*post_busfn
,
890 * device_cold_reset() - perform a recursive cold reset on a device
891 * @dev: device to reset.
893 * Reset device @dev and perform a recursive processing using the resettable
894 * interface. It triggers a RESET_TYPE_COLD.
896 void device_cold_reset(DeviceState
*dev
);
899 * bus_cold_reset() - perform a recursive cold reset on a bus
902 * Reset bus @bus and perform a recursive processing using the resettable
903 * interface. It triggers a RESET_TYPE_COLD.
905 void bus_cold_reset(BusState
*bus
);
908 * device_is_in_reset() - check device reset state
909 * @dev: device to check
911 * Return: true if the device @dev is currently being reset.
913 bool device_is_in_reset(DeviceState
*dev
);
916 * bus_is_in_reset() - check bus reset state
919 * Return: true if the bus @bus is currently being reset.
921 bool bus_is_in_reset(BusState
*bus
);
923 /* This should go away once we get rid of the NULL bus hack */
924 BusState
*sysbus_get_default(void);
926 char *qdev_get_fw_dev_path(DeviceState
*dev
);
927 char *qdev_get_own_fw_dev_path_from_handler(BusState
*bus
, DeviceState
*dev
);
930 * device_class_set_props(): add a set of properties to an device
931 * @dc: the parent DeviceClass all devices inherit
932 * @props: an array of properties, terminate by DEFINE_PROP_END_OF_LIST()
934 * This will add a set of properties to the object. It will fault if
935 * you attempt to add an existing property defined by a parent class.
936 * To modify an inherited property you need to use????
938 void device_class_set_props(DeviceClass
*dc
, Property
*props
);
941 * device_class_set_parent_reset() - legacy set device reset handlers
943 * @dev_reset: function pointer to reset handler
944 * @parent_reset: function pointer to parents reset handler
946 * Modern code should use the ResettableClass interface to
947 * implement a multi-phase reset instead.
949 * TODO: remove the function when DeviceClass's reset method
950 * is not used anymore.
952 void device_class_set_parent_reset(DeviceClass
*dc
,
953 DeviceReset dev_reset
,
954 DeviceReset
*parent_reset
);
955 void device_class_set_parent_realize(DeviceClass
*dc
,
956 DeviceRealize dev_realize
,
957 DeviceRealize
*parent_realize
);
958 void device_class_set_parent_unrealize(DeviceClass
*dc
,
959 DeviceUnrealize dev_unrealize
,
960 DeviceUnrealize
*parent_unrealize
);
962 const VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
);
964 const char *qdev_fw_name(DeviceState
*dev
);
966 void qdev_assert_realized_properly(void);
967 Object
*qdev_get_machine(void);
969 /* FIXME: make this a link<> */
970 bool qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
, Error
**errp
);
972 extern bool qdev_hot_removed
;
974 char *qdev_get_dev_path(DeviceState
*dev
);
976 void qbus_set_hotplug_handler(BusState
*bus
, Object
*handler
);
977 void qbus_set_bus_hotplug_handler(BusState
*bus
);
979 static inline bool qbus_is_hotpluggable(BusState
*bus
)
981 HotplugHandler
*plug_handler
= bus
->hotplug_handler
;
982 bool ret
= !!plug_handler
;
985 HotplugHandlerClass
*hdc
;
987 hdc
= HOTPLUG_HANDLER_GET_CLASS(plug_handler
);
988 if (hdc
->is_hotpluggable_bus
) {
989 ret
= hdc
->is_hotpluggable_bus(plug_handler
, bus
);
996 * qbus_mark_full: Mark this bus as full, so no more devices can be attached
997 * @bus: Bus to mark as full
999 * By default, QEMU will allow devices to be plugged into a bus up
1000 * to the bus class's device count limit. Calling this function
1001 * marks a particular bus as full, so that no more devices can be
1002 * plugged into it. In particular this means that the bus will not
1003 * be considered as a candidate for plugging in devices created by
1004 * the user on the commandline or via the monitor.
1005 * If a machine has multiple buses of a given type, such as I2C,
1006 * where some of those buses in the real hardware are used only for
1007 * internal devices and some are exposed via expansion ports, you
1008 * can use this function to mark the internal-only buses as full
1009 * after you have created all their internal devices. Then user
1010 * created devices will appear on the expansion-port bus where
1011 * guest software expects them.
1013 static inline void qbus_mark_full(BusState
*bus
)
1018 void device_listener_register(DeviceListener
*listener
);
1019 void device_listener_unregister(DeviceListener
*listener
);
1022 * qdev_should_hide_device() - check if device should be hidden
1024 * @opts: options QDict
1025 * @from_json: true if @opts entries are typed, false for all strings
1026 * @errp: pointer to error object
1028 * When a device is added via qdev_device_add() this will be called.
1030 * Return: if the device should be added now or not.
1032 bool qdev_should_hide_device(const QDict
*opts
, bool from_json
, Error
**errp
);
1034 typedef enum MachineInitPhase
{
1035 /* current_machine is NULL. */
1038 /* current_machine is not NULL, but current_machine->accel is NULL. */
1039 PHASE_MACHINE_CREATED
,
1042 * current_machine->accel is not NULL, but the machine properties have
1043 * not been validated and machine_class->init has not yet been called.
1045 PHASE_ACCEL_CREATED
,
1048 * machine_class->init has been called, thus creating any embedded
1049 * devices and validating machine properties. Devices created at
1050 * this time are considered to be cold-plugged.
1052 PHASE_MACHINE_INITIALIZED
,
1055 * QEMU is ready to start CPUs and devices created at this time
1056 * are considered to be hot-plugged. The monitor is not restricted
1057 * to "preconfig" commands.
1059 PHASE_MACHINE_READY
,
1062 extern bool phase_check(MachineInitPhase phase
);
1063 extern void phase_advance(MachineInitPhase phase
);