xref: /openbmc/qemu/include/hw/qdev-core.h (revision 7f6c3d1a)
1 #ifndef QDEV_CORE_H
2 #define QDEV_CORE_H
3 
4 #include "qemu/queue.h"
5 #include "qemu/bitmap.h"
6 #include "qom/object.h"
7 #include "hw/hotplug.h"
8 #include "hw/resettable.h"
9 
10 enum {
11     DEV_NVECTORS_UNSPECIFIED = -1,
12 };
13 
14 #define TYPE_DEVICE "device"
15 OBJECT_DECLARE_TYPE(DeviceState, DeviceClass, DEVICE)
16 
17 typedef enum DeviceCategory {
18     DEVICE_CATEGORY_BRIDGE,
19     DEVICE_CATEGORY_USB,
20     DEVICE_CATEGORY_STORAGE,
21     DEVICE_CATEGORY_NETWORK,
22     DEVICE_CATEGORY_INPUT,
23     DEVICE_CATEGORY_DISPLAY,
24     DEVICE_CATEGORY_SOUND,
25     DEVICE_CATEGORY_MISC,
26     DEVICE_CATEGORY_CPU,
27     DEVICE_CATEGORY_MAX
28 } DeviceCategory;
29 
30 typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
31 typedef void (*DeviceUnrealize)(DeviceState *dev);
32 typedef void (*DeviceReset)(DeviceState *dev);
33 typedef void (*BusRealize)(BusState *bus, Error **errp);
34 typedef void (*BusUnrealize)(BusState *bus);
35 
36 /**
37  * DeviceClass:
38  * @props: Properties accessing state fields.
39  * @realize: Callback function invoked when the #DeviceState:realized
40  * property is changed to %true.
41  * @unrealize: Callback function invoked when the #DeviceState:realized
42  * property is changed to %false.
43  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
44  * as readonly "hotpluggable" property of #DeviceState instance
45  *
46  * # Realization #
47  * Devices are constructed in two stages,
48  * 1) object instantiation via object_initialize() and
49  * 2) device realization via #DeviceState:realized property.
50  * The former may not fail (and must not abort or exit, since it is called
51  * during device introspection already), and the latter may return error
52  * information to the caller and must be re-entrant.
53  * Trivial field initializations should go into #TypeInfo.instance_init.
54  * Operations depending on @props static properties should go into @realize.
55  * After successful realization, setting static properties will fail.
56  *
57  * As an interim step, the #DeviceState:realized property can also be
58  * set with qdev_realize().
59  * In the future, devices will propagate this state change to their children
60  * and along busses they expose.
61  * The point in time will be deferred to machine creation, so that values
62  * set in @realize will not be introspectable beforehand. Therefore devices
63  * must not create children during @realize; they should initialize them via
64  * object_initialize() in their own #TypeInfo.instance_init and forward the
65  * realization events appropriately.
66  *
67  * Any type may override the @realize and/or @unrealize callbacks but needs
68  * to call the parent type's implementation if keeping their functionality
69  * is desired. Refer to QOM documentation for further discussion and examples.
70  *
71  * <note>
72  *   <para>
73  * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
74  * derived directly from it need not call their parent's @realize and
75  * @unrealize.
76  * For other types consult the documentation and implementation of the
77  * respective parent types.
78  *   </para>
79  * </note>
80  *
81  * # Hiding a device #
82  * To hide a device, a DeviceListener function should_be_hidden() needs to
83  * be registered.
84  * It can be used to defer adding a device and therefore hide it from the
85  * guest. The handler registering to this DeviceListener can save the QOpts
86  * passed to it for re-using it later and must return that it wants the device
87  * to be/remain hidden or not. When the handler function decides the device
88  * shall not be hidden it will be added in qdev_device_add() and
89  * realized as any other device. Otherwise qdev_device_add() will return early
90  * without adding the device. The guest will not see a "hidden" device
91  * until it was marked don't hide and qdev_device_add called again.
92  *
93  */
94 struct DeviceClass {
95     /*< private >*/
96     ObjectClass parent_class;
97     /*< public >*/
98 
99     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
100     const char *fw_name;
101     const char *desc;
102 
103     /*
104      * The underscore at the end ensures a compile-time error if someone
105      * assigns to dc->props instead of using device_class_set_props.
106      */
107     Property *props_;
108 
109     /*
110      * Can this device be instantiated with -device / device_add?
111      * All devices should support instantiation with device_add, and
112      * this flag should not exist.  But we're not there, yet.  Some
113      * devices fail to instantiate with cryptic error messages.
114      * Others instantiate, but don't work.  Exposing users to such
115      * behavior would be cruel; clearing this flag will protect them.
116      * It should never be cleared without a comment explaining why it
117      * is cleared.
118      * TODO remove once we're there
119      */
120     bool user_creatable;
121     bool hotpluggable;
122 
123     /* callbacks */
124     /*
125      * Reset method here is deprecated and replaced by methods in the
126      * resettable class interface to implement a multi-phase reset.
127      * TODO: remove once every reset callback is unused
128      */
129     DeviceReset reset;
130     DeviceRealize realize;
131     DeviceUnrealize unrealize;
132 
133     /* device state */
134     const VMStateDescription *vmsd;
135 
136     /* Private to qdev / bus.  */
137     const char *bus_type;
138 };
139 
140 typedef struct NamedGPIOList NamedGPIOList;
141 
142 struct NamedGPIOList {
143     char *name;
144     qemu_irq *in;
145     int num_in;
146     int num_out;
147     QLIST_ENTRY(NamedGPIOList) node;
148 };
149 
150 typedef struct Clock Clock;
151 typedef struct NamedClockList NamedClockList;
152 
153 struct NamedClockList {
154     char *name;
155     Clock *clock;
156     bool output;
157     bool alias;
158     QLIST_ENTRY(NamedClockList) node;
159 };
160 
161 /**
162  * DeviceState:
163  * @realized: Indicates whether the device has been fully constructed.
164  * @reset: ResettableState for the device; handled by Resettable interface.
165  *
166  * This structure should not be accessed directly.  We declare it here
167  * so that it can be embedded in individual device state structures.
168  */
169 struct DeviceState {
170     /*< private >*/
171     Object parent_obj;
172     /*< public >*/
173 
174     const char *id;
175     char *canonical_path;
176     bool realized;
177     bool pending_deleted_event;
178     QemuOpts *opts;
179     int hotplugged;
180     bool allow_unplug_during_migration;
181     BusState *parent_bus;
182     QLIST_HEAD(, NamedGPIOList) gpios;
183     QLIST_HEAD(, NamedClockList) clocks;
184     QLIST_HEAD(, BusState) child_bus;
185     int num_child_bus;
186     int instance_id_alias;
187     int alias_required_for_version;
188     ResettableState reset;
189 };
190 
191 struct DeviceListener {
192     void (*realize)(DeviceListener *listener, DeviceState *dev);
193     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
194     /*
195      * This callback is called upon init of the DeviceState and allows to
196      * inform qdev that a device should be hidden, depending on the device
197      * opts, for example, to hide a standby device.
198      */
199     int (*should_be_hidden)(DeviceListener *listener, QemuOpts *device_opts);
200     QTAILQ_ENTRY(DeviceListener) link;
201 };
202 
203 #define TYPE_BUS "bus"
204 DECLARE_OBJ_CHECKERS(BusState, BusClass,
205                      BUS, TYPE_BUS)
206 
207 struct BusClass {
208     ObjectClass parent_class;
209 
210     /* FIXME first arg should be BusState */
211     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
212     char *(*get_dev_path)(DeviceState *dev);
213     /*
214      * This callback is used to create Open Firmware device path in accordance
215      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
216      * bindings can be found at http://playground.sun.com/1275/bindings/.
217      */
218     char *(*get_fw_dev_path)(DeviceState *dev);
219     void (*reset)(BusState *bus);
220     BusRealize realize;
221     BusUnrealize unrealize;
222 
223     /* maximum devices allowed on the bus, 0: no limit. */
224     int max_dev;
225     /* number of automatically allocated bus ids (e.g. ide.0) */
226     int automatic_ids;
227 };
228 
229 typedef struct BusChild {
230     DeviceState *child;
231     int index;
232     QTAILQ_ENTRY(BusChild) sibling;
233 } BusChild;
234 
235 #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
236 
237 /**
238  * BusState:
239  * @hotplug_handler: link to a hotplug handler associated with bus.
240  * @reset: ResettableState for the bus; handled by Resettable interface.
241  */
242 struct BusState {
243     Object obj;
244     DeviceState *parent;
245     char *name;
246     HotplugHandler *hotplug_handler;
247     int max_index;
248     bool realized;
249     int num_children;
250     QTAILQ_HEAD(, BusChild) children;
251     QLIST_ENTRY(BusState) sibling;
252     ResettableState reset;
253 };
254 
255 /**
256  * Property:
257  * @set_default: true if the default value should be set from @defval,
258  *    in which case @info->set_default_value must not be NULL
259  *    (if false then no default value is set by the property system
260  *     and the field retains whatever value it was given by instance_init).
261  * @defval: default value for the property. This is used only if @set_default
262  *     is true.
263  */
264 struct Property {
265     const char   *name;
266     const PropertyInfo *info;
267     ptrdiff_t    offset;
268     uint8_t      bitnr;
269     bool         set_default;
270     union {
271         int64_t i;
272         uint64_t u;
273     } defval;
274     int          arrayoffset;
275     const PropertyInfo *arrayinfo;
276     int          arrayfieldsize;
277     const char   *link_type;
278 };
279 
280 struct PropertyInfo {
281     const char *name;
282     const char *description;
283     const QEnumLookup *enum_table;
284     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
285     void (*set_default_value)(ObjectProperty *op, const Property *prop);
286     void (*create)(ObjectClass *oc, Property *prop);
287     ObjectPropertyAccessor *get;
288     ObjectPropertyAccessor *set;
289     ObjectPropertyRelease *release;
290 };
291 
292 /**
293  * GlobalProperty:
294  * @used: Set to true if property was used when initializing a device.
295  * @optional: If set to true, GlobalProperty will be skipped without errors
296  *            if the property doesn't exist.
297  *
298  * An error is fatal for non-hotplugged devices, when the global is applied.
299  */
300 typedef struct GlobalProperty {
301     const char *driver;
302     const char *property;
303     const char *value;
304     bool used;
305     bool optional;
306 } GlobalProperty;
307 
308 static inline void
309 compat_props_add(GPtrArray *arr,
310                  GlobalProperty props[], size_t nelem)
311 {
312     int i;
313     for (i = 0; i < nelem; i++) {
314         g_ptr_array_add(arr, (void *)&props[i]);
315     }
316 }
317 
318 /*** Board API.  This should go away once we have a machine config file.  ***/
319 
320 /**
321  * qdev_new: Create a device on the heap
322  * @name: device type to create (we assert() that this type exists)
323  *
324  * This only allocates the memory and initializes the device state
325  * structure, ready for the caller to set properties if they wish.
326  * The device still needs to be realized.
327  * The returned object has a reference count of 1.
328  */
329 DeviceState *qdev_new(const char *name);
330 /**
331  * qdev_try_new: Try to create a device on the heap
332  * @name: device type to create
333  *
334  * This is like qdev_new(), except it returns %NULL when type @name
335  * does not exist, rather than asserting.
336  */
337 DeviceState *qdev_try_new(const char *name);
338 /**
339  * qdev_realize: Realize @dev.
340  * @dev: device to realize
341  * @bus: bus to plug it into (may be NULL)
342  * @errp: pointer to error object
343  *
344  * "Realize" the device, i.e. perform the second phase of device
345  * initialization.
346  * @dev must not be plugged into a bus already.
347  * If @bus, plug @dev into @bus.  This takes a reference to @dev.
348  * If @dev has no QOM parent, make one up, taking another reference.
349  * On success, return true.
350  * On failure, store an error through @errp and return false.
351  *
352  * If you created @dev using qdev_new(), you probably want to use
353  * qdev_realize_and_unref() instead.
354  */
355 bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
356 /**
357  * qdev_realize_and_unref: Realize @dev and drop a reference
358  * @dev: device to realize
359  * @bus: bus to plug it into (may be NULL)
360  * @errp: pointer to error object
361  *
362  * Realize @dev and drop a reference.
363  * This is like qdev_realize(), except the caller must hold a
364  * (private) reference, which is dropped on return regardless of
365  * success or failure.  Intended use::
366  *
367  *     dev = qdev_new();
368  *     [...]
369  *     qdev_realize_and_unref(dev, bus, errp);
370  *
371  * Now @dev can go away without further ado.
372  *
373  * If you are embedding the device into some other QOM device and
374  * initialized it via some variant on object_initialize_child() then
375  * do not use this function, because that family of functions arrange
376  * for the only reference to the child device to be held by the parent
377  * via the child<> property, and so the reference-count-drop done here
378  * would be incorrect. For that use case you want qdev_realize().
379  */
380 bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
381 /**
382  * qdev_unrealize: Unrealize a device
383  * @dev: device to unrealize
384  *
385  * This function will "unrealize" a device, which is the first phase
386  * of correctly destroying a device that has been realized. It will:
387  *
388  *  - unrealize any child buses by calling qbus_unrealize()
389  *    (this will recursively unrealize any devices on those buses)
390  *  - call the the unrealize method of @dev
391  *
392  * The device can then be freed by causing its reference count to go
393  * to zero.
394  *
395  * Warning: most devices in QEMU do not expect to be unrealized.  Only
396  * devices which are hot-unpluggable should be unrealized (as part of
397  * the unplugging process); all other devices are expected to last for
398  * the life of the simulation and should not be unrealized and freed.
399  */
400 void qdev_unrealize(DeviceState *dev);
401 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
402                                  int required_for_version);
403 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
404 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
405 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
406 /**
407  * qdev_get_hotplug_handler: Get handler responsible for device wiring
408  *
409  * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
410  *
411  * Note: in case @dev has a parent bus, it will be returned as handler unless
412  * machine handler overrides it.
413  *
414  * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
415  *          or NULL if there aren't any.
416  */
417 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
418 void qdev_unplug(DeviceState *dev, Error **errp);
419 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
420                                   DeviceState *dev, Error **errp);
421 void qdev_machine_creation_done(void);
422 bool qdev_machine_modified(void);
423 
424 /**
425  * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
426  * @dev: Device whose GPIO we want
427  * @n: Number of the anonymous GPIO line (which must be in range)
428  *
429  * Returns the qemu_irq corresponding to an anonymous input GPIO line
430  * (which the device has set up with qdev_init_gpio_in()). The index
431  * @n of the GPIO line must be valid (i.e. be at least 0 and less than
432  * the total number of anonymous input GPIOs the device has); this
433  * function will assert() if passed an invalid index.
434  *
435  * This function is intended to be used by board code or SoC "container"
436  * device models to wire up the GPIO lines; usually the return value
437  * will be passed to qdev_connect_gpio_out() or a similar function to
438  * connect another device's output GPIO line to this input.
439  *
440  * For named input GPIO lines, use qdev_get_gpio_in_named().
441  */
442 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
443 /**
444  * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
445  * @dev: Device whose GPIO we want
446  * @name: Name of the input GPIO array
447  * @n: Number of the GPIO line in that array (which must be in range)
448  *
449  * Returns the qemu_irq corresponding to a named input GPIO line
450  * (which the device has set up with qdev_init_gpio_in_named()).
451  * The @name string must correspond to an input GPIO array which exists on
452  * the device, and the index @n of the GPIO line must be valid (i.e.
453  * be at least 0 and less than the total number of input GPIOs in that
454  * array); this function will assert() if passed an invalid name or index.
455  *
456  * For anonymous input GPIO lines, use qdev_get_gpio_in().
457  */
458 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
459 
460 /**
461  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
462  * @dev: Device whose GPIO to connect
463  * @n: Number of the anonymous output GPIO line (which must be in range)
464  * @pin: qemu_irq to connect the output line to
465  *
466  * This function connects an anonymous output GPIO line on a device
467  * up to an arbitrary qemu_irq, so that when the device asserts that
468  * output GPIO line, the qemu_irq's callback is invoked.
469  * The index @n of the GPIO line must be valid (i.e. be at least 0 and
470  * less than the total number of anonymous output GPIOs the device has
471  * created with qdev_init_gpio_out()); otherwise this function will assert().
472  *
473  * Outbound GPIO lines can be connected to any qemu_irq, but the common
474  * case is connecting them to another device's inbound GPIO line, using
475  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
476  *
477  * It is not valid to try to connect one outbound GPIO to multiple
478  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
479  * same qemu_irq. (Warning: there is no assertion or other guard to
480  * catch this error: the model will just not do the right thing.)
481  * Instead, for fan-out you can use the TYPE_IRQ_SPLIT device: connect
482  * a device's outbound GPIO to the splitter's input, and connect each
483  * of the splitter's outputs to a different device.  For fan-in you
484  * can use the TYPE_OR_IRQ device, which is a model of a logical OR
485  * gate with multiple inputs and one output.
486  *
487  * For named output GPIO lines, use qdev_connect_gpio_out_named().
488  */
489 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
490 /**
491  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
492  * @dev: Device whose GPIO to connect
493  * @name: Name of the output GPIO array
494  * @n: Number of the anonymous output GPIO line (which must be in range)
495  * @pin: qemu_irq to connect the output line to
496  *
497  * This function connects an anonymous output GPIO line on a device
498  * up to an arbitrary qemu_irq, so that when the device asserts that
499  * output GPIO line, the qemu_irq's callback is invoked.
500  * The @name string must correspond to an output GPIO array which exists on
501  * the device, and the index @n of the GPIO line must be valid (i.e.
502  * be at least 0 and less than the total number of input GPIOs in that
503  * array); this function will assert() if passed an invalid name or index.
504  *
505  * Outbound GPIO lines can be connected to any qemu_irq, but the common
506  * case is connecting them to another device's inbound GPIO line, using
507  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
508  *
509  * It is not valid to try to connect one outbound GPIO to multiple
510  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
511  * same qemu_irq; see qdev_connect_gpio_out() for details.
512  *
513  * For named output GPIO lines, use qdev_connect_gpio_out_named().
514  */
515 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
516                                  qemu_irq pin);
517 /**
518  * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
519  * @dev: Device whose output GPIO we are interested in
520  * @name: Name of the output GPIO array
521  * @n: Number of the output GPIO line within that array
522  *
523  * Returns whatever qemu_irq is currently connected to the specified
524  * output GPIO line of @dev. This will be NULL if the output GPIO line
525  * has never been wired up to the anything.  Note that the qemu_irq
526  * returned does not belong to @dev -- it will be the input GPIO or
527  * IRQ of whichever device the board code has connected up to @dev's
528  * output GPIO.
529  *
530  * You probably don't need to use this function -- it is used only
531  * by the platform-bus subsystem.
532  */
533 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
534 /**
535  * qdev_intercept_gpio_out: Intercept an existing GPIO connection
536  * @dev: Device to intercept the outbound GPIO line from
537  * @icpt: New qemu_irq to connect instead
538  * @name: Name of the output GPIO array
539  * @n: Number of the GPIO line in the array
540  *
541  * This function is provided only for use by the qtest testing framework
542  * and is not suitable for use in non-testing parts of QEMU.
543  *
544  * This function breaks an existing connection of an outbound GPIO
545  * line from @dev, and replaces it with the new qemu_irq @icpt, as if
546  * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
547  * The previously connected qemu_irq is returned, so it can be restored
548  * by a second call to qdev_intercept_gpio_out() if desired.
549  */
550 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
551                                  const char *name, int n);
552 
553 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
554 
555 /*** Device API.  ***/
556 
557 /**
558  * qdev_init_gpio_in: create an array of anonymous input GPIO lines
559  * @dev: Device to create input GPIOs for
560  * @handler: Function to call when GPIO line value is set
561  * @n: Number of GPIO lines to create
562  *
563  * Devices should use functions in the qdev_init_gpio_in* family in
564  * their instance_init or realize methods to create any input GPIO
565  * lines they need. There is no functional difference between
566  * anonymous and named GPIO lines. Stylistically, named GPIOs are
567  * preferable (easier to understand at callsites) unless a device
568  * has exactly one uniform kind of GPIO input whose purpose is obvious.
569  * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
570  *
571  * See qdev_get_gpio_in() for how code that uses such a device can get
572  * hold of an input GPIO line to manipulate it.
573  */
574 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
575 /**
576  * qdev_init_gpio_out: create an array of anonymous output GPIO lines
577  * @dev: Device to create output GPIOs for
578  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
579  * @n: Number of GPIO lines to create
580  *
581  * Devices should use functions in the qdev_init_gpio_out* family
582  * in their instance_init or realize methods to create any output
583  * GPIO lines they need. There is no functional difference between
584  * anonymous and named GPIO lines. Stylistically, named GPIOs are
585  * preferable (easier to understand at callsites) unless a device
586  * has exactly one uniform kind of GPIO output whose purpose is obvious.
587  *
588  * The @pins argument should be a pointer to either a "qemu_irq"
589  * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
590  * state structure. The device implementation can then raise and
591  * lower the GPIO line by calling qemu_set_irq(). (If anything is
592  * connected to the other end of the GPIO this will cause the handler
593  * function for that input GPIO to be called.)
594  *
595  * See qdev_connect_gpio_out() for how code that uses such a device
596  * can connect to one of its output GPIO lines.
597  */
598 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
599 /**
600  * qdev_init_gpio_out: create an array of named output GPIO lines
601  * @dev: Device to create output GPIOs for
602  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
603  * @name: Name to give this array of GPIO lines
604  * @n: Number of GPIO lines to create
605  *
606  * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
607  * with a name. Code using the device can then connect these GPIO lines
608  * using qdev_connect_gpio_out_named().
609  */
610 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
611                               const char *name, int n);
612 /**
613  * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
614  *   for the specified device
615  *
616  * @dev: Device to create input GPIOs for
617  * @handler: Function to call when GPIO line value is set
618  * @opaque: Opaque data pointer to pass to @handler
619  * @name: Name of the GPIO input (must be unique for this device)
620  * @n: Number of GPIO lines in this input set
621  */
622 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
623                                          qemu_irq_handler handler,
624                                          void *opaque,
625                                          const char *name, int n);
626 
627 /**
628  * qdev_init_gpio_in_named: create an array of input GPIO lines
629  *   for the specified device
630  *
631  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
632  * passed to the handler is @dev (which is the most commonly desired behaviour).
633  */
634 static inline void qdev_init_gpio_in_named(DeviceState *dev,
635                                            qemu_irq_handler handler,
636                                            const char *name, int n)
637 {
638     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
639 }
640 
641 /**
642  * qdev_pass_gpios: create GPIO lines on container which pass through to device
643  * @dev: Device which has GPIO lines
644  * @container: Container device which needs to expose them
645  * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
646  *
647  * In QEMU, complicated devices like SoCs are often modelled with a
648  * "container" QOM device which itself contains other QOM devices and
649  * which wires them up appropriately. This function allows the container
650  * to create GPIO arrays on itself which simply pass through to a GPIO
651  * array of one of its internal devices.
652  *
653  * If @dev has both input and output GPIOs named @name then both will
654  * be passed through. It is not possible to pass a subset of the array
655  * with this function.
656  *
657  * To users of the container device, the GPIO array created on @container
658  * behaves exactly like any other.
659  */
660 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
661                      const char *name);
662 
663 BusState *qdev_get_parent_bus(DeviceState *dev);
664 
665 /*** BUS API. ***/
666 
667 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
668 
669 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
670 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
671 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
672 
673 void qbus_create_inplace(void *bus, size_t size, const char *typename,
674                          DeviceState *parent, const char *name);
675 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
676 bool qbus_realize(BusState *bus, Error **errp);
677 void qbus_unrealize(BusState *bus);
678 
679 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
680  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
681  *           0 otherwise. */
682 int qbus_walk_children(BusState *bus,
683                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
684                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
685                        void *opaque);
686 int qdev_walk_children(DeviceState *dev,
687                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
688                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
689                        void *opaque);
690 
691 /**
692  * @qdev_reset_all:
693  * Reset @dev. See @qbus_reset_all() for more details.
694  *
695  * Note: This function is deprecated and will be removed when it becomes unused.
696  * Please use device_cold_reset() now.
697  */
698 void qdev_reset_all(DeviceState *dev);
699 void qdev_reset_all_fn(void *opaque);
700 
701 /**
702  * @qbus_reset_all:
703  * @bus: Bus to be reset.
704  *
705  * Reset @bus and perform a bus-level ("hard") reset of all devices connected
706  * to it, including recursive processing of all buses below @bus itself.  A
707  * hard reset means that qbus_reset_all will reset all state of the device.
708  * For PCI devices, for example, this will include the base address registers
709  * or configuration space.
710  *
711  * Note: This function is deprecated and will be removed when it becomes unused.
712  * Please use bus_cold_reset() now.
713  */
714 void qbus_reset_all(BusState *bus);
715 void qbus_reset_all_fn(void *opaque);
716 
717 /**
718  * device_cold_reset:
719  * Reset device @dev and perform a recursive processing using the resettable
720  * interface. It triggers a RESET_TYPE_COLD.
721  */
722 void device_cold_reset(DeviceState *dev);
723 
724 /**
725  * bus_cold_reset:
726  *
727  * Reset bus @bus and perform a recursive processing using the resettable
728  * interface. It triggers a RESET_TYPE_COLD.
729  */
730 void bus_cold_reset(BusState *bus);
731 
732 /**
733  * device_is_in_reset:
734  * Return true if the device @dev is currently being reset.
735  */
736 bool device_is_in_reset(DeviceState *dev);
737 
738 /**
739  * bus_is_in_reset:
740  * Return true if the bus @bus is currently being reset.
741  */
742 bool bus_is_in_reset(BusState *bus);
743 
744 /* This should go away once we get rid of the NULL bus hack */
745 BusState *sysbus_get_default(void);
746 
747 char *qdev_get_fw_dev_path(DeviceState *dev);
748 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
749 
750 /**
751  * @qdev_machine_init
752  *
753  * Initialize platform devices before machine init.  This is a hack until full
754  * support for composition is added.
755  */
756 void qdev_machine_init(void);
757 
758 /**
759  * device_legacy_reset:
760  *
761  * Reset a single device (by calling the reset method).
762  * Note: This function is deprecated and will be removed when it becomes unused.
763  * Please use device_cold_reset() now.
764  */
765 void device_legacy_reset(DeviceState *dev);
766 
767 void device_class_set_props(DeviceClass *dc, Property *props);
768 
769 /**
770  * device_class_set_parent_reset:
771  * TODO: remove the function when DeviceClass's reset method
772  * is not used anymore.
773  */
774 void device_class_set_parent_reset(DeviceClass *dc,
775                                    DeviceReset dev_reset,
776                                    DeviceReset *parent_reset);
777 void device_class_set_parent_realize(DeviceClass *dc,
778                                      DeviceRealize dev_realize,
779                                      DeviceRealize *parent_realize);
780 void device_class_set_parent_unrealize(DeviceClass *dc,
781                                        DeviceUnrealize dev_unrealize,
782                                        DeviceUnrealize *parent_unrealize);
783 
784 const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
785 
786 const char *qdev_fw_name(DeviceState *dev);
787 
788 Object *qdev_get_machine(void);
789 
790 /* FIXME: make this a link<> */
791 void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
792 
793 extern bool qdev_hotplug;
794 extern bool qdev_hot_removed;
795 
796 char *qdev_get_dev_path(DeviceState *dev);
797 
798 void qbus_set_hotplug_handler(BusState *bus, Object *handler);
799 void qbus_set_bus_hotplug_handler(BusState *bus);
800 
801 static inline bool qbus_is_hotpluggable(BusState *bus)
802 {
803    return bus->hotplug_handler;
804 }
805 
806 void device_listener_register(DeviceListener *listener);
807 void device_listener_unregister(DeviceListener *listener);
808 
809 /**
810  * @qdev_should_hide_device:
811  * @opts: QemuOpts as passed on cmdline.
812  *
813  * Check if a device should be added.
814  * When a device is added via qdev_device_add() this will be called,
815  * and return if the device should be added now or not.
816  */
817 bool qdev_should_hide_device(QemuOpts *opts);
818 
819 #endif
820