qdev-core.h 14 KB

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