xref: /linux/drivers/cpuidle/cpuidle.c (revision cbc9ef0287ab764d3da0129efa673808df641fe3)
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 
204e1689795SRobert Lee /**
205e1689795SRobert Lee  * cpuidle_wrap_enter - performs timekeeping and irqen around enter function
206e1689795SRobert Lee  * @dev: pointer to a valid cpuidle_device object
207e1689795SRobert Lee  * @drv: pointer to a valid cpuidle_driver object
208e1689795SRobert Lee  * @index: index of the target cpuidle state.
209e1689795SRobert Lee  */
210e1689795SRobert Lee int cpuidle_wrap_enter(struct cpuidle_device *dev,
211e1689795SRobert Lee 				struct cpuidle_driver *drv, int index,
212e1689795SRobert Lee 				int (*enter)(struct cpuidle_device *dev,
213e1689795SRobert Lee 					struct cpuidle_driver *drv, int index))
214e1689795SRobert Lee {
215e1689795SRobert Lee 	ktime_t time_start, time_end;
216e1689795SRobert Lee 	s64 diff;
217e1689795SRobert Lee 
218e1689795SRobert Lee 	time_start = ktime_get();
219e1689795SRobert Lee 
220e1689795SRobert Lee 	index = enter(dev, drv, index);
221e1689795SRobert Lee 
222e1689795SRobert Lee 	time_end = ktime_get();
223e1689795SRobert Lee 
224e1689795SRobert Lee 	local_irq_enable();
225e1689795SRobert Lee 
226e1689795SRobert Lee 	diff = ktime_to_us(ktime_sub(time_end, time_start));
227e1689795SRobert Lee 	if (diff > INT_MAX)
228e1689795SRobert Lee 		diff = INT_MAX;
229e1689795SRobert Lee 
230e1689795SRobert Lee 	dev->last_residency = (int) diff;
231e1689795SRobert Lee 
232e1689795SRobert Lee 	return index;
233e1689795SRobert Lee }
234e1689795SRobert Lee 
235d8c216cfSRafael J. Wysocki #ifdef CONFIG_ARCH_HAS_CPU_RELAX
23646bcfad7SDeepthi Dharwar static int poll_idle(struct cpuidle_device *dev,
23746bcfad7SDeepthi Dharwar 		struct cpuidle_driver *drv, int index)
238d8c216cfSRafael J. Wysocki {
239d8c216cfSRafael J. Wysocki 	ktime_t	t1, t2;
240d8c216cfSRafael J. Wysocki 	s64 diff;
241d8c216cfSRafael J. Wysocki 
242d8c216cfSRafael J. Wysocki 	t1 = ktime_get();
243d8c216cfSRafael J. Wysocki 	local_irq_enable();
244d8c216cfSRafael J. Wysocki 	while (!need_resched())
245d8c216cfSRafael J. Wysocki 		cpu_relax();
246d8c216cfSRafael J. Wysocki 
247d8c216cfSRafael J. Wysocki 	t2 = ktime_get();
248d8c216cfSRafael J. Wysocki 	diff = ktime_to_us(ktime_sub(t2, t1));
249d8c216cfSRafael J. Wysocki 	if (diff > INT_MAX)
250d8c216cfSRafael J. Wysocki 		diff = INT_MAX;
251d8c216cfSRafael J. Wysocki 
252e978aa7dSDeepthi Dharwar 	dev->last_residency = (int) diff;
253e978aa7dSDeepthi Dharwar 
254e978aa7dSDeepthi Dharwar 	return index;
255d8c216cfSRafael J. Wysocki }
256d8c216cfSRafael J. Wysocki 
25746bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv)
258d8c216cfSRafael J. Wysocki {
25946bcfad7SDeepthi Dharwar 	struct cpuidle_state *state = &drv->states[0];
260d8c216cfSRafael J. Wysocki 
261720f1c30SThomas Renninger 	snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
262d8c216cfSRafael J. Wysocki 	snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
263d8c216cfSRafael J. Wysocki 	state->exit_latency = 0;
264d8c216cfSRafael J. Wysocki 	state->target_residency = 0;
265d8c216cfSRafael J. Wysocki 	state->power_usage = -1;
266d247632cSLen Brown 	state->flags = 0;
267d8c216cfSRafael J. Wysocki 	state->enter = poll_idle;
268*cbc9ef02SRafael J. Wysocki 	state->disabled = false;
269d8c216cfSRafael J. Wysocki }
270d8c216cfSRafael J. Wysocki #else
27146bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) {}
272d8c216cfSRafael J. Wysocki #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
273d8c216cfSRafael J. Wysocki 
2744f86d3a8SLen Brown /**
2754f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
2764f86d3a8SLen Brown  * @dev: the CPU
2774f86d3a8SLen Brown  *
2784f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
2794f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
2804f86d3a8SLen Brown  */
2814f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
2824f86d3a8SLen Brown {
2834f86d3a8SLen Brown 	int ret, i;
284e1689795SRobert Lee 	struct cpuidle_driver *drv = cpuidle_get_driver();
2854f86d3a8SLen Brown 
2864f86d3a8SLen Brown 	if (dev->enabled)
2874f86d3a8SLen Brown 		return 0;
288e1689795SRobert Lee 	if (!drv || !cpuidle_curr_governor)
2894f86d3a8SLen Brown 		return -EIO;
2904f86d3a8SLen Brown 	if (!dev->state_count)
291fc850f39SDaniel Lezcano 		dev->state_count = drv->state_count;
2924f86d3a8SLen Brown 
293dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0) {
294dcb84f33SVenkatesh Pallipadi 		ret = __cpuidle_register_device(dev);
295dcb84f33SVenkatesh Pallipadi 		if (ret)
296dcb84f33SVenkatesh Pallipadi 			return ret;
297dcb84f33SVenkatesh Pallipadi 	}
298dcb84f33SVenkatesh Pallipadi 
299e1689795SRobert Lee 	cpuidle_enter_ops = drv->en_core_tk_irqen ?
300e1689795SRobert Lee 		cpuidle_enter_tk : cpuidle_enter;
301e1689795SRobert Lee 
302e1689795SRobert Lee 	poll_idle_init(drv);
303d8c216cfSRafael J. Wysocki 
3044f86d3a8SLen Brown 	if ((ret = cpuidle_add_state_sysfs(dev)))
3054f86d3a8SLen Brown 		return ret;
3064f86d3a8SLen Brown 
3074f86d3a8SLen Brown 	if (cpuidle_curr_governor->enable &&
308e1689795SRobert Lee 	    (ret = cpuidle_curr_governor->enable(drv, dev)))
3094f86d3a8SLen Brown 		goto fail_sysfs;
3104f86d3a8SLen Brown 
3114f86d3a8SLen Brown 	for (i = 0; i < dev->state_count; i++) {
3124202735eSDeepthi Dharwar 		dev->states_usage[i].usage = 0;
3134202735eSDeepthi Dharwar 		dev->states_usage[i].time = 0;
3144f86d3a8SLen Brown 	}
3154f86d3a8SLen Brown 	dev->last_residency = 0;
3164f86d3a8SLen Brown 
3174f86d3a8SLen Brown 	smp_wmb();
3184f86d3a8SLen Brown 
3194f86d3a8SLen Brown 	dev->enabled = 1;
3204f86d3a8SLen Brown 
3214f86d3a8SLen Brown 	enabled_devices++;
3224f86d3a8SLen Brown 	return 0;
3234f86d3a8SLen Brown 
3244f86d3a8SLen Brown fail_sysfs:
3254f86d3a8SLen Brown 	cpuidle_remove_state_sysfs(dev);
3264f86d3a8SLen Brown 
3274f86d3a8SLen Brown 	return ret;
3284f86d3a8SLen Brown }
3294f86d3a8SLen Brown 
3304f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
3314f86d3a8SLen Brown 
3324f86d3a8SLen Brown /**
3334f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
3344f86d3a8SLen Brown  * @dev: the CPU
3354f86d3a8SLen Brown  *
3364f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
3374f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
3384f86d3a8SLen Brown  */
3394f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
3404f86d3a8SLen Brown {
3414f86d3a8SLen Brown 	if (!dev->enabled)
3424f86d3a8SLen Brown 		return;
343752138dfSLen Brown 	if (!cpuidle_get_driver() || !cpuidle_curr_governor)
3444f86d3a8SLen Brown 		return;
3454f86d3a8SLen Brown 
3464f86d3a8SLen Brown 	dev->enabled = 0;
3474f86d3a8SLen Brown 
3484f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
34946bcfad7SDeepthi Dharwar 		cpuidle_curr_governor->disable(cpuidle_get_driver(), dev);
3504f86d3a8SLen Brown 
3514f86d3a8SLen Brown 	cpuidle_remove_state_sysfs(dev);
3524f86d3a8SLen Brown 	enabled_devices--;
3534f86d3a8SLen Brown }
3544f86d3a8SLen Brown 
3554f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
3564f86d3a8SLen Brown 
3574f86d3a8SLen Brown /**
358dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
359dcb84f33SVenkatesh Pallipadi  * and enable routines
3604f86d3a8SLen Brown  * @dev: the cpu
361dcb84f33SVenkatesh Pallipadi  *
362dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
3634f86d3a8SLen Brown  */
364dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
3654f86d3a8SLen Brown {
3664f86d3a8SLen Brown 	int ret;
3678a25a2fdSKay Sievers 	struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
368752138dfSLen Brown 	struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
3694f86d3a8SLen Brown 
3708a25a2fdSKay Sievers 	if (!dev)
3714f86d3a8SLen Brown 		return -EINVAL;
372752138dfSLen Brown 	if (!try_module_get(cpuidle_driver->owner))
3734f86d3a8SLen Brown 		return -EINVAL;
3744f86d3a8SLen Brown 
3754f86d3a8SLen Brown 	init_completion(&dev->kobj_unregister);
3764f86d3a8SLen Brown 
3774f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
3784f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
3798a25a2fdSKay Sievers 	if ((ret = cpuidle_add_sysfs(cpu_dev))) {
380752138dfSLen Brown 		module_put(cpuidle_driver->owner);
3814f86d3a8SLen Brown 		return ret;
3824f86d3a8SLen Brown 	}
3834f86d3a8SLen Brown 
384dcb84f33SVenkatesh Pallipadi 	dev->registered = 1;
385dcb84f33SVenkatesh Pallipadi 	return 0;
386dcb84f33SVenkatesh Pallipadi }
387dcb84f33SVenkatesh Pallipadi 
388dcb84f33SVenkatesh Pallipadi /**
389dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
390dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
391dcb84f33SVenkatesh Pallipadi  */
392dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
393dcb84f33SVenkatesh Pallipadi {
394dcb84f33SVenkatesh Pallipadi 	int ret;
395dcb84f33SVenkatesh Pallipadi 
396dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
397dcb84f33SVenkatesh Pallipadi 
398dcb84f33SVenkatesh Pallipadi 	if ((ret = __cpuidle_register_device(dev))) {
399dcb84f33SVenkatesh Pallipadi 		mutex_unlock(&cpuidle_lock);
400dcb84f33SVenkatesh Pallipadi 		return ret;
401dcb84f33SVenkatesh Pallipadi 	}
402dcb84f33SVenkatesh Pallipadi 
4034f86d3a8SLen Brown 	cpuidle_enable_device(dev);
4044f86d3a8SLen Brown 	cpuidle_install_idle_handler();
4054f86d3a8SLen Brown 
4064f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
4074f86d3a8SLen Brown 
4084f86d3a8SLen Brown 	return 0;
4094f86d3a8SLen Brown 
4104f86d3a8SLen Brown }
4114f86d3a8SLen Brown 
4124f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
4134f86d3a8SLen Brown 
4144f86d3a8SLen Brown /**
4154f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
4164f86d3a8SLen Brown  * @dev: the cpu
4174f86d3a8SLen Brown  */
4184f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
4194f86d3a8SLen Brown {
4208a25a2fdSKay Sievers 	struct device *cpu_dev = get_cpu_device((unsigned long)dev->cpu);
421752138dfSLen Brown 	struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
4224f86d3a8SLen Brown 
423dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0)
424dcb84f33SVenkatesh Pallipadi 		return;
425dcb84f33SVenkatesh Pallipadi 
4264f86d3a8SLen Brown 	cpuidle_pause_and_lock();
4274f86d3a8SLen Brown 
4284f86d3a8SLen Brown 	cpuidle_disable_device(dev);
4294f86d3a8SLen Brown 
4308a25a2fdSKay Sievers 	cpuidle_remove_sysfs(cpu_dev);
4314f86d3a8SLen Brown 	list_del(&dev->device_list);
4324f86d3a8SLen Brown 	wait_for_completion(&dev->kobj_unregister);
4334f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
4344f86d3a8SLen Brown 
4354f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
4364f86d3a8SLen Brown 
437752138dfSLen Brown 	module_put(cpuidle_driver->owner);
4384f86d3a8SLen Brown }
4394f86d3a8SLen Brown 
4404f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
4414f86d3a8SLen Brown 
4424f86d3a8SLen Brown #ifdef CONFIG_SMP
4434f86d3a8SLen Brown 
4444f86d3a8SLen Brown static void smp_callback(void *v)
4454f86d3a8SLen Brown {
4464f86d3a8SLen Brown 	/* we already woke the CPU up, nothing more to do */
4474f86d3a8SLen Brown }
4484f86d3a8SLen Brown 
4494f86d3a8SLen Brown /*
4504f86d3a8SLen Brown  * This function gets called when a part of the kernel has a new latency
4514f86d3a8SLen Brown  * requirement.  This means we need to get all processors out of their C-state,
4524f86d3a8SLen Brown  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
4534f86d3a8SLen Brown  * wakes them all right up.
4544f86d3a8SLen Brown  */
4554f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b,
4564f86d3a8SLen Brown 		unsigned long l, void *v)
4574f86d3a8SLen Brown {
4588691e5a8SJens Axboe 	smp_call_function(smp_callback, NULL, 1);
4594f86d3a8SLen Brown 	return NOTIFY_OK;
4604f86d3a8SLen Brown }
4614f86d3a8SLen Brown 
4624f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = {
4634f86d3a8SLen Brown 	.notifier_call = cpuidle_latency_notify,
4644f86d3a8SLen Brown };
4654f86d3a8SLen Brown 
466d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n)
467d82b3518SMark Gross {
468d82b3518SMark Gross 	pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
469d82b3518SMark Gross }
4704f86d3a8SLen Brown 
4714f86d3a8SLen Brown #else /* CONFIG_SMP */
4724f86d3a8SLen Brown 
4734f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0)
4744f86d3a8SLen Brown 
4754f86d3a8SLen Brown #endif /* CONFIG_SMP */
4764f86d3a8SLen Brown 
4774f86d3a8SLen Brown /**
4784f86d3a8SLen Brown  * cpuidle_init - core initializer
4794f86d3a8SLen Brown  */
4804f86d3a8SLen Brown static int __init cpuidle_init(void)
4814f86d3a8SLen Brown {
4824f86d3a8SLen Brown 	int ret;
4834f86d3a8SLen Brown 
48462027aeaSLen Brown 	if (cpuidle_disabled())
48562027aeaSLen Brown 		return -ENODEV;
48662027aeaSLen Brown 
4878a25a2fdSKay Sievers 	ret = cpuidle_add_interface(cpu_subsys.dev_root);
4884f86d3a8SLen Brown 	if (ret)
4894f86d3a8SLen Brown 		return ret;
4904f86d3a8SLen Brown 
4914f86d3a8SLen Brown 	latency_notifier_init(&cpuidle_latency_notifier);
4924f86d3a8SLen Brown 
4934f86d3a8SLen Brown 	return 0;
4944f86d3a8SLen Brown }
4954f86d3a8SLen Brown 
49662027aeaSLen Brown module_param(off, int, 0444);
4974f86d3a8SLen Brown core_initcall(cpuidle_init);
498