xref: /openbmc/linux/drivers/idle/intel_idle.c (revision 7c52d551)
126717172SLen Brown /*
226717172SLen Brown  * intel_idle.c - native hardware idle loop for modern Intel processors
326717172SLen Brown  *
426717172SLen Brown  * Copyright (c) 2010, Intel Corporation.
526717172SLen Brown  * Len Brown <len.brown@intel.com>
626717172SLen Brown  *
726717172SLen Brown  * This program is free software; you can redistribute it and/or modify it
826717172SLen Brown  * under the terms and conditions of the GNU General Public License,
926717172SLen Brown  * version 2, as published by the Free Software Foundation.
1026717172SLen Brown  *
1126717172SLen Brown  * This program is distributed in the hope it will be useful, but WITHOUT
1226717172SLen Brown  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1326717172SLen Brown  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1426717172SLen Brown  * more details.
1526717172SLen Brown  *
1626717172SLen Brown  * You should have received a copy of the GNU General Public License along with
1726717172SLen Brown  * this program; if not, write to the Free Software Foundation, Inc.,
1826717172SLen Brown  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
1926717172SLen Brown  */
2026717172SLen Brown 
2126717172SLen Brown /*
2226717172SLen Brown  * intel_idle is a cpuidle driver that loads on specific Intel processors
2326717172SLen Brown  * in lieu of the legacy ACPI processor_idle driver.  The intent is to
2426717172SLen Brown  * make Linux more efficient on these processors, as intel_idle knows
2526717172SLen Brown  * more than ACPI, as well as make Linux more immune to ACPI BIOS bugs.
2626717172SLen Brown  */
2726717172SLen Brown 
2826717172SLen Brown /*
2926717172SLen Brown  * Design Assumptions
3026717172SLen Brown  *
3126717172SLen Brown  * All CPUs have same idle states as boot CPU
3226717172SLen Brown  *
3326717172SLen Brown  * Chipset BM_STS (bus master status) bit is a NOP
3426717172SLen Brown  *	for preventing entry into deep C-stats
3526717172SLen Brown  */
3626717172SLen Brown 
3726717172SLen Brown /*
3826717172SLen Brown  * Known limitations
3926717172SLen Brown  *
4026717172SLen Brown  * The driver currently initializes for_each_online_cpu() upon modprobe.
4126717172SLen Brown  * It it unaware of subsequent processors hot-added to the system.
4226717172SLen Brown  * This means that if you boot with maxcpus=n and later online
4326717172SLen Brown  * processors above n, those processors will use C1 only.
4426717172SLen Brown  *
4526717172SLen Brown  * ACPI has a .suspend hack to turn off deep c-statees during suspend
4626717172SLen Brown  * to avoid complications with the lapic timer workaround.
4726717172SLen Brown  * Have not seen issues with suspend, but may need same workaround here.
4826717172SLen Brown  *
4926717172SLen Brown  * There is currently no kernel-based automatic probing/loading mechanism
5026717172SLen Brown  * if the driver is built as a module.
5126717172SLen Brown  */
5226717172SLen Brown 
5326717172SLen Brown /* un-comment DEBUG to enable pr_debug() statements */
5426717172SLen Brown #define DEBUG
5526717172SLen Brown 
5626717172SLen Brown #include <linux/kernel.h>
5726717172SLen Brown #include <linux/cpuidle.h>
5826717172SLen Brown #include <linux/clockchips.h>
5926717172SLen Brown #include <linux/hrtimer.h>	/* ktime_get_real() */
6026717172SLen Brown #include <trace/events/power.h>
6126717172SLen Brown #include <linux/sched.h>
622a2d31c8SShaohua Li #include <linux/notifier.h>
632a2d31c8SShaohua Li #include <linux/cpu.h>
647c52d551SPaul Gortmaker #include <linux/module.h>
65bc83ccccSH. Peter Anvin #include <asm/mwait.h>
6614796fcaSLen Brown #include <asm/msr.h>
6726717172SLen Brown 
6826717172SLen Brown #define INTEL_IDLE_VERSION "0.4"
6926717172SLen Brown #define PREFIX "intel_idle: "
7026717172SLen Brown 
7126717172SLen Brown static struct cpuidle_driver intel_idle_driver = {
7226717172SLen Brown 	.name = "intel_idle",
7326717172SLen Brown 	.owner = THIS_MODULE,
7426717172SLen Brown };
7526717172SLen Brown /* intel_idle.max_cstate=0 disables driver */
7626717172SLen Brown static int max_cstate = MWAIT_MAX_NUM_CSTATES - 1;
7726717172SLen Brown 
78c4236282SLen Brown static unsigned int mwait_substates;
7926717172SLen Brown 
802a2d31c8SShaohua Li #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
8126717172SLen Brown /* Reliable LAPIC Timer States, bit 1 for C1 etc.  */
82d13780d4SLen Brown static unsigned int lapic_timer_reliable_states = (1 << 1);	 /* Default to only C1 */
8326717172SLen Brown 
843265eba0SNamhyung Kim static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
8526717172SLen Brown static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state);
8626717172SLen Brown 
8726717172SLen Brown static struct cpuidle_state *cpuidle_state_table;
8826717172SLen Brown 
8926717172SLen Brown /*
9014796fcaSLen Brown  * Hardware C-state auto-demotion may not always be optimal.
9114796fcaSLen Brown  * Indicate which enable bits to clear here.
9214796fcaSLen Brown  */
9314796fcaSLen Brown static unsigned long long auto_demotion_disable_flags;
9414796fcaSLen Brown 
9514796fcaSLen Brown /*
96956d033fSLen Brown  * Set this flag for states where the HW flushes the TLB for us
97956d033fSLen Brown  * and so we don't need cross-calls to keep it consistent.
98956d033fSLen Brown  * If this flag is set, SW flushes the TLB, so even if the
99956d033fSLen Brown  * HW doesn't do the flushing, this flag is safe to use.
100956d033fSLen Brown  */
101956d033fSLen Brown #define CPUIDLE_FLAG_TLB_FLUSHED	0x10000
102956d033fSLen Brown 
103956d033fSLen Brown /*
10426717172SLen Brown  * States are indexed by the cstate number,
10526717172SLen Brown  * which is also the index into the MWAIT hint array.
10626717172SLen Brown  * Thus C0 is a dummy.
10726717172SLen Brown  */
10826717172SLen Brown static struct cpuidle_state nehalem_cstates[MWAIT_MAX_NUM_CSTATES] = {
10926717172SLen Brown 	{ /* MWAIT C0 */ },
11026717172SLen Brown 	{ /* MWAIT C1 */
11115e123e5SThomas Renninger 		.name = "C1-NHM",
11226717172SLen Brown 		.desc = "MWAIT 0x00",
11326717172SLen Brown 		.driver_data = (void *) 0x00,
11426717172SLen Brown 		.flags = CPUIDLE_FLAG_TIME_VALID,
11526717172SLen Brown 		.exit_latency = 3,
11626717172SLen Brown 		.target_residency = 6,
11726717172SLen Brown 		.enter = &intel_idle },
11826717172SLen Brown 	{ /* MWAIT C2 */
11915e123e5SThomas Renninger 		.name = "C3-NHM",
12026717172SLen Brown 		.desc = "MWAIT 0x10",
12126717172SLen Brown 		.driver_data = (void *) 0x10,
1226110a1f4SSuresh Siddha 		.flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
12326717172SLen Brown 		.exit_latency = 20,
12426717172SLen Brown 		.target_residency = 80,
12526717172SLen Brown 		.enter = &intel_idle },
12626717172SLen Brown 	{ /* MWAIT C3 */
12715e123e5SThomas Renninger 		.name = "C6-NHM",
12826717172SLen Brown 		.desc = "MWAIT 0x20",
12926717172SLen Brown 		.driver_data = (void *) 0x20,
1306110a1f4SSuresh Siddha 		.flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
13126717172SLen Brown 		.exit_latency = 200,
13226717172SLen Brown 		.target_residency = 800,
13326717172SLen Brown 		.enter = &intel_idle },
13426717172SLen Brown };
13526717172SLen Brown 
136d13780d4SLen Brown static struct cpuidle_state snb_cstates[MWAIT_MAX_NUM_CSTATES] = {
137d13780d4SLen Brown 	{ /* MWAIT C0 */ },
138d13780d4SLen Brown 	{ /* MWAIT C1 */
13915e123e5SThomas Renninger 		.name = "C1-SNB",
140d13780d4SLen Brown 		.desc = "MWAIT 0x00",
141d13780d4SLen Brown 		.driver_data = (void *) 0x00,
142d13780d4SLen Brown 		.flags = CPUIDLE_FLAG_TIME_VALID,
143d13780d4SLen Brown 		.exit_latency = 1,
144ddbd550dSLen Brown 		.target_residency = 1,
145d13780d4SLen Brown 		.enter = &intel_idle },
146d13780d4SLen Brown 	{ /* MWAIT C2 */
14715e123e5SThomas Renninger 		.name = "C3-SNB",
148d13780d4SLen Brown 		.desc = "MWAIT 0x10",
149d13780d4SLen Brown 		.driver_data = (void *) 0x10,
15000527cc6SLen Brown 		.flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
151d13780d4SLen Brown 		.exit_latency = 80,
152ddbd550dSLen Brown 		.target_residency = 211,
153d13780d4SLen Brown 		.enter = &intel_idle },
154d13780d4SLen Brown 	{ /* MWAIT C3 */
15515e123e5SThomas Renninger 		.name = "C6-SNB",
156d13780d4SLen Brown 		.desc = "MWAIT 0x20",
157d13780d4SLen Brown 		.driver_data = (void *) 0x20,
15800527cc6SLen Brown 		.flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
159d13780d4SLen Brown 		.exit_latency = 104,
160ddbd550dSLen Brown 		.target_residency = 345,
161d13780d4SLen Brown 		.enter = &intel_idle },
162d13780d4SLen Brown 	{ /* MWAIT C4 */
16315e123e5SThomas Renninger 		.name = "C7-SNB",
164d13780d4SLen Brown 		.desc = "MWAIT 0x30",
165d13780d4SLen Brown 		.driver_data = (void *) 0x30,
16600527cc6SLen Brown 		.flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
167d13780d4SLen Brown 		.exit_latency = 109,
168ddbd550dSLen Brown 		.target_residency = 345,
169d13780d4SLen Brown 		.enter = &intel_idle },
170d13780d4SLen Brown };
171d13780d4SLen Brown 
17226717172SLen Brown static struct cpuidle_state atom_cstates[MWAIT_MAX_NUM_CSTATES] = {
17326717172SLen Brown 	{ /* MWAIT C0 */ },
17426717172SLen Brown 	{ /* MWAIT C1 */
17515e123e5SThomas Renninger 		.name = "C1-ATM",
17626717172SLen Brown 		.desc = "MWAIT 0x00",
17726717172SLen Brown 		.driver_data = (void *) 0x00,
17826717172SLen Brown 		.flags = CPUIDLE_FLAG_TIME_VALID,
17926717172SLen Brown 		.exit_latency = 1,
18026717172SLen Brown 		.target_residency = 4,
18126717172SLen Brown 		.enter = &intel_idle },
18226717172SLen Brown 	{ /* MWAIT C2 */
18315e123e5SThomas Renninger 		.name = "C2-ATM",
18426717172SLen Brown 		.desc = "MWAIT 0x10",
18526717172SLen Brown 		.driver_data = (void *) 0x10,
18626717172SLen Brown 		.flags = CPUIDLE_FLAG_TIME_VALID,
18726717172SLen Brown 		.exit_latency = 20,
18826717172SLen Brown 		.target_residency = 80,
18926717172SLen Brown 		.enter = &intel_idle },
19026717172SLen Brown 	{ /* MWAIT C3 */ },
19126717172SLen Brown 	{ /* MWAIT C4 */
19215e123e5SThomas Renninger 		.name = "C4-ATM",
19326717172SLen Brown 		.desc = "MWAIT 0x30",
19426717172SLen Brown 		.driver_data = (void *) 0x30,
1956110a1f4SSuresh Siddha 		.flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
19626717172SLen Brown 		.exit_latency = 100,
19726717172SLen Brown 		.target_residency = 400,
19826717172SLen Brown 		.enter = &intel_idle },
19926717172SLen Brown 	{ /* MWAIT C5 */ },
20026717172SLen Brown 	{ /* MWAIT C6 */
20115e123e5SThomas Renninger 		.name = "C6-ATM",
2027fcca7d9SLen Brown 		.desc = "MWAIT 0x52",
2037fcca7d9SLen Brown 		.driver_data = (void *) 0x52,
2046110a1f4SSuresh Siddha 		.flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
2057fcca7d9SLen Brown 		.exit_latency = 140,
2067fcca7d9SLen Brown 		.target_residency = 560,
2077fcca7d9SLen Brown 		.enter = &intel_idle },
20826717172SLen Brown };
20926717172SLen Brown 
21026717172SLen Brown /**
21126717172SLen Brown  * intel_idle
21226717172SLen Brown  * @dev: cpuidle_device
21326717172SLen Brown  * @state: cpuidle state
21426717172SLen Brown  *
21526717172SLen Brown  */
21626717172SLen Brown static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
21726717172SLen Brown {
21826717172SLen Brown 	unsigned long ecx = 1; /* break on interrupt flag */
21926717172SLen Brown 	unsigned long eax = (unsigned long)cpuidle_get_statedata(state);
22026717172SLen Brown 	unsigned int cstate;
22126717172SLen Brown 	ktime_t kt_before, kt_after;
22226717172SLen Brown 	s64 usec_delta;
22326717172SLen Brown 	int cpu = smp_processor_id();
22426717172SLen Brown 
22526717172SLen Brown 	cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
22626717172SLen Brown 
22726717172SLen Brown 	local_irq_disable();
22826717172SLen Brown 
2296110a1f4SSuresh Siddha 	/*
230c8381cc3SLen Brown 	 * leave_mm() to avoid costly and often unnecessary wakeups
231c8381cc3SLen Brown 	 * for flushing the user TLB's associated with the active mm.
2326110a1f4SSuresh Siddha 	 */
233c8381cc3SLen Brown 	if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
2346110a1f4SSuresh Siddha 		leave_mm(cpu);
2356110a1f4SSuresh Siddha 
23626717172SLen Brown 	if (!(lapic_timer_reliable_states & (1 << (cstate))))
23726717172SLen Brown 		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
23826717172SLen Brown 
23926717172SLen Brown 	kt_before = ktime_get_real();
24026717172SLen Brown 
24126717172SLen Brown 	stop_critical_timings();
24226717172SLen Brown 	if (!need_resched()) {
24326717172SLen Brown 
24426717172SLen Brown 		__monitor((void *)&current_thread_info()->flags, 0, 0);
24526717172SLen Brown 		smp_mb();
24626717172SLen Brown 		if (!need_resched())
24726717172SLen Brown 			__mwait(eax, ecx);
24826717172SLen Brown 	}
24926717172SLen Brown 
25026717172SLen Brown 	start_critical_timings();
25126717172SLen Brown 
25226717172SLen Brown 	kt_after = ktime_get_real();
25326717172SLen Brown 	usec_delta = ktime_to_us(ktime_sub(kt_after, kt_before));
25426717172SLen Brown 
25526717172SLen Brown 	local_irq_enable();
25626717172SLen Brown 
25726717172SLen Brown 	if (!(lapic_timer_reliable_states & (1 << (cstate))))
25826717172SLen Brown 		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
25926717172SLen Brown 
26026717172SLen Brown 	return usec_delta;
26126717172SLen Brown }
26226717172SLen Brown 
2632a2d31c8SShaohua Li static void __setup_broadcast_timer(void *arg)
2642a2d31c8SShaohua Li {
2652a2d31c8SShaohua Li 	unsigned long reason = (unsigned long)arg;
2662a2d31c8SShaohua Li 	int cpu = smp_processor_id();
2672a2d31c8SShaohua Li 
2682a2d31c8SShaohua Li 	reason = reason ?
2692a2d31c8SShaohua Li 		CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
2702a2d31c8SShaohua Li 
2712a2d31c8SShaohua Li 	clockevents_notify(reason, &cpu);
2722a2d31c8SShaohua Li }
2732a2d31c8SShaohua Li 
274ec30f343SShaohua Li static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
2752a2d31c8SShaohua Li 		unsigned long action, void *hcpu)
2762a2d31c8SShaohua Li {
2772a2d31c8SShaohua Li 	int hotcpu = (unsigned long)hcpu;
2782a2d31c8SShaohua Li 
2792a2d31c8SShaohua Li 	switch (action & 0xf) {
2802a2d31c8SShaohua Li 	case CPU_ONLINE:
2812a2d31c8SShaohua Li 		smp_call_function_single(hotcpu, __setup_broadcast_timer,
2822a2d31c8SShaohua Li 			(void *)true, 1);
2832a2d31c8SShaohua Li 		break;
2842a2d31c8SShaohua Li 	}
2852a2d31c8SShaohua Li 	return NOTIFY_OK;
2862a2d31c8SShaohua Li }
2872a2d31c8SShaohua Li 
288ec30f343SShaohua Li static struct notifier_block setup_broadcast_notifier = {
2892a2d31c8SShaohua Li 	.notifier_call = setup_broadcast_cpuhp_notify,
2902a2d31c8SShaohua Li };
2912a2d31c8SShaohua Li 
29214796fcaSLen Brown static void auto_demotion_disable(void *dummy)
29314796fcaSLen Brown {
29414796fcaSLen Brown 	unsigned long long msr_bits;
29514796fcaSLen Brown 
29614796fcaSLen Brown 	rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
29714796fcaSLen Brown 	msr_bits &= ~auto_demotion_disable_flags;
29814796fcaSLen Brown 	wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
29914796fcaSLen Brown }
30014796fcaSLen Brown 
30126717172SLen Brown /*
30226717172SLen Brown  * intel_idle_probe()
30326717172SLen Brown  */
30426717172SLen Brown static int intel_idle_probe(void)
30526717172SLen Brown {
306c4236282SLen Brown 	unsigned int eax, ebx, ecx;
30726717172SLen Brown 
30826717172SLen Brown 	if (max_cstate == 0) {
30926717172SLen Brown 		pr_debug(PREFIX "disabled\n");
31026717172SLen Brown 		return -EPERM;
31126717172SLen Brown 	}
31226717172SLen Brown 
31326717172SLen Brown 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
31426717172SLen Brown 		return -ENODEV;
31526717172SLen Brown 
31626717172SLen Brown 	if (!boot_cpu_has(X86_FEATURE_MWAIT))
31726717172SLen Brown 		return -ENODEV;
31826717172SLen Brown 
31926717172SLen Brown 	if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
32026717172SLen Brown 		return -ENODEV;
32126717172SLen Brown 
322c4236282SLen Brown 	cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
32326717172SLen Brown 
32426717172SLen Brown 	if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
32526717172SLen Brown 		!(ecx & CPUID5_ECX_INTERRUPT_BREAK))
32626717172SLen Brown 			return -ENODEV;
32726717172SLen Brown 
328c4236282SLen Brown 	pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
32926717172SLen Brown 
33026717172SLen Brown 
33126717172SLen Brown 	if (boot_cpu_data.x86 != 6)	/* family 6 */
33226717172SLen Brown 		return -ENODEV;
33326717172SLen Brown 
33426717172SLen Brown 	switch (boot_cpu_data.x86_model) {
33526717172SLen Brown 
33626717172SLen Brown 	case 0x1A:	/* Core i7, Xeon 5500 series */
33726717172SLen Brown 	case 0x1E:	/* Core i7 and i5 Processor - Lynnfield Jasper Forest */
33826717172SLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
33926717172SLen Brown 	case 0x2E:	/* Nehalem-EX Xeon */
340ec67a2baSLen Brown 	case 0x2F:	/* Westmere-EX Xeon */
34126717172SLen Brown 	case 0x25:	/* Westmere */
34226717172SLen Brown 	case 0x2C:	/* Westmere */
34326717172SLen Brown 		cpuidle_state_table = nehalem_cstates;
34414796fcaSLen Brown 		auto_demotion_disable_flags =
34514796fcaSLen Brown 			(NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE);
34626717172SLen Brown 		break;
34726717172SLen Brown 
34826717172SLen Brown 	case 0x1C:	/* 28 - Atom Processor */
349bfb53ccfSLen Brown 		cpuidle_state_table = atom_cstates;
350bfb53ccfSLen Brown 		break;
351bfb53ccfSLen Brown 
3524725fd3cSArjan van de Ven 	case 0x26:	/* 38 - Lincroft Atom Processor */
35326717172SLen Brown 		cpuidle_state_table = atom_cstates;
354bfb53ccfSLen Brown 		auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE;
35526717172SLen Brown 		break;
356d13780d4SLen Brown 
357d13780d4SLen Brown 	case 0x2A:	/* SNB */
358d13780d4SLen Brown 	case 0x2D:	/* SNB Xeon */
359d13780d4SLen Brown 		cpuidle_state_table = snb_cstates;
360d13780d4SLen Brown 		break;
36126717172SLen Brown 
36226717172SLen Brown 	default:
36326717172SLen Brown 		pr_debug(PREFIX "does not run on family %d model %d\n",
36426717172SLen Brown 			boot_cpu_data.x86, boot_cpu_data.x86_model);
36526717172SLen Brown 		return -ENODEV;
36626717172SLen Brown 	}
36726717172SLen Brown 
36856b9aea3SLen Brown 	if (boot_cpu_has(X86_FEATURE_ARAT))	/* Always Reliable APIC Timer */
3692a2d31c8SShaohua Li 		lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
3702a2d31c8SShaohua Li 	else {
3712a2d31c8SShaohua Li 		smp_call_function(__setup_broadcast_timer, (void *)true, 1);
3722a2d31c8SShaohua Li 		register_cpu_notifier(&setup_broadcast_notifier);
3732a2d31c8SShaohua Li 	}
37456b9aea3SLen Brown 
37526717172SLen Brown 	pr_debug(PREFIX "v" INTEL_IDLE_VERSION
37626717172SLen Brown 		" model 0x%X\n", boot_cpu_data.x86_model);
37726717172SLen Brown 
37826717172SLen Brown 	pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
37926717172SLen Brown 		lapic_timer_reliable_states);
38026717172SLen Brown 	return 0;
38126717172SLen Brown }
38226717172SLen Brown 
38326717172SLen Brown /*
38426717172SLen Brown  * intel_idle_cpuidle_devices_uninit()
38526717172SLen Brown  * unregister, free cpuidle_devices
38626717172SLen Brown  */
38726717172SLen Brown static void intel_idle_cpuidle_devices_uninit(void)
38826717172SLen Brown {
38926717172SLen Brown 	int i;
39026717172SLen Brown 	struct cpuidle_device *dev;
39126717172SLen Brown 
39226717172SLen Brown 	for_each_online_cpu(i) {
39326717172SLen Brown 		dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
39426717172SLen Brown 		cpuidle_unregister_device(dev);
39526717172SLen Brown 	}
39626717172SLen Brown 
39726717172SLen Brown 	free_percpu(intel_idle_cpuidle_devices);
39826717172SLen Brown 	return;
39926717172SLen Brown }
40026717172SLen Brown /*
40126717172SLen Brown  * intel_idle_cpuidle_devices_init()
40226717172SLen Brown  * allocate, initialize, register cpuidle_devices
40326717172SLen Brown  */
40426717172SLen Brown static int intel_idle_cpuidle_devices_init(void)
40526717172SLen Brown {
40626717172SLen Brown 	int i, cstate;
40726717172SLen Brown 	struct cpuidle_device *dev;
40826717172SLen Brown 
40926717172SLen Brown 	intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
41026717172SLen Brown 	if (intel_idle_cpuidle_devices == NULL)
41126717172SLen Brown 		return -ENOMEM;
41226717172SLen Brown 
41326717172SLen Brown 	for_each_online_cpu(i) {
41426717172SLen Brown 		dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
41526717172SLen Brown 
41626717172SLen Brown 		dev->state_count = 1;
41726717172SLen Brown 
41826717172SLen Brown 		for (cstate = 1; cstate < MWAIT_MAX_NUM_CSTATES; ++cstate) {
41926717172SLen Brown 			int num_substates;
42026717172SLen Brown 
42126717172SLen Brown 			if (cstate > max_cstate) {
42226717172SLen Brown 				printk(PREFIX "max_cstate %d reached\n",
42326717172SLen Brown 					max_cstate);
42426717172SLen Brown 				break;
42526717172SLen Brown 			}
42626717172SLen Brown 
42726717172SLen Brown 			/* does the state exist in CPUID.MWAIT? */
428c4236282SLen Brown 			num_substates = (mwait_substates >> ((cstate) * 4))
42926717172SLen Brown 						& MWAIT_SUBSTATE_MASK;
43026717172SLen Brown 			if (num_substates == 0)
43126717172SLen Brown 				continue;
43226717172SLen Brown 			/* is the state not enabled? */
43326717172SLen Brown 			if (cpuidle_state_table[cstate].enter == NULL) {
43426717172SLen Brown 				/* does the driver not know about the state? */
43526717172SLen Brown 				if (*cpuidle_state_table[cstate].name == '\0')
43626717172SLen Brown 					pr_debug(PREFIX "unaware of model 0x%x"
43726717172SLen Brown 						" MWAIT %d please"
43826717172SLen Brown 						" contact lenb@kernel.org",
43926717172SLen Brown 					boot_cpu_data.x86_model, cstate);
44026717172SLen Brown 				continue;
44126717172SLen Brown 			}
44226717172SLen Brown 
44326717172SLen Brown 			if ((cstate > 2) &&
44426717172SLen Brown 				!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
44526717172SLen Brown 				mark_tsc_unstable("TSC halts in idle"
44626717172SLen Brown 					" states deeper than C2");
44726717172SLen Brown 
44826717172SLen Brown 			dev->states[dev->state_count] =	/* structure copy */
44926717172SLen Brown 				cpuidle_state_table[cstate];
45026717172SLen Brown 
45126717172SLen Brown 			dev->state_count += 1;
45226717172SLen Brown 		}
45326717172SLen Brown 
45426717172SLen Brown 		dev->cpu = i;
45526717172SLen Brown 		if (cpuidle_register_device(dev)) {
45626717172SLen Brown 			pr_debug(PREFIX "cpuidle_register_device %d failed!\n",
45726717172SLen Brown 				 i);
45826717172SLen Brown 			intel_idle_cpuidle_devices_uninit();
45926717172SLen Brown 			return -EIO;
46026717172SLen Brown 		}
46126717172SLen Brown 	}
46214796fcaSLen Brown 	if (auto_demotion_disable_flags)
46314796fcaSLen Brown 		smp_call_function(auto_demotion_disable, NULL, 1);
46426717172SLen Brown 
46526717172SLen Brown 	return 0;
46626717172SLen Brown }
46726717172SLen Brown 
46826717172SLen Brown 
46926717172SLen Brown static int __init intel_idle_init(void)
47026717172SLen Brown {
47126717172SLen Brown 	int retval;
47226717172SLen Brown 
473d1896049SThomas Renninger 	/* Do not load intel_idle at all for now if idle= is passed */
474d1896049SThomas Renninger 	if (boot_option_idle_override != IDLE_NO_OVERRIDE)
475d1896049SThomas Renninger 		return -ENODEV;
476d1896049SThomas Renninger 
47726717172SLen Brown 	retval = intel_idle_probe();
47826717172SLen Brown 	if (retval)
47926717172SLen Brown 		return retval;
48026717172SLen Brown 
48126717172SLen Brown 	retval = cpuidle_register_driver(&intel_idle_driver);
48226717172SLen Brown 	if (retval) {
48326717172SLen Brown 		printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
48426717172SLen Brown 			cpuidle_get_driver()->name);
48526717172SLen Brown 		return retval;
48626717172SLen Brown 	}
48726717172SLen Brown 
48826717172SLen Brown 	retval = intel_idle_cpuidle_devices_init();
48926717172SLen Brown 	if (retval) {
49026717172SLen Brown 		cpuidle_unregister_driver(&intel_idle_driver);
49126717172SLen Brown 		return retval;
49226717172SLen Brown 	}
49326717172SLen Brown 
49426717172SLen Brown 	return 0;
49526717172SLen Brown }
49626717172SLen Brown 
49726717172SLen Brown static void __exit intel_idle_exit(void)
49826717172SLen Brown {
49926717172SLen Brown 	intel_idle_cpuidle_devices_uninit();
50026717172SLen Brown 	cpuidle_unregister_driver(&intel_idle_driver);
50126717172SLen Brown 
5022a2d31c8SShaohua Li 	if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE) {
5032a2d31c8SShaohua Li 		smp_call_function(__setup_broadcast_timer, (void *)false, 1);
5042a2d31c8SShaohua Li 		unregister_cpu_notifier(&setup_broadcast_notifier);
5052a2d31c8SShaohua Li 	}
5062a2d31c8SShaohua Li 
50726717172SLen Brown 	return;
50826717172SLen Brown }
50926717172SLen Brown 
51026717172SLen Brown module_init(intel_idle_init);
51126717172SLen Brown module_exit(intel_idle_exit);
51226717172SLen Brown 
51326717172SLen Brown module_param(max_cstate, int, 0444);
51426717172SLen Brown 
51526717172SLen Brown MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
51626717172SLen Brown MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
51726717172SLen Brown MODULE_LICENSE("GPL");
518