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