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