xref: /qemu/include/hw/qdev-core.h (revision 1088d41795101479e2d88f1e6140071732f9bdb3)
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);
983f98408eSVladimir Sementsov-Ogievskiy typedef int (*DeviceSyncConfig)(DeviceState *dev, Error **errp);
99074a86fcSAnthony Liguori 
100249d4172SAndreas Färber /**
1016aebb1f6SAlex Bennée  * struct DeviceClass - The base class for all devices.
102249d4172SAndreas Färber  * @props: Properties accessing state fields.
103249d4172SAndreas Färber  * @realize: Callback function invoked when the #DeviceState:realized
104ff46d9d4SPhilippe Mathieu-Daudé  * property is changed to %true.
105249d4172SAndreas Färber  * @unrealize: Callback function invoked when the #DeviceState:realized
106249d4172SAndreas Färber  * property is changed to %false.
1073f98408eSVladimir Sementsov-Ogievskiy  * @sync_config: Callback function invoked when QMP command device-sync-config
1083f98408eSVladimir Sementsov-Ogievskiy  * is called. Should synchronize device configuration from host to guest part
1093f98408eSVladimir Sementsov-Ogievskiy  * and notify the guest about the change.
1101a37eca1SIgor Mammedov  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
1111a37eca1SIgor Mammedov  * as readonly "hotpluggable" property of #DeviceState instance
112249d4172SAndreas Färber  *
113249d4172SAndreas Färber  */
114db1015e9SEduardo Habkost struct DeviceClass {
1156aebb1f6SAlex Bennée     /* private: */
116074a86fcSAnthony Liguori     ObjectClass parent_class;
117074a86fcSAnthony Liguori 
1186aebb1f6SAlex Bennée     /* public: */
1196aebb1f6SAlex Bennée 
1206aebb1f6SAlex Bennée     /**
1216aebb1f6SAlex Bennée      * @categories: device categories device belongs to
1226aebb1f6SAlex Bennée      */
1233d1237fbSMarcel Apfelbaum     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
1246aebb1f6SAlex Bennée     /**
1256aebb1f6SAlex Bennée      * @fw_name: name used to identify device to firmware interfaces
1266aebb1f6SAlex Bennée      */
127074a86fcSAnthony Liguori     const char *fw_name;
1286aebb1f6SAlex Bennée     /**
1296aebb1f6SAlex Bennée      * @desc: human readable description of device
1306aebb1f6SAlex Bennée      */
131074a86fcSAnthony Liguori     const char *desc;
132385d8f22SPaolo Bonzini 
1336aebb1f6SAlex Bennée     /**
1346aebb1f6SAlex Bennée      * @props_: properties associated with device, should only be
1356aebb1f6SAlex Bennée      * assigned by using device_class_set_props(). The underscore
1366aebb1f6SAlex Bennée      * ensures a compile-time error if someone attempts to assign
1376aebb1f6SAlex Bennée      * dc->props directly.
138385d8f22SPaolo Bonzini      */
139d36f165dSPaolo Bonzini     const Property *props_;
140efec3dd6SMarkus Armbruster 
1416aebb1f6SAlex Bennée     /**
142cb9f4b28SRichard Henderson      * @props_count_: number of elements in @props_; should only be
143cb9f4b28SRichard Henderson      * assigned by using device_class_set_props().
144cb9f4b28SRichard Henderson      */
145cb9f4b28SRichard Henderson     uint16_t props_count_;
146cb9f4b28SRichard Henderson 
147cb9f4b28SRichard Henderson     /**
1486aebb1f6SAlex Bennée      * @user_creatable: Can user instantiate with -device / device_add?
1496aebb1f6SAlex Bennée      *
150efec3dd6SMarkus Armbruster      * All devices should support instantiation with device_add, and
151efec3dd6SMarkus Armbruster      * this flag should not exist.  But we're not there, yet.  Some
152efec3dd6SMarkus Armbruster      * devices fail to instantiate with cryptic error messages.
153efec3dd6SMarkus Armbruster      * Others instantiate, but don't work.  Exposing users to such
154e90f2a8cSEduardo Habkost      * behavior would be cruel; clearing this flag will protect them.
155e90f2a8cSEduardo Habkost      * It should never be cleared without a comment explaining why it
156e90f2a8cSEduardo Habkost      * is cleared.
1576aebb1f6SAlex Bennée      *
158efec3dd6SMarkus Armbruster      * TODO remove once we're there
159efec3dd6SMarkus Armbruster      */
160e90f2a8cSEduardo Habkost     bool user_creatable;
1611a37eca1SIgor Mammedov     bool hotpluggable;
162074a86fcSAnthony Liguori 
163074a86fcSAnthony Liguori     /* callbacks */
1646aebb1f6SAlex Bennée     /**
1651000872dSPeter Maydell      * @legacy_reset: deprecated device reset method pointer
1666aebb1f6SAlex Bennée      *
1676aebb1f6SAlex Bennée      * Modern code should use the ResettableClass interface to
1686aebb1f6SAlex Bennée      * implement a multi-phase reset.
1696aebb1f6SAlex Bennée      *
170c11256aaSDamien Hedde      * TODO: remove once every reset callback is unused
171c11256aaSDamien Hedde      */
1721000872dSPeter Maydell     DeviceReset legacy_reset;
173249d4172SAndreas Färber     DeviceRealize realize;
174249d4172SAndreas Färber     DeviceUnrealize unrealize;
1753f98408eSVladimir Sementsov-Ogievskiy     DeviceSyncConfig sync_config;
176074a86fcSAnthony Liguori 
1776aebb1f6SAlex Bennée     /**
1786aebb1f6SAlex Bennée      * @vmsd: device state serialisation description for
1796aebb1f6SAlex Bennée      * migration/save/restore
1806aebb1f6SAlex Bennée      */
1818a9358ccSMarkus Armbruster     const VMStateDescription *vmsd;
182074a86fcSAnthony Liguori 
1836aebb1f6SAlex Bennée     /**
1846aebb1f6SAlex Bennée      * @bus_type: bus type
1856aebb1f6SAlex Bennée      * private: to qdev / bus.
1866aebb1f6SAlex Bennée      */
187074a86fcSAnthony Liguori     const char *bus_type;
188db1015e9SEduardo Habkost };
189074a86fcSAnthony Liguori 
190a5f54290SPeter Crosthwaite typedef struct NamedGPIOList NamedGPIOList;
191a5f54290SPeter Crosthwaite 
192a5f54290SPeter Crosthwaite struct NamedGPIOList {
193a5f54290SPeter Crosthwaite     char *name;
194a5f54290SPeter Crosthwaite     qemu_irq *in;
195a5f54290SPeter Crosthwaite     int num_in;
196a5f54290SPeter Crosthwaite     int num_out;
197a5f54290SPeter Crosthwaite     QLIST_ENTRY(NamedGPIOList) node;
198a5f54290SPeter Crosthwaite };
199a5f54290SPeter Crosthwaite 
2000e6934f2SDamien Hedde typedef struct Clock Clock;
2010e6934f2SDamien Hedde typedef struct NamedClockList NamedClockList;
2020e6934f2SDamien Hedde 
2030e6934f2SDamien Hedde struct NamedClockList {
2040e6934f2SDamien Hedde     char *name;
2050e6934f2SDamien Hedde     Clock *clock;
2060e6934f2SDamien Hedde     bool output;
2070e6934f2SDamien Hedde     bool alias;
2080e6934f2SDamien Hedde     QLIST_ENTRY(NamedClockList) node;
2090e6934f2SDamien Hedde };
2100e6934f2SDamien Hedde 
211a2e1753bSAlexander Bulekov typedef struct {
212a2e1753bSAlexander Bulekov     bool engaged_in_io;
213a2e1753bSAlexander Bulekov } MemReentrancyGuard;
214a2e1753bSAlexander Bulekov 
2156aebb1f6SAlex Bennée 
2166aebb1f6SAlex Bennée typedef QLIST_HEAD(, NamedGPIOList) NamedGPIOListHead;
2176aebb1f6SAlex Bennée typedef QLIST_HEAD(, NamedClockList) NamedClockListHead;
2186aebb1f6SAlex Bennée typedef QLIST_HEAD(, BusState) BusStateHead;
2196aebb1f6SAlex Bennée 
2207983c8a3SAndreas Färber /**
2216aebb1f6SAlex Bennée  * struct DeviceState - common device state, accessed with qdev helpers
2227983c8a3SAndreas Färber  *
2237983c8a3SAndreas Färber  * This structure should not be accessed directly.  We declare it here
2247983c8a3SAndreas Färber  * so that it can be embedded in individual device state structures.
2257983c8a3SAndreas Färber  */
226074a86fcSAnthony Liguori struct DeviceState {
2276aebb1f6SAlex Bennée     /* private: */
228074a86fcSAnthony Liguori     Object parent_obj;
2296aebb1f6SAlex Bennée     /* public: */
230074a86fcSAnthony Liguori 
2316aebb1f6SAlex Bennée     /**
2326aebb1f6SAlex Bennée      * @id: global device id
2336aebb1f6SAlex Bennée      */
234163f3847SKevin Wolf     char *id;
2356aebb1f6SAlex Bennée     /**
2366aebb1f6SAlex Bennée      * @canonical_path: canonical path of realized device in the QOM tree
2376aebb1f6SAlex Bennée      */
23804162f8fSMichael Roth     char *canonical_path;
2396aebb1f6SAlex Bennée     /**
2406aebb1f6SAlex Bennée      * @realized: has device been realized?
2416aebb1f6SAlex Bennée      */
2427983c8a3SAndreas Färber     bool realized;
2436aebb1f6SAlex Bennée     /**
2446aebb1f6SAlex Bennée      * @pending_deleted_event: track pending deletion events during unplug
2456aebb1f6SAlex Bennée      */
246352e8da7SPaolo Bonzini     bool pending_deleted_event;
2476aebb1f6SAlex Bennée     /**
2486aebb1f6SAlex Bennée      * @pending_deleted_expires_ms: optional timeout for deletion events
2496aebb1f6SAlex Bennée      */
25018416c62SGerd Hoffmann     int64_t pending_deleted_expires_ms;
2516aebb1f6SAlex Bennée     /**
2526aebb1f6SAlex Bennée      * @opts: QDict of options for the device
2536aebb1f6SAlex Bennée      */
254f3558b1bSKevin Wolf     QDict *opts;
2556aebb1f6SAlex Bennée     /**
2566aebb1f6SAlex Bennée      * @hotplugged: was device added after PHASE_MACHINE_READY?
2576aebb1f6SAlex Bennée      */
258074a86fcSAnthony Liguori     int hotplugged;
2596aebb1f6SAlex Bennée     /**
2606aebb1f6SAlex Bennée      * @allow_unplug_during_migration: can device be unplugged during migration
2616aebb1f6SAlex Bennée      */
262a1190ab6SJens Freimann     bool allow_unplug_during_migration;
2636aebb1f6SAlex Bennée     /**
2646aebb1f6SAlex Bennée      * @parent_bus: bus this device belongs to
2656aebb1f6SAlex Bennée      */
266074a86fcSAnthony Liguori     BusState *parent_bus;
2676aebb1f6SAlex Bennée     /**
2686aebb1f6SAlex Bennée      * @gpios: QLIST of named GPIOs the device provides.
2696aebb1f6SAlex Bennée      */
2706aebb1f6SAlex Bennée     NamedGPIOListHead gpios;
2716aebb1f6SAlex Bennée     /**
2726aebb1f6SAlex Bennée      * @clocks: QLIST of named clocks the device provides.
2736aebb1f6SAlex Bennée      */
2746aebb1f6SAlex Bennée     NamedClockListHead clocks;
2756aebb1f6SAlex Bennée     /**
2766aebb1f6SAlex Bennée      * @child_bus: QLIST of child buses
2776aebb1f6SAlex Bennée      */
2786aebb1f6SAlex Bennée     BusStateHead child_bus;
2796aebb1f6SAlex Bennée     /**
2806aebb1f6SAlex Bennée      * @num_child_bus: number of @child_bus entries
2816aebb1f6SAlex Bennée      */
282074a86fcSAnthony Liguori     int num_child_bus;
2836aebb1f6SAlex Bennée     /**
2846aebb1f6SAlex Bennée      * @instance_id_alias: device alias for handling legacy migration setups
2856aebb1f6SAlex Bennée      */
286074a86fcSAnthony Liguori     int instance_id_alias;
2876aebb1f6SAlex Bennée     /**
2886aebb1f6SAlex Bennée      * @alias_required_for_version: indicates @instance_id_alias is
2896aebb1f6SAlex Bennée      * needed for migration
2906aebb1f6SAlex Bennée      */
291074a86fcSAnthony Liguori     int alias_required_for_version;
2926aebb1f6SAlex Bennée     /**
2936aebb1f6SAlex Bennée      * @reset: ResettableState for the device; handled by Resettable interface.
2946aebb1f6SAlex Bennée      */
295c11256aaSDamien Hedde     ResettableState reset;
2966aebb1f6SAlex Bennée     /**
2976aebb1f6SAlex Bennée      * @unplug_blockers: list of reasons to block unplugging of device
2986aebb1f6SAlex Bennée      */
299217c7f01SJagannathan Raman     GSList *unplug_blockers;
3006aebb1f6SAlex Bennée     /**
3016aebb1f6SAlex Bennée      * @mem_reentrancy_guard: Is the device currently in mmio/pio/dma?
3026aebb1f6SAlex Bennée      *
3036aebb1f6SAlex Bennée      * Used to prevent re-entrancy confusing things.
3046aebb1f6SAlex Bennée      */
305a2e1753bSAlexander Bulekov     MemReentrancyGuard mem_reentrancy_guard;
306074a86fcSAnthony Liguori };
307074a86fcSAnthony Liguori 
308667cdad0SPaolo Bonzini typedef struct DeviceListener DeviceListener;
309707ff800SPaul Durrant struct DeviceListener {
310707ff800SPaul Durrant     void (*realize)(DeviceListener *listener, DeviceState *dev);
311707ff800SPaul Durrant     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
312f3a85056SJens Freimann     /*
313b91ad981SJuan Quintela      * This callback is called upon init of the DeviceState and
314b91ad981SJuan Quintela      * informs qdev if a device should be visible or hidden.  We can
315b91ad981SJuan Quintela      * hide a failover device depending for example on the device
316b91ad981SJuan Quintela      * opts.
3177d618082SKevin Wolf      *
3187d618082SKevin Wolf      * On errors, it returns false and errp is set. Device creation
3197d618082SKevin Wolf      * should fail in this case.
320f3a85056SJens Freimann      */
321f3558b1bSKevin Wolf     bool (*hide_device)(DeviceListener *listener, const QDict *device_opts,
322f3558b1bSKevin Wolf                         bool from_json, Error **errp);
323707ff800SPaul Durrant     QTAILQ_ENTRY(DeviceListener) link;
324707ff800SPaul Durrant };
325707ff800SPaul Durrant 
326074a86fcSAnthony Liguori #define TYPE_BUS "bus"
3278110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(BusState, BusClass,
3288110fa1dSEduardo Habkost                      BUS, TYPE_BUS)
329074a86fcSAnthony Liguori 
330074a86fcSAnthony Liguori struct BusClass {
331074a86fcSAnthony Liguori     ObjectClass parent_class;
332074a86fcSAnthony Liguori 
333074a86fcSAnthony Liguori     /* FIXME first arg should be BusState */
334074a86fcSAnthony Liguori     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
335074a86fcSAnthony Liguori     char *(*get_dev_path)(DeviceState *dev);
336bb755ba4SPaolo Bonzini 
337074a86fcSAnthony Liguori     /*
338074a86fcSAnthony Liguori      * This callback is used to create Open Firmware device path in accordance
339074a86fcSAnthony Liguori      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
340074a86fcSAnthony Liguori      * bindings can be found at http://playground.sun.com/1275/bindings/.
341074a86fcSAnthony Liguori      */
342074a86fcSAnthony Liguori     char *(*get_fw_dev_path)(DeviceState *dev);
343bb755ba4SPaolo Bonzini 
344bb755ba4SPaolo Bonzini     /*
345bb755ba4SPaolo Bonzini      * Return whether the device can be added to @bus,
346bb755ba4SPaolo Bonzini      * based on the address that was set (via device properties)
347bb755ba4SPaolo Bonzini      * before realize.  If not, on return @errp contains the
348bb755ba4SPaolo Bonzini      * human-readable error message.
349bb755ba4SPaolo Bonzini      */
350bb755ba4SPaolo Bonzini     bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
351bb755ba4SPaolo Bonzini 
35202e7f85dSBandan Das     BusRealize realize;
35302e7f85dSBandan Das     BusUnrealize unrealize;
35402e7f85dSBandan Das 
3551395af6fSKONRAD Frederic     /* maximum devices allowed on the bus, 0: no limit. */
3561395af6fSKONRAD Frederic     int max_dev;
35761de3676SAlexander Graf     /* number of automatically allocated bus ids (e.g. ide.0) */
35861de3676SAlexander Graf     int automatic_ids;
359074a86fcSAnthony Liguori };
360074a86fcSAnthony Liguori 
361074a86fcSAnthony Liguori typedef struct BusChild {
3622d24a646SMaxim Levitsky     struct rcu_head rcu;
363074a86fcSAnthony Liguori     DeviceState *child;
364074a86fcSAnthony Liguori     int index;
365074a86fcSAnthony Liguori     QTAILQ_ENTRY(BusChild) sibling;
366074a86fcSAnthony Liguori } BusChild;
367074a86fcSAnthony Liguori 
3680ee4de6cSIgor Mammedov #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
3690ee4de6cSIgor Mammedov 
3706aebb1f6SAlex Bennée typedef QTAILQ_HEAD(, BusChild) BusChildHead;
3716aebb1f6SAlex Bennée typedef QLIST_ENTRY(BusState) BusStateEntry;
3726aebb1f6SAlex Bennée 
373074a86fcSAnthony Liguori /**
3746aebb1f6SAlex Bennée  * struct BusState:
3756aebb1f6SAlex Bennée  * @obj: parent object
3766aebb1f6SAlex Bennée  * @parent: parent Device
3776aebb1f6SAlex Bennée  * @name: name of bus
37827c6ef1bSLi Qiang  * @hotplug_handler: link to a hotplug handler associated with bus.
3796aebb1f6SAlex Bennée  * @max_index: max number of child buses
3806aebb1f6SAlex Bennée  * @realized: is the bus itself realized?
3816aebb1f6SAlex Bennée  * @full: is the bus full?
3826aebb1f6SAlex Bennée  * @num_children: current number of child buses
383074a86fcSAnthony Liguori  */
384074a86fcSAnthony Liguori struct BusState {
3856aebb1f6SAlex Bennée     /* private: */
386074a86fcSAnthony Liguori     Object obj;
3876aebb1f6SAlex Bennée     /* public: */
388074a86fcSAnthony Liguori     DeviceState *parent;
389f73480c3SMarc-André Lureau     char *name;
3900ee4de6cSIgor Mammedov     HotplugHandler *hotplug_handler;
391074a86fcSAnthony Liguori     int max_index;
39202e7f85dSBandan Das     bool realized;
3931518562bSPeter Maydell     bool full;
39412b2e9f3STony Krowiak     int num_children;
3952d24a646SMaxim Levitsky 
3966aebb1f6SAlex Bennée     /**
3976aebb1f6SAlex Bennée      * @children: an RCU protected QTAILQ, thus readers must use RCU
3986aebb1f6SAlex Bennée      * to access it, and writers must hold the big qemu lock
3992d24a646SMaxim Levitsky      */
4006aebb1f6SAlex Bennée     BusChildHead children;
4016aebb1f6SAlex Bennée     /**
4026aebb1f6SAlex Bennée      * @sibling: next bus
4036aebb1f6SAlex Bennée      */
4046aebb1f6SAlex Bennée     BusStateEntry sibling;
4056aebb1f6SAlex Bennée     /**
4066aebb1f6SAlex Bennée      * @reset: ResettableState for the bus; handled by Resettable interface.
4076aebb1f6SAlex Bennée      */
408c11256aaSDamien Hedde     ResettableState reset;
409074a86fcSAnthony Liguori };
410074a86fcSAnthony Liguori 
4115cc56cc6SPeter Maydell /**
4126aebb1f6SAlex Bennée  * typedef GlobalProperty - a global property type
4136aebb1f6SAlex Bennée  *
414b3ce84feSEduardo Habkost  * @used: Set to true if property was used when initializing a device.
41592fd453cSDr. David Alan Gilbert  * @optional: If set to true, GlobalProperty will be skipped without errors
41692fd453cSDr. David Alan Gilbert  *            if the property doesn't exist.
417cff8b715SMarc-André Lureau  *
418cff8b715SMarc-André Lureau  * An error is fatal for non-hotplugged devices, when the global is applied.
4199f9260a3SDon Slutz  */
420074a86fcSAnthony Liguori typedef struct GlobalProperty {
421074a86fcSAnthony Liguori     const char *driver;
422074a86fcSAnthony Liguori     const char *property;
423074a86fcSAnthony Liguori     const char *value;
424b3ce84feSEduardo Habkost     bool used;
42592fd453cSDr. David Alan Gilbert     bool optional;
426074a86fcSAnthony Liguori } GlobalProperty;
427074a86fcSAnthony Liguori 
428ea9ce893SMarc-André Lureau static inline void
429ea9ce893SMarc-André Lureau compat_props_add(GPtrArray *arr,
430ea9ce893SMarc-André Lureau                  GlobalProperty props[], size_t nelem)
431ea9ce893SMarc-André Lureau {
432ea9ce893SMarc-André Lureau     int i;
433ea9ce893SMarc-André Lureau     for (i = 0; i < nelem; i++) {
434ea9ce893SMarc-André Lureau         g_ptr_array_add(arr, (void *)&props[i]);
435ea9ce893SMarc-André Lureau     }
436ea9ce893SMarc-André Lureau }
437ea9ce893SMarc-André Lureau 
438074a86fcSAnthony Liguori /*** Board API.  This should go away once we have a machine config file.  ***/
439074a86fcSAnthony Liguori 
440b51238e2SPeter Maydell /**
441b51238e2SPeter Maydell  * qdev_new: Create a device on the heap
442b51238e2SPeter Maydell  * @name: device type to create (we assert() that this type exists)
443b51238e2SPeter Maydell  *
444b51238e2SPeter Maydell  * This only allocates the memory and initializes the device state
445b51238e2SPeter Maydell  * structure, ready for the caller to set properties if they wish.
446b51238e2SPeter Maydell  * The device still needs to be realized.
4476aebb1f6SAlex Bennée  *
4486aebb1f6SAlex Bennée  * Return: a derived DeviceState object with a reference count of 1.
449b51238e2SPeter Maydell  */
4509940b2cfSMarkus Armbruster DeviceState *qdev_new(const char *name);
451694804edSPhilippe Mathieu-Daudé 
452b51238e2SPeter Maydell /**
453b51238e2SPeter Maydell  * qdev_try_new: Try to create a device on the heap
454b51238e2SPeter Maydell  * @name: device type to create
455b51238e2SPeter Maydell  *
456b51238e2SPeter Maydell  * This is like qdev_new(), except it returns %NULL when type @name
457b51238e2SPeter Maydell  * does not exist, rather than asserting.
4586aebb1f6SAlex Bennée  *
4596aebb1f6SAlex Bennée  * Return: a derived DeviceState object with a reference count of 1 or
4606aebb1f6SAlex Bennée  * NULL if type @name does not exist.
461b51238e2SPeter Maydell  */
4629940b2cfSMarkus Armbruster DeviceState *qdev_try_new(const char *name);
463694804edSPhilippe Mathieu-Daudé 
464b51238e2SPeter Maydell /**
4656aebb1f6SAlex Bennée  * qdev_is_realized() - check if device is realized
46626462a70SStefan Hajnoczi  * @dev: The device to check.
46726462a70SStefan Hajnoczi  *
4686aebb1f6SAlex Bennée  * Context: May be called outside big qemu lock.
4696aebb1f6SAlex Bennée  * Return: true if the device has been fully constructed, false otherwise.
47026462a70SStefan Hajnoczi  */
47126462a70SStefan Hajnoczi static inline bool qdev_is_realized(DeviceState *dev)
47226462a70SStefan Hajnoczi {
47326462a70SStefan Hajnoczi     return qatomic_load_acquire(&dev->realized);
47426462a70SStefan Hajnoczi }
47526462a70SStefan Hajnoczi 
47626462a70SStefan Hajnoczi /**
477b51238e2SPeter Maydell  * qdev_realize: Realize @dev.
478b51238e2SPeter Maydell  * @dev: device to realize
479b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
480b51238e2SPeter Maydell  * @errp: pointer to error object
481b51238e2SPeter Maydell  *
482b51238e2SPeter Maydell  * "Realize" the device, i.e. perform the second phase of device
483b51238e2SPeter Maydell  * initialization.
484b51238e2SPeter Maydell  * @dev must not be plugged into a bus already.
485b51238e2SPeter Maydell  * If @bus, plug @dev into @bus.  This takes a reference to @dev.
486b51238e2SPeter Maydell  * If @dev has no QOM parent, make one up, taking another reference.
487b51238e2SPeter Maydell  *
488b51238e2SPeter Maydell  * If you created @dev using qdev_new(), you probably want to use
489b51238e2SPeter Maydell  * qdev_realize_and_unref() instead.
4906aebb1f6SAlex Bennée  *
4916aebb1f6SAlex Bennée  * Return: true on success, else false setting @errp with error
492b51238e2SPeter Maydell  */
4939940b2cfSMarkus Armbruster bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
494694804edSPhilippe Mathieu-Daudé 
495b51238e2SPeter Maydell /**
496b51238e2SPeter Maydell  * qdev_realize_and_unref: Realize @dev and drop a reference
497b51238e2SPeter Maydell  * @dev: device to realize
498b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
499b51238e2SPeter Maydell  * @errp: pointer to error object
500b51238e2SPeter Maydell  *
501b51238e2SPeter Maydell  * Realize @dev and drop a reference.
502b51238e2SPeter Maydell  * This is like qdev_realize(), except the caller must hold a
503b51238e2SPeter Maydell  * (private) reference, which is dropped on return regardless of
504b51238e2SPeter Maydell  * success or failure.  Intended use::
505b51238e2SPeter Maydell  *
506b51238e2SPeter Maydell  *     dev = qdev_new();
507b51238e2SPeter Maydell  *     [...]
508b51238e2SPeter Maydell  *     qdev_realize_and_unref(dev, bus, errp);
509b51238e2SPeter Maydell  *
510b51238e2SPeter Maydell  * Now @dev can go away without further ado.
511b51238e2SPeter Maydell  *
512b51238e2SPeter Maydell  * If you are embedding the device into some other QOM device and
513b51238e2SPeter Maydell  * initialized it via some variant on object_initialize_child() then
514b51238e2SPeter Maydell  * do not use this function, because that family of functions arrange
515b51238e2SPeter Maydell  * for the only reference to the child device to be held by the parent
516b51238e2SPeter Maydell  * via the child<> property, and so the reference-count-drop done here
517b51238e2SPeter Maydell  * would be incorrect. For that use case you want qdev_realize().
5186aebb1f6SAlex Bennée  *
5196aebb1f6SAlex Bennée  * Return: true on success, else false setting @errp with error
520b51238e2SPeter Maydell  */
5219940b2cfSMarkus Armbruster bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
522694804edSPhilippe Mathieu-Daudé 
52346ea1be1SPeter Maydell /**
52446ea1be1SPeter Maydell  * qdev_unrealize: Unrealize a device
52546ea1be1SPeter Maydell  * @dev: device to unrealize
52646ea1be1SPeter Maydell  *
52746ea1be1SPeter Maydell  * This function will "unrealize" a device, which is the first phase
52846ea1be1SPeter Maydell  * of correctly destroying a device that has been realized. It will:
52946ea1be1SPeter Maydell  *
53046ea1be1SPeter Maydell  *  - unrealize any child buses by calling qbus_unrealize()
53146ea1be1SPeter Maydell  *    (this will recursively unrealize any devices on those buses)
5327a21bee2SDaniel P. Berrangé  *  - call the unrealize method of @dev
53346ea1be1SPeter Maydell  *
53446ea1be1SPeter Maydell  * The device can then be freed by causing its reference count to go
53546ea1be1SPeter Maydell  * to zero.
53646ea1be1SPeter Maydell  *
53746ea1be1SPeter Maydell  * Warning: most devices in QEMU do not expect to be unrealized.  Only
53846ea1be1SPeter Maydell  * devices which are hot-unpluggable should be unrealized (as part of
53946ea1be1SPeter Maydell  * the unplugging process); all other devices are expected to last for
54046ea1be1SPeter Maydell  * the life of the simulation and should not be unrealized and freed.
54146ea1be1SPeter Maydell  */
5429940b2cfSMarkus Armbruster void qdev_unrealize(DeviceState *dev);
543074a86fcSAnthony Liguori void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
544074a86fcSAnthony Liguori                                  int required_for_version);
54514405c27SDavid Hildenbrand HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
54603fcbd9dSThomas Huth HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
547d2321d31SPeter Xu bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
5486aebb1f6SAlex Bennée 
54917cc0128SIgor Mammedov /**
5506aebb1f6SAlex Bennée  * qdev_get_hotplug_handler() - Get handler responsible for device wiring
5516aebb1f6SAlex Bennée  * @dev: the device we want the HOTPLUG_HANDLER for.
55217cc0128SIgor Mammedov  *
55317cc0128SIgor Mammedov  * Note: in case @dev has a parent bus, it will be returned as handler unless
55417cc0128SIgor Mammedov  * machine handler overrides it.
55517cc0128SIgor Mammedov  *
5566aebb1f6SAlex Bennée  * Return: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
55717cc0128SIgor Mammedov  * or NULL if there aren't any.
55817cc0128SIgor Mammedov  */
559c06b2ffbSZhu Guihua HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
560074a86fcSAnthony Liguori void qdev_unplug(DeviceState *dev, Error **errp);
5613f98408eSVladimir Sementsov-Ogievskiy int qdev_sync_config(DeviceState *dev, Error **errp);
562014176f9SIgor Mammedov void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
563014176f9SIgor Mammedov                                   DeviceState *dev, Error **errp);
564074a86fcSAnthony Liguori void qdev_machine_creation_done(void);
565074a86fcSAnthony Liguori bool qdev_machine_modified(void);
566074a86fcSAnthony Liguori 
567cd07d7f9SPeter Maydell /**
568217c7f01SJagannathan Raman  * qdev_add_unplug_blocker: Add an unplug blocker to a device
569217c7f01SJagannathan Raman  *
570217c7f01SJagannathan Raman  * @dev: Device to be blocked from unplug
571217c7f01SJagannathan Raman  * @reason: Reason for blocking
572217c7f01SJagannathan Raman  */
573217c7f01SJagannathan Raman void qdev_add_unplug_blocker(DeviceState *dev, Error *reason);
574217c7f01SJagannathan Raman 
575217c7f01SJagannathan Raman /**
576217c7f01SJagannathan Raman  * qdev_del_unplug_blocker: Remove an unplug blocker from a device
577217c7f01SJagannathan Raman  *
578217c7f01SJagannathan Raman  * @dev: Device to be unblocked
579217c7f01SJagannathan Raman  * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
580217c7f01SJagannathan Raman  *          Used as a handle to lookup the blocker for deletion.
581217c7f01SJagannathan Raman  */
582217c7f01SJagannathan Raman void qdev_del_unplug_blocker(DeviceState *dev, Error *reason);
583217c7f01SJagannathan Raman 
584217c7f01SJagannathan Raman /**
585217c7f01SJagannathan Raman  * qdev_unplug_blocked: Confirm if a device is blocked from unplug
586217c7f01SJagannathan Raman  *
587217c7f01SJagannathan Raman  * @dev: Device to be tested
5886aebb1f6SAlex Bennée  * @errp: The reasons why the device is blocked, if any
589217c7f01SJagannathan Raman  *
5906aebb1f6SAlex Bennée  * Returns: true (also setting @errp) if device is blocked from unplug,
5916aebb1f6SAlex Bennée  * false otherwise
592217c7f01SJagannathan Raman  */
593217c7f01SJagannathan Raman bool qdev_unplug_blocked(DeviceState *dev, Error **errp);
594217c7f01SJagannathan Raman 
595217c7f01SJagannathan Raman /**
5966aebb1f6SAlex Bennée  * typedef GpioPolarity - Polarity of a GPIO line
597ddb67f64SPhilippe Mathieu-Daudé  *
598ddb67f64SPhilippe Mathieu-Daudé  * GPIO lines use either positive (active-high) logic,
599ddb67f64SPhilippe Mathieu-Daudé  * or negative (active-low) logic.
600ddb67f64SPhilippe Mathieu-Daudé  *
601ddb67f64SPhilippe Mathieu-Daudé  * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
602ddb67f64SPhilippe Mathieu-Daudé  * active when the voltage on the pin is high (relative to ground);
603ddb67f64SPhilippe Mathieu-Daudé  * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
604ddb67f64SPhilippe Mathieu-Daudé  * is active when the voltage on the pin is low (or grounded).
605ddb67f64SPhilippe Mathieu-Daudé  */
606ddb67f64SPhilippe Mathieu-Daudé typedef enum {
607ddb67f64SPhilippe Mathieu-Daudé     GPIO_POLARITY_ACTIVE_LOW,
608ddb67f64SPhilippe Mathieu-Daudé     GPIO_POLARITY_ACTIVE_HIGH
609ddb67f64SPhilippe Mathieu-Daudé } GpioPolarity;
610ddb67f64SPhilippe Mathieu-Daudé 
611ddb67f64SPhilippe Mathieu-Daudé /**
612cd07d7f9SPeter Maydell  * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
613cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
614cd07d7f9SPeter Maydell  * @n: Number of the anonymous GPIO line (which must be in range)
615cd07d7f9SPeter Maydell  *
616cd07d7f9SPeter Maydell  * Returns the qemu_irq corresponding to an anonymous input GPIO line
617cd07d7f9SPeter Maydell  * (which the device has set up with qdev_init_gpio_in()). The index
618cd07d7f9SPeter Maydell  * @n of the GPIO line must be valid (i.e. be at least 0 and less than
619cd07d7f9SPeter Maydell  * the total number of anonymous input GPIOs the device has); this
620cd07d7f9SPeter Maydell  * function will assert() if passed an invalid index.
621cd07d7f9SPeter Maydell  *
622cd07d7f9SPeter Maydell  * This function is intended to be used by board code or SoC "container"
623cd07d7f9SPeter Maydell  * device models to wire up the GPIO lines; usually the return value
624cd07d7f9SPeter Maydell  * will be passed to qdev_connect_gpio_out() or a similar function to
625cd07d7f9SPeter Maydell  * connect another device's output GPIO line to this input.
626cd07d7f9SPeter Maydell  *
627cd07d7f9SPeter Maydell  * For named input GPIO lines, use qdev_get_gpio_in_named().
6286aebb1f6SAlex Bennée  *
6296aebb1f6SAlex Bennée  * Return: qemu_irq corresponding to anonymous input GPIO line
630cd07d7f9SPeter Maydell  */
631074a86fcSAnthony Liguori qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
632694804edSPhilippe Mathieu-Daudé 
633cd07d7f9SPeter Maydell /**
634cd07d7f9SPeter Maydell  * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
635cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
636cd07d7f9SPeter Maydell  * @name: Name of the input GPIO array
637cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in that array (which must be in range)
638cd07d7f9SPeter Maydell  *
6391ee5f645SPeter Maydell  * Returns the qemu_irq corresponding to a single input GPIO line
6401ee5f645SPeter Maydell  * in a named array of input GPIO lines on a device (which the device
6411ee5f645SPeter Maydell  * has set up with qdev_init_gpio_in_named()).
642cd07d7f9SPeter Maydell  * The @name string must correspond to an input GPIO array which exists on
643cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
644cd07d7f9SPeter Maydell  * be at least 0 and less than the total number of input GPIOs in that
645cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
646cd07d7f9SPeter Maydell  *
647cd07d7f9SPeter Maydell  * For anonymous input GPIO lines, use qdev_get_gpio_in().
6486aebb1f6SAlex Bennée  *
6496aebb1f6SAlex Bennée  * Return: qemu_irq corresponding to named input GPIO line
650cd07d7f9SPeter Maydell  */
651a5f54290SPeter Crosthwaite qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
652a5f54290SPeter Crosthwaite 
653cd07d7f9SPeter Maydell /**
654cd07d7f9SPeter Maydell  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
655cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
656cd07d7f9SPeter Maydell  * @n: Number of the anonymous output GPIO line (which must be in range)
6576aebb1f6SAlex Bennée  * @pin: qemu_irq to connect the output line to
658cd07d7f9SPeter Maydell  *
659cd07d7f9SPeter Maydell  * This function connects an anonymous output GPIO line on a device
660cd07d7f9SPeter Maydell  * up to an arbitrary qemu_irq, so that when the device asserts that
661cd07d7f9SPeter Maydell  * output GPIO line, the qemu_irq's callback is invoked.
662cd07d7f9SPeter Maydell  * The index @n of the GPIO line must be valid (i.e. be at least 0 and
663cd07d7f9SPeter Maydell  * less than the total number of anonymous output GPIOs the device has
664cd07d7f9SPeter Maydell  * created with qdev_init_gpio_out()); otherwise this function will assert().
665cd07d7f9SPeter Maydell  *
666cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
667cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
668cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
669cd07d7f9SPeter Maydell  *
670cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
671cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
672cd07d7f9SPeter Maydell  * same qemu_irq. (Warning: there is no assertion or other guard to
673cd07d7f9SPeter Maydell  * catch this error: the model will just not do the right thing.)
6745df69ab8SPeter Maydell  * Instead, for fan-out you can use the TYPE_SPLIT_IRQ device: connect
675cd07d7f9SPeter Maydell  * a device's outbound GPIO to the splitter's input, and connect each
676cd07d7f9SPeter Maydell  * of the splitter's outputs to a different device.  For fan-in you
677cd07d7f9SPeter Maydell  * can use the TYPE_OR_IRQ device, which is a model of a logical OR
678cd07d7f9SPeter Maydell  * gate with multiple inputs and one output.
679cd07d7f9SPeter Maydell  *
680cd07d7f9SPeter Maydell  * For named output GPIO lines, use qdev_connect_gpio_out_named().
681cd07d7f9SPeter Maydell  */
682074a86fcSAnthony Liguori void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
683694804edSPhilippe Mathieu-Daudé 
684cd07d7f9SPeter Maydell /**
6851fbd004bSPhilippe Mathieu-Daudé  * qdev_connect_gpio_out_named: Connect one of a device's named output
6861fbd004bSPhilippe Mathieu-Daudé  *                              GPIO lines
687cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
688cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
6891ee5f645SPeter Maydell  * @n: Number of the output GPIO line within that array (which must be in range)
6902ebd9ce1SPhilippe Mathieu-Daudé  * @input_pin: qemu_irq to connect the output line to
691cd07d7f9SPeter Maydell  *
6921ee5f645SPeter Maydell  * This function connects a single GPIO output in a named array of output
6931ee5f645SPeter Maydell  * GPIO lines on a device up to an arbitrary qemu_irq, so that when the
6941ee5f645SPeter Maydell  * device asserts that output GPIO line, the qemu_irq's callback is invoked.
695cd07d7f9SPeter Maydell  * The @name string must correspond to an output GPIO array which exists on
696cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
6971ee5f645SPeter Maydell  * be at least 0 and less than the total number of output GPIOs in that
698cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
699cd07d7f9SPeter Maydell  *
700cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
701cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
702cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
703cd07d7f9SPeter Maydell  *
704cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
705cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
706cd07d7f9SPeter Maydell  * same qemu_irq; see qdev_connect_gpio_out() for details.
707cd07d7f9SPeter Maydell  *
7081fbd004bSPhilippe Mathieu-Daudé  * For anonymous output GPIO lines, use qdev_connect_gpio_out().
709cd07d7f9SPeter Maydell  */
710a5f54290SPeter Crosthwaite void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
7112ebd9ce1SPhilippe Mathieu-Daudé                                  qemu_irq input_pin);
712694804edSPhilippe Mathieu-Daudé 
713cd07d7f9SPeter Maydell /**
714cd07d7f9SPeter Maydell  * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
715cd07d7f9SPeter Maydell  * @dev: Device whose output GPIO we are interested in
716cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
717cd07d7f9SPeter Maydell  * @n: Number of the output GPIO line within that array
718cd07d7f9SPeter Maydell  *
719cd07d7f9SPeter Maydell  * Returns whatever qemu_irq is currently connected to the specified
720cd07d7f9SPeter Maydell  * output GPIO line of @dev. This will be NULL if the output GPIO line
721cd07d7f9SPeter Maydell  * has never been wired up to the anything.  Note that the qemu_irq
722cd07d7f9SPeter Maydell  * returned does not belong to @dev -- it will be the input GPIO or
723cd07d7f9SPeter Maydell  * IRQ of whichever device the board code has connected up to @dev's
724cd07d7f9SPeter Maydell  * output GPIO.
725cd07d7f9SPeter Maydell  *
726cd07d7f9SPeter Maydell  * You probably don't need to use this function -- it is used only
727cd07d7f9SPeter Maydell  * by the platform-bus subsystem.
7286aebb1f6SAlex Bennée  *
7296aebb1f6SAlex Bennée  * Return: qemu_irq associated with GPIO or NULL if un-wired.
730cd07d7f9SPeter Maydell  */
731b7973186SAlexander Graf qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
732694804edSPhilippe Mathieu-Daudé 
733cd07d7f9SPeter Maydell /**
734cd07d7f9SPeter Maydell  * qdev_intercept_gpio_out: Intercept an existing GPIO connection
735cd07d7f9SPeter Maydell  * @dev: Device to intercept the outbound GPIO line from
736cd07d7f9SPeter Maydell  * @icpt: New qemu_irq to connect instead
737cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
738cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in the array
739cd07d7f9SPeter Maydell  *
7406aebb1f6SAlex Bennée  * .. note::
741cd07d7f9SPeter Maydell  *   This function is provided only for use by the qtest testing framework
742cd07d7f9SPeter Maydell  *   and is not suitable for use in non-testing parts of QEMU.
743cd07d7f9SPeter Maydell  *
744cd07d7f9SPeter Maydell  * This function breaks an existing connection of an outbound GPIO
745cd07d7f9SPeter Maydell  * line from @dev, and replaces it with the new qemu_irq @icpt, as if
746cd07d7f9SPeter Maydell  * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
747cd07d7f9SPeter Maydell  * The previously connected qemu_irq is returned, so it can be restored
748cd07d7f9SPeter Maydell  * by a second call to qdev_intercept_gpio_out() if desired.
7496aebb1f6SAlex Bennée  *
7506aebb1f6SAlex Bennée  * Return: old disconnected qemu_irq if one existed
751cd07d7f9SPeter Maydell  */
7520c24db2bSPeter Crosthwaite qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
7530c24db2bSPeter Crosthwaite                                  const char *name, int n);
754074a86fcSAnthony Liguori 
755074a86fcSAnthony Liguori BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
756074a86fcSAnthony Liguori 
757074a86fcSAnthony Liguori /*** Device API.  ***/
758074a86fcSAnthony Liguori 
759cd07d7f9SPeter Maydell /**
760cd07d7f9SPeter Maydell  * qdev_init_gpio_in: create an array of anonymous input GPIO lines
761cd07d7f9SPeter Maydell  * @dev: Device to create input GPIOs for
762cd07d7f9SPeter Maydell  * @handler: Function to call when GPIO line value is set
763cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
764cd07d7f9SPeter Maydell  *
765cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_in* family in
766cd07d7f9SPeter Maydell  * their instance_init or realize methods to create any input GPIO
767cd07d7f9SPeter Maydell  * lines they need. There is no functional difference between
768cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
769cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
770cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO input whose purpose is obvious.
771cd07d7f9SPeter Maydell  * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
772cd07d7f9SPeter Maydell  *
773cd07d7f9SPeter Maydell  * See qdev_get_gpio_in() for how code that uses such a device can get
774cd07d7f9SPeter Maydell  * hold of an input GPIO line to manipulate it.
775cd07d7f9SPeter Maydell  */
776074a86fcSAnthony Liguori void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
777694804edSPhilippe Mathieu-Daudé 
778cd07d7f9SPeter Maydell /**
779cd07d7f9SPeter Maydell  * qdev_init_gpio_out: create an array of anonymous output GPIO lines
780cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
781cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
782cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
783cd07d7f9SPeter Maydell  *
784cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_out* family
785cd07d7f9SPeter Maydell  * in their instance_init or realize methods to create any output
786cd07d7f9SPeter Maydell  * GPIO lines they need. There is no functional difference between
787cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
788cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
789cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO output whose purpose is obvious.
790cd07d7f9SPeter Maydell  *
791cd07d7f9SPeter Maydell  * The @pins argument should be a pointer to either a "qemu_irq"
792cd07d7f9SPeter Maydell  * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
793cd07d7f9SPeter Maydell  * state structure. The device implementation can then raise and
794cd07d7f9SPeter Maydell  * lower the GPIO line by calling qemu_set_irq(). (If anything is
795cd07d7f9SPeter Maydell  * connected to the other end of the GPIO this will cause the handler
796cd07d7f9SPeter Maydell  * function for that input GPIO to be called.)
797cd07d7f9SPeter Maydell  *
798cd07d7f9SPeter Maydell  * See qdev_connect_gpio_out() for how code that uses such a device
799cd07d7f9SPeter Maydell  * can connect to one of its output GPIO lines.
800526dc840SPhilippe Mathieu-Daudé  *
801526dc840SPhilippe Mathieu-Daudé  * There is no need to release the @pins allocated array because it
802526dc840SPhilippe Mathieu-Daudé  * will be automatically released when @dev calls its instance_finalize()
803526dc840SPhilippe Mathieu-Daudé  * handler.
804cd07d7f9SPeter Maydell  */
805074a86fcSAnthony Liguori void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
806694804edSPhilippe Mathieu-Daudé 
807cd07d7f9SPeter Maydell /**
80814b0375bSPhilippe Mathieu-Daudé  * qdev_init_gpio_out_named: create an array of named output GPIO lines
809cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
810cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
811cd07d7f9SPeter Maydell  * @name: Name to give this array of GPIO lines
8121ee5f645SPeter Maydell  * @n: Number of GPIO lines to create in this array
813cd07d7f9SPeter Maydell  *
814cd07d7f9SPeter Maydell  * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
815cd07d7f9SPeter Maydell  * with a name. Code using the device can then connect these GPIO lines
816cd07d7f9SPeter Maydell  * using qdev_connect_gpio_out_named().
817cd07d7f9SPeter Maydell  */
818a5f54290SPeter Crosthwaite void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
819a5f54290SPeter Crosthwaite                               const char *name, int n);
820694804edSPhilippe Mathieu-Daudé 
8214a151677SPeter Maydell /**
8226aebb1f6SAlex Bennée  * qdev_init_gpio_in_named_with_opaque() - create an array of input GPIO lines
8234a151677SPeter Maydell  * @dev: Device to create input GPIOs for
8244a151677SPeter Maydell  * @handler: Function to call when GPIO line value is set
8254a151677SPeter Maydell  * @opaque: Opaque data pointer to pass to @handler
8264a151677SPeter Maydell  * @name: Name of the GPIO input (must be unique for this device)
8274a151677SPeter Maydell  * @n: Number of GPIO lines in this input set
8284a151677SPeter Maydell  */
8294a151677SPeter Maydell void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
8304a151677SPeter Maydell                                          qemu_irq_handler handler,
8314a151677SPeter Maydell                                          void *opaque,
8324a151677SPeter Maydell                                          const char *name, int n);
8334a151677SPeter Maydell 
8344a151677SPeter Maydell /**
8356aebb1f6SAlex Bennée  * qdev_init_gpio_in_named() - create an array of input GPIO lines
8366aebb1f6SAlex Bennée  * @dev: device to add array to
8376aebb1f6SAlex Bennée  * @handler: a &typedef qemu_irq_handler function to call when GPIO is set
8386aebb1f6SAlex Bennée  * @name: Name of the GPIO input (must be unique for this device)
8396aebb1f6SAlex Bennée  * @n: Number of GPIO lines in this input set
8404a151677SPeter Maydell  *
8414a151677SPeter Maydell  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
8424a151677SPeter Maydell  * passed to the handler is @dev (which is the most commonly desired behaviour).
8434a151677SPeter Maydell  */
8444a151677SPeter Maydell static inline void qdev_init_gpio_in_named(DeviceState *dev,
8454a151677SPeter Maydell                                            qemu_irq_handler handler,
8464a151677SPeter Maydell                                            const char *name, int n)
8474a151677SPeter Maydell {
8484a151677SPeter Maydell     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
8494a151677SPeter Maydell }
850074a86fcSAnthony Liguori 
851cd07d7f9SPeter Maydell /**
852cd07d7f9SPeter Maydell  * qdev_pass_gpios: create GPIO lines on container which pass through to device
853cd07d7f9SPeter Maydell  * @dev: Device which has GPIO lines
854cd07d7f9SPeter Maydell  * @container: Container device which needs to expose them
855cd07d7f9SPeter Maydell  * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
856cd07d7f9SPeter Maydell  *
857cd07d7f9SPeter Maydell  * In QEMU, complicated devices like SoCs are often modelled with a
858cd07d7f9SPeter Maydell  * "container" QOM device which itself contains other QOM devices and
859cd07d7f9SPeter Maydell  * which wires them up appropriately. This function allows the container
860cd07d7f9SPeter Maydell  * to create GPIO arrays on itself which simply pass through to a GPIO
861cd07d7f9SPeter Maydell  * array of one of its internal devices.
862cd07d7f9SPeter Maydell  *
863cd07d7f9SPeter Maydell  * If @dev has both input and output GPIOs named @name then both will
864cd07d7f9SPeter Maydell  * be passed through. It is not possible to pass a subset of the array
865cd07d7f9SPeter Maydell  * with this function.
866cd07d7f9SPeter Maydell  *
867cd07d7f9SPeter Maydell  * To users of the container device, the GPIO array created on @container
868cd07d7f9SPeter Maydell  * behaves exactly like any other.
869cd07d7f9SPeter Maydell  */
87017a96a14SPeter Crosthwaite void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
87117a96a14SPeter Crosthwaite                      const char *name);
87217a96a14SPeter Crosthwaite 
8732d2f2507SPhilippe Mathieu-Daudé BusState *qdev_get_parent_bus(const DeviceState *dev);
874074a86fcSAnthony Liguori 
875074a86fcSAnthony Liguori /*** BUS API. ***/
876074a86fcSAnthony Liguori 
877074a86fcSAnthony Liguori DeviceState *qdev_find_recursive(BusState *bus, const char *id);
878074a86fcSAnthony Liguori 
879074a86fcSAnthony Liguori /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
880074a86fcSAnthony Liguori typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
881074a86fcSAnthony Liguori typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
882074a86fcSAnthony Liguori 
883d637e1dcSPeter Maydell void qbus_init(void *bus, size_t size, const char *typename,
884074a86fcSAnthony Liguori                DeviceState *parent, const char *name);
8859388d170SPeter Maydell BusState *qbus_new(const char *typename, DeviceState *parent, const char *name);
8869940b2cfSMarkus Armbruster bool qbus_realize(BusState *bus, Error **errp);
8879940b2cfSMarkus Armbruster void qbus_unrealize(BusState *bus);
8889940b2cfSMarkus Armbruster 
889074a86fcSAnthony Liguori /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
890074a86fcSAnthony Liguori  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
891074a86fcSAnthony Liguori  *           0 otherwise. */
8920293214bSPaolo Bonzini int qbus_walk_children(BusState *bus,
8930293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
8940293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
8950293214bSPaolo Bonzini                        void *opaque);
8960293214bSPaolo Bonzini int qdev_walk_children(DeviceState *dev,
8970293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
8980293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
8990293214bSPaolo Bonzini                        void *opaque);
9000293214bSPaolo Bonzini 
901abb89dbfSDamien Hedde /**
9026aebb1f6SAlex Bennée  * device_cold_reset() - perform a recursive cold reset on a device
9036aebb1f6SAlex Bennée  * @dev: device to reset.
9046aebb1f6SAlex Bennée  *
905abb89dbfSDamien Hedde  * Reset device @dev and perform a recursive processing using the resettable
906abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
907abb89dbfSDamien Hedde  */
908abb89dbfSDamien Hedde void device_cold_reset(DeviceState *dev);
909abb89dbfSDamien Hedde 
910abb89dbfSDamien Hedde /**
9116aebb1f6SAlex Bennée  * bus_cold_reset() - perform a recursive cold reset on a bus
9126aebb1f6SAlex Bennée  * @bus: bus to reset
913abb89dbfSDamien Hedde  *
914abb89dbfSDamien Hedde  * Reset bus @bus and perform a recursive processing using the resettable
915abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
916abb89dbfSDamien Hedde  */
917abb89dbfSDamien Hedde void bus_cold_reset(BusState *bus);
918abb89dbfSDamien Hedde 
919abb89dbfSDamien Hedde /**
9206aebb1f6SAlex Bennée  * device_is_in_reset() - check device reset state
9216aebb1f6SAlex Bennée  * @dev: device to check
9226aebb1f6SAlex Bennée  *
9236aebb1f6SAlex Bennée  * Return: true if the device @dev is currently being reset.
924c11256aaSDamien Hedde  */
925c11256aaSDamien Hedde bool device_is_in_reset(DeviceState *dev);
926c11256aaSDamien Hedde 
927c11256aaSDamien Hedde /**
9286aebb1f6SAlex Bennée  * bus_is_in_reset() - check bus reset state
9296aebb1f6SAlex Bennée  * @bus: bus to check
9306aebb1f6SAlex Bennée  *
9316aebb1f6SAlex Bennée  * Return: true if the bus @bus is currently being reset.
932c11256aaSDamien Hedde  */
933c11256aaSDamien Hedde bool bus_is_in_reset(BusState *bus);
934c11256aaSDamien Hedde 
935074a86fcSAnthony Liguori /* This should go away once we get rid of the NULL bus hack */
936074a86fcSAnthony Liguori BusState *sysbus_get_default(void);
937074a86fcSAnthony Liguori 
938074a86fcSAnthony Liguori char *qdev_get_fw_dev_path(DeviceState *dev);
9390be63901SGonglei char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
940074a86fcSAnthony Liguori 
941e57fc3deSAlex Bennée /**
942e57fc3deSAlex Bennée  * device_class_set_props(): add a set of properties to an device
943e57fc3deSAlex Bennée  * @dc: the parent DeviceClass all devices inherit
944e57fc3deSAlex Bennée  * @props: an array of properties, terminate by DEFINE_PROP_END_OF_LIST()
945e57fc3deSAlex Bennée  *
946e57fc3deSAlex Bennée  * This will add a set of properties to the object. It will fault if
947e57fc3deSAlex Bennée  * you attempt to add an existing property defined by a parent class.
948e57fc3deSAlex Bennée  * To modify an inherited property you need to use????
94958861197SRichard Henderson  *
95058861197SRichard Henderson  * Validate that @props has at least one Property plus the terminator.
951*1088d417SRichard Henderson  * Validate that @props is an array, not a pointer, via ARRAY_SIZE.
95258861197SRichard Henderson  * Validate that the array is terminated at compile-time (with -O2),
95358861197SRichard Henderson  * which requires the array to be const.
954e57fc3deSAlex Bennée  */
95558861197SRichard Henderson #define device_class_set_props(dc, props) \
95658861197SRichard Henderson     do {                                                                \
957*1088d417SRichard Henderson         QEMU_BUILD_BUG_ON(sizeof(props) == 0);                          \
958*1088d417SRichard Henderson         size_t props_count_ = ARRAY_SIZE(props) - 1;                    \
95958861197SRichard Henderson         if ((props)[props_count_].name != NULL) {                       \
96058861197SRichard Henderson             qemu_build_not_reached();                                   \
96158861197SRichard Henderson         }                                                               \
962*1088d417SRichard Henderson         device_class_set_props_n((dc), (props), props_count_);          \
96358861197SRichard Henderson     } while (0)
96458861197SRichard Henderson 
965c11256aaSDamien Hedde /**
966cb9f4b28SRichard Henderson  * device_class_set_props_n(): add a set of properties to an device
967cb9f4b28SRichard Henderson  * @dc: the parent DeviceClass all devices inherit
968cb9f4b28SRichard Henderson  * @props: an array of properties, not terminated by DEFINE_PROP_END_OF_LIST.
969cb9f4b28SRichard Henderson  * @n: ARRAY_SIZE(@props)
970cb9f4b28SRichard Henderson  *
971cb9f4b28SRichard Henderson  * This will add a set of properties to the object. It will fault if
972cb9f4b28SRichard Henderson  * you attempt to add an existing property defined by a parent class.
973cb9f4b28SRichard Henderson  * To modify an inherited property you need to use????
974cb9f4b28SRichard Henderson  */
975cb9f4b28SRichard Henderson void device_class_set_props_n(DeviceClass *dc, const Property *props, size_t n);
976cb9f4b28SRichard Henderson 
977cb9f4b28SRichard Henderson /**
978c378e882SAlex Bennée  * device_class_set_parent_realize() - set up for chaining realize fns
979c378e882SAlex Bennée  * @dc: The device class
980c378e882SAlex Bennée  * @dev_realize: the device realize function
981c378e882SAlex Bennée  * @parent_realize: somewhere to save the parents realize function
982c378e882SAlex Bennée  *
983c378e882SAlex Bennée  * This is intended to be used when the new realize function will
984c378e882SAlex Bennée  * eventually call its parent realization function during creation.
985c378e882SAlex Bennée  * This requires storing the function call somewhere (usually in the
986c378e882SAlex Bennée  * instance structure) so you can eventually call
987c378e882SAlex Bennée  * dc->parent_realize(dev, errp)
988c378e882SAlex Bennée  */
98946795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_realize(DeviceClass *dc,
99046795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize dev_realize,
99146795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize *parent_realize);
992c378e882SAlex Bennée 
993134e0944SPeter Maydell /**
994134e0944SPeter Maydell  * device_class_set_legacy_reset(): set the DeviceClass::reset method
995134e0944SPeter Maydell  * @dc: The device class
996134e0944SPeter Maydell  * @dev_reset: the reset function
997134e0944SPeter Maydell  *
998134e0944SPeter Maydell  * This function sets the DeviceClass::reset method. This is widely
999134e0944SPeter Maydell  * used in existing code, but new code should prefer to use the
1000134e0944SPeter Maydell  * Resettable API as documented in docs/devel/reset.rst.
1001134e0944SPeter Maydell  * In addition, devices which need to chain to their parent class's
1002134e0944SPeter Maydell  * reset methods or which need to be subclassed must use Resettable.
1003134e0944SPeter Maydell  */
1004134e0944SPeter Maydell void device_class_set_legacy_reset(DeviceClass *dc,
1005134e0944SPeter Maydell                                    DeviceReset dev_reset);
1006c378e882SAlex Bennée 
1007c378e882SAlex Bennée /**
1008c378e882SAlex Bennée  * device_class_set_parent_unrealize() - set up for chaining unrealize fns
1009c378e882SAlex Bennée  * @dc: The device class
1010c378e882SAlex Bennée  * @dev_unrealize: the device realize function
1011c378e882SAlex Bennée  * @parent_unrealize: somewhere to save the parents unrealize function
1012c378e882SAlex Bennée  *
1013c378e882SAlex Bennée  * This is intended to be used when the new unrealize function will
1014c378e882SAlex Bennée  * eventually call its parent unrealization function during the
1015c378e882SAlex Bennée  * unrealize phase. This requires storing the function call somewhere
1016c378e882SAlex Bennée  * (usually in the instance structure) so you can eventually call
1017c378e882SAlex Bennée  * dc->parent_unrealize(dev);
1018c378e882SAlex Bennée  */
101946795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_unrealize(DeviceClass *dc,
102046795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize dev_unrealize,
102146795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize *parent_unrealize);
102246795cf2SPhilippe Mathieu-Daudé 
10238a9358ccSMarkus Armbruster const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
1024074a86fcSAnthony Liguori 
1025074a86fcSAnthony Liguori const char *qdev_fw_name(DeviceState *dev);
1026074a86fcSAnthony Liguori 
1027f66dc873SPaolo Bonzini void qdev_assert_realized_properly(void);
1028074a86fcSAnthony Liguori Object *qdev_get_machine(void);
1029074a86fcSAnthony Liguori 
1030956ef499SManos Pitsidianakis /**
1031956ef499SManos Pitsidianakis  * qdev_get_human_name() - Return a human-readable name for a device
1032956ef499SManos Pitsidianakis  * @dev: The device. Must be a valid and non-NULL pointer.
1033956ef499SManos Pitsidianakis  *
1034956ef499SManos Pitsidianakis  * .. note::
1035956ef499SManos Pitsidianakis  *    This function is intended for user friendly error messages.
1036956ef499SManos Pitsidianakis  *
1037956ef499SManos Pitsidianakis  * Returns: A newly allocated string containing the device id if not null,
1038956ef499SManos Pitsidianakis  * else the object canonical path.
1039956ef499SManos Pitsidianakis  *
1040956ef499SManos Pitsidianakis  * Use g_free() to free it.
1041956ef499SManos Pitsidianakis  */
1042956ef499SManos Pitsidianakis char *qdev_get_human_name(DeviceState *dev);
1043956ef499SManos Pitsidianakis 
1044074a86fcSAnthony Liguori /* FIXME: make this a link<> */
1045bb755ba4SPaolo Bonzini bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
1046074a86fcSAnthony Liguori 
104721def24aSJuan Quintela extern bool qdev_hot_removed;
1048074a86fcSAnthony Liguori 
1049074a86fcSAnthony Liguori char *qdev_get_dev_path(DeviceState *dev);
1050074a86fcSAnthony Liguori 
10519bc6bfdfSMarkus Armbruster void qbus_set_hotplug_handler(BusState *bus, Object *handler);
1052cd7c8660SMarkus Armbruster void qbus_set_bus_hotplug_handler(BusState *bus);
105339b888bdSIgor Mammedov 
105439b888bdSIgor Mammedov static inline bool qbus_is_hotpluggable(BusState *bus)
105539b888bdSIgor Mammedov {
1056ceefa0b7SIgor Mammedov     HotplugHandler *plug_handler = bus->hotplug_handler;
1057ceefa0b7SIgor Mammedov     bool ret = !!plug_handler;
1058ceefa0b7SIgor Mammedov 
1059ceefa0b7SIgor Mammedov     if (plug_handler) {
1060ceefa0b7SIgor Mammedov         HotplugHandlerClass *hdc;
1061ceefa0b7SIgor Mammedov 
1062ceefa0b7SIgor Mammedov         hdc = HOTPLUG_HANDLER_GET_CLASS(plug_handler);
1063ceefa0b7SIgor Mammedov         if (hdc->is_hotpluggable_bus) {
1064ceefa0b7SIgor Mammedov             ret = hdc->is_hotpluggable_bus(plug_handler, bus);
1065ceefa0b7SIgor Mammedov         }
1066ceefa0b7SIgor Mammedov     }
1067ceefa0b7SIgor Mammedov     return ret;
106839b888bdSIgor Mammedov }
1069707ff800SPaul Durrant 
10701518562bSPeter Maydell /**
10711518562bSPeter Maydell  * qbus_mark_full: Mark this bus as full, so no more devices can be attached
10721518562bSPeter Maydell  * @bus: Bus to mark as full
10731518562bSPeter Maydell  *
10741518562bSPeter Maydell  * By default, QEMU will allow devices to be plugged into a bus up
10751518562bSPeter Maydell  * to the bus class's device count limit. Calling this function
10761518562bSPeter Maydell  * marks a particular bus as full, so that no more devices can be
10771518562bSPeter Maydell  * plugged into it. In particular this means that the bus will not
10781518562bSPeter Maydell  * be considered as a candidate for plugging in devices created by
10791518562bSPeter Maydell  * the user on the commandline or via the monitor.
10801518562bSPeter Maydell  * If a machine has multiple buses of a given type, such as I2C,
10811518562bSPeter Maydell  * where some of those buses in the real hardware are used only for
10821518562bSPeter Maydell  * internal devices and some are exposed via expansion ports, you
10831518562bSPeter Maydell  * can use this function to mark the internal-only buses as full
10841518562bSPeter Maydell  * after you have created all their internal devices. Then user
10851518562bSPeter Maydell  * created devices will appear on the expansion-port bus where
10861518562bSPeter Maydell  * guest software expects them.
10871518562bSPeter Maydell  */
10881518562bSPeter Maydell static inline void qbus_mark_full(BusState *bus)
10891518562bSPeter Maydell {
10901518562bSPeter Maydell     bus->full = true;
10911518562bSPeter Maydell }
10921518562bSPeter Maydell 
1093707ff800SPaul Durrant void device_listener_register(DeviceListener *listener);
1094707ff800SPaul Durrant void device_listener_unregister(DeviceListener *listener);
1095707ff800SPaul Durrant 
1096f3a85056SJens Freimann /**
10976aebb1f6SAlex Bennée  * qdev_should_hide_device() - check if device should be hidden
10986aebb1f6SAlex Bennée  *
1099f3558b1bSKevin Wolf  * @opts: options QDict
1100f3558b1bSKevin Wolf  * @from_json: true if @opts entries are typed, false for all strings
1101f3558b1bSKevin Wolf  * @errp: pointer to error object
1102f3a85056SJens Freimann  *
11036aebb1f6SAlex Bennée  * When a device is added via qdev_device_add() this will be called.
11046aebb1f6SAlex Bennée  *
11056aebb1f6SAlex Bennée  * Return: if the device should be added now or not.
1106f3a85056SJens Freimann  */
1107f3558b1bSKevin Wolf bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp);
1108f3a85056SJens Freimann 
11092f181fbdSPaolo Bonzini typedef enum MachineInitPhase {
11102f181fbdSPaolo Bonzini     /* current_machine is NULL.  */
11112f181fbdSPaolo Bonzini     PHASE_NO_MACHINE,
11122f181fbdSPaolo Bonzini 
11132f181fbdSPaolo Bonzini     /* current_machine is not NULL, but current_machine->accel is NULL.  */
11142f181fbdSPaolo Bonzini     PHASE_MACHINE_CREATED,
11152f181fbdSPaolo Bonzini 
11162f181fbdSPaolo Bonzini     /*
11172f181fbdSPaolo Bonzini      * current_machine->accel is not NULL, but the machine properties have
11182f181fbdSPaolo Bonzini      * not been validated and machine_class->init has not yet been called.
11192f181fbdSPaolo Bonzini      */
11202f181fbdSPaolo Bonzini     PHASE_ACCEL_CREATED,
11212f181fbdSPaolo Bonzini 
11222f181fbdSPaolo Bonzini     /*
112304accf43SMark Kanda      * Late backend objects have been created and initialized.
112404accf43SMark Kanda      */
112504accf43SMark Kanda     PHASE_LATE_BACKENDS_CREATED,
112604accf43SMark Kanda 
112704accf43SMark Kanda     /*
11282f181fbdSPaolo Bonzini      * machine_class->init has been called, thus creating any embedded
11292f181fbdSPaolo Bonzini      * devices and validating machine properties.  Devices created at
11302f181fbdSPaolo Bonzini      * this time are considered to be cold-plugged.
11312f181fbdSPaolo Bonzini      */
11322f181fbdSPaolo Bonzini     PHASE_MACHINE_INITIALIZED,
11332f181fbdSPaolo Bonzini 
11342f181fbdSPaolo Bonzini     /*
11352f181fbdSPaolo Bonzini      * QEMU is ready to start CPUs and devices created at this time
11362f181fbdSPaolo Bonzini      * are considered to be hot-plugged.  The monitor is not restricted
11372f181fbdSPaolo Bonzini      * to "preconfig" commands.
11382f181fbdSPaolo Bonzini      */
11392f181fbdSPaolo Bonzini     PHASE_MACHINE_READY,
11402f181fbdSPaolo Bonzini } MachineInitPhase;
11412f181fbdSPaolo Bonzini 
1142f703f1efSPhilippe Mathieu-Daudé bool phase_check(MachineInitPhase phase);
1143f703f1efSPhilippe Mathieu-Daudé void phase_advance(MachineInitPhase phase);
11442f181fbdSPaolo Bonzini 
1145074a86fcSAnthony Liguori #endif
1146