xref: /illumos-kvm-cmd/hw/qdev.h (revision 68396ea9)
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  * 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  */
57 typedef char *(*bus_get_fw_dev_path)(DeviceState *dev);
58 typedef int (qbus_resetfn)(BusState *bus);
59 
60 struct BusInfo {
61     const char *name;
62     size_t size;
63     bus_dev_printfn print_dev;
64     bus_get_dev_path get_dev_path;
65     bus_get_fw_dev_path get_fw_dev_path;
66     qbus_resetfn *reset;
67     Property *props;
68 };
69 
70 struct BusState {
71     DeviceState *parent;
72     BusInfo *info;
73     const char *name;
74     int allow_hotplug;
75     int qdev_allocated;
76     QLIST_HEAD(, DeviceState) children;
77     QLIST_ENTRY(BusState) sibling;
78 };
79 
80 struct Property {
81     const char   *name;
82     PropertyInfo *info;
83     int          offset;
84     int          bitnr;
85     void         *defval;
86 };
87 
88 enum PropertyType {
89     PROP_TYPE_UNSPEC = 0,
90     PROP_TYPE_UINT8,
91     PROP_TYPE_UINT16,
92     PROP_TYPE_UINT32,
93     PROP_TYPE_INT32,
94     PROP_TYPE_UINT64,
95     PROP_TYPE_TADDR,
96     PROP_TYPE_MACADDR,
97     PROP_TYPE_DRIVE,
98     PROP_TYPE_CHR,
99     PROP_TYPE_STRING,
100     PROP_TYPE_NETDEV,
101     PROP_TYPE_VLAN,
102     PROP_TYPE_PTR,
103     PROP_TYPE_BIT,
104 };
105 
106 struct 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);
112     void (*free)(DeviceState *dev, Property *prop);
113 };
114 
115 typedef struct GlobalProperty {
116     const char *driver;
117     const char *property;
118     const char *value;
119     QTAILQ_ENTRY(GlobalProperty) next;
120 } GlobalProperty;
121 
122 /*** Board API.  This should go away once we have a machine config file.  ***/
123 
124 DeviceState *qdev_create(BusState *bus, const char *name);
125 int qdev_device_help(QemuOpts *opts);
126 DeviceState *qdev_device_add(QemuOpts *opts);
127 int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
128 void qdev_init_nofail(DeviceState *dev);
129 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
130                                  int required_for_version);
131 int qdev_unplug(DeviceState *dev);
132 void qdev_free(DeviceState *dev);
133 int qdev_simple_unplug_cb(DeviceState *dev);
134 void qdev_machine_creation_done(void);
135 bool qdev_machine_modified(void);
136 
137 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
138 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
139 
140 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
141 
142 /*** Device API.  ***/
143 
144 typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
145 typedef int (*qdev_event)(DeviceState *dev);
146 typedef void (*qdev_resetfn)(DeviceState *dev);
147 
148 struct DeviceInfo {
149     const char *name;
150     const char *fw_name;
151     const char *alias;
152     const char *desc;
153     size_t size;
154     Property *props;
155     int no_user;
156 
157     /* callbacks */
158     qdev_resetfn reset;
159 
160     /* device state */
161     const VMStateDescription *vmsd;
162 
163     /* Private to qdev / bus.  */
164     qdev_initfn init;
165     qdev_event unplug;
166     qdev_event exit;
167     BusInfo *bus_info;
168     struct DeviceInfo *next;
169 };
170 extern DeviceInfo *device_info_list;
171 
172 void qdev_register(DeviceInfo *info);
173 
174 /* Register device properties.  */
175 /* GPIO inputs also double as IRQ sinks.  */
176 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
177 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
178 
179 CharDriverState *qdev_init_chardev(DeviceState *dev);
180 
181 BusState *qdev_get_parent_bus(DeviceState *dev);
182 
183 /*** BUS API. ***/
184 
185 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
186 
187 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
188 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
189 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
190 
191 void qbus_create_inplace(BusState *bus, BusInfo *info,
192                          DeviceState *parent, const char *name);
193 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
194 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
195  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
196  *           0 otherwise. */
197 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
198                        qbus_walkerfn *busfn, void *opaque);
199 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
200                        qbus_walkerfn *busfn, void *opaque);
201 void qdev_reset_all(DeviceState *dev);
202 void qbus_reset_all_fn(void *opaque);
203 
204 void qbus_free(BusState *bus);
205 
206 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
207 
208 /* This should go away once we get rid of the NULL bus hack */
209 BusState *sysbus_get_default(void);
210 
211 /*** monitor commands ***/
212 
213 void do_info_qtree(Monitor *mon);
214 void do_info_qdm(Monitor *mon);
215 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
216 int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
217 
218 /*** qdev-properties.c ***/
219 
220 extern PropertyInfo qdev_prop_bit;
221 extern PropertyInfo qdev_prop_uint8;
222 extern PropertyInfo qdev_prop_uint16;
223 extern PropertyInfo qdev_prop_uint32;
224 extern PropertyInfo qdev_prop_int32;
225 extern PropertyInfo qdev_prop_uint64;
226 extern PropertyInfo qdev_prop_hex32;
227 extern PropertyInfo qdev_prop_hex64;
228 extern PropertyInfo qdev_prop_string;
229 extern PropertyInfo qdev_prop_chr;
230 extern PropertyInfo qdev_prop_ptr;
231 extern PropertyInfo qdev_prop_macaddr;
232 extern PropertyInfo qdev_prop_drive;
233 extern PropertyInfo qdev_prop_netdev;
234 extern PropertyInfo qdev_prop_vlan;
235 extern PropertyInfo qdev_prop_pci_devfn;
236 
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         }
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         }
258 
259 #define DEFINE_PROP_UINT8(_n, _s, _f, _d)                       \
260     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
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)
265 #define DEFINE_PROP_INT32(_n, _s, _f, _d)                      \
266     DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
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*)
278 #define DEFINE_PROP_CHR(_n, _s, _f)             \
279     DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
280 #define DEFINE_PROP_STRING(_n, _s, _f)             \
281     DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
282 #define DEFINE_PROP_NETDEV(_n, _s, _f)             \
283     DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
284 #define DEFINE_PROP_VLAN(_n, _s, _f)             \
285     DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
286 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
287     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
288 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
289     DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
290 
291 #define DEFINE_PROP_END_OF_LIST()               \
292     {}
293 
294 /* Set properties between creation and init.  */
295 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
296 int qdev_prop_exists(DeviceState *dev, const char *name);
297 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
298 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
299 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
300 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
301 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
302 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
303 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
304 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
305 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
306 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
307 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
308 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
309 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
310 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
311 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
312 /* FIXME: Remove opaque pointer properties.  */
313 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
314 void qdev_prop_set_defaults(DeviceState *dev, Property *props);
315 
316 void qdev_prop_register_global_list(GlobalProperty *props);
317 void qdev_prop_set_globals(DeviceState *dev);
318 
qdev_fw_name(DeviceState * dev)319 static inline const char *qdev_fw_name(DeviceState *dev)
320 {
321     return dev->info->fw_name ? : dev->info->alias ? : dev->info->name;
322 }
323 
324 char *qdev_get_fw_dev_path(DeviceState *dev);
325 /* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
326 extern struct BusInfo system_bus_info;
327 
328 #endif
329