xref: /qemu/include/hw/qdev-core.h (revision 7d61808206cc651ae42024ab6def827afa2f807b)
1074a86fcSAnthony Liguori #ifndef QDEV_CORE_H
2074a86fcSAnthony Liguori #define QDEV_CORE_H
3074a86fcSAnthony Liguori 
41de7afc9SPaolo Bonzini #include "qemu/queue.h"
5949fc823SMarcel Apfelbaum #include "qemu/bitmap.h"
62d24a646SMaxim Levitsky #include "qemu/rcu.h"
72d24a646SMaxim Levitsky #include "qemu/rcu_queue.h"
814cccb61SPaolo Bonzini #include "qom/object.h"
90ee4de6cSIgor Mammedov #include "hw/hotplug.h"
10c11256aaSDamien Hedde #include "hw/resettable.h"
11074a86fcSAnthony Liguori 
12074a86fcSAnthony Liguori enum {
13074a86fcSAnthony Liguori     DEV_NVECTORS_UNSPECIFIED = -1,
14074a86fcSAnthony Liguori };
15074a86fcSAnthony Liguori 
16074a86fcSAnthony Liguori #define TYPE_DEVICE "device"
17a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(DeviceState, DeviceClass, DEVICE)
18074a86fcSAnthony Liguori 
193d1237fbSMarcel Apfelbaum typedef enum DeviceCategory {
203d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_BRIDGE,
213d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_USB,
223d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_STORAGE,
233d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_NETWORK,
243d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_INPUT,
253d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_DISPLAY,
263d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_SOUND,
273d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MISC,
28ba31cc72SThomas Huth     DEVICE_CATEGORY_CPU,
293d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MAX
303d1237fbSMarcel Apfelbaum } DeviceCategory;
313d1237fbSMarcel Apfelbaum 
32249d4172SAndreas Färber typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
33b69c3c21SMarkus Armbruster typedef void (*DeviceUnrealize)(DeviceState *dev);
34b850f664SPhilippe Mathieu-Daudé typedef void (*DeviceReset)(DeviceState *dev);
3502e7f85dSBandan Das typedef void (*BusRealize)(BusState *bus, Error **errp);
36b69c3c21SMarkus Armbruster typedef void (*BusUnrealize)(BusState *bus);
37074a86fcSAnthony Liguori 
38249d4172SAndreas Färber /**
39249d4172SAndreas Färber  * DeviceClass:
40249d4172SAndreas Färber  * @props: Properties accessing state fields.
41249d4172SAndreas Färber  * @realize: Callback function invoked when the #DeviceState:realized
42ff46d9d4SPhilippe Mathieu-Daudé  * property is changed to %true.
43249d4172SAndreas Färber  * @unrealize: Callback function invoked when the #DeviceState:realized
44249d4172SAndreas Färber  * property is changed to %false.
451a37eca1SIgor Mammedov  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
461a37eca1SIgor Mammedov  * as readonly "hotpluggable" property of #DeviceState instance
47249d4172SAndreas Färber  *
48249d4172SAndreas Färber  * # Realization #
49249d4172SAndreas Färber  * Devices are constructed in two stages,
50249d4172SAndreas Färber  * 1) object instantiation via object_initialize() and
51249d4172SAndreas Färber  * 2) device realization via #DeviceState:realized property.
526038f989SThomas Huth  * The former may not fail (and must not abort or exit, since it is called
536038f989SThomas Huth  * during device introspection already), and the latter may return error
546038f989SThomas Huth  * information to the caller and must be re-entrant.
55249d4172SAndreas Färber  * Trivial field initializations should go into #TypeInfo.instance_init.
56249d4172SAndreas Färber  * Operations depending on @props static properties should go into @realize.
57249d4172SAndreas Färber  * After successful realization, setting static properties will fail.
58249d4172SAndreas Färber  *
59daeba969SMarkus Armbruster  * As an interim step, the #DeviceState:realized property can also be
60c835fac3SMarkus Armbruster  * set with qdev_realize().
61249d4172SAndreas Färber  * In the future, devices will propagate this state change to their children
62249d4172SAndreas Färber  * and along busses they expose.
63249d4172SAndreas Färber  * The point in time will be deferred to machine creation, so that values
64249d4172SAndreas Färber  * set in @realize will not be introspectable beforehand. Therefore devices
65249d4172SAndreas Färber  * must not create children during @realize; they should initialize them via
66249d4172SAndreas Färber  * object_initialize() in their own #TypeInfo.instance_init and forward the
67249d4172SAndreas Färber  * realization events appropriately.
68249d4172SAndreas Färber  *
69249d4172SAndreas Färber  * Any type may override the @realize and/or @unrealize callbacks but needs
70782beb52SAndreas Färber  * to call the parent type's implementation if keeping their functionality
71782beb52SAndreas Färber  * is desired. Refer to QOM documentation for further discussion and examples.
72782beb52SAndreas Färber  *
73782beb52SAndreas Färber  * <note>
74782beb52SAndreas Färber  *   <para>
75ff46d9d4SPhilippe Mathieu-Daudé  * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
76ff46d9d4SPhilippe Mathieu-Daudé  * derived directly from it need not call their parent's @realize and
77ff46d9d4SPhilippe Mathieu-Daudé  * @unrealize.
78782beb52SAndreas Färber  * For other types consult the documentation and implementation of the
79782beb52SAndreas Färber  * respective parent types.
80782beb52SAndreas Färber  *   </para>
81782beb52SAndreas Färber  * </note>
82f3a85056SJens Freimann  *
83f3a85056SJens Freimann  * # Hiding a device #
84b91ad981SJuan Quintela  * To hide a device, a DeviceListener function hide_device() needs to
85f3a85056SJens Freimann  * be registered.
86b91ad981SJuan Quintela  * It can be used to defer adding a device and therefore hide it from
87b91ad981SJuan Quintela  * the guest. The handler registering to this DeviceListener can save
88b91ad981SJuan Quintela  * the QOpts passed to it for re-using it later. It must return if it
89b91ad981SJuan Quintela  * wants the device to be hidden or visible. When the handler function
90b91ad981SJuan Quintela  * decides the device shall be visible it will be added with
91b91ad981SJuan Quintela  * qdev_device_add() and realized as any other device. Otherwise
92b91ad981SJuan Quintela  * qdev_device_add() will return early without adding the device. The
93b91ad981SJuan Quintela  * guest will not see a "hidden" device until it was marked visible
94b91ad981SJuan Quintela  * and qdev_device_add called again.
95f3a85056SJens Freimann  *
96249d4172SAndreas Färber  */
97db1015e9SEduardo Habkost struct DeviceClass {
98249d4172SAndreas Färber     /*< private >*/
99074a86fcSAnthony Liguori     ObjectClass parent_class;
100249d4172SAndreas Färber     /*< public >*/
101074a86fcSAnthony Liguori 
1023d1237fbSMarcel Apfelbaum     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
103074a86fcSAnthony Liguori     const char *fw_name;
104074a86fcSAnthony Liguori     const char *desc;
105385d8f22SPaolo Bonzini 
106385d8f22SPaolo Bonzini     /*
107385d8f22SPaolo Bonzini      * The underscore at the end ensures a compile-time error if someone
108385d8f22SPaolo Bonzini      * assigns to dc->props instead of using device_class_set_props.
109385d8f22SPaolo Bonzini      */
110385d8f22SPaolo Bonzini     Property *props_;
111efec3dd6SMarkus Armbruster 
112efec3dd6SMarkus Armbruster     /*
113e90f2a8cSEduardo Habkost      * Can this device be instantiated with -device / device_add?
114efec3dd6SMarkus Armbruster      * All devices should support instantiation with device_add, and
115efec3dd6SMarkus Armbruster      * this flag should not exist.  But we're not there, yet.  Some
116efec3dd6SMarkus Armbruster      * devices fail to instantiate with cryptic error messages.
117efec3dd6SMarkus Armbruster      * Others instantiate, but don't work.  Exposing users to such
118e90f2a8cSEduardo Habkost      * behavior would be cruel; clearing this flag will protect them.
119e90f2a8cSEduardo Habkost      * It should never be cleared without a comment explaining why it
120e90f2a8cSEduardo Habkost      * is cleared.
121efec3dd6SMarkus Armbruster      * TODO remove once we're there
122efec3dd6SMarkus Armbruster      */
123e90f2a8cSEduardo Habkost     bool user_creatable;
1241a37eca1SIgor Mammedov     bool hotpluggable;
125074a86fcSAnthony Liguori 
126074a86fcSAnthony Liguori     /* callbacks */
127c11256aaSDamien Hedde     /*
128c11256aaSDamien Hedde      * Reset method here is deprecated and replaced by methods in the
129c11256aaSDamien Hedde      * resettable class interface to implement a multi-phase reset.
130c11256aaSDamien Hedde      * TODO: remove once every reset callback is unused
131c11256aaSDamien Hedde      */
132b850f664SPhilippe Mathieu-Daudé     DeviceReset reset;
133249d4172SAndreas Färber     DeviceRealize realize;
134249d4172SAndreas Färber     DeviceUnrealize unrealize;
135074a86fcSAnthony Liguori 
136074a86fcSAnthony Liguori     /* device state */
1378a9358ccSMarkus Armbruster     const VMStateDescription *vmsd;
138074a86fcSAnthony Liguori 
139074a86fcSAnthony Liguori     /* Private to qdev / bus.  */
140074a86fcSAnthony Liguori     const char *bus_type;
141db1015e9SEduardo Habkost };
142074a86fcSAnthony Liguori 
143a5f54290SPeter Crosthwaite typedef struct NamedGPIOList NamedGPIOList;
144a5f54290SPeter Crosthwaite 
145a5f54290SPeter Crosthwaite struct NamedGPIOList {
146a5f54290SPeter Crosthwaite     char *name;
147a5f54290SPeter Crosthwaite     qemu_irq *in;
148a5f54290SPeter Crosthwaite     int num_in;
149a5f54290SPeter Crosthwaite     int num_out;
150a5f54290SPeter Crosthwaite     QLIST_ENTRY(NamedGPIOList) node;
151a5f54290SPeter Crosthwaite };
152a5f54290SPeter Crosthwaite 
1530e6934f2SDamien Hedde typedef struct Clock Clock;
1540e6934f2SDamien Hedde typedef struct NamedClockList NamedClockList;
1550e6934f2SDamien Hedde 
1560e6934f2SDamien Hedde struct NamedClockList {
1570e6934f2SDamien Hedde     char *name;
1580e6934f2SDamien Hedde     Clock *clock;
1590e6934f2SDamien Hedde     bool output;
1600e6934f2SDamien Hedde     bool alias;
1610e6934f2SDamien Hedde     QLIST_ENTRY(NamedClockList) node;
1620e6934f2SDamien Hedde };
1630e6934f2SDamien Hedde 
1647983c8a3SAndreas Färber /**
1657983c8a3SAndreas Färber  * DeviceState:
1667983c8a3SAndreas Färber  * @realized: Indicates whether the device has been fully constructed.
1675dae6fadSMaxim Levitsky  *            When accessed outside big qemu lock, must be accessed with
1685dae6fadSMaxim Levitsky  *            qatomic_load_acquire()
169c11256aaSDamien Hedde  * @reset: ResettableState for the device; handled by Resettable interface.
1707983c8a3SAndreas Färber  *
1717983c8a3SAndreas Färber  * This structure should not be accessed directly.  We declare it here
1727983c8a3SAndreas Färber  * so that it can be embedded in individual device state structures.
1737983c8a3SAndreas Färber  */
174074a86fcSAnthony Liguori struct DeviceState {
1757983c8a3SAndreas Färber     /*< private >*/
176074a86fcSAnthony Liguori     Object parent_obj;
1777983c8a3SAndreas Färber     /*< public >*/
178074a86fcSAnthony Liguori 
179163f3847SKevin Wolf     char *id;
18004162f8fSMichael Roth     char *canonical_path;
1817983c8a3SAndreas Färber     bool realized;
182352e8da7SPaolo Bonzini     bool pending_deleted_event;
183074a86fcSAnthony Liguori     QemuOpts *opts;
184074a86fcSAnthony Liguori     int hotplugged;
185a1190ab6SJens Freimann     bool allow_unplug_during_migration;
186074a86fcSAnthony Liguori     BusState *parent_bus;
187a5f54290SPeter Crosthwaite     QLIST_HEAD(, NamedGPIOList) gpios;
1880e6934f2SDamien Hedde     QLIST_HEAD(, NamedClockList) clocks;
189074a86fcSAnthony Liguori     QLIST_HEAD(, BusState) child_bus;
190074a86fcSAnthony Liguori     int num_child_bus;
191074a86fcSAnthony Liguori     int instance_id_alias;
192074a86fcSAnthony Liguori     int alias_required_for_version;
193c11256aaSDamien Hedde     ResettableState reset;
194074a86fcSAnthony Liguori };
195074a86fcSAnthony Liguori 
196707ff800SPaul Durrant struct DeviceListener {
197707ff800SPaul Durrant     void (*realize)(DeviceListener *listener, DeviceState *dev);
198707ff800SPaul Durrant     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
199f3a85056SJens Freimann     /*
200b91ad981SJuan Quintela      * This callback is called upon init of the DeviceState and
201b91ad981SJuan Quintela      * informs qdev if a device should be visible or hidden.  We can
202b91ad981SJuan Quintela      * hide a failover device depending for example on the device
203b91ad981SJuan Quintela      * opts.
204*7d618082SKevin Wolf      *
205*7d618082SKevin Wolf      * On errors, it returns false and errp is set. Device creation
206*7d618082SKevin Wolf      * should fail in this case.
207f3a85056SJens Freimann      */
208*7d618082SKevin Wolf     bool (*hide_device)(DeviceListener *listener, QemuOpts *device_opts,
209*7d618082SKevin Wolf                         Error **errp);
210707ff800SPaul Durrant     QTAILQ_ENTRY(DeviceListener) link;
211707ff800SPaul Durrant };
212707ff800SPaul Durrant 
213074a86fcSAnthony Liguori #define TYPE_BUS "bus"
2148110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(BusState, BusClass,
2158110fa1dSEduardo Habkost                      BUS, TYPE_BUS)
216074a86fcSAnthony Liguori 
217074a86fcSAnthony Liguori struct BusClass {
218074a86fcSAnthony Liguori     ObjectClass parent_class;
219074a86fcSAnthony Liguori 
220074a86fcSAnthony Liguori     /* FIXME first arg should be BusState */
221074a86fcSAnthony Liguori     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
222074a86fcSAnthony Liguori     char *(*get_dev_path)(DeviceState *dev);
223bb755ba4SPaolo Bonzini 
224074a86fcSAnthony Liguori     /*
225074a86fcSAnthony Liguori      * This callback is used to create Open Firmware device path in accordance
226074a86fcSAnthony Liguori      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
227074a86fcSAnthony Liguori      * bindings can be found at http://playground.sun.com/1275/bindings/.
228074a86fcSAnthony Liguori      */
229074a86fcSAnthony Liguori     char *(*get_fw_dev_path)(DeviceState *dev);
230bb755ba4SPaolo Bonzini 
231dcc20931SPaolo Bonzini     void (*reset)(BusState *bus);
232bb755ba4SPaolo Bonzini 
233bb755ba4SPaolo Bonzini     /*
234bb755ba4SPaolo Bonzini      * Return whether the device can be added to @bus,
235bb755ba4SPaolo Bonzini      * based on the address that was set (via device properties)
236bb755ba4SPaolo Bonzini      * before realize.  If not, on return @errp contains the
237bb755ba4SPaolo Bonzini      * human-readable error message.
238bb755ba4SPaolo Bonzini      */
239bb755ba4SPaolo Bonzini     bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
240bb755ba4SPaolo Bonzini 
24102e7f85dSBandan Das     BusRealize realize;
24202e7f85dSBandan Das     BusUnrealize unrealize;
24302e7f85dSBandan Das 
2441395af6fSKONRAD Frederic     /* maximum devices allowed on the bus, 0: no limit. */
2451395af6fSKONRAD Frederic     int max_dev;
24661de3676SAlexander Graf     /* number of automatically allocated bus ids (e.g. ide.0) */
24761de3676SAlexander Graf     int automatic_ids;
248074a86fcSAnthony Liguori };
249074a86fcSAnthony Liguori 
250074a86fcSAnthony Liguori typedef struct BusChild {
2512d24a646SMaxim Levitsky     struct rcu_head rcu;
252074a86fcSAnthony Liguori     DeviceState *child;
253074a86fcSAnthony Liguori     int index;
254074a86fcSAnthony Liguori     QTAILQ_ENTRY(BusChild) sibling;
255074a86fcSAnthony Liguori } BusChild;
256074a86fcSAnthony Liguori 
2570ee4de6cSIgor Mammedov #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
2580ee4de6cSIgor Mammedov 
259074a86fcSAnthony Liguori /**
260074a86fcSAnthony Liguori  * BusState:
26127c6ef1bSLi Qiang  * @hotplug_handler: link to a hotplug handler associated with bus.
262c11256aaSDamien Hedde  * @reset: ResettableState for the bus; handled by Resettable interface.
263074a86fcSAnthony Liguori  */
264074a86fcSAnthony Liguori struct BusState {
265074a86fcSAnthony Liguori     Object obj;
266074a86fcSAnthony Liguori     DeviceState *parent;
267f73480c3SMarc-André Lureau     char *name;
2680ee4de6cSIgor Mammedov     HotplugHandler *hotplug_handler;
269074a86fcSAnthony Liguori     int max_index;
27002e7f85dSBandan Das     bool realized;
2711518562bSPeter Maydell     bool full;
27212b2e9f3STony Krowiak     int num_children;
2732d24a646SMaxim Levitsky 
2742d24a646SMaxim Levitsky     /*
2752d24a646SMaxim Levitsky      * children is a RCU QTAILQ, thus readers must use RCU to access it,
2762d24a646SMaxim Levitsky      * and writers must hold the big qemu lock
2772d24a646SMaxim Levitsky      */
2782d24a646SMaxim Levitsky 
279eae3eb3eSPaolo Bonzini     QTAILQ_HEAD(, BusChild) children;
280074a86fcSAnthony Liguori     QLIST_ENTRY(BusState) sibling;
281c11256aaSDamien Hedde     ResettableState reset;
282074a86fcSAnthony Liguori };
283074a86fcSAnthony Liguori 
2845cc56cc6SPeter Maydell /**
2859f9260a3SDon Slutz  * GlobalProperty:
286b3ce84feSEduardo Habkost  * @used: Set to true if property was used when initializing a device.
28792fd453cSDr. David Alan Gilbert  * @optional: If set to true, GlobalProperty will be skipped without errors
28892fd453cSDr. David Alan Gilbert  *            if the property doesn't exist.
289cff8b715SMarc-André Lureau  *
290cff8b715SMarc-André Lureau  * An error is fatal for non-hotplugged devices, when the global is applied.
2919f9260a3SDon Slutz  */
292074a86fcSAnthony Liguori typedef struct GlobalProperty {
293074a86fcSAnthony Liguori     const char *driver;
294074a86fcSAnthony Liguori     const char *property;
295074a86fcSAnthony Liguori     const char *value;
296b3ce84feSEduardo Habkost     bool used;
29792fd453cSDr. David Alan Gilbert     bool optional;
298074a86fcSAnthony Liguori } GlobalProperty;
299074a86fcSAnthony Liguori 
300ea9ce893SMarc-André Lureau static inline void
301ea9ce893SMarc-André Lureau compat_props_add(GPtrArray *arr,
302ea9ce893SMarc-André Lureau                  GlobalProperty props[], size_t nelem)
303ea9ce893SMarc-André Lureau {
304ea9ce893SMarc-André Lureau     int i;
305ea9ce893SMarc-André Lureau     for (i = 0; i < nelem; i++) {
306ea9ce893SMarc-André Lureau         g_ptr_array_add(arr, (void *)&props[i]);
307ea9ce893SMarc-André Lureau     }
308ea9ce893SMarc-André Lureau }
309ea9ce893SMarc-André Lureau 
310074a86fcSAnthony Liguori /*** Board API.  This should go away once we have a machine config file.  ***/
311074a86fcSAnthony Liguori 
312b51238e2SPeter Maydell /**
313b51238e2SPeter Maydell  * qdev_new: Create a device on the heap
314b51238e2SPeter Maydell  * @name: device type to create (we assert() that this type exists)
315b51238e2SPeter Maydell  *
316b51238e2SPeter Maydell  * This only allocates the memory and initializes the device state
317b51238e2SPeter Maydell  * structure, ready for the caller to set properties if they wish.
318b51238e2SPeter Maydell  * The device still needs to be realized.
319b51238e2SPeter Maydell  * The returned object has a reference count of 1.
320b51238e2SPeter Maydell  */
3219940b2cfSMarkus Armbruster DeviceState *qdev_new(const char *name);
322b51238e2SPeter Maydell /**
323b51238e2SPeter Maydell  * qdev_try_new: Try to create a device on the heap
324b51238e2SPeter Maydell  * @name: device type to create
325b51238e2SPeter Maydell  *
326b51238e2SPeter Maydell  * This is like qdev_new(), except it returns %NULL when type @name
327b51238e2SPeter Maydell  * does not exist, rather than asserting.
328b51238e2SPeter Maydell  */
3299940b2cfSMarkus Armbruster DeviceState *qdev_try_new(const char *name);
330b51238e2SPeter Maydell /**
331b51238e2SPeter Maydell  * qdev_realize: Realize @dev.
332b51238e2SPeter Maydell  * @dev: device to realize
333b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
334b51238e2SPeter Maydell  * @errp: pointer to error object
335b51238e2SPeter Maydell  *
336b51238e2SPeter Maydell  * "Realize" the device, i.e. perform the second phase of device
337b51238e2SPeter Maydell  * initialization.
338b51238e2SPeter Maydell  * @dev must not be plugged into a bus already.
339b51238e2SPeter Maydell  * If @bus, plug @dev into @bus.  This takes a reference to @dev.
340b51238e2SPeter Maydell  * If @dev has no QOM parent, make one up, taking another reference.
341b51238e2SPeter Maydell  * On success, return true.
342b51238e2SPeter Maydell  * On failure, store an error through @errp and return false.
343b51238e2SPeter Maydell  *
344b51238e2SPeter Maydell  * If you created @dev using qdev_new(), you probably want to use
345b51238e2SPeter Maydell  * qdev_realize_and_unref() instead.
346b51238e2SPeter Maydell  */
3479940b2cfSMarkus Armbruster bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
348b51238e2SPeter Maydell /**
349b51238e2SPeter Maydell  * qdev_realize_and_unref: Realize @dev and drop a reference
350b51238e2SPeter Maydell  * @dev: device to realize
351b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
352b51238e2SPeter Maydell  * @errp: pointer to error object
353b51238e2SPeter Maydell  *
354b51238e2SPeter Maydell  * Realize @dev and drop a reference.
355b51238e2SPeter Maydell  * This is like qdev_realize(), except the caller must hold a
356b51238e2SPeter Maydell  * (private) reference, which is dropped on return regardless of
357b51238e2SPeter Maydell  * success or failure.  Intended use::
358b51238e2SPeter Maydell  *
359b51238e2SPeter Maydell  *     dev = qdev_new();
360b51238e2SPeter Maydell  *     [...]
361b51238e2SPeter Maydell  *     qdev_realize_and_unref(dev, bus, errp);
362b51238e2SPeter Maydell  *
363b51238e2SPeter Maydell  * Now @dev can go away without further ado.
364b51238e2SPeter Maydell  *
365b51238e2SPeter Maydell  * If you are embedding the device into some other QOM device and
366b51238e2SPeter Maydell  * initialized it via some variant on object_initialize_child() then
367b51238e2SPeter Maydell  * do not use this function, because that family of functions arrange
368b51238e2SPeter Maydell  * for the only reference to the child device to be held by the parent
369b51238e2SPeter Maydell  * via the child<> property, and so the reference-count-drop done here
370b51238e2SPeter Maydell  * would be incorrect. For that use case you want qdev_realize().
371b51238e2SPeter Maydell  */
3729940b2cfSMarkus Armbruster bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
37346ea1be1SPeter Maydell /**
37446ea1be1SPeter Maydell  * qdev_unrealize: Unrealize a device
37546ea1be1SPeter Maydell  * @dev: device to unrealize
37646ea1be1SPeter Maydell  *
37746ea1be1SPeter Maydell  * This function will "unrealize" a device, which is the first phase
37846ea1be1SPeter Maydell  * of correctly destroying a device that has been realized. It will:
37946ea1be1SPeter Maydell  *
38046ea1be1SPeter Maydell  *  - unrealize any child buses by calling qbus_unrealize()
38146ea1be1SPeter Maydell  *    (this will recursively unrealize any devices on those buses)
38246ea1be1SPeter Maydell  *  - call the the unrealize method of @dev
38346ea1be1SPeter Maydell  *
38446ea1be1SPeter Maydell  * The device can then be freed by causing its reference count to go
38546ea1be1SPeter Maydell  * to zero.
38646ea1be1SPeter Maydell  *
38746ea1be1SPeter Maydell  * Warning: most devices in QEMU do not expect to be unrealized.  Only
38846ea1be1SPeter Maydell  * devices which are hot-unpluggable should be unrealized (as part of
38946ea1be1SPeter Maydell  * the unplugging process); all other devices are expected to last for
39046ea1be1SPeter Maydell  * the life of the simulation and should not be unrealized and freed.
39146ea1be1SPeter Maydell  */
3929940b2cfSMarkus Armbruster void qdev_unrealize(DeviceState *dev);
393074a86fcSAnthony Liguori void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
394074a86fcSAnthony Liguori                                  int required_for_version);
39514405c27SDavid Hildenbrand HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
39603fcbd9dSThomas Huth HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
397d2321d31SPeter Xu bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
39817cc0128SIgor Mammedov /**
39917cc0128SIgor Mammedov  * qdev_get_hotplug_handler: Get handler responsible for device wiring
40017cc0128SIgor Mammedov  *
40117cc0128SIgor Mammedov  * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
40217cc0128SIgor Mammedov  *
40317cc0128SIgor Mammedov  * Note: in case @dev has a parent bus, it will be returned as handler unless
40417cc0128SIgor Mammedov  * machine handler overrides it.
40517cc0128SIgor Mammedov  *
40617cc0128SIgor Mammedov  * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
40717cc0128SIgor Mammedov  *          or NULL if there aren't any.
40817cc0128SIgor Mammedov  */
409c06b2ffbSZhu Guihua HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
410074a86fcSAnthony Liguori void qdev_unplug(DeviceState *dev, Error **errp);
411014176f9SIgor Mammedov void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
412014176f9SIgor Mammedov                                   DeviceState *dev, Error **errp);
413074a86fcSAnthony Liguori void qdev_machine_creation_done(void);
414074a86fcSAnthony Liguori bool qdev_machine_modified(void);
415074a86fcSAnthony Liguori 
416cd07d7f9SPeter Maydell /**
417ddb67f64SPhilippe Mathieu-Daudé  * GpioPolarity: Polarity of a GPIO line
418ddb67f64SPhilippe Mathieu-Daudé  *
419ddb67f64SPhilippe Mathieu-Daudé  * GPIO lines use either positive (active-high) logic,
420ddb67f64SPhilippe Mathieu-Daudé  * or negative (active-low) logic.
421ddb67f64SPhilippe Mathieu-Daudé  *
422ddb67f64SPhilippe Mathieu-Daudé  * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
423ddb67f64SPhilippe Mathieu-Daudé  * active when the voltage on the pin is high (relative to ground);
424ddb67f64SPhilippe Mathieu-Daudé  * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
425ddb67f64SPhilippe Mathieu-Daudé  * is active when the voltage on the pin is low (or grounded).
426ddb67f64SPhilippe Mathieu-Daudé  */
427ddb67f64SPhilippe Mathieu-Daudé typedef enum {
428ddb67f64SPhilippe Mathieu-Daudé     GPIO_POLARITY_ACTIVE_LOW,
429ddb67f64SPhilippe Mathieu-Daudé     GPIO_POLARITY_ACTIVE_HIGH
430ddb67f64SPhilippe Mathieu-Daudé } GpioPolarity;
431ddb67f64SPhilippe Mathieu-Daudé 
432ddb67f64SPhilippe Mathieu-Daudé /**
433cd07d7f9SPeter Maydell  * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
434cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
435cd07d7f9SPeter Maydell  * @n: Number of the anonymous GPIO line (which must be in range)
436cd07d7f9SPeter Maydell  *
437cd07d7f9SPeter Maydell  * Returns the qemu_irq corresponding to an anonymous input GPIO line
438cd07d7f9SPeter Maydell  * (which the device has set up with qdev_init_gpio_in()). The index
439cd07d7f9SPeter Maydell  * @n of the GPIO line must be valid (i.e. be at least 0 and less than
440cd07d7f9SPeter Maydell  * the total number of anonymous input GPIOs the device has); this
441cd07d7f9SPeter Maydell  * function will assert() if passed an invalid index.
442cd07d7f9SPeter Maydell  *
443cd07d7f9SPeter Maydell  * This function is intended to be used by board code or SoC "container"
444cd07d7f9SPeter Maydell  * device models to wire up the GPIO lines; usually the return value
445cd07d7f9SPeter Maydell  * will be passed to qdev_connect_gpio_out() or a similar function to
446cd07d7f9SPeter Maydell  * connect another device's output GPIO line to this input.
447cd07d7f9SPeter Maydell  *
448cd07d7f9SPeter Maydell  * For named input GPIO lines, use qdev_get_gpio_in_named().
449cd07d7f9SPeter Maydell  */
450074a86fcSAnthony Liguori qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
451cd07d7f9SPeter Maydell /**
452cd07d7f9SPeter Maydell  * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
453cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
454cd07d7f9SPeter Maydell  * @name: Name of the input GPIO array
455cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in that array (which must be in range)
456cd07d7f9SPeter Maydell  *
457cd07d7f9SPeter Maydell  * Returns the qemu_irq corresponding to a named input GPIO line
458cd07d7f9SPeter Maydell  * (which the device has set up with qdev_init_gpio_in_named()).
459cd07d7f9SPeter Maydell  * The @name string must correspond to an input GPIO array which exists on
460cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
461cd07d7f9SPeter Maydell  * be at least 0 and less than the total number of input GPIOs in that
462cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
463cd07d7f9SPeter Maydell  *
464cd07d7f9SPeter Maydell  * For anonymous input GPIO lines, use qdev_get_gpio_in().
465cd07d7f9SPeter Maydell  */
466a5f54290SPeter Crosthwaite qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
467a5f54290SPeter Crosthwaite 
468cd07d7f9SPeter Maydell /**
469cd07d7f9SPeter Maydell  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
470cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
471cd07d7f9SPeter Maydell  * @n: Number of the anonymous output GPIO line (which must be in range)
472cd07d7f9SPeter Maydell  * @pin: qemu_irq to connect the output line to
473cd07d7f9SPeter Maydell  *
474cd07d7f9SPeter Maydell  * This function connects an anonymous output GPIO line on a device
475cd07d7f9SPeter Maydell  * up to an arbitrary qemu_irq, so that when the device asserts that
476cd07d7f9SPeter Maydell  * output GPIO line, the qemu_irq's callback is invoked.
477cd07d7f9SPeter Maydell  * The index @n of the GPIO line must be valid (i.e. be at least 0 and
478cd07d7f9SPeter Maydell  * less than the total number of anonymous output GPIOs the device has
479cd07d7f9SPeter Maydell  * created with qdev_init_gpio_out()); otherwise this function will assert().
480cd07d7f9SPeter Maydell  *
481cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
482cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
483cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
484cd07d7f9SPeter Maydell  *
485cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
486cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
487cd07d7f9SPeter Maydell  * same qemu_irq. (Warning: there is no assertion or other guard to
488cd07d7f9SPeter Maydell  * catch this error: the model will just not do the right thing.)
489cd07d7f9SPeter Maydell  * Instead, for fan-out you can use the TYPE_IRQ_SPLIT device: connect
490cd07d7f9SPeter Maydell  * a device's outbound GPIO to the splitter's input, and connect each
491cd07d7f9SPeter Maydell  * of the splitter's outputs to a different device.  For fan-in you
492cd07d7f9SPeter Maydell  * can use the TYPE_OR_IRQ device, which is a model of a logical OR
493cd07d7f9SPeter Maydell  * gate with multiple inputs and one output.
494cd07d7f9SPeter Maydell  *
495cd07d7f9SPeter Maydell  * For named output GPIO lines, use qdev_connect_gpio_out_named().
496cd07d7f9SPeter Maydell  */
497074a86fcSAnthony Liguori void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
498cd07d7f9SPeter Maydell /**
499cd07d7f9SPeter Maydell  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
500cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
501cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
502cd07d7f9SPeter Maydell  * @n: Number of the anonymous output GPIO line (which must be in range)
503cd07d7f9SPeter Maydell  * @pin: qemu_irq to connect the output line to
504cd07d7f9SPeter Maydell  *
505cd07d7f9SPeter Maydell  * This function connects an anonymous output GPIO line on a device
506cd07d7f9SPeter Maydell  * up to an arbitrary qemu_irq, so that when the device asserts that
507cd07d7f9SPeter Maydell  * output GPIO line, the qemu_irq's callback is invoked.
508cd07d7f9SPeter Maydell  * The @name string must correspond to an output GPIO array which exists on
509cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
510cd07d7f9SPeter Maydell  * be at least 0 and less than the total number of input GPIOs in that
511cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
512cd07d7f9SPeter Maydell  *
513cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
514cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
515cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
516cd07d7f9SPeter Maydell  *
517cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
518cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
519cd07d7f9SPeter Maydell  * same qemu_irq; see qdev_connect_gpio_out() for details.
520cd07d7f9SPeter Maydell  *
521cd07d7f9SPeter Maydell  * For named output GPIO lines, use qdev_connect_gpio_out_named().
522cd07d7f9SPeter Maydell  */
523a5f54290SPeter Crosthwaite void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
524a5f54290SPeter Crosthwaite                                  qemu_irq pin);
525cd07d7f9SPeter Maydell /**
526cd07d7f9SPeter Maydell  * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
527cd07d7f9SPeter Maydell  * @dev: Device whose output GPIO we are interested in
528cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
529cd07d7f9SPeter Maydell  * @n: Number of the output GPIO line within that array
530cd07d7f9SPeter Maydell  *
531cd07d7f9SPeter Maydell  * Returns whatever qemu_irq is currently connected to the specified
532cd07d7f9SPeter Maydell  * output GPIO line of @dev. This will be NULL if the output GPIO line
533cd07d7f9SPeter Maydell  * has never been wired up to the anything.  Note that the qemu_irq
534cd07d7f9SPeter Maydell  * returned does not belong to @dev -- it will be the input GPIO or
535cd07d7f9SPeter Maydell  * IRQ of whichever device the board code has connected up to @dev's
536cd07d7f9SPeter Maydell  * output GPIO.
537cd07d7f9SPeter Maydell  *
538cd07d7f9SPeter Maydell  * You probably don't need to use this function -- it is used only
539cd07d7f9SPeter Maydell  * by the platform-bus subsystem.
540cd07d7f9SPeter Maydell  */
541b7973186SAlexander Graf qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
542cd07d7f9SPeter Maydell /**
543cd07d7f9SPeter Maydell  * qdev_intercept_gpio_out: Intercept an existing GPIO connection
544cd07d7f9SPeter Maydell  * @dev: Device to intercept the outbound GPIO line from
545cd07d7f9SPeter Maydell  * @icpt: New qemu_irq to connect instead
546cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
547cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in the array
548cd07d7f9SPeter Maydell  *
549cd07d7f9SPeter Maydell  * This function is provided only for use by the qtest testing framework
550cd07d7f9SPeter Maydell  * and is not suitable for use in non-testing parts of QEMU.
551cd07d7f9SPeter Maydell  *
552cd07d7f9SPeter Maydell  * This function breaks an existing connection of an outbound GPIO
553cd07d7f9SPeter Maydell  * line from @dev, and replaces it with the new qemu_irq @icpt, as if
554cd07d7f9SPeter Maydell  * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
555cd07d7f9SPeter Maydell  * The previously connected qemu_irq is returned, so it can be restored
556cd07d7f9SPeter Maydell  * by a second call to qdev_intercept_gpio_out() if desired.
557cd07d7f9SPeter Maydell  */
5580c24db2bSPeter Crosthwaite qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
5590c24db2bSPeter Crosthwaite                                  const char *name, int n);
560074a86fcSAnthony Liguori 
561074a86fcSAnthony Liguori BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
562074a86fcSAnthony Liguori 
563074a86fcSAnthony Liguori /*** Device API.  ***/
564074a86fcSAnthony Liguori 
565cd07d7f9SPeter Maydell /**
566cd07d7f9SPeter Maydell  * qdev_init_gpio_in: create an array of anonymous input GPIO lines
567cd07d7f9SPeter Maydell  * @dev: Device to create input GPIOs for
568cd07d7f9SPeter Maydell  * @handler: Function to call when GPIO line value is set
569cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
570cd07d7f9SPeter Maydell  *
571cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_in* family in
572cd07d7f9SPeter Maydell  * their instance_init or realize methods to create any input GPIO
573cd07d7f9SPeter Maydell  * lines they need. There is no functional difference between
574cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
575cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
576cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO input whose purpose is obvious.
577cd07d7f9SPeter Maydell  * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
578cd07d7f9SPeter Maydell  *
579cd07d7f9SPeter Maydell  * See qdev_get_gpio_in() for how code that uses such a device can get
580cd07d7f9SPeter Maydell  * hold of an input GPIO line to manipulate it.
581cd07d7f9SPeter Maydell  */
582074a86fcSAnthony Liguori void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
583cd07d7f9SPeter Maydell /**
584cd07d7f9SPeter Maydell  * qdev_init_gpio_out: create an array of anonymous output GPIO lines
585cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
586cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
587cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
588cd07d7f9SPeter Maydell  *
589cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_out* family
590cd07d7f9SPeter Maydell  * in their instance_init or realize methods to create any output
591cd07d7f9SPeter Maydell  * GPIO lines they need. There is no functional difference between
592cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
593cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
594cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO output whose purpose is obvious.
595cd07d7f9SPeter Maydell  *
596cd07d7f9SPeter Maydell  * The @pins argument should be a pointer to either a "qemu_irq"
597cd07d7f9SPeter Maydell  * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
598cd07d7f9SPeter Maydell  * state structure. The device implementation can then raise and
599cd07d7f9SPeter Maydell  * lower the GPIO line by calling qemu_set_irq(). (If anything is
600cd07d7f9SPeter Maydell  * connected to the other end of the GPIO this will cause the handler
601cd07d7f9SPeter Maydell  * function for that input GPIO to be called.)
602cd07d7f9SPeter Maydell  *
603cd07d7f9SPeter Maydell  * See qdev_connect_gpio_out() for how code that uses such a device
604cd07d7f9SPeter Maydell  * can connect to one of its output GPIO lines.
605526dc840SPhilippe Mathieu-Daudé  *
606526dc840SPhilippe Mathieu-Daudé  * There is no need to release the @pins allocated array because it
607526dc840SPhilippe Mathieu-Daudé  * will be automatically released when @dev calls its instance_finalize()
608526dc840SPhilippe Mathieu-Daudé  * handler.
609cd07d7f9SPeter Maydell  */
610074a86fcSAnthony Liguori void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
611cd07d7f9SPeter Maydell /**
612cd07d7f9SPeter Maydell  * qdev_init_gpio_out: create an array of named output GPIO lines
613cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
614cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
615cd07d7f9SPeter Maydell  * @name: Name to give this array of GPIO lines
616cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
617cd07d7f9SPeter Maydell  *
618cd07d7f9SPeter Maydell  * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
619cd07d7f9SPeter Maydell  * with a name. Code using the device can then connect these GPIO lines
620cd07d7f9SPeter Maydell  * using qdev_connect_gpio_out_named().
621cd07d7f9SPeter Maydell  */
622a5f54290SPeter Crosthwaite void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
623a5f54290SPeter Crosthwaite                               const char *name, int n);
6244a151677SPeter Maydell /**
6254a151677SPeter Maydell  * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
6264a151677SPeter Maydell  *   for the specified device
6274a151677SPeter Maydell  *
6284a151677SPeter Maydell  * @dev: Device to create input GPIOs for
6294a151677SPeter Maydell  * @handler: Function to call when GPIO line value is set
6304a151677SPeter Maydell  * @opaque: Opaque data pointer to pass to @handler
6314a151677SPeter Maydell  * @name: Name of the GPIO input (must be unique for this device)
6324a151677SPeter Maydell  * @n: Number of GPIO lines in this input set
6334a151677SPeter Maydell  */
6344a151677SPeter Maydell void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
6354a151677SPeter Maydell                                          qemu_irq_handler handler,
6364a151677SPeter Maydell                                          void *opaque,
6374a151677SPeter Maydell                                          const char *name, int n);
6384a151677SPeter Maydell 
6394a151677SPeter Maydell /**
6404a151677SPeter Maydell  * qdev_init_gpio_in_named: create an array of input GPIO lines
6414a151677SPeter Maydell  *   for the specified device
6424a151677SPeter Maydell  *
6434a151677SPeter Maydell  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
6444a151677SPeter Maydell  * passed to the handler is @dev (which is the most commonly desired behaviour).
6454a151677SPeter Maydell  */
6464a151677SPeter Maydell static inline void qdev_init_gpio_in_named(DeviceState *dev,
6474a151677SPeter Maydell                                            qemu_irq_handler handler,
6484a151677SPeter Maydell                                            const char *name, int n)
6494a151677SPeter Maydell {
6504a151677SPeter Maydell     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
6514a151677SPeter Maydell }
652074a86fcSAnthony Liguori 
653cd07d7f9SPeter Maydell /**
654cd07d7f9SPeter Maydell  * qdev_pass_gpios: create GPIO lines on container which pass through to device
655cd07d7f9SPeter Maydell  * @dev: Device which has GPIO lines
656cd07d7f9SPeter Maydell  * @container: Container device which needs to expose them
657cd07d7f9SPeter Maydell  * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
658cd07d7f9SPeter Maydell  *
659cd07d7f9SPeter Maydell  * In QEMU, complicated devices like SoCs are often modelled with a
660cd07d7f9SPeter Maydell  * "container" QOM device which itself contains other QOM devices and
661cd07d7f9SPeter Maydell  * which wires them up appropriately. This function allows the container
662cd07d7f9SPeter Maydell  * to create GPIO arrays on itself which simply pass through to a GPIO
663cd07d7f9SPeter Maydell  * array of one of its internal devices.
664cd07d7f9SPeter Maydell  *
665cd07d7f9SPeter Maydell  * If @dev has both input and output GPIOs named @name then both will
666cd07d7f9SPeter Maydell  * be passed through. It is not possible to pass a subset of the array
667cd07d7f9SPeter Maydell  * with this function.
668cd07d7f9SPeter Maydell  *
669cd07d7f9SPeter Maydell  * To users of the container device, the GPIO array created on @container
670cd07d7f9SPeter Maydell  * behaves exactly like any other.
671cd07d7f9SPeter Maydell  */
67217a96a14SPeter Crosthwaite void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
67317a96a14SPeter Crosthwaite                      const char *name);
67417a96a14SPeter Crosthwaite 
675074a86fcSAnthony Liguori BusState *qdev_get_parent_bus(DeviceState *dev);
676074a86fcSAnthony Liguori 
677074a86fcSAnthony Liguori /*** BUS API. ***/
678074a86fcSAnthony Liguori 
679074a86fcSAnthony Liguori DeviceState *qdev_find_recursive(BusState *bus, const char *id);
680074a86fcSAnthony Liguori 
681074a86fcSAnthony Liguori /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
682074a86fcSAnthony Liguori typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
683074a86fcSAnthony Liguori typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
684074a86fcSAnthony Liguori 
685d637e1dcSPeter Maydell void qbus_init(void *bus, size_t size, const char *typename,
686074a86fcSAnthony Liguori                DeviceState *parent, const char *name);
6879388d170SPeter Maydell BusState *qbus_new(const char *typename, DeviceState *parent, const char *name);
6889940b2cfSMarkus Armbruster bool qbus_realize(BusState *bus, Error **errp);
6899940b2cfSMarkus Armbruster void qbus_unrealize(BusState *bus);
6909940b2cfSMarkus Armbruster 
691074a86fcSAnthony Liguori /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
692074a86fcSAnthony Liguori  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
693074a86fcSAnthony Liguori  *           0 otherwise. */
6940293214bSPaolo Bonzini int qbus_walk_children(BusState *bus,
6950293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
6960293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
6970293214bSPaolo Bonzini                        void *opaque);
6980293214bSPaolo Bonzini int qdev_walk_children(DeviceState *dev,
6990293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
7000293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
7010293214bSPaolo Bonzini                        void *opaque);
7020293214bSPaolo Bonzini 
703abb89dbfSDamien Hedde /**
704abb89dbfSDamien Hedde  * @qdev_reset_all:
705abb89dbfSDamien Hedde  * Reset @dev. See @qbus_reset_all() for more details.
706abb89dbfSDamien Hedde  *
707abb89dbfSDamien Hedde  * Note: This function is deprecated and will be removed when it becomes unused.
708abb89dbfSDamien Hedde  * Please use device_cold_reset() now.
709abb89dbfSDamien Hedde  */
710074a86fcSAnthony Liguori void qdev_reset_all(DeviceState *dev);
711ff8de075SDavid Hildenbrand void qdev_reset_all_fn(void *opaque);
712d0508c36SPaolo Bonzini 
713d0508c36SPaolo Bonzini /**
714d0508c36SPaolo Bonzini  * @qbus_reset_all:
715d0508c36SPaolo Bonzini  * @bus: Bus to be reset.
716d0508c36SPaolo Bonzini  *
717d0508c36SPaolo Bonzini  * Reset @bus and perform a bus-level ("hard") reset of all devices connected
718d0508c36SPaolo Bonzini  * to it, including recursive processing of all buses below @bus itself.  A
719d0508c36SPaolo Bonzini  * hard reset means that qbus_reset_all will reset all state of the device.
720d0508c36SPaolo Bonzini  * For PCI devices, for example, this will include the base address registers
721d0508c36SPaolo Bonzini  * or configuration space.
722abb89dbfSDamien Hedde  *
723abb89dbfSDamien Hedde  * Note: This function is deprecated and will be removed when it becomes unused.
724abb89dbfSDamien Hedde  * Please use bus_cold_reset() now.
725d0508c36SPaolo Bonzini  */
726d0508c36SPaolo Bonzini void qbus_reset_all(BusState *bus);
727074a86fcSAnthony Liguori void qbus_reset_all_fn(void *opaque);
728074a86fcSAnthony Liguori 
729c11256aaSDamien Hedde /**
730abb89dbfSDamien Hedde  * device_cold_reset:
731abb89dbfSDamien Hedde  * Reset device @dev and perform a recursive processing using the resettable
732abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
733abb89dbfSDamien Hedde  */
734abb89dbfSDamien Hedde void device_cold_reset(DeviceState *dev);
735abb89dbfSDamien Hedde 
736abb89dbfSDamien Hedde /**
737abb89dbfSDamien Hedde  * bus_cold_reset:
738abb89dbfSDamien Hedde  *
739abb89dbfSDamien Hedde  * Reset bus @bus and perform a recursive processing using the resettable
740abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
741abb89dbfSDamien Hedde  */
742abb89dbfSDamien Hedde void bus_cold_reset(BusState *bus);
743abb89dbfSDamien Hedde 
744abb89dbfSDamien Hedde /**
745c11256aaSDamien Hedde  * device_is_in_reset:
746c11256aaSDamien Hedde  * Return true if the device @dev is currently being reset.
747c11256aaSDamien Hedde  */
748c11256aaSDamien Hedde bool device_is_in_reset(DeviceState *dev);
749c11256aaSDamien Hedde 
750c11256aaSDamien Hedde /**
751c11256aaSDamien Hedde  * bus_is_in_reset:
752c11256aaSDamien Hedde  * Return true if the bus @bus is currently being reset.
753c11256aaSDamien Hedde  */
754c11256aaSDamien Hedde bool bus_is_in_reset(BusState *bus);
755c11256aaSDamien Hedde 
756074a86fcSAnthony Liguori /* This should go away once we get rid of the NULL bus hack */
757074a86fcSAnthony Liguori BusState *sysbus_get_default(void);
758074a86fcSAnthony Liguori 
759074a86fcSAnthony Liguori char *qdev_get_fw_dev_path(DeviceState *dev);
7600be63901SGonglei char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
761074a86fcSAnthony Liguori 
762074a86fcSAnthony Liguori /**
763f703a04cSDamien Hedde  * device_legacy_reset:
764074a86fcSAnthony Liguori  *
765074a86fcSAnthony Liguori  * Reset a single device (by calling the reset method).
766abb89dbfSDamien Hedde  * Note: This function is deprecated and will be removed when it becomes unused.
767abb89dbfSDamien Hedde  * Please use device_cold_reset() now.
768074a86fcSAnthony Liguori  */
769f703a04cSDamien Hedde void device_legacy_reset(DeviceState *dev);
770074a86fcSAnthony Liguori 
7714f67d30bSMarc-André Lureau void device_class_set_props(DeviceClass *dc, Property *props);
7724f67d30bSMarc-André Lureau 
773c11256aaSDamien Hedde /**
774c11256aaSDamien Hedde  * device_class_set_parent_reset:
775c11256aaSDamien Hedde  * TODO: remove the function when DeviceClass's reset method
776c11256aaSDamien Hedde  * is not used anymore.
777c11256aaSDamien Hedde  */
77846795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_reset(DeviceClass *dc,
77946795cf2SPhilippe Mathieu-Daudé                                    DeviceReset dev_reset,
78046795cf2SPhilippe Mathieu-Daudé                                    DeviceReset *parent_reset);
78146795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_realize(DeviceClass *dc,
78246795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize dev_realize,
78346795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize *parent_realize);
78446795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_unrealize(DeviceClass *dc,
78546795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize dev_unrealize,
78646795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize *parent_unrealize);
78746795cf2SPhilippe Mathieu-Daudé 
7888a9358ccSMarkus Armbruster const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
789074a86fcSAnthony Liguori 
790074a86fcSAnthony Liguori const char *qdev_fw_name(DeviceState *dev);
791074a86fcSAnthony Liguori 
792f66dc873SPaolo Bonzini void qdev_assert_realized_properly(void);
793074a86fcSAnthony Liguori Object *qdev_get_machine(void);
794074a86fcSAnthony Liguori 
795074a86fcSAnthony Liguori /* FIXME: make this a link<> */
796bb755ba4SPaolo Bonzini bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
797074a86fcSAnthony Liguori 
79821def24aSJuan Quintela extern bool qdev_hot_removed;
799074a86fcSAnthony Liguori 
800074a86fcSAnthony Liguori char *qdev_get_dev_path(DeviceState *dev);
801074a86fcSAnthony Liguori 
8029bc6bfdfSMarkus Armbruster void qbus_set_hotplug_handler(BusState *bus, Object *handler);
803cd7c8660SMarkus Armbruster void qbus_set_bus_hotplug_handler(BusState *bus);
80439b888bdSIgor Mammedov 
80539b888bdSIgor Mammedov static inline bool qbus_is_hotpluggable(BusState *bus)
80639b888bdSIgor Mammedov {
8072d9a982fSIgor Mammedov    return bus->hotplug_handler;
80839b888bdSIgor Mammedov }
809707ff800SPaul Durrant 
8101518562bSPeter Maydell /**
8111518562bSPeter Maydell  * qbus_mark_full: Mark this bus as full, so no more devices can be attached
8121518562bSPeter Maydell  * @bus: Bus to mark as full
8131518562bSPeter Maydell  *
8141518562bSPeter Maydell  * By default, QEMU will allow devices to be plugged into a bus up
8151518562bSPeter Maydell  * to the bus class's device count limit. Calling this function
8161518562bSPeter Maydell  * marks a particular bus as full, so that no more devices can be
8171518562bSPeter Maydell  * plugged into it. In particular this means that the bus will not
8181518562bSPeter Maydell  * be considered as a candidate for plugging in devices created by
8191518562bSPeter Maydell  * the user on the commandline or via the monitor.
8201518562bSPeter Maydell  * If a machine has multiple buses of a given type, such as I2C,
8211518562bSPeter Maydell  * where some of those buses in the real hardware are used only for
8221518562bSPeter Maydell  * internal devices and some are exposed via expansion ports, you
8231518562bSPeter Maydell  * can use this function to mark the internal-only buses as full
8241518562bSPeter Maydell  * after you have created all their internal devices. Then user
8251518562bSPeter Maydell  * created devices will appear on the expansion-port bus where
8261518562bSPeter Maydell  * guest software expects them.
8271518562bSPeter Maydell  */
8281518562bSPeter Maydell static inline void qbus_mark_full(BusState *bus)
8291518562bSPeter Maydell {
8301518562bSPeter Maydell     bus->full = true;
8311518562bSPeter Maydell }
8321518562bSPeter Maydell 
833707ff800SPaul Durrant void device_listener_register(DeviceListener *listener);
834707ff800SPaul Durrant void device_listener_unregister(DeviceListener *listener);
835707ff800SPaul Durrant 
836f3a85056SJens Freimann /**
837f3a85056SJens Freimann  * @qdev_should_hide_device:
838f3a85056SJens Freimann  * @opts: QemuOpts as passed on cmdline.
839f3a85056SJens Freimann  *
840f3a85056SJens Freimann  * Check if a device should be added.
841f3a85056SJens Freimann  * When a device is added via qdev_device_add() this will be called,
842f3a85056SJens Freimann  * and return if the device should be added now or not.
843f3a85056SJens Freimann  */
844*7d618082SKevin Wolf bool qdev_should_hide_device(QemuOpts *opts, Error **errp);
845f3a85056SJens Freimann 
8462f181fbdSPaolo Bonzini typedef enum MachineInitPhase {
8472f181fbdSPaolo Bonzini     /* current_machine is NULL.  */
8482f181fbdSPaolo Bonzini     PHASE_NO_MACHINE,
8492f181fbdSPaolo Bonzini 
8502f181fbdSPaolo Bonzini     /* current_machine is not NULL, but current_machine->accel is NULL.  */
8512f181fbdSPaolo Bonzini     PHASE_MACHINE_CREATED,
8522f181fbdSPaolo Bonzini 
8532f181fbdSPaolo Bonzini     /*
8542f181fbdSPaolo Bonzini      * current_machine->accel is not NULL, but the machine properties have
8552f181fbdSPaolo Bonzini      * not been validated and machine_class->init has not yet been called.
8562f181fbdSPaolo Bonzini      */
8572f181fbdSPaolo Bonzini     PHASE_ACCEL_CREATED,
8582f181fbdSPaolo Bonzini 
8592f181fbdSPaolo Bonzini     /*
8602f181fbdSPaolo Bonzini      * machine_class->init has been called, thus creating any embedded
8612f181fbdSPaolo Bonzini      * devices and validating machine properties.  Devices created at
8622f181fbdSPaolo Bonzini      * this time are considered to be cold-plugged.
8632f181fbdSPaolo Bonzini      */
8642f181fbdSPaolo Bonzini     PHASE_MACHINE_INITIALIZED,
8652f181fbdSPaolo Bonzini 
8662f181fbdSPaolo Bonzini     /*
8672f181fbdSPaolo Bonzini      * QEMU is ready to start CPUs and devices created at this time
8682f181fbdSPaolo Bonzini      * are considered to be hot-plugged.  The monitor is not restricted
8692f181fbdSPaolo Bonzini      * to "preconfig" commands.
8702f181fbdSPaolo Bonzini      */
8712f181fbdSPaolo Bonzini     PHASE_MACHINE_READY,
8722f181fbdSPaolo Bonzini } MachineInitPhase;
8732f181fbdSPaolo Bonzini 
8742f181fbdSPaolo Bonzini extern bool phase_check(MachineInitPhase phase);
8752f181fbdSPaolo Bonzini extern void phase_advance(MachineInitPhase phase);
8762f181fbdSPaolo Bonzini 
877074a86fcSAnthony Liguori #endif
878