]> git.proxmox.com Git - mirror_qemu.git/blob - hw/qdev.h
qdev: Convert busses to QEMU Object Model
[mirror_qemu.git] / hw / qdev.h
1 #ifndef QDEV_H
2 #define QDEV_H
3
4 #include "hw.h"
5 #include "qemu-queue.h"
6 #include "qemu-char.h"
7 #include "qemu-option.h"
8 #include "qapi/qapi-visit-core.h"
9 #include "qemu/object.h"
10 #include "error.h"
11
12 typedef struct Property Property;
13
14 typedef struct PropertyInfo PropertyInfo;
15
16 typedef struct CompatProperty CompatProperty;
17
18 typedef struct BusState BusState;
19
20 typedef struct BusClass BusClass;
21
22 enum DevState {
23 DEV_STATE_CREATED = 1,
24 DEV_STATE_INITIALIZED,
25 };
26
27 enum {
28 DEV_NVECTORS_UNSPECIFIED = -1,
29 };
30
31 #define TYPE_DEVICE "device"
32 #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
33 #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
34 #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
35
36 typedef int (*qdev_initfn)(DeviceState *dev);
37 typedef int (*qdev_event)(DeviceState *dev);
38 typedef void (*qdev_resetfn)(DeviceState *dev);
39
40 typedef struct DeviceClass {
41 ObjectClass parent_class;
42
43 const char *fw_name;
44 const char *desc;
45 Property *props;
46 int no_user;
47
48 /* callbacks */
49 void (*reset)(DeviceState *dev);
50
51 /* device state */
52 const VMStateDescription *vmsd;
53
54 /* Private to qdev / bus. */
55 qdev_initfn init;
56 qdev_event unplug;
57 qdev_event exit;
58 const char *bus_type;
59 } DeviceClass;
60
61 /* This structure should not be accessed directly. We declare it here
62 so that it can be embedded in individual device state structures. */
63 struct DeviceState {
64 Object parent_obj;
65
66 const char *id;
67 enum DevState state;
68 QemuOpts *opts;
69 int hotplugged;
70 BusState *parent_bus;
71 int num_gpio_out;
72 qemu_irq *gpio_out;
73 int num_gpio_in;
74 qemu_irq *gpio_in;
75 QLIST_HEAD(, BusState) child_bus;
76 int num_child_bus;
77 QTAILQ_ENTRY(DeviceState) sibling;
78 int instance_id_alias;
79 int alias_required_for_version;
80 };
81
82 /*
83 * This callback is used to create Open Firmware device path in accordance with
84 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
85 * can be found here http://playground.sun.com/1275/bindings/.
86 */
87
88 #define TYPE_BUS "bus"
89 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
90 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
91 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
92
93 struct BusClass {
94 ObjectClass parent_class;
95
96 /* FIXME first arg should be BusState */
97 void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
98 char *(*get_dev_path)(DeviceState *dev);
99 char *(*get_fw_dev_path)(DeviceState *dev);
100 int (*reset)(BusState *bus);
101 };
102
103 /**
104 * BusState:
105 * @qom_allocated: Indicates whether the object was allocated by QOM.
106 * @glib_allocated: Indicates whether the object was initialized in-place
107 * yet is expected to be freed with g_free().
108 */
109 struct BusState {
110 Object obj;
111 DeviceState *parent;
112 const char *name;
113 int allow_hotplug;
114 bool qom_allocated;
115 bool glib_allocated;
116 QTAILQ_HEAD(ChildrenHead, DeviceState) children;
117 QLIST_ENTRY(BusState) sibling;
118 };
119
120 struct Property {
121 const char *name;
122 PropertyInfo *info;
123 int offset;
124 uint8_t bitnr;
125 uint8_t qtype;
126 int64_t defval;
127 };
128
129 struct PropertyInfo {
130 const char *name;
131 const char *legacy_name;
132 const char **enum_table;
133 int (*parse)(DeviceState *dev, Property *prop, const char *str);
134 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
135 ObjectPropertyAccessor *get;
136 ObjectPropertyAccessor *set;
137 ObjectPropertyRelease *release;
138 };
139
140 typedef struct GlobalProperty {
141 const char *driver;
142 const char *property;
143 const char *value;
144 QTAILQ_ENTRY(GlobalProperty) next;
145 } GlobalProperty;
146
147 /*** Board API. This should go away once we have a machine config file. ***/
148
149 DeviceState *qdev_create(BusState *bus, const char *name);
150 DeviceState *qdev_try_create(BusState *bus, const char *name);
151 bool qdev_exists(const char *name);
152 int qdev_device_help(QemuOpts *opts);
153 DeviceState *qdev_device_add(QemuOpts *opts);
154 int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
155 void qdev_init_nofail(DeviceState *dev);
156 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
157 int required_for_version);
158 void qdev_unplug(DeviceState *dev, Error **errp);
159 void qdev_free(DeviceState *dev);
160 int qdev_simple_unplug_cb(DeviceState *dev);
161 void qdev_machine_creation_done(void);
162 bool qdev_machine_modified(void);
163
164 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
165 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
166
167 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
168
169 /*** Device API. ***/
170
171 /* Register device properties. */
172 /* GPIO inputs also double as IRQ sinks. */
173 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
174 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
175
176 BusState *qdev_get_parent_bus(DeviceState *dev);
177
178 /*** BUS API. ***/
179
180 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
181
182 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
183 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
184 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
185
186 void qbus_create_inplace(BusState *bus, const char *typename,
187 DeviceState *parent, const char *name);
188 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
189 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
190 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
191 * 0 otherwise. */
192 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
193 qbus_walkerfn *busfn, void *opaque);
194 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
195 qbus_walkerfn *busfn, void *opaque);
196 void qdev_reset_all(DeviceState *dev);
197 void qbus_reset_all_fn(void *opaque);
198
199 void qbus_free(BusState *bus);
200
201 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
202
203 /* This should go away once we get rid of the NULL bus hack */
204 BusState *sysbus_get_default(void);
205
206 /*** monitor commands ***/
207
208 void do_info_qtree(Monitor *mon);
209 void do_info_qdm(Monitor *mon);
210 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
211 int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
212
213 /*** qdev-properties.c ***/
214
215 extern PropertyInfo qdev_prop_bit;
216 extern PropertyInfo qdev_prop_uint8;
217 extern PropertyInfo qdev_prop_uint16;
218 extern PropertyInfo qdev_prop_uint32;
219 extern PropertyInfo qdev_prop_int32;
220 extern PropertyInfo qdev_prop_uint64;
221 extern PropertyInfo qdev_prop_hex8;
222 extern PropertyInfo qdev_prop_hex32;
223 extern PropertyInfo qdev_prop_hex64;
224 extern PropertyInfo qdev_prop_string;
225 extern PropertyInfo qdev_prop_chr;
226 extern PropertyInfo qdev_prop_ptr;
227 extern PropertyInfo qdev_prop_macaddr;
228 extern PropertyInfo qdev_prop_losttickpolicy;
229 extern PropertyInfo qdev_prop_drive;
230 extern PropertyInfo qdev_prop_netdev;
231 extern PropertyInfo qdev_prop_vlan;
232 extern PropertyInfo qdev_prop_pci_devfn;
233 extern PropertyInfo qdev_prop_blocksize;
234
235 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
236 .name = (_name), \
237 .info = &(_prop), \
238 .offset = offsetof(_state, _field) \
239 + type_check(_type,typeof_field(_state, _field)), \
240 }
241 #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
242 .name = (_name), \
243 .info = &(_prop), \
244 .offset = offsetof(_state, _field) \
245 + type_check(_type,typeof_field(_state, _field)), \
246 .qtype = QTYPE_QINT, \
247 .defval = (_type)_defval, \
248 }
249 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
250 .name = (_name), \
251 .info = &(qdev_prop_bit), \
252 .bitnr = (_bit), \
253 .offset = offsetof(_state, _field) \
254 + type_check(uint32_t,typeof_field(_state, _field)), \
255 .qtype = QTYPE_QBOOL, \
256 .defval = (bool)_defval, \
257 }
258
259 #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
260 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
261 #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
262 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
263 #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
264 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
265 #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
266 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
267 #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
268 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
269 #define DEFINE_PROP_HEX8(_n, _s, _f, _d) \
270 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
271 #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
272 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
273 #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
274 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
275 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
276 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
277
278 #define DEFINE_PROP_PTR(_n, _s, _f) \
279 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
280 #define DEFINE_PROP_CHR(_n, _s, _f) \
281 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
282 #define DEFINE_PROP_STRING(_n, _s, _f) \
283 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
284 #define DEFINE_PROP_NETDEV(_n, _s, _f) \
285 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
286 #define DEFINE_PROP_VLAN(_n, _s, _f) \
287 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
288 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
289 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
290 #define DEFINE_PROP_MACADDR(_n, _s, _f) \
291 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
292 #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
293 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
294 LostTickPolicy)
295 #define DEFINE_PROP_BLOCKSIZE(_n, _s, _f, _d) \
296 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_blocksize, uint16_t)
297
298 #define DEFINE_PROP_END_OF_LIST() \
299 {}
300
301 /* Set properties between creation and init. */
302 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
303 int qdev_prop_exists(DeviceState *dev, const char *name);
304 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
305 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
306 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
307 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
308 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
309 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
310 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
311 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
312 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
313 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
314 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
315 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
316 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
317 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
318 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
319 /* FIXME: Remove opaque pointer properties. */
320 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
321
322 void qdev_prop_register_global_list(GlobalProperty *props);
323 void qdev_prop_set_globals(DeviceState *dev);
324 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
325 Property *prop, const char *value);
326
327 char *qdev_get_fw_dev_path(DeviceState *dev);
328
329 /**
330 * @qdev_property_add_static - add a @Property to a device referencing a
331 * field in a struct.
332 */
333 void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp);
334
335 /**
336 * @qdev_machine_init
337 *
338 * Initialize platform devices before machine init. This is a hack until full
339 * support for composition is added.
340 */
341 void qdev_machine_init(void);
342
343 /**
344 * @device_reset
345 *
346 * Reset a single device (by calling the reset method).
347 */
348 void device_reset(DeviceState *dev);
349
350 const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
351
352 const char *qdev_fw_name(DeviceState *dev);
353
354 Object *qdev_get_machine(void);
355
356 /* FIXME: make this a link<> */
357 void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
358
359 extern int qdev_hotplug;
360
361 char *qdev_get_dev_path(DeviceState *dev);
362
363 #endif