126717172SLen Brown /* 226717172SLen Brown * intel_idle.c - native hardware idle loop for modern Intel processors 326717172SLen Brown * 4fab04b22SLen Brown * Copyright (c) 2013, Intel Corporation. 526717172SLen Brown * Len Brown <len.brown@intel.com> 626717172SLen Brown * 726717172SLen Brown * This program is free software; you can redistribute it and/or modify it 826717172SLen Brown * under the terms and conditions of the GNU General Public License, 926717172SLen Brown * version 2, as published by the Free Software Foundation. 1026717172SLen Brown * 1126717172SLen Brown * This program is distributed in the hope it will be useful, but WITHOUT 1226717172SLen Brown * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1326717172SLen Brown * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1426717172SLen Brown * more details. 1526717172SLen Brown * 1626717172SLen Brown * You should have received a copy of the GNU General Public License along with 1726717172SLen Brown * this program; if not, write to the Free Software Foundation, Inc., 1826717172SLen Brown * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 1926717172SLen Brown */ 2026717172SLen Brown 2126717172SLen Brown /* 2226717172SLen Brown * intel_idle is a cpuidle driver that loads on specific Intel processors 2326717172SLen Brown * in lieu of the legacy ACPI processor_idle driver. The intent is to 2426717172SLen Brown * make Linux more efficient on these processors, as intel_idle knows 2526717172SLen Brown * more than ACPI, as well as make Linux more immune to ACPI BIOS bugs. 2626717172SLen Brown */ 2726717172SLen Brown 2826717172SLen Brown /* 2926717172SLen Brown * Design Assumptions 3026717172SLen Brown * 3126717172SLen Brown * All CPUs have same idle states as boot CPU 3226717172SLen Brown * 3326717172SLen Brown * Chipset BM_STS (bus master status) bit is a NOP 3426717172SLen Brown * for preventing entry into deep C-stats 3526717172SLen Brown */ 3626717172SLen Brown 3726717172SLen Brown /* 3826717172SLen Brown * Known limitations 3926717172SLen Brown * 4026717172SLen Brown * The driver currently initializes for_each_online_cpu() upon modprobe. 4126717172SLen Brown * It it unaware of subsequent processors hot-added to the system. 4226717172SLen Brown * This means that if you boot with maxcpus=n and later online 4326717172SLen Brown * processors above n, those processors will use C1 only. 4426717172SLen Brown * 4526717172SLen Brown * ACPI has a .suspend hack to turn off deep c-statees during suspend 4626717172SLen Brown * to avoid complications with the lapic timer workaround. 4726717172SLen Brown * Have not seen issues with suspend, but may need same workaround here. 4826717172SLen Brown * 4926717172SLen Brown * There is currently no kernel-based automatic probing/loading mechanism 5026717172SLen Brown * if the driver is built as a module. 5126717172SLen Brown */ 5226717172SLen Brown 5326717172SLen Brown /* un-comment DEBUG to enable pr_debug() statements */ 5426717172SLen Brown #define DEBUG 5526717172SLen Brown 5626717172SLen Brown #include <linux/kernel.h> 5726717172SLen Brown #include <linux/cpuidle.h> 5826717172SLen Brown #include <linux/clockchips.h> 5926717172SLen Brown #include <trace/events/power.h> 6026717172SLen Brown #include <linux/sched.h> 612a2d31c8SShaohua Li #include <linux/notifier.h> 622a2d31c8SShaohua Li #include <linux/cpu.h> 637c52d551SPaul Gortmaker #include <linux/module.h> 64b66b8b9aSAndi Kleen #include <asm/cpu_device_id.h> 65bc83ccccSH. Peter Anvin #include <asm/mwait.h> 6614796fcaSLen Brown #include <asm/msr.h> 6726717172SLen Brown 6826717172SLen Brown #define INTEL_IDLE_VERSION "0.4" 6926717172SLen Brown #define PREFIX "intel_idle: " 7026717172SLen Brown 7126717172SLen Brown static struct cpuidle_driver intel_idle_driver = { 7226717172SLen Brown .name = "intel_idle", 7326717172SLen Brown .owner = THIS_MODULE, 7426717172SLen Brown }; 7526717172SLen Brown /* intel_idle.max_cstate=0 disables driver */ 76137ecc77SLen Brown static int max_cstate = CPUIDLE_STATE_MAX - 1; 7726717172SLen Brown 78c4236282SLen Brown static unsigned int mwait_substates; 7926717172SLen Brown 802a2d31c8SShaohua Li #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF 8126717172SLen Brown /* Reliable LAPIC Timer States, bit 1 for C1 etc. */ 82d13780d4SLen Brown static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */ 8326717172SLen Brown 84b66b8b9aSAndi Kleen struct idle_cpu { 85b66b8b9aSAndi Kleen struct cpuidle_state *state_table; 8626717172SLen Brown 8726717172SLen Brown /* 8814796fcaSLen Brown * Hardware C-state auto-demotion may not always be optimal. 8914796fcaSLen Brown * Indicate which enable bits to clear here. 9014796fcaSLen Brown */ 91b66b8b9aSAndi Kleen unsigned long auto_demotion_disable_flags; 9232e95180SLen Brown bool disable_promotion_to_c1e; 93b66b8b9aSAndi Kleen }; 94b66b8b9aSAndi Kleen 95b66b8b9aSAndi Kleen static const struct idle_cpu *icpu; 96b66b8b9aSAndi Kleen static struct cpuidle_device __percpu *intel_idle_cpuidle_devices; 97b66b8b9aSAndi Kleen static int intel_idle(struct cpuidle_device *dev, 98b66b8b9aSAndi Kleen struct cpuidle_driver *drv, int index); 9925ac7761SDaniel Lezcano static int intel_idle_cpu_init(int cpu); 100b66b8b9aSAndi Kleen 101b66b8b9aSAndi Kleen static struct cpuidle_state *cpuidle_state_table; 10214796fcaSLen Brown 10314796fcaSLen Brown /* 104956d033fSLen Brown * Set this flag for states where the HW flushes the TLB for us 105956d033fSLen Brown * and so we don't need cross-calls to keep it consistent. 106956d033fSLen Brown * If this flag is set, SW flushes the TLB, so even if the 107956d033fSLen Brown * HW doesn't do the flushing, this flag is safe to use. 108956d033fSLen Brown */ 109956d033fSLen Brown #define CPUIDLE_FLAG_TLB_FLUSHED 0x10000 110956d033fSLen Brown 111956d033fSLen Brown /* 112b1beab48SLen Brown * MWAIT takes an 8-bit "hint" in EAX "suggesting" 113b1beab48SLen Brown * the C-state (top nibble) and sub-state (bottom nibble) 114b1beab48SLen Brown * 0x00 means "MWAIT(C1)", 0x10 means "MWAIT(C2)" etc. 115b1beab48SLen Brown * 116b1beab48SLen Brown * We store the hint at the top of our "flags" for each state. 117b1beab48SLen Brown */ 118b1beab48SLen Brown #define flg2MWAIT(flags) (((flags) >> 24) & 0xFF) 119b1beab48SLen Brown #define MWAIT2flg(eax) ((eax & 0xFF) << 24) 120b1beab48SLen Brown 121b1beab48SLen Brown /* 12226717172SLen Brown * States are indexed by the cstate number, 12326717172SLen Brown * which is also the index into the MWAIT hint array. 12426717172SLen Brown * Thus C0 is a dummy. 12526717172SLen Brown */ 1269d046ccbSBartlomiej Zolnierkiewicz static struct cpuidle_state nehalem_cstates[] __initdata = { 127e022e7ebSLen Brown { 12815e123e5SThomas Renninger .name = "C1-NHM", 12926717172SLen Brown .desc = "MWAIT 0x00", 130b1beab48SLen Brown .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID, 13126717172SLen Brown .exit_latency = 3, 13226717172SLen Brown .target_residency = 6, 13326717172SLen Brown .enter = &intel_idle }, 134e022e7ebSLen Brown { 13532e95180SLen Brown .name = "C1E-NHM", 13632e95180SLen Brown .desc = "MWAIT 0x01", 13732e95180SLen Brown .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID, 13832e95180SLen Brown .exit_latency = 10, 13932e95180SLen Brown .target_residency = 20, 14032e95180SLen Brown .enter = &intel_idle }, 14132e95180SLen Brown { 14215e123e5SThomas Renninger .name = "C3-NHM", 14326717172SLen Brown .desc = "MWAIT 0x10", 144b1beab48SLen Brown .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 14526717172SLen Brown .exit_latency = 20, 14626717172SLen Brown .target_residency = 80, 14726717172SLen Brown .enter = &intel_idle }, 148e022e7ebSLen Brown { 14915e123e5SThomas Renninger .name = "C6-NHM", 15026717172SLen Brown .desc = "MWAIT 0x20", 151b1beab48SLen Brown .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 15226717172SLen Brown .exit_latency = 200, 15326717172SLen Brown .target_residency = 800, 15426717172SLen Brown .enter = &intel_idle }, 155e022e7ebSLen Brown { 156e022e7ebSLen Brown .enter = NULL } 15726717172SLen Brown }; 15826717172SLen Brown 1599d046ccbSBartlomiej Zolnierkiewicz static struct cpuidle_state snb_cstates[] __initdata = { 160e022e7ebSLen Brown { 16115e123e5SThomas Renninger .name = "C1-SNB", 162d13780d4SLen Brown .desc = "MWAIT 0x00", 163b1beab48SLen Brown .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID, 16432e95180SLen Brown .exit_latency = 2, 16532e95180SLen Brown .target_residency = 2, 16632e95180SLen Brown .enter = &intel_idle }, 16732e95180SLen Brown { 16832e95180SLen Brown .name = "C1E-SNB", 16932e95180SLen Brown .desc = "MWAIT 0x01", 17032e95180SLen Brown .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID, 17132e95180SLen Brown .exit_latency = 10, 17232e95180SLen Brown .target_residency = 20, 173d13780d4SLen Brown .enter = &intel_idle }, 174e022e7ebSLen Brown { 17515e123e5SThomas Renninger .name = "C3-SNB", 176d13780d4SLen Brown .desc = "MWAIT 0x10", 177b1beab48SLen Brown .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 178d13780d4SLen Brown .exit_latency = 80, 179ddbd550dSLen Brown .target_residency = 211, 180d13780d4SLen Brown .enter = &intel_idle }, 181e022e7ebSLen Brown { 18215e123e5SThomas Renninger .name = "C6-SNB", 183d13780d4SLen Brown .desc = "MWAIT 0x20", 184b1beab48SLen Brown .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 185d13780d4SLen Brown .exit_latency = 104, 186ddbd550dSLen Brown .target_residency = 345, 187d13780d4SLen Brown .enter = &intel_idle }, 188e022e7ebSLen Brown { 18915e123e5SThomas Renninger .name = "C7-SNB", 190d13780d4SLen Brown .desc = "MWAIT 0x30", 191b1beab48SLen Brown .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 192d13780d4SLen Brown .exit_latency = 109, 193ddbd550dSLen Brown .target_residency = 345, 194d13780d4SLen Brown .enter = &intel_idle }, 195e022e7ebSLen Brown { 196e022e7ebSLen Brown .enter = NULL } 197d13780d4SLen Brown }; 198d13780d4SLen Brown 1999d046ccbSBartlomiej Zolnierkiewicz static struct cpuidle_state ivb_cstates[] __initdata = { 200e022e7ebSLen Brown { 2016edab08cSLen Brown .name = "C1-IVB", 2026edab08cSLen Brown .desc = "MWAIT 0x00", 203b1beab48SLen Brown .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID, 2046edab08cSLen Brown .exit_latency = 1, 2056edab08cSLen Brown .target_residency = 1, 2066edab08cSLen Brown .enter = &intel_idle }, 207e022e7ebSLen Brown { 20832e95180SLen Brown .name = "C1E-IVB", 20932e95180SLen Brown .desc = "MWAIT 0x01", 21032e95180SLen Brown .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID, 21132e95180SLen Brown .exit_latency = 10, 21232e95180SLen Brown .target_residency = 20, 21332e95180SLen Brown .enter = &intel_idle }, 21432e95180SLen Brown { 2156edab08cSLen Brown .name = "C3-IVB", 2166edab08cSLen Brown .desc = "MWAIT 0x10", 217b1beab48SLen Brown .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 2186edab08cSLen Brown .exit_latency = 59, 2196edab08cSLen Brown .target_residency = 156, 2206edab08cSLen Brown .enter = &intel_idle }, 221e022e7ebSLen Brown { 2226edab08cSLen Brown .name = "C6-IVB", 2236edab08cSLen Brown .desc = "MWAIT 0x20", 224b1beab48SLen Brown .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 2256edab08cSLen Brown .exit_latency = 80, 2266edab08cSLen Brown .target_residency = 300, 2276edab08cSLen Brown .enter = &intel_idle }, 228e022e7ebSLen Brown { 2296edab08cSLen Brown .name = "C7-IVB", 2306edab08cSLen Brown .desc = "MWAIT 0x30", 231b1beab48SLen Brown .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 2326edab08cSLen Brown .exit_latency = 87, 2336edab08cSLen Brown .target_residency = 300, 2346edab08cSLen Brown .enter = &intel_idle }, 235e022e7ebSLen Brown { 236e022e7ebSLen Brown .enter = NULL } 2376edab08cSLen Brown }; 2386edab08cSLen Brown 2399d046ccbSBartlomiej Zolnierkiewicz static struct cpuidle_state hsw_cstates[] __initdata = { 240e022e7ebSLen Brown { 24185a4d2d4SLen Brown .name = "C1-HSW", 24285a4d2d4SLen Brown .desc = "MWAIT 0x00", 24385a4d2d4SLen Brown .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID, 24485a4d2d4SLen Brown .exit_latency = 2, 24585a4d2d4SLen Brown .target_residency = 2, 24685a4d2d4SLen Brown .enter = &intel_idle }, 247e022e7ebSLen Brown { 24832e95180SLen Brown .name = "C1E-HSW", 24932e95180SLen Brown .desc = "MWAIT 0x01", 25032e95180SLen Brown .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID, 25132e95180SLen Brown .exit_latency = 10, 25232e95180SLen Brown .target_residency = 20, 25332e95180SLen Brown .enter = &intel_idle }, 25432e95180SLen Brown { 25585a4d2d4SLen Brown .name = "C3-HSW", 25685a4d2d4SLen Brown .desc = "MWAIT 0x10", 25785a4d2d4SLen Brown .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 25885a4d2d4SLen Brown .exit_latency = 33, 25985a4d2d4SLen Brown .target_residency = 100, 26085a4d2d4SLen Brown .enter = &intel_idle }, 261e022e7ebSLen Brown { 26285a4d2d4SLen Brown .name = "C6-HSW", 26385a4d2d4SLen Brown .desc = "MWAIT 0x20", 26485a4d2d4SLen Brown .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 26585a4d2d4SLen Brown .exit_latency = 133, 26685a4d2d4SLen Brown .target_residency = 400, 26785a4d2d4SLen Brown .enter = &intel_idle }, 268e022e7ebSLen Brown { 26985a4d2d4SLen Brown .name = "C7s-HSW", 27085a4d2d4SLen Brown .desc = "MWAIT 0x32", 27185a4d2d4SLen Brown .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 27285a4d2d4SLen Brown .exit_latency = 166, 27385a4d2d4SLen Brown .target_residency = 500, 27485a4d2d4SLen Brown .enter = &intel_idle }, 275e022e7ebSLen Brown { 27686239cebSLen Brown .name = "C8-HSW", 27786239cebSLen Brown .desc = "MWAIT 0x40", 27886239cebSLen Brown .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 27986239cebSLen Brown .exit_latency = 300, 28086239cebSLen Brown .target_residency = 900, 28186239cebSLen Brown .enter = &intel_idle }, 28286239cebSLen Brown { 28386239cebSLen Brown .name = "C9-HSW", 28486239cebSLen Brown .desc = "MWAIT 0x50", 28586239cebSLen Brown .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 28686239cebSLen Brown .exit_latency = 600, 28786239cebSLen Brown .target_residency = 1800, 28886239cebSLen Brown .enter = &intel_idle }, 28986239cebSLen Brown { 29086239cebSLen Brown .name = "C10-HSW", 29186239cebSLen Brown .desc = "MWAIT 0x60", 29286239cebSLen Brown .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 29386239cebSLen Brown .exit_latency = 2600, 29486239cebSLen Brown .target_residency = 7700, 29586239cebSLen Brown .enter = &intel_idle }, 29686239cebSLen Brown { 297e022e7ebSLen Brown .enter = NULL } 29885a4d2d4SLen Brown }; 29985a4d2d4SLen Brown 3009d046ccbSBartlomiej Zolnierkiewicz static struct cpuidle_state atom_cstates[] __initdata = { 301e022e7ebSLen Brown { 30232e95180SLen Brown .name = "C1E-ATM", 30326717172SLen Brown .desc = "MWAIT 0x00", 304b1beab48SLen Brown .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID, 30532e95180SLen Brown .exit_latency = 10, 30632e95180SLen Brown .target_residency = 20, 30726717172SLen Brown .enter = &intel_idle }, 308e022e7ebSLen Brown { 30915e123e5SThomas Renninger .name = "C2-ATM", 31026717172SLen Brown .desc = "MWAIT 0x10", 311b1beab48SLen Brown .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID, 31226717172SLen Brown .exit_latency = 20, 31326717172SLen Brown .target_residency = 80, 31426717172SLen Brown .enter = &intel_idle }, 315e022e7ebSLen Brown { 31615e123e5SThomas Renninger .name = "C4-ATM", 31726717172SLen Brown .desc = "MWAIT 0x30", 318b1beab48SLen Brown .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 31926717172SLen Brown .exit_latency = 100, 32026717172SLen Brown .target_residency = 400, 32126717172SLen Brown .enter = &intel_idle }, 322e022e7ebSLen Brown { 32315e123e5SThomas Renninger .name = "C6-ATM", 3247fcca7d9SLen Brown .desc = "MWAIT 0x52", 325b1beab48SLen Brown .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 3267fcca7d9SLen Brown .exit_latency = 140, 3277fcca7d9SLen Brown .target_residency = 560, 3287fcca7d9SLen Brown .enter = &intel_idle }, 329e022e7ebSLen Brown { 330e022e7ebSLen Brown .enter = NULL } 33126717172SLen Brown }; 33222e580d0SBockholdt Arne static struct cpuidle_state avn_cstates[] __initdata = { 333fab04b22SLen Brown { 334fab04b22SLen Brown .name = "C1-AVN", 335fab04b22SLen Brown .desc = "MWAIT 0x00", 336fab04b22SLen Brown .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID, 337fab04b22SLen Brown .exit_latency = 2, 338fab04b22SLen Brown .target_residency = 2, 339fab04b22SLen Brown .enter = &intel_idle }, 340fab04b22SLen Brown { 341fab04b22SLen Brown .name = "C6-AVN", 342fab04b22SLen Brown .desc = "MWAIT 0x51", 34322e580d0SBockholdt Arne .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 344fab04b22SLen Brown .exit_latency = 15, 345fab04b22SLen Brown .target_residency = 45, 346fab04b22SLen Brown .enter = &intel_idle }, 347fab04b22SLen Brown }; 34826717172SLen Brown 34926717172SLen Brown /** 35026717172SLen Brown * intel_idle 35126717172SLen Brown * @dev: cpuidle_device 35246bcfad7SDeepthi Dharwar * @drv: cpuidle driver 353e978aa7dSDeepthi Dharwar * @index: index of cpuidle state 35426717172SLen Brown * 35563ff07beSYanmin Zhang * Must be called under local_irq_disable(). 35626717172SLen Brown */ 35746bcfad7SDeepthi Dharwar static int intel_idle(struct cpuidle_device *dev, 35846bcfad7SDeepthi Dharwar struct cpuidle_driver *drv, int index) 35926717172SLen Brown { 36026717172SLen Brown unsigned long ecx = 1; /* break on interrupt flag */ 36146bcfad7SDeepthi Dharwar struct cpuidle_state *state = &drv->states[index]; 362b1beab48SLen Brown unsigned long eax = flg2MWAIT(state->flags); 36326717172SLen Brown unsigned int cstate; 36426717172SLen Brown int cpu = smp_processor_id(); 36526717172SLen Brown 36626717172SLen Brown cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1; 36726717172SLen Brown 3686110a1f4SSuresh Siddha /* 369c8381cc3SLen Brown * leave_mm() to avoid costly and often unnecessary wakeups 370c8381cc3SLen Brown * for flushing the user TLB's associated with the active mm. 3716110a1f4SSuresh Siddha */ 372c8381cc3SLen Brown if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED) 3736110a1f4SSuresh Siddha leave_mm(cpu); 3746110a1f4SSuresh Siddha 37526717172SLen Brown if (!(lapic_timer_reliable_states & (1 << (cstate)))) 37626717172SLen Brown clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu); 37726717172SLen Brown 37816824255SPeter Zijlstra mwait_idle_with_hints(eax, ecx); 37926717172SLen Brown 38026717172SLen Brown if (!(lapic_timer_reliable_states & (1 << (cstate)))) 38126717172SLen Brown clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu); 38226717172SLen Brown 383e978aa7dSDeepthi Dharwar return index; 38426717172SLen Brown } 38526717172SLen Brown 3862a2d31c8SShaohua Li static void __setup_broadcast_timer(void *arg) 3872a2d31c8SShaohua Li { 3882a2d31c8SShaohua Li unsigned long reason = (unsigned long)arg; 3892a2d31c8SShaohua Li int cpu = smp_processor_id(); 3902a2d31c8SShaohua Li 3912a2d31c8SShaohua Li reason = reason ? 3922a2d31c8SShaohua Li CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF; 3932a2d31c8SShaohua Li 3942a2d31c8SShaohua Li clockevents_notify(reason, &cpu); 3952a2d31c8SShaohua Li } 3962a2d31c8SShaohua Li 39725ac7761SDaniel Lezcano static int cpu_hotplug_notify(struct notifier_block *n, 3982a2d31c8SShaohua Li unsigned long action, void *hcpu) 3992a2d31c8SShaohua Li { 4002a2d31c8SShaohua Li int hotcpu = (unsigned long)hcpu; 40125ac7761SDaniel Lezcano struct cpuidle_device *dev; 4022a2d31c8SShaohua Li 403e2401453SPrarit Bhargava switch (action & ~CPU_TASKS_FROZEN) { 4042a2d31c8SShaohua Li case CPU_ONLINE: 40525ac7761SDaniel Lezcano 40625ac7761SDaniel Lezcano if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE) 4072a2d31c8SShaohua Li smp_call_function_single(hotcpu, __setup_broadcast_timer, 4082a2d31c8SShaohua Li (void *)true, 1); 40925ac7761SDaniel Lezcano 41025ac7761SDaniel Lezcano /* 41125ac7761SDaniel Lezcano * Some systems can hotplug a cpu at runtime after 41225ac7761SDaniel Lezcano * the kernel has booted, we have to initialize the 41325ac7761SDaniel Lezcano * driver in this case 41425ac7761SDaniel Lezcano */ 41525ac7761SDaniel Lezcano dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu); 41625ac7761SDaniel Lezcano if (!dev->registered) 41725ac7761SDaniel Lezcano intel_idle_cpu_init(hotcpu); 41825ac7761SDaniel Lezcano 4192a2d31c8SShaohua Li break; 4202a2d31c8SShaohua Li } 4212a2d31c8SShaohua Li return NOTIFY_OK; 4222a2d31c8SShaohua Li } 4232a2d31c8SShaohua Li 42425ac7761SDaniel Lezcano static struct notifier_block cpu_hotplug_notifier = { 42525ac7761SDaniel Lezcano .notifier_call = cpu_hotplug_notify, 4262a2d31c8SShaohua Li }; 4272a2d31c8SShaohua Li 42814796fcaSLen Brown static void auto_demotion_disable(void *dummy) 42914796fcaSLen Brown { 43014796fcaSLen Brown unsigned long long msr_bits; 43114796fcaSLen Brown 43214796fcaSLen Brown rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits); 433b66b8b9aSAndi Kleen msr_bits &= ~(icpu->auto_demotion_disable_flags); 43414796fcaSLen Brown wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits); 43514796fcaSLen Brown } 43632e95180SLen Brown static void c1e_promotion_disable(void *dummy) 43732e95180SLen Brown { 43832e95180SLen Brown unsigned long long msr_bits; 43932e95180SLen Brown 44032e95180SLen Brown rdmsrl(MSR_IA32_POWER_CTL, msr_bits); 44132e95180SLen Brown msr_bits &= ~0x2; 44232e95180SLen Brown wrmsrl(MSR_IA32_POWER_CTL, msr_bits); 44332e95180SLen Brown } 44414796fcaSLen Brown 445b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_nehalem = { 446b66b8b9aSAndi Kleen .state_table = nehalem_cstates, 447b66b8b9aSAndi Kleen .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE, 44832e95180SLen Brown .disable_promotion_to_c1e = true, 449b66b8b9aSAndi Kleen }; 450b66b8b9aSAndi Kleen 451b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_atom = { 452b66b8b9aSAndi Kleen .state_table = atom_cstates, 453b66b8b9aSAndi Kleen }; 454b66b8b9aSAndi Kleen 455b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_lincroft = { 456b66b8b9aSAndi Kleen .state_table = atom_cstates, 457b66b8b9aSAndi Kleen .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE, 458b66b8b9aSAndi Kleen }; 459b66b8b9aSAndi Kleen 460b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_snb = { 461b66b8b9aSAndi Kleen .state_table = snb_cstates, 46232e95180SLen Brown .disable_promotion_to_c1e = true, 463b66b8b9aSAndi Kleen }; 464b66b8b9aSAndi Kleen 4656edab08cSLen Brown static const struct idle_cpu idle_cpu_ivb = { 4666edab08cSLen Brown .state_table = ivb_cstates, 46732e95180SLen Brown .disable_promotion_to_c1e = true, 4686edab08cSLen Brown }; 4696edab08cSLen Brown 47085a4d2d4SLen Brown static const struct idle_cpu idle_cpu_hsw = { 47185a4d2d4SLen Brown .state_table = hsw_cstates, 47232e95180SLen Brown .disable_promotion_to_c1e = true, 47385a4d2d4SLen Brown }; 47485a4d2d4SLen Brown 475fab04b22SLen Brown static const struct idle_cpu idle_cpu_avn = { 476fab04b22SLen Brown .state_table = avn_cstates, 477fab04b22SLen Brown .disable_promotion_to_c1e = true, 478fab04b22SLen Brown }; 479fab04b22SLen Brown 480b66b8b9aSAndi Kleen #define ICPU(model, cpu) \ 481b66b8b9aSAndi Kleen { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu } 482b66b8b9aSAndi Kleen 483b66b8b9aSAndi Kleen static const struct x86_cpu_id intel_idle_ids[] = { 484b66b8b9aSAndi Kleen ICPU(0x1a, idle_cpu_nehalem), 485b66b8b9aSAndi Kleen ICPU(0x1e, idle_cpu_nehalem), 486b66b8b9aSAndi Kleen ICPU(0x1f, idle_cpu_nehalem), 4878bf11938SBen Hutchings ICPU(0x25, idle_cpu_nehalem), 4888bf11938SBen Hutchings ICPU(0x2c, idle_cpu_nehalem), 4898bf11938SBen Hutchings ICPU(0x2e, idle_cpu_nehalem), 490b66b8b9aSAndi Kleen ICPU(0x1c, idle_cpu_atom), 491b66b8b9aSAndi Kleen ICPU(0x26, idle_cpu_lincroft), 4928bf11938SBen Hutchings ICPU(0x2f, idle_cpu_nehalem), 493b66b8b9aSAndi Kleen ICPU(0x2a, idle_cpu_snb), 494b66b8b9aSAndi Kleen ICPU(0x2d, idle_cpu_snb), 4956edab08cSLen Brown ICPU(0x3a, idle_cpu_ivb), 49623795e58SLen Brown ICPU(0x3e, idle_cpu_ivb), 49785a4d2d4SLen Brown ICPU(0x3c, idle_cpu_hsw), 49885a4d2d4SLen Brown ICPU(0x3f, idle_cpu_hsw), 49985a4d2d4SLen Brown ICPU(0x45, idle_cpu_hsw), 5000b15841bSLen Brown ICPU(0x46, idle_cpu_hsw), 501fab04b22SLen Brown ICPU(0x4D, idle_cpu_avn), 502b66b8b9aSAndi Kleen {} 503b66b8b9aSAndi Kleen }; 504b66b8b9aSAndi Kleen MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids); 505b66b8b9aSAndi Kleen 50626717172SLen Brown /* 50726717172SLen Brown * intel_idle_probe() 50826717172SLen Brown */ 50900f3e755SBartlomiej Zolnierkiewicz static int __init intel_idle_probe(void) 51026717172SLen Brown { 511c4236282SLen Brown unsigned int eax, ebx, ecx; 512b66b8b9aSAndi Kleen const struct x86_cpu_id *id; 51326717172SLen Brown 51426717172SLen Brown if (max_cstate == 0) { 51526717172SLen Brown pr_debug(PREFIX "disabled\n"); 51626717172SLen Brown return -EPERM; 51726717172SLen Brown } 51826717172SLen Brown 519b66b8b9aSAndi Kleen id = x86_match_cpu(intel_idle_ids); 520b66b8b9aSAndi Kleen if (!id) { 521b66b8b9aSAndi Kleen if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && 522b66b8b9aSAndi Kleen boot_cpu_data.x86 == 6) 523b66b8b9aSAndi Kleen pr_debug(PREFIX "does not run on family %d model %d\n", 524b66b8b9aSAndi Kleen boot_cpu_data.x86, boot_cpu_data.x86_model); 52526717172SLen Brown return -ENODEV; 526b66b8b9aSAndi Kleen } 52726717172SLen Brown 52826717172SLen Brown if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF) 52926717172SLen Brown return -ENODEV; 53026717172SLen Brown 531c4236282SLen Brown cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates); 53226717172SLen Brown 53326717172SLen Brown if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) || 5345c2a9f06SThomas Renninger !(ecx & CPUID5_ECX_INTERRUPT_BREAK) || 5355c2a9f06SThomas Renninger !mwait_substates) 53626717172SLen Brown return -ENODEV; 53726717172SLen Brown 538c4236282SLen Brown pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates); 53926717172SLen Brown 540b66b8b9aSAndi Kleen icpu = (const struct idle_cpu *)id->driver_data; 541b66b8b9aSAndi Kleen cpuidle_state_table = icpu->state_table; 54226717172SLen Brown 54356b9aea3SLen Brown if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */ 5442a2d31c8SShaohua Li lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE; 54525ac7761SDaniel Lezcano else 54639a74fdeSShaohua Li on_each_cpu(__setup_broadcast_timer, (void *)true, 1); 54725ac7761SDaniel Lezcano 54826717172SLen Brown pr_debug(PREFIX "v" INTEL_IDLE_VERSION 54926717172SLen Brown " model 0x%X\n", boot_cpu_data.x86_model); 55026717172SLen Brown 55126717172SLen Brown pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n", 55226717172SLen Brown lapic_timer_reliable_states); 55326717172SLen Brown return 0; 55426717172SLen Brown } 55526717172SLen Brown 55626717172SLen Brown /* 55726717172SLen Brown * intel_idle_cpuidle_devices_uninit() 55826717172SLen Brown * unregister, free cpuidle_devices 55926717172SLen Brown */ 56026717172SLen Brown static void intel_idle_cpuidle_devices_uninit(void) 56126717172SLen Brown { 56226717172SLen Brown int i; 56326717172SLen Brown struct cpuidle_device *dev; 56426717172SLen Brown 56526717172SLen Brown for_each_online_cpu(i) { 56626717172SLen Brown dev = per_cpu_ptr(intel_idle_cpuidle_devices, i); 56726717172SLen Brown cpuidle_unregister_device(dev); 56826717172SLen Brown } 56926717172SLen Brown 57026717172SLen Brown free_percpu(intel_idle_cpuidle_devices); 57126717172SLen Brown return; 57226717172SLen Brown } 57326717172SLen Brown /* 57446bcfad7SDeepthi Dharwar * intel_idle_cpuidle_driver_init() 57546bcfad7SDeepthi Dharwar * allocate, initialize cpuidle_states 57646bcfad7SDeepthi Dharwar */ 57700f3e755SBartlomiej Zolnierkiewicz static int __init intel_idle_cpuidle_driver_init(void) 57846bcfad7SDeepthi Dharwar { 57946bcfad7SDeepthi Dharwar int cstate; 58046bcfad7SDeepthi Dharwar struct cpuidle_driver *drv = &intel_idle_driver; 58146bcfad7SDeepthi Dharwar 58246bcfad7SDeepthi Dharwar drv->state_count = 1; 58346bcfad7SDeepthi Dharwar 584e022e7ebSLen Brown for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) { 585e022e7ebSLen Brown int num_substates, mwait_hint, mwait_cstate, mwait_substate; 58646bcfad7SDeepthi Dharwar 587e022e7ebSLen Brown if (cpuidle_state_table[cstate].enter == NULL) 588e022e7ebSLen Brown break; 589e022e7ebSLen Brown 590e022e7ebSLen Brown if (cstate + 1 > max_cstate) { 59146bcfad7SDeepthi Dharwar printk(PREFIX "max_cstate %d reached\n", 59246bcfad7SDeepthi Dharwar max_cstate); 59346bcfad7SDeepthi Dharwar break; 59446bcfad7SDeepthi Dharwar } 59546bcfad7SDeepthi Dharwar 596e022e7ebSLen Brown mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags); 597e022e7ebSLen Brown mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint); 598e022e7ebSLen Brown mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint); 59946bcfad7SDeepthi Dharwar 600e022e7ebSLen Brown /* does the state exist in CPUID.MWAIT? */ 601e022e7ebSLen Brown num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4)) 602e022e7ebSLen Brown & MWAIT_SUBSTATE_MASK; 603e022e7ebSLen Brown 604e022e7ebSLen Brown /* if sub-state in table is not enumerated by CPUID */ 605e022e7ebSLen Brown if ((mwait_substate + 1) > num_substates) 606e022e7ebSLen Brown continue; 607e022e7ebSLen Brown 608e022e7ebSLen Brown if (((mwait_cstate + 1) > 2) && 60946bcfad7SDeepthi Dharwar !boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) 61046bcfad7SDeepthi Dharwar mark_tsc_unstable("TSC halts in idle" 61146bcfad7SDeepthi Dharwar " states deeper than C2"); 61246bcfad7SDeepthi Dharwar 61346bcfad7SDeepthi Dharwar drv->states[drv->state_count] = /* structure copy */ 61446bcfad7SDeepthi Dharwar cpuidle_state_table[cstate]; 61546bcfad7SDeepthi Dharwar 61646bcfad7SDeepthi Dharwar drv->state_count += 1; 61746bcfad7SDeepthi Dharwar } 61846bcfad7SDeepthi Dharwar 619b66b8b9aSAndi Kleen if (icpu->auto_demotion_disable_flags) 62039a74fdeSShaohua Li on_each_cpu(auto_demotion_disable, NULL, 1); 62146bcfad7SDeepthi Dharwar 62232e95180SLen Brown if (icpu->disable_promotion_to_c1e) /* each-cpu is redundant */ 62332e95180SLen Brown on_each_cpu(c1e_promotion_disable, NULL, 1); 62432e95180SLen Brown 62546bcfad7SDeepthi Dharwar return 0; 62646bcfad7SDeepthi Dharwar } 62746bcfad7SDeepthi Dharwar 62846bcfad7SDeepthi Dharwar 62946bcfad7SDeepthi Dharwar /* 63065b7f839SThomas Renninger * intel_idle_cpu_init() 63126717172SLen Brown * allocate, initialize, register cpuidle_devices 63265b7f839SThomas Renninger * @cpu: cpu/core to initialize 63326717172SLen Brown */ 63425ac7761SDaniel Lezcano static int intel_idle_cpu_init(int cpu) 63526717172SLen Brown { 63665b7f839SThomas Renninger int cstate; 63726717172SLen Brown struct cpuidle_device *dev; 63826717172SLen Brown 63965b7f839SThomas Renninger dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu); 64026717172SLen Brown 64126717172SLen Brown dev->state_count = 1; 64226717172SLen Brown 643e022e7ebSLen Brown for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) { 644e022e7ebSLen Brown int num_substates, mwait_hint, mwait_cstate, mwait_substate; 64526717172SLen Brown 646e022e7ebSLen Brown if (cpuidle_state_table[cstate].enter == NULL) 647eba682a5SBartlomiej Zolnierkiewicz break; 648e022e7ebSLen Brown 649e022e7ebSLen Brown if (cstate + 1 > max_cstate) { 650dc716e96SMarcos Paulo de Souza printk(PREFIX "max_cstate %d reached\n", max_cstate); 65126717172SLen Brown break; 65226717172SLen Brown } 65326717172SLen Brown 654e022e7ebSLen Brown mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags); 655e022e7ebSLen Brown mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint); 656e022e7ebSLen Brown mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint); 657e022e7ebSLen Brown 65826717172SLen Brown /* does the state exist in CPUID.MWAIT? */ 659e022e7ebSLen Brown num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4)) 66026717172SLen Brown & MWAIT_SUBSTATE_MASK; 661e022e7ebSLen Brown 662e022e7ebSLen Brown /* if sub-state in table is not enumerated by CPUID */ 663e022e7ebSLen Brown if ((mwait_substate + 1) > num_substates) 66426717172SLen Brown continue; 66526717172SLen Brown 66626717172SLen Brown dev->state_count += 1; 66726717172SLen Brown } 668dc716e96SMarcos Paulo de Souza 66965b7f839SThomas Renninger dev->cpu = cpu; 67026717172SLen Brown 67126717172SLen Brown if (cpuidle_register_device(dev)) { 67265b7f839SThomas Renninger pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu); 67326717172SLen Brown intel_idle_cpuidle_devices_uninit(); 67426717172SLen Brown return -EIO; 67526717172SLen Brown } 67665b7f839SThomas Renninger 677b66b8b9aSAndi Kleen if (icpu->auto_demotion_disable_flags) 67865b7f839SThomas Renninger smp_call_function_single(cpu, auto_demotion_disable, NULL, 1); 67926717172SLen Brown 68026717172SLen Brown return 0; 68126717172SLen Brown } 68226717172SLen Brown 68326717172SLen Brown static int __init intel_idle_init(void) 68426717172SLen Brown { 68565b7f839SThomas Renninger int retval, i; 68626717172SLen Brown 687d1896049SThomas Renninger /* Do not load intel_idle at all for now if idle= is passed */ 688d1896049SThomas Renninger if (boot_option_idle_override != IDLE_NO_OVERRIDE) 689d1896049SThomas Renninger return -ENODEV; 690d1896049SThomas Renninger 69126717172SLen Brown retval = intel_idle_probe(); 69226717172SLen Brown if (retval) 69326717172SLen Brown return retval; 69426717172SLen Brown 69546bcfad7SDeepthi Dharwar intel_idle_cpuidle_driver_init(); 69626717172SLen Brown retval = cpuidle_register_driver(&intel_idle_driver); 69726717172SLen Brown if (retval) { 6983735d524SKonrad Rzeszutek Wilk struct cpuidle_driver *drv = cpuidle_get_driver(); 69926717172SLen Brown printk(KERN_DEBUG PREFIX "intel_idle yielding to %s", 7003735d524SKonrad Rzeszutek Wilk drv ? drv->name : "none"); 70126717172SLen Brown return retval; 70226717172SLen Brown } 70326717172SLen Brown 70465b7f839SThomas Renninger intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device); 70565b7f839SThomas Renninger if (intel_idle_cpuidle_devices == NULL) 70665b7f839SThomas Renninger return -ENOMEM; 70765b7f839SThomas Renninger 70865b7f839SThomas Renninger for_each_online_cpu(i) { 70965b7f839SThomas Renninger retval = intel_idle_cpu_init(i); 71026717172SLen Brown if (retval) { 71126717172SLen Brown cpuidle_unregister_driver(&intel_idle_driver); 71226717172SLen Brown return retval; 71326717172SLen Brown } 71465b7f839SThomas Renninger } 7156f8c2e79SKonrad Rzeszutek Wilk register_cpu_notifier(&cpu_hotplug_notifier); 71626717172SLen Brown 71726717172SLen Brown return 0; 71826717172SLen Brown } 71926717172SLen Brown 72026717172SLen Brown static void __exit intel_idle_exit(void) 72126717172SLen Brown { 72226717172SLen Brown intel_idle_cpuidle_devices_uninit(); 72326717172SLen Brown cpuidle_unregister_driver(&intel_idle_driver); 72426717172SLen Brown 72525ac7761SDaniel Lezcano 72625ac7761SDaniel Lezcano if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE) 72739a74fdeSShaohua Li on_each_cpu(__setup_broadcast_timer, (void *)false, 1); 72825ac7761SDaniel Lezcano unregister_cpu_notifier(&cpu_hotplug_notifier); 7292a2d31c8SShaohua Li 73026717172SLen Brown return; 73126717172SLen Brown } 73226717172SLen Brown 73326717172SLen Brown module_init(intel_idle_init); 73426717172SLen Brown module_exit(intel_idle_exit); 73526717172SLen Brown 73626717172SLen Brown module_param(max_cstate, int, 0444); 73726717172SLen Brown 73826717172SLen Brown MODULE_AUTHOR("Len Brown <len.brown@intel.com>"); 73926717172SLen Brown MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION); 74026717172SLen Brown MODULE_LICENSE("GPL"); 741