]>
Commit | Line | Data |
---|---|---|
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 |
12 | typedef struct Property Property; |
13 | ||
14 | typedef struct PropertyInfo PropertyInfo; | |
aae9460e | 15 | |
b6b61144 GH |
16 | typedef struct CompatProperty CompatProperty; |
17 | ||
02e2da45 | 18 | typedef struct BusState BusState; |
4d6ae674 | 19 | |
0d936928 | 20 | typedef struct BusClass BusClass; |
10c4c98a | 21 | |
131ec1bd GH |
22 | enum DevState { |
23 | DEV_STATE_CREATED = 1, | |
24 | DEV_STATE_INITIALIZED, | |
25 | }; | |
26 | ||
75422b0d AS |
27 | enum { |
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 | 36 | typedef int (*qdev_initfn)(DeviceState *dev); |
6e008585 AL |
37 | typedef int (*qdev_event)(DeviceState *dev); |
38 | typedef void (*qdev_resetfn)(DeviceState *dev); | |
39 | ||
32fea402 AL |
40 | typedef 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; | |
0d936928 | 58 | const char *bus_type; |
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 | 63 | struct 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; |
4d2ffa08 JK |
77 | int instance_id_alias; |
78 | int alias_required_for_version; | |
02e2da45 PB |
79 | }; |
80 | ||
0d936928 AL |
81 | #define TYPE_BUS "bus" |
82 | #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS) | |
83 | #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS) | |
84 | #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS) | |
85 | ||
86 | struct BusClass { | |
87 | ObjectClass parent_class; | |
88 | ||
89 | /* FIXME first arg should be BusState */ | |
90 | void (*print_dev)(Monitor *mon, DeviceState *dev, int indent); | |
91 | char *(*get_dev_path)(DeviceState *dev); | |
c5788614 SW |
92 | /* |
93 | * This callback is used to create Open Firmware device path in accordance | |
94 | * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus | |
95 | * bindings can be found at http://playground.sun.com/1275/bindings/. | |
96 | */ | |
0d936928 AL |
97 | char *(*get_fw_dev_path)(DeviceState *dev); |
98 | int (*reset)(BusState *bus); | |
10c4c98a | 99 | }; |
02e2da45 | 100 | |
0866aca1 AL |
101 | typedef struct BusChild { |
102 | DeviceState *child; | |
103 | int index; | |
104 | QTAILQ_ENTRY(BusChild) sibling; | |
105 | } BusChild; | |
106 | ||
0d936928 AL |
107 | /** |
108 | * BusState: | |
109 | * @qom_allocated: Indicates whether the object was allocated by QOM. | |
110 | * @glib_allocated: Indicates whether the object was initialized in-place | |
111 | * yet is expected to be freed with g_free(). | |
112 | */ | |
02e2da45 | 113 | struct BusState { |
0d936928 | 114 | Object obj; |
02e2da45 PB |
115 | DeviceState *parent; |
116 | const char *name; | |
3418bd25 | 117 | int allow_hotplug; |
0d936928 AL |
118 | bool qom_allocated; |
119 | bool glib_allocated; | |
0866aca1 AL |
120 | int max_index; |
121 | QTAILQ_HEAD(ChildrenHead, BusChild) children; | |
72cf2d4f | 122 | QLIST_ENTRY(BusState) sibling; |
aae9460e PB |
123 | }; |
124 | ||
ee6847d1 GH |
125 | struct Property { |
126 | const char *name; | |
127 | PropertyInfo *info; | |
128 | int offset; | |
4f2d3d70 PB |
129 | uint8_t bitnr; |
130 | uint8_t qtype; | |
131 | int64_t defval; | |
ee6847d1 GH |
132 | }; |
133 | ||
ee6847d1 GH |
134 | struct PropertyInfo { |
135 | const char *name; | |
cafe5bdb | 136 | const char *legacy_name; |
1ce05125 | 137 | const char **enum_table; |
ee6847d1 GH |
138 | int (*parse)(DeviceState *dev, Property *prop, const char *str); |
139 | int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len); | |
57c9fafe AL |
140 | ObjectPropertyAccessor *get; |
141 | ObjectPropertyAccessor *set; | |
dd0ba250 | 142 | ObjectPropertyRelease *release; |
ee6847d1 GH |
143 | }; |
144 | ||
458fb679 | 145 | typedef struct GlobalProperty { |
b6b61144 GH |
146 | const char *driver; |
147 | const char *property; | |
148 | const char *value; | |
458fb679 GH |
149 | QTAILQ_ENTRY(GlobalProperty) next; |
150 | } GlobalProperty; | |
b6b61144 | 151 | |
aae9460e PB |
152 | /*** Board API. This should go away once we have a machine config file. ***/ |
153 | ||
02e2da45 | 154 | DeviceState *qdev_create(BusState *bus, const char *name); |
0bcdeda7 | 155 | DeviceState *qdev_try_create(BusState *bus, const char *name); |
ff952ba2 | 156 | int qdev_device_help(QemuOpts *opts); |
f31d07d1 | 157 | DeviceState *qdev_device_add(QemuOpts *opts); |
747bbdf7 | 158 | int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT; |
e23a1b33 | 159 | void qdev_init_nofail(DeviceState *dev); |
4d2ffa08 JK |
160 | void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, |
161 | int required_for_version); | |
56f9107e | 162 | void qdev_unplug(DeviceState *dev, Error **errp); |
02e2da45 | 163 | void qdev_free(DeviceState *dev); |
3418bd25 GH |
164 | int qdev_simple_unplug_cb(DeviceState *dev); |
165 | void qdev_machine_creation_done(void); | |
0ac8ef71 | 166 | bool qdev_machine_modified(void); |
aae9460e | 167 | |
aae9460e PB |
168 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n); |
169 | void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin); | |
170 | ||
02e2da45 | 171 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name); |
4d6ae674 | 172 | |
aae9460e PB |
173 | /*** Device API. ***/ |
174 | ||
aae9460e | 175 | /* Register device properties. */ |
067a3ddc | 176 | /* GPIO inputs also double as IRQ sinks. */ |
aae9460e PB |
177 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n); |
178 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n); | |
179 | ||
02e2da45 | 180 | BusState *qdev_get_parent_bus(DeviceState *dev); |
aae9460e | 181 | |
02e2da45 PB |
182 | /*** BUS API. ***/ |
183 | ||
a2ee6b4f IY |
184 | DeviceState *qdev_find_recursive(BusState *bus, const char *id); |
185 | ||
81699d8a AL |
186 | /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */ |
187 | typedef int (qbus_walkerfn)(BusState *bus, void *opaque); | |
188 | typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque); | |
189 | ||
0d936928 | 190 | void qbus_create_inplace(BusState *bus, const char *typename, |
cd739fb6 | 191 | DeviceState *parent, const char *name); |
0d936928 | 192 | BusState *qbus_create(const char *typename, DeviceState *parent, const char *name); |
81699d8a AL |
193 | /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion, |
194 | * < 0 if either devfn or busfn terminate walk somewhere in cursion, | |
195 | * 0 otherwise. */ | |
196 | int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, | |
197 | qbus_walkerfn *busfn, void *opaque); | |
198 | int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, | |
199 | qbus_walkerfn *busfn, void *opaque); | |
5af0a04b | 200 | void qdev_reset_all(DeviceState *dev); |
80376c3f IY |
201 | void qbus_reset_all_fn(void *opaque); |
202 | ||
131ec1bd | 203 | void qbus_free(BusState *bus); |
02e2da45 PB |
204 | |
205 | #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev) | |
206 | ||
ec990eb6 AL |
207 | /* This should go away once we get rid of the NULL bus hack */ |
208 | BusState *sysbus_get_default(void); | |
209 | ||
cae4956e GH |
210 | /*** monitor commands ***/ |
211 | ||
212 | void do_info_qtree(Monitor *mon); | |
f6c64e0e | 213 | void do_info_qdm(Monitor *mon); |
8bc27249 | 214 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data); |
17a38eaa | 215 | int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data); |
cae4956e | 216 | |
ee6847d1 GH |
217 | /*** qdev-properties.c ***/ |
218 | ||
d2364ee4 | 219 | extern PropertyInfo qdev_prop_bit; |
c7cc172d | 220 | extern PropertyInfo qdev_prop_uint8; |
ee6847d1 GH |
221 | extern PropertyInfo qdev_prop_uint16; |
222 | extern PropertyInfo qdev_prop_uint32; | |
316940b0 | 223 | extern PropertyInfo qdev_prop_int32; |
5a053d1f | 224 | extern PropertyInfo qdev_prop_uint64; |
6835678c | 225 | extern PropertyInfo qdev_prop_hex8; |
ee6847d1 | 226 | extern PropertyInfo qdev_prop_hex32; |
5a053d1f | 227 | extern PropertyInfo qdev_prop_hex64; |
59419663 | 228 | extern PropertyInfo qdev_prop_string; |
313feaab | 229 | extern PropertyInfo qdev_prop_chr; |
ee6847d1 GH |
230 | extern PropertyInfo qdev_prop_ptr; |
231 | extern PropertyInfo qdev_prop_macaddr; | |
4e4fa398 | 232 | extern PropertyInfo qdev_prop_losttickpolicy; |
8cd41745 | 233 | extern PropertyInfo qdev_prop_bios_chs_trans; |
14b41872 | 234 | extern PropertyInfo qdev_prop_drive; |
851bec09 GH |
235 | extern PropertyInfo qdev_prop_netdev; |
236 | extern PropertyInfo qdev_prop_vlan; | |
05cb5fe4 | 237 | extern PropertyInfo qdev_prop_pci_devfn; |
02fda01c | 238 | extern PropertyInfo qdev_prop_blocksize; |
679042f0 | 239 | extern PropertyInfo qdev_prop_pci_host_devaddr; |
ee6847d1 | 240 | |
cf12b95b GH |
241 | #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \ |
242 | .name = (_name), \ | |
243 | .info = &(_prop), \ | |
244 | .offset = offsetof(_state, _field) \ | |
245 | + type_check(_type,typeof_field(_state, _field)), \ | |
246 | } | |
247 | #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \ | |
248 | .name = (_name), \ | |
249 | .info = &(_prop), \ | |
250 | .offset = offsetof(_state, _field) \ | |
251 | + type_check(_type,typeof_field(_state, _field)), \ | |
4f2d3d70 PB |
252 | .qtype = QTYPE_QINT, \ |
253 | .defval = (_type)_defval, \ | |
cf12b95b | 254 | } |
d2364ee4 MT |
255 | #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \ |
256 | .name = (_name), \ | |
257 | .info = &(qdev_prop_bit), \ | |
258 | .bitnr = (_bit), \ | |
259 | .offset = offsetof(_state, _field) \ | |
260 | + type_check(uint32_t,typeof_field(_state, _field)), \ | |
4f2d3d70 PB |
261 | .qtype = QTYPE_QBOOL, \ |
262 | .defval = (bool)_defval, \ | |
d2364ee4 | 263 | } |
cf12b95b | 264 | |
c7cc172d JQ |
265 | #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \ |
266 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t) | |
cf12b95b GH |
267 | #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \ |
268 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t) | |
269 | #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \ | |
270 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t) | |
316940b0 GH |
271 | #define DEFINE_PROP_INT32(_n, _s, _f, _d) \ |
272 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t) | |
cf12b95b GH |
273 | #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \ |
274 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t) | |
6835678c JK |
275 | #define DEFINE_PROP_HEX8(_n, _s, _f, _d) \ |
276 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t) | |
cf12b95b GH |
277 | #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \ |
278 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t) | |
279 | #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \ | |
280 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t) | |
281 | #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \ | |
09f1bbcd | 282 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t) |
cf12b95b GH |
283 | |
284 | #define DEFINE_PROP_PTR(_n, _s, _f) \ | |
285 | DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*) | |
313feaab GH |
286 | #define DEFINE_PROP_CHR(_n, _s, _f) \ |
287 | DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*) | |
59419663 GH |
288 | #define DEFINE_PROP_STRING(_n, _s, _f) \ |
289 | DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*) | |
2ef924b4 | 290 | #define DEFINE_PROP_NETDEV(_n, _s, _f) \ |
4e68f7a0 | 291 | DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, NetClientState*) |
851bec09 | 292 | #define DEFINE_PROP_VLAN(_n, _s, _f) \ |
4e68f7a0 | 293 | DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, NetClientState*) |
f8b6cc00 MA |
294 | #define DEFINE_PROP_DRIVE(_n, _s, _f) \ |
295 | DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *) | |
cf12b95b | 296 | #define DEFINE_PROP_MACADDR(_n, _s, _f) \ |
1503fff3 | 297 | DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr) |
4e4fa398 JK |
298 | #define DEFINE_PROP_LOSTTICKPOLICY(_n, _s, _f, _d) \ |
299 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_losttickpolicy, \ | |
300 | LostTickPolicy) | |
8cd41745 MA |
301 | #define DEFINE_PROP_BIOS_CHS_TRANS(_n, _s, _f, _d) \ |
302 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_bios_chs_trans, int) | |
02fda01c SH |
303 | #define DEFINE_PROP_BLOCKSIZE(_n, _s, _f, _d) \ |
304 | DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_blocksize, uint16_t) | |
679042f0 AP |
305 | #define DEFINE_PROP_PCI_HOST_DEVADDR(_n, _s, _f) \ |
306 | DEFINE_PROP(_n, _s, _f, qdev_prop_pci_host_devaddr, PCIHostDeviceAddress) | |
cf12b95b GH |
307 | |
308 | #define DEFINE_PROP_END_OF_LIST() \ | |
309 | {} | |
310 | ||
ee6847d1 GH |
311 | /* Set properties between creation and init. */ |
312 | void *qdev_get_prop_ptr(DeviceState *dev, Property *prop); | |
313 | int qdev_prop_parse(DeviceState *dev, const char *name, const char *value); | |
f4594a3b | 314 | void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value); |
c7cc172d | 315 | void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value); |
ee6847d1 GH |
316 | void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value); |
317 | void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value); | |
316940b0 | 318 | void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value); |
5a053d1f | 319 | void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value); |
3b25597b | 320 | void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value); |
313feaab | 321 | void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value); |
4e68f7a0 | 322 | void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value); |
18846dee MA |
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); | |
1503fff3 | 325 | void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value); |
9b170e60 | 326 | void qdev_prop_set_enum(DeviceState *dev, const char *name, int value); |
ee6847d1 GH |
327 | /* FIXME: Remove opaque pointer properties. */ |
328 | void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value); | |
ee6847d1 | 329 | |
458fb679 GH |
330 | void qdev_prop_register_global_list(GlobalProperty *props); |
331 | void qdev_prop_set_globals(DeviceState *dev); | |
7db4c4e8 PB |
332 | void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev, |
333 | Property *prop, const char *value); | |
b6b61144 | 334 | |
1ca4d09a | 335 | char *qdev_get_fw_dev_path(DeviceState *dev); |
4be9f0d1 | 336 | |
a5296ca9 | 337 | /** |
ca2cc788 PB |
338 | * @qdev_property_add_static - add a @Property to a device referencing a |
339 | * field in a struct. | |
a5296ca9 | 340 | */ |
ca2cc788 | 341 | void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp); |
a5296ca9 | 342 | |
1de81d28 AL |
343 | /** |
344 | * @qdev_machine_init | |
345 | * | |
346 | * Initialize platform devices before machine init. This is a hack until full | |
347 | * support for composition is added. | |
348 | */ | |
349 | void qdev_machine_init(void); | |
350 | ||
94afdadc AL |
351 | /** |
352 | * @device_reset | |
353 | * | |
354 | * Reset a single device (by calling the reset method). | |
355 | */ | |
356 | void device_reset(DeviceState *dev); | |
357 | ||
4be9f0d1 AL |
358 | const VMStateDescription *qdev_get_vmsd(DeviceState *dev); |
359 | ||
360 | const char *qdev_fw_name(DeviceState *dev); | |
361 | ||
f05f6b4a PB |
362 | Object *qdev_get_machine(void); |
363 | ||
9fbe6127 AL |
364 | /* FIXME: make this a link<> */ |
365 | void qdev_set_parent_bus(DeviceState *dev, BusState *bus); | |
366 | ||
ee46d8a5 AL |
367 | extern int qdev_hotplug; |
368 | ||
09e5ab63 AL |
369 | char *qdev_get_dev_path(DeviceState *dev); |
370 | ||
aae9460e | 371 | #endif |