aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--MAINTAINERS7
-rw-r--r--arch/x86/include/asm/thread_info.h4
-rw-r--r--drivers/Makefile2
-rw-r--r--drivers/acpi/acpi_pad.c9
-rw-r--r--drivers/acpi/processor_driver.c15
-rw-r--r--drivers/acpi/processor_idle.c28
-rw-r--r--drivers/cpuidle/cpuidle.c12
-rw-r--r--drivers/cpuidle/cpuidle.h1
-rw-r--r--drivers/cpuidle/driver.c16
-rw-r--r--drivers/cpuidle/sysfs.c5
-rw-r--r--drivers/idle/Kconfig11
-rw-r--r--drivers/idle/Makefile1
-rwxr-xr-xdrivers/idle/intel_idle.c461
-rw-r--r--include/linux/cpuidle.h8
14 files changed, 538 insertions, 42 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index 33047a605438..13608bd2e791 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2887,6 +2887,13 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git
2887S: Maintained 2887S: Maintained
2888F: drivers/input/ 2888F: drivers/input/
2889 2889
2890INTEL IDLE DRIVER
2891M: Len Brown <lenb@kernel.org>
2892L: linux-pm@lists.linux-foundation.org
2893T: git git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux-idle-2.6.git
2894S: Supported
2895F: drivers/idle/intel_idle.c
2896
2890INTEL FRAMEBUFFER DRIVER (excluding 810 and 815) 2897INTEL FRAMEBUFFER DRIVER (excluding 810 and 815)
2891M: Maik Broemme <mbroemme@plusserver.de> 2898M: Maik Broemme <mbroemme@plusserver.de>
2892L: linux-fbdev@vger.kernel.org 2899L: linux-fbdev@vger.kernel.org
diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
index 62ba9400cc43..f0b6e5dbc5a0 100644
--- a/arch/x86/include/asm/thread_info.h
+++ b/arch/x86/include/asm/thread_info.h
@@ -239,8 +239,8 @@ static inline struct thread_info *current_thread_info(void)
239#define TS_USEDFPU 0x0001 /* FPU was used by this task 239#define TS_USEDFPU 0x0001 /* FPU was used by this task
240 this quantum (SMP) */ 240 this quantum (SMP) */
241#define TS_COMPAT 0x0002 /* 32bit syscall active (64BIT)*/ 241#define TS_COMPAT 0x0002 /* 32bit syscall active (64BIT)*/
242#define TS_POLLING 0x0004 /* true if in idle loop 242#define TS_POLLING 0x0004 /* idle task polling need_resched,
243 and not sleeping */ 243 skip sending interrupt */
244#define TS_RESTORE_SIGMASK 0x0008 /* restore signal mask in do_signal() */ 244#define TS_RESTORE_SIGMASK 0x0008 /* restore signal mask in do_signal() */
245 245
246#define tsk_is_polling(t) (task_thread_info(t)->status & TS_POLLING) 246#define tsk_is_polling(t) (task_thread_info(t)->status & TS_POLLING)
diff --git a/drivers/Makefile b/drivers/Makefile
index f42a03029b7c..91874e048552 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_PCI) += pci/
10obj-$(CONFIG_PARISC) += parisc/ 10obj-$(CONFIG_PARISC) += parisc/
11obj-$(CONFIG_RAPIDIO) += rapidio/ 11obj-$(CONFIG_RAPIDIO) += rapidio/
12obj-y += video/ 12obj-y += video/
13obj-y += idle/
13obj-$(CONFIG_ACPI) += acpi/ 14obj-$(CONFIG_ACPI) += acpi/
14obj-$(CONFIG_SFI) += sfi/ 15obj-$(CONFIG_SFI) += sfi/
15# PnP must come after ACPI since it will eventually need to check if acpi 16# PnP must come after ACPI since it will eventually need to check if acpi
@@ -91,7 +92,6 @@ obj-$(CONFIG_EISA) += eisa/
91obj-y += lguest/ 92obj-y += lguest/
92obj-$(CONFIG_CPU_FREQ) += cpufreq/ 93obj-$(CONFIG_CPU_FREQ) += cpufreq/
93obj-$(CONFIG_CPU_IDLE) += cpuidle/ 94obj-$(CONFIG_CPU_IDLE) += cpuidle/
94obj-y += idle/
95obj-$(CONFIG_MMC) += mmc/ 95obj-$(CONFIG_MMC) += mmc/
96obj-$(CONFIG_MEMSTICK) += memstick/ 96obj-$(CONFIG_MEMSTICK) += memstick/
97obj-$(CONFIG_NEW_LEDS) += leds/ 97obj-$(CONFIG_NEW_LEDS) += leds/
diff --git a/drivers/acpi/acpi_pad.c b/drivers/acpi/acpi_pad.c
index f169e516a1af..d269a8f3329c 100644
--- a/drivers/acpi/acpi_pad.c
+++ b/drivers/acpi/acpi_pad.c
@@ -172,13 +172,6 @@ static int power_saving_thread(void *data)
172 172
173 do_sleep = 0; 173 do_sleep = 0;
174 174
175 current_thread_info()->status &= ~TS_POLLING;
176 /*
177 * TS_POLLING-cleared state must be visible before we test
178 * NEED_RESCHED:
179 */
180 smp_mb();
181
182 expire_time = jiffies + HZ * (100 - idle_pct) / 100; 175 expire_time = jiffies + HZ * (100 - idle_pct) / 100;
183 176
184 while (!need_resched()) { 177 while (!need_resched()) {
@@ -209,8 +202,6 @@ static int power_saving_thread(void *data)
209 } 202 }
210 } 203 }
211 204
212 current_thread_info()->status |= TS_POLLING;
213
214 /* 205 /*
215 * current sched_rt has threshold for rt task running time. 206 * current sched_rt has threshold for rt task running time.
216 * When a rt task uses 95% CPU time, the rt thread will be 207 * When a rt task uses 95% CPU time, the rt thread will be
diff --git a/drivers/acpi/processor_driver.c b/drivers/acpi/processor_driver.c
index 5675d9747e87..b1034a9ada4e 100644
--- a/drivers/acpi/processor_driver.c
+++ b/drivers/acpi/processor_driver.c
@@ -616,7 +616,8 @@ static int __cpuinit acpi_processor_add(struct acpi_device *device)
616 acpi_processor_get_limit_info(pr); 616 acpi_processor_get_limit_info(pr);
617 617
618 618
619 acpi_processor_power_init(pr, device); 619 if (cpuidle_get_driver() == &acpi_idle_driver)
620 acpi_processor_power_init(pr, device);
620 621
621 pr->cdev = thermal_cooling_device_register("Processor", device, 622 pr->cdev = thermal_cooling_device_register("Processor", device,
622 &processor_cooling_ops); 623 &processor_cooling_ops);
@@ -920,9 +921,14 @@ static int __init acpi_processor_init(void)
920 if (!acpi_processor_dir) 921 if (!acpi_processor_dir)
921 return -ENOMEM; 922 return -ENOMEM;
922#endif 923#endif
923 result = cpuidle_register_driver(&acpi_idle_driver); 924
924 if (result < 0) 925 if (!cpuidle_register_driver(&acpi_idle_driver)) {
925 goto out_proc; 926 printk(KERN_DEBUG "ACPI: %s registered with cpuidle\n",
927 acpi_idle_driver.name);
928 } else {
929 printk(KERN_DEBUG "ACPI: acpi_idle yielding to %s",
930 cpuidle_get_driver()->name);
931 }
926 932
927 result = acpi_bus_register_driver(&acpi_processor_driver); 933 result = acpi_bus_register_driver(&acpi_processor_driver);
928 if (result < 0) 934 if (result < 0)
@@ -941,7 +947,6 @@ static int __init acpi_processor_init(void)
941out_cpuidle: 947out_cpuidle:
942 cpuidle_unregister_driver(&acpi_idle_driver); 948 cpuidle_unregister_driver(&acpi_idle_driver);
943 949
944out_proc:
945#ifdef CONFIG_ACPI_PROCFS 950#ifdef CONFIG_ACPI_PROCFS
946 remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir); 951 remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
947#endif 952#endif
diff --git a/drivers/acpi/processor_idle.c b/drivers/acpi/processor_idle.c
index 13c6cb703f1d..2e8c27d48f2b 100644
--- a/drivers/acpi/processor_idle.c
+++ b/drivers/acpi/processor_idle.c
@@ -872,6 +872,7 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
872 return(acpi_idle_enter_c1(dev, state)); 872 return(acpi_idle_enter_c1(dev, state));
873 873
874 local_irq_disable(); 874 local_irq_disable();
875
875 if (cx->entry_method != ACPI_CSTATE_FFH) { 876 if (cx->entry_method != ACPI_CSTATE_FFH) {
876 current_thread_info()->status &= ~TS_POLLING; 877 current_thread_info()->status &= ~TS_POLLING;
877 /* 878 /*
@@ -879,12 +880,12 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
879 * NEED_RESCHED: 880 * NEED_RESCHED:
880 */ 881 */
881 smp_mb(); 882 smp_mb();
882 }
883 883
884 if (unlikely(need_resched())) { 884 if (unlikely(need_resched())) {
885 current_thread_info()->status |= TS_POLLING; 885 current_thread_info()->status |= TS_POLLING;
886 local_irq_enable(); 886 local_irq_enable();
887 return 0; 887 return 0;
888 }
888 } 889 }
889 890
890 /* 891 /*
@@ -911,7 +912,8 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
911 sched_clock_idle_wakeup_event(idle_time_ns); 912 sched_clock_idle_wakeup_event(idle_time_ns);
912 913
913 local_irq_enable(); 914 local_irq_enable();
914 current_thread_info()->status |= TS_POLLING; 915 if (cx->entry_method != ACPI_CSTATE_FFH)
916 current_thread_info()->status |= TS_POLLING;
915 917
916 cx->usage++; 918 cx->usage++;
917 919
@@ -962,6 +964,7 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
962 } 964 }
963 965
964 local_irq_disable(); 966 local_irq_disable();
967
965 if (cx->entry_method != ACPI_CSTATE_FFH) { 968 if (cx->entry_method != ACPI_CSTATE_FFH) {
966 current_thread_info()->status &= ~TS_POLLING; 969 current_thread_info()->status &= ~TS_POLLING;
967 /* 970 /*
@@ -969,12 +972,12 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
969 * NEED_RESCHED: 972 * NEED_RESCHED:
970 */ 973 */
971 smp_mb(); 974 smp_mb();
972 }
973 975
974 if (unlikely(need_resched())) { 976 if (unlikely(need_resched())) {
975 current_thread_info()->status |= TS_POLLING; 977 current_thread_info()->status |= TS_POLLING;
976 local_irq_enable(); 978 local_irq_enable();
977 return 0; 979 return 0;
980 }
978 } 981 }
979 982
980 acpi_unlazy_tlb(smp_processor_id()); 983 acpi_unlazy_tlb(smp_processor_id());
@@ -1028,7 +1031,8 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1028 sched_clock_idle_wakeup_event(idle_time_ns); 1031 sched_clock_idle_wakeup_event(idle_time_ns);
1029 1032
1030 local_irq_enable(); 1033 local_irq_enable();
1031 current_thread_info()->status |= TS_POLLING; 1034 if (cx->entry_method != ACPI_CSTATE_FFH)
1035 current_thread_info()->status |= TS_POLLING;
1032 1036
1033 cx->usage++; 1037 cx->usage++;
1034 1038
diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c
index 12fdd3987a36..199488576a05 100644
--- a/drivers/cpuidle/cpuidle.c
+++ b/drivers/cpuidle/cpuidle.c
@@ -156,7 +156,7 @@ int cpuidle_enable_device(struct cpuidle_device *dev)
156 156
157 if (dev->enabled) 157 if (dev->enabled)
158 return 0; 158 return 0;
159 if (!cpuidle_curr_driver || !cpuidle_curr_governor) 159 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
160 return -EIO; 160 return -EIO;
161 if (!dev->state_count) 161 if (!dev->state_count)
162 return -EINVAL; 162 return -EINVAL;
@@ -207,7 +207,7 @@ void cpuidle_disable_device(struct cpuidle_device *dev)
207{ 207{
208 if (!dev->enabled) 208 if (!dev->enabled)
209 return; 209 return;
210 if (!cpuidle_curr_driver || !cpuidle_curr_governor) 210 if (!cpuidle_get_driver() || !cpuidle_curr_governor)
211 return; 211 return;
212 212
213 dev->enabled = 0; 213 dev->enabled = 0;
@@ -271,10 +271,11 @@ static int __cpuidle_register_device(struct cpuidle_device *dev)
271{ 271{
272 int ret; 272 int ret;
273 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); 273 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
274 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
274 275
275 if (!sys_dev) 276 if (!sys_dev)
276 return -EINVAL; 277 return -EINVAL;
277 if (!try_module_get(cpuidle_curr_driver->owner)) 278 if (!try_module_get(cpuidle_driver->owner))
278 return -EINVAL; 279 return -EINVAL;
279 280
280 init_completion(&dev->kobj_unregister); 281 init_completion(&dev->kobj_unregister);
@@ -284,7 +285,7 @@ static int __cpuidle_register_device(struct cpuidle_device *dev)
284 per_cpu(cpuidle_devices, dev->cpu) = dev; 285 per_cpu(cpuidle_devices, dev->cpu) = dev;
285 list_add(&dev->device_list, &cpuidle_detected_devices); 286 list_add(&dev->device_list, &cpuidle_detected_devices);
286 if ((ret = cpuidle_add_sysfs(sys_dev))) { 287 if ((ret = cpuidle_add_sysfs(sys_dev))) {
287 module_put(cpuidle_curr_driver->owner); 288 module_put(cpuidle_driver->owner);
288 return ret; 289 return ret;
289 } 290 }
290 291
@@ -325,6 +326,7 @@ EXPORT_SYMBOL_GPL(cpuidle_register_device);
325void cpuidle_unregister_device(struct cpuidle_device *dev) 326void cpuidle_unregister_device(struct cpuidle_device *dev)
326{ 327{
327 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); 328 struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
329 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
328 330
329 if (dev->registered == 0) 331 if (dev->registered == 0)
330 return; 332 return;
@@ -340,7 +342,7 @@ void cpuidle_unregister_device(struct cpuidle_device *dev)
340 342
341 cpuidle_resume_and_unlock(); 343 cpuidle_resume_and_unlock();
342 344
343 module_put(cpuidle_curr_driver->owner); 345 module_put(cpuidle_driver->owner);
344} 346}
345 347
346EXPORT_SYMBOL_GPL(cpuidle_unregister_device); 348EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
diff --git a/drivers/cpuidle/cpuidle.h b/drivers/cpuidle/cpuidle.h
index 9476ba33ee2c..33e50d556f17 100644
--- a/drivers/cpuidle/cpuidle.h
+++ b/drivers/cpuidle/cpuidle.h
@@ -9,7 +9,6 @@
9 9
10/* For internal use only */ 10/* For internal use only */
11extern struct cpuidle_governor *cpuidle_curr_governor; 11extern struct cpuidle_governor *cpuidle_curr_governor;
12extern struct cpuidle_driver *cpuidle_curr_driver;
13extern struct list_head cpuidle_governors; 12extern struct list_head cpuidle_governors;
14extern struct list_head cpuidle_detected_devices; 13extern struct list_head cpuidle_detected_devices;
15extern struct mutex cpuidle_lock; 14extern struct mutex cpuidle_lock;
diff --git a/drivers/cpuidle/driver.c b/drivers/cpuidle/driver.c
index 2257004fe33d..fd1601e3d125 100644
--- a/drivers/cpuidle/driver.c
+++ b/drivers/cpuidle/driver.c
@@ -14,7 +14,7 @@
14 14
15#include "cpuidle.h" 15#include "cpuidle.h"
16 16
17struct cpuidle_driver *cpuidle_curr_driver; 17static struct cpuidle_driver *cpuidle_curr_driver;
18DEFINE_SPINLOCK(cpuidle_driver_lock); 18DEFINE_SPINLOCK(cpuidle_driver_lock);
19 19
20/** 20/**
@@ -40,13 +40,25 @@ int cpuidle_register_driver(struct cpuidle_driver *drv)
40EXPORT_SYMBOL_GPL(cpuidle_register_driver); 40EXPORT_SYMBOL_GPL(cpuidle_register_driver);
41 41
42/** 42/**
43 * cpuidle_get_driver - return the current driver
44 */
45struct cpuidle_driver *cpuidle_get_driver(void)
46{
47 return cpuidle_curr_driver;
48}
49EXPORT_SYMBOL_GPL(cpuidle_get_driver);
50
51/**
43 * cpuidle_unregister_driver - unregisters a driver 52 * cpuidle_unregister_driver - unregisters a driver
44 * @drv: the driver 53 * @drv: the driver
45 */ 54 */
46void cpuidle_unregister_driver(struct cpuidle_driver *drv) 55void cpuidle_unregister_driver(struct cpuidle_driver *drv)
47{ 56{
48 if (!drv) 57 if (drv != cpuidle_curr_driver) {
58 WARN(1, "invalid cpuidle_unregister_driver(%s)\n",
59 drv->name);
49 return; 60 return;
61 }
50 62
51 spin_lock(&cpuidle_driver_lock); 63 spin_lock(&cpuidle_driver_lock);
52 cpuidle_curr_driver = NULL; 64 cpuidle_curr_driver = NULL;
diff --git a/drivers/cpuidle/sysfs.c b/drivers/cpuidle/sysfs.c
index 0ba9c8b8ee74..0310ffaec9df 100644
--- a/drivers/cpuidle/sysfs.c
+++ b/drivers/cpuidle/sysfs.c
@@ -47,10 +47,11 @@ static ssize_t show_current_driver(struct sysdev_class *class,
47 char *buf) 47 char *buf)
48{ 48{
49 ssize_t ret; 49 ssize_t ret;
50 struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
50 51
51 spin_lock(&cpuidle_driver_lock); 52 spin_lock(&cpuidle_driver_lock);
52 if (cpuidle_curr_driver) 53 if (cpuidle_driver)
53 ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name); 54 ret = sprintf(buf, "%s\n", cpuidle_driver->name);
54 else 55 else
55 ret = sprintf(buf, "none\n"); 56 ret = sprintf(buf, "none\n");
56 spin_unlock(&cpuidle_driver_lock); 57 spin_unlock(&cpuidle_driver_lock);
diff --git a/drivers/idle/Kconfig b/drivers/idle/Kconfig
index f15e90a453d1..fb5c5186d4aa 100644
--- a/drivers/idle/Kconfig
+++ b/drivers/idle/Kconfig
@@ -1,3 +1,14 @@
1config INTEL_IDLE
2 tristate "Cpuidle Driver for Intel Processors"
3 depends on CPU_IDLE
4 depends on X86
5 depends on CPU_SUP_INTEL
6 depends on EXPERIMENTAL
7 help
8 Enable intel_idle, a cpuidle driver that includes knowledge of
9 native Intel hardware idle features. The acpi_idle driver
10 can be configured at the same time, in order to handle
11 processors intel_idle does not support.
1 12
2menu "Memory power savings" 13menu "Memory power savings"
3depends on X86_64 14depends on X86_64
diff --git a/drivers/idle/Makefile b/drivers/idle/Makefile
index 5f68fc377e21..23d295cf10f2 100644
--- a/drivers/idle/Makefile
+++ b/drivers/idle/Makefile
@@ -1,2 +1,3 @@
1obj-$(CONFIG_I7300_IDLE) += i7300_idle.o 1obj-$(CONFIG_I7300_IDLE) += i7300_idle.o
2obj-$(CONFIG_INTEL_IDLE) += intel_idle.o
2 3
diff --git a/drivers/idle/intel_idle.c b/drivers/idle/intel_idle.c
new file mode 100755
index 000000000000..54f0fb4cd5d2
--- /dev/null
+++ b/drivers/idle/intel_idle.c
@@ -0,0 +1,461 @@
1/*
2 * intel_idle.c - native hardware idle loop for modern Intel processors
3 *
4 * Copyright (c) 2010, Intel Corporation.
5 * Len Brown <len.brown@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21/*
22 * intel_idle is a cpuidle driver that loads on specific Intel processors
23 * in lieu of the legacy ACPI processor_idle driver. The intent is to
24 * make Linux more efficient on these processors, as intel_idle knows
25 * more than ACPI, as well as make Linux more immune to ACPI BIOS bugs.
26 */
27
28/*
29 * Design Assumptions
30 *
31 * All CPUs have same idle states as boot CPU
32 *
33 * Chipset BM_STS (bus master status) bit is a NOP
34 * for preventing entry into deep C-stats
35 */
36
37/*
38 * Known limitations
39 *
40 * The driver currently initializes for_each_online_cpu() upon modprobe.
41 * It it unaware of subsequent processors hot-added to the system.
42 * This means that if you boot with maxcpus=n and later online
43 * processors above n, those processors will use C1 only.
44 *
45 * ACPI has a .suspend hack to turn off deep c-statees during suspend
46 * to avoid complications with the lapic timer workaround.
47 * Have not seen issues with suspend, but may need same workaround here.
48 *
49 * There is currently no kernel-based automatic probing/loading mechanism
50 * if the driver is built as a module.
51 */
52
53/* un-comment DEBUG to enable pr_debug() statements */
54#define DEBUG
55
56#include <linux/kernel.h>
57#include <linux/cpuidle.h>
58#include <linux/clockchips.h>
59#include <linux/hrtimer.h> /* ktime_get_real() */
60#include <trace/events/power.h>
61#include <linux/sched.h>
62
63#define INTEL_IDLE_VERSION "0.4"
64#define PREFIX "intel_idle: "
65
66#define MWAIT_SUBSTATE_MASK (0xf)
67#define MWAIT_CSTATE_MASK (0xf)
68#define MWAIT_SUBSTATE_SIZE (4)
69#define MWAIT_MAX_NUM_CSTATES 8
70#define CPUID_MWAIT_LEAF (5)
71#define CPUID5_ECX_EXTENSIONS_SUPPORTED (0x1)
72#define CPUID5_ECX_INTERRUPT_BREAK (0x2)
73
74static struct cpuidle_driver intel_idle_driver = {
75 .name = "intel_idle",
76 .owner = THIS_MODULE,
77};
78/* intel_idle.max_cstate=0 disables driver */
79static int max_cstate = MWAIT_MAX_NUM_CSTATES - 1;
80static int power_policy = 7; /* 0 = max perf; 15 = max powersave */
81
82static unsigned int substates;
83static int (*choose_substate)(int);
84
85/* Reliable LAPIC Timer States, bit 1 for C1 etc. */
86static unsigned int lapic_timer_reliable_states;
87
88static struct cpuidle_device *intel_idle_cpuidle_devices;
89static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state);
90
91static struct cpuidle_state *cpuidle_state_table;
92
93/*
94 * States are indexed by the cstate number,
95 * which is also the index into the MWAIT hint array.
96 * Thus C0 is a dummy.
97 */
98static struct cpuidle_state nehalem_cstates[MWAIT_MAX_NUM_CSTATES] = {
99 { /* MWAIT C0 */ },
100 { /* MWAIT C1 */
101 .name = "NHM-C1",
102 .desc = "MWAIT 0x00",
103 .driver_data = (void *) 0x00,
104 .flags = CPUIDLE_FLAG_TIME_VALID,
105 .exit_latency = 3,
106 .power_usage = 1000,
107 .target_residency = 6,
108 .enter = &intel_idle },
109 { /* MWAIT C2 */
110 .name = "NHM-C3",
111 .desc = "MWAIT 0x10",
112 .driver_data = (void *) 0x10,
113 .flags = CPUIDLE_FLAG_TIME_VALID,
114 .exit_latency = 20,
115 .power_usage = 500,
116 .target_residency = 80,
117 .enter = &intel_idle },
118 { /* MWAIT C3 */
119 .name = "NHM-C6",
120 .desc = "MWAIT 0x20",
121 .driver_data = (void *) 0x20,
122 .flags = CPUIDLE_FLAG_TIME_VALID,
123 .exit_latency = 200,
124 .power_usage = 350,
125 .target_residency = 800,
126 .enter = &intel_idle },
127};
128
129static struct cpuidle_state atom_cstates[MWAIT_MAX_NUM_CSTATES] = {
130 { /* MWAIT C0 */ },
131 { /* MWAIT C1 */
132 .name = "ATM-C1",
133 .desc = "MWAIT 0x00",
134 .driver_data = (void *) 0x00,
135 .flags = CPUIDLE_FLAG_TIME_VALID,
136 .exit_latency = 1,
137 .power_usage = 1000,
138 .target_residency = 4,
139 .enter = &intel_idle },
140 { /* MWAIT C2 */
141 .name = "ATM-C2",
142 .desc = "MWAIT 0x10",
143 .driver_data = (void *) 0x10,
144 .flags = CPUIDLE_FLAG_TIME_VALID,
145 .exit_latency = 20,
146 .power_usage = 500,
147 .target_residency = 80,
148 .enter = &intel_idle },
149 { /* MWAIT C3 */ },
150 { /* MWAIT C4 */
151 .name = "ATM-C4",
152 .desc = "MWAIT 0x30",
153 .driver_data = (void *) 0x30,
154 .flags = CPUIDLE_FLAG_TIME_VALID,
155 .exit_latency = 100,
156 .power_usage = 250,
157 .target_residency = 400,
158 .enter = &intel_idle },
159 { /* MWAIT C5 */ },
160 { /* MWAIT C6 */
161 .name = "ATM-C6",
162 .desc = "MWAIT 0x40",
163 .driver_data = (void *) 0x40,
164 .flags = CPUIDLE_FLAG_TIME_VALID,
165 .exit_latency = 200,
166 .power_usage = 150,
167 .target_residency = 800,
168 .enter = NULL }, /* disabled */
169};
170
171/*
172 * choose_tunable_substate()
173 *
174 * Run-time decision on which C-state substate to invoke
175 * If power_policy = 0, choose shallowest substate (0)
176 * If power_policy = 15, choose deepest substate
177 * If power_policy = middle, choose middle substate etc.
178 */
179static int choose_tunable_substate(int cstate)
180{
181 unsigned int num_substates;
182 unsigned int substate_choice;
183
184 power_policy &= 0xF; /* valid range: 0-15 */
185 cstate &= 7; /* valid range: 0-7 */
186
187 num_substates = (substates >> ((cstate) * 4)) & MWAIT_SUBSTATE_MASK;
188
189 if (num_substates <= 1)
190 return 0;
191
192 substate_choice = ((power_policy + (power_policy + 1) *
193 (num_substates - 1)) / 16);
194
195 return substate_choice;
196}
197
198/*
199 * choose_zero_substate()
200 */
201static int choose_zero_substate(int cstate)
202{
203 return 0;
204}
205
206/**
207 * intel_idle
208 * @dev: cpuidle_device
209 * @state: cpuidle state
210 *
211 */
212static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
213{
214 unsigned long ecx = 1; /* break on interrupt flag */
215 unsigned long eax = (unsigned long)cpuidle_get_statedata(state);
216 unsigned int cstate;
217 ktime_t kt_before, kt_after;
218 s64 usec_delta;
219 int cpu = smp_processor_id();
220
221 cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
222
223 eax = eax + (choose_substate)(cstate);
224
225 local_irq_disable();
226
227 if (!(lapic_timer_reliable_states & (1 << (cstate))))
228 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
229
230 kt_before = ktime_get_real();
231
232 stop_critical_timings();
233#ifndef MODULE
234 trace_power_start(POWER_CSTATE, (eax >> 4) + 1);
235#endif
236 if (!need_resched()) {
237
238 __monitor((void *)&current_thread_info()->flags, 0, 0);
239 smp_mb();
240 if (!need_resched())
241 __mwait(eax, ecx);
242 }
243
244 start_critical_timings();
245
246 kt_after = ktime_get_real();
247 usec_delta = ktime_to_us(ktime_sub(kt_after, kt_before));
248
249 local_irq_enable();
250
251 if (!(lapic_timer_reliable_states & (1 << (cstate))))
252 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
253
254 return usec_delta;
255}
256
257/*
258 * intel_idle_probe()
259 */
260static int intel_idle_probe(void)
261{
262 unsigned int eax, ebx, ecx, edx;
263
264 if (max_cstate == 0) {
265 pr_debug(PREFIX "disabled\n");
266 return -EPERM;
267 }
268
269 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
270 return -ENODEV;
271
272 if (!boot_cpu_has(X86_FEATURE_MWAIT))
273 return -ENODEV;
274
275 if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
276 return -ENODEV;
277
278 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
279
280 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
281 !(ecx & CPUID5_ECX_INTERRUPT_BREAK))
282 return -ENODEV;
283#ifdef DEBUG
284 if (substates == 0) /* can over-ride via modparam */
285#endif
286 substates = edx;
287
288 pr_debug(PREFIX "MWAIT substates: 0x%x\n", substates);
289
290 if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
291 lapic_timer_reliable_states = 0xFFFFFFFF;
292
293 if (boot_cpu_data.x86 != 6) /* family 6 */
294 return -ENODEV;
295
296 switch (boot_cpu_data.x86_model) {
297
298 case 0x1A: /* Core i7, Xeon 5500 series */
299 case 0x1E: /* Core i7 and i5 Processor - Lynnfield Jasper Forest */
300 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
301 case 0x2E: /* Nehalem-EX Xeon */
302 lapic_timer_reliable_states = (1 << 1); /* C1 */
303
304 case 0x25: /* Westmere */
305 case 0x2C: /* Westmere */
306 cpuidle_state_table = nehalem_cstates;
307 choose_substate = choose_tunable_substate;
308 break;
309
310 case 0x1C: /* 28 - Atom Processor */
311 lapic_timer_reliable_states = (1 << 2) | (1 << 1); /* C2, C1 */
312 cpuidle_state_table = atom_cstates;
313 choose_substate = choose_zero_substate;
314 break;
315#ifdef FUTURE_USE
316 case 0x17: /* 23 - Core 2 Duo */
317 lapic_timer_reliable_states = (1 << 2) | (1 << 1); /* C2, C1 */
318#endif
319
320 default:
321 pr_debug(PREFIX "does not run on family %d model %d\n",
322 boot_cpu_data.x86, boot_cpu_data.x86_model);
323 return -ENODEV;
324 }
325
326 pr_debug(PREFIX "v" INTEL_IDLE_VERSION
327 " model 0x%X\n", boot_cpu_data.x86_model);
328
329 pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
330 lapic_timer_reliable_states);
331 return 0;
332}
333
334/*
335 * intel_idle_cpuidle_devices_uninit()
336 * unregister, free cpuidle_devices
337 */
338static void intel_idle_cpuidle_devices_uninit(void)
339{
340 int i;
341 struct cpuidle_device *dev;
342
343 for_each_online_cpu(i) {
344 dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
345 cpuidle_unregister_device(dev);
346 }
347
348 free_percpu(intel_idle_cpuidle_devices);
349 return;
350}
351/*
352 * intel_idle_cpuidle_devices_init()
353 * allocate, initialize, register cpuidle_devices
354 */
355static int intel_idle_cpuidle_devices_init(void)
356{
357 int i, cstate;
358 struct cpuidle_device *dev;
359
360 intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
361 if (intel_idle_cpuidle_devices == NULL)
362 return -ENOMEM;
363
364 for_each_online_cpu(i) {
365 dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
366
367 dev->state_count = 1;
368
369 for (cstate = 1; cstate < MWAIT_MAX_NUM_CSTATES; ++cstate) {
370 int num_substates;
371
372 if (cstate > max_cstate) {
373 printk(PREFIX "max_cstate %d reached\n",
374 max_cstate);
375 break;
376 }
377
378 /* does the state exist in CPUID.MWAIT? */
379 num_substates = (substates >> ((cstate) * 4))
380 & MWAIT_SUBSTATE_MASK;
381 if (num_substates == 0)
382 continue;
383 /* is the state not enabled? */
384 if (cpuidle_state_table[cstate].enter == NULL) {
385 /* does the driver not know about the state? */
386 if (*cpuidle_state_table[cstate].name == '\0')
387 pr_debug(PREFIX "unaware of model 0x%x"
388 " MWAIT %d please"
389 " contact lenb@kernel.org",
390 boot_cpu_data.x86_model, cstate);
391 continue;
392 }
393
394 if ((cstate > 2) &&
395 !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
396 mark_tsc_unstable("TSC halts in idle"
397 " states deeper than C2");
398
399 dev->states[dev->state_count] = /* structure copy */
400 cpuidle_state_table[cstate];
401
402 dev->state_count += 1;
403 }
404
405 dev->cpu = i;
406 if (cpuidle_register_device(dev)) {
407 pr_debug(PREFIX "cpuidle_register_device %d failed!\n",
408 i);
409 intel_idle_cpuidle_devices_uninit();
410 return -EIO;
411 }
412 }
413
414 return 0;
415}
416
417
418static int __init intel_idle_init(void)
419{
420 int retval;
421
422 retval = intel_idle_probe();
423 if (retval)
424 return retval;
425
426 retval = cpuidle_register_driver(&intel_idle_driver);
427 if (retval) {
428 printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
429 cpuidle_get_driver()->name);
430 return retval;
431 }
432
433 retval = intel_idle_cpuidle_devices_init();
434 if (retval) {
435 cpuidle_unregister_driver(&intel_idle_driver);
436 return retval;
437 }
438
439 return 0;
440}
441
442static void __exit intel_idle_exit(void)
443{
444 intel_idle_cpuidle_devices_uninit();
445 cpuidle_unregister_driver(&intel_idle_driver);
446
447 return;
448}
449
450module_init(intel_idle_init);
451module_exit(intel_idle_exit);
452
453module_param(power_policy, int, 0644);
454module_param(max_cstate, int, 0444);
455#ifdef DEBUG
456module_param(substates, int, 0444);
457#endif
458
459MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
460MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
461MODULE_LICENSE("GPL");
diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h
index dcf77fa826b5..55215cce5005 100644
--- a/include/linux/cpuidle.h
+++ b/include/linux/cpuidle.h
@@ -125,6 +125,7 @@ struct cpuidle_driver {
125#ifdef CONFIG_CPU_IDLE 125#ifdef CONFIG_CPU_IDLE
126 126
127extern int cpuidle_register_driver(struct cpuidle_driver *drv); 127extern int cpuidle_register_driver(struct cpuidle_driver *drv);
128struct cpuidle_driver *cpuidle_get_driver(void);
128extern void cpuidle_unregister_driver(struct cpuidle_driver *drv); 129extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
129extern int cpuidle_register_device(struct cpuidle_device *dev); 130extern int cpuidle_register_device(struct cpuidle_device *dev);
130extern void cpuidle_unregister_device(struct cpuidle_device *dev); 131extern void cpuidle_unregister_device(struct cpuidle_device *dev);
@@ -137,16 +138,17 @@ extern void cpuidle_disable_device(struct cpuidle_device *dev);
137#else 138#else
138 139
139static inline int cpuidle_register_driver(struct cpuidle_driver *drv) 140static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
140{return 0;} 141{return -ENODEV; }
142static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
141static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { } 143static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
142static inline int cpuidle_register_device(struct cpuidle_device *dev) 144static inline int cpuidle_register_device(struct cpuidle_device *dev)
143{return 0;} 145{return -ENODEV; }
144static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { } 146static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
145 147
146static inline void cpuidle_pause_and_lock(void) { } 148static inline void cpuidle_pause_and_lock(void) { }
147static inline void cpuidle_resume_and_unlock(void) { } 149static inline void cpuidle_resume_and_unlock(void) { }
148static inline int cpuidle_enable_device(struct cpuidle_device *dev) 150static inline int cpuidle_enable_device(struct cpuidle_device *dev)
149{return 0;} 151{return -ENODEV; }
150static inline void cpuidle_disable_device(struct cpuidle_device *dev) { } 152static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
151 153
152#endif 154#endif