xref: /linux/drivers/cpuidle/cpuidle.c (revision 8651f97bd951d0bb1c10fa24e3fa3455193f3548)
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 
114f86d3a8SLen Brown #include <linux/kernel.h>
124f86d3a8SLen Brown #include <linux/mutex.h>
134f86d3a8SLen Brown #include <linux/sched.h>
144f86d3a8SLen Brown #include <linux/notifier.h>
15e8db0be1SJean Pihet #include <linux/pm_qos.h>
164f86d3a8SLen Brown #include <linux/cpu.h>
174f86d3a8SLen Brown #include <linux/cpuidle.h>
189a0b8415Svenkatesh.pallipadi@intel.com #include <linux/ktime.h>
192e94d1f7SArjan van de Ven #include <linux/hrtimer.h>
20884b17e1SPaul Gortmaker #include <linux/module.h>
21288f023eSArjan van de Ven #include <trace/events/power.h>
224f86d3a8SLen Brown 
234f86d3a8SLen Brown #include "cpuidle.h"
244f86d3a8SLen Brown 
254f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
264f86d3a8SLen Brown 
274f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock);
284f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices);
294f86d3a8SLen Brown 
304f86d3a8SLen Brown static int enabled_devices;
3162027aeaSLen Brown static int off __read_mostly;
32a0bfa137SLen Brown static int initialized __read_mostly;
3362027aeaSLen Brown 
3462027aeaSLen Brown int cpuidle_disabled(void)
3562027aeaSLen Brown {
3662027aeaSLen Brown 	return off;
3762027aeaSLen Brown }
38d91ee586SLen Brown void disable_cpuidle(void)
39d91ee586SLen Brown {
40d91ee586SLen Brown 	off = 1;
41d91ee586SLen Brown }
424f86d3a8SLen Brown 
43dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev);
44dcb84f33SVenkatesh Pallipadi 
45e1689795SRobert Lee static inline int cpuidle_enter(struct cpuidle_device *dev,
46e1689795SRobert Lee 				struct cpuidle_driver *drv, int index)
47e1689795SRobert Lee {
48e1689795SRobert Lee 	struct cpuidle_state *target_state = &drv->states[index];
49e1689795SRobert Lee 	return target_state->enter(dev, drv, index);
50e1689795SRobert Lee }
51e1689795SRobert Lee 
52e1689795SRobert Lee static inline int cpuidle_enter_tk(struct cpuidle_device *dev,
53e1689795SRobert Lee 			       struct cpuidle_driver *drv, int index)
54e1689795SRobert Lee {
55e1689795SRobert Lee 	return cpuidle_wrap_enter(dev, drv, index, cpuidle_enter);
56e1689795SRobert Lee }
57e1689795SRobert Lee 
58e1689795SRobert Lee typedef int (*cpuidle_enter_t)(struct cpuidle_device *dev,
59e1689795SRobert Lee 			       struct cpuidle_driver *drv, int index);
60e1689795SRobert Lee 
61e1689795SRobert Lee static cpuidle_enter_t cpuidle_enter_ops;
62e1689795SRobert Lee 
634f86d3a8SLen Brown /**
641a022e3fSBoris Ostrovsky  * cpuidle_play_dead - cpu off-lining
651a022e3fSBoris Ostrovsky  *
66ee01e663SToshi Kani  * Returns in case of an error or no driver
671a022e3fSBoris Ostrovsky  */
681a022e3fSBoris Ostrovsky int cpuidle_play_dead(void)
691a022e3fSBoris Ostrovsky {
701a022e3fSBoris Ostrovsky 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
711a022e3fSBoris Ostrovsky 	struct cpuidle_driver *drv = cpuidle_get_driver();
721a022e3fSBoris Ostrovsky 	int i, dead_state = -1;
731a022e3fSBoris Ostrovsky 	int power_usage = -1;
741a022e3fSBoris Ostrovsky 
75ee01e663SToshi Kani 	if (!drv)
76ee01e663SToshi Kani 		return -ENODEV;
77ee01e663SToshi Kani 
781a022e3fSBoris Ostrovsky 	/* Find lowest-power state that supports long-term idle */
791a022e3fSBoris Ostrovsky 	for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
801a022e3fSBoris Ostrovsky 		struct cpuidle_state *s = &drv->states[i];
811a022e3fSBoris Ostrovsky 
821a022e3fSBoris Ostrovsky 		if (s->power_usage < power_usage && s->enter_dead) {
831a022e3fSBoris Ostrovsky 			power_usage = s->power_usage;
841a022e3fSBoris Ostrovsky 			dead_state = i;
851a022e3fSBoris Ostrovsky 		}
861a022e3fSBoris Ostrovsky 	}
871a022e3fSBoris Ostrovsky 
881a022e3fSBoris Ostrovsky 	if (dead_state != -1)
891a022e3fSBoris Ostrovsky 		return drv->states[dead_state].enter_dead(dev, dead_state);
901a022e3fSBoris Ostrovsky 
911a022e3fSBoris Ostrovsky 	return -ENODEV;
921a022e3fSBoris Ostrovsky }
931a022e3fSBoris Ostrovsky 
941a022e3fSBoris Ostrovsky /**
954f86d3a8SLen Brown  * cpuidle_idle_call - the main idle loop
964f86d3a8SLen Brown  *
974f86d3a8SLen Brown  * NOTE: no locks or semaphores should be used here
98a0bfa137SLen Brown  * return non-zero on failure
994f86d3a8SLen Brown  */
100a0bfa137SLen Brown int cpuidle_idle_call(void)
1014f86d3a8SLen Brown {
1024a6f4fe8SChristoph Lameter 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
10346bcfad7SDeepthi Dharwar 	struct cpuidle_driver *drv = cpuidle_get_driver();
104e978aa7dSDeepthi Dharwar 	int next_state, entered_state;
1054f86d3a8SLen Brown 
106a0bfa137SLen Brown 	if (off)
107a0bfa137SLen Brown 		return -ENODEV;
108a0bfa137SLen Brown 
109a0bfa137SLen Brown 	if (!initialized)
110a0bfa137SLen Brown 		return -ENODEV;
111a0bfa137SLen Brown 
1124f86d3a8SLen Brown 	/* check if the device is ready */
113a0bfa137SLen Brown 	if (!dev || !dev->enabled)
114a0bfa137SLen Brown 		return -EBUSY;
1154f86d3a8SLen Brown 
1169a655837SArjan van de Ven #if 0
1179a655837SArjan van de Ven 	/* shows regressions, re-enable for 2.6.29 */
1182e94d1f7SArjan van de Ven 	/*
1192e94d1f7SArjan van de Ven 	 * run any timers that can be run now, at this point
1202e94d1f7SArjan van de Ven 	 * before calculating the idle duration etc.
1212e94d1f7SArjan van de Ven 	 */
1222e94d1f7SArjan van de Ven 	hrtimer_peek_ahead_timers();
1239a655837SArjan van de Ven #endif
12471abbbf8SAi Li 
1254f86d3a8SLen Brown 	/* ask the governor for the next state */
12646bcfad7SDeepthi Dharwar 	next_state = cpuidle_curr_governor->select(drv, dev);
127246eb7f0SKevin Hilman 	if (need_resched()) {
128246eb7f0SKevin Hilman 		local_irq_enable();
129a0bfa137SLen Brown 		return 0;
130246eb7f0SKevin Hilman 	}
131246eb7f0SKevin Hilman 
13276027ea8SSteven Rostedt 	trace_power_start_rcuidle(POWER_CSTATE, next_state, dev->cpu);
13376027ea8SSteven Rostedt 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
134f77cfe4eSThomas Renninger 
135e1689795SRobert Lee 	entered_state = cpuidle_enter_ops(dev, drv, next_state);
136f77cfe4eSThomas Renninger 
13776027ea8SSteven Rostedt 	trace_power_end_rcuidle(dev->cpu);
13876027ea8SSteven Rostedt 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
139f77cfe4eSThomas Renninger 
140e978aa7dSDeepthi Dharwar 	if (entered_state >= 0) {
141e978aa7dSDeepthi Dharwar 		/* Update cpuidle counters */
142e978aa7dSDeepthi Dharwar 		/* This can be moved to within driver enter routine
143e978aa7dSDeepthi Dharwar 		 * but that results in multiple copies of same code.
144e978aa7dSDeepthi Dharwar 		 */
1454202735eSDeepthi Dharwar 		dev->states_usage[entered_state].time +=
146e978aa7dSDeepthi Dharwar 				(unsigned long long)dev->last_residency;
1474202735eSDeepthi Dharwar 		dev->states_usage[entered_state].usage++;
148e1689795SRobert Lee 	} else {
149e1689795SRobert Lee 		dev->last_residency = 0;
150e978aa7dSDeepthi Dharwar 	}
1514f86d3a8SLen Brown 
1524f86d3a8SLen Brown 	/* give the governor an opportunity to reflect on the outcome */
1534f86d3a8SLen Brown 	if (cpuidle_curr_governor->reflect)
154e978aa7dSDeepthi Dharwar 		cpuidle_curr_governor->reflect(dev, entered_state);
155a0bfa137SLen Brown 
156a0bfa137SLen Brown 	return 0;
1574f86d3a8SLen Brown }
1584f86d3a8SLen Brown 
1594f86d3a8SLen Brown /**
1604f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
1614f86d3a8SLen Brown  */
1624f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
1634f86d3a8SLen Brown {
164a0bfa137SLen Brown 	if (enabled_devices) {
1654f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
1664f86d3a8SLen Brown 		smp_wmb();
167a0bfa137SLen Brown 		initialized = 1;
1684f86d3a8SLen Brown 	}
1694f86d3a8SLen Brown }
1704f86d3a8SLen Brown 
1714f86d3a8SLen Brown /**
1724f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
1734f86d3a8SLen Brown  */
1744f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
1754f86d3a8SLen Brown {
176a0bfa137SLen Brown 	if (enabled_devices) {
177a0bfa137SLen Brown 		initialized = 0;
1784a162513SThomas Gleixner 		kick_all_cpus_sync();
1794f86d3a8SLen Brown 	}
1804f86d3a8SLen Brown }
1814f86d3a8SLen Brown 
1824f86d3a8SLen Brown /**
1834f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
1844f86d3a8SLen Brown  */
1854f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
1864f86d3a8SLen Brown {
1874f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
1884f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
1894f86d3a8SLen Brown }
1904f86d3a8SLen Brown 
1914f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
1924f86d3a8SLen Brown 
1934f86d3a8SLen Brown /**
1944f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
1954f86d3a8SLen Brown  */
1964f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
1974f86d3a8SLen Brown {
1984f86d3a8SLen Brown 	cpuidle_install_idle_handler();
1994f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
2004f86d3a8SLen Brown }
2014f86d3a8SLen Brown 
2024f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
2034f86d3a8SLen Brown 
204*8651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
205*8651f97bSPreeti U Murthy void cpuidle_pause(void)
206*8651f97bSPreeti U Murthy {
207*8651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
208*8651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
209*8651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
210*8651f97bSPreeti U Murthy }
211*8651f97bSPreeti U Murthy 
212*8651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
213*8651f97bSPreeti U Murthy void cpuidle_resume(void)
214*8651f97bSPreeti U Murthy {
215*8651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
216*8651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
217*8651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
218*8651f97bSPreeti U Murthy }
219*8651f97bSPreeti U Murthy 
220e1689795SRobert Lee /**
221e1689795SRobert Lee  * cpuidle_wrap_enter - performs timekeeping and irqen around enter function
222e1689795SRobert Lee  * @dev: pointer to a valid cpuidle_device object
223e1689795SRobert Lee  * @drv: pointer to a valid cpuidle_driver object
224e1689795SRobert Lee  * @index: index of the target cpuidle state.
225e1689795SRobert Lee  */
226e1689795SRobert Lee int cpuidle_wrap_enter(struct cpuidle_device *dev,
227e1689795SRobert Lee 				struct cpuidle_driver *drv, int index,
228e1689795SRobert Lee 				int (*enter)(struct cpuidle_device *dev,
229e1689795SRobert Lee 					struct cpuidle_driver *drv, int index))
230e1689795SRobert Lee {
231e1689795SRobert Lee 	ktime_t time_start, time_end;
232e1689795SRobert Lee 	s64 diff;
233e1689795SRobert Lee 
234e1689795SRobert Lee 	time_start = ktime_get();
235e1689795SRobert Lee 
236e1689795SRobert Lee 	index = enter(dev, drv, index);
237e1689795SRobert Lee 
238e1689795SRobert Lee 	time_end = ktime_get();
239e1689795SRobert Lee 
240e1689795SRobert Lee 	local_irq_enable();
241e1689795SRobert Lee 
242e1689795SRobert Lee 	diff = ktime_to_us(ktime_sub(time_end, time_start));
243e1689795SRobert Lee 	if (diff > INT_MAX)
244e1689795SRobert Lee 		diff = INT_MAX;
245e1689795SRobert Lee 
246e1689795SRobert Lee 	dev->last_residency = (int) diff;
247e1689795SRobert Lee 
248e1689795SRobert Lee 	return index;
249e1689795SRobert Lee }
250e1689795SRobert Lee 
251d8c216cfSRafael J. Wysocki #ifdef CONFIG_ARCH_HAS_CPU_RELAX
25246bcfad7SDeepthi Dharwar static int poll_idle(struct cpuidle_device *dev,
25346bcfad7SDeepthi Dharwar 		struct cpuidle_driver *drv, int index)
254d8c216cfSRafael J. Wysocki {
255d8c216cfSRafael J. Wysocki 	ktime_t	t1, t2;
256d8c216cfSRafael J. Wysocki 	s64 diff;
257d8c216cfSRafael J. Wysocki 
258d8c216cfSRafael J. Wysocki 	t1 = ktime_get();
259d8c216cfSRafael J. Wysocki 	local_irq_enable();
260d8c216cfSRafael J. Wysocki 	while (!need_resched())
261d8c216cfSRafael J. Wysocki 		cpu_relax();
262d8c216cfSRafael J. Wysocki 
263d8c216cfSRafael J. Wysocki 	t2 = ktime_get();
264d8c216cfSRafael J. Wysocki 	diff = ktime_to_us(ktime_sub(t2, t1));
265d8c216cfSRafael J. Wysocki 	if (diff > INT_MAX)
266d8c216cfSRafael J. Wysocki 		diff = INT_MAX;
267d8c216cfSRafael J. Wysocki 
268e978aa7dSDeepthi Dharwar 	dev->last_residency = (int) diff;
269e978aa7dSDeepthi Dharwar 
270e978aa7dSDeepthi Dharwar 	return index;
271d8c216cfSRafael J. Wysocki }
272d8c216cfSRafael J. Wysocki 
27346bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv)
274d8c216cfSRafael J. Wysocki {
27546bcfad7SDeepthi Dharwar 	struct cpuidle_state *state = &drv->states[0];
276d8c216cfSRafael J. Wysocki 
277720f1c30SThomas Renninger 	snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
278d8c216cfSRafael J. Wysocki 	snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
279d8c216cfSRafael J. Wysocki 	state->exit_latency = 0;
280d8c216cfSRafael J. Wysocki 	state->target_residency = 0;
281d8c216cfSRafael J. Wysocki 	state->power_usage = -1;
282d247632cSLen Brown 	state->flags = 0;
283d8c216cfSRafael J. Wysocki 	state->enter = poll_idle;
284d8c216cfSRafael J. Wysocki }
285d8c216cfSRafael J. Wysocki #else
28646bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) {}
287d8c216cfSRafael J. Wysocki #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
288d8c216cfSRafael J. Wysocki 
2894f86d3a8SLen Brown /**
2904f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
2914f86d3a8SLen Brown  * @dev: the CPU
2924f86d3a8SLen Brown  *
2934f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
2944f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
2954f86d3a8SLen Brown  */
2964f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
2974f86d3a8SLen Brown {
2984f86d3a8SLen Brown 	int ret, i;
299e1689795SRobert Lee 	struct cpuidle_driver *drv = cpuidle_get_driver();
3004f86d3a8SLen Brown 
3014f86d3a8SLen Brown 	if (dev->enabled)
3024f86d3a8SLen Brown 		return 0;
303e1689795SRobert Lee 	if (!drv || !cpuidle_curr_governor)
3044f86d3a8SLen Brown 		return -EIO;
3054f86d3a8SLen Brown 	if (!dev->state_count)
306fc850f39SDaniel Lezcano 		dev->state_count = drv->state_count;
3074f86d3a8SLen Brown 
308dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0) {
309dcb84f33SVenkatesh Pallipadi 		ret = __cpuidle_register_device(dev);
310dcb84f33SVenkatesh Pallipadi 		if (ret)
311dcb84f33SVenkatesh Pallipadi 			return ret;
312dcb84f33SVenkatesh Pallipadi 	}
313dcb84f33SVenkatesh Pallipadi 
314e1689795SRobert Lee 	cpuidle_enter_ops = drv->en_core_tk_irqen ?
315e1689795SRobert Lee 		cpuidle_enter_tk : cpuidle_enter;
316e1689795SRobert Lee 
317e1689795SRobert Lee 	poll_idle_init(drv);
318d8c216cfSRafael J. Wysocki 
3194f86d3a8SLen Brown 	if ((ret = cpuidle_add_state_sysfs(dev)))
3204f86d3a8SLen Brown 		return ret;
3214f86d3a8SLen Brown 
3224f86d3a8SLen Brown 	if (cpuidle_curr_governor->enable &&
323e1689795SRobert Lee 	    (ret = cpuidle_curr_governor->enable(drv, dev)))
3244f86d3a8SLen Brown 		goto fail_sysfs;
3254f86d3a8SLen Brown 
3264f86d3a8SLen Brown 	for (i = 0; i < dev->state_count; i++) {
3274202735eSDeepthi Dharwar 		dev->states_usage[i].usage = 0;
3284202735eSDeepthi Dharwar 		dev->states_usage[i].time = 0;
3294f86d3a8SLen Brown 	}
3304f86d3a8SLen Brown 	dev->last_residency = 0;
3314f86d3a8SLen Brown 
3324f86d3a8SLen Brown 	smp_wmb();
3334f86d3a8SLen Brown 
3344f86d3a8SLen Brown 	dev->enabled = 1;
3354f86d3a8SLen Brown 
3364f86d3a8SLen Brown 	enabled_devices++;
3374f86d3a8SLen Brown 	return 0;
3384f86d3a8SLen Brown 
3394f86d3a8SLen Brown fail_sysfs:
3404f86d3a8SLen Brown 	cpuidle_remove_state_sysfs(dev);
3414f86d3a8SLen Brown 
3424f86d3a8SLen Brown 	return ret;
3434f86d3a8SLen Brown }
3444f86d3a8SLen Brown 
3454f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
3464f86d3a8SLen Brown 
3474f86d3a8SLen Brown /**
3484f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
3494f86d3a8SLen Brown  * @dev: the CPU
3504f86d3a8SLen Brown  *
3514f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
3524f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
3534f86d3a8SLen Brown  */
3544f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
3554f86d3a8SLen Brown {
3564f86d3a8SLen Brown 	if (!dev->enabled)
3574f86d3a8SLen Brown 		return;
358752138dfSLen Brown 	if (!cpuidle_get_driver() || !cpuidle_curr_governor)
3594f86d3a8SLen Brown 		return;
3604f86d3a8SLen Brown 
3614f86d3a8SLen Brown 	dev->enabled = 0;
3624f86d3a8SLen Brown 
3634f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
36446bcfad7SDeepthi Dharwar 		cpuidle_curr_governor->disable(cpuidle_get_driver(), dev);
3654f86d3a8SLen Brown 
3664f86d3a8SLen Brown 	cpuidle_remove_state_sysfs(dev);
3674f86d3a8SLen Brown 	enabled_devices--;
3684f86d3a8SLen Brown }
3694f86d3a8SLen Brown 
3704f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
3714f86d3a8SLen Brown 
3724f86d3a8SLen Brown /**
373dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
374dcb84f33SVenkatesh Pallipadi  * and enable routines
3754f86d3a8SLen Brown  * @dev: the cpu
376dcb84f33SVenkatesh Pallipadi  *
377dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
3784f86d3a8SLen Brown  */
379dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
3804f86d3a8SLen Brown {
3814f86d3a8SLen Brown 	int ret;
3828a25a2fdSKay Sievers 	struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
383752138dfSLen Brown 	struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
3844f86d3a8SLen Brown 
3858a25a2fdSKay Sievers 	if (!dev)
3864f86d3a8SLen Brown 		return -EINVAL;
387752138dfSLen Brown 	if (!try_module_get(cpuidle_driver->owner))
3884f86d3a8SLen Brown 		return -EINVAL;
3894f86d3a8SLen Brown 
3904f86d3a8SLen Brown 	init_completion(&dev->kobj_unregister);
3914f86d3a8SLen Brown 
3924f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
3934f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
3948a25a2fdSKay Sievers 	if ((ret = cpuidle_add_sysfs(cpu_dev))) {
395752138dfSLen Brown 		module_put(cpuidle_driver->owner);
3964f86d3a8SLen Brown 		return ret;
3974f86d3a8SLen Brown 	}
3984f86d3a8SLen Brown 
399dcb84f33SVenkatesh Pallipadi 	dev->registered = 1;
400dcb84f33SVenkatesh Pallipadi 	return 0;
401dcb84f33SVenkatesh Pallipadi }
402dcb84f33SVenkatesh Pallipadi 
403dcb84f33SVenkatesh Pallipadi /**
404dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
405dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
406dcb84f33SVenkatesh Pallipadi  */
407dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
408dcb84f33SVenkatesh Pallipadi {
409dcb84f33SVenkatesh Pallipadi 	int ret;
410dcb84f33SVenkatesh Pallipadi 
411dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
412dcb84f33SVenkatesh Pallipadi 
413dcb84f33SVenkatesh Pallipadi 	if ((ret = __cpuidle_register_device(dev))) {
414dcb84f33SVenkatesh Pallipadi 		mutex_unlock(&cpuidle_lock);
415dcb84f33SVenkatesh Pallipadi 		return ret;
416dcb84f33SVenkatesh Pallipadi 	}
417dcb84f33SVenkatesh Pallipadi 
4184f86d3a8SLen Brown 	cpuidle_enable_device(dev);
4194f86d3a8SLen Brown 	cpuidle_install_idle_handler();
4204f86d3a8SLen Brown 
4214f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
4224f86d3a8SLen Brown 
4234f86d3a8SLen Brown 	return 0;
4244f86d3a8SLen Brown 
4254f86d3a8SLen Brown }
4264f86d3a8SLen Brown 
4274f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
4284f86d3a8SLen Brown 
4294f86d3a8SLen Brown /**
4304f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
4314f86d3a8SLen Brown  * @dev: the cpu
4324f86d3a8SLen Brown  */
4334f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
4344f86d3a8SLen Brown {
4358a25a2fdSKay Sievers 	struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
436752138dfSLen Brown 	struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
4374f86d3a8SLen Brown 
438dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0)
439dcb84f33SVenkatesh Pallipadi 		return;
440dcb84f33SVenkatesh Pallipadi 
4414f86d3a8SLen Brown 	cpuidle_pause_and_lock();
4424f86d3a8SLen Brown 
4434f86d3a8SLen Brown 	cpuidle_disable_device(dev);
4444f86d3a8SLen Brown 
4458a25a2fdSKay Sievers 	cpuidle_remove_sysfs(cpu_dev);
4464f86d3a8SLen Brown 	list_del(&dev->device_list);
4474f86d3a8SLen Brown 	wait_for_completion(&dev->kobj_unregister);
4484f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
4494f86d3a8SLen Brown 
4504f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
4514f86d3a8SLen Brown 
452752138dfSLen Brown 	module_put(cpuidle_driver->owner);
4534f86d3a8SLen Brown }
4544f86d3a8SLen Brown 
4554f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
4564f86d3a8SLen Brown 
4574f86d3a8SLen Brown #ifdef CONFIG_SMP
4584f86d3a8SLen Brown 
4594f86d3a8SLen Brown static void smp_callback(void *v)
4604f86d3a8SLen Brown {
4614f86d3a8SLen Brown 	/* we already woke the CPU up, nothing more to do */
4624f86d3a8SLen Brown }
4634f86d3a8SLen Brown 
4644f86d3a8SLen Brown /*
4654f86d3a8SLen Brown  * This function gets called when a part of the kernel has a new latency
4664f86d3a8SLen Brown  * requirement.  This means we need to get all processors out of their C-state,
4674f86d3a8SLen Brown  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
4684f86d3a8SLen Brown  * wakes them all right up.
4694f86d3a8SLen Brown  */
4704f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b,
4714f86d3a8SLen Brown 		unsigned long l, void *v)
4724f86d3a8SLen Brown {
4738691e5a8SJens Axboe 	smp_call_function(smp_callback, NULL, 1);
4744f86d3a8SLen Brown 	return NOTIFY_OK;
4754f86d3a8SLen Brown }
4764f86d3a8SLen Brown 
4774f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = {
4784f86d3a8SLen Brown 	.notifier_call = cpuidle_latency_notify,
4794f86d3a8SLen Brown };
4804f86d3a8SLen Brown 
481d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n)
482d82b3518SMark Gross {
483d82b3518SMark Gross 	pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
484d82b3518SMark Gross }
4854f86d3a8SLen Brown 
4864f86d3a8SLen Brown #else /* CONFIG_SMP */
4874f86d3a8SLen Brown 
4884f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0)
4894f86d3a8SLen Brown 
4904f86d3a8SLen Brown #endif /* CONFIG_SMP */
4914f86d3a8SLen Brown 
4924f86d3a8SLen Brown /**
4934f86d3a8SLen Brown  * cpuidle_init - core initializer
4944f86d3a8SLen Brown  */
4954f86d3a8SLen Brown static int __init cpuidle_init(void)
4964f86d3a8SLen Brown {
4974f86d3a8SLen Brown 	int ret;
4984f86d3a8SLen Brown 
49962027aeaSLen Brown 	if (cpuidle_disabled())
50062027aeaSLen Brown 		return -ENODEV;
50162027aeaSLen Brown 
5028a25a2fdSKay Sievers 	ret = cpuidle_add_interface(cpu_subsys.dev_root);
5034f86d3a8SLen Brown 	if (ret)
5044f86d3a8SLen Brown 		return ret;
5054f86d3a8SLen Brown 
5064f86d3a8SLen Brown 	latency_notifier_init(&cpuidle_latency_notifier);
5074f86d3a8SLen Brown 
5084f86d3a8SLen Brown 	return 0;
5094f86d3a8SLen Brown }
5104f86d3a8SLen Brown 
51162027aeaSLen Brown module_param(off, int, 0444);
5124f86d3a8SLen Brown core_initcall(cpuidle_init);
513