xref: /openbmc/qemu/include/hw/qdev-core.h (revision e50a24ea)
1 #ifndef QDEV_CORE_H
2 #define QDEV_CORE_H
3 
4 #include "qemu/atomic.h"
5 #include "qemu/queue.h"
6 #include "qemu/bitmap.h"
7 #include "qemu/rcu.h"
8 #include "qemu/rcu_queue.h"
9 #include "qom/object.h"
10 #include "hw/hotplug.h"
11 #include "hw/resettable.h"
12 
13 /**
14  * DOC: The QEMU Device API
15  *
16  * All modern devices should represented as a derived QOM class of
17  * TYPE_DEVICE. The device API introduces the additional methods of
18  * @realize and @unrealize to represent additional stages in a device
19  * objects life cycle.
20  *
21  * Realization
22  * -----------
23  *
24  * Devices are constructed in two stages:
25  *
26  * 1) object instantiation via object_initialize() and
27  * 2) device realization via the #DeviceState.realized property
28  *
29  * The former may not fail (and must not abort or exit, since it is called
30  * during device introspection already), and the latter may return error
31  * information to the caller and must be re-entrant.
32  * Trivial field initializations should go into #TypeInfo.instance_init.
33  * Operations depending on @props static properties should go into @realize.
34  * After successful realization, setting static properties will fail.
35  *
36  * As an interim step, the #DeviceState.realized property can also be
37  * set with qdev_realize(). In the future, devices will propagate this
38  * state change to their children and along busses they expose. The
39  * point in time will be deferred to machine creation, so that values
40  * set in @realize will not be introspectable beforehand. Therefore
41  * devices must not create children during @realize; they should
42  * initialize them via object_initialize() in their own
43  * #TypeInfo.instance_init and forward the realization events
44  * appropriately.
45  *
46  * Any type may override the @realize and/or @unrealize callbacks but needs
47  * to call the parent type's implementation if keeping their functionality
48  * is desired. Refer to QOM documentation for further discussion and examples.
49  *
50  * .. note::
51  *   Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
52  *   derived directly from it need not call their parent's @realize and
53  *   @unrealize. For other types consult the documentation and
54  *   implementation of the respective parent types.
55  *
56  * Hiding a device
57  * ---------------
58  *
59  * To hide a device, a DeviceListener function hide_device() needs to
60  * be registered. It can be used to defer adding a device and
61  * therefore hide it from the guest. The handler registering to this
62  * DeviceListener can save the QOpts passed to it for re-using it
63  * later. It must return if it wants the device to be hidden or
64  * visible. When the handler function decides the device shall be
65  * visible it will be added with qdev_device_add() and realized as any
66  * other device. Otherwise qdev_device_add() will return early without
67  * adding the device. The guest will not see a "hidden" device until
68  * it was marked visible and qdev_device_add called again.
69  *
70  */
71 
72 enum {
73     DEV_NVECTORS_UNSPECIFIED = -1,
74 };
75 
76 #define TYPE_DEVICE "device"
77 OBJECT_DECLARE_TYPE(DeviceState, DeviceClass, DEVICE)
78 
79 typedef enum DeviceCategory {
80     DEVICE_CATEGORY_BRIDGE,
81     DEVICE_CATEGORY_USB,
82     DEVICE_CATEGORY_STORAGE,
83     DEVICE_CATEGORY_NETWORK,
84     DEVICE_CATEGORY_INPUT,
85     DEVICE_CATEGORY_DISPLAY,
86     DEVICE_CATEGORY_SOUND,
87     DEVICE_CATEGORY_MISC,
88     DEVICE_CATEGORY_CPU,
89     DEVICE_CATEGORY_WATCHDOG,
90     DEVICE_CATEGORY_MAX
91 } DeviceCategory;
92 
93 typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
94 typedef void (*DeviceUnrealize)(DeviceState *dev);
95 typedef void (*DeviceReset)(DeviceState *dev);
96 typedef void (*BusRealize)(BusState *bus, Error **errp);
97 typedef void (*BusUnrealize)(BusState *bus);
98 typedef int (*DeviceSyncConfig)(DeviceState *dev, Error **errp);
99 
100 /**
101  * struct DeviceClass - The base class for all devices.
102  * @props: Properties accessing state fields.
103  * @realize: Callback function invoked when the #DeviceState:realized
104  * property is changed to %true.
105  * @unrealize: Callback function invoked when the #DeviceState:realized
106  * property is changed to %false.
107  * @sync_config: Callback function invoked when QMP command device-sync-config
108  * is called. Should synchronize device configuration from host to guest part
109  * and notify the guest about the change.
110  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
111  * as readonly "hotpluggable" property of #DeviceState instance
112  *
113  */
114 struct DeviceClass {
115     /* private: */
116     ObjectClass parent_class;
117 
118     /* public: */
119 
120     /**
121      * @categories: device categories device belongs to
122      */
123     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
124     /**
125      * @fw_name: name used to identify device to firmware interfaces
126      */
127     const char *fw_name;
128     /**
129      * @desc: human readable description of device
130      */
131     const char *desc;
132 
133     /**
134      * @props_: properties associated with device, should only be
135      * assigned by using device_class_set_props(). The underscore
136      * ensures a compile-time error if someone attempts to assign
137      * dc->props directly.
138      */
139     const Property *props_;
140 
141     /**
142      * @user_creatable: Can user instantiate with -device / device_add?
143      *
144      * All devices should support instantiation with device_add, and
145      * this flag should not exist.  But we're not there, yet.  Some
146      * devices fail to instantiate with cryptic error messages.
147      * Others instantiate, but don't work.  Exposing users to such
148      * behavior would be cruel; clearing this flag will protect them.
149      * It should never be cleared without a comment explaining why it
150      * is cleared.
151      *
152      * TODO remove once we're there
153      */
154     bool user_creatable;
155     bool hotpluggable;
156 
157     /* callbacks */
158     /**
159      * @legacy_reset: deprecated device reset method pointer
160      *
161      * Modern code should use the ResettableClass interface to
162      * implement a multi-phase reset.
163      *
164      * TODO: remove once every reset callback is unused
165      */
166     DeviceReset legacy_reset;
167     DeviceRealize realize;
168     DeviceUnrealize unrealize;
169     DeviceSyncConfig sync_config;
170 
171     /**
172      * @vmsd: device state serialisation description for
173      * migration/save/restore
174      */
175     const VMStateDescription *vmsd;
176 
177     /**
178      * @bus_type: bus type
179      * private: to qdev / bus.
180      */
181     const char *bus_type;
182 };
183 
184 typedef struct NamedGPIOList NamedGPIOList;
185 
186 struct NamedGPIOList {
187     char *name;
188     qemu_irq *in;
189     int num_in;
190     int num_out;
191     QLIST_ENTRY(NamedGPIOList) node;
192 };
193 
194 typedef struct Clock Clock;
195 typedef struct NamedClockList NamedClockList;
196 
197 struct NamedClockList {
198     char *name;
199     Clock *clock;
200     bool output;
201     bool alias;
202     QLIST_ENTRY(NamedClockList) node;
203 };
204 
205 typedef struct {
206     bool engaged_in_io;
207 } MemReentrancyGuard;
208 
209 
210 typedef QLIST_HEAD(, NamedGPIOList) NamedGPIOListHead;
211 typedef QLIST_HEAD(, NamedClockList) NamedClockListHead;
212 typedef QLIST_HEAD(, BusState) BusStateHead;
213 
214 /**
215  * struct DeviceState - common device state, accessed with qdev helpers
216  *
217  * This structure should not be accessed directly.  We declare it here
218  * so that it can be embedded in individual device state structures.
219  */
220 struct DeviceState {
221     /* private: */
222     Object parent_obj;
223     /* public: */
224 
225     /**
226      * @id: global device id
227      */
228     char *id;
229     /**
230      * @canonical_path: canonical path of realized device in the QOM tree
231      */
232     char *canonical_path;
233     /**
234      * @realized: has device been realized?
235      */
236     bool realized;
237     /**
238      * @pending_deleted_event: track pending deletion events during unplug
239      */
240     bool pending_deleted_event;
241     /**
242      * @pending_deleted_expires_ms: optional timeout for deletion events
243      */
244     int64_t pending_deleted_expires_ms;
245     /**
246      * @opts: QDict of options for the device
247      */
248     QDict *opts;
249     /**
250      * @hotplugged: was device added after PHASE_MACHINE_READY?
251      */
252     int hotplugged;
253     /**
254      * @allow_unplug_during_migration: can device be unplugged during migration
255      */
256     bool allow_unplug_during_migration;
257     /**
258      * @parent_bus: bus this device belongs to
259      */
260     BusState *parent_bus;
261     /**
262      * @gpios: QLIST of named GPIOs the device provides.
263      */
264     NamedGPIOListHead gpios;
265     /**
266      * @clocks: QLIST of named clocks the device provides.
267      */
268     NamedClockListHead clocks;
269     /**
270      * @child_bus: QLIST of child buses
271      */
272     BusStateHead child_bus;
273     /**
274      * @num_child_bus: number of @child_bus entries
275      */
276     int num_child_bus;
277     /**
278      * @instance_id_alias: device alias for handling legacy migration setups
279      */
280     int instance_id_alias;
281     /**
282      * @alias_required_for_version: indicates @instance_id_alias is
283      * needed for migration
284      */
285     int alias_required_for_version;
286     /**
287      * @reset: ResettableState for the device; handled by Resettable interface.
288      */
289     ResettableState reset;
290     /**
291      * @unplug_blockers: list of reasons to block unplugging of device
292      */
293     GSList *unplug_blockers;
294     /**
295      * @mem_reentrancy_guard: Is the device currently in mmio/pio/dma?
296      *
297      * Used to prevent re-entrancy confusing things.
298      */
299     MemReentrancyGuard mem_reentrancy_guard;
300 };
301 
302 typedef struct DeviceListener DeviceListener;
303 struct DeviceListener {
304     void (*realize)(DeviceListener *listener, DeviceState *dev);
305     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
306     /*
307      * This callback is called upon init of the DeviceState and
308      * informs qdev if a device should be visible or hidden.  We can
309      * hide a failover device depending for example on the device
310      * opts.
311      *
312      * On errors, it returns false and errp is set. Device creation
313      * should fail in this case.
314      */
315     bool (*hide_device)(DeviceListener *listener, const QDict *device_opts,
316                         bool from_json, Error **errp);
317     QTAILQ_ENTRY(DeviceListener) link;
318 };
319 
320 #define TYPE_BUS "bus"
321 DECLARE_OBJ_CHECKERS(BusState, BusClass,
322                      BUS, TYPE_BUS)
323 
324 struct BusClass {
325     ObjectClass parent_class;
326 
327     /* FIXME first arg should be BusState */
328     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
329     char *(*get_dev_path)(DeviceState *dev);
330 
331     /*
332      * This callback is used to create Open Firmware device path in accordance
333      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
334      * bindings can be found at http://playground.sun.com/1275/bindings/.
335      */
336     char *(*get_fw_dev_path)(DeviceState *dev);
337 
338     /*
339      * Return whether the device can be added to @bus,
340      * based on the address that was set (via device properties)
341      * before realize.  If not, on return @errp contains the
342      * human-readable error message.
343      */
344     bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
345 
346     BusRealize realize;
347     BusUnrealize unrealize;
348 
349     /* maximum devices allowed on the bus, 0: no limit. */
350     int max_dev;
351     /* number of automatically allocated bus ids (e.g. ide.0) */
352     int automatic_ids;
353 };
354 
355 typedef struct BusChild {
356     struct rcu_head rcu;
357     DeviceState *child;
358     int index;
359     QTAILQ_ENTRY(BusChild) sibling;
360 } BusChild;
361 
362 #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
363 
364 typedef QTAILQ_HEAD(, BusChild) BusChildHead;
365 typedef QLIST_ENTRY(BusState) BusStateEntry;
366 
367 /**
368  * struct BusState:
369  * @obj: parent object
370  * @parent: parent Device
371  * @name: name of bus
372  * @hotplug_handler: link to a hotplug handler associated with bus.
373  * @max_index: max number of child buses
374  * @realized: is the bus itself realized?
375  * @full: is the bus full?
376  * @num_children: current number of child buses
377  */
378 struct BusState {
379     /* private: */
380     Object obj;
381     /* public: */
382     DeviceState *parent;
383     char *name;
384     HotplugHandler *hotplug_handler;
385     int max_index;
386     bool realized;
387     bool full;
388     int num_children;
389 
390     /**
391      * @children: an RCU protected QTAILQ, thus readers must use RCU
392      * to access it, and writers must hold the big qemu lock
393      */
394     BusChildHead children;
395     /**
396      * @sibling: next bus
397      */
398     BusStateEntry sibling;
399     /**
400      * @reset: ResettableState for the bus; handled by Resettable interface.
401      */
402     ResettableState reset;
403 };
404 
405 /**
406  * typedef GlobalProperty - a global property type
407  *
408  * @used: Set to true if property was used when initializing a device.
409  * @optional: If set to true, GlobalProperty will be skipped without errors
410  *            if the property doesn't exist.
411  *
412  * An error is fatal for non-hotplugged devices, when the global is applied.
413  */
414 typedef struct GlobalProperty {
415     const char *driver;
416     const char *property;
417     const char *value;
418     bool used;
419     bool optional;
420 } GlobalProperty;
421 
422 static inline void
423 compat_props_add(GPtrArray *arr,
424                  GlobalProperty props[], size_t nelem)
425 {
426     int i;
427     for (i = 0; i < nelem; i++) {
428         g_ptr_array_add(arr, (void *)&props[i]);
429     }
430 }
431 
432 /*** Board API.  This should go away once we have a machine config file.  ***/
433 
434 /**
435  * qdev_new: Create a device on the heap
436  * @name: device type to create (we assert() that this type exists)
437  *
438  * This only allocates the memory and initializes the device state
439  * structure, ready for the caller to set properties if they wish.
440  * The device still needs to be realized.
441  *
442  * Return: a derived DeviceState object with a reference count of 1.
443  */
444 DeviceState *qdev_new(const char *name);
445 
446 /**
447  * qdev_try_new: Try to create a device on the heap
448  * @name: device type to create
449  *
450  * This is like qdev_new(), except it returns %NULL when type @name
451  * does not exist, rather than asserting.
452  *
453  * Return: a derived DeviceState object with a reference count of 1 or
454  * NULL if type @name does not exist.
455  */
456 DeviceState *qdev_try_new(const char *name);
457 
458 /**
459  * qdev_is_realized() - check if device is realized
460  * @dev: The device to check.
461  *
462  * Context: May be called outside big qemu lock.
463  * Return: true if the device has been fully constructed, false otherwise.
464  */
465 static inline bool qdev_is_realized(DeviceState *dev)
466 {
467     return qatomic_load_acquire(&dev->realized);
468 }
469 
470 /**
471  * qdev_realize: Realize @dev.
472  * @dev: device to realize
473  * @bus: bus to plug it into (may be NULL)
474  * @errp: pointer to error object
475  *
476  * "Realize" the device, i.e. perform the second phase of device
477  * initialization.
478  * @dev must not be plugged into a bus already.
479  * If @bus, plug @dev into @bus.  This takes a reference to @dev.
480  * If @dev has no QOM parent, make one up, taking another reference.
481  *
482  * If you created @dev using qdev_new(), you probably want to use
483  * qdev_realize_and_unref() instead.
484  *
485  * Return: true on success, else false setting @errp with error
486  */
487 bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
488 
489 /**
490  * qdev_realize_and_unref: Realize @dev and drop a reference
491  * @dev: device to realize
492  * @bus: bus to plug it into (may be NULL)
493  * @errp: pointer to error object
494  *
495  * Realize @dev and drop a reference.
496  * This is like qdev_realize(), except the caller must hold a
497  * (private) reference, which is dropped on return regardless of
498  * success or failure.  Intended use::
499  *
500  *     dev = qdev_new();
501  *     [...]
502  *     qdev_realize_and_unref(dev, bus, errp);
503  *
504  * Now @dev can go away without further ado.
505  *
506  * If you are embedding the device into some other QOM device and
507  * initialized it via some variant on object_initialize_child() then
508  * do not use this function, because that family of functions arrange
509  * for the only reference to the child device to be held by the parent
510  * via the child<> property, and so the reference-count-drop done here
511  * would be incorrect. For that use case you want qdev_realize().
512  *
513  * Return: true on success, else false setting @errp with error
514  */
515 bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
516 
517 /**
518  * qdev_unrealize: Unrealize a device
519  * @dev: device to unrealize
520  *
521  * This function will "unrealize" a device, which is the first phase
522  * of correctly destroying a device that has been realized. It will:
523  *
524  *  - unrealize any child buses by calling qbus_unrealize()
525  *    (this will recursively unrealize any devices on those buses)
526  *  - call the unrealize method of @dev
527  *
528  * The device can then be freed by causing its reference count to go
529  * to zero.
530  *
531  * Warning: most devices in QEMU do not expect to be unrealized.  Only
532  * devices which are hot-unpluggable should be unrealized (as part of
533  * the unplugging process); all other devices are expected to last for
534  * the life of the simulation and should not be unrealized and freed.
535  */
536 void qdev_unrealize(DeviceState *dev);
537 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
538                                  int required_for_version);
539 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
540 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
541 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
542 
543 /**
544  * qdev_get_hotplug_handler() - Get handler responsible for device wiring
545  * @dev: the device we want the HOTPLUG_HANDLER for.
546  *
547  * Note: in case @dev has a parent bus, it will be returned as handler unless
548  * machine handler overrides it.
549  *
550  * Return: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
551  * or NULL if there aren't any.
552  */
553 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
554 void qdev_unplug(DeviceState *dev, Error **errp);
555 int qdev_sync_config(DeviceState *dev, Error **errp);
556 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
557                                   DeviceState *dev, Error **errp);
558 void qdev_machine_creation_done(void);
559 bool qdev_machine_modified(void);
560 
561 /**
562  * qdev_add_unplug_blocker: Add an unplug blocker to a device
563  *
564  * @dev: Device to be blocked from unplug
565  * @reason: Reason for blocking
566  */
567 void qdev_add_unplug_blocker(DeviceState *dev, Error *reason);
568 
569 /**
570  * qdev_del_unplug_blocker: Remove an unplug blocker from a device
571  *
572  * @dev: Device to be unblocked
573  * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
574  *          Used as a handle to lookup the blocker for deletion.
575  */
576 void qdev_del_unplug_blocker(DeviceState *dev, Error *reason);
577 
578 /**
579  * qdev_unplug_blocked: Confirm if a device is blocked from unplug
580  *
581  * @dev: Device to be tested
582  * @errp: The reasons why the device is blocked, if any
583  *
584  * Returns: true (also setting @errp) if device is blocked from unplug,
585  * false otherwise
586  */
587 bool qdev_unplug_blocked(DeviceState *dev, Error **errp);
588 
589 /**
590  * typedef GpioPolarity - Polarity of a GPIO line
591  *
592  * GPIO lines use either positive (active-high) logic,
593  * or negative (active-low) logic.
594  *
595  * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
596  * active when the voltage on the pin is high (relative to ground);
597  * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
598  * is active when the voltage on the pin is low (or grounded).
599  */
600 typedef enum {
601     GPIO_POLARITY_ACTIVE_LOW,
602     GPIO_POLARITY_ACTIVE_HIGH
603 } GpioPolarity;
604 
605 /**
606  * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
607  * @dev: Device whose GPIO we want
608  * @n: Number of the anonymous GPIO line (which must be in range)
609  *
610  * Returns the qemu_irq corresponding to an anonymous input GPIO line
611  * (which the device has set up with qdev_init_gpio_in()). The index
612  * @n of the GPIO line must be valid (i.e. be at least 0 and less than
613  * the total number of anonymous input GPIOs the device has); this
614  * function will assert() if passed an invalid index.
615  *
616  * This function is intended to be used by board code or SoC "container"
617  * device models to wire up the GPIO lines; usually the return value
618  * will be passed to qdev_connect_gpio_out() or a similar function to
619  * connect another device's output GPIO line to this input.
620  *
621  * For named input GPIO lines, use qdev_get_gpio_in_named().
622  *
623  * Return: qemu_irq corresponding to anonymous input GPIO line
624  */
625 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
626 
627 /**
628  * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
629  * @dev: Device whose GPIO we want
630  * @name: Name of the input GPIO array
631  * @n: Number of the GPIO line in that array (which must be in range)
632  *
633  * Returns the qemu_irq corresponding to a single input GPIO line
634  * in a named array of input GPIO lines on a device (which the device
635  * has set up with qdev_init_gpio_in_named()).
636  * The @name string must correspond to an input GPIO array which exists on
637  * the device, and the index @n of the GPIO line must be valid (i.e.
638  * be at least 0 and less than the total number of input GPIOs in that
639  * array); this function will assert() if passed an invalid name or index.
640  *
641  * For anonymous input GPIO lines, use qdev_get_gpio_in().
642  *
643  * Return: qemu_irq corresponding to named input GPIO line
644  */
645 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
646 
647 /**
648  * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
649  * @dev: Device whose GPIO to connect
650  * @n: Number of the anonymous output GPIO line (which must be in range)
651  * @pin: qemu_irq to connect the output line to
652  *
653  * This function connects an anonymous output GPIO line on a device
654  * up to an arbitrary qemu_irq, so that when the device asserts that
655  * output GPIO line, the qemu_irq's callback is invoked.
656  * The index @n of the GPIO line must be valid (i.e. be at least 0 and
657  * less than the total number of anonymous output GPIOs the device has
658  * created with qdev_init_gpio_out()); otherwise this function will assert().
659  *
660  * Outbound GPIO lines can be connected to any qemu_irq, but the common
661  * case is connecting them to another device's inbound GPIO line, using
662  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
663  *
664  * It is not valid to try to connect one outbound GPIO to multiple
665  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
666  * same qemu_irq. (Warning: there is no assertion or other guard to
667  * catch this error: the model will just not do the right thing.)
668  * Instead, for fan-out you can use the TYPE_SPLIT_IRQ device: connect
669  * a device's outbound GPIO to the splitter's input, and connect each
670  * of the splitter's outputs to a different device.  For fan-in you
671  * can use the TYPE_OR_IRQ device, which is a model of a logical OR
672  * gate with multiple inputs and one output.
673  *
674  * For named output GPIO lines, use qdev_connect_gpio_out_named().
675  */
676 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
677 
678 /**
679  * qdev_connect_gpio_out_named: Connect one of a device's named output
680  *                              GPIO lines
681  * @dev: Device whose GPIO to connect
682  * @name: Name of the output GPIO array
683  * @n: Number of the output GPIO line within that array (which must be in range)
684  * @input_pin: qemu_irq to connect the output line to
685  *
686  * This function connects a single GPIO output in a named array of output
687  * GPIO lines on a device up to an arbitrary qemu_irq, so that when the
688  * device asserts that output GPIO line, the qemu_irq's callback is invoked.
689  * The @name string must correspond to an output GPIO array which exists on
690  * the device, and the index @n of the GPIO line must be valid (i.e.
691  * be at least 0 and less than the total number of output GPIOs in that
692  * array); this function will assert() if passed an invalid name or index.
693  *
694  * Outbound GPIO lines can be connected to any qemu_irq, but the common
695  * case is connecting them to another device's inbound GPIO line, using
696  * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
697  *
698  * It is not valid to try to connect one outbound GPIO to multiple
699  * qemu_irqs at once, or to connect multiple outbound GPIOs to the
700  * same qemu_irq; see qdev_connect_gpio_out() for details.
701  *
702  * For anonymous output GPIO lines, use qdev_connect_gpio_out().
703  */
704 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
705                                  qemu_irq input_pin);
706 
707 /**
708  * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
709  * @dev: Device whose output GPIO we are interested in
710  * @name: Name of the output GPIO array
711  * @n: Number of the output GPIO line within that array
712  *
713  * Returns whatever qemu_irq is currently connected to the specified
714  * output GPIO line of @dev. This will be NULL if the output GPIO line
715  * has never been wired up to the anything.  Note that the qemu_irq
716  * returned does not belong to @dev -- it will be the input GPIO or
717  * IRQ of whichever device the board code has connected up to @dev's
718  * output GPIO.
719  *
720  * You probably don't need to use this function -- it is used only
721  * by the platform-bus subsystem.
722  *
723  * Return: qemu_irq associated with GPIO or NULL if un-wired.
724  */
725 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
726 
727 /**
728  * qdev_intercept_gpio_out: Intercept an existing GPIO connection
729  * @dev: Device to intercept the outbound GPIO line from
730  * @icpt: New qemu_irq to connect instead
731  * @name: Name of the output GPIO array
732  * @n: Number of the GPIO line in the array
733  *
734  * .. note::
735  *   This function is provided only for use by the qtest testing framework
736  *   and is not suitable for use in non-testing parts of QEMU.
737  *
738  * This function breaks an existing connection of an outbound GPIO
739  * line from @dev, and replaces it with the new qemu_irq @icpt, as if
740  * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
741  * The previously connected qemu_irq is returned, so it can be restored
742  * by a second call to qdev_intercept_gpio_out() if desired.
743  *
744  * Return: old disconnected qemu_irq if one existed
745  */
746 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
747                                  const char *name, int n);
748 
749 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
750 
751 /*** Device API.  ***/
752 
753 /**
754  * qdev_init_gpio_in: create an array of anonymous input GPIO lines
755  * @dev: Device to create input GPIOs for
756  * @handler: Function to call when GPIO line value is set
757  * @n: Number of GPIO lines to create
758  *
759  * Devices should use functions in the qdev_init_gpio_in* family in
760  * their instance_init or realize methods to create any input GPIO
761  * lines they need. There is no functional difference between
762  * anonymous and named GPIO lines. Stylistically, named GPIOs are
763  * preferable (easier to understand at callsites) unless a device
764  * has exactly one uniform kind of GPIO input whose purpose is obvious.
765  * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
766  *
767  * See qdev_get_gpio_in() for how code that uses such a device can get
768  * hold of an input GPIO line to manipulate it.
769  */
770 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
771 
772 /**
773  * qdev_init_gpio_out: create an array of anonymous output GPIO lines
774  * @dev: Device to create output GPIOs for
775  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
776  * @n: Number of GPIO lines to create
777  *
778  * Devices should use functions in the qdev_init_gpio_out* family
779  * in their instance_init or realize methods to create any output
780  * GPIO lines they need. There is no functional difference between
781  * anonymous and named GPIO lines. Stylistically, named GPIOs are
782  * preferable (easier to understand at callsites) unless a device
783  * has exactly one uniform kind of GPIO output whose purpose is obvious.
784  *
785  * The @pins argument should be a pointer to either a "qemu_irq"
786  * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
787  * state structure. The device implementation can then raise and
788  * lower the GPIO line by calling qemu_set_irq(). (If anything is
789  * connected to the other end of the GPIO this will cause the handler
790  * function for that input GPIO to be called.)
791  *
792  * See qdev_connect_gpio_out() for how code that uses such a device
793  * can connect to one of its output GPIO lines.
794  *
795  * There is no need to release the @pins allocated array because it
796  * will be automatically released when @dev calls its instance_finalize()
797  * handler.
798  */
799 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
800 
801 /**
802  * qdev_init_gpio_out_named: create an array of named output GPIO lines
803  * @dev: Device to create output GPIOs for
804  * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
805  * @name: Name to give this array of GPIO lines
806  * @n: Number of GPIO lines to create in this array
807  *
808  * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
809  * with a name. Code using the device can then connect these GPIO lines
810  * using qdev_connect_gpio_out_named().
811  */
812 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
813                               const char *name, int n);
814 
815 /**
816  * qdev_init_gpio_in_named_with_opaque() - create an array of input GPIO lines
817  * @dev: Device to create input GPIOs for
818  * @handler: Function to call when GPIO line value is set
819  * @opaque: Opaque data pointer to pass to @handler
820  * @name: Name of the GPIO input (must be unique for this device)
821  * @n: Number of GPIO lines in this input set
822  */
823 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
824                                          qemu_irq_handler handler,
825                                          void *opaque,
826                                          const char *name, int n);
827 
828 /**
829  * qdev_init_gpio_in_named() - create an array of input GPIO lines
830  * @dev: device to add array to
831  * @handler: a &typedef qemu_irq_handler function to call when GPIO is set
832  * @name: Name of the GPIO input (must be unique for this device)
833  * @n: Number of GPIO lines in this input set
834  *
835  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
836  * passed to the handler is @dev (which is the most commonly desired behaviour).
837  */
838 static inline void qdev_init_gpio_in_named(DeviceState *dev,
839                                            qemu_irq_handler handler,
840                                            const char *name, int n)
841 {
842     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
843 }
844 
845 /**
846  * qdev_pass_gpios: create GPIO lines on container which pass through to device
847  * @dev: Device which has GPIO lines
848  * @container: Container device which needs to expose them
849  * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
850  *
851  * In QEMU, complicated devices like SoCs are often modelled with a
852  * "container" QOM device which itself contains other QOM devices and
853  * which wires them up appropriately. This function allows the container
854  * to create GPIO arrays on itself which simply pass through to a GPIO
855  * array of one of its internal devices.
856  *
857  * If @dev has both input and output GPIOs named @name then both will
858  * be passed through. It is not possible to pass a subset of the array
859  * with this function.
860  *
861  * To users of the container device, the GPIO array created on @container
862  * behaves exactly like any other.
863  */
864 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
865                      const char *name);
866 
867 BusState *qdev_get_parent_bus(const DeviceState *dev);
868 
869 /*** BUS API. ***/
870 
871 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
872 
873 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
874 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
875 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
876 
877 void qbus_init(void *bus, size_t size, const char *typename,
878                DeviceState *parent, const char *name);
879 BusState *qbus_new(const char *typename, DeviceState *parent, const char *name);
880 bool qbus_realize(BusState *bus, Error **errp);
881 void qbus_unrealize(BusState *bus);
882 
883 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
884  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
885  *           0 otherwise. */
886 int qbus_walk_children(BusState *bus,
887                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
888                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
889                        void *opaque);
890 int qdev_walk_children(DeviceState *dev,
891                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
892                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
893                        void *opaque);
894 
895 /**
896  * device_cold_reset() - perform a recursive cold reset on a device
897  * @dev: device to reset.
898  *
899  * Reset device @dev and perform a recursive processing using the resettable
900  * interface. It triggers a RESET_TYPE_COLD.
901  */
902 void device_cold_reset(DeviceState *dev);
903 
904 /**
905  * bus_cold_reset() - perform a recursive cold reset on a bus
906  * @bus: bus to reset
907  *
908  * Reset bus @bus and perform a recursive processing using the resettable
909  * interface. It triggers a RESET_TYPE_COLD.
910  */
911 void bus_cold_reset(BusState *bus);
912 
913 /**
914  * device_is_in_reset() - check device reset state
915  * @dev: device to check
916  *
917  * Return: true if the device @dev is currently being reset.
918  */
919 bool device_is_in_reset(DeviceState *dev);
920 
921 /**
922  * bus_is_in_reset() - check bus reset state
923  * @bus: bus to check
924  *
925  * Return: true if the bus @bus is currently being reset.
926  */
927 bool bus_is_in_reset(BusState *bus);
928 
929 /* This should go away once we get rid of the NULL bus hack */
930 BusState *sysbus_get_default(void);
931 
932 char *qdev_get_fw_dev_path(DeviceState *dev);
933 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
934 
935 /**
936  * device_class_set_props(): add a set of properties to an device
937  * @dc: the parent DeviceClass all devices inherit
938  * @props: an array of properties, terminate by DEFINE_PROP_END_OF_LIST()
939  *
940  * This will add a set of properties to the object. It will fault if
941  * you attempt to add an existing property defined by a parent class.
942  * To modify an inherited property you need to use????
943  */
944 void device_class_set_props(DeviceClass *dc, const Property *props);
945 
946 /**
947  * device_class_set_parent_realize() - set up for chaining realize fns
948  * @dc: The device class
949  * @dev_realize: the device realize function
950  * @parent_realize: somewhere to save the parents realize function
951  *
952  * This is intended to be used when the new realize function will
953  * eventually call its parent realization function during creation.
954  * This requires storing the function call somewhere (usually in the
955  * instance structure) so you can eventually call
956  * dc->parent_realize(dev, errp)
957  */
958 void device_class_set_parent_realize(DeviceClass *dc,
959                                      DeviceRealize dev_realize,
960                                      DeviceRealize *parent_realize);
961 
962 /**
963  * device_class_set_legacy_reset(): set the DeviceClass::reset method
964  * @dc: The device class
965  * @dev_reset: the reset function
966  *
967  * This function sets the DeviceClass::reset method. This is widely
968  * used in existing code, but new code should prefer to use the
969  * Resettable API as documented in docs/devel/reset.rst.
970  * In addition, devices which need to chain to their parent class's
971  * reset methods or which need to be subclassed must use Resettable.
972  */
973 void device_class_set_legacy_reset(DeviceClass *dc,
974                                    DeviceReset dev_reset);
975 
976 /**
977  * device_class_set_parent_unrealize() - set up for chaining unrealize fns
978  * @dc: The device class
979  * @dev_unrealize: the device realize function
980  * @parent_unrealize: somewhere to save the parents unrealize function
981  *
982  * This is intended to be used when the new unrealize function will
983  * eventually call its parent unrealization function during the
984  * unrealize phase. This requires storing the function call somewhere
985  * (usually in the instance structure) so you can eventually call
986  * dc->parent_unrealize(dev);
987  */
988 void device_class_set_parent_unrealize(DeviceClass *dc,
989                                        DeviceUnrealize dev_unrealize,
990                                        DeviceUnrealize *parent_unrealize);
991 
992 const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
993 
994 const char *qdev_fw_name(DeviceState *dev);
995 
996 void qdev_assert_realized_properly(void);
997 Object *qdev_get_machine(void);
998 
999 /**
1000  * qdev_get_human_name() - Return a human-readable name for a device
1001  * @dev: The device. Must be a valid and non-NULL pointer.
1002  *
1003  * .. note::
1004  *    This function is intended for user friendly error messages.
1005  *
1006  * Returns: A newly allocated string containing the device id if not null,
1007  * else the object canonical path.
1008  *
1009  * Use g_free() to free it.
1010  */
1011 char *qdev_get_human_name(DeviceState *dev);
1012 
1013 /* FIXME: make this a link<> */
1014 bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
1015 
1016 extern bool qdev_hot_removed;
1017 
1018 char *qdev_get_dev_path(DeviceState *dev);
1019 
1020 void qbus_set_hotplug_handler(BusState *bus, Object *handler);
1021 void qbus_set_bus_hotplug_handler(BusState *bus);
1022 
1023 static inline bool qbus_is_hotpluggable(BusState *bus)
1024 {
1025     HotplugHandler *plug_handler = bus->hotplug_handler;
1026     bool ret = !!plug_handler;
1027 
1028     if (plug_handler) {
1029         HotplugHandlerClass *hdc;
1030 
1031         hdc = HOTPLUG_HANDLER_GET_CLASS(plug_handler);
1032         if (hdc->is_hotpluggable_bus) {
1033             ret = hdc->is_hotpluggable_bus(plug_handler, bus);
1034         }
1035     }
1036     return ret;
1037 }
1038 
1039 /**
1040  * qbus_mark_full: Mark this bus as full, so no more devices can be attached
1041  * @bus: Bus to mark as full
1042  *
1043  * By default, QEMU will allow devices to be plugged into a bus up
1044  * to the bus class's device count limit. Calling this function
1045  * marks a particular bus as full, so that no more devices can be
1046  * plugged into it. In particular this means that the bus will not
1047  * be considered as a candidate for plugging in devices created by
1048  * the user on the commandline or via the monitor.
1049  * If a machine has multiple buses of a given type, such as I2C,
1050  * where some of those buses in the real hardware are used only for
1051  * internal devices and some are exposed via expansion ports, you
1052  * can use this function to mark the internal-only buses as full
1053  * after you have created all their internal devices. Then user
1054  * created devices will appear on the expansion-port bus where
1055  * guest software expects them.
1056  */
1057 static inline void qbus_mark_full(BusState *bus)
1058 {
1059     bus->full = true;
1060 }
1061 
1062 void device_listener_register(DeviceListener *listener);
1063 void device_listener_unregister(DeviceListener *listener);
1064 
1065 /**
1066  * qdev_should_hide_device() - check if device should be hidden
1067  *
1068  * @opts: options QDict
1069  * @from_json: true if @opts entries are typed, false for all strings
1070  * @errp: pointer to error object
1071  *
1072  * When a device is added via qdev_device_add() this will be called.
1073  *
1074  * Return: if the device should be added now or not.
1075  */
1076 bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp);
1077 
1078 typedef enum MachineInitPhase {
1079     /* current_machine is NULL.  */
1080     PHASE_NO_MACHINE,
1081 
1082     /* current_machine is not NULL, but current_machine->accel is NULL.  */
1083     PHASE_MACHINE_CREATED,
1084 
1085     /*
1086      * current_machine->accel is not NULL, but the machine properties have
1087      * not been validated and machine_class->init has not yet been called.
1088      */
1089     PHASE_ACCEL_CREATED,
1090 
1091     /*
1092      * Late backend objects have been created and initialized.
1093      */
1094     PHASE_LATE_BACKENDS_CREATED,
1095 
1096     /*
1097      * machine_class->init has been called, thus creating any embedded
1098      * devices and validating machine properties.  Devices created at
1099      * this time are considered to be cold-plugged.
1100      */
1101     PHASE_MACHINE_INITIALIZED,
1102 
1103     /*
1104      * QEMU is ready to start CPUs and devices created at this time
1105      * are considered to be hot-plugged.  The monitor is not restricted
1106      * to "preconfig" commands.
1107      */
1108     PHASE_MACHINE_READY,
1109 } MachineInitPhase;
1110 
1111 bool phase_check(MachineInitPhase phase);
1112 void phase_advance(MachineInitPhase phase);
1113 
1114 #endif
1115