xref: /qemu/include/hw/qdev-core.h (revision d36f165d9523a5d151f11888938fcc66a6dd10df)
1074a86fcSAnthony Liguori #ifndef QDEV_CORE_H
2074a86fcSAnthony Liguori #define QDEV_CORE_H
3074a86fcSAnthony Liguori 
426462a70SStefan Hajnoczi #include "qemu/atomic.h"
51de7afc9SPaolo Bonzini #include "qemu/queue.h"
6949fc823SMarcel Apfelbaum #include "qemu/bitmap.h"
72d24a646SMaxim Levitsky #include "qemu/rcu.h"
82d24a646SMaxim Levitsky #include "qemu/rcu_queue.h"
914cccb61SPaolo Bonzini #include "qom/object.h"
100ee4de6cSIgor Mammedov #include "hw/hotplug.h"
11c11256aaSDamien Hedde #include "hw/resettable.h"
12074a86fcSAnthony Liguori 
136aebb1f6SAlex Bennée /**
146aebb1f6SAlex Bennée  * DOC: The QEMU Device API
156aebb1f6SAlex Bennée  *
166aebb1f6SAlex Bennée  * All modern devices should represented as a derived QOM class of
176aebb1f6SAlex Bennée  * TYPE_DEVICE. The device API introduces the additional methods of
186aebb1f6SAlex Bennée  * @realize and @unrealize to represent additional stages in a device
196aebb1f6SAlex Bennée  * objects life cycle.
206aebb1f6SAlex Bennée  *
216aebb1f6SAlex Bennée  * Realization
226aebb1f6SAlex Bennée  * -----------
236aebb1f6SAlex Bennée  *
246aebb1f6SAlex Bennée  * Devices are constructed in two stages:
256aebb1f6SAlex Bennée  *
266aebb1f6SAlex Bennée  * 1) object instantiation via object_initialize() and
276aebb1f6SAlex Bennée  * 2) device realization via the #DeviceState.realized property
286aebb1f6SAlex Bennée  *
296aebb1f6SAlex Bennée  * The former may not fail (and must not abort or exit, since it is called
306aebb1f6SAlex Bennée  * during device introspection already), and the latter may return error
316aebb1f6SAlex Bennée  * information to the caller and must be re-entrant.
326aebb1f6SAlex Bennée  * Trivial field initializations should go into #TypeInfo.instance_init.
336aebb1f6SAlex Bennée  * Operations depending on @props static properties should go into @realize.
346aebb1f6SAlex Bennée  * After successful realization, setting static properties will fail.
356aebb1f6SAlex Bennée  *
366aebb1f6SAlex Bennée  * As an interim step, the #DeviceState.realized property can also be
376aebb1f6SAlex Bennée  * set with qdev_realize(). In the future, devices will propagate this
386aebb1f6SAlex Bennée  * state change to their children and along busses they expose. The
396aebb1f6SAlex Bennée  * point in time will be deferred to machine creation, so that values
406aebb1f6SAlex Bennée  * set in @realize will not be introspectable beforehand. Therefore
416aebb1f6SAlex Bennée  * devices must not create children during @realize; they should
426aebb1f6SAlex Bennée  * initialize them via object_initialize() in their own
436aebb1f6SAlex Bennée  * #TypeInfo.instance_init and forward the realization events
446aebb1f6SAlex Bennée  * appropriately.
456aebb1f6SAlex Bennée  *
466aebb1f6SAlex Bennée  * Any type may override the @realize and/or @unrealize callbacks but needs
476aebb1f6SAlex Bennée  * to call the parent type's implementation if keeping their functionality
486aebb1f6SAlex Bennée  * is desired. Refer to QOM documentation for further discussion and examples.
496aebb1f6SAlex Bennée  *
506aebb1f6SAlex Bennée  * .. note::
516aebb1f6SAlex Bennée  *   Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
526aebb1f6SAlex Bennée  *   derived directly from it need not call their parent's @realize and
536aebb1f6SAlex Bennée  *   @unrealize. For other types consult the documentation and
546aebb1f6SAlex Bennée  *   implementation of the respective parent types.
556aebb1f6SAlex Bennée  *
566aebb1f6SAlex Bennée  * Hiding a device
576aebb1f6SAlex Bennée  * ---------------
586aebb1f6SAlex Bennée  *
596aebb1f6SAlex Bennée  * To hide a device, a DeviceListener function hide_device() needs to
606aebb1f6SAlex Bennée  * be registered. It can be used to defer adding a device and
616aebb1f6SAlex Bennée  * therefore hide it from the guest. The handler registering to this
626aebb1f6SAlex Bennée  * DeviceListener can save the QOpts passed to it for re-using it
636aebb1f6SAlex Bennée  * later. It must return if it wants the device to be hidden or
646aebb1f6SAlex Bennée  * visible. When the handler function decides the device shall be
656aebb1f6SAlex Bennée  * visible it will be added with qdev_device_add() and realized as any
666aebb1f6SAlex Bennée  * other device. Otherwise qdev_device_add() will return early without
676aebb1f6SAlex Bennée  * adding the device. The guest will not see a "hidden" device until
686aebb1f6SAlex Bennée  * it was marked visible and qdev_device_add called again.
696aebb1f6SAlex Bennée  *
706aebb1f6SAlex Bennée  */
716aebb1f6SAlex Bennée 
72074a86fcSAnthony Liguori enum {
73074a86fcSAnthony Liguori     DEV_NVECTORS_UNSPECIFIED = -1,
74074a86fcSAnthony Liguori };
75074a86fcSAnthony Liguori 
76074a86fcSAnthony Liguori #define TYPE_DEVICE "device"
77a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(DeviceState, DeviceClass, DEVICE)
78074a86fcSAnthony Liguori 
793d1237fbSMarcel Apfelbaum typedef enum DeviceCategory {
803d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_BRIDGE,
813d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_USB,
823d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_STORAGE,
833d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_NETWORK,
843d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_INPUT,
853d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_DISPLAY,
863d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_SOUND,
873d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MISC,
88ba31cc72SThomas Huth     DEVICE_CATEGORY_CPU,
89b10cb627SPaolo Bonzini     DEVICE_CATEGORY_WATCHDOG,
903d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MAX
913d1237fbSMarcel Apfelbaum } DeviceCategory;
923d1237fbSMarcel Apfelbaum 
93249d4172SAndreas Färber typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
94b69c3c21SMarkus Armbruster typedef void (*DeviceUnrealize)(DeviceState *dev);
95b850f664SPhilippe Mathieu-Daudé typedef void (*DeviceReset)(DeviceState *dev);
9602e7f85dSBandan Das typedef void (*BusRealize)(BusState *bus, Error **errp);
97b69c3c21SMarkus Armbruster typedef void (*BusUnrealize)(BusState *bus);
98074a86fcSAnthony Liguori 
99249d4172SAndreas Färber /**
1006aebb1f6SAlex Bennée  * struct DeviceClass - The base class for all devices.
101249d4172SAndreas Färber  * @props: Properties accessing state fields.
102249d4172SAndreas Färber  * @realize: Callback function invoked when the #DeviceState:realized
103ff46d9d4SPhilippe Mathieu-Daudé  * property is changed to %true.
104249d4172SAndreas Färber  * @unrealize: Callback function invoked when the #DeviceState:realized
105249d4172SAndreas Färber  * property is changed to %false.
1061a37eca1SIgor Mammedov  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
1071a37eca1SIgor Mammedov  * as readonly "hotpluggable" property of #DeviceState instance
108249d4172SAndreas Färber  *
109249d4172SAndreas Färber  */
110db1015e9SEduardo Habkost struct DeviceClass {
1116aebb1f6SAlex Bennée     /* private: */
112074a86fcSAnthony Liguori     ObjectClass parent_class;
113074a86fcSAnthony Liguori 
1146aebb1f6SAlex Bennée     /* public: */
1156aebb1f6SAlex Bennée 
1166aebb1f6SAlex Bennée     /**
1176aebb1f6SAlex Bennée      * @categories: device categories device belongs to
1186aebb1f6SAlex Bennée      */
1193d1237fbSMarcel Apfelbaum     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
1206aebb1f6SAlex Bennée     /**
1216aebb1f6SAlex Bennée      * @fw_name: name used to identify device to firmware interfaces
1226aebb1f6SAlex Bennée      */
123074a86fcSAnthony Liguori     const char *fw_name;
1246aebb1f6SAlex Bennée     /**
1256aebb1f6SAlex Bennée      * @desc: human readable description of device
1266aebb1f6SAlex Bennée      */
127074a86fcSAnthony Liguori     const char *desc;
128385d8f22SPaolo Bonzini 
1296aebb1f6SAlex Bennée     /**
1306aebb1f6SAlex Bennée      * @props_: properties associated with device, should only be
1316aebb1f6SAlex Bennée      * assigned by using device_class_set_props(). The underscore
1326aebb1f6SAlex Bennée      * ensures a compile-time error if someone attempts to assign
1336aebb1f6SAlex Bennée      * dc->props directly.
134385d8f22SPaolo Bonzini      */
135*d36f165dSPaolo Bonzini     const Property *props_;
136efec3dd6SMarkus Armbruster 
1376aebb1f6SAlex Bennée     /**
1386aebb1f6SAlex Bennée      * @user_creatable: Can user instantiate with -device / device_add?
1396aebb1f6SAlex Bennée      *
140efec3dd6SMarkus Armbruster      * All devices should support instantiation with device_add, and
141efec3dd6SMarkus Armbruster      * this flag should not exist.  But we're not there, yet.  Some
142efec3dd6SMarkus Armbruster      * devices fail to instantiate with cryptic error messages.
143efec3dd6SMarkus Armbruster      * Others instantiate, but don't work.  Exposing users to such
144e90f2a8cSEduardo Habkost      * behavior would be cruel; clearing this flag will protect them.
145e90f2a8cSEduardo Habkost      * It should never be cleared without a comment explaining why it
146e90f2a8cSEduardo Habkost      * is cleared.
1476aebb1f6SAlex Bennée      *
148efec3dd6SMarkus Armbruster      * TODO remove once we're there
149efec3dd6SMarkus Armbruster      */
150e90f2a8cSEduardo Habkost     bool user_creatable;
1511a37eca1SIgor Mammedov     bool hotpluggable;
152074a86fcSAnthony Liguori 
153074a86fcSAnthony Liguori     /* callbacks */
1546aebb1f6SAlex Bennée     /**
1551000872dSPeter Maydell      * @legacy_reset: deprecated device reset method pointer
1566aebb1f6SAlex Bennée      *
1576aebb1f6SAlex Bennée      * Modern code should use the ResettableClass interface to
1586aebb1f6SAlex Bennée      * implement a multi-phase reset.
1596aebb1f6SAlex Bennée      *
160c11256aaSDamien Hedde      * TODO: remove once every reset callback is unused
161c11256aaSDamien Hedde      */
1621000872dSPeter Maydell     DeviceReset legacy_reset;
163249d4172SAndreas Färber     DeviceRealize realize;
164249d4172SAndreas Färber     DeviceUnrealize unrealize;
165074a86fcSAnthony Liguori 
1666aebb1f6SAlex Bennée     /**
1676aebb1f6SAlex Bennée      * @vmsd: device state serialisation description for
1686aebb1f6SAlex Bennée      * migration/save/restore
1696aebb1f6SAlex Bennée      */
1708a9358ccSMarkus Armbruster     const VMStateDescription *vmsd;
171074a86fcSAnthony Liguori 
1726aebb1f6SAlex Bennée     /**
1736aebb1f6SAlex Bennée      * @bus_type: bus type
1746aebb1f6SAlex Bennée      * private: to qdev / bus.
1756aebb1f6SAlex Bennée      */
176074a86fcSAnthony Liguori     const char *bus_type;
177db1015e9SEduardo Habkost };
178074a86fcSAnthony Liguori 
179a5f54290SPeter Crosthwaite typedef struct NamedGPIOList NamedGPIOList;
180a5f54290SPeter Crosthwaite 
181a5f54290SPeter Crosthwaite struct NamedGPIOList {
182a5f54290SPeter Crosthwaite     char *name;
183a5f54290SPeter Crosthwaite     qemu_irq *in;
184a5f54290SPeter Crosthwaite     int num_in;
185a5f54290SPeter Crosthwaite     int num_out;
186a5f54290SPeter Crosthwaite     QLIST_ENTRY(NamedGPIOList) node;
187a5f54290SPeter Crosthwaite };
188a5f54290SPeter Crosthwaite 
1890e6934f2SDamien Hedde typedef struct Clock Clock;
1900e6934f2SDamien Hedde typedef struct NamedClockList NamedClockList;
1910e6934f2SDamien Hedde 
1920e6934f2SDamien Hedde struct NamedClockList {
1930e6934f2SDamien Hedde     char *name;
1940e6934f2SDamien Hedde     Clock *clock;
1950e6934f2SDamien Hedde     bool output;
1960e6934f2SDamien Hedde     bool alias;
1970e6934f2SDamien Hedde     QLIST_ENTRY(NamedClockList) node;
1980e6934f2SDamien Hedde };
1990e6934f2SDamien Hedde 
200a2e1753bSAlexander Bulekov typedef struct {
201a2e1753bSAlexander Bulekov     bool engaged_in_io;
202a2e1753bSAlexander Bulekov } MemReentrancyGuard;
203a2e1753bSAlexander Bulekov 
2046aebb1f6SAlex Bennée 
2056aebb1f6SAlex Bennée typedef QLIST_HEAD(, NamedGPIOList) NamedGPIOListHead;
2066aebb1f6SAlex Bennée typedef QLIST_HEAD(, NamedClockList) NamedClockListHead;
2076aebb1f6SAlex Bennée typedef QLIST_HEAD(, BusState) BusStateHead;
2086aebb1f6SAlex Bennée 
2097983c8a3SAndreas Färber /**
2106aebb1f6SAlex Bennée  * struct DeviceState - common device state, accessed with qdev helpers
2117983c8a3SAndreas Färber  *
2127983c8a3SAndreas Färber  * This structure should not be accessed directly.  We declare it here
2137983c8a3SAndreas Färber  * so that it can be embedded in individual device state structures.
2147983c8a3SAndreas Färber  */
215074a86fcSAnthony Liguori struct DeviceState {
2166aebb1f6SAlex Bennée     /* private: */
217074a86fcSAnthony Liguori     Object parent_obj;
2186aebb1f6SAlex Bennée     /* public: */
219074a86fcSAnthony Liguori 
2206aebb1f6SAlex Bennée     /**
2216aebb1f6SAlex Bennée      * @id: global device id
2226aebb1f6SAlex Bennée      */
223163f3847SKevin Wolf     char *id;
2246aebb1f6SAlex Bennée     /**
2256aebb1f6SAlex Bennée      * @canonical_path: canonical path of realized device in the QOM tree
2266aebb1f6SAlex Bennée      */
22704162f8fSMichael Roth     char *canonical_path;
2286aebb1f6SAlex Bennée     /**
2296aebb1f6SAlex Bennée      * @realized: has device been realized?
2306aebb1f6SAlex Bennée      */
2317983c8a3SAndreas Färber     bool realized;
2326aebb1f6SAlex Bennée     /**
2336aebb1f6SAlex Bennée      * @pending_deleted_event: track pending deletion events during unplug
2346aebb1f6SAlex Bennée      */
235352e8da7SPaolo Bonzini     bool pending_deleted_event;
2366aebb1f6SAlex Bennée     /**
2376aebb1f6SAlex Bennée      * @pending_deleted_expires_ms: optional timeout for deletion events
2386aebb1f6SAlex Bennée      */
23918416c62SGerd Hoffmann     int64_t pending_deleted_expires_ms;
2406aebb1f6SAlex Bennée     /**
2416aebb1f6SAlex Bennée      * @opts: QDict of options for the device
2426aebb1f6SAlex Bennée      */
243f3558b1bSKevin Wolf     QDict *opts;
2446aebb1f6SAlex Bennée     /**
2456aebb1f6SAlex Bennée      * @hotplugged: was device added after PHASE_MACHINE_READY?
2466aebb1f6SAlex Bennée      */
247074a86fcSAnthony Liguori     int hotplugged;
2486aebb1f6SAlex Bennée     /**
2496aebb1f6SAlex Bennée      * @allow_unplug_during_migration: can device be unplugged during migration
2506aebb1f6SAlex Bennée      */
251a1190ab6SJens Freimann     bool allow_unplug_during_migration;
2526aebb1f6SAlex Bennée     /**
2536aebb1f6SAlex Bennée      * @parent_bus: bus this device belongs to
2546aebb1f6SAlex Bennée      */
255074a86fcSAnthony Liguori     BusState *parent_bus;
2566aebb1f6SAlex Bennée     /**
2576aebb1f6SAlex Bennée      * @gpios: QLIST of named GPIOs the device provides.
2586aebb1f6SAlex Bennée      */
2596aebb1f6SAlex Bennée     NamedGPIOListHead gpios;
2606aebb1f6SAlex Bennée     /**
2616aebb1f6SAlex Bennée      * @clocks: QLIST of named clocks the device provides.
2626aebb1f6SAlex Bennée      */
2636aebb1f6SAlex Bennée     NamedClockListHead clocks;
2646aebb1f6SAlex Bennée     /**
2656aebb1f6SAlex Bennée      * @child_bus: QLIST of child buses
2666aebb1f6SAlex Bennée      */
2676aebb1f6SAlex Bennée     BusStateHead child_bus;
2686aebb1f6SAlex Bennée     /**
2696aebb1f6SAlex Bennée      * @num_child_bus: number of @child_bus entries
2706aebb1f6SAlex Bennée      */
271074a86fcSAnthony Liguori     int num_child_bus;
2726aebb1f6SAlex Bennée     /**
2736aebb1f6SAlex Bennée      * @instance_id_alias: device alias for handling legacy migration setups
2746aebb1f6SAlex Bennée      */
275074a86fcSAnthony Liguori     int instance_id_alias;
2766aebb1f6SAlex Bennée     /**
2776aebb1f6SAlex Bennée      * @alias_required_for_version: indicates @instance_id_alias is
2786aebb1f6SAlex Bennée      * needed for migration
2796aebb1f6SAlex Bennée      */
280074a86fcSAnthony Liguori     int alias_required_for_version;
2816aebb1f6SAlex Bennée     /**
2826aebb1f6SAlex Bennée      * @reset: ResettableState for the device; handled by Resettable interface.
2836aebb1f6SAlex Bennée      */
284c11256aaSDamien Hedde     ResettableState reset;
2856aebb1f6SAlex Bennée     /**
2866aebb1f6SAlex Bennée      * @unplug_blockers: list of reasons to block unplugging of device
2876aebb1f6SAlex Bennée      */
288217c7f01SJagannathan Raman     GSList *unplug_blockers;
2896aebb1f6SAlex Bennée     /**
2906aebb1f6SAlex Bennée      * @mem_reentrancy_guard: Is the device currently in mmio/pio/dma?
2916aebb1f6SAlex Bennée      *
2926aebb1f6SAlex Bennée      * Used to prevent re-entrancy confusing things.
2936aebb1f6SAlex Bennée      */
294a2e1753bSAlexander Bulekov     MemReentrancyGuard mem_reentrancy_guard;
295074a86fcSAnthony Liguori };
296074a86fcSAnthony Liguori 
297667cdad0SPaolo Bonzini typedef struct DeviceListener DeviceListener;
298707ff800SPaul Durrant struct DeviceListener {
299707ff800SPaul Durrant     void (*realize)(DeviceListener *listener, DeviceState *dev);
300707ff800SPaul Durrant     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
301f3a85056SJens Freimann     /*
302b91ad981SJuan Quintela      * This callback is called upon init of the DeviceState and
303b91ad981SJuan Quintela      * informs qdev if a device should be visible or hidden.  We can
304b91ad981SJuan Quintela      * hide a failover device depending for example on the device
305b91ad981SJuan Quintela      * opts.
3067d618082SKevin Wolf      *
3077d618082SKevin Wolf      * On errors, it returns false and errp is set. Device creation
3087d618082SKevin Wolf      * should fail in this case.
309f3a85056SJens Freimann      */
310f3558b1bSKevin Wolf     bool (*hide_device)(DeviceListener *listener, const QDict *device_opts,
311f3558b1bSKevin Wolf                         bool from_json, Error **errp);
312707ff800SPaul Durrant     QTAILQ_ENTRY(DeviceListener) link;
313707ff800SPaul Durrant };
314707ff800SPaul Durrant 
315074a86fcSAnthony Liguori #define TYPE_BUS "bus"
3168110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(BusState, BusClass,
3178110fa1dSEduardo Habkost                      BUS, TYPE_BUS)
318074a86fcSAnthony Liguori 
319074a86fcSAnthony Liguori struct BusClass {
320074a86fcSAnthony Liguori     ObjectClass parent_class;
321074a86fcSAnthony Liguori 
322074a86fcSAnthony Liguori     /* FIXME first arg should be BusState */
323074a86fcSAnthony Liguori     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
324074a86fcSAnthony Liguori     char *(*get_dev_path)(DeviceState *dev);
325bb755ba4SPaolo Bonzini 
326074a86fcSAnthony Liguori     /*
327074a86fcSAnthony Liguori      * This callback is used to create Open Firmware device path in accordance
328074a86fcSAnthony Liguori      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
329074a86fcSAnthony Liguori      * bindings can be found at http://playground.sun.com/1275/bindings/.
330074a86fcSAnthony Liguori      */
331074a86fcSAnthony Liguori     char *(*get_fw_dev_path)(DeviceState *dev);
332bb755ba4SPaolo Bonzini 
333bb755ba4SPaolo Bonzini     /*
334bb755ba4SPaolo Bonzini      * Return whether the device can be added to @bus,
335bb755ba4SPaolo Bonzini      * based on the address that was set (via device properties)
336bb755ba4SPaolo Bonzini      * before realize.  If not, on return @errp contains the
337bb755ba4SPaolo Bonzini      * human-readable error message.
338bb755ba4SPaolo Bonzini      */
339bb755ba4SPaolo Bonzini     bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
340bb755ba4SPaolo Bonzini 
34102e7f85dSBandan Das     BusRealize realize;
34202e7f85dSBandan Das     BusUnrealize unrealize;
34302e7f85dSBandan Das 
3441395af6fSKONRAD Frederic     /* maximum devices allowed on the bus, 0: no limit. */
3451395af6fSKONRAD Frederic     int max_dev;
34661de3676SAlexander Graf     /* number of automatically allocated bus ids (e.g. ide.0) */
34761de3676SAlexander Graf     int automatic_ids;
348074a86fcSAnthony Liguori };
349074a86fcSAnthony Liguori 
350074a86fcSAnthony Liguori typedef struct BusChild {
3512d24a646SMaxim Levitsky     struct rcu_head rcu;
352074a86fcSAnthony Liguori     DeviceState *child;
353074a86fcSAnthony Liguori     int index;
354074a86fcSAnthony Liguori     QTAILQ_ENTRY(BusChild) sibling;
355074a86fcSAnthony Liguori } BusChild;
356074a86fcSAnthony Liguori 
3570ee4de6cSIgor Mammedov #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
3580ee4de6cSIgor Mammedov 
3596aebb1f6SAlex Bennée typedef QTAILQ_HEAD(, BusChild) BusChildHead;
3606aebb1f6SAlex Bennée typedef QLIST_ENTRY(BusState) BusStateEntry;
3616aebb1f6SAlex Bennée 
362074a86fcSAnthony Liguori /**
3636aebb1f6SAlex Bennée  * struct BusState:
3646aebb1f6SAlex Bennée  * @obj: parent object
3656aebb1f6SAlex Bennée  * @parent: parent Device
3666aebb1f6SAlex Bennée  * @name: name of bus
36727c6ef1bSLi Qiang  * @hotplug_handler: link to a hotplug handler associated with bus.
3686aebb1f6SAlex Bennée  * @max_index: max number of child buses
3696aebb1f6SAlex Bennée  * @realized: is the bus itself realized?
3706aebb1f6SAlex Bennée  * @full: is the bus full?
3716aebb1f6SAlex Bennée  * @num_children: current number of child buses
372074a86fcSAnthony Liguori  */
373074a86fcSAnthony Liguori struct BusState {
3746aebb1f6SAlex Bennée     /* private: */
375074a86fcSAnthony Liguori     Object obj;
3766aebb1f6SAlex Bennée     /* public: */
377074a86fcSAnthony Liguori     DeviceState *parent;
378f73480c3SMarc-André Lureau     char *name;
3790ee4de6cSIgor Mammedov     HotplugHandler *hotplug_handler;
380074a86fcSAnthony Liguori     int max_index;
38102e7f85dSBandan Das     bool realized;
3821518562bSPeter Maydell     bool full;
38312b2e9f3STony Krowiak     int num_children;
3842d24a646SMaxim Levitsky 
3856aebb1f6SAlex Bennée     /**
3866aebb1f6SAlex Bennée      * @children: an RCU protected QTAILQ, thus readers must use RCU
3876aebb1f6SAlex Bennée      * to access it, and writers must hold the big qemu lock
3882d24a646SMaxim Levitsky      */
3896aebb1f6SAlex Bennée     BusChildHead children;
3906aebb1f6SAlex Bennée     /**
3916aebb1f6SAlex Bennée      * @sibling: next bus
3926aebb1f6SAlex Bennée      */
3936aebb1f6SAlex Bennée     BusStateEntry sibling;
3946aebb1f6SAlex Bennée     /**
3956aebb1f6SAlex Bennée      * @reset: ResettableState for the bus; handled by Resettable interface.
3966aebb1f6SAlex Bennée      */
397c11256aaSDamien Hedde     ResettableState reset;
398074a86fcSAnthony Liguori };
399074a86fcSAnthony Liguori 
4005cc56cc6SPeter Maydell /**
4016aebb1f6SAlex Bennée  * typedef GlobalProperty - a global property type
4026aebb1f6SAlex Bennée  *
403b3ce84feSEduardo Habkost  * @used: Set to true if property was used when initializing a device.
40492fd453cSDr. David Alan Gilbert  * @optional: If set to true, GlobalProperty will be skipped without errors
40592fd453cSDr. David Alan Gilbert  *            if the property doesn't exist.
406cff8b715SMarc-André Lureau  *
407cff8b715SMarc-André Lureau  * An error is fatal for non-hotplugged devices, when the global is applied.
4089f9260a3SDon Slutz  */
409074a86fcSAnthony Liguori typedef struct GlobalProperty {
410074a86fcSAnthony Liguori     const char *driver;
411074a86fcSAnthony Liguori     const char *property;
412074a86fcSAnthony Liguori     const char *value;
413b3ce84feSEduardo Habkost     bool used;
41492fd453cSDr. David Alan Gilbert     bool optional;
415074a86fcSAnthony Liguori } GlobalProperty;
416074a86fcSAnthony Liguori 
417ea9ce893SMarc-André Lureau static inline void
418ea9ce893SMarc-André Lureau compat_props_add(GPtrArray *arr,
419ea9ce893SMarc-André Lureau                  GlobalProperty props[], size_t nelem)
420ea9ce893SMarc-André Lureau {
421ea9ce893SMarc-André Lureau     int i;
422ea9ce893SMarc-André Lureau     for (i = 0; i < nelem; i++) {
423ea9ce893SMarc-André Lureau         g_ptr_array_add(arr, (void *)&props[i]);
424ea9ce893SMarc-André Lureau     }
425ea9ce893SMarc-André Lureau }
426ea9ce893SMarc-André Lureau 
427074a86fcSAnthony Liguori /*** Board API.  This should go away once we have a machine config file.  ***/
428074a86fcSAnthony Liguori 
429b51238e2SPeter Maydell /**
430b51238e2SPeter Maydell  * qdev_new: Create a device on the heap
431b51238e2SPeter Maydell  * @name: device type to create (we assert() that this type exists)
432b51238e2SPeter Maydell  *
433b51238e2SPeter Maydell  * This only allocates the memory and initializes the device state
434b51238e2SPeter Maydell  * structure, ready for the caller to set properties if they wish.
435b51238e2SPeter Maydell  * The device still needs to be realized.
4366aebb1f6SAlex Bennée  *
4376aebb1f6SAlex Bennée  * Return: a derived DeviceState object with a reference count of 1.
438b51238e2SPeter Maydell  */
4399940b2cfSMarkus Armbruster DeviceState *qdev_new(const char *name);
440694804edSPhilippe Mathieu-Daudé 
441b51238e2SPeter Maydell /**
442b51238e2SPeter Maydell  * qdev_try_new: Try to create a device on the heap
443b51238e2SPeter Maydell  * @name: device type to create
444b51238e2SPeter Maydell  *
445b51238e2SPeter Maydell  * This is like qdev_new(), except it returns %NULL when type @name
446b51238e2SPeter Maydell  * does not exist, rather than asserting.
4476aebb1f6SAlex Bennée  *
4486aebb1f6SAlex Bennée  * Return: a derived DeviceState object with a reference count of 1 or
4496aebb1f6SAlex Bennée  * NULL if type @name does not exist.
450b51238e2SPeter Maydell  */
4519940b2cfSMarkus Armbruster DeviceState *qdev_try_new(const char *name);
452694804edSPhilippe Mathieu-Daudé 
453b51238e2SPeter Maydell /**
4546aebb1f6SAlex Bennée  * qdev_is_realized() - check if device is realized
45526462a70SStefan Hajnoczi  * @dev: The device to check.
45626462a70SStefan Hajnoczi  *
4576aebb1f6SAlex Bennée  * Context: May be called outside big qemu lock.
4586aebb1f6SAlex Bennée  * Return: true if the device has been fully constructed, false otherwise.
45926462a70SStefan Hajnoczi  */
46026462a70SStefan Hajnoczi static inline bool qdev_is_realized(DeviceState *dev)
46126462a70SStefan Hajnoczi {
46226462a70SStefan Hajnoczi     return qatomic_load_acquire(&dev->realized);
46326462a70SStefan Hajnoczi }
46426462a70SStefan Hajnoczi 
46526462a70SStefan Hajnoczi /**
466b51238e2SPeter Maydell  * qdev_realize: Realize @dev.
467b51238e2SPeter Maydell  * @dev: device to realize
468b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
469b51238e2SPeter Maydell  * @errp: pointer to error object
470b51238e2SPeter Maydell  *
471b51238e2SPeter Maydell  * "Realize" the device, i.e. perform the second phase of device
472b51238e2SPeter Maydell  * initialization.
473b51238e2SPeter Maydell  * @dev must not be plugged into a bus already.
474b51238e2SPeter Maydell  * If @bus, plug @dev into @bus.  This takes a reference to @dev.
475b51238e2SPeter Maydell  * If @dev has no QOM parent, make one up, taking another reference.
476b51238e2SPeter Maydell  *
477b51238e2SPeter Maydell  * If you created @dev using qdev_new(), you probably want to use
478b51238e2SPeter Maydell  * qdev_realize_and_unref() instead.
4796aebb1f6SAlex Bennée  *
4806aebb1f6SAlex Bennée  * Return: true on success, else false setting @errp with error
481b51238e2SPeter Maydell  */
4829940b2cfSMarkus Armbruster bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
483694804edSPhilippe Mathieu-Daudé 
484b51238e2SPeter Maydell /**
485b51238e2SPeter Maydell  * qdev_realize_and_unref: Realize @dev and drop a reference
486b51238e2SPeter Maydell  * @dev: device to realize
487b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
488b51238e2SPeter Maydell  * @errp: pointer to error object
489b51238e2SPeter Maydell  *
490b51238e2SPeter Maydell  * Realize @dev and drop a reference.
491b51238e2SPeter Maydell  * This is like qdev_realize(), except the caller must hold a
492b51238e2SPeter Maydell  * (private) reference, which is dropped on return regardless of
493b51238e2SPeter Maydell  * success or failure.  Intended use::
494b51238e2SPeter Maydell  *
495b51238e2SPeter Maydell  *     dev = qdev_new();
496b51238e2SPeter Maydell  *     [...]
497b51238e2SPeter Maydell  *     qdev_realize_and_unref(dev, bus, errp);
498b51238e2SPeter Maydell  *
499b51238e2SPeter Maydell  * Now @dev can go away without further ado.
500b51238e2SPeter Maydell  *
501b51238e2SPeter Maydell  * If you are embedding the device into some other QOM device and
502b51238e2SPeter Maydell  * initialized it via some variant on object_initialize_child() then
503b51238e2SPeter Maydell  * do not use this function, because that family of functions arrange
504b51238e2SPeter Maydell  * for the only reference to the child device to be held by the parent
505b51238e2SPeter Maydell  * via the child<> property, and so the reference-count-drop done here
506b51238e2SPeter Maydell  * would be incorrect. For that use case you want qdev_realize().
5076aebb1f6SAlex Bennée  *
5086aebb1f6SAlex Bennée  * Return: true on success, else false setting @errp with error
509b51238e2SPeter Maydell  */
5109940b2cfSMarkus Armbruster bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
511694804edSPhilippe Mathieu-Daudé 
51246ea1be1SPeter Maydell /**
51346ea1be1SPeter Maydell  * qdev_unrealize: Unrealize a device
51446ea1be1SPeter Maydell  * @dev: device to unrealize
51546ea1be1SPeter Maydell  *
51646ea1be1SPeter Maydell  * This function will "unrealize" a device, which is the first phase
51746ea1be1SPeter Maydell  * of correctly destroying a device that has been realized. It will:
51846ea1be1SPeter Maydell  *
51946ea1be1SPeter Maydell  *  - unrealize any child buses by calling qbus_unrealize()
52046ea1be1SPeter Maydell  *    (this will recursively unrealize any devices on those buses)
5217a21bee2SDaniel P. Berrangé  *  - call the unrealize method of @dev
52246ea1be1SPeter Maydell  *
52346ea1be1SPeter Maydell  * The device can then be freed by causing its reference count to go
52446ea1be1SPeter Maydell  * to zero.
52546ea1be1SPeter Maydell  *
52646ea1be1SPeter Maydell  * Warning: most devices in QEMU do not expect to be unrealized.  Only
52746ea1be1SPeter Maydell  * devices which are hot-unpluggable should be unrealized (as part of
52846ea1be1SPeter Maydell  * the unplugging process); all other devices are expected to last for
52946ea1be1SPeter Maydell  * the life of the simulation and should not be unrealized and freed.
53046ea1be1SPeter Maydell  */
5319940b2cfSMarkus Armbruster void qdev_unrealize(DeviceState *dev);
532074a86fcSAnthony Liguori void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
533074a86fcSAnthony Liguori                                  int required_for_version);
53414405c27SDavid Hildenbrand HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
53503fcbd9dSThomas Huth HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
536d2321d31SPeter Xu bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
5376aebb1f6SAlex Bennée 
53817cc0128SIgor Mammedov /**
5396aebb1f6SAlex Bennée  * qdev_get_hotplug_handler() - Get handler responsible for device wiring
5406aebb1f6SAlex Bennée  * @dev: the device we want the HOTPLUG_HANDLER for.
54117cc0128SIgor Mammedov  *
54217cc0128SIgor Mammedov  * Note: in case @dev has a parent bus, it will be returned as handler unless
54317cc0128SIgor Mammedov  * machine handler overrides it.
54417cc0128SIgor Mammedov  *
5456aebb1f6SAlex Bennée  * Return: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
54617cc0128SIgor Mammedov  * or NULL if there aren't any.
54717cc0128SIgor Mammedov  */
548c06b2ffbSZhu Guihua HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
549074a86fcSAnthony Liguori void qdev_unplug(DeviceState *dev, Error **errp);
550014176f9SIgor Mammedov void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
551014176f9SIgor Mammedov                                   DeviceState *dev, Error **errp);
552074a86fcSAnthony Liguori void qdev_machine_creation_done(void);
553074a86fcSAnthony Liguori bool qdev_machine_modified(void);
554074a86fcSAnthony Liguori 
555cd07d7f9SPeter Maydell /**
556217c7f01SJagannathan Raman  * qdev_add_unplug_blocker: Add an unplug blocker to a device
557217c7f01SJagannathan Raman  *
558217c7f01SJagannathan Raman  * @dev: Device to be blocked from unplug
559217c7f01SJagannathan Raman  * @reason: Reason for blocking
560217c7f01SJagannathan Raman  */
561217c7f01SJagannathan Raman void qdev_add_unplug_blocker(DeviceState *dev, Error *reason);
562217c7f01SJagannathan Raman 
563217c7f01SJagannathan Raman /**
564217c7f01SJagannathan Raman  * qdev_del_unplug_blocker: Remove an unplug blocker from a device
565217c7f01SJagannathan Raman  *
566217c7f01SJagannathan Raman  * @dev: Device to be unblocked
567217c7f01SJagannathan Raman  * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
568217c7f01SJagannathan Raman  *          Used as a handle to lookup the blocker for deletion.
569217c7f01SJagannathan Raman  */
570217c7f01SJagannathan Raman void qdev_del_unplug_blocker(DeviceState *dev, Error *reason);
571217c7f01SJagannathan Raman 
572217c7f01SJagannathan Raman /**
573217c7f01SJagannathan Raman  * qdev_unplug_blocked: Confirm if a device is blocked from unplug
574217c7f01SJagannathan Raman  *
575217c7f01SJagannathan Raman  * @dev: Device to be tested
5766aebb1f6SAlex Bennée  * @errp: The reasons why the device is blocked, if any
577217c7f01SJagannathan Raman  *
5786aebb1f6SAlex Bennée  * Returns: true (also setting @errp) if device is blocked from unplug,
5796aebb1f6SAlex Bennée  * false otherwise
580217c7f01SJagannathan Raman  */
581217c7f01SJagannathan Raman bool qdev_unplug_blocked(DeviceState *dev, Error **errp);
582217c7f01SJagannathan Raman 
583217c7f01SJagannathan Raman /**
5846aebb1f6SAlex Bennée  * typedef GpioPolarity - Polarity of a GPIO line
585ddb67f64SPhilippe Mathieu-Daudé  *
586ddb67f64SPhilippe Mathieu-Daudé  * GPIO lines use either positive (active-high) logic,
587ddb67f64SPhilippe Mathieu-Daudé  * or negative (active-low) logic.
588ddb67f64SPhilippe Mathieu-Daudé  *
589ddb67f64SPhilippe Mathieu-Daudé  * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
590ddb67f64SPhilippe Mathieu-Daudé  * active when the voltage on the pin is high (relative to ground);
591ddb67f64SPhilippe Mathieu-Daudé  * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
592ddb67f64SPhilippe Mathieu-Daudé  * is active when the voltage on the pin is low (or grounded).
593ddb67f64SPhilippe Mathieu-Daudé  */
594ddb67f64SPhilippe Mathieu-Daudé typedef enum {
595ddb67f64SPhilippe Mathieu-Daudé     GPIO_POLARITY_ACTIVE_LOW,
596ddb67f64SPhilippe Mathieu-Daudé     GPIO_POLARITY_ACTIVE_HIGH
597ddb67f64SPhilippe Mathieu-Daudé } GpioPolarity;
598ddb67f64SPhilippe Mathieu-Daudé 
599ddb67f64SPhilippe Mathieu-Daudé /**
600cd07d7f9SPeter Maydell  * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
601cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
602cd07d7f9SPeter Maydell  * @n: Number of the anonymous GPIO line (which must be in range)
603cd07d7f9SPeter Maydell  *
604cd07d7f9SPeter Maydell  * Returns the qemu_irq corresponding to an anonymous input GPIO line
605cd07d7f9SPeter Maydell  * (which the device has set up with qdev_init_gpio_in()). The index
606cd07d7f9SPeter Maydell  * @n of the GPIO line must be valid (i.e. be at least 0 and less than
607cd07d7f9SPeter Maydell  * the total number of anonymous input GPIOs the device has); this
608cd07d7f9SPeter Maydell  * function will assert() if passed an invalid index.
609cd07d7f9SPeter Maydell  *
610cd07d7f9SPeter Maydell  * This function is intended to be used by board code or SoC "container"
611cd07d7f9SPeter Maydell  * device models to wire up the GPIO lines; usually the return value
612cd07d7f9SPeter Maydell  * will be passed to qdev_connect_gpio_out() or a similar function to
613cd07d7f9SPeter Maydell  * connect another device's output GPIO line to this input.
614cd07d7f9SPeter Maydell  *
615cd07d7f9SPeter Maydell  * For named input GPIO lines, use qdev_get_gpio_in_named().
6166aebb1f6SAlex Bennée  *
6176aebb1f6SAlex Bennée  * Return: qemu_irq corresponding to anonymous input GPIO line
618cd07d7f9SPeter Maydell  */
619074a86fcSAnthony Liguori qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
620694804edSPhilippe Mathieu-Daudé 
621cd07d7f9SPeter Maydell /**
622cd07d7f9SPeter Maydell  * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
623cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
624cd07d7f9SPeter Maydell  * @name: Name of the input GPIO array
625cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in that array (which must be in range)
626cd07d7f9SPeter Maydell  *
6271ee5f645SPeter Maydell  * Returns the qemu_irq corresponding to a single input GPIO line
6281ee5f645SPeter Maydell  * in a named array of input GPIO lines on a device (which the device
6291ee5f645SPeter Maydell  * has set up with qdev_init_gpio_in_named()).
630cd07d7f9SPeter Maydell  * The @name string must correspond to an input GPIO array which exists on
631cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
632cd07d7f9SPeter Maydell  * be at least 0 and less than the total number of input GPIOs in that
633cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
634cd07d7f9SPeter Maydell  *
635cd07d7f9SPeter Maydell  * For anonymous input GPIO lines, use qdev_get_gpio_in().
6366aebb1f6SAlex Bennée  *
6376aebb1f6SAlex Bennée  * Return: qemu_irq corresponding to named input GPIO line
638cd07d7f9SPeter Maydell  */
639a5f54290SPeter Crosthwaite qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
640a5f54290SPeter Crosthwaite 
641cd07d7f9SPeter Maydell /**
642cd07d7f9SPeter Maydell  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
643cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
644cd07d7f9SPeter Maydell  * @n: Number of the anonymous output GPIO line (which must be in range)
6456aebb1f6SAlex Bennée  * @pin: qemu_irq to connect the output line to
646cd07d7f9SPeter Maydell  *
647cd07d7f9SPeter Maydell  * This function connects an anonymous output GPIO line on a device
648cd07d7f9SPeter Maydell  * up to an arbitrary qemu_irq, so that when the device asserts that
649cd07d7f9SPeter Maydell  * output GPIO line, the qemu_irq's callback is invoked.
650cd07d7f9SPeter Maydell  * The index @n of the GPIO line must be valid (i.e. be at least 0 and
651cd07d7f9SPeter Maydell  * less than the total number of anonymous output GPIOs the device has
652cd07d7f9SPeter Maydell  * created with qdev_init_gpio_out()); otherwise this function will assert().
653cd07d7f9SPeter Maydell  *
654cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
655cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
656cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
657cd07d7f9SPeter Maydell  *
658cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
659cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
660cd07d7f9SPeter Maydell  * same qemu_irq. (Warning: there is no assertion or other guard to
661cd07d7f9SPeter Maydell  * catch this error: the model will just not do the right thing.)
6625df69ab8SPeter Maydell  * Instead, for fan-out you can use the TYPE_SPLIT_IRQ device: connect
663cd07d7f9SPeter Maydell  * a device's outbound GPIO to the splitter's input, and connect each
664cd07d7f9SPeter Maydell  * of the splitter's outputs to a different device.  For fan-in you
665cd07d7f9SPeter Maydell  * can use the TYPE_OR_IRQ device, which is a model of a logical OR
666cd07d7f9SPeter Maydell  * gate with multiple inputs and one output.
667cd07d7f9SPeter Maydell  *
668cd07d7f9SPeter Maydell  * For named output GPIO lines, use qdev_connect_gpio_out_named().
669cd07d7f9SPeter Maydell  */
670074a86fcSAnthony Liguori void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
671694804edSPhilippe Mathieu-Daudé 
672cd07d7f9SPeter Maydell /**
6731fbd004bSPhilippe Mathieu-Daudé  * qdev_connect_gpio_out_named: Connect one of a device's named output
6741fbd004bSPhilippe Mathieu-Daudé  *                              GPIO lines
675cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
676cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
6771ee5f645SPeter Maydell  * @n: Number of the output GPIO line within that array (which must be in range)
6782ebd9ce1SPhilippe Mathieu-Daudé  * @input_pin: qemu_irq to connect the output line to
679cd07d7f9SPeter Maydell  *
6801ee5f645SPeter Maydell  * This function connects a single GPIO output in a named array of output
6811ee5f645SPeter Maydell  * GPIO lines on a device up to an arbitrary qemu_irq, so that when the
6821ee5f645SPeter Maydell  * device asserts that output GPIO line, the qemu_irq's callback is invoked.
683cd07d7f9SPeter Maydell  * The @name string must correspond to an output GPIO array which exists on
684cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
6851ee5f645SPeter Maydell  * be at least 0 and less than the total number of output GPIOs in that
686cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
687cd07d7f9SPeter Maydell  *
688cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
689cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
690cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
691cd07d7f9SPeter Maydell  *
692cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
693cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
694cd07d7f9SPeter Maydell  * same qemu_irq; see qdev_connect_gpio_out() for details.
695cd07d7f9SPeter Maydell  *
6961fbd004bSPhilippe Mathieu-Daudé  * For anonymous output GPIO lines, use qdev_connect_gpio_out().
697cd07d7f9SPeter Maydell  */
698a5f54290SPeter Crosthwaite void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
6992ebd9ce1SPhilippe Mathieu-Daudé                                  qemu_irq input_pin);
700694804edSPhilippe Mathieu-Daudé 
701cd07d7f9SPeter Maydell /**
702cd07d7f9SPeter Maydell  * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
703cd07d7f9SPeter Maydell  * @dev: Device whose output GPIO we are interested in
704cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
705cd07d7f9SPeter Maydell  * @n: Number of the output GPIO line within that array
706cd07d7f9SPeter Maydell  *
707cd07d7f9SPeter Maydell  * Returns whatever qemu_irq is currently connected to the specified
708cd07d7f9SPeter Maydell  * output GPIO line of @dev. This will be NULL if the output GPIO line
709cd07d7f9SPeter Maydell  * has never been wired up to the anything.  Note that the qemu_irq
710cd07d7f9SPeter Maydell  * returned does not belong to @dev -- it will be the input GPIO or
711cd07d7f9SPeter Maydell  * IRQ of whichever device the board code has connected up to @dev's
712cd07d7f9SPeter Maydell  * output GPIO.
713cd07d7f9SPeter Maydell  *
714cd07d7f9SPeter Maydell  * You probably don't need to use this function -- it is used only
715cd07d7f9SPeter Maydell  * by the platform-bus subsystem.
7166aebb1f6SAlex Bennée  *
7176aebb1f6SAlex Bennée  * Return: qemu_irq associated with GPIO or NULL if un-wired.
718cd07d7f9SPeter Maydell  */
719b7973186SAlexander Graf qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
720694804edSPhilippe Mathieu-Daudé 
721cd07d7f9SPeter Maydell /**
722cd07d7f9SPeter Maydell  * qdev_intercept_gpio_out: Intercept an existing GPIO connection
723cd07d7f9SPeter Maydell  * @dev: Device to intercept the outbound GPIO line from
724cd07d7f9SPeter Maydell  * @icpt: New qemu_irq to connect instead
725cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
726cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in the array
727cd07d7f9SPeter Maydell  *
7286aebb1f6SAlex Bennée  * .. note::
729cd07d7f9SPeter Maydell  *   This function is provided only for use by the qtest testing framework
730cd07d7f9SPeter Maydell  *   and is not suitable for use in non-testing parts of QEMU.
731cd07d7f9SPeter Maydell  *
732cd07d7f9SPeter Maydell  * This function breaks an existing connection of an outbound GPIO
733cd07d7f9SPeter Maydell  * line from @dev, and replaces it with the new qemu_irq @icpt, as if
734cd07d7f9SPeter Maydell  * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
735cd07d7f9SPeter Maydell  * The previously connected qemu_irq is returned, so it can be restored
736cd07d7f9SPeter Maydell  * by a second call to qdev_intercept_gpio_out() if desired.
7376aebb1f6SAlex Bennée  *
7386aebb1f6SAlex Bennée  * Return: old disconnected qemu_irq if one existed
739cd07d7f9SPeter Maydell  */
7400c24db2bSPeter Crosthwaite qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
7410c24db2bSPeter Crosthwaite                                  const char *name, int n);
742074a86fcSAnthony Liguori 
743074a86fcSAnthony Liguori BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
744074a86fcSAnthony Liguori 
745074a86fcSAnthony Liguori /*** Device API.  ***/
746074a86fcSAnthony Liguori 
747cd07d7f9SPeter Maydell /**
748cd07d7f9SPeter Maydell  * qdev_init_gpio_in: create an array of anonymous input GPIO lines
749cd07d7f9SPeter Maydell  * @dev: Device to create input GPIOs for
750cd07d7f9SPeter Maydell  * @handler: Function to call when GPIO line value is set
751cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
752cd07d7f9SPeter Maydell  *
753cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_in* family in
754cd07d7f9SPeter Maydell  * their instance_init or realize methods to create any input GPIO
755cd07d7f9SPeter Maydell  * lines they need. There is no functional difference between
756cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
757cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
758cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO input whose purpose is obvious.
759cd07d7f9SPeter Maydell  * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
760cd07d7f9SPeter Maydell  *
761cd07d7f9SPeter Maydell  * See qdev_get_gpio_in() for how code that uses such a device can get
762cd07d7f9SPeter Maydell  * hold of an input GPIO line to manipulate it.
763cd07d7f9SPeter Maydell  */
764074a86fcSAnthony Liguori void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
765694804edSPhilippe Mathieu-Daudé 
766cd07d7f9SPeter Maydell /**
767cd07d7f9SPeter Maydell  * qdev_init_gpio_out: create an array of anonymous output GPIO lines
768cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
769cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
770cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
771cd07d7f9SPeter Maydell  *
772cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_out* family
773cd07d7f9SPeter Maydell  * in their instance_init or realize methods to create any output
774cd07d7f9SPeter Maydell  * GPIO lines they need. There is no functional difference between
775cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
776cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
777cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO output whose purpose is obvious.
778cd07d7f9SPeter Maydell  *
779cd07d7f9SPeter Maydell  * The @pins argument should be a pointer to either a "qemu_irq"
780cd07d7f9SPeter Maydell  * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
781cd07d7f9SPeter Maydell  * state structure. The device implementation can then raise and
782cd07d7f9SPeter Maydell  * lower the GPIO line by calling qemu_set_irq(). (If anything is
783cd07d7f9SPeter Maydell  * connected to the other end of the GPIO this will cause the handler
784cd07d7f9SPeter Maydell  * function for that input GPIO to be called.)
785cd07d7f9SPeter Maydell  *
786cd07d7f9SPeter Maydell  * See qdev_connect_gpio_out() for how code that uses such a device
787cd07d7f9SPeter Maydell  * can connect to one of its output GPIO lines.
788526dc840SPhilippe Mathieu-Daudé  *
789526dc840SPhilippe Mathieu-Daudé  * There is no need to release the @pins allocated array because it
790526dc840SPhilippe Mathieu-Daudé  * will be automatically released when @dev calls its instance_finalize()
791526dc840SPhilippe Mathieu-Daudé  * handler.
792cd07d7f9SPeter Maydell  */
793074a86fcSAnthony Liguori void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
794694804edSPhilippe Mathieu-Daudé 
795cd07d7f9SPeter Maydell /**
79614b0375bSPhilippe Mathieu-Daudé  * qdev_init_gpio_out_named: create an array of named output GPIO lines
797cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
798cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
799cd07d7f9SPeter Maydell  * @name: Name to give this array of GPIO lines
8001ee5f645SPeter Maydell  * @n: Number of GPIO lines to create in this array
801cd07d7f9SPeter Maydell  *
802cd07d7f9SPeter Maydell  * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
803cd07d7f9SPeter Maydell  * with a name. Code using the device can then connect these GPIO lines
804cd07d7f9SPeter Maydell  * using qdev_connect_gpio_out_named().
805cd07d7f9SPeter Maydell  */
806a5f54290SPeter Crosthwaite void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
807a5f54290SPeter Crosthwaite                               const char *name, int n);
808694804edSPhilippe Mathieu-Daudé 
8094a151677SPeter Maydell /**
8106aebb1f6SAlex Bennée  * qdev_init_gpio_in_named_with_opaque() - create an array of input GPIO lines
8114a151677SPeter Maydell  * @dev: Device to create input GPIOs for
8124a151677SPeter Maydell  * @handler: Function to call when GPIO line value is set
8134a151677SPeter Maydell  * @opaque: Opaque data pointer to pass to @handler
8144a151677SPeter Maydell  * @name: Name of the GPIO input (must be unique for this device)
8154a151677SPeter Maydell  * @n: Number of GPIO lines in this input set
8164a151677SPeter Maydell  */
8174a151677SPeter Maydell void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
8184a151677SPeter Maydell                                          qemu_irq_handler handler,
8194a151677SPeter Maydell                                          void *opaque,
8204a151677SPeter Maydell                                          const char *name, int n);
8214a151677SPeter Maydell 
8224a151677SPeter Maydell /**
8236aebb1f6SAlex Bennée  * qdev_init_gpio_in_named() - create an array of input GPIO lines
8246aebb1f6SAlex Bennée  * @dev: device to add array to
8256aebb1f6SAlex Bennée  * @handler: a &typedef qemu_irq_handler function to call when GPIO is set
8266aebb1f6SAlex Bennée  * @name: Name of the GPIO input (must be unique for this device)
8276aebb1f6SAlex Bennée  * @n: Number of GPIO lines in this input set
8284a151677SPeter Maydell  *
8294a151677SPeter Maydell  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
8304a151677SPeter Maydell  * passed to the handler is @dev (which is the most commonly desired behaviour).
8314a151677SPeter Maydell  */
8324a151677SPeter Maydell static inline void qdev_init_gpio_in_named(DeviceState *dev,
8334a151677SPeter Maydell                                            qemu_irq_handler handler,
8344a151677SPeter Maydell                                            const char *name, int n)
8354a151677SPeter Maydell {
8364a151677SPeter Maydell     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
8374a151677SPeter Maydell }
838074a86fcSAnthony Liguori 
839cd07d7f9SPeter Maydell /**
840cd07d7f9SPeter Maydell  * qdev_pass_gpios: create GPIO lines on container which pass through to device
841cd07d7f9SPeter Maydell  * @dev: Device which has GPIO lines
842cd07d7f9SPeter Maydell  * @container: Container device which needs to expose them
843cd07d7f9SPeter Maydell  * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
844cd07d7f9SPeter Maydell  *
845cd07d7f9SPeter Maydell  * In QEMU, complicated devices like SoCs are often modelled with a
846cd07d7f9SPeter Maydell  * "container" QOM device which itself contains other QOM devices and
847cd07d7f9SPeter Maydell  * which wires them up appropriately. This function allows the container
848cd07d7f9SPeter Maydell  * to create GPIO arrays on itself which simply pass through to a GPIO
849cd07d7f9SPeter Maydell  * array of one of its internal devices.
850cd07d7f9SPeter Maydell  *
851cd07d7f9SPeter Maydell  * If @dev has both input and output GPIOs named @name then both will
852cd07d7f9SPeter Maydell  * be passed through. It is not possible to pass a subset of the array
853cd07d7f9SPeter Maydell  * with this function.
854cd07d7f9SPeter Maydell  *
855cd07d7f9SPeter Maydell  * To users of the container device, the GPIO array created on @container
856cd07d7f9SPeter Maydell  * behaves exactly like any other.
857cd07d7f9SPeter Maydell  */
85817a96a14SPeter Crosthwaite void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
85917a96a14SPeter Crosthwaite                      const char *name);
86017a96a14SPeter Crosthwaite 
8612d2f2507SPhilippe Mathieu-Daudé BusState *qdev_get_parent_bus(const DeviceState *dev);
862074a86fcSAnthony Liguori 
863074a86fcSAnthony Liguori /*** BUS API. ***/
864074a86fcSAnthony Liguori 
865074a86fcSAnthony Liguori DeviceState *qdev_find_recursive(BusState *bus, const char *id);
866074a86fcSAnthony Liguori 
867074a86fcSAnthony Liguori /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
868074a86fcSAnthony Liguori typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
869074a86fcSAnthony Liguori typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
870074a86fcSAnthony Liguori 
871d637e1dcSPeter Maydell void qbus_init(void *bus, size_t size, const char *typename,
872074a86fcSAnthony Liguori                DeviceState *parent, const char *name);
8739388d170SPeter Maydell BusState *qbus_new(const char *typename, DeviceState *parent, const char *name);
8749940b2cfSMarkus Armbruster bool qbus_realize(BusState *bus, Error **errp);
8759940b2cfSMarkus Armbruster void qbus_unrealize(BusState *bus);
8769940b2cfSMarkus Armbruster 
877074a86fcSAnthony Liguori /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
878074a86fcSAnthony Liguori  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
879074a86fcSAnthony Liguori  *           0 otherwise. */
8800293214bSPaolo Bonzini int qbus_walk_children(BusState *bus,
8810293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
8820293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
8830293214bSPaolo Bonzini                        void *opaque);
8840293214bSPaolo Bonzini int qdev_walk_children(DeviceState *dev,
8850293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
8860293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
8870293214bSPaolo Bonzini                        void *opaque);
8880293214bSPaolo Bonzini 
889abb89dbfSDamien Hedde /**
8906aebb1f6SAlex Bennée  * device_cold_reset() - perform a recursive cold reset on a device
8916aebb1f6SAlex Bennée  * @dev: device to reset.
8926aebb1f6SAlex Bennée  *
893abb89dbfSDamien Hedde  * Reset device @dev and perform a recursive processing using the resettable
894abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
895abb89dbfSDamien Hedde  */
896abb89dbfSDamien Hedde void device_cold_reset(DeviceState *dev);
897abb89dbfSDamien Hedde 
898abb89dbfSDamien Hedde /**
8996aebb1f6SAlex Bennée  * bus_cold_reset() - perform a recursive cold reset on a bus
9006aebb1f6SAlex Bennée  * @bus: bus to reset
901abb89dbfSDamien Hedde  *
902abb89dbfSDamien Hedde  * Reset bus @bus and perform a recursive processing using the resettable
903abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
904abb89dbfSDamien Hedde  */
905abb89dbfSDamien Hedde void bus_cold_reset(BusState *bus);
906abb89dbfSDamien Hedde 
907abb89dbfSDamien Hedde /**
9086aebb1f6SAlex Bennée  * device_is_in_reset() - check device reset state
9096aebb1f6SAlex Bennée  * @dev: device to check
9106aebb1f6SAlex Bennée  *
9116aebb1f6SAlex Bennée  * Return: true if the device @dev is currently being reset.
912c11256aaSDamien Hedde  */
913c11256aaSDamien Hedde bool device_is_in_reset(DeviceState *dev);
914c11256aaSDamien Hedde 
915c11256aaSDamien Hedde /**
9166aebb1f6SAlex Bennée  * bus_is_in_reset() - check bus reset state
9176aebb1f6SAlex Bennée  * @bus: bus to check
9186aebb1f6SAlex Bennée  *
9196aebb1f6SAlex Bennée  * Return: true if the bus @bus is currently being reset.
920c11256aaSDamien Hedde  */
921c11256aaSDamien Hedde bool bus_is_in_reset(BusState *bus);
922c11256aaSDamien Hedde 
923074a86fcSAnthony Liguori /* This should go away once we get rid of the NULL bus hack */
924074a86fcSAnthony Liguori BusState *sysbus_get_default(void);
925074a86fcSAnthony Liguori 
926074a86fcSAnthony Liguori char *qdev_get_fw_dev_path(DeviceState *dev);
9270be63901SGonglei char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
928074a86fcSAnthony Liguori 
929e57fc3deSAlex Bennée /**
930e57fc3deSAlex Bennée  * device_class_set_props(): add a set of properties to an device
931e57fc3deSAlex Bennée  * @dc: the parent DeviceClass all devices inherit
932e57fc3deSAlex Bennée  * @props: an array of properties, terminate by DEFINE_PROP_END_OF_LIST()
933e57fc3deSAlex Bennée  *
934e57fc3deSAlex Bennée  * This will add a set of properties to the object. It will fault if
935e57fc3deSAlex Bennée  * you attempt to add an existing property defined by a parent class.
936e57fc3deSAlex Bennée  * To modify an inherited property you need to use????
937e57fc3deSAlex Bennée  */
938*d36f165dSPaolo Bonzini void device_class_set_props(DeviceClass *dc, const Property *props);
9394f67d30bSMarc-André Lureau 
940c11256aaSDamien Hedde /**
941c378e882SAlex Bennée  * device_class_set_parent_realize() - set up for chaining realize fns
942c378e882SAlex Bennée  * @dc: The device class
943c378e882SAlex Bennée  * @dev_realize: the device realize function
944c378e882SAlex Bennée  * @parent_realize: somewhere to save the parents realize function
945c378e882SAlex Bennée  *
946c378e882SAlex Bennée  * This is intended to be used when the new realize function will
947c378e882SAlex Bennée  * eventually call its parent realization function during creation.
948c378e882SAlex Bennée  * This requires storing the function call somewhere (usually in the
949c378e882SAlex Bennée  * instance structure) so you can eventually call
950c378e882SAlex Bennée  * dc->parent_realize(dev, errp)
951c378e882SAlex Bennée  */
95246795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_realize(DeviceClass *dc,
95346795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize dev_realize,
95446795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize *parent_realize);
955c378e882SAlex Bennée 
956134e0944SPeter Maydell /**
957134e0944SPeter Maydell  * device_class_set_legacy_reset(): set the DeviceClass::reset method
958134e0944SPeter Maydell  * @dc: The device class
959134e0944SPeter Maydell  * @dev_reset: the reset function
960134e0944SPeter Maydell  *
961134e0944SPeter Maydell  * This function sets the DeviceClass::reset method. This is widely
962134e0944SPeter Maydell  * used in existing code, but new code should prefer to use the
963134e0944SPeter Maydell  * Resettable API as documented in docs/devel/reset.rst.
964134e0944SPeter Maydell  * In addition, devices which need to chain to their parent class's
965134e0944SPeter Maydell  * reset methods or which need to be subclassed must use Resettable.
966134e0944SPeter Maydell  */
967134e0944SPeter Maydell void device_class_set_legacy_reset(DeviceClass *dc,
968134e0944SPeter Maydell                                    DeviceReset dev_reset);
969c378e882SAlex Bennée 
970c378e882SAlex Bennée /**
971c378e882SAlex Bennée  * device_class_set_parent_unrealize() - set up for chaining unrealize fns
972c378e882SAlex Bennée  * @dc: The device class
973c378e882SAlex Bennée  * @dev_unrealize: the device realize function
974c378e882SAlex Bennée  * @parent_unrealize: somewhere to save the parents unrealize function
975c378e882SAlex Bennée  *
976c378e882SAlex Bennée  * This is intended to be used when the new unrealize function will
977c378e882SAlex Bennée  * eventually call its parent unrealization function during the
978c378e882SAlex Bennée  * unrealize phase. This requires storing the function call somewhere
979c378e882SAlex Bennée  * (usually in the instance structure) so you can eventually call
980c378e882SAlex Bennée  * dc->parent_unrealize(dev);
981c378e882SAlex Bennée  */
98246795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_unrealize(DeviceClass *dc,
98346795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize dev_unrealize,
98446795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize *parent_unrealize);
98546795cf2SPhilippe Mathieu-Daudé 
9868a9358ccSMarkus Armbruster const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
987074a86fcSAnthony Liguori 
988074a86fcSAnthony Liguori const char *qdev_fw_name(DeviceState *dev);
989074a86fcSAnthony Liguori 
990f66dc873SPaolo Bonzini void qdev_assert_realized_properly(void);
991074a86fcSAnthony Liguori Object *qdev_get_machine(void);
992074a86fcSAnthony Liguori 
993956ef499SManos Pitsidianakis /**
994956ef499SManos Pitsidianakis  * qdev_get_human_name() - Return a human-readable name for a device
995956ef499SManos Pitsidianakis  * @dev: The device. Must be a valid and non-NULL pointer.
996956ef499SManos Pitsidianakis  *
997956ef499SManos Pitsidianakis  * .. note::
998956ef499SManos Pitsidianakis  *    This function is intended for user friendly error messages.
999956ef499SManos Pitsidianakis  *
1000956ef499SManos Pitsidianakis  * Returns: A newly allocated string containing the device id if not null,
1001956ef499SManos Pitsidianakis  * else the object canonical path.
1002956ef499SManos Pitsidianakis  *
1003956ef499SManos Pitsidianakis  * Use g_free() to free it.
1004956ef499SManos Pitsidianakis  */
1005956ef499SManos Pitsidianakis char *qdev_get_human_name(DeviceState *dev);
1006956ef499SManos Pitsidianakis 
1007074a86fcSAnthony Liguori /* FIXME: make this a link<> */
1008bb755ba4SPaolo Bonzini bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
1009074a86fcSAnthony Liguori 
101021def24aSJuan Quintela extern bool qdev_hot_removed;
1011074a86fcSAnthony Liguori 
1012074a86fcSAnthony Liguori char *qdev_get_dev_path(DeviceState *dev);
1013074a86fcSAnthony Liguori 
10149bc6bfdfSMarkus Armbruster void qbus_set_hotplug_handler(BusState *bus, Object *handler);
1015cd7c8660SMarkus Armbruster void qbus_set_bus_hotplug_handler(BusState *bus);
101639b888bdSIgor Mammedov 
101739b888bdSIgor Mammedov static inline bool qbus_is_hotpluggable(BusState *bus)
101839b888bdSIgor Mammedov {
1019ceefa0b7SIgor Mammedov     HotplugHandler *plug_handler = bus->hotplug_handler;
1020ceefa0b7SIgor Mammedov     bool ret = !!plug_handler;
1021ceefa0b7SIgor Mammedov 
1022ceefa0b7SIgor Mammedov     if (plug_handler) {
1023ceefa0b7SIgor Mammedov         HotplugHandlerClass *hdc;
1024ceefa0b7SIgor Mammedov 
1025ceefa0b7SIgor Mammedov         hdc = HOTPLUG_HANDLER_GET_CLASS(plug_handler);
1026ceefa0b7SIgor Mammedov         if (hdc->is_hotpluggable_bus) {
1027ceefa0b7SIgor Mammedov             ret = hdc->is_hotpluggable_bus(plug_handler, bus);
1028ceefa0b7SIgor Mammedov         }
1029ceefa0b7SIgor Mammedov     }
1030ceefa0b7SIgor Mammedov     return ret;
103139b888bdSIgor Mammedov }
1032707ff800SPaul Durrant 
10331518562bSPeter Maydell /**
10341518562bSPeter Maydell  * qbus_mark_full: Mark this bus as full, so no more devices can be attached
10351518562bSPeter Maydell  * @bus: Bus to mark as full
10361518562bSPeter Maydell  *
10371518562bSPeter Maydell  * By default, QEMU will allow devices to be plugged into a bus up
10381518562bSPeter Maydell  * to the bus class's device count limit. Calling this function
10391518562bSPeter Maydell  * marks a particular bus as full, so that no more devices can be
10401518562bSPeter Maydell  * plugged into it. In particular this means that the bus will not
10411518562bSPeter Maydell  * be considered as a candidate for plugging in devices created by
10421518562bSPeter Maydell  * the user on the commandline or via the monitor.
10431518562bSPeter Maydell  * If a machine has multiple buses of a given type, such as I2C,
10441518562bSPeter Maydell  * where some of those buses in the real hardware are used only for
10451518562bSPeter Maydell  * internal devices and some are exposed via expansion ports, you
10461518562bSPeter Maydell  * can use this function to mark the internal-only buses as full
10471518562bSPeter Maydell  * after you have created all their internal devices. Then user
10481518562bSPeter Maydell  * created devices will appear on the expansion-port bus where
10491518562bSPeter Maydell  * guest software expects them.
10501518562bSPeter Maydell  */
10511518562bSPeter Maydell static inline void qbus_mark_full(BusState *bus)
10521518562bSPeter Maydell {
10531518562bSPeter Maydell     bus->full = true;
10541518562bSPeter Maydell }
10551518562bSPeter Maydell 
1056707ff800SPaul Durrant void device_listener_register(DeviceListener *listener);
1057707ff800SPaul Durrant void device_listener_unregister(DeviceListener *listener);
1058707ff800SPaul Durrant 
1059f3a85056SJens Freimann /**
10606aebb1f6SAlex Bennée  * qdev_should_hide_device() - check if device should be hidden
10616aebb1f6SAlex Bennée  *
1062f3558b1bSKevin Wolf  * @opts: options QDict
1063f3558b1bSKevin Wolf  * @from_json: true if @opts entries are typed, false for all strings
1064f3558b1bSKevin Wolf  * @errp: pointer to error object
1065f3a85056SJens Freimann  *
10666aebb1f6SAlex Bennée  * When a device is added via qdev_device_add() this will be called.
10676aebb1f6SAlex Bennée  *
10686aebb1f6SAlex Bennée  * Return: if the device should be added now or not.
1069f3a85056SJens Freimann  */
1070f3558b1bSKevin Wolf bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp);
1071f3a85056SJens Freimann 
10722f181fbdSPaolo Bonzini typedef enum MachineInitPhase {
10732f181fbdSPaolo Bonzini     /* current_machine is NULL.  */
10742f181fbdSPaolo Bonzini     PHASE_NO_MACHINE,
10752f181fbdSPaolo Bonzini 
10762f181fbdSPaolo Bonzini     /* current_machine is not NULL, but current_machine->accel is NULL.  */
10772f181fbdSPaolo Bonzini     PHASE_MACHINE_CREATED,
10782f181fbdSPaolo Bonzini 
10792f181fbdSPaolo Bonzini     /*
10802f181fbdSPaolo Bonzini      * current_machine->accel is not NULL, but the machine properties have
10812f181fbdSPaolo Bonzini      * not been validated and machine_class->init has not yet been called.
10822f181fbdSPaolo Bonzini      */
10832f181fbdSPaolo Bonzini     PHASE_ACCEL_CREATED,
10842f181fbdSPaolo Bonzini 
10852f181fbdSPaolo Bonzini     /*
108604accf43SMark Kanda      * Late backend objects have been created and initialized.
108704accf43SMark Kanda      */
108804accf43SMark Kanda     PHASE_LATE_BACKENDS_CREATED,
108904accf43SMark Kanda 
109004accf43SMark Kanda     /*
10912f181fbdSPaolo Bonzini      * machine_class->init has been called, thus creating any embedded
10922f181fbdSPaolo Bonzini      * devices and validating machine properties.  Devices created at
10932f181fbdSPaolo Bonzini      * this time are considered to be cold-plugged.
10942f181fbdSPaolo Bonzini      */
10952f181fbdSPaolo Bonzini     PHASE_MACHINE_INITIALIZED,
10962f181fbdSPaolo Bonzini 
10972f181fbdSPaolo Bonzini     /*
10982f181fbdSPaolo Bonzini      * QEMU is ready to start CPUs and devices created at this time
10992f181fbdSPaolo Bonzini      * are considered to be hot-plugged.  The monitor is not restricted
11002f181fbdSPaolo Bonzini      * to "preconfig" commands.
11012f181fbdSPaolo Bonzini      */
11022f181fbdSPaolo Bonzini     PHASE_MACHINE_READY,
11032f181fbdSPaolo Bonzini } MachineInitPhase;
11042f181fbdSPaolo Bonzini 
1105f703f1efSPhilippe Mathieu-Daudé bool phase_check(MachineInitPhase phase);
1106f703f1efSPhilippe Mathieu-Daudé void phase_advance(MachineInitPhase phase);
11072f181fbdSPaolo Bonzini 
1108074a86fcSAnthony Liguori #endif
1109