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