2
0

resettable.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Resettable interface header.
  3. *
  4. * Copyright (c) 2019 GreenSocs SAS
  5. *
  6. * Authors:
  7. * Damien Hedde
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #ifndef HW_RESETTABLE_H
  13. #define HW_RESETTABLE_H
  14. #include "qom/object.h"
  15. #define TYPE_RESETTABLE_INTERFACE "resettable"
  16. typedef struct ResettableClass ResettableClass;
  17. DECLARE_CLASS_CHECKERS(ResettableClass, RESETTABLE,
  18. TYPE_RESETTABLE_INTERFACE)
  19. typedef struct ResettableState ResettableState;
  20. /**
  21. * ResetType:
  22. * Types of reset.
  23. *
  24. * + Cold: reset resulting from a power cycle of the object.
  25. * + Wakeup: reset resulting from a wake-up from a suspended state.
  26. *
  27. * TODO: Support has to be added to handle more types. In particular,
  28. * ResettableState structure needs to be expanded.
  29. */
  30. typedef enum ResetType {
  31. RESET_TYPE_COLD,
  32. RESET_TYPE_SNAPSHOT_LOAD,
  33. RESET_TYPE_WAKEUP,
  34. RESET_TYPE_S390_CPU_INITIAL,
  35. RESET_TYPE_S390_CPU_NORMAL,
  36. } ResetType;
  37. /*
  38. * ResettableClass:
  39. * Interface for resettable objects.
  40. *
  41. * See docs/devel/reset.rst for more detailed information about how QEMU models
  42. * reset. This whole API must only be used when holding the iothread mutex.
  43. *
  44. * All objects which can be reset must implement this interface;
  45. * it is usually provided by a base class such as DeviceClass or BusClass.
  46. * Every Resettable object must maintain some state tracking the
  47. * progress of a reset operation by providing a ResettableState structure.
  48. * The functions defined in this module take care of updating the
  49. * state of the reset.
  50. * The base class implementation of the interface provides this
  51. * state and implements the associated method: get_state.
  52. *
  53. * Concrete object implementations (typically specific devices
  54. * such as a UART model) should provide the functions
  55. * for the phases.enter, phases.hold and phases.exit methods, which
  56. * they can set in their class init function, either directly or
  57. * by calling resettable_class_set_parent_phases().
  58. * The phase methods are guaranteed to only only ever be called once
  59. * for any reset event, in the order 'enter', 'hold', 'exit'.
  60. * An object will always move quickly from 'enter' to 'hold'
  61. * but might remain in 'hold' for an arbitrary period of time
  62. * before eventually reset is deasserted and the 'exit' phase is called.
  63. * Object implementations should be prepared for functions handling
  64. * inbound connections from other devices (such as qemu_irq handler
  65. * functions) to be called at any point during reset after their
  66. * 'enter' method has been called.
  67. *
  68. * Users of a resettable object should not call these methods
  69. * directly, but instead use the function resettable_reset().
  70. *
  71. * @phases.enter: This phase is called when the object enters reset. It
  72. * should reset local state of the object, but it must not do anything that
  73. * has a side-effect on other objects, such as raising or lowering a qemu_irq
  74. * line or reading or writing guest memory. It takes the reset's type as
  75. * argument.
  76. *
  77. * @phases.hold: This phase is called for entry into reset, once every object
  78. * in the system which is being reset has had its @phases.enter method called.
  79. * At this point devices can do actions that affect other objects.
  80. *
  81. * @phases.exit: This phase is called when the object leaves the reset state.
  82. * Actions affecting other objects are permitted.
  83. *
  84. * @get_state: Mandatory method which must return a pointer to a
  85. * ResettableState.
  86. *
  87. * @child_foreach: Executes a given callback on every Resettable child. Child
  88. * in this context means a child in the qbus tree, so the children of a qbus
  89. * are the devices on it, and the children of a device are all the buses it
  90. * owns. This is not the same as the QOM object hierarchy. The function takes
  91. * additional opaque and ResetType arguments which must be passed unmodified to
  92. * the callback.
  93. */
  94. typedef void (*ResettableEnterPhase)(Object *obj, ResetType type);
  95. typedef void (*ResettableHoldPhase)(Object *obj, ResetType type);
  96. typedef void (*ResettableExitPhase)(Object *obj, ResetType type);
  97. typedef ResettableState * (*ResettableGetState)(Object *obj);
  98. typedef void (*ResettableChildCallback)(Object *, void *opaque,
  99. ResetType type);
  100. typedef void (*ResettableChildForeach)(Object *obj,
  101. ResettableChildCallback cb,
  102. void *opaque, ResetType type);
  103. typedef struct ResettablePhases {
  104. ResettableEnterPhase enter;
  105. ResettableHoldPhase hold;
  106. ResettableExitPhase exit;
  107. } ResettablePhases;
  108. struct ResettableClass {
  109. InterfaceClass parent_class;
  110. /* Phase methods */
  111. ResettablePhases phases;
  112. /* State access method */
  113. ResettableGetState get_state;
  114. /* Hierarchy handling method */
  115. ResettableChildForeach child_foreach;
  116. };
  117. /**
  118. * ResettableState:
  119. * Structure holding reset related state. The fields should not be accessed
  120. * directly; the definition is here to allow further inclusion into other
  121. * objects.
  122. *
  123. * @count: Number of reset level the object is into. It is incremented when
  124. * the reset operation starts and decremented when it finishes.
  125. * @hold_phase_pending: flag which indicates that we need to invoke the 'hold'
  126. * phase handler for this object.
  127. * @exit_phase_in_progress: true if we are currently in the exit phase
  128. */
  129. struct ResettableState {
  130. unsigned count;
  131. bool hold_phase_pending;
  132. bool exit_phase_in_progress;
  133. };
  134. /**
  135. * resettable_state_clear:
  136. * Clear the state. It puts the state to the initial (zeroed) state required
  137. * to reuse an object. Typically used in realize step of base classes
  138. * implementing the interface.
  139. */
  140. static inline void resettable_state_clear(ResettableState *state)
  141. {
  142. memset(state, 0, sizeof(ResettableState));
  143. }
  144. /**
  145. * resettable_reset:
  146. * Trigger a reset on an object @obj of type @type. @obj must implement
  147. * Resettable interface.
  148. *
  149. * Calling this function is equivalent to calling @resettable_assert_reset()
  150. * then @resettable_release_reset().
  151. */
  152. void resettable_reset(Object *obj, ResetType type);
  153. /**
  154. * resettable_assert_reset:
  155. * Put an object @obj into reset. @obj must implement Resettable interface.
  156. *
  157. * @resettable_release_reset() must eventually be called after this call.
  158. * There must be one call to @resettable_release_reset() per call of
  159. * @resettable_assert_reset(), with the same type argument.
  160. *
  161. * NOTE: Until support for migration is added, the @resettable_release_reset()
  162. * must not be delayed. It must occur just after @resettable_assert_reset() so
  163. * that migration cannot be triggered in between. Prefer using
  164. * @resettable_reset() for now.
  165. */
  166. void resettable_assert_reset(Object *obj, ResetType type);
  167. /**
  168. * resettable_release_reset:
  169. * Release the object @obj from reset. @obj must implement Resettable interface.
  170. *
  171. * See @resettable_assert_reset() description for details.
  172. */
  173. void resettable_release_reset(Object *obj, ResetType type);
  174. /**
  175. * resettable_is_in_reset:
  176. * Return true if @obj is under reset.
  177. *
  178. * @obj must implement Resettable interface.
  179. */
  180. bool resettable_is_in_reset(Object *obj);
  181. /**
  182. * resettable_change_parent:
  183. * Indicate that the parent of Ressettable @obj is changing from @oldp to @newp.
  184. * All 3 objects must implement resettable interface. @oldp or @newp may be
  185. * NULL.
  186. *
  187. * This function will adapt the reset state of @obj so that it is coherent
  188. * with the reset state of @newp. It may trigger @resettable_assert_reset()
  189. * or @resettable_release_reset(). It will do such things only if the reset
  190. * state of @newp and @oldp are different.
  191. *
  192. * When using this function during reset, it must only be called during
  193. * a hold phase method. Calling this during enter or exit phase is an error.
  194. */
  195. void resettable_change_parent(Object *obj, Object *newp, Object *oldp);
  196. /**
  197. * resettable_cold_reset_fn:
  198. * Helper to call resettable_reset((Object *) opaque, RESET_TYPE_COLD).
  199. *
  200. * This function is typically useful to register a reset handler with
  201. * qemu_register_reset.
  202. */
  203. void resettable_cold_reset_fn(void *opaque);
  204. /**
  205. * resettable_class_set_parent_phases:
  206. *
  207. * Save @rc current reset phases into @parent_phases and override @rc phases
  208. * by the given new methods (@enter, @hold and @exit).
  209. * Each phase is overridden only if the new one is not NULL allowing to
  210. * override a subset of phases.
  211. */
  212. void resettable_class_set_parent_phases(ResettableClass *rc,
  213. ResettableEnterPhase enter,
  214. ResettableHoldPhase hold,
  215. ResettableExitPhase exit,
  216. ResettablePhases *parent_phases);
  217. #endif