qdev-core.h 39 KB

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