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