cpuidle.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. include:: <isonum.txt>
  3. ========================
  4. CPU Idle Time Management
  5. ========================
  6. :Copyright: |copy| 2019 Intel Corporation
  7. :Author: Rafael J. Wysocki <[email protected]>
  8. CPU Idle Time Management Subsystem
  9. ==================================
  10. Every time one of the logical CPUs in the system (the entities that appear to
  11. fetch and execute instructions: hardware threads, if present, or processor
  12. cores) is idle after an interrupt or equivalent wakeup event, which means that
  13. there are no tasks to run on it except for the special "idle" task associated
  14. with it, there is an opportunity to save energy for the processor that it
  15. belongs to. That can be done by making the idle logical CPU stop fetching
  16. instructions from memory and putting some of the processor's functional units
  17. depended on by it into an idle state in which they will draw less power.
  18. However, there may be multiple different idle states that can be used in such a
  19. situation in principle, so it may be necessary to find the most suitable one
  20. (from the kernel perspective) and ask the processor to use (or "enter") that
  21. particular idle state. That is the role of the CPU idle time management
  22. subsystem in the kernel, called ``CPUIdle``.
  23. The design of ``CPUIdle`` is modular and based on the code duplication avoidance
  24. principle, so the generic code that in principle need not depend on the hardware
  25. or platform design details in it is separate from the code that interacts with
  26. the hardware. It generally is divided into three categories of functional
  27. units: *governors* responsible for selecting idle states to ask the processor
  28. to enter, *drivers* that pass the governors' decisions on to the hardware and
  29. the *core* providing a common framework for them.
  30. CPU Idle Time Governors
  31. =======================
  32. A CPU idle time (``CPUIdle``) governor is a bundle of policy code invoked when
  33. one of the logical CPUs in the system turns out to be idle. Its role is to
  34. select an idle state to ask the processor to enter in order to save some energy.
  35. ``CPUIdle`` governors are generic and each of them can be used on any hardware
  36. platform that the Linux kernel can run on. For this reason, data structures
  37. operated on by them cannot depend on any hardware architecture or platform
  38. design details as well.
  39. The governor itself is represented by a struct cpuidle_governor object
  40. containing four callback pointers, :c:member:`enable`, :c:member:`disable`,
  41. :c:member:`select`, :c:member:`reflect`, a :c:member:`rating` field described
  42. below, and a name (string) used for identifying it.
  43. For the governor to be available at all, that object needs to be registered
  44. with the ``CPUIdle`` core by calling :c:func:`cpuidle_register_governor()` with
  45. a pointer to it passed as the argument. If successful, that causes the core to
  46. add the governor to the global list of available governors and, if it is the
  47. only one in the list (that is, the list was empty before) or the value of its
  48. :c:member:`rating` field is greater than the value of that field for the
  49. governor currently in use, or the name of the new governor was passed to the
  50. kernel as the value of the ``cpuidle.governor=`` command line parameter, the new
  51. governor will be used from that point on (there can be only one ``CPUIdle``
  52. governor in use at a time). Also, user space can choose the ``CPUIdle``
  53. governor to use at run time via ``sysfs``.
  54. Once registered, ``CPUIdle`` governors cannot be unregistered, so it is not
  55. practical to put them into loadable kernel modules.
  56. The interface between ``CPUIdle`` governors and the core consists of four
  57. callbacks:
  58. :c:member:`enable`
  59. ::
  60. int (*enable) (struct cpuidle_driver *drv, struct cpuidle_device *dev);
  61. The role of this callback is to prepare the governor for handling the
  62. (logical) CPU represented by the struct cpuidle_device object pointed
  63. to by the ``dev`` argument. The struct cpuidle_driver object pointed
  64. to by the ``drv`` argument represents the ``CPUIdle`` driver to be used
  65. with that CPU (among other things, it should contain the list of
  66. struct cpuidle_state objects representing idle states that the
  67. processor holding the given CPU can be asked to enter).
  68. It may fail, in which case it is expected to return a negative error
  69. code, and that causes the kernel to run the architecture-specific
  70. default code for idle CPUs on the CPU in question instead of ``CPUIdle``
  71. until the ``->enable()`` governor callback is invoked for that CPU
  72. again.
  73. :c:member:`disable`
  74. ::
  75. void (*disable) (struct cpuidle_driver *drv, struct cpuidle_device *dev);
  76. Called to make the governor stop handling the (logical) CPU represented
  77. by the struct cpuidle_device object pointed to by the ``dev``
  78. argument.
  79. It is expected to reverse any changes made by the ``->enable()``
  80. callback when it was last invoked for the target CPU, free all memory
  81. allocated by that callback and so on.
  82. :c:member:`select`
  83. ::
  84. int (*select) (struct cpuidle_driver *drv, struct cpuidle_device *dev,
  85. bool *stop_tick);
  86. Called to select an idle state for the processor holding the (logical)
  87. CPU represented by the struct cpuidle_device object pointed to by the
  88. ``dev`` argument.
  89. The list of idle states to take into consideration is represented by the
  90. :c:member:`states` array of struct cpuidle_state objects held by the
  91. struct cpuidle_driver object pointed to by the ``drv`` argument (which
  92. represents the ``CPUIdle`` driver to be used with the CPU at hand). The
  93. value returned by this callback is interpreted as an index into that
  94. array (unless it is a negative error code).
  95. The ``stop_tick`` argument is used to indicate whether or not to stop
  96. the scheduler tick before asking the processor to enter the selected
  97. idle state. When the ``bool`` variable pointed to by it (which is set
  98. to ``true`` before invoking this callback) is cleared to ``false``, the
  99. processor will be asked to enter the selected idle state without
  100. stopping the scheduler tick on the given CPU (if the tick has been
  101. stopped on that CPU already, however, it will not be restarted before
  102. asking the processor to enter the idle state).
  103. This callback is mandatory (i.e. the :c:member:`select` callback pointer
  104. in struct cpuidle_governor must not be ``NULL`` for the registration
  105. of the governor to succeed).
  106. :c:member:`reflect`
  107. ::
  108. void (*reflect) (struct cpuidle_device *dev, int index);
  109. Called to allow the governor to evaluate the accuracy of the idle state
  110. selection made by the ``->select()`` callback (when it was invoked last
  111. time) and possibly use the result of that to improve the accuracy of
  112. idle state selections in the future.
  113. In addition, ``CPUIdle`` governors are required to take power management
  114. quality of service (PM QoS) constraints on the processor wakeup latency into
  115. account when selecting idle states. In order to obtain the current effective
  116. PM QoS wakeup latency constraint for a given CPU, a ``CPUIdle`` governor is
  117. expected to pass the number of the CPU to
  118. :c:func:`cpuidle_governor_latency_req()`. Then, the governor's ``->select()``
  119. callback must not return the index of an indle state whose
  120. :c:member:`exit_latency` value is greater than the number returned by that
  121. function.
  122. CPU Idle Time Management Drivers
  123. ================================
  124. CPU idle time management (``CPUIdle``) drivers provide an interface between the
  125. other parts of ``CPUIdle`` and the hardware.
  126. First of all, a ``CPUIdle`` driver has to populate the :c:member:`states` array
  127. of struct cpuidle_state objects included in the struct cpuidle_driver object
  128. representing it. Going forward this array will represent the list of available
  129. idle states that the processor hardware can be asked to enter shared by all of
  130. the logical CPUs handled by the given driver.
  131. The entries in the :c:member:`states` array are expected to be sorted by the
  132. value of the :c:member:`target_residency` field in struct cpuidle_state in
  133. the ascending order (that is, index 0 should correspond to the idle state with
  134. the minimum value of :c:member:`target_residency`). [Since the
  135. :c:member:`target_residency` value is expected to reflect the "depth" of the
  136. idle state represented by the struct cpuidle_state object holding it, this
  137. sorting order should be the same as the ascending sorting order by the idle
  138. state "depth".]
  139. Three fields in struct cpuidle_state are used by the existing ``CPUIdle``
  140. governors for computations related to idle state selection:
  141. :c:member:`target_residency`
  142. Minimum time to spend in this idle state including the time needed to
  143. enter it (which may be substantial) to save more energy than could
  144. be saved by staying in a shallower idle state for the same amount of
  145. time, in microseconds.
  146. :c:member:`exit_latency`
  147. Maximum time it will take a CPU asking the processor to enter this idle
  148. state to start executing the first instruction after a wakeup from it,
  149. in microseconds.
  150. :c:member:`flags`
  151. Flags representing idle state properties. Currently, governors only use
  152. the ``CPUIDLE_FLAG_POLLING`` flag which is set if the given object
  153. does not represent a real idle state, but an interface to a software
  154. "loop" that can be used in order to avoid asking the processor to enter
  155. any idle state at all. [There are other flags used by the ``CPUIdle``
  156. core in special situations.]
  157. The :c:member:`enter` callback pointer in struct cpuidle_state, which must not
  158. be ``NULL``, points to the routine to execute in order to ask the processor to
  159. enter this particular idle state:
  160. ::
  161. void (*enter) (struct cpuidle_device *dev, struct cpuidle_driver *drv,
  162. int index);
  163. The first two arguments of it point to the struct cpuidle_device object
  164. representing the logical CPU running this callback and the
  165. struct cpuidle_driver object representing the driver itself, respectively,
  166. and the last one is an index of the struct cpuidle_state entry in the driver's
  167. :c:member:`states` array representing the idle state to ask the processor to
  168. enter.
  169. The analogous ``->enter_s2idle()`` callback in struct cpuidle_state is used
  170. only for implementing the suspend-to-idle system-wide power management feature.
  171. The difference between in and ``->enter()`` is that it must not re-enable
  172. interrupts at any point (even temporarily) or attempt to change the states of
  173. clock event devices, which the ``->enter()`` callback may do sometimes.
  174. Once the :c:member:`states` array has been populated, the number of valid
  175. entries in it has to be stored in the :c:member:`state_count` field of the
  176. struct cpuidle_driver object representing the driver. Moreover, if any
  177. entries in the :c:member:`states` array represent "coupled" idle states (that
  178. is, idle states that can only be asked for if multiple related logical CPUs are
  179. idle), the :c:member:`safe_state_index` field in struct cpuidle_driver needs
  180. to be the index of an idle state that is not "coupled" (that is, one that can be
  181. asked for if only one logical CPU is idle).
  182. In addition to that, if the given ``CPUIdle`` driver is only going to handle a
  183. subset of logical CPUs in the system, the :c:member:`cpumask` field in its
  184. struct cpuidle_driver object must point to the set (mask) of CPUs that will be
  185. handled by it.
  186. A ``CPUIdle`` driver can only be used after it has been registered. If there
  187. are no "coupled" idle state entries in the driver's :c:member:`states` array,
  188. that can be accomplished by passing the driver's struct cpuidle_driver object
  189. to :c:func:`cpuidle_register_driver()`. Otherwise, :c:func:`cpuidle_register()`
  190. should be used for this purpose.
  191. However, it also is necessary to register struct cpuidle_device objects for
  192. all of the logical CPUs to be handled by the given ``CPUIdle`` driver with the
  193. help of :c:func:`cpuidle_register_device()` after the driver has been registered
  194. and :c:func:`cpuidle_register_driver()`, unlike :c:func:`cpuidle_register()`,
  195. does not do that automatically. For this reason, the drivers that use
  196. :c:func:`cpuidle_register_driver()` to register themselves must also take care
  197. of registering the struct cpuidle_device objects as needed, so it is generally
  198. recommended to use :c:func:`cpuidle_register()` for ``CPUIdle`` driver
  199. registration in all cases.
  200. The registration of a struct cpuidle_device object causes the ``CPUIdle``
  201. ``sysfs`` interface to be created and the governor's ``->enable()`` callback to
  202. be invoked for the logical CPU represented by it, so it must take place after
  203. registering the driver that will handle the CPU in question.
  204. ``CPUIdle`` drivers and struct cpuidle_device objects can be unregistered
  205. when they are not necessary any more which allows some resources associated with
  206. them to be released. Due to dependencies between them, all of the
  207. struct cpuidle_device objects representing CPUs handled by the given
  208. ``CPUIdle`` driver must be unregistered, with the help of
  209. :c:func:`cpuidle_unregister_device()`, before calling
  210. :c:func:`cpuidle_unregister_driver()` to unregister the driver. Alternatively,
  211. :c:func:`cpuidle_unregister()` can be called to unregister a ``CPUIdle`` driver
  212. along with all of the struct cpuidle_device objects representing CPUs handled
  213. by it.
  214. ``CPUIdle`` drivers can respond to runtime system configuration changes that
  215. lead to modifications of the list of available processor idle states (which can
  216. happen, for example, when the system's power source is switched from AC to
  217. battery or the other way around). Upon a notification of such a change,
  218. a ``CPUIdle`` driver is expected to call :c:func:`cpuidle_pause_and_lock()` to
  219. turn ``CPUIdle`` off temporarily and then :c:func:`cpuidle_disable_device()` for
  220. all of the struct cpuidle_device objects representing CPUs affected by that
  221. change. Next, it can update its :c:member:`states` array in accordance with
  222. the new configuration of the system, call :c:func:`cpuidle_enable_device()` for
  223. all of the relevant struct cpuidle_device objects and invoke
  224. :c:func:`cpuidle_resume_and_unlock()` to allow ``CPUIdle`` to be used again.