Statistics
| Branch: | Revision:

root / hw / qdev.h @ 81699d8a

History | View | Annotate | Download (10.8 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_free(BusState *bus);
191

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

    
194
/*** monitor commands ***/
195

    
196
void do_info_qtree(Monitor *mon);
197
void do_info_qdm(Monitor *mon);
198
int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
199
int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
200

    
201
/*** qdev-properties.c ***/
202

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

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

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

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

    
274
#define DEFINE_PROP_END_OF_LIST()               \
275
    {}
276

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

    
299
void qdev_prop_register_global_list(GlobalProperty *props);
300
void qdev_prop_set_globals(DeviceState *dev);
301

    
302
/* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
303
extern struct BusInfo system_bus_info;
304

    
305
#endif