1#ifndef QDEV_CORE_H
2#define QDEV_CORE_H
3
4#include "qemu/queue.h"
5#include "qemu/option.h"
6#include "qemu/bitmap.h"
7#include "qom/object.h"
8#include "hw/irq.h"
9#include "hw/hotplug.h"
10
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
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
32typedef int (*qdev_initfn)(DeviceState *dev);
33typedef int (*qdev_event)(DeviceState *dev);
34typedef void (*qdev_resetfn)(DeviceState *dev);
35typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
36typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
37typedef void (*BusRealize)(BusState *bus, Error **errp);
38typedef void (*BusUnrealize)(BusState *bus, Error **errp);
39
40struct VMStateDescription;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94typedef struct DeviceClass {
95
96 ObjectClass parent_class;
97
98
99 DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
100 const char *fw_name;
101 const char *desc;
102 Property *props;
103
104
105
106
107
108
109
110
111
112
113
114 bool cannot_instantiate_with_device_add_yet;
115
116
117
118
119
120
121
122
123
124
125
126 bool cannot_destroy_with_object_finalize_yet;
127
128 bool hotpluggable;
129
130
131 void (*reset)(DeviceState *dev);
132 void (*halt)(DeviceState *dev);
133 void (*unhalt)(DeviceState *dev);
134
135
136 void (*pwr_cntrl)(void *opaque, int n, int level);
137 void (*hlt_cntrl)(void *opaque, int n, int level);
138
139 void (*rst_cntrl)(void *opaque, int n, int level);
140
141 DeviceRealize realize;
142 DeviceUnrealize unrealize;
143
144
145 const struct VMStateDescription *vmsd;
146
147
148 qdev_initfn init;
149 qdev_event exit;
150 const char *bus_type;
151} DeviceClass;
152
153typedef struct NamedGPIOList NamedGPIOList;
154
155struct NamedGPIOList {
156 char *name;
157 qemu_irq *in;
158 int num_in;
159 int num_out;
160 QLIST_ENTRY(NamedGPIOList) node;
161};
162
163typedef struct PowerState {
164 bool power;
165 bool halt;
166 bool active;
167} PowerState;
168
169
170
171
172
173
174
175
176struct DeviceState {
177
178 Object parent_obj;
179
180
181 char *id;
182 bool realized;
183 bool pending_deleted_event;
184 QemuOpts *opts;
185 int hotplugged;
186 BusState *parent_bus;
187 QLIST_HEAD(, NamedGPIOList) gpios;
188 QLIST_HEAD(, BusState) child_bus;
189 int num_child_bus;
190 int instance_id_alias;
191 int alias_required_for_version;
192 PowerState ps;
193};
194
195struct DeviceListener {
196 void (*realize)(DeviceListener *listener, DeviceState *dev);
197 void (*unrealize)(DeviceListener *listener, DeviceState *dev);
198 QTAILQ_ENTRY(DeviceListener) link;
199};
200
201#define TYPE_BUS "bus"
202#define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
203#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
204#define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
205
206struct BusClass {
207 ObjectClass parent_class;
208
209
210 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
211 char *(*get_dev_path)(DeviceState *dev);
212
213
214
215
216
217 char *(*get_fw_dev_path)(DeviceState *dev);
218 void (*reset)(BusState *bus);
219 BusRealize realize;
220 BusUnrealize unrealize;
221
222
223 int max_dev;
224
225 int automatic_ids;
226};
227
228typedef struct BusChild {
229 DeviceState *child;
230 int index;
231 QTAILQ_ENTRY(BusChild) sibling;
232} BusChild;
233
234#define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
235
236
237
238
239
240struct BusState {
241 Object obj;
242 DeviceState *parent;
243 char *name;
244 HotplugHandler *hotplug_handler;
245 int max_index;
246 bool realized;
247 QTAILQ_HEAD(ChildrenHead, BusChild) children;
248 QLIST_ENTRY(BusState) sibling;
249};
250
251struct Property {
252 const char *name;
253 PropertyInfo *info;
254 ptrdiff_t offset;
255 uint8_t bitnr;
256 QType qtype;
257 int64_t defval;
258 int arrayoffset;
259 PropertyInfo *arrayinfo;
260 int arrayfieldsize;
261};
262
263struct PropertyInfo {
264 const char *name;
265 const char *description;
266 const char * const *enum_table;
267 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
268 ObjectPropertyAccessor *get;
269 ObjectPropertyAccessor *set;
270 ObjectPropertyRelease *release;
271};
272
273
274
275
276
277
278
279
280
281
282
283
284typedef struct GlobalProperty {
285 const char *driver;
286 const char *property;
287 const char *value;
288 bool user_provided;
289 bool used;
290 Error **errp;
291} GlobalProperty;
292
293
294
295DeviceState *qdev_create(BusState *bus, const char *name);
296DeviceState *qdev_try_create(BusState *bus, const char *name);
297void qdev_init_nofail(DeviceState *dev);
298void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
299 int required_for_version);
300HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
301void qdev_unplug(DeviceState *dev, Error **errp);
302void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
303 DeviceState *dev, Error **errp);
304void qdev_machine_creation_done(void);
305bool qdev_machine_modified(void);
306
307qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
308qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
309qemu_irq qdev_get_gpio_out(DeviceState *dev, int n);
310qemu_irq qdev_get_gpio_out_named(DeviceState *dev, const char *name, int n);
311
312void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
313void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
314 qemu_irq pin);
315qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
316qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
317 const char *name, int n);
318
319BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
320
321
322
323
324
325void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
326void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
327void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
328 const char *name, int n);
329void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
330 const char *name, int n);
331
332void qdev_pass_all_gpios(DeviceState *dev, DeviceState *container);
333void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
334 const char *name);
335
336BusState *qdev_get_parent_bus(DeviceState *dev);
337
338
339
340DeviceState *qdev_find_recursive(BusState *bus, const char *id);
341
342
343typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
344typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
345
346void qbus_create_inplace(void *bus, size_t size, const char *typename,
347 DeviceState *parent, const char *name);
348BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
349
350
351
352int qbus_walk_children(BusState *bus,
353 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
354 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
355 void *opaque);
356int qdev_walk_children(DeviceState *dev,
357 qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
358 qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
359 void *opaque);
360
361void qdev_reset_all(DeviceState *dev);
362void qdev_reset_all_fn(void *opaque);
363
364
365
366
367
368
369
370
371
372
373
374void qbus_reset_all(BusState *bus);
375void qbus_reset_all_fn(void *opaque);
376
377
378BusState *sysbus_get_default(void);
379
380char *qdev_get_fw_dev_path(DeviceState *dev);
381char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
382
383
384
385
386
387
388
389void qdev_machine_init(void);
390
391
392
393
394
395
396void device_reset(DeviceState *dev);
397
398
399
400
401
402
403void device_halt(DeviceState *dev);
404
405
406
407
408
409
410void device_unhalt(DeviceState *dev);
411
412const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
413
414const char *qdev_fw_name(DeviceState *dev);
415
416Object *qdev_get_machine(void);
417
418
419void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
420
421extern int qdev_hotplug;
422
423char *qdev_get_dev_path(DeviceState *dev);
424
425GSList *qdev_build_hotpluggable_device_list(Object *peripheral);
426
427void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler,
428 Error **errp);
429
430void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp);
431
432static inline bool qbus_is_hotpluggable(BusState *bus)
433{
434 return bus->hotplug_handler;
435}
436
437void device_listener_register(DeviceListener *listener);
438void device_listener_unregister(DeviceListener *listener);
439
440#endif
441