xref: /linux/Documentation/admin-guide/pm/intel_idle.rst (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1.. SPDX-License-Identifier: GPL-2.0
2.. include:: <isonum.txt>
3
4==============================================
5``intel_idle`` CPU Idle Time Management Driver
6==============================================
7
8:Copyright: |copy| 2020 Intel Corporation
9
10:Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
11
12
13General Information
14===================
15
16``intel_idle`` is a part of the
17:doc:`CPU idle time management subsystem <cpuidle>` in the Linux kernel
18(``CPUIdle``).  It is the default CPU idle time management driver for the
19Nehalem and later generations of Intel processors, but the level of support for
20a particular processor model in it depends on whether or not it recognizes that
21processor model and may also depend on information coming from the platform
22firmware.  [To understand ``intel_idle`` it is necessary to know how ``CPUIdle``
23works in general, so this is the time to get familiar with
24Documentation/admin-guide/pm/cpuidle.rst if you have not done that yet.]
25
26``intel_idle`` uses the ``MWAIT`` instruction to inform the processor that the
27logical CPU executing it is idle and so it may be possible to put some of the
28processor's functional blocks into low-power states.  That instruction takes two
29arguments (passed in the ``EAX`` and ``ECX`` registers of the target CPU), the
30first of which, referred to as a *hint*, can be used by the processor to
31determine what can be done (for details refer to Intel Software Developer’s
32Manual [1]_).  Accordingly, ``intel_idle`` refuses to work with processors in
33which the support for the ``MWAIT`` instruction has been disabled (for example,
34via the platform firmware configuration menu) or which do not support that
35instruction at all.
36
37``intel_idle`` is not modular, so it cannot be unloaded, which means that the
38only way to pass early-configuration-time parameters to it is via the kernel
39command line.
40
41Sysfs Interface
42===============
43
44The ``intel_idle`` driver exposes the following ``sysfs`` attributes in
45``/sys/devices/system/cpu/cpuidle/``:
46
47``intel_c1_demotion``
48	Enable or disable C1 demotion for all CPUs in the system. This file is
49	only exposed on platforms that support the C1 demotion feature and where
50	it was tested. Value 0 means that C1 demotion is disabled, value 1 means
51	that it is enabled. Write 0 or 1 to disable or enable C1 demotion for
52	all CPUs.
53
54	The C1 demotion feature involves the platform firmware demoting deep
55	C-state requests from the OS (e.g., C6 requests) to C1. The idea is that
56	firmware monitors CPU wake-up rate, and if it is higher than a
57	platform-specific threshold, the firmware demotes deep C-state requests
58	to C1. For example, Linux requests C6, but firmware noticed too many
59	wake-ups per second, and it keeps the CPU in C1. When the CPU stays in
60	C1 long enough, the platform promotes it back to C6. This may improve
61	some workloads' performance, but it may also increase power consumption.
62
63.. _intel-idle-enumeration-of-states:
64
65Enumeration of Idle States
66==========================
67
68Each ``MWAIT`` hint value is interpreted by the processor as a license to
69reconfigure itself in a certain way in order to save energy.  The processor
70configurations (with reduced power draw) resulting from that are referred to
71as C-states (in the ACPI terminology) or idle states.  The list of meaningful
72``MWAIT`` hint values and idle states (i.e. low-power configurations of the
73processor) corresponding to them depends on the processor model and it may also
74depend on the configuration of the platform.
75
76In order to create a list of available idle states required by the ``CPUIdle``
77subsystem (see :ref:`idle-states-representation` in
78Documentation/admin-guide/pm/cpuidle.rst),
79``intel_idle`` can use two sources of information: static tables of idle states
80for different processor models included in the driver itself and the ACPI tables
81of the system.  The former are always used if the processor model at hand is
82recognized by ``intel_idle`` and the latter are used if that is required for
83the given processor model (which is the case for all server processor models
84recognized by ``intel_idle``) or if the processor model is not recognized.
85[There is a module parameter that can be used to make the driver use the ACPI
86tables with any processor model recognized by it; see
87`below <intel-idle-parameters_>`_.]
88
89If the ACPI tables are going to be used for building the list of available idle
90states, ``intel_idle`` will be looking for ``_LPI`` or ``_CST`` objects in them
91(refer to the ACPI specification [2]_ for the definitions of the ``_LPI`` and
92``_CST`` objects).  If ``_LPI`` is present under at least one of the ACPI
93objects representing the CPUs in the system and ``_LPI`` processing produces a
94non-empty list of valid idle states, it will be used.  Otherwise, ``_CST`` will
95be used so long as it is present under at least one of the ACPI objects
96representing the CPUs in the system and it returns a non-empty list of valid
97idle states.  In either case, since the ``CPUIdle`` subsystem expects that the
98list of idle states supplied by the driver will be suitable for all of the CPUs
99handled by it and ``intel_idle`` is registered as the ``CPUIdle`` driver for all
100of the CPUs in the system, ``intel_idle`` looks for the first CPU where the
101ACPI-supplied list of idle states (coming from either ``_LPI`` or ``_CST``)
102is not empty.  Moreover, all of the states in that list need to be of the FFH
103(Functional Fixed Hardware) type, which means that the ``MWAIT`` instruction is
104expected to be used to tell the processor that the given idle state may be
105entered.  If that expectation is met, the list of idle states is assumed to be
106applicable to all of the other CPUs in the system and the idle state
107descriptions extracted from it are stored in a preliminary list of idle states
108coming from the ACPI tables.  [This step is skipped if ``intel_idle`` is
109configured to ignore the ACPI tables; see `below <intel-idle-parameters_>`_.]
110
111Next, the first (index 0) entry in the list of available idle states is
112initialized to represent a "polling idle state" (a pseudo-idle state in which
113the target CPU continuously fetches and executes instructions), and the
114subsequent (real) idle state entries are populated as follows.
115
116If the processor model at hand is recognized by ``intel_idle``, there is a
117(static) table of idle state descriptions for it in the driver.  In that case,
118the "internal" table is the primary source of information on idle states and the
119information from it is copied to the final list of available idle states.  If
120using the ACPI tables for the enumeration of idle states is not required
121(depending on the processor model), all of the listed idle state are enabled by
122default (so all of them will be taken into consideration by ``CPUIdle``
123governors during CPU idle state selection).  Otherwise, some of the listed idle
124states may not be enabled by default if there are no matching entries in the
125preliminary list of idle states coming from the ACPI tables.  In that case user
126space still can enable them later (on a per-CPU basis) with the help of
127the ``disable`` idle state attribute in ``sysfs`` (see
128:ref:`idle-states-representation` in
129Documentation/admin-guide/pm/cpuidle.rst).  This basically means that
130the idle states "known" to the driver may not be enabled by default if they have
131not been exposed by the platform firmware (through the ACPI tables).
132
133If the given processor model is not recognized by ``intel_idle``, but it
134supports ``MWAIT``, the preliminary list of idle states coming from the ACPI
135tables is used for building the final list that will be supplied to the
136``CPUIdle`` core during driver registration.  For each idle state in that list,
137the description, ``MWAIT`` hint and exit (wake) latency are copied to the
138corresponding entry in the final list of idle states.  If the preliminary list
139of idle states has been obtained through ``_LPI`` processing, the minimum
140residency parameter of the given idle state is taken as its target residency.
141Otherwise, for C1-type idle states, the exit latency value is also used as the
142target residency (for compatibility with the majority of the "internal" tables
143of idle states for various processor models recognized by ``intel_idle``), and
144for the other idle state types (C2 and C3) the target residency value is 3 times
145the exit latency (again, that is because it reflects the target residency to
146exit latency ratio in the majority of cases for the processor models recognized
147by ``intel_idle``).  The name of the idle state (to be returned by the ``name``
148idle state attribute in ``sysfs``) either is equal to the state name value that
149comes from ``_LPI`` processing (with the possible fallback to "Cx_LPI" if the
150state name is not provided) or it is "Cx_ACPI", where x is the index of that
151idle state in the final list (note that the minimum value of x is 1, because 0
152is reserved for the "polling" state), and its target residency is based on the
153exit latency value.  All of the idle states in the final list are enabled by
154default in this case.
155
156
157.. _intel-idle-initialization:
158
159Initialization
160==============
161
162The initialization of ``intel_idle`` starts with checking if the kernel command
163line options forbid the use of the ``MWAIT`` instruction.  If that is the case,
164an error code is returned right away.
165
166The next step is to check whether or not the processor model is known to the
167driver, which determines the idle states enumeration method (see
168`above <intel-idle-enumeration-of-states_>`_), and whether or not the processor
169supports ``MWAIT`` (the initialization fails if that is not the case).  Then,
170the ``MWAIT`` support in the processor is enumerated through ``CPUID`` and the
171driver initialization fails if the level of support is not as expected (for
172example, if the total number of ``MWAIT`` substates returned is 0).
173
174Next, if the driver is not configured to ignore the ACPI tables (see
175`below <intel-idle-parameters_>`_), the idle states information provided by the
176platform firmware is extracted from them.
177
178Then, ``CPUIdle`` device objects are allocated for all CPUs and the list of
179available idle states is created as explained
180`above <intel-idle-enumeration-of-states_>`_.
181
182Finally, ``intel_idle`` is registered with the help of cpuidle_register_driver()
183as the ``CPUIdle`` driver for all CPUs in the system and a CPU online callback
184for configuring individual CPUs is registered via cpuhp_setup_state(), which
185(among other things) causes the callback routine to be invoked for all of the
186CPUs present in the system at that time (each CPU executes its own instance of
187the callback routine).  That routine registers a ``CPUIdle`` device for the CPU
188running it (which enables the ``CPUIdle`` subsystem to operate that CPU) and
189optionally performs some CPU-specific initialization actions that may be
190required for the given processor model.
191
192
193.. _intel-idle-parameters:
194
195Kernel Command Line Options and Module Parameters
196=================================================
197
198The *x86* architecture support code recognizes three kernel command line
199options related to CPU idle time management: ``idle=poll``, ``idle=halt``,
200and ``idle=nomwait``.  If any of them is present in the kernel command line, the
201``MWAIT`` instruction is not allowed to be used, so the initialization of
202``intel_idle`` will fail.
203
204Apart from that there are five module parameters recognized by ``intel_idle``
205itself that can be set via the kernel command line (they cannot be updated via
206sysfs, so that is the only way to change their values).
207
208The ``max_cstate`` parameter value is the maximum idle state index in the list
209of idle states supplied to the ``CPUIdle`` core during the registration of the
210driver.  It is also the maximum number of regular (non-polling) idle states that
211can be used by ``intel_idle``, so the enumeration of idle states is terminated
212after finding that number of usable idle states (the other idle states that
213potentially might have been used if ``max_cstate`` had been greater are not
214taken into consideration at all).  Setting ``max_cstate`` can prevent
215``intel_idle`` from exposing idle states that are regarded as "too deep" for
216some reason to the ``CPUIdle`` core, but it does so by making them effectively
217invisible until the system is shut down and started again which may not always
218be desirable.  In practice, it is only really necessary to do that if the idle
219states in question cannot be enabled during system startup, because in the
220working state of the system the CPU power management quality of service (PM
221QoS) feature can be used to prevent ``CPUIdle`` from touching those idle states
222even if they have been enumerated (see :ref:`cpu-pm-qos` in
223Documentation/admin-guide/pm/cpuidle.rst).
224Setting ``max_cstate`` to 0 causes the ``intel_idle`` initialization to fail.
225
226The ``no_acpi``, ``use_acpi`` and ``no_native`` module parameters are
227recognized by ``intel_idle`` if the kernel has been configured with ACPI
228support.  In the case that ACPI is not configured these flags have no impact
229on functionality.
230
231``no_acpi`` - Do not use ACPI at all.  Only native mode is available, no
232ACPI mode.
233
234``use_acpi`` - No-op in ACPI mode, the driver will consult ACPI tables for
235C-states on/off status in native mode.
236
237``no_native`` - Work only in ACPI mode, no native mode available (ignore
238all custom tables).
239
240The value of the ``states_off`` module parameter (0 by default) represents a
241list of idle states to be disabled by default in the form of a bitmask.
242
243Namely, the positions of the bits that are set in the ``states_off`` value are
244the indices of idle states to be disabled by default (as reflected by the names
245of the corresponding idle state directories in ``sysfs``, :file:`state0`,
246:file:`state1` ... :file:`state<i>` ..., where ``<i>`` is the index of the given
247idle state; see :ref:`idle-states-representation` in
248Documentation/admin-guide/pm/cpuidle.rst).
249
250For example, if ``states_off`` is equal to 3, the driver will disable idle
251states 0 and 1 by default, and if it is equal to 8, idle state 3 will be
252disabled by default and so on (bit positions beyond the maximum idle state index
253are ignored).
254
255The idle states disabled this way can be enabled (on a per-CPU basis) from user
256space via ``sysfs``.
257
258The ``ibrs_off`` module parameter is a boolean flag (defaults to
259false). If set, it is used to control if IBRS (Indirect Branch Restricted
260Speculation) should be turned off when the CPU enters an idle state.
261This flag does not affect CPUs that use Enhanced IBRS which can remain
262on with little performance impact.
263
264For some CPUs, IBRS will be selected as mitigation for Spectre v2 and Retbleed
265security vulnerabilities by default.  Leaving the IBRS mode on while idling may
266have a performance impact on its sibling CPU.  The IBRS mode will be turned off
267by default when the CPU enters into a deep idle state, but not in some
268shallower ones.  Setting the ``ibrs_off`` module parameter will force the IBRS
269mode to off when the CPU is in any one of the available idle states.  This may
270help performance of a sibling CPU at the expense of a slightly higher wakeup
271latency for the idle CPU.
272
273The ``table`` argument allows customization of idle state latency and target
274residency. The syntax is a comma-separated list of ``name:latency:residency``
275entries, where ``name`` is the idle state name, ``latency`` is the exit latency
276in microseconds, and ``residency`` is the target residency in microseconds. It
277is not necessary to specify all idle states; only those to be customized. For
278example, ``C1:1:3,C6:50:100`` sets the exit latency and target residency for
279C1 and C6 to 1/3 and 50/100 microseconds, respectively. Remaining idle states
280keep their default values. The driver verifies that deeper idle states have
281higher latency and target residency than shallower ones. Also, target
282residency cannot be smaller than exit latency. If any of these conditions is
283not met, the driver ignores the entire ``table`` parameter.
284
285.. _intel-idle-core-and-package-idle-states:
286
287Core and Package Levels of Idle States
288======================================
289
290Typically, in a processor supporting the ``MWAIT`` instruction there are (at
291least) two levels of idle states (or C-states).  One level, referred to as
292"core C-states", covers individual cores in the processor, whereas the other
293level, referred to as "package C-states", covers the entire processor package
294and it may also involve other components of the system (GPUs, memory
295controllers, I/O hubs etc.).
296
297Some of the ``MWAIT`` hint values allow the processor to use core C-states only
298(most importantly, that is the case for the ``MWAIT`` hint value corresponding
299to the ``C1`` idle state), but the majority of them give it a license to put
300the target core (i.e. the core containing the logical CPU executing ``MWAIT``
301with the given hint value) into a specific core C-state and then (if possible)
302to enter a specific package C-state at the deeper level.  For example, the
303``MWAIT`` hint value representing the ``C3`` idle state allows the processor to
304put the target core into the low-power state referred to as "core ``C3``" (or
305``CC3``), which happens if all of the logical CPUs (SMT siblings) in that core
306have executed ``MWAIT`` with the ``C3`` hint value (or with a hint value
307representing a deeper idle state), and in addition to that (in the majority of
308cases) it gives the processor a license to put the entire package (possibly
309including some non-CPU components such as a GPU or a memory controller) into the
310low-power state referred to as "package ``C3``" (or ``PC3``), which happens if
311all of the cores have gone into the ``CC3`` state and (possibly) some additional
312conditions are satisfied (for instance, if the GPU is covered by ``PC3``, it may
313be required to be in a certain GPU-specific low-power state for ``PC3`` to be
314reachable).
315
316As a rule, there is no simple way to make the processor use core C-states only
317if the conditions for entering the corresponding package C-states are met, so
318the logical CPU executing ``MWAIT`` with a hint value that is not core-level
319only (like for ``C1``) must always assume that this may cause the processor to
320enter a package C-state.  [That is why the exit latency and target residency
321values corresponding to the majority of ``MWAIT`` hint values in the "internal"
322tables of idle states in ``intel_idle`` reflect the properties of package
323C-states.]  If using package C-states is not desirable at all, either
324:ref:`PM QoS <cpu-pm-qos>` or the ``max_cstate`` module parameter of
325``intel_idle`` described `above <intel-idle-parameters_>`_ must be used to
326restrict the range of permissible idle states to the ones with core-level only
327``MWAIT`` hint values (like ``C1``).
328
329
330References
331==========
332
333.. [1] *Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2B*,
334       https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-2b-manual.html
335
336.. [2] *Advanced Configuration and Power Interface (ACPI) Specification*,
337       https://uefi.org/specifications
338