2 * intel_idle.c - native hardware idle loop for modern Intel processors
4 * Copyright (c) 2013, Intel Corporation.
5 * Len Brown <len.brown@intel.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 * intel_idle is a cpuidle driver that loads on specific Intel processors
23 * in lieu of the legacy ACPI processor_idle driver. The intent is to
24 * make Linux more efficient on these processors, as intel_idle knows
25 * more than ACPI, as well as make Linux more immune to ACPI BIOS bugs.
31 * All CPUs have same idle states as boot CPU
33 * Chipset BM_STS (bus master status) bit is a NOP
34 * for preventing entry into deep C-stats
40 * The driver currently initializes for_each_online_cpu() upon modprobe.
41 * It it unaware of subsequent processors hot-added to the system.
42 * This means that if you boot with maxcpus=n and later online
43 * processors above n, those processors will use C1 only.
45 * ACPI has a .suspend hack to turn off deep c-statees during suspend
46 * to avoid complications with the lapic timer workaround.
47 * Have not seen issues with suspend, but may need same workaround here.
49 * There is currently no kernel-based automatic probing/loading mechanism
50 * if the driver is built as a module.
53 /* un-comment DEBUG to enable pr_debug() statements */
56 #include <linux/kernel.h>
57 #include <linux/cpuidle.h>
58 #include <linux/tick.h>
59 #include <trace/events/power.h>
60 #include <linux/sched.h>
61 #include <linux/notifier.h>
62 #include <linux/cpu.h>
63 #include <linux/module.h>
64 #include <asm/cpu_device_id.h>
65 #include <asm/intel-family.h>
66 #include <asm/mwait.h>
69 #define INTEL_IDLE_VERSION "0.4.1"
70 #define PREFIX "intel_idle: "
72 static struct cpuidle_driver intel_idle_driver = {
76 /* intel_idle.max_cstate=0 disables driver */
77 static int max_cstate = CPUIDLE_STATE_MAX - 1;
79 static unsigned int mwait_substates;
81 #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
82 /* Reliable LAPIC Timer States, bit 1 for C1 etc. */
83 static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
86 struct cpuidle_state *state_table;
89 * Hardware C-state auto-demotion may not always be optimal.
90 * Indicate which enable bits to clear here.
92 unsigned long auto_demotion_disable_flags;
93 bool byt_auto_demotion_disable_flag;
94 bool disable_promotion_to_c1e;
97 static const struct idle_cpu *icpu;
98 static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
99 static int intel_idle(struct cpuidle_device *dev,
100 struct cpuidle_driver *drv, int index);
101 static void intel_idle_freeze(struct cpuidle_device *dev,
102 struct cpuidle_driver *drv, int index);
103 static int intel_idle_cpu_init(int cpu);
105 static struct cpuidle_state *cpuidle_state_table;
108 * Set this flag for states where the HW flushes the TLB for us
109 * and so we don't need cross-calls to keep it consistent.
110 * If this flag is set, SW flushes the TLB, so even if the
111 * HW doesn't do the flushing, this flag is safe to use.
113 #define CPUIDLE_FLAG_TLB_FLUSHED 0x10000
116 * MWAIT takes an 8-bit "hint" in EAX "suggesting"
117 * the C-state (top nibble) and sub-state (bottom nibble)
118 * 0x00 means "MWAIT(C1)", 0x10 means "MWAIT(C2)" etc.
120 * We store the hint at the top of our "flags" for each state.
122 #define flg2MWAIT(flags) (((flags) >> 24) & 0xFF)
123 #define MWAIT2flg(eax) ((eax & 0xFF) << 24)
126 * States are indexed by the cstate number,
127 * which is also the index into the MWAIT hint array.
128 * Thus C0 is a dummy.
130 static struct cpuidle_state nehalem_cstates[] = {
133 .desc = "MWAIT 0x00",
134 .flags = MWAIT2flg(0x00),
136 .target_residency = 6,
137 .enter = &intel_idle,
138 .enter_freeze = intel_idle_freeze, },
141 .desc = "MWAIT 0x01",
142 .flags = MWAIT2flg(0x01),
144 .target_residency = 20,
145 .enter = &intel_idle,
146 .enter_freeze = intel_idle_freeze, },
149 .desc = "MWAIT 0x10",
150 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
152 .target_residency = 80,
153 .enter = &intel_idle,
154 .enter_freeze = intel_idle_freeze, },
157 .desc = "MWAIT 0x20",
158 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
160 .target_residency = 800,
161 .enter = &intel_idle,
162 .enter_freeze = intel_idle_freeze, },
167 static struct cpuidle_state snb_cstates[] = {
170 .desc = "MWAIT 0x00",
171 .flags = MWAIT2flg(0x00),
173 .target_residency = 2,
174 .enter = &intel_idle,
175 .enter_freeze = intel_idle_freeze, },
178 .desc = "MWAIT 0x01",
179 .flags = MWAIT2flg(0x01),
181 .target_residency = 20,
182 .enter = &intel_idle,
183 .enter_freeze = intel_idle_freeze, },
186 .desc = "MWAIT 0x10",
187 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
189 .target_residency = 211,
190 .enter = &intel_idle,
191 .enter_freeze = intel_idle_freeze, },
194 .desc = "MWAIT 0x20",
195 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
197 .target_residency = 345,
198 .enter = &intel_idle,
199 .enter_freeze = intel_idle_freeze, },
202 .desc = "MWAIT 0x30",
203 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
205 .target_residency = 345,
206 .enter = &intel_idle,
207 .enter_freeze = intel_idle_freeze, },
212 static struct cpuidle_state byt_cstates[] = {
215 .desc = "MWAIT 0x00",
216 .flags = MWAIT2flg(0x00),
218 .target_residency = 1,
219 .enter = &intel_idle,
220 .enter_freeze = intel_idle_freeze, },
223 .desc = "MWAIT 0x58",
224 .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
226 .target_residency = 275,
227 .enter = &intel_idle,
228 .enter_freeze = intel_idle_freeze, },
231 .desc = "MWAIT 0x52",
232 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
234 .target_residency = 560,
235 .enter = &intel_idle,
236 .enter_freeze = intel_idle_freeze, },
239 .desc = "MWAIT 0x60",
240 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
241 .exit_latency = 1200,
242 .target_residency = 4000,
243 .enter = &intel_idle,
244 .enter_freeze = intel_idle_freeze, },
247 .desc = "MWAIT 0x64",
248 .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
249 .exit_latency = 10000,
250 .target_residency = 20000,
251 .enter = &intel_idle,
252 .enter_freeze = intel_idle_freeze, },
257 static struct cpuidle_state cht_cstates[] = {
260 .desc = "MWAIT 0x00",
261 .flags = MWAIT2flg(0x00),
263 .target_residency = 1,
264 .enter = &intel_idle,
265 .enter_freeze = intel_idle_freeze, },
268 .desc = "MWAIT 0x58",
269 .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
271 .target_residency = 275,
272 .enter = &intel_idle,
273 .enter_freeze = intel_idle_freeze, },
276 .desc = "MWAIT 0x52",
277 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
279 .target_residency = 560,
280 .enter = &intel_idle,
281 .enter_freeze = intel_idle_freeze, },
284 .desc = "MWAIT 0x60",
285 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
286 .exit_latency = 1200,
287 .target_residency = 4000,
288 .enter = &intel_idle,
289 .enter_freeze = intel_idle_freeze, },
292 .desc = "MWAIT 0x64",
293 .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
294 .exit_latency = 10000,
295 .target_residency = 20000,
296 .enter = &intel_idle,
297 .enter_freeze = intel_idle_freeze, },
302 static struct cpuidle_state ivb_cstates[] = {
305 .desc = "MWAIT 0x00",
306 .flags = MWAIT2flg(0x00),
308 .target_residency = 1,
309 .enter = &intel_idle,
310 .enter_freeze = intel_idle_freeze, },
313 .desc = "MWAIT 0x01",
314 .flags = MWAIT2flg(0x01),
316 .target_residency = 20,
317 .enter = &intel_idle,
318 .enter_freeze = intel_idle_freeze, },
321 .desc = "MWAIT 0x10",
322 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
324 .target_residency = 156,
325 .enter = &intel_idle,
326 .enter_freeze = intel_idle_freeze, },
329 .desc = "MWAIT 0x20",
330 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
332 .target_residency = 300,
333 .enter = &intel_idle,
334 .enter_freeze = intel_idle_freeze, },
337 .desc = "MWAIT 0x30",
338 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
340 .target_residency = 300,
341 .enter = &intel_idle,
342 .enter_freeze = intel_idle_freeze, },
347 static struct cpuidle_state ivt_cstates[] = {
350 .desc = "MWAIT 0x00",
351 .flags = MWAIT2flg(0x00),
353 .target_residency = 1,
354 .enter = &intel_idle,
355 .enter_freeze = intel_idle_freeze, },
358 .desc = "MWAIT 0x01",
359 .flags = MWAIT2flg(0x01),
361 .target_residency = 80,
362 .enter = &intel_idle,
363 .enter_freeze = intel_idle_freeze, },
366 .desc = "MWAIT 0x10",
367 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
369 .target_residency = 156,
370 .enter = &intel_idle,
371 .enter_freeze = intel_idle_freeze, },
374 .desc = "MWAIT 0x20",
375 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
377 .target_residency = 300,
378 .enter = &intel_idle,
379 .enter_freeze = intel_idle_freeze, },
384 static struct cpuidle_state ivt_cstates_4s[] = {
387 .desc = "MWAIT 0x00",
388 .flags = MWAIT2flg(0x00),
390 .target_residency = 1,
391 .enter = &intel_idle,
392 .enter_freeze = intel_idle_freeze, },
394 .name = "C1E-IVT-4S",
395 .desc = "MWAIT 0x01",
396 .flags = MWAIT2flg(0x01),
398 .target_residency = 250,
399 .enter = &intel_idle,
400 .enter_freeze = intel_idle_freeze, },
403 .desc = "MWAIT 0x10",
404 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
406 .target_residency = 300,
407 .enter = &intel_idle,
408 .enter_freeze = intel_idle_freeze, },
411 .desc = "MWAIT 0x20",
412 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
414 .target_residency = 400,
415 .enter = &intel_idle,
416 .enter_freeze = intel_idle_freeze, },
421 static struct cpuidle_state ivt_cstates_8s[] = {
424 .desc = "MWAIT 0x00",
425 .flags = MWAIT2flg(0x00),
427 .target_residency = 1,
428 .enter = &intel_idle,
429 .enter_freeze = intel_idle_freeze, },
431 .name = "C1E-IVT-8S",
432 .desc = "MWAIT 0x01",
433 .flags = MWAIT2flg(0x01),
435 .target_residency = 500,
436 .enter = &intel_idle,
437 .enter_freeze = intel_idle_freeze, },
440 .desc = "MWAIT 0x10",
441 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
443 .target_residency = 600,
444 .enter = &intel_idle,
445 .enter_freeze = intel_idle_freeze, },
448 .desc = "MWAIT 0x20",
449 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
451 .target_residency = 700,
452 .enter = &intel_idle,
453 .enter_freeze = intel_idle_freeze, },
458 static struct cpuidle_state hsw_cstates[] = {
461 .desc = "MWAIT 0x00",
462 .flags = MWAIT2flg(0x00),
464 .target_residency = 2,
465 .enter = &intel_idle,
466 .enter_freeze = intel_idle_freeze, },
469 .desc = "MWAIT 0x01",
470 .flags = MWAIT2flg(0x01),
472 .target_residency = 20,
473 .enter = &intel_idle,
474 .enter_freeze = intel_idle_freeze, },
477 .desc = "MWAIT 0x10",
478 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
480 .target_residency = 100,
481 .enter = &intel_idle,
482 .enter_freeze = intel_idle_freeze, },
485 .desc = "MWAIT 0x20",
486 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
488 .target_residency = 400,
489 .enter = &intel_idle,
490 .enter_freeze = intel_idle_freeze, },
493 .desc = "MWAIT 0x32",
494 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
496 .target_residency = 500,
497 .enter = &intel_idle,
498 .enter_freeze = intel_idle_freeze, },
501 .desc = "MWAIT 0x40",
502 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
504 .target_residency = 900,
505 .enter = &intel_idle,
506 .enter_freeze = intel_idle_freeze, },
509 .desc = "MWAIT 0x50",
510 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
512 .target_residency = 1800,
513 .enter = &intel_idle,
514 .enter_freeze = intel_idle_freeze, },
517 .desc = "MWAIT 0x60",
518 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
519 .exit_latency = 2600,
520 .target_residency = 7700,
521 .enter = &intel_idle,
522 .enter_freeze = intel_idle_freeze, },
526 static struct cpuidle_state bdw_cstates[] = {
529 .desc = "MWAIT 0x00",
530 .flags = MWAIT2flg(0x00),
532 .target_residency = 2,
533 .enter = &intel_idle,
534 .enter_freeze = intel_idle_freeze, },
537 .desc = "MWAIT 0x01",
538 .flags = MWAIT2flg(0x01),
540 .target_residency = 20,
541 .enter = &intel_idle,
542 .enter_freeze = intel_idle_freeze, },
545 .desc = "MWAIT 0x10",
546 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
548 .target_residency = 100,
549 .enter = &intel_idle,
550 .enter_freeze = intel_idle_freeze, },
553 .desc = "MWAIT 0x20",
554 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
556 .target_residency = 400,
557 .enter = &intel_idle,
558 .enter_freeze = intel_idle_freeze, },
561 .desc = "MWAIT 0x32",
562 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
564 .target_residency = 500,
565 .enter = &intel_idle,
566 .enter_freeze = intel_idle_freeze, },
569 .desc = "MWAIT 0x40",
570 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
572 .target_residency = 900,
573 .enter = &intel_idle,
574 .enter_freeze = intel_idle_freeze, },
577 .desc = "MWAIT 0x50",
578 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
580 .target_residency = 1800,
581 .enter = &intel_idle,
582 .enter_freeze = intel_idle_freeze, },
585 .desc = "MWAIT 0x60",
586 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
587 .exit_latency = 2600,
588 .target_residency = 7700,
589 .enter = &intel_idle,
590 .enter_freeze = intel_idle_freeze, },
595 static struct cpuidle_state skl_cstates[] = {
598 .desc = "MWAIT 0x00",
599 .flags = MWAIT2flg(0x00),
601 .target_residency = 2,
602 .enter = &intel_idle,
603 .enter_freeze = intel_idle_freeze, },
606 .desc = "MWAIT 0x01",
607 .flags = MWAIT2flg(0x01),
609 .target_residency = 20,
610 .enter = &intel_idle,
611 .enter_freeze = intel_idle_freeze, },
614 .desc = "MWAIT 0x10",
615 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
617 .target_residency = 100,
618 .enter = &intel_idle,
619 .enter_freeze = intel_idle_freeze, },
622 .desc = "MWAIT 0x20",
623 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
625 .target_residency = 200,
626 .enter = &intel_idle,
627 .enter_freeze = intel_idle_freeze, },
630 .desc = "MWAIT 0x33",
631 .flags = MWAIT2flg(0x33) | CPUIDLE_FLAG_TLB_FLUSHED,
633 .target_residency = 800,
634 .enter = &intel_idle,
635 .enter_freeze = intel_idle_freeze, },
638 .desc = "MWAIT 0x40",
639 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
641 .target_residency = 800,
642 .enter = &intel_idle,
643 .enter_freeze = intel_idle_freeze, },
646 .desc = "MWAIT 0x50",
647 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
649 .target_residency = 5000,
650 .enter = &intel_idle,
651 .enter_freeze = intel_idle_freeze, },
654 .desc = "MWAIT 0x60",
655 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
657 .target_residency = 5000,
658 .enter = &intel_idle,
659 .enter_freeze = intel_idle_freeze, },
664 static struct cpuidle_state skx_cstates[] = {
667 .desc = "MWAIT 0x00",
668 .flags = MWAIT2flg(0x00),
670 .target_residency = 2,
671 .enter = &intel_idle,
672 .enter_freeze = intel_idle_freeze, },
675 .desc = "MWAIT 0x01",
676 .flags = MWAIT2flg(0x01),
678 .target_residency = 20,
679 .enter = &intel_idle,
680 .enter_freeze = intel_idle_freeze, },
683 .desc = "MWAIT 0x20",
684 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
686 .target_residency = 600,
687 .enter = &intel_idle,
688 .enter_freeze = intel_idle_freeze, },
693 static struct cpuidle_state atom_cstates[] = {
696 .desc = "MWAIT 0x00",
697 .flags = MWAIT2flg(0x00),
699 .target_residency = 20,
700 .enter = &intel_idle,
701 .enter_freeze = intel_idle_freeze, },
704 .desc = "MWAIT 0x10",
705 .flags = MWAIT2flg(0x10),
707 .target_residency = 80,
708 .enter = &intel_idle,
709 .enter_freeze = intel_idle_freeze, },
712 .desc = "MWAIT 0x30",
713 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
715 .target_residency = 400,
716 .enter = &intel_idle,
717 .enter_freeze = intel_idle_freeze, },
720 .desc = "MWAIT 0x52",
721 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
723 .target_residency = 560,
724 .enter = &intel_idle,
725 .enter_freeze = intel_idle_freeze, },
729 static struct cpuidle_state avn_cstates[] = {
732 .desc = "MWAIT 0x00",
733 .flags = MWAIT2flg(0x00),
735 .target_residency = 2,
736 .enter = &intel_idle,
737 .enter_freeze = intel_idle_freeze, },
740 .desc = "MWAIT 0x51",
741 .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TLB_FLUSHED,
743 .target_residency = 45,
744 .enter = &intel_idle,
745 .enter_freeze = intel_idle_freeze, },
749 static struct cpuidle_state knl_cstates[] = {
752 .desc = "MWAIT 0x00",
753 .flags = MWAIT2flg(0x00),
755 .target_residency = 2,
756 .enter = &intel_idle,
757 .enter_freeze = intel_idle_freeze },
760 .desc = "MWAIT 0x10",
761 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
763 .target_residency = 500,
764 .enter = &intel_idle,
765 .enter_freeze = intel_idle_freeze },
770 static struct cpuidle_state bxt_cstates[] = {
773 .desc = "MWAIT 0x00",
774 .flags = MWAIT2flg(0x00),
776 .target_residency = 2,
777 .enter = &intel_idle,
778 .enter_freeze = intel_idle_freeze, },
781 .desc = "MWAIT 0x01",
782 .flags = MWAIT2flg(0x01),
784 .target_residency = 20,
785 .enter = &intel_idle,
786 .enter_freeze = intel_idle_freeze, },
789 .desc = "MWAIT 0x20",
790 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
792 .target_residency = 133,
793 .enter = &intel_idle,
794 .enter_freeze = intel_idle_freeze, },
797 .desc = "MWAIT 0x31",
798 .flags = MWAIT2flg(0x31) | CPUIDLE_FLAG_TLB_FLUSHED,
800 .target_residency = 155,
801 .enter = &intel_idle,
802 .enter_freeze = intel_idle_freeze, },
805 .desc = "MWAIT 0x40",
806 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
807 .exit_latency = 1000,
808 .target_residency = 1000,
809 .enter = &intel_idle,
810 .enter_freeze = intel_idle_freeze, },
813 .desc = "MWAIT 0x50",
814 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
815 .exit_latency = 2000,
816 .target_residency = 2000,
817 .enter = &intel_idle,
818 .enter_freeze = intel_idle_freeze, },
821 .desc = "MWAIT 0x60",
822 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
823 .exit_latency = 10000,
824 .target_residency = 10000,
825 .enter = &intel_idle,
826 .enter_freeze = intel_idle_freeze, },
833 * @dev: cpuidle_device
834 * @drv: cpuidle driver
835 * @index: index of cpuidle state
837 * Must be called under local_irq_disable().
839 static int intel_idle(struct cpuidle_device *dev,
840 struct cpuidle_driver *drv, int index)
842 unsigned long ecx = 1; /* break on interrupt flag */
843 struct cpuidle_state *state = &drv->states[index];
844 unsigned long eax = flg2MWAIT(state->flags);
846 int cpu = smp_processor_id();
848 cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
851 * leave_mm() to avoid costly and often unnecessary wakeups
852 * for flushing the user TLB's associated with the active mm.
854 if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
857 if (!(lapic_timer_reliable_states & (1 << (cstate))))
858 tick_broadcast_enter();
860 mwait_idle_with_hints(eax, ecx);
862 if (!(lapic_timer_reliable_states & (1 << (cstate))))
863 tick_broadcast_exit();
869 * intel_idle_freeze - simplified "enter" callback routine for suspend-to-idle
870 * @dev: cpuidle_device
871 * @drv: cpuidle driver
872 * @index: state index
874 static void intel_idle_freeze(struct cpuidle_device *dev,
875 struct cpuidle_driver *drv, int index)
877 unsigned long ecx = 1; /* break on interrupt flag */
878 unsigned long eax = flg2MWAIT(drv->states[index].flags);
880 mwait_idle_with_hints(eax, ecx);
883 static void __setup_broadcast_timer(void *arg)
885 unsigned long on = (unsigned long)arg;
888 tick_broadcast_enable();
890 tick_broadcast_disable();
893 static int cpu_hotplug_notify(struct notifier_block *n,
894 unsigned long action, void *hcpu)
896 int hotcpu = (unsigned long)hcpu;
897 struct cpuidle_device *dev;
899 switch (action & ~CPU_TASKS_FROZEN) {
902 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
903 smp_call_function_single(hotcpu, __setup_broadcast_timer,
907 * Some systems can hotplug a cpu at runtime after
908 * the kernel has booted, we have to initialize the
909 * driver in this case
911 dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu);
915 if (intel_idle_cpu_init(hotcpu))
923 static struct notifier_block cpu_hotplug_notifier = {
924 .notifier_call = cpu_hotplug_notify,
927 static void auto_demotion_disable(void *dummy)
929 unsigned long long msr_bits;
931 rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
932 msr_bits &= ~(icpu->auto_demotion_disable_flags);
933 wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
935 static void c1e_promotion_disable(void *dummy)
937 unsigned long long msr_bits;
939 rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
941 wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
944 static const struct idle_cpu idle_cpu_nehalem = {
945 .state_table = nehalem_cstates,
946 .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
947 .disable_promotion_to_c1e = true,
950 static const struct idle_cpu idle_cpu_atom = {
951 .state_table = atom_cstates,
954 static const struct idle_cpu idle_cpu_lincroft = {
955 .state_table = atom_cstates,
956 .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
959 static const struct idle_cpu idle_cpu_snb = {
960 .state_table = snb_cstates,
961 .disable_promotion_to_c1e = true,
964 static const struct idle_cpu idle_cpu_byt = {
965 .state_table = byt_cstates,
966 .disable_promotion_to_c1e = true,
967 .byt_auto_demotion_disable_flag = true,
970 static const struct idle_cpu idle_cpu_cht = {
971 .state_table = cht_cstates,
972 .disable_promotion_to_c1e = true,
973 .byt_auto_demotion_disable_flag = true,
976 static const struct idle_cpu idle_cpu_ivb = {
977 .state_table = ivb_cstates,
978 .disable_promotion_to_c1e = true,
981 static const struct idle_cpu idle_cpu_ivt = {
982 .state_table = ivt_cstates,
983 .disable_promotion_to_c1e = true,
986 static const struct idle_cpu idle_cpu_hsw = {
987 .state_table = hsw_cstates,
988 .disable_promotion_to_c1e = true,
991 static const struct idle_cpu idle_cpu_bdw = {
992 .state_table = bdw_cstates,
993 .disable_promotion_to_c1e = true,
996 static const struct idle_cpu idle_cpu_skl = {
997 .state_table = skl_cstates,
998 .disable_promotion_to_c1e = true,
1001 static const struct idle_cpu idle_cpu_skx = {
1002 .state_table = skx_cstates,
1003 .disable_promotion_to_c1e = true,
1006 static const struct idle_cpu idle_cpu_avn = {
1007 .state_table = avn_cstates,
1008 .disable_promotion_to_c1e = true,
1011 static const struct idle_cpu idle_cpu_knl = {
1012 .state_table = knl_cstates,
1015 static const struct idle_cpu idle_cpu_bxt = {
1016 .state_table = bxt_cstates,
1017 .disable_promotion_to_c1e = true,
1020 #define ICPU(model, cpu) \
1021 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu }
1023 static const struct x86_cpu_id intel_idle_ids[] __initconst = {
1024 ICPU(INTEL_FAM6_NEHALEM_EP, idle_cpu_nehalem),
1025 ICPU(INTEL_FAM6_NEHALEM, idle_cpu_nehalem),
1026 ICPU(INTEL_FAM6_WESTMERE2, idle_cpu_nehalem),
1027 ICPU(INTEL_FAM6_WESTMERE, idle_cpu_nehalem),
1028 ICPU(INTEL_FAM6_WESTMERE_EP, idle_cpu_nehalem),
1029 ICPU(INTEL_FAM6_NEHALEM_EX, idle_cpu_nehalem),
1030 ICPU(INTEL_FAM6_ATOM_PINEVIEW, idle_cpu_atom),
1031 ICPU(INTEL_FAM6_ATOM_LINCROFT, idle_cpu_lincroft),
1032 ICPU(INTEL_FAM6_WESTMERE_EX, idle_cpu_nehalem),
1033 ICPU(INTEL_FAM6_SANDYBRIDGE, idle_cpu_snb),
1034 ICPU(INTEL_FAM6_SANDYBRIDGE_X, idle_cpu_snb),
1035 ICPU(INTEL_FAM6_ATOM_CEDARVIEW, idle_cpu_atom),
1036 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, idle_cpu_byt),
1037 ICPU(INTEL_FAM6_ATOM_AIRMONT, idle_cpu_cht),
1038 ICPU(INTEL_FAM6_IVYBRIDGE, idle_cpu_ivb),
1039 ICPU(INTEL_FAM6_IVYBRIDGE_X, idle_cpu_ivt),
1040 ICPU(INTEL_FAM6_HASWELL_CORE, idle_cpu_hsw),
1041 ICPU(INTEL_FAM6_HASWELL_X, idle_cpu_hsw),
1042 ICPU(INTEL_FAM6_HASWELL_ULT, idle_cpu_hsw),
1043 ICPU(INTEL_FAM6_HASWELL_GT3E, idle_cpu_hsw),
1044 ICPU(INTEL_FAM6_ATOM_SILVERMONT2, idle_cpu_avn),
1045 ICPU(INTEL_FAM6_BROADWELL_CORE, idle_cpu_bdw),
1046 ICPU(INTEL_FAM6_BROADWELL_GT3E, idle_cpu_bdw),
1047 ICPU(INTEL_FAM6_BROADWELL_X, idle_cpu_bdw),
1048 ICPU(INTEL_FAM6_BROADWELL_XEON_D, idle_cpu_bdw),
1049 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, idle_cpu_skl),
1050 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, idle_cpu_skl),
1051 ICPU(INTEL_FAM6_KABYLAKE_MOBILE, idle_cpu_skl),
1052 ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, idle_cpu_skl),
1053 ICPU(INTEL_FAM6_SKYLAKE_X, idle_cpu_skx),
1054 ICPU(INTEL_FAM6_XEON_PHI_KNL, idle_cpu_knl),
1055 ICPU(INTEL_FAM6_ATOM_GOLDMONT, idle_cpu_bxt),
1058 MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
1061 * intel_idle_probe()
1063 static int __init intel_idle_probe(void)
1065 unsigned int eax, ebx, ecx;
1066 const struct x86_cpu_id *id;
1068 if (max_cstate == 0) {
1069 pr_debug(PREFIX "disabled\n");
1073 id = x86_match_cpu(intel_idle_ids);
1075 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
1076 boot_cpu_data.x86 == 6)
1077 pr_debug(PREFIX "does not run on family %d model %d\n",
1078 boot_cpu_data.x86, boot_cpu_data.x86_model);
1082 if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
1085 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
1087 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
1088 !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
1092 pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
1094 icpu = (const struct idle_cpu *)id->driver_data;
1095 cpuidle_state_table = icpu->state_table;
1097 pr_debug(PREFIX "v" INTEL_IDLE_VERSION
1098 " model 0x%X\n", boot_cpu_data.x86_model);
1104 * intel_idle_cpuidle_devices_uninit()
1105 * Unregisters the cpuidle devices.
1107 static void intel_idle_cpuidle_devices_uninit(void)
1110 struct cpuidle_device *dev;
1112 for_each_online_cpu(i) {
1113 dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
1114 cpuidle_unregister_device(dev);
1119 * ivt_idle_state_table_update(void)
1121 * Tune IVT multi-socket targets
1122 * Assumption: num_sockets == (max_package_num + 1)
1124 static void ivt_idle_state_table_update(void)
1126 /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
1127 int cpu, package_num, num_sockets = 1;
1129 for_each_online_cpu(cpu) {
1130 package_num = topology_physical_package_id(cpu);
1131 if (package_num + 1 > num_sockets) {
1132 num_sockets = package_num + 1;
1134 if (num_sockets > 4) {
1135 cpuidle_state_table = ivt_cstates_8s;
1141 if (num_sockets > 2)
1142 cpuidle_state_table = ivt_cstates_4s;
1144 /* else, 1 and 2 socket systems use default ivt_cstates */
1148 * Translate IRTL (Interrupt Response Time Limit) MSR to usec
1151 static unsigned int irtl_ns_units[] = {
1152 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1154 static unsigned long long irtl_2_usec(unsigned long long irtl)
1156 unsigned long long ns;
1158 ns = irtl_ns_units[(irtl >> 10) & 0x3];
1160 return div64_u64((irtl & 0x3FF) * ns, 1000);
1163 * bxt_idle_state_table_update(void)
1165 * On BXT, we trust the IRTL to show the definitive maximum latency
1166 * We use the same value for target_residency.
1168 static void bxt_idle_state_table_update(void)
1170 unsigned long long msr;
1172 rdmsrl(MSR_PKGC6_IRTL, msr);
1174 unsigned int usec = irtl_2_usec(msr);
1176 bxt_cstates[2].exit_latency = usec;
1177 bxt_cstates[2].target_residency = usec;
1180 rdmsrl(MSR_PKGC7_IRTL, msr);
1182 unsigned int usec = irtl_2_usec(msr);
1184 bxt_cstates[3].exit_latency = usec;
1185 bxt_cstates[3].target_residency = usec;
1188 rdmsrl(MSR_PKGC8_IRTL, msr);
1190 unsigned int usec = irtl_2_usec(msr);
1192 bxt_cstates[4].exit_latency = usec;
1193 bxt_cstates[4].target_residency = usec;
1196 rdmsrl(MSR_PKGC9_IRTL, msr);
1198 unsigned int usec = irtl_2_usec(msr);
1200 bxt_cstates[5].exit_latency = usec;
1201 bxt_cstates[5].target_residency = usec;
1204 rdmsrl(MSR_PKGC10_IRTL, msr);
1206 unsigned int usec = irtl_2_usec(msr);
1208 bxt_cstates[6].exit_latency = usec;
1209 bxt_cstates[6].target_residency = usec;
1214 * sklh_idle_state_table_update(void)
1216 * On SKL-H (model 0x5e) disable C8 and C9 if:
1217 * C10 is enabled and SGX disabled
1219 static void sklh_idle_state_table_update(void)
1221 unsigned long long msr;
1222 unsigned int eax, ebx, ecx, edx;
1225 /* if PC10 disabled via cmdline intel_idle.max_cstate=7 or shallower */
1226 if (max_cstate <= 7)
1229 /* if PC10 not present in CPUID.MWAIT.EDX */
1230 if ((mwait_substates & (0xF << 28)) == 0)
1233 rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr);
1235 /* PC10 is not enabled in PKG C-state limit */
1236 if ((msr & 0xF) != 8)
1240 cpuid(7, &eax, &ebx, &ecx, &edx);
1242 /* if SGX is present */
1243 if (ebx & (1 << 2)) {
1245 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
1247 /* if SGX is enabled */
1248 if (msr & (1 << 18))
1252 skl_cstates[5].disabled = 1; /* C8-SKL */
1253 skl_cstates[6].disabled = 1; /* C9-SKL */
1256 * intel_idle_state_table_update()
1258 * Update the default state_table for this CPU-id
1261 static void intel_idle_state_table_update(void)
1263 switch (boot_cpu_data.x86_model) {
1265 case INTEL_FAM6_IVYBRIDGE_X:
1266 ivt_idle_state_table_update();
1268 case INTEL_FAM6_ATOM_GOLDMONT:
1269 bxt_idle_state_table_update();
1271 case INTEL_FAM6_SKYLAKE_DESKTOP:
1272 sklh_idle_state_table_update();
1278 * intel_idle_cpuidle_driver_init()
1279 * allocate, initialize cpuidle_states
1281 static void __init intel_idle_cpuidle_driver_init(void)
1284 struct cpuidle_driver *drv = &intel_idle_driver;
1286 intel_idle_state_table_update();
1288 drv->state_count = 1;
1290 for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
1291 int num_substates, mwait_hint, mwait_cstate;
1293 if ((cpuidle_state_table[cstate].enter == NULL) &&
1294 (cpuidle_state_table[cstate].enter_freeze == NULL))
1297 if (cstate + 1 > max_cstate) {
1298 printk(PREFIX "max_cstate %d reached\n",
1303 mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
1304 mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
1306 /* number of sub-states for this state in CPUID.MWAIT */
1307 num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
1308 & MWAIT_SUBSTATE_MASK;
1310 /* if NO sub-states for this state in CPUID, skip it */
1311 if (num_substates == 0)
1314 /* if state marked as disabled, skip it */
1315 if (cpuidle_state_table[cstate].disabled != 0) {
1316 pr_debug(PREFIX "state %s is disabled",
1317 cpuidle_state_table[cstate].name);
1322 if (((mwait_cstate + 1) > 2) &&
1323 !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
1324 mark_tsc_unstable("TSC halts in idle"
1325 " states deeper than C2");
1327 drv->states[drv->state_count] = /* structure copy */
1328 cpuidle_state_table[cstate];
1330 drv->state_count += 1;
1333 if (icpu->byt_auto_demotion_disable_flag) {
1334 wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
1335 wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
1341 * intel_idle_cpu_init()
1342 * allocate, initialize, register cpuidle_devices
1343 * @cpu: cpu/core to initialize
1345 static int intel_idle_cpu_init(int cpu)
1347 struct cpuidle_device *dev;
1349 dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
1353 if (cpuidle_register_device(dev)) {
1354 pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
1358 if (icpu->auto_demotion_disable_flags)
1359 smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
1361 if (icpu->disable_promotion_to_c1e)
1362 smp_call_function_single(cpu, c1e_promotion_disable, NULL, 1);
1367 static int __init intel_idle_init(void)
1371 /* Do not load intel_idle at all for now if idle= is passed */
1372 if (boot_option_idle_override != IDLE_NO_OVERRIDE)
1375 retval = intel_idle_probe();
1379 intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
1380 if (intel_idle_cpuidle_devices == NULL)
1383 intel_idle_cpuidle_driver_init();
1384 retval = cpuidle_register_driver(&intel_idle_driver);
1386 struct cpuidle_driver *drv = cpuidle_get_driver();
1387 printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
1388 drv ? drv->name : "none");
1389 free_percpu(intel_idle_cpuidle_devices);
1393 cpu_notifier_register_begin();
1395 for_each_online_cpu(i) {
1396 retval = intel_idle_cpu_init(i);
1398 intel_idle_cpuidle_devices_uninit();
1399 cpu_notifier_register_done();
1400 cpuidle_unregister_driver(&intel_idle_driver);
1401 free_percpu(intel_idle_cpuidle_devices);
1405 __register_cpu_notifier(&cpu_hotplug_notifier);
1407 if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
1408 lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
1410 on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
1412 cpu_notifier_register_done();
1414 pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
1415 lapic_timer_reliable_states);
1420 static void __exit intel_idle_exit(void)
1422 struct cpuidle_device *dev;
1425 cpu_notifier_register_begin();
1427 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
1428 on_each_cpu(__setup_broadcast_timer, (void *)false, 1);
1429 __unregister_cpu_notifier(&cpu_hotplug_notifier);
1431 for_each_possible_cpu(i) {
1432 dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
1433 cpuidle_unregister_device(dev);
1436 cpu_notifier_register_done();
1438 cpuidle_unregister_driver(&intel_idle_driver);
1439 free_percpu(intel_idle_cpuidle_devices);
1442 module_init(intel_idle_init);
1443 module_exit(intel_idle_exit);
1445 module_param(max_cstate, int, 0444);
1447 MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
1448 MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
1449 MODULE_LICENSE("GPL");