qdev-core.h 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. const 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. * @legacy_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 legacy_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. typedef struct DeviceListener DeviceListener;
  274. struct DeviceListener {
  275. void (*realize)(DeviceListener *listener, DeviceState *dev);
  276. void (*unrealize)(DeviceListener *listener, DeviceState *dev);
  277. /*
  278. * This callback is called upon init of the DeviceState and
  279. * informs qdev if a device should be visible or hidden. We can
  280. * hide a failover device depending for example on the device
  281. * opts.
  282. *
  283. * On errors, it returns false and errp is set. Device creation
  284. * should fail in this case.
  285. */
  286. bool (*hide_device)(DeviceListener *listener, const QDict *device_opts,
  287. bool from_json, Error **errp);
  288. QTAILQ_ENTRY(DeviceListener) link;
  289. };
  290. #define TYPE_BUS "bus"
  291. DECLARE_OBJ_CHECKERS(BusState, BusClass,
  292. BUS, TYPE_BUS)
  293. struct BusClass {
  294. ObjectClass parent_class;
  295. /* FIXME first arg should be BusState */
  296. void (*print_dev)(Monitor *mon, DeviceState *dev, int indent);
  297. char *(*get_dev_path)(DeviceState *dev);
  298. /*
  299. * This callback is used to create Open Firmware device path in accordance
  300. * with OF spec http://forthworks.com/standards/of1275.pdf. Individual bus
  301. * bindings can be found at http://playground.sun.com/1275/bindings/.
  302. */
  303. char *(*get_fw_dev_path)(DeviceState *dev);
  304. /*
  305. * Return whether the device can be added to @bus,
  306. * based on the address that was set (via device properties)
  307. * before realize. If not, on return @errp contains the
  308. * human-readable error message.
  309. */
  310. bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp);
  311. BusRealize realize;
  312. BusUnrealize unrealize;
  313. /* maximum devices allowed on the bus, 0: no limit. */
  314. int max_dev;
  315. /* number of automatically allocated bus ids (e.g. ide.0) */
  316. int automatic_ids;
  317. };
  318. typedef struct BusChild {
  319. struct rcu_head rcu;
  320. DeviceState *child;
  321. int index;
  322. QTAILQ_ENTRY(BusChild) sibling;
  323. } BusChild;
  324. #define QDEV_HOTPLUG_HANDLER_PROPERTY "hotplug-handler"
  325. typedef QTAILQ_HEAD(, BusChild) BusChildHead;
  326. typedef QLIST_ENTRY(BusState) BusStateEntry;
  327. /**
  328. * struct BusState:
  329. * @obj: parent object
  330. * @parent: parent Device
  331. * @name: name of bus
  332. * @hotplug_handler: link to a hotplug handler associated with bus.
  333. * @max_index: max number of child buses
  334. * @realized: is the bus itself realized?
  335. * @full: is the bus full?
  336. * @num_children: current number of child buses
  337. */
  338. struct BusState {
  339. /* private: */
  340. Object obj;
  341. /* public: */
  342. DeviceState *parent;
  343. char *name;
  344. HotplugHandler *hotplug_handler;
  345. int max_index;
  346. bool realized;
  347. bool full;
  348. int num_children;
  349. /**
  350. * @children: an RCU protected QTAILQ, thus readers must use RCU
  351. * to access it, and writers must hold the big qemu lock
  352. */
  353. BusChildHead children;
  354. /**
  355. * @sibling: next bus
  356. */
  357. BusStateEntry sibling;
  358. /**
  359. * @reset: ResettableState for the bus; handled by Resettable interface.
  360. */
  361. ResettableState reset;
  362. };
  363. /**
  364. * typedef GlobalProperty - a global property type
  365. *
  366. * @used: Set to true if property was used when initializing a device.
  367. * @optional: If set to true, GlobalProperty will be skipped without errors
  368. * if the property doesn't exist.
  369. *
  370. * An error is fatal for non-hotplugged devices, when the global is applied.
  371. */
  372. typedef struct GlobalProperty {
  373. const char *driver;
  374. const char *property;
  375. const char *value;
  376. bool used;
  377. bool optional;
  378. } GlobalProperty;
  379. static inline void
  380. compat_props_add(GPtrArray *arr,
  381. GlobalProperty props[], size_t nelem)
  382. {
  383. int i;
  384. for (i = 0; i < nelem; i++) {
  385. g_ptr_array_add(arr, (void *)&props[i]);
  386. }
  387. }
  388. /*** Board API. This should go away once we have a machine config file. ***/
  389. /**
  390. * qdev_new: Create a device on the heap
  391. * @name: device type to create (we assert() that this type exists)
  392. *
  393. * This only allocates the memory and initializes the device state
  394. * structure, ready for the caller to set properties if they wish.
  395. * The device still needs to be realized.
  396. *
  397. * Return: a derived DeviceState object with a reference count of 1.
  398. */
  399. DeviceState *qdev_new(const char *name);
  400. /**
  401. * qdev_try_new: Try to create a device on the heap
  402. * @name: device type to create
  403. *
  404. * This is like qdev_new(), except it returns %NULL when type @name
  405. * does not exist, rather than asserting.
  406. *
  407. * Return: a derived DeviceState object with a reference count of 1 or
  408. * NULL if type @name does not exist.
  409. */
  410. DeviceState *qdev_try_new(const char *name);
  411. /**
  412. * qdev_is_realized() - check if device is realized
  413. * @dev: The device to check.
  414. *
  415. * Context: May be called outside big qemu lock.
  416. * Return: true if the device has been fully constructed, false otherwise.
  417. */
  418. static inline bool qdev_is_realized(DeviceState *dev)
  419. {
  420. return qatomic_load_acquire(&dev->realized);
  421. }
  422. /**
  423. * qdev_realize: Realize @dev.
  424. * @dev: device to realize
  425. * @bus: bus to plug it into (may be NULL)
  426. * @errp: pointer to error object
  427. *
  428. * "Realize" the device, i.e. perform the second phase of device
  429. * initialization.
  430. * @dev must not be plugged into a bus already.
  431. * If @bus, plug @dev into @bus. This takes a reference to @dev.
  432. * If @dev has no QOM parent, make one up, taking another reference.
  433. *
  434. * If you created @dev using qdev_new(), you probably want to use
  435. * qdev_realize_and_unref() instead.
  436. *
  437. * Return: true on success, else false setting @errp with error
  438. */
  439. bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp);
  440. /**
  441. * qdev_realize_and_unref: Realize @dev and drop a reference
  442. * @dev: device to realize
  443. * @bus: bus to plug it into (may be NULL)
  444. * @errp: pointer to error object
  445. *
  446. * Realize @dev and drop a reference.
  447. * This is like qdev_realize(), except the caller must hold a
  448. * (private) reference, which is dropped on return regardless of
  449. * success or failure. Intended use::
  450. *
  451. * dev = qdev_new();
  452. * [...]
  453. * qdev_realize_and_unref(dev, bus, errp);
  454. *
  455. * Now @dev can go away without further ado.
  456. *
  457. * If you are embedding the device into some other QOM device and
  458. * initialized it via some variant on object_initialize_child() then
  459. * do not use this function, because that family of functions arrange
  460. * for the only reference to the child device to be held by the parent
  461. * via the child<> property, and so the reference-count-drop done here
  462. * would be incorrect. For that use case you want qdev_realize().
  463. *
  464. * Return: true on success, else false setting @errp with error
  465. */
  466. bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
  467. /**
  468. * qdev_unrealize: Unrealize a device
  469. * @dev: device to unrealize
  470. *
  471. * This function will "unrealize" a device, which is the first phase
  472. * of correctly destroying a device that has been realized. It will:
  473. *
  474. * - unrealize any child buses by calling qbus_unrealize()
  475. * (this will recursively unrealize any devices on those buses)
  476. * - call the unrealize method of @dev
  477. *
  478. * The device can then be freed by causing its reference count to go
  479. * to zero.
  480. *
  481. * Warning: most devices in QEMU do not expect to be unrealized. Only
  482. * devices which are hot-unpluggable should be unrealized (as part of
  483. * the unplugging process); all other devices are expected to last for
  484. * the life of the simulation and should not be unrealized and freed.
  485. */
  486. void qdev_unrealize(DeviceState *dev);
  487. void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
  488. int required_for_version);
  489. HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);
  490. HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
  491. bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
  492. /**
  493. * qdev_get_hotplug_handler() - Get handler responsible for device wiring
  494. * @dev: the device we want the HOTPLUG_HANDLER for.
  495. *
  496. * Note: in case @dev has a parent bus, it will be returned as handler unless
  497. * machine handler overrides it.
  498. *
  499. * Return: pointer to object that implements TYPE_HOTPLUG_HANDLER interface
  500. * or NULL if there aren't any.
  501. */
  502. HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
  503. void qdev_unplug(DeviceState *dev, Error **errp);
  504. void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
  505. DeviceState *dev, Error **errp);
  506. void qdev_machine_creation_done(void);
  507. bool qdev_machine_modified(void);
  508. /**
  509. * qdev_add_unplug_blocker: Add an unplug blocker to a device
  510. *
  511. * @dev: Device to be blocked from unplug
  512. * @reason: Reason for blocking
  513. */
  514. void qdev_add_unplug_blocker(DeviceState *dev, Error *reason);
  515. /**
  516. * qdev_del_unplug_blocker: Remove an unplug blocker from a device
  517. *
  518. * @dev: Device to be unblocked
  519. * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
  520. * Used as a handle to lookup the blocker for deletion.
  521. */
  522. void qdev_del_unplug_blocker(DeviceState *dev, Error *reason);
  523. /**
  524. * qdev_unplug_blocked: Confirm if a device is blocked from unplug
  525. *
  526. * @dev: Device to be tested
  527. * @errp: The reasons why the device is blocked, if any
  528. *
  529. * Returns: true (also setting @errp) if device is blocked from unplug,
  530. * false otherwise
  531. */
  532. bool qdev_unplug_blocked(DeviceState *dev, Error **errp);
  533. /**
  534. * typedef GpioPolarity - Polarity of a GPIO line
  535. *
  536. * GPIO lines use either positive (active-high) logic,
  537. * or negative (active-low) logic.
  538. *
  539. * In active-high logic (%GPIO_POLARITY_ACTIVE_HIGH), a pin is
  540. * active when the voltage on the pin is high (relative to ground);
  541. * whereas in active-low logic (%GPIO_POLARITY_ACTIVE_LOW), a pin
  542. * is active when the voltage on the pin is low (or grounded).
  543. */
  544. typedef enum {
  545. GPIO_POLARITY_ACTIVE_LOW,
  546. GPIO_POLARITY_ACTIVE_HIGH
  547. } GpioPolarity;
  548. /**
  549. * qdev_get_gpio_in: Get one of a device's anonymous input GPIO lines
  550. * @dev: Device whose GPIO we want
  551. * @n: Number of the anonymous GPIO line (which must be in range)
  552. *
  553. * Returns the qemu_irq corresponding to an anonymous input GPIO line
  554. * (which the device has set up with qdev_init_gpio_in()). The index
  555. * @n of the GPIO line must be valid (i.e. be at least 0 and less than
  556. * the total number of anonymous input GPIOs the device has); this
  557. * function will assert() if passed an invalid index.
  558. *
  559. * This function is intended to be used by board code or SoC "container"
  560. * device models to wire up the GPIO lines; usually the return value
  561. * will be passed to qdev_connect_gpio_out() or a similar function to
  562. * connect another device's output GPIO line to this input.
  563. *
  564. * For named input GPIO lines, use qdev_get_gpio_in_named().
  565. *
  566. * Return: qemu_irq corresponding to anonymous input GPIO line
  567. */
  568. qemu_irq qdev_get_gpio_in(DeviceState *dev, int n);
  569. /**
  570. * qdev_get_gpio_in_named: Get one of a device's named input GPIO lines
  571. * @dev: Device whose GPIO we want
  572. * @name: Name of the input GPIO array
  573. * @n: Number of the GPIO line in that array (which must be in range)
  574. *
  575. * Returns the qemu_irq corresponding to a single input GPIO line
  576. * in a named array of input GPIO lines on a device (which the device
  577. * has set up with qdev_init_gpio_in_named()).
  578. * The @name string must correspond to an input GPIO array which exists on
  579. * the device, and the index @n of the GPIO line must be valid (i.e.
  580. * be at least 0 and less than the total number of input GPIOs in that
  581. * array); this function will assert() if passed an invalid name or index.
  582. *
  583. * For anonymous input GPIO lines, use qdev_get_gpio_in().
  584. *
  585. * Return: qemu_irq corresponding to named input GPIO line
  586. */
  587. qemu_irq qdev_get_gpio_in_named(DeviceState *dev, const char *name, int n);
  588. /**
  589. * qdev_connect_gpio_out: Connect one of a device's anonymous output GPIO lines
  590. * @dev: Device whose GPIO to connect
  591. * @n: Number of the anonymous output GPIO line (which must be in range)
  592. * @pin: qemu_irq to connect the output line to
  593. *
  594. * This function connects an anonymous output GPIO line on a device
  595. * up to an arbitrary qemu_irq, so that when the device asserts that
  596. * output GPIO line, the qemu_irq's callback is invoked.
  597. * The index @n of the GPIO line must be valid (i.e. be at least 0 and
  598. * less than the total number of anonymous output GPIOs the device has
  599. * created with qdev_init_gpio_out()); otherwise this function will assert().
  600. *
  601. * Outbound GPIO lines can be connected to any qemu_irq, but the common
  602. * case is connecting them to another device's inbound GPIO line, using
  603. * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
  604. *
  605. * It is not valid to try to connect one outbound GPIO to multiple
  606. * qemu_irqs at once, or to connect multiple outbound GPIOs to the
  607. * same qemu_irq. (Warning: there is no assertion or other guard to
  608. * catch this error: the model will just not do the right thing.)
  609. * Instead, for fan-out you can use the TYPE_SPLIT_IRQ device: connect
  610. * a device's outbound GPIO to the splitter's input, and connect each
  611. * of the splitter's outputs to a different device. For fan-in you
  612. * can use the TYPE_OR_IRQ device, which is a model of a logical OR
  613. * gate with multiple inputs and one output.
  614. *
  615. * For named output GPIO lines, use qdev_connect_gpio_out_named().
  616. */
  617. void qdev_connect_gpio_out(DeviceState *dev, int n, qemu_irq pin);
  618. /**
  619. * qdev_connect_gpio_out_named: Connect one of a device's named output
  620. * GPIO lines
  621. * @dev: Device whose GPIO to connect
  622. * @name: Name of the output GPIO array
  623. * @n: Number of the output GPIO line within that array (which must be in range)
  624. * @input_pin: qemu_irq to connect the output line to
  625. *
  626. * This function connects a single GPIO output in a named array of output
  627. * GPIO lines on a device up to an arbitrary qemu_irq, so that when the
  628. * device asserts that output GPIO line, the qemu_irq's callback is invoked.
  629. * The @name string must correspond to an output GPIO array which exists on
  630. * the device, and the index @n of the GPIO line must be valid (i.e.
  631. * be at least 0 and less than the total number of output GPIOs in that
  632. * array); this function will assert() if passed an invalid name or index.
  633. *
  634. * Outbound GPIO lines can be connected to any qemu_irq, but the common
  635. * case is connecting them to another device's inbound GPIO line, using
  636. * the qemu_irq returned by qdev_get_gpio_in() or qdev_get_gpio_in_named().
  637. *
  638. * It is not valid to try to connect one outbound GPIO to multiple
  639. * qemu_irqs at once, or to connect multiple outbound GPIOs to the
  640. * same qemu_irq; see qdev_connect_gpio_out() for details.
  641. *
  642. * For anonymous output GPIO lines, use qdev_connect_gpio_out().
  643. */
  644. void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
  645. qemu_irq input_pin);
  646. /**
  647. * qdev_get_gpio_out_connector: Get the qemu_irq connected to an output GPIO
  648. * @dev: Device whose output GPIO we are interested in
  649. * @name: Name of the output GPIO array
  650. * @n: Number of the output GPIO line within that array
  651. *
  652. * Returns whatever qemu_irq is currently connected to the specified
  653. * output GPIO line of @dev. This will be NULL if the output GPIO line
  654. * has never been wired up to the anything. Note that the qemu_irq
  655. * returned does not belong to @dev -- it will be the input GPIO or
  656. * IRQ of whichever device the board code has connected up to @dev's
  657. * output GPIO.
  658. *
  659. * You probably don't need to use this function -- it is used only
  660. * by the platform-bus subsystem.
  661. *
  662. * Return: qemu_irq associated with GPIO or NULL if un-wired.
  663. */
  664. qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, int n);
  665. /**
  666. * qdev_intercept_gpio_out: Intercept an existing GPIO connection
  667. * @dev: Device to intercept the outbound GPIO line from
  668. * @icpt: New qemu_irq to connect instead
  669. * @name: Name of the output GPIO array
  670. * @n: Number of the GPIO line in the array
  671. *
  672. * .. note::
  673. * This function is provided only for use by the qtest testing framework
  674. * and is not suitable for use in non-testing parts of QEMU.
  675. *
  676. * This function breaks an existing connection of an outbound GPIO
  677. * line from @dev, and replaces it with the new qemu_irq @icpt, as if
  678. * ``qdev_connect_gpio_out_named(dev, icpt, name, n)`` had been called.
  679. * The previously connected qemu_irq is returned, so it can be restored
  680. * by a second call to qdev_intercept_gpio_out() if desired.
  681. *
  682. * Return: old disconnected qemu_irq if one existed
  683. */
  684. qemu_irq qdev_intercept_gpio_out(DeviceState *dev, qemu_irq icpt,
  685. const char *name, int n);
  686. BusState *qdev_get_child_bus(DeviceState *dev, const char *name);
  687. /*** Device API. ***/
  688. /**
  689. * qdev_init_gpio_in: create an array of anonymous input GPIO lines
  690. * @dev: Device to create input GPIOs for
  691. * @handler: Function to call when GPIO line value is set
  692. * @n: Number of GPIO lines to create
  693. *
  694. * Devices should use functions in the qdev_init_gpio_in* family in
  695. * their instance_init or realize methods to create any input GPIO
  696. * lines they need. There is no functional difference between
  697. * anonymous and named GPIO lines. Stylistically, named GPIOs are
  698. * preferable (easier to understand at callsites) unless a device
  699. * has exactly one uniform kind of GPIO input whose purpose is obvious.
  700. * Note that input GPIO lines can serve as 'sinks' for IRQ lines.
  701. *
  702. * See qdev_get_gpio_in() for how code that uses such a device can get
  703. * hold of an input GPIO line to manipulate it.
  704. */
  705. void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n);
  706. /**
  707. * qdev_init_gpio_out: create an array of anonymous output GPIO lines
  708. * @dev: Device to create output GPIOs for
  709. * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
  710. * @n: Number of GPIO lines to create
  711. *
  712. * Devices should use functions in the qdev_init_gpio_out* family
  713. * in their instance_init or realize methods to create any output
  714. * GPIO lines they need. There is no functional difference between
  715. * anonymous and named GPIO lines. Stylistically, named GPIOs are
  716. * preferable (easier to understand at callsites) unless a device
  717. * has exactly one uniform kind of GPIO output whose purpose is obvious.
  718. *
  719. * The @pins argument should be a pointer to either a "qemu_irq"
  720. * (if @n == 1) or a "qemu_irq []" array (if @n > 1) in the device's
  721. * state structure. The device implementation can then raise and
  722. * lower the GPIO line by calling qemu_set_irq(). (If anything is
  723. * connected to the other end of the GPIO this will cause the handler
  724. * function for that input GPIO to be called.)
  725. *
  726. * See qdev_connect_gpio_out() for how code that uses such a device
  727. * can connect to one of its output GPIO lines.
  728. *
  729. * There is no need to release the @pins allocated array because it
  730. * will be automatically released when @dev calls its instance_finalize()
  731. * handler.
  732. */
  733. void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n);
  734. /**
  735. * qdev_init_gpio_out_named: create an array of named output GPIO lines
  736. * @dev: Device to create output GPIOs for
  737. * @pins: Pointer to qemu_irq or qemu_irq array for the GPIO lines
  738. * @name: Name to give this array of GPIO lines
  739. * @n: Number of GPIO lines to create in this array
  740. *
  741. * Like qdev_init_gpio_out(), but creates an array of GPIO output lines
  742. * with a name. Code using the device can then connect these GPIO lines
  743. * using qdev_connect_gpio_out_named().
  744. */
  745. void qdev_init_gpio_out_named(DeviceState *dev, qemu_irq *pins,
  746. const char *name, int n);
  747. /**
  748. * qdev_init_gpio_in_named_with_opaque() - create an array of input GPIO lines
  749. * @dev: Device to create input GPIOs for
  750. * @handler: Function to call when GPIO line value is set
  751. * @opaque: Opaque data pointer to pass to @handler
  752. * @name: Name of the GPIO input (must be unique for this device)
  753. * @n: Number of GPIO lines in this input set
  754. */
  755. void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
  756. qemu_irq_handler handler,
  757. void *opaque,
  758. const char *name, int n);
  759. /**
  760. * qdev_init_gpio_in_named() - create an array of input GPIO lines
  761. * @dev: device to add array to
  762. * @handler: a &typedef qemu_irq_handler function to call when GPIO is set
  763. * @name: Name of the GPIO input (must be unique for this device)
  764. * @n: Number of GPIO lines in this input set
  765. *
  766. * Like qdev_init_gpio_in_named_with_opaque(), but the opaque pointer
  767. * passed to the handler is @dev (which is the most commonly desired behaviour).
  768. */
  769. static inline void qdev_init_gpio_in_named(DeviceState *dev,
  770. qemu_irq_handler handler,
  771. const char *name, int n)
  772. {
  773. qdev_init_gpio_in_named_with_opaque(dev, handler, dev, name, n);
  774. }
  775. /**
  776. * qdev_pass_gpios: create GPIO lines on container which pass through to device
  777. * @dev: Device which has GPIO lines
  778. * @container: Container device which needs to expose them
  779. * @name: Name of GPIO array to pass through (NULL for the anonymous GPIO array)
  780. *
  781. * In QEMU, complicated devices like SoCs are often modelled with a
  782. * "container" QOM device which itself contains other QOM devices and
  783. * which wires them up appropriately. This function allows the container
  784. * to create GPIO arrays on itself which simply pass through to a GPIO
  785. * array of one of its internal devices.
  786. *
  787. * If @dev has both input and output GPIOs named @name then both will
  788. * be passed through. It is not possible to pass a subset of the array
  789. * with this function.
  790. *
  791. * To users of the container device, the GPIO array created on @container
  792. * behaves exactly like any other.
  793. */
  794. void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
  795. const char *name);
  796. BusState *qdev_get_parent_bus(const DeviceState *dev);
  797. /*** BUS API. ***/
  798. DeviceState *qdev_find_recursive(BusState *bus, const char *id);
  799. /* Returns 0 to walk children, > 0 to skip walk, < 0 to terminate walk. */
  800. typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
  801. typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
  802. void qbus_init(void *bus, size_t size, const char *typename,
  803. DeviceState *parent, const char *name);
  804. BusState *qbus_new(const char *typename, DeviceState *parent, const char *name);
  805. bool qbus_realize(BusState *bus, Error **errp);
  806. void qbus_unrealize(BusState *bus);
  807. /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
  808. * < 0 if either devfn or busfn terminate walk somewhere in cursion,
  809. * 0 otherwise. */
  810. int qbus_walk_children(BusState *bus,
  811. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  812. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  813. void *opaque);
  814. int qdev_walk_children(DeviceState *dev,
  815. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  816. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  817. void *opaque);
  818. /**
  819. * device_cold_reset() - perform a recursive cold reset on a device
  820. * @dev: device to reset.
  821. *
  822. * Reset device @dev and perform a recursive processing using the resettable
  823. * interface. It triggers a RESET_TYPE_COLD.
  824. */
  825. void device_cold_reset(DeviceState *dev);
  826. /**
  827. * bus_cold_reset() - perform a recursive cold reset on a bus
  828. * @bus: bus to reset
  829. *
  830. * Reset bus @bus and perform a recursive processing using the resettable
  831. * interface. It triggers a RESET_TYPE_COLD.
  832. */
  833. void bus_cold_reset(BusState *bus);
  834. /**
  835. * device_is_in_reset() - check device reset state
  836. * @dev: device to check
  837. *
  838. * Return: true if the device @dev is currently being reset.
  839. */
  840. bool device_is_in_reset(DeviceState *dev);
  841. /**
  842. * bus_is_in_reset() - check bus reset state
  843. * @bus: bus to check
  844. *
  845. * Return: true if the bus @bus is currently being reset.
  846. */
  847. bool bus_is_in_reset(BusState *bus);
  848. /* This should go away once we get rid of the NULL bus hack */
  849. BusState *sysbus_get_default(void);
  850. char *qdev_get_fw_dev_path(DeviceState *dev);
  851. char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev);
  852. /**
  853. * device_class_set_props(): add a set of properties to an device
  854. * @dc: the parent DeviceClass all devices inherit
  855. * @props: an array of properties, terminate by DEFINE_PROP_END_OF_LIST()
  856. *
  857. * This will add a set of properties to the object. It will fault if
  858. * you attempt to add an existing property defined by a parent class.
  859. * To modify an inherited property you need to use????
  860. */
  861. void device_class_set_props(DeviceClass *dc, const Property *props);
  862. /**
  863. * device_class_set_parent_realize() - set up for chaining realize fns
  864. * @dc: The device class
  865. * @dev_realize: the device realize function
  866. * @parent_realize: somewhere to save the parents realize function
  867. *
  868. * This is intended to be used when the new realize function will
  869. * eventually call its parent realization function during creation.
  870. * This requires storing the function call somewhere (usually in the
  871. * instance structure) so you can eventually call
  872. * dc->parent_realize(dev, errp)
  873. */
  874. void device_class_set_parent_realize(DeviceClass *dc,
  875. DeviceRealize dev_realize,
  876. DeviceRealize *parent_realize);
  877. /**
  878. * device_class_set_legacy_reset(): set the DeviceClass::reset method
  879. * @dc: The device class
  880. * @dev_reset: the reset function
  881. *
  882. * This function sets the DeviceClass::reset method. This is widely
  883. * used in existing code, but new code should prefer to use the
  884. * Resettable API as documented in docs/devel/reset.rst.
  885. * In addition, devices which need to chain to their parent class's
  886. * reset methods or which need to be subclassed must use Resettable.
  887. */
  888. void device_class_set_legacy_reset(DeviceClass *dc,
  889. DeviceReset dev_reset);
  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. * Late backend objects have been created and initialized.
  989. */
  990. PHASE_LATE_BACKENDS_CREATED,
  991. /*
  992. * machine_class->init has been called, thus creating any embedded
  993. * devices and validating machine properties. Devices created at
  994. * this time are considered to be cold-plugged.
  995. */
  996. PHASE_MACHINE_INITIALIZED,
  997. /*
  998. * QEMU is ready to start CPUs and devices created at this time
  999. * are considered to be hot-plugged. The monitor is not restricted
  1000. * to "preconfig" commands.
  1001. */
  1002. PHASE_MACHINE_READY,
  1003. } MachineInitPhase;
  1004. bool phase_check(MachineInitPhase phase);
  1005. void phase_advance(MachineInitPhase phase);
  1006. #endif