2
0

qdev-core.h 41 KB

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