blob: 93f84f7f0586aa01a48b8e088d5567b9a2061704 [file] [log] [blame]
Len Brown26717172010-03-08 14:07:30 -05001/*
2 * intel_idle.c - native hardware idle loop for modern Intel processors
3 *
Len Brownfab04b22013-11-09 00:30:17 -05004 * Copyright (c) 2013, Intel Corporation.
Len Brown26717172010-03-08 14:07:30 -05005 * 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>
Thomas Gleixner76962ca2015-04-03 02:02:34 +020058#include <linux/tick.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,
74};
75/* intel_idle.max_cstate=0 disables driver */
Len Brown137ecc72013-02-01 21:35:35 -050076static int max_cstate = CPUIDLE_STATE_MAX - 1;
Len Brown26717172010-03-08 14:07:30 -050077
Len Brownc4236282010-05-28 02:22:03 -040078static unsigned int mwait_substates;
Len Brown26717172010-03-08 14:07:30 -050079
Shaohua Li2a2d31c2011-01-10 09:38:12 +080080#define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
Len Brown26717172010-03-08 14:07:30 -050081/* Reliable LAPIC Timer States, bit 1 for C1 etc. */
Len Brownd13780d2010-07-07 00:12:03 -040082static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
Len Brown26717172010-03-08 14:07:30 -050083
Andi Kleenb66b8b92012-01-26 00:09:07 +010084struct idle_cpu {
85 struct cpuidle_state *state_table;
86
87 /*
88 * Hardware C-state auto-demotion may not always be optimal.
89 * Indicate which enable bits to clear here.
90 */
91 unsigned long auto_demotion_disable_flags;
Len Brown8c058d532014-07-31 15:21:24 -040092 bool byt_auto_demotion_disable_flag;
Len Brown32e95182013-02-02 01:31:56 -050093 bool disable_promotion_to_c1e;
Andi Kleenb66b8b92012-01-26 00:09:07 +010094};
95
96static const struct idle_cpu *icpu;
Namhyung Kim3265eba2010-08-08 03:10:03 +090097static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053098static int intel_idle(struct cpuidle_device *dev,
99 struct cpuidle_driver *drv, int index);
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100100static void intel_idle_freeze(struct cpuidle_device *dev,
101 struct cpuidle_driver *drv, int index);
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200102static int intel_idle_cpu_init(int cpu);
Len Brown26717172010-03-08 14:07:30 -0500103
104static struct cpuidle_state *cpuidle_state_table;
105
106/*
Len Brown956d0332011-01-12 02:51:20 -0500107 * Set this flag for states where the HW flushes the TLB for us
108 * and so we don't need cross-calls to keep it consistent.
109 * If this flag is set, SW flushes the TLB, so even if the
110 * HW doesn't do the flushing, this flag is safe to use.
111 */
112#define CPUIDLE_FLAG_TLB_FLUSHED 0x10000
113
114/*
Len Brownb1beab42013-01-31 19:55:37 -0500115 * MWAIT takes an 8-bit "hint" in EAX "suggesting"
116 * the C-state (top nibble) and sub-state (bottom nibble)
117 * 0x00 means "MWAIT(C1)", 0x10 means "MWAIT(C2)" etc.
118 *
119 * We store the hint at the top of our "flags" for each state.
120 */
121#define flg2MWAIT(flags) (((flags) >> 24) & 0xFF)
122#define MWAIT2flg(eax) ((eax & 0xFF) << 24)
123
124/*
Len Brown26717172010-03-08 14:07:30 -0500125 * States are indexed by the cstate number,
126 * which is also the index into the MWAIT hint array.
127 * Thus C0 is a dummy.
128 */
Jiang Liuba0dc812014-01-09 15:30:26 +0800129static struct cpuidle_state nehalem_cstates[] = {
Len Browne022e7e2013-02-01 23:37:30 -0500130 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100131 .name = "C1-NHM",
Len Brown26717172010-03-08 14:07:30 -0500132 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100133 .flags = MWAIT2flg(0x00),
Len Brown26717172010-03-08 14:07:30 -0500134 .exit_latency = 3,
Len Brown26717172010-03-08 14:07:30 -0500135 .target_residency = 6,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100136 .enter = &intel_idle,
137 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500138 {
Len Brown32e95182013-02-02 01:31:56 -0500139 .name = "C1E-NHM",
140 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100141 .flags = MWAIT2flg(0x01),
Len Brown32e95182013-02-02 01:31:56 -0500142 .exit_latency = 10,
143 .target_residency = 20,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100144 .enter = &intel_idle,
145 .enter_freeze = intel_idle_freeze, },
Len Brown32e95182013-02-02 01:31:56 -0500146 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100147 .name = "C3-NHM",
Len Brown26717172010-03-08 14:07:30 -0500148 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100149 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown26717172010-03-08 14:07:30 -0500150 .exit_latency = 20,
Len Brown26717172010-03-08 14:07:30 -0500151 .target_residency = 80,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100152 .enter = &intel_idle,
153 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500154 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100155 .name = "C6-NHM",
Len Brown26717172010-03-08 14:07:30 -0500156 .desc = "MWAIT 0x20",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100157 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown26717172010-03-08 14:07:30 -0500158 .exit_latency = 200,
Len Brown26717172010-03-08 14:07:30 -0500159 .target_residency = 800,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100160 .enter = &intel_idle,
161 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500162 {
163 .enter = NULL }
Len Brown26717172010-03-08 14:07:30 -0500164};
165
Jiang Liuba0dc812014-01-09 15:30:26 +0800166static struct cpuidle_state snb_cstates[] = {
Len Browne022e7e2013-02-01 23:37:30 -0500167 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100168 .name = "C1-SNB",
Len Brownd13780d2010-07-07 00:12:03 -0400169 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100170 .flags = MWAIT2flg(0x00),
Len Brown32e95182013-02-02 01:31:56 -0500171 .exit_latency = 2,
172 .target_residency = 2,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100173 .enter = &intel_idle,
174 .enter_freeze = intel_idle_freeze, },
Len Brown32e95182013-02-02 01:31:56 -0500175 {
176 .name = "C1E-SNB",
177 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100178 .flags = MWAIT2flg(0x01),
Len Brown32e95182013-02-02 01:31:56 -0500179 .exit_latency = 10,
180 .target_residency = 20,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100181 .enter = &intel_idle,
182 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500183 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100184 .name = "C3-SNB",
Len Brownd13780d2010-07-07 00:12:03 -0400185 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100186 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brownd13780d2010-07-07 00:12:03 -0400187 .exit_latency = 80,
Len Brownddbd5502010-12-13 18:28:22 -0500188 .target_residency = 211,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100189 .enter = &intel_idle,
190 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500191 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100192 .name = "C6-SNB",
Len Brownd13780d2010-07-07 00:12:03 -0400193 .desc = "MWAIT 0x20",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100194 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brownd13780d2010-07-07 00:12:03 -0400195 .exit_latency = 104,
Len Brownddbd5502010-12-13 18:28:22 -0500196 .target_residency = 345,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100197 .enter = &intel_idle,
198 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500199 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100200 .name = "C7-SNB",
Len Brownd13780d2010-07-07 00:12:03 -0400201 .desc = "MWAIT 0x30",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100202 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brownd13780d2010-07-07 00:12:03 -0400203 .exit_latency = 109,
Len Brownddbd5502010-12-13 18:28:22 -0500204 .target_residency = 345,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100205 .enter = &intel_idle,
206 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500207 {
208 .enter = NULL }
Len Brownd13780d2010-07-07 00:12:03 -0400209};
210
Len Brown718987d2014-02-14 02:30:00 -0500211static struct cpuidle_state byt_cstates[] = {
212 {
213 .name = "C1-BYT",
214 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100215 .flags = MWAIT2flg(0x00),
Len Brown718987d2014-02-14 02:30:00 -0500216 .exit_latency = 1,
217 .target_residency = 1,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100218 .enter = &intel_idle,
219 .enter_freeze = intel_idle_freeze, },
Len Brown718987d2014-02-14 02:30:00 -0500220 {
221 .name = "C1E-BYT",
222 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100223 .flags = MWAIT2flg(0x01),
Len Brown718987d2014-02-14 02:30:00 -0500224 .exit_latency = 15,
225 .target_residency = 30,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100226 .enter = &intel_idle,
227 .enter_freeze = intel_idle_freeze, },
Len Brown718987d2014-02-14 02:30:00 -0500228 {
229 .name = "C6N-BYT",
230 .desc = "MWAIT 0x58",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100231 .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown718987d2014-02-14 02:30:00 -0500232 .exit_latency = 40,
233 .target_residency = 275,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100234 .enter = &intel_idle,
235 .enter_freeze = intel_idle_freeze, },
Len Brown718987d2014-02-14 02:30:00 -0500236 {
237 .name = "C6S-BYT",
238 .desc = "MWAIT 0x52",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100239 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown718987d2014-02-14 02:30:00 -0500240 .exit_latency = 140,
241 .target_residency = 560,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100242 .enter = &intel_idle,
243 .enter_freeze = intel_idle_freeze, },
Len Brown718987d2014-02-14 02:30:00 -0500244 {
245 .name = "C7-BYT",
246 .desc = "MWAIT 0x60",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100247 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown718987d2014-02-14 02:30:00 -0500248 .exit_latency = 1200,
249 .target_residency = 1500,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100250 .enter = &intel_idle,
251 .enter_freeze = intel_idle_freeze, },
Len Brown718987d2014-02-14 02:30:00 -0500252 {
253 .name = "C7S-BYT",
254 .desc = "MWAIT 0x64",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100255 .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown718987d2014-02-14 02:30:00 -0500256 .exit_latency = 10000,
257 .target_residency = 20000,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100258 .enter = &intel_idle,
259 .enter_freeze = intel_idle_freeze, },
Len Brown718987d2014-02-14 02:30:00 -0500260 {
261 .enter = NULL }
262};
263
Jiang Liuba0dc812014-01-09 15:30:26 +0800264static struct cpuidle_state ivb_cstates[] = {
Len Browne022e7e2013-02-01 23:37:30 -0500265 {
Len Brown6edab082012-06-01 19:45:32 -0400266 .name = "C1-IVB",
267 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100268 .flags = MWAIT2flg(0x00),
Len Brown6edab082012-06-01 19:45:32 -0400269 .exit_latency = 1,
270 .target_residency = 1,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100271 .enter = &intel_idle,
272 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500273 {
Len Brown32e95182013-02-02 01:31:56 -0500274 .name = "C1E-IVB",
275 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100276 .flags = MWAIT2flg(0x01),
Len Brown32e95182013-02-02 01:31:56 -0500277 .exit_latency = 10,
278 .target_residency = 20,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100279 .enter = &intel_idle,
280 .enter_freeze = intel_idle_freeze, },
Len Brown32e95182013-02-02 01:31:56 -0500281 {
Len Brown6edab082012-06-01 19:45:32 -0400282 .name = "C3-IVB",
283 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100284 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown6edab082012-06-01 19:45:32 -0400285 .exit_latency = 59,
286 .target_residency = 156,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100287 .enter = &intel_idle,
288 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500289 {
Len Brown6edab082012-06-01 19:45:32 -0400290 .name = "C6-IVB",
291 .desc = "MWAIT 0x20",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100292 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown6edab082012-06-01 19:45:32 -0400293 .exit_latency = 80,
294 .target_residency = 300,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100295 .enter = &intel_idle,
296 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500297 {
Len Brown6edab082012-06-01 19:45:32 -0400298 .name = "C7-IVB",
299 .desc = "MWAIT 0x30",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100300 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown6edab082012-06-01 19:45:32 -0400301 .exit_latency = 87,
302 .target_residency = 300,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100303 .enter = &intel_idle,
304 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500305 {
306 .enter = NULL }
Len Brown6edab082012-06-01 19:45:32 -0400307};
308
Len Brown0138d8f2014-04-04 01:21:07 -0400309static struct cpuidle_state ivt_cstates[] = {
310 {
311 .name = "C1-IVT",
312 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100313 .flags = MWAIT2flg(0x00),
Len Brown0138d8f2014-04-04 01:21:07 -0400314 .exit_latency = 1,
315 .target_residency = 1,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100316 .enter = &intel_idle,
317 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400318 {
319 .name = "C1E-IVT",
320 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100321 .flags = MWAIT2flg(0x01),
Len Brown0138d8f2014-04-04 01:21:07 -0400322 .exit_latency = 10,
323 .target_residency = 80,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100324 .enter = &intel_idle,
325 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400326 {
327 .name = "C3-IVT",
328 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100329 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown0138d8f2014-04-04 01:21:07 -0400330 .exit_latency = 59,
331 .target_residency = 156,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100332 .enter = &intel_idle,
333 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400334 {
335 .name = "C6-IVT",
336 .desc = "MWAIT 0x20",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100337 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown0138d8f2014-04-04 01:21:07 -0400338 .exit_latency = 82,
339 .target_residency = 300,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100340 .enter = &intel_idle,
341 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400342 {
343 .enter = NULL }
344};
345
346static struct cpuidle_state ivt_cstates_4s[] = {
347 {
348 .name = "C1-IVT-4S",
349 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100350 .flags = MWAIT2flg(0x00),
Len Brown0138d8f2014-04-04 01:21:07 -0400351 .exit_latency = 1,
352 .target_residency = 1,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100353 .enter = &intel_idle,
354 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400355 {
356 .name = "C1E-IVT-4S",
357 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100358 .flags = MWAIT2flg(0x01),
Len Brown0138d8f2014-04-04 01:21:07 -0400359 .exit_latency = 10,
360 .target_residency = 250,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100361 .enter = &intel_idle,
362 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400363 {
364 .name = "C3-IVT-4S",
365 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100366 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown0138d8f2014-04-04 01:21:07 -0400367 .exit_latency = 59,
368 .target_residency = 300,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100369 .enter = &intel_idle,
370 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400371 {
372 .name = "C6-IVT-4S",
373 .desc = "MWAIT 0x20",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100374 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown0138d8f2014-04-04 01:21:07 -0400375 .exit_latency = 84,
376 .target_residency = 400,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100377 .enter = &intel_idle,
378 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400379 {
380 .enter = NULL }
381};
382
383static struct cpuidle_state ivt_cstates_8s[] = {
384 {
385 .name = "C1-IVT-8S",
386 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100387 .flags = MWAIT2flg(0x00),
Len Brown0138d8f2014-04-04 01:21:07 -0400388 .exit_latency = 1,
389 .target_residency = 1,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100390 .enter = &intel_idle,
391 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400392 {
393 .name = "C1E-IVT-8S",
394 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100395 .flags = MWAIT2flg(0x01),
Len Brown0138d8f2014-04-04 01:21:07 -0400396 .exit_latency = 10,
397 .target_residency = 500,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100398 .enter = &intel_idle,
399 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400400 {
401 .name = "C3-IVT-8S",
402 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100403 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown0138d8f2014-04-04 01:21:07 -0400404 .exit_latency = 59,
405 .target_residency = 600,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100406 .enter = &intel_idle,
407 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400408 {
409 .name = "C6-IVT-8S",
410 .desc = "MWAIT 0x20",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100411 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown0138d8f2014-04-04 01:21:07 -0400412 .exit_latency = 88,
413 .target_residency = 700,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100414 .enter = &intel_idle,
415 .enter_freeze = intel_idle_freeze, },
Len Brown0138d8f2014-04-04 01:21:07 -0400416 {
417 .enter = NULL }
418};
419
Jiang Liuba0dc812014-01-09 15:30:26 +0800420static struct cpuidle_state hsw_cstates[] = {
Len Browne022e7e2013-02-01 23:37:30 -0500421 {
Len Brown85a4d2d2013-01-31 14:40:49 -0500422 .name = "C1-HSW",
423 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100424 .flags = MWAIT2flg(0x00),
Len Brown85a4d2d2013-01-31 14:40:49 -0500425 .exit_latency = 2,
426 .target_residency = 2,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100427 .enter = &intel_idle,
428 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500429 {
Len Brown32e95182013-02-02 01:31:56 -0500430 .name = "C1E-HSW",
431 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100432 .flags = MWAIT2flg(0x01),
Len Brown32e95182013-02-02 01:31:56 -0500433 .exit_latency = 10,
434 .target_residency = 20,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100435 .enter = &intel_idle,
436 .enter_freeze = intel_idle_freeze, },
Len Brown32e95182013-02-02 01:31:56 -0500437 {
Len Brown85a4d2d2013-01-31 14:40:49 -0500438 .name = "C3-HSW",
439 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100440 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown85a4d2d2013-01-31 14:40:49 -0500441 .exit_latency = 33,
442 .target_residency = 100,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100443 .enter = &intel_idle,
444 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500445 {
Len Brown85a4d2d2013-01-31 14:40:49 -0500446 .name = "C6-HSW",
447 .desc = "MWAIT 0x20",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100448 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown85a4d2d2013-01-31 14:40:49 -0500449 .exit_latency = 133,
450 .target_residency = 400,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100451 .enter = &intel_idle,
452 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500453 {
Len Brown85a4d2d2013-01-31 14:40:49 -0500454 .name = "C7s-HSW",
455 .desc = "MWAIT 0x32",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100456 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown85a4d2d2013-01-31 14:40:49 -0500457 .exit_latency = 166,
458 .target_residency = 500,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100459 .enter = &intel_idle,
460 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500461 {
Len Brown86239ce2013-02-27 13:18:50 -0500462 .name = "C8-HSW",
463 .desc = "MWAIT 0x40",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100464 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown86239ce2013-02-27 13:18:50 -0500465 .exit_latency = 300,
466 .target_residency = 900,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100467 .enter = &intel_idle,
468 .enter_freeze = intel_idle_freeze, },
Len Brown86239ce2013-02-27 13:18:50 -0500469 {
470 .name = "C9-HSW",
471 .desc = "MWAIT 0x50",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100472 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown86239ce2013-02-27 13:18:50 -0500473 .exit_latency = 600,
474 .target_residency = 1800,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100475 .enter = &intel_idle,
476 .enter_freeze = intel_idle_freeze, },
Len Brown86239ce2013-02-27 13:18:50 -0500477 {
478 .name = "C10-HSW",
479 .desc = "MWAIT 0x60",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100480 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown86239ce2013-02-27 13:18:50 -0500481 .exit_latency = 2600,
482 .target_residency = 7700,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100483 .enter = &intel_idle,
484 .enter_freeze = intel_idle_freeze, },
Len Brown86239ce2013-02-27 13:18:50 -0500485 {
Len Browne022e7e2013-02-01 23:37:30 -0500486 .enter = NULL }
Len Brown85a4d2d2013-01-31 14:40:49 -0500487};
Len Browna138b562014-02-04 23:56:40 -0500488static struct cpuidle_state bdw_cstates[] = {
489 {
490 .name = "C1-BDW",
491 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100492 .flags = MWAIT2flg(0x00),
Len Browna138b562014-02-04 23:56:40 -0500493 .exit_latency = 2,
494 .target_residency = 2,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100495 .enter = &intel_idle,
496 .enter_freeze = intel_idle_freeze, },
Len Browna138b562014-02-04 23:56:40 -0500497 {
498 .name = "C1E-BDW",
499 .desc = "MWAIT 0x01",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100500 .flags = MWAIT2flg(0x01),
Len Browna138b562014-02-04 23:56:40 -0500501 .exit_latency = 10,
502 .target_residency = 20,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100503 .enter = &intel_idle,
504 .enter_freeze = intel_idle_freeze, },
Len Browna138b562014-02-04 23:56:40 -0500505 {
506 .name = "C3-BDW",
507 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100508 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Browna138b562014-02-04 23:56:40 -0500509 .exit_latency = 40,
510 .target_residency = 100,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100511 .enter = &intel_idle,
512 .enter_freeze = intel_idle_freeze, },
Len Browna138b562014-02-04 23:56:40 -0500513 {
514 .name = "C6-BDW",
515 .desc = "MWAIT 0x20",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100516 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Browna138b562014-02-04 23:56:40 -0500517 .exit_latency = 133,
518 .target_residency = 400,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100519 .enter = &intel_idle,
520 .enter_freeze = intel_idle_freeze, },
Len Browna138b562014-02-04 23:56:40 -0500521 {
522 .name = "C7s-BDW",
523 .desc = "MWAIT 0x32",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100524 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Browna138b562014-02-04 23:56:40 -0500525 .exit_latency = 166,
526 .target_residency = 500,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100527 .enter = &intel_idle,
528 .enter_freeze = intel_idle_freeze, },
Len Browna138b562014-02-04 23:56:40 -0500529 {
530 .name = "C8-BDW",
531 .desc = "MWAIT 0x40",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100532 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Browna138b562014-02-04 23:56:40 -0500533 .exit_latency = 300,
534 .target_residency = 900,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100535 .enter = &intel_idle,
536 .enter_freeze = intel_idle_freeze, },
Len Browna138b562014-02-04 23:56:40 -0500537 {
538 .name = "C9-BDW",
539 .desc = "MWAIT 0x50",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100540 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Browna138b562014-02-04 23:56:40 -0500541 .exit_latency = 600,
542 .target_residency = 1800,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100543 .enter = &intel_idle,
544 .enter_freeze = intel_idle_freeze, },
Len Browna138b562014-02-04 23:56:40 -0500545 {
546 .name = "C10-BDW",
547 .desc = "MWAIT 0x60",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100548 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Browna138b562014-02-04 23:56:40 -0500549 .exit_latency = 2600,
550 .target_residency = 7700,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100551 .enter = &intel_idle,
552 .enter_freeze = intel_idle_freeze, },
Len Browna138b562014-02-04 23:56:40 -0500553 {
554 .enter = NULL }
555};
Len Brown85a4d2d2013-01-31 14:40:49 -0500556
Jiang Liuba0dc812014-01-09 15:30:26 +0800557static struct cpuidle_state atom_cstates[] = {
Len Browne022e7e2013-02-01 23:37:30 -0500558 {
Len Brown32e95182013-02-02 01:31:56 -0500559 .name = "C1E-ATM",
Len Brown26717172010-03-08 14:07:30 -0500560 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100561 .flags = MWAIT2flg(0x00),
Len Brown32e95182013-02-02 01:31:56 -0500562 .exit_latency = 10,
563 .target_residency = 20,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100564 .enter = &intel_idle,
565 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500566 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100567 .name = "C2-ATM",
Len Brown26717172010-03-08 14:07:30 -0500568 .desc = "MWAIT 0x10",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100569 .flags = MWAIT2flg(0x10),
Len Brown26717172010-03-08 14:07:30 -0500570 .exit_latency = 20,
Len Brown26717172010-03-08 14:07:30 -0500571 .target_residency = 80,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100572 .enter = &intel_idle,
573 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500574 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100575 .name = "C4-ATM",
Len Brown26717172010-03-08 14:07:30 -0500576 .desc = "MWAIT 0x30",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100577 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown26717172010-03-08 14:07:30 -0500578 .exit_latency = 100,
Len Brown26717172010-03-08 14:07:30 -0500579 .target_residency = 400,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100580 .enter = &intel_idle,
581 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500582 {
Thomas Renninger15e123e2011-02-27 22:36:43 +0100583 .name = "C6-ATM",
Len Brown7fcca7d2010-10-05 13:43:14 -0400584 .desc = "MWAIT 0x52",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100585 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brown7fcca7d2010-10-05 13:43:14 -0400586 .exit_latency = 140,
Len Brown7fcca7d2010-10-05 13:43:14 -0400587 .target_residency = 560,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100588 .enter = &intel_idle,
589 .enter_freeze = intel_idle_freeze, },
Len Browne022e7e2013-02-01 23:37:30 -0500590 {
591 .enter = NULL }
Len Brown26717172010-03-08 14:07:30 -0500592};
Jiang Liu88390992014-01-09 15:30:27 +0800593static struct cpuidle_state avn_cstates[] = {
Len Brownfab04b22013-11-09 00:30:17 -0500594 {
595 .name = "C1-AVN",
596 .desc = "MWAIT 0x00",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100597 .flags = MWAIT2flg(0x00),
Len Brownfab04b22013-11-09 00:30:17 -0500598 .exit_latency = 2,
599 .target_residency = 2,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100600 .enter = &intel_idle,
601 .enter_freeze = intel_idle_freeze, },
Len Brownfab04b22013-11-09 00:30:17 -0500602 {
603 .name = "C6-AVN",
604 .desc = "MWAIT 0x51",
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +0100605 .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TLB_FLUSHED,
Len Brownfab04b22013-11-09 00:30:17 -0500606 .exit_latency = 15,
607 .target_residency = 45,
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100608 .enter = &intel_idle,
609 .enter_freeze = intel_idle_freeze, },
Jiang Liu88390992014-01-09 15:30:27 +0800610 {
611 .enter = NULL }
Len Brownfab04b22013-11-09 00:30:17 -0500612};
Len Brown26717172010-03-08 14:07:30 -0500613
Len Brown26717172010-03-08 14:07:30 -0500614/**
615 * intel_idle
616 * @dev: cpuidle_device
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530617 * @drv: cpuidle driver
Deepthi Dharware978aa72011-10-28 16:20:09 +0530618 * @index: index of cpuidle state
Len Brown26717172010-03-08 14:07:30 -0500619 *
Yanmin Zhang63ff07b2012-01-10 15:48:21 -0800620 * Must be called under local_irq_disable().
Len Brown26717172010-03-08 14:07:30 -0500621 */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530622static int intel_idle(struct cpuidle_device *dev,
623 struct cpuidle_driver *drv, int index)
Len Brown26717172010-03-08 14:07:30 -0500624{
625 unsigned long ecx = 1; /* break on interrupt flag */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530626 struct cpuidle_state *state = &drv->states[index];
Len Brownb1beab42013-01-31 19:55:37 -0500627 unsigned long eax = flg2MWAIT(state->flags);
Len Brown26717172010-03-08 14:07:30 -0500628 unsigned int cstate;
Len Brown26717172010-03-08 14:07:30 -0500629 int cpu = smp_processor_id();
630
631 cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
632
Suresh Siddha6110a1f2010-09-30 21:19:07 -0400633 /*
Len Brownc8381cc2010-10-15 20:43:06 -0400634 * leave_mm() to avoid costly and often unnecessary wakeups
635 * for flushing the user TLB's associated with the active mm.
Suresh Siddha6110a1f2010-09-30 21:19:07 -0400636 */
Len Brownc8381cc2010-10-15 20:43:06 -0400637 if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
Suresh Siddha6110a1f2010-09-30 21:19:07 -0400638 leave_mm(cpu);
639
Len Brown26717172010-03-08 14:07:30 -0500640 if (!(lapic_timer_reliable_states & (1 << (cstate))))
641 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
642
Peter Zijlstra16824252013-12-12 15:08:36 +0100643 mwait_idle_with_hints(eax, ecx);
Len Brown26717172010-03-08 14:07:30 -0500644
Len Brown26717172010-03-08 14:07:30 -0500645 if (!(lapic_timer_reliable_states & (1 << (cstate))))
646 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
647
Deepthi Dharware978aa72011-10-28 16:20:09 +0530648 return index;
Len Brown26717172010-03-08 14:07:30 -0500649}
650
Rafael J. Wysocki5fe2e522015-02-11 05:04:17 +0100651/**
652 * intel_idle_freeze - simplified "enter" callback routine for suspend-to-idle
653 * @dev: cpuidle_device
654 * @drv: cpuidle driver
655 * @index: state index
656 */
657static void intel_idle_freeze(struct cpuidle_device *dev,
658 struct cpuidle_driver *drv, int index)
659{
660 unsigned long ecx = 1; /* break on interrupt flag */
661 unsigned long eax = flg2MWAIT(drv->states[index].flags);
662
663 mwait_idle_with_hints(eax, ecx);
664}
665
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800666static void __setup_broadcast_timer(void *arg)
667{
Thomas Gleixner76962ca2015-04-03 02:02:34 +0200668 unsigned long on = (unsigned long)arg;
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800669
Thomas Gleixner76962ca2015-04-03 02:02:34 +0200670 if (on)
671 tick_broadcast_enable();
672 else
673 tick_broadcast_disable();
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800674}
675
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200676static int cpu_hotplug_notify(struct notifier_block *n,
677 unsigned long action, void *hcpu)
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800678{
679 int hotcpu = (unsigned long)hcpu;
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200680 struct cpuidle_device *dev;
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800681
Prarit Bhargavae2401452013-10-23 09:44:51 -0400682 switch (action & ~CPU_TASKS_FROZEN) {
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800683 case CPU_ONLINE:
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200684
685 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
686 smp_call_function_single(hotcpu, __setup_broadcast_timer,
687 (void *)true, 1);
688
689 /*
690 * Some systems can hotplug a cpu at runtime after
691 * the kernel has booted, we have to initialize the
692 * driver in this case
693 */
694 dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu);
695 if (!dev->registered)
696 intel_idle_cpu_init(hotcpu);
697
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800698 break;
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800699 }
700 return NOTIFY_OK;
701}
702
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200703static struct notifier_block cpu_hotplug_notifier = {
704 .notifier_call = cpu_hotplug_notify,
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800705};
706
Len Brown14796fc2011-01-18 20:48:27 -0500707static void auto_demotion_disable(void *dummy)
708{
709 unsigned long long msr_bits;
710
711 rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
Andi Kleenb66b8b92012-01-26 00:09:07 +0100712 msr_bits &= ~(icpu->auto_demotion_disable_flags);
Len Brown14796fc2011-01-18 20:48:27 -0500713 wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
714}
Len Brown32e95182013-02-02 01:31:56 -0500715static void c1e_promotion_disable(void *dummy)
716{
717 unsigned long long msr_bits;
718
719 rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
720 msr_bits &= ~0x2;
721 wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
722}
Len Brown14796fc2011-01-18 20:48:27 -0500723
Andi Kleenb66b8b92012-01-26 00:09:07 +0100724static const struct idle_cpu idle_cpu_nehalem = {
725 .state_table = nehalem_cstates,
Andi Kleenb66b8b92012-01-26 00:09:07 +0100726 .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
Len Brown32e95182013-02-02 01:31:56 -0500727 .disable_promotion_to_c1e = true,
Andi Kleenb66b8b92012-01-26 00:09:07 +0100728};
729
730static const struct idle_cpu idle_cpu_atom = {
731 .state_table = atom_cstates,
732};
733
734static const struct idle_cpu idle_cpu_lincroft = {
735 .state_table = atom_cstates,
736 .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
737};
738
739static const struct idle_cpu idle_cpu_snb = {
740 .state_table = snb_cstates,
Len Brown32e95182013-02-02 01:31:56 -0500741 .disable_promotion_to_c1e = true,
Andi Kleenb66b8b92012-01-26 00:09:07 +0100742};
743
Len Brown718987d2014-02-14 02:30:00 -0500744static const struct idle_cpu idle_cpu_byt = {
745 .state_table = byt_cstates,
746 .disable_promotion_to_c1e = true,
Len Brown8c058d532014-07-31 15:21:24 -0400747 .byt_auto_demotion_disable_flag = true,
Len Brown718987d2014-02-14 02:30:00 -0500748};
749
Len Brown6edab082012-06-01 19:45:32 -0400750static const struct idle_cpu idle_cpu_ivb = {
751 .state_table = ivb_cstates,
Len Brown32e95182013-02-02 01:31:56 -0500752 .disable_promotion_to_c1e = true,
Len Brown6edab082012-06-01 19:45:32 -0400753};
754
Len Brown0138d8f2014-04-04 01:21:07 -0400755static const struct idle_cpu idle_cpu_ivt = {
756 .state_table = ivt_cstates,
757 .disable_promotion_to_c1e = true,
758};
759
Len Brown85a4d2d2013-01-31 14:40:49 -0500760static const struct idle_cpu idle_cpu_hsw = {
761 .state_table = hsw_cstates,
Len Brown32e95182013-02-02 01:31:56 -0500762 .disable_promotion_to_c1e = true,
Len Brown85a4d2d2013-01-31 14:40:49 -0500763};
764
Len Browna138b562014-02-04 23:56:40 -0500765static const struct idle_cpu idle_cpu_bdw = {
766 .state_table = bdw_cstates,
767 .disable_promotion_to_c1e = true,
768};
769
Len Brownfab04b22013-11-09 00:30:17 -0500770static const struct idle_cpu idle_cpu_avn = {
771 .state_table = avn_cstates,
772 .disable_promotion_to_c1e = true,
773};
774
Andi Kleenb66b8b92012-01-26 00:09:07 +0100775#define ICPU(model, cpu) \
776 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu }
777
778static const struct x86_cpu_id intel_idle_ids[] = {
779 ICPU(0x1a, idle_cpu_nehalem),
780 ICPU(0x1e, idle_cpu_nehalem),
781 ICPU(0x1f, idle_cpu_nehalem),
Ben Hutchings8bf11932012-02-16 04:13:14 +0000782 ICPU(0x25, idle_cpu_nehalem),
783 ICPU(0x2c, idle_cpu_nehalem),
784 ICPU(0x2e, idle_cpu_nehalem),
Andi Kleenb66b8b92012-01-26 00:09:07 +0100785 ICPU(0x1c, idle_cpu_atom),
786 ICPU(0x26, idle_cpu_lincroft),
Ben Hutchings8bf11932012-02-16 04:13:14 +0000787 ICPU(0x2f, idle_cpu_nehalem),
Andi Kleenb66b8b92012-01-26 00:09:07 +0100788 ICPU(0x2a, idle_cpu_snb),
789 ICPU(0x2d, idle_cpu_snb),
Jan Kiszkaacead1b2014-01-25 22:24:22 +0100790 ICPU(0x36, idle_cpu_atom),
Len Brown718987d2014-02-14 02:30:00 -0500791 ICPU(0x37, idle_cpu_byt),
Len Brown6edab082012-06-01 19:45:32 -0400792 ICPU(0x3a, idle_cpu_ivb),
Len Brown0138d8f2014-04-04 01:21:07 -0400793 ICPU(0x3e, idle_cpu_ivt),
Len Brown85a4d2d2013-01-31 14:40:49 -0500794 ICPU(0x3c, idle_cpu_hsw),
795 ICPU(0x3f, idle_cpu_hsw),
796 ICPU(0x45, idle_cpu_hsw),
Len Brown0b158412013-03-15 10:55:31 -0400797 ICPU(0x46, idle_cpu_hsw),
Len Browna138b562014-02-04 23:56:40 -0500798 ICPU(0x4d, idle_cpu_avn),
799 ICPU(0x3d, idle_cpu_bdw),
Len Brownbea57072015-02-10 15:42:03 -0500800 ICPU(0x47, idle_cpu_bdw),
Len Browna138b562014-02-04 23:56:40 -0500801 ICPU(0x4f, idle_cpu_bdw),
802 ICPU(0x56, idle_cpu_bdw),
Andi Kleenb66b8b92012-01-26 00:09:07 +0100803 {}
804};
805MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
806
Len Brown26717172010-03-08 14:07:30 -0500807/*
808 * intel_idle_probe()
809 */
Bartlomiej Zolnierkiewicz00f3e752013-08-30 12:27:45 +0200810static int __init intel_idle_probe(void)
Len Brown26717172010-03-08 14:07:30 -0500811{
Len Brownc4236282010-05-28 02:22:03 -0400812 unsigned int eax, ebx, ecx;
Andi Kleenb66b8b92012-01-26 00:09:07 +0100813 const struct x86_cpu_id *id;
Len Brown26717172010-03-08 14:07:30 -0500814
815 if (max_cstate == 0) {
816 pr_debug(PREFIX "disabled\n");
817 return -EPERM;
818 }
819
Andi Kleenb66b8b92012-01-26 00:09:07 +0100820 id = x86_match_cpu(intel_idle_ids);
821 if (!id) {
822 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
823 boot_cpu_data.x86 == 6)
824 pr_debug(PREFIX "does not run on family %d model %d\n",
825 boot_cpu_data.x86, boot_cpu_data.x86_model);
Len Brown26717172010-03-08 14:07:30 -0500826 return -ENODEV;
Andi Kleenb66b8b92012-01-26 00:09:07 +0100827 }
Len Brown26717172010-03-08 14:07:30 -0500828
829 if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
830 return -ENODEV;
831
Len Brownc4236282010-05-28 02:22:03 -0400832 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
Len Brown26717172010-03-08 14:07:30 -0500833
834 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
Thomas Renninger5c2a9f02011-12-04 22:17:29 +0100835 !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
836 !mwait_substates)
Len Brown26717172010-03-08 14:07:30 -0500837 return -ENODEV;
Len Brown26717172010-03-08 14:07:30 -0500838
Len Brownc4236282010-05-28 02:22:03 -0400839 pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
Len Brown26717172010-03-08 14:07:30 -0500840
Andi Kleenb66b8b92012-01-26 00:09:07 +0100841 icpu = (const struct idle_cpu *)id->driver_data;
842 cpuidle_state_table = icpu->state_table;
Len Brown26717172010-03-08 14:07:30 -0500843
Len Brown56b9aea2010-12-02 01:19:32 -0500844 if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
Shaohua Li2a2d31c2011-01-10 09:38:12 +0800845 lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200846 else
Shaohua Li39a74fd2012-01-10 15:48:19 -0800847 on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200848
Len Brown26717172010-03-08 14:07:30 -0500849 pr_debug(PREFIX "v" INTEL_IDLE_VERSION
850 " model 0x%X\n", boot_cpu_data.x86_model);
851
852 pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
853 lapic_timer_reliable_states);
854 return 0;
855}
856
857/*
858 * intel_idle_cpuidle_devices_uninit()
859 * unregister, free cpuidle_devices
860 */
861static void intel_idle_cpuidle_devices_uninit(void)
862{
863 int i;
864 struct cpuidle_device *dev;
865
866 for_each_online_cpu(i) {
867 dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
868 cpuidle_unregister_device(dev);
869 }
870
871 free_percpu(intel_idle_cpuidle_devices);
872 return;
873}
Len Brown0138d8f2014-04-04 01:21:07 -0400874
875/*
876 * intel_idle_state_table_update()
877 *
878 * Update the default state_table for this CPU-id
879 *
880 * Currently used to access tuned IVT multi-socket targets
881 * Assumption: num_sockets == (max_package_num + 1)
882 */
883void intel_idle_state_table_update(void)
884{
885 /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
886 if (boot_cpu_data.x86_model == 0x3e) { /* IVT */
887 int cpu, package_num, num_sockets = 1;
888
889 for_each_online_cpu(cpu) {
890 package_num = topology_physical_package_id(cpu);
891 if (package_num + 1 > num_sockets) {
892 num_sockets = package_num + 1;
893
Christoph Jaegerd27dca42014-04-12 19:57:30 +0200894 if (num_sockets > 4) {
Len Brown0138d8f2014-04-04 01:21:07 -0400895 cpuidle_state_table = ivt_cstates_8s;
896 return;
Christoph Jaegerd27dca42014-04-12 19:57:30 +0200897 }
Len Brown0138d8f2014-04-04 01:21:07 -0400898 }
899 }
900
901 if (num_sockets > 2)
902 cpuidle_state_table = ivt_cstates_4s;
903 /* else, 1 and 2 socket systems use default ivt_cstates */
904 }
905 return;
906}
907
Len Brown26717172010-03-08 14:07:30 -0500908/*
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530909 * intel_idle_cpuidle_driver_init()
910 * allocate, initialize cpuidle_states
911 */
Bartlomiej Zolnierkiewicz00f3e752013-08-30 12:27:45 +0200912static int __init intel_idle_cpuidle_driver_init(void)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530913{
914 int cstate;
915 struct cpuidle_driver *drv = &intel_idle_driver;
916
Len Brown0138d8f2014-04-04 01:21:07 -0400917 intel_idle_state_table_update();
918
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530919 drv->state_count = 1;
920
Len Browne022e7e2013-02-01 23:37:30 -0500921 for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
Len Brown24bfa952014-02-14 00:50:34 -0500922 int num_substates, mwait_hint, mwait_cstate;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530923
Len Browne022e7e2013-02-01 23:37:30 -0500924 if (cpuidle_state_table[cstate].enter == NULL)
925 break;
926
927 if (cstate + 1 > max_cstate) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530928 printk(PREFIX "max_cstate %d reached\n",
929 max_cstate);
930 break;
931 }
932
Len Browne022e7e2013-02-01 23:37:30 -0500933 mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
934 mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530935
Len Brown24bfa952014-02-14 00:50:34 -0500936 /* number of sub-states for this state in CPUID.MWAIT */
Len Browne022e7e2013-02-01 23:37:30 -0500937 num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
938 & MWAIT_SUBSTATE_MASK;
939
Len Brown24bfa952014-02-14 00:50:34 -0500940 /* if NO sub-states for this state in CPUID, skip it */
941 if (num_substates == 0)
Len Browne022e7e2013-02-01 23:37:30 -0500942 continue;
943
944 if (((mwait_cstate + 1) > 2) &&
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530945 !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
946 mark_tsc_unstable("TSC halts in idle"
947 " states deeper than C2");
948
949 drv->states[drv->state_count] = /* structure copy */
950 cpuidle_state_table[cstate];
951
952 drv->state_count += 1;
953 }
954
Andi Kleenb66b8b92012-01-26 00:09:07 +0100955 if (icpu->auto_demotion_disable_flags)
Shaohua Li39a74fd2012-01-10 15:48:19 -0800956 on_each_cpu(auto_demotion_disable, NULL, 1);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530957
Len Brown8c058d532014-07-31 15:21:24 -0400958 if (icpu->byt_auto_demotion_disable_flag) {
959 wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
960 wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
961 }
962
Len Brown32e95182013-02-02 01:31:56 -0500963 if (icpu->disable_promotion_to_c1e) /* each-cpu is redundant */
964 on_each_cpu(c1e_promotion_disable, NULL, 1);
965
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530966 return 0;
967}
968
969
970/*
Thomas Renninger65b7f832012-01-17 22:40:08 +0100971 * intel_idle_cpu_init()
Len Brown26717172010-03-08 14:07:30 -0500972 * allocate, initialize, register cpuidle_devices
Thomas Renninger65b7f832012-01-17 22:40:08 +0100973 * @cpu: cpu/core to initialize
Len Brown26717172010-03-08 14:07:30 -0500974 */
Daniel Lezcano25ac7762012-07-05 15:23:25 +0200975static int intel_idle_cpu_init(int cpu)
Len Brown26717172010-03-08 14:07:30 -0500976{
Len Brown26717172010-03-08 14:07:30 -0500977 struct cpuidle_device *dev;
978
Thomas Renninger65b7f832012-01-17 22:40:08 +0100979 dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
Len Brown26717172010-03-08 14:07:30 -0500980
Thomas Renninger65b7f832012-01-17 22:40:08 +0100981 dev->cpu = cpu;
Len Brown26717172010-03-08 14:07:30 -0500982
Thomas Renninger65b7f832012-01-17 22:40:08 +0100983 if (cpuidle_register_device(dev)) {
984 pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
985 intel_idle_cpuidle_devices_uninit();
986 return -EIO;
Len Brown26717172010-03-08 14:07:30 -0500987 }
988
Andi Kleenb66b8b92012-01-26 00:09:07 +0100989 if (icpu->auto_demotion_disable_flags)
Thomas Renninger65b7f832012-01-17 22:40:08 +0100990 smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
991
Bartlomiej Zolnierkiewiczdbf87ab2013-12-20 19:47:28 +0100992 if (icpu->disable_promotion_to_c1e)
993 smp_call_function_single(cpu, c1e_promotion_disable, NULL, 1);
994
Len Brown26717172010-03-08 14:07:30 -0500995 return 0;
996}
Len Brown26717172010-03-08 14:07:30 -0500997
998static int __init intel_idle_init(void)
999{
Thomas Renninger65b7f832012-01-17 22:40:08 +01001000 int retval, i;
Len Brown26717172010-03-08 14:07:30 -05001001
Thomas Renningerd1896042010-11-03 17:06:14 +01001002 /* Do not load intel_idle at all for now if idle= is passed */
1003 if (boot_option_idle_override != IDLE_NO_OVERRIDE)
1004 return -ENODEV;
1005
Len Brown26717172010-03-08 14:07:30 -05001006 retval = intel_idle_probe();
1007 if (retval)
1008 return retval;
1009
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301010 intel_idle_cpuidle_driver_init();
Len Brown26717172010-03-08 14:07:30 -05001011 retval = cpuidle_register_driver(&intel_idle_driver);
1012 if (retval) {
Konrad Rzeszutek Wilk3735d522012-08-16 22:06:55 +02001013 struct cpuidle_driver *drv = cpuidle_get_driver();
Len Brown26717172010-03-08 14:07:30 -05001014 printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
Konrad Rzeszutek Wilk3735d522012-08-16 22:06:55 +02001015 drv ? drv->name : "none");
Len Brown26717172010-03-08 14:07:30 -05001016 return retval;
1017 }
1018
Thomas Renninger65b7f832012-01-17 22:40:08 +01001019 intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
1020 if (intel_idle_cpuidle_devices == NULL)
1021 return -ENOMEM;
1022
Srivatsa S. Bhat07494d52014-03-11 02:10:30 +05301023 cpu_notifier_register_begin();
1024
Thomas Renninger65b7f832012-01-17 22:40:08 +01001025 for_each_online_cpu(i) {
1026 retval = intel_idle_cpu_init(i);
1027 if (retval) {
Srivatsa S. Bhat07494d52014-03-11 02:10:30 +05301028 cpu_notifier_register_done();
Thomas Renninger65b7f832012-01-17 22:40:08 +01001029 cpuidle_unregister_driver(&intel_idle_driver);
1030 return retval;
1031 }
Len Brown26717172010-03-08 14:07:30 -05001032 }
Srivatsa S. Bhat07494d52014-03-11 02:10:30 +05301033 __register_cpu_notifier(&cpu_hotplug_notifier);
1034
1035 cpu_notifier_register_done();
Len Brown26717172010-03-08 14:07:30 -05001036
1037 return 0;
1038}
1039
1040static void __exit intel_idle_exit(void)
1041{
1042 intel_idle_cpuidle_devices_uninit();
1043 cpuidle_unregister_driver(&intel_idle_driver);
1044
Srivatsa S. Bhat07494d52014-03-11 02:10:30 +05301045 cpu_notifier_register_begin();
Daniel Lezcano25ac7762012-07-05 15:23:25 +02001046
1047 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
Shaohua Li39a74fd2012-01-10 15:48:19 -08001048 on_each_cpu(__setup_broadcast_timer, (void *)false, 1);
Srivatsa S. Bhat07494d52014-03-11 02:10:30 +05301049 __unregister_cpu_notifier(&cpu_hotplug_notifier);
1050
1051 cpu_notifier_register_done();
Shaohua Li2a2d31c2011-01-10 09:38:12 +08001052
Len Brown26717172010-03-08 14:07:30 -05001053 return;
1054}
1055
1056module_init(intel_idle_init);
1057module_exit(intel_idle_exit);
1058
Len Brown26717172010-03-08 14:07:30 -05001059module_param(max_cstate, int, 0444);
Len Brown26717172010-03-08 14:07:30 -05001060
1061MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
1062MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
1063MODULE_LICENSE("GPL");