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