]> Git Repo - qemu.git/blame - include/hw/qdev-core.h
qdev/qbus: add hidden device support
[qemu.git] / include / hw / qdev-core.h
CommitLineData
074a86fc
AL
1#ifndef QDEV_CORE_H
2#define QDEV_CORE_H
3
1de7afc9 4#include "qemu/queue.h"
949fc823 5#include "qemu/bitmap.h"
14cccb61 6#include "qom/object.h"
0ee4de6c 7#include "hw/hotplug.h"
074a86fc 8
074a86fc
AL
9enum {
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
3d1237fb
MA
18typedef 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,
ba31cc72 27 DEVICE_CATEGORY_CPU,
3d1237fb
MA
28 DEVICE_CATEGORY_MAX
29} DeviceCategory;
30
249d4172
AF
31typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
32typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
b850f664 33typedef void (*DeviceReset)(DeviceState *dev);
02e7f85d
BD
34typedef void (*BusRealize)(BusState *bus, Error **errp);
35typedef void (*BusUnrealize)(BusState *bus, Error **errp);
074a86fc 36
249d4172
AF
37/**
38 * DeviceClass:
39 * @props: Properties accessing state fields.
40 * @realize: Callback function invoked when the #DeviceState:realized
ff46d9d4 41 * property is changed to %true.
249d4172
AF
42 * @unrealize: Callback function invoked when the #DeviceState:realized
43 * property is changed to %false.
1a37eca1
IM
44 * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
45 * as readonly "hotpluggable" property of #DeviceState instance
249d4172
AF
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.
6038f989
TH
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.
249d4172
AF
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 *
daeba969
MA
58 * As an interim step, the #DeviceState:realized property can also be
59 * set with qdev_init_nofail().
249d4172
AF
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 *
249d4172 68 * Any type may override the @realize and/or @unrealize callbacks but needs
782beb52
AF
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>
ff46d9d4
PMD
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.
782beb52
AF
77 * For other types consult the documentation and implementation of the
78 * respective parent types.
79 * </para>
80 * </note>
f3a85056
JF
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 *
249d4172 94 */
074a86fc 95typedef struct DeviceClass {
249d4172 96 /*< private >*/
074a86fc 97 ObjectClass parent_class;
249d4172 98 /*< public >*/
074a86fc 99
3d1237fb 100 DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
074a86fc
AL
101 const char *fw_name;
102 const char *desc;
103 Property *props;
efec3dd6
MA
104
105 /*
e90f2a8c 106 * Can this device be instantiated with -device / device_add?
efec3dd6
MA
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
e90f2a8c
EH
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.
efec3dd6
MA
114 * TODO remove once we're there
115 */
e90f2a8c 116 bool user_creatable;
1a37eca1 117 bool hotpluggable;
074a86fc
AL
118
119 /* callbacks */
b850f664 120 DeviceReset reset;
249d4172
AF
121 DeviceRealize realize;
122 DeviceUnrealize unrealize;
074a86fc
AL
123
124 /* device state */
8a9358cc 125 const VMStateDescription *vmsd;
074a86fc
AL
126
127 /* Private to qdev / bus. */
074a86fc
AL
128 const char *bus_type;
129} DeviceClass;
130
a5f54290
PC
131typedef struct NamedGPIOList NamedGPIOList;
132
133struct NamedGPIOList {
134 char *name;
135 qemu_irq *in;
136 int num_in;
a5f54290
PC
137 int num_out;
138 QLIST_ENTRY(NamedGPIOList) node;
139};
140
7983c8a3
AF
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 */
074a86fc 148struct DeviceState {
7983c8a3 149 /*< private >*/
074a86fc 150 Object parent_obj;
7983c8a3 151 /*< public >*/
074a86fc
AL
152
153 const char *id;
04162f8f 154 char *canonical_path;
7983c8a3 155 bool realized;
352e8da7 156 bool pending_deleted_event;
074a86fc
AL
157 QemuOpts *opts;
158 int hotplugged;
159 BusState *parent_bus;
a5f54290 160 QLIST_HEAD(, NamedGPIOList) gpios;
074a86fc
AL
161 QLIST_HEAD(, BusState) child_bus;
162 int num_child_bus;
163 int instance_id_alias;
164 int alias_required_for_version;
165};
166
707ff800
PD
167struct DeviceListener {
168 void (*realize)(DeviceListener *listener, DeviceState *dev);
169 void (*unrealize)(DeviceListener *listener, DeviceState *dev);
f3a85056
JF
170 /*
171 * This callback is called upon init of the DeviceState and allows to
172 * inform qdev that a device should be hidden, depending on the device
173 * opts, for example, to hide a standby device.
174 */
175 int (*should_be_hidden)(DeviceListener *listener, QemuOpts *device_opts);
707ff800
PD
176 QTAILQ_ENTRY(DeviceListener) link;
177};
178
074a86fc
AL
179#define TYPE_BUS "bus"
180#define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
181#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
182#define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
183
184struct BusClass {
185 ObjectClass parent_class;
186
187 /* FIXME first arg should be BusState */
188 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
189 char *(*get_dev_path)(DeviceState *dev);
190 /*
191 * This callback is used to create Open Firmware device path in accordance
192 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
193 * bindings can be found at http://playground.sun.com/1275/bindings/.
194 */
195 char *(*get_fw_dev_path)(DeviceState *dev);
dcc20931 196 void (*reset)(BusState *bus);
02e7f85d
BD
197 BusRealize realize;
198 BusUnrealize unrealize;
199
1395af6f
FK
200 /* maximum devices allowed on the bus, 0: no limit. */
201 int max_dev;
61de3676
AG
202 /* number of automatically allocated bus ids (e.g. ide.0) */
203 int automatic_ids;
074a86fc
AL
204};
205
206typedef struct BusChild {
207 DeviceState *child;
208 int index;
209 QTAILQ_ENTRY(BusChild) sibling;
210} BusChild;
211
0ee4de6c
IM
212#define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
213
074a86fc
AL
214/**
215 * BusState:
27c6ef1b 216 * @hotplug_handler: link to a hotplug handler associated with bus.
074a86fc
AL
217 */
218struct BusState {
219 Object obj;
220 DeviceState *parent;
f73480c3 221 char *name;
0ee4de6c 222 HotplugHandler *hotplug_handler;
074a86fc 223 int max_index;
02e7f85d 224 bool realized;
12b2e9f3 225 int num_children;
eae3eb3e 226 QTAILQ_HEAD(, BusChild) children;
074a86fc
AL
227 QLIST_ENTRY(BusState) sibling;
228};
229
5cc56cc6
PM
230/**
231 * Property:
232 * @set_default: true if the default value should be set from @defval,
233 * in which case @info->set_default_value must not be NULL
234 * (if false then no default value is set by the property system
235 * and the field retains whatever value it was given by instance_init).
236 * @defval: default value for the property. This is used only if @set_default
237 * is true.
238 */
074a86fc
AL
239struct Property {
240 const char *name;
1b6b7d10 241 const PropertyInfo *info;
3b6ca402 242 ptrdiff_t offset;
074a86fc 243 uint8_t bitnr;
5cc56cc6 244 bool set_default;
76318657
MAL
245 union {
246 int64_t i;
3fb2111f 247 uint64_t u;
76318657 248 } defval;
0be6bfac 249 int arrayoffset;
1b6b7d10 250 const PropertyInfo *arrayinfo;
0be6bfac 251 int arrayfieldsize;
5b4ff3c6 252 const char *link_type;
074a86fc
AL
253};
254
255struct PropertyInfo {
256 const char *name;
51b2e8c3 257 const char *description;
f7abe0ec 258 const QEnumLookup *enum_table;
074a86fc 259 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
a2740ad5 260 void (*set_default_value)(Object *obj, const Property *prop);
faabdbb7 261 void (*create)(Object *obj, Property *prop, Error **errp);
074a86fc
AL
262 ObjectPropertyAccessor *get;
263 ObjectPropertyAccessor *set;
264 ObjectPropertyRelease *release;
265};
266
9f9260a3
DS
267/**
268 * GlobalProperty:
b3ce84fe 269 * @used: Set to true if property was used when initializing a device.
92fd453c
DDAG
270 * @optional: If set to true, GlobalProperty will be skipped without errors
271 * if the property doesn't exist.
cff8b715
MAL
272 *
273 * An error is fatal for non-hotplugged devices, when the global is applied.
9f9260a3 274 */
074a86fc
AL
275typedef struct GlobalProperty {
276 const char *driver;
277 const char *property;
278 const char *value;
b3ce84fe 279 bool used;
92fd453c 280 bool optional;
074a86fc
AL
281} GlobalProperty;
282
ea9ce893
MAL
283static inline void
284compat_props_add(GPtrArray *arr,
285 GlobalProperty props[], size_t nelem)
286{
287 int i;
288 for (i = 0; i < nelem; i++) {
289 g_ptr_array_add(arr, (void *)&props[i]);
290 }
291}
292
074a86fc
AL
293/*** Board API. This should go away once we have a machine config file. ***/
294
295DeviceState *qdev_create(BusState *bus, const char *name);
296DeviceState *qdev_try_create(BusState *bus, const char *name);
074a86fc
AL
297void qdev_init_nofail(DeviceState *dev);
298void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
299 int required_for_version);
14405c27 300HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
03fcbd9d 301HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
d2321d31 302bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
17cc0128
IM
303/**
304 * qdev_get_hotplug_handler: Get handler responsible for device wiring
305 *
306 * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
307 *
308 * Note: in case @dev has a parent bus, it will be returned as handler unless
309 * machine handler overrides it.
310 *
311 * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
312 * or NULL if there aren't any.
313 */
c06b2ffb 314HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
074a86fc 315void qdev_unplug(DeviceState *dev, Error **errp);
014176f9
IM
316void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
317 DeviceState *dev, Error **errp);
074a86fc
AL
318void qdev_machine_creation_done(void);
319bool qdev_machine_modified(void);
320
321qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
a5f54290
PC
322qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
323
074a86fc 324void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
a5f54290
PC
325void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
326 qemu_irq pin);
b7973186 327qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
0c24db2b
PC
328qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
329 const char *name, int n);
074a86fc
AL
330
331BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
332
333/*** Device API. ***/
334
335/* Register device properties. */
336/* GPIO inputs also double as IRQ sinks. */
337void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
338void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
a5f54290
PC
339void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
340 const char *name, int n);
4a151677
PM
341/**
342 * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
343 * for the specified device
344 *
345 * @dev: Device to create input GPIOs for
346 * @handler: Function to call when GPIO line value is set
347 * @opaque: Opaque data pointer to pass to @handler
348 * @name: Name of the GPIO input (must be unique for this device)
349 * @n: Number of GPIO lines in this input set
350 */
351void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
352 qemu_irq_handler handler,
353 void *opaque,
354 const char *name, int n);
355
356/**
357 * qdev_init_gpio_in_named: create an array of input GPIO lines
358 * for the specified device
359 *
360 * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
361 * passed to the handler is @dev (which is the most commonly desired behaviour).
362 */
363static inline void qdev_init_gpio_in_named(DeviceState *dev,
364 qemu_irq_handler handler,
365 const char *name, int n)
366{
367 qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
368}
074a86fc 369
17a96a14
PC
370void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
371 const char *name);
372
074a86fc
AL
373BusState *qdev_get_parent_bus(DeviceState *dev);
374
375/*** BUS API. ***/
376
377DeviceState *qdev_find_recursive(BusState *bus, const char *id);
378
379/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
380typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
381typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
382
fb17dfe0 383void qbus_create_inplace(void *bus, size_t size, const char *typename,
074a86fc
AL
384 DeviceState *parent, const char *name);
385BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
386/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
387 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
388 * 0 otherwise. */
0293214b
PB
389int qbus_walk_children(BusState *bus,
390 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
391 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
392 void *opaque);
393int qdev_walk_children(DeviceState *dev,
394 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
395 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
396 void *opaque);
397
074a86fc 398void qdev_reset_all(DeviceState *dev);
ff8de075 399void qdev_reset_all_fn(void *opaque);
d0508c36
PB
400
401/**
402 * @qbus_reset_all:
403 * @bus: Bus to be reset.
404 *
405 * Reset @bus and perform a bus-level ("hard") reset of all devices connected
406 * to it, including recursive processing of all buses below @bus itself. A
407 * hard reset means that qbus_reset_all will reset all state of the device.
408 * For PCI devices, for example, this will include the base address registers
409 * or configuration space.
410 */
411void qbus_reset_all(BusState *bus);
074a86fc
AL
412void qbus_reset_all_fn(void *opaque);
413
074a86fc
AL
414/* This should go away once we get rid of the NULL bus hack */
415BusState *sysbus_get_default(void);
416
417char *qdev_get_fw_dev_path(DeviceState *dev);
0be63901 418char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
074a86fc
AL
419
420/**
421 * @qdev_machine_init
422 *
423 * Initialize platform devices before machine init. This is a hack until full
424 * support for composition is added.
425 */
426void qdev_machine_init(void);
427
428/**
429 * @device_reset
430 *
431 * Reset a single device (by calling the reset method).
432 */
433void device_reset(DeviceState *dev);
434
46795cf2
PMD
435void device_class_set_parent_reset(DeviceClass *dc,
436 DeviceReset dev_reset,
437 DeviceReset *parent_reset);
438void device_class_set_parent_realize(DeviceClass *dc,
439 DeviceRealize dev_realize,
440 DeviceRealize *parent_realize);
441void device_class_set_parent_unrealize(DeviceClass *dc,
442 DeviceUnrealize dev_unrealize,
443 DeviceUnrealize *parent_unrealize);
444
8a9358cc 445const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
074a86fc
AL
446
447const char *qdev_fw_name(DeviceState *dev);
448
449Object *qdev_get_machine(void);
450
451/* FIXME: make this a link<> */
452void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
453
9bed84c1 454extern bool qdev_hotplug;
21def24a 455extern bool qdev_hot_removed;
074a86fc
AL
456
457char *qdev_get_dev_path(DeviceState *dev);
458
4cae4d5a 459GSList *qdev_build_hotpluggable_device_list(Object *peripheral);
66e56b13 460
94d1cc5f 461void qbus_set_hotplug_handler(BusState *bus, Object *handler, Error **errp);
431bbb26
IM
462
463void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp);
39b888bd
IM
464
465static inline bool qbus_is_hotpluggable(BusState *bus)
466{
2d9a982f 467 return bus->hotplug_handler;
39b888bd 468}
707ff800
PD
469
470void device_listener_register(DeviceListener *listener);
471void device_listener_unregister(DeviceListener *listener);
472
f3a85056
JF
473/**
474 * @qdev_should_hide_device:
475 * @opts: QemuOpts as passed on cmdline.
476 *
477 * Check if a device should be added.
478 * When a device is added via qdev_device_add() this will be called,
479 * and return if the device should be added now or not.
480 */
481bool qdev_should_hide_device(QemuOpts *opts);
482
074a86fc 483#endif
This page took 0.437392 seconds and 4 git commands to generate.