4 #include "qemu/queue.h"
5 #include "qemu/option.h"
6 #include "qemu/typedefs.h"
7 #include "qom/object.h"
9 #include "qapi/error.h"
12 DEV_NVECTORS_UNSPECIFIED
= -1,
15 #define TYPE_DEVICE "device"
16 #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
17 #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
18 #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
20 typedef int (*qdev_initfn
)(DeviceState
*dev
);
21 typedef int (*qdev_event
)(DeviceState
*dev
);
22 typedef void (*qdev_resetfn
)(DeviceState
*dev
);
23 typedef void (*DeviceRealize
)(DeviceState
*dev
, Error
**errp
);
24 typedef void (*DeviceUnrealize
)(DeviceState
*dev
, Error
**errp
);
26 struct VMStateDescription
;
30 * @props: Properties accessing state fields.
31 * @realize: Callback function invoked when the #DeviceState:realized
32 * property is changed to %true. The default invokes @init if not %NULL.
33 * @unrealize: Callback function invoked when the #DeviceState:realized
34 * property is changed to %false.
35 * @init: Callback function invoked when the #DeviceState::realized property
36 * is changed to %true. Deprecated, new types inheriting directly from
37 * TYPE_DEVICE should use @realize instead, new leaf types should consult
38 * their respective parent type.
41 * Devices are constructed in two stages,
42 * 1) object instantiation via object_initialize() and
43 * 2) device realization via #DeviceState:realized property.
44 * The former may not fail (it might assert or exit), the latter may return
45 * error information to the caller and must be re-entrant.
46 * Trivial field initializations should go into #TypeInfo.instance_init.
47 * Operations depending on @props static properties should go into @realize.
48 * After successful realization, setting static properties will fail.
50 * As an interim step, the #DeviceState:realized property is set by deprecated
51 * functions qdev_init() and qdev_init_nofail().
52 * In the future, devices will propagate this state change to their children
53 * and along busses they expose.
54 * The point in time will be deferred to machine creation, so that values
55 * set in @realize will not be introspectable beforehand. Therefore devices
56 * must not create children during @realize; they should initialize them via
57 * object_initialize() in their own #TypeInfo.instance_init and forward the
58 * realization events appropriately.
60 * The @init callback is considered private to a particular bus implementation
61 * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an
62 * "init" callback on their parent class instead.
63 * Any type may override the @realize and/or @unrealize callbacks but needs
64 * to call (and thus save) the parent type's implementation if so desired.
65 * Usually this means storing the previous value of, e.g., @realized inside
66 * the type's class structure and overwriting it with a function that first
67 * invokes the stored callback, then performs any additional steps.
68 * If a type derived directly from TYPE_DEVICE implements @realize, it does
69 * not need to implement @init and therefore does not need to store and call
70 * #DeviceClass' default @realize callback.
72 typedef struct DeviceClass
{
74 ObjectClass parent_class
;
83 void (*reset
)(DeviceState
*dev
);
84 DeviceRealize realize
;
85 DeviceUnrealize unrealize
;
88 const struct VMStateDescription
*vmsd
;
90 /* Private to qdev / bus. */
91 qdev_initfn init
; /* TODO remove, once users are converted to realize */
99 * @realized: Indicates whether the device has been fully constructed.
101 * This structure should not be accessed directly. We declare it here
102 * so that it can be embedded in individual device state structures.
113 BusState
*parent_bus
;
118 QLIST_HEAD(, BusState
) child_bus
;
120 int instance_id_alias
;
121 int alias_required_for_version
;
124 #define TYPE_BUS "bus"
125 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
126 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
127 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
130 ObjectClass parent_class
;
132 /* FIXME first arg should be BusState */
133 void (*print_dev
)(Monitor
*mon
, DeviceState
*dev
, int indent
);
134 char *(*get_dev_path
)(DeviceState
*dev
);
136 * This callback is used to create Open Firmware device path in accordance
137 * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
138 * bindings can be found at http://playground.sun.com/1275/bindings/.
140 char *(*get_fw_dev_path
)(DeviceState
*dev
);
141 int (*reset
)(BusState
*bus
);
144 typedef struct BusChild
{
147 QTAILQ_ENTRY(BusChild
) sibling
;
159 QTAILQ_HEAD(ChildrenHead
, BusChild
) children
;
160 QLIST_ENTRY(BusState
) sibling
;
172 struct PropertyInfo
{
174 const char *legacy_name
;
175 const char **enum_table
;
176 int (*parse
)(DeviceState
*dev
, Property
*prop
, const char *str
);
177 int (*print
)(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
);
178 ObjectPropertyAccessor
*get
;
179 ObjectPropertyAccessor
*set
;
180 ObjectPropertyRelease
*release
;
183 typedef struct GlobalProperty
{
185 const char *property
;
187 QTAILQ_ENTRY(GlobalProperty
) next
;
190 /*** Board API. This should go away once we have a machine config file. ***/
192 DeviceState
*qdev_create(BusState
*bus
, const char *name
);
193 DeviceState
*qdev_try_create(BusState
*bus
, const char *name
);
194 int qdev_init(DeviceState
*dev
) QEMU_WARN_UNUSED_RESULT
;
195 void qdev_init_nofail(DeviceState
*dev
);
196 void qdev_set_legacy_instance_id(DeviceState
*dev
, int alias_id
,
197 int required_for_version
);
198 void qdev_unplug(DeviceState
*dev
, Error
**errp
);
199 void qdev_free(DeviceState
*dev
);
200 int qdev_simple_unplug_cb(DeviceState
*dev
);
201 void qdev_machine_creation_done(void);
202 bool qdev_machine_modified(void);
204 qemu_irq
qdev_get_gpio_in(DeviceState
*dev
, int n
);
205 void qdev_connect_gpio_out(DeviceState
*dev
, int n
, qemu_irq pin
);
207 BusState
*qdev_get_child_bus(DeviceState
*dev
, const char *name
);
209 /*** Device API. ***/
211 /* Register device properties. */
212 /* GPIO inputs also double as IRQ sinks. */
213 void qdev_init_gpio_in(DeviceState
*dev
, qemu_irq_handler handler
, int n
);
214 void qdev_init_gpio_out(DeviceState
*dev
, qemu_irq
*pins
, int n
);
216 BusState
*qdev_get_parent_bus(DeviceState
*dev
);
220 DeviceState
*qdev_find_recursive(BusState
*bus
, const char *id
);
222 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
223 typedef int (qbus_walkerfn
)(BusState
*bus
, void *opaque
);
224 typedef int (qdev_walkerfn
)(DeviceState
*dev
, void *opaque
);
226 void qbus_create_inplace(BusState
*bus
, const char *typename
,
227 DeviceState
*parent
, const char *name
);
228 BusState
*qbus_create(const char *typename
, DeviceState
*parent
, const char *name
);
229 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
230 * < 0 if either devfn or busfn terminate walk somewhere in cursion,
232 int qbus_walk_children(BusState
*bus
, qdev_walkerfn
*devfn
,
233 qbus_walkerfn
*busfn
, void *opaque
);
234 int qdev_walk_children(DeviceState
*dev
, qdev_walkerfn
*devfn
,
235 qbus_walkerfn
*busfn
, void *opaque
);
236 void qdev_reset_all(DeviceState
*dev
);
240 * @bus: Bus to be reset.
242 * Reset @bus and perform a bus-level ("hard") reset of all devices connected
243 * to it, including recursive processing of all buses below @bus itself. A
244 * hard reset means that qbus_reset_all will reset all state of the device.
245 * For PCI devices, for example, this will include the base address registers
246 * or configuration space.
248 void qbus_reset_all(BusState
*bus
);
249 void qbus_reset_all_fn(void *opaque
);
251 void qbus_free(BusState
*bus
);
253 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
255 /* This should go away once we get rid of the NULL bus hack */
256 BusState
*sysbus_get_default(void);
258 char *qdev_get_fw_dev_path(DeviceState
*dev
);
263 * Initialize platform devices before machine init. This is a hack until full
264 * support for composition is added.
266 void qdev_machine_init(void);
271 * Reset a single device (by calling the reset method).
273 void device_reset(DeviceState
*dev
);
275 const struct VMStateDescription
*qdev_get_vmsd(DeviceState
*dev
);
277 const char *qdev_fw_name(DeviceState
*dev
);
279 Object
*qdev_get_machine(void);
281 /* FIXME: make this a link<> */
282 void qdev_set_parent_bus(DeviceState
*dev
, BusState
*bus
);
284 extern int qdev_hotplug
;
286 char *qdev_get_dev_path(DeviceState
*dev
);