intel_idle: Update support for Silvermont Core in Baytrail SOC
[deliverable/linux.git] / drivers / idle / intel_idle.c
CommitLineData
26717172
LB
1/*
2 * intel_idle.c - native hardware idle loop for modern Intel processors
3 *
fab04b22 4 * Copyright (c) 2013, Intel Corporation.
26717172
LB
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>
26717172
LB
59#include <trace/events/power.h>
60#include <linux/sched.h>
2a2d31c8
SL
61#include <linux/notifier.h>
62#include <linux/cpu.h>
7c52d551 63#include <linux/module.h>
b66b8b9a 64#include <asm/cpu_device_id.h>
bc83cccc 65#include <asm/mwait.h>
14796fca 66#include <asm/msr.h>
26717172
LB
67
68#define INTEL_IDLE_VERSION "0.4"
69#define PREFIX "intel_idle: "
70
26717172
LB
71static struct cpuidle_driver intel_idle_driver = {
72 .name = "intel_idle",
73 .owner = THIS_MODULE,
74};
75/* intel_idle.max_cstate=0 disables driver */
137ecc77 76static int max_cstate = CPUIDLE_STATE_MAX - 1;
26717172 77
c4236282 78static unsigned int mwait_substates;
26717172 79
2a2d31c8 80#define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
26717172 81/* Reliable LAPIC Timer States, bit 1 for C1 etc. */
d13780d4 82static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
26717172 83
b66b8b9a
AK
84struct 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;
8c058d53 92 bool byt_auto_demotion_disable_flag;
32e95180 93 bool disable_promotion_to_c1e;
b66b8b9a
AK
94};
95
96static const struct idle_cpu *icpu;
3265eba0 97static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
46bcfad7
DD
98static int intel_idle(struct cpuidle_device *dev,
99 struct cpuidle_driver *drv, int index);
5fe2e527
RW
100static void intel_idle_freeze(struct cpuidle_device *dev,
101 struct cpuidle_driver *drv, int index);
25ac7761 102static int intel_idle_cpu_init(int cpu);
26717172
LB
103
104static struct cpuidle_state *cpuidle_state_table;
105
956d033f
LB
106/*
107 * 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
b1beab48
LB
114/*
115 * 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
26717172
LB
124/*
125 * 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 */
ba0dc81e 129static struct cpuidle_state nehalem_cstates[] = {
e022e7eb 130 {
15e123e5 131 .name = "C1-NHM",
26717172 132 .desc = "MWAIT 0x00",
b82b6cca 133 .flags = MWAIT2flg(0x00),
26717172 134 .exit_latency = 3,
26717172 135 .target_residency = 6,
5fe2e527
RW
136 .enter = &intel_idle,
137 .enter_freeze = intel_idle_freeze, },
32e95180
LB
138 {
139 .name = "C1E-NHM",
140 .desc = "MWAIT 0x01",
b82b6cca 141 .flags = MWAIT2flg(0x01),
32e95180
LB
142 .exit_latency = 10,
143 .target_residency = 20,
5fe2e527
RW
144 .enter = &intel_idle,
145 .enter_freeze = intel_idle_freeze, },
e022e7eb 146 {
15e123e5 147 .name = "C3-NHM",
26717172 148 .desc = "MWAIT 0x10",
b82b6cca 149 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
26717172 150 .exit_latency = 20,
26717172 151 .target_residency = 80,
5fe2e527
RW
152 .enter = &intel_idle,
153 .enter_freeze = intel_idle_freeze, },
e022e7eb 154 {
15e123e5 155 .name = "C6-NHM",
26717172 156 .desc = "MWAIT 0x20",
b82b6cca 157 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
26717172 158 .exit_latency = 200,
26717172 159 .target_residency = 800,
5fe2e527
RW
160 .enter = &intel_idle,
161 .enter_freeze = intel_idle_freeze, },
e022e7eb
LB
162 {
163 .enter = NULL }
26717172
LB
164};
165
ba0dc81e 166static struct cpuidle_state snb_cstates[] = {
e022e7eb 167 {
15e123e5 168 .name = "C1-SNB",
d13780d4 169 .desc = "MWAIT 0x00",
b82b6cca 170 .flags = MWAIT2flg(0x00),
32e95180
LB
171 .exit_latency = 2,
172 .target_residency = 2,
5fe2e527
RW
173 .enter = &intel_idle,
174 .enter_freeze = intel_idle_freeze, },
32e95180
LB
175 {
176 .name = "C1E-SNB",
177 .desc = "MWAIT 0x01",
b82b6cca 178 .flags = MWAIT2flg(0x01),
32e95180
LB
179 .exit_latency = 10,
180 .target_residency = 20,
5fe2e527
RW
181 .enter = &intel_idle,
182 .enter_freeze = intel_idle_freeze, },
e022e7eb 183 {
15e123e5 184 .name = "C3-SNB",
d13780d4 185 .desc = "MWAIT 0x10",
b82b6cca 186 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
d13780d4 187 .exit_latency = 80,
ddbd550d 188 .target_residency = 211,
5fe2e527
RW
189 .enter = &intel_idle,
190 .enter_freeze = intel_idle_freeze, },
e022e7eb 191 {
15e123e5 192 .name = "C6-SNB",
d13780d4 193 .desc = "MWAIT 0x20",
b82b6cca 194 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
d13780d4 195 .exit_latency = 104,
ddbd550d 196 .target_residency = 345,
5fe2e527
RW
197 .enter = &intel_idle,
198 .enter_freeze = intel_idle_freeze, },
e022e7eb 199 {
15e123e5 200 .name = "C7-SNB",
d13780d4 201 .desc = "MWAIT 0x30",
b82b6cca 202 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
d13780d4 203 .exit_latency = 109,
ddbd550d 204 .target_residency = 345,
5fe2e527
RW
205 .enter = &intel_idle,
206 .enter_freeze = intel_idle_freeze, },
e022e7eb
LB
207 {
208 .enter = NULL }
d13780d4
LB
209};
210
718987d6
LB
211static struct cpuidle_state byt_cstates[] = {
212 {
213 .name = "C1-BYT",
214 .desc = "MWAIT 0x00",
b82b6cca 215 .flags = MWAIT2flg(0x00),
718987d6
LB
216 .exit_latency = 1,
217 .target_residency = 1,
5fe2e527
RW
218 .enter = &intel_idle,
219 .enter_freeze = intel_idle_freeze, },
718987d6
LB
220 {
221 .name = "C6N-BYT",
222 .desc = "MWAIT 0x58",
b82b6cca 223 .flags = MWAIT2flg(0x58) | CPUIDLE_FLAG_TLB_FLUSHED,
d7ef7671 224 .exit_latency = 300,
718987d6 225 .target_residency = 275,
5fe2e527
RW
226 .enter = &intel_idle,
227 .enter_freeze = intel_idle_freeze, },
718987d6
LB
228 {
229 .name = "C6S-BYT",
230 .desc = "MWAIT 0x52",
b82b6cca 231 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
d7ef7671 232 .exit_latency = 500,
718987d6 233 .target_residency = 560,
5fe2e527
RW
234 .enter = &intel_idle,
235 .enter_freeze = intel_idle_freeze, },
718987d6
LB
236 {
237 .name = "C7-BYT",
238 .desc = "MWAIT 0x60",
b82b6cca 239 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
718987d6 240 .exit_latency = 1200,
d7ef7671 241 .target_residency = 4000,
5fe2e527
RW
242 .enter = &intel_idle,
243 .enter_freeze = intel_idle_freeze, },
718987d6
LB
244 {
245 .name = "C7S-BYT",
246 .desc = "MWAIT 0x64",
b82b6cca 247 .flags = MWAIT2flg(0x64) | CPUIDLE_FLAG_TLB_FLUSHED,
718987d6
LB
248 .exit_latency = 10000,
249 .target_residency = 20000,
5fe2e527
RW
250 .enter = &intel_idle,
251 .enter_freeze = intel_idle_freeze, },
718987d6
LB
252 {
253 .enter = NULL }
254};
255
ba0dc81e 256static struct cpuidle_state ivb_cstates[] = {
e022e7eb 257 {
6edab08c
LB
258 .name = "C1-IVB",
259 .desc = "MWAIT 0x00",
b82b6cca 260 .flags = MWAIT2flg(0x00),
6edab08c
LB
261 .exit_latency = 1,
262 .target_residency = 1,
5fe2e527
RW
263 .enter = &intel_idle,
264 .enter_freeze = intel_idle_freeze, },
32e95180
LB
265 {
266 .name = "C1E-IVB",
267 .desc = "MWAIT 0x01",
b82b6cca 268 .flags = MWAIT2flg(0x01),
32e95180
LB
269 .exit_latency = 10,
270 .target_residency = 20,
5fe2e527
RW
271 .enter = &intel_idle,
272 .enter_freeze = intel_idle_freeze, },
e022e7eb 273 {
6edab08c
LB
274 .name = "C3-IVB",
275 .desc = "MWAIT 0x10",
b82b6cca 276 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
6edab08c
LB
277 .exit_latency = 59,
278 .target_residency = 156,
5fe2e527
RW
279 .enter = &intel_idle,
280 .enter_freeze = intel_idle_freeze, },
e022e7eb 281 {
6edab08c
LB
282 .name = "C6-IVB",
283 .desc = "MWAIT 0x20",
b82b6cca 284 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
6edab08c
LB
285 .exit_latency = 80,
286 .target_residency = 300,
5fe2e527
RW
287 .enter = &intel_idle,
288 .enter_freeze = intel_idle_freeze, },
e022e7eb 289 {
6edab08c
LB
290 .name = "C7-IVB",
291 .desc = "MWAIT 0x30",
b82b6cca 292 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
6edab08c
LB
293 .exit_latency = 87,
294 .target_residency = 300,
5fe2e527
RW
295 .enter = &intel_idle,
296 .enter_freeze = intel_idle_freeze, },
e022e7eb
LB
297 {
298 .enter = NULL }
6edab08c
LB
299};
300
0138d8f0
LB
301static struct cpuidle_state ivt_cstates[] = {
302 {
303 .name = "C1-IVT",
304 .desc = "MWAIT 0x00",
b82b6cca 305 .flags = MWAIT2flg(0x00),
0138d8f0
LB
306 .exit_latency = 1,
307 .target_residency = 1,
5fe2e527
RW
308 .enter = &intel_idle,
309 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
310 {
311 .name = "C1E-IVT",
312 .desc = "MWAIT 0x01",
b82b6cca 313 .flags = MWAIT2flg(0x01),
0138d8f0
LB
314 .exit_latency = 10,
315 .target_residency = 80,
5fe2e527
RW
316 .enter = &intel_idle,
317 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
318 {
319 .name = "C3-IVT",
320 .desc = "MWAIT 0x10",
b82b6cca 321 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
0138d8f0
LB
322 .exit_latency = 59,
323 .target_residency = 156,
5fe2e527
RW
324 .enter = &intel_idle,
325 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
326 {
327 .name = "C6-IVT",
328 .desc = "MWAIT 0x20",
b82b6cca 329 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
0138d8f0
LB
330 .exit_latency = 82,
331 .target_residency = 300,
5fe2e527
RW
332 .enter = &intel_idle,
333 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
334 {
335 .enter = NULL }
336};
337
338static struct cpuidle_state ivt_cstates_4s[] = {
339 {
340 .name = "C1-IVT-4S",
341 .desc = "MWAIT 0x00",
b82b6cca 342 .flags = MWAIT2flg(0x00),
0138d8f0
LB
343 .exit_latency = 1,
344 .target_residency = 1,
5fe2e527
RW
345 .enter = &intel_idle,
346 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
347 {
348 .name = "C1E-IVT-4S",
349 .desc = "MWAIT 0x01",
b82b6cca 350 .flags = MWAIT2flg(0x01),
0138d8f0
LB
351 .exit_latency = 10,
352 .target_residency = 250,
5fe2e527
RW
353 .enter = &intel_idle,
354 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
355 {
356 .name = "C3-IVT-4S",
357 .desc = "MWAIT 0x10",
b82b6cca 358 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
0138d8f0
LB
359 .exit_latency = 59,
360 .target_residency = 300,
5fe2e527
RW
361 .enter = &intel_idle,
362 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
363 {
364 .name = "C6-IVT-4S",
365 .desc = "MWAIT 0x20",
b82b6cca 366 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
0138d8f0
LB
367 .exit_latency = 84,
368 .target_residency = 400,
5fe2e527
RW
369 .enter = &intel_idle,
370 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
371 {
372 .enter = NULL }
373};
374
375static struct cpuidle_state ivt_cstates_8s[] = {
376 {
377 .name = "C1-IVT-8S",
378 .desc = "MWAIT 0x00",
b82b6cca 379 .flags = MWAIT2flg(0x00),
0138d8f0
LB
380 .exit_latency = 1,
381 .target_residency = 1,
5fe2e527
RW
382 .enter = &intel_idle,
383 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
384 {
385 .name = "C1E-IVT-8S",
386 .desc = "MWAIT 0x01",
b82b6cca 387 .flags = MWAIT2flg(0x01),
0138d8f0
LB
388 .exit_latency = 10,
389 .target_residency = 500,
5fe2e527
RW
390 .enter = &intel_idle,
391 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
392 {
393 .name = "C3-IVT-8S",
394 .desc = "MWAIT 0x10",
b82b6cca 395 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
0138d8f0
LB
396 .exit_latency = 59,
397 .target_residency = 600,
5fe2e527
RW
398 .enter = &intel_idle,
399 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
400 {
401 .name = "C6-IVT-8S",
402 .desc = "MWAIT 0x20",
b82b6cca 403 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
0138d8f0
LB
404 .exit_latency = 88,
405 .target_residency = 700,
5fe2e527
RW
406 .enter = &intel_idle,
407 .enter_freeze = intel_idle_freeze, },
0138d8f0
LB
408 {
409 .enter = NULL }
410};
411
ba0dc81e 412static struct cpuidle_state hsw_cstates[] = {
e022e7eb 413 {
85a4d2d4
LB
414 .name = "C1-HSW",
415 .desc = "MWAIT 0x00",
b82b6cca 416 .flags = MWAIT2flg(0x00),
85a4d2d4
LB
417 .exit_latency = 2,
418 .target_residency = 2,
5fe2e527
RW
419 .enter = &intel_idle,
420 .enter_freeze = intel_idle_freeze, },
32e95180
LB
421 {
422 .name = "C1E-HSW",
423 .desc = "MWAIT 0x01",
b82b6cca 424 .flags = MWAIT2flg(0x01),
32e95180
LB
425 .exit_latency = 10,
426 .target_residency = 20,
5fe2e527
RW
427 .enter = &intel_idle,
428 .enter_freeze = intel_idle_freeze, },
e022e7eb 429 {
85a4d2d4
LB
430 .name = "C3-HSW",
431 .desc = "MWAIT 0x10",
b82b6cca 432 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
85a4d2d4
LB
433 .exit_latency = 33,
434 .target_residency = 100,
5fe2e527
RW
435 .enter = &intel_idle,
436 .enter_freeze = intel_idle_freeze, },
e022e7eb 437 {
85a4d2d4
LB
438 .name = "C6-HSW",
439 .desc = "MWAIT 0x20",
b82b6cca 440 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
85a4d2d4
LB
441 .exit_latency = 133,
442 .target_residency = 400,
5fe2e527
RW
443 .enter = &intel_idle,
444 .enter_freeze = intel_idle_freeze, },
e022e7eb 445 {
85a4d2d4
LB
446 .name = "C7s-HSW",
447 .desc = "MWAIT 0x32",
b82b6cca 448 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
85a4d2d4
LB
449 .exit_latency = 166,
450 .target_residency = 500,
5fe2e527
RW
451 .enter = &intel_idle,
452 .enter_freeze = intel_idle_freeze, },
86239ceb
LB
453 {
454 .name = "C8-HSW",
455 .desc = "MWAIT 0x40",
b82b6cca 456 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
86239ceb
LB
457 .exit_latency = 300,
458 .target_residency = 900,
5fe2e527
RW
459 .enter = &intel_idle,
460 .enter_freeze = intel_idle_freeze, },
86239ceb
LB
461 {
462 .name = "C9-HSW",
463 .desc = "MWAIT 0x50",
b82b6cca 464 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
86239ceb
LB
465 .exit_latency = 600,
466 .target_residency = 1800,
5fe2e527
RW
467 .enter = &intel_idle,
468 .enter_freeze = intel_idle_freeze, },
86239ceb
LB
469 {
470 .name = "C10-HSW",
471 .desc = "MWAIT 0x60",
b82b6cca 472 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
86239ceb
LB
473 .exit_latency = 2600,
474 .target_residency = 7700,
5fe2e527
RW
475 .enter = &intel_idle,
476 .enter_freeze = intel_idle_freeze, },
e022e7eb
LB
477 {
478 .enter = NULL }
85a4d2d4 479};
a138b568
LB
480static struct cpuidle_state bdw_cstates[] = {
481 {
482 .name = "C1-BDW",
483 .desc = "MWAIT 0x00",
b82b6cca 484 .flags = MWAIT2flg(0x00),
a138b568
LB
485 .exit_latency = 2,
486 .target_residency = 2,
5fe2e527
RW
487 .enter = &intel_idle,
488 .enter_freeze = intel_idle_freeze, },
a138b568
LB
489 {
490 .name = "C1E-BDW",
491 .desc = "MWAIT 0x01",
b82b6cca 492 .flags = MWAIT2flg(0x01),
a138b568
LB
493 .exit_latency = 10,
494 .target_residency = 20,
5fe2e527
RW
495 .enter = &intel_idle,
496 .enter_freeze = intel_idle_freeze, },
a138b568
LB
497 {
498 .name = "C3-BDW",
499 .desc = "MWAIT 0x10",
b82b6cca 500 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TLB_FLUSHED,
a138b568
LB
501 .exit_latency = 40,
502 .target_residency = 100,
5fe2e527
RW
503 .enter = &intel_idle,
504 .enter_freeze = intel_idle_freeze, },
a138b568
LB
505 {
506 .name = "C6-BDW",
507 .desc = "MWAIT 0x20",
b82b6cca 508 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TLB_FLUSHED,
a138b568
LB
509 .exit_latency = 133,
510 .target_residency = 400,
5fe2e527
RW
511 .enter = &intel_idle,
512 .enter_freeze = intel_idle_freeze, },
a138b568
LB
513 {
514 .name = "C7s-BDW",
515 .desc = "MWAIT 0x32",
b82b6cca 516 .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TLB_FLUSHED,
a138b568
LB
517 .exit_latency = 166,
518 .target_residency = 500,
5fe2e527
RW
519 .enter = &intel_idle,
520 .enter_freeze = intel_idle_freeze, },
a138b568
LB
521 {
522 .name = "C8-BDW",
523 .desc = "MWAIT 0x40",
b82b6cca 524 .flags = MWAIT2flg(0x40) | CPUIDLE_FLAG_TLB_FLUSHED,
a138b568
LB
525 .exit_latency = 300,
526 .target_residency = 900,
5fe2e527
RW
527 .enter = &intel_idle,
528 .enter_freeze = intel_idle_freeze, },
a138b568
LB
529 {
530 .name = "C9-BDW",
531 .desc = "MWAIT 0x50",
b82b6cca 532 .flags = MWAIT2flg(0x50) | CPUIDLE_FLAG_TLB_FLUSHED,
a138b568
LB
533 .exit_latency = 600,
534 .target_residency = 1800,
5fe2e527
RW
535 .enter = &intel_idle,
536 .enter_freeze = intel_idle_freeze, },
a138b568
LB
537 {
538 .name = "C10-BDW",
539 .desc = "MWAIT 0x60",
b82b6cca 540 .flags = MWAIT2flg(0x60) | CPUIDLE_FLAG_TLB_FLUSHED,
a138b568
LB
541 .exit_latency = 2600,
542 .target_residency = 7700,
5fe2e527
RW
543 .enter = &intel_idle,
544 .enter_freeze = intel_idle_freeze, },
a138b568
LB
545 {
546 .enter = NULL }
547};
85a4d2d4 548
ba0dc81e 549static struct cpuidle_state atom_cstates[] = {
e022e7eb 550 {
32e95180 551 .name = "C1E-ATM",
26717172 552 .desc = "MWAIT 0x00",
b82b6cca 553 .flags = MWAIT2flg(0x00),
32e95180
LB
554 .exit_latency = 10,
555 .target_residency = 20,
5fe2e527
RW
556 .enter = &intel_idle,
557 .enter_freeze = intel_idle_freeze, },
e022e7eb 558 {
15e123e5 559 .name = "C2-ATM",
26717172 560 .desc = "MWAIT 0x10",
b82b6cca 561 .flags = MWAIT2flg(0x10),
26717172 562 .exit_latency = 20,
26717172 563 .target_residency = 80,
5fe2e527
RW
564 .enter = &intel_idle,
565 .enter_freeze = intel_idle_freeze, },
e022e7eb 566 {
15e123e5 567 .name = "C4-ATM",
26717172 568 .desc = "MWAIT 0x30",
b82b6cca 569 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TLB_FLUSHED,
26717172 570 .exit_latency = 100,
26717172 571 .target_residency = 400,
5fe2e527
RW
572 .enter = &intel_idle,
573 .enter_freeze = intel_idle_freeze, },
e022e7eb 574 {
15e123e5 575 .name = "C6-ATM",
7fcca7d9 576 .desc = "MWAIT 0x52",
b82b6cca 577 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TLB_FLUSHED,
7fcca7d9 578 .exit_latency = 140,
7fcca7d9 579 .target_residency = 560,
5fe2e527
RW
580 .enter = &intel_idle,
581 .enter_freeze = intel_idle_freeze, },
e022e7eb
LB
582 {
583 .enter = NULL }
26717172 584};
88390996 585static struct cpuidle_state avn_cstates[] = {
fab04b22
LB
586 {
587 .name = "C1-AVN",
588 .desc = "MWAIT 0x00",
b82b6cca 589 .flags = MWAIT2flg(0x00),
fab04b22
LB
590 .exit_latency = 2,
591 .target_residency = 2,
5fe2e527
RW
592 .enter = &intel_idle,
593 .enter_freeze = intel_idle_freeze, },
fab04b22
LB
594 {
595 .name = "C6-AVN",
596 .desc = "MWAIT 0x51",
b82b6cca 597 .flags = MWAIT2flg(0x51) | CPUIDLE_FLAG_TLB_FLUSHED,
fab04b22
LB
598 .exit_latency = 15,
599 .target_residency = 45,
5fe2e527
RW
600 .enter = &intel_idle,
601 .enter_freeze = intel_idle_freeze, },
88390996
JL
602 {
603 .enter = NULL }
fab04b22 604};
26717172 605
26717172
LB
606/**
607 * intel_idle
608 * @dev: cpuidle_device
46bcfad7 609 * @drv: cpuidle driver
e978aa7d 610 * @index: index of cpuidle state
26717172 611 *
63ff07be 612 * Must be called under local_irq_disable().
26717172 613 */
46bcfad7
DD
614static int intel_idle(struct cpuidle_device *dev,
615 struct cpuidle_driver *drv, int index)
26717172
LB
616{
617 unsigned long ecx = 1; /* break on interrupt flag */
46bcfad7 618 struct cpuidle_state *state = &drv->states[index];
b1beab48 619 unsigned long eax = flg2MWAIT(state->flags);
26717172 620 unsigned int cstate;
26717172
LB
621 int cpu = smp_processor_id();
622
623 cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
624
6110a1f4 625 /*
c8381cc3
LB
626 * leave_mm() to avoid costly and often unnecessary wakeups
627 * for flushing the user TLB's associated with the active mm.
6110a1f4 628 */
c8381cc3 629 if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
6110a1f4
SS
630 leave_mm(cpu);
631
26717172
LB
632 if (!(lapic_timer_reliable_states & (1 << (cstate))))
633 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
634
16824255 635 mwait_idle_with_hints(eax, ecx);
26717172 636
26717172
LB
637 if (!(lapic_timer_reliable_states & (1 << (cstate))))
638 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
639
e978aa7d 640 return index;
26717172
LB
641}
642
5fe2e527
RW
643/**
644 * intel_idle_freeze - simplified "enter" callback routine for suspend-to-idle
645 * @dev: cpuidle_device
646 * @drv: cpuidle driver
647 * @index: state index
648 */
649static void intel_idle_freeze(struct cpuidle_device *dev,
650 struct cpuidle_driver *drv, int index)
651{
652 unsigned long ecx = 1; /* break on interrupt flag */
653 unsigned long eax = flg2MWAIT(drv->states[index].flags);
654
655 mwait_idle_with_hints(eax, ecx);
656}
657
2a2d31c8
SL
658static void __setup_broadcast_timer(void *arg)
659{
660 unsigned long reason = (unsigned long)arg;
661 int cpu = smp_processor_id();
662
663 reason = reason ?
664 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
665
666 clockevents_notify(reason, &cpu);
667}
668
25ac7761
DL
669static int cpu_hotplug_notify(struct notifier_block *n,
670 unsigned long action, void *hcpu)
2a2d31c8
SL
671{
672 int hotcpu = (unsigned long)hcpu;
25ac7761 673 struct cpuidle_device *dev;
2a2d31c8 674
e2401453 675 switch (action & ~CPU_TASKS_FROZEN) {
2a2d31c8 676 case CPU_ONLINE:
25ac7761
DL
677
678 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
679 smp_call_function_single(hotcpu, __setup_broadcast_timer,
680 (void *)true, 1);
681
682 /*
683 * Some systems can hotplug a cpu at runtime after
684 * the kernel has booted, we have to initialize the
685 * driver in this case
686 */
687 dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu);
688 if (!dev->registered)
689 intel_idle_cpu_init(hotcpu);
690
2a2d31c8 691 break;
2a2d31c8
SL
692 }
693 return NOTIFY_OK;
694}
695
25ac7761
DL
696static struct notifier_block cpu_hotplug_notifier = {
697 .notifier_call = cpu_hotplug_notify,
2a2d31c8
SL
698};
699
14796fca
LB
700static void auto_demotion_disable(void *dummy)
701{
702 unsigned long long msr_bits;
703
704 rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
b66b8b9a 705 msr_bits &= ~(icpu->auto_demotion_disable_flags);
14796fca
LB
706 wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
707}
32e95180
LB
708static void c1e_promotion_disable(void *dummy)
709{
710 unsigned long long msr_bits;
711
712 rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
713 msr_bits &= ~0x2;
714 wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
715}
14796fca 716
b66b8b9a
AK
717static const struct idle_cpu idle_cpu_nehalem = {
718 .state_table = nehalem_cstates,
b66b8b9a 719 .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
32e95180 720 .disable_promotion_to_c1e = true,
b66b8b9a
AK
721};
722
723static const struct idle_cpu idle_cpu_atom = {
724 .state_table = atom_cstates,
725};
726
727static const struct idle_cpu idle_cpu_lincroft = {
728 .state_table = atom_cstates,
729 .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
730};
731
732static const struct idle_cpu idle_cpu_snb = {
733 .state_table = snb_cstates,
32e95180 734 .disable_promotion_to_c1e = true,
b66b8b9a
AK
735};
736
718987d6
LB
737static const struct idle_cpu idle_cpu_byt = {
738 .state_table = byt_cstates,
739 .disable_promotion_to_c1e = true,
8c058d53 740 .byt_auto_demotion_disable_flag = true,
718987d6
LB
741};
742
6edab08c
LB
743static const struct idle_cpu idle_cpu_ivb = {
744 .state_table = ivb_cstates,
32e95180 745 .disable_promotion_to_c1e = true,
6edab08c
LB
746};
747
0138d8f0
LB
748static const struct idle_cpu idle_cpu_ivt = {
749 .state_table = ivt_cstates,
750 .disable_promotion_to_c1e = true,
751};
752
85a4d2d4
LB
753static const struct idle_cpu idle_cpu_hsw = {
754 .state_table = hsw_cstates,
32e95180 755 .disable_promotion_to_c1e = true,
85a4d2d4
LB
756};
757
a138b568
LB
758static const struct idle_cpu idle_cpu_bdw = {
759 .state_table = bdw_cstates,
760 .disable_promotion_to_c1e = true,
761};
762
fab04b22
LB
763static const struct idle_cpu idle_cpu_avn = {
764 .state_table = avn_cstates,
765 .disable_promotion_to_c1e = true,
766};
767
b66b8b9a
AK
768#define ICPU(model, cpu) \
769 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu }
770
771static const struct x86_cpu_id intel_idle_ids[] = {
772 ICPU(0x1a, idle_cpu_nehalem),
773 ICPU(0x1e, idle_cpu_nehalem),
774 ICPU(0x1f, idle_cpu_nehalem),
8bf11938
BH
775 ICPU(0x25, idle_cpu_nehalem),
776 ICPU(0x2c, idle_cpu_nehalem),
777 ICPU(0x2e, idle_cpu_nehalem),
b66b8b9a
AK
778 ICPU(0x1c, idle_cpu_atom),
779 ICPU(0x26, idle_cpu_lincroft),
8bf11938 780 ICPU(0x2f, idle_cpu_nehalem),
b66b8b9a
AK
781 ICPU(0x2a, idle_cpu_snb),
782 ICPU(0x2d, idle_cpu_snb),
acead1b0 783 ICPU(0x36, idle_cpu_atom),
718987d6 784 ICPU(0x37, idle_cpu_byt),
6edab08c 785 ICPU(0x3a, idle_cpu_ivb),
0138d8f0 786 ICPU(0x3e, idle_cpu_ivt),
85a4d2d4
LB
787 ICPU(0x3c, idle_cpu_hsw),
788 ICPU(0x3f, idle_cpu_hsw),
789 ICPU(0x45, idle_cpu_hsw),
0b15841b 790 ICPU(0x46, idle_cpu_hsw),
a138b568
LB
791 ICPU(0x4d, idle_cpu_avn),
792 ICPU(0x3d, idle_cpu_bdw),
bea57077 793 ICPU(0x47, idle_cpu_bdw),
a138b568
LB
794 ICPU(0x4f, idle_cpu_bdw),
795 ICPU(0x56, idle_cpu_bdw),
b66b8b9a
AK
796 {}
797};
798MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
799
26717172
LB
800/*
801 * intel_idle_probe()
802 */
00f3e755 803static int __init intel_idle_probe(void)
26717172 804{
c4236282 805 unsigned int eax, ebx, ecx;
b66b8b9a 806 const struct x86_cpu_id *id;
26717172
LB
807
808 if (max_cstate == 0) {
809 pr_debug(PREFIX "disabled\n");
810 return -EPERM;
811 }
812
b66b8b9a
AK
813 id = x86_match_cpu(intel_idle_ids);
814 if (!id) {
815 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
816 boot_cpu_data.x86 == 6)
817 pr_debug(PREFIX "does not run on family %d model %d\n",
818 boot_cpu_data.x86, boot_cpu_data.x86_model);
26717172 819 return -ENODEV;
b66b8b9a 820 }
26717172
LB
821
822 if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
823 return -ENODEV;
824
c4236282 825 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
26717172
LB
826
827 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
5c2a9f06
TR
828 !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
829 !mwait_substates)
26717172 830 return -ENODEV;
26717172 831
c4236282 832 pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
26717172 833
b66b8b9a
AK
834 icpu = (const struct idle_cpu *)id->driver_data;
835 cpuidle_state_table = icpu->state_table;
26717172 836
56b9aea3 837 if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
2a2d31c8 838 lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
25ac7761 839 else
39a74fde 840 on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
25ac7761 841
26717172
LB
842 pr_debug(PREFIX "v" INTEL_IDLE_VERSION
843 " model 0x%X\n", boot_cpu_data.x86_model);
844
845 pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
846 lapic_timer_reliable_states);
847 return 0;
848}
849
850/*
851 * intel_idle_cpuidle_devices_uninit()
852 * unregister, free cpuidle_devices
853 */
854static void intel_idle_cpuidle_devices_uninit(void)
855{
856 int i;
857 struct cpuidle_device *dev;
858
859 for_each_online_cpu(i) {
860 dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
861 cpuidle_unregister_device(dev);
862 }
863
864 free_percpu(intel_idle_cpuidle_devices);
865 return;
866}
0138d8f0
LB
867
868/*
869 * intel_idle_state_table_update()
870 *
871 * Update the default state_table for this CPU-id
872 *
873 * Currently used to access tuned IVT multi-socket targets
874 * Assumption: num_sockets == (max_package_num + 1)
875 */
876void intel_idle_state_table_update(void)
877{
878 /* IVT uses a different table for 1-2, 3-4, and > 4 sockets */
879 if (boot_cpu_data.x86_model == 0x3e) { /* IVT */
880 int cpu, package_num, num_sockets = 1;
881
882 for_each_online_cpu(cpu) {
883 package_num = topology_physical_package_id(cpu);
884 if (package_num + 1 > num_sockets) {
885 num_sockets = package_num + 1;
886
d27dca42 887 if (num_sockets > 4) {
0138d8f0
LB
888 cpuidle_state_table = ivt_cstates_8s;
889 return;
d27dca42 890 }
0138d8f0
LB
891 }
892 }
893
894 if (num_sockets > 2)
895 cpuidle_state_table = ivt_cstates_4s;
896 /* else, 1 and 2 socket systems use default ivt_cstates */
897 }
898 return;
899}
900
46bcfad7
DD
901/*
902 * intel_idle_cpuidle_driver_init()
903 * allocate, initialize cpuidle_states
904 */
00f3e755 905static int __init intel_idle_cpuidle_driver_init(void)
46bcfad7
DD
906{
907 int cstate;
908 struct cpuidle_driver *drv = &intel_idle_driver;
909
0138d8f0
LB
910 intel_idle_state_table_update();
911
46bcfad7
DD
912 drv->state_count = 1;
913
e022e7eb 914 for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
24bfa950 915 int num_substates, mwait_hint, mwait_cstate;
46bcfad7 916
e022e7eb
LB
917 if (cpuidle_state_table[cstate].enter == NULL)
918 break;
919
920 if (cstate + 1 > max_cstate) {
46bcfad7
DD
921 printk(PREFIX "max_cstate %d reached\n",
922 max_cstate);
923 break;
924 }
925
e022e7eb
LB
926 mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
927 mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
e022e7eb 928
24bfa950 929 /* number of sub-states for this state in CPUID.MWAIT */
e022e7eb 930 num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
46bcfad7 931 & MWAIT_SUBSTATE_MASK;
e022e7eb 932
24bfa950
LB
933 /* if NO sub-states for this state in CPUID, skip it */
934 if (num_substates == 0)
46bcfad7 935 continue;
46bcfad7 936
e022e7eb 937 if (((mwait_cstate + 1) > 2) &&
46bcfad7
DD
938 !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
939 mark_tsc_unstable("TSC halts in idle"
940 " states deeper than C2");
941
942 drv->states[drv->state_count] = /* structure copy */
943 cpuidle_state_table[cstate];
944
945 drv->state_count += 1;
946 }
947
b66b8b9a 948 if (icpu->auto_demotion_disable_flags)
39a74fde 949 on_each_cpu(auto_demotion_disable, NULL, 1);
46bcfad7 950
8c058d53
LB
951 if (icpu->byt_auto_demotion_disable_flag) {
952 wrmsrl(MSR_CC6_DEMOTION_POLICY_CONFIG, 0);
953 wrmsrl(MSR_MC6_DEMOTION_POLICY_CONFIG, 0);
954 }
955
32e95180
LB
956 if (icpu->disable_promotion_to_c1e) /* each-cpu is redundant */
957 on_each_cpu(c1e_promotion_disable, NULL, 1);
958
46bcfad7
DD
959 return 0;
960}
961
962
26717172 963/*
65b7f839 964 * intel_idle_cpu_init()
26717172 965 * allocate, initialize, register cpuidle_devices
65b7f839 966 * @cpu: cpu/core to initialize
26717172 967 */
25ac7761 968static int intel_idle_cpu_init(int cpu)
26717172 969{
26717172
LB
970 struct cpuidle_device *dev;
971
65b7f839 972 dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
26717172 973
65b7f839 974 dev->cpu = cpu;
26717172 975
65b7f839
TR
976 if (cpuidle_register_device(dev)) {
977 pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
978 intel_idle_cpuidle_devices_uninit();
979 return -EIO;
26717172
LB
980 }
981
b66b8b9a 982 if (icpu->auto_demotion_disable_flags)
65b7f839
TR
983 smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
984
dbf87ab8
BZ
985 if (icpu->disable_promotion_to_c1e)
986 smp_call_function_single(cpu, c1e_promotion_disable, NULL, 1);
987
26717172
LB
988 return 0;
989}
26717172
LB
990
991static int __init intel_idle_init(void)
992{
65b7f839 993 int retval, i;
26717172 994
d1896049
TR
995 /* Do not load intel_idle at all for now if idle= is passed */
996 if (boot_option_idle_override != IDLE_NO_OVERRIDE)
997 return -ENODEV;
998
26717172
LB
999 retval = intel_idle_probe();
1000 if (retval)
1001 return retval;
1002
46bcfad7 1003 intel_idle_cpuidle_driver_init();
26717172
LB
1004 retval = cpuidle_register_driver(&intel_idle_driver);
1005 if (retval) {
3735d524 1006 struct cpuidle_driver *drv = cpuidle_get_driver();
26717172 1007 printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
3735d524 1008 drv ? drv->name : "none");
26717172
LB
1009 return retval;
1010 }
1011
65b7f839
TR
1012 intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
1013 if (intel_idle_cpuidle_devices == NULL)
1014 return -ENOMEM;
1015
07494d54
SB
1016 cpu_notifier_register_begin();
1017
65b7f839
TR
1018 for_each_online_cpu(i) {
1019 retval = intel_idle_cpu_init(i);
1020 if (retval) {
07494d54 1021 cpu_notifier_register_done();
65b7f839
TR
1022 cpuidle_unregister_driver(&intel_idle_driver);
1023 return retval;
1024 }
26717172 1025 }
07494d54
SB
1026 __register_cpu_notifier(&cpu_hotplug_notifier);
1027
1028 cpu_notifier_register_done();
26717172
LB
1029
1030 return 0;
1031}
1032
1033static void __exit intel_idle_exit(void)
1034{
1035 intel_idle_cpuidle_devices_uninit();
1036 cpuidle_unregister_driver(&intel_idle_driver);
1037
07494d54 1038 cpu_notifier_register_begin();
25ac7761
DL
1039
1040 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
39a74fde 1041 on_each_cpu(__setup_broadcast_timer, (void *)false, 1);
07494d54
SB
1042 __unregister_cpu_notifier(&cpu_hotplug_notifier);
1043
1044 cpu_notifier_register_done();
2a2d31c8 1045
26717172
LB
1046 return;
1047}
1048
1049module_init(intel_idle_init);
1050module_exit(intel_idle_exit);
1051
26717172 1052module_param(max_cstate, int, 0444);
26717172
LB
1053
1054MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
1055MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
1056MODULE_LICENSE("GPL");
This page took 0.304324 seconds and 5 git commands to generate.