blob: 927cfb4d66f40961f9ab1c86048e5159a992d47b [file] [log] [blame]
Len Brown26717172010-03-08 14:07:30 -05001/*
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>
Len Brown26717172010-03-08 14:07:30 -050059#include <trace/events/power.h>
60#include <linux/sched.h>
Shaohua Li2a2d31c2011-01-10 09:38:12 +080061#include <linux/notifier.h>
62#include <linux/cpu.h>
Paul Gortmaker7c52d552011-05-27 12:33:10 -040063#include <linux/module.h>
Andi Kleenb66b8b92012-01-26 00:09:07 +010064#include <asm/cpu_device_id.h>
H. Peter Anvinbc83ccc2010-09-17 15:36:40 -070065#include <asm/mwait.h>
Len Brown14796fc2011-01-18 20:48:27 -050066#include <asm/msr.h>
Len Brown26717172010-03-08 14:07:30 -050067
68#define INTEL_IDLE_VERSION "0.4"
69#define PREFIX "intel_idle: "
70
Len Brown26717172010-03-08 14:07:30 -050071static struct cpuidle_driver intel_idle_driver = {
72 .name = "intel_idle",
73 .owner = THIS_MODULE,
Julius Wernera474a512012-11-27 14:17:58 +010074 .en_core_tk_irqen = 1,
Len Brown26717172010-03-08 14:07:30 -050075};
76/* intel_idle.max_cstate=0 disables driver */
Len Brown137ecc72013-02-01 21:35:35 -050077static int max_cstate = CPUIDLE_STATE_MAX - 1;
Len Brown26717172010-03-08 14:07:30 -050078
Len Brownc4236282010-05-28 02:22:03 -040079static unsigned int mwait_substates;
Len Brown26717172010-03-08 14:07:30 -050080
Shaohua Li2a2d31c2011-01-10 09:38:12 +080081#define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
Len Brown26717172010-03-08 14:07:30 -050082/* Reliable LAPIC Timer States, bit 1 for C1 etc. */
Len Brownd13780d2010-07-07 00:12:03 -040083static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
Len Brown26717172010-03-08 14:07:30 -050084
Andi Kleenb66b8b92012-01-26 00:09:07 +010085struct idle_cpu {
86 struct cpuidle_state *state_table;
87
88 /*
89 * Hardware C-state auto-demotion may not always be optimal.
90 * Indicate which enable bits to clear here.
91 */
92 unsigned long auto_demotion_disable_flags;
93};
94
95static const struct idle_cpu *icpu;
Namhyung Kim3265eba2010-08-08 03:10:03 +090096static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053097static int intel_idle(struct cpuidle_device *dev,
98 struct cpuidle_driver *drv, int index);
Daniel Lezcano25ac7762012-07-05 15:23:25 +020099static int intel_idle_cpu_init(int cpu);
Len Brown26717172010-03-08 14:07:30 -0500100
101static struct cpuidle_state *cpuidle_state_table;
102
103/*
Len Brown956d0332011-01-12 02:51:20 -0500104 * Set this flag for states where the HW flushes the TLB for us
105 * and so we don't need cross-calls to keep it consistent.
106 * If this flag is set, SW flushes the TLB, so even if the
107 * HW doesn't do the flushing, this flag is safe to use.
108 */
109#define CPUIDLE_FLAG_TLB_FLUSHED 0x10000
110
111/*
Len Brownb1beab42013-01-31 19:55:37 -0500112 * MWAIT takes an 8-bit "hint" in EAX "suggesting"
113 * the C-state (top nibble) and sub-state (bottom nibble)
114 * 0x00 means "MWAIT(C1)", 0x10 means "MWAIT(C2)" etc.
115 *
116 * We store the hint at the top of our "flags" for each state.
117 */
118#define flg2MWAIT(flags) (((flags) >> 24) & 0xFF)
119#define MWAIT2flg(eax) ((eax & 0xFF) << 24)
120
121/*
Len Brown26717172010-03-08 14:07:30 -0500122 * States are indexed by the cstate number,
123 * which is also the index into the MWAIT hint array.
124 * Thus C0 is a dummy.
125 */
Len Brown137ecc72013-02-01 21:35:35 -0500126static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
Len Browne022e7e2013-02-01 23:37:30 -0500127 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100128 .name = "C1-NHM",
Len Brown26717172010-03-08 14:07:30 -0500129 .desc = "MWAIT 0x00",
Len Brownb1beab42013-01-31 19:55:37 -0500130 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
Len Brown26717172010-03-08 14:07:30 -0500131 .exit_latency = 3,
Len Brown26717172010-03-08 14:07:30 -0500132 .target_residency = 6,
133 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500134 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100135 .name = "C3-NHM",
Len Brown26717172010-03-08 14:07:30 -0500136 .desc = "MWAIT 0x10",
Len Brownb1beab42013-01-31 19:55:37 -0500137 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown26717172010-03-08 14:07:30 -0500138 .exit_latency = 20,
Len Brown26717172010-03-08 14:07:30 -0500139 .target_residency = 80,
140 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500141 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100142 .name = "C6-NHM",
Len Brown26717172010-03-08 14:07:30 -0500143 .desc = "MWAIT 0x20",
Len Brownb1beab42013-01-31 19:55:37 -0500144 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown26717172010-03-08 14:07:30 -0500145 .exit_latency = 200,
Len Brown26717172010-03-08 14:07:30 -0500146 .target_residency = 800,
147 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500148 {
149 .enter = NULL }
Len Brown26717172010-03-08 14:07:30 -0500150};
151
Len Brown137ecc72013-02-01 21:35:35 -0500152static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
Len Browne022e7e2013-02-01 23:37:30 -0500153 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100154 .name = "C1-SNB",
Len Brownd13780d2010-07-07 00:12:03 -0400155 .desc = "MWAIT 0x00",
Len Brownb1beab42013-01-31 19:55:37 -0500156 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
Len Brownd13780d2010-07-07 00:12:03 -0400157 .exit_latency = 1,
Len Brownddbd5502010-12-13 18:28:22 -0500158 .target_residency = 1,
Len Brownd13780d2010-07-07 00:12:03 -0400159 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500160 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100161 .name = "C3-SNB",
Len Brownd13780d2010-07-07 00:12:03 -0400162 .desc = "MWAIT 0x10",
Len Brownb1beab42013-01-31 19:55:37 -0500163 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brownd13780d2010-07-07 00:12:03 -0400164 .exit_latency = 80,
Len Brownddbd5502010-12-13 18:28:22 -0500165 .target_residency = 211,
Len Brownd13780d2010-07-07 00:12:03 -0400166 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500167 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100168 .name = "C6-SNB",
Len Brownd13780d2010-07-07 00:12:03 -0400169 .desc = "MWAIT 0x20",
Len Brownb1beab42013-01-31 19:55:37 -0500170 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brownd13780d2010-07-07 00:12:03 -0400171 .exit_latency = 104,
Len Brownddbd5502010-12-13 18:28:22 -0500172 .target_residency = 345,
Len Brownd13780d2010-07-07 00:12:03 -0400173 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500174 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100175 .name = "C7-SNB",
Len Brownd13780d2010-07-07 00:12:03 -0400176 .desc = "MWAIT 0x30",
Len Brownb1beab42013-01-31 19:55:37 -0500177 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brownd13780d2010-07-07 00:12:03 -0400178 .exit_latency = 109,
Len Brownddbd5502010-12-13 18:28:22 -0500179 .target_residency = 345,
Len Brownd13780d2010-07-07 00:12:03 -0400180 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500181 {
182 .enter = NULL }
Len Brownd13780d2010-07-07 00:12:03 -0400183};
184
Len Brown137ecc72013-02-01 21:35:35 -0500185static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
Len Browne022e7e2013-02-01 23:37:30 -0500186 {
Len Brown6edab082012-06-01 19:45:32 -0400187 .name = "C1-IVB",
188 .desc = "MWAIT 0x00",
Len Brownb1beab42013-01-31 19:55:37 -0500189 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
Len Brown6edab082012-06-01 19:45:32 -0400190 .exit_latency = 1,
191 .target_residency = 1,
192 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500193 {
Len Brown6edab082012-06-01 19:45:32 -0400194 .name = "C3-IVB",
195 .desc = "MWAIT 0x10",
Len Brownb1beab42013-01-31 19:55:37 -0500196 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown6edab082012-06-01 19:45:32 -0400197 .exit_latency = 59,
198 .target_residency = 156,
199 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500200 {
Len Brown6edab082012-06-01 19:45:32 -0400201 .name = "C6-IVB",
202 .desc = "MWAIT 0x20",
Len Brownb1beab42013-01-31 19:55:37 -0500203 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown6edab082012-06-01 19:45:32 -0400204 .exit_latency = 80,
205 .target_residency = 300,
206 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500207 {
Len Brown6edab082012-06-01 19:45:32 -0400208 .name = "C7-IVB",
209 .desc = "MWAIT 0x30",
Len Brownb1beab42013-01-31 19:55:37 -0500210 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown6edab082012-06-01 19:45:32 -0400211 .exit_latency = 87,
212 .target_residency = 300,
213 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500214 {
215 .enter = NULL }
Len Brown6edab082012-06-01 19:45:32 -0400216};
217
Len Brown137ecc72013-02-01 21:35:35 -0500218static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
Len Browne022e7e2013-02-01 23:37:30 -0500219 {
Len Brown85a4d2d2013-01-31 14:40:49 -0500220 .name = "C1-HSW",
221 .desc = "MWAIT 0x00",
222 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
223 .exit_latency = 2,
224 .target_residency = 2,
225 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500226 {
Len Brown85a4d2d2013-01-31 14:40:49 -0500227 .name = "C3-HSW",
228 .desc = "MWAIT 0x10",
229 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
230 .exit_latency = 33,
231 .target_residency = 100,
232 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500233 {
Len Brown85a4d2d2013-01-31 14:40:49 -0500234 .name = "C6-HSW",
235 .desc = "MWAIT 0x20",
236 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
237 .exit_latency = 133,
238 .target_residency = 400,
239 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500240 {
Len Brown85a4d2d2013-01-31 14:40:49 -0500241 .name = "C7s-HSW",
242 .desc = "MWAIT 0x32",
243 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
244 .exit_latency = 166,
245 .target_residency = 500,
246 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500247 {
248 .enter = NULL }
Len Brown85a4d2d2013-01-31 14:40:49 -0500249};
250
Len Brown137ecc72013-02-01 21:35:35 -0500251static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] = {
Len Browne022e7e2013-02-01 23:37:30 -0500252 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100253 .name = "C1-ATM",
Len Brown26717172010-03-08 14:07:30 -0500254 .desc = "MWAIT 0x00",
Len Brownb1beab42013-01-31 19:55:37 -0500255 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
Len Brown26717172010-03-08 14:07:30 -0500256 .exit_latency = 1,
Len Brown26717172010-03-08 14:07:30 -0500257 .target_residency = 4,
258 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500259 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100260 .name = "C2-ATM",
Len Brown26717172010-03-08 14:07:30 -0500261 .desc = "MWAIT 0x10",
Len Brownb1beab42013-01-31 19:55:37 -0500262 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID,
Len Brown26717172010-03-08 14:07:30 -0500263 .exit_latency = 20,
Len Brown26717172010-03-08 14:07:30 -0500264 .target_residency = 80,
265 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500266 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100267 .name = "C4-ATM",
Len Brown26717172010-03-08 14:07:30 -0500268 .desc = "MWAIT 0x30",
Len Brownb1beab42013-01-31 19:55:37 -0500269 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown26717172010-03-08 14:07:30 -0500270 .exit_latency = 100,
Len Brown26717172010-03-08 14:07:30 -0500271 .target_residency = 400,
272 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500273 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100274 .name = "C6-ATM",
Len Brown7fcca7d2010-10-05 13:43:14 -0400275 .desc = "MWAIT 0x52",
Len Brownb1beab42013-01-31 19:55:37 -0500276 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown7fcca7d2010-10-05 13:43:14 -0400277 .exit_latency = 140,
Len Brown7fcca7d2010-10-05 13:43:14 -0400278 .target_residency = 560,
279 .enter = &intel_idle },
Len Browne022e7e2013-02-01 23:37:30 -0500280 {
281 .enter = NULL }
Len Brown26717172010-03-08 14:07:30 -0500282};
283
Len Brown26717172010-03-08 14:07:30 -0500284/**
285 * intel_idle
286 * @dev: cpuidle_device
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530287 * @drv: cpuidle driver
Deepthi Dharware978aa72011-10-28 16:20:09 +0530288 * @index: index of cpuidle state
Len Brown26717172010-03-08 14:07:30 -0500289 *
Yanmin Zhang63ff07b2012-01-10 15:48:21 -0800290 * Must be called under local_irq_disable().
Len Brown26717172010-03-08 14:07:30 -0500291 */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530292static int intel_idle(struct cpuidle_device *dev,
293 struct cpuidle_driver *drv, int index)
Len Brown26717172010-03-08 14:07:30 -0500294{
295 unsigned long ecx = 1; /* break on interrupt flag */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530296 struct cpuidle_state *state = &drv->states[index];
Len Brownb1beab42013-01-31 19:55:37 -0500297 unsigned long eax = flg2MWAIT(state->flags);
Len Brown26717172010-03-08 14:07:30 -0500298 unsigned int cstate;
Len Brown26717172010-03-08 14:07:30 -0500299 int cpu = smp_processor_id();
300
301 cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
302
Suresh Siddha6110a1f2010-09-30 21:19:07 -0400303 /*
Len Brownc8381cc2010-10-15 20:43:06 -0400304 * leave_mm() to avoid costly and often unnecessary wakeups
305 * for flushing the user TLB's associated with the active mm.
Suresh Siddha6110a1f2010-09-30 21:19:07 -0400306 */
Len Brownc8381cc2010-10-15 20:43:06 -0400307 if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
Suresh Siddha6110a1f2010-09-30 21:19:07 -0400308 leave_mm(cpu);
309
Len Brown26717172010-03-08 14:07:30 -0500310 if (!(lapic_timer_reliable_states & (1 << (cstate))))
311 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
312
Len Brown26717172010-03-08 14:07:30 -0500313 stop_critical_timings();
Len Brown26717172010-03-08 14:07:30 -0500314 if (!need_resched()) {
315
316 __monitor((void *)&current_thread_info()->flags, 0, 0);
317 smp_mb();
318 if (!need_resched())
319 __mwait(eax, ecx);
320 }
321
322 start_critical_timings();
323
Len Brown26717172010-03-08 14:07:30 -0500324 if (!(lapic_timer_reliable_states & (1 << (cstate))))
325 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
326
Deepthi Dharware978aa72011-10-28 16:20:09 +0530327 return index;
Len Brown26717172010-03-08 14:07:30 -0500328}
329
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800330static void __setup_broadcast_timer(void *arg)
331{
332 unsigned long reason = (unsigned long)arg;
333 int cpu = smp_processor_id();
334
335 reason = reason ?
336 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
337
338 clockevents_notify(reason, &cpu);
339}
340
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200341static int cpu_hotplug_notify(struct notifier_block *n,
342 unsigned long action, void *hcpu)
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800343{
344 int hotcpu = (unsigned long)hcpu;
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200345 struct cpuidle_device *dev;
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800346
347 switch (action & 0xf) {
348 case CPU_ONLINE:
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200349
350 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
351 smp_call_function_single(hotcpu, __setup_broadcast_timer,
352 (void *)true, 1);
353
354 /*
355 * Some systems can hotplug a cpu at runtime after
356 * the kernel has booted, we have to initialize the
357 * driver in this case
358 */
359 dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu);
360 if (!dev->registered)
361 intel_idle_cpu_init(hotcpu);
362
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800363 break;
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800364 }
365 return NOTIFY_OK;
366}
367
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200368static struct notifier_block cpu_hotplug_notifier = {
369 .notifier_call = cpu_hotplug_notify,
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800370};
371
Len Brown14796fc2011-01-18 20:48:27 -0500372static void auto_demotion_disable(void *dummy)
373{
374 unsigned long long msr_bits;
375
376 rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
Andi Kleenb66b8b92012-01-26 00:09:07 +0100377 msr_bits &= ~(icpu->auto_demotion_disable_flags);
Len Brown14796fc2011-01-18 20:48:27 -0500378 wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
379}
380
Andi Kleenb66b8b92012-01-26 00:09:07 +0100381static const struct idle_cpu idle_cpu_nehalem = {
382 .state_table = nehalem_cstates,
Andi Kleenb66b8b92012-01-26 00:09:07 +0100383 .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
384};
385
386static const struct idle_cpu idle_cpu_atom = {
387 .state_table = atom_cstates,
388};
389
390static const struct idle_cpu idle_cpu_lincroft = {
391 .state_table = atom_cstates,
392 .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
393};
394
395static const struct idle_cpu idle_cpu_snb = {
396 .state_table = snb_cstates,
397};
398
Len Brown6edab082012-06-01 19:45:32 -0400399static const struct idle_cpu idle_cpu_ivb = {
400 .state_table = ivb_cstates,
401};
402
Len Brown85a4d2d2013-01-31 14:40:49 -0500403static const struct idle_cpu idle_cpu_hsw = {
404 .state_table = hsw_cstates,
405};
406
Andi Kleenb66b8b92012-01-26 00:09:07 +0100407#define ICPU(model, cpu) \
408 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu }
409
410static const struct x86_cpu_id intel_idle_ids[] = {
411 ICPU(0x1a, idle_cpu_nehalem),
412 ICPU(0x1e, idle_cpu_nehalem),
413 ICPU(0x1f, idle_cpu_nehalem),
Ben Hutchings8bf11932012-02-16 04:13:14 +0000414 ICPU(0x25, idle_cpu_nehalem),
415 ICPU(0x2c, idle_cpu_nehalem),
416 ICPU(0x2e, idle_cpu_nehalem),
Andi Kleenb66b8b92012-01-26 00:09:07 +0100417 ICPU(0x1c, idle_cpu_atom),
418 ICPU(0x26, idle_cpu_lincroft),
Ben Hutchings8bf11932012-02-16 04:13:14 +0000419 ICPU(0x2f, idle_cpu_nehalem),
Andi Kleenb66b8b92012-01-26 00:09:07 +0100420 ICPU(0x2a, idle_cpu_snb),
421 ICPU(0x2d, idle_cpu_snb),
Len Brown6edab082012-06-01 19:45:32 -0400422 ICPU(0x3a, idle_cpu_ivb),
Len Brown23795e52012-09-26 22:28:21 -0400423 ICPU(0x3e, idle_cpu_ivb),
Len Brown85a4d2d2013-01-31 14:40:49 -0500424 ICPU(0x3c, idle_cpu_hsw),
425 ICPU(0x3f, idle_cpu_hsw),
426 ICPU(0x45, idle_cpu_hsw),
Andi Kleenb66b8b92012-01-26 00:09:07 +0100427 {}
428};
429MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
430
Len Brown26717172010-03-08 14:07:30 -0500431/*
432 * intel_idle_probe()
433 */
434static int intel_idle_probe(void)
435{
Len Brownc4236282010-05-28 02:22:03 -0400436 unsigned int eax, ebx, ecx;
Andi Kleenb66b8b92012-01-26 00:09:07 +0100437 const struct x86_cpu_id *id;
Len Brown26717172010-03-08 14:07:30 -0500438
439 if (max_cstate == 0) {
440 pr_debug(PREFIX "disabled\n");
441 return -EPERM;
442 }
443
Andi Kleenb66b8b92012-01-26 00:09:07 +0100444 id = x86_match_cpu(intel_idle_ids);
445 if (!id) {
446 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
447 boot_cpu_data.x86 == 6)
448 pr_debug(PREFIX "does not run on family %d model %d\n",
449 boot_cpu_data.x86, boot_cpu_data.x86_model);
Len Brown26717172010-03-08 14:07:30 -0500450 return -ENODEV;
Andi Kleenb66b8b92012-01-26 00:09:07 +0100451 }
Len Brown26717172010-03-08 14:07:30 -0500452
453 if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
454 return -ENODEV;
455
Len Brownc4236282010-05-28 02:22:03 -0400456 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
Len Brown26717172010-03-08 14:07:30 -0500457
458 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
Thomas Renninger5c2a9f02011-12-04 22:17:29 +0100459 !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
460 !mwait_substates)
Len Brown26717172010-03-08 14:07:30 -0500461 return -ENODEV;
Len Brown26717172010-03-08 14:07:30 -0500462
Len Brownc4236282010-05-28 02:22:03 -0400463 pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
Len Brown26717172010-03-08 14:07:30 -0500464
Andi Kleenb66b8b92012-01-26 00:09:07 +0100465 icpu = (const struct idle_cpu *)id->driver_data;
466 cpuidle_state_table = icpu->state_table;
Len Brown26717172010-03-08 14:07:30 -0500467
Len Brown56b9aea2010-12-02 01:19:32 -0500468 if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800469 lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200470 else
Shaohua Li39a74fd2012-01-10 15:48:19 -0800471 on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200472
Len Brown26717172010-03-08 14:07:30 -0500473 pr_debug(PREFIX "v" INTEL_IDLE_VERSION
474 " model 0x%X\n", boot_cpu_data.x86_model);
475
476 pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
477 lapic_timer_reliable_states);
478 return 0;
479}
480
481/*
482 * intel_idle_cpuidle_devices_uninit()
483 * unregister, free cpuidle_devices
484 */
485static void intel_idle_cpuidle_devices_uninit(void)
486{
487 int i;
488 struct cpuidle_device *dev;
489
490 for_each_online_cpu(i) {
491 dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
492 cpuidle_unregister_device(dev);
493 }
494
495 free_percpu(intel_idle_cpuidle_devices);
496 return;
497}
498/*
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530499 * intel_idle_cpuidle_driver_init()
500 * allocate, initialize cpuidle_states
501 */
502static int intel_idle_cpuidle_driver_init(void)
503{
504 int cstate;
505 struct cpuidle_driver *drv = &intel_idle_driver;
506
507 drv->state_count = 1;
508
Len Browne022e7e2013-02-01 23:37:30 -0500509 for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
510 int num_substates, mwait_hint, mwait_cstate, mwait_substate;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530511
Len Browne022e7e2013-02-01 23:37:30 -0500512 if (cpuidle_state_table[cstate].enter == NULL)
513 break;
514
515 if (cstate + 1 > max_cstate) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530516 printk(PREFIX "max_cstate %d reached\n",
517 max_cstate);
518 break;
519 }
520
Len Browne022e7e2013-02-01 23:37:30 -0500521 mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
522 mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
523 mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530524
Len Browne022e7e2013-02-01 23:37:30 -0500525 /* does the state exist in CPUID.MWAIT? */
526 num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
527 & MWAIT_SUBSTATE_MASK;
528
529 /* if sub-state in table is not enumerated by CPUID */
530 if ((mwait_substate + 1) > num_substates)
531 continue;
532
533 if (((mwait_cstate + 1) > 2) &&
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530534 !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
535 mark_tsc_unstable("TSC halts in idle"
536 " states deeper than C2");
537
538 drv->states[drv->state_count] = /* structure copy */
539 cpuidle_state_table[cstate];
540
541 drv->state_count += 1;
542 }
543
Andi Kleenb66b8b92012-01-26 00:09:07 +0100544 if (icpu->auto_demotion_disable_flags)
Shaohua Li39a74fd2012-01-10 15:48:19 -0800545 on_each_cpu(auto_demotion_disable, NULL, 1);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530546
547 return 0;
548}
549
550
551/*
Thomas Renninger65b7f832012-01-17 22:40:08 +0100552 * intel_idle_cpu_init()
Len Brown26717172010-03-08 14:07:30 -0500553 * allocate, initialize, register cpuidle_devices
Thomas Renninger65b7f832012-01-17 22:40:08 +0100554 * @cpu: cpu/core to initialize
Len Brown26717172010-03-08 14:07:30 -0500555 */
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200556static int intel_idle_cpu_init(int cpu)
Len Brown26717172010-03-08 14:07:30 -0500557{
Thomas Renninger65b7f832012-01-17 22:40:08 +0100558 int cstate;
Len Brown26717172010-03-08 14:07:30 -0500559 struct cpuidle_device *dev;
560
Thomas Renninger65b7f832012-01-17 22:40:08 +0100561 dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
Len Brown26717172010-03-08 14:07:30 -0500562
Thomas Renninger65b7f832012-01-17 22:40:08 +0100563 dev->state_count = 1;
Len Brown26717172010-03-08 14:07:30 -0500564
Len Browne022e7e2013-02-01 23:37:30 -0500565 for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
566 int num_substates, mwait_hint, mwait_cstate, mwait_substate;
Len Brown26717172010-03-08 14:07:30 -0500567
Len Browne022e7e2013-02-01 23:37:30 -0500568 if (cpuidle_state_table[cstate].enter == NULL)
569 continue;
570
571 if (cstate + 1 > max_cstate) {
Marcos Paulo de Souzadc716e92012-03-21 16:33:43 -0700572 printk(PREFIX "max_cstate %d reached\n", max_cstate);
Thomas Renninger65b7f832012-01-17 22:40:08 +0100573 break;
574 }
Len Brown26717172010-03-08 14:07:30 -0500575
Len Browne022e7e2013-02-01 23:37:30 -0500576 mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
577 mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
578 mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
579
Thomas Renninger65b7f832012-01-17 22:40:08 +0100580 /* does the state exist in CPUID.MWAIT? */
Len Browne022e7e2013-02-01 23:37:30 -0500581 num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
582 & MWAIT_SUBSTATE_MASK;
583
584 /* if sub-state in table is not enumerated by CPUID */
585 if ((mwait_substate + 1) > num_substates)
Thomas Renninger65b7f832012-01-17 22:40:08 +0100586 continue;
Len Brown26717172010-03-08 14:07:30 -0500587
Marcos Paulo de Souzadc716e92012-03-21 16:33:43 -0700588 dev->state_count += 1;
589 }
590
Thomas Renninger65b7f832012-01-17 22:40:08 +0100591 dev->cpu = cpu;
Len Brown26717172010-03-08 14:07:30 -0500592
Thomas Renninger65b7f832012-01-17 22:40:08 +0100593 if (cpuidle_register_device(dev)) {
594 pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
595 intel_idle_cpuidle_devices_uninit();
596 return -EIO;
Len Brown26717172010-03-08 14:07:30 -0500597 }
598
Andi Kleenb66b8b92012-01-26 00:09:07 +0100599 if (icpu->auto_demotion_disable_flags)
Thomas Renninger65b7f832012-01-17 22:40:08 +0100600 smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
601
Len Brown26717172010-03-08 14:07:30 -0500602 return 0;
603}
Len Brown26717172010-03-08 14:07:30 -0500604
605static int __init intel_idle_init(void)
606{
Thomas Renninger65b7f832012-01-17 22:40:08 +0100607 int retval, i;
Len Brown26717172010-03-08 14:07:30 -0500608
Thomas Renningerd1896042010-11-03 17:06:14 +0100609 /* Do not load intel_idle at all for now if idle= is passed */
610 if (boot_option_idle_override != IDLE_NO_OVERRIDE)
611 return -ENODEV;
612
Len Brown26717172010-03-08 14:07:30 -0500613 retval = intel_idle_probe();
614 if (retval)
615 return retval;
616
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530617 intel_idle_cpuidle_driver_init();
Len Brown26717172010-03-08 14:07:30 -0500618 retval = cpuidle_register_driver(&intel_idle_driver);
619 if (retval) {
Konrad Rzeszutek Wilk3735d522012-08-16 22:06:55 +0200620 struct cpuidle_driver *drv = cpuidle_get_driver();
Len Brown26717172010-03-08 14:07:30 -0500621 printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
Konrad Rzeszutek Wilk3735d522012-08-16 22:06:55 +0200622 drv ? drv->name : "none");
Len Brown26717172010-03-08 14:07:30 -0500623 return retval;
624 }
625
Thomas Renninger65b7f832012-01-17 22:40:08 +0100626 intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
627 if (intel_idle_cpuidle_devices == NULL)
628 return -ENOMEM;
629
630 for_each_online_cpu(i) {
631 retval = intel_idle_cpu_init(i);
632 if (retval) {
633 cpuidle_unregister_driver(&intel_idle_driver);
634 return retval;
635 }
Len Brown26717172010-03-08 14:07:30 -0500636 }
Konrad Rzeszutek Wilk6f8c2e72013-01-16 23:40:01 +0100637 register_cpu_notifier(&cpu_hotplug_notifier);
Len Brown26717172010-03-08 14:07:30 -0500638
639 return 0;
640}
641
642static void __exit intel_idle_exit(void)
643{
644 intel_idle_cpuidle_devices_uninit();
645 cpuidle_unregister_driver(&intel_idle_driver);
646
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200647
648 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
Shaohua Li39a74fd2012-01-10 15:48:19 -0800649 on_each_cpu(__setup_broadcast_timer, (void *)false, 1);
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200650 unregister_cpu_notifier(&cpu_hotplug_notifier);
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800651
Len Brown26717172010-03-08 14:07:30 -0500652 return;
653}
654
655module_init(intel_idle_init);
656module_exit(intel_idle_exit);
657
Len Brown26717172010-03-08 14:07:30 -0500658module_param(max_cstate, int, 0444);
Len Brown26717172010-03-08 14:07:30 -0500659
660MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
661MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
662MODULE_LICENSE("GPL");