]> git.proxmox.com Git - qemu.git/blob - hw/qdev.h
vga: improve VGA logic
[qemu.git] / hw / qdev.h
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 #include "qapi/qapi-visit-core.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 enum DevState {
23 DEV_STATE_CREATED = 1,
24 DEV_STATE_INITIALIZED,
25 };
26
27 enum {
28 DEV_NVECTORS_UNSPECIFIED = -1,
29 };
30
31 /**
32 * @DevicePropertyAccessor - called when trying to get/set a property
33 *
34 * @dev the device that owns the property
35 * @v the visitor that contains the property data
36 * @opaque the device property opaque
37 * @name the name of the property
38 * @errp a pointer to an Error that is filled if getting/setting fails.
39 */
40 typedef void (DevicePropertyAccessor)(DeviceState *dev,
41 Visitor *v,
42 void *opaque,
43 const char *name,
44 Error **errp);
45
46 /**
47 * @DevicePropertyRelease - called when a property is removed from a device
48 *
49 * @dev the device that owns the property
50 * @name the name of the property
51 * @opaque the opaque registered with the property
52 */
53 typedef void (DevicePropertyRelease)(DeviceState *dev,
54 const char *name,
55 void *opaque);
56
57 typedef struct DeviceProperty
58 {
59 gchar *name;
60 gchar *type;
61 DevicePropertyAccessor *get;
62 DevicePropertyAccessor *set;
63 DevicePropertyRelease *release;
64 void *opaque;
65
66 QTAILQ_ENTRY(DeviceProperty) node;
67 } DeviceProperty;
68
69 /* This structure should not be accessed directly. We declare it here
70 so that it can be embedded in individual device state structures. */
71 struct DeviceState {
72 const char *id;
73 enum DevState state;
74 QemuOpts *opts;
75 int hotplugged;
76 DeviceInfo *info;
77 BusState *parent_bus;
78 int num_gpio_out;
79 qemu_irq *gpio_out;
80 int num_gpio_in;
81 qemu_irq *gpio_in;
82 QLIST_HEAD(, BusState) child_bus;
83 int num_child_bus;
84 QTAILQ_ENTRY(DeviceState) sibling;
85 int instance_id_alias;
86 int alias_required_for_version;
87
88 /**
89 * This tracks the number of references between devices. See @qdev_ref for
90 * more information.
91 */
92 uint32_t ref;
93
94 QTAILQ_HEAD(, DeviceProperty) properties;
95
96 /* Do not, under any circumstance, use this parent link below anywhere
97 * outside of qdev.c. You have been warned. */
98 DeviceState *parent;
99 };
100
101 typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent);
102 typedef char *(*bus_get_dev_path)(DeviceState *dev);
103 /*
104 * This callback is used to create Open Firmware device path in accordance with
105 * OF spec http://forthworks.com/standards/of1275.pdf. Indicidual bus bindings
106 * can be found here http://playground.sun.com/1275/bindings/.
107 */
108 typedef char *(*bus_get_fw_dev_path)(DeviceState *dev);
109 typedef int (qbus_resetfn)(BusState *bus);
110
111 struct BusInfo {
112 const char *name;
113 size_t size;
114 bus_dev_printfn print_dev;
115 bus_get_dev_path get_dev_path;
116 bus_get_fw_dev_path get_fw_dev_path;
117 qbus_resetfn *reset;
118 Property *props;
119 };
120
121 struct BusState {
122 DeviceState *parent;
123 BusInfo *info;
124 const char *name;
125 int allow_hotplug;
126 int qdev_allocated;
127 QTAILQ_HEAD(ChildrenHead, DeviceState) children;
128 QLIST_ENTRY(BusState) sibling;
129 };
130
131 struct Property {
132 const char *name;
133 PropertyInfo *info;
134 int offset;
135 int bitnr;
136 void *defval;
137 };
138
139 enum PropertyType {
140 PROP_TYPE_UNSPEC = 0,
141 PROP_TYPE_UINT8,
142 PROP_TYPE_UINT16,
143 PROP_TYPE_UINT32,
144 PROP_TYPE_INT32,
145 PROP_TYPE_UINT64,
146 PROP_TYPE_TADDR,
147 PROP_TYPE_MACADDR,
148 PROP_TYPE_DRIVE,
149 PROP_TYPE_CHR,
150 PROP_TYPE_STRING,
151 PROP_TYPE_NETDEV,
152 PROP_TYPE_VLAN,
153 PROP_TYPE_PTR,
154 PROP_TYPE_BIT,
155 };
156
157 struct PropertyInfo {
158 const char *name;
159 const char *legacy_name;
160 size_t size;
161 enum PropertyType type;
162 int64_t min;
163 int64_t max;
164 int (*parse)(DeviceState *dev, Property *prop, const char *str);
165 int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
166 void (*free)(DeviceState *dev, Property *prop);
167 DevicePropertyAccessor *get;
168 DevicePropertyAccessor *set;
169 };
170
171 typedef struct GlobalProperty {
172 const char *driver;
173 const char *property;
174 const char *value;
175 QTAILQ_ENTRY(GlobalProperty) next;
176 } GlobalProperty;
177
178 /*** Board API. This should go away once we have a machine config file. ***/
179
180 DeviceState *qdev_create(BusState *bus, const char *name);
181 DeviceState *qdev_try_create(BusState *bus, const char *name);
182 bool qdev_exists(const char *name);
183 int qdev_device_help(QemuOpts *opts);
184 DeviceState *qdev_device_add(QemuOpts *opts);
185 int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
186 void qdev_init_nofail(DeviceState *dev);
187 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
188 int required_for_version);
189 int qdev_unplug(DeviceState *dev);
190 void qdev_free(DeviceState *dev);
191 int qdev_simple_unplug_cb(DeviceState *dev);
192 void qdev_machine_creation_done(void);
193 bool qdev_machine_modified(void);
194
195 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
196 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
197
198 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
199
200 /*** Device API. ***/
201
202 typedef int (*qdev_initfn)(DeviceState *dev, DeviceInfo *info);
203 typedef int (*qdev_event)(DeviceState *dev);
204 typedef void (*qdev_resetfn)(DeviceState *dev);
205
206 struct DeviceInfo {
207 const char *name;
208 const char *fw_name;
209 const char *alias;
210 const char *desc;
211 size_t size;
212 Property *props;
213 int no_user;
214
215 /* callbacks */
216 qdev_resetfn reset;
217
218 /* device state */
219 const VMStateDescription *vmsd;
220
221 /* Private to qdev / bus. */
222 qdev_initfn init;
223 qdev_event unplug;
224 qdev_event exit;
225 BusInfo *bus_info;
226 struct DeviceInfo *next;
227 };
228 extern DeviceInfo *device_info_list;
229
230 void qdev_register(DeviceInfo *info);
231
232 /* Register device properties. */
233 /* GPIO inputs also double as IRQ sinks. */
234 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
235 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
236
237 CharDriverState *qdev_init_chardev(DeviceState *dev);
238
239 BusState *qdev_get_parent_bus(DeviceState *dev);
240
241 /*** BUS API. ***/
242
243 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
244
245 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
246 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
247 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
248
249 void qbus_create_inplace(BusState *bus, BusInfo *info,
250 DeviceState *parent, const char *name);
251 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
252 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
253 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
254 * 0 otherwise. */
255 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
256 qbus_walkerfn *busfn, void *opaque);
257 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
258 qbus_walkerfn *busfn, void *opaque);
259 void qdev_reset_all(DeviceState *dev);
260 void qbus_reset_all_fn(void *opaque);
261
262 void qbus_free(BusState *bus);
263
264 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
265
266 /* This should go away once we get rid of the NULL bus hack */
267 BusState *sysbus_get_default(void);
268
269 /*** monitor commands ***/
270
271 void do_info_qtree(Monitor *mon);
272 void do_info_qdm(Monitor *mon);
273 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
274 int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
275
276 /*** qdev-properties.c ***/
277
278 extern PropertyInfo qdev_prop_bit;
279 extern PropertyInfo qdev_prop_uint8;
280 extern PropertyInfo qdev_prop_uint16;
281 extern PropertyInfo qdev_prop_uint32;
282 extern PropertyInfo qdev_prop_int32;
283 extern PropertyInfo qdev_prop_uint64;
284 extern PropertyInfo qdev_prop_hex8;
285 extern PropertyInfo qdev_prop_hex32;
286 extern PropertyInfo qdev_prop_hex64;
287 extern PropertyInfo qdev_prop_string;
288 extern PropertyInfo qdev_prop_chr;
289 extern PropertyInfo qdev_prop_ptr;
290 extern PropertyInfo qdev_prop_macaddr;
291 extern PropertyInfo qdev_prop_drive;
292 extern PropertyInfo qdev_prop_netdev;
293 extern PropertyInfo qdev_prop_vlan;
294 extern PropertyInfo qdev_prop_pci_devfn;
295
296 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
297 .name = (_name), \
298 .info = &(_prop), \
299 .offset = offsetof(_state, _field) \
300 + type_check(_type,typeof_field(_state, _field)), \
301 }
302 #define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
303 .name = (_name), \
304 .info = &(_prop), \
305 .offset = offsetof(_state, _field) \
306 + type_check(_type,typeof_field(_state, _field)), \
307 .defval = (_type[]) { _defval }, \
308 }
309 #define DEFINE_PROP_BIT(_name, _state, _field, _bit, _defval) { \
310 .name = (_name), \
311 .info = &(qdev_prop_bit), \
312 .bitnr = (_bit), \
313 .offset = offsetof(_state, _field) \
314 + type_check(uint32_t,typeof_field(_state, _field)), \
315 .defval = (bool[]) { (_defval) }, \
316 }
317
318 #define DEFINE_PROP_UINT8(_n, _s, _f, _d) \
319 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint8, uint8_t)
320 #define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
321 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
322 #define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
323 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
324 #define DEFINE_PROP_INT32(_n, _s, _f, _d) \
325 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_int32, int32_t)
326 #define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
327 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
328 #define DEFINE_PROP_HEX8(_n, _s, _f, _d) \
329 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex8, uint8_t)
330 #define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
331 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
332 #define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
333 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
334 #define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
335 DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
336
337 #define DEFINE_PROP_PTR(_n, _s, _f) \
338 DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
339 #define DEFINE_PROP_CHR(_n, _s, _f) \
340 DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
341 #define DEFINE_PROP_STRING(_n, _s, _f) \
342 DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*)
343 #define DEFINE_PROP_NETDEV(_n, _s, _f) \
344 DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
345 #define DEFINE_PROP_VLAN(_n, _s, _f) \
346 DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
347 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
348 DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
349 #define DEFINE_PROP_MACADDR(_n, _s, _f) \
350 DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
351
352 #define DEFINE_PROP_END_OF_LIST() \
353 {}
354
355 /* Set properties between creation and init. */
356 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
357 int qdev_prop_exists(DeviceState *dev, const char *name);
358 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
359 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
360 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value);
361 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
362 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value);
363 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
364 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
365 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
366 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value);
367 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
368 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
369 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
370 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value) QEMU_WARN_UNUSED_RESULT;
371 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value);
372 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
373 /* FIXME: Remove opaque pointer properties. */
374 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
375 void qdev_prop_set_defaults(DeviceState *dev, Property *props);
376
377 void qdev_prop_register_global_list(GlobalProperty *props);
378 void qdev_prop_set_globals(DeviceState *dev);
379 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
380 Property *prop, const char *value);
381
382 static inline const char *qdev_fw_name(DeviceState *dev)
383 {
384 return dev->info->fw_name ? : dev->info->alias ? : dev->info->name;
385 }
386
387 char *qdev_get_fw_dev_path(DeviceState *dev);
388 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
389 extern struct BusInfo system_bus_info;
390
391 /**
392 * @qdev_ref
393 *
394 * Increase the reference count of a device. A device cannot be freed as long
395 * as its reference count is greater than zero.
396 *
397 * @dev - the device
398 */
399 void qdev_ref(DeviceState *dev);
400
401 /**
402 * @qdef_unref
403 *
404 * Decrease the reference count of a device. A device cannot be freed as long
405 * as its reference count is greater than zero.
406 *
407 * @dev - the device
408 */
409 void qdev_unref(DeviceState *dev);
410
411 /**
412 * @qdev_property_add - add a new property to a device
413 *
414 * @dev - the device to add a property to
415 *
416 * @name - the name of the property. This can contain any character except for
417 * a forward slash. In general, you should use hyphens '-' instead of
418 * underscores '_' when naming properties.
419 *
420 * @type - the type name of the property. This namespace is pretty loosely
421 * defined. Sub namespaces are constructed by using a prefix and then
422 * to angle brackets. For instance, the type 'virtio-net-pci' in the
423 * 'link' namespace would be 'link<virtio-net-pci>'.
424 *
425 * @get - the getter to be called to read a property. If this is NULL, then
426 * the property cannot be read.
427 *
428 * @set - the setter to be called to write a property. If this is NULL,
429 * then the property cannot be written.
430 *
431 * @release - called when the property is removed from the device. This is
432 * meant to allow a property to free its opaque upon device
433 * destruction. This may be NULL.
434 *
435 * @opaque - an opaque pointer to pass to the callbacks for the property
436 *
437 * @errp - returns an error if this function fails
438 */
439 void qdev_property_add(DeviceState *dev, const char *name, const char *type,
440 DevicePropertyAccessor *get, DevicePropertyAccessor *set,
441 DevicePropertyRelease *release,
442 void *opaque, Error **errp);
443
444 /**
445 * @qdev_property_get - reads a property from a device
446 *
447 * @dev - the device
448 *
449 * @v - the visitor that will receive the property value. This should be an
450 * Output visitor and the data will be written with @name as the name.
451 *
452 * @name - the name of the property
453 *
454 * @errp - returns an error if this function fails
455 */
456 void qdev_property_get(DeviceState *dev, Visitor *v, const char *name,
457 Error **errp);
458
459 /**
460 * @qdev_property_set - writes a property to a device
461 *
462 * @dev - the device
463 *
464 * @v - the visitor that will be used to write the property value. This should
465 * be an Input visitor and the data will be first read with @name as the
466 * name and then written as the property value.
467 *
468 * @name - the name of the property
469 *
470 * @errp - returns an error if this function fails
471 */
472 void qdev_property_set(DeviceState *dev, Visitor *v, const char *name,
473 Error **errp);
474
475 /**
476 * @qdev_property_get_type - returns the type of a property
477 *
478 * @dev - the device
479 *
480 * @name - the name of the property
481 *
482 * @errp - returns an error if this function fails
483 *
484 * Returns:
485 * The type name of the property.
486 */
487 const char *qdev_property_get_type(DeviceState *dev, const char *name,
488 Error **errp);
489
490 /**
491 * @qdev_property_add_static - add a @Property to a device referencing a
492 * field in a struct.
493 */
494 void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp);
495
496 /**
497 * @qdev_get_root - returns the root device of the composition tree
498 *
499 * Returns:
500 * The root of the composition tree.
501 */
502 DeviceState *qdev_get_root(void);
503
504 /**
505 * @qdev_get_canonical_path - returns the canonical path for a device. This
506 * is the path within the composition tree starting from the root.
507 *
508 * Returns:
509 * The canonical path in the composition tree.
510 */
511 gchar *qdev_get_canonical_path(DeviceState *dev);
512
513 /**
514 * @qdev_resolve_path - resolves a path returning a device
515 *
516 * There are two types of supported paths--absolute paths and partial paths.
517 *
518 * Absolute paths are derived from the root device and can follow child<> or
519 * link<> properties. Since they can follow link<> properties, they can be
520 * arbitrarily long. Absolute paths look like absolute filenames and are
521 * prefixed with a leading slash.
522 *
523 * Partial paths look like relative filenames. They do not begin with a
524 * prefix. The matching rules for partial paths are subtle but designed to make
525 * specifying devices easy. At each level of the composition tree, the partial
526 * path is matched as an absolute path. The first match is not returned. At
527 * least two matches are searched for. A successful result is only returned if
528 * only one match is founded. If more than one match is found, a flag is
529 * return to indicate that the match was ambiguous.
530 *
531 * @path - the path to resolve
532 *
533 * @ambiguous - returns true if the path resolution failed because of an
534 * ambiguous match
535 *
536 * Returns:
537 * The matched device or NULL on path lookup failure.
538 */
539 DeviceState *qdev_resolve_path(const char *path, bool *ambiguous);
540
541 /**
542 * @qdev_property_add_child - Add a child property to a device
543 *
544 * Child properties form the composition tree. All devices need to be a child
545 * of another device. Devices can only be a child of one device.
546 *
547 * There is no way for a child to determine what its parent is. It is not
548 * a bidirectional relationship. This is by design.
549 *
550 * @dev - the device to add a property to
551 *
552 * @name - the name of the property
553 *
554 * @child - the child device
555 *
556 * @errp - if an error occurs, a pointer to an area to store the area
557 */
558 void qdev_property_add_child(DeviceState *dev, const char *name,
559 DeviceState *child, Error **errp);
560
561 /**
562 * @qdev_property_add_link - Add a link property to a device
563 *
564 * Links establish relationships between devices. Links are unidirectional
565 * although two links can be combined to form a bidirectional relationship
566 * between devices.
567 *
568 * Links form the graph in the device model.
569 *
570 * @dev - the device to add a property to
571 *
572 * @name - the name of the property
573 *
574 * @type - the qdev type of the link
575 *
576 * @child - a pointer to where the link device reference is stored
577 *
578 * @errp - if an error occurs, a pointer to an area to store the area
579 */
580 void qdev_property_add_link(DeviceState *dev, const char *name,
581 const char *type, DeviceState **child,
582 Error **errp);
583
584 /**
585 * @qdev_property_add_str
586 *
587 * Add a string property using getters/setters. This function will add a
588 * property of type 'string'.
589 *
590 * @dev - the device to add a property to
591 *
592 * @name - the name of the property
593 *
594 * @get - the getter or NULL if the property is write-only. This function must
595 * return a string to be freed by @g_free().
596 *
597 * @set - the setter or NULL if the property is read-only
598 *
599 * @errp - if an error occurs, a pointer to an area to store the error
600 */
601 void qdev_property_add_str(DeviceState *dev, const char *name,
602 char *(*get)(DeviceState *, Error **),
603 void (*set)(DeviceState *, const char *, Error **),
604 Error **errp);
605
606 /**
607 * @qdev_get_type
608 *
609 * Returns the string representation of the type of this object.
610 *
611 * @dev - the device
612 *
613 * @errp - if an error occurs, a pointer to an area to store the error
614 *
615 * Returns: a string representing the type. This must be freed by the caller
616 * with g_free().
617 */
618 char *qdev_get_type(DeviceState *dev, Error **errp);
619
620 /**
621 * @qdev_machine_init
622 *
623 * Initialize platform devices before machine init. This is a hack until full
624 * support for composition is added.
625 */
626 void qdev_machine_init(void);
627
628 #endif