xref: /openbmc/qemu/include/hw/qdev-core.h (revision 949fc82314cc84162e64a5323764527a542421ce)
1 #ifndef QDEV_CORE_H
2 #define QDEV_CORE_H
3 
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"
9 #include "hw/irq.h"
10 #include "qapi/error.h"
11 
12 enum {
13     DEV_NVECTORS_UNSPECIFIED = -1,
14 };
15 
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)
20 
21 typedef int (*qdev_initfn)(DeviceState *dev);
22 typedef int (*qdev_event)(DeviceState *dev);
23 typedef void (*qdev_resetfn)(DeviceState *dev);
24 typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
25 typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
26 
27 struct VMStateDescription;
28 
29 /**
30  * DeviceClass:
31  * @props: Properties accessing state fields.
32  * @realize: Callback function invoked when the #DeviceState:realized
33  * property is changed to %true. The default invokes @init if not %NULL.
34  * @unrealize: Callback function invoked when the #DeviceState:realized
35  * property is changed to %false.
36  * @init: Callback function invoked when the #DeviceState::realized property
37  * is changed to %true. Deprecated, new types inheriting directly from
38  * TYPE_DEVICE should use @realize instead, new leaf types should consult
39  * their respective parent type.
40  *
41  * # Realization #
42  * Devices are constructed in two stages,
43  * 1) object instantiation via object_initialize() and
44  * 2) device realization via #DeviceState:realized property.
45  * The former may not fail (it might assert or exit), the latter may return
46  * error information to the caller and must be re-entrant.
47  * Trivial field initializations should go into #TypeInfo.instance_init.
48  * Operations depending on @props static properties should go into @realize.
49  * After successful realization, setting static properties will fail.
50  *
51  * As an interim step, the #DeviceState:realized property is set by deprecated
52  * functions qdev_init() and qdev_init_nofail().
53  * In the future, devices will propagate this state change to their children
54  * and along busses they expose.
55  * The point in time will be deferred to machine creation, so that values
56  * set in @realize will not be introspectable beforehand. Therefore devices
57  * must not create children during @realize; they should initialize them via
58  * object_initialize() in their own #TypeInfo.instance_init and forward the
59  * realization events appropriately.
60  *
61  * The @init callback is considered private to a particular bus implementation
62  * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an
63  * "init" callback on their parent class instead.
64  *
65  * Any type may override the @realize and/or @unrealize callbacks but needs
66  * to call the parent type's implementation if keeping their functionality
67  * is desired. Refer to QOM documentation for further discussion and examples.
68  *
69  * <note>
70  *   <para>
71  * If a type derived directly from TYPE_DEVICE implements @realize, it does
72  * not need to implement @init and therefore does not need to store and call
73  * #DeviceClass' default @realize callback.
74  * For other types consult the documentation and implementation of the
75  * respective parent types.
76  *   </para>
77  * </note>
78  */
79 typedef struct DeviceClass {
80     /*< private >*/
81     ObjectClass parent_class;
82     /*< public >*/
83 
84     const char *fw_name;
85     const char *desc;
86     Property *props;
87     int no_user;
88 
89     /* callbacks */
90     void (*reset)(DeviceState *dev);
91     DeviceRealize realize;
92     DeviceUnrealize unrealize;
93 
94     /* device state */
95     const struct VMStateDescription *vmsd;
96 
97     /* Private to qdev / bus.  */
98     qdev_initfn init; /* TODO remove, once users are converted to realize */
99     qdev_event unplug;
100     qdev_event exit; /* TODO remove, once users are converted to unrealize */
101     const char *bus_type;
102 } DeviceClass;
103 
104 /**
105  * DeviceState:
106  * @realized: Indicates whether the device has been fully constructed.
107  *
108  * This structure should not be accessed directly.  We declare it here
109  * so that it can be embedded in individual device state structures.
110  */
111 struct DeviceState {
112     /*< private >*/
113     Object parent_obj;
114     /*< public >*/
115 
116     const char *id;
117     bool realized;
118     QemuOpts *opts;
119     int hotplugged;
120     BusState *parent_bus;
121     int num_gpio_out;
122     qemu_irq *gpio_out;
123     int num_gpio_in;
124     qemu_irq *gpio_in;
125     QLIST_HEAD(, BusState) child_bus;
126     int num_child_bus;
127     int instance_id_alias;
128     int alias_required_for_version;
129 };
130 
131 #define TYPE_BUS "bus"
132 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
133 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
134 #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
135 
136 struct BusClass {
137     ObjectClass parent_class;
138 
139     /* FIXME first arg should be BusState */
140     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
141     char *(*get_dev_path)(DeviceState *dev);
142     /*
143      * This callback is used to create Open Firmware device path in accordance
144      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
145      * bindings can be found at http://playground.sun.com/1275/bindings/.
146      */
147     char *(*get_fw_dev_path)(DeviceState *dev);
148     int (*reset)(BusState *bus);
149     /* maximum devices allowed on the bus, 0: no limit. */
150     int max_dev;
151 };
152 
153 typedef struct BusChild {
154     DeviceState *child;
155     int index;
156     QTAILQ_ENTRY(BusChild) sibling;
157 } BusChild;
158 
159 /**
160  * BusState:
161  */
162 struct BusState {
163     Object obj;
164     DeviceState *parent;
165     const char *name;
166     int allow_hotplug;
167     int max_index;
168     QTAILQ_HEAD(ChildrenHead, BusChild) children;
169     QLIST_ENTRY(BusState) sibling;
170 };
171 
172 struct Property {
173     const char   *name;
174     PropertyInfo *info;
175     int          offset;
176     uint8_t      bitnr;
177     uint8_t      qtype;
178     int64_t      defval;
179     int          arrayoffset;
180     PropertyInfo *arrayinfo;
181     int          arrayfieldsize;
182 };
183 
184 struct PropertyInfo {
185     const char *name;
186     const char *legacy_name;
187     const char **enum_table;
188     int (*parse)(DeviceState *dev, Property *prop, const char *str);
189     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
190     ObjectPropertyAccessor *get;
191     ObjectPropertyAccessor *set;
192     ObjectPropertyRelease *release;
193 };
194 
195 typedef struct GlobalProperty {
196     const char *driver;
197     const char *property;
198     const char *value;
199     QTAILQ_ENTRY(GlobalProperty) next;
200 } GlobalProperty;
201 
202 /*** Board API.  This should go away once we have a machine config file.  ***/
203 
204 DeviceState *qdev_create(BusState *bus, const char *name);
205 DeviceState *qdev_try_create(BusState *bus, const char *name);
206 int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
207 void qdev_init_nofail(DeviceState *dev);
208 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
209                                  int required_for_version);
210 void qdev_unplug(DeviceState *dev, Error **errp);
211 void qdev_free(DeviceState *dev);
212 int qdev_simple_unplug_cb(DeviceState *dev);
213 void qdev_machine_creation_done(void);
214 bool qdev_machine_modified(void);
215 
216 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
217 void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
218 
219 BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
220 
221 /*** Device API.  ***/
222 
223 /* Register device properties.  */
224 /* GPIO inputs also double as IRQ sinks.  */
225 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
226 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
227 
228 BusState *qdev_get_parent_bus(DeviceState *dev);
229 
230 /*** BUS API. ***/
231 
232 DeviceState *qdev_find_recursive(BusState *bus, const char *id);
233 
234 /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
235 typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
236 typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
237 
238 void qbus_create_inplace(void *bus, const char *typename,
239                          DeviceState *parent, const char *name);
240 BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
241 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
242  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
243  *           0 otherwise. */
244 int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
245                        qbus_walkerfn *busfn, void *opaque);
246 int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
247                        qbus_walkerfn *busfn, void *opaque);
248 void qdev_reset_all(DeviceState *dev);
249 
250 /**
251  * @qbus_reset_all:
252  * @bus: Bus to be reset.
253  *
254  * Reset @bus and perform a bus-level ("hard") reset of all devices connected
255  * to it, including recursive processing of all buses below @bus itself.  A
256  * hard reset means that qbus_reset_all will reset all state of the device.
257  * For PCI devices, for example, this will include the base address registers
258  * or configuration space.
259  */
260 void qbus_reset_all(BusState *bus);
261 void qbus_reset_all_fn(void *opaque);
262 
263 void qbus_free(BusState *bus);
264 
265 /* This should go away once we get rid of the NULL bus hack */
266 BusState *sysbus_get_default(void);
267 
268 char *qdev_get_fw_dev_path(DeviceState *dev);
269 
270 /**
271  * @qdev_machine_init
272  *
273  * Initialize platform devices before machine init.  This is a hack until full
274  * support for composition is added.
275  */
276 void qdev_machine_init(void);
277 
278 /**
279  * @device_reset
280  *
281  * Reset a single device (by calling the reset method).
282  */
283 void device_reset(DeviceState *dev);
284 
285 const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
286 
287 const char *qdev_fw_name(DeviceState *dev);
288 
289 Object *qdev_get_machine(void);
290 
291 /* FIXME: make this a link<> */
292 void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
293 
294 extern int qdev_hotplug;
295 
296 char *qdev_get_dev_path(DeviceState *dev);
297 
298 #endif
299