2
0

reset.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. =======================================
  2. Reset in QEMU: the Resettable interface
  3. =======================================
  4. The reset of qemu objects is handled using the resettable interface declared
  5. in ``include/hw/resettable.h``.
  6. This interface allows objects to be grouped (on a tree basis); so that the
  7. whole group can be reset consistently. Each individual member object does not
  8. have to care about others; in particular, problems of order (which object is
  9. reset first) are addressed.
  10. The main object types which implement this interface are DeviceClass
  11. and BusClass.
  12. Triggering reset
  13. ----------------
  14. This section documents the APIs which "users" of a resettable object should use
  15. to control it. All resettable control functions must be called while holding
  16. the BQL.
  17. You can apply a reset to an object using ``resettable_assert_reset()``. You need
  18. to call ``resettable_release_reset()`` to release the object from reset. To
  19. instantly reset an object, without keeping it in reset state, just call
  20. ``resettable_reset()``. These functions take two parameters: a pointer to the
  21. object to reset and a reset type.
  22. The Resettable interface handles reset types with an enum ``ResetType``:
  23. ``RESET_TYPE_COLD``
  24. Cold reset is supported by every resettable object. In QEMU, it means we reset
  25. to the initial state corresponding to the start of QEMU; this might differ
  26. from what is a real hardware cold reset. It differs from other resets (like
  27. warm or bus resets) which may keep certain parts untouched.
  28. ``RESET_TYPE_SNAPSHOT_LOAD``
  29. This is called for a reset which is being done to put the system into a
  30. clean state prior to loading a snapshot. (This corresponds to a reset
  31. with ``SHUTDOWN_CAUSE_SNAPSHOT_LOAD``.) Almost all devices should treat
  32. this the same as ``RESET_TYPE_COLD``. The main exception is devices which
  33. have some non-deterministic state they want to reinitialize to a different
  34. value on each cold reset, such as RNG seed information, and which they
  35. must not reinitialize on a snapshot-load reset.
  36. ``RESET_TYPE_WAKEUP``
  37. If the machine supports waking up from a suspended state and needs to reset
  38. its devices during wake-up (from the ``MachineClass::wakeup()`` method), this
  39. reset type should be used for such a request. Devices can utilize this reset
  40. type to differentiate the reset requested during machine wake-up from other
  41. reset requests. For example, RAM content must not be lost during wake-up, and
  42. memory devices like virtio-mem that provide additional RAM must not reset
  43. such state during wake-ups, but might do so during cold resets. However, this
  44. reset type should not be used for wake-up detection, as not every machine
  45. type issues a device reset request during wake-up.
  46. ``RESET_TYPE_S390_CPU_NORMAL``
  47. This is only used for S390 CPU objects; it clears interrupts, stops
  48. processing, and clears the TLB, but does not touch register contents.
  49. ``RESET_TYPE_S390_CPU_INITIAL``
  50. This is only used for S390 CPU objects; it does everything
  51. ``RESET_TYPE_S390_CPU_NORMAL`` does and also clears the PSW, prefix,
  52. FPC, timer and control registers. It does not touch gprs, fprs or acrs.
  53. Devices which implement reset methods must treat any unknown ``ResetType``
  54. as equivalent to ``RESET_TYPE_COLD``; this will reduce the amount of
  55. existing code we need to change if we add more types in future.
  56. Calling ``resettable_reset()`` is equivalent to calling
  57. ``resettable_assert_reset()`` then ``resettable_release_reset()``. It is
  58. possible to interleave multiple calls to these three functions. There may
  59. be several reset sources/controllers of a given object. The interface handles
  60. everything and the different reset controllers do not need to know anything
  61. about each others. The object will leave reset state only when each other
  62. controllers end their reset operation. This point is handled internally by
  63. maintaining a count of in-progress resets; it is crucial to call
  64. ``resettable_release_reset()`` one time and only one time per
  65. ``resettable_assert_reset()`` call.
  66. For now migration of a device or bus in reset is not supported. Care must be
  67. taken not to delay ``resettable_release_reset()`` after its
  68. ``resettable_assert_reset()`` counterpart.
  69. Note that, since resettable is an interface, the API takes a simple Object as
  70. parameter. Still, it is a programming error to call a resettable function on a
  71. non-resettable object and it will trigger a run time assert error. Since most
  72. calls to resettable interface are done through base class functions, such an
  73. error is not likely to happen.
  74. For Devices and Buses, the following helper functions exist:
  75. - ``device_cold_reset()``
  76. - ``bus_cold_reset()``
  77. These are simple wrappers around resettable_reset() function; they only cast the
  78. Device or Bus into an Object and pass the cold reset type. When possible
  79. prefer to use these functions instead of ``resettable_reset()``.
  80. Device and bus functions co-exist because there can be semantic differences
  81. between resetting a bus and resetting the controller bridge which owns it.
  82. For example, consider a SCSI controller. Resetting the controller puts all
  83. its registers back to what reset state was as well as reset everything on the
  84. SCSI bus, whereas resetting just the SCSI bus only resets everything that's on
  85. it but not the controller.
  86. Multi-phase mechanism
  87. ---------------------
  88. This section documents the internals of the resettable interface.
  89. The resettable interface uses a multi-phase system to relieve objects and
  90. machines from reset ordering problems. To address this, the reset operation
  91. of an object is split into three well defined phases.
  92. When resetting several objects (for example the whole machine at simulation
  93. startup), all first phases of all objects are executed, then all second phases
  94. and then all third phases.
  95. The three phases are:
  96. 1. The **enter** phase is executed when the object enters reset. It resets only
  97. local state of the object; it must not do anything that has a side-effect
  98. on other objects, such as raising or lowering a qemu_irq line or reading or
  99. writing guest memory.
  100. 2. The **hold** phase is executed for entry into reset, once every object in the
  101. group which is being reset has had its *enter* phase executed. At this point
  102. devices can do actions that affect other objects.
  103. 3. The **exit** phase is executed when the object leaves the reset state.
  104. Actions affecting other objects are permitted.
  105. As said in previous section, the interface maintains a count of reset. This
  106. count is used to ensure phases are executed only when required. *enter* and
  107. *hold* phases are executed only when asserting reset for the first time
  108. (if an object is already in reset state when calling
  109. ``resettable_assert_reset()`` or ``resettable_reset()``, they are not
  110. executed).
  111. The *exit* phase is executed only when the last reset operation ends. Therefore
  112. the object does not need to care how many of reset controllers it has and how
  113. many of them have started a reset.
  114. DMA capable devices are expected to cancel all outstanding DMA operations
  115. during either 'enter' or 'hold' phases. IOMMUs are expected to reset during
  116. the 'exit' phase and this sequencing makes sure no outstanding DMA request
  117. will fault.
  118. Handling reset in a resettable object
  119. -------------------------------------
  120. This section documents the APIs that an implementation of a resettable object
  121. must provide and what functions it has access to. It is intended for people
  122. who want to implement or convert a class which has the resettable interface;
  123. for example when specializing an existing device or bus.
  124. Methods to implement
  125. ....................
  126. Three methods should be defined or left empty. Each method corresponds to a
  127. phase of the reset; they are name ``phases.enter()``, ``phases.hold()`` and
  128. ``phases.exit()``. They all take the object as parameter. The *enter* method
  129. also take the reset type as second parameter.
  130. When extending an existing class, these methods may need to be extended too.
  131. The ``resettable_class_set_parent_phases()`` class function may be used to
  132. backup parent class methods.
  133. Here follows an example to implement reset for a Device which sets an IO while
  134. in reset.
  135. ::
  136. static void mydev_reset_enter(Object *obj, ResetType type)
  137. {
  138. MyDevClass *myclass = MYDEV_GET_CLASS(obj);
  139. MyDevState *mydev = MYDEV(obj);
  140. /* call parent class enter phase */
  141. if (myclass->parent_phases.enter) {
  142. myclass->parent_phases.enter(obj, type);
  143. }
  144. /* initialize local state only */
  145. mydev->var = 0;
  146. }
  147. static void mydev_reset_hold(Object *obj, ResetType type)
  148. {
  149. MyDevClass *myclass = MYDEV_GET_CLASS(obj);
  150. MyDevState *mydev = MYDEV(obj);
  151. /* call parent class hold phase */
  152. if (myclass->parent_phases.hold) {
  153. myclass->parent_phases.hold(obj, type);
  154. }
  155. /* set an IO */
  156. qemu_set_irq(mydev->irq, 1);
  157. }
  158. static void mydev_reset_exit(Object *obj, ResetType type)
  159. {
  160. MyDevClass *myclass = MYDEV_GET_CLASS(obj);
  161. MyDevState *mydev = MYDEV(obj);
  162. /* call parent class exit phase */
  163. if (myclass->parent_phases.exit) {
  164. myclass->parent_phases.exit(obj, type);
  165. }
  166. /* clear an IO */
  167. qemu_set_irq(mydev->irq, 0);
  168. }
  169. typedef struct MyDevClass {
  170. MyParentClass parent_class;
  171. /* to store eventual parent reset methods */
  172. ResettablePhases parent_phases;
  173. } MyDevClass;
  174. static void mydev_class_init(ObjectClass *class, void *data)
  175. {
  176. MyDevClass *myclass = MYDEV_CLASS(class);
  177. ResettableClass *rc = RESETTABLE_CLASS(class);
  178. resettable_class_set_parent_phases(rc,
  179. mydev_reset_enter,
  180. mydev_reset_hold,
  181. mydev_reset_exit,
  182. &myclass->parent_phases);
  183. }
  184. In the above example, we override all three phases. It is possible to override
  185. only some of them by passing NULL instead of a function pointer to
  186. ``resettable_class_set_parent_phases()``. For example, the following will
  187. only override the *enter* phase and leave *hold* and *exit* untouched::
  188. resettable_class_set_parent_phases(rc, mydev_reset_enter, NULL, NULL,
  189. &myclass->parent_phases);
  190. This is equivalent to providing a trivial implementation of the hold and exit
  191. phases which does nothing but call the parent class's implementation of the
  192. phase.
  193. Polling the reset state
  194. .......................
  195. Resettable interface provides the ``resettable_is_in_reset()`` function.
  196. This function returns true if the object parameter is currently under reset.
  197. An object is under reset from the beginning of the *enter* phase (before
  198. either its children or its own enter method is called) to the *exit*
  199. phase. During *enter* and *hold* phase only, the function will return that the
  200. object is in reset. The state is changed after the *exit* is propagated to
  201. its children and just before calling the object's own *exit* method.
  202. This function may be used if the object behavior has to be adapted
  203. while in reset state. For example if a device has an irq input,
  204. it will probably need to ignore it while in reset; then it can for
  205. example check the reset state at the beginning of the irq callback.
  206. Note that until migration of the reset state is supported, an object
  207. should not be left in reset. So apart from being currently executing
  208. one of the reset phases, the only cases when this function will return
  209. true is if an external interaction (like changing an io) is made during
  210. *hold* or *exit* phase of another object in the same reset group.
  211. Helpers ``device_is_in_reset()`` and ``bus_is_in_reset()`` are also provided
  212. for devices and buses and should be preferred.
  213. Base class handling of reset
  214. ----------------------------
  215. This section documents parts of the reset mechanism that you only need to know
  216. about if you are extending it to work with a new base class other than
  217. DeviceClass or BusClass, or maintaining the existing code in those classes. Most
  218. people can ignore it.
  219. Methods to implement
  220. ....................
  221. There are two other methods that need to exist in a class implementing the
  222. interface: ``get_state()`` and ``child_foreach()``.
  223. ``get_state()`` is simple. *resettable* is an interface and, as a consequence,
  224. does not have any class state structure. But in order to factorize the code, we
  225. need one. This method must return a pointer to ``ResettableState`` structure.
  226. The structure must be allocated by the base class; preferably it should be
  227. located inside the object instance structure.
  228. ``child_foreach()`` is more complex. It should execute the given callback on
  229. every reset child of the given resettable object. All children must be
  230. resettable too. Additional parameters (a reset type and an opaque pointer) must
  231. be passed to the callback too.
  232. In ``DeviceClass`` and ``BusClass`` the ``ResettableState`` is located in the
  233. ``DeviceState`` and ``BusState`` structures. ``child_foreach()`` is implemented
  234. to follow the bus hierarchy; for a bus, it calls the function on every child
  235. device; for a device, it calls the function on every bus child. When we reset
  236. the main system bus, we reset the whole machine bus tree.
  237. Changing a resettable parent
  238. ............................
  239. One thing which should be taken care of by the base class is handling reset
  240. hierarchy changes.
  241. The reset hierarchy is supposed to be static and built during machine creation.
  242. But there are actually some exceptions. To cope with this, the resettable API
  243. provides ``resettable_change_parent()``. This function allows to set, update or
  244. remove the parent of a resettable object after machine creation is done. As
  245. parameters, it takes the object being moved, the old parent if any and the new
  246. parent if any.
  247. This function can be used at any time when not in a reset operation. During
  248. a reset operation it must be used only in *hold* phase. Using it in *enter* or
  249. *exit* phase is an error.
  250. Also it should not be used during machine creation, although it is harmless to
  251. do so: the function is a no-op as long as old and new parent are NULL or not
  252. in reset.
  253. There is currently 2 cases where this function is used:
  254. 1. *device hotplug*; it means a new device is introduced on a live bus.
  255. 2. *hot bus change*; it means an existing live device is added, moved or
  256. removed in the bus hierarchy. At the moment, it occurs only in the raspi
  257. machines for changing the sdbus used by sd card.
  258. Reset of the complete system
  259. ----------------------------
  260. Reset of the complete system is a little complicated. The typical
  261. flow is:
  262. 1. Code which wishes to reset the entire system does so by calling
  263. ``qemu_system_reset_request()``. This schedules a reset, but the
  264. reset will happen asynchronously after the function returns.
  265. That makes this safe to call from, for example, device models.
  266. 2. The function which is called to make the reset happen is
  267. ``qemu_system_reset()``. Generally only core system code should
  268. call this directly.
  269. 3. ``qemu_system_reset()`` calls the ``MachineClass::reset`` method of
  270. the current machine, if it has one. That method must call
  271. ``qemu_devices_reset()``. If the machine has no reset method,
  272. ``qemu_system_reset()`` calls ``qemu_devices_reset()`` directly.
  273. 4. ``qemu_devices_reset()`` performs a reset of the system, using
  274. the three-phase mechanism listed above. It resets all objects
  275. that were registered with it using ``qemu_register_resettable()``.
  276. It also calls all the functions registered with it using
  277. ``qemu_register_reset()``. Those functions are called during the
  278. "hold" phase of this reset.
  279. 5. The most important object that this reset resets is the
  280. 'sysbus' bus. The sysbus bus is the root of the qbus tree. This
  281. means that all devices on the sysbus are reset, and all their
  282. child buses, and all the devices on those child buses.
  283. 6. Devices which are not on the qbus tree are *not* automatically
  284. reset! (The most obvious example of this is CPU objects, but
  285. anything that directly inherits from ``TYPE_OBJECT`` or ``TYPE_DEVICE``
  286. rather than from ``TYPE_SYS_BUS_DEVICE`` or some other plugs-into-a-bus
  287. type will be in this category.) You need to therefore arrange for these
  288. to be reset in some other way (e.g. using ``qemu_register_resettable()``
  289. or ``qemu_register_reset()``).