xref: /qemu/include/hw/qdev-core.h (revision bb755ba47f3747251c0eadf681ee68b9033309b8)
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"
614cccb61SPaolo Bonzini #include "qom/object.h"
70ee4de6cSIgor Mammedov #include "hw/hotplug.h"
8c11256aaSDamien Hedde #include "hw/resettable.h"
9074a86fcSAnthony Liguori 
10074a86fcSAnthony Liguori enum {
11074a86fcSAnthony Liguori     DEV_NVECTORS_UNSPECIFIED = -1,
12074a86fcSAnthony Liguori };
13074a86fcSAnthony Liguori 
14074a86fcSAnthony Liguori #define TYPE_DEVICE "device"
15a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(DeviceState, DeviceClass, DEVICE)
16074a86fcSAnthony Liguori 
173d1237fbSMarcel Apfelbaum typedef enum DeviceCategory {
183d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_BRIDGE,
193d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_USB,
203d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_STORAGE,
213d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_NETWORK,
223d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_INPUT,
233d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_DISPLAY,
243d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_SOUND,
253d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MISC,
26ba31cc72SThomas Huth     DEVICE_CATEGORY_CPU,
273d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MAX
283d1237fbSMarcel Apfelbaum } DeviceCategory;
293d1237fbSMarcel Apfelbaum 
30249d4172SAndreas Färber typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
31b69c3c21SMarkus Armbruster typedef void (*DeviceUnrealize)(DeviceState *dev);
32b850f664SPhilippe Mathieu-Daudé typedef void (*DeviceReset)(DeviceState *dev);
3302e7f85dSBandan Das typedef void (*BusRealize)(BusState *bus, Error **errp);
34b69c3c21SMarkus Armbruster typedef void (*BusUnrealize)(BusState *bus);
35074a86fcSAnthony Liguori 
36249d4172SAndreas Färber /**
37249d4172SAndreas Färber  * DeviceClass:
38249d4172SAndreas Färber  * @props: Properties accessing state fields.
39249d4172SAndreas Färber  * @realize: Callback function invoked when the #DeviceState:realized
40ff46d9d4SPhilippe Mathieu-Daudé  * property is changed to %true.
41249d4172SAndreas Färber  * @unrealize: Callback function invoked when the #DeviceState:realized
42249d4172SAndreas Färber  * property is changed to %false.
431a37eca1SIgor Mammedov  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
441a37eca1SIgor Mammedov  * as readonly "hotpluggable" property of #DeviceState instance
45249d4172SAndreas Färber  *
46249d4172SAndreas Färber  * # Realization #
47249d4172SAndreas Färber  * Devices are constructed in two stages,
48249d4172SAndreas Färber  * 1) object instantiation via object_initialize() and
49249d4172SAndreas Färber  * 2) device realization via #DeviceState:realized property.
506038f989SThomas Huth  * The former may not fail (and must not abort or exit, since it is called
516038f989SThomas Huth  * during device introspection already), and the latter may return error
526038f989SThomas Huth  * information to the caller and must be re-entrant.
53249d4172SAndreas Färber  * Trivial field initializations should go into #TypeInfo.instance_init.
54249d4172SAndreas Färber  * Operations depending on @props static properties should go into @realize.
55249d4172SAndreas Färber  * After successful realization, setting static properties will fail.
56249d4172SAndreas Färber  *
57daeba969SMarkus Armbruster  * As an interim step, the #DeviceState:realized property can also be
58c835fac3SMarkus Armbruster  * set with qdev_realize().
59249d4172SAndreas Färber  * In the future, devices will propagate this state change to their children
60249d4172SAndreas Färber  * and along busses they expose.
61249d4172SAndreas Färber  * The point in time will be deferred to machine creation, so that values
62249d4172SAndreas Färber  * set in @realize will not be introspectable beforehand. Therefore devices
63249d4172SAndreas Färber  * must not create children during @realize; they should initialize them via
64249d4172SAndreas Färber  * object_initialize() in their own #TypeInfo.instance_init and forward the
65249d4172SAndreas Färber  * realization events appropriately.
66249d4172SAndreas Färber  *
67249d4172SAndreas Färber  * Any type may override the @realize and/or @unrealize callbacks but needs
68782beb52SAndreas Färber  * to call the parent type's implementation if keeping their functionality
69782beb52SAndreas Färber  * is desired. Refer to QOM documentation for further discussion and examples.
70782beb52SAndreas Färber  *
71782beb52SAndreas Färber  * <note>
72782beb52SAndreas Färber  *   <para>
73ff46d9d4SPhilippe Mathieu-Daudé  * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
74ff46d9d4SPhilippe Mathieu-Daudé  * derived directly from it need not call their parent's @realize and
75ff46d9d4SPhilippe Mathieu-Daudé  * @unrealize.
76782beb52SAndreas Färber  * For other types consult the documentation and implementation of the
77782beb52SAndreas Färber  * respective parent types.
78782beb52SAndreas Färber  *   </para>
79782beb52SAndreas Färber  * </note>
80f3a85056SJens Freimann  *
81f3a85056SJens Freimann  * # Hiding a device #
82f3a85056SJens Freimann  * To hide a device, a DeviceListener function should_be_hidden() needs to
83f3a85056SJens Freimann  * be registered.
84f3a85056SJens Freimann  * It can be used to defer adding a device and therefore hide it from the
85f3a85056SJens Freimann  * guest. The handler registering to this DeviceListener can save the QOpts
86f3a85056SJens Freimann  * passed to it for re-using it later and must return that it wants the device
87f3a85056SJens Freimann  * to be/remain hidden or not. When the handler function decides the device
88f3a85056SJens Freimann  * shall not be hidden it will be added in qdev_device_add() and
89f3a85056SJens Freimann  * realized as any other device. Otherwise qdev_device_add() will return early
90f3a85056SJens Freimann  * without adding the device. The guest will not see a "hidden" device
91f3a85056SJens Freimann  * until it was marked don't hide and qdev_device_add called again.
92f3a85056SJens Freimann  *
93249d4172SAndreas Färber  */
94db1015e9SEduardo Habkost struct DeviceClass {
95249d4172SAndreas Färber     /*< private >*/
96074a86fcSAnthony Liguori     ObjectClass parent_class;
97249d4172SAndreas Färber     /*< public >*/
98074a86fcSAnthony Liguori 
993d1237fbSMarcel Apfelbaum     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
100074a86fcSAnthony Liguori     const char *fw_name;
101074a86fcSAnthony Liguori     const char *desc;
102385d8f22SPaolo Bonzini 
103385d8f22SPaolo Bonzini     /*
104385d8f22SPaolo Bonzini      * The underscore at the end ensures a compile-time error if someone
105385d8f22SPaolo Bonzini      * assigns to dc->props instead of using device_class_set_props.
106385d8f22SPaolo Bonzini      */
107385d8f22SPaolo Bonzini     Property *props_;
108efec3dd6SMarkus Armbruster 
109efec3dd6SMarkus Armbruster     /*
110e90f2a8cSEduardo Habkost      * Can this device be instantiated with -device / device_add?
111efec3dd6SMarkus Armbruster      * All devices should support instantiation with device_add, and
112efec3dd6SMarkus Armbruster      * this flag should not exist.  But we're not there, yet.  Some
113efec3dd6SMarkus Armbruster      * devices fail to instantiate with cryptic error messages.
114efec3dd6SMarkus Armbruster      * Others instantiate, but don't work.  Exposing users to such
115e90f2a8cSEduardo Habkost      * behavior would be cruel; clearing this flag will protect them.
116e90f2a8cSEduardo Habkost      * It should never be cleared without a comment explaining why it
117e90f2a8cSEduardo Habkost      * is cleared.
118efec3dd6SMarkus Armbruster      * TODO remove once we're there
119efec3dd6SMarkus Armbruster      */
120e90f2a8cSEduardo Habkost     bool user_creatable;
1211a37eca1SIgor Mammedov     bool hotpluggable;
122074a86fcSAnthony Liguori 
123074a86fcSAnthony Liguori     /* callbacks */
124c11256aaSDamien Hedde     /*
125c11256aaSDamien Hedde      * Reset method here is deprecated and replaced by methods in the
126c11256aaSDamien Hedde      * resettable class interface to implement a multi-phase reset.
127c11256aaSDamien Hedde      * TODO: remove once every reset callback is unused
128c11256aaSDamien Hedde      */
129b850f664SPhilippe Mathieu-Daudé     DeviceReset reset;
130249d4172SAndreas Färber     DeviceRealize realize;
131249d4172SAndreas Färber     DeviceUnrealize unrealize;
132074a86fcSAnthony Liguori 
133074a86fcSAnthony Liguori     /* device state */
1348a9358ccSMarkus Armbruster     const VMStateDescription *vmsd;
135074a86fcSAnthony Liguori 
136074a86fcSAnthony Liguori     /* Private to qdev / bus.  */
137074a86fcSAnthony Liguori     const char *bus_type;
138db1015e9SEduardo Habkost };
139074a86fcSAnthony Liguori 
140a5f54290SPeter Crosthwaite typedef struct NamedGPIOList NamedGPIOList;
141a5f54290SPeter Crosthwaite 
142a5f54290SPeter Crosthwaite struct NamedGPIOList {
143a5f54290SPeter Crosthwaite     char *name;
144a5f54290SPeter Crosthwaite     qemu_irq *in;
145a5f54290SPeter Crosthwaite     int num_in;
146a5f54290SPeter Crosthwaite     int num_out;
147a5f54290SPeter Crosthwaite     QLIST_ENTRY(NamedGPIOList) node;
148a5f54290SPeter Crosthwaite };
149a5f54290SPeter Crosthwaite 
1500e6934f2SDamien Hedde typedef struct Clock Clock;
1510e6934f2SDamien Hedde typedef struct NamedClockList NamedClockList;
1520e6934f2SDamien Hedde 
1530e6934f2SDamien Hedde struct NamedClockList {
1540e6934f2SDamien Hedde     char *name;
1550e6934f2SDamien Hedde     Clock *clock;
1560e6934f2SDamien Hedde     bool output;
1570e6934f2SDamien Hedde     bool alias;
1580e6934f2SDamien Hedde     QLIST_ENTRY(NamedClockList) node;
1590e6934f2SDamien Hedde };
1600e6934f2SDamien Hedde 
1617983c8a3SAndreas Färber /**
1627983c8a3SAndreas Färber  * DeviceState:
1637983c8a3SAndreas Färber  * @realized: Indicates whether the device has been fully constructed.
164c11256aaSDamien Hedde  * @reset: ResettableState for the device; handled by Resettable interface.
1657983c8a3SAndreas Färber  *
1667983c8a3SAndreas Färber  * This structure should not be accessed directly.  We declare it here
1677983c8a3SAndreas Färber  * so that it can be embedded in individual device state structures.
1687983c8a3SAndreas Färber  */
169074a86fcSAnthony Liguori struct DeviceState {
1707983c8a3SAndreas Färber     /*< private >*/
171074a86fcSAnthony Liguori     Object parent_obj;
1727983c8a3SAndreas Färber     /*< public >*/
173074a86fcSAnthony Liguori 
174074a86fcSAnthony Liguori     const char *id;
17504162f8fSMichael Roth     char *canonical_path;
1767983c8a3SAndreas Färber     bool realized;
177352e8da7SPaolo Bonzini     bool pending_deleted_event;
178074a86fcSAnthony Liguori     QemuOpts *opts;
179074a86fcSAnthony Liguori     int hotplugged;
180a1190ab6SJens Freimann     bool allow_unplug_during_migration;
181074a86fcSAnthony Liguori     BusState *parent_bus;
182a5f54290SPeter Crosthwaite     QLIST_HEAD(, NamedGPIOList) gpios;
1830e6934f2SDamien Hedde     QLIST_HEAD(, NamedClockList) clocks;
184074a86fcSAnthony Liguori     QLIST_HEAD(, BusState) child_bus;
185074a86fcSAnthony Liguori     int num_child_bus;
186074a86fcSAnthony Liguori     int instance_id_alias;
187074a86fcSAnthony Liguori     int alias_required_for_version;
188c11256aaSDamien Hedde     ResettableState reset;
189074a86fcSAnthony Liguori };
190074a86fcSAnthony Liguori 
191707ff800SPaul Durrant struct DeviceListener {
192707ff800SPaul Durrant     void (*realize)(DeviceListener *listener, DeviceState *dev);
193707ff800SPaul Durrant     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
194f3a85056SJens Freimann     /*
195f3a85056SJens Freimann      * This callback is called upon init of the DeviceState and allows to
196f3a85056SJens Freimann      * inform qdev that a device should be hidden, depending on the device
197f3a85056SJens Freimann      * opts, for example, to hide a standby device.
198f3a85056SJens Freimann      */
199f3a85056SJens Freimann     int (*should_be_hidden)(DeviceListener *listener, QemuOpts *device_opts);
200707ff800SPaul Durrant     QTAILQ_ENTRY(DeviceListener) link;
201707ff800SPaul Durrant };
202707ff800SPaul Durrant 
203074a86fcSAnthony Liguori #define TYPE_BUS "bus"
2048110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(BusState, BusClass,
2058110fa1dSEduardo Habkost                      BUS, TYPE_BUS)
206074a86fcSAnthony Liguori 
207074a86fcSAnthony Liguori struct BusClass {
208074a86fcSAnthony Liguori     ObjectClass parent_class;
209074a86fcSAnthony Liguori 
210074a86fcSAnthony Liguori     /* FIXME first arg should be BusState */
211074a86fcSAnthony Liguori     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
212074a86fcSAnthony Liguori     char *(*get_dev_path)(DeviceState *dev);
213*bb755ba4SPaolo Bonzini 
214074a86fcSAnthony Liguori     /*
215074a86fcSAnthony Liguori      * This callback is used to create Open Firmware device path in accordance
216074a86fcSAnthony Liguori      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
217074a86fcSAnthony Liguori      * bindings can be found at http://playground.sun.com/1275/bindings/.
218074a86fcSAnthony Liguori      */
219074a86fcSAnthony Liguori     char *(*get_fw_dev_path)(DeviceState *dev);
220*bb755ba4SPaolo Bonzini 
221dcc20931SPaolo Bonzini     void (*reset)(BusState *bus);
222*bb755ba4SPaolo Bonzini 
223*bb755ba4SPaolo Bonzini     /*
224*bb755ba4SPaolo Bonzini      * Return whether the device can be added to @bus,
225*bb755ba4SPaolo Bonzini      * based on the address that was set (via device properties)
226*bb755ba4SPaolo Bonzini      * before realize.  If not, on return @errp contains the
227*bb755ba4SPaolo Bonzini      * human-readable error message.
228*bb755ba4SPaolo Bonzini      */
229*bb755ba4SPaolo Bonzini     bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
230*bb755ba4SPaolo Bonzini 
23102e7f85dSBandan Das     BusRealize realize;
23202e7f85dSBandan Das     BusUnrealize unrealize;
23302e7f85dSBandan Das 
2341395af6fSKONRAD Frederic     /* maximum devices allowed on the bus, 0: no limit. */
2351395af6fSKONRAD Frederic     int max_dev;
23661de3676SAlexander Graf     /* number of automatically allocated bus ids (e.g. ide.0) */
23761de3676SAlexander Graf     int automatic_ids;
238074a86fcSAnthony Liguori };
239074a86fcSAnthony Liguori 
240074a86fcSAnthony Liguori typedef struct BusChild {
241074a86fcSAnthony Liguori     DeviceState *child;
242074a86fcSAnthony Liguori     int index;
243074a86fcSAnthony Liguori     QTAILQ_ENTRY(BusChild) sibling;
244074a86fcSAnthony Liguori } BusChild;
245074a86fcSAnthony Liguori 
2460ee4de6cSIgor Mammedov #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
2470ee4de6cSIgor Mammedov 
248074a86fcSAnthony Liguori /**
249074a86fcSAnthony Liguori  * BusState:
25027c6ef1bSLi Qiang  * @hotplug_handler: link to a hotplug handler associated with bus.
251c11256aaSDamien Hedde  * @reset: ResettableState for the bus; handled by Resettable interface.
252074a86fcSAnthony Liguori  */
253074a86fcSAnthony Liguori struct BusState {
254074a86fcSAnthony Liguori     Object obj;
255074a86fcSAnthony Liguori     DeviceState *parent;
256f73480c3SMarc-André Lureau     char *name;
2570ee4de6cSIgor Mammedov     HotplugHandler *hotplug_handler;
258074a86fcSAnthony Liguori     int max_index;
25902e7f85dSBandan Das     bool realized;
26012b2e9f3STony Krowiak     int num_children;
261eae3eb3eSPaolo Bonzini     QTAILQ_HEAD(, BusChild) children;
262074a86fcSAnthony Liguori     QLIST_ENTRY(BusState) sibling;
263c11256aaSDamien Hedde     ResettableState reset;
264074a86fcSAnthony Liguori };
265074a86fcSAnthony Liguori 
2665cc56cc6SPeter Maydell /**
2675cc56cc6SPeter Maydell  * Property:
2685cc56cc6SPeter Maydell  * @set_default: true if the default value should be set from @defval,
2695cc56cc6SPeter Maydell  *    in which case @info->set_default_value must not be NULL
2705cc56cc6SPeter Maydell  *    (if false then no default value is set by the property system
2715cc56cc6SPeter Maydell  *     and the field retains whatever value it was given by instance_init).
2725cc56cc6SPeter Maydell  * @defval: default value for the property. This is used only if @set_default
2735cc56cc6SPeter Maydell  *     is true.
2745cc56cc6SPeter Maydell  */
275074a86fcSAnthony Liguori struct Property {
276074a86fcSAnthony Liguori     const char   *name;
2771b6b7d10SFam Zheng     const PropertyInfo *info;
2783b6ca402SIldar Isaev     ptrdiff_t    offset;
279074a86fcSAnthony Liguori     uint8_t      bitnr;
2805cc56cc6SPeter Maydell     bool         set_default;
28176318657SMarc-André Lureau     union {
28276318657SMarc-André Lureau         int64_t i;
2833fb2111fSMarc-André Lureau         uint64_t u;
28476318657SMarc-André Lureau     } defval;
2850be6bfacSPeter Maydell     int          arrayoffset;
2861b6b7d10SFam Zheng     const PropertyInfo *arrayinfo;
2870be6bfacSPeter Maydell     int          arrayfieldsize;
2885b4ff3c6SFam Zheng     const char   *link_type;
289074a86fcSAnthony Liguori };
290074a86fcSAnthony Liguori 
291074a86fcSAnthony Liguori struct PropertyInfo {
292074a86fcSAnthony Liguori     const char *name;
29351b2e8c3SGonglei     const char *description;
294f7abe0ecSMarc-André Lureau     const QEnumLookup *enum_table;
295074a86fcSAnthony Liguori     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
29677b06bbaSMarc-André Lureau     void (*set_default_value)(ObjectProperty *op, const Property *prop);
29740c2281cSMarkus Armbruster     void (*create)(ObjectClass *oc, Property *prop);
298074a86fcSAnthony Liguori     ObjectPropertyAccessor *get;
299074a86fcSAnthony Liguori     ObjectPropertyAccessor *set;
300074a86fcSAnthony Liguori     ObjectPropertyRelease *release;
301074a86fcSAnthony Liguori };
302074a86fcSAnthony Liguori 
3039f9260a3SDon Slutz /**
3049f9260a3SDon Slutz  * GlobalProperty:
305b3ce84feSEduardo Habkost  * @used: Set to true if property was used when initializing a device.
30692fd453cSDr. David Alan Gilbert  * @optional: If set to true, GlobalProperty will be skipped without errors
30792fd453cSDr. David Alan Gilbert  *            if the property doesn't exist.
308cff8b715SMarc-André Lureau  *
309cff8b715SMarc-André Lureau  * An error is fatal for non-hotplugged devices, when the global is applied.
3109f9260a3SDon Slutz  */
311074a86fcSAnthony Liguori typedef struct GlobalProperty {
312074a86fcSAnthony Liguori     const char *driver;
313074a86fcSAnthony Liguori     const char *property;
314074a86fcSAnthony Liguori     const char *value;
315b3ce84feSEduardo Habkost     bool used;
31692fd453cSDr. David Alan Gilbert     bool optional;
317074a86fcSAnthony Liguori } GlobalProperty;
318074a86fcSAnthony Liguori 
319ea9ce893SMarc-André Lureau static inline void
320ea9ce893SMarc-André Lureau compat_props_add(GPtrArray *arr,
321ea9ce893SMarc-André Lureau                  GlobalProperty props[], size_t nelem)
322ea9ce893SMarc-André Lureau {
323ea9ce893SMarc-André Lureau     int i;
324ea9ce893SMarc-André Lureau     for (i = 0; i < nelem; i++) {
325ea9ce893SMarc-André Lureau         g_ptr_array_add(arr, (void *)&props[i]);
326ea9ce893SMarc-André Lureau     }
327ea9ce893SMarc-André Lureau }
328ea9ce893SMarc-André Lureau 
329074a86fcSAnthony Liguori /*** Board API.  This should go away once we have a machine config file.  ***/
330074a86fcSAnthony Liguori 
331b51238e2SPeter Maydell /**
332b51238e2SPeter Maydell  * qdev_new: Create a device on the heap
333b51238e2SPeter Maydell  * @name: device type to create (we assert() that this type exists)
334b51238e2SPeter Maydell  *
335b51238e2SPeter Maydell  * This only allocates the memory and initializes the device state
336b51238e2SPeter Maydell  * structure, ready for the caller to set properties if they wish.
337b51238e2SPeter Maydell  * The device still needs to be realized.
338b51238e2SPeter Maydell  * The returned object has a reference count of 1.
339b51238e2SPeter Maydell  */
3409940b2cfSMarkus Armbruster DeviceState *qdev_new(const char *name);
341b51238e2SPeter Maydell /**
342b51238e2SPeter Maydell  * qdev_try_new: Try to create a device on the heap
343b51238e2SPeter Maydell  * @name: device type to create
344b51238e2SPeter Maydell  *
345b51238e2SPeter Maydell  * This is like qdev_new(), except it returns %NULL when type @name
346b51238e2SPeter Maydell  * does not exist, rather than asserting.
347b51238e2SPeter Maydell  */
3489940b2cfSMarkus Armbruster DeviceState *qdev_try_new(const char *name);
349b51238e2SPeter Maydell /**
350b51238e2SPeter Maydell  * qdev_realize: Realize @dev.
351b51238e2SPeter Maydell  * @dev: device to realize
352b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
353b51238e2SPeter Maydell  * @errp: pointer to error object
354b51238e2SPeter Maydell  *
355b51238e2SPeter Maydell  * "Realize" the device, i.e. perform the second phase of device
356b51238e2SPeter Maydell  * initialization.
357b51238e2SPeter Maydell  * @dev must not be plugged into a bus already.
358b51238e2SPeter Maydell  * If @bus, plug @dev into @bus.  This takes a reference to @dev.
359b51238e2SPeter Maydell  * If @dev has no QOM parent, make one up, taking another reference.
360b51238e2SPeter Maydell  * On success, return true.
361b51238e2SPeter Maydell  * On failure, store an error through @errp and return false.
362b51238e2SPeter Maydell  *
363b51238e2SPeter Maydell  * If you created @dev using qdev_new(), you probably want to use
364b51238e2SPeter Maydell  * qdev_realize_and_unref() instead.
365b51238e2SPeter Maydell  */
3669940b2cfSMarkus Armbruster bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
367b51238e2SPeter Maydell /**
368b51238e2SPeter Maydell  * qdev_realize_and_unref: Realize @dev and drop a reference
369b51238e2SPeter Maydell  * @dev: device to realize
370b51238e2SPeter Maydell  * @bus: bus to plug it into (may be NULL)
371b51238e2SPeter Maydell  * @errp: pointer to error object
372b51238e2SPeter Maydell  *
373b51238e2SPeter Maydell  * Realize @dev and drop a reference.
374b51238e2SPeter Maydell  * This is like qdev_realize(), except the caller must hold a
375b51238e2SPeter Maydell  * (private) reference, which is dropped on return regardless of
376b51238e2SPeter Maydell  * success or failure.  Intended use::
377b51238e2SPeter Maydell  *
378b51238e2SPeter Maydell  *     dev = qdev_new();
379b51238e2SPeter Maydell  *     [...]
380b51238e2SPeter Maydell  *     qdev_realize_and_unref(dev, bus, errp);
381b51238e2SPeter Maydell  *
382b51238e2SPeter Maydell  * Now @dev can go away without further ado.
383b51238e2SPeter Maydell  *
384b51238e2SPeter Maydell  * If you are embedding the device into some other QOM device and
385b51238e2SPeter Maydell  * initialized it via some variant on object_initialize_child() then
386b51238e2SPeter Maydell  * do not use this function, because that family of functions arrange
387b51238e2SPeter Maydell  * for the only reference to the child device to be held by the parent
388b51238e2SPeter Maydell  * via the child<> property, and so the reference-count-drop done here
389b51238e2SPeter Maydell  * would be incorrect. For that use case you want qdev_realize().
390b51238e2SPeter Maydell  */
3919940b2cfSMarkus Armbruster bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
39246ea1be1SPeter Maydell /**
39346ea1be1SPeter Maydell  * qdev_unrealize: Unrealize a device
39446ea1be1SPeter Maydell  * @dev: device to unrealize
39546ea1be1SPeter Maydell  *
39646ea1be1SPeter Maydell  * This function will "unrealize" a device, which is the first phase
39746ea1be1SPeter Maydell  * of correctly destroying a device that has been realized. It will:
39846ea1be1SPeter Maydell  *
39946ea1be1SPeter Maydell  *  - unrealize any child buses by calling qbus_unrealize()
40046ea1be1SPeter Maydell  *    (this will recursively unrealize any devices on those buses)
40146ea1be1SPeter Maydell  *  - call the the unrealize method of @dev
40246ea1be1SPeter Maydell  *
40346ea1be1SPeter Maydell  * The device can then be freed by causing its reference count to go
40446ea1be1SPeter Maydell  * to zero.
40546ea1be1SPeter Maydell  *
40646ea1be1SPeter Maydell  * Warning: most devices in QEMU do not expect to be unrealized.  Only
40746ea1be1SPeter Maydell  * devices which are hot-unpluggable should be unrealized (as part of
40846ea1be1SPeter Maydell  * the unplugging process); all other devices are expected to last for
40946ea1be1SPeter Maydell  * the life of the simulation and should not be unrealized and freed.
41046ea1be1SPeter Maydell  */
4119940b2cfSMarkus Armbruster void qdev_unrealize(DeviceState *dev);
412074a86fcSAnthony Liguori void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
413074a86fcSAnthony Liguori                                  int required_for_version);
41414405c27SDavid Hildenbrand HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
41503fcbd9dSThomas Huth HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
416d2321d31SPeter Xu bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
41717cc0128SIgor Mammedov /**
41817cc0128SIgor Mammedov  * qdev_get_hotplug_handler: Get handler responsible for device wiring
41917cc0128SIgor Mammedov  *
42017cc0128SIgor Mammedov  * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
42117cc0128SIgor Mammedov  *
42217cc0128SIgor Mammedov  * Note: in case @dev has a parent bus, it will be returned as handler unless
42317cc0128SIgor Mammedov  * machine handler overrides it.
42417cc0128SIgor Mammedov  *
42517cc0128SIgor Mammedov  * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
42617cc0128SIgor Mammedov  *          or NULL if there aren't any.
42717cc0128SIgor Mammedov  */
428c06b2ffbSZhu Guihua HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
429074a86fcSAnthony Liguori void qdev_unplug(DeviceState *dev, Error **errp);
430014176f9SIgor Mammedov void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
431014176f9SIgor Mammedov                                   DeviceState *dev, Error **errp);
432074a86fcSAnthony Liguori void qdev_machine_creation_done(void);
433074a86fcSAnthony Liguori bool qdev_machine_modified(void);
434074a86fcSAnthony Liguori 
435cd07d7f9SPeter Maydell /**
436cd07d7f9SPeter Maydell  * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
437cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
438cd07d7f9SPeter Maydell  * @n: Number of the anonymous GPIO line (which must be in range)
439cd07d7f9SPeter Maydell  *
440cd07d7f9SPeter Maydell  * Returns the qemu_irq corresponding to an anonymous input GPIO line
441cd07d7f9SPeter Maydell  * (which the device has set up with qdev_init_gpio_in()). The index
442cd07d7f9SPeter Maydell  * @n of the GPIO line must be valid (i.e. be at least 0 and less than
443cd07d7f9SPeter Maydell  * the total number of anonymous input GPIOs the device has); this
444cd07d7f9SPeter Maydell  * function will assert() if passed an invalid index.
445cd07d7f9SPeter Maydell  *
446cd07d7f9SPeter Maydell  * This function is intended to be used by board code or SoC "container"
447cd07d7f9SPeter Maydell  * device models to wire up the GPIO lines; usually the return value
448cd07d7f9SPeter Maydell  * will be passed to qdev_connect_gpio_out() or a similar function to
449cd07d7f9SPeter Maydell  * connect another device's output GPIO line to this input.
450cd07d7f9SPeter Maydell  *
451cd07d7f9SPeter Maydell  * For named input GPIO lines, use qdev_get_gpio_in_named().
452cd07d7f9SPeter Maydell  */
453074a86fcSAnthony Liguori qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
454cd07d7f9SPeter Maydell /**
455cd07d7f9SPeter Maydell  * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
456cd07d7f9SPeter Maydell  * @dev: Device whose GPIO we want
457cd07d7f9SPeter Maydell  * @name: Name of the input GPIO array
458cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in that array (which must be in range)
459cd07d7f9SPeter Maydell  *
460cd07d7f9SPeter Maydell  * Returns the qemu_irq corresponding to a named input GPIO line
461cd07d7f9SPeter Maydell  * (which the device has set up with qdev_init_gpio_in_named()).
462cd07d7f9SPeter Maydell  * The @name string must correspond to an input GPIO array which exists on
463cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
464cd07d7f9SPeter Maydell  * be at least 0 and less than the total number of input GPIOs in that
465cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
466cd07d7f9SPeter Maydell  *
467cd07d7f9SPeter Maydell  * For anonymous input GPIO lines, use qdev_get_gpio_in().
468cd07d7f9SPeter Maydell  */
469a5f54290SPeter Crosthwaite qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
470a5f54290SPeter Crosthwaite 
471cd07d7f9SPeter Maydell /**
472cd07d7f9SPeter Maydell  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
473cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
474cd07d7f9SPeter Maydell  * @n: Number of the anonymous output GPIO line (which must be in range)
475cd07d7f9SPeter Maydell  * @pin: qemu_irq to connect the output line to
476cd07d7f9SPeter Maydell  *
477cd07d7f9SPeter Maydell  * This function connects an anonymous output GPIO line on a device
478cd07d7f9SPeter Maydell  * up to an arbitrary qemu_irq, so that when the device asserts that
479cd07d7f9SPeter Maydell  * output GPIO line, the qemu_irq's callback is invoked.
480cd07d7f9SPeter Maydell  * The index @n of the GPIO line must be valid (i.e. be at least 0 and
481cd07d7f9SPeter Maydell  * less than the total number of anonymous output GPIOs the device has
482cd07d7f9SPeter Maydell  * created with qdev_init_gpio_out()); otherwise this function will assert().
483cd07d7f9SPeter Maydell  *
484cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
485cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
486cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
487cd07d7f9SPeter Maydell  *
488cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
489cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
490cd07d7f9SPeter Maydell  * same qemu_irq. (Warning: there is no assertion or other guard to
491cd07d7f9SPeter Maydell  * catch this error: the model will just not do the right thing.)
492cd07d7f9SPeter Maydell  * Instead, for fan-out you can use the TYPE_IRQ_SPLIT device: connect
493cd07d7f9SPeter Maydell  * a device's outbound GPIO to the splitter's input, and connect each
494cd07d7f9SPeter Maydell  * of the splitter's outputs to a different device.  For fan-in you
495cd07d7f9SPeter Maydell  * can use the TYPE_OR_IRQ device, which is a model of a logical OR
496cd07d7f9SPeter Maydell  * gate with multiple inputs and one output.
497cd07d7f9SPeter Maydell  *
498cd07d7f9SPeter Maydell  * For named output GPIO lines, use qdev_connect_gpio_out_named().
499cd07d7f9SPeter Maydell  */
500074a86fcSAnthony Liguori void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
501cd07d7f9SPeter Maydell /**
502cd07d7f9SPeter Maydell  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
503cd07d7f9SPeter Maydell  * @dev: Device whose GPIO to connect
504cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
505cd07d7f9SPeter Maydell  * @n: Number of the anonymous output GPIO line (which must be in range)
506cd07d7f9SPeter Maydell  * @pin: qemu_irq to connect the output line to
507cd07d7f9SPeter Maydell  *
508cd07d7f9SPeter Maydell  * This function connects an anonymous output GPIO line on a device
509cd07d7f9SPeter Maydell  * up to an arbitrary qemu_irq, so that when the device asserts that
510cd07d7f9SPeter Maydell  * output GPIO line, the qemu_irq's callback is invoked.
511cd07d7f9SPeter Maydell  * The @name string must correspond to an output GPIO array which exists on
512cd07d7f9SPeter Maydell  * the device, and the index @n of the GPIO line must be valid (i.e.
513cd07d7f9SPeter Maydell  * be at least 0 and less than the total number of input GPIOs in that
514cd07d7f9SPeter Maydell  * array); this function will assert() if passed an invalid name or index.
515cd07d7f9SPeter Maydell  *
516cd07d7f9SPeter Maydell  * Outbound GPIO lines can be connected to any qemu_irq, but the common
517cd07d7f9SPeter Maydell  * case is connecting them to another device's inbound GPIO line, using
518cd07d7f9SPeter Maydell  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
519cd07d7f9SPeter Maydell  *
520cd07d7f9SPeter Maydell  * It is not valid to try to connect one outbound GPIO to multiple
521cd07d7f9SPeter Maydell  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
522cd07d7f9SPeter Maydell  * same qemu_irq; see qdev_connect_gpio_out() for details.
523cd07d7f9SPeter Maydell  *
524cd07d7f9SPeter Maydell  * For named output GPIO lines, use qdev_connect_gpio_out_named().
525cd07d7f9SPeter Maydell  */
526a5f54290SPeter Crosthwaite void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
527a5f54290SPeter Crosthwaite                                  qemu_irq pin);
528cd07d7f9SPeter Maydell /**
529cd07d7f9SPeter Maydell  * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
530cd07d7f9SPeter Maydell  * @dev: Device whose output GPIO we are interested in
531cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
532cd07d7f9SPeter Maydell  * @n: Number of the output GPIO line within that array
533cd07d7f9SPeter Maydell  *
534cd07d7f9SPeter Maydell  * Returns whatever qemu_irq is currently connected to the specified
535cd07d7f9SPeter Maydell  * output GPIO line of @dev. This will be NULL if the output GPIO line
536cd07d7f9SPeter Maydell  * has never been wired up to the anything.  Note that the qemu_irq
537cd07d7f9SPeter Maydell  * returned does not belong to @dev -- it will be the input GPIO or
538cd07d7f9SPeter Maydell  * IRQ of whichever device the board code has connected up to @dev's
539cd07d7f9SPeter Maydell  * output GPIO.
540cd07d7f9SPeter Maydell  *
541cd07d7f9SPeter Maydell  * You probably don't need to use this function -- it is used only
542cd07d7f9SPeter Maydell  * by the platform-bus subsystem.
543cd07d7f9SPeter Maydell  */
544b7973186SAlexander Graf qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
545cd07d7f9SPeter Maydell /**
546cd07d7f9SPeter Maydell  * qdev_intercept_gpio_out: Intercept an existing GPIO connection
547cd07d7f9SPeter Maydell  * @dev: Device to intercept the outbound GPIO line from
548cd07d7f9SPeter Maydell  * @icpt: New qemu_irq to connect instead
549cd07d7f9SPeter Maydell  * @name: Name of the output GPIO array
550cd07d7f9SPeter Maydell  * @n: Number of the GPIO line in the array
551cd07d7f9SPeter Maydell  *
552cd07d7f9SPeter Maydell  * This function is provided only for use by the qtest testing framework
553cd07d7f9SPeter Maydell  * and is not suitable for use in non-testing parts of QEMU.
554cd07d7f9SPeter Maydell  *
555cd07d7f9SPeter Maydell  * This function breaks an existing connection of an outbound GPIO
556cd07d7f9SPeter Maydell  * line from @dev, and replaces it with the new qemu_irq @icpt, as if
557cd07d7f9SPeter Maydell  * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
558cd07d7f9SPeter Maydell  * The previously connected qemu_irq is returned, so it can be restored
559cd07d7f9SPeter Maydell  * by a second call to qdev_intercept_gpio_out() if desired.
560cd07d7f9SPeter Maydell  */
5610c24db2bSPeter Crosthwaite qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
5620c24db2bSPeter Crosthwaite                                  const char *name, int n);
563074a86fcSAnthony Liguori 
564074a86fcSAnthony Liguori BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
565074a86fcSAnthony Liguori 
566074a86fcSAnthony Liguori /*** Device API.  ***/
567074a86fcSAnthony Liguori 
568cd07d7f9SPeter Maydell /**
569cd07d7f9SPeter Maydell  * qdev_init_gpio_in: create an array of anonymous input GPIO lines
570cd07d7f9SPeter Maydell  * @dev: Device to create input GPIOs for
571cd07d7f9SPeter Maydell  * @handler: Function to call when GPIO line value is set
572cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
573cd07d7f9SPeter Maydell  *
574cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_in* family in
575cd07d7f9SPeter Maydell  * their instance_init or realize methods to create any input GPIO
576cd07d7f9SPeter Maydell  * lines they need. There is no functional difference between
577cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
578cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
579cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO input whose purpose is obvious.
580cd07d7f9SPeter Maydell  * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
581cd07d7f9SPeter Maydell  *
582cd07d7f9SPeter Maydell  * See qdev_get_gpio_in() for how code that uses such a device can get
583cd07d7f9SPeter Maydell  * hold of an input GPIO line to manipulate it.
584cd07d7f9SPeter Maydell  */
585074a86fcSAnthony Liguori void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
586cd07d7f9SPeter Maydell /**
587cd07d7f9SPeter Maydell  * qdev_init_gpio_out: create an array of anonymous output GPIO lines
588cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
589cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
590cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
591cd07d7f9SPeter Maydell  *
592cd07d7f9SPeter Maydell  * Devices should use functions in the qdev_init_gpio_out* family
593cd07d7f9SPeter Maydell  * in their instance_init or realize methods to create any output
594cd07d7f9SPeter Maydell  * GPIO lines they need. There is no functional difference between
595cd07d7f9SPeter Maydell  * anonymous and named GPIO lines. Stylistically, named GPIOs are
596cd07d7f9SPeter Maydell  * preferable (easier to understand at callsites) unless a device
597cd07d7f9SPeter Maydell  * has exactly one uniform kind of GPIO output whose purpose is obvious.
598cd07d7f9SPeter Maydell  *
599cd07d7f9SPeter Maydell  * The @pins argument should be a pointer to either a "qemu_irq"
600cd07d7f9SPeter Maydell  * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
601cd07d7f9SPeter Maydell  * state structure. The device implementation can then raise and
602cd07d7f9SPeter Maydell  * lower the GPIO line by calling qemu_set_irq(). (If anything is
603cd07d7f9SPeter Maydell  * connected to the other end of the GPIO this will cause the handler
604cd07d7f9SPeter Maydell  * function for that input GPIO to be called.)
605cd07d7f9SPeter Maydell  *
606cd07d7f9SPeter Maydell  * See qdev_connect_gpio_out() for how code that uses such a device
607cd07d7f9SPeter Maydell  * can connect to one of its output GPIO lines.
608cd07d7f9SPeter Maydell  */
609074a86fcSAnthony Liguori void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
610cd07d7f9SPeter Maydell /**
611cd07d7f9SPeter Maydell  * qdev_init_gpio_out: create an array of named output GPIO lines
612cd07d7f9SPeter Maydell  * @dev: Device to create output GPIOs for
613cd07d7f9SPeter Maydell  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
614cd07d7f9SPeter Maydell  * @name: Name to give this array of GPIO lines
615cd07d7f9SPeter Maydell  * @n: Number of GPIO lines to create
616cd07d7f9SPeter Maydell  *
617cd07d7f9SPeter Maydell  * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
618cd07d7f9SPeter Maydell  * with a name. Code using the device can then connect these GPIO lines
619cd07d7f9SPeter Maydell  * using qdev_connect_gpio_out_named().
620cd07d7f9SPeter Maydell  */
621a5f54290SPeter Crosthwaite void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
622a5f54290SPeter Crosthwaite                               const char *name, int n);
6234a151677SPeter Maydell /**
6244a151677SPeter Maydell  * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
6254a151677SPeter Maydell  *   for the specified device
6264a151677SPeter Maydell  *
6274a151677SPeter Maydell  * @dev: Device to create input GPIOs for
6284a151677SPeter Maydell  * @handler: Function to call when GPIO line value is set
6294a151677SPeter Maydell  * @opaque: Opaque data pointer to pass to @handler
6304a151677SPeter Maydell  * @name: Name of the GPIO input (must be unique for this device)
6314a151677SPeter Maydell  * @n: Number of GPIO lines in this input set
6324a151677SPeter Maydell  */
6334a151677SPeter Maydell void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
6344a151677SPeter Maydell                                          qemu_irq_handler handler,
6354a151677SPeter Maydell                                          void *opaque,
6364a151677SPeter Maydell                                          const char *name, int n);
6374a151677SPeter Maydell 
6384a151677SPeter Maydell /**
6394a151677SPeter Maydell  * qdev_init_gpio_in_named: create an array of input GPIO lines
6404a151677SPeter Maydell  *   for the specified device
6414a151677SPeter Maydell  *
6424a151677SPeter Maydell  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
6434a151677SPeter Maydell  * passed to the handler is @dev (which is the most commonly desired behaviour).
6444a151677SPeter Maydell  */
6454a151677SPeter Maydell static inline void qdev_init_gpio_in_named(DeviceState *dev,
6464a151677SPeter Maydell                                            qemu_irq_handler handler,
6474a151677SPeter Maydell                                            const char *name, int n)
6484a151677SPeter Maydell {
6494a151677SPeter Maydell     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
6504a151677SPeter Maydell }
651074a86fcSAnthony Liguori 
652cd07d7f9SPeter Maydell /**
653cd07d7f9SPeter Maydell  * qdev_pass_gpios: create GPIO lines on container which pass through to device
654cd07d7f9SPeter Maydell  * @dev: Device which has GPIO lines
655cd07d7f9SPeter Maydell  * @container: Container device which needs to expose them
656cd07d7f9SPeter Maydell  * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
657cd07d7f9SPeter Maydell  *
658cd07d7f9SPeter Maydell  * In QEMU, complicated devices like SoCs are often modelled with a
659cd07d7f9SPeter Maydell  * "container" QOM device which itself contains other QOM devices and
660cd07d7f9SPeter Maydell  * which wires them up appropriately. This function allows the container
661cd07d7f9SPeter Maydell  * to create GPIO arrays on itself which simply pass through to a GPIO
662cd07d7f9SPeter Maydell  * array of one of its internal devices.
663cd07d7f9SPeter Maydell  *
664cd07d7f9SPeter Maydell  * If @dev has both input and output GPIOs named @name then both will
665cd07d7f9SPeter Maydell  * be passed through. It is not possible to pass a subset of the array
666cd07d7f9SPeter Maydell  * with this function.
667cd07d7f9SPeter Maydell  *
668cd07d7f9SPeter Maydell  * To users of the container device, the GPIO array created on @container
669cd07d7f9SPeter Maydell  * behaves exactly like any other.
670cd07d7f9SPeter Maydell  */
67117a96a14SPeter Crosthwaite void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
67217a96a14SPeter Crosthwaite                      const char *name);
67317a96a14SPeter Crosthwaite 
674074a86fcSAnthony Liguori BusState *qdev_get_parent_bus(DeviceState *dev);
675074a86fcSAnthony Liguori 
676074a86fcSAnthony Liguori /*** BUS API. ***/
677074a86fcSAnthony Liguori 
678074a86fcSAnthony Liguori DeviceState *qdev_find_recursive(BusState *bus, const char *id);
679074a86fcSAnthony Liguori 
680074a86fcSAnthony Liguori /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
681074a86fcSAnthony Liguori typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
682074a86fcSAnthony Liguori typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
683074a86fcSAnthony Liguori 
684fb17dfe0SAndreas Färber void qbus_create_inplace(void *bus, size_t size, const char *typename,
685074a86fcSAnthony Liguori                          DeviceState *parent, const char *name);
686074a86fcSAnthony Liguori BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
6879940b2cfSMarkus Armbruster bool qbus_realize(BusState *bus, Error **errp);
6889940b2cfSMarkus Armbruster void qbus_unrealize(BusState *bus);
6899940b2cfSMarkus Armbruster 
690074a86fcSAnthony Liguori /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
691074a86fcSAnthony Liguori  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
692074a86fcSAnthony Liguori  *           0 otherwise. */
6930293214bSPaolo Bonzini int qbus_walk_children(BusState *bus,
6940293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
6950293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
6960293214bSPaolo Bonzini                        void *opaque);
6970293214bSPaolo Bonzini int qdev_walk_children(DeviceState *dev,
6980293214bSPaolo Bonzini                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
6990293214bSPaolo Bonzini                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
7000293214bSPaolo Bonzini                        void *opaque);
7010293214bSPaolo Bonzini 
702abb89dbfSDamien Hedde /**
703abb89dbfSDamien Hedde  * @qdev_reset_all:
704abb89dbfSDamien Hedde  * Reset @dev. See @qbus_reset_all() for more details.
705abb89dbfSDamien Hedde  *
706abb89dbfSDamien Hedde  * Note: This function is deprecated and will be removed when it becomes unused.
707abb89dbfSDamien Hedde  * Please use device_cold_reset() now.
708abb89dbfSDamien Hedde  */
709074a86fcSAnthony Liguori void qdev_reset_all(DeviceState *dev);
710ff8de075SDavid Hildenbrand void qdev_reset_all_fn(void *opaque);
711d0508c36SPaolo Bonzini 
712d0508c36SPaolo Bonzini /**
713d0508c36SPaolo Bonzini  * @qbus_reset_all:
714d0508c36SPaolo Bonzini  * @bus: Bus to be reset.
715d0508c36SPaolo Bonzini  *
716d0508c36SPaolo Bonzini  * Reset @bus and perform a bus-level ("hard") reset of all devices connected
717d0508c36SPaolo Bonzini  * to it, including recursive processing of all buses below @bus itself.  A
718d0508c36SPaolo Bonzini  * hard reset means that qbus_reset_all will reset all state of the device.
719d0508c36SPaolo Bonzini  * For PCI devices, for example, this will include the base address registers
720d0508c36SPaolo Bonzini  * or configuration space.
721abb89dbfSDamien Hedde  *
722abb89dbfSDamien Hedde  * Note: This function is deprecated and will be removed when it becomes unused.
723abb89dbfSDamien Hedde  * Please use bus_cold_reset() now.
724d0508c36SPaolo Bonzini  */
725d0508c36SPaolo Bonzini void qbus_reset_all(BusState *bus);
726074a86fcSAnthony Liguori void qbus_reset_all_fn(void *opaque);
727074a86fcSAnthony Liguori 
728c11256aaSDamien Hedde /**
729abb89dbfSDamien Hedde  * device_cold_reset:
730abb89dbfSDamien Hedde  * Reset device @dev and perform a recursive processing using the resettable
731abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
732abb89dbfSDamien Hedde  */
733abb89dbfSDamien Hedde void device_cold_reset(DeviceState *dev);
734abb89dbfSDamien Hedde 
735abb89dbfSDamien Hedde /**
736abb89dbfSDamien Hedde  * bus_cold_reset:
737abb89dbfSDamien Hedde  *
738abb89dbfSDamien Hedde  * Reset bus @bus and perform a recursive processing using the resettable
739abb89dbfSDamien Hedde  * interface. It triggers a RESET_TYPE_COLD.
740abb89dbfSDamien Hedde  */
741abb89dbfSDamien Hedde void bus_cold_reset(BusState *bus);
742abb89dbfSDamien Hedde 
743abb89dbfSDamien Hedde /**
744c11256aaSDamien Hedde  * device_is_in_reset:
745c11256aaSDamien Hedde  * Return true if the device @dev is currently being reset.
746c11256aaSDamien Hedde  */
747c11256aaSDamien Hedde bool device_is_in_reset(DeviceState *dev);
748c11256aaSDamien Hedde 
749c11256aaSDamien Hedde /**
750c11256aaSDamien Hedde  * bus_is_in_reset:
751c11256aaSDamien Hedde  * Return true if the bus @bus is currently being reset.
752c11256aaSDamien Hedde  */
753c11256aaSDamien Hedde bool bus_is_in_reset(BusState *bus);
754c11256aaSDamien Hedde 
755074a86fcSAnthony Liguori /* This should go away once we get rid of the NULL bus hack */
756074a86fcSAnthony Liguori BusState *sysbus_get_default(void);
757074a86fcSAnthony Liguori 
758074a86fcSAnthony Liguori char *qdev_get_fw_dev_path(DeviceState *dev);
7590be63901SGonglei char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
760074a86fcSAnthony Liguori 
761074a86fcSAnthony Liguori /**
762074a86fcSAnthony Liguori  * @qdev_machine_init
763074a86fcSAnthony Liguori  *
764074a86fcSAnthony Liguori  * Initialize platform devices before machine init.  This is a hack until full
765074a86fcSAnthony Liguori  * support for composition is added.
766074a86fcSAnthony Liguori  */
767074a86fcSAnthony Liguori void qdev_machine_init(void);
768074a86fcSAnthony Liguori 
769074a86fcSAnthony Liguori /**
770f703a04cSDamien Hedde  * device_legacy_reset:
771074a86fcSAnthony Liguori  *
772074a86fcSAnthony Liguori  * Reset a single device (by calling the reset method).
773abb89dbfSDamien Hedde  * Note: This function is deprecated and will be removed when it becomes unused.
774abb89dbfSDamien Hedde  * Please use device_cold_reset() now.
775074a86fcSAnthony Liguori  */
776f703a04cSDamien Hedde void device_legacy_reset(DeviceState *dev);
777074a86fcSAnthony Liguori 
7784f67d30bSMarc-André Lureau void device_class_set_props(DeviceClass *dc, Property *props);
7794f67d30bSMarc-André Lureau 
780c11256aaSDamien Hedde /**
781c11256aaSDamien Hedde  * device_class_set_parent_reset:
782c11256aaSDamien Hedde  * TODO: remove the function when DeviceClass's reset method
783c11256aaSDamien Hedde  * is not used anymore.
784c11256aaSDamien Hedde  */
78546795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_reset(DeviceClass *dc,
78646795cf2SPhilippe Mathieu-Daudé                                    DeviceReset dev_reset,
78746795cf2SPhilippe Mathieu-Daudé                                    DeviceReset *parent_reset);
78846795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_realize(DeviceClass *dc,
78946795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize dev_realize,
79046795cf2SPhilippe Mathieu-Daudé                                      DeviceRealize *parent_realize);
79146795cf2SPhilippe Mathieu-Daudé void device_class_set_parent_unrealize(DeviceClass *dc,
79246795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize dev_unrealize,
79346795cf2SPhilippe Mathieu-Daudé                                        DeviceUnrealize *parent_unrealize);
79446795cf2SPhilippe Mathieu-Daudé 
7958a9358ccSMarkus Armbruster const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
796074a86fcSAnthony Liguori 
797074a86fcSAnthony Liguori const char *qdev_fw_name(DeviceState *dev);
798074a86fcSAnthony Liguori 
799074a86fcSAnthony Liguori Object *qdev_get_machine(void);
800074a86fcSAnthony Liguori 
801074a86fcSAnthony Liguori /* FIXME: make this a link<> */
802*bb755ba4SPaolo Bonzini bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
803074a86fcSAnthony Liguori 
8049bed84c1SJuan Quintela extern bool qdev_hotplug;
80521def24aSJuan Quintela extern bool qdev_hot_removed;
806074a86fcSAnthony Liguori 
807074a86fcSAnthony Liguori char *qdev_get_dev_path(DeviceState *dev);
808074a86fcSAnthony Liguori 
8099bc6bfdfSMarkus Armbruster void qbus_set_hotplug_handler(BusState *bus, Object *handler);
810cd7c8660SMarkus Armbruster void qbus_set_bus_hotplug_handler(BusState *bus);
81139b888bdSIgor Mammedov 
81239b888bdSIgor Mammedov static inline bool qbus_is_hotpluggable(BusState *bus)
81339b888bdSIgor Mammedov {
8142d9a982fSIgor Mammedov    return bus->hotplug_handler;
81539b888bdSIgor Mammedov }
816707ff800SPaul Durrant 
817707ff800SPaul Durrant void device_listener_register(DeviceListener *listener);
818707ff800SPaul Durrant void device_listener_unregister(DeviceListener *listener);
819707ff800SPaul Durrant 
820f3a85056SJens Freimann /**
821f3a85056SJens Freimann  * @qdev_should_hide_device:
822f3a85056SJens Freimann  * @opts: QemuOpts as passed on cmdline.
823f3a85056SJens Freimann  *
824f3a85056SJens Freimann  * Check if a device should be added.
825f3a85056SJens Freimann  * When a device is added via qdev_device_add() this will be called,
826f3a85056SJens Freimann  * and return if the device should be added now or not.
827f3a85056SJens Freimann  */
828f3a85056SJens Freimann bool qdev_should_hide_device(QemuOpts *opts);
829f3a85056SJens Freimann 
830074a86fcSAnthony Liguori #endif
831