qdev-core.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. #ifndef QDEV_CORE_H
  2. #define QDEV_CORE_H
  3. #include "qemu/queue.h"
  4. #include "qemu/bitmap.h"
  5. #include "qemu/rcu.h"
  6. #include "qemu/rcu_queue.h"
  7. #include "qom/object.h"
  8. #include "hw/hotplug.h"
  9. #include "hw/resettable.h"
  10. enum {
  11. DEV_NVECTORS_UNSPECIFIED = -1,
  12. };
  13. #define TYPE_DEVICE "device"
  14. OBJECT_DECLARE_TYPE(DeviceState, DeviceClass, 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_WATCHDOG,
  26. DEVICE_CATEGORY_MAX
  27. } DeviceCategory;
  28. typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
  29. typedef void (*DeviceUnrealize)(DeviceState *dev);
  30. typedef void (*DeviceReset)(DeviceState *dev);
  31. typedef void (*BusRealize)(BusState *bus, Error **errp);
  32. typedef void (*BusUnrealize)(BusState *bus);
  33. /**
  34. * DeviceClass:
  35. * @props: Properties accessing state fields.
  36. * @realize: Callback function invoked when the #DeviceState:realized
  37. * property is changed to %true.
  38. * @unrealize: Callback function invoked when the #DeviceState:realized
  39. * property is changed to %false.
  40. * @hotpluggable: indicates if #DeviceClass is hotpluggable, available
  41. * as readonly "hotpluggable" property of #DeviceState instance
  42. *
  43. * # Realization #
  44. * Devices are constructed in two stages,
  45. * 1) object instantiation via object_initialize() and
  46. * 2) device realization via #DeviceState:realized property.
  47. * The former may not fail (and must not abort or exit, since it is called
  48. * during device introspection already), and the latter may return error
  49. * information to the caller and must be re-entrant.
  50. * Trivial field initializations should go into #TypeInfo.instance_init.
  51. * Operations depending on @props static properties should go into @realize.
  52. * After successful realization, setting static properties will fail.
  53. *
  54. * As an interim step, the #DeviceState:realized property can also be
  55. * set with qdev_realize().
  56. * In the future, devices will propagate this state change to their children
  57. * and along busses they expose.
  58. * The point in time will be deferred to machine creation, so that values
  59. * set in @realize will not be introspectable beforehand. Therefore devices
  60. * must not create children during @realize; they should initialize them via
  61. * object_initialize() in their own #TypeInfo.instance_init and forward the
  62. * realization events appropriately.
  63. *
  64. * Any type may override the @realize and/or @unrealize callbacks but needs
  65. * to call the parent type's implementation if keeping their functionality
  66. * is desired. Refer to QOM documentation for further discussion and examples.
  67. *
  68. * <note>
  69. * <para>
  70. * Since TYPE_DEVICE doesn't implement @realize and @unrealize, types
  71. * derived directly from it need not call their parent's @realize and
  72. * @unrealize.
  73. * For other types consult the documentation and implementation of the
  74. * respective parent types.
  75. * </para>
  76. * </note>
  77. *
  78. * # Hiding a device #
  79. * To hide a device, a DeviceListener function hide_device() needs to
  80. * be registered.
  81. * It can be used to defer adding a device and therefore hide it from
  82. * the guest. The handler registering to this DeviceListener can save
  83. * the QOpts passed to it for re-using it later. It must return if it
  84. * wants the device to be hidden or visible. When the handler function
  85. * decides the device shall be visible it will be added with
  86. * qdev_device_add() and realized as any other device. Otherwise
  87. * qdev_device_add() will return early without adding the device. The
  88. * guest will not see a "hidden" device until it was marked visible
  89. * and qdev_device_add called again.
  90. *
  91. */
  92. struct DeviceClass {
  93. /*< private >*/
  94. ObjectClass parent_class;
  95. /*< public >*/
  96. DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX);
  97. const char *fw_name;
  98. const char *desc;
  99. /*
  100. * The underscore at the end ensures a compile-time error if someone
  101. * assigns to dc->props instead of using device_class_set_props.
  102. */
  103. Property *props_;
  104. /*
  105. * Can this device be instantiated with -device / device_add?
  106. * All devices should support instantiation with device_add, and
  107. * this flag should not exist. But we're not there, yet. Some
  108. * devices fail to instantiate with cryptic error messages.
  109. * Others instantiate, but don't work. Exposing users to such
  110. * behavior would be cruel; clearing this flag will protect them.
  111. * It should never be cleared without a comment explaining why it
  112. * is cleared.
  113. * TODO remove once we're there
  114. */
  115. bool user_creatable;
  116. bool hotpluggable;
  117. /* callbacks */
  118. /*
  119. * Reset method here is deprecated and replaced by methods in the
  120. * resettable class interface to implement a multi-phase reset.
  121. * TODO: remove once every reset callback is unused
  122. */
  123. DeviceReset reset;
  124. DeviceRealize realize;
  125. DeviceUnrealize unrealize;
  126. /* device state */
  127. const VMStateDescription *vmsd;
  128. /* Private to qdev / bus. */
  129. const char *bus_type;
  130. };
  131. typedef struct NamedGPIOList NamedGPIOList;
  132. struct NamedGPIOList {
  133. char *name;
  134. qemu_irq *in;
  135. int num_in;
  136. int num_out;
  137. QLIST_ENTRY(NamedGPIOList) node;
  138. };
  139. typedef struct Clock Clock;
  140. typedef struct NamedClockList NamedClockList;
  141. struct NamedClockList {
  142. char *name;
  143. Clock *clock;
  144. bool output;
  145. bool alias;
  146. QLIST_ENTRY(NamedClockList) node;
  147. };
  148. /**
  149. * DeviceState:
  150. * @realized: Indicates whether the device has been fully constructed.
  151. * When accessed outside big qemu lock, must be accessed with
  152. * qatomic_load_acquire()
  153. * @reset: ResettableState for the device; handled by Resettable interface.
  154. *
  155. * This structure should not be accessed directly. We declare it here
  156. * so that it can be embedded in individual device state structures.
  157. */
  158. struct DeviceState {
  159. /*< private >*/
  160. Object parent_obj;
  161. /*< public >*/
  162. char *id;
  163. char *canonical_path;
  164. bool realized;
  165. bool pending_deleted_event;
  166. int64_t pending_deleted_expires_ms;
  167. QDict *opts;
  168. int hotplugged;
  169. bool allow_unplug_during_migration;
  170. BusState *parent_bus;
  171. QLIST_HEAD(, NamedGPIOList) gpios;
  172. QLIST_HEAD(, NamedClockList) clocks;
  173. QLIST_HEAD(, BusState) child_bus;
  174. int num_child_bus;
  175. int instance_id_alias;
  176. int alias_required_for_version;
  177. ResettableState reset;
  178. GSList *unplug_blockers;
  179. };
  180. struct DeviceListener {
  181. void (*realize)(DeviceListener *listener, DeviceState *dev);
  182. void (*unrealize)(DeviceListener *listener, DeviceState *dev);
  183. /*
  184. * This callback is called upon init of the DeviceState and
  185. * informs qdev if a device should be visible or hidden. We can
  186. * hide a failover device depending for example on the device
  187. * opts.
  188. *
  189. * On errors, it returns false and errp is set. Device creation
  190. * should fail in this case.
  191. */
  192. bool (*hide_device)(DeviceListener *listener, const QDict *device_opts,
  193. bool from_json, Error **errp);
  194. QTAILQ_ENTRY(DeviceListener) link;
  195. };
  196. #define TYPE_BUS "bus"
  197. DECLARE_OBJ_CHECKERS(BusState, BusClass,
  198. BUS, TYPE_BUS)
  199. struct BusClass {
  200. ObjectClass parent_class;
  201. /* FIXME first arg should be BusState */
  202. void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
  203. char *(*get_dev_path)(DeviceState *dev);
  204. /*
  205. * This callback is used to create Open Firmware device path in accordance
  206. * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
  207. * bindings can be found at http://playground.sun.com/1275/bindings/.
  208. */
  209. char *(*get_fw_dev_path)(DeviceState *dev);
  210. void (*reset)(BusState *bus);
  211. /*
  212. * Return whether the device can be added to @bus,
  213. * based on the address that was set (via device properties)
  214. * before realize. If not, on return @errp contains the
  215. * human-readable error message.
  216. */
  217. bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
  218. BusRealize realize;
  219. BusUnrealize unrealize;
  220. /* maximum devices allowed on the bus, 0: no limit. */
  221. int max_dev;
  222. /* number of automatically allocated bus ids (e.g. ide.0) */
  223. int automatic_ids;
  224. };
  225. typedef struct BusChild {
  226. struct rcu_head rcu;
  227. DeviceState *child;
  228. int index;
  229. QTAILQ_ENTRY(BusChild) sibling;
  230. } BusChild;
  231. #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
  232. /**
  233. * BusState:
  234. * @hotplug_handler: link to a hotplug handler associated with bus.
  235. * @reset: ResettableState for the bus; handled by Resettable interface.
  236. */
  237. struct BusState {
  238. Object obj;
  239. DeviceState *parent;
  240. char *name;
  241. HotplugHandler *hotplug_handler;
  242. int max_index;
  243. bool realized;
  244. bool full;
  245. int num_children;
  246. /*
  247. * children is a RCU QTAILQ, thus readers must use RCU to access it,
  248. * and writers must hold the big qemu lock
  249. */
  250. QTAILQ_HEAD(, BusChild) children;
  251. QLIST_ENTRY(BusState) sibling;
  252. ResettableState reset;
  253. };
  254. /**
  255. * GlobalProperty:
  256. * @used: Set to true if property was used when initializing a device.
  257. * @optional: If set to true, GlobalProperty will be skipped without errors
  258. * if the property doesn't exist.
  259. *
  260. * An error is fatal for non-hotplugged devices, when the global is applied.
  261. */
  262. typedef struct GlobalProperty {
  263. const char *driver;
  264. const char *property;
  265. const char *value;
  266. bool used;
  267. bool optional;
  268. } GlobalProperty;
  269. static inline void
  270. compat_props_add(GPtrArray *arr,
  271. GlobalProperty props[], size_t nelem)
  272. {
  273. int i;
  274. for (i = 0; i < nelem; i++) {
  275. g_ptr_array_add(arr, (void *)&props[i]);
  276. }
  277. }
  278. /*** Board API. This should go away once we have a machine config file. ***/
  279. /**
  280. * qdev_new: Create a device on the heap
  281. * @name: device type to create (we assert() that this type exists)
  282. *
  283. * This only allocates the memory and initializes the device state
  284. * structure, ready for the caller to set properties if they wish.
  285. * The device still needs to be realized.
  286. * The returned object has a reference count of 1.
  287. */
  288. DeviceState *qdev_new(const char *name);
  289. /**
  290. * qdev_try_new: Try to create a device on the heap
  291. * @name: device type to create
  292. *
  293. * This is like qdev_new(), except it returns %NULL when type @name
  294. * does not exist, rather than asserting.
  295. */
  296. DeviceState *qdev_try_new(const char *name);
  297. /**
  298. * qdev_realize: Realize @dev.
  299. * @dev: device to realize
  300. * @bus: bus to plug it into (may be NULL)
  301. * @errp: pointer to error object
  302. *
  303. * "Realize" the device, i.e. perform the second phase of device
  304. * initialization.
  305. * @dev must not be plugged into a bus already.
  306. * If @bus, plug @dev into @bus. This takes a reference to @dev.
  307. * If @dev has no QOM parent, make one up, taking another reference.
  308. * On success, return true.
  309. * On failure, store an error through @errp and return false.
  310. *
  311. * If you created @dev using qdev_new(), you probably want to use
  312. * qdev_realize_and_unref() instead.
  313. */
  314. bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
  315. /**
  316. * qdev_realize_and_unref: Realize @dev and drop a reference
  317. * @dev: device to realize
  318. * @bus: bus to plug it into (may be NULL)
  319. * @errp: pointer to error object
  320. *
  321. * Realize @dev and drop a reference.
  322. * This is like qdev_realize(), except the caller must hold a
  323. * (private) reference, which is dropped on return regardless of
  324. * success or failure. Intended use::
  325. *
  326. * dev = qdev_new();
  327. * [...]
  328. * qdev_realize_and_unref(dev, bus, errp);
  329. *
  330. * Now @dev can go away without further ado.
  331. *
  332. * If you are embedding the device into some other QOM device and
  333. * initialized it via some variant on object_initialize_child() then
  334. * do not use this function, because that family of functions arrange
  335. * for the only reference to the child device to be held by the parent
  336. * via the child<> property, and so the reference-count-drop done here
  337. * would be incorrect. For that use case you want qdev_realize().
  338. */
  339. bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
  340. /**
  341. * qdev_unrealize: Unrealize a device
  342. * @dev: device to unrealize
  343. *
  344. * This function will "unrealize" a device, which is the first phase
  345. * of correctly destroying a device that has been realized. It will:
  346. *
  347. * - unrealize any child buses by calling qbus_unrealize()
  348. * (this will recursively unrealize any devices on those buses)
  349. * - call the unrealize method of @dev
  350. *
  351. * The device can then be freed by causing its reference count to go
  352. * to zero.
  353. *
  354. * Warning: most devices in QEMU do not expect to be unrealized. Only
  355. * devices which are hot-unpluggable should be unrealized (as part of
  356. * the unplugging process); all other devices are expected to last for
  357. * the life of the simulation and should not be unrealized and freed.
  358. */
  359. void qdev_unrealize(DeviceState *dev);
  360. void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
  361. int required_for_version);
  362. HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
  363. HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
  364. bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
  365. /**
  366. * qdev_get_hotplug_handler: Get handler responsible for device wiring
  367. *
  368. * Find HOTPLUG_HANDLER for @dev that provides [pre|un]plug callbacks for it.
  369. *
  370. * Note: in case @dev has a parent bus, it will be returned as handler unless
  371. * machine handler overrides it.
  372. *
  373. * Returns: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
  374. * or NULL if there aren't any.
  375. */
  376. HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
  377. void qdev_unplug(DeviceState *dev, Error **errp);
  378. void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
  379. DeviceState *dev, Error **errp);
  380. void qdev_machine_creation_done(void);
  381. bool qdev_machine_modified(void);
  382. /**
  383. * qdev_add_unplug_blocker: Add an unplug blocker to a device
  384. *
  385. * @dev: Device to be blocked from unplug
  386. * @reason: Reason for blocking
  387. */
  388. void qdev_add_unplug_blocker(DeviceState *dev, Error *reason);
  389. /**
  390. * qdev_del_unplug_blocker: Remove an unplug blocker from a device
  391. *
  392. * @dev: Device to be unblocked
  393. * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
  394. * Used as a handle to lookup the blocker for deletion.
  395. */
  396. void qdev_del_unplug_blocker(DeviceState *dev, Error *reason);
  397. /**
  398. * qdev_unplug_blocked: Confirm if a device is blocked from unplug
  399. *
  400. * @dev: Device to be tested
  401. * @reason: Returns one of the reasons why the device is blocked,
  402. * if any
  403. *
  404. * Returns: true if device is blocked from unplug, false otherwise
  405. */
  406. bool qdev_unplug_blocked(DeviceState *dev, Error **errp);
  407. /**
  408. * GpioPolarity: Polarity of a GPIO line
  409. *
  410. * GPIO lines use either positive (active-high) logic,
  411. * or negative (active-low) logic.
  412. *
  413. * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
  414. * active when the voltage on the pin is high (relative to ground);
  415. * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
  416. * is active when the voltage on the pin is low (or grounded).
  417. */
  418. typedef enum {
  419. GPIO_POLARITY_ACTIVE_LOW,
  420. GPIO_POLARITY_ACTIVE_HIGH
  421. } GpioPolarity;
  422. /**
  423. * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
  424. * @dev: Device whose GPIO we want
  425. * @n: Number of the anonymous GPIO line (which must be in range)
  426. *
  427. * Returns the qemu_irq corresponding to an anonymous input GPIO line
  428. * (which the device has set up with qdev_init_gpio_in()). The index
  429. * @n of the GPIO line must be valid (i.e. be at least 0 and less than
  430. * the total number of anonymous input GPIOs the device has); this
  431. * function will assert() if passed an invalid index.
  432. *
  433. * This function is intended to be used by board code or SoC "container"
  434. * device models to wire up the GPIO lines; usually the return value
  435. * will be passed to qdev_connect_gpio_out() or a similar function to
  436. * connect another device's output GPIO line to this input.
  437. *
  438. * For named input GPIO lines, use qdev_get_gpio_in_named().
  439. */
  440. qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
  441. /**
  442. * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
  443. * @dev: Device whose GPIO we want
  444. * @name: Name of the input GPIO array
  445. * @n: Number of the GPIO line in that array (which must be in range)
  446. *
  447. * Returns the qemu_irq corresponding to a named input GPIO line
  448. * (which the device has set up with qdev_init_gpio_in_named()).
  449. * The @name string must correspond to an input GPIO array which exists on
  450. * the device, and the index @n of the GPIO line must be valid (i.e.
  451. * be at least 0 and less than the total number of input GPIOs in that
  452. * array); this function will assert() if passed an invalid name or index.
  453. *
  454. * For anonymous input GPIO lines, use qdev_get_gpio_in().
  455. */
  456. qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
  457. /**
  458. * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
  459. * @dev: Device whose GPIO to connect
  460. * @n: Number of the anonymous output GPIO line (which must be in range)
  461. * @input_pin: qemu_irq to connect the output line to
  462. *
  463. * This function connects an anonymous output GPIO line on a device
  464. * up to an arbitrary qemu_irq, so that when the device asserts that
  465. * output GPIO line, the qemu_irq's callback is invoked.
  466. * The index @n of the GPIO line must be valid (i.e. be at least 0 and
  467. * less than the total number of anonymous output GPIOs the device has
  468. * created with qdev_init_gpio_out()); otherwise this function will assert().
  469. *
  470. * Outbound GPIO lines can be connected to any qemu_irq, but the common
  471. * case is connecting them to another device's inbound GPIO line, using
  472. * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
  473. *
  474. * It is not valid to try to connect one outbound GPIO to multiple
  475. * qemu_irqs at once, or to connect multiple outbound GPIOs to the
  476. * same qemu_irq. (Warning: there is no assertion or other guard to
  477. * catch this error: the model will just not do the right thing.)
  478. * Instead, for fan-out you can use the TYPE_SPLIT_IRQ device: connect
  479. * a device's outbound GPIO to the splitter's input, and connect each
  480. * of the splitter's outputs to a different device. For fan-in you
  481. * can use the TYPE_OR_IRQ device, which is a model of a logical OR
  482. * gate with multiple inputs and one output.
  483. *
  484. * For named output GPIO lines, use qdev_connect_gpio_out_named().
  485. */
  486. void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
  487. /**
  488. * qdev_connect_gpio_out_named: Connect one of a device's named output
  489. * GPIO lines
  490. * @dev: Device whose GPIO to connect
  491. * @name: Name of the output GPIO array
  492. * @n: Number of the anonymous output GPIO line (which must be in range)
  493. * @input_pin: qemu_irq to connect the output line to
  494. *
  495. * This function connects an anonymous output GPIO line on a device
  496. * up to an arbitrary qemu_irq, so that when the device asserts that
  497. * output GPIO line, the qemu_irq's callback is invoked.
  498. * The @name string must correspond to an output GPIO array which exists on
  499. * the device, and the index @n of the GPIO line must be valid (i.e.
  500. * be at least 0 and less than the total number of input GPIOs in that
  501. * array); this function will assert() if passed an invalid name or index.
  502. *
  503. * Outbound GPIO lines can be connected to any qemu_irq, but the common
  504. * case is connecting them to another device's inbound GPIO line, using
  505. * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
  506. *
  507. * It is not valid to try to connect one outbound GPIO to multiple
  508. * qemu_irqs at once, or to connect multiple outbound GPIOs to the
  509. * same qemu_irq; see qdev_connect_gpio_out() for details.
  510. *
  511. * For anonymous output GPIO lines, use qdev_connect_gpio_out().
  512. */
  513. void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
  514. qemu_irq input_pin);
  515. /**
  516. * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
  517. * @dev: Device whose output GPIO we are interested in
  518. * @name: Name of the output GPIO array
  519. * @n: Number of the output GPIO line within that array
  520. *
  521. * Returns whatever qemu_irq is currently connected to the specified
  522. * output GPIO line of @dev. This will be NULL if the output GPIO line
  523. * has never been wired up to the anything. Note that the qemu_irq
  524. * returned does not belong to @dev -- it will be the input GPIO or
  525. * IRQ of whichever device the board code has connected up to @dev's
  526. * output GPIO.
  527. *
  528. * You probably don't need to use this function -- it is used only
  529. * by the platform-bus subsystem.
  530. */
  531. qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
  532. /**
  533. * qdev_intercept_gpio_out: Intercept an existing GPIO connection
  534. * @dev: Device to intercept the outbound GPIO line from
  535. * @icpt: New qemu_irq to connect instead
  536. * @name: Name of the output GPIO array
  537. * @n: Number of the GPIO line in the array
  538. *
  539. * This function is provided only for use by the qtest testing framework
  540. * and is not suitable for use in non-testing parts of QEMU.
  541. *
  542. * This function breaks an existing connection of an outbound GPIO
  543. * line from @dev, and replaces it with the new qemu_irq @icpt, as if
  544. * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
  545. * The previously connected qemu_irq is returned, so it can be restored
  546. * by a second call to qdev_intercept_gpio_out() if desired.
  547. */
  548. qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
  549. const char *name, int n);
  550. BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
  551. /*** Device API. ***/
  552. /**
  553. * qdev_init_gpio_in: create an array of anonymous input GPIO lines
  554. * @dev: Device to create input GPIOs for
  555. * @handler: Function to call when GPIO line value is set
  556. * @n: Number of GPIO lines to create
  557. *
  558. * Devices should use functions in the qdev_init_gpio_in* family in
  559. * their instance_init or realize methods to create any input GPIO
  560. * lines they need. There is no functional difference between
  561. * anonymous and named GPIO lines. Stylistically, named GPIOs are
  562. * preferable (easier to understand at callsites) unless a device
  563. * has exactly one uniform kind of GPIO input whose purpose is obvious.
  564. * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
  565. *
  566. * See qdev_get_gpio_in() for how code that uses such a device can get
  567. * hold of an input GPIO line to manipulate it.
  568. */
  569. void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
  570. /**
  571. * qdev_init_gpio_out: create an array of anonymous output GPIO lines
  572. * @dev: Device to create output GPIOs for
  573. * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
  574. * @n: Number of GPIO lines to create
  575. *
  576. * Devices should use functions in the qdev_init_gpio_out* family
  577. * in their instance_init or realize methods to create any output
  578. * GPIO lines they need. There is no functional difference between
  579. * anonymous and named GPIO lines. Stylistically, named GPIOs are
  580. * preferable (easier to understand at callsites) unless a device
  581. * has exactly one uniform kind of GPIO output whose purpose is obvious.
  582. *
  583. * The @pins argument should be a pointer to either a "qemu_irq"
  584. * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
  585. * state structure. The device implementation can then raise and
  586. * lower the GPIO line by calling qemu_set_irq(). (If anything is
  587. * connected to the other end of the GPIO this will cause the handler
  588. * function for that input GPIO to be called.)
  589. *
  590. * See qdev_connect_gpio_out() for how code that uses such a device
  591. * can connect to one of its output GPIO lines.
  592. *
  593. * There is no need to release the @pins allocated array because it
  594. * will be automatically released when @dev calls its instance_finalize()
  595. * handler.
  596. */
  597. void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
  598. /**
  599. * qdev_init_gpio_out_named: create an array of named output GPIO lines
  600. * @dev: Device to create output GPIOs for
  601. * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
  602. * @name: Name to give this array of GPIO lines
  603. * @n: Number of GPIO lines to create
  604. *
  605. * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
  606. * with a name. Code using the device can then connect these GPIO lines
  607. * using qdev_connect_gpio_out_named().
  608. */
  609. void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
  610. const char *name, int n);
  611. /**
  612. * qdev_init_gpio_in_named_with_opaque: create an array of input GPIO lines
  613. * for the specified device
  614. *
  615. * @dev: Device to create input GPIOs for
  616. * @handler: Function to call when GPIO line value is set
  617. * @opaque: Opaque data pointer to pass to @handler
  618. * @name: Name of the GPIO input (must be unique for this device)
  619. * @n: Number of GPIO lines in this input set
  620. */
  621. void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
  622. qemu_irq_handler handler,
  623. void *opaque,
  624. const char *name, int n);
  625. /**
  626. * qdev_init_gpio_in_named: create an array of input GPIO lines
  627. * for the specified device
  628. *
  629. * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
  630. * passed to the handler is @dev (which is the most commonly desired behaviour).
  631. */
  632. static inline void qdev_init_gpio_in_named(DeviceState *dev,
  633. qemu_irq_handler handler,
  634. const char *name, int n)
  635. {
  636. qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
  637. }
  638. /**
  639. * qdev_pass_gpios: create GPIO lines on container which pass through to device
  640. * @dev: Device which has GPIO lines
  641. * @container: Container device which needs to expose them
  642. * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
  643. *
  644. * In QEMU, complicated devices like SoCs are often modelled with a
  645. * "container" QOM device which itself contains other QOM devices and
  646. * which wires them up appropriately. This function allows the container
  647. * to create GPIO arrays on itself which simply pass through to a GPIO
  648. * array of one of its internal devices.
  649. *
  650. * If @dev has both input and output GPIOs named @name then both will
  651. * be passed through. It is not possible to pass a subset of the array
  652. * with this function.
  653. *
  654. * To users of the container device, the GPIO array created on @container
  655. * behaves exactly like any other.
  656. */
  657. void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
  658. const char *name);
  659. BusState *qdev_get_parent_bus(DeviceState *dev);
  660. /*** BUS API. ***/
  661. DeviceState *qdev_find_recursive(BusState *bus, const char *id);
  662. /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
  663. typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
  664. typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
  665. void qbus_init(void *bus, size_t size, const char *typename,
  666. DeviceState *parent, const char *name);
  667. BusState *qbus_new(const char *typename, DeviceState *parent, const char *name);
  668. bool qbus_realize(BusState *bus, Error **errp);
  669. void qbus_unrealize(BusState *bus);
  670. /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
  671. * < 0 if either devfn or busfn terminate walk somewhere in cursion,
  672. * 0 otherwise. */
  673. int qbus_walk_children(BusState *bus,
  674. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  675. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  676. void *opaque);
  677. int qdev_walk_children(DeviceState *dev,
  678. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  679. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  680. void *opaque);
  681. /**
  682. * @qdev_reset_all:
  683. * Reset @dev. See @qbus_reset_all() for more details.
  684. *
  685. * Note: This function is deprecated and will be removed when it becomes unused.
  686. * Please use device_cold_reset() now.
  687. */
  688. void qdev_reset_all(DeviceState *dev);
  689. void qdev_reset_all_fn(void *opaque);
  690. /**
  691. * @qbus_reset_all:
  692. * @bus: Bus to be reset.
  693. *
  694. * Reset @bus and perform a bus-level ("hard") reset of all devices connected
  695. * to it, including recursive processing of all buses below @bus itself. A
  696. * hard reset means that qbus_reset_all will reset all state of the device.
  697. * For PCI devices, for example, this will include the base address registers
  698. * or configuration space.
  699. *
  700. * Note: This function is deprecated and will be removed when it becomes unused.
  701. * Please use bus_cold_reset() now.
  702. */
  703. void qbus_reset_all(BusState *bus);
  704. void qbus_reset_all_fn(void *opaque);
  705. /**
  706. * device_cold_reset:
  707. * Reset device @dev and perform a recursive processing using the resettable
  708. * interface. It triggers a RESET_TYPE_COLD.
  709. */
  710. void device_cold_reset(DeviceState *dev);
  711. /**
  712. * bus_cold_reset:
  713. *
  714. * Reset bus @bus and perform a recursive processing using the resettable
  715. * interface. It triggers a RESET_TYPE_COLD.
  716. */
  717. void bus_cold_reset(BusState *bus);
  718. /**
  719. * device_is_in_reset:
  720. * Return true if the device @dev is currently being reset.
  721. */
  722. bool device_is_in_reset(DeviceState *dev);
  723. /**
  724. * bus_is_in_reset:
  725. * Return true if the bus @bus is currently being reset.
  726. */
  727. bool bus_is_in_reset(BusState *bus);
  728. /* This should go away once we get rid of the NULL bus hack */
  729. BusState *sysbus_get_default(void);
  730. char *qdev_get_fw_dev_path(DeviceState *dev);
  731. char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
  732. /**
  733. * device_legacy_reset:
  734. *
  735. * Reset a single device (by calling the reset method).
  736. * Note: This function is deprecated and will be removed when it becomes unused.
  737. * Please use device_cold_reset() now.
  738. */
  739. void device_legacy_reset(DeviceState *dev);
  740. void device_class_set_props(DeviceClass *dc, Property *props);
  741. /**
  742. * device_class_set_parent_reset:
  743. * TODO: remove the function when DeviceClass's reset method
  744. * is not used anymore.
  745. */
  746. void device_class_set_parent_reset(DeviceClass *dc,
  747. DeviceReset dev_reset,
  748. DeviceReset *parent_reset);
  749. void device_class_set_parent_realize(DeviceClass *dc,
  750. DeviceRealize dev_realize,
  751. DeviceRealize *parent_realize);
  752. void device_class_set_parent_unrealize(DeviceClass *dc,
  753. DeviceUnrealize dev_unrealize,
  754. DeviceUnrealize *parent_unrealize);
  755. const VMStateDescription *qdev_get_vmsd(DeviceState *dev);
  756. const char *qdev_fw_name(DeviceState *dev);
  757. void qdev_assert_realized_properly(void);
  758. Object *qdev_get_machine(void);
  759. /* FIXME: make this a link<> */
  760. bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp);
  761. extern bool qdev_hot_removed;
  762. char *qdev_get_dev_path(DeviceState *dev);
  763. void qbus_set_hotplug_handler(BusState *bus, Object *handler);
  764. void qbus_set_bus_hotplug_handler(BusState *bus);
  765. static inline bool qbus_is_hotpluggable(BusState *bus)
  766. {
  767. return bus->hotplug_handler;
  768. }
  769. /**
  770. * qbus_mark_full: Mark this bus as full, so no more devices can be attached
  771. * @bus: Bus to mark as full
  772. *
  773. * By default, QEMU will allow devices to be plugged into a bus up
  774. * to the bus class's device count limit. Calling this function
  775. * marks a particular bus as full, so that no more devices can be
  776. * plugged into it. In particular this means that the bus will not
  777. * be considered as a candidate for plugging in devices created by
  778. * the user on the commandline or via the monitor.
  779. * If a machine has multiple buses of a given type, such as I2C,
  780. * where some of those buses in the real hardware are used only for
  781. * internal devices and some are exposed via expansion ports, you
  782. * can use this function to mark the internal-only buses as full
  783. * after you have created all their internal devices. Then user
  784. * created devices will appear on the expansion-port bus where
  785. * guest software expects them.
  786. */
  787. static inline void qbus_mark_full(BusState *bus)
  788. {
  789. bus->full = true;
  790. }
  791. void device_listener_register(DeviceListener *listener);
  792. void device_listener_unregister(DeviceListener *listener);
  793. /**
  794. * @qdev_should_hide_device:
  795. * @opts: options QDict
  796. * @from_json: true if @opts entries are typed, false for all strings
  797. * @errp: pointer to error object
  798. *
  799. * Check if a device should be added.
  800. * When a device is added via qdev_device_add() this will be called,
  801. * and return if the device should be added now or not.
  802. */
  803. bool qdev_should_hide_device(const QDict *opts, bool from_json, Error **errp);
  804. typedef enum MachineInitPhase {
  805. /* current_machine is NULL. */
  806. PHASE_NO_MACHINE,
  807. /* current_machine is not NULL, but current_machine->accel is NULL. */
  808. PHASE_MACHINE_CREATED,
  809. /*
  810. * current_machine->accel is not NULL, but the machine properties have
  811. * not been validated and machine_class->init has not yet been called.
  812. */
  813. PHASE_ACCEL_CREATED,
  814. /*
  815. * machine_class->init has been called, thus creating any embedded
  816. * devices and validating machine properties. Devices created at
  817. * this time are considered to be cold-plugged.
  818. */
  819. PHASE_MACHINE_INITIALIZED,
  820. /*
  821. * QEMU is ready to start CPUs and devices created at this time
  822. * are considered to be hot-plugged. The monitor is not restricted
  823. * to "preconfig" commands.
  824. */
  825. PHASE_MACHINE_READY,
  826. } MachineInitPhase;
  827. extern bool phase_check(MachineInitPhase phase);
  828. extern void phase_advance(MachineInitPhase phase);
  829. #endif