]> git.proxmox.com Git - mirror_qemu.git/blame - include/hw/qdev-core.h
define hotplug interface
[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;
efec3dd6
MA
100
101 /*
102 * Shall we hide this device model from -device / device_add?
103 * All devices should support instantiation with device_add, and
104 * this flag should not exist. But we're not there, yet. Some
105 * devices fail to instantiate with cryptic error messages.
106 * Others instantiate, but don't work. Exposing users to such
107 * behavior would be cruel; this flag serves to protect them. It
108 * should never be set without a comment explaining why it is set.
109 * TODO remove once we're there
110 */
111 bool cannot_instantiate_with_device_add_yet;
074a86fc
AL
112
113 /* callbacks */
114 void (*reset)(DeviceState *dev);
249d4172
AF
115 DeviceRealize realize;
116 DeviceUnrealize unrealize;
074a86fc
AL
117
118 /* device state */
119 const struct VMStateDescription *vmsd;
120
121 /* Private to qdev / bus. */
249d4172 122 qdev_initfn init; /* TODO remove, once users are converted to realize */
074a86fc 123 qdev_event unplug;
fe6c2117 124 qdev_event exit; /* TODO remove, once users are converted to unrealize */
074a86fc
AL
125 const char *bus_type;
126} DeviceClass;
127
7983c8a3
AF
128/**
129 * DeviceState:
130 * @realized: Indicates whether the device has been fully constructed.
131 *
132 * This structure should not be accessed directly. We declare it here
133 * so that it can be embedded in individual device state structures.
134 */
074a86fc 135struct DeviceState {
7983c8a3 136 /*< private >*/
074a86fc 137 Object parent_obj;
7983c8a3 138 /*< public >*/
074a86fc
AL
139
140 const char *id;
7983c8a3 141 bool realized;
074a86fc
AL
142 QemuOpts *opts;
143 int hotplugged;
144 BusState *parent_bus;
145 int num_gpio_out;
146 qemu_irq *gpio_out;
147 int num_gpio_in;
148 qemu_irq *gpio_in;
149 QLIST_HEAD(, BusState) child_bus;
150 int num_child_bus;
151 int instance_id_alias;
152 int alias_required_for_version;
153};
154
155#define TYPE_BUS "bus"
156#define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
157#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
158#define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
159
160struct BusClass {
161 ObjectClass parent_class;
162
163 /* FIXME first arg should be BusState */
164 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
165 char *(*get_dev_path)(DeviceState *dev);
166 /*
167 * This callback is used to create Open Firmware device path in accordance
168 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
169 * bindings can be found at http://playground.sun.com/1275/bindings/.
170 */
171 char *(*get_fw_dev_path)(DeviceState *dev);
dcc20931 172 void (*reset)(BusState *bus);
1395af6f
FK
173 /* maximum devices allowed on the bus, 0: no limit. */
174 int max_dev;
074a86fc
AL
175};
176
177typedef struct BusChild {
178 DeviceState *child;
179 int index;
180 QTAILQ_ENTRY(BusChild) sibling;
181} BusChild;
182
183/**
184 * BusState:
074a86fc
AL
185 */
186struct BusState {
187 Object obj;
188 DeviceState *parent;
189 const char *name;
190 int allow_hotplug;
074a86fc
AL
191 int max_index;
192 QTAILQ_HEAD(ChildrenHead, BusChild) children;
193 QLIST_ENTRY(BusState) sibling;
194};
195
196struct Property {
197 const char *name;
198 PropertyInfo *info;
199 int offset;
200 uint8_t bitnr;
201 uint8_t qtype;
202 int64_t defval;
0be6bfac
PM
203 int arrayoffset;
204 PropertyInfo *arrayinfo;
205 int arrayfieldsize;
074a86fc
AL
206};
207
208struct PropertyInfo {
209 const char *name;
210 const char *legacy_name;
211 const char **enum_table;
212 int (*parse)(DeviceState *dev, Property *prop, const char *str);
213 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
214 ObjectPropertyAccessor *get;
215 ObjectPropertyAccessor *set;
216 ObjectPropertyRelease *release;
217};
218
219typedef struct GlobalProperty {
220 const char *driver;
221 const char *property;
222 const char *value;
223 QTAILQ_ENTRY(GlobalProperty) next;
224} GlobalProperty;
225
226/*** Board API. This should go away once we have a machine config file. ***/
227
228DeviceState *qdev_create(BusState *bus, const char *name);
229DeviceState *qdev_try_create(BusState *bus, const char *name);
230int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
231void qdev_init_nofail(DeviceState *dev);
232void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
233 int required_for_version);
234void qdev_unplug(DeviceState *dev, Error **errp);
074a86fc
AL
235int qdev_simple_unplug_cb(DeviceState *dev);
236void qdev_machine_creation_done(void);
237bool qdev_machine_modified(void);
238
239qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
240void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
241
242BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
243
244/*** Device API. ***/
245
246/* Register device properties. */
247/* GPIO inputs also double as IRQ sinks. */
248void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
249void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
250
251BusState *qdev_get_parent_bus(DeviceState *dev);
252
253/*** BUS API. ***/
254
255DeviceState *qdev_find_recursive(BusState *bus, const char *id);
256
257/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
258typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
259typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
260
fb17dfe0 261void qbus_create_inplace(void *bus, size_t size, const char *typename,
074a86fc
AL
262 DeviceState *parent, const char *name);
263BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
264/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
265 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
266 * 0 otherwise. */
0293214b
PB
267int qbus_walk_children(BusState *bus,
268 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
269 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
270 void *opaque);
271int qdev_walk_children(DeviceState *dev,
272 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
273 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
274 void *opaque);
275
074a86fc 276void qdev_reset_all(DeviceState *dev);
d0508c36
PB
277
278/**
279 * @qbus_reset_all:
280 * @bus: Bus to be reset.
281 *
282 * Reset @bus and perform a bus-level ("hard") reset of all devices connected
283 * to it, including recursive processing of all buses below @bus itself. A
284 * hard reset means that qbus_reset_all will reset all state of the device.
285 * For PCI devices, for example, this will include the base address registers
286 * or configuration space.
287 */
288void qbus_reset_all(BusState *bus);
074a86fc
AL
289void qbus_reset_all_fn(void *opaque);
290
074a86fc
AL
291/* This should go away once we get rid of the NULL bus hack */
292BusState *sysbus_get_default(void);
293
294char *qdev_get_fw_dev_path(DeviceState *dev);
295
296/**
297 * @qdev_machine_init
298 *
299 * Initialize platform devices before machine init. This is a hack until full
300 * support for composition is added.
301 */
302void qdev_machine_init(void);
303
304/**
305 * @device_reset
306 *
307 * Reset a single device (by calling the reset method).
308 */
309void device_reset(DeviceState *dev);
310
311const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
312
313const char *qdev_fw_name(DeviceState *dev);
314
315Object *qdev_get_machine(void);
316
317/* FIXME: make this a link<> */
318void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
319
320extern int qdev_hotplug;
321
322char *qdev_get_dev_path(DeviceState *dev);
323
324#endif