1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_idle.c - native hardware idle loop for modern Intel processors
5 * Copyright (c) 2013 - 2020, Intel Corporation.
11 * intel_idle is a cpuidle driver that loads on all Intel CPUs with MWAIT
12 * in lieu of the legacy ACPI processor_idle driver. The intent is to
13 * make Linux more efficient on these processors, as intel_idle knows
14 * more than ACPI, as well as make Linux more immune to ACPI BIOS bugs.
20 * All CPUs have same idle states as boot CPU
22 * Chipset BM_STS (bus master status) bit is a NOP
23 * for preventing entry into deep C-states
25 * CPU will flush caches as needed when entering a C-state via MWAIT
26 * (in contrast to entering ACPI C3, in which case the WBINVD
27 * instruction needs to be executed to flush the caches)
33 * ACPI has a .suspend hack to turn off deep c-statees during suspend
34 * to avoid complications with the lapic timer workaround.
35 * Have not seen issues with suspend, but may need same workaround here.
39 /* un-comment DEBUG to enable pr_debug() statements */
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44 #include <linux/acpi.h>
45 #include <linux/kernel.h>
46 #include <linux/cpuidle.h>
47 #include <linux/tick.h>
48 #include <trace/events/power.h>
49 #include <linux/sched.h>
50 #include <linux/sched/smt.h>
51 #include <linux/notifier.h>
52 #include <linux/cpu.h>
53 #include <linux/moduleparam.h>
54 #include <asm/cpu_device_id.h>
55 #include <asm/intel-family.h>
56 #include <asm/nospec-branch.h>
57 #include <asm/mwait.h>
59 #include <asm/fpu/api.h>
61 #define INTEL_IDLE_VERSION "0.5.1"
63 static struct cpuidle_driver intel_idle_driver = {
67 /* intel_idle.max_cstate=0 disables driver */
68 static int max_cstate = CPUIDLE_STATE_MAX - 1;
69 static unsigned int disabled_states_mask;
70 static unsigned int preferred_states_mask;
72 static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
74 static unsigned long auto_demotion_disable_flags;
77 C1E_PROMOTION_PRESERVE,
80 } c1e_promotion = C1E_PROMOTION_PRESERVE;
83 struct cpuidle_state *state_table;
86 * Hardware C-state auto-demotion may not always be optimal.
87 * Indicate which enable bits to clear here.
89 unsigned long auto_demotion_disable_flags;
90 bool byt_auto_demotion_disable_flag;
91 bool disable_promotion_to_c1e;
95 static const struct idle_cpu *icpu __initdata;
96 static struct cpuidle_state *cpuidle_state_table __initdata;
98 static unsigned int mwait_substates __initdata;
101 * Enable interrupts before entering the C-state. On some platforms and for
102 * some C-states, this may measurably decrease interrupt latency.
104 #define CPUIDLE_FLAG_IRQ_ENABLE BIT(14)
107 * Enable this state by default even if the ACPI _CST does not list it.
109 #define CPUIDLE_FLAG_ALWAYS_ENABLE BIT(15)
112 * Disable IBRS across idle (when KERNEL_IBRS), is exclusive vs IRQ_ENABLE
115 #define CPUIDLE_FLAG_IBRS BIT(16)
118 * Initialize large xstate for the C6-state entrance.
120 #define CPUIDLE_FLAG_INIT_XSTATE BIT(17)
123 * MWAIT takes an 8-bit "hint" in EAX "suggesting"
124 * the C-state (top nibble) and sub-state (bottom nibble)
125 * 0x00 means "MWAIT(C1)", 0x10 means "MWAIT(C2)" etc.
127 * We store the hint at the top of our "flags" for each state.
129 #define flg2MWAIT(flags) (((flags) >> 24) & 0xFF)
130 #define MWAIT2flg(eax) ((eax & 0xFF) << 24)
132 static __always_inline int __intel_idle(struct cpuidle_device *dev,
133 struct cpuidle_driver *drv, int index)
135 struct cpuidle_state *state = &drv->states[index];
136 unsigned long eax = flg2MWAIT(state->flags);
137 unsigned long ecx = 1; /* break on interrupt flag */
139 mwait_idle_with_hints(eax, ecx);
145 * intel_idle - Ask the processor to enter the given idle state.
146 * @dev: cpuidle device of the target CPU.
147 * @drv: cpuidle driver (assumed to point to intel_idle_driver).
148 * @index: Target idle state index.
150 * Use the MWAIT instruction to notify the processor that the CPU represented by
151 * @dev is idle and it can try to enter the idle state corresponding to @index.
153 * If the local APIC timer is not known to be reliable in the target idle state,
154 * enable one-shot tick broadcasting for the target CPU before executing MWAIT.
156 * Must be called under local_irq_disable().
158 static __cpuidle int intel_idle(struct cpuidle_device *dev,
159 struct cpuidle_driver *drv, int index)
161 return __intel_idle(dev, drv, index);
164 static __cpuidle int intel_idle_irq(struct cpuidle_device *dev,
165 struct cpuidle_driver *drv, int index)
169 raw_local_irq_enable();
170 ret = __intel_idle(dev, drv, index);
171 raw_local_irq_disable();
176 static __cpuidle int intel_idle_ibrs(struct cpuidle_device *dev,
177 struct cpuidle_driver *drv, int index)
179 bool smt_active = sched_smt_active();
180 u64 spec_ctrl = spec_ctrl_current();
184 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
186 ret = __intel_idle(dev, drv, index);
189 native_wrmsrl(MSR_IA32_SPEC_CTRL, spec_ctrl);
194 static __cpuidle int intel_idle_xstate(struct cpuidle_device *dev,
195 struct cpuidle_driver *drv, int index)
198 return __intel_idle(dev, drv, index);
202 * intel_idle_s2idle - Ask the processor to enter the given idle state.
203 * @dev: cpuidle device of the target CPU.
204 * @drv: cpuidle driver (assumed to point to intel_idle_driver).
205 * @index: Target idle state index.
207 * Use the MWAIT instruction to notify the processor that the CPU represented by
208 * @dev is idle and it can try to enter the idle state corresponding to @index.
210 * Invoked as a suspend-to-idle callback routine with frozen user space, frozen
211 * scheduler tick and suspended scheduler clock on the target CPU.
213 static __cpuidle int intel_idle_s2idle(struct cpuidle_device *dev,
214 struct cpuidle_driver *drv, int index)
216 unsigned long ecx = 1; /* break on interrupt flag */
217 struct cpuidle_state *state = &drv->states[index];
218 unsigned long eax = flg2MWAIT(state->flags);
220 if (state->flags & CPUIDLE_FLAG_INIT_XSTATE)
223 mwait_idle_with_hints(eax, ecx);
229 * States are indexed by the cstate number,
230 * which is also the index into the MWAIT hint array.
231 * Thus C0 is a dummy.
233 static struct cpuidle_state nehalem_cstates[] __initdata = {
236 .desc = "MWAIT 0x00",
237 .flags = MWAIT2flg(0x00),
239 .target_residency = 6,
240 .enter = &intel_idle,
241 .enter_s2idle = intel_idle_s2idle, },
244 .desc = "MWAIT 0x01",
245 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
247 .target_residency = 20,
248 .enter = &intel_idle,
249 .enter_s2idle = intel_idle_s2idle, },
252 .desc = "MWAIT 0x10",
253 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
255 .target_residency = 80,
256 .enter = &intel_idle,
257 .enter_s2idle = intel_idle_s2idle, },
260 .desc = "MWAIT 0x20",
261 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
263 .target_residency = 800,
264 .enter = &intel_idle,
265 .enter_s2idle = intel_idle_s2idle, },
270 static struct cpuidle_state snb_cstates[] __initdata = {
273 .desc = "MWAIT 0x00",
274 .flags = MWAIT2flg(0x00),
276 .target_residency = 2,
277 .enter = &intel_idle,
278 .enter_s2idle = intel_idle_s2idle, },
281 .desc = "MWAIT 0x01",
282 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
284 .target_residency = 20,
285 .enter = &intel_idle,
286 .enter_s2idle = intel_idle_s2idle, },
289 .desc = "MWAIT 0x10",
290 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
292 .target_residency = 211,
293 .enter = &intel_idle,
294 .enter_s2idle = intel_idle_s2idle, },
297 .desc = "MWAIT 0x20",
298 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
300 .target_residency = 345,
301 .enter = &intel_idle,
302 .enter_s2idle = intel_idle_s2idle, },
305 .desc = "MWAIT 0x30",
306 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
308 .target_residency = 345,
309 .enter = &intel_idle,
310 .enter_s2idle = intel_idle_s2idle, },
315 static struct cpuidle_state byt_cstates[] __initdata = {
318 .desc = "MWAIT 0x00",
319 .flags = MWAIT2flg(0x00),
321 .target_residency = 1,
322 .enter = &intel_idle,
323 .enter_s2idle = intel_idle_s2idle, },
326 .desc = "MWAIT 0x58",
327 .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
329 .target_residency = 275,
330 .enter = &intel_idle,
331 .enter_s2idle = intel_idle_s2idle, },
334 .desc = "MWAIT 0x52",
335 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
337 .target_residency = 560,
338 .enter = &intel_idle,
339 .enter_s2idle = intel_idle_s2idle, },
342 .desc = "MWAIT 0x60",
343 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
344 .exit_latency = 1200,
345 .target_residency = 4000,
346 .enter = &intel_idle,
347 .enter_s2idle = intel_idle_s2idle, },
350 .desc = "MWAIT 0x64",
351 .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
352 .exit_latency = 10000,
353 .target_residency = 20000,
354 .enter = &intel_idle,
355 .enter_s2idle = intel_idle_s2idle, },
360 static struct cpuidle_state cht_cstates[] __initdata = {
363 .desc = "MWAIT 0x00",
364 .flags = MWAIT2flg(0x00),
366 .target_residency = 1,
367 .enter = &intel_idle,
368 .enter_s2idle = intel_idle_s2idle, },
371 .desc = "MWAIT 0x58",
372 .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
374 .target_residency = 275,
375 .enter = &intel_idle,
376 .enter_s2idle = intel_idle_s2idle, },
379 .desc = "MWAIT 0x52",
380 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
382 .target_residency = 560,
383 .enter = &intel_idle,
384 .enter_s2idle = intel_idle_s2idle, },
387 .desc = "MWAIT 0x60",
388 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
389 .exit_latency = 1200,
390 .target_residency = 4000,
391 .enter = &intel_idle,
392 .enter_s2idle = intel_idle_s2idle, },
395 .desc = "MWAIT 0x64",
396 .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
397 .exit_latency = 10000,
398 .target_residency = 20000,
399 .enter = &intel_idle,
400 .enter_s2idle = intel_idle_s2idle, },
405 static struct cpuidle_state ivb_cstates[] __initdata = {
408 .desc = "MWAIT 0x00",
409 .flags = MWAIT2flg(0x00),
411 .target_residency = 1,
412 .enter = &intel_idle,
413 .enter_s2idle = intel_idle_s2idle, },
416 .desc = "MWAIT 0x01",
417 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
419 .target_residency = 20,
420 .enter = &intel_idle,
421 .enter_s2idle = intel_idle_s2idle, },
424 .desc = "MWAIT 0x10",
425 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
427 .target_residency = 156,
428 .enter = &intel_idle,
429 .enter_s2idle = intel_idle_s2idle, },
432 .desc = "MWAIT 0x20",
433 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
435 .target_residency = 300,
436 .enter = &intel_idle,
437 .enter_s2idle = intel_idle_s2idle, },
440 .desc = "MWAIT 0x30",
441 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
443 .target_residency = 300,
444 .enter = &intel_idle,
445 .enter_s2idle = intel_idle_s2idle, },
450 static struct cpuidle_state ivt_cstates[] __initdata = {
453 .desc = "MWAIT 0x00",
454 .flags = MWAIT2flg(0x00),
456 .target_residency = 1,
457 .enter = &intel_idle,
458 .enter_s2idle = intel_idle_s2idle, },
461 .desc = "MWAIT 0x01",
462 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
464 .target_residency = 80,
465 .enter = &intel_idle,
466 .enter_s2idle = intel_idle_s2idle, },
469 .desc = "MWAIT 0x10",
470 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
472 .target_residency = 156,
473 .enter = &intel_idle,
474 .enter_s2idle = intel_idle_s2idle, },
477 .desc = "MWAIT 0x20",
478 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
480 .target_residency = 300,
481 .enter = &intel_idle,
482 .enter_s2idle = intel_idle_s2idle, },
487 static struct cpuidle_state ivt_cstates_4s[] __initdata = {
490 .desc = "MWAIT 0x00",
491 .flags = MWAIT2flg(0x00),
493 .target_residency = 1,
494 .enter = &intel_idle,
495 .enter_s2idle = intel_idle_s2idle, },
498 .desc = "MWAIT 0x01",
499 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
501 .target_residency = 250,
502 .enter = &intel_idle,
503 .enter_s2idle = intel_idle_s2idle, },
506 .desc = "MWAIT 0x10",
507 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
509 .target_residency = 300,
510 .enter = &intel_idle,
511 .enter_s2idle = intel_idle_s2idle, },
514 .desc = "MWAIT 0x20",
515 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
517 .target_residency = 400,
518 .enter = &intel_idle,
519 .enter_s2idle = intel_idle_s2idle, },
524 static struct cpuidle_state ivt_cstates_8s[] __initdata = {
527 .desc = "MWAIT 0x00",
528 .flags = MWAIT2flg(0x00),
530 .target_residency = 1,
531 .enter = &intel_idle,
532 .enter_s2idle = intel_idle_s2idle, },
535 .desc = "MWAIT 0x01",
536 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
538 .target_residency = 500,
539 .enter = &intel_idle,
540 .enter_s2idle = intel_idle_s2idle, },
543 .desc = "MWAIT 0x10",
544 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
546 .target_residency = 600,
547 .enter = &intel_idle,
548 .enter_s2idle = intel_idle_s2idle, },
551 .desc = "MWAIT 0x20",
552 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
554 .target_residency = 700,
555 .enter = &intel_idle,
556 .enter_s2idle = intel_idle_s2idle, },
561 static struct cpuidle_state hsw_cstates[] __initdata = {
564 .desc = "MWAIT 0x00",
565 .flags = MWAIT2flg(0x00),
567 .target_residency = 2,
568 .enter = &intel_idle,
569 .enter_s2idle = intel_idle_s2idle, },
572 .desc = "MWAIT 0x01",
573 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
575 .target_residency = 20,
576 .enter = &intel_idle,
577 .enter_s2idle = intel_idle_s2idle, },
580 .desc = "MWAIT 0x10",
581 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
583 .target_residency = 100,
584 .enter = &intel_idle,
585 .enter_s2idle = intel_idle_s2idle, },
588 .desc = "MWAIT 0x20",
589 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
591 .target_residency = 400,
592 .enter = &intel_idle,
593 .enter_s2idle = intel_idle_s2idle, },
596 .desc = "MWAIT 0x32",
597 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
599 .target_residency = 500,
600 .enter = &intel_idle,
601 .enter_s2idle = intel_idle_s2idle, },
604 .desc = "MWAIT 0x40",
605 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
607 .target_residency = 900,
608 .enter = &intel_idle,
609 .enter_s2idle = intel_idle_s2idle, },
612 .desc = "MWAIT 0x50",
613 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
615 .target_residency = 1800,
616 .enter = &intel_idle,
617 .enter_s2idle = intel_idle_s2idle, },
620 .desc = "MWAIT 0x60",
621 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
622 .exit_latency = 2600,
623 .target_residency = 7700,
624 .enter = &intel_idle,
625 .enter_s2idle = intel_idle_s2idle, },
629 static struct cpuidle_state bdw_cstates[] __initdata = {
632 .desc = "MWAIT 0x00",
633 .flags = MWAIT2flg(0x00),
635 .target_residency = 2,
636 .enter = &intel_idle,
637 .enter_s2idle = intel_idle_s2idle, },
640 .desc = "MWAIT 0x01",
641 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
643 .target_residency = 20,
644 .enter = &intel_idle,
645 .enter_s2idle = intel_idle_s2idle, },
648 .desc = "MWAIT 0x10",
649 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
651 .target_residency = 100,
652 .enter = &intel_idle,
653 .enter_s2idle = intel_idle_s2idle, },
656 .desc = "MWAIT 0x20",
657 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
659 .target_residency = 400,
660 .enter = &intel_idle,
661 .enter_s2idle = intel_idle_s2idle, },
664 .desc = "MWAIT 0x32",
665 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
667 .target_residency = 500,
668 .enter = &intel_idle,
669 .enter_s2idle = intel_idle_s2idle, },
672 .desc = "MWAIT 0x40",
673 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
675 .target_residency = 900,
676 .enter = &intel_idle,
677 .enter_s2idle = intel_idle_s2idle, },
680 .desc = "MWAIT 0x50",
681 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
683 .target_residency = 1800,
684 .enter = &intel_idle,
685 .enter_s2idle = intel_idle_s2idle, },
688 .desc = "MWAIT 0x60",
689 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
690 .exit_latency = 2600,
691 .target_residency = 7700,
692 .enter = &intel_idle,
693 .enter_s2idle = intel_idle_s2idle, },
698 static struct cpuidle_state skl_cstates[] __initdata = {
701 .desc = "MWAIT 0x00",
702 .flags = MWAIT2flg(0x00),
704 .target_residency = 2,
705 .enter = &intel_idle,
706 .enter_s2idle = intel_idle_s2idle, },
709 .desc = "MWAIT 0x01",
710 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
712 .target_residency = 20,
713 .enter = &intel_idle,
714 .enter_s2idle = intel_idle_s2idle, },
717 .desc = "MWAIT 0x10",
718 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
720 .target_residency = 100,
721 .enter = &intel_idle,
722 .enter_s2idle = intel_idle_s2idle, },
725 .desc = "MWAIT 0x20",
726 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED | CPUIDLE_FLAG_IBRS,
728 .target_residency = 200,
729 .enter = &intel_idle,
730 .enter_s2idle = intel_idle_s2idle, },
733 .desc = "MWAIT 0x33",
734 .flags = MWAIT2flg(0x33) | CPUIDLE_FLAG_TLB_FLUSHED | CPUIDLE_FLAG_IBRS,
736 .target_residency = 800,
737 .enter = &intel_idle,
738 .enter_s2idle = intel_idle_s2idle, },
741 .desc = "MWAIT 0x40",
742 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED | CPUIDLE_FLAG_IBRS,
744 .target_residency = 800,
745 .enter = &intel_idle,
746 .enter_s2idle = intel_idle_s2idle, },
749 .desc = "MWAIT 0x50",
750 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED | CPUIDLE_FLAG_IBRS,
752 .target_residency = 5000,
753 .enter = &intel_idle,
754 .enter_s2idle = intel_idle_s2idle, },
757 .desc = "MWAIT 0x60",
758 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED | CPUIDLE_FLAG_IBRS,
760 .target_residency = 5000,
761 .enter = &intel_idle,
762 .enter_s2idle = intel_idle_s2idle, },
767 static struct cpuidle_state skx_cstates[] __initdata = {
770 .desc = "MWAIT 0x00",
771 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_IRQ_ENABLE,
773 .target_residency = 2,
774 .enter = &intel_idle,
775 .enter_s2idle = intel_idle_s2idle, },
778 .desc = "MWAIT 0x01",
779 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
781 .target_residency = 20,
782 .enter = &intel_idle,
783 .enter_s2idle = intel_idle_s2idle, },
786 .desc = "MWAIT 0x20",
787 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED | CPUIDLE_FLAG_IBRS,
789 .target_residency = 600,
790 .enter = &intel_idle,
791 .enter_s2idle = intel_idle_s2idle, },
796 static struct cpuidle_state icx_cstates[] __initdata = {
799 .desc = "MWAIT 0x00",
800 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_IRQ_ENABLE,
802 .target_residency = 1,
803 .enter = &intel_idle,
804 .enter_s2idle = intel_idle_s2idle, },
807 .desc = "MWAIT 0x01",
808 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
810 .target_residency = 4,
811 .enter = &intel_idle,
812 .enter_s2idle = intel_idle_s2idle, },
815 .desc = "MWAIT 0x20",
816 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
818 .target_residency = 600,
819 .enter = &intel_idle,
820 .enter_s2idle = intel_idle_s2idle, },
826 * On AlderLake C1 has to be disabled if C1E is enabled, and vice versa.
827 * C1E is enabled only if "C1E promotion" bit is set in MSR_IA32_POWER_CTL.
828 * But in this case there is effectively no C1, because C1 requests are
829 * promoted to C1E. If the "C1E promotion" bit is cleared, then both C1
830 * and C1E requests end up with C1, so there is effectively no C1E.
832 * By default we enable C1E and disable C1 by marking it with
833 * 'CPUIDLE_FLAG_UNUSABLE'.
835 static struct cpuidle_state adl_cstates[] __initdata = {
838 .desc = "MWAIT 0x00",
839 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_UNUSABLE,
841 .target_residency = 1,
842 .enter = &intel_idle,
843 .enter_s2idle = intel_idle_s2idle, },
846 .desc = "MWAIT 0x01",
847 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
849 .target_residency = 4,
850 .enter = &intel_idle,
851 .enter_s2idle = intel_idle_s2idle, },
854 .desc = "MWAIT 0x20",
855 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
857 .target_residency = 600,
858 .enter = &intel_idle,
859 .enter_s2idle = intel_idle_s2idle, },
862 .desc = "MWAIT 0x40",
863 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
865 .target_residency = 800,
866 .enter = &intel_idle,
867 .enter_s2idle = intel_idle_s2idle, },
870 .desc = "MWAIT 0x60",
871 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
873 .target_residency = 2000,
874 .enter = &intel_idle,
875 .enter_s2idle = intel_idle_s2idle, },
880 static struct cpuidle_state adl_l_cstates[] __initdata = {
883 .desc = "MWAIT 0x00",
884 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_UNUSABLE,
886 .target_residency = 1,
887 .enter = &intel_idle,
888 .enter_s2idle = intel_idle_s2idle, },
891 .desc = "MWAIT 0x01",
892 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
894 .target_residency = 4,
895 .enter = &intel_idle,
896 .enter_s2idle = intel_idle_s2idle, },
899 .desc = "MWAIT 0x20",
900 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
902 .target_residency = 500,
903 .enter = &intel_idle,
904 .enter_s2idle = intel_idle_s2idle, },
907 .desc = "MWAIT 0x40",
908 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
910 .target_residency = 600,
911 .enter = &intel_idle,
912 .enter_s2idle = intel_idle_s2idle, },
915 .desc = "MWAIT 0x60",
916 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
918 .target_residency = 700,
919 .enter = &intel_idle,
920 .enter_s2idle = intel_idle_s2idle, },
925 static struct cpuidle_state adl_n_cstates[] __initdata = {
928 .desc = "MWAIT 0x00",
929 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_UNUSABLE,
931 .target_residency = 1,
932 .enter = &intel_idle,
933 .enter_s2idle = intel_idle_s2idle, },
936 .desc = "MWAIT 0x01",
937 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
939 .target_residency = 4,
940 .enter = &intel_idle,
941 .enter_s2idle = intel_idle_s2idle, },
944 .desc = "MWAIT 0x20",
945 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
947 .target_residency = 585,
948 .enter = &intel_idle,
949 .enter_s2idle = intel_idle_s2idle, },
952 .desc = "MWAIT 0x40",
953 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
955 .target_residency = 1040,
956 .enter = &intel_idle,
957 .enter_s2idle = intel_idle_s2idle, },
960 .desc = "MWAIT 0x60",
961 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
963 .target_residency = 1980,
964 .enter = &intel_idle,
965 .enter_s2idle = intel_idle_s2idle, },
970 static struct cpuidle_state spr_cstates[] __initdata = {
973 .desc = "MWAIT 0x00",
974 .flags = MWAIT2flg(0x00),
976 .target_residency = 1,
977 .enter = &intel_idle,
978 .enter_s2idle = intel_idle_s2idle, },
981 .desc = "MWAIT 0x01",
982 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
984 .target_residency = 4,
985 .enter = &intel_idle,
986 .enter_s2idle = intel_idle_s2idle, },
989 .desc = "MWAIT 0x20",
990 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED |
991 CPUIDLE_FLAG_INIT_XSTATE,
993 .target_residency = 800,
994 .enter = &intel_idle,
995 .enter_s2idle = intel_idle_s2idle, },
1000 static struct cpuidle_state atom_cstates[] __initdata = {
1003 .desc = "MWAIT 0x00",
1004 .flags = MWAIT2flg(0x00),
1006 .target_residency = 20,
1007 .enter = &intel_idle,
1008 .enter_s2idle = intel_idle_s2idle, },
1011 .desc = "MWAIT 0x10",
1012 .flags = MWAIT2flg(0x10),
1014 .target_residency = 80,
1015 .enter = &intel_idle,
1016 .enter_s2idle = intel_idle_s2idle, },
1019 .desc = "MWAIT 0x30",
1020 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
1021 .exit_latency = 100,
1022 .target_residency = 400,
1023 .enter = &intel_idle,
1024 .enter_s2idle = intel_idle_s2idle, },
1027 .desc = "MWAIT 0x52",
1028 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
1029 .exit_latency = 140,
1030 .target_residency = 560,
1031 .enter = &intel_idle,
1032 .enter_s2idle = intel_idle_s2idle, },
1036 static struct cpuidle_state tangier_cstates[] __initdata = {
1039 .desc = "MWAIT 0x00",
1040 .flags = MWAIT2flg(0x00),
1042 .target_residency = 4,
1043 .enter = &intel_idle,
1044 .enter_s2idle = intel_idle_s2idle, },
1047 .desc = "MWAIT 0x30",
1048 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
1049 .exit_latency = 100,
1050 .target_residency = 400,
1051 .enter = &intel_idle,
1052 .enter_s2idle = intel_idle_s2idle, },
1055 .desc = "MWAIT 0x52",
1056 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
1057 .exit_latency = 140,
1058 .target_residency = 560,
1059 .enter = &intel_idle,
1060 .enter_s2idle = intel_idle_s2idle, },
1063 .desc = "MWAIT 0x60",
1064 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
1065 .exit_latency = 1200,
1066 .target_residency = 4000,
1067 .enter = &intel_idle,
1068 .enter_s2idle = intel_idle_s2idle, },
1071 .desc = "MWAIT 0x64",
1072 .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
1073 .exit_latency = 10000,
1074 .target_residency = 20000,
1075 .enter = &intel_idle,
1076 .enter_s2idle = intel_idle_s2idle, },
1080 static struct cpuidle_state avn_cstates[] __initdata = {
1083 .desc = "MWAIT 0x00",
1084 .flags = MWAIT2flg(0x00),
1086 .target_residency = 2,
1087 .enter = &intel_idle,
1088 .enter_s2idle = intel_idle_s2idle, },
1091 .desc = "MWAIT 0x51",
1092 .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TLB_FLUSHED,
1094 .target_residency = 45,
1095 .enter = &intel_idle,
1096 .enter_s2idle = intel_idle_s2idle, },
1100 static struct cpuidle_state knl_cstates[] __initdata = {
1103 .desc = "MWAIT 0x00",
1104 .flags = MWAIT2flg(0x00),
1106 .target_residency = 2,
1107 .enter = &intel_idle,
1108 .enter_s2idle = intel_idle_s2idle },
1111 .desc = "MWAIT 0x10",
1112 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
1113 .exit_latency = 120,
1114 .target_residency = 500,
1115 .enter = &intel_idle,
1116 .enter_s2idle = intel_idle_s2idle },
1121 static struct cpuidle_state bxt_cstates[] __initdata = {
1124 .desc = "MWAIT 0x00",
1125 .flags = MWAIT2flg(0x00),
1127 .target_residency = 2,
1128 .enter = &intel_idle,
1129 .enter_s2idle = intel_idle_s2idle, },
1132 .desc = "MWAIT 0x01",
1133 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
1135 .target_residency = 20,
1136 .enter = &intel_idle,
1137 .enter_s2idle = intel_idle_s2idle, },
1140 .desc = "MWAIT 0x20",
1141 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
1142 .exit_latency = 133,
1143 .target_residency = 133,
1144 .enter = &intel_idle,
1145 .enter_s2idle = intel_idle_s2idle, },
1148 .desc = "MWAIT 0x31",
1149 .flags = MWAIT2flg(0x31) | CPUIDLE_FLAG_TLB_FLUSHED,
1150 .exit_latency = 155,
1151 .target_residency = 155,
1152 .enter = &intel_idle,
1153 .enter_s2idle = intel_idle_s2idle, },
1156 .desc = "MWAIT 0x40",
1157 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
1158 .exit_latency = 1000,
1159 .target_residency = 1000,
1160 .enter = &intel_idle,
1161 .enter_s2idle = intel_idle_s2idle, },
1164 .desc = "MWAIT 0x50",
1165 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
1166 .exit_latency = 2000,
1167 .target_residency = 2000,
1168 .enter = &intel_idle,
1169 .enter_s2idle = intel_idle_s2idle, },
1172 .desc = "MWAIT 0x60",
1173 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
1174 .exit_latency = 10000,
1175 .target_residency = 10000,
1176 .enter = &intel_idle,
1177 .enter_s2idle = intel_idle_s2idle, },
1182 static struct cpuidle_state dnv_cstates[] __initdata = {
1185 .desc = "MWAIT 0x00",
1186 .flags = MWAIT2flg(0x00),
1188 .target_residency = 2,
1189 .enter = &intel_idle,
1190 .enter_s2idle = intel_idle_s2idle, },
1193 .desc = "MWAIT 0x01",
1194 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
1196 .target_residency = 20,
1197 .enter = &intel_idle,
1198 .enter_s2idle = intel_idle_s2idle, },
1201 .desc = "MWAIT 0x20",
1202 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
1204 .target_residency = 500,
1205 .enter = &intel_idle,
1206 .enter_s2idle = intel_idle_s2idle, },
1212 * Note, depending on HW and FW revision, SnowRidge SoC may or may not support
1213 * C6, and this is indicated in the CPUID mwait leaf.
1215 static struct cpuidle_state snr_cstates[] __initdata = {
1218 .desc = "MWAIT 0x00",
1219 .flags = MWAIT2flg(0x00),
1221 .target_residency = 2,
1222 .enter = &intel_idle,
1223 .enter_s2idle = intel_idle_s2idle, },
1226 .desc = "MWAIT 0x01",
1227 .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_ALWAYS_ENABLE,
1229 .target_residency = 25,
1230 .enter = &intel_idle,
1231 .enter_s2idle = intel_idle_s2idle, },
1234 .desc = "MWAIT 0x20",
1235 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
1236 .exit_latency = 130,
1237 .target_residency = 500,
1238 .enter = &intel_idle,
1239 .enter_s2idle = intel_idle_s2idle, },
1244 static const struct idle_cpu idle_cpu_nehalem __initconst = {
1245 .state_table = nehalem_cstates,
1246 .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
1247 .disable_promotion_to_c1e = true,
1250 static const struct idle_cpu idle_cpu_nhx __initconst = {
1251 .state_table = nehalem_cstates,
1252 .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
1253 .disable_promotion_to_c1e = true,
1257 static const struct idle_cpu idle_cpu_atom __initconst = {
1258 .state_table = atom_cstates,
1261 static const struct idle_cpu idle_cpu_tangier __initconst = {
1262 .state_table = tangier_cstates,
1265 static const struct idle_cpu idle_cpu_lincroft __initconst = {
1266 .state_table = atom_cstates,
1267 .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
1270 static const struct idle_cpu idle_cpu_snb __initconst = {
1271 .state_table = snb_cstates,
1272 .disable_promotion_to_c1e = true,
1275 static const struct idle_cpu idle_cpu_snx __initconst = {
1276 .state_table = snb_cstates,
1277 .disable_promotion_to_c1e = true,
1281 static const struct idle_cpu idle_cpu_byt __initconst = {
1282 .state_table = byt_cstates,
1283 .disable_promotion_to_c1e = true,
1284 .byt_auto_demotion_disable_flag = true,
1287 static const struct idle_cpu idle_cpu_cht __initconst = {
1288 .state_table = cht_cstates,
1289 .disable_promotion_to_c1e = true,
1290 .byt_auto_demotion_disable_flag = true,
1293 static const struct idle_cpu idle_cpu_ivb __initconst = {
1294 .state_table = ivb_cstates,
1295 .disable_promotion_to_c1e = true,
1298 static const struct idle_cpu idle_cpu_ivt __initconst = {
1299 .state_table = ivt_cstates,
1300 .disable_promotion_to_c1e = true,
1304 static const struct idle_cpu idle_cpu_hsw __initconst = {
1305 .state_table = hsw_cstates,
1306 .disable_promotion_to_c1e = true,
1309 static const struct idle_cpu idle_cpu_hsx __initconst = {
1310 .state_table = hsw_cstates,
1311 .disable_promotion_to_c1e = true,
1315 static const struct idle_cpu idle_cpu_bdw __initconst = {
1316 .state_table = bdw_cstates,
1317 .disable_promotion_to_c1e = true,
1320 static const struct idle_cpu idle_cpu_bdx __initconst = {
1321 .state_table = bdw_cstates,
1322 .disable_promotion_to_c1e = true,
1326 static const struct idle_cpu idle_cpu_skl __initconst = {
1327 .state_table = skl_cstates,
1328 .disable_promotion_to_c1e = true,
1331 static const struct idle_cpu idle_cpu_skx __initconst = {
1332 .state_table = skx_cstates,
1333 .disable_promotion_to_c1e = true,
1337 static const struct idle_cpu idle_cpu_icx __initconst = {
1338 .state_table = icx_cstates,
1339 .disable_promotion_to_c1e = true,
1343 static const struct idle_cpu idle_cpu_adl __initconst = {
1344 .state_table = adl_cstates,
1347 static const struct idle_cpu idle_cpu_adl_l __initconst = {
1348 .state_table = adl_l_cstates,
1351 static const struct idle_cpu idle_cpu_adl_n __initconst = {
1352 .state_table = adl_n_cstates,
1355 static const struct idle_cpu idle_cpu_spr __initconst = {
1356 .state_table = spr_cstates,
1357 .disable_promotion_to_c1e = true,
1361 static const struct idle_cpu idle_cpu_avn __initconst = {
1362 .state_table = avn_cstates,
1363 .disable_promotion_to_c1e = true,
1367 static const struct idle_cpu idle_cpu_knl __initconst = {
1368 .state_table = knl_cstates,
1372 static const struct idle_cpu idle_cpu_bxt __initconst = {
1373 .state_table = bxt_cstates,
1374 .disable_promotion_to_c1e = true,
1377 static const struct idle_cpu idle_cpu_dnv __initconst = {
1378 .state_table = dnv_cstates,
1379 .disable_promotion_to_c1e = true,
1383 static const struct idle_cpu idle_cpu_snr __initconst = {
1384 .state_table = snr_cstates,
1385 .disable_promotion_to_c1e = true,
1389 static const struct x86_cpu_id intel_idle_ids[] __initconst = {
1390 X86_MATCH_INTEL_FAM6_MODEL(NEHALEM_EP, &idle_cpu_nhx),
1391 X86_MATCH_INTEL_FAM6_MODEL(NEHALEM, &idle_cpu_nehalem),
1392 X86_MATCH_INTEL_FAM6_MODEL(NEHALEM_G, &idle_cpu_nehalem),
1393 X86_MATCH_INTEL_FAM6_MODEL(WESTMERE, &idle_cpu_nehalem),
1394 X86_MATCH_INTEL_FAM6_MODEL(WESTMERE_EP, &idle_cpu_nhx),
1395 X86_MATCH_INTEL_FAM6_MODEL(NEHALEM_EX, &idle_cpu_nhx),
1396 X86_MATCH_INTEL_FAM6_MODEL(ATOM_BONNELL, &idle_cpu_atom),
1397 X86_MATCH_INTEL_FAM6_MODEL(ATOM_BONNELL_MID, &idle_cpu_lincroft),
1398 X86_MATCH_INTEL_FAM6_MODEL(WESTMERE_EX, &idle_cpu_nhx),
1399 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE, &idle_cpu_snb),
1400 X86_MATCH_INTEL_FAM6_MODEL(SANDYBRIDGE_X, &idle_cpu_snx),
1401 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SALTWELL, &idle_cpu_atom),
1402 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, &idle_cpu_byt),
1403 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_MID, &idle_cpu_tangier),
1404 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, &idle_cpu_cht),
1405 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE, &idle_cpu_ivb),
1406 X86_MATCH_INTEL_FAM6_MODEL(IVYBRIDGE_X, &idle_cpu_ivt),
1407 X86_MATCH_INTEL_FAM6_MODEL(HASWELL, &idle_cpu_hsw),
1408 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, &idle_cpu_hsx),
1409 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_L, &idle_cpu_hsw),
1410 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_G, &idle_cpu_hsw),
1411 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT_D, &idle_cpu_avn),
1412 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, &idle_cpu_bdw),
1413 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, &idle_cpu_bdw),
1414 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, &idle_cpu_bdx),
1415 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, &idle_cpu_bdx),
1416 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_L, &idle_cpu_skl),
1417 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE, &idle_cpu_skl),
1418 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE_L, &idle_cpu_skl),
1419 X86_MATCH_INTEL_FAM6_MODEL(KABYLAKE, &idle_cpu_skl),
1420 X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, &idle_cpu_skx),
1421 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, &idle_cpu_icx),
1422 X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, &idle_cpu_icx),
1423 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE, &idle_cpu_adl),
1424 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, &idle_cpu_adl_l),
1425 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_N, &idle_cpu_adl_n),
1426 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, &idle_cpu_spr),
1427 X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, &idle_cpu_spr),
1428 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNL, &idle_cpu_knl),
1429 X86_MATCH_INTEL_FAM6_MODEL(XEON_PHI_KNM, &idle_cpu_knl),
1430 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, &idle_cpu_bxt),
1431 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_PLUS, &idle_cpu_bxt),
1432 X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT_D, &idle_cpu_dnv),
1433 X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, &idle_cpu_snr),
1437 static const struct x86_cpu_id intel_mwait_ids[] __initconst = {
1438 X86_MATCH_VENDOR_FAM_FEATURE(INTEL, 6, X86_FEATURE_MWAIT, NULL),
1442 static bool __init intel_idle_max_cstate_reached(int cstate)
1444 if (cstate + 1 > max_cstate) {
1445 pr_info("max_cstate %d reached\n", max_cstate);
1451 static bool __init intel_idle_state_needs_timer_stop(struct cpuidle_state *state)
1453 unsigned long eax = flg2MWAIT(state->flags);
1455 if (boot_cpu_has(X86_FEATURE_ARAT))
1459 * Switch over to one-shot tick broadcast if the target C-state
1460 * is deeper than C1.
1462 return !!((eax >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK);
1465 #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
1466 #include <acpi/processor.h>
1468 static bool no_acpi __read_mostly;
1469 module_param(no_acpi, bool, 0444);
1470 MODULE_PARM_DESC(no_acpi, "Do not use ACPI _CST for building the idle states list");
1472 static bool force_use_acpi __read_mostly; /* No effect if no_acpi is set. */
1473 module_param_named(use_acpi, force_use_acpi, bool, 0444);
1474 MODULE_PARM_DESC(use_acpi, "Use ACPI _CST for building the idle states list");
1476 static struct acpi_processor_power acpi_state_table __initdata;
1479 * intel_idle_cst_usable - Check if the _CST information can be used.
1481 * Check if all of the C-states listed by _CST in the max_cstate range are
1482 * ACPI_CSTATE_FFH, which means that they should be entered via MWAIT.
1484 static bool __init intel_idle_cst_usable(void)
1488 limit = min_t(int, min_t(int, CPUIDLE_STATE_MAX, max_cstate + 1),
1489 acpi_state_table.count);
1491 for (cstate = 1; cstate < limit; cstate++) {
1492 struct acpi_processor_cx *cx = &acpi_state_table.states[cstate];
1494 if (cx->entry_method != ACPI_CSTATE_FFH)
1501 static bool __init intel_idle_acpi_cst_extract(void)
1506 pr_debug("Not allowed to use ACPI _CST\n");
1510 for_each_possible_cpu(cpu) {
1511 struct acpi_processor *pr = per_cpu(processors, cpu);
1516 if (acpi_processor_evaluate_cst(pr->handle, cpu, &acpi_state_table))
1519 acpi_state_table.count++;
1521 if (!intel_idle_cst_usable())
1524 if (!acpi_processor_claim_cst_control())
1530 acpi_state_table.count = 0;
1531 pr_debug("ACPI _CST not found or not usable\n");
1535 static void __init intel_idle_init_cstates_acpi(struct cpuidle_driver *drv)
1537 int cstate, limit = min_t(int, CPUIDLE_STATE_MAX, acpi_state_table.count);
1540 * If limit > 0, intel_idle_cst_usable() has returned 'true', so all of
1541 * the interesting states are ACPI_CSTATE_FFH.
1543 for (cstate = 1; cstate < limit; cstate++) {
1544 struct acpi_processor_cx *cx;
1545 struct cpuidle_state *state;
1547 if (intel_idle_max_cstate_reached(cstate - 1))
1550 cx = &acpi_state_table.states[cstate];
1552 state = &drv->states[drv->state_count++];
1554 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d_ACPI", cstate);
1555 strscpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
1556 state->exit_latency = cx->latency;
1558 * For C1-type C-states use the same number for both the exit
1559 * latency and target residency, because that is the case for
1560 * C1 in the majority of the static C-states tables above.
1561 * For the other types of C-states, however, set the target
1562 * residency to 3 times the exit latency which should lead to
1563 * a reasonable balance between energy-efficiency and
1564 * performance in the majority of interesting cases.
1566 state->target_residency = cx->latency;
1567 if (cx->type > ACPI_STATE_C1)
1568 state->target_residency *= 3;
1570 state->flags = MWAIT2flg(cx->address);
1571 if (cx->type > ACPI_STATE_C2)
1572 state->flags |= CPUIDLE_FLAG_TLB_FLUSHED;
1574 if (disabled_states_mask & BIT(cstate))
1575 state->flags |= CPUIDLE_FLAG_OFF;
1577 if (intel_idle_state_needs_timer_stop(state))
1578 state->flags |= CPUIDLE_FLAG_TIMER_STOP;
1580 state->enter = intel_idle;
1581 state->enter_s2idle = intel_idle_s2idle;
1585 static bool __init intel_idle_off_by_default(u32 mwait_hint)
1590 * If there are no _CST C-states, do not disable any C-states by
1593 if (!acpi_state_table.count)
1596 limit = min_t(int, CPUIDLE_STATE_MAX, acpi_state_table.count);
1598 * If limit > 0, intel_idle_cst_usable() has returned 'true', so all of
1599 * the interesting states are ACPI_CSTATE_FFH.
1601 for (cstate = 1; cstate < limit; cstate++) {
1602 if (acpi_state_table.states[cstate].address == mwait_hint)
1607 #else /* !CONFIG_ACPI_PROCESSOR_CSTATE */
1608 #define force_use_acpi (false)
1610 static inline bool intel_idle_acpi_cst_extract(void) { return false; }
1611 static inline void intel_idle_init_cstates_acpi(struct cpuidle_driver *drv) { }
1612 static inline bool intel_idle_off_by_default(u32 mwait_hint) { return false; }
1613 #endif /* !CONFIG_ACPI_PROCESSOR_CSTATE */
1616 * ivt_idle_state_table_update - Tune the idle states table for Ivy Town.
1618 * Tune IVT multi-socket targets.
1619 * Assumption: num_sockets == (max_package_num + 1).
1621 static void __init ivt_idle_state_table_update(void)
1623 /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
1624 int cpu, package_num, num_sockets = 1;
1626 for_each_online_cpu(cpu) {
1627 package_num = topology_physical_package_id(cpu);
1628 if (package_num + 1 > num_sockets) {
1629 num_sockets = package_num + 1;
1631 if (num_sockets > 4) {
1632 cpuidle_state_table = ivt_cstates_8s;
1638 if (num_sockets > 2)
1639 cpuidle_state_table = ivt_cstates_4s;
1641 /* else, 1 and 2 socket systems use default ivt_cstates */
1645 * irtl_2_usec - IRTL to microseconds conversion.
1646 * @irtl: IRTL MSR value.
1648 * Translate the IRTL (Interrupt Response Time Limit) MSR value to microseconds.
1650 static unsigned long long __init irtl_2_usec(unsigned long long irtl)
1652 static const unsigned int irtl_ns_units[] __initconst = {
1653 1, 32, 1024, 32768, 1048576, 33554432, 0, 0
1655 unsigned long long ns;
1660 ns = irtl_ns_units[(irtl >> 10) & 0x7];
1662 return div_u64((irtl & 0x3FF) * ns, NSEC_PER_USEC);
1666 * bxt_idle_state_table_update - Fix up the Broxton idle states table.
1668 * On BXT, trust the IRTL (Interrupt Response Time Limit) MSR to show the
1669 * definitive maximum latency and use the same value for target_residency.
1671 static void __init bxt_idle_state_table_update(void)
1673 unsigned long long msr;
1676 rdmsrl(MSR_PKGC6_IRTL, msr);
1677 usec = irtl_2_usec(msr);
1679 bxt_cstates[2].exit_latency = usec;
1680 bxt_cstates[2].target_residency = usec;
1683 rdmsrl(MSR_PKGC7_IRTL, msr);
1684 usec = irtl_2_usec(msr);
1686 bxt_cstates[3].exit_latency = usec;
1687 bxt_cstates[3].target_residency = usec;
1690 rdmsrl(MSR_PKGC8_IRTL, msr);
1691 usec = irtl_2_usec(msr);
1693 bxt_cstates[4].exit_latency = usec;
1694 bxt_cstates[4].target_residency = usec;
1697 rdmsrl(MSR_PKGC9_IRTL, msr);
1698 usec = irtl_2_usec(msr);
1700 bxt_cstates[5].exit_latency = usec;
1701 bxt_cstates[5].target_residency = usec;
1704 rdmsrl(MSR_PKGC10_IRTL, msr);
1705 usec = irtl_2_usec(msr);
1707 bxt_cstates[6].exit_latency = usec;
1708 bxt_cstates[6].target_residency = usec;
1714 * sklh_idle_state_table_update - Fix up the Sky Lake idle states table.
1716 * On SKL-H (model 0x5e) skip C8 and C9 if C10 is enabled and SGX disabled.
1718 static void __init sklh_idle_state_table_update(void)
1720 unsigned long long msr;
1721 unsigned int eax, ebx, ecx, edx;
1724 /* if PC10 disabled via cmdline intel_idle.max_cstate=7 or shallower */
1725 if (max_cstate <= 7)
1728 /* if PC10 not present in CPUID.MWAIT.EDX */
1729 if ((mwait_substates & (0xF << 28)) == 0)
1732 rdmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr);
1734 /* PC10 is not enabled in PKG C-state limit */
1735 if ((msr & 0xF) != 8)
1739 cpuid(7, &eax, &ebx, &ecx, &edx);
1741 /* if SGX is present */
1742 if (ebx & (1 << 2)) {
1744 rdmsrl(MSR_IA32_FEAT_CTL, msr);
1746 /* if SGX is enabled */
1747 if (msr & (1 << 18))
1751 skl_cstates[5].flags |= CPUIDLE_FLAG_UNUSABLE; /* C8-SKL */
1752 skl_cstates[6].flags |= CPUIDLE_FLAG_UNUSABLE; /* C9-SKL */
1756 * skx_idle_state_table_update - Adjust the Sky Lake/Cascade Lake
1757 * idle states table.
1759 static void __init skx_idle_state_table_update(void)
1761 unsigned long long msr;
1763 rdmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr);
1766 * 000b: C0/C1 (no package C-state support)
1768 * 010b: C6 (non-retention)
1769 * 011b: C6 (retention)
1770 * 111b: No Package C state limits.
1772 if ((msr & 0x7) < 2) {
1774 * Uses the CC6 + PC0 latency and 3 times of
1775 * latency for target_residency if the PC6
1776 * is disabled in BIOS. This is consistent
1777 * with how intel_idle driver uses _CST
1778 * to set the target_residency.
1780 skx_cstates[2].exit_latency = 92;
1781 skx_cstates[2].target_residency = 276;
1786 * adl_idle_state_table_update - Adjust AlderLake idle states table.
1788 static void __init adl_idle_state_table_update(void)
1790 /* Check if user prefers C1 over C1E. */
1791 if (preferred_states_mask & BIT(1) && !(preferred_states_mask & BIT(2))) {
1792 cpuidle_state_table[0].flags &= ~CPUIDLE_FLAG_UNUSABLE;
1793 cpuidle_state_table[1].flags |= CPUIDLE_FLAG_UNUSABLE;
1795 /* Disable C1E by clearing the "C1E promotion" bit. */
1796 c1e_promotion = C1E_PROMOTION_DISABLE;
1800 /* Make sure C1E is enabled by default */
1801 c1e_promotion = C1E_PROMOTION_ENABLE;
1805 * spr_idle_state_table_update - Adjust Sapphire Rapids idle states table.
1807 static void __init spr_idle_state_table_update(void)
1809 unsigned long long msr;
1812 * By default, the C6 state assumes the worst-case scenario of package
1813 * C6. However, if PC6 is disabled, we update the numbers to match
1816 rdmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr);
1818 /* Limit value 2 and above allow for PC6. */
1819 if ((msr & 0x7) < 2) {
1820 spr_cstates[2].exit_latency = 190;
1821 spr_cstates[2].target_residency = 600;
1825 static bool __init intel_idle_verify_cstate(unsigned int mwait_hint)
1827 unsigned int mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint) + 1;
1828 unsigned int num_substates = (mwait_substates >> mwait_cstate * 4) &
1829 MWAIT_SUBSTATE_MASK;
1831 /* Ignore the C-state if there are NO sub-states in CPUID for it. */
1832 if (num_substates == 0)
1835 if (mwait_cstate > 2 && !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
1836 mark_tsc_unstable("TSC halts in idle states deeper than C2");
1841 static bool force_irq_on __read_mostly;
1842 module_param(force_irq_on, bool, 0444);
1844 static void __init intel_idle_init_cstates_icpu(struct cpuidle_driver *drv)
1848 switch (boot_cpu_data.x86_model) {
1849 case INTEL_FAM6_IVYBRIDGE_X:
1850 ivt_idle_state_table_update();
1852 case INTEL_FAM6_ATOM_GOLDMONT:
1853 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
1854 bxt_idle_state_table_update();
1856 case INTEL_FAM6_SKYLAKE:
1857 sklh_idle_state_table_update();
1859 case INTEL_FAM6_SKYLAKE_X:
1860 skx_idle_state_table_update();
1862 case INTEL_FAM6_SAPPHIRERAPIDS_X:
1863 case INTEL_FAM6_EMERALDRAPIDS_X:
1864 spr_idle_state_table_update();
1866 case INTEL_FAM6_ALDERLAKE:
1867 case INTEL_FAM6_ALDERLAKE_L:
1868 case INTEL_FAM6_ALDERLAKE_N:
1869 adl_idle_state_table_update();
1873 for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
1874 unsigned int mwait_hint;
1876 if (intel_idle_max_cstate_reached(cstate))
1879 if (!cpuidle_state_table[cstate].enter &&
1880 !cpuidle_state_table[cstate].enter_s2idle)
1883 /* If marked as unusable, skip this state. */
1884 if (cpuidle_state_table[cstate].flags & CPUIDLE_FLAG_UNUSABLE) {
1885 pr_debug("state %s is disabled\n",
1886 cpuidle_state_table[cstate].name);
1890 mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
1891 if (!intel_idle_verify_cstate(mwait_hint))
1894 /* Structure copy. */
1895 drv->states[drv->state_count] = cpuidle_state_table[cstate];
1897 if ((cpuidle_state_table[cstate].flags & CPUIDLE_FLAG_IRQ_ENABLE) || force_irq_on) {
1898 printk("intel_idle: forced intel_idle_irq for state %d\n", cstate);
1899 drv->states[drv->state_count].enter = intel_idle_irq;
1902 if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS) &&
1903 cpuidle_state_table[cstate].flags & CPUIDLE_FLAG_IBRS) {
1904 WARN_ON_ONCE(cpuidle_state_table[cstate].flags & CPUIDLE_FLAG_IRQ_ENABLE);
1905 drv->states[drv->state_count].enter = intel_idle_ibrs;
1908 if (cpuidle_state_table[cstate].flags & CPUIDLE_FLAG_INIT_XSTATE)
1909 drv->states[drv->state_count].enter = intel_idle_xstate;
1911 if ((disabled_states_mask & BIT(drv->state_count)) ||
1912 ((icpu->use_acpi || force_use_acpi) &&
1913 intel_idle_off_by_default(mwait_hint) &&
1914 !(cpuidle_state_table[cstate].flags & CPUIDLE_FLAG_ALWAYS_ENABLE)))
1915 drv->states[drv->state_count].flags |= CPUIDLE_FLAG_OFF;
1917 if (intel_idle_state_needs_timer_stop(&drv->states[drv->state_count]))
1918 drv->states[drv->state_count].flags |= CPUIDLE_FLAG_TIMER_STOP;
1923 if (icpu->byt_auto_demotion_disable_flag) {
1924 wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
1925 wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
1930 * intel_idle_cpuidle_driver_init - Create the list of available idle states.
1931 * @drv: cpuidle driver structure to initialize.
1933 static void __init intel_idle_cpuidle_driver_init(struct cpuidle_driver *drv)
1935 cpuidle_poll_state_init(drv);
1937 if (disabled_states_mask & BIT(0))
1938 drv->states[0].flags |= CPUIDLE_FLAG_OFF;
1940 drv->state_count = 1;
1943 intel_idle_init_cstates_icpu(drv);
1945 intel_idle_init_cstates_acpi(drv);
1948 static void auto_demotion_disable(void)
1950 unsigned long long msr_bits;
1952 rdmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr_bits);
1953 msr_bits &= ~auto_demotion_disable_flags;
1954 wrmsrl(MSR_PKG_CST_CONFIG_CONTROL, msr_bits);
1957 static void c1e_promotion_enable(void)
1959 unsigned long long msr_bits;
1961 rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
1963 wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
1966 static void c1e_promotion_disable(void)
1968 unsigned long long msr_bits;
1970 rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
1972 wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
1976 * intel_idle_cpu_init - Register the target CPU with the cpuidle core.
1977 * @cpu: CPU to initialize.
1979 * Register a cpuidle device object for @cpu and update its MSRs in accordance
1980 * with the processor model flags.
1982 static int intel_idle_cpu_init(unsigned int cpu)
1984 struct cpuidle_device *dev;
1986 dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
1989 if (cpuidle_register_device(dev)) {
1990 pr_debug("cpuidle_register_device %d failed!\n", cpu);
1994 if (auto_demotion_disable_flags)
1995 auto_demotion_disable();
1997 if (c1e_promotion == C1E_PROMOTION_ENABLE)
1998 c1e_promotion_enable();
1999 else if (c1e_promotion == C1E_PROMOTION_DISABLE)
2000 c1e_promotion_disable();
2005 static int intel_idle_cpu_online(unsigned int cpu)
2007 struct cpuidle_device *dev;
2009 if (!boot_cpu_has(X86_FEATURE_ARAT))
2010 tick_broadcast_enable();
2013 * Some systems can hotplug a cpu at runtime after
2014 * the kernel has booted, we have to initialize the
2015 * driver in this case
2017 dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
2018 if (!dev->registered)
2019 return intel_idle_cpu_init(cpu);
2025 * intel_idle_cpuidle_devices_uninit - Unregister all cpuidle devices.
2027 static void __init intel_idle_cpuidle_devices_uninit(void)
2031 for_each_online_cpu(i)
2032 cpuidle_unregister_device(per_cpu_ptr(intel_idle_cpuidle_devices, i));
2035 static int __init intel_idle_init(void)
2037 const struct x86_cpu_id *id;
2038 unsigned int eax, ebx, ecx;
2041 /* Do not load intel_idle at all for now if idle= is passed */
2042 if (boot_option_idle_override != IDLE_NO_OVERRIDE)
2045 if (max_cstate == 0) {
2046 pr_debug("disabled\n");
2050 id = x86_match_cpu(intel_idle_ids);
2052 if (!boot_cpu_has(X86_FEATURE_MWAIT)) {
2053 pr_debug("Please enable MWAIT in BIOS SETUP\n");
2057 id = x86_match_cpu(intel_mwait_ids);
2062 if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
2065 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
2067 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
2068 !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
2072 pr_debug("MWAIT substates: 0x%x\n", mwait_substates);
2074 icpu = (const struct idle_cpu *)id->driver_data;
2076 cpuidle_state_table = icpu->state_table;
2077 auto_demotion_disable_flags = icpu->auto_demotion_disable_flags;
2078 if (icpu->disable_promotion_to_c1e)
2079 c1e_promotion = C1E_PROMOTION_DISABLE;
2080 if (icpu->use_acpi || force_use_acpi)
2081 intel_idle_acpi_cst_extract();
2082 } else if (!intel_idle_acpi_cst_extract()) {
2086 pr_debug("v" INTEL_IDLE_VERSION " model 0x%X\n",
2087 boot_cpu_data.x86_model);
2089 intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
2090 if (!intel_idle_cpuidle_devices)
2093 intel_idle_cpuidle_driver_init(&intel_idle_driver);
2095 retval = cpuidle_register_driver(&intel_idle_driver);
2097 struct cpuidle_driver *drv = cpuidle_get_driver();
2098 printk(KERN_DEBUG pr_fmt("intel_idle yielding to %s\n"),
2099 drv ? drv->name : "none");
2100 goto init_driver_fail;
2103 retval = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "idle/intel:online",
2104 intel_idle_cpu_online, NULL);
2108 pr_debug("Local APIC timer is reliable in %s\n",
2109 boot_cpu_has(X86_FEATURE_ARAT) ? "all C-states" : "C1");
2114 intel_idle_cpuidle_devices_uninit();
2115 cpuidle_unregister_driver(&intel_idle_driver);
2117 free_percpu(intel_idle_cpuidle_devices);
2121 device_initcall(intel_idle_init);
2124 * We are not really modular, but we used to support that. Meaning we also
2125 * support "intel_idle.max_cstate=..." at boot and also a read-only export of
2126 * it at /sys/module/intel_idle/parameters/max_cstate -- so using module_param
2127 * is the easiest way (currently) to continue doing that.
2129 module_param(max_cstate, int, 0444);
2131 * The positions of the bits that are set in this number are the indices of the
2132 * idle states to be disabled by default (as reflected by the names of the
2133 * corresponding idle state directories in sysfs, "state0", "state1" ...
2134 * "state<i>" ..., where <i> is the index of the given state).
2136 module_param_named(states_off, disabled_states_mask, uint, 0444);
2137 MODULE_PARM_DESC(states_off, "Mask of disabled idle states");
2139 * Some platforms come with mutually exclusive C-states, so that if one is
2140 * enabled, the other C-states must not be used. Example: C1 and C1E on
2141 * Sapphire Rapids platform. This parameter allows for selecting the
2142 * preferred C-states among the groups of mutually exclusive C-states - the
2143 * selected C-states will be registered, the other C-states from the mutually
2144 * exclusive group won't be registered. If the platform has no mutually
2145 * exclusive C-states, this parameter has no effect.
2147 module_param_named(preferred_cstates, preferred_states_mask, uint, 0444);
2148 MODULE_PARM_DESC(preferred_cstates, "Mask of preferred idle states");