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 */ 126ba0dc81eSJiang Liu static struct cpuidle_state nehalem_cstates[] = { 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 159ba0dc81eSJiang Liu static struct cpuidle_state snb_cstates[] = { 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 199ba0dc81eSJiang Liu static struct cpuidle_state ivb_cstates[] = { 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 239ba0dc81eSJiang Liu static struct cpuidle_state hsw_cstates[] = { 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 300ba0dc81eSJiang Liu static struct cpuidle_state atom_cstates[] = { 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 }; 33288390996SJiang Liu static struct cpuidle_state avn_cstates[] = { 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 }, 34788390996SJiang Liu { 34888390996SJiang Liu .enter = NULL } 349fab04b22SLen Brown }; 35026717172SLen Brown 35126717172SLen Brown /** 35226717172SLen Brown * intel_idle 35326717172SLen Brown * @dev: cpuidle_device 35446bcfad7SDeepthi Dharwar * @drv: cpuidle driver 355e978aa7dSDeepthi Dharwar * @index: index of cpuidle state 35626717172SLen Brown * 35763ff07beSYanmin Zhang * Must be called under local_irq_disable(). 35826717172SLen Brown */ 35946bcfad7SDeepthi Dharwar static int intel_idle(struct cpuidle_device *dev, 36046bcfad7SDeepthi Dharwar struct cpuidle_driver *drv, int index) 36126717172SLen Brown { 36226717172SLen Brown unsigned long ecx = 1; /* break on interrupt flag */ 36346bcfad7SDeepthi Dharwar struct cpuidle_state *state = &drv->states[index]; 364b1beab48SLen Brown unsigned long eax = flg2MWAIT(state->flags); 36526717172SLen Brown unsigned int cstate; 36626717172SLen Brown int cpu = smp_processor_id(); 36726717172SLen Brown 36826717172SLen Brown cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1; 36926717172SLen Brown 3706110a1f4SSuresh Siddha /* 371c8381cc3SLen Brown * leave_mm() to avoid costly and often unnecessary wakeups 372c8381cc3SLen Brown * for flushing the user TLB's associated with the active mm. 3736110a1f4SSuresh Siddha */ 374c8381cc3SLen Brown if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED) 3756110a1f4SSuresh Siddha leave_mm(cpu); 3766110a1f4SSuresh Siddha 37726717172SLen Brown if (!(lapic_timer_reliable_states & (1 << (cstate)))) 37826717172SLen Brown clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu); 37926717172SLen Brown 38016824255SPeter Zijlstra mwait_idle_with_hints(eax, ecx); 38126717172SLen Brown 38226717172SLen Brown if (!(lapic_timer_reliable_states & (1 << (cstate)))) 38326717172SLen Brown clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu); 38426717172SLen Brown 385e978aa7dSDeepthi Dharwar return index; 38626717172SLen Brown } 38726717172SLen Brown 3882a2d31c8SShaohua Li static void __setup_broadcast_timer(void *arg) 3892a2d31c8SShaohua Li { 3902a2d31c8SShaohua Li unsigned long reason = (unsigned long)arg; 3912a2d31c8SShaohua Li int cpu = smp_processor_id(); 3922a2d31c8SShaohua Li 3932a2d31c8SShaohua Li reason = reason ? 3942a2d31c8SShaohua Li CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF; 3952a2d31c8SShaohua Li 3962a2d31c8SShaohua Li clockevents_notify(reason, &cpu); 3972a2d31c8SShaohua Li } 3982a2d31c8SShaohua Li 39925ac7761SDaniel Lezcano static int cpu_hotplug_notify(struct notifier_block *n, 4002a2d31c8SShaohua Li unsigned long action, void *hcpu) 4012a2d31c8SShaohua Li { 4022a2d31c8SShaohua Li int hotcpu = (unsigned long)hcpu; 40325ac7761SDaniel Lezcano struct cpuidle_device *dev; 4042a2d31c8SShaohua Li 405e2401453SPrarit Bhargava switch (action & ~CPU_TASKS_FROZEN) { 4062a2d31c8SShaohua Li case CPU_ONLINE: 40725ac7761SDaniel Lezcano 40825ac7761SDaniel Lezcano if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE) 4092a2d31c8SShaohua Li smp_call_function_single(hotcpu, __setup_broadcast_timer, 4102a2d31c8SShaohua Li (void *)true, 1); 41125ac7761SDaniel Lezcano 41225ac7761SDaniel Lezcano /* 41325ac7761SDaniel Lezcano * Some systems can hotplug a cpu at runtime after 41425ac7761SDaniel Lezcano * the kernel has booted, we have to initialize the 41525ac7761SDaniel Lezcano * driver in this case 41625ac7761SDaniel Lezcano */ 41725ac7761SDaniel Lezcano dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu); 41825ac7761SDaniel Lezcano if (!dev->registered) 41925ac7761SDaniel Lezcano intel_idle_cpu_init(hotcpu); 42025ac7761SDaniel Lezcano 4212a2d31c8SShaohua Li break; 4222a2d31c8SShaohua Li } 4232a2d31c8SShaohua Li return NOTIFY_OK; 4242a2d31c8SShaohua Li } 4252a2d31c8SShaohua Li 42625ac7761SDaniel Lezcano static struct notifier_block cpu_hotplug_notifier = { 42725ac7761SDaniel Lezcano .notifier_call = cpu_hotplug_notify, 4282a2d31c8SShaohua Li }; 4292a2d31c8SShaohua Li 43014796fcaSLen Brown static void auto_demotion_disable(void *dummy) 43114796fcaSLen Brown { 43214796fcaSLen Brown unsigned long long msr_bits; 43314796fcaSLen Brown 43414796fcaSLen Brown rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits); 435b66b8b9aSAndi Kleen msr_bits &= ~(icpu->auto_demotion_disable_flags); 43614796fcaSLen Brown wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits); 43714796fcaSLen Brown } 43832e95180SLen Brown static void c1e_promotion_disable(void *dummy) 43932e95180SLen Brown { 44032e95180SLen Brown unsigned long long msr_bits; 44132e95180SLen Brown 44232e95180SLen Brown rdmsrl(MSR_IA32_POWER_CTL, msr_bits); 44332e95180SLen Brown msr_bits &= ~0x2; 44432e95180SLen Brown wrmsrl(MSR_IA32_POWER_CTL, msr_bits); 44532e95180SLen Brown } 44614796fcaSLen Brown 447b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_nehalem = { 448b66b8b9aSAndi Kleen .state_table = nehalem_cstates, 449b66b8b9aSAndi Kleen .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE, 45032e95180SLen Brown .disable_promotion_to_c1e = true, 451b66b8b9aSAndi Kleen }; 452b66b8b9aSAndi Kleen 453b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_atom = { 454b66b8b9aSAndi Kleen .state_table = atom_cstates, 455b66b8b9aSAndi Kleen }; 456b66b8b9aSAndi Kleen 457b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_lincroft = { 458b66b8b9aSAndi Kleen .state_table = atom_cstates, 459b66b8b9aSAndi Kleen .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE, 460b66b8b9aSAndi Kleen }; 461b66b8b9aSAndi Kleen 462b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_snb = { 463b66b8b9aSAndi Kleen .state_table = snb_cstates, 46432e95180SLen Brown .disable_promotion_to_c1e = true, 465b66b8b9aSAndi Kleen }; 466b66b8b9aSAndi Kleen 4676edab08cSLen Brown static const struct idle_cpu idle_cpu_ivb = { 4686edab08cSLen Brown .state_table = ivb_cstates, 46932e95180SLen Brown .disable_promotion_to_c1e = true, 4706edab08cSLen Brown }; 4716edab08cSLen Brown 47285a4d2d4SLen Brown static const struct idle_cpu idle_cpu_hsw = { 47385a4d2d4SLen Brown .state_table = hsw_cstates, 47432e95180SLen Brown .disable_promotion_to_c1e = true, 47585a4d2d4SLen Brown }; 47685a4d2d4SLen Brown 477fab04b22SLen Brown static const struct idle_cpu idle_cpu_avn = { 478fab04b22SLen Brown .state_table = avn_cstates, 479fab04b22SLen Brown .disable_promotion_to_c1e = true, 480fab04b22SLen Brown }; 481fab04b22SLen Brown 482b66b8b9aSAndi Kleen #define ICPU(model, cpu) \ 483b66b8b9aSAndi Kleen { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu } 484b66b8b9aSAndi Kleen 485b66b8b9aSAndi Kleen static const struct x86_cpu_id intel_idle_ids[] = { 486b66b8b9aSAndi Kleen ICPU(0x1a, idle_cpu_nehalem), 487b66b8b9aSAndi Kleen ICPU(0x1e, idle_cpu_nehalem), 488b66b8b9aSAndi Kleen ICPU(0x1f, idle_cpu_nehalem), 4898bf11938SBen Hutchings ICPU(0x25, idle_cpu_nehalem), 4908bf11938SBen Hutchings ICPU(0x2c, idle_cpu_nehalem), 4918bf11938SBen Hutchings ICPU(0x2e, idle_cpu_nehalem), 492b66b8b9aSAndi Kleen ICPU(0x1c, idle_cpu_atom), 493b66b8b9aSAndi Kleen ICPU(0x26, idle_cpu_lincroft), 4948bf11938SBen Hutchings ICPU(0x2f, idle_cpu_nehalem), 495b66b8b9aSAndi Kleen ICPU(0x2a, idle_cpu_snb), 496b66b8b9aSAndi Kleen ICPU(0x2d, idle_cpu_snb), 4976edab08cSLen Brown ICPU(0x3a, idle_cpu_ivb), 49823795e58SLen Brown ICPU(0x3e, idle_cpu_ivb), 49985a4d2d4SLen Brown ICPU(0x3c, idle_cpu_hsw), 50085a4d2d4SLen Brown ICPU(0x3f, idle_cpu_hsw), 50185a4d2d4SLen Brown ICPU(0x45, idle_cpu_hsw), 5020b15841bSLen Brown ICPU(0x46, idle_cpu_hsw), 503fab04b22SLen Brown ICPU(0x4D, idle_cpu_avn), 504b66b8b9aSAndi Kleen {} 505b66b8b9aSAndi Kleen }; 506b66b8b9aSAndi Kleen MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids); 507b66b8b9aSAndi Kleen 50826717172SLen Brown /* 50926717172SLen Brown * intel_idle_probe() 51026717172SLen Brown */ 51100f3e755SBartlomiej Zolnierkiewicz static int __init intel_idle_probe(void) 51226717172SLen Brown { 513c4236282SLen Brown unsigned int eax, ebx, ecx; 514b66b8b9aSAndi Kleen const struct x86_cpu_id *id; 51526717172SLen Brown 51626717172SLen Brown if (max_cstate == 0) { 51726717172SLen Brown pr_debug(PREFIX "disabled\n"); 51826717172SLen Brown return -EPERM; 51926717172SLen Brown } 52026717172SLen Brown 521b66b8b9aSAndi Kleen id = x86_match_cpu(intel_idle_ids); 522b66b8b9aSAndi Kleen if (!id) { 523b66b8b9aSAndi Kleen if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && 524b66b8b9aSAndi Kleen boot_cpu_data.x86 == 6) 525b66b8b9aSAndi Kleen pr_debug(PREFIX "does not run on family %d model %d\n", 526b66b8b9aSAndi Kleen boot_cpu_data.x86, boot_cpu_data.x86_model); 52726717172SLen Brown return -ENODEV; 528b66b8b9aSAndi Kleen } 52926717172SLen Brown 53026717172SLen Brown if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF) 53126717172SLen Brown return -ENODEV; 53226717172SLen Brown 533c4236282SLen Brown cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates); 53426717172SLen Brown 53526717172SLen Brown if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) || 5365c2a9f06SThomas Renninger !(ecx & CPUID5_ECX_INTERRUPT_BREAK) || 5375c2a9f06SThomas Renninger !mwait_substates) 53826717172SLen Brown return -ENODEV; 53926717172SLen Brown 540c4236282SLen Brown pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates); 54126717172SLen Brown 542b66b8b9aSAndi Kleen icpu = (const struct idle_cpu *)id->driver_data; 543b66b8b9aSAndi Kleen cpuidle_state_table = icpu->state_table; 54426717172SLen Brown 54556b9aea3SLen Brown if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */ 5462a2d31c8SShaohua Li lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE; 54725ac7761SDaniel Lezcano else 54839a74fdeSShaohua Li on_each_cpu(__setup_broadcast_timer, (void *)true, 1); 54925ac7761SDaniel Lezcano 55026717172SLen Brown pr_debug(PREFIX "v" INTEL_IDLE_VERSION 55126717172SLen Brown " model 0x%X\n", boot_cpu_data.x86_model); 55226717172SLen Brown 55326717172SLen Brown pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n", 55426717172SLen Brown lapic_timer_reliable_states); 55526717172SLen Brown return 0; 55626717172SLen Brown } 55726717172SLen Brown 55826717172SLen Brown /* 55926717172SLen Brown * intel_idle_cpuidle_devices_uninit() 56026717172SLen Brown * unregister, free cpuidle_devices 56126717172SLen Brown */ 56226717172SLen Brown static void intel_idle_cpuidle_devices_uninit(void) 56326717172SLen Brown { 56426717172SLen Brown int i; 56526717172SLen Brown struct cpuidle_device *dev; 56626717172SLen Brown 56726717172SLen Brown for_each_online_cpu(i) { 56826717172SLen Brown dev = per_cpu_ptr(intel_idle_cpuidle_devices, i); 56926717172SLen Brown cpuidle_unregister_device(dev); 57026717172SLen Brown } 57126717172SLen Brown 57226717172SLen Brown free_percpu(intel_idle_cpuidle_devices); 57326717172SLen Brown return; 57426717172SLen Brown } 57526717172SLen Brown /* 57646bcfad7SDeepthi Dharwar * intel_idle_cpuidle_driver_init() 57746bcfad7SDeepthi Dharwar * allocate, initialize cpuidle_states 57846bcfad7SDeepthi Dharwar */ 57900f3e755SBartlomiej Zolnierkiewicz static int __init intel_idle_cpuidle_driver_init(void) 58046bcfad7SDeepthi Dharwar { 58146bcfad7SDeepthi Dharwar int cstate; 58246bcfad7SDeepthi Dharwar struct cpuidle_driver *drv = &intel_idle_driver; 58346bcfad7SDeepthi Dharwar 58446bcfad7SDeepthi Dharwar drv->state_count = 1; 58546bcfad7SDeepthi Dharwar 586e022e7ebSLen Brown for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) { 587e022e7ebSLen Brown int num_substates, mwait_hint, mwait_cstate, mwait_substate; 58846bcfad7SDeepthi Dharwar 589e022e7ebSLen Brown if (cpuidle_state_table[cstate].enter == NULL) 590e022e7ebSLen Brown break; 591e022e7ebSLen Brown 592e022e7ebSLen Brown if (cstate + 1 > max_cstate) { 59346bcfad7SDeepthi Dharwar printk(PREFIX "max_cstate %d reached\n", 59446bcfad7SDeepthi Dharwar max_cstate); 59546bcfad7SDeepthi Dharwar break; 59646bcfad7SDeepthi Dharwar } 59746bcfad7SDeepthi Dharwar 598e022e7ebSLen Brown mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags); 599e022e7ebSLen Brown mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint); 600e022e7ebSLen Brown mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint); 60146bcfad7SDeepthi Dharwar 602e022e7ebSLen Brown /* does the state exist in CPUID.MWAIT? */ 603e022e7ebSLen Brown num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4)) 604e022e7ebSLen Brown & MWAIT_SUBSTATE_MASK; 605e022e7ebSLen Brown 606e022e7ebSLen Brown /* if sub-state in table is not enumerated by CPUID */ 607e022e7ebSLen Brown if ((mwait_substate + 1) > num_substates) 608e022e7ebSLen Brown continue; 609e022e7ebSLen Brown 610e022e7ebSLen Brown if (((mwait_cstate + 1) > 2) && 61146bcfad7SDeepthi Dharwar !boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) 61246bcfad7SDeepthi Dharwar mark_tsc_unstable("TSC halts in idle" 61346bcfad7SDeepthi Dharwar " states deeper than C2"); 61446bcfad7SDeepthi Dharwar 61546bcfad7SDeepthi Dharwar drv->states[drv->state_count] = /* structure copy */ 61646bcfad7SDeepthi Dharwar cpuidle_state_table[cstate]; 61746bcfad7SDeepthi Dharwar 61846bcfad7SDeepthi Dharwar drv->state_count += 1; 61946bcfad7SDeepthi Dharwar } 62046bcfad7SDeepthi Dharwar 621b66b8b9aSAndi Kleen if (icpu->auto_demotion_disable_flags) 62239a74fdeSShaohua Li on_each_cpu(auto_demotion_disable, NULL, 1); 62346bcfad7SDeepthi Dharwar 62432e95180SLen Brown if (icpu->disable_promotion_to_c1e) /* each-cpu is redundant */ 62532e95180SLen Brown on_each_cpu(c1e_promotion_disable, NULL, 1); 62632e95180SLen Brown 62746bcfad7SDeepthi Dharwar return 0; 62846bcfad7SDeepthi Dharwar } 62946bcfad7SDeepthi Dharwar 63046bcfad7SDeepthi Dharwar 63146bcfad7SDeepthi Dharwar /* 63265b7f839SThomas Renninger * intel_idle_cpu_init() 63326717172SLen Brown * allocate, initialize, register cpuidle_devices 63465b7f839SThomas Renninger * @cpu: cpu/core to initialize 63526717172SLen Brown */ 63625ac7761SDaniel Lezcano static int intel_idle_cpu_init(int cpu) 63726717172SLen Brown { 63826717172SLen Brown struct cpuidle_device *dev; 63926717172SLen Brown 64065b7f839SThomas Renninger dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu); 64126717172SLen Brown 64265b7f839SThomas Renninger dev->cpu = cpu; 64326717172SLen Brown 64426717172SLen Brown if (cpuidle_register_device(dev)) { 64565b7f839SThomas Renninger pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu); 64626717172SLen Brown intel_idle_cpuidle_devices_uninit(); 64726717172SLen Brown return -EIO; 64826717172SLen Brown } 64965b7f839SThomas Renninger 650b66b8b9aSAndi Kleen if (icpu->auto_demotion_disable_flags) 65165b7f839SThomas Renninger smp_call_function_single(cpu, auto_demotion_disable, NULL, 1); 65226717172SLen Brown 653dbf87ab8SBartlomiej Zolnierkiewicz if (icpu->disable_promotion_to_c1e) 654dbf87ab8SBartlomiej Zolnierkiewicz smp_call_function_single(cpu, c1e_promotion_disable, NULL, 1); 655dbf87ab8SBartlomiej Zolnierkiewicz 65626717172SLen Brown return 0; 65726717172SLen Brown } 65826717172SLen Brown 65926717172SLen Brown static int __init intel_idle_init(void) 66026717172SLen Brown { 66165b7f839SThomas Renninger int retval, i; 66226717172SLen Brown 663d1896049SThomas Renninger /* Do not load intel_idle at all for now if idle= is passed */ 664d1896049SThomas Renninger if (boot_option_idle_override != IDLE_NO_OVERRIDE) 665d1896049SThomas Renninger return -ENODEV; 666d1896049SThomas Renninger 66726717172SLen Brown retval = intel_idle_probe(); 66826717172SLen Brown if (retval) 66926717172SLen Brown return retval; 67026717172SLen Brown 67146bcfad7SDeepthi Dharwar intel_idle_cpuidle_driver_init(); 67226717172SLen Brown retval = cpuidle_register_driver(&intel_idle_driver); 67326717172SLen Brown if (retval) { 6743735d524SKonrad Rzeszutek Wilk struct cpuidle_driver *drv = cpuidle_get_driver(); 67526717172SLen Brown printk(KERN_DEBUG PREFIX "intel_idle yielding to %s", 6763735d524SKonrad Rzeszutek Wilk drv ? drv->name : "none"); 67726717172SLen Brown return retval; 67826717172SLen Brown } 67926717172SLen Brown 68065b7f839SThomas Renninger intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device); 68165b7f839SThomas Renninger if (intel_idle_cpuidle_devices == NULL) 68265b7f839SThomas Renninger return -ENOMEM; 68365b7f839SThomas Renninger 68407494d54SSrivatsa S. Bhat cpu_notifier_register_begin(); 68507494d54SSrivatsa S. Bhat 68665b7f839SThomas Renninger for_each_online_cpu(i) { 68765b7f839SThomas Renninger retval = intel_idle_cpu_init(i); 68826717172SLen Brown if (retval) { 68907494d54SSrivatsa S. Bhat cpu_notifier_register_done(); 69026717172SLen Brown cpuidle_unregister_driver(&intel_idle_driver); 69126717172SLen Brown return retval; 69226717172SLen Brown } 69365b7f839SThomas Renninger } 69407494d54SSrivatsa S. Bhat __register_cpu_notifier(&cpu_hotplug_notifier); 69507494d54SSrivatsa S. Bhat 69607494d54SSrivatsa S. Bhat cpu_notifier_register_done(); 69726717172SLen Brown 69826717172SLen Brown return 0; 69926717172SLen Brown } 70026717172SLen Brown 70126717172SLen Brown static void __exit intel_idle_exit(void) 70226717172SLen Brown { 70326717172SLen Brown intel_idle_cpuidle_devices_uninit(); 70426717172SLen Brown cpuidle_unregister_driver(&intel_idle_driver); 70526717172SLen Brown 70607494d54SSrivatsa S. Bhat cpu_notifier_register_begin(); 70725ac7761SDaniel Lezcano 70825ac7761SDaniel Lezcano if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE) 70939a74fdeSShaohua Li on_each_cpu(__setup_broadcast_timer, (void *)false, 1); 71007494d54SSrivatsa S. Bhat __unregister_cpu_notifier(&cpu_hotplug_notifier); 71107494d54SSrivatsa S. Bhat 71207494d54SSrivatsa S. Bhat cpu_notifier_register_done(); 7132a2d31c8SShaohua Li 71426717172SLen Brown return; 71526717172SLen Brown } 71626717172SLen Brown 71726717172SLen Brown module_init(intel_idle_init); 71826717172SLen Brown module_exit(intel_idle_exit); 71926717172SLen Brown 72026717172SLen Brown module_param(max_cstate, int, 0444); 72126717172SLen Brown 72226717172SLen Brown MODULE_AUTHOR("Len Brown <len.brown@intel.com>"); 72326717172SLen Brown MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION); 72426717172SLen Brown MODULE_LICENSE("GPL"); 725