]> git.proxmox.com Git - qemu.git/blame_incremental - hw/qdev.h
Merge remote-tracking branch 'spice/spice.v39' into staging
[qemu.git] / hw / qdev.h
... / ...
CommitLineData
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
9typedef struct Property Property;
10
11typedef struct PropertyInfo PropertyInfo;
12
13typedef struct CompatProperty CompatProperty;
14
15typedef struct DeviceInfo DeviceInfo;
16
17typedef struct BusState BusState;
18
19typedef struct BusInfo BusInfo;
20
21enum DevState {
22 DEV_STATE_CREATED = 1,
23 DEV_STATE_INITIALIZED,
24};
25
26enum {
27 DEV_NVECTORS_UNSPECIFIED = -1,
28};
29
30/* This structure should not be accessed directly. We declare it here
31 so that it can be embedded in individual device state structures. */
32struct DeviceState {
33 const char *id;
34 enum DevState state;
35 QemuOpts *opts;
36 int hotplugged;
37 DeviceInfo *info;
38 BusState *parent_bus;
39 int num_gpio_out;
40 qemu_irq *gpio_out;
41 int num_gpio_in;
42 qemu_irq *gpio_in;
43 QLIST_HEAD(, BusState) child_bus;
44 int num_child_bus;
45 QLIST_ENTRY(DeviceState) sibling;
46 int instance_id_alias;
47 int alias_required_for_version;
48};
49
50typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
51typedef char *(*bus_get_dev_path)(DeviceState *dev);
52/*
53 * This callback is used to create Open Firmware device path in accordance with
54 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
55 * can be found here http://playground.sun.com/1275/bindings/.
56 */
57typedef char *(*bus_get_fw_dev_path)(DeviceState *dev);
58typedef int (qbus_resetfn)(BusState *bus);
59
60struct BusInfo {
61 const char *name;
62 size_t size;
63 bus_dev_printfn print_dev;
64 bus_get_dev_path get_dev_path;
65 bus_get_fw_dev_path get_fw_dev_path;
66 qbus_resetfn *reset;
67 Property *props;
68};
69
70struct BusState {
71 DeviceState *parent;
72 BusInfo *info;
73 const char *name;
74 int allow_hotplug;
75 int qdev_allocated;
76 QLIST_HEAD(, DeviceState) children;
77 QLIST_ENTRY(BusState) sibling;
78};
79
80struct Property {
81 const char *name;
82 PropertyInfo *info;
83 int offset;
84 int bitnr;
85 void *defval;
86};
87
88enum PropertyType {
89 PROP_TYPE_UNSPEC = 0,
90 PROP_TYPE_UINT8,
91 PROP_TYPE_UINT16,
92 PROP_TYPE_UINT32,
93 PROP_TYPE_INT32,
94 PROP_TYPE_UINT64,
95 PROP_TYPE_TADDR,
96 PROP_TYPE_MACADDR,
97 PROP_TYPE_DRIVE,
98 PROP_TYPE_CHR,
99 PROP_TYPE_STRING,
100 PROP_TYPE_NETDEV,
101 PROP_TYPE_VLAN,
102 PROP_TYPE_PTR,
103 PROP_TYPE_BIT,
104};
105
106struct PropertyInfo {
107 const char *name;
108 size_t size;
109 enum PropertyType type;
110 int (*parse)(DeviceState *dev, Property *prop, const char *str);
111 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
112 void (*free)(DeviceState *dev, Property *prop);
113};
114
115typedef struct GlobalProperty {
116 const char *driver;
117 const char *property;
118 const char *value;
119 QTAILQ_ENTRY(GlobalProperty) next;
120} GlobalProperty;
121
122/*** Board API. This should go away once we have a machine config file. ***/
123
124DeviceState *qdev_create(BusState *bus, const char *name);
125DeviceState *qdev_try_create(BusState *bus, const char *name);
126int qdev_device_help(QemuOpts *opts);
127DeviceState *qdev_device_add(QemuOpts *opts);
128int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
129void qdev_init_nofail(DeviceState *dev);
130void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
131 int required_for_version);
132int qdev_unplug(DeviceState *dev);
133void qdev_free(DeviceState *dev);
134int qdev_simple_unplug_cb(DeviceState *dev);
135void qdev_machine_creation_done(void);
136bool qdev_machine_modified(void);
137
138qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
139void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
140
141BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
142
143/*** Device API. ***/
144
145typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
146typedef int (*qdev_event)(DeviceState *dev);
147typedef void (*qdev_resetfn)(DeviceState *dev);
148
149struct DeviceInfo {
150 const char *name;
151 const char *fw_name;
152 const char *alias;
153 const char *desc;
154 size_t size;
155 Property *props;
156 int no_user;
157
158 /* callbacks */
159 qdev_resetfn reset;
160
161 /* device state */
162 const VMStateDescription *vmsd;
163
164 /* Private to qdev / bus. */
165 qdev_initfn init;
166 qdev_event unplug;
167 qdev_event exit;
168 BusInfo *bus_info;
169 struct DeviceInfo *next;
170};
171extern DeviceInfo *device_info_list;
172
173void qdev_register(DeviceInfo *info);
174
175/* Register device properties. */
176/* GPIO inputs also double as IRQ sinks. */
177void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
178void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
179
180CharDriverState *qdev_init_chardev(DeviceState *dev);
181
182BusState *qdev_get_parent_bus(DeviceState *dev);
183
184/*** BUS API. ***/
185
186DeviceState *qdev_find_recursive(BusState *bus, const char *id);
187
188/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
189typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
190typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
191
192void qbus_create_inplace(BusState *bus, BusInfo *info,
193 DeviceState *parent, const char *name);
194BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
195/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
196 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
197 * 0 otherwise. */
198int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
199 qbus_walkerfn *busfn, void *opaque);
200int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
201 qbus_walkerfn *busfn, void *opaque);
202void qdev_reset_all(DeviceState *dev);
203void qbus_reset_all_fn(void *opaque);
204
205void qbus_free(BusState *bus);
206
207#define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
208
209/* This should go away once we get rid of the NULL bus hack */
210BusState *sysbus_get_default(void);
211
212/*** monitor commands ***/
213
214void do_info_qtree(Monitor *mon);
215void do_info_qdm(Monitor *mon);
216int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
217int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
218
219/*** qdev-properties.c ***/
220
221extern PropertyInfo qdev_prop_bit;
222extern PropertyInfo qdev_prop_uint8;
223extern PropertyInfo qdev_prop_uint16;
224extern PropertyInfo qdev_prop_uint32;
225extern PropertyInfo qdev_prop_int32;
226extern PropertyInfo qdev_prop_uint64;
227extern PropertyInfo qdev_prop_hex32;
228extern PropertyInfo qdev_prop_hex64;
229extern PropertyInfo qdev_prop_string;
230extern PropertyInfo qdev_prop_chr;
231extern PropertyInfo qdev_prop_ptr;
232extern PropertyInfo qdev_prop_macaddr;
233extern PropertyInfo qdev_prop_drive;
234extern PropertyInfo qdev_prop_netdev;
235extern PropertyInfo qdev_prop_vlan;
236extern PropertyInfo qdev_prop_pci_devfn;
237
238#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
239 .name = (_name), \
240 .info = &(_prop), \
241 .offset = offsetof(_state, _field) \
242 + type_check(_type,typeof_field(_state, _field)), \
243 }
244#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
245 .name = (_name), \
246 .info = &(_prop), \
247 .offset = offsetof(_state, _field) \
248 + type_check(_type,typeof_field(_state, _field)), \
249 .defval = (_type[]) { _defval }, \
250 }
251#define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
252 .name = (_name), \
253 .info = &(qdev_prop_bit), \
254 .bitnr = (_bit), \
255 .offset = offsetof(_state, _field) \
256 + type_check(uint32_t,typeof_field(_state, _field)), \
257 .defval = (bool[]) { (_defval) }, \
258 }
259
260#define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
261 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
262#define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
263 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
264#define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
265 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
266#define DEFINE_PROP_INT32(_n, _s, _f, _d) \
267 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
268#define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
269 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
270#define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
271 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
272#define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
273 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
274#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
275 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
276
277#define DEFINE_PROP_PTR(_n, _s, _f) \
278 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
279#define DEFINE_PROP_CHR(_n, _s, _f) \
280 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
281#define DEFINE_PROP_STRING(_n, _s, _f) \
282 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
283#define DEFINE_PROP_NETDEV(_n, _s, _f) \
284 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
285#define DEFINE_PROP_VLAN(_n, _s, _f) \
286 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
287#define DEFINE_PROP_DRIVE(_n, _s, _f) \
288 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
289#define DEFINE_PROP_MACADDR(_n, _s, _f) \
290 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
291
292#define DEFINE_PROP_END_OF_LIST() \
293 {}
294
295/* Set properties between creation and init. */
296void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
297int qdev_prop_exists(DeviceState *dev, const char *name);
298int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
299void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
300void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
301void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
302void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
303void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
304void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
305void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
306void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
307void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
308void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
309void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
310int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
311void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
312void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
313/* FIXME: Remove opaque pointer properties. */
314void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
315void qdev_prop_set_defaults(DeviceState *dev, Property *props);
316
317void qdev_prop_register_global_list(GlobalProperty *props);
318void qdev_prop_set_globals(DeviceState *dev);
319
320static inline const char *qdev_fw_name(DeviceState *dev)
321{
322 return dev->info->fw_name ? : dev->info->alias ? : dev->info->name;
323}
324
325char *qdev_get_fw_dev_path(DeviceState *dev);
326/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
327extern struct BusInfo system_bus_info;
328
329#endif