]> git.proxmox.com Git - mirror_qemu.git/blame - hw/qdev.h
qdev: Move bus properties to abstract superclasses
[mirror_qemu.git] / hw / qdev.h
CommitLineData
aae9460e
PB
1#ifndef QDEV_H
2#define QDEV_H
3
4#include "hw.h"
72cf2d4f 5#include "qemu-queue.h"
313feaab 6#include "qemu-char.h"
f31d07d1 7#include "qemu-option.h"
44677ded 8#include "qapi/qapi-visit-core.h"
32fea402 9#include "qemu/object.h"
56f9107e 10#include "error.h"
aae9460e 11
ee6847d1
GH
12typedef struct Property Property;
13
14typedef struct PropertyInfo PropertyInfo;
aae9460e 15
b6b61144
GH
16typedef struct CompatProperty CompatProperty;
17
02e2da45 18typedef struct BusState BusState;
4d6ae674 19
10c4c98a
GH
20typedef struct BusInfo BusInfo;
21
131ec1bd
GH
22enum DevState {
23 DEV_STATE_CREATED = 1,
24 DEV_STATE_INITIALIZED,
25};
26
75422b0d
AS
27enum {
28 DEV_NVECTORS_UNSPECIFIED = -1,
29};
30
32fea402
AL
31#define TYPE_DEVICE "device"
32#define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
30fbb9fc
AL
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)
32fea402 35
d307af79 36typedef int (*qdev_initfn)(DeviceState *dev);
6e008585
AL
37typedef int (*qdev_event)(DeviceState *dev);
38typedef void (*qdev_resetfn)(DeviceState *dev);
39
32fea402
AL
40typedef struct DeviceClass {
41 ObjectClass parent_class;
6e008585
AL
42
43 const char *fw_name;
6e008585
AL
44 const char *desc;
45 Property *props;
46 int no_user;
47
48 /* callbacks */
94afdadc 49 void (*reset)(DeviceState *dev);
6e008585
AL
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 BusInfo *bus_info;
32fea402
AL
59} DeviceClass;
60
aae9460e
PB
61/* This structure should not be accessed directly. We declare it here
62 so that it can be embedded in individual device state structures. */
02e2da45 63struct DeviceState {
32fea402
AL
64 Object parent_obj;
65
f31d07d1 66 const char *id;
131ec1bd 67 enum DevState state;
ef80b466 68 QemuOpts *opts;
3418bd25 69 int hotplugged;
02e2da45 70 BusState *parent_bus;
aae9460e
PB
71 int num_gpio_out;
72 qemu_irq *gpio_out;
73 int num_gpio_in;
74 qemu_irq *gpio_in;
72cf2d4f 75 QLIST_HEAD(, BusState) child_bus;
d271de9f 76 int num_child_bus;
d8bb00d6 77 QTAILQ_ENTRY(DeviceState) sibling;
4d2ffa08
JK
78 int instance_id_alias;
79 int alias_required_for_version;
02e2da45
PB
80};
81
10c4c98a 82typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
6772b936 83typedef char *(*bus_get_dev_path)(DeviceState *dev);
21150814
GN
84/*
85 * This callback is used to create Open Firmware device path in accordance with
86 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
87 * can be found here http://playground.sun.com/1275/bindings/.
88 */
89typedef char *(*bus_get_fw_dev_path)(DeviceState *dev);
b4694b7c 90typedef int (qbus_resetfn)(BusState *bus);
6772b936 91
10c4c98a
GH
92struct BusInfo {
93 const char *name;
94 size_t size;
95 bus_dev_printfn print_dev;
6772b936 96 bus_get_dev_path get_dev_path;
21150814 97 bus_get_fw_dev_path get_fw_dev_path;
b4694b7c 98 qbus_resetfn *reset;
10c4c98a 99};
02e2da45
PB
100
101struct BusState {
102 DeviceState *parent;
10c4c98a 103 BusInfo *info;
02e2da45 104 const char *name;
3418bd25 105 int allow_hotplug;
cd739fb6 106 int qdev_allocated;
f48a7a6e 107 QTAILQ_HEAD(ChildrenHead, DeviceState) children;
72cf2d4f 108 QLIST_ENTRY(BusState) sibling;
aae9460e
PB
109};
110
ee6847d1
GH
111struct Property {
112 const char *name;
113 PropertyInfo *info;
114 int offset;
4f2d3d70
PB
115 uint8_t bitnr;
116 uint8_t qtype;
117 int64_t defval;
ee6847d1
GH
118};
119
ee6847d1
GH
120struct PropertyInfo {
121 const char *name;
cafe5bdb 122 const char *legacy_name;
1ce05125 123 const char **enum_table;
ee6847d1
GH
124 int (*parse)(DeviceState *dev, Property *prop, const char *str);
125 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
57c9fafe
AL
126 ObjectPropertyAccessor *get;
127 ObjectPropertyAccessor *set;
dd0ba250 128 ObjectPropertyRelease *release;
ee6847d1
GH
129};
130
458fb679 131typedef struct GlobalProperty {
b6b61144
GH
132 const char *driver;
133 const char *property;
134 const char *value;
458fb679
GH
135 QTAILQ_ENTRY(GlobalProperty) next;
136} GlobalProperty;
b6b61144 137
aae9460e
PB
138/*** Board API. This should go away once we have a machine config file. ***/
139
02e2da45 140DeviceState *qdev_create(BusState *bus, const char *name);
0bcdeda7 141DeviceState *qdev_try_create(BusState *bus, const char *name);
a369da5f 142bool qdev_exists(const char *name);
ff952ba2 143int qdev_device_help(QemuOpts *opts);
f31d07d1 144DeviceState *qdev_device_add(QemuOpts *opts);
747bbdf7 145int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
e23a1b33 146void qdev_init_nofail(DeviceState *dev);
4d2ffa08
JK
147void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
148 int required_for_version);
56f9107e 149void qdev_unplug(DeviceState *dev, Error **errp);
02e2da45 150void qdev_free(DeviceState *dev);
3418bd25
GH
151int qdev_simple_unplug_cb(DeviceState *dev);
152void qdev_machine_creation_done(void);
0ac8ef71 153bool qdev_machine_modified(void);
aae9460e 154
aae9460e
PB
155qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
156void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
157
02e2da45 158BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
4d6ae674 159
aae9460e
PB
160/*** Device API. ***/
161
aae9460e 162/* Register device properties. */
067a3ddc 163/* GPIO inputs also double as IRQ sinks. */
aae9460e
PB
164void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
165void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
166
02e2da45 167BusState *qdev_get_parent_bus(DeviceState *dev);
aae9460e 168
02e2da45
PB
169/*** BUS API. ***/
170
a2ee6b4f
IY
171DeviceState *qdev_find_recursive(BusState *bus, const char *id);
172
81699d8a
AL
173/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
174typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
175typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
176
cd739fb6
GH
177void qbus_create_inplace(BusState *bus, BusInfo *info,
178 DeviceState *parent, const char *name);
10c4c98a 179BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
81699d8a
AL
180/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
181 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
182 * 0 otherwise. */
183int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
184 qbus_walkerfn *busfn, void *opaque);
185int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
186 qbus_walkerfn *busfn, void *opaque);
5af0a04b 187void qdev_reset_all(DeviceState *dev);
80376c3f
IY
188void qbus_reset_all_fn(void *opaque);
189
131ec1bd 190void qbus_free(BusState *bus);
02e2da45
PB
191
192#define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
193
ec990eb6
AL
194/* This should go away once we get rid of the NULL bus hack */
195BusState *sysbus_get_default(void);
196
cae4956e
GH
197/*** monitor commands ***/
198
199void do_info_qtree(Monitor *mon);
f6c64e0e 200void do_info_qdm(Monitor *mon);
8bc27249 201int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
17a38eaa 202int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
cae4956e 203
ee6847d1
GH
204/*** qdev-properties.c ***/
205
d2364ee4 206extern PropertyInfo qdev_prop_bit;
c7cc172d 207extern PropertyInfo qdev_prop_uint8;
ee6847d1
GH
208extern PropertyInfo qdev_prop_uint16;
209extern PropertyInfo qdev_prop_uint32;
316940b0 210extern PropertyInfo qdev_prop_int32;
5a053d1f 211extern PropertyInfo qdev_prop_uint64;
6835678c 212extern PropertyInfo qdev_prop_hex8;
ee6847d1 213extern PropertyInfo qdev_prop_hex32;
5a053d1f 214extern PropertyInfo qdev_prop_hex64;
59419663 215extern PropertyInfo qdev_prop_string;
313feaab 216extern PropertyInfo qdev_prop_chr;
ee6847d1
GH
217extern PropertyInfo qdev_prop_ptr;
218extern PropertyInfo qdev_prop_macaddr;
4e4fa398 219extern PropertyInfo qdev_prop_losttickpolicy;
14b41872 220extern PropertyInfo qdev_prop_drive;
851bec09
GH
221extern PropertyInfo qdev_prop_netdev;
222extern PropertyInfo qdev_prop_vlan;
05cb5fe4 223extern PropertyInfo qdev_prop_pci_devfn;
02fda01c 224extern PropertyInfo qdev_prop_blocksize;
ee6847d1 225
cf12b95b
GH
226#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
227 .name = (_name), \
228 .info = &(_prop), \
229 .offset = offsetof(_state, _field) \
230 + type_check(_type,typeof_field(_state, _field)), \
231 }
232#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
233 .name = (_name), \
234 .info = &(_prop), \
235 .offset = offsetof(_state, _field) \
236 + type_check(_type,typeof_field(_state, _field)), \
4f2d3d70
PB
237 .qtype = QTYPE_QINT, \
238 .defval = (_type)_defval, \
cf12b95b 239 }
d2364ee4
MT
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)), \
4f2d3d70
PB
246 .qtype = QTYPE_QBOOL, \
247 .defval = (bool)_defval, \
d2364ee4 248 }
cf12b95b 249
c7cc172d
JQ
250#define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
251 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
cf12b95b
GH
252#define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
253 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
254#define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
255 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
316940b0
GH
256#define DEFINE_PROP_INT32(_n, _s, _f, _d) \
257 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
cf12b95b
GH
258#define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
259 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
6835678c
JK
260#define DEFINE_PROP_HEX8(_n, _s, _f, _d) \
261 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
cf12b95b
GH
262#define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
263 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
264#define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
265 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
266#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
09f1bbcd 267 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
cf12b95b
GH
268
269#define DEFINE_PROP_PTR(_n, _s, _f) \
270 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
313feaab
GH
271#define DEFINE_PROP_CHR(_n, _s, _f) \
272 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
59419663
GH
273#define DEFINE_PROP_STRING(_n, _s, _f) \
274 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
2ef924b4
GH
275#define DEFINE_PROP_NETDEV(_n, _s, _f) \
276 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
851bec09
GH
277#define DEFINE_PROP_VLAN(_n, _s, _f) \
278 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
f8b6cc00
MA
279#define DEFINE_PROP_DRIVE(_n, _s, _f) \
280 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
cf12b95b 281#define DEFINE_PROP_MACADDR(_n, _s, _f) \
1503fff3 282 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
4e4fa398
JK
283#define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \
284 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_losttickpolicy, \
285 LostTickPolicy)
02fda01c
SH
286#define DEFINE_PROP_BLOCKSIZE(_n, _s, _f, _d) \
287 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_blocksize, uint16_t)
cf12b95b
GH
288
289#define DEFINE_PROP_END_OF_LIST() \
290 {}
291
ee6847d1
GH
292/* Set properties between creation and init. */
293void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
d8ed79ae 294int qdev_prop_exists(DeviceState *dev, const char *name);
ee6847d1 295int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
f4594a3b 296void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
c7cc172d 297void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
ee6847d1
GH
298void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
299void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
316940b0 300void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
5a053d1f 301void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
cc984673 302void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
313feaab 303void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
2ef924b4 304void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
851bec09 305void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
18846dee
MA
306int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
307void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
1503fff3 308void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
9b170e60 309void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
ee6847d1
GH
310/* FIXME: Remove opaque pointer properties. */
311void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
312void qdev_prop_set_defaults(DeviceState *dev, Property *props);
313
458fb679
GH
314void qdev_prop_register_global_list(GlobalProperty *props);
315void qdev_prop_set_globals(DeviceState *dev);
7db4c4e8
PB
316void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
317 Property *prop, const char *value);
b6b61144 318
1ca4d09a 319char *qdev_get_fw_dev_path(DeviceState *dev);
4be9f0d1 320
a9ff9df1
BS
321/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
322extern struct BusInfo system_bus_info;
323
a5296ca9 324/**
ca2cc788
PB
325 * @qdev_property_add_static - add a @Property to a device referencing a
326 * field in a struct.
a5296ca9 327 */
ca2cc788 328void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp);
a5296ca9 329
1de81d28
AL
330/**
331 * @qdev_machine_init
332 *
333 * Initialize platform devices before machine init. This is a hack until full
334 * support for composition is added.
335 */
336void qdev_machine_init(void);
337
94afdadc
AL
338/**
339 * @device_reset
340 *
341 * Reset a single device (by calling the reset method).
342 */
343void device_reset(DeviceState *dev);
344
4be9f0d1
AL
345const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
346
347const char *qdev_fw_name(DeviceState *dev);
348
f05f6b4a
PB
349Object *qdev_get_machine(void);
350
9fbe6127
AL
351/* FIXME: make this a link<> */
352void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
353
ee46d8a5
AL
354extern int qdev_hotplug;
355
aae9460e 356#endif