Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 .. include:: <isonum.txt>
0003 
0004 ========================
0005 CPU Idle Time Management
0006 ========================
0007 
0008 :Copyright: |copy| 2019 Intel Corporation
0009 
0010 :Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
0011 
0012 
0013 CPU Idle Time Management Subsystem
0014 ==================================
0015 
0016 Every time one of the logical CPUs in the system (the entities that appear to
0017 fetch and execute instructions: hardware threads, if present, or processor
0018 cores) is idle after an interrupt or equivalent wakeup event, which means that
0019 there are no tasks to run on it except for the special "idle" task associated
0020 with it, there is an opportunity to save energy for the processor that it
0021 belongs to.  That can be done by making the idle logical CPU stop fetching
0022 instructions from memory and putting some of the processor's functional units
0023 depended on by it into an idle state in which they will draw less power.
0024 
0025 However, there may be multiple different idle states that can be used in such a
0026 situation in principle, so it may be necessary to find the most suitable one
0027 (from the kernel perspective) and ask the processor to use (or "enter") that
0028 particular idle state.  That is the role of the CPU idle time management
0029 subsystem in the kernel, called ``CPUIdle``.
0030 
0031 The design of ``CPUIdle`` is modular and based on the code duplication avoidance
0032 principle, so the generic code that in principle need not depend on the hardware
0033 or platform design details in it is separate from the code that interacts with
0034 the hardware.  It generally is divided into three categories of functional
0035 units: *governors* responsible for selecting idle states to ask the processor
0036 to enter, *drivers* that pass the governors' decisions on to the hardware and
0037 the *core* providing a common framework for them.
0038 
0039 
0040 CPU Idle Time Governors
0041 =======================
0042 
0043 A CPU idle time (``CPUIdle``) governor is a bundle of policy code invoked when
0044 one of the logical CPUs in the system turns out to be idle.  Its role is to
0045 select an idle state to ask the processor to enter in order to save some energy.
0046 
0047 ``CPUIdle`` governors are generic and each of them can be used on any hardware
0048 platform that the Linux kernel can run on.  For this reason, data structures
0049 operated on by them cannot depend on any hardware architecture or platform
0050 design details as well.
0051 
0052 The governor itself is represented by a struct cpuidle_governor object
0053 containing four callback pointers, :c:member:`enable`, :c:member:`disable`,
0054 :c:member:`select`, :c:member:`reflect`, a :c:member:`rating` field described
0055 below, and a name (string) used for identifying it.
0056 
0057 For the governor to be available at all, that object needs to be registered
0058 with the ``CPUIdle`` core by calling :c:func:`cpuidle_register_governor()` with
0059 a pointer to it passed as the argument.  If successful, that causes the core to
0060 add the governor to the global list of available governors and, if it is the
0061 only one in the list (that is, the list was empty before) or the value of its
0062 :c:member:`rating` field is greater than the value of that field for the
0063 governor currently in use, or the name of the new governor was passed to the
0064 kernel as the value of the ``cpuidle.governor=`` command line parameter, the new
0065 governor will be used from that point on (there can be only one ``CPUIdle``
0066 governor in use at a time).  Also, user space can choose the ``CPUIdle``
0067 governor to use at run time via ``sysfs``.
0068 
0069 Once registered, ``CPUIdle`` governors cannot be unregistered, so it is not
0070 practical to put them into loadable kernel modules.
0071 
0072 The interface between ``CPUIdle`` governors and the core consists of four
0073 callbacks:
0074 
0075 :c:member:`enable`
0076         ::
0077 
0078           int (*enable) (struct cpuidle_driver *drv, struct cpuidle_device *dev);
0079 
0080         The role of this callback is to prepare the governor for handling the
0081         (logical) CPU represented by the struct cpuidle_device object   pointed
0082         to by the ``dev`` argument.  The struct cpuidle_driver object pointed
0083         to by the ``drv`` argument represents the ``CPUIdle`` driver to be used
0084         with that CPU (among other things, it should contain the list of
0085         struct cpuidle_state objects representing idle states that the
0086         processor holding the given CPU can be asked to enter).
0087 
0088         It may fail, in which case it is expected to return a negative error
0089         code, and that causes the kernel to run the architecture-specific
0090         default code for idle CPUs on the CPU in question instead of ``CPUIdle``
0091         until the ``->enable()`` governor callback is invoked for that CPU
0092         again.
0093 
0094 :c:member:`disable`
0095         ::
0096 
0097           void (*disable) (struct cpuidle_driver *drv, struct cpuidle_device *dev);
0098 
0099         Called to make the governor stop handling the (logical) CPU represented
0100         by the struct cpuidle_device object pointed to by the ``dev``
0101         argument.
0102 
0103         It is expected to reverse any changes made by the ``->enable()``
0104         callback when it was last invoked for the target CPU, free all memory
0105         allocated by that callback and so on.
0106 
0107 :c:member:`select`
0108         ::
0109 
0110           int (*select) (struct cpuidle_driver *drv, struct cpuidle_device *dev,
0111                          bool *stop_tick);
0112 
0113         Called to select an idle state for the processor holding the (logical)
0114         CPU represented by the struct cpuidle_device object pointed to by the
0115         ``dev`` argument.
0116 
0117         The list of idle states to take into consideration is represented by the
0118         :c:member:`states` array of struct cpuidle_state objects held by the
0119         struct cpuidle_driver object pointed to by the ``drv`` argument (which
0120         represents the ``CPUIdle`` driver to be used with the CPU at hand).  The
0121         value returned by this callback is interpreted as an index into that
0122         array (unless it is a negative error code).
0123 
0124         The ``stop_tick`` argument is used to indicate whether or not to stop
0125         the scheduler tick before asking the processor to enter the selected
0126         idle state.  When the ``bool`` variable pointed to by it (which is set
0127         to ``true`` before invoking this callback) is cleared to ``false``, the
0128         processor will be asked to enter the selected idle state without
0129         stopping the scheduler tick on the given CPU (if the tick has been
0130         stopped on that CPU already, however, it will not be restarted before
0131         asking the processor to enter the idle state).
0132 
0133         This callback is mandatory (i.e. the :c:member:`select` callback pointer
0134         in struct cpuidle_governor must not be ``NULL`` for the registration
0135         of the governor to succeed).
0136 
0137 :c:member:`reflect`
0138         ::
0139 
0140           void (*reflect) (struct cpuidle_device *dev, int index);
0141 
0142         Called to allow the governor to evaluate the accuracy of the idle state
0143         selection made by the ``->select()`` callback (when it was invoked last
0144         time) and possibly use the result of that to improve the accuracy of
0145         idle state selections in the future.
0146 
0147 In addition, ``CPUIdle`` governors are required to take power management
0148 quality of service (PM QoS) constraints on the processor wakeup latency into
0149 account when selecting idle states.  In order to obtain the current effective
0150 PM QoS wakeup latency constraint for a given CPU, a ``CPUIdle`` governor is
0151 expected to pass the number of the CPU to
0152 :c:func:`cpuidle_governor_latency_req()`.  Then, the governor's ``->select()``
0153 callback must not return the index of an indle state whose
0154 :c:member:`exit_latency` value is greater than the number returned by that
0155 function.
0156 
0157 
0158 CPU Idle Time Management Drivers
0159 ================================
0160 
0161 CPU idle time management (``CPUIdle``) drivers provide an interface between the
0162 other parts of ``CPUIdle`` and the hardware.
0163 
0164 First of all, a ``CPUIdle`` driver has to populate the :c:member:`states` array
0165 of struct cpuidle_state objects included in the struct cpuidle_driver object
0166 representing it.  Going forward this array will represent the list of available
0167 idle states that the processor hardware can be asked to enter shared by all of
0168 the logical CPUs handled by the given driver.
0169 
0170 The entries in the :c:member:`states` array are expected to be sorted by the
0171 value of the :c:member:`target_residency` field in struct cpuidle_state in
0172 the ascending order (that is, index 0 should correspond to the idle state with
0173 the minimum value of :c:member:`target_residency`).  [Since the
0174 :c:member:`target_residency` value is expected to reflect the "depth" of the
0175 idle state represented by the struct cpuidle_state object holding it, this
0176 sorting order should be the same as the ascending sorting order by the idle
0177 state "depth".]
0178 
0179 Three fields in struct cpuidle_state are used by the existing ``CPUIdle``
0180 governors for computations related to idle state selection:
0181 
0182 :c:member:`target_residency`
0183         Minimum time to spend in this idle state including the time needed to
0184         enter it (which may be substantial) to save more energy than could
0185         be saved by staying in a shallower idle state for the same amount of
0186         time, in microseconds.
0187 
0188 :c:member:`exit_latency`
0189         Maximum time it will take a CPU asking the processor to enter this idle
0190         state to start executing the first instruction after a wakeup from it,
0191         in microseconds.
0192 
0193 :c:member:`flags`
0194         Flags representing idle state properties.  Currently, governors only use
0195         the ``CPUIDLE_FLAG_POLLING`` flag which is set if the given object
0196         does not represent a real idle state, but an interface to a software
0197         "loop" that can be used in order to avoid asking the processor to enter
0198         any idle state at all.  [There are other flags used by the ``CPUIdle``
0199         core in special situations.]
0200 
0201 The :c:member:`enter` callback pointer in struct cpuidle_state, which must not
0202 be ``NULL``, points to the routine to execute in order to ask the processor to
0203 enter this particular idle state:
0204 
0205 ::
0206 
0207   void (*enter) (struct cpuidle_device *dev, struct cpuidle_driver *drv,
0208                  int index);
0209 
0210 The first two arguments of it point to the struct cpuidle_device object
0211 representing the logical CPU running this callback and the
0212 struct cpuidle_driver object representing the driver itself, respectively,
0213 and the last one is an index of the struct cpuidle_state entry in the driver's
0214 :c:member:`states` array representing the idle state to ask the processor to
0215 enter.
0216 
0217 The analogous ``->enter_s2idle()`` callback in struct cpuidle_state is used
0218 only for implementing the suspend-to-idle system-wide power management feature.
0219 The difference between in and ``->enter()`` is that it must not re-enable
0220 interrupts at any point (even temporarily) or attempt to change the states of
0221 clock event devices, which the ``->enter()`` callback may do sometimes.
0222 
0223 Once the :c:member:`states` array has been populated, the number of valid
0224 entries in it has to be stored in the :c:member:`state_count` field of the
0225 struct cpuidle_driver object representing the driver.  Moreover, if any
0226 entries in the :c:member:`states` array represent "coupled" idle states (that
0227 is, idle states that can only be asked for if multiple related logical CPUs are
0228 idle), the :c:member:`safe_state_index` field in struct cpuidle_driver needs
0229 to be the index of an idle state that is not "coupled" (that is, one that can be
0230 asked for if only one logical CPU is idle).
0231 
0232 In addition to that, if the given ``CPUIdle`` driver is only going to handle a
0233 subset of logical CPUs in the system, the :c:member:`cpumask` field in its
0234 struct cpuidle_driver object must point to the set (mask) of CPUs that will be
0235 handled by it.
0236 
0237 A ``CPUIdle`` driver can only be used after it has been registered.  If there
0238 are no "coupled" idle state entries in the driver's :c:member:`states` array,
0239 that can be accomplished by passing the driver's struct cpuidle_driver object
0240 to :c:func:`cpuidle_register_driver()`.  Otherwise, :c:func:`cpuidle_register()`
0241 should be used for this purpose.
0242 
0243 However, it also is necessary to register struct cpuidle_device objects for
0244 all of the logical CPUs to be handled by the given ``CPUIdle`` driver with the
0245 help of :c:func:`cpuidle_register_device()` after the driver has been registered
0246 and :c:func:`cpuidle_register_driver()`, unlike :c:func:`cpuidle_register()`,
0247 does not do that automatically.  For this reason, the drivers that use
0248 :c:func:`cpuidle_register_driver()` to register themselves must also take care
0249 of registering the struct cpuidle_device objects as needed, so it is generally
0250 recommended to use :c:func:`cpuidle_register()` for ``CPUIdle`` driver
0251 registration in all cases.
0252 
0253 The registration of a struct cpuidle_device object causes the ``CPUIdle``
0254 ``sysfs`` interface to be created and the governor's ``->enable()`` callback to
0255 be invoked for the logical CPU represented by it, so it must take place after
0256 registering the driver that will handle the CPU in question.
0257 
0258 ``CPUIdle`` drivers and struct cpuidle_device objects can be unregistered
0259 when they are not necessary any more which allows some resources associated with
0260 them to be released.  Due to dependencies between them, all of the
0261 struct cpuidle_device objects representing CPUs handled by the given
0262 ``CPUIdle`` driver must be unregistered, with the help of
0263 :c:func:`cpuidle_unregister_device()`, before calling
0264 :c:func:`cpuidle_unregister_driver()` to unregister the driver.  Alternatively,
0265 :c:func:`cpuidle_unregister()` can be called to unregister a ``CPUIdle`` driver
0266 along with all of the struct cpuidle_device objects representing CPUs handled
0267 by it.
0268 
0269 ``CPUIdle`` drivers can respond to runtime system configuration changes that
0270 lead to modifications of the list of available processor idle states (which can
0271 happen, for example, when the system's power source is switched from AC to
0272 battery or the other way around).  Upon a notification of such a change,
0273 a ``CPUIdle`` driver is expected to call :c:func:`cpuidle_pause_and_lock()` to
0274 turn ``CPUIdle`` off temporarily and then :c:func:`cpuidle_disable_device()` for
0275 all of the struct cpuidle_device objects representing CPUs affected by that
0276 change.  Next, it can update its :c:member:`states` array in accordance with
0277 the new configuration of the system, call :c:func:`cpuidle_enable_device()` for
0278 all of the relevant struct cpuidle_device objects and invoke
0279 :c:func:`cpuidle_resume_and_unlock()` to allow ``CPUIdle`` to be used again.