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