]> git.proxmox.com Git - mirror_qemu.git/blame - include/hw/qdev-core.h
qdev: allow both pre- and post-order vists in qdev walking functions
[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"
6#include "qemu/typedefs.h"
949fc823 7#include "qemu/bitmap.h"
14cccb61 8#include "qom/object.h"
074a86fc 9#include "hw/irq.h"
7b1b5d19 10#include "qapi/error.h"
074a86fc 11
074a86fc
AL
12enum {
13 DEV_NVECTORS_UNSPECIFIED = -1,
14};
15
16#define TYPE_DEVICE "device"
17#define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
18#define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
19#define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
20
3d1237fb
MA
21typedef enum DeviceCategory {
22 DEVICE_CATEGORY_BRIDGE,
23 DEVICE_CATEGORY_USB,
24 DEVICE_CATEGORY_STORAGE,
25 DEVICE_CATEGORY_NETWORK,
26 DEVICE_CATEGORY_INPUT,
27 DEVICE_CATEGORY_DISPLAY,
28 DEVICE_CATEGORY_SOUND,
29 DEVICE_CATEGORY_MISC,
30 DEVICE_CATEGORY_MAX
31} DeviceCategory;
32
074a86fc
AL
33typedef int (*qdev_initfn)(DeviceState *dev);
34typedef int (*qdev_event)(DeviceState *dev);
35typedef void (*qdev_resetfn)(DeviceState *dev);
249d4172
AF
36typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
37typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
074a86fc
AL
38
39struct VMStateDescription;
40
249d4172
AF
41/**
42 * DeviceClass:
43 * @props: Properties accessing state fields.
44 * @realize: Callback function invoked when the #DeviceState:realized
45 * property is changed to %true. The default invokes @init if not %NULL.
46 * @unrealize: Callback function invoked when the #DeviceState:realized
47 * property is changed to %false.
48 * @init: Callback function invoked when the #DeviceState::realized property
49 * is changed to %true. Deprecated, new types inheriting directly from
50 * TYPE_DEVICE should use @realize instead, new leaf types should consult
51 * their respective parent type.
52 *
53 * # Realization #
54 * Devices are constructed in two stages,
55 * 1) object instantiation via object_initialize() and
56 * 2) device realization via #DeviceState:realized property.
57 * The former may not fail (it might assert or exit), the latter may return
58 * error information to the caller and must be re-entrant.
59 * Trivial field initializations should go into #TypeInfo.instance_init.
60 * Operations depending on @props static properties should go into @realize.
61 * After successful realization, setting static properties will fail.
62 *
63 * As an interim step, the #DeviceState:realized property is set by deprecated
64 * functions qdev_init() and qdev_init_nofail().
65 * In the future, devices will propagate this state change to their children
66 * and along busses they expose.
67 * The point in time will be deferred to machine creation, so that values
68 * set in @realize will not be introspectable beforehand. Therefore devices
69 * must not create children during @realize; they should initialize them via
70 * object_initialize() in their own #TypeInfo.instance_init and forward the
71 * realization events appropriately.
72 *
73 * The @init callback is considered private to a particular bus implementation
74 * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an
75 * "init" callback on their parent class instead.
782beb52 76 *
249d4172 77 * Any type may override the @realize and/or @unrealize callbacks but needs
782beb52
AF
78 * to call the parent type's implementation if keeping their functionality
79 * is desired. Refer to QOM documentation for further discussion and examples.
80 *
81 * <note>
82 * <para>
249d4172
AF
83 * If a type derived directly from TYPE_DEVICE implements @realize, it does
84 * not need to implement @init and therefore does not need to store and call
85 * #DeviceClass' default @realize callback.
782beb52
AF
86 * For other types consult the documentation and implementation of the
87 * respective parent types.
88 * </para>
89 * </note>
249d4172 90 */
074a86fc 91typedef struct DeviceClass {
249d4172 92 /*< private >*/
074a86fc 93 ObjectClass parent_class;
249d4172 94 /*< public >*/
074a86fc 95
3d1237fb 96 DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
074a86fc
AL
97 const char *fw_name;
98 const char *desc;
99 Property *props;
100 int no_user;
101
102 /* callbacks */
103 void (*reset)(DeviceState *dev);
249d4172
AF
104 DeviceRealize realize;
105 DeviceUnrealize unrealize;
074a86fc
AL
106
107 /* device state */
108 const struct VMStateDescription *vmsd;
109
110 /* Private to qdev / bus. */
249d4172 111 qdev_initfn init; /* TODO remove, once users are converted to realize */
074a86fc 112 qdev_event unplug;
fe6c2117 113 qdev_event exit; /* TODO remove, once users are converted to unrealize */
074a86fc
AL
114 const char *bus_type;
115} DeviceClass;
116
7983c8a3
AF
117/**
118 * DeviceState:
119 * @realized: Indicates whether the device has been fully constructed.
120 *
121 * This structure should not be accessed directly. We declare it here
122 * so that it can be embedded in individual device state structures.
123 */
074a86fc 124struct DeviceState {
7983c8a3 125 /*< private >*/
074a86fc 126 Object parent_obj;
7983c8a3 127 /*< public >*/
074a86fc
AL
128
129 const char *id;
7983c8a3 130 bool realized;
074a86fc
AL
131 QemuOpts *opts;
132 int hotplugged;
133 BusState *parent_bus;
134 int num_gpio_out;
135 qemu_irq *gpio_out;
136 int num_gpio_in;
137 qemu_irq *gpio_in;
138 QLIST_HEAD(, BusState) child_bus;
139 int num_child_bus;
140 int instance_id_alias;
141 int alias_required_for_version;
142};
143
144#define TYPE_BUS "bus"
145#define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
146#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
147#define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
148
149struct BusClass {
150 ObjectClass parent_class;
151
152 /* FIXME first arg should be BusState */
153 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
154 char *(*get_dev_path)(DeviceState *dev);
155 /*
156 * This callback is used to create Open Firmware device path in accordance
157 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
158 * bindings can be found at http://playground.sun.com/1275/bindings/.
159 */
160 char *(*get_fw_dev_path)(DeviceState *dev);
161 int (*reset)(BusState *bus);
1395af6f
FK
162 /* maximum devices allowed on the bus, 0: no limit. */
163 int max_dev;
074a86fc
AL
164};
165
166typedef struct BusChild {
167 DeviceState *child;
168 int index;
169 QTAILQ_ENTRY(BusChild) sibling;
170} BusChild;
171
172/**
173 * BusState:
074a86fc
AL
174 */
175struct BusState {
176 Object obj;
177 DeviceState *parent;
178 const char *name;
179 int allow_hotplug;
074a86fc
AL
180 int max_index;
181 QTAILQ_HEAD(ChildrenHead, BusChild) children;
182 QLIST_ENTRY(BusState) sibling;
183};
184
185struct Property {
186 const char *name;
187 PropertyInfo *info;
188 int offset;
189 uint8_t bitnr;
190 uint8_t qtype;
191 int64_t defval;
0be6bfac
PM
192 int arrayoffset;
193 PropertyInfo *arrayinfo;
194 int arrayfieldsize;
074a86fc
AL
195};
196
197struct PropertyInfo {
198 const char *name;
199 const char *legacy_name;
200 const char **enum_table;
201 int (*parse)(DeviceState *dev, Property *prop, const char *str);
202 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
203 ObjectPropertyAccessor *get;
204 ObjectPropertyAccessor *set;
205 ObjectPropertyRelease *release;
206};
207
208typedef struct GlobalProperty {
209 const char *driver;
210 const char *property;
211 const char *value;
212 QTAILQ_ENTRY(GlobalProperty) next;
213} GlobalProperty;
214
215/*** Board API. This should go away once we have a machine config file. ***/
216
217DeviceState *qdev_create(BusState *bus, const char *name);
218DeviceState *qdev_try_create(BusState *bus, const char *name);
219int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
220void qdev_init_nofail(DeviceState *dev);
221void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
222 int required_for_version);
223void qdev_unplug(DeviceState *dev, Error **errp);
074a86fc
AL
224int qdev_simple_unplug_cb(DeviceState *dev);
225void qdev_machine_creation_done(void);
226bool qdev_machine_modified(void);
227
228qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
229void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
230
231BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
232
233/*** Device API. ***/
234
235/* Register device properties. */
236/* GPIO inputs also double as IRQ sinks. */
237void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
238void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
239
240BusState *qdev_get_parent_bus(DeviceState *dev);
241
242/*** BUS API. ***/
243
244DeviceState *qdev_find_recursive(BusState *bus, const char *id);
245
246/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
247typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
248typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
249
fb17dfe0 250void qbus_create_inplace(void *bus, size_t size, const char *typename,
074a86fc
AL
251 DeviceState *parent, const char *name);
252BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
253/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
254 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
255 * 0 otherwise. */
0293214b
PB
256int qbus_walk_children(BusState *bus,
257 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
258 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
259 void *opaque);
260int qdev_walk_children(DeviceState *dev,
261 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
262 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
263 void *opaque);
264
074a86fc 265void qdev_reset_all(DeviceState *dev);
d0508c36
PB
266
267/**
268 * @qbus_reset_all:
269 * @bus: Bus to be reset.
270 *
271 * Reset @bus and perform a bus-level ("hard") reset of all devices connected
272 * to it, including recursive processing of all buses below @bus itself. A
273 * hard reset means that qbus_reset_all will reset all state of the device.
274 * For PCI devices, for example, this will include the base address registers
275 * or configuration space.
276 */
277void qbus_reset_all(BusState *bus);
074a86fc
AL
278void qbus_reset_all_fn(void *opaque);
279
280void qbus_free(BusState *bus);
281
074a86fc
AL
282/* This should go away once we get rid of the NULL bus hack */
283BusState *sysbus_get_default(void);
284
285char *qdev_get_fw_dev_path(DeviceState *dev);
286
287/**
288 * @qdev_machine_init
289 *
290 * Initialize platform devices before machine init. This is a hack until full
291 * support for composition is added.
292 */
293void qdev_machine_init(void);
294
295/**
296 * @device_reset
297 *
298 * Reset a single device (by calling the reset method).
299 */
300void device_reset(DeviceState *dev);
301
302const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
303
304const char *qdev_fw_name(DeviceState *dev);
305
306Object *qdev_get_machine(void);
307
308/* FIXME: make this a link<> */
309void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
310
311extern int qdev_hotplug;
312
313char *qdev_get_dev_path(DeviceState *dev);
314
315#endif