qdev-core.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #ifndef QDEV_CORE_H
  2. #define QDEV_CORE_H
  3. #include "qemu/queue.h"
  4. #include "qemu/bitmap.h"
  5. #include "qom/object.h"
  6. #include "hw/irq.h"
  7. #include "hw/hotplug.h"
  8. enum {
  9. DEV_NVECTORS_UNSPECIFIED = -1,
  10. };
  11. #define TYPE_DEVICE "device"
  12. #define DEVICE(obj) OBJECT_CHECK(DeviceState, (obj), TYPE_DEVICE)
  13. #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE)
  14. #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE)
  15. typedef enum DeviceCategory {
  16. DEVICE_CATEGORY_BRIDGE,
  17. DEVICE_CATEGORY_USB,
  18. DEVICE_CATEGORY_STORAGE,
  19. DEVICE_CATEGORY_NETWORK,
  20. DEVICE_CATEGORY_INPUT,
  21. DEVICE_CATEGORY_DISPLAY,
  22. DEVICE_CATEGORY_SOUND,
  23. DEVICE_CATEGORY_MISC,
  24. DEVICE_CATEGORY_CPU,
  25. DEVICE_CATEGORY_MAX
  26. } DeviceCategory;
  27. typedef int (*qdev_initfn)(DeviceState *dev);
  28. typedef int (*qdev_event)(DeviceState *dev);
  29. typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
  30. typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
  31. typedef void (*DeviceReset)(DeviceState *dev);
  32. typedef void (*BusRealize)(BusState *bus, Error **errp);
  33. typedef void (*BusUnrealize)(BusState *bus, Error **errp);
  34. struct VMStateDescription;
  35. /**
  36. * DeviceClass:
  37. * @props: Properties accessing state fields.
  38. * @realize: Callback function invoked when the #DeviceState:realized
  39. * property is changed to %true. The default invokes @init if not %NULL.
  40. * @unrealize: Callback function invoked when the #DeviceState:realized
  41. * property is changed to %false.
  42. * @init: Callback function invoked when the #DeviceState::realized property
  43. * is changed to %true. Deprecated, new types inheriting directly from
  44. * TYPE_DEVICE should use @realize instead, new leaf types should consult
  45. * their respective parent type.
  46. * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
  47. * as readonly "hotpluggable" property of #DeviceState instance
  48. *
  49. * # Realization #
  50. * Devices are constructed in two stages,
  51. * 1) object instantiation via object_initialize() and
  52. * 2) device realization via #DeviceState:realized property.
  53. * The former may not fail (it might assert or exit), the latter may return
  54. * error information to the caller and must be re-entrant.
  55. * Trivial field initializations should go into #TypeInfo.instance_init.
  56. * Operations depending on @props static properties should go into @realize.
  57. * After successful realization, setting static properties will fail.
  58. *
  59. * As an interim step, the #DeviceState:realized property can also be
  60. * set with qdev_init_nofail().
  61. * In the future, devices will propagate this state change to their children
  62. * and along busses they expose.
  63. * The point in time will be deferred to machine creation, so that values
  64. * set in @realize will not be introspectable beforehand. Therefore devices
  65. * must not create children during @realize; they should initialize them via
  66. * object_initialize() in their own #TypeInfo.instance_init and forward the
  67. * realization events appropriately.
  68. *
  69. * The @init callback is considered private to a particular bus implementation
  70. * (immediate abstract child types of TYPE_DEVICE). Derived leaf types set an
  71. * "init" callback on their parent class instead.
  72. *
  73. * Any type may override the @realize and/or @unrealize callbacks but needs
  74. * to call the parent type's implementation if keeping their functionality
  75. * is desired. Refer to QOM documentation for further discussion and examples.
  76. *
  77. * <note>
  78. * <para>
  79. * If a type derived directly from TYPE_DEVICE implements @realize, it does
  80. * not need to implement @init and therefore does not need to store and call
  81. * #DeviceClass' default @realize callback.
  82. * For other types consult the documentation and implementation of the
  83. * respective parent types.
  84. * </para>
  85. * </note>
  86. */
  87. typedef struct DeviceClass {
  88. /*< private >*/
  89. ObjectClass parent_class;
  90. /*< public >*/
  91. DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
  92. const char *fw_name;
  93. const char *desc;
  94. Property *props;
  95. /*
  96. * Can this device be instantiated with -device / device_add?
  97. * All devices should support instantiation with device_add, and
  98. * this flag should not exist. But we're not there, yet. Some
  99. * devices fail to instantiate with cryptic error messages.
  100. * Others instantiate, but don't work. Exposing users to such
  101. * behavior would be cruel; clearing this flag will protect them.
  102. * It should never be cleared without a comment explaining why it
  103. * is cleared.
  104. * TODO remove once we're there
  105. */
  106. bool user_creatable;
  107. bool hotpluggable;
  108. /* callbacks */
  109. DeviceReset reset;
  110. DeviceRealize realize;
  111. DeviceUnrealize unrealize;
  112. /* device state */
  113. const struct VMStateDescription *vmsd;
  114. /* Private to qdev / bus. */
  115. qdev_initfn init; /* TODO remove, once users are converted to realize */
  116. qdev_event exit; /* TODO remove, once users are converted to unrealize */
  117. const char *bus_type;
  118. } DeviceClass;
  119. typedef struct NamedGPIOList NamedGPIOList;
  120. struct NamedGPIOList {
  121. char *name;
  122. qemu_irq *in;
  123. int num_in;
  124. int num_out;
  125. QLIST_ENTRY(NamedGPIOList) node;
  126. };
  127. /**
  128. * DeviceState:
  129. * @realized: Indicates whether the device has been fully constructed.
  130. *
  131. * This structure should not be accessed directly. We declare it here
  132. * so that it can be embedded in individual device state structures.
  133. */
  134. struct DeviceState {
  135. /*< private >*/
  136. Object parent_obj;
  137. /*< public >*/
  138. const char *id;
  139. char *canonical_path;
  140. bool realized;
  141. bool pending_deleted_event;
  142. QemuOpts *opts;
  143. int hotplugged;
  144. BusState *parent_bus;
  145. QLIST_HEAD(, NamedGPIOList) gpios;
  146. QLIST_HEAD(, BusState) child_bus;
  147. int num_child_bus;
  148. int instance_id_alias;
  149. int alias_required_for_version;
  150. };
  151. struct DeviceListener {
  152. void (*realize)(DeviceListener *listener, DeviceState *dev);
  153. void (*unrealize)(DeviceListener *listener, DeviceState *dev);
  154. QTAILQ_ENTRY(DeviceListener) link;
  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. struct BusClass {
  161. ObjectClass parent_class;
  162. /* FIXME first arg should be BusState */
  163. void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
  164. char *(*get_dev_path)(DeviceState *dev);
  165. /*
  166. * This callback is used to create Open Firmware device path in accordance
  167. * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
  168. * bindings can be found at http://playground.sun.com/1275/bindings/.
  169. */
  170. char *(*get_fw_dev_path)(DeviceState *dev);
  171. void (*reset)(BusState *bus);
  172. BusRealize realize;
  173. BusUnrealize unrealize;
  174. /* maximum devices allowed on the bus, 0: no limit. */
  175. int max_dev;
  176. /* number of automatically allocated bus ids (e.g. ide.0) */
  177. int automatic_ids;
  178. };
  179. typedef struct BusChild {
  180. DeviceState *child;
  181. int index;
  182. QTAILQ_ENTRY(BusChild) sibling;
  183. } BusChild;
  184. #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
  185. /**
  186. * BusState:
  187. * @hotplug_device: link to a hotplug device associated with bus.
  188. */
  189. struct BusState {
  190. Object obj;
  191. DeviceState *parent;
  192. char *name;
  193. HotplugHandler *hotplug_handler;
  194. int max_index;
  195. bool realized;
  196. QTAILQ_HEAD(ChildrenHead, BusChild) children;
  197. QLIST_ENTRY(BusState) sibling;
  198. };
  199. /**
  200. * Property:
  201. * @set_default: true if the default value should be set from @defval,
  202. * in which case @info->set_default_value must not be NULL
  203. * (if false then no default value is set by the property system
  204. * and the field retains whatever value it was given by instance_init).
  205. * @defval: default value for the property. This is used only if @set_default
  206. * is true.
  207. */
  208. struct Property {
  209. const char *name;
  210. const PropertyInfo *info;
  211. ptrdiff_t offset;
  212. uint8_t bitnr;
  213. bool set_default;
  214. union {
  215. int64_t i;
  216. uint64_t u;
  217. } defval;
  218. int arrayoffset;
  219. const PropertyInfo *arrayinfo;
  220. int arrayfieldsize;
  221. const char *link_type;
  222. };
  223. struct PropertyInfo {
  224. const char *name;
  225. const char *description;
  226. const QEnumLookup *enum_table;
  227. int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
  228. void (*set_default_value)(Object *obj, const Property *prop);
  229. void (*create)(Object *obj, Property *prop, Error **errp);
  230. ObjectPropertyAccessor *get;
  231. ObjectPropertyAccessor *set;
  232. ObjectPropertyRelease *release;
  233. };
  234. /**
  235. * GlobalProperty:
  236. * @user_provided: Set to true if property comes from user-provided config
  237. * (command-line or config file).
  238. * @used: Set to true if property was used when initializing a device.
  239. * @errp: Error destination, used like first argument of error_setg()
  240. * in case property setting fails later. If @errp is NULL, we
  241. * print warnings instead of ignoring errors silently. For
  242. * hotplugged devices, errp is always ignored and warnings are
  243. * printed instead.
  244. */
  245. typedef struct GlobalProperty {
  246. const char *driver;
  247. const char *property;
  248. const char *value;
  249. bool user_provided;
  250. bool used;
  251. Error **errp;
  252. } GlobalProperty;
  253. /*** Board API. This should go away once we have a machine config file. ***/
  254. DeviceState *qdev_create(BusState *bus, const char *name);
  255. DeviceState *qdev_try_create(BusState *bus, const char *name);
  256. void qdev_init_nofail(DeviceState *dev);
  257. void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
  258. int required_for_version);
  259. HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
  260. HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
  261. void qdev_unplug(DeviceState *dev, Error **errp);
  262. void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
  263. DeviceState *dev, Error **errp);
  264. void qdev_machine_creation_done(void);
  265. bool qdev_machine_modified(void);
  266. qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
  267. qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
  268. void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
  269. void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
  270. qemu_irq pin);
  271. qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
  272. qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
  273. const char *name, int n);
  274. BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
  275. /*** Device API. ***/
  276. /* Register device properties. */
  277. /* GPIO inputs also double as IRQ sinks. */
  278. void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
  279. void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
  280. void qdev_init_gpio_in_named(DeviceState *dev, qemu_irq_handler handler,
  281. const char *name, int n);
  282. void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
  283. const char *name, int n);
  284. void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
  285. const char *name);
  286. BusState *qdev_get_parent_bus(DeviceState *dev);
  287. /*** BUS API. ***/
  288. DeviceState *qdev_find_recursive(BusState *bus, const char *id);
  289. /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
  290. typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
  291. typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
  292. void qbus_create_inplace(void *bus, size_t size, const char *typename,
  293. DeviceState *parent, const char *name);
  294. BusState *qbus_create(const char *typename, DeviceState *parent, const char *name);
  295. /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
  296. * < 0 if either devfn or busfn terminate walk somewhere in cursion,
  297. * 0 otherwise. */
  298. int qbus_walk_children(BusState *bus,
  299. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  300. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  301. void *opaque);
  302. int qdev_walk_children(DeviceState *dev,
  303. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  304. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  305. void *opaque);
  306. void qdev_reset_all(DeviceState *dev);
  307. void qdev_reset_all_fn(void *opaque);
  308. /**
  309. * @qbus_reset_all:
  310. * @bus: Bus to be reset.
  311. *
  312. * Reset @bus and perform a bus-level ("hard") reset of all devices connected
  313. * to it, including recursive processing of all buses below @bus itself. A
  314. * hard reset means that qbus_reset_all will reset all state of the device.
  315. * For PCI devices, for example, this will include the base address registers
  316. * or configuration space.
  317. */
  318. void qbus_reset_all(BusState *bus);
  319. void qbus_reset_all_fn(void *opaque);
  320. /* This should go away once we get rid of the NULL bus hack */
  321. BusState *sysbus_get_default(void);
  322. char *qdev_get_fw_dev_path(DeviceState *dev);
  323. char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
  324. /**
  325. * @qdev_machine_init
  326. *
  327. * Initialize platform devices before machine init. This is a hack until full
  328. * support for composition is added.
  329. */
  330. void qdev_machine_init(void);
  331. /**
  332. * @device_reset
  333. *
  334. * Reset a single device (by calling the reset method).
  335. */
  336. void device_reset(DeviceState *dev);
  337. void device_class_set_parent_reset(DeviceClass *dc,
  338. DeviceReset dev_reset,
  339. DeviceReset *parent_reset);
  340. void device_class_set_parent_realize(DeviceClass *dc,
  341. DeviceRealize dev_realize,
  342. DeviceRealize *parent_realize);
  343. void device_class_set_parent_unrealize(DeviceClass *dc,
  344. DeviceUnrealize dev_unrealize,
  345. DeviceUnrealize *parent_unrealize);
  346. const struct VMStateDescription *qdev_get_vmsd(DeviceState *dev);
  347. const char *qdev_fw_name(DeviceState *dev);
  348. Object *qdev_get_machine(void);
  349. /* FIXME: make this a link<> */
  350. void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
  351. extern bool qdev_hotplug;
  352. extern bool qdev_hot_removed;
  353. char *qdev_get_dev_path(DeviceState *dev);
  354. GSList *qdev_build_hotpluggable_device_list(Object *peripheral);
  355. void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler,
  356. Error **errp);
  357. void qbus_set_bus_hotplug_handler(BusState *bus, Error **errp);
  358. static inline bool qbus_is_hotpluggable(BusState *bus)
  359. {
  360. return bus->hotplug_handler;
  361. }
  362. void device_listener_register(DeviceListener *listener);
  363. void device_listener_unregister(DeviceListener *listener);
  364. #endif