c2609b4fe93f7bca989be4f640ddac2586ee3dba
[qemu] / hw / qdev.h
1 #ifndef QDEV_H
2 #define QDEV_H
3
4 #include "hw.h"
5 #include "sysemu.h"
6 #include "sys-queue.h"
7 #include "qemu-char.h"
8 #include "qemu-option.h"
9
10 typedef struct Property Property;
11
12 typedef struct PropertyInfo PropertyInfo;
13
14 typedef struct CompatProperty CompatProperty;
15
16 typedef struct DeviceInfo DeviceInfo;
17
18 typedef struct BusState BusState;
19
20 typedef struct BusInfo BusInfo;
21
22 /* This structure should not be accessed directly.  We declare it here
23    so that it can be embedded in individual device state structures.  */
24 struct DeviceState {
25     const char *id;
26     DeviceInfo *info;
27     BusState *parent_bus;
28     int num_gpio_out;
29     qemu_irq *gpio_out;
30     int num_gpio_in;
31     qemu_irq *gpio_in;
32     LIST_HEAD(, BusState) child_bus;
33     int num_child_bus;
34     NICInfo *nd;
35     LIST_ENTRY(DeviceState) sibling;
36 };
37
38 typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
39 struct BusInfo {
40     const char *name;
41     size_t size;
42     bus_dev_printfn print_dev;
43     Property *props;
44 };
45
46 struct BusState {
47     DeviceState *parent;
48     BusInfo *info;
49     const char *name;
50     LIST_HEAD(, DeviceState) children;
51     LIST_ENTRY(BusState) sibling;
52 };
53
54 struct Property {
55     const char   *name;
56     PropertyInfo *info;
57     int          offset;
58     void         *defval;
59 };
60
61 enum PropertyType {
62     PROP_TYPE_UNSPEC = 0,
63     PROP_TYPE_UINT16,
64     PROP_TYPE_UINT32,
65     PROP_TYPE_INT32,
66     PROP_TYPE_UINT64,
67     PROP_TYPE_TADDR,
68     PROP_TYPE_MACADDR,
69     PROP_TYPE_DRIVE,
70     PROP_TYPE_CHR,
71     PROP_TYPE_PTR,
72 };
73
74 struct PropertyInfo {
75     const char *name;
76     size_t size;
77     enum PropertyType type;
78     int (*parse)(DeviceState *dev, Property *prop, const char *str);
79     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
80 };
81
82 struct CompatProperty {
83     const char *driver;
84     const char *property;
85     const char *value;
86 };
87
88 /*** Board API.  This should go away once we have a machine config file.  ***/
89
90 DeviceState *qdev_create(BusState *bus, const char *name);
91 DeviceState *qdev_device_add(QemuOpts *opts);
92 int qdev_init(DeviceState *dev);
93 void qdev_free(DeviceState *dev);
94
95 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
96 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
97
98 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
99
100 /*** Device API.  ***/
101
102 typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
103
104 struct DeviceInfo {
105     const char *name;
106     const char *alias;
107     const char *desc;
108     size_t size;
109     Property *props;
110     int no_user;
111
112     /* callbacks */
113     QEMUResetHandler *reset;
114
115     /* device state */
116     const VMStateDescription *vmsd;
117
118     /* Private to qdev / bus.  */
119     qdev_initfn init;
120     BusInfo *bus_info;
121     struct DeviceInfo *next;
122 };
123
124 void qdev_register(DeviceInfo *info);
125
126 /* Register device properties.  */
127 /* GPIO inputs also double as IRQ sinks.  */
128 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
129 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
130
131 CharDriverState *qdev_init_chardev(DeviceState *dev);
132
133 BusState *qdev_get_parent_bus(DeviceState *dev);
134
135 /* Convery from a base type to a parent type, with compile time checking.  */
136 #ifdef __GNUC__
137 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
138     char __attribute__((unused)) offset_must_be_zero[ \
139         -offsetof(type, field)]; \
140     container_of(dev, type, field);}))
141 #else
142 #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
143 #endif
144
145 /*** BUS API. ***/
146
147 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
148
149 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
150
151 /*** monitor commands ***/
152
153 void do_info_qtree(Monitor *mon);
154 void do_info_qdm(Monitor *mon);
155
156 /*** qdev-properties.c ***/
157
158 extern PropertyInfo qdev_prop_uint16;
159 extern PropertyInfo qdev_prop_uint32;
160 extern PropertyInfo qdev_prop_int32;
161 extern PropertyInfo qdev_prop_uint64;
162 extern PropertyInfo qdev_prop_hex32;
163 extern PropertyInfo qdev_prop_hex64;
164 extern PropertyInfo qdev_prop_chr;
165 extern PropertyInfo qdev_prop_ptr;
166 extern PropertyInfo qdev_prop_macaddr;
167 extern PropertyInfo qdev_prop_drive;
168 extern PropertyInfo qdev_prop_pci_devfn;
169
170 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
171         .name      = (_name),                                    \
172         .info      = &(_prop),                                   \
173         .offset    = offsetof(_state, _field)                    \
174             + type_check(_type,typeof_field(_state, _field)),    \
175         }
176 #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
177         .name      = (_name),                                           \
178         .info      = &(_prop),                                          \
179         .offset    = offsetof(_state, _field)                           \
180             + type_check(_type,typeof_field(_state, _field)),           \
181         .defval    = (_type[]) { _defval },                             \
182         }
183
184 #define DEFINE_PROP_UINT16(_n, _s, _f, _d)                      \
185     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
186 #define DEFINE_PROP_UINT32(_n, _s, _f, _d)                      \
187     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
188 #define DEFINE_PROP_INT32(_n, _s, _f, _d)                      \
189     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
190 #define DEFINE_PROP_UINT64(_n, _s, _f, _d)                      \
191     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
192 #define DEFINE_PROP_HEX32(_n, _s, _f, _d)                       \
193     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
194 #define DEFINE_PROP_HEX64(_n, _s, _f, _d)                       \
195     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
196 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d)                   \
197     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
198
199 #define DEFINE_PROP_PTR(_n, _s, _f)             \
200     DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
201 #define DEFINE_PROP_CHR(_n, _s, _f)             \
202     DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
203 #define DEFINE_PROP_DRIVE(_n, _s, _f)             \
204     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, DriveInfo*)
205 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
206     DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, uint8_t[6])
207
208 #define DEFINE_PROP_END_OF_LIST()               \
209     {}
210
211 /* Set properties between creation and init.  */
212 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
213 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
214 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
215 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
216 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
217 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
218 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
219 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
220 void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value);
221 /* FIXME: Remove opaque pointer properties.  */
222 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
223 void qdev_prop_set_defaults(DeviceState *dev, Property *props);
224
225 void qdev_prop_register_compat(CompatProperty *props);
226 void qdev_prop_set_compat(DeviceState *dev);
227
228 /* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
229 extern struct BusInfo system_bus_info;
230
231 #endif