]>
Commit | Line | Data |
---|---|---|
074a86fc AL |
1 | #ifndef QDEV_CORE_H |
2 | #define QDEV_CORE_H | |
3 | ||
1de7afc9 PB |
4 | #include "qemu/queue.h" |
5 | #include "qemu/option.h" | |
6 | #include "qemu/typedefs.h" | |
949fc823 | 7 | #include "qemu/bitmap.h" |
14cccb61 | 8 | #include "qom/object.h" |
074a86fc | 9 | #include "hw/irq.h" |
7b1b5d19 | 10 | #include "qapi/error.h" |
0ee4de6c | 11 | #include "hw/hotplug.h" |
074a86fc | 12 | |
074a86fc AL |
13 | enum { |
14 | DEV_NVECTORS_UNSPECIFIED = -1, | |
15 | }; | |
16 | ||
17 | #define TYPE_DEVICE "device" | |
18 | #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE) | |
19 | #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE) | |
20 | #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE) | |
21 | ||
3d1237fb MA |
22 | typedef enum DeviceCategory { |
23 | DEVICE_CATEGORY_BRIDGE, | |
24 | DEVICE_CATEGORY_USB, | |
25 | DEVICE_CATEGORY_STORAGE, | |
26 | DEVICE_CATEGORY_NETWORK, | |
27 | DEVICE_CATEGORY_INPUT, | |
28 | DEVICE_CATEGORY_DISPLAY, | |
29 | DEVICE_CATEGORY_SOUND, | |
30 | DEVICE_CATEGORY_MISC, | |
31 | DEVICE_CATEGORY_MAX | |
32 | } DeviceCategory; | |
33 | ||
074a86fc AL |
34 | typedef int (*qdev_initfn)(DeviceState *dev); |
35 | typedef int (*qdev_event)(DeviceState *dev); | |
36 | typedef void (*qdev_resetfn)(DeviceState *dev); | |
249d4172 AF |
37 | typedef void (*DeviceRealize)(DeviceState *dev, Error **errp); |
38 | typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp); | |
074a86fc AL |
39 | |
40 | struct VMStateDescription; | |
41 | ||
249d4172 AF |
42 | /** |
43 | * DeviceClass: | |
44 | * @props: Properties accessing state fields. | |
45 | * @realize: Callback function invoked when the #DeviceState:realized | |
46 | * property is changed to %true. The default invokes @init if not %NULL. | |
47 | * @unrealize: Callback function invoked when the #DeviceState:realized | |
48 | * property is changed to %false. | |
49 | * @init: Callback function invoked when the #DeviceState::realized property | |
50 | * is changed to %true. Deprecated, new types inheriting directly from | |
51 | * TYPE_DEVICE should use @realize instead, new leaf types should consult | |
52 | * their respective parent type. | |
53 | * | |
54 | * # Realization # | |
55 | * Devices are constructed in two stages, | |
56 | * 1) object instantiation via object_initialize() and | |
57 | * 2) device realization via #DeviceState:realized property. | |
58 | * The former may not fail (it might assert or exit), the latter may return | |
59 | * error information to the caller and must be re-entrant. | |
60 | * Trivial field initializations should go into #TypeInfo.instance_init. | |
61 | * Operations depending on @props static properties should go into @realize. | |
62 | * After successful realization, setting static properties will fail. | |
63 | * | |
64 | * As an interim step, the #DeviceState:realized property is set by deprecated | |
65 | * functions qdev_init() and qdev_init_nofail(). | |
66 | * In the future, devices will propagate this state change to their children | |
67 | * and along busses they expose. | |
68 | * The point in time will be deferred to machine creation, so that values | |
69 | * set in @realize will not be introspectable beforehand. Therefore devices | |
70 | * must not create children during @realize; they should initialize them via | |
71 | * object_initialize() in their own #TypeInfo.instance_init and forward the | |
72 | * realization events appropriately. | |
73 | * | |
74 | * The @init callback is considered private to a particular bus implementation | |
75 | * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an | |
76 | * "init" callback on their parent class instead. | |
782beb52 | 77 | * |
249d4172 | 78 | * Any type may override the @realize and/or @unrealize callbacks but needs |
782beb52 AF |
79 | * to call the parent type's implementation if keeping their functionality |
80 | * is desired. Refer to QOM documentation for further discussion and examples. | |
81 | * | |
82 | * <note> | |
83 | * <para> | |
249d4172 AF |
84 | * If a type derived directly from TYPE_DEVICE implements @realize, it does |
85 | * not need to implement @init and therefore does not need to store and call | |
86 | * #DeviceClass' default @realize callback. | |
782beb52 AF |
87 | * For other types consult the documentation and implementation of the |
88 | * respective parent types. | |
89 | * </para> | |
90 | * </note> | |
249d4172 | 91 | */ |
074a86fc | 92 | typedef struct DeviceClass { |
249d4172 | 93 | /*< private >*/ |
074a86fc | 94 | ObjectClass parent_class; |
249d4172 | 95 | /*< public >*/ |
074a86fc | 96 | |
3d1237fb | 97 | DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX); |
074a86fc AL |
98 | const char *fw_name; |
99 | const char *desc; | |
100 | Property *props; | |
efec3dd6 MA |
101 | |
102 | /* | |
103 | * Shall we hide this device model from -device / device_add? | |
104 | * All devices should support instantiation with device_add, and | |
105 | * this flag should not exist. But we're not there, yet. Some | |
106 | * devices fail to instantiate with cryptic error messages. | |
107 | * Others instantiate, but don't work. Exposing users to such | |
108 | * behavior would be cruel; this flag serves to protect them. It | |
109 | * should never be set without a comment explaining why it is set. | |
110 | * TODO remove once we're there | |
111 | */ | |
112 | bool cannot_instantiate_with_device_add_yet; | |
074a86fc AL |
113 | |
114 | /* callbacks */ | |
115 | void (*reset)(DeviceState *dev); | |
249d4172 AF |
116 | DeviceRealize realize; |
117 | DeviceUnrealize unrealize; | |
074a86fc AL |
118 | |
119 | /* device state */ | |
120 | const struct VMStateDescription *vmsd; | |
121 | ||
122 | /* Private to qdev / bus. */ | |
249d4172 | 123 | qdev_initfn init; /* TODO remove, once users are converted to realize */ |
074a86fc | 124 | qdev_event unplug; |
fe6c2117 | 125 | qdev_event exit; /* TODO remove, once users are converted to unrealize */ |
074a86fc AL |
126 | const char *bus_type; |
127 | } DeviceClass; | |
128 | ||
7983c8a3 AF |
129 | /** |
130 | * DeviceState: | |
131 | * @realized: Indicates whether the device has been fully constructed. | |
132 | * | |
133 | * This structure should not be accessed directly. We declare it here | |
134 | * so that it can be embedded in individual device state structures. | |
135 | */ | |
074a86fc | 136 | struct DeviceState { |
7983c8a3 | 137 | /*< private >*/ |
074a86fc | 138 | Object parent_obj; |
7983c8a3 | 139 | /*< public >*/ |
074a86fc AL |
140 | |
141 | const char *id; | |
7983c8a3 | 142 | bool realized; |
074a86fc AL |
143 | QemuOpts *opts; |
144 | int hotplugged; | |
145 | BusState *parent_bus; | |
146 | int num_gpio_out; | |
147 | qemu_irq *gpio_out; | |
148 | int num_gpio_in; | |
149 | qemu_irq *gpio_in; | |
150 | QLIST_HEAD(, BusState) child_bus; | |
151 | int num_child_bus; | |
152 | int instance_id_alias; | |
153 | int alias_required_for_version; | |
154 | }; | |
155 | ||
156 | #define TYPE_BUS "bus" | |
157 | #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS) | |
158 | #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS) | |
159 | #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS) | |
160 | ||
161 | struct BusClass { | |
162 | ObjectClass parent_class; | |
163 | ||
164 | /* FIXME first arg should be BusState */ | |
165 | void (*print_dev)(Monitor *mon, DeviceState *dev, int indent); | |
166 | char *(*get_dev_path)(DeviceState *dev); | |
167 | /* | |
168 | * This callback is used to create Open Firmware device path in accordance | |
169 | * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus | |
170 | * bindings can be found at http://playground.sun.com/1275/bindings/. | |
171 | */ | |
172 | char *(*get_fw_dev_path)(DeviceState *dev); | |
dcc20931 | 173 | void (*reset)(BusState *bus); |
1395af6f FK |
174 | /* maximum devices allowed on the bus, 0: no limit. */ |
175 | int max_dev; | |
074a86fc AL |
176 | }; |
177 | ||
178 | typedef struct BusChild { | |
179 | DeviceState *child; | |
180 | int index; | |
181 | QTAILQ_ENTRY(BusChild) sibling; | |
182 | } BusChild; | |
183 | ||
0ee4de6c IM |
184 | #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler" |
185 | ||
074a86fc AL |
186 | /** |
187 | * BusState: | |
0ee4de6c | 188 | * @hotplug_device: link to a hotplug device associated with bus. |
074a86fc AL |
189 | */ |
190 | struct BusState { | |
191 | Object obj; | |
192 | DeviceState *parent; | |
193 | const char *name; | |
194 | int allow_hotplug; | |
0ee4de6c | 195 | HotplugHandler *hotplug_handler; |
074a86fc AL |
196 | int max_index; |
197 | QTAILQ_HEAD(ChildrenHead, BusChild) children; | |
198 | QLIST_ENTRY(BusState) sibling; | |
199 | }; | |
200 | ||
201 | struct Property { | |
202 | const char *name; | |
203 | PropertyInfo *info; | |
204 | int offset; | |
205 | uint8_t bitnr; | |
206 | uint8_t qtype; | |
207 | int64_t defval; | |
0be6bfac PM |
208 | int arrayoffset; |
209 | PropertyInfo *arrayinfo; | |
210 | int arrayfieldsize; | |
074a86fc AL |
211 | }; |
212 | ||
213 | struct PropertyInfo { | |
214 | const char *name; | |
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; | |
222 | }; | |
223 | ||
224 | typedef struct GlobalProperty { | |
225 | const char *driver; | |
226 | const char *property; | |
227 | const char *value; | |
228 | QTAILQ_ENTRY(GlobalProperty) next; | |
229 | } GlobalProperty; | |
230 | ||
231 | /*** Board API. This should go away once we have a machine config file. ***/ | |
232 | ||
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); | |
074a86fc AL |
240 | int qdev_simple_unplug_cb(DeviceState *dev); |
241 | void qdev_machine_creation_done(void); | |
242 | bool qdev_machine_modified(void); | |
243 | ||
244 | qemu_irq qdev_get_gpio_in(DeviceState *dev, int n); | |
245 | void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin); | |
246 | ||
247 | BusState *qdev_get_child_bus(DeviceState *dev, const char *name); | |
248 | ||
249 | /*** Device API. ***/ | |
250 | ||
251 | /* Register device properties. */ | |
252 | /* GPIO inputs also double as IRQ sinks. */ | |
253 | void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n); | |
254 | void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n); | |
255 | ||
256 | BusState *qdev_get_parent_bus(DeviceState *dev); | |
257 | ||
258 | /*** BUS API. ***/ | |
259 | ||
260 | DeviceState *qdev_find_recursive(BusState *bus, const char *id); | |
261 | ||
262 | /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */ | |
263 | typedef int (qbus_walkerfn)(BusState *bus, void *opaque); | |
264 | typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque); | |
265 | ||
fb17dfe0 | 266 | void qbus_create_inplace(void *bus, size_t size, const char *typename, |
074a86fc AL |
267 | DeviceState *parent, const char *name); |
268 | BusState *qbus_create(const char *typename, DeviceState *parent, const char *name); | |
269 | /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion, | |
270 | * < 0 if either devfn or busfn terminate walk somewhere in cursion, | |
271 | * 0 otherwise. */ | |
0293214b PB |
272 | int qbus_walk_children(BusState *bus, |
273 | qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, | |
274 | qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, | |
275 | void *opaque); | |
276 | int qdev_walk_children(DeviceState *dev, | |
277 | qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn, | |
278 | qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn, | |
279 | void *opaque); | |
280 | ||
074a86fc | 281 | void qdev_reset_all(DeviceState *dev); |
d0508c36 PB |
282 | |
283 | /** | |
284 | * @qbus_reset_all: | |
285 | * @bus: Bus to be reset. | |
286 | * | |
287 | * Reset @bus and perform a bus-level ("hard") reset of all devices connected | |
288 | * to it, including recursive processing of all buses below @bus itself. A | |
289 | * hard reset means that qbus_reset_all will reset all state of the device. | |
290 | * For PCI devices, for example, this will include the base address registers | |
291 | * or configuration space. | |
292 | */ | |
293 | void qbus_reset_all(BusState *bus); | |
074a86fc AL |
294 | void qbus_reset_all_fn(void *opaque); |
295 | ||
074a86fc AL |
296 | /* This should go away once we get rid of the NULL bus hack */ |
297 | BusState *sysbus_get_default(void); | |
298 | ||
299 | char *qdev_get_fw_dev_path(DeviceState *dev); | |
300 | ||
301 | /** | |
302 | * @qdev_machine_init | |
303 | * | |
304 | * Initialize platform devices before machine init. This is a hack until full | |
305 | * support for composition is added. | |
306 | */ | |
307 | void qdev_machine_init(void); | |
308 | ||
309 | /** | |
310 | * @device_reset | |
311 | * | |
312 | * Reset a single device (by calling the reset method). | |
313 | */ | |
314 | void device_reset(DeviceState *dev); | |
315 | ||
316 | const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev); | |
317 | ||
318 | const char *qdev_fw_name(DeviceState *dev); | |
319 | ||
320 | Object *qdev_get_machine(void); | |
321 | ||
322 | /* FIXME: make this a link<> */ | |
323 | void qdev_set_parent_bus(DeviceState *dev, BusState *bus); | |
324 | ||
325 | extern int qdev_hotplug; | |
326 | ||
327 | char *qdev_get_dev_path(DeviceState *dev); | |
328 | ||
0ee4de6c IM |
329 | static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, |
330 | Error **errp) | |
331 | { | |
332 | object_property_set_link(OBJECT(bus), OBJECT(handler), | |
333 | QDEV_HOTPLUG_HANDLER_PROPERTY, errp); | |
334 | bus->allow_hotplug = 1; | |
335 | } | |
074a86fc | 336 | #endif |