xref: /qemu/include/hw/qdev-core.h (revision fb17dfe0575243a3f60dcefca37fa82ae682f146)
1074a86fcSAnthony Liguori #ifndef QDEV_CORE_H
2074a86fcSAnthony Liguori #define QDEV_CORE_H
3074a86fcSAnthony Liguori 
41de7afc9SPaolo Bonzini #include "qemu/queue.h"
51de7afc9SPaolo Bonzini #include "qemu/option.h"
61de7afc9SPaolo Bonzini #include "qemu/typedefs.h"
7949fc823SMarcel Apfelbaum #include "qemu/bitmap.h"
814cccb61SPaolo Bonzini #include "qom/object.h"
9074a86fcSAnthony Liguori #include "hw/irq.h"
107b1b5d19SPaolo Bonzini #include "qapi/error.h"
11074a86fcSAnthony Liguori 
12074a86fcSAnthony Liguori enum {
13074a86fcSAnthony Liguori     DEV_NVECTORS_UNSPECIFIED = -1,
14074a86fcSAnthony Liguori };
15074a86fcSAnthony Liguori 
16074a86fcSAnthony Liguori #define TYPE_DEVICE "device"
17074a86fcSAnthony Liguori #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
18074a86fcSAnthony Liguori #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
19074a86fcSAnthony Liguori #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
20074a86fcSAnthony Liguori 
213d1237fbSMarcel Apfelbaum typedef enum DeviceCategory {
223d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_BRIDGE,
233d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_USB,
243d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_STORAGE,
253d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_NETWORK,
263d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_INPUT,
273d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_DISPLAY,
283d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_SOUND,
293d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MISC,
303d1237fbSMarcel Apfelbaum     DEVICE_CATEGORY_MAX
313d1237fbSMarcel Apfelbaum } DeviceCategory;
323d1237fbSMarcel Apfelbaum 
333d1237fbSMarcel Apfelbaum static inline const char *qdev_category_get_name(DeviceCategory category)
343d1237fbSMarcel Apfelbaum {
353d1237fbSMarcel Apfelbaum     static const char *category_names[DEVICE_CATEGORY_MAX] = {
363d1237fbSMarcel Apfelbaum         [DEVICE_CATEGORY_BRIDGE]  = "Controller/Bridge/Hub",
373d1237fbSMarcel Apfelbaum         [DEVICE_CATEGORY_USB]     = "USB",
383d1237fbSMarcel Apfelbaum         [DEVICE_CATEGORY_STORAGE] = "Storage",
393d1237fbSMarcel Apfelbaum         [DEVICE_CATEGORY_NETWORK] = "Network",
403d1237fbSMarcel Apfelbaum         [DEVICE_CATEGORY_INPUT]   = "Input",
413d1237fbSMarcel Apfelbaum         [DEVICE_CATEGORY_DISPLAY] = "Display",
423d1237fbSMarcel Apfelbaum         [DEVICE_CATEGORY_SOUND]   = "Sound",
433d1237fbSMarcel Apfelbaum         [DEVICE_CATEGORY_MISC]    = "Misc",
443d1237fbSMarcel Apfelbaum     };
453d1237fbSMarcel Apfelbaum 
463d1237fbSMarcel Apfelbaum     return category_names[category];
473d1237fbSMarcel Apfelbaum };
483d1237fbSMarcel Apfelbaum 
49074a86fcSAnthony Liguori typedef int (*qdev_initfn)(DeviceState *dev);
50074a86fcSAnthony Liguori typedef int (*qdev_event)(DeviceState *dev);
51074a86fcSAnthony Liguori typedef void (*qdev_resetfn)(DeviceState *dev);
52249d4172SAndreas Färber typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
53249d4172SAndreas Färber typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
54074a86fcSAnthony Liguori 
55074a86fcSAnthony Liguori struct VMStateDescription;
56074a86fcSAnthony Liguori 
57249d4172SAndreas Färber /**
58249d4172SAndreas Färber  * DeviceClass:
59249d4172SAndreas Färber  * @props: Properties accessing state fields.
60249d4172SAndreas Färber  * @realize: Callback function invoked when the #DeviceState:realized
61249d4172SAndreas Färber  * property is changed to %true. The default invokes @init if not %NULL.
62249d4172SAndreas Färber  * @unrealize: Callback function invoked when the #DeviceState:realized
63249d4172SAndreas Färber  * property is changed to %false.
64249d4172SAndreas Färber  * @init: Callback function invoked when the #DeviceState::realized property
65249d4172SAndreas Färber  * is changed to %true. Deprecated, new types inheriting directly from
66249d4172SAndreas Färber  * TYPE_DEVICE should use @realize instead, new leaf types should consult
67249d4172SAndreas Färber  * their respective parent type.
68249d4172SAndreas Färber  *
69249d4172SAndreas Färber  * # Realization #
70249d4172SAndreas Färber  * Devices are constructed in two stages,
71249d4172SAndreas Färber  * 1) object instantiation via object_initialize() and
72249d4172SAndreas Färber  * 2) device realization via #DeviceState:realized property.
73249d4172SAndreas Färber  * The former may not fail (it might assert or exit), the latter may return
74249d4172SAndreas Färber  * error information to the caller and must be re-entrant.
75249d4172SAndreas Färber  * Trivial field initializations should go into #TypeInfo.instance_init.
76249d4172SAndreas Färber  * Operations depending on @props static properties should go into @realize.
77249d4172SAndreas Färber  * After successful realization, setting static properties will fail.
78249d4172SAndreas Färber  *
79249d4172SAndreas Färber  * As an interim step, the #DeviceState:realized property is set by deprecated
80249d4172SAndreas Färber  * functions qdev_init() and qdev_init_nofail().
81249d4172SAndreas Färber  * In the future, devices will propagate this state change to their children
82249d4172SAndreas Färber  * and along busses they expose.
83249d4172SAndreas Färber  * The point in time will be deferred to machine creation, so that values
84249d4172SAndreas Färber  * set in @realize will not be introspectable beforehand. Therefore devices
85249d4172SAndreas Färber  * must not create children during @realize; they should initialize them via
86249d4172SAndreas Färber  * object_initialize() in their own #TypeInfo.instance_init and forward the
87249d4172SAndreas Färber  * realization events appropriately.
88249d4172SAndreas Färber  *
89249d4172SAndreas Färber  * The @init callback is considered private to a particular bus implementation
90249d4172SAndreas Färber  * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an
91249d4172SAndreas Färber  * "init" callback on their parent class instead.
92782beb52SAndreas Färber  *
93249d4172SAndreas Färber  * Any type may override the @realize and/or @unrealize callbacks but needs
94782beb52SAndreas Färber  * to call the parent type's implementation if keeping their functionality
95782beb52SAndreas Färber  * is desired. Refer to QOM documentation for further discussion and examples.
96782beb52SAndreas Färber  *
97782beb52SAndreas Färber  * <note>
98782beb52SAndreas Färber  *   <para>
99249d4172SAndreas Färber  * If a type derived directly from TYPE_DEVICE implements @realize, it does
100249d4172SAndreas Färber  * not need to implement @init and therefore does not need to store and call
101249d4172SAndreas Färber  * #DeviceClass' default @realize callback.
102782beb52SAndreas Färber  * For other types consult the documentation and implementation of the
103782beb52SAndreas Färber  * respective parent types.
104782beb52SAndreas Färber  *   </para>
105782beb52SAndreas Färber  * </note>
106249d4172SAndreas Färber  */
107074a86fcSAnthony Liguori typedef struct DeviceClass {
108249d4172SAndreas Färber     /*< private >*/
109074a86fcSAnthony Liguori     ObjectClass parent_class;
110249d4172SAndreas Färber     /*< public >*/
111074a86fcSAnthony Liguori 
1123d1237fbSMarcel Apfelbaum     DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
113074a86fcSAnthony Liguori     const char *fw_name;
114074a86fcSAnthony Liguori     const char *desc;
115074a86fcSAnthony Liguori     Property *props;
116074a86fcSAnthony Liguori     int no_user;
117074a86fcSAnthony Liguori 
118074a86fcSAnthony Liguori     /* callbacks */
119074a86fcSAnthony Liguori     void (*reset)(DeviceState *dev);
120249d4172SAndreas Färber     DeviceRealize realize;
121249d4172SAndreas Färber     DeviceUnrealize unrealize;
122074a86fcSAnthony Liguori 
123074a86fcSAnthony Liguori     /* device state */
124074a86fcSAnthony Liguori     const struct VMStateDescription *vmsd;
125074a86fcSAnthony Liguori 
126074a86fcSAnthony Liguori     /* Private to qdev / bus.  */
127249d4172SAndreas Färber     qdev_initfn init; /* TODO remove, once users are converted to realize */
128074a86fcSAnthony Liguori     qdev_event unplug;
129fe6c2117SAndreas Färber     qdev_event exit; /* TODO remove, once users are converted to unrealize */
130074a86fcSAnthony Liguori     const char *bus_type;
131074a86fcSAnthony Liguori } DeviceClass;
132074a86fcSAnthony Liguori 
1337983c8a3SAndreas Färber /**
1347983c8a3SAndreas Färber  * DeviceState:
1357983c8a3SAndreas Färber  * @realized: Indicates whether the device has been fully constructed.
1367983c8a3SAndreas Färber  *
1377983c8a3SAndreas Färber  * This structure should not be accessed directly.  We declare it here
1387983c8a3SAndreas Färber  * so that it can be embedded in individual device state structures.
1397983c8a3SAndreas Färber  */
140074a86fcSAnthony Liguori struct DeviceState {
1417983c8a3SAndreas Färber     /*< private >*/
142074a86fcSAnthony Liguori     Object parent_obj;
1437983c8a3SAndreas Färber     /*< public >*/
144074a86fcSAnthony Liguori 
145074a86fcSAnthony Liguori     const char *id;
1467983c8a3SAndreas Färber     bool realized;
147074a86fcSAnthony Liguori     QemuOpts *opts;
148074a86fcSAnthony Liguori     int hotplugged;
149074a86fcSAnthony Liguori     BusState *parent_bus;
150074a86fcSAnthony Liguori     int num_gpio_out;
151074a86fcSAnthony Liguori     qemu_irq *gpio_out;
152074a86fcSAnthony Liguori     int num_gpio_in;
153074a86fcSAnthony Liguori     qemu_irq *gpio_in;
154074a86fcSAnthony Liguori     QLIST_HEAD(, BusState) child_bus;
155074a86fcSAnthony Liguori     int num_child_bus;
156074a86fcSAnthony Liguori     int instance_id_alias;
157074a86fcSAnthony Liguori     int alias_required_for_version;
158074a86fcSAnthony Liguori };
159074a86fcSAnthony Liguori 
160074a86fcSAnthony Liguori #define TYPE_BUS "bus"
161074a86fcSAnthony Liguori #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
162074a86fcSAnthony Liguori #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
163074a86fcSAnthony Liguori #define BUS_GET_CLASS(obj) OBJECT_GET_CLASS(BusClass, (obj), TYPE_BUS)
164074a86fcSAnthony Liguori 
165074a86fcSAnthony Liguori struct BusClass {
166074a86fcSAnthony Liguori     ObjectClass parent_class;
167074a86fcSAnthony Liguori 
168074a86fcSAnthony Liguori     /* FIXME first arg should be BusState */
169074a86fcSAnthony Liguori     void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
170074a86fcSAnthony Liguori     char *(*get_dev_path)(DeviceState *dev);
171074a86fcSAnthony Liguori     /*
172074a86fcSAnthony Liguori      * This callback is used to create Open Firmware device path in accordance
173074a86fcSAnthony Liguori      * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
174074a86fcSAnthony Liguori      * bindings can be found at http://playground.sun.com/1275/bindings/.
175074a86fcSAnthony Liguori      */
176074a86fcSAnthony Liguori     char *(*get_fw_dev_path)(DeviceState *dev);
177074a86fcSAnthony Liguori     int (*reset)(BusState *bus);
1781395af6fSKONRAD Frederic     /* maximum devices allowed on the bus, 0: no limit. */
1791395af6fSKONRAD Frederic     int max_dev;
180074a86fcSAnthony Liguori };
181074a86fcSAnthony Liguori 
182074a86fcSAnthony Liguori typedef struct BusChild {
183074a86fcSAnthony Liguori     DeviceState *child;
184074a86fcSAnthony Liguori     int index;
185074a86fcSAnthony Liguori     QTAILQ_ENTRY(BusChild) sibling;
186074a86fcSAnthony Liguori } BusChild;
187074a86fcSAnthony Liguori 
188074a86fcSAnthony Liguori /**
189074a86fcSAnthony Liguori  * BusState:
190074a86fcSAnthony Liguori  */
191074a86fcSAnthony Liguori struct BusState {
192074a86fcSAnthony Liguori     Object obj;
193074a86fcSAnthony Liguori     DeviceState *parent;
194074a86fcSAnthony Liguori     const char *name;
195074a86fcSAnthony Liguori     int allow_hotplug;
196074a86fcSAnthony Liguori     int max_index;
197074a86fcSAnthony Liguori     QTAILQ_HEAD(ChildrenHead, BusChild) children;
198074a86fcSAnthony Liguori     QLIST_ENTRY(BusState) sibling;
199074a86fcSAnthony Liguori };
200074a86fcSAnthony Liguori 
201074a86fcSAnthony Liguori struct Property {
202074a86fcSAnthony Liguori     const char   *name;
203074a86fcSAnthony Liguori     PropertyInfo *info;
204074a86fcSAnthony Liguori     int          offset;
205074a86fcSAnthony Liguori     uint8_t      bitnr;
206074a86fcSAnthony Liguori     uint8_t      qtype;
207074a86fcSAnthony Liguori     int64_t      defval;
2080be6bfacSPeter Maydell     int          arrayoffset;
2090be6bfacSPeter Maydell     PropertyInfo *arrayinfo;
2100be6bfacSPeter Maydell     int          arrayfieldsize;
211074a86fcSAnthony Liguori };
212074a86fcSAnthony Liguori 
213074a86fcSAnthony Liguori struct PropertyInfo {
214074a86fcSAnthony Liguori     const char *name;
215074a86fcSAnthony Liguori     const char *legacy_name;
216074a86fcSAnthony Liguori     const char **enum_table;
217074a86fcSAnthony Liguori     int (*parse)(DeviceState *dev, Property *prop, const char *str);
218074a86fcSAnthony Liguori     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
219074a86fcSAnthony Liguori     ObjectPropertyAccessor *get;
220074a86fcSAnthony Liguori     ObjectPropertyAccessor *set;
221074a86fcSAnthony Liguori     ObjectPropertyRelease *release;
222074a86fcSAnthony Liguori };
223074a86fcSAnthony Liguori 
224074a86fcSAnthony Liguori typedef struct GlobalProperty {
225074a86fcSAnthony Liguori     const char *driver;
226074a86fcSAnthony Liguori     const char *property;
227074a86fcSAnthony Liguori     const char *value;
228074a86fcSAnthony Liguori     QTAILQ_ENTRY(GlobalProperty) next;
229074a86fcSAnthony Liguori } GlobalProperty;
230074a86fcSAnthony Liguori 
231074a86fcSAnthony Liguori /*** Board API.  This should go away once we have a machine config file.  ***/
232074a86fcSAnthony Liguori 
233074a86fcSAnthony Liguori DeviceState *qdev_create(BusState *bus, const char *name);
234074a86fcSAnthony Liguori DeviceState *qdev_try_create(BusState *bus, const char *name);
235074a86fcSAnthony Liguori int qdev_init(DeviceState *dev) QEMU_WARN_UNUSED_RESULT;
236074a86fcSAnthony Liguori void qdev_init_nofail(DeviceState *dev);
237074a86fcSAnthony Liguori void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
238074a86fcSAnthony Liguori                                  int required_for_version);
239074a86fcSAnthony Liguori void qdev_unplug(DeviceState *dev, Error **errp);
240074a86fcSAnthony Liguori void qdev_free(DeviceState *dev);
241074a86fcSAnthony Liguori int qdev_simple_unplug_cb(DeviceState *dev);
242074a86fcSAnthony Liguori void qdev_machine_creation_done(void);
243074a86fcSAnthony Liguori bool qdev_machine_modified(void);
244074a86fcSAnthony Liguori 
245074a86fcSAnthony Liguori qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
246074a86fcSAnthony Liguori void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
247074a86fcSAnthony Liguori 
248074a86fcSAnthony Liguori BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
249074a86fcSAnthony Liguori 
250074a86fcSAnthony Liguori /*** Device API.  ***/
251074a86fcSAnthony Liguori 
252074a86fcSAnthony Liguori /* Register device properties.  */
253074a86fcSAnthony Liguori /* GPIO inputs also double as IRQ sinks.  */
254074a86fcSAnthony Liguori void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
255074a86fcSAnthony Liguori void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
256074a86fcSAnthony Liguori 
257074a86fcSAnthony Liguori BusState *qdev_get_parent_bus(DeviceState *dev);
258074a86fcSAnthony Liguori 
259074a86fcSAnthony Liguori /*** BUS API. ***/
260074a86fcSAnthony Liguori 
261074a86fcSAnthony Liguori DeviceState *qdev_find_recursive(BusState *bus, const char *id);
262074a86fcSAnthony Liguori 
263074a86fcSAnthony Liguori /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
264074a86fcSAnthony Liguori typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
265074a86fcSAnthony Liguori typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
266074a86fcSAnthony Liguori 
267*fb17dfe0SAndreas Färber void qbus_create_inplace(void *bus, size_t size, const char *typename,
268074a86fcSAnthony Liguori                          DeviceState *parent, const char *name);
269074a86fcSAnthony Liguori BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
270074a86fcSAnthony Liguori /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
271074a86fcSAnthony Liguori  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
272074a86fcSAnthony Liguori  *           0 otherwise. */
273074a86fcSAnthony Liguori int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
274074a86fcSAnthony Liguori                        qbus_walkerfn *busfn, void *opaque);
275074a86fcSAnthony Liguori int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
276074a86fcSAnthony Liguori                        qbus_walkerfn *busfn, void *opaque);
277074a86fcSAnthony Liguori void qdev_reset_all(DeviceState *dev);
278d0508c36SPaolo Bonzini 
279d0508c36SPaolo Bonzini /**
280d0508c36SPaolo Bonzini  * @qbus_reset_all:
281d0508c36SPaolo Bonzini  * @bus: Bus to be reset.
282d0508c36SPaolo Bonzini  *
283d0508c36SPaolo Bonzini  * Reset @bus and perform a bus-level ("hard") reset of all devices connected
284d0508c36SPaolo Bonzini  * to it, including recursive processing of all buses below @bus itself.  A
285d0508c36SPaolo Bonzini  * hard reset means that qbus_reset_all will reset all state of the device.
286d0508c36SPaolo Bonzini  * For PCI devices, for example, this will include the base address registers
287d0508c36SPaolo Bonzini  * or configuration space.
288d0508c36SPaolo Bonzini  */
289d0508c36SPaolo Bonzini void qbus_reset_all(BusState *bus);
290074a86fcSAnthony Liguori void qbus_reset_all_fn(void *opaque);
291074a86fcSAnthony Liguori 
292074a86fcSAnthony Liguori void qbus_free(BusState *bus);
293074a86fcSAnthony Liguori 
294074a86fcSAnthony Liguori /* This should go away once we get rid of the NULL bus hack */
295074a86fcSAnthony Liguori BusState *sysbus_get_default(void);
296074a86fcSAnthony Liguori 
297074a86fcSAnthony Liguori char *qdev_get_fw_dev_path(DeviceState *dev);
298074a86fcSAnthony Liguori 
299074a86fcSAnthony Liguori /**
300074a86fcSAnthony Liguori  * @qdev_machine_init
301074a86fcSAnthony Liguori  *
302074a86fcSAnthony Liguori  * Initialize platform devices before machine init.  This is a hack until full
303074a86fcSAnthony Liguori  * support for composition is added.
304074a86fcSAnthony Liguori  */
305074a86fcSAnthony Liguori void qdev_machine_init(void);
306074a86fcSAnthony Liguori 
307074a86fcSAnthony Liguori /**
308074a86fcSAnthony Liguori  * @device_reset
309074a86fcSAnthony Liguori  *
310074a86fcSAnthony Liguori  * Reset a single device (by calling the reset method).
311074a86fcSAnthony Liguori  */
312074a86fcSAnthony Liguori void device_reset(DeviceState *dev);
313074a86fcSAnthony Liguori 
314074a86fcSAnthony Liguori const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
315074a86fcSAnthony Liguori 
316074a86fcSAnthony Liguori const char *qdev_fw_name(DeviceState *dev);
317074a86fcSAnthony Liguori 
318074a86fcSAnthony Liguori Object *qdev_get_machine(void);
319074a86fcSAnthony Liguori 
320074a86fcSAnthony Liguori /* FIXME: make this a link<> */
321074a86fcSAnthony Liguori void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
322074a86fcSAnthony Liguori 
323074a86fcSAnthony Liguori extern int qdev_hotplug;
324074a86fcSAnthony Liguori 
325074a86fcSAnthony Liguori char *qdev_get_dev_path(DeviceState *dev);
326074a86fcSAnthony Liguori 
327074a86fcSAnthony Liguori #endif
328