xref: /linux/drivers/cpuidle/cpuidle.c (revision 554c06ba3ee29cf453fca17e9e61120b75aa476d)
14f86d3a8SLen Brown /*
24f86d3a8SLen Brown  * cpuidle.c - core cpuidle infrastructure
34f86d3a8SLen Brown  *
44f86d3a8SLen Brown  * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
54f86d3a8SLen Brown  *               Shaohua Li <shaohua.li@intel.com>
64f86d3a8SLen Brown  *               Adam Belay <abelay@novell.com>
74f86d3a8SLen Brown  *
84f86d3a8SLen Brown  * This code is licenced under the GPL.
94f86d3a8SLen Brown  */
104f86d3a8SLen Brown 
11b60e6a0eSDaniel Lezcano #include <linux/clockchips.h>
124f86d3a8SLen Brown #include <linux/kernel.h>
134f86d3a8SLen Brown #include <linux/mutex.h>
144f86d3a8SLen Brown #include <linux/sched.h>
154f86d3a8SLen Brown #include <linux/notifier.h>
16e8db0be1SJean Pihet #include <linux/pm_qos.h>
174f86d3a8SLen Brown #include <linux/cpu.h>
184f86d3a8SLen Brown #include <linux/cpuidle.h>
199a0b8415Svenkatesh.pallipadi@intel.com #include <linux/ktime.h>
202e94d1f7SArjan van de Ven #include <linux/hrtimer.h>
21884b17e1SPaul Gortmaker #include <linux/module.h>
22288f023eSArjan van de Ven #include <trace/events/power.h>
234f86d3a8SLen Brown 
244f86d3a8SLen Brown #include "cpuidle.h"
254f86d3a8SLen Brown 
264f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
274f86d3a8SLen Brown 
284f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock);
294f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices);
304f86d3a8SLen Brown 
314f86d3a8SLen Brown static int enabled_devices;
3262027aeaSLen Brown static int off __read_mostly;
33a0bfa137SLen Brown static int initialized __read_mostly;
3462027aeaSLen Brown 
3562027aeaSLen Brown int cpuidle_disabled(void)
3662027aeaSLen Brown {
3762027aeaSLen Brown 	return off;
3862027aeaSLen Brown }
39d91ee586SLen Brown void disable_cpuidle(void)
40d91ee586SLen Brown {
41d91ee586SLen Brown 	off = 1;
42d91ee586SLen Brown }
434f86d3a8SLen Brown 
44dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev);
45dcb84f33SVenkatesh Pallipadi 
464f86d3a8SLen Brown /**
471a022e3fSBoris Ostrovsky  * cpuidle_play_dead - cpu off-lining
481a022e3fSBoris Ostrovsky  *
49ee01e663SToshi Kani  * Returns in case of an error or no driver
501a022e3fSBoris Ostrovsky  */
511a022e3fSBoris Ostrovsky int cpuidle_play_dead(void)
521a022e3fSBoris Ostrovsky {
531a022e3fSBoris Ostrovsky 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
54bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
558aef33a7SDaniel Lezcano 	int i;
561a022e3fSBoris Ostrovsky 
57ee01e663SToshi Kani 	if (!drv)
58ee01e663SToshi Kani 		return -ENODEV;
59ee01e663SToshi Kani 
601a022e3fSBoris Ostrovsky 	/* Find lowest-power state that supports long-term idle */
618aef33a7SDaniel Lezcano 	for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
628aef33a7SDaniel Lezcano 		if (drv->states[i].enter_dead)
638aef33a7SDaniel Lezcano 			return drv->states[i].enter_dead(dev, i);
641a022e3fSBoris Ostrovsky 
651a022e3fSBoris Ostrovsky 	return -ENODEV;
661a022e3fSBoris Ostrovsky }
671a022e3fSBoris Ostrovsky 
681a022e3fSBoris Ostrovsky /**
6956cfbf74SColin Cross  * cpuidle_enter_state - enter the state and update stats
7056cfbf74SColin Cross  * @dev: cpuidle device for this cpu
7156cfbf74SColin Cross  * @drv: cpuidle driver for this cpu
7256cfbf74SColin Cross  * @next_state: index into drv->states of the state to enter
7356cfbf74SColin Cross  */
7456cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
75*554c06baSDaniel Lezcano 			int index)
7656cfbf74SColin Cross {
7756cfbf74SColin Cross 	int entered_state;
7856cfbf74SColin Cross 
79*554c06baSDaniel Lezcano 	struct cpuidle_state *target_state = &drv->states[index];
80*554c06baSDaniel Lezcano 	ktime_t time_start, time_end;
81*554c06baSDaniel Lezcano 	s64 diff;
82*554c06baSDaniel Lezcano 
83*554c06baSDaniel Lezcano 	time_start = ktime_get();
84*554c06baSDaniel Lezcano 
85*554c06baSDaniel Lezcano 	entered_state = target_state->enter(dev, drv, index);
86*554c06baSDaniel Lezcano 
87*554c06baSDaniel Lezcano 	time_end = ktime_get();
88*554c06baSDaniel Lezcano 
89*554c06baSDaniel Lezcano 	local_irq_enable();
90*554c06baSDaniel Lezcano 
91*554c06baSDaniel Lezcano 	diff = ktime_to_us(ktime_sub(time_end, time_start));
92*554c06baSDaniel Lezcano 	if (diff > INT_MAX)
93*554c06baSDaniel Lezcano 		diff = INT_MAX;
94*554c06baSDaniel Lezcano 
95*554c06baSDaniel Lezcano 	dev->last_residency = (int) diff;
9656cfbf74SColin Cross 
9756cfbf74SColin Cross 	if (entered_state >= 0) {
9856cfbf74SColin Cross 		/* Update cpuidle counters */
9956cfbf74SColin Cross 		/* This can be moved to within driver enter routine
10056cfbf74SColin Cross 		 * but that results in multiple copies of same code.
10156cfbf74SColin Cross 		 */
102a474a515SJulius Werner 		dev->states_usage[entered_state].time += dev->last_residency;
10356cfbf74SColin Cross 		dev->states_usage[entered_state].usage++;
10456cfbf74SColin Cross 	} else {
10556cfbf74SColin Cross 		dev->last_residency = 0;
10656cfbf74SColin Cross 	}
10756cfbf74SColin Cross 
10856cfbf74SColin Cross 	return entered_state;
10956cfbf74SColin Cross }
11056cfbf74SColin Cross 
11156cfbf74SColin Cross /**
1124f86d3a8SLen Brown  * cpuidle_idle_call - the main idle loop
1134f86d3a8SLen Brown  *
1144f86d3a8SLen Brown  * NOTE: no locks or semaphores should be used here
115a0bfa137SLen Brown  * return non-zero on failure
1164f86d3a8SLen Brown  */
117a0bfa137SLen Brown int cpuidle_idle_call(void)
1184f86d3a8SLen Brown {
1194a6f4fe8SChristoph Lameter 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
120bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
121e978aa7dSDeepthi Dharwar 	int next_state, entered_state;
1224f86d3a8SLen Brown 
123a0bfa137SLen Brown 	if (off)
124a0bfa137SLen Brown 		return -ENODEV;
125a0bfa137SLen Brown 
126a0bfa137SLen Brown 	if (!initialized)
127a0bfa137SLen Brown 		return -ENODEV;
128a0bfa137SLen Brown 
1294f86d3a8SLen Brown 	/* check if the device is ready */
130a0bfa137SLen Brown 	if (!dev || !dev->enabled)
131a0bfa137SLen Brown 		return -EBUSY;
1324f86d3a8SLen Brown 
133bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
134bf4d1b5dSDaniel Lezcano 
1354f86d3a8SLen Brown 	/* ask the governor for the next state */
13646bcfad7SDeepthi Dharwar 	next_state = cpuidle_curr_governor->select(drv, dev);
137246eb7f0SKevin Hilman 	if (need_resched()) {
138d73d68dcSYouquan Song 		dev->last_residency = 0;
139d73d68dcSYouquan Song 		/* give the governor an opportunity to reflect on the outcome */
140d73d68dcSYouquan Song 		if (cpuidle_curr_governor->reflect)
141d73d68dcSYouquan Song 			cpuidle_curr_governor->reflect(dev, next_state);
142246eb7f0SKevin Hilman 		local_irq_enable();
143a0bfa137SLen Brown 		return 0;
144246eb7f0SKevin Hilman 	}
145246eb7f0SKevin Hilman 
14676027ea8SSteven Rostedt 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
147f77cfe4eSThomas Renninger 
148b60e6a0eSDaniel Lezcano 	if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
149b60e6a0eSDaniel Lezcano 		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
150b60e6a0eSDaniel Lezcano 				   &dev->cpu);
151b60e6a0eSDaniel Lezcano 
1524126c019SColin Cross 	if (cpuidle_state_is_coupled(dev, drv, next_state))
1534126c019SColin Cross 		entered_state = cpuidle_enter_state_coupled(dev, drv,
1544126c019SColin Cross 							    next_state);
1554126c019SColin Cross 	else
15656cfbf74SColin Cross 		entered_state = cpuidle_enter_state(dev, drv, next_state);
157f77cfe4eSThomas Renninger 
158b60e6a0eSDaniel Lezcano 	if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
159b60e6a0eSDaniel Lezcano 		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
160b60e6a0eSDaniel Lezcano 				   &dev->cpu);
161b60e6a0eSDaniel Lezcano 
16276027ea8SSteven Rostedt 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
163f77cfe4eSThomas Renninger 
1644f86d3a8SLen Brown 	/* give the governor an opportunity to reflect on the outcome */
1654f86d3a8SLen Brown 	if (cpuidle_curr_governor->reflect)
166e978aa7dSDeepthi Dharwar 		cpuidle_curr_governor->reflect(dev, entered_state);
167a0bfa137SLen Brown 
168a0bfa137SLen Brown 	return 0;
1694f86d3a8SLen Brown }
1704f86d3a8SLen Brown 
1714f86d3a8SLen Brown /**
1724f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
1734f86d3a8SLen Brown  */
1744f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
1754f86d3a8SLen Brown {
176a0bfa137SLen Brown 	if (enabled_devices) {
1774f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
1784f86d3a8SLen Brown 		smp_wmb();
179a0bfa137SLen Brown 		initialized = 1;
1804f86d3a8SLen Brown 	}
1814f86d3a8SLen Brown }
1824f86d3a8SLen Brown 
1834f86d3a8SLen Brown /**
1844f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
1854f86d3a8SLen Brown  */
1864f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
1874f86d3a8SLen Brown {
188a0bfa137SLen Brown 	if (enabled_devices) {
189a0bfa137SLen Brown 		initialized = 0;
1904a162513SThomas Gleixner 		kick_all_cpus_sync();
1914f86d3a8SLen Brown 	}
1924f86d3a8SLen Brown }
1934f86d3a8SLen Brown 
1944f86d3a8SLen Brown /**
1954f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
1964f86d3a8SLen Brown  */
1974f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
1984f86d3a8SLen Brown {
1994f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
2004f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
2014f86d3a8SLen Brown }
2024f86d3a8SLen Brown 
2034f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
2044f86d3a8SLen Brown 
2054f86d3a8SLen Brown /**
2064f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
2074f86d3a8SLen Brown  */
2084f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
2094f86d3a8SLen Brown {
2104f86d3a8SLen Brown 	cpuidle_install_idle_handler();
2114f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
2124f86d3a8SLen Brown }
2134f86d3a8SLen Brown 
2144f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
2154f86d3a8SLen Brown 
2168651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
2178651f97bSPreeti U Murthy void cpuidle_pause(void)
2188651f97bSPreeti U Murthy {
2198651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
2208651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
2218651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
2228651f97bSPreeti U Murthy }
2238651f97bSPreeti U Murthy 
2248651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
2258651f97bSPreeti U Murthy void cpuidle_resume(void)
2268651f97bSPreeti U Murthy {
2278651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
2288651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
2298651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
2308651f97bSPreeti U Murthy }
2318651f97bSPreeti U Murthy 
232d8c216cfSRafael J. Wysocki #ifdef CONFIG_ARCH_HAS_CPU_RELAX
23346bcfad7SDeepthi Dharwar static int poll_idle(struct cpuidle_device *dev,
23446bcfad7SDeepthi Dharwar 		struct cpuidle_driver *drv, int index)
235d8c216cfSRafael J. Wysocki {
236d8c216cfSRafael J. Wysocki 	ktime_t	t1, t2;
237d8c216cfSRafael J. Wysocki 	s64 diff;
238d8c216cfSRafael J. Wysocki 
239d8c216cfSRafael J. Wysocki 	t1 = ktime_get();
240d8c216cfSRafael J. Wysocki 	local_irq_enable();
241d8c216cfSRafael J. Wysocki 	while (!need_resched())
242d8c216cfSRafael J. Wysocki 		cpu_relax();
243d8c216cfSRafael J. Wysocki 
244d8c216cfSRafael J. Wysocki 	t2 = ktime_get();
245d8c216cfSRafael J. Wysocki 	diff = ktime_to_us(ktime_sub(t2, t1));
246d8c216cfSRafael J. Wysocki 	if (diff > INT_MAX)
247d8c216cfSRafael J. Wysocki 		diff = INT_MAX;
248d8c216cfSRafael J. Wysocki 
249e978aa7dSDeepthi Dharwar 	dev->last_residency = (int) diff;
250e978aa7dSDeepthi Dharwar 
251e978aa7dSDeepthi Dharwar 	return index;
252d8c216cfSRafael J. Wysocki }
253d8c216cfSRafael J. Wysocki 
25446bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv)
255d8c216cfSRafael J. Wysocki {
25646bcfad7SDeepthi Dharwar 	struct cpuidle_state *state = &drv->states[0];
257d8c216cfSRafael J. Wysocki 
258720f1c30SThomas Renninger 	snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
259d8c216cfSRafael J. Wysocki 	snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
260d8c216cfSRafael J. Wysocki 	state->exit_latency = 0;
261d8c216cfSRafael J. Wysocki 	state->target_residency = 0;
262d8c216cfSRafael J. Wysocki 	state->power_usage = -1;
263d247632cSLen Brown 	state->flags = 0;
264d8c216cfSRafael J. Wysocki 	state->enter = poll_idle;
265cbc9ef02SRafael J. Wysocki 	state->disabled = false;
266d8c216cfSRafael J. Wysocki }
267d8c216cfSRafael J. Wysocki #else
26846bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) {}
269d8c216cfSRafael J. Wysocki #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
270d8c216cfSRafael J. Wysocki 
2714f86d3a8SLen Brown /**
2724f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
2734f86d3a8SLen Brown  * @dev: the CPU
2744f86d3a8SLen Brown  *
2754f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
2764f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
2774f86d3a8SLen Brown  */
2784f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
2794f86d3a8SLen Brown {
2804f86d3a8SLen Brown 	int ret, i;
281bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
2824f86d3a8SLen Brown 
2831b0a0e9aSSrivatsa S. Bhat 	if (!dev)
2841b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
2851b0a0e9aSSrivatsa S. Bhat 
2864f86d3a8SLen Brown 	if (dev->enabled)
2874f86d3a8SLen Brown 		return 0;
288bf4d1b5dSDaniel Lezcano 
289bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
290bf4d1b5dSDaniel Lezcano 
291e1689795SRobert Lee 	if (!drv || !cpuidle_curr_governor)
2924f86d3a8SLen Brown 		return -EIO;
293bf4d1b5dSDaniel Lezcano 
2944f86d3a8SLen Brown 	if (!dev->state_count)
295fc850f39SDaniel Lezcano 		dev->state_count = drv->state_count;
2964f86d3a8SLen Brown 
297dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0) {
298dcb84f33SVenkatesh Pallipadi 		ret = __cpuidle_register_device(dev);
299dcb84f33SVenkatesh Pallipadi 		if (ret)
300dcb84f33SVenkatesh Pallipadi 			return ret;
301dcb84f33SVenkatesh Pallipadi 	}
302dcb84f33SVenkatesh Pallipadi 
303e1689795SRobert Lee 	poll_idle_init(drv);
304d8c216cfSRafael J. Wysocki 
305bf4d1b5dSDaniel Lezcano 	ret = cpuidle_add_device_sysfs(dev);
306bf4d1b5dSDaniel Lezcano 	if (ret)
3074f86d3a8SLen Brown 		return ret;
3084f86d3a8SLen Brown 
3094f86d3a8SLen Brown 	if (cpuidle_curr_governor->enable &&
310e1689795SRobert Lee 	    (ret = cpuidle_curr_governor->enable(drv, dev)))
3114f86d3a8SLen Brown 		goto fail_sysfs;
3124f86d3a8SLen Brown 
3134f86d3a8SLen Brown 	for (i = 0; i < dev->state_count; i++) {
3144202735eSDeepthi Dharwar 		dev->states_usage[i].usage = 0;
3154202735eSDeepthi Dharwar 		dev->states_usage[i].time = 0;
3164f86d3a8SLen Brown 	}
3174f86d3a8SLen Brown 	dev->last_residency = 0;
3184f86d3a8SLen Brown 
3194f86d3a8SLen Brown 	smp_wmb();
3204f86d3a8SLen Brown 
3214f86d3a8SLen Brown 	dev->enabled = 1;
3224f86d3a8SLen Brown 
3234f86d3a8SLen Brown 	enabled_devices++;
3244f86d3a8SLen Brown 	return 0;
3254f86d3a8SLen Brown 
3264f86d3a8SLen Brown fail_sysfs:
327bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
3284f86d3a8SLen Brown 
3294f86d3a8SLen Brown 	return ret;
3304f86d3a8SLen Brown }
3314f86d3a8SLen Brown 
3324f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
3334f86d3a8SLen Brown 
3344f86d3a8SLen Brown /**
3354f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
3364f86d3a8SLen Brown  * @dev: the CPU
3374f86d3a8SLen Brown  *
3384f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
3394f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
3404f86d3a8SLen Brown  */
3414f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
3424f86d3a8SLen Brown {
343bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
344bf4d1b5dSDaniel Lezcano 
345cf31cd1aSSrivatsa S. Bhat 	if (!dev || !dev->enabled)
3464f86d3a8SLen Brown 		return;
347bf4d1b5dSDaniel Lezcano 
348bf4d1b5dSDaniel Lezcano 	if (!drv || !cpuidle_curr_governor)
3494f86d3a8SLen Brown 		return;
3504f86d3a8SLen Brown 
3514f86d3a8SLen Brown 	dev->enabled = 0;
3524f86d3a8SLen Brown 
3534f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
354bf4d1b5dSDaniel Lezcano 		cpuidle_curr_governor->disable(drv, dev);
3554f86d3a8SLen Brown 
356bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
3574f86d3a8SLen Brown 	enabled_devices--;
3584f86d3a8SLen Brown }
3594f86d3a8SLen Brown 
3604f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
3614f86d3a8SLen Brown 
3624f86d3a8SLen Brown /**
363dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
364dcb84f33SVenkatesh Pallipadi  * and enable routines
3654f86d3a8SLen Brown  * @dev: the cpu
366dcb84f33SVenkatesh Pallipadi  *
367dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
3684f86d3a8SLen Brown  */
369dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
3704f86d3a8SLen Brown {
3714f86d3a8SLen Brown 	int ret;
372bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
3734f86d3a8SLen Brown 
374bf4d1b5dSDaniel Lezcano 	if (!try_module_get(drv->owner))
3754f86d3a8SLen Brown 		return -EINVAL;
3764f86d3a8SLen Brown 
3774f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
3784f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
3791aef40e2SDaniel Lezcano 	ret = cpuidle_add_sysfs(dev);
3803af272abSColin Cross 	if (ret)
3813af272abSColin Cross 		goto err_sysfs;
3824f86d3a8SLen Brown 
3834126c019SColin Cross 	ret = cpuidle_coupled_register_device(dev);
3844126c019SColin Cross 	if (ret)
3854126c019SColin Cross 		goto err_coupled;
3864f86d3a8SLen Brown 
387dcb84f33SVenkatesh Pallipadi 	dev->registered = 1;
388dcb84f33SVenkatesh Pallipadi 	return 0;
3893af272abSColin Cross 
3904126c019SColin Cross err_coupled:
3911aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
3923af272abSColin Cross err_sysfs:
3933af272abSColin Cross 	list_del(&dev->device_list);
3943af272abSColin Cross 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
395bf4d1b5dSDaniel Lezcano 	module_put(drv->owner);
3963af272abSColin Cross 	return ret;
397dcb84f33SVenkatesh Pallipadi }
398dcb84f33SVenkatesh Pallipadi 
399dcb84f33SVenkatesh Pallipadi /**
400dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
401dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
402dcb84f33SVenkatesh Pallipadi  */
403dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
404dcb84f33SVenkatesh Pallipadi {
405dcb84f33SVenkatesh Pallipadi 	int ret;
406dcb84f33SVenkatesh Pallipadi 
4071b0a0e9aSSrivatsa S. Bhat 	if (!dev)
4081b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
4091b0a0e9aSSrivatsa S. Bhat 
410dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
411dcb84f33SVenkatesh Pallipadi 
412dcb84f33SVenkatesh Pallipadi 	if ((ret = __cpuidle_register_device(dev))) {
413dcb84f33SVenkatesh Pallipadi 		mutex_unlock(&cpuidle_lock);
414dcb84f33SVenkatesh Pallipadi 		return ret;
415dcb84f33SVenkatesh Pallipadi 	}
416dcb84f33SVenkatesh Pallipadi 
4174f86d3a8SLen Brown 	cpuidle_enable_device(dev);
4184f86d3a8SLen Brown 	cpuidle_install_idle_handler();
4194f86d3a8SLen Brown 
4204f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
4214f86d3a8SLen Brown 
4224f86d3a8SLen Brown 	return 0;
4234f86d3a8SLen Brown 
4244f86d3a8SLen Brown }
4254f86d3a8SLen Brown 
4264f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
4274f86d3a8SLen Brown 
4284f86d3a8SLen Brown /**
4294f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
4304f86d3a8SLen Brown  * @dev: the cpu
4314f86d3a8SLen Brown  */
4324f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
4334f86d3a8SLen Brown {
434bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4354f86d3a8SLen Brown 
436dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0)
437dcb84f33SVenkatesh Pallipadi 		return;
438dcb84f33SVenkatesh Pallipadi 
4394f86d3a8SLen Brown 	cpuidle_pause_and_lock();
4404f86d3a8SLen Brown 
4414f86d3a8SLen Brown 	cpuidle_disable_device(dev);
4424f86d3a8SLen Brown 
4431aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
4444f86d3a8SLen Brown 	list_del(&dev->device_list);
4454f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
4464f86d3a8SLen Brown 
4474126c019SColin Cross 	cpuidle_coupled_unregister_device(dev);
4484126c019SColin Cross 
4494f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
4504f86d3a8SLen Brown 
451bf4d1b5dSDaniel Lezcano 	module_put(drv->owner);
4524f86d3a8SLen Brown }
4534f86d3a8SLen Brown 
4544f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
4554f86d3a8SLen Brown 
4564f86d3a8SLen Brown #ifdef CONFIG_SMP
4574f86d3a8SLen Brown 
4584f86d3a8SLen Brown static void smp_callback(void *v)
4594f86d3a8SLen Brown {
4604f86d3a8SLen Brown 	/* we already woke the CPU up, nothing more to do */
4614f86d3a8SLen Brown }
4624f86d3a8SLen Brown 
4634f86d3a8SLen Brown /*
4644f86d3a8SLen Brown  * This function gets called when a part of the kernel has a new latency
4654f86d3a8SLen Brown  * requirement.  This means we need to get all processors out of their C-state,
4664f86d3a8SLen Brown  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
4674f86d3a8SLen Brown  * wakes them all right up.
4684f86d3a8SLen Brown  */
4694f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b,
4704f86d3a8SLen Brown 		unsigned long l, void *v)
4714f86d3a8SLen Brown {
4728691e5a8SJens Axboe 	smp_call_function(smp_callback, NULL, 1);
4734f86d3a8SLen Brown 	return NOTIFY_OK;
4744f86d3a8SLen Brown }
4754f86d3a8SLen Brown 
4764f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = {
4774f86d3a8SLen Brown 	.notifier_call = cpuidle_latency_notify,
4784f86d3a8SLen Brown };
4794f86d3a8SLen Brown 
480d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n)
481d82b3518SMark Gross {
482d82b3518SMark Gross 	pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
483d82b3518SMark Gross }
4844f86d3a8SLen Brown 
4854f86d3a8SLen Brown #else /* CONFIG_SMP */
4864f86d3a8SLen Brown 
4874f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0)
4884f86d3a8SLen Brown 
4894f86d3a8SLen Brown #endif /* CONFIG_SMP */
4904f86d3a8SLen Brown 
4914f86d3a8SLen Brown /**
4924f86d3a8SLen Brown  * cpuidle_init - core initializer
4934f86d3a8SLen Brown  */
4944f86d3a8SLen Brown static int __init cpuidle_init(void)
4954f86d3a8SLen Brown {
4964f86d3a8SLen Brown 	int ret;
4974f86d3a8SLen Brown 
49862027aeaSLen Brown 	if (cpuidle_disabled())
49962027aeaSLen Brown 		return -ENODEV;
50062027aeaSLen Brown 
5018a25a2fdSKay Sievers 	ret = cpuidle_add_interface(cpu_subsys.dev_root);
5024f86d3a8SLen Brown 	if (ret)
5034f86d3a8SLen Brown 		return ret;
5044f86d3a8SLen Brown 
5054f86d3a8SLen Brown 	latency_notifier_init(&cpuidle_latency_notifier);
5064f86d3a8SLen Brown 
5074f86d3a8SLen Brown 	return 0;
5084f86d3a8SLen Brown }
5094f86d3a8SLen Brown 
51062027aeaSLen Brown module_param(off, int, 0444);
5114f86d3a8SLen Brown core_initcall(cpuidle_init);
512