]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/idle/intel_idle.c
intel_idle: remove assumption of one C-state per MWAIT flag
[mirror_ubuntu-bionic-kernel.git] / drivers / idle / intel_idle.c
CommitLineData
26717172
LB
1/*
2 * intel_idle.c - native hardware idle loop for modern Intel processors
3 *
4 * Copyright (c) 2010, Intel Corporation.
5 * Len Brown <len.brown@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21/*
22 * intel_idle is a cpuidle driver that loads on specific Intel processors
23 * in lieu of the legacy ACPI processor_idle driver. The intent is to
24 * make Linux more efficient on these processors, as intel_idle knows
25 * more than ACPI, as well as make Linux more immune to ACPI BIOS bugs.
26 */
27
28/*
29 * Design Assumptions
30 *
31 * All CPUs have same idle states as boot CPU
32 *
33 * Chipset BM_STS (bus master status) bit is a NOP
34 * for preventing entry into deep C-stats
35 */
36
37/*
38 * Known limitations
39 *
40 * The driver currently initializes for_each_online_cpu() upon modprobe.
41 * It it unaware of subsequent processors hot-added to the system.
42 * This means that if you boot with maxcpus=n and later online
43 * processors above n, those processors will use C1 only.
44 *
45 * ACPI has a .suspend hack to turn off deep c-statees during suspend
46 * to avoid complications with the lapic timer workaround.
47 * Have not seen issues with suspend, but may need same workaround here.
48 *
49 * There is currently no kernel-based automatic probing/loading mechanism
50 * if the driver is built as a module.
51 */
52
53/* un-comment DEBUG to enable pr_debug() statements */
54#define DEBUG
55
56#include <linux/kernel.h>
57#include <linux/cpuidle.h>
58#include <linux/clockchips.h>
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,
a474a515 74 .en_core_tk_irqen = 1,
26717172
LB
75};
76/* intel_idle.max_cstate=0 disables driver */
137ecc77 77static int max_cstate = CPUIDLE_STATE_MAX - 1;
26717172 78
c4236282 79static unsigned int mwait_substates;
26717172 80
2a2d31c8 81#define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
26717172 82/* Reliable LAPIC Timer States, bit 1 for C1 etc. */
d13780d4 83static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
26717172 84
b66b8b9a
AK
85struct 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;
3265eba0 96static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
46bcfad7
DD
97static int intel_idle(struct cpuidle_device *dev,
98 struct cpuidle_driver *drv, int index);
25ac7761 99static int intel_idle_cpu_init(int cpu);
26717172
LB
100
101static struct cpuidle_state *cpuidle_state_table;
102
956d033f
LB
103/*
104 * 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
b1beab48
LB
111/*
112 * 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
26717172
LB
121/*
122 * 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 */
137ecc77 126static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
e022e7eb 127 {
15e123e5 128 .name = "C1-NHM",
26717172 129 .desc = "MWAIT 0x00",
b1beab48 130 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
26717172 131 .exit_latency = 3,
26717172
LB
132 .target_residency = 6,
133 .enter = &intel_idle },
e022e7eb 134 {
15e123e5 135 .name = "C3-NHM",
26717172 136 .desc = "MWAIT 0x10",
b1beab48 137 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
26717172 138 .exit_latency = 20,
26717172
LB
139 .target_residency = 80,
140 .enter = &intel_idle },
e022e7eb 141 {
15e123e5 142 .name = "C6-NHM",
26717172 143 .desc = "MWAIT 0x20",
b1beab48 144 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
26717172 145 .exit_latency = 200,
26717172
LB
146 .target_residency = 800,
147 .enter = &intel_idle },
e022e7eb
LB
148 {
149 .enter = NULL }
26717172
LB
150};
151
137ecc77 152static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
e022e7eb 153 {
15e123e5 154 .name = "C1-SNB",
d13780d4 155 .desc = "MWAIT 0x00",
b1beab48 156 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
d13780d4 157 .exit_latency = 1,
ddbd550d 158 .target_residency = 1,
d13780d4 159 .enter = &intel_idle },
e022e7eb 160 {
15e123e5 161 .name = "C3-SNB",
d13780d4 162 .desc = "MWAIT 0x10",
b1beab48 163 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
d13780d4 164 .exit_latency = 80,
ddbd550d 165 .target_residency = 211,
d13780d4 166 .enter = &intel_idle },
e022e7eb 167 {
15e123e5 168 .name = "C6-SNB",
d13780d4 169 .desc = "MWAIT 0x20",
b1beab48 170 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
d13780d4 171 .exit_latency = 104,
ddbd550d 172 .target_residency = 345,
d13780d4 173 .enter = &intel_idle },
e022e7eb 174 {
15e123e5 175 .name = "C7-SNB",
d13780d4 176 .desc = "MWAIT 0x30",
b1beab48 177 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
d13780d4 178 .exit_latency = 109,
ddbd550d 179 .target_residency = 345,
d13780d4 180 .enter = &intel_idle },
e022e7eb
LB
181 {
182 .enter = NULL }
d13780d4
LB
183};
184
137ecc77 185static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
e022e7eb 186 {
6edab08c
LB
187 .name = "C1-IVB",
188 .desc = "MWAIT 0x00",
b1beab48 189 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
6edab08c
LB
190 .exit_latency = 1,
191 .target_residency = 1,
192 .enter = &intel_idle },
e022e7eb 193 {
6edab08c
LB
194 .name = "C3-IVB",
195 .desc = "MWAIT 0x10",
b1beab48 196 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
6edab08c
LB
197 .exit_latency = 59,
198 .target_residency = 156,
199 .enter = &intel_idle },
e022e7eb 200 {
6edab08c
LB
201 .name = "C6-IVB",
202 .desc = "MWAIT 0x20",
b1beab48 203 .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
6edab08c
LB
204 .exit_latency = 80,
205 .target_residency = 300,
206 .enter = &intel_idle },
e022e7eb 207 {
6edab08c
LB
208 .name = "C7-IVB",
209 .desc = "MWAIT 0x30",
b1beab48 210 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
6edab08c
LB
211 .exit_latency = 87,
212 .target_residency = 300,
213 .enter = &intel_idle },
e022e7eb
LB
214 {
215 .enter = NULL }
6edab08c
LB
216};
217
137ecc77 218static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
e022e7eb 219 {
85a4d2d4
LB
220 .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 },
e022e7eb 226 {
85a4d2d4
LB
227 .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 },
e022e7eb 233 {
85a4d2d4
LB
234 .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 },
e022e7eb 240 {
85a4d2d4
LB
241 .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 },
e022e7eb
LB
247 {
248 .enter = NULL }
85a4d2d4
LB
249};
250
137ecc77 251static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] = {
e022e7eb 252 {
15e123e5 253 .name = "C1-ATM",
26717172 254 .desc = "MWAIT 0x00",
b1beab48 255 .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
26717172 256 .exit_latency = 1,
26717172
LB
257 .target_residency = 4,
258 .enter = &intel_idle },
e022e7eb 259 {
15e123e5 260 .name = "C2-ATM",
26717172 261 .desc = "MWAIT 0x10",
b1beab48 262 .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID,
26717172 263 .exit_latency = 20,
26717172
LB
264 .target_residency = 80,
265 .enter = &intel_idle },
e022e7eb 266 {
15e123e5 267 .name = "C4-ATM",
26717172 268 .desc = "MWAIT 0x30",
b1beab48 269 .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
26717172 270 .exit_latency = 100,
26717172
LB
271 .target_residency = 400,
272 .enter = &intel_idle },
e022e7eb 273 {
15e123e5 274 .name = "C6-ATM",
7fcca7d9 275 .desc = "MWAIT 0x52",
b1beab48 276 .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
7fcca7d9 277 .exit_latency = 140,
7fcca7d9
LB
278 .target_residency = 560,
279 .enter = &intel_idle },
e022e7eb
LB
280 {
281 .enter = NULL }
26717172
LB
282};
283
26717172
LB
284/**
285 * intel_idle
286 * @dev: cpuidle_device
46bcfad7 287 * @drv: cpuidle driver
e978aa7d 288 * @index: index of cpuidle state
26717172 289 *
63ff07be 290 * Must be called under local_irq_disable().
26717172 291 */
46bcfad7
DD
292static int intel_idle(struct cpuidle_device *dev,
293 struct cpuidle_driver *drv, int index)
26717172
LB
294{
295 unsigned long ecx = 1; /* break on interrupt flag */
46bcfad7 296 struct cpuidle_state *state = &drv->states[index];
b1beab48 297 unsigned long eax = flg2MWAIT(state->flags);
26717172 298 unsigned int cstate;
26717172
LB
299 int cpu = smp_processor_id();
300
301 cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
302
6110a1f4 303 /*
c8381cc3
LB
304 * leave_mm() to avoid costly and often unnecessary wakeups
305 * for flushing the user TLB's associated with the active mm.
6110a1f4 306 */
c8381cc3 307 if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
6110a1f4
SS
308 leave_mm(cpu);
309
26717172
LB
310 if (!(lapic_timer_reliable_states & (1 << (cstate))))
311 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
312
26717172 313 stop_critical_timings();
26717172
LB
314 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
26717172
LB
324 if (!(lapic_timer_reliable_states & (1 << (cstate))))
325 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
326
e978aa7d 327 return index;
26717172
LB
328}
329
2a2d31c8
SL
330static 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
25ac7761
DL
341static int cpu_hotplug_notify(struct notifier_block *n,
342 unsigned long action, void *hcpu)
2a2d31c8
SL
343{
344 int hotcpu = (unsigned long)hcpu;
25ac7761 345 struct cpuidle_device *dev;
2a2d31c8
SL
346
347 switch (action & 0xf) {
348 case CPU_ONLINE:
25ac7761
DL
349
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
2a2d31c8 363 break;
2a2d31c8
SL
364 }
365 return NOTIFY_OK;
366}
367
25ac7761
DL
368static struct notifier_block cpu_hotplug_notifier = {
369 .notifier_call = cpu_hotplug_notify,
2a2d31c8
SL
370};
371
14796fca
LB
372static 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);
b66b8b9a 377 msr_bits &= ~(icpu->auto_demotion_disable_flags);
14796fca
LB
378 wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
379}
380
b66b8b9a
AK
381static const struct idle_cpu idle_cpu_nehalem = {
382 .state_table = nehalem_cstates,
b66b8b9a
AK
383 .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
6edab08c
LB
399static const struct idle_cpu idle_cpu_ivb = {
400 .state_table = ivb_cstates,
401};
402
85a4d2d4
LB
403static const struct idle_cpu idle_cpu_hsw = {
404 .state_table = hsw_cstates,
405};
406
b66b8b9a
AK
407#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),
8bf11938
BH
414 ICPU(0x25, idle_cpu_nehalem),
415 ICPU(0x2c, idle_cpu_nehalem),
416 ICPU(0x2e, idle_cpu_nehalem),
b66b8b9a
AK
417 ICPU(0x1c, idle_cpu_atom),
418 ICPU(0x26, idle_cpu_lincroft),
8bf11938 419 ICPU(0x2f, idle_cpu_nehalem),
b66b8b9a
AK
420 ICPU(0x2a, idle_cpu_snb),
421 ICPU(0x2d, idle_cpu_snb),
6edab08c 422 ICPU(0x3a, idle_cpu_ivb),
23795e58 423 ICPU(0x3e, idle_cpu_ivb),
85a4d2d4
LB
424 ICPU(0x3c, idle_cpu_hsw),
425 ICPU(0x3f, idle_cpu_hsw),
426 ICPU(0x45, idle_cpu_hsw),
b66b8b9a
AK
427 {}
428};
429MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
430
26717172
LB
431/*
432 * intel_idle_probe()
433 */
434static int intel_idle_probe(void)
435{
c4236282 436 unsigned int eax, ebx, ecx;
b66b8b9a 437 const struct x86_cpu_id *id;
26717172
LB
438
439 if (max_cstate == 0) {
440 pr_debug(PREFIX "disabled\n");
441 return -EPERM;
442 }
443
b66b8b9a
AK
444 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);
26717172 450 return -ENODEV;
b66b8b9a 451 }
26717172
LB
452
453 if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
454 return -ENODEV;
455
c4236282 456 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
26717172
LB
457
458 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
5c2a9f06
TR
459 !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
460 !mwait_substates)
26717172 461 return -ENODEV;
26717172 462
c4236282 463 pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
26717172 464
b66b8b9a
AK
465 icpu = (const struct idle_cpu *)id->driver_data;
466 cpuidle_state_table = icpu->state_table;
26717172 467
56b9aea3 468 if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
2a2d31c8 469 lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
25ac7761 470 else
39a74fde 471 on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
25ac7761 472
26717172
LB
473 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}
46bcfad7
DD
498/*
499 * 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
e022e7eb
LB
509 for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
510 int num_substates, mwait_hint, mwait_cstate, mwait_substate;
46bcfad7 511
e022e7eb
LB
512 if (cpuidle_state_table[cstate].enter == NULL)
513 break;
514
515 if (cstate + 1 > max_cstate) {
46bcfad7
DD
516 printk(PREFIX "max_cstate %d reached\n",
517 max_cstate);
518 break;
519 }
520
e022e7eb
LB
521 mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
522 mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
523 mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
524
46bcfad7 525 /* does the state exist in CPUID.MWAIT? */
e022e7eb 526 num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
46bcfad7 527 & MWAIT_SUBSTATE_MASK;
e022e7eb
LB
528
529 /* if sub-state in table is not enumerated by CPUID */
530 if ((mwait_substate + 1) > num_substates)
46bcfad7 531 continue;
46bcfad7 532
e022e7eb 533 if (((mwait_cstate + 1) > 2) &&
46bcfad7
DD
534 !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
b66b8b9a 544 if (icpu->auto_demotion_disable_flags)
39a74fde 545 on_each_cpu(auto_demotion_disable, NULL, 1);
46bcfad7
DD
546
547 return 0;
548}
549
550
26717172 551/*
65b7f839 552 * intel_idle_cpu_init()
26717172 553 * allocate, initialize, register cpuidle_devices
65b7f839 554 * @cpu: cpu/core to initialize
26717172 555 */
25ac7761 556static int intel_idle_cpu_init(int cpu)
26717172 557{
65b7f839 558 int cstate;
26717172
LB
559 struct cpuidle_device *dev;
560
65b7f839 561 dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
26717172 562
65b7f839 563 dev->state_count = 1;
26717172 564
e022e7eb
LB
565 for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
566 int num_substates, mwait_hint, mwait_cstate, mwait_substate;
26717172 567
e022e7eb
LB
568 if (cpuidle_state_table[cstate].enter == NULL)
569 continue;
570
571 if (cstate + 1 > max_cstate) {
dc716e96 572 printk(PREFIX "max_cstate %d reached\n", max_cstate);
65b7f839
TR
573 break;
574 }
26717172 575
e022e7eb
LB
576 mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
577 mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
578 mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
579
65b7f839 580 /* does the state exist in CPUID.MWAIT? */
e022e7eb
LB
581 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)
65b7f839 586 continue;
26717172 587
dc716e96
MPS
588 dev->state_count += 1;
589 }
590
65b7f839 591 dev->cpu = cpu;
26717172 592
65b7f839
TR
593 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;
26717172
LB
597 }
598
b66b8b9a 599 if (icpu->auto_demotion_disable_flags)
65b7f839
TR
600 smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
601
26717172
LB
602 return 0;
603}
26717172
LB
604
605static int __init intel_idle_init(void)
606{
65b7f839 607 int retval, i;
26717172 608
d1896049
TR
609 /* 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
26717172
LB
613 retval = intel_idle_probe();
614 if (retval)
615 return retval;
616
46bcfad7 617 intel_idle_cpuidle_driver_init();
26717172
LB
618 retval = cpuidle_register_driver(&intel_idle_driver);
619 if (retval) {
3735d524 620 struct cpuidle_driver *drv = cpuidle_get_driver();
26717172 621 printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
3735d524 622 drv ? drv->name : "none");
26717172
LB
623 return retval;
624 }
625
65b7f839
TR
626 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 }
26717172 636 }
6f8c2e79 637 register_cpu_notifier(&cpu_hotplug_notifier);
26717172
LB
638
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
25ac7761
DL
647
648 if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
39a74fde 649 on_each_cpu(__setup_broadcast_timer, (void *)false, 1);
25ac7761 650 unregister_cpu_notifier(&cpu_hotplug_notifier);
2a2d31c8 651
26717172
LB
652 return;
653}
654
655module_init(intel_idle_init);
656module_exit(intel_idle_exit);
657
26717172 658module_param(max_cstate, int, 0444);
26717172
LB
659
660MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
661MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
662MODULE_LICENSE("GPL");