Statistics
| Branch: | Revision:

root / hw / qdev.h @ ec990eb6

History | View | Annotate | Download (10.9 kB)

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

    
53
struct BusInfo {
54
    const char *name;
55
    size_t size;
56
    bus_dev_printfn print_dev;
57
    bus_get_dev_path get_dev_path;
58
    Property *props;
59
};
60

    
61
struct BusState {
62
    DeviceState *parent;
63
    BusInfo *info;
64
    const char *name;
65
    int allow_hotplug;
66
    int qdev_allocated;
67
    QLIST_HEAD(, DeviceState) children;
68
    QLIST_ENTRY(BusState) sibling;
69
};
70

    
71
struct Property {
72
    const char   *name;
73
    PropertyInfo *info;
74
    int          offset;
75
    int          bitnr;
76
    void         *defval;
77
};
78

    
79
enum PropertyType {
80
    PROP_TYPE_UNSPEC = 0,
81
    PROP_TYPE_UINT8,
82
    PROP_TYPE_UINT16,
83
    PROP_TYPE_UINT32,
84
    PROP_TYPE_INT32,
85
    PROP_TYPE_UINT64,
86
    PROP_TYPE_TADDR,
87
    PROP_TYPE_MACADDR,
88
    PROP_TYPE_DRIVE,
89
    PROP_TYPE_CHR,
90
    PROP_TYPE_STRING,
91
    PROP_TYPE_NETDEV,
92
    PROP_TYPE_VLAN,
93
    PROP_TYPE_PTR,
94
    PROP_TYPE_BIT,
95
};
96

    
97
struct PropertyInfo {
98
    const char *name;
99
    size_t size;
100
    enum PropertyType type;
101
    int (*parse)(DeviceState *dev, Property *prop, const char *str);
102
    int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
103
    void (*free)(DeviceState *dev, Property *prop);
104
};
105

    
106
typedef struct GlobalProperty {
107
    const char *driver;
108
    const char *property;
109
    const char *value;
110
    QTAILQ_ENTRY(GlobalProperty) next;
111
} GlobalProperty;
112

    
113
/*** Board API.  This should go away once we have a machine config file.  ***/
114

    
115
DeviceState *qdev_create(BusState *bus, const char *name);
116
int qdev_device_help(QemuOpts *opts);
117
DeviceState *qdev_device_add(QemuOpts *opts);
118
int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
119
void qdev_init_nofail(DeviceState *dev);
120
void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
121
                                 int required_for_version);
122
int qdev_unplug(DeviceState *dev);
123
void qdev_free(DeviceState *dev);
124
int qdev_simple_unplug_cb(DeviceState *dev);
125
void qdev_machine_creation_done(void);
126

    
127
qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
128
void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
129

    
130
BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type);
131

    
132
BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
133

    
134
/*** Device API.  ***/
135

    
136
typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
137
typedef int (*qdev_event)(DeviceState *dev);
138
typedef void (*qdev_resetfn)(DeviceState *dev);
139

    
140
struct DeviceInfo {
141
    const char *name;
142
    const char *alias;
143
    const char *desc;
144
    size_t size;
145
    Property *props;
146
    int no_user;
147

    
148
    /* callbacks */
149
    qdev_resetfn reset;
150

    
151
    /* device state */
152
    const VMStateDescription *vmsd;
153

    
154
    /* Private to qdev / bus.  */
155
    qdev_initfn init;
156
    qdev_event unplug;
157
    qdev_event exit;
158
    BusInfo *bus_info;
159
    struct DeviceInfo *next;
160
};
161
extern DeviceInfo *device_info_list;
162

    
163
void qdev_register(DeviceInfo *info);
164

    
165
/* Register device properties.  */
166
/* GPIO inputs also double as IRQ sinks.  */
167
void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
168
void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
169

    
170
CharDriverState *qdev_init_chardev(DeviceState *dev);
171

    
172
BusState *qdev_get_parent_bus(DeviceState *dev);
173

    
174
/*** BUS API. ***/
175

    
176
/* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
177
typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
178
typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
179

    
180
void qbus_create_inplace(BusState *bus, BusInfo *info,
181
                         DeviceState *parent, const char *name);
182
BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
183
/* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
184
 *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
185
 *           0 otherwise. */
186
int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
187
                       qbus_walkerfn *busfn, void *opaque);
188
int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
189
                       qbus_walkerfn *busfn, void *opaque);
190
void qbus_reset_all(BusState *bus);
191
void qbus_free(BusState *bus);
192

    
193
#define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
194

    
195
/* This should go away once we get rid of the NULL bus hack */
196
BusState *sysbus_get_default(void);
197

    
198
/*** monitor commands ***/
199

    
200
void do_info_qtree(Monitor *mon);
201
void do_info_qdm(Monitor *mon);
202
int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
203
int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
204

    
205
/*** qdev-properties.c ***/
206

    
207
extern PropertyInfo qdev_prop_bit;
208
extern PropertyInfo qdev_prop_uint8;
209
extern PropertyInfo qdev_prop_uint16;
210
extern PropertyInfo qdev_prop_uint32;
211
extern PropertyInfo qdev_prop_int32;
212
extern PropertyInfo qdev_prop_uint64;
213
extern PropertyInfo qdev_prop_hex32;
214
extern PropertyInfo qdev_prop_hex64;
215
extern PropertyInfo qdev_prop_string;
216
extern PropertyInfo qdev_prop_chr;
217
extern PropertyInfo qdev_prop_ptr;
218
extern PropertyInfo qdev_prop_macaddr;
219
extern PropertyInfo qdev_prop_drive;
220
extern PropertyInfo qdev_prop_netdev;
221
extern PropertyInfo qdev_prop_vlan;
222
extern PropertyInfo qdev_prop_pci_devfn;
223

    
224
#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
225
        .name      = (_name),                                    \
226
        .info      = &(_prop),                                   \
227
        .offset    = offsetof(_state, _field)                    \
228
            + type_check(_type,typeof_field(_state, _field)),    \
229
        }
230
#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
231
        .name      = (_name),                                           \
232
        .info      = &(_prop),                                          \
233
        .offset    = offsetof(_state, _field)                           \
234
            + type_check(_type,typeof_field(_state, _field)),           \
235
        .defval    = (_type[]) { _defval },                             \
236
        }
237
#define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) {  \
238
        .name      = (_name),                                    \
239
        .info      = &(qdev_prop_bit),                           \
240
        .bitnr    = (_bit),                                      \
241
        .offset    = offsetof(_state, _field)                    \
242
            + type_check(uint32_t,typeof_field(_state, _field)), \
243
        .defval    = (bool[]) { (_defval) },                     \
244
        }
245

    
246
#define DEFINE_PROP_UINT8(_n, _s, _f, _d)                       \
247
    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
248
#define DEFINE_PROP_UINT16(_n, _s, _f, _d)                      \
249
    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
250
#define DEFINE_PROP_UINT32(_n, _s, _f, _d)                      \
251
    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
252
#define DEFINE_PROP_INT32(_n, _s, _f, _d)                      \
253
    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
254
#define DEFINE_PROP_UINT64(_n, _s, _f, _d)                      \
255
    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
256
#define DEFINE_PROP_HEX32(_n, _s, _f, _d)                       \
257
    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
258
#define DEFINE_PROP_HEX64(_n, _s, _f, _d)                       \
259
    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
260
#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d)                   \
261
    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
262

    
263
#define DEFINE_PROP_PTR(_n, _s, _f)             \
264
    DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
265
#define DEFINE_PROP_CHR(_n, _s, _f)             \
266
    DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
267
#define DEFINE_PROP_STRING(_n, _s, _f)             \
268
    DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
269
#define DEFINE_PROP_NETDEV(_n, _s, _f)             \
270
    DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
271
#define DEFINE_PROP_VLAN(_n, _s, _f)             \
272
    DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
273
#define DEFINE_PROP_DRIVE(_n, _s, _f) \
274
    DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
275
#define DEFINE_PROP_MACADDR(_n, _s, _f)         \
276
    DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
277

    
278
#define DEFINE_PROP_END_OF_LIST()               \
279
    {}
280

    
281
/* Set properties between creation and init.  */
282
void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
283
int qdev_prop_exists(DeviceState *dev, const char *name);
284
int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
285
void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
286
void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
287
void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
288
void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
289
void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
290
void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
291
void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
292
void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
293
void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
294
void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
295
void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
296
int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
297
void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
298
void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
299
/* FIXME: Remove opaque pointer properties.  */
300
void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
301
void qdev_prop_set_defaults(DeviceState *dev, Property *props);
302

    
303
void qdev_prop_register_global_list(GlobalProperty *props);
304
void qdev_prop_set_globals(DeviceState *dev);
305

    
306
/* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
307
extern struct BusInfo system_bus_info;
308

    
309
#endif