xref: /qemu/include/hw/qdev-core.h (revision 6c5627bb)
1 #ifndef QDEV_CORE_H
2 #define QDEV_CORE_H
3 
4 #include "qemu/queue.h"
5 #include "qemu/bitmap.h"
6 #include "qom/object.h"
7 #include "hw/hotplug.h"
8 #include "hw/resettable.h"
9 
10 enum {
11     DEV_NVECTORS_UNSPECIFIED = -1,
12 };
13 
14 #define TYPE_DEVICE "device"
15 #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
16 #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
17 #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
18 
19 typedef enum DeviceCategory {
20     DEVICE_CATEGORY_BRIDGE,
21     DEVICE_CATEGORY_USB,
22     DEVICE_CATEGORY_STORAGE,
23     DEVICE_CATEGORY_NETWORK,
24     DEVICE_CATEGORY_INPUT,
25     DEVICE_CATEGORY_DISPLAY,
26     DEVICE_CATEGORY_SOUND,
27     DEVICE_CATEGORY_MISC,
28     DEVICE_CATEGORY_CPU,
29     DEVICE_CATEGORY_MAX
30 } DeviceCategory;
31 
32 typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
33 typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
34 typedef void (*DeviceReset)(DeviceState *dev);
35 typedef void (*BusRealize)(BusState *bus, Error **errp);
36 typedef void (*BusUnrealize)(BusState *bus, Error **errp);
37 
38 /**
39  * DeviceClass:
40  * @props: Properties accessing state fields.
41  * @realize: Callback function invoked when the #DeviceState:realized
42  * property is changed to %true.
43  * @unrealize: Callback function invoked when the #DeviceState:realized
44  * property is changed to %false.
45  * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
46  * as readonly "hotpluggable" property of #DeviceState instance
47  *
48  * # Realization #
49  * Devices are constructed in two stages,
50  * 1) object instantiation via object_initialize() and
51  * 2) device realization via #DeviceState:realized property.
52  * The former may not fail (and must not abort or exit, since it is called
53  * during device introspection already), and the latter may return error
54  * information to the caller and must be re-entrant.
55  * Trivial field initializations should go into #TypeInfo.instance_init.
56  * Operations depending on @props static properties should go into @realize.
57  * After successful realization, setting static properties will fail.
58  *
59  * As an interim step, the #DeviceState:realized property can also be
60  * set with qdev_init_nofail().
61  * In the future, devices will propagate this state change to their children
62  * and along busses they expose.
63  * The point in time will be deferred to machine creation, so that values
64  * set in @realize will not be introspectable beforehand. Therefore devices
65  * must not create children during @realize; they should initialize them via
66  * object_initialize() in their own #TypeInfo.instance_init and forward the
67  * realization events appropriately.
68  *
69  * Any type may override the @realize and/or @unrealize callbacks but needs
70  * to call the parent type's implementation if keeping their functionality
71  * is desired. Refer to QOM documentation for further discussion and examples.
72  *
73  * <note>
74  *   <para>
75  * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
76  * derived directly from it need not call their parent's @realize and
77  * @unrealize.
78  * For other types consult the documentation and implementation of the
79  * respective parent types.
80  *   </para>
81  * </note>
82  *
83  * # Hiding a device #
84  * To hide a device, a DeviceListener function should_be_hidden() needs to
85  * be registered.
86  * It can be used to defer adding a device and therefore hide it from the
87  * guest. The handler registering to this DeviceListener can save the QOpts
88  * passed to it for re-using it later and must return that it wants the device
89  * to be/remain hidden or not. When the handler function decides the device
90  * shall not be hidden it will be added in qdev_device_add() and
91  * realized as any other device. Otherwise qdev_device_add() will return early
92  * without adding the device. The guest will not see a "hidden" device
93  * until it was marked don't hide and qdev_device_add called again.
94  *
95  */
96 typedef struct DeviceClass {
97     /*< private >*/
98     ObjectClass parent_class;
99     /*< public >*/
100 
101     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
102     const char *fw_name;
103     const char *desc;
104 
105     /*
106      * The underscore at the end ensures a compile-time error if someone
107      * assigns to dc->props instead of using device_class_set_props.
108      */
109     Property *props_;
110 
111     /*
112      * Can this device be instantiated with -device / device_add?
113      * All devices should support instantiation with device_add, and
114      * this flag should not exist.  But we're not there, yet.  Some
115      * devices fail to instantiate with cryptic error messages.
116      * Others instantiate, but don't work.  Exposing users to such
117      * behavior would be cruel; clearing this flag will protect them.
118      * It should never be cleared without a comment explaining why it
119      * is cleared.
120      * TODO remove once we're there
121      */
122     bool user_creatable;
123     bool hotpluggable;
124 
125     /* callbacks */
126     /*
127      * Reset method here is deprecated and replaced by methods in the
128      * resettable class interface to implement a multi-phase reset.
129      * TODO: remove once every reset callback is unused
130      */
131     DeviceReset reset;
132     DeviceRealize realize;
133     DeviceUnrealize unrealize;
134 
135     /* device state */
136     const VMStateDescription *vmsd;
137 
138     /* Private to qdev / bus.  */
139     const char *bus_type;
140 } DeviceClass;
141 
142 typedef struct NamedGPIOList NamedGPIOList;
143 
144 struct NamedGPIOList {
145     char *name;
146     qemu_irq *in;
147     int num_in;
148     int num_out;
149     QLIST_ENTRY(NamedGPIOList) node;
150 };
151 
152 /**
153  * DeviceState:
154  * @realized: Indicates whether the device has been fully constructed.
155  * @reset: ResettableState for the device; handled by Resettable interface.
156  *
157  * This structure should not be accessed directly.  We declare it here
158  * so that it can be embedded in individual device state structures.
159  */
160 struct DeviceState {
161     /*< private >*/
162     Object parent_obj;
163     /*< public >*/
164 
165     const char *id;
166     char *canonical_path;
167     bool realized;
168     bool pending_deleted_event;
169     QemuOpts *opts;
170     int hotplugged;
171     bool allow_unplug_during_migration;
172     BusState *parent_bus;
173     QLIST_HEAD(, NamedGPIOList) gpios;
174     QLIST_HEAD(, BusState) child_bus;
175     int num_child_bus;
176     int instance_id_alias;
177     int alias_required_for_version;
178     ResettableState reset;
179 };
180 
181 struct DeviceListener {
182     void (*realize)(DeviceListener *listener, DeviceState *dev);
183     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
184     /*
185      * This callback is called upon init of the DeviceState and allows to
186      * inform qdev that a device should be hidden, depending on the device
187      * opts, for example, to hide a standby device.
188      */
189     int (*should_be_hidden)(DeviceListener *listener, QemuOpts *device_opts);
190     QTAILQ_ENTRY(DeviceListener) link;
191 };
192 
193 #define TYPE_BUS "bus"
194 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
195 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
196 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
197 
198 struct BusClass {
199     ObjectClass parent_class;
200 
201     /* FIXME first arg should be BusState */
202     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
203     char *(*get_dev_path)(DeviceState *dev);
204     /*
205      * This callback is used to create Open Firmware device path in accordance
206      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
207      * bindings can be found at http://playground.sun.com/1275/bindings/.
208      */
209     char *(*get_fw_dev_path)(DeviceState *dev);
210     void (*reset)(BusState *bus);
211     BusRealize realize;
212     BusUnrealize unrealize;
213 
214     /* maximum devices allowed on the bus, 0: no limit. */
215     int max_dev;
216     /* number of automatically allocated bus ids (e.g. ide.0) */
217     int automatic_ids;
218 };
219 
220 typedef struct BusChild {
221     DeviceState *child;
222     int index;
223     QTAILQ_ENTRY(BusChild) sibling;
224 } BusChild;
225 
226 #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
227 
228 /**
229  * BusState:
230  * @hotplug_handler: link to a hotplug handler associated with bus.
231  * @reset: ResettableState for the bus; handled by Resettable interface.
232  */
233 struct BusState {
234     Object obj;
235     DeviceState *parent;
236     char *name;
237     HotplugHandler *hotplug_handler;
238     int max_index;
239     bool realized;
240     int num_children;
241     QTAILQ_HEAD(, BusChild) children;
242     QLIST_ENTRY(BusState) sibling;
243     ResettableState reset;
244 };
245 
246 /**
247  * Property:
248  * @set_default: true if the default value should be set from @defval,
249  *    in which case @info->set_default_value must not be NULL
250  *    (if false then no default value is set by the property system
251  *     and the field retains whatever value it was given by instance_init).
252  * @defval: default value for the property. This is used only if @set_default
253  *     is true.
254  */
255 struct Property {
256     const char   *name;
257     const PropertyInfo *info;
258     ptrdiff_t    offset;
259     uint8_t      bitnr;
260     bool         set_default;
261     union {
262         int64_t i;
263         uint64_t u;
264     } defval;
265     int          arrayoffset;
266     const PropertyInfo *arrayinfo;
267     int          arrayfieldsize;
268     const char   *link_type;
269 };
270 
271 struct PropertyInfo {
272     const char *name;
273     const char *description;
274     const QEnumLookup *enum_table;
275     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
276     void (*set_default_value)(ObjectProperty *op, const Property *prop);
277     void (*create)(ObjectClass *oc, Property *prop, Error **errp);
278     ObjectPropertyAccessor *get;
279     ObjectPropertyAccessor *set;
280     ObjectPropertyRelease *release;
281 };
282 
283 /**
284  * GlobalProperty:
285  * @used: Set to true if property was used when initializing a device.
286  * @optional: If set to true, GlobalProperty will be skipped without errors
287  *            if the property doesn't exist.
288  *
289  * An error is fatal for non-hotplugged devices, when the global is applied.
290  */
291 typedef struct GlobalProperty {
292     const char *driver;
293     const char *property;
294     const char *value;
295     bool used;
296     bool optional;
297 } GlobalProperty;
298 
299 static inline void
300 compat_props_add(GPtrArray *arr,
301                  GlobalProperty props[], size_t nelem)
302 {
303     int i;
304     for (i = 0; i < nelem; i++) {
305         g_ptr_array_add(arr, (void *)&props[i]);
306     }
307 }
308 
309 /*** Board API.  This should go away once we have a machine config file.  ***/
310 
311 DeviceState *qdev_create(BusState *bus, const char *name);
312 DeviceState *qdev_try_create(BusState *bus, const char *name);
313 void qdev_init_nofail(DeviceState *dev);
314 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
315                                  int required_for_version);
316 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
317 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
318 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
319 /**
320  * qdev_get_hotplug_handler: Get handler responsible for device wiring
321  *
322  * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
323  *
324  * Note: in case @dev has a parent bus, it will be returned as handler unless
325  * machine handler overrides it.
326  *
327  * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
328  *          or NULL if there aren't any.
329  */
330 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
331 void qdev_unplug(DeviceState *dev, Error **errp);
332 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
333                                   DeviceState *dev, Error **errp);
334 void qdev_machine_creation_done(void);
335 bool qdev_machine_modified(void);
336 
337 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
338 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
339 
340 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
341 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
342                                  qemu_irq pin);
343 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
344 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
345                                  const char *name, int n);
346 
347 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
348 
349 /*** Device API.  ***/
350 
351 /* Register device properties.  */
352 /* GPIO inputs also double as IRQ sinks.  */
353 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
354 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
355 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
356                               const char *name, int n);
357 /**
358  * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
359  *   for the specified device
360  *
361  * @dev: Device to create input GPIOs for
362  * @handler: Function to call when GPIO line value is set
363  * @opaque: Opaque data pointer to pass to @handler
364  * @name: Name of the GPIO input (must be unique for this device)
365  * @n: Number of GPIO lines in this input set
366  */
367 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
368                                          qemu_irq_handler handler,
369                                          void *opaque,
370                                          const char *name, int n);
371 
372 /**
373  * qdev_init_gpio_in_named: create an array of input GPIO lines
374  *   for the specified device
375  *
376  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
377  * passed to the handler is @dev (which is the most commonly desired behaviour).
378  */
379 static inline void qdev_init_gpio_in_named(DeviceState *dev,
380                                            qemu_irq_handler handler,
381                                            const char *name, int n)
382 {
383     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
384 }
385 
386 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
387                      const char *name);
388 
389 BusState *qdev_get_parent_bus(DeviceState *dev);
390 
391 /*** BUS API. ***/
392 
393 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
394 
395 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
396 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
397 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
398 
399 void qbus_create_inplace(void *bus, size_t size, const char *typename,
400                          DeviceState *parent, const char *name);
401 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
402 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
403  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
404  *           0 otherwise. */
405 int qbus_walk_children(BusState *bus,
406                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
407                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
408                        void *opaque);
409 int qdev_walk_children(DeviceState *dev,
410                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
411                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
412                        void *opaque);
413 
414 /**
415  * @qdev_reset_all:
416  * Reset @dev. See @qbus_reset_all() for more details.
417  *
418  * Note: This function is deprecated and will be removed when it becomes unused.
419  * Please use device_cold_reset() now.
420  */
421 void qdev_reset_all(DeviceState *dev);
422 void qdev_reset_all_fn(void *opaque);
423 
424 /**
425  * @qbus_reset_all:
426  * @bus: Bus to be reset.
427  *
428  * Reset @bus and perform a bus-level ("hard") reset of all devices connected
429  * to it, including recursive processing of all buses below @bus itself.  A
430  * hard reset means that qbus_reset_all will reset all state of the device.
431  * For PCI devices, for example, this will include the base address registers
432  * or configuration space.
433  *
434  * Note: This function is deprecated and will be removed when it becomes unused.
435  * Please use bus_cold_reset() now.
436  */
437 void qbus_reset_all(BusState *bus);
438 void qbus_reset_all_fn(void *opaque);
439 
440 /**
441  * device_cold_reset:
442  * Reset device @dev and perform a recursive processing using the resettable
443  * interface. It triggers a RESET_TYPE_COLD.
444  */
445 void device_cold_reset(DeviceState *dev);
446 
447 /**
448  * bus_cold_reset:
449  *
450  * Reset bus @bus and perform a recursive processing using the resettable
451  * interface. It triggers a RESET_TYPE_COLD.
452  */
453 void bus_cold_reset(BusState *bus);
454 
455 /**
456  * device_is_in_reset:
457  * Return true if the device @dev is currently being reset.
458  */
459 bool device_is_in_reset(DeviceState *dev);
460 
461 /**
462  * bus_is_in_reset:
463  * Return true if the bus @bus is currently being reset.
464  */
465 bool bus_is_in_reset(BusState *bus);
466 
467 /* This should go away once we get rid of the NULL bus hack */
468 BusState *sysbus_get_default(void);
469 
470 char *qdev_get_fw_dev_path(DeviceState *dev);
471 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
472 
473 /**
474  * @qdev_machine_init
475  *
476  * Initialize platform devices before machine init.  This is a hack until full
477  * support for composition is added.
478  */
479 void qdev_machine_init(void);
480 
481 /**
482  * device_legacy_reset:
483  *
484  * Reset a single device (by calling the reset method).
485  * Note: This function is deprecated and will be removed when it becomes unused.
486  * Please use device_cold_reset() now.
487  */
488 void device_legacy_reset(DeviceState *dev);
489 
490 void device_class_set_props(DeviceClass *dc, Property *props);
491 
492 /**
493  * device_class_set_parent_reset:
494  * TODO: remove the function when DeviceClass's reset method
495  * is not used anymore.
496  */
497 void device_class_set_parent_reset(DeviceClass *dc,
498                                    DeviceReset dev_reset,
499                                    DeviceReset *parent_reset);
500 void device_class_set_parent_realize(DeviceClass *dc,
501                                      DeviceRealize dev_realize,
502                                      DeviceRealize *parent_realize);
503 void device_class_set_parent_unrealize(DeviceClass *dc,
504                                        DeviceUnrealize dev_unrealize,
505                                        DeviceUnrealize *parent_unrealize);
506 
507 const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
508 
509 const char *qdev_fw_name(DeviceState *dev);
510 
511 Object *qdev_get_machine(void);
512 
513 /* FIXME: make this a link<> */
514 void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
515 
516 extern bool qdev_hotplug;
517 extern bool qdev_hot_removed;
518 
519 char *qdev_get_dev_path(DeviceState *dev);
520 
521 void qbus_set_hotplug_handler(BusState *bus, Object *handler, Error **errp);
522 
523 void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp);
524 
525 static inline bool qbus_is_hotpluggable(BusState *bus)
526 {
527    return bus->hotplug_handler;
528 }
529 
530 void device_listener_register(DeviceListener *listener);
531 void device_listener_unregister(DeviceListener *listener);
532 
533 /**
534  * @qdev_should_hide_device:
535  * @opts: QemuOpts as passed on cmdline.
536  *
537  * Check if a device should be added.
538  * When a device is added via qdev_device_add() this will be called,
539  * and return if the device should be added now or not.
540  */
541 bool qdev_should_hide_device(QemuOpts *opts);
542 
543 #endif
544