suspend-flows.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. include:: <isonum.txt>
  3. =========================
  4. System Suspend Code Flows
  5. =========================
  6. :Copyright: |copy| 2020 Intel Corporation
  7. :Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  8. At least one global system-wide transition needs to be carried out for the
  9. system to get from the working state into one of the supported
  10. :doc:`sleep states <sleep-states>`. Hibernation requires more than one
  11. transition to occur for this purpose, but the other sleep states, commonly
  12. referred to as *system-wide suspend* (or simply *system suspend*) states, need
  13. only one.
  14. For those sleep states, the transition from the working state of the system into
  15. the target sleep state is referred to as *system suspend* too (in the majority
  16. of cases, whether this means a transition or a sleep state of the system should
  17. be clear from the context) and the transition back from the sleep state into the
  18. working state is referred to as *system resume*.
  19. The kernel code flows associated with the suspend and resume transitions for
  20. different sleep states of the system are quite similar, but there are some
  21. significant differences between the :ref:`suspend-to-idle <s2idle>` code flows
  22. and the code flows related to the :ref:`suspend-to-RAM <s2ram>` and
  23. :ref:`standby <standby>` sleep states.
  24. The :ref:`suspend-to-RAM <s2ram>` and :ref:`standby <standby>` sleep states
  25. cannot be implemented without platform support and the difference between them
  26. boils down to the platform-specific actions carried out by the suspend and
  27. resume hooks that need to be provided by the platform driver to make them
  28. available. Apart from that, the suspend and resume code flows for these sleep
  29. states are mostly identical, so they both together will be referred to as
  30. *platform-dependent suspend* states in what follows.
  31. .. _s2idle_suspend:
  32. Suspend-to-idle Suspend Code Flow
  33. =================================
  34. The following steps are taken in order to transition the system from the working
  35. state to the :ref:`suspend-to-idle <s2idle>` sleep state:
  36. 1. Invoking system-wide suspend notifiers.
  37. Kernel subsystems can register callbacks to be invoked when the suspend
  38. transition is about to occur and when the resume transition has finished.
  39. That allows them to prepare for the change of the system state and to clean
  40. up after getting back to the working state.
  41. 2. Freezing tasks.
  42. Tasks are frozen primarily in order to avoid unchecked hardware accesses
  43. from user space through MMIO regions or I/O registers exposed directly to
  44. it and to prevent user space from entering the kernel while the next step
  45. of the transition is in progress (which might have been problematic for
  46. various reasons).
  47. All user space tasks are intercepted as though they were sent a signal and
  48. put into uninterruptible sleep until the end of the subsequent system resume
  49. transition.
  50. The kernel threads that choose to be frozen during system suspend for
  51. specific reasons are frozen subsequently, but they are not intercepted.
  52. Instead, they are expected to periodically check whether or not they need
  53. to be frozen and to put themselves into uninterruptible sleep if so. [Note,
  54. however, that kernel threads can use locking and other concurrency controls
  55. available in kernel space to synchronize themselves with system suspend and
  56. resume, which can be much more precise than the freezing, so the latter is
  57. not a recommended option for kernel threads.]
  58. 3. Suspending devices and reconfiguring IRQs.
  59. Devices are suspended in four phases called *prepare*, *suspend*,
  60. *late suspend* and *noirq suspend* (see :ref:`driverapi_pm_devices` for more
  61. information on what exactly happens in each phase).
  62. Every device is visited in each phase, but typically it is not physically
  63. accessed in more than two of them.
  64. The runtime PM API is disabled for every device during the *late* suspend
  65. phase and high-level ("action") interrupt handlers are prevented from being
  66. invoked before the *noirq* suspend phase.
  67. Interrupts are still handled after that, but they are only acknowledged to
  68. interrupt controllers without performing any device-specific actions that
  69. would be triggered in the working state of the system (those actions are
  70. deferred till the subsequent system resume transition as described
  71. `below <s2idle_resume_>`_).
  72. IRQs associated with system wakeup devices are "armed" so that the resume
  73. transition of the system is started when one of them signals an event.
  74. 4. Freezing the scheduler tick and suspending timekeeping.
  75. When all devices have been suspended, CPUs enter the idle loop and are put
  76. into the deepest available idle state. While doing that, each of them
  77. "freezes" its own scheduler tick so that the timer events associated with
  78. the tick do not occur until the CPU is woken up by another interrupt source.
  79. The last CPU to enter the idle state also stops the timekeeping which
  80. (among other things) prevents high resolution timers from triggering going
  81. forward until the first CPU that is woken up restarts the timekeeping.
  82. That allows the CPUs to stay in the deep idle state relatively long in one
  83. go.
  84. From this point on, the CPUs can only be woken up by non-timer hardware
  85. interrupts. If that happens, they go back to the idle state unless the
  86. interrupt that woke up one of them comes from an IRQ that has been armed for
  87. system wakeup, in which case the system resume transition is started.
  88. .. _s2idle_resume:
  89. Suspend-to-idle Resume Code Flow
  90. ================================
  91. The following steps are taken in order to transition the system from the
  92. :ref:`suspend-to-idle <s2idle>` sleep state into the working state:
  93. 1. Resuming timekeeping and unfreezing the scheduler tick.
  94. When one of the CPUs is woken up (by a non-timer hardware interrupt), it
  95. leaves the idle state entered in the last step of the preceding suspend
  96. transition, restarts the timekeeping (unless it has been restarted already
  97. by another CPU that woke up earlier) and the scheduler tick on that CPU is
  98. unfrozen.
  99. If the interrupt that has woken up the CPU was armed for system wakeup,
  100. the system resume transition begins.
  101. 2. Resuming devices and restoring the working-state configuration of IRQs.
  102. Devices are resumed in four phases called *noirq resume*, *early resume*,
  103. *resume* and *complete* (see :ref:`driverapi_pm_devices` for more
  104. information on what exactly happens in each phase).
  105. Every device is visited in each phase, but typically it is not physically
  106. accessed in more than two of them.
  107. The working-state configuration of IRQs is restored after the *noirq* resume
  108. phase and the runtime PM API is re-enabled for every device whose driver
  109. supports it during the *early* resume phase.
  110. 3. Thawing tasks.
  111. Tasks frozen in step 2 of the preceding `suspend <s2idle_suspend_>`_
  112. transition are "thawed", which means that they are woken up from the
  113. uninterruptible sleep that they went into at that time and user space tasks
  114. are allowed to exit the kernel.
  115. 4. Invoking system-wide resume notifiers.
  116. This is analogous to step 1 of the `suspend <s2idle_suspend_>`_ transition
  117. and the same set of callbacks is invoked at this point, but a different
  118. "notification type" parameter value is passed to them.
  119. Platform-dependent Suspend Code Flow
  120. ====================================
  121. The following steps are taken in order to transition the system from the working
  122. state to platform-dependent suspend state:
  123. 1. Invoking system-wide suspend notifiers.
  124. This step is the same as step 1 of the suspend-to-idle suspend transition
  125. described `above <s2idle_suspend_>`_.
  126. 2. Freezing tasks.
  127. This step is the same as step 2 of the suspend-to-idle suspend transition
  128. described `above <s2idle_suspend_>`_.
  129. 3. Suspending devices and reconfiguring IRQs.
  130. This step is analogous to step 3 of the suspend-to-idle suspend transition
  131. described `above <s2idle_suspend_>`_, but the arming of IRQs for system
  132. wakeup generally does not have any effect on the platform.
  133. There are platforms that can go into a very deep low-power state internally
  134. when all CPUs in them are in sufficiently deep idle states and all I/O
  135. devices have been put into low-power states. On those platforms,
  136. suspend-to-idle can reduce system power very effectively.
  137. On the other platforms, however, low-level components (like interrupt
  138. controllers) need to be turned off in a platform-specific way (implemented
  139. in the hooks provided by the platform driver) to achieve comparable power
  140. reduction.
  141. That usually prevents in-band hardware interrupts from waking up the system,
  142. which must be done in a special platform-dependent way. Then, the
  143. configuration of system wakeup sources usually starts when system wakeup
  144. devices are suspended and is finalized by the platform suspend hooks later
  145. on.
  146. 4. Disabling non-boot CPUs.
  147. On some platforms the suspend hooks mentioned above must run in a one-CPU
  148. configuration of the system (in particular, the hardware cannot be accessed
  149. by any code running in parallel with the platform suspend hooks that may,
  150. and often do, trap into the platform firmware in order to finalize the
  151. suspend transition).
  152. For this reason, the CPU offline/online (CPU hotplug) framework is used
  153. to take all of the CPUs in the system, except for one (the boot CPU),
  154. offline (typically, the CPUs that have been taken offline go into deep idle
  155. states).
  156. This means that all tasks are migrated away from those CPUs and all IRQs are
  157. rerouted to the only CPU that remains online.
  158. 5. Suspending core system components.
  159. This prepares the core system components for (possibly) losing power going
  160. forward and suspends the timekeeping.
  161. 6. Platform-specific power removal.
  162. This is expected to remove power from all of the system components except
  163. for the memory controller and RAM (in order to preserve the contents of the
  164. latter) and some devices designated for system wakeup.
  165. In many cases control is passed to the platform firmware which is expected
  166. to finalize the suspend transition as needed.
  167. Platform-dependent Resume Code Flow
  168. ===================================
  169. The following steps are taken in order to transition the system from a
  170. platform-dependent suspend state into the working state:
  171. 1. Platform-specific system wakeup.
  172. The platform is woken up by a signal from one of the designated system
  173. wakeup devices (which need not be an in-band hardware interrupt) and
  174. control is passed back to the kernel (the working configuration of the
  175. platform may need to be restored by the platform firmware before the
  176. kernel gets control again).
  177. 2. Resuming core system components.
  178. The suspend-time configuration of the core system components is restored and
  179. the timekeeping is resumed.
  180. 3. Re-enabling non-boot CPUs.
  181. The CPUs disabled in step 4 of the preceding suspend transition are taken
  182. back online and their suspend-time configuration is restored.
  183. 4. Resuming devices and restoring the working-state configuration of IRQs.
  184. This step is the same as step 2 of the suspend-to-idle suspend transition
  185. described `above <s2idle_resume_>`_.
  186. 5. Thawing tasks.
  187. This step is the same as step 3 of the suspend-to-idle suspend transition
  188. described `above <s2idle_resume_>`_.
  189. 6. Invoking system-wide resume notifiers.
  190. This step is the same as step 4 of the suspend-to-idle suspend transition
  191. described `above <s2idle_resume_>`_.