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> 6226717172SLen Brown 6326717172SLen Brown #define INTEL_IDLE_VERSION "0.4" 6426717172SLen Brown #define PREFIX "intel_idle: " 6526717172SLen Brown 6626717172SLen Brown #define MWAIT_SUBSTATE_MASK (0xf) 6726717172SLen Brown #define MWAIT_CSTATE_MASK (0xf) 6826717172SLen Brown #define MWAIT_SUBSTATE_SIZE (4) 6926717172SLen Brown #define MWAIT_MAX_NUM_CSTATES 8 7026717172SLen Brown #define CPUID_MWAIT_LEAF (5) 7126717172SLen Brown #define CPUID5_ECX_EXTENSIONS_SUPPORTED (0x1) 7226717172SLen Brown #define CPUID5_ECX_INTERRUPT_BREAK (0x2) 7326717172SLen Brown 7426717172SLen Brown static struct cpuidle_driver intel_idle_driver = { 7526717172SLen Brown .name = "intel_idle", 7626717172SLen Brown .owner = THIS_MODULE, 7726717172SLen Brown }; 7826717172SLen Brown /* intel_idle.max_cstate=0 disables driver */ 7926717172SLen Brown static int max_cstate = MWAIT_MAX_NUM_CSTATES - 1; 8026717172SLen Brown 81c4236282SLen Brown static unsigned int mwait_substates; 8226717172SLen Brown 8326717172SLen Brown /* Reliable LAPIC Timer States, bit 1 for C1 etc. */ 8426717172SLen Brown static unsigned int lapic_timer_reliable_states; 8526717172SLen Brown 8626717172SLen Brown static struct cpuidle_device *intel_idle_cpuidle_devices; 8726717172SLen Brown static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state); 8826717172SLen Brown 8926717172SLen Brown static struct cpuidle_state *cpuidle_state_table; 9026717172SLen Brown 9126717172SLen Brown /* 9226717172SLen Brown * States are indexed by the cstate number, 9326717172SLen Brown * which is also the index into the MWAIT hint array. 9426717172SLen Brown * Thus C0 is a dummy. 9526717172SLen Brown */ 9626717172SLen Brown static struct cpuidle_state nehalem_cstates[MWAIT_MAX_NUM_CSTATES] = { 9726717172SLen Brown { /* MWAIT C0 */ }, 9826717172SLen Brown { /* MWAIT C1 */ 9926717172SLen Brown .name = "NHM-C1", 10026717172SLen Brown .desc = "MWAIT 0x00", 10126717172SLen Brown .driver_data = (void *) 0x00, 10226717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 10326717172SLen Brown .exit_latency = 3, 10426717172SLen Brown .power_usage = 1000, 10526717172SLen Brown .target_residency = 6, 10626717172SLen Brown .enter = &intel_idle }, 10726717172SLen Brown { /* MWAIT C2 */ 10826717172SLen Brown .name = "NHM-C3", 10926717172SLen Brown .desc = "MWAIT 0x10", 11026717172SLen Brown .driver_data = (void *) 0x10, 11126717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 11226717172SLen Brown .exit_latency = 20, 11326717172SLen Brown .power_usage = 500, 11426717172SLen Brown .target_residency = 80, 11526717172SLen Brown .enter = &intel_idle }, 11626717172SLen Brown { /* MWAIT C3 */ 11726717172SLen Brown .name = "NHM-C6", 11826717172SLen Brown .desc = "MWAIT 0x20", 11926717172SLen Brown .driver_data = (void *) 0x20, 12026717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 12126717172SLen Brown .exit_latency = 200, 12226717172SLen Brown .power_usage = 350, 12326717172SLen Brown .target_residency = 800, 12426717172SLen Brown .enter = &intel_idle }, 12526717172SLen Brown }; 12626717172SLen Brown 12726717172SLen Brown static struct cpuidle_state atom_cstates[MWAIT_MAX_NUM_CSTATES] = { 12826717172SLen Brown { /* MWAIT C0 */ }, 12926717172SLen Brown { /* MWAIT C1 */ 13026717172SLen Brown .name = "ATM-C1", 13126717172SLen Brown .desc = "MWAIT 0x00", 13226717172SLen Brown .driver_data = (void *) 0x00, 13326717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 13426717172SLen Brown .exit_latency = 1, 13526717172SLen Brown .power_usage = 1000, 13626717172SLen Brown .target_residency = 4, 13726717172SLen Brown .enter = &intel_idle }, 13826717172SLen Brown { /* MWAIT C2 */ 13926717172SLen Brown .name = "ATM-C2", 14026717172SLen Brown .desc = "MWAIT 0x10", 14126717172SLen Brown .driver_data = (void *) 0x10, 14226717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 14326717172SLen Brown .exit_latency = 20, 14426717172SLen Brown .power_usage = 500, 14526717172SLen Brown .target_residency = 80, 14626717172SLen Brown .enter = &intel_idle }, 14726717172SLen Brown { /* MWAIT C3 */ }, 14826717172SLen Brown { /* MWAIT C4 */ 14926717172SLen Brown .name = "ATM-C4", 15026717172SLen Brown .desc = "MWAIT 0x30", 15126717172SLen Brown .driver_data = (void *) 0x30, 15226717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 15326717172SLen Brown .exit_latency = 100, 15426717172SLen Brown .power_usage = 250, 15526717172SLen Brown .target_residency = 400, 15626717172SLen Brown .enter = &intel_idle }, 15726717172SLen Brown { /* MWAIT C5 */ }, 15826717172SLen Brown { /* MWAIT C6 */ 15926717172SLen Brown .name = "ATM-C6", 16026717172SLen Brown .desc = "MWAIT 0x40", 16126717172SLen Brown .driver_data = (void *) 0x40, 16226717172SLen Brown .flags = CPUIDLE_FLAG_TIME_VALID, 16326717172SLen Brown .exit_latency = 200, 16426717172SLen Brown .power_usage = 150, 16526717172SLen Brown .target_residency = 800, 16626717172SLen Brown .enter = NULL }, /* disabled */ 16726717172SLen Brown }; 16826717172SLen Brown 16926717172SLen Brown /** 17026717172SLen Brown * intel_idle 17126717172SLen Brown * @dev: cpuidle_device 17226717172SLen Brown * @state: cpuidle state 17326717172SLen Brown * 17426717172SLen Brown */ 17526717172SLen Brown static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state) 17626717172SLen Brown { 17726717172SLen Brown unsigned long ecx = 1; /* break on interrupt flag */ 17826717172SLen Brown unsigned long eax = (unsigned long)cpuidle_get_statedata(state); 17926717172SLen Brown unsigned int cstate; 18026717172SLen Brown ktime_t kt_before, kt_after; 18126717172SLen Brown s64 usec_delta; 18226717172SLen Brown int cpu = smp_processor_id(); 18326717172SLen Brown 18426717172SLen Brown cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1; 18526717172SLen Brown 18626717172SLen Brown local_irq_disable(); 18726717172SLen Brown 18826717172SLen Brown if (!(lapic_timer_reliable_states & (1 << (cstate)))) 18926717172SLen Brown clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu); 19026717172SLen Brown 19126717172SLen Brown kt_before = ktime_get_real(); 19226717172SLen Brown 19326717172SLen Brown stop_critical_timings(); 19426717172SLen Brown #ifndef MODULE 19526717172SLen Brown trace_power_start(POWER_CSTATE, (eax >> 4) + 1); 19626717172SLen Brown #endif 19726717172SLen Brown if (!need_resched()) { 19826717172SLen Brown 19926717172SLen Brown __monitor((void *)¤t_thread_info()->flags, 0, 0); 20026717172SLen Brown smp_mb(); 20126717172SLen Brown if (!need_resched()) 20226717172SLen Brown __mwait(eax, ecx); 20326717172SLen Brown } 20426717172SLen Brown 20526717172SLen Brown start_critical_timings(); 20626717172SLen Brown 20726717172SLen Brown kt_after = ktime_get_real(); 20826717172SLen Brown usec_delta = ktime_to_us(ktime_sub(kt_after, kt_before)); 20926717172SLen Brown 21026717172SLen Brown local_irq_enable(); 21126717172SLen Brown 21226717172SLen Brown if (!(lapic_timer_reliable_states & (1 << (cstate)))) 21326717172SLen Brown clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu); 21426717172SLen Brown 21526717172SLen Brown return usec_delta; 21626717172SLen Brown } 21726717172SLen Brown 21826717172SLen Brown /* 21926717172SLen Brown * intel_idle_probe() 22026717172SLen Brown */ 22126717172SLen Brown static int intel_idle_probe(void) 22226717172SLen Brown { 223c4236282SLen Brown unsigned int eax, ebx, ecx; 22426717172SLen Brown 22526717172SLen Brown if (max_cstate == 0) { 22626717172SLen Brown pr_debug(PREFIX "disabled\n"); 22726717172SLen Brown return -EPERM; 22826717172SLen Brown } 22926717172SLen Brown 23026717172SLen Brown if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 23126717172SLen Brown return -ENODEV; 23226717172SLen Brown 23326717172SLen Brown if (!boot_cpu_has(X86_FEATURE_MWAIT)) 23426717172SLen Brown return -ENODEV; 23526717172SLen Brown 23626717172SLen Brown if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF) 23726717172SLen Brown return -ENODEV; 23826717172SLen Brown 239c4236282SLen Brown cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates); 24026717172SLen Brown 24126717172SLen Brown if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) || 24226717172SLen Brown !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) 24326717172SLen Brown return -ENODEV; 24426717172SLen Brown 245c4236282SLen Brown pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates); 24626717172SLen Brown 24726717172SLen Brown if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */ 24826717172SLen Brown lapic_timer_reliable_states = 0xFFFFFFFF; 24926717172SLen Brown 25026717172SLen Brown if (boot_cpu_data.x86 != 6) /* family 6 */ 25126717172SLen Brown return -ENODEV; 25226717172SLen Brown 25326717172SLen Brown switch (boot_cpu_data.x86_model) { 25426717172SLen Brown 25526717172SLen Brown case 0x1A: /* Core i7, Xeon 5500 series */ 25626717172SLen Brown case 0x1E: /* Core i7 and i5 Processor - Lynnfield Jasper Forest */ 25726717172SLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 25826717172SLen Brown case 0x2E: /* Nehalem-EX Xeon */ 259ec67a2baSLen Brown case 0x2F: /* Westmere-EX Xeon */ 26026717172SLen Brown lapic_timer_reliable_states = (1 << 1); /* C1 */ 26126717172SLen Brown 26226717172SLen Brown case 0x25: /* Westmere */ 26326717172SLen Brown case 0x2C: /* Westmere */ 26426717172SLen Brown cpuidle_state_table = nehalem_cstates; 26526717172SLen Brown break; 26626717172SLen Brown 26726717172SLen Brown case 0x1C: /* 28 - Atom Processor */ 26826717172SLen Brown lapic_timer_reliable_states = (1 << 2) | (1 << 1); /* C2, C1 */ 26926717172SLen Brown cpuidle_state_table = atom_cstates; 27026717172SLen Brown break; 27126717172SLen Brown #ifdef FUTURE_USE 27226717172SLen Brown case 0x17: /* 23 - Core 2 Duo */ 27326717172SLen Brown lapic_timer_reliable_states = (1 << 2) | (1 << 1); /* C2, C1 */ 27426717172SLen Brown #endif 27526717172SLen Brown 27626717172SLen Brown default: 27726717172SLen Brown pr_debug(PREFIX "does not run on family %d model %d\n", 27826717172SLen Brown boot_cpu_data.x86, boot_cpu_data.x86_model); 27926717172SLen Brown return -ENODEV; 28026717172SLen Brown } 28126717172SLen Brown 28226717172SLen Brown pr_debug(PREFIX "v" INTEL_IDLE_VERSION 28326717172SLen Brown " model 0x%X\n", boot_cpu_data.x86_model); 28426717172SLen Brown 28526717172SLen Brown pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n", 28626717172SLen Brown lapic_timer_reliable_states); 28726717172SLen Brown return 0; 28826717172SLen Brown } 28926717172SLen Brown 29026717172SLen Brown /* 29126717172SLen Brown * intel_idle_cpuidle_devices_uninit() 29226717172SLen Brown * unregister, free cpuidle_devices 29326717172SLen Brown */ 29426717172SLen Brown static void intel_idle_cpuidle_devices_uninit(void) 29526717172SLen Brown { 29626717172SLen Brown int i; 29726717172SLen Brown struct cpuidle_device *dev; 29826717172SLen Brown 29926717172SLen Brown for_each_online_cpu(i) { 30026717172SLen Brown dev = per_cpu_ptr(intel_idle_cpuidle_devices, i); 30126717172SLen Brown cpuidle_unregister_device(dev); 30226717172SLen Brown } 30326717172SLen Brown 30426717172SLen Brown free_percpu(intel_idle_cpuidle_devices); 30526717172SLen Brown return; 30626717172SLen Brown } 30726717172SLen Brown /* 30826717172SLen Brown * intel_idle_cpuidle_devices_init() 30926717172SLen Brown * allocate, initialize, register cpuidle_devices 31026717172SLen Brown */ 31126717172SLen Brown static int intel_idle_cpuidle_devices_init(void) 31226717172SLen Brown { 31326717172SLen Brown int i, cstate; 31426717172SLen Brown struct cpuidle_device *dev; 31526717172SLen Brown 31626717172SLen Brown intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device); 31726717172SLen Brown if (intel_idle_cpuidle_devices == NULL) 31826717172SLen Brown return -ENOMEM; 31926717172SLen Brown 32026717172SLen Brown for_each_online_cpu(i) { 32126717172SLen Brown dev = per_cpu_ptr(intel_idle_cpuidle_devices, i); 32226717172SLen Brown 32326717172SLen Brown dev->state_count = 1; 32426717172SLen Brown 32526717172SLen Brown for (cstate = 1; cstate < MWAIT_MAX_NUM_CSTATES; ++cstate) { 32626717172SLen Brown int num_substates; 32726717172SLen Brown 32826717172SLen Brown if (cstate > max_cstate) { 32926717172SLen Brown printk(PREFIX "max_cstate %d reached\n", 33026717172SLen Brown max_cstate); 33126717172SLen Brown break; 33226717172SLen Brown } 33326717172SLen Brown 33426717172SLen Brown /* does the state exist in CPUID.MWAIT? */ 335c4236282SLen Brown num_substates = (mwait_substates >> ((cstate) * 4)) 33626717172SLen Brown & MWAIT_SUBSTATE_MASK; 33726717172SLen Brown if (num_substates == 0) 33826717172SLen Brown continue; 33926717172SLen Brown /* is the state not enabled? */ 34026717172SLen Brown if (cpuidle_state_table[cstate].enter == NULL) { 34126717172SLen Brown /* does the driver not know about the state? */ 34226717172SLen Brown if (*cpuidle_state_table[cstate].name == '\0') 34326717172SLen Brown pr_debug(PREFIX "unaware of model 0x%x" 34426717172SLen Brown " MWAIT %d please" 34526717172SLen Brown " contact lenb@kernel.org", 34626717172SLen Brown boot_cpu_data.x86_model, cstate); 34726717172SLen Brown continue; 34826717172SLen Brown } 34926717172SLen Brown 35026717172SLen Brown if ((cstate > 2) && 35126717172SLen Brown !boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) 35226717172SLen Brown mark_tsc_unstable("TSC halts in idle" 35326717172SLen Brown " states deeper than C2"); 35426717172SLen Brown 35526717172SLen Brown dev->states[dev->state_count] = /* structure copy */ 35626717172SLen Brown cpuidle_state_table[cstate]; 35726717172SLen Brown 35826717172SLen Brown dev->state_count += 1; 35926717172SLen Brown } 36026717172SLen Brown 36126717172SLen Brown dev->cpu = i; 36226717172SLen Brown if (cpuidle_register_device(dev)) { 36326717172SLen Brown pr_debug(PREFIX "cpuidle_register_device %d failed!\n", 36426717172SLen Brown i); 36526717172SLen Brown intel_idle_cpuidle_devices_uninit(); 36626717172SLen Brown return -EIO; 36726717172SLen Brown } 36826717172SLen Brown } 36926717172SLen Brown 37026717172SLen Brown return 0; 37126717172SLen Brown } 37226717172SLen Brown 37326717172SLen Brown 37426717172SLen Brown static int __init intel_idle_init(void) 37526717172SLen Brown { 37626717172SLen Brown int retval; 37726717172SLen Brown 37826717172SLen Brown retval = intel_idle_probe(); 37926717172SLen Brown if (retval) 38026717172SLen Brown return retval; 38126717172SLen Brown 38226717172SLen Brown retval = cpuidle_register_driver(&intel_idle_driver); 38326717172SLen Brown if (retval) { 38426717172SLen Brown printk(KERN_DEBUG PREFIX "intel_idle yielding to %s", 38526717172SLen Brown cpuidle_get_driver()->name); 38626717172SLen Brown return retval; 38726717172SLen Brown } 38826717172SLen Brown 38926717172SLen Brown retval = intel_idle_cpuidle_devices_init(); 39026717172SLen Brown if (retval) { 39126717172SLen Brown cpuidle_unregister_driver(&intel_idle_driver); 39226717172SLen Brown return retval; 39326717172SLen Brown } 39426717172SLen Brown 39526717172SLen Brown return 0; 39626717172SLen Brown } 39726717172SLen Brown 39826717172SLen Brown static void __exit intel_idle_exit(void) 39926717172SLen Brown { 40026717172SLen Brown intel_idle_cpuidle_devices_uninit(); 40126717172SLen Brown cpuidle_unregister_driver(&intel_idle_driver); 40226717172SLen Brown 40326717172SLen Brown return; 40426717172SLen Brown } 40526717172SLen Brown 40626717172SLen Brown module_init(intel_idle_init); 40726717172SLen Brown module_exit(intel_idle_exit); 40826717172SLen Brown 40926717172SLen Brown module_param(max_cstate, int, 0444); 41026717172SLen Brown 41126717172SLen Brown MODULE_AUTHOR("Len Brown <len.brown@intel.com>"); 41226717172SLen Brown MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION); 41326717172SLen Brown MODULE_LICENSE("GPL"); 414