qdev-core.h 9.6 KB

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