126717172SLen Brown /* 226717172SLen Brown * intel_idle.c - native hardware idle loop for modern Intel processors 326717172SLen Brown * 426717172SLen Brown * Copyright (c) 2010, 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 <linux/hrtimer.h> /* ktime_get_real() */ 6026717172SLen Brown #include <trace/events/power.h> 6126717172SLen Brown #include <linux/sched.h> 622a2d31c8SShaohua Li #include <linux/notifier.h> 632a2d31c8SShaohua Li #include <linux/cpu.h> 647c52d551SPaul Gortmaker #include <linux/module.h> 65b66b8b9aSAndi Kleen #include <asm/cpu_device_id.h> 66bc83ccccSH. Peter Anvin #include <asm/mwait.h> 6714796fcaSLen Brown #include <asm/msr.h> 6826717172SLen Brown 6926717172SLen Brown #define INTEL_IDLE_VERSION "0.4" 7026717172SLen Brown #define PREFIX "intel_idle: " 7126717172SLen Brown 7226717172SLen Brown static struct cpuidle_driver intel_idle_driver = { 7326717172SLen Brown .name = "intel_idle", 7426717172SLen Brown .owner = THIS_MODULE, 7526717172SLen Brown }; 7626717172SLen Brown /* intel_idle.max_cstate=0 disables driver */ 7726717172SLen Brown static int max_cstate = MWAIT_MAX_NUM_CSTATES - 1; 7826717172SLen Brown 79c4236282SLen Brown static unsigned int mwait_substates; 8026717172SLen Brown 812a2d31c8SShaohua Li #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF 8226717172SLen Brown /* Reliable LAPIC Timer States, bit 1 for C1 etc. */ 83d13780d4SLen Brown static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */ 8426717172SLen Brown 85b66b8b9aSAndi Kleen struct idle_cpu { 86b66b8b9aSAndi Kleen struct cpuidle_state *state_table; 8726717172SLen Brown 8826717172SLen Brown /* 8914796fcaSLen Brown * Hardware C-state auto-demotion may not always be optimal. 9014796fcaSLen Brown * Indicate which enable bits to clear here. 9114796fcaSLen Brown */ 92b66b8b9aSAndi Kleen unsigned long auto_demotion_disable_flags; 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); 99b66b8b9aSAndi Kleen 100b66b8b9aSAndi Kleen static struct cpuidle_state *cpuidle_state_table; 10114796fcaSLen Brown 10214796fcaSLen Brown /* 103956d033fSLen Brown * Set this flag for states where the HW flushes the TLB for us 104956d033fSLen Brown * and so we don't need cross-calls to keep it consistent. 105956d033fSLen Brown * If this flag is set, SW flushes the TLB, so even if the 106956d033fSLen Brown * HW doesn't do the flushing, this flag is safe to use. 107956d033fSLen Brown */ 108956d033fSLen Brown #define CPUIDLE_FLAG_TLB_FLUSHED 0x10000 109956d033fSLen Brown 110956d033fSLen Brown /* 11126717172SLen Brown * States are indexed by the cstate number, 11226717172SLen Brown * which is also the index into the MWAIT hint array. 11326717172SLen Brown * Thus C0 is a dummy. 11426717172SLen Brown */ 11526717172SLen Brown static struct cpuidle_state nehalem_cstates[MWAIT_MAX_NUM_CSTATES] = { 11626717172SLen Brown { /* MWAIT C0 */ }, 11726717172SLen Brown { /* MWAIT C1 */ 11815e123e5SThomas Renninger .name = "C1-NHM", 11926717172SLen Brown .desc = "MWAIT 0x00", 12026717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 12126717172SLen Brown .exit_latency = 3, 12226717172SLen Brown .target_residency = 6, 12326717172SLen Brown .enter = &intel_idle }, 12426717172SLen Brown { /* MWAIT C2 */ 12515e123e5SThomas Renninger .name = "C3-NHM", 12626717172SLen Brown .desc = "MWAIT 0x10", 1276110a1f4SSuresh Siddha .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 12826717172SLen Brown .exit_latency = 20, 12926717172SLen Brown .target_residency = 80, 13026717172SLen Brown .enter = &intel_idle }, 13126717172SLen Brown { /* MWAIT C3 */ 13215e123e5SThomas Renninger .name = "C6-NHM", 13326717172SLen Brown .desc = "MWAIT 0x20", 1346110a1f4SSuresh Siddha .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 13526717172SLen Brown .exit_latency = 200, 13626717172SLen Brown .target_residency = 800, 13726717172SLen Brown .enter = &intel_idle }, 13826717172SLen Brown }; 13926717172SLen Brown 140d13780d4SLen Brown static struct cpuidle_state snb_cstates[MWAIT_MAX_NUM_CSTATES] = { 141d13780d4SLen Brown { /* MWAIT C0 */ }, 142d13780d4SLen Brown { /* MWAIT C1 */ 14315e123e5SThomas Renninger .name = "C1-SNB", 144d13780d4SLen Brown .desc = "MWAIT 0x00", 145d13780d4SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 146d13780d4SLen Brown .exit_latency = 1, 147ddbd550dSLen Brown .target_residency = 1, 148d13780d4SLen Brown .enter = &intel_idle }, 149d13780d4SLen Brown { /* MWAIT C2 */ 15015e123e5SThomas Renninger .name = "C3-SNB", 151d13780d4SLen Brown .desc = "MWAIT 0x10", 15200527cc6SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 153d13780d4SLen Brown .exit_latency = 80, 154ddbd550dSLen Brown .target_residency = 211, 155d13780d4SLen Brown .enter = &intel_idle }, 156d13780d4SLen Brown { /* MWAIT C3 */ 15715e123e5SThomas Renninger .name = "C6-SNB", 158d13780d4SLen Brown .desc = "MWAIT 0x20", 15900527cc6SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 160d13780d4SLen Brown .exit_latency = 104, 161ddbd550dSLen Brown .target_residency = 345, 162d13780d4SLen Brown .enter = &intel_idle }, 163d13780d4SLen Brown { /* MWAIT C4 */ 16415e123e5SThomas Renninger .name = "C7-SNB", 165d13780d4SLen Brown .desc = "MWAIT 0x30", 16600527cc6SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 167d13780d4SLen Brown .exit_latency = 109, 168ddbd550dSLen Brown .target_residency = 345, 169d13780d4SLen Brown .enter = &intel_idle }, 170d13780d4SLen Brown }; 171d13780d4SLen Brown 17226717172SLen Brown static struct cpuidle_state atom_cstates[MWAIT_MAX_NUM_CSTATES] = { 17326717172SLen Brown { /* MWAIT C0 */ }, 17426717172SLen Brown { /* MWAIT C1 */ 17515e123e5SThomas Renninger .name = "C1-ATM", 17626717172SLen Brown .desc = "MWAIT 0x00", 17726717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 17826717172SLen Brown .exit_latency = 1, 17926717172SLen Brown .target_residency = 4, 18026717172SLen Brown .enter = &intel_idle }, 18126717172SLen Brown { /* MWAIT C2 */ 18215e123e5SThomas Renninger .name = "C2-ATM", 18326717172SLen Brown .desc = "MWAIT 0x10", 18426717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 18526717172SLen Brown .exit_latency = 20, 18626717172SLen Brown .target_residency = 80, 18726717172SLen Brown .enter = &intel_idle }, 18826717172SLen Brown { /* MWAIT C3 */ }, 18926717172SLen Brown { /* MWAIT C4 */ 19015e123e5SThomas Renninger .name = "C4-ATM", 19126717172SLen Brown .desc = "MWAIT 0x30", 1926110a1f4SSuresh Siddha .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 19326717172SLen Brown .exit_latency = 100, 19426717172SLen Brown .target_residency = 400, 19526717172SLen Brown .enter = &intel_idle }, 19626717172SLen Brown { /* MWAIT C5 */ }, 19726717172SLen Brown { /* MWAIT C6 */ 19815e123e5SThomas Renninger .name = "C6-ATM", 1997fcca7d9SLen Brown .desc = "MWAIT 0x52", 2006110a1f4SSuresh Siddha .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED, 2017fcca7d9SLen Brown .exit_latency = 140, 2027fcca7d9SLen Brown .target_residency = 560, 2037fcca7d9SLen Brown .enter = &intel_idle }, 20426717172SLen Brown }; 20526717172SLen Brown 20695e3ec11SDavid Howells static long get_driver_data(int cstate) 2074202735eSDeepthi Dharwar { 2084202735eSDeepthi Dharwar int driver_data; 2094202735eSDeepthi Dharwar switch (cstate) { 2104202735eSDeepthi Dharwar 2114202735eSDeepthi Dharwar case 1: /* MWAIT C1 */ 2124202735eSDeepthi Dharwar driver_data = 0x00; 2134202735eSDeepthi Dharwar break; 2144202735eSDeepthi Dharwar case 2: /* MWAIT C2 */ 2154202735eSDeepthi Dharwar driver_data = 0x10; 2164202735eSDeepthi Dharwar break; 2174202735eSDeepthi Dharwar case 3: /* MWAIT C3 */ 2184202735eSDeepthi Dharwar driver_data = 0x20; 2194202735eSDeepthi Dharwar break; 2204202735eSDeepthi Dharwar case 4: /* MWAIT C4 */ 2214202735eSDeepthi Dharwar driver_data = 0x30; 2224202735eSDeepthi Dharwar break; 2234202735eSDeepthi Dharwar case 5: /* MWAIT C5 */ 2244202735eSDeepthi Dharwar driver_data = 0x40; 2254202735eSDeepthi Dharwar break; 2264202735eSDeepthi Dharwar case 6: /* MWAIT C6 */ 2274202735eSDeepthi Dharwar driver_data = 0x52; 2284202735eSDeepthi Dharwar break; 2294202735eSDeepthi Dharwar default: 2304202735eSDeepthi Dharwar driver_data = 0x00; 2314202735eSDeepthi Dharwar } 2324202735eSDeepthi Dharwar return driver_data; 2334202735eSDeepthi Dharwar } 2344202735eSDeepthi Dharwar 23526717172SLen Brown /** 23626717172SLen Brown * intel_idle 23726717172SLen Brown * @dev: cpuidle_device 23846bcfad7SDeepthi Dharwar * @drv: cpuidle driver 239e978aa7dSDeepthi Dharwar * @index: index of cpuidle state 24026717172SLen Brown * 24163ff07beSYanmin Zhang * Must be called under local_irq_disable(). 24226717172SLen Brown */ 24346bcfad7SDeepthi Dharwar static int intel_idle(struct cpuidle_device *dev, 24446bcfad7SDeepthi Dharwar struct cpuidle_driver *drv, int index) 24526717172SLen Brown { 24626717172SLen Brown unsigned long ecx = 1; /* break on interrupt flag */ 24746bcfad7SDeepthi Dharwar struct cpuidle_state *state = &drv->states[index]; 2484202735eSDeepthi Dharwar struct cpuidle_state_usage *state_usage = &dev->states_usage[index]; 2494202735eSDeepthi Dharwar unsigned long eax = (unsigned long)cpuidle_get_statedata(state_usage); 25026717172SLen Brown unsigned int cstate; 25126717172SLen Brown ktime_t kt_before, kt_after; 25226717172SLen Brown s64 usec_delta; 25326717172SLen Brown int cpu = smp_processor_id(); 25426717172SLen Brown 25526717172SLen Brown cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1; 25626717172SLen Brown 2576110a1f4SSuresh Siddha /* 258c8381cc3SLen Brown * leave_mm() to avoid costly and often unnecessary wakeups 259c8381cc3SLen Brown * for flushing the user TLB's associated with the active mm. 2606110a1f4SSuresh Siddha */ 261c8381cc3SLen Brown if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED) 2626110a1f4SSuresh Siddha leave_mm(cpu); 2636110a1f4SSuresh Siddha 26426717172SLen Brown if (!(lapic_timer_reliable_states & (1 << (cstate)))) 26526717172SLen Brown clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu); 26626717172SLen Brown 26726717172SLen Brown kt_before = ktime_get_real(); 26826717172SLen Brown 26926717172SLen Brown stop_critical_timings(); 27026717172SLen Brown if (!need_resched()) { 27126717172SLen Brown 27226717172SLen Brown __monitor((void *)¤t_thread_info()->flags, 0, 0); 27326717172SLen Brown smp_mb(); 27426717172SLen Brown if (!need_resched()) 27526717172SLen Brown __mwait(eax, ecx); 27626717172SLen Brown } 27726717172SLen Brown 27826717172SLen Brown start_critical_timings(); 27926717172SLen Brown 28026717172SLen Brown kt_after = ktime_get_real(); 28126717172SLen Brown usec_delta = ktime_to_us(ktime_sub(kt_after, kt_before)); 28226717172SLen Brown 28326717172SLen Brown local_irq_enable(); 28426717172SLen Brown 28526717172SLen Brown if (!(lapic_timer_reliable_states & (1 << (cstate)))) 28626717172SLen Brown clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu); 28726717172SLen Brown 288e978aa7dSDeepthi Dharwar /* Update cpuidle counters */ 289e978aa7dSDeepthi Dharwar dev->last_residency = (int)usec_delta; 290e978aa7dSDeepthi Dharwar 291e978aa7dSDeepthi Dharwar return index; 29226717172SLen Brown } 29326717172SLen Brown 2942a2d31c8SShaohua Li static void __setup_broadcast_timer(void *arg) 2952a2d31c8SShaohua Li { 2962a2d31c8SShaohua Li unsigned long reason = (unsigned long)arg; 2972a2d31c8SShaohua Li int cpu = smp_processor_id(); 2982a2d31c8SShaohua Li 2992a2d31c8SShaohua Li reason = reason ? 3002a2d31c8SShaohua Li CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF; 3012a2d31c8SShaohua Li 3022a2d31c8SShaohua Li clockevents_notify(reason, &cpu); 3032a2d31c8SShaohua Li } 3042a2d31c8SShaohua Li 305ec30f343SShaohua Li static int setup_broadcast_cpuhp_notify(struct notifier_block *n, 3062a2d31c8SShaohua Li unsigned long action, void *hcpu) 3072a2d31c8SShaohua Li { 3082a2d31c8SShaohua Li int hotcpu = (unsigned long)hcpu; 3092a2d31c8SShaohua Li 3102a2d31c8SShaohua Li switch (action & 0xf) { 3112a2d31c8SShaohua Li case CPU_ONLINE: 3122a2d31c8SShaohua Li smp_call_function_single(hotcpu, __setup_broadcast_timer, 3132a2d31c8SShaohua Li (void *)true, 1); 3142a2d31c8SShaohua Li break; 3152a2d31c8SShaohua Li } 3162a2d31c8SShaohua Li return NOTIFY_OK; 3172a2d31c8SShaohua Li } 3182a2d31c8SShaohua Li 319ec30f343SShaohua Li static struct notifier_block setup_broadcast_notifier = { 3202a2d31c8SShaohua Li .notifier_call = setup_broadcast_cpuhp_notify, 3212a2d31c8SShaohua Li }; 3222a2d31c8SShaohua Li 32314796fcaSLen Brown static void auto_demotion_disable(void *dummy) 32414796fcaSLen Brown { 32514796fcaSLen Brown unsigned long long msr_bits; 32614796fcaSLen Brown 32714796fcaSLen Brown rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits); 328b66b8b9aSAndi Kleen msr_bits &= ~(icpu->auto_demotion_disable_flags); 32914796fcaSLen Brown wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits); 33014796fcaSLen Brown } 33114796fcaSLen Brown 332b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_nehalem = { 333b66b8b9aSAndi Kleen .state_table = nehalem_cstates, 334b66b8b9aSAndi Kleen .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE, 335b66b8b9aSAndi Kleen }; 336b66b8b9aSAndi Kleen 337b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_atom = { 338b66b8b9aSAndi Kleen .state_table = atom_cstates, 339b66b8b9aSAndi Kleen }; 340b66b8b9aSAndi Kleen 341b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_lincroft = { 342b66b8b9aSAndi Kleen .state_table = atom_cstates, 343b66b8b9aSAndi Kleen .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE, 344b66b8b9aSAndi Kleen }; 345b66b8b9aSAndi Kleen 346b66b8b9aSAndi Kleen static const struct idle_cpu idle_cpu_snb = { 347b66b8b9aSAndi Kleen .state_table = snb_cstates, 348b66b8b9aSAndi Kleen }; 349b66b8b9aSAndi Kleen 350b66b8b9aSAndi Kleen #define ICPU(model, cpu) \ 351b66b8b9aSAndi Kleen { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu } 352b66b8b9aSAndi Kleen 353b66b8b9aSAndi Kleen static const struct x86_cpu_id intel_idle_ids[] = { 354b66b8b9aSAndi Kleen ICPU(0x1a, idle_cpu_nehalem), 355b66b8b9aSAndi Kleen ICPU(0x1e, idle_cpu_nehalem), 356b66b8b9aSAndi Kleen ICPU(0x1f, idle_cpu_nehalem), 3578bf11938SBen Hutchings ICPU(0x25, idle_cpu_nehalem), 3588bf11938SBen Hutchings ICPU(0x2c, idle_cpu_nehalem), 3598bf11938SBen Hutchings ICPU(0x2e, idle_cpu_nehalem), 360b66b8b9aSAndi Kleen ICPU(0x1c, idle_cpu_atom), 361b66b8b9aSAndi Kleen ICPU(0x26, idle_cpu_lincroft), 3628bf11938SBen Hutchings ICPU(0x2f, idle_cpu_nehalem), 363b66b8b9aSAndi Kleen ICPU(0x2a, idle_cpu_snb), 364b66b8b9aSAndi Kleen ICPU(0x2d, idle_cpu_snb), 365b66b8b9aSAndi Kleen {} 366b66b8b9aSAndi Kleen }; 367b66b8b9aSAndi Kleen MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids); 368b66b8b9aSAndi Kleen 36926717172SLen Brown /* 37026717172SLen Brown * intel_idle_probe() 37126717172SLen Brown */ 37226717172SLen Brown static int intel_idle_probe(void) 37326717172SLen Brown { 374c4236282SLen Brown unsigned int eax, ebx, ecx; 375b66b8b9aSAndi Kleen const struct x86_cpu_id *id; 37626717172SLen Brown 37726717172SLen Brown if (max_cstate == 0) { 37826717172SLen Brown pr_debug(PREFIX "disabled\n"); 37926717172SLen Brown return -EPERM; 38026717172SLen Brown } 38126717172SLen Brown 382b66b8b9aSAndi Kleen id = x86_match_cpu(intel_idle_ids); 383b66b8b9aSAndi Kleen if (!id) { 384b66b8b9aSAndi Kleen if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && 385b66b8b9aSAndi Kleen boot_cpu_data.x86 == 6) 386b66b8b9aSAndi Kleen pr_debug(PREFIX "does not run on family %d model %d\n", 387b66b8b9aSAndi Kleen boot_cpu_data.x86, boot_cpu_data.x86_model); 38826717172SLen Brown return -ENODEV; 389b66b8b9aSAndi Kleen } 39026717172SLen Brown 39126717172SLen Brown if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF) 39226717172SLen Brown return -ENODEV; 39326717172SLen Brown 394c4236282SLen Brown cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates); 39526717172SLen Brown 39626717172SLen Brown if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) || 3975c2a9f06SThomas Renninger !(ecx & CPUID5_ECX_INTERRUPT_BREAK) || 3985c2a9f06SThomas Renninger !mwait_substates) 39926717172SLen Brown return -ENODEV; 40026717172SLen Brown 401c4236282SLen Brown pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates); 40226717172SLen Brown 403b66b8b9aSAndi Kleen icpu = (const struct idle_cpu *)id->driver_data; 404b66b8b9aSAndi Kleen cpuidle_state_table = icpu->state_table; 40526717172SLen Brown 40656b9aea3SLen Brown if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */ 4072a2d31c8SShaohua Li lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE; 4082a2d31c8SShaohua Li else { 40939a74fdeSShaohua Li on_each_cpu(__setup_broadcast_timer, (void *)true, 1); 4102a2d31c8SShaohua Li register_cpu_notifier(&setup_broadcast_notifier); 4112a2d31c8SShaohua Li } 41256b9aea3SLen Brown 41326717172SLen Brown pr_debug(PREFIX "v" INTEL_IDLE_VERSION 41426717172SLen Brown " model 0x%X\n", boot_cpu_data.x86_model); 41526717172SLen Brown 41626717172SLen Brown pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n", 41726717172SLen Brown lapic_timer_reliable_states); 41826717172SLen Brown return 0; 41926717172SLen Brown } 42026717172SLen Brown 42126717172SLen Brown /* 42226717172SLen Brown * intel_idle_cpuidle_devices_uninit() 42326717172SLen Brown * unregister, free cpuidle_devices 42426717172SLen Brown */ 42526717172SLen Brown static void intel_idle_cpuidle_devices_uninit(void) 42626717172SLen Brown { 42726717172SLen Brown int i; 42826717172SLen Brown struct cpuidle_device *dev; 42926717172SLen Brown 43026717172SLen Brown for_each_online_cpu(i) { 43126717172SLen Brown dev = per_cpu_ptr(intel_idle_cpuidle_devices, i); 43226717172SLen Brown cpuidle_unregister_device(dev); 43326717172SLen Brown } 43426717172SLen Brown 43526717172SLen Brown free_percpu(intel_idle_cpuidle_devices); 43626717172SLen Brown return; 43726717172SLen Brown } 43826717172SLen Brown /* 43946bcfad7SDeepthi Dharwar * intel_idle_cpuidle_driver_init() 44046bcfad7SDeepthi Dharwar * allocate, initialize cpuidle_states 44146bcfad7SDeepthi Dharwar */ 44246bcfad7SDeepthi Dharwar static int intel_idle_cpuidle_driver_init(void) 44346bcfad7SDeepthi Dharwar { 44446bcfad7SDeepthi Dharwar int cstate; 44546bcfad7SDeepthi Dharwar struct cpuidle_driver *drv = &intel_idle_driver; 44646bcfad7SDeepthi Dharwar 44746bcfad7SDeepthi Dharwar drv->state_count = 1; 44846bcfad7SDeepthi Dharwar 44946bcfad7SDeepthi Dharwar for (cstate = 1; cstate < MWAIT_MAX_NUM_CSTATES; ++cstate) { 45046bcfad7SDeepthi Dharwar int num_substates; 45146bcfad7SDeepthi Dharwar 45246bcfad7SDeepthi Dharwar if (cstate > max_cstate) { 45346bcfad7SDeepthi Dharwar printk(PREFIX "max_cstate %d reached\n", 45446bcfad7SDeepthi Dharwar max_cstate); 45546bcfad7SDeepthi Dharwar break; 45646bcfad7SDeepthi Dharwar } 45746bcfad7SDeepthi Dharwar 45846bcfad7SDeepthi Dharwar /* does the state exist in CPUID.MWAIT? */ 45946bcfad7SDeepthi Dharwar num_substates = (mwait_substates >> ((cstate) * 4)) 46046bcfad7SDeepthi Dharwar & MWAIT_SUBSTATE_MASK; 46146bcfad7SDeepthi Dharwar if (num_substates == 0) 46246bcfad7SDeepthi Dharwar continue; 46346bcfad7SDeepthi Dharwar /* is the state not enabled? */ 46446bcfad7SDeepthi Dharwar if (cpuidle_state_table[cstate].enter == NULL) { 46546bcfad7SDeepthi Dharwar /* does the driver not know about the state? */ 46646bcfad7SDeepthi Dharwar if (*cpuidle_state_table[cstate].name == '\0') 46746bcfad7SDeepthi Dharwar pr_debug(PREFIX "unaware of model 0x%x" 46846bcfad7SDeepthi Dharwar " MWAIT %d please" 46946bcfad7SDeepthi Dharwar " contact lenb@kernel.org", 47046bcfad7SDeepthi Dharwar boot_cpu_data.x86_model, cstate); 47146bcfad7SDeepthi Dharwar continue; 47246bcfad7SDeepthi Dharwar } 47346bcfad7SDeepthi Dharwar 47446bcfad7SDeepthi Dharwar if ((cstate > 2) && 47546bcfad7SDeepthi Dharwar !boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) 47646bcfad7SDeepthi Dharwar mark_tsc_unstable("TSC halts in idle" 47746bcfad7SDeepthi Dharwar " states deeper than C2"); 47846bcfad7SDeepthi Dharwar 47946bcfad7SDeepthi Dharwar drv->states[drv->state_count] = /* structure copy */ 48046bcfad7SDeepthi Dharwar cpuidle_state_table[cstate]; 48146bcfad7SDeepthi Dharwar 48246bcfad7SDeepthi Dharwar drv->state_count += 1; 48346bcfad7SDeepthi Dharwar } 48446bcfad7SDeepthi Dharwar 485b66b8b9aSAndi Kleen if (icpu->auto_demotion_disable_flags) 48639a74fdeSShaohua Li on_each_cpu(auto_demotion_disable, NULL, 1); 48746bcfad7SDeepthi Dharwar 48846bcfad7SDeepthi Dharwar return 0; 48946bcfad7SDeepthi Dharwar } 49046bcfad7SDeepthi Dharwar 49146bcfad7SDeepthi Dharwar 49246bcfad7SDeepthi Dharwar /* 49365b7f839SThomas Renninger * intel_idle_cpu_init() 49426717172SLen Brown * allocate, initialize, register cpuidle_devices 49565b7f839SThomas Renninger * @cpu: cpu/core to initialize 49626717172SLen Brown */ 49765b7f839SThomas Renninger int intel_idle_cpu_init(int cpu) 49826717172SLen Brown { 49965b7f839SThomas Renninger int cstate; 50026717172SLen Brown struct cpuidle_device *dev; 50126717172SLen Brown 50265b7f839SThomas Renninger dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu); 50326717172SLen Brown 50426717172SLen Brown dev->state_count = 1; 50526717172SLen Brown 50626717172SLen Brown for (cstate = 1; cstate < MWAIT_MAX_NUM_CSTATES; ++cstate) { 50726717172SLen Brown int num_substates; 50826717172SLen Brown 50926717172SLen Brown if (cstate > max_cstate) { 510dc716e96SMarcos Paulo de Souza printk(PREFIX "max_cstate %d reached\n", max_cstate); 51126717172SLen Brown break; 51226717172SLen Brown } 51326717172SLen Brown 51426717172SLen Brown /* does the state exist in CPUID.MWAIT? */ 515c4236282SLen Brown num_substates = (mwait_substates >> ((cstate) * 4)) 51626717172SLen Brown & MWAIT_SUBSTATE_MASK; 51726717172SLen Brown if (num_substates == 0) 51826717172SLen Brown continue; 51926717172SLen Brown /* is the state not enabled? */ 52065b7f839SThomas Renninger if (cpuidle_state_table[cstate].enter == NULL) 52126717172SLen Brown continue; 52226717172SLen Brown 5234202735eSDeepthi Dharwar dev->states_usage[dev->state_count].driver_data = 5244202735eSDeepthi Dharwar (void *)get_driver_data(cstate); 52526717172SLen Brown 52626717172SLen Brown dev->state_count += 1; 52726717172SLen Brown } 528dc716e96SMarcos Paulo de Souza 52965b7f839SThomas Renninger dev->cpu = cpu; 53026717172SLen Brown 53126717172SLen Brown if (cpuidle_register_device(dev)) { 53265b7f839SThomas Renninger pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu); 53326717172SLen Brown intel_idle_cpuidle_devices_uninit(); 53426717172SLen Brown return -EIO; 53526717172SLen Brown } 53665b7f839SThomas Renninger 537b66b8b9aSAndi Kleen if (icpu->auto_demotion_disable_flags) 53865b7f839SThomas Renninger smp_call_function_single(cpu, auto_demotion_disable, NULL, 1); 53926717172SLen Brown 54026717172SLen Brown return 0; 54126717172SLen Brown } 54299b72508SThomas Renninger EXPORT_SYMBOL_GPL(intel_idle_cpu_init); 54326717172SLen Brown 54426717172SLen Brown static int __init intel_idle_init(void) 54526717172SLen Brown { 54665b7f839SThomas Renninger int retval, i; 54726717172SLen Brown 548d1896049SThomas Renninger /* Do not load intel_idle at all for now if idle= is passed */ 549d1896049SThomas Renninger if (boot_option_idle_override != IDLE_NO_OVERRIDE) 550d1896049SThomas Renninger return -ENODEV; 551d1896049SThomas Renninger 55226717172SLen Brown retval = intel_idle_probe(); 55326717172SLen Brown if (retval) 55426717172SLen Brown return retval; 55526717172SLen Brown 55646bcfad7SDeepthi Dharwar intel_idle_cpuidle_driver_init(); 55726717172SLen Brown retval = cpuidle_register_driver(&intel_idle_driver); 55826717172SLen Brown if (retval) { 55926717172SLen Brown printk(KERN_DEBUG PREFIX "intel_idle yielding to %s", 56026717172SLen Brown cpuidle_get_driver()->name); 56126717172SLen Brown return retval; 56226717172SLen Brown } 56326717172SLen Brown 56465b7f839SThomas Renninger intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device); 56565b7f839SThomas Renninger if (intel_idle_cpuidle_devices == NULL) 56665b7f839SThomas Renninger return -ENOMEM; 56765b7f839SThomas Renninger 56865b7f839SThomas Renninger for_each_online_cpu(i) { 56965b7f839SThomas Renninger retval = intel_idle_cpu_init(i); 57026717172SLen Brown if (retval) { 57126717172SLen Brown cpuidle_unregister_driver(&intel_idle_driver); 57226717172SLen Brown return retval; 57326717172SLen Brown } 57465b7f839SThomas Renninger } 57526717172SLen Brown 57626717172SLen Brown return 0; 57726717172SLen Brown } 57826717172SLen Brown 57926717172SLen Brown static void __exit intel_idle_exit(void) 58026717172SLen Brown { 58126717172SLen Brown intel_idle_cpuidle_devices_uninit(); 58226717172SLen Brown cpuidle_unregister_driver(&intel_idle_driver); 58326717172SLen Brown 5842a2d31c8SShaohua Li if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE) { 58539a74fdeSShaohua Li on_each_cpu(__setup_broadcast_timer, (void *)false, 1); 5862a2d31c8SShaohua Li unregister_cpu_notifier(&setup_broadcast_notifier); 5872a2d31c8SShaohua Li } 5882a2d31c8SShaohua Li 58926717172SLen Brown return; 59026717172SLen Brown } 59126717172SLen Brown 59226717172SLen Brown module_init(intel_idle_init); 59326717172SLen Brown module_exit(intel_idle_exit); 59426717172SLen Brown 59526717172SLen Brown module_param(max_cstate, int, 0444); 59626717172SLen Brown 59726717172SLen Brown MODULE_AUTHOR("Len Brown <len.brown@intel.com>"); 59826717172SLen Brown MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION); 59926717172SLen Brown MODULE_LICENSE("GPL"); 600