4 #include "qemu/queue.h"
5 #include "qemu/option.h"
6 #include "qemu/typedefs.h"
7 #include "qemu/bitmap.h"
8 #include "qom/object.h"
10 #include "qapi/error.h"
13 DEV_NVECTORS_UNSPECIFIED
= -1,
16 #define TYPE_DEVICE "device"
17 #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
18 #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
19 #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
21 typedef enum DeviceCategory
{
22 DEVICE_CATEGORY_BRIDGE
,
24 DEVICE_CATEGORY_STORAGE
,
25 DEVICE_CATEGORY_NETWORK
,
26 DEVICE_CATEGORY_INPUT
,
27 DEVICE_CATEGORY_DISPLAY
,
28 DEVICE_CATEGORY_SOUND
,
33 static inline const char *qdev_category_get_name(DeviceCategory category
)
35 static const char *category_names
[DEVICE_CATEGORY_MAX
] = {
36 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
37 [DEVICE_CATEGORY_USB
] = "USB",
38 [DEVICE_CATEGORY_STORAGE
] = "Storage",
39 [DEVICE_CATEGORY_NETWORK
] = "Network",
40 [DEVICE_CATEGORY_INPUT
] = "Input",
41 [DEVICE_CATEGORY_DISPLAY
] = "Display",
42 [DEVICE_CATEGORY_SOUND
] = "Sound",
43 [DEVICE_CATEGORY_MISC
] = "Misc",
46 return category_names
[category
];
49 typedef int (*qdev_initfn
)(DeviceState
*dev
);
50 typedef int (*qdev_event
)(DeviceState
*dev
);
51 typedef void (*qdev_resetfn
)(DeviceState
*dev
);
52 typedef void (*DeviceRealize
)(DeviceState
*dev
, Error
**errp
);
53 typedef void (*DeviceUnrealize
)(DeviceState
*dev
, Error
**errp
);
55 struct VMStateDescription
;
59 * @props: Properties accessing state fields.
60 * @realize: Callback function invoked when the #DeviceState:realized
61 * property is changed to %true. The default invokes @init if not %NULL.
62 * @unrealize: Callback function invoked when the #DeviceState:realized
63 * property is changed to %false.
64 * @init: Callback function invoked when the #DeviceState::realized property
65 * is changed to %true. Deprecated, new types inheriting directly from
66 * TYPE_DEVICE should use @realize instead, new leaf types should consult
67 * their respective parent type.
70 * Devices are constructed in two stages,
71 * 1) object instantiation via object_initialize() and
72 * 2) device realization via #DeviceState:realized property.
73 * The former may not fail (it might assert or exit), the latter may return
74 * error information to the caller and must be re-entrant.
75 * Trivial field initializations should go into #TypeInfo.instance_init.
76 * Operations depending on @props static properties should go into @realize.
77 * After successful realization, setting static properties will fail.
79 * As an interim step, the #DeviceState:realized property is set by deprecated
80 * functions qdev_init() and qdev_init_nofail().
81 * In the future, devices will propagate this state change to their children
82 * and along busses they expose.
83 * The point in time will be deferred to machine creation, so that values
84 * set in @realize will not be introspectable beforehand. Therefore devices
85 * must not create children during @realize; they should initialize them via
86 * object_initialize() in their own #TypeInfo.instance_init and forward the
87 * realization events appropriately.
89 * The @init callback is considered private to a particular bus implementation
90 * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an
91 * "init" callback on their parent class instead.
93 * Any type may override the @realize and/or @unrealize callbacks but needs
94 * to call the parent type's implementation if keeping their functionality
95 * is desired. Refer to QOM documentation for further discussion and examples.
99 * If a type derived directly from TYPE_DEVICE implements @realize, it does
100 * not need to implement @init and therefore does not need to store and call
101 * #DeviceClass' default @realize callback.
102 * For other types consult the documentation and implementation of the
103 * respective parent types.
107 typedef struct DeviceClass
{
109 ObjectClass parent_class
;
112 DECLARE_BITMAP(categories
, DEVICE_CATEGORY_MAX
);
119 void (*reset
)(DeviceState
*dev
);
120 DeviceRealize realize
;
121 DeviceUnrealize unrealize
;
124 const struct VMStateDescription
*vmsd
;
126 /* Private to qdev / bus. */
127 qdev_initfn init
; /* TODO remove, once users are converted to realize */
129 qdev_event exit
; /* TODO remove, once users are converted to unrealize */
130 const char *bus_type
;
135 * @realized: Indicates whether the device has been fully constructed.
137 * This structure should not be accessed directly. We declare it here
138 * so that it can be embedded in individual device state structures.
149 BusState
*parent_bus
;
154 QLIST_HEAD(, BusState
) child_bus
;
156 int instance_id_alias
;
157 int alias_required_for_version
;
160 #define TYPE_BUS "bus"
161 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
162 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
163 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
166 ObjectClass parent_class
;
168 /* FIXME first arg should be BusState */
169 void (*print_dev
)(Monitor
*mon
, DeviceState
*dev
, int indent
);
170 char *(*get_dev_path
)(DeviceState
*dev
);
172 * This callback is used to create Open Firmware device path in accordance
173 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
174 * bindings can be found at http://playground.sun.com/1275/bindings/.
176 char *(*get_fw_dev_path
)(DeviceState
*dev
);
177 int (*reset
)(BusState
*bus
);
178 /* maximum devices allowed on the bus, 0: no limit. */
182 typedef struct BusChild
{
185 QTAILQ_ENTRY(BusChild
) sibling
;
197 QTAILQ_HEAD(ChildrenHead
, BusChild
) children
;
198 QLIST_ENTRY(BusState
) sibling
;
209 PropertyInfo
*arrayinfo
;
213 struct PropertyInfo
{
215 const char *legacy_name
;
216 const char **enum_table
;
217 int (*parse
)(DeviceState
*dev
, Property
*prop
, const char *str
);
218 int (*print
)(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
);
219 ObjectPropertyAccessor
*get
;
220 ObjectPropertyAccessor
*set
;
221 ObjectPropertyRelease
*release
;
224 typedef struct GlobalProperty
{
226 const char *property
;
228 QTAILQ_ENTRY(GlobalProperty
) next
;
231 /*** Board API. This should go away once we have a machine config file. ***/
233 DeviceState
*qdev_create(BusState
*bus
, const char *name
);
234 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
);
235 int qdev_init(DeviceState
*dev
) QEMU_WARN_UNUSED_RESULT
;
236 void qdev_init_nofail(DeviceState
*dev
);
237 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
238 int required_for_version
);
239 void qdev_unplug(DeviceState
*dev
, Error
**errp
);
240 void qdev_free(DeviceState
*dev
);
241 int qdev_simple_unplug_cb(DeviceState
*dev
);
242 void qdev_machine_creation_done(void);
243 bool qdev_machine_modified(void);
245 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
);
246 void qdev_connect_gpio_out(DeviceState
*dev
, int n
, qemu_irq pin
);
248 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
);
250 /*** Device API. ***/
252 /* Register device properties. */
253 /* GPIO inputs also double as IRQ sinks. */
254 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
);
255 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
);
257 BusState
*qdev_get_parent_bus(DeviceState
*dev
);
261 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
);
263 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
264 typedef int (qbus_walkerfn
)(BusState
*bus
, void *opaque
);
265 typedef int (qdev_walkerfn
)(DeviceState
*dev
, void *opaque
);
267 void qbus_create_inplace(void *bus
, const char *typename
,
268 DeviceState
*parent
, const char *name
);
269 BusState
*qbus_create(const char *typename
, DeviceState
*parent
, const char *name
);
270 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
271 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
273 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
274 qbus_walkerfn
*busfn
, void *opaque
);
275 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
276 qbus_walkerfn
*busfn
, void *opaque
);
277 void qdev_reset_all(DeviceState
*dev
);
281 * @bus: Bus to be reset.
283 * Reset @bus and perform a bus-level ("hard") reset of all devices connected
284 * to it, including recursive processing of all buses below @bus itself. A
285 * hard reset means that qbus_reset_all will reset all state of the device.
286 * For PCI devices, for example, this will include the base address registers
287 * or configuration space.
289 void qbus_reset_all(BusState
*bus
);
290 void qbus_reset_all_fn(void *opaque
);
292 void qbus_free(BusState
*bus
);
294 /* This should go away once we get rid of the NULL bus hack */
295 BusState
*sysbus_get_default(void);
297 char *qdev_get_fw_dev_path(DeviceState
*dev
);
302 * Initialize platform devices before machine init. This is a hack until full
303 * support for composition is added.
305 void qdev_machine_init(void);
310 * Reset a single device (by calling the reset method).
312 void device_reset(DeviceState
*dev
);
314 const struct VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
);
316 const char *qdev_fw_name(DeviceState
*dev
);
318 Object
*qdev_get_machine(void);
320 /* FIXME: make this a link<> */
321 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
);
323 extern int qdev_hotplug
;
325 char *qdev_get_dev_path(DeviceState
*dev
);