xref: /qemu/include/hw/qdev-core.h (revision 26462a700c8c5d30802c2254a35b5064762e00f0)
1074a86fcSAnthony Liguori #ifndef QDEV_CORE_H
2074a86fcSAnthony Liguori #define QDEV_CORE_H
3074a86fcSAnthony Liguori 
4*26462a70SStefan 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 
13074a86fcSAnthony Liguori enum {
14074a86fcSAnthony Liguori     DEV_NVECTORS_UNSPECIFIED = -1,
15074a86fcSAnthony Liguori };
16074a86fcSAnthony Liguori 
17074a86fcSAnthony Liguori #define TYPE_DEVICE "device"
18a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(DeviceState, DeviceClass, DEVICE)
19074a86fcSAnthony Liguori 
203d1237fbSMarcel Apfelbaum typedef enum DeviceCategory {
213d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_BRIDGE,
223d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_USB,
233d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_STORAGE,
243d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_NETWORK,
253d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_INPUT,
263d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_DISPLAY,
273d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_SOUND,
283d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MISC,
29ba31cc72SThomas Huth     DEVICE_CATEGORY_CPU,
30b10cb627SPaolo Bonzini     DEVICE_CATEGORY_WATCHDOG,
313d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MAX
323d1237fbSMarcel Apfelbaum } DeviceCategory;
333d1237fbSMarcel Apfelbaum 
34249d4172SAndreas Färber typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
35b69c3c21SMarkus Armbruster typedef void (*DeviceUnrealize)(DeviceState *dev);
36b850f664SPhilippe Mathieu-Daudé typedef void (*DeviceReset)(DeviceState *dev);
3702e7f85dSBandan Das typedef void (*BusRealize)(BusState *bus, Error **errp);
38b69c3c21SMarkus Armbruster typedef void (*BusUnrealize)(BusState *bus);
39074a86fcSAnthony Liguori 
40249d4172SAndreas Färber /**
41249d4172SAndreas Färber  * DeviceClass:
42249d4172SAndreas Färber  * @props: Properties accessing state fields.
43249d4172SAndreas Färber  * @realize: Callback function invoked when the #DeviceState:realized
44ff46d9d4SPhilippe Mathieu-Daudé  * property is changed to %true.
45249d4172SAndreas Färber  * @unrealize: Callback function invoked when the #DeviceState:realized
46249d4172SAndreas Färber  * property is changed to %false.
471a37eca1SIgor Mammedov  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
481a37eca1SIgor Mammedov  * as readonly "hotpluggable" property of #DeviceState instance
49249d4172SAndreas Färber  *
50249d4172SAndreas Färber  * # Realization #
51249d4172SAndreas Färber  * Devices are constructed in two stages,
52249d4172SAndreas Färber  * 1) object instantiation via object_initialize() and
53249d4172SAndreas Färber  * 2) device realization via #DeviceState:realized property.
546038f989SThomas Huth  * The former may not fail (and must not abort or exit, since it is called
556038f989SThomas Huth  * during device introspection already), and the latter may return error
566038f989SThomas Huth  * information to the caller and must be re-entrant.
57249d4172SAndreas Färber  * Trivial field initializations should go into #TypeInfo.instance_init.
58249d4172SAndreas Färber  * Operations depending on @props static properties should go into @realize.
59249d4172SAndreas Färber  * After successful realization, setting static properties will fail.
60249d4172SAndreas Färber  *
61daeba969SMarkus Armbruster  * As an interim step, the #DeviceState:realized property can also be
62c835fac3SMarkus Armbruster  * set with qdev_realize().
63249d4172SAndreas Färber  * In the future, devices will propagate this state change to their children
64249d4172SAndreas Färber  * and along busses they expose.
65249d4172SAndreas Färber  * The point in time will be deferred to machine creation, so that values
66249d4172SAndreas Färber  * set in @realize will not be introspectable beforehand. Therefore devices
67249d4172SAndreas Färber  * must not create children during @realize; they should initialize them via
68249d4172SAndreas Färber  * object_initialize() in their own #TypeInfo.instance_init and forward the
69249d4172SAndreas Färber  * realization events appropriately.
70249d4172SAndreas Färber  *
71249d4172SAndreas Färber  * Any type may override the @realize and/or @unrealize callbacks but needs
72782beb52SAndreas Färber  * to call the parent type's implementation if keeping their functionality
73782beb52SAndreas Färber  * is desired. Refer to QOM documentation for further discussion and examples.
74782beb52SAndreas Färber  *
75782beb52SAndreas Färber  * <note>
76782beb52SAndreas Färber  *   <para>
77ff46d9d4SPhilippe Mathieu-Daudé  * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
78ff46d9d4SPhilippe Mathieu-Daudé  * derived directly from it need not call their parent's @realize and
79ff46d9d4SPhilippe Mathieu-Daudé  * @unrealize.
80782beb52SAndreas Färber  * For other types consult the documentation and implementation of the
81782beb52SAndreas Färber  * respective parent types.
82782beb52SAndreas Färber  *   </para>
83782beb52SAndreas Färber  * </note>
84f3a85056SJens Freimann  *
85f3a85056SJens Freimann  * # Hiding a device #
86b91ad981SJuan Quintela  * To hide a device, a DeviceListener function hide_device() needs to
87f3a85056SJens Freimann  * be registered.
88b91ad981SJuan Quintela  * It can be used to defer adding a device and therefore hide it from
89b91ad981SJuan Quintela  * the guest. The handler registering to this DeviceListener can save
90b91ad981SJuan Quintela  * the QOpts passed to it for re-using it later. It must return if it
91b91ad981SJuan Quintela  * wants the device to be hidden or visible. When the handler function
92b91ad981SJuan Quintela  * decides the device shall be visible it will be added with
93b91ad981SJuan Quintela  * qdev_device_add() and realized as any other device. Otherwise
94b91ad981SJuan Quintela  * qdev_device_add() will return early without adding the device. The
95b91ad981SJuan Quintela  * guest will not see a "hidden" device until it was marked visible
96b91ad981SJuan Quintela  * and qdev_device_add called again.
97f3a85056SJens Freimann  *
98249d4172SAndreas Färber  */
99db1015e9SEduardo Habkost struct DeviceClass {
100249d4172SAndreas Färber     /*< private >*/
101074a86fcSAnthony Liguori     ObjectClass parent_class;
102249d4172SAndreas Färber     /*< public >*/
103074a86fcSAnthony Liguori 
1043d1237fbSMarcel Apfelbaum     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
105074a86fcSAnthony Liguori     const char *fw_name;
106074a86fcSAnthony Liguori     const char *desc;
107385d8f22SPaolo Bonzini 
108385d8f22SPaolo Bonzini     /*
109385d8f22SPaolo Bonzini      * The underscore at the end ensures a compile-time error if someone
110385d8f22SPaolo Bonzini      * assigns to dc->props instead of using device_class_set_props.
111385d8f22SPaolo Bonzini      */
112385d8f22SPaolo Bonzini     Property *props_;
113efec3dd6SMarkus Armbruster 
114efec3dd6SMarkus Armbruster     /*
115e90f2a8cSEduardo Habkost      * Can this device be instantiated with -device / device_add?
116efec3dd6SMarkus Armbruster      * All devices should support instantiation with device_add, and
117efec3dd6SMarkus Armbruster      * this flag should not exist.  But we're not there, yet.  Some
118efec3dd6SMarkus Armbruster      * devices fail to instantiate with cryptic error messages.
119efec3dd6SMarkus Armbruster      * Others instantiate, but don't work.  Exposing users to such
120e90f2a8cSEduardo Habkost      * behavior would be cruel; clearing this flag will protect them.
121e90f2a8cSEduardo Habkost      * It should never be cleared without a comment explaining why it
122e90f2a8cSEduardo Habkost      * is cleared.
123efec3dd6SMarkus Armbruster      * TODO remove once we're there
124efec3dd6SMarkus Armbruster      */
125e90f2a8cSEduardo Habkost     bool user_creatable;
1261a37eca1SIgor Mammedov     bool hotpluggable;
127074a86fcSAnthony Liguori 
128074a86fcSAnthony Liguori     /* callbacks */
129c11256aaSDamien Hedde     /*
130c11256aaSDamien Hedde      * Reset method here is deprecated and replaced by methods in the
131c11256aaSDamien Hedde      * resettable class interface to implement a multi-phase reset.
132c11256aaSDamien Hedde      * TODO: remove once every reset callback is unused
133c11256aaSDamien Hedde      */
134b850f664SPhilippe Mathieu-Daudé     DeviceReset reset;
135249d4172SAndreas Färber     DeviceRealize realize;
136249d4172SAndreas Färber     DeviceUnrealize unrealize;
137074a86fcSAnthony Liguori 
138074a86fcSAnthony Liguori     /* device state */
1398a9358ccSMarkus Armbruster     const VMStateDescription *vmsd;
140074a86fcSAnthony Liguori 
141074a86fcSAnthony Liguori     /* Private to qdev / bus.  */
142074a86fcSAnthony Liguori     const char *bus_type;
143db1015e9SEduardo Habkost };
144074a86fcSAnthony Liguori 
145a5f54290SPeter Crosthwaite typedef struct NamedGPIOList NamedGPIOList;
146a5f54290SPeter Crosthwaite 
147a5f54290SPeter Crosthwaite struct NamedGPIOList {
148a5f54290SPeter Crosthwaite     char *name;
149a5f54290SPeter Crosthwaite     qemu_irq *in;
150a5f54290SPeter Crosthwaite     int num_in;
151a5f54290SPeter Crosthwaite     int num_out;
152a5f54290SPeter Crosthwaite     QLIST_ENTRY(NamedGPIOList) node;
153a5f54290SPeter Crosthwaite };
154a5f54290SPeter Crosthwaite 
1550e6934f2SDamien Hedde typedef struct Clock Clock;
1560e6934f2SDamien Hedde typedef struct NamedClockList NamedClockList;
1570e6934f2SDamien Hedde 
1580e6934f2SDamien Hedde struct NamedClockList {
1590e6934f2SDamien Hedde     char *name;
1600e6934f2SDamien Hedde     Clock *clock;
1610e6934f2SDamien Hedde     bool output;
1620e6934f2SDamien Hedde     bool alias;
1630e6934f2SDamien Hedde     QLIST_ENTRY(NamedClockList) node;
1640e6934f2SDamien Hedde };
1650e6934f2SDamien Hedde 
166a2e1753bSAlexander Bulekov typedef struct {
167a2e1753bSAlexander Bulekov     bool engaged_in_io;
168a2e1753bSAlexander Bulekov } MemReentrancyGuard;
169a2e1753bSAlexander Bulekov 
1707983c8a3SAndreas Färber /**
1717983c8a3SAndreas Färber  * DeviceState:
172c11256aaSDamien Hedde  * @reset: ResettableState for the device; handled by Resettable interface.
1737983c8a3SAndreas Färber  *
1747983c8a3SAndreas Färber  * This structure should not be accessed directly.  We declare it here
1757983c8a3SAndreas Färber  * so that it can be embedded in individual device state structures.
1767983c8a3SAndreas Färber  */
177074a86fcSAnthony Liguori struct DeviceState {
1787983c8a3SAndreas Färber     /*< private >*/
179074a86fcSAnthony Liguori     Object parent_obj;
1807983c8a3SAndreas Färber     /*< public >*/
181074a86fcSAnthony Liguori 
182163f3847SKevin Wolf     char *id;
18304162f8fSMichael Roth     char *canonical_path;
1847983c8a3SAndreas Färber     bool realized;
185352e8da7SPaolo Bonzini     bool pending_deleted_event;
18618416c62SGerd Hoffmann     int64_t pending_deleted_expires_ms;
187f3558b1bSKevin Wolf     QDict *opts;
188074a86fcSAnthony Liguori     int hotplugged;
189a1190ab6SJens Freimann     bool allow_unplug_during_migration;
190074a86fcSAnthony Liguori     BusState *parent_bus;
191a5f54290SPeter Crosthwaite     QLIST_HEAD(, NamedGPIOList) gpios;
1920e6934f2SDamien Hedde     QLIST_HEAD(, NamedClockList) clocks;
193074a86fcSAnthony Liguori     QLIST_HEAD(, BusState) child_bus;
194074a86fcSAnthony Liguori     int num_child_bus;
195074a86fcSAnthony Liguori     int instance_id_alias;
196074a86fcSAnthony Liguori     int alias_required_for_version;
197c11256aaSDamien Hedde     ResettableState reset;
198217c7f01SJagannathan Raman     GSList *unplug_blockers;
199a2e1753bSAlexander Bulekov 
200a2e1753bSAlexander Bulekov     /* Is the device currently in mmio/pio/dma? Used to prevent re-entrancy */
201a2e1753bSAlexander Bulekov     MemReentrancyGuard mem_reentrancy_guard;
202074a86fcSAnthony Liguori };
203074a86fcSAnthony Liguori 
204707ff800SPaul Durrant struct DeviceListener {
205707ff800SPaul Durrant     void (*realize)(DeviceListener *listener, DeviceState *dev);
206707ff800SPaul Durrant     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
207f3a85056SJens Freimann     /*
208b91ad981SJuan Quintela      * This callback is called upon init of the DeviceState and
209b91ad981SJuan Quintela      * informs qdev if a device should be visible or hidden.  We can
210b91ad981SJuan Quintela      * hide a failover device depending for example on the device
211b91ad981SJuan Quintela      * opts.
2127d618082SKevin Wolf      *
2137d618082SKevin Wolf      * On errors, it returns false and errp is set. Device creation
2147d618082SKevin Wolf      * should fail in this case.
215f3a85056SJens Freimann      */
216f3558b1bSKevin Wolf     bool (*hide_device)(DeviceListener *listener, const QDict *device_opts,
217f3558b1bSKevin Wolf                         bool from_json, Error **errp);
218707ff800SPaul Durrant     QTAILQ_ENTRY(DeviceListener) link;
219707ff800SPaul Durrant };
220707ff800SPaul Durrant 
221074a86fcSAnthony Liguori #define TYPE_BUS "bus"
2228110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(BusState, BusClass,
2238110fa1dSEduardo Habkost                      BUS, TYPE_BUS)
224074a86fcSAnthony Liguori 
225074a86fcSAnthony Liguori struct BusClass {
226074a86fcSAnthony Liguori     ObjectClass parent_class;
227074a86fcSAnthony Liguori 
228074a86fcSAnthony Liguori     /* FIXME first arg should be BusState */
229074a86fcSAnthony Liguori     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
230074a86fcSAnthony Liguori     char *(*get_dev_path)(DeviceState *dev);
231bb755ba4SPaolo Bonzini 
232074a86fcSAnthony Liguori     /*
233074a86fcSAnthony Liguori      * This callback is used to create Open Firmware device path in accordance
234074a86fcSAnthony Liguori      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
235074a86fcSAnthony Liguori      * bindings can be found at http://playground.sun.com/1275/bindings/.
236074a86fcSAnthony Liguori      */
237074a86fcSAnthony Liguori     char *(*get_fw_dev_path)(DeviceState *dev);
238bb755ba4SPaolo Bonzini 
239dcc20931SPaolo Bonzini     void (*reset)(BusState *bus);
240bb755ba4SPaolo Bonzini 
241bb755ba4SPaolo Bonzini     /*
242bb755ba4SPaolo Bonzini      * Return whether the device can be added to @bus,
243bb755ba4SPaolo Bonzini      * based on the address that was set (via device properties)
244bb755ba4SPaolo Bonzini      * before realize.  If not, on return @errp contains the
245bb755ba4SPaolo Bonzini      * human-readable error message.
246bb755ba4SPaolo Bonzini      */
247bb755ba4SPaolo Bonzini     bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
248bb755ba4SPaolo Bonzini 
24902e7f85dSBandan Das     BusRealize realize;
25002e7f85dSBandan Das     BusUnrealize unrealize;
25102e7f85dSBandan Das 
2521395af6fSKONRAD Frederic     /* maximum devices allowed on the bus, 0: no limit. */
2531395af6fSKONRAD Frederic     int max_dev;
25461de3676SAlexander Graf     /* number of automatically allocated bus ids (e.g. ide.0) */
25561de3676SAlexander Graf     int automatic_ids;
256074a86fcSAnthony Liguori };
257074a86fcSAnthony Liguori 
258074a86fcSAnthony Liguori typedef struct BusChild {
2592d24a646SMaxim Levitsky     struct rcu_head rcu;
260074a86fcSAnthony Liguori     DeviceState *child;
261074a86fcSAnthony Liguori     int index;
262074a86fcSAnthony Liguori     QTAILQ_ENTRY(BusChild) sibling;
263074a86fcSAnthony Liguori } BusChild;
264074a86fcSAnthony Liguori 
2650ee4de6cSIgor Mammedov #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
2660ee4de6cSIgor Mammedov 
267074a86fcSAnthony Liguori /**
268074a86fcSAnthony Liguori  * BusState:
26927c6ef1bSLi Qiang  * @hotplug_handler: link to a hotplug handler associated with bus.
270c11256aaSDamien Hedde  * @reset: ResettableState for the bus; handled by Resettable interface.
271074a86fcSAnthony Liguori  */
272074a86fcSAnthony Liguori struct BusState {
273074a86fcSAnthony Liguori     Object obj;
274074a86fcSAnthony Liguori     DeviceState *parent;
275f73480c3SMarc-André Lureau     char *name;
2760ee4de6cSIgor Mammedov     HotplugHandler *hotplug_handler;
277074a86fcSAnthony Liguori     int max_index;
27802e7f85dSBandan Das     bool realized;
2791518562bSPeter Maydell     bool full;
28012b2e9f3STony Krowiak     int num_children;
2812d24a646SMaxim Levitsky 
2822d24a646SMaxim Levitsky     /*
2832d24a646SMaxim Levitsky      * children is a RCU QTAILQ, thus readers must use RCU to access it,
2842d24a646SMaxim Levitsky      * and writers must hold the big qemu lock
2852d24a646SMaxim Levitsky      */
2862d24a646SMaxim Levitsky 
287eae3eb3eSPaolo Bonzini     QTAILQ_HEAD(, BusChild) children;
288074a86fcSAnthony Liguori     QLIST_ENTRY(BusState) sibling;
289c11256aaSDamien Hedde     ResettableState reset;
290074a86fcSAnthony Liguori };
291074a86fcSAnthony Liguori 
2925cc56cc6SPeter Maydell /**
2939f9260a3SDon Slutz  * GlobalProperty:
294b3ce84feSEduardo Habkost  * @used: Set to true if property was used when initializing a device.
29592fd453cSDr. David Alan Gilbert  * @optional: If set to true, GlobalProperty will be skipped without errors
29692fd453cSDr. David Alan Gilbert  *            if the property doesn't exist.
297cff8b715SMarc-André Lureau  *
298cff8b715SMarc-André Lureau  * An error is fatal for non-hotplugged devices, when the global is applied.
2999f9260a3SDon Slutz  */
300074a86fcSAnthony Liguori typedef struct GlobalProperty {
301074a86fcSAnthony Liguori     const char *driver;
302074a86fcSAnthony Liguori     const char *property;
303074a86fcSAnthony Liguori     const char *value;
304b3ce84feSEduardo Habkost     bool used;
30592fd453cSDr. David Alan Gilbert     bool optional;
306074a86fcSAnthony Liguori } GlobalProperty;
307074a86fcSAnthony Liguori 
308ea9ce893SMarc-André Lureau static inline void
309ea9ce893SMarc-André Lureau compat_props_add(GPtrArray *arr,
310ea9ce893SMarc-André Lureau                  GlobalProperty props[], size_t nelem)
311ea9ce893SMarc-André Lureau {
312ea9ce893SMarc-André Lureau     int i;
313ea9ce893SMarc-André Lureau     for (i = 0; i < nelem; i++) {
314ea9ce893SMarc-André Lureau         g_ptr_array_add(arr, (void *)&props[i]);
315ea9ce893SMarc-André Lureau     }
316ea9ce893SMarc-André Lureau }
317ea9ce893SMarc-André Lureau 
318074a86fcSAnthony Liguori /*** Board API.  This should go away once we have a machine config file.  ***/
319074a86fcSAnthony Liguori 
320b51238e2SPeter Maydell /**
321b51238e2SPeter Maydell  * qdev_new: Create a device on the heap
322b51238e2SPeter Maydell  * @name: device type to create (we assert() that this type exists)
323b51238e2SPeter Maydell  *
324b51238e2SPeter Maydell  * This only allocates the memory and initializes the device state
325b51238e2SPeter Maydell  * structure, ready for the caller to set properties if they wish.
326b51238e2SPeter Maydell  * The device still needs to be realized.
327b51238e2SPeter Maydell  * The returned object has a reference count of 1.
328b51238e2SPeter Maydell  */
3299940b2cfSMarkus Armbruster DeviceState *qdev_new(const char *name);
330694804edSPhilippe Mathieu-Daudé 
331b51238e2SPeter Maydell /**
332b51238e2SPeter Maydell  * qdev_try_new: Try to create a device on the heap
333b51238e2SPeter Maydell  * @name: device type to create
334b51238e2SPeter Maydell  *
335b51238e2SPeter Maydell  * This is like qdev_new(), except it returns %NULL when type @name
336b51238e2SPeter Maydell  * does not exist, rather than asserting.
337b51238e2SPeter Maydell  */
3389940b2cfSMarkus Armbruster DeviceState *qdev_try_new(const char *name);
339694804edSPhilippe Mathieu-Daudé 
340b51238e2SPeter Maydell /**
341*26462a70SStefan Hajnoczi  * qdev_is_realized:
342*26462a70SStefan Hajnoczi  * @dev: The device to check.
343*26462a70SStefan Hajnoczi  *
344*26462a70SStefan Hajnoczi  * May be called outside big qemu lock.
345*26462a70SStefan Hajnoczi  *
346*26462a70SStefan Hajnoczi  * Returns: %true% if the device has been fully constructed, %false% otherwise.
347*26462a70SStefan Hajnoczi  */
348*26462a70SStefan Hajnoczi static inline bool qdev_is_realized(DeviceState *dev)
349*26462a70SStefan Hajnoczi {
350*26462a70SStefan Hajnoczi     return qatomic_load_acquire(&dev->realized);
351*26462a70SStefan Hajnoczi }
352*26462a70SStefan Hajnoczi 
353*26462a70SStefan Hajnoczi /**
354b51238e2SPeter Maydell  * qdev_realize: Realize @dev.
355b51238e2SPeter Maydell  * @dev: device to realize
356b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
357b51238e2SPeter Maydell  * @errp: pointer to error object
358b51238e2SPeter Maydell  *
359b51238e2SPeter Maydell  * "Realize" the device, i.e. perform the second phase of device
360b51238e2SPeter Maydell  * initialization.
361b51238e2SPeter Maydell  * @dev must not be plugged into a bus already.
362b51238e2SPeter Maydell  * If @bus, plug @dev into @bus.  This takes a reference to @dev.
363b51238e2SPeter Maydell  * If @dev has no QOM parent, make one up, taking another reference.
364b51238e2SPeter Maydell  * On success, return true.
365b51238e2SPeter Maydell  * On failure, store an error through @errp and return false.
366b51238e2SPeter Maydell  *
367b51238e2SPeter Maydell  * If you created @dev using qdev_new(), you probably want to use
368b51238e2SPeter Maydell  * qdev_realize_and_unref() instead.
369b51238e2SPeter Maydell  */
3709940b2cfSMarkus Armbruster bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
371694804edSPhilippe Mathieu-Daudé 
372b51238e2SPeter Maydell /**
373b51238e2SPeter Maydell  * qdev_realize_and_unref: Realize @dev and drop a reference
374b51238e2SPeter Maydell  * @dev: device to realize
375b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
376b51238e2SPeter Maydell  * @errp: pointer to error object
377b51238e2SPeter Maydell  *
378b51238e2SPeter Maydell  * Realize @dev and drop a reference.
379b51238e2SPeter Maydell  * This is like qdev_realize(), except the caller must hold a
380b51238e2SPeter Maydell  * (private) reference, which is dropped on return regardless of
381b51238e2SPeter Maydell  * success or failure.  Intended use::
382b51238e2SPeter Maydell  *
383b51238e2SPeter Maydell  *     dev = qdev_new();
384b51238e2SPeter Maydell  *     [...]
385b51238e2SPeter Maydell  *     qdev_realize_and_unref(dev, bus, errp);
386b51238e2SPeter Maydell  *
387b51238e2SPeter Maydell  * Now @dev can go away without further ado.
388b51238e2SPeter Maydell  *
389b51238e2SPeter Maydell  * If you are embedding the device into some other QOM device and
390b51238e2SPeter Maydell  * initialized it via some variant on object_initialize_child() then
391b51238e2SPeter Maydell  * do not use this function, because that family of functions arrange
392b51238e2SPeter Maydell  * for the only reference to the child device to be held by the parent
393b51238e2SPeter Maydell  * via the child<> property, and so the reference-count-drop done here
394b51238e2SPeter Maydell  * would be incorrect. For that use case you want qdev_realize().
395b51238e2SPeter Maydell  */
3969940b2cfSMarkus Armbruster bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
397694804edSPhilippe Mathieu-Daudé 
39846ea1be1SPeter Maydell /**
39946ea1be1SPeter Maydell  * qdev_unrealize: Unrealize a device
40046ea1be1SPeter Maydell  * @dev: device to unrealize
40146ea1be1SPeter Maydell  *
40246ea1be1SPeter Maydell  * This function will "unrealize" a device, which is the first phase
40346ea1be1SPeter Maydell  * of correctly destroying a device that has been realized. It will:
40446ea1be1SPeter Maydell  *
40546ea1be1SPeter Maydell  *  - unrealize any child buses by calling qbus_unrealize()
40646ea1be1SPeter Maydell  *    (this will recursively unrealize any devices on those buses)
4077a21bee2SDaniel P. Berrangé  *  - call the unrealize method of @dev
40846ea1be1SPeter Maydell  *
40946ea1be1SPeter Maydell  * The device can then be freed by causing its reference count to go
41046ea1be1SPeter Maydell  * to zero.
41146ea1be1SPeter Maydell  *
41246ea1be1SPeter Maydell  * Warning: most devices in QEMU do not expect to be unrealized.  Only
41346ea1be1SPeter Maydell  * devices which are hot-unpluggable should be unrealized (as part of
41446ea1be1SPeter Maydell  * the unplugging process); all other devices are expected to last for
41546ea1be1SPeter Maydell  * the life of the simulation and should not be unrealized and freed.
41646ea1be1SPeter Maydell  */
4179940b2cfSMarkus Armbruster void qdev_unrealize(DeviceState *dev);
418074a86fcSAnthony Liguori void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
419074a86fcSAnthony Liguori                                  int required_for_version);
42014405c27SDavid Hildenbrand HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
42103fcbd9dSThomas Huth HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
422d2321d31SPeter Xu bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
42317cc0128SIgor Mammedov /**
42417cc0128SIgor Mammedov  * qdev_get_hotplug_handler: Get handler responsible for device wiring
42517cc0128SIgor Mammedov  *
42617cc0128SIgor Mammedov  * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
42717cc0128SIgor Mammedov  *
42817cc0128SIgor Mammedov  * Note: in case @dev has a parent bus, it will be returned as handler unless
42917cc0128SIgor Mammedov  * machine handler overrides it.
43017cc0128SIgor Mammedov  *
43117cc0128SIgor Mammedov  * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
43217cc0128SIgor Mammedov  *          or NULL if there aren't any.
43317cc0128SIgor Mammedov  */
434c06b2ffbSZhu Guihua HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
435074a86fcSAnthony Liguori void qdev_unplug(DeviceState *dev, Error **errp);
436014176f9SIgor Mammedov void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
437014176f9SIgor Mammedov                                   DeviceState *dev, Error **errp);
438074a86fcSAnthony Liguori void qdev_machine_creation_done(void);
439074a86fcSAnthony Liguori bool qdev_machine_modified(void);
440074a86fcSAnthony Liguori 
441cd07d7f9SPeter Maydell /**
442217c7f01SJagannathan Raman  * qdev_add_unplug_blocker: Add an unplug blocker to a device
443217c7f01SJagannathan Raman  *
444217c7f01SJagannathan Raman  * @dev: Device to be blocked from unplug
445217c7f01SJagannathan Raman  * @reason: Reason for blocking
446217c7f01SJagannathan Raman  */
447217c7f01SJagannathan Raman void qdev_add_unplug_blocker(DeviceState *dev, Error *reason);
448217c7f01SJagannathan Raman 
449217c7f01SJagannathan Raman /**
450217c7f01SJagannathan Raman  * qdev_del_unplug_blocker: Remove an unplug blocker from a device
451217c7f01SJagannathan Raman  *
452217c7f01SJagannathan Raman  * @dev: Device to be unblocked
453217c7f01SJagannathan Raman  * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
454217c7f01SJagannathan Raman  *          Used as a handle to lookup the blocker for deletion.
455217c7f01SJagannathan Raman  */
456217c7f01SJagannathan Raman void qdev_del_unplug_blocker(DeviceState *dev, Error *reason);
457217c7f01SJagannathan Raman 
458217c7f01SJagannathan Raman /**
459217c7f01SJagannathan Raman  * qdev_unplug_blocked: Confirm if a device is blocked from unplug
460217c7f01SJagannathan Raman  *
461217c7f01SJagannathan Raman  * @dev: Device to be tested
462217c7f01SJagannathan Raman  * @reason: Returns one of the reasons why the device is blocked,
463217c7f01SJagannathan Raman  *          if any
464217c7f01SJagannathan Raman  *
465217c7f01SJagannathan Raman  * Returns: true if device is blocked from unplug, false otherwise
466217c7f01SJagannathan Raman  */
467217c7f01SJagannathan Raman bool qdev_unplug_blocked(DeviceState *dev, Error **errp);
468217c7f01SJagannathan Raman 
469217c7f01SJagannathan Raman /**
470ddb67f64SPhilippe Mathieu-Daudé  * GpioPolarity: Polarity of a GPIO line
471ddb67f64SPhilippe Mathieu-Daudé  *
472ddb67f64SPhilippe Mathieu-Daudé  * GPIO lines use either positive (active-high) logic,
473ddb67f64SPhilippe Mathieu-Daudé  * or negative (active-low) logic.
474ddb67f64SPhilippe Mathieu-Daudé  *
475ddb67f64SPhilippe Mathieu-Daudé  * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
476ddb67f64SPhilippe Mathieu-Daudé  * active when the voltage on the pin is high (relative to ground);
477ddb67f64SPhilippe Mathieu-Daudé  * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
478ddb67f64SPhilippe Mathieu-Daudé  * is active when the voltage on the pin is low (or grounded).
479ddb67f64SPhilippe Mathieu-Daudé  */
480ddb67f64SPhilippe Mathieu-Daudé typedef enum {
481ddb67f64SPhilippe Mathieu-Daudé     GPIO_POLARITY_ACTIVE_LOW,
482ddb67f64SPhilippe Mathieu-Daudé     GPIO_POLARITY_ACTIVE_HIGH
483ddb67f64SPhilippe Mathieu-Daudé } GpioPolarity;
484ddb67f64SPhilippe Mathieu-Daudé 
485ddb67f64SPhilippe Mathieu-Daudé /**
486cd07d7f9SPeter Maydell  * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
487cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
488cd07d7f9SPeter Maydell  * @n: Number of the anonymous GPIO line (which must be in range)
489cd07d7f9SPeter Maydell  *
490cd07d7f9SPeter Maydell  * Returns the qemu_irq corresponding to an anonymous input GPIO line
491cd07d7f9SPeter Maydell  * (which the device has set up with qdev_init_gpio_in()). The index
492cd07d7f9SPeter Maydell  * @n of the GPIO line must be valid (i.e. be at least 0 and less than
493cd07d7f9SPeter Maydell  * the total number of anonymous input GPIOs the device has); this
494cd07d7f9SPeter Maydell  * function will assert() if passed an invalid index.
495cd07d7f9SPeter Maydell  *
496cd07d7f9SPeter Maydell  * This function is intended to be used by board code or SoC "container"
497cd07d7f9SPeter Maydell  * device models to wire up the GPIO lines; usually the return value
498cd07d7f9SPeter Maydell  * will be passed to qdev_connect_gpio_out() or a similar function to
499cd07d7f9SPeter Maydell  * connect another device's output GPIO line to this input.
500cd07d7f9SPeter Maydell  *
501cd07d7f9SPeter Maydell  * For named input GPIO lines, use qdev_get_gpio_in_named().
502cd07d7f9SPeter Maydell  */
503074a86fcSAnthony Liguori qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
504694804edSPhilippe Mathieu-Daudé 
505cd07d7f9SPeter Maydell /**
506cd07d7f9SPeter Maydell  * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
507cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
508cd07d7f9SPeter Maydell  * @name: Name of the input GPIO array
509cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in that array (which must be in range)
510cd07d7f9SPeter Maydell  *
511cd07d7f9SPeter Maydell  * Returns the qemu_irq corresponding to a named input GPIO line
512cd07d7f9SPeter Maydell  * (which the device has set up with qdev_init_gpio_in_named()).
513cd07d7f9SPeter Maydell  * The @name string must correspond to an input GPIO array which exists on
514cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
515cd07d7f9SPeter Maydell  * be at least 0 and less than the total number of input GPIOs in that
516cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
517cd07d7f9SPeter Maydell  *
518cd07d7f9SPeter Maydell  * For anonymous input GPIO lines, use qdev_get_gpio_in().
519cd07d7f9SPeter Maydell  */
520a5f54290SPeter Crosthwaite qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
521a5f54290SPeter Crosthwaite 
522cd07d7f9SPeter Maydell /**
523cd07d7f9SPeter Maydell  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
524cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
525cd07d7f9SPeter Maydell  * @n: Number of the anonymous output GPIO line (which must be in range)
5262ebd9ce1SPhilippe Mathieu-Daudé  * @input_pin: qemu_irq to connect the output line to
527cd07d7f9SPeter Maydell  *
528cd07d7f9SPeter Maydell  * This function connects an anonymous output GPIO line on a device
529cd07d7f9SPeter Maydell  * up to an arbitrary qemu_irq, so that when the device asserts that
530cd07d7f9SPeter Maydell  * output GPIO line, the qemu_irq's callback is invoked.
531cd07d7f9SPeter Maydell  * The index @n of the GPIO line must be valid (i.e. be at least 0 and
532cd07d7f9SPeter Maydell  * less than the total number of anonymous output GPIOs the device has
533cd07d7f9SPeter Maydell  * created with qdev_init_gpio_out()); otherwise this function will assert().
534cd07d7f9SPeter Maydell  *
535cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
536cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
537cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
538cd07d7f9SPeter Maydell  *
539cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
540cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
541cd07d7f9SPeter Maydell  * same qemu_irq. (Warning: there is no assertion or other guard to
542cd07d7f9SPeter Maydell  * catch this error: the model will just not do the right thing.)
5435df69ab8SPeter Maydell  * Instead, for fan-out you can use the TYPE_SPLIT_IRQ device: connect
544cd07d7f9SPeter Maydell  * a device's outbound GPIO to the splitter's input, and connect each
545cd07d7f9SPeter Maydell  * of the splitter's outputs to a different device.  For fan-in you
546cd07d7f9SPeter Maydell  * can use the TYPE_OR_IRQ device, which is a model of a logical OR
547cd07d7f9SPeter Maydell  * gate with multiple inputs and one output.
548cd07d7f9SPeter Maydell  *
549cd07d7f9SPeter Maydell  * For named output GPIO lines, use qdev_connect_gpio_out_named().
550cd07d7f9SPeter Maydell  */
551074a86fcSAnthony Liguori void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
552694804edSPhilippe Mathieu-Daudé 
553cd07d7f9SPeter Maydell /**
5541fbd004bSPhilippe Mathieu-Daudé  * qdev_connect_gpio_out_named: Connect one of a device's named output
5551fbd004bSPhilippe Mathieu-Daudé  *                              GPIO lines
556cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
557cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
558cd07d7f9SPeter Maydell  * @n: Number of the anonymous output GPIO line (which must be in range)
5592ebd9ce1SPhilippe Mathieu-Daudé  * @input_pin: qemu_irq to connect the output line to
560cd07d7f9SPeter Maydell  *
561cd07d7f9SPeter Maydell  * This function connects an anonymous output GPIO line on a device
562cd07d7f9SPeter Maydell  * up to an arbitrary qemu_irq, so that when the device asserts that
563cd07d7f9SPeter Maydell  * output GPIO line, the qemu_irq's callback is invoked.
564cd07d7f9SPeter Maydell  * The @name string must correspond to an output GPIO array which exists on
565cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
566cd07d7f9SPeter Maydell  * be at least 0 and less than the total number of input GPIOs in that
567cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
568cd07d7f9SPeter Maydell  *
569cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
570cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
571cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
572cd07d7f9SPeter Maydell  *
573cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
574cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
575cd07d7f9SPeter Maydell  * same qemu_irq; see qdev_connect_gpio_out() for details.
576cd07d7f9SPeter Maydell  *
5771fbd004bSPhilippe Mathieu-Daudé  * For anonymous output GPIO lines, use qdev_connect_gpio_out().
578cd07d7f9SPeter Maydell  */
579a5f54290SPeter Crosthwaite void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
5802ebd9ce1SPhilippe Mathieu-Daudé                                  qemu_irq input_pin);
581694804edSPhilippe Mathieu-Daudé 
582cd07d7f9SPeter Maydell /**
583cd07d7f9SPeter Maydell  * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
584cd07d7f9SPeter Maydell  * @dev: Device whose output GPIO we are interested in
585cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
586cd07d7f9SPeter Maydell  * @n: Number of the output GPIO line within that array
587cd07d7f9SPeter Maydell  *
588cd07d7f9SPeter Maydell  * Returns whatever qemu_irq is currently connected to the specified
589cd07d7f9SPeter Maydell  * output GPIO line of @dev. This will be NULL if the output GPIO line
590cd07d7f9SPeter Maydell  * has never been wired up to the anything.  Note that the qemu_irq
591cd07d7f9SPeter Maydell  * returned does not belong to @dev -- it will be the input GPIO or
592cd07d7f9SPeter Maydell  * IRQ of whichever device the board code has connected up to @dev's
593cd07d7f9SPeter Maydell  * output GPIO.
594cd07d7f9SPeter Maydell  *
595cd07d7f9SPeter Maydell  * You probably don't need to use this function -- it is used only
596cd07d7f9SPeter Maydell  * by the platform-bus subsystem.
597cd07d7f9SPeter Maydell  */
598b7973186SAlexander Graf qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
599694804edSPhilippe Mathieu-Daudé 
600cd07d7f9SPeter Maydell /**
601cd07d7f9SPeter Maydell  * qdev_intercept_gpio_out: Intercept an existing GPIO connection
602cd07d7f9SPeter Maydell  * @dev: Device to intercept the outbound GPIO line from
603cd07d7f9SPeter Maydell  * @icpt: New qemu_irq to connect instead
604cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
605cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in the array
606cd07d7f9SPeter Maydell  *
607cd07d7f9SPeter Maydell  * This function is provided only for use by the qtest testing framework
608cd07d7f9SPeter Maydell  * and is not suitable for use in non-testing parts of QEMU.
609cd07d7f9SPeter Maydell  *
610cd07d7f9SPeter Maydell  * This function breaks an existing connection of an outbound GPIO
611cd07d7f9SPeter Maydell  * line from @dev, and replaces it with the new qemu_irq @icpt, as if
612cd07d7f9SPeter Maydell  * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
613cd07d7f9SPeter Maydell  * The previously connected qemu_irq is returned, so it can be restored
614cd07d7f9SPeter Maydell  * by a second call to qdev_intercept_gpio_out() if desired.
615cd07d7f9SPeter Maydell  */
6160c24db2bSPeter Crosthwaite qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
6170c24db2bSPeter Crosthwaite                                  const char *name, int n);
618074a86fcSAnthony Liguori 
619074a86fcSAnthony Liguori BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
620074a86fcSAnthony Liguori 
621074a86fcSAnthony Liguori /*** Device API.  ***/
622074a86fcSAnthony Liguori 
623cd07d7f9SPeter Maydell /**
624cd07d7f9SPeter Maydell  * qdev_init_gpio_in: create an array of anonymous input GPIO lines
625cd07d7f9SPeter Maydell  * @dev: Device to create input GPIOs for
626cd07d7f9SPeter Maydell  * @handler: Function to call when GPIO line value is set
627cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
628cd07d7f9SPeter Maydell  *
629cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_in* family in
630cd07d7f9SPeter Maydell  * their instance_init or realize methods to create any input GPIO
631cd07d7f9SPeter Maydell  * lines they need. There is no functional difference between
632cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
633cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
634cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO input whose purpose is obvious.
635cd07d7f9SPeter Maydell  * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
636cd07d7f9SPeter Maydell  *
637cd07d7f9SPeter Maydell  * See qdev_get_gpio_in() for how code that uses such a device can get
638cd07d7f9SPeter Maydell  * hold of an input GPIO line to manipulate it.
639cd07d7f9SPeter Maydell  */
640074a86fcSAnthony Liguori void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
641694804edSPhilippe Mathieu-Daudé 
642cd07d7f9SPeter Maydell /**
643cd07d7f9SPeter Maydell  * qdev_init_gpio_out: create an array of anonymous output GPIO lines
644cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
645cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
646cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
647cd07d7f9SPeter Maydell  *
648cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_out* family
649cd07d7f9SPeter Maydell  * in their instance_init or realize methods to create any output
650cd07d7f9SPeter Maydell  * GPIO lines they need. There is no functional difference between
651cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
652cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
653cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO output whose purpose is obvious.
654cd07d7f9SPeter Maydell  *
655cd07d7f9SPeter Maydell  * The @pins argument should be a pointer to either a "qemu_irq"
656cd07d7f9SPeter Maydell  * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
657cd07d7f9SPeter Maydell  * state structure. The device implementation can then raise and
658cd07d7f9SPeter Maydell  * lower the GPIO line by calling qemu_set_irq(). (If anything is
659cd07d7f9SPeter Maydell  * connected to the other end of the GPIO this will cause the handler
660cd07d7f9SPeter Maydell  * function for that input GPIO to be called.)
661cd07d7f9SPeter Maydell  *
662cd07d7f9SPeter Maydell  * See qdev_connect_gpio_out() for how code that uses such a device
663cd07d7f9SPeter Maydell  * can connect to one of its output GPIO lines.
664526dc840SPhilippe Mathieu-Daudé  *
665526dc840SPhilippe Mathieu-Daudé  * There is no need to release the @pins allocated array because it
666526dc840SPhilippe Mathieu-Daudé  * will be automatically released when @dev calls its instance_finalize()
667526dc840SPhilippe Mathieu-Daudé  * handler.
668cd07d7f9SPeter Maydell  */
669074a86fcSAnthony Liguori void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
670694804edSPhilippe Mathieu-Daudé 
671cd07d7f9SPeter Maydell /**
67214b0375bSPhilippe Mathieu-Daudé  * qdev_init_gpio_out_named: create an array of named output GPIO lines
673cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
674cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
675cd07d7f9SPeter Maydell  * @name: Name to give this array of GPIO lines
676cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
677cd07d7f9SPeter Maydell  *
678cd07d7f9SPeter Maydell  * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
679cd07d7f9SPeter Maydell  * with a name. Code using the device can then connect these GPIO lines
680cd07d7f9SPeter Maydell  * using qdev_connect_gpio_out_named().
681cd07d7f9SPeter Maydell  */
682a5f54290SPeter Crosthwaite void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
683a5f54290SPeter Crosthwaite                               const char *name, int n);
684694804edSPhilippe Mathieu-Daudé 
6854a151677SPeter Maydell /**
6864a151677SPeter Maydell  * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
6874a151677SPeter Maydell  *   for the specified device
6884a151677SPeter Maydell  *
6894a151677SPeter Maydell  * @dev: Device to create input GPIOs for
6904a151677SPeter Maydell  * @handler: Function to call when GPIO line value is set
6914a151677SPeter Maydell  * @opaque: Opaque data pointer to pass to @handler
6924a151677SPeter Maydell  * @name: Name of the GPIO input (must be unique for this device)
6934a151677SPeter Maydell  * @n: Number of GPIO lines in this input set
6944a151677SPeter Maydell  */
6954a151677SPeter Maydell void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
6964a151677SPeter Maydell                                          qemu_irq_handler handler,
6974a151677SPeter Maydell                                          void *opaque,
6984a151677SPeter Maydell                                          const char *name, int n);
6994a151677SPeter Maydell 
7004a151677SPeter Maydell /**
7014a151677SPeter Maydell  * qdev_init_gpio_in_named: create an array of input GPIO lines
7024a151677SPeter Maydell  *   for the specified device
7034a151677SPeter Maydell  *
7044a151677SPeter Maydell  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
7054a151677SPeter Maydell  * passed to the handler is @dev (which is the most commonly desired behaviour).
7064a151677SPeter Maydell  */
7074a151677SPeter Maydell static inline void qdev_init_gpio_in_named(DeviceState *dev,
7084a151677SPeter Maydell                                            qemu_irq_handler handler,
7094a151677SPeter Maydell                                            const char *name, int n)
7104a151677SPeter Maydell {
7114a151677SPeter Maydell     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
7124a151677SPeter Maydell }
713074a86fcSAnthony Liguori 
714cd07d7f9SPeter Maydell /**
715cd07d7f9SPeter Maydell  * qdev_pass_gpios: create GPIO lines on container which pass through to device
716cd07d7f9SPeter Maydell  * @dev: Device which has GPIO lines
717cd07d7f9SPeter Maydell  * @container: Container device which needs to expose them
718cd07d7f9SPeter Maydell  * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
719cd07d7f9SPeter Maydell  *
720cd07d7f9SPeter Maydell  * In QEMU, complicated devices like SoCs are often modelled with a
721cd07d7f9SPeter Maydell  * "container" QOM device which itself contains other QOM devices and
722cd07d7f9SPeter Maydell  * which wires them up appropriately. This function allows the container
723cd07d7f9SPeter Maydell  * to create GPIO arrays on itself which simply pass through to a GPIO
724cd07d7f9SPeter Maydell  * array of one of its internal devices.
725cd07d7f9SPeter Maydell  *
726cd07d7f9SPeter Maydell  * If @dev has both input and output GPIOs named @name then both will
727cd07d7f9SPeter Maydell  * be passed through. It is not possible to pass a subset of the array
728cd07d7f9SPeter Maydell  * with this function.
729cd07d7f9SPeter Maydell  *
730cd07d7f9SPeter Maydell  * To users of the container device, the GPIO array created on @container
731cd07d7f9SPeter Maydell  * behaves exactly like any other.
732cd07d7f9SPeter Maydell  */
73317a96a14SPeter Crosthwaite void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
73417a96a14SPeter Crosthwaite                      const char *name);
73517a96a14SPeter Crosthwaite 
7362d2f2507SPhilippe Mathieu-Daudé BusState *qdev_get_parent_bus(const DeviceState *dev);
737074a86fcSAnthony Liguori 
738074a86fcSAnthony Liguori /*** BUS API. ***/
739074a86fcSAnthony Liguori 
740074a86fcSAnthony Liguori DeviceState *qdev_find_recursive(BusState *bus, const char *id);
741074a86fcSAnthony Liguori 
742074a86fcSAnthony Liguori /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
743074a86fcSAnthony Liguori typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
744074a86fcSAnthony Liguori typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
745074a86fcSAnthony Liguori 
746d637e1dcSPeter Maydell void qbus_init(void *bus, size_t size, const char *typename,
747074a86fcSAnthony Liguori                DeviceState *parent, const char *name);
7489388d170SPeter Maydell BusState *qbus_new(const char *typename, DeviceState *parent, const char *name);
7499940b2cfSMarkus Armbruster bool qbus_realize(BusState *bus, Error **errp);
7509940b2cfSMarkus Armbruster void qbus_unrealize(BusState *bus);
7519940b2cfSMarkus Armbruster 
752074a86fcSAnthony Liguori /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
753074a86fcSAnthony Liguori  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
754074a86fcSAnthony Liguori  *           0 otherwise. */
7550293214bSPaolo Bonzini int qbus_walk_children(BusState *bus,
7560293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
7570293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
7580293214bSPaolo Bonzini                        void *opaque);
7590293214bSPaolo Bonzini int qdev_walk_children(DeviceState *dev,
7600293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
7610293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
7620293214bSPaolo Bonzini                        void *opaque);
7630293214bSPaolo Bonzini 
764abb89dbfSDamien Hedde /**
765abb89dbfSDamien Hedde  * device_cold_reset:
766abb89dbfSDamien Hedde  * Reset device @dev and perform a recursive processing using the resettable
767abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
768abb89dbfSDamien Hedde  */
769abb89dbfSDamien Hedde void device_cold_reset(DeviceState *dev);
770abb89dbfSDamien Hedde 
771abb89dbfSDamien Hedde /**
772abb89dbfSDamien Hedde  * bus_cold_reset:
773abb89dbfSDamien Hedde  *
774abb89dbfSDamien Hedde  * Reset bus @bus and perform a recursive processing using the resettable
775abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
776abb89dbfSDamien Hedde  */
777abb89dbfSDamien Hedde void bus_cold_reset(BusState *bus);
778abb89dbfSDamien Hedde 
779abb89dbfSDamien Hedde /**
780c11256aaSDamien Hedde  * device_is_in_reset:
781c11256aaSDamien Hedde  * Return true if the device @dev is currently being reset.
782c11256aaSDamien Hedde  */
783c11256aaSDamien Hedde bool device_is_in_reset(DeviceState *dev);
784c11256aaSDamien Hedde 
785c11256aaSDamien Hedde /**
786c11256aaSDamien Hedde  * bus_is_in_reset:
787c11256aaSDamien Hedde  * Return true if the bus @bus is currently being reset.
788c11256aaSDamien Hedde  */
789c11256aaSDamien Hedde bool bus_is_in_reset(BusState *bus);
790c11256aaSDamien Hedde 
791074a86fcSAnthony Liguori /* This should go away once we get rid of the NULL bus hack */
792074a86fcSAnthony Liguori BusState *sysbus_get_default(void);
793074a86fcSAnthony Liguori 
794074a86fcSAnthony Liguori char *qdev_get_fw_dev_path(DeviceState *dev);
7950be63901SGonglei char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
796074a86fcSAnthony Liguori 
7974f67d30bSMarc-André Lureau void device_class_set_props(DeviceClass *dc, Property *props);
7984f67d30bSMarc-André Lureau 
799c11256aaSDamien Hedde /**
800c11256aaSDamien Hedde  * device_class_set_parent_reset:
801c11256aaSDamien Hedde  * TODO: remove the function when DeviceClass's reset method
802c11256aaSDamien Hedde  * is not used anymore.
803c11256aaSDamien Hedde  */
80446795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_reset(DeviceClass *dc,
80546795cf2SPhilippe Mathieu-Daudé                                    DeviceReset dev_reset,
80646795cf2SPhilippe Mathieu-Daudé                                    DeviceReset *parent_reset);
80746795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_realize(DeviceClass *dc,
80846795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize dev_realize,
80946795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize *parent_realize);
81046795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_unrealize(DeviceClass *dc,
81146795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize dev_unrealize,
81246795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize *parent_unrealize);
81346795cf2SPhilippe Mathieu-Daudé 
8148a9358ccSMarkus Armbruster const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
815074a86fcSAnthony Liguori 
816074a86fcSAnthony Liguori const char *qdev_fw_name(DeviceState *dev);
817074a86fcSAnthony Liguori 
818f66dc873SPaolo Bonzini void qdev_assert_realized_properly(void);
819074a86fcSAnthony Liguori Object *qdev_get_machine(void);
820074a86fcSAnthony Liguori 
821074a86fcSAnthony Liguori /* FIXME: make this a link<> */
822bb755ba4SPaolo Bonzini bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
823074a86fcSAnthony Liguori 
82421def24aSJuan Quintela extern bool qdev_hot_removed;
825074a86fcSAnthony Liguori 
826074a86fcSAnthony Liguori char *qdev_get_dev_path(DeviceState *dev);
827074a86fcSAnthony Liguori 
8289bc6bfdfSMarkus Armbruster void qbus_set_hotplug_handler(BusState *bus, Object *handler);
829cd7c8660SMarkus Armbruster void qbus_set_bus_hotplug_handler(BusState *bus);
83039b888bdSIgor Mammedov 
83139b888bdSIgor Mammedov static inline bool qbus_is_hotpluggable(BusState *bus)
83239b888bdSIgor Mammedov {
833ceefa0b7SIgor Mammedov     HotplugHandler *plug_handler = bus->hotplug_handler;
834ceefa0b7SIgor Mammedov     bool ret = !!plug_handler;
835ceefa0b7SIgor Mammedov 
836ceefa0b7SIgor Mammedov     if (plug_handler) {
837ceefa0b7SIgor Mammedov         HotplugHandlerClass *hdc;
838ceefa0b7SIgor Mammedov 
839ceefa0b7SIgor Mammedov         hdc = HOTPLUG_HANDLER_GET_CLASS(plug_handler);
840ceefa0b7SIgor Mammedov         if (hdc->is_hotpluggable_bus) {
841ceefa0b7SIgor Mammedov             ret = hdc->is_hotpluggable_bus(plug_handler, bus);
842ceefa0b7SIgor Mammedov         }
843ceefa0b7SIgor Mammedov     }
844ceefa0b7SIgor Mammedov     return ret;
84539b888bdSIgor Mammedov }
846707ff800SPaul Durrant 
8471518562bSPeter Maydell /**
8481518562bSPeter Maydell  * qbus_mark_full: Mark this bus as full, so no more devices can be attached
8491518562bSPeter Maydell  * @bus: Bus to mark as full
8501518562bSPeter Maydell  *
8511518562bSPeter Maydell  * By default, QEMU will allow devices to be plugged into a bus up
8521518562bSPeter Maydell  * to the bus class's device count limit. Calling this function
8531518562bSPeter Maydell  * marks a particular bus as full, so that no more devices can be
8541518562bSPeter Maydell  * plugged into it. In particular this means that the bus will not
8551518562bSPeter Maydell  * be considered as a candidate for plugging in devices created by
8561518562bSPeter Maydell  * the user on the commandline or via the monitor.
8571518562bSPeter Maydell  * If a machine has multiple buses of a given type, such as I2C,
8581518562bSPeter Maydell  * where some of those buses in the real hardware are used only for
8591518562bSPeter Maydell  * internal devices and some are exposed via expansion ports, you
8601518562bSPeter Maydell  * can use this function to mark the internal-only buses as full
8611518562bSPeter Maydell  * after you have created all their internal devices. Then user
8621518562bSPeter Maydell  * created devices will appear on the expansion-port bus where
8631518562bSPeter Maydell  * guest software expects them.
8641518562bSPeter Maydell  */
8651518562bSPeter Maydell static inline void qbus_mark_full(BusState *bus)
8661518562bSPeter Maydell {
8671518562bSPeter Maydell     bus->full = true;
8681518562bSPeter Maydell }
8691518562bSPeter Maydell 
870707ff800SPaul Durrant void device_listener_register(DeviceListener *listener);
871707ff800SPaul Durrant void device_listener_unregister(DeviceListener *listener);
872707ff800SPaul Durrant 
873f3a85056SJens Freimann /**
874f3a85056SJens Freimann  * @qdev_should_hide_device:
875f3558b1bSKevin Wolf  * @opts: options QDict
876f3558b1bSKevin Wolf  * @from_json: true if @opts entries are typed, false for all strings
877f3558b1bSKevin Wolf  * @errp: pointer to error object
878f3a85056SJens Freimann  *
879f3a85056SJens Freimann  * Check if a device should be added.
880f3a85056SJens Freimann  * When a device is added via qdev_device_add() this will be called,
881f3a85056SJens Freimann  * and return if the device should be added now or not.
882f3a85056SJens Freimann  */
883f3558b1bSKevin Wolf bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp);
884f3a85056SJens Freimann 
8852f181fbdSPaolo Bonzini typedef enum MachineInitPhase {
8862f181fbdSPaolo Bonzini     /* current_machine is NULL.  */
8872f181fbdSPaolo Bonzini     PHASE_NO_MACHINE,
8882f181fbdSPaolo Bonzini 
8892f181fbdSPaolo Bonzini     /* current_machine is not NULL, but current_machine->accel is NULL.  */
8902f181fbdSPaolo Bonzini     PHASE_MACHINE_CREATED,
8912f181fbdSPaolo Bonzini 
8922f181fbdSPaolo Bonzini     /*
8932f181fbdSPaolo Bonzini      * current_machine->accel is not NULL, but the machine properties have
8942f181fbdSPaolo Bonzini      * not been validated and machine_class->init has not yet been called.
8952f181fbdSPaolo Bonzini      */
8962f181fbdSPaolo Bonzini     PHASE_ACCEL_CREATED,
8972f181fbdSPaolo Bonzini 
8982f181fbdSPaolo Bonzini     /*
8992f181fbdSPaolo Bonzini      * machine_class->init has been called, thus creating any embedded
9002f181fbdSPaolo Bonzini      * devices and validating machine properties.  Devices created at
9012f181fbdSPaolo Bonzini      * this time are considered to be cold-plugged.
9022f181fbdSPaolo Bonzini      */
9032f181fbdSPaolo Bonzini     PHASE_MACHINE_INITIALIZED,
9042f181fbdSPaolo Bonzini 
9052f181fbdSPaolo Bonzini     /*
9062f181fbdSPaolo Bonzini      * QEMU is ready to start CPUs and devices created at this time
9072f181fbdSPaolo Bonzini      * are considered to be hot-plugged.  The monitor is not restricted
9082f181fbdSPaolo Bonzini      * to "preconfig" commands.
9092f181fbdSPaolo Bonzini      */
9102f181fbdSPaolo Bonzini     PHASE_MACHINE_READY,
9112f181fbdSPaolo Bonzini } MachineInitPhase;
9122f181fbdSPaolo Bonzini 
9132f181fbdSPaolo Bonzini extern bool phase_check(MachineInitPhase phase);
9142f181fbdSPaolo Bonzini extern void phase_advance(MachineInitPhase phase);
9152f181fbdSPaolo Bonzini 
916074a86fcSAnthony Liguori #endif
917