qdev-core.h 38 KB

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