xref: /openbmc/qemu/include/hw/qdev-core.h (revision 99029be1)
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 
104     /*
105      * The underscore at the end ensures a compile-time error if someone
106      * assigns to dc->props instead of using device_class_set_props.
107      */
108     Property *props_;
109 
110     /*
111      * Can this device be instantiated with -device / device_add?
112      * All devices should support instantiation with device_add, and
113      * this flag should not exist.  But we're not there, yet.  Some
114      * devices fail to instantiate with cryptic error messages.
115      * Others instantiate, but don't work.  Exposing users to such
116      * behavior would be cruel; clearing this flag will protect them.
117      * It should never be cleared without a comment explaining why it
118      * is cleared.
119      * TODO remove once we're there
120      */
121     bool user_creatable;
122     bool hotpluggable;
123 
124     /* callbacks */
125     DeviceReset reset;
126     DeviceRealize realize;
127     DeviceUnrealize unrealize;
128 
129     /* device state */
130     const VMStateDescription *vmsd;
131 
132     /* Private to qdev / bus.  */
133     const char *bus_type;
134 } DeviceClass;
135 
136 typedef struct NamedGPIOList NamedGPIOList;
137 
138 struct NamedGPIOList {
139     char *name;
140     qemu_irq *in;
141     int num_in;
142     int num_out;
143     QLIST_ENTRY(NamedGPIOList) node;
144 };
145 
146 /**
147  * DeviceState:
148  * @realized: Indicates whether the device has been fully constructed.
149  *
150  * This structure should not be accessed directly.  We declare it here
151  * so that it can be embedded in individual device state structures.
152  */
153 struct DeviceState {
154     /*< private >*/
155     Object parent_obj;
156     /*< public >*/
157 
158     const char *id;
159     char *canonical_path;
160     bool realized;
161     bool pending_deleted_event;
162     QemuOpts *opts;
163     int hotplugged;
164     bool allow_unplug_during_migration;
165     BusState *parent_bus;
166     QLIST_HEAD(, NamedGPIOList) gpios;
167     QLIST_HEAD(, BusState) child_bus;
168     int num_child_bus;
169     int instance_id_alias;
170     int alias_required_for_version;
171 };
172 
173 struct DeviceListener {
174     void (*realize)(DeviceListener *listener, DeviceState *dev);
175     void (*unrealize)(DeviceListener *listener, DeviceState *dev);
176     /*
177      * This callback is called upon init of the DeviceState and allows to
178      * inform qdev that a device should be hidden, depending on the device
179      * opts, for example, to hide a standby device.
180      */
181     int (*should_be_hidden)(DeviceListener *listener, QemuOpts *device_opts);
182     QTAILQ_ENTRY(DeviceListener) link;
183 };
184 
185 #define TYPE_BUS "bus"
186 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
187 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
188 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
189 
190 struct BusClass {
191     ObjectClass parent_class;
192 
193     /* FIXME first arg should be BusState */
194     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
195     char *(*get_dev_path)(DeviceState *dev);
196     /*
197      * This callback is used to create Open Firmware device path in accordance
198      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
199      * bindings can be found at http://playground.sun.com/1275/bindings/.
200      */
201     char *(*get_fw_dev_path)(DeviceState *dev);
202     void (*reset)(BusState *bus);
203     BusRealize realize;
204     BusUnrealize unrealize;
205 
206     /* maximum devices allowed on the bus, 0: no limit. */
207     int max_dev;
208     /* number of automatically allocated bus ids (e.g. ide.0) */
209     int automatic_ids;
210 };
211 
212 typedef struct BusChild {
213     DeviceState *child;
214     int index;
215     QTAILQ_ENTRY(BusChild) sibling;
216 } BusChild;
217 
218 #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
219 
220 /**
221  * BusState:
222  * @hotplug_handler: link to a hotplug handler associated with bus.
223  */
224 struct BusState {
225     Object obj;
226     DeviceState *parent;
227     char *name;
228     HotplugHandler *hotplug_handler;
229     int max_index;
230     bool realized;
231     int num_children;
232     QTAILQ_HEAD(, BusChild) children;
233     QLIST_ENTRY(BusState) sibling;
234 };
235 
236 /**
237  * Property:
238  * @set_default: true if the default value should be set from @defval,
239  *    in which case @info->set_default_value must not be NULL
240  *    (if false then no default value is set by the property system
241  *     and the field retains whatever value it was given by instance_init).
242  * @defval: default value for the property. This is used only if @set_default
243  *     is true.
244  */
245 struct Property {
246     const char   *name;
247     const PropertyInfo *info;
248     ptrdiff_t    offset;
249     uint8_t      bitnr;
250     bool         set_default;
251     union {
252         int64_t i;
253         uint64_t u;
254     } defval;
255     int          arrayoffset;
256     const PropertyInfo *arrayinfo;
257     int          arrayfieldsize;
258     const char   *link_type;
259 };
260 
261 struct PropertyInfo {
262     const char *name;
263     const char *description;
264     const QEnumLookup *enum_table;
265     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
266     void (*set_default_value)(ObjectProperty *op, const Property *prop);
267     void (*create)(ObjectClass *oc, Property *prop, Error **errp);
268     ObjectPropertyAccessor *get;
269     ObjectPropertyAccessor *set;
270     ObjectPropertyRelease *release;
271 };
272 
273 /**
274  * GlobalProperty:
275  * @used: Set to true if property was used when initializing a device.
276  * @optional: If set to true, GlobalProperty will be skipped without errors
277  *            if the property doesn't exist.
278  *
279  * An error is fatal for non-hotplugged devices, when the global is applied.
280  */
281 typedef struct GlobalProperty {
282     const char *driver;
283     const char *property;
284     const char *value;
285     bool used;
286     bool optional;
287 } GlobalProperty;
288 
289 static inline void
290 compat_props_add(GPtrArray *arr,
291                  GlobalProperty props[], size_t nelem)
292 {
293     int i;
294     for (i = 0; i < nelem; i++) {
295         g_ptr_array_add(arr, (void *)&props[i]);
296     }
297 }
298 
299 /*** Board API.  This should go away once we have a machine config file.  ***/
300 
301 DeviceState *qdev_create(BusState *bus, const char *name);
302 DeviceState *qdev_try_create(BusState *bus, const char *name);
303 void qdev_init_nofail(DeviceState *dev);
304 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
305                                  int required_for_version);
306 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
307 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
308 bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
309 /**
310  * qdev_get_hotplug_handler: Get handler responsible for device wiring
311  *
312  * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
313  *
314  * Note: in case @dev has a parent bus, it will be returned as handler unless
315  * machine handler overrides it.
316  *
317  * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
318  *          or NULL if there aren't any.
319  */
320 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
321 void qdev_unplug(DeviceState *dev, Error **errp);
322 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
323                                   DeviceState *dev, Error **errp);
324 void qdev_machine_creation_done(void);
325 bool qdev_machine_modified(void);
326 
327 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
328 qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
329 
330 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
331 void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
332                                  qemu_irq pin);
333 qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
334 qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
335                                  const char *name, int n);
336 
337 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
338 
339 /*** Device API.  ***/
340 
341 /* Register device properties.  */
342 /* GPIO inputs also double as IRQ sinks.  */
343 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
344 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
345 void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
346                               const char *name, int n);
347 /**
348  * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
349  *   for the specified device
350  *
351  * @dev: Device to create input GPIOs for
352  * @handler: Function to call when GPIO line value is set
353  * @opaque: Opaque data pointer to pass to @handler
354  * @name: Name of the GPIO input (must be unique for this device)
355  * @n: Number of GPIO lines in this input set
356  */
357 void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
358                                          qemu_irq_handler handler,
359                                          void *opaque,
360                                          const char *name, int n);
361 
362 /**
363  * qdev_init_gpio_in_named: create an array of input GPIO lines
364  *   for the specified device
365  *
366  * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
367  * passed to the handler is @dev (which is the most commonly desired behaviour).
368  */
369 static inline void qdev_init_gpio_in_named(DeviceState *dev,
370                                            qemu_irq_handler handler,
371                                            const char *name, int n)
372 {
373     qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
374 }
375 
376 void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
377                      const char *name);
378 
379 BusState *qdev_get_parent_bus(DeviceState *dev);
380 
381 /*** BUS API. ***/
382 
383 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
384 
385 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
386 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
387 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
388 
389 void qbus_create_inplace(void *bus, size_t size, const char *typename,
390                          DeviceState *parent, const char *name);
391 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
392 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
393  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
394  *           0 otherwise. */
395 int qbus_walk_children(BusState *bus,
396                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
397                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
398                        void *opaque);
399 int qdev_walk_children(DeviceState *dev,
400                        qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
401                        qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
402                        void *opaque);
403 
404 void qdev_reset_all(DeviceState *dev);
405 void qdev_reset_all_fn(void *opaque);
406 
407 /**
408  * @qbus_reset_all:
409  * @bus: Bus to be reset.
410  *
411  * Reset @bus and perform a bus-level ("hard") reset of all devices connected
412  * to it, including recursive processing of all buses below @bus itself.  A
413  * hard reset means that qbus_reset_all will reset all state of the device.
414  * For PCI devices, for example, this will include the base address registers
415  * or configuration space.
416  */
417 void qbus_reset_all(BusState *bus);
418 void qbus_reset_all_fn(void *opaque);
419 
420 /* This should go away once we get rid of the NULL bus hack */
421 BusState *sysbus_get_default(void);
422 
423 char *qdev_get_fw_dev_path(DeviceState *dev);
424 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
425 
426 /**
427  * @qdev_machine_init
428  *
429  * Initialize platform devices before machine init.  This is a hack until full
430  * support for composition is added.
431  */
432 void qdev_machine_init(void);
433 
434 /**
435  * @device_reset
436  *
437  * Reset a single device (by calling the reset method).
438  */
439 void device_reset(DeviceState *dev);
440 
441 void device_class_set_props(DeviceClass *dc, Property *props);
442 
443 void device_class_set_parent_reset(DeviceClass *dc,
444                                    DeviceReset dev_reset,
445                                    DeviceReset *parent_reset);
446 void device_class_set_parent_realize(DeviceClass *dc,
447                                      DeviceRealize dev_realize,
448                                      DeviceRealize *parent_realize);
449 void device_class_set_parent_unrealize(DeviceClass *dc,
450                                        DeviceUnrealize dev_unrealize,
451                                        DeviceUnrealize *parent_unrealize);
452 
453 const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
454 
455 const char *qdev_fw_name(DeviceState *dev);
456 
457 Object *qdev_get_machine(void);
458 
459 /* FIXME: make this a link<> */
460 void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
461 
462 extern bool qdev_hotplug;
463 extern bool qdev_hot_removed;
464 
465 char *qdev_get_dev_path(DeviceState *dev);
466 
467 void qbus_set_hotplug_handler(BusState *bus, Object *handler, Error **errp);
468 
469 void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp);
470 
471 static inline bool qbus_is_hotpluggable(BusState *bus)
472 {
473    return bus->hotplug_handler;
474 }
475 
476 void device_listener_register(DeviceListener *listener);
477 void device_listener_unregister(DeviceListener *listener);
478 
479 /**
480  * @qdev_should_hide_device:
481  * @opts: QemuOpts as passed on cmdline.
482  *
483  * Check if a device should be added.
484  * When a device is added via qdev_device_add() this will be called,
485  * and return if the device should be added now or not.
486  */
487 bool qdev_should_hide_device(QemuOpts *opts);
488 
489 #endif
490