]> git.proxmox.com Git - qemu.git/blame - hw/qdev.h
qbus: register reset handler for qbus whose parent is NULL
[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"
aae9460e 8
ee6847d1
GH
9typedef struct Property Property;
10
11typedef struct PropertyInfo PropertyInfo;
aae9460e 12
b6b61144
GH
13typedef struct CompatProperty CompatProperty;
14
ee6847d1 15typedef struct DeviceInfo DeviceInfo;
aae9460e 16
02e2da45 17typedef struct BusState BusState;
4d6ae674 18
10c4c98a
GH
19typedef struct BusInfo BusInfo;
20
131ec1bd
GH
21enum DevState {
22 DEV_STATE_CREATED = 1,
23 DEV_STATE_INITIALIZED,
24};
25
75422b0d
AS
26enum {
27 DEV_NVECTORS_UNSPECIFIED = -1,
28};
29
aae9460e
PB
30/* This structure should not be accessed directly. We declare it here
31 so that it can be embedded in individual device state structures. */
02e2da45 32struct DeviceState {
f31d07d1 33 const char *id;
131ec1bd 34 enum DevState state;
ef80b466 35 QemuOpts *opts;
3418bd25 36 int hotplugged;
042f84d0 37 DeviceInfo *info;
02e2da45 38 BusState *parent_bus;
aae9460e
PB
39 int num_gpio_out;
40 qemu_irq *gpio_out;
41 int num_gpio_in;
42 qemu_irq *gpio_in;
72cf2d4f 43 QLIST_HEAD(, BusState) child_bus;
d271de9f 44 int num_child_bus;
72cf2d4f 45 QLIST_ENTRY(DeviceState) sibling;
4d2ffa08
JK
46 int instance_id_alias;
47 int alias_required_for_version;
02e2da45
PB
48};
49
10c4c98a 50typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
6772b936 51typedef char *(*bus_get_dev_path)(DeviceState *dev);
21150814
GN
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);
b4694b7c 58typedef int (qbus_resetfn)(BusState *bus);
6772b936 59
10c4c98a
GH
60struct BusInfo {
61 const char *name;
62 size_t size;
63 bus_dev_printfn print_dev;
6772b936 64 bus_get_dev_path get_dev_path;
21150814 65 bus_get_fw_dev_path get_fw_dev_path;
b4694b7c 66 qbus_resetfn *reset;
ee6847d1 67 Property *props;
10c4c98a 68};
02e2da45
PB
69
70struct BusState {
71 DeviceState *parent;
10c4c98a 72 BusInfo *info;
02e2da45 73 const char *name;
3418bd25 74 int allow_hotplug;
cd739fb6 75 int qdev_allocated;
72cf2d4f
BS
76 QLIST_HEAD(, DeviceState) children;
77 QLIST_ENTRY(BusState) sibling;
aae9460e
PB
78};
79
ee6847d1
GH
80struct Property {
81 const char *name;
82 PropertyInfo *info;
83 int offset;
d2364ee4 84 int bitnr;
ee6847d1
GH
85 void *defval;
86};
87
88enum PropertyType {
89 PROP_TYPE_UNSPEC = 0,
c7cc172d 90 PROP_TYPE_UINT8,
ee6847d1
GH
91 PROP_TYPE_UINT16,
92 PROP_TYPE_UINT32,
316940b0 93 PROP_TYPE_INT32,
5a053d1f 94 PROP_TYPE_UINT64,
ee6847d1
GH
95 PROP_TYPE_TADDR,
96 PROP_TYPE_MACADDR,
14b41872 97 PROP_TYPE_DRIVE,
313feaab 98 PROP_TYPE_CHR,
59419663 99 PROP_TYPE_STRING,
2ef924b4 100 PROP_TYPE_NETDEV,
851bec09 101 PROP_TYPE_VLAN,
ee6847d1 102 PROP_TYPE_PTR,
d2364ee4 103 PROP_TYPE_BIT,
ee6847d1
GH
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);
d21357df 112 void (*free)(DeviceState *dev, Property *prop);
ee6847d1
GH
113};
114
458fb679 115typedef struct GlobalProperty {
b6b61144
GH
116 const char *driver;
117 const char *property;
118 const char *value;
458fb679
GH
119 QTAILQ_ENTRY(GlobalProperty) next;
120} GlobalProperty;
b6b61144 121
aae9460e
PB
122/*** Board API. This should go away once we have a machine config file. ***/
123
02e2da45 124DeviceState *qdev_create(BusState *bus, const char *name);
ff952ba2 125int qdev_device_help(QemuOpts *opts);
f31d07d1 126DeviceState *qdev_device_add(QemuOpts *opts);
747bbdf7 127int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
e23a1b33 128void qdev_init_nofail(DeviceState *dev);
4d2ffa08
JK
129void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
130 int required_for_version);
3418bd25 131int qdev_unplug(DeviceState *dev);
02e2da45 132void qdev_free(DeviceState *dev);
3418bd25
GH
133int qdev_simple_unplug_cb(DeviceState *dev);
134void qdev_machine_creation_done(void);
aae9460e 135
aae9460e
PB
136qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
137void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
138
7b370f51
MA
139BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type);
140
02e2da45 141BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
4d6ae674 142
aae9460e
PB
143/*** Device API. ***/
144
81a322d4 145typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
3418bd25 146typedef int (*qdev_event)(DeviceState *dev);
7f23f812 147typedef void (*qdev_resetfn)(DeviceState *dev);
aae9460e 148
02e2da45 149struct DeviceInfo {
074f2fff 150 const char *name;
779206de 151 const char *fw_name;
3320e56e
GH
152 const char *alias;
153 const char *desc;
074f2fff 154 size_t size;
ee6847d1 155 Property *props;
3320e56e 156 int no_user;
074f2fff 157
959f733a 158 /* callbacks */
7f23f812 159 qdev_resetfn reset;
959f733a 160
391a079e
GH
161 /* device state */
162 const VMStateDescription *vmsd;
163
074f2fff 164 /* Private to qdev / bus. */
02e2da45 165 qdev_initfn init;
3418bd25
GH
166 qdev_event unplug;
167 qdev_event exit;
10c4c98a 168 BusInfo *bus_info;
042f84d0 169 struct DeviceInfo *next;
02e2da45 170};
0958b4cc 171extern DeviceInfo *device_info_list;
02e2da45 172
074f2fff 173void qdev_register(DeviceInfo *info);
aae9460e
PB
174
175/* Register device properties. */
067a3ddc 176/* GPIO inputs also double as IRQ sinks. */
aae9460e
PB
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
02e2da45 182BusState *qdev_get_parent_bus(DeviceState *dev);
aae9460e 183
02e2da45
PB
184/*** BUS API. ***/
185
81699d8a
AL
186/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
187typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
188typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
189
cd739fb6
GH
190void qbus_create_inplace(BusState *bus, BusInfo *info,
191 DeviceState *parent, const char *name);
10c4c98a 192BusState *qbus_create(BusInfo *info, 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. */
196int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
197 qbus_walkerfn *busfn, void *opaque);
198int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
199 qbus_walkerfn *busfn, void *opaque);
5af0a04b 200void qdev_reset_all(DeviceState *dev);
ec990eb6 201void qbus_reset_all(BusState *bus);
80376c3f
IY
202void qbus_reset_all_fn(void *opaque);
203
131ec1bd 204void qbus_free(BusState *bus);
02e2da45
PB
205
206#define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
207
ec990eb6
AL
208/* This should go away once we get rid of the NULL bus hack */
209BusState *sysbus_get_default(void);
210
cae4956e
GH
211/*** monitor commands ***/
212
213void do_info_qtree(Monitor *mon);
f6c64e0e 214void do_info_qdm(Monitor *mon);
8bc27249 215int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
17a38eaa 216int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
cae4956e 217
ee6847d1
GH
218/*** qdev-properties.c ***/
219
d2364ee4 220extern PropertyInfo qdev_prop_bit;
c7cc172d 221extern PropertyInfo qdev_prop_uint8;
ee6847d1
GH
222extern PropertyInfo qdev_prop_uint16;
223extern PropertyInfo qdev_prop_uint32;
316940b0 224extern PropertyInfo qdev_prop_int32;
5a053d1f 225extern PropertyInfo qdev_prop_uint64;
ee6847d1 226extern PropertyInfo qdev_prop_hex32;
5a053d1f 227extern PropertyInfo qdev_prop_hex64;
59419663 228extern PropertyInfo qdev_prop_string;
313feaab 229extern PropertyInfo qdev_prop_chr;
ee6847d1
GH
230extern PropertyInfo qdev_prop_ptr;
231extern PropertyInfo qdev_prop_macaddr;
14b41872 232extern PropertyInfo qdev_prop_drive;
851bec09
GH
233extern PropertyInfo qdev_prop_netdev;
234extern PropertyInfo qdev_prop_vlan;
05cb5fe4 235extern PropertyInfo qdev_prop_pci_devfn;
ee6847d1 236
cf12b95b
GH
237#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
238 .name = (_name), \
239 .info = &(_prop), \
240 .offset = offsetof(_state, _field) \
241 + type_check(_type,typeof_field(_state, _field)), \
242 }
243#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
244 .name = (_name), \
245 .info = &(_prop), \
246 .offset = offsetof(_state, _field) \
247 + type_check(_type,typeof_field(_state, _field)), \
248 .defval = (_type[]) { _defval }, \
249 }
d2364ee4
MT
250#define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
251 .name = (_name), \
252 .info = &(qdev_prop_bit), \
253 .bitnr = (_bit), \
254 .offset = offsetof(_state, _field) \
255 + type_check(uint32_t,typeof_field(_state, _field)), \
256 .defval = (bool[]) { (_defval) }, \
257 }
cf12b95b 258
c7cc172d
JQ
259#define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
260 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
cf12b95b
GH
261#define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
262 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
263#define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
264 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
316940b0
GH
265#define DEFINE_PROP_INT32(_n, _s, _f, _d) \
266 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
cf12b95b
GH
267#define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
268 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
269#define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
270 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
271#define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
272 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
273#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
274 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
275
276#define DEFINE_PROP_PTR(_n, _s, _f) \
277 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
313feaab
GH
278#define DEFINE_PROP_CHR(_n, _s, _f) \
279 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
59419663
GH
280#define DEFINE_PROP_STRING(_n, _s, _f) \
281 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
2ef924b4
GH
282#define DEFINE_PROP_NETDEV(_n, _s, _f) \
283 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
851bec09
GH
284#define DEFINE_PROP_VLAN(_n, _s, _f) \
285 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
f8b6cc00
MA
286#define DEFINE_PROP_DRIVE(_n, _s, _f) \
287 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
cf12b95b 288#define DEFINE_PROP_MACADDR(_n, _s, _f) \
1503fff3 289 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
cf12b95b
GH
290
291#define DEFINE_PROP_END_OF_LIST() \
292 {}
293
ee6847d1
GH
294/* Set properties between creation and init. */
295void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
d8ed79ae 296int qdev_prop_exists(DeviceState *dev, const char *name);
ee6847d1
GH
297int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
298void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
f4594a3b 299void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
c7cc172d 300void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
ee6847d1
GH
301void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
302void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
316940b0 303void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
5a053d1f 304void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
cc984673 305void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
313feaab 306void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
2ef924b4 307void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
851bec09 308void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
18846dee
MA
309int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
310void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
1503fff3 311void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
ee6847d1
GH
312/* FIXME: Remove opaque pointer properties. */
313void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
314void qdev_prop_set_defaults(DeviceState *dev, Property *props);
315
458fb679
GH
316void qdev_prop_register_global_list(GlobalProperty *props);
317void qdev_prop_set_globals(DeviceState *dev);
b6b61144 318
779206de
GN
319static inline const char *qdev_fw_name(DeviceState *dev)
320{
321 return dev->info->fw_name ? : dev->info->alias ? : dev->info->name;
322}
323
1ca4d09a 324char *qdev_get_fw_dev_path(DeviceState *dev);
a9ff9df1
BS
325/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
326extern struct BusInfo system_bus_info;
327
aae9460e 328#endif