]> git.proxmox.com Git - mirror_qemu.git/blame - include/hw/qdev-core.h
ipmi: free extern timer
[mirror_qemu.git] / include / hw / qdev-core.h
CommitLineData
074a86fc
AL
1#ifndef QDEV_CORE_H
2#define QDEV_CORE_H
3
1de7afc9
PB
4#include "qemu/queue.h"
5#include "qemu/option.h"
949fc823 6#include "qemu/bitmap.h"
14cccb61 7#include "qom/object.h"
074a86fc 8#include "hw/irq.h"
0ee4de6c 9#include "hw/hotplug.h"
074a86fc 10
074a86fc
AL
11enum {
12 DEV_NVECTORS_UNSPECIFIED = -1,
13};
14
15#define TYPE_DEVICE "device"
16#define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
17#define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
18#define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
19
3d1237fb
MA
20typedef enum DeviceCategory {
21 DEVICE_CATEGORY_BRIDGE,
22 DEVICE_CATEGORY_USB,
23 DEVICE_CATEGORY_STORAGE,
24 DEVICE_CATEGORY_NETWORK,
25 DEVICE_CATEGORY_INPUT,
26 DEVICE_CATEGORY_DISPLAY,
27 DEVICE_CATEGORY_SOUND,
28 DEVICE_CATEGORY_MISC,
29 DEVICE_CATEGORY_MAX
30} DeviceCategory;
31
074a86fc
AL
32typedef int (*qdev_initfn)(DeviceState *dev);
33typedef int (*qdev_event)(DeviceState *dev);
34typedef void (*qdev_resetfn)(DeviceState *dev);
249d4172
AF
35typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
36typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
02e7f85d
BD
37typedef void (*BusRealize)(BusState *bus, Error **errp);
38typedef void (*BusUnrealize)(BusState *bus, Error **errp);
074a86fc
AL
39
40struct VMStateDescription;
41
249d4172
AF
42/**
43 * DeviceClass:
44 * @props: Properties accessing state fields.
45 * @realize: Callback function invoked when the #DeviceState:realized
46 * property is changed to %true. The default invokes @init if not %NULL.
47 * @unrealize: Callback function invoked when the #DeviceState:realized
48 * property is changed to %false.
49 * @init: Callback function invoked when the #DeviceState::realized property
50 * is changed to %true. Deprecated, new types inheriting directly from
51 * TYPE_DEVICE should use @realize instead, new leaf types should consult
52 * their respective parent type.
1a37eca1
IM
53 * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
54 * as readonly "hotpluggable" property of #DeviceState instance
249d4172
AF
55 *
56 * # Realization #
57 * Devices are constructed in two stages,
58 * 1) object instantiation via object_initialize() and
59 * 2) device realization via #DeviceState:realized property.
60 * The former may not fail (it might assert or exit), the latter may return
61 * error information to the caller and must be re-entrant.
62 * Trivial field initializations should go into #TypeInfo.instance_init.
63 * Operations depending on @props static properties should go into @realize.
64 * After successful realization, setting static properties will fail.
65 *
daeba969
MA
66 * As an interim step, the #DeviceState:realized property can also be
67 * set with qdev_init_nofail().
249d4172
AF
68 * In the future, devices will propagate this state change to their children
69 * and along busses they expose.
70 * The point in time will be deferred to machine creation, so that values
71 * set in @realize will not be introspectable beforehand. Therefore devices
72 * must not create children during @realize; they should initialize them via
73 * object_initialize() in their own #TypeInfo.instance_init and forward the
74 * realization events appropriately.
75 *
76 * The @init callback is considered private to a particular bus implementation
77 * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an
78 * "init" callback on their parent class instead.
782beb52 79 *
249d4172 80 * Any type may override the @realize and/or @unrealize callbacks but needs
782beb52
AF
81 * to call the parent type's implementation if keeping their functionality
82 * is desired. Refer to QOM documentation for further discussion and examples.
83 *
84 * <note>
85 * <para>
249d4172
AF
86 * If a type derived directly from TYPE_DEVICE implements @realize, it does
87 * not need to implement @init and therefore does not need to store and call
88 * #DeviceClass' default @realize callback.
782beb52
AF
89 * For other types consult the documentation and implementation of the
90 * respective parent types.
91 * </para>
92 * </note>
249d4172 93 */
074a86fc 94typedef struct DeviceClass {
249d4172 95 /*< private >*/
074a86fc 96 ObjectClass parent_class;
249d4172 97 /*< public >*/
074a86fc 98
3d1237fb 99 DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
074a86fc
AL
100 const char *fw_name;
101 const char *desc;
102 Property *props;
efec3dd6
MA
103
104 /*
105 * Shall we hide this device model from -device / device_add?
106 * All devices should support instantiation with device_add, and
107 * this flag should not exist. But we're not there, yet. Some
108 * devices fail to instantiate with cryptic error messages.
109 * Others instantiate, but don't work. Exposing users to such
110 * behavior would be cruel; this flag serves to protect them. It
111 * should never be set without a comment explaining why it is set.
112 * TODO remove once we're there
113 */
114 bool cannot_instantiate_with_device_add_yet;
4c315c27
MA
115 /*
116 * Does this device model survive object_unref(object_new(TNAME))?
117 * All device models should, and this flag shouldn't exist. Some
118 * devices crash in object_new(), some crash or hang in
119 * object_unref(). Makes introspecting properties with
120 * qmp_device_list_properties() dangerous. Bad, because it's used
121 * by -device FOO,help. This flag serves to protect that code.
122 * It should never be set without a comment explaining why it is
123 * set.
124 * TODO remove once we're there
125 */
126 bool cannot_destroy_with_object_finalize_yet;
127
1a37eca1 128 bool hotpluggable;
074a86fc
AL
129
130 /* callbacks */
131 void (*reset)(DeviceState *dev);
249d4172
AF
132 DeviceRealize realize;
133 DeviceUnrealize unrealize;
074a86fc
AL
134
135 /* device state */
136 const struct VMStateDescription *vmsd;
137
138 /* Private to qdev / bus. */
249d4172 139 qdev_initfn init; /* TODO remove, once users are converted to realize */
fe6c2117 140 qdev_event exit; /* TODO remove, once users are converted to unrealize */
074a86fc
AL
141 const char *bus_type;
142} DeviceClass;
143
a5f54290
PC
144typedef struct NamedGPIOList NamedGPIOList;
145
146struct NamedGPIOList {
147 char *name;
148 qemu_irq *in;
149 int num_in;
a5f54290
PC
150 int num_out;
151 QLIST_ENTRY(NamedGPIOList) node;
152};
153
7983c8a3
AF
154/**
155 * DeviceState:
156 * @realized: Indicates whether the device has been fully constructed.
157 *
158 * This structure should not be accessed directly. We declare it here
159 * so that it can be embedded in individual device state structures.
160 */
074a86fc 161struct DeviceState {
7983c8a3 162 /*< private >*/
074a86fc 163 Object parent_obj;
7983c8a3 164 /*< public >*/
074a86fc
AL
165
166 const char *id;
7983c8a3 167 bool realized;
352e8da7 168 bool pending_deleted_event;
074a86fc
AL
169 QemuOpts *opts;
170 int hotplugged;
171 BusState *parent_bus;
a5f54290 172 QLIST_HEAD(, NamedGPIOList) gpios;
074a86fc
AL
173 QLIST_HEAD(, BusState) child_bus;
174 int num_child_bus;
175 int instance_id_alias;
176 int alias_required_for_version;
177};
178
707ff800
PD
179struct DeviceListener {
180 void (*realize)(DeviceListener *listener, DeviceState *dev);
181 void (*unrealize)(DeviceListener *listener, DeviceState *dev);
182 QTAILQ_ENTRY(DeviceListener) link;
183};
184
074a86fc
AL
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
190struct 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);
dcc20931 202 void (*reset)(BusState *bus);
02e7f85d
BD
203 BusRealize realize;
204 BusUnrealize unrealize;
205
1395af6f
FK
206 /* maximum devices allowed on the bus, 0: no limit. */
207 int max_dev;
61de3676
AG
208 /* number of automatically allocated bus ids (e.g. ide.0) */
209 int automatic_ids;
074a86fc
AL
210};
211
212typedef struct BusChild {
213 DeviceState *child;
214 int index;
215 QTAILQ_ENTRY(BusChild) sibling;
216} BusChild;
217
0ee4de6c
IM
218#define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
219
074a86fc
AL
220/**
221 * BusState:
0ee4de6c 222 * @hotplug_device: link to a hotplug device associated with bus.
074a86fc
AL
223 */
224struct BusState {
225 Object obj;
226 DeviceState *parent;
227 const char *name;
0ee4de6c 228 HotplugHandler *hotplug_handler;
074a86fc 229 int max_index;
02e7f85d 230 bool realized;
074a86fc
AL
231 QTAILQ_HEAD(ChildrenHead, BusChild) children;
232 QLIST_ENTRY(BusState) sibling;
233};
234
235struct Property {
236 const char *name;
237 PropertyInfo *info;
3b6ca402 238 ptrdiff_t offset;
074a86fc 239 uint8_t bitnr;
1310a3d3 240 QType qtype;
074a86fc 241 int64_t defval;
0be6bfac
PM
242 int arrayoffset;
243 PropertyInfo *arrayinfo;
244 int arrayfieldsize;
074a86fc
AL
245};
246
247struct PropertyInfo {
248 const char *name;
51b2e8c3 249 const char *description;
2e4450ff 250 const char * const *enum_table;
074a86fc
AL
251 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
252 ObjectPropertyAccessor *get;
253 ObjectPropertyAccessor *set;
254 ObjectPropertyRelease *release;
255};
256
9f9260a3
DS
257/**
258 * GlobalProperty:
b3ce84fe
EH
259 * @user_provided: Set to true if property comes from user-provided config
260 * (command-line or config file).
261 * @used: Set to true if property was used when initializing a device.
77280adb
EH
262 * @errp: Error destination, used like first argument of error_setg()
263 * in case property setting fails later. If @errp is NULL, we
b3443f43
GK
264 * print warnings instead of ignoring errors silently. For
265 * hotplugged devices, errp is always ignored and warnings are
266 * printed instead.
9f9260a3 267 */
074a86fc
AL
268typedef struct GlobalProperty {
269 const char *driver;
270 const char *property;
271 const char *value;
b3ce84fe
EH
272 bool user_provided;
273 bool used;
77280adb 274 Error **errp;
074a86fc
AL
275} GlobalProperty;
276
277/*** Board API. This should go away once we have a machine config file. ***/
278
279DeviceState *qdev_create(BusState *bus, const char *name);
280DeviceState *qdev_try_create(BusState *bus, const char *name);
074a86fc
AL
281void qdev_init_nofail(DeviceState *dev);
282void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
283 int required_for_version);
c06b2ffb 284HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
074a86fc 285void qdev_unplug(DeviceState *dev, Error **errp);
014176f9
IM
286void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
287 DeviceState *dev, Error **errp);
074a86fc
AL
288void qdev_machine_creation_done(void);
289bool qdev_machine_modified(void);
290
291qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
a5f54290
PC
292qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
293
074a86fc 294void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
a5f54290
PC
295void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
296 qemu_irq pin);
b7973186 297qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
0c24db2b
PC
298qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
299 const char *name, int n);
074a86fc
AL
300
301BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
302
303/*** Device API. ***/
304
305/* Register device properties. */
306/* GPIO inputs also double as IRQ sinks. */
307void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
308void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
a5f54290
PC
309void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
310 const char *name, int n);
311void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
312 const char *name, int n);
074a86fc 313
17a96a14
PC
314void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
315 const char *name);
316
074a86fc
AL
317BusState *qdev_get_parent_bus(DeviceState *dev);
318
319/*** BUS API. ***/
320
321DeviceState *qdev_find_recursive(BusState *bus, const char *id);
322
323/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
324typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
325typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
326
fb17dfe0 327void qbus_create_inplace(void *bus, size_t size, const char *typename,
074a86fc
AL
328 DeviceState *parent, const char *name);
329BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
330/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
331 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
332 * 0 otherwise. */
0293214b
PB
333int qbus_walk_children(BusState *bus,
334 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
335 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
336 void *opaque);
337int qdev_walk_children(DeviceState *dev,
338 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
339 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
340 void *opaque);
341
074a86fc 342void qdev_reset_all(DeviceState *dev);
ff8de075 343void qdev_reset_all_fn(void *opaque);
d0508c36
PB
344
345/**
346 * @qbus_reset_all:
347 * @bus: Bus to be reset.
348 *
349 * Reset @bus and perform a bus-level ("hard") reset of all devices connected
350 * to it, including recursive processing of all buses below @bus itself. A
351 * hard reset means that qbus_reset_all will reset all state of the device.
352 * For PCI devices, for example, this will include the base address registers
353 * or configuration space.
354 */
355void qbus_reset_all(BusState *bus);
074a86fc
AL
356void qbus_reset_all_fn(void *opaque);
357
074a86fc
AL
358/* This should go away once we get rid of the NULL bus hack */
359BusState *sysbus_get_default(void);
360
361char *qdev_get_fw_dev_path(DeviceState *dev);
0be63901 362char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
074a86fc
AL
363
364/**
365 * @qdev_machine_init
366 *
367 * Initialize platform devices before machine init. This is a hack until full
368 * support for composition is added.
369 */
370void qdev_machine_init(void);
371
372/**
373 * @device_reset
374 *
375 * Reset a single device (by calling the reset method).
376 */
377void device_reset(DeviceState *dev);
378
379const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
380
381const char *qdev_fw_name(DeviceState *dev);
382
383Object *qdev_get_machine(void);
384
385/* FIXME: make this a link<> */
386void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
387
388extern int qdev_hotplug;
389
390char *qdev_get_dev_path(DeviceState *dev);
391
4cae4d5a 392GSList *qdev_build_hotpluggable_device_list(Object *peripheral);
66e56b13 393
431bbb26
IM
394void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler,
395 Error **errp);
396
397void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp);
39b888bd
IM
398
399static inline bool qbus_is_hotpluggable(BusState *bus)
400{
2d9a982f 401 return bus->hotplug_handler;
39b888bd 402}
707ff800
PD
403
404void device_listener_register(DeviceListener *listener);
405void device_listener_unregister(DeviceListener *listener);
406
074a86fc 407#endif