xref: /linux/drivers/cpuidle/cpuidle.c (revision 4c637b2175a0dc65d533494225525c6c82d73293)
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);
27*4c637b21SDaniel Lezcano DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
284f86d3a8SLen Brown 
294f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock);
304f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices);
314f86d3a8SLen Brown 
324f86d3a8SLen Brown static int enabled_devices;
3362027aeaSLen Brown static int off __read_mostly;
34a0bfa137SLen Brown static int initialized __read_mostly;
3562027aeaSLen Brown 
3662027aeaSLen Brown int cpuidle_disabled(void)
3762027aeaSLen Brown {
3862027aeaSLen Brown 	return off;
3962027aeaSLen Brown }
40d91ee586SLen Brown void disable_cpuidle(void)
41d91ee586SLen Brown {
42d91ee586SLen Brown 	off = 1;
43d91ee586SLen Brown }
444f86d3a8SLen Brown 
45dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev);
46dcb84f33SVenkatesh Pallipadi 
474f86d3a8SLen Brown /**
481a022e3fSBoris Ostrovsky  * cpuidle_play_dead - cpu off-lining
491a022e3fSBoris Ostrovsky  *
50ee01e663SToshi Kani  * Returns in case of an error or no driver
511a022e3fSBoris Ostrovsky  */
521a022e3fSBoris Ostrovsky int cpuidle_play_dead(void)
531a022e3fSBoris Ostrovsky {
541a022e3fSBoris Ostrovsky 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
55bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
568aef33a7SDaniel Lezcano 	int i;
571a022e3fSBoris Ostrovsky 
58ee01e663SToshi Kani 	if (!drv)
59ee01e663SToshi Kani 		return -ENODEV;
60ee01e663SToshi Kani 
611a022e3fSBoris Ostrovsky 	/* Find lowest-power state that supports long-term idle */
628aef33a7SDaniel Lezcano 	for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
638aef33a7SDaniel Lezcano 		if (drv->states[i].enter_dead)
648aef33a7SDaniel Lezcano 			return drv->states[i].enter_dead(dev, i);
651a022e3fSBoris Ostrovsky 
661a022e3fSBoris Ostrovsky 	return -ENODEV;
671a022e3fSBoris Ostrovsky }
681a022e3fSBoris Ostrovsky 
691a022e3fSBoris Ostrovsky /**
7056cfbf74SColin Cross  * cpuidle_enter_state - enter the state and update stats
7156cfbf74SColin Cross  * @dev: cpuidle device for this cpu
7256cfbf74SColin Cross  * @drv: cpuidle driver for this cpu
7356cfbf74SColin Cross  * @next_state: index into drv->states of the state to enter
7456cfbf74SColin Cross  */
7556cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
76554c06baSDaniel Lezcano 			int index)
7756cfbf74SColin Cross {
7856cfbf74SColin Cross 	int entered_state;
7956cfbf74SColin Cross 
80554c06baSDaniel Lezcano 	struct cpuidle_state *target_state = &drv->states[index];
81554c06baSDaniel Lezcano 	ktime_t time_start, time_end;
82554c06baSDaniel Lezcano 	s64 diff;
83554c06baSDaniel Lezcano 
84554c06baSDaniel Lezcano 	time_start = ktime_get();
85554c06baSDaniel Lezcano 
86554c06baSDaniel Lezcano 	entered_state = target_state->enter(dev, drv, index);
87554c06baSDaniel Lezcano 
88554c06baSDaniel Lezcano 	time_end = ktime_get();
89554c06baSDaniel Lezcano 
90554c06baSDaniel Lezcano 	local_irq_enable();
91554c06baSDaniel Lezcano 
92554c06baSDaniel Lezcano 	diff = ktime_to_us(ktime_sub(time_end, time_start));
93554c06baSDaniel Lezcano 	if (diff > INT_MAX)
94554c06baSDaniel Lezcano 		diff = INT_MAX;
95554c06baSDaniel Lezcano 
96554c06baSDaniel Lezcano 	dev->last_residency = (int) diff;
9756cfbf74SColin Cross 
9856cfbf74SColin Cross 	if (entered_state >= 0) {
9956cfbf74SColin Cross 		/* Update cpuidle counters */
10056cfbf74SColin Cross 		/* This can be moved to within driver enter routine
10156cfbf74SColin Cross 		 * but that results in multiple copies of same code.
10256cfbf74SColin Cross 		 */
103a474a515SJulius Werner 		dev->states_usage[entered_state].time += dev->last_residency;
10456cfbf74SColin Cross 		dev->states_usage[entered_state].usage++;
10556cfbf74SColin Cross 	} else {
10656cfbf74SColin Cross 		dev->last_residency = 0;
10756cfbf74SColin Cross 	}
10856cfbf74SColin Cross 
10956cfbf74SColin Cross 	return entered_state;
11056cfbf74SColin Cross }
11156cfbf74SColin Cross 
11256cfbf74SColin Cross /**
1134f86d3a8SLen Brown  * cpuidle_idle_call - the main idle loop
1144f86d3a8SLen Brown  *
1154f86d3a8SLen Brown  * NOTE: no locks or semaphores should be used here
116a0bfa137SLen Brown  * return non-zero on failure
1174f86d3a8SLen Brown  */
118a0bfa137SLen Brown int cpuidle_idle_call(void)
1194f86d3a8SLen Brown {
1204a6f4fe8SChristoph Lameter 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
121bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
122e978aa7dSDeepthi Dharwar 	int next_state, entered_state;
1234f86d3a8SLen Brown 
124a0bfa137SLen Brown 	if (off)
125a0bfa137SLen Brown 		return -ENODEV;
126a0bfa137SLen Brown 
127a0bfa137SLen Brown 	if (!initialized)
128a0bfa137SLen Brown 		return -ENODEV;
129a0bfa137SLen Brown 
1304f86d3a8SLen Brown 	/* check if the device is ready */
131a0bfa137SLen Brown 	if (!dev || !dev->enabled)
132a0bfa137SLen Brown 		return -EBUSY;
1334f86d3a8SLen Brown 
134bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
135bf4d1b5dSDaniel Lezcano 
1364f86d3a8SLen Brown 	/* ask the governor for the next state */
13746bcfad7SDeepthi Dharwar 	next_state = cpuidle_curr_governor->select(drv, dev);
138246eb7f0SKevin Hilman 	if (need_resched()) {
139d73d68dcSYouquan Song 		dev->last_residency = 0;
140d73d68dcSYouquan Song 		/* give the governor an opportunity to reflect on the outcome */
141d73d68dcSYouquan Song 		if (cpuidle_curr_governor->reflect)
142d73d68dcSYouquan Song 			cpuidle_curr_governor->reflect(dev, next_state);
143246eb7f0SKevin Hilman 		local_irq_enable();
144a0bfa137SLen Brown 		return 0;
145246eb7f0SKevin Hilman 	}
146246eb7f0SKevin Hilman 
14776027ea8SSteven Rostedt 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
148f77cfe4eSThomas Renninger 
149b60e6a0eSDaniel Lezcano 	if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
150b60e6a0eSDaniel Lezcano 		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
151b60e6a0eSDaniel Lezcano 				   &dev->cpu);
152b60e6a0eSDaniel Lezcano 
1534126c019SColin Cross 	if (cpuidle_state_is_coupled(dev, drv, next_state))
1544126c019SColin Cross 		entered_state = cpuidle_enter_state_coupled(dev, drv,
1554126c019SColin Cross 							    next_state);
1564126c019SColin Cross 	else
15756cfbf74SColin Cross 		entered_state = cpuidle_enter_state(dev, drv, next_state);
158f77cfe4eSThomas Renninger 
159b60e6a0eSDaniel Lezcano 	if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
160b60e6a0eSDaniel Lezcano 		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
161b60e6a0eSDaniel Lezcano 				   &dev->cpu);
162b60e6a0eSDaniel Lezcano 
16376027ea8SSteven Rostedt 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
164f77cfe4eSThomas Renninger 
1654f86d3a8SLen Brown 	/* give the governor an opportunity to reflect on the outcome */
1664f86d3a8SLen Brown 	if (cpuidle_curr_governor->reflect)
167e978aa7dSDeepthi Dharwar 		cpuidle_curr_governor->reflect(dev, entered_state);
168a0bfa137SLen Brown 
169a0bfa137SLen Brown 	return 0;
1704f86d3a8SLen Brown }
1714f86d3a8SLen Brown 
1724f86d3a8SLen Brown /**
1734f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
1744f86d3a8SLen Brown  */
1754f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
1764f86d3a8SLen Brown {
177a0bfa137SLen Brown 	if (enabled_devices) {
1784f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
1794f86d3a8SLen Brown 		smp_wmb();
180a0bfa137SLen Brown 		initialized = 1;
1814f86d3a8SLen Brown 	}
1824f86d3a8SLen Brown }
1834f86d3a8SLen Brown 
1844f86d3a8SLen Brown /**
1854f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
1864f86d3a8SLen Brown  */
1874f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
1884f86d3a8SLen Brown {
189a0bfa137SLen Brown 	if (enabled_devices) {
190a0bfa137SLen Brown 		initialized = 0;
1914a162513SThomas Gleixner 		kick_all_cpus_sync();
1924f86d3a8SLen Brown 	}
1934f86d3a8SLen Brown }
1944f86d3a8SLen Brown 
1954f86d3a8SLen Brown /**
1964f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
1974f86d3a8SLen Brown  */
1984f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
1994f86d3a8SLen Brown {
2004f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
2014f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
2024f86d3a8SLen Brown }
2034f86d3a8SLen Brown 
2044f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
2054f86d3a8SLen Brown 
2064f86d3a8SLen Brown /**
2074f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
2084f86d3a8SLen Brown  */
2094f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
2104f86d3a8SLen Brown {
2114f86d3a8SLen Brown 	cpuidle_install_idle_handler();
2124f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
2134f86d3a8SLen Brown }
2144f86d3a8SLen Brown 
2154f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
2164f86d3a8SLen Brown 
2178651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
2188651f97bSPreeti U Murthy void cpuidle_pause(void)
2198651f97bSPreeti U Murthy {
2208651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
2218651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
2228651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
2238651f97bSPreeti U Murthy }
2248651f97bSPreeti U Murthy 
2258651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
2268651f97bSPreeti U Murthy void cpuidle_resume(void)
2278651f97bSPreeti U Murthy {
2288651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
2298651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
2308651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
2318651f97bSPreeti U Murthy }
2328651f97bSPreeti U Murthy 
233d8c216cfSRafael J. Wysocki #ifdef CONFIG_ARCH_HAS_CPU_RELAX
23446bcfad7SDeepthi Dharwar static int poll_idle(struct cpuidle_device *dev,
23546bcfad7SDeepthi Dharwar 		struct cpuidle_driver *drv, int index)
236d8c216cfSRafael J. Wysocki {
237d8c216cfSRafael J. Wysocki 	ktime_t	t1, t2;
238d8c216cfSRafael J. Wysocki 	s64 diff;
239d8c216cfSRafael J. Wysocki 
240d8c216cfSRafael J. Wysocki 	t1 = ktime_get();
241d8c216cfSRafael J. Wysocki 	local_irq_enable();
242d8c216cfSRafael J. Wysocki 	while (!need_resched())
243d8c216cfSRafael J. Wysocki 		cpu_relax();
244d8c216cfSRafael J. Wysocki 
245d8c216cfSRafael J. Wysocki 	t2 = ktime_get();
246d8c216cfSRafael J. Wysocki 	diff = ktime_to_us(ktime_sub(t2, t1));
247d8c216cfSRafael J. Wysocki 	if (diff > INT_MAX)
248d8c216cfSRafael J. Wysocki 		diff = INT_MAX;
249d8c216cfSRafael J. Wysocki 
250e978aa7dSDeepthi Dharwar 	dev->last_residency = (int) diff;
251e978aa7dSDeepthi Dharwar 
252e978aa7dSDeepthi Dharwar 	return index;
253d8c216cfSRafael J. Wysocki }
254d8c216cfSRafael J. Wysocki 
25546bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv)
256d8c216cfSRafael J. Wysocki {
25746bcfad7SDeepthi Dharwar 	struct cpuidle_state *state = &drv->states[0];
258d8c216cfSRafael J. Wysocki 
259720f1c30SThomas Renninger 	snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
260d8c216cfSRafael J. Wysocki 	snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
261d8c216cfSRafael J. Wysocki 	state->exit_latency = 0;
262d8c216cfSRafael J. Wysocki 	state->target_residency = 0;
263d8c216cfSRafael J. Wysocki 	state->power_usage = -1;
264d247632cSLen Brown 	state->flags = 0;
265d8c216cfSRafael J. Wysocki 	state->enter = poll_idle;
266cbc9ef02SRafael J. Wysocki 	state->disabled = false;
267d8c216cfSRafael J. Wysocki }
268d8c216cfSRafael J. Wysocki #else
26946bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) {}
270d8c216cfSRafael J. Wysocki #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
271d8c216cfSRafael J. Wysocki 
2724f86d3a8SLen Brown /**
2734f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
2744f86d3a8SLen Brown  * @dev: the CPU
2754f86d3a8SLen Brown  *
2764f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
2774f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
2784f86d3a8SLen Brown  */
2794f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
2804f86d3a8SLen Brown {
2814f86d3a8SLen Brown 	int ret, i;
282bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
2834f86d3a8SLen Brown 
2841b0a0e9aSSrivatsa S. Bhat 	if (!dev)
2851b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
2861b0a0e9aSSrivatsa S. Bhat 
2874f86d3a8SLen Brown 	if (dev->enabled)
2884f86d3a8SLen Brown 		return 0;
289bf4d1b5dSDaniel Lezcano 
290bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
291bf4d1b5dSDaniel Lezcano 
292e1689795SRobert Lee 	if (!drv || !cpuidle_curr_governor)
2934f86d3a8SLen Brown 		return -EIO;
294bf4d1b5dSDaniel Lezcano 
2954f86d3a8SLen Brown 	if (!dev->state_count)
296fc850f39SDaniel Lezcano 		dev->state_count = drv->state_count;
2974f86d3a8SLen Brown 
298dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0) {
299dcb84f33SVenkatesh Pallipadi 		ret = __cpuidle_register_device(dev);
300dcb84f33SVenkatesh Pallipadi 		if (ret)
301dcb84f33SVenkatesh Pallipadi 			return ret;
302dcb84f33SVenkatesh Pallipadi 	}
303dcb84f33SVenkatesh Pallipadi 
304e1689795SRobert Lee 	poll_idle_init(drv);
305d8c216cfSRafael J. Wysocki 
306bf4d1b5dSDaniel Lezcano 	ret = cpuidle_add_device_sysfs(dev);
307bf4d1b5dSDaniel Lezcano 	if (ret)
3084f86d3a8SLen Brown 		return ret;
3094f86d3a8SLen Brown 
3104f86d3a8SLen Brown 	if (cpuidle_curr_governor->enable &&
311e1689795SRobert Lee 	    (ret = cpuidle_curr_governor->enable(drv, dev)))
3124f86d3a8SLen Brown 		goto fail_sysfs;
3134f86d3a8SLen Brown 
3144f86d3a8SLen Brown 	for (i = 0; i < dev->state_count; i++) {
3154202735eSDeepthi Dharwar 		dev->states_usage[i].usage = 0;
3164202735eSDeepthi Dharwar 		dev->states_usage[i].time = 0;
3174f86d3a8SLen Brown 	}
3184f86d3a8SLen Brown 	dev->last_residency = 0;
3194f86d3a8SLen Brown 
3204f86d3a8SLen Brown 	smp_wmb();
3214f86d3a8SLen Brown 
3224f86d3a8SLen Brown 	dev->enabled = 1;
3234f86d3a8SLen Brown 
3244f86d3a8SLen Brown 	enabled_devices++;
3254f86d3a8SLen Brown 	return 0;
3264f86d3a8SLen Brown 
3274f86d3a8SLen Brown fail_sysfs:
328bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
3294f86d3a8SLen Brown 
3304f86d3a8SLen Brown 	return ret;
3314f86d3a8SLen Brown }
3324f86d3a8SLen Brown 
3334f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
3344f86d3a8SLen Brown 
3354f86d3a8SLen Brown /**
3364f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
3374f86d3a8SLen Brown  * @dev: the CPU
3384f86d3a8SLen Brown  *
3394f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
3404f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
3414f86d3a8SLen Brown  */
3424f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
3434f86d3a8SLen Brown {
344bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
345bf4d1b5dSDaniel Lezcano 
346cf31cd1aSSrivatsa S. Bhat 	if (!dev || !dev->enabled)
3474f86d3a8SLen Brown 		return;
348bf4d1b5dSDaniel Lezcano 
349bf4d1b5dSDaniel Lezcano 	if (!drv || !cpuidle_curr_governor)
3504f86d3a8SLen Brown 		return;
3514f86d3a8SLen Brown 
3524f86d3a8SLen Brown 	dev->enabled = 0;
3534f86d3a8SLen Brown 
3544f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
355bf4d1b5dSDaniel Lezcano 		cpuidle_curr_governor->disable(drv, dev);
3564f86d3a8SLen Brown 
357bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
3584f86d3a8SLen Brown 	enabled_devices--;
3594f86d3a8SLen Brown }
3604f86d3a8SLen Brown 
3614f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
3624f86d3a8SLen Brown 
3634f86d3a8SLen Brown /**
364dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
365dcb84f33SVenkatesh Pallipadi  * and enable routines
3664f86d3a8SLen Brown  * @dev: the cpu
367dcb84f33SVenkatesh Pallipadi  *
368dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
3694f86d3a8SLen Brown  */
370dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
3714f86d3a8SLen Brown {
3724f86d3a8SLen Brown 	int ret;
373bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
3744f86d3a8SLen Brown 
375bf4d1b5dSDaniel Lezcano 	if (!try_module_get(drv->owner))
3764f86d3a8SLen Brown 		return -EINVAL;
3774f86d3a8SLen Brown 
3784f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
3794f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
3801aef40e2SDaniel Lezcano 	ret = cpuidle_add_sysfs(dev);
3813af272abSColin Cross 	if (ret)
3823af272abSColin Cross 		goto err_sysfs;
3834f86d3a8SLen Brown 
3844126c019SColin Cross 	ret = cpuidle_coupled_register_device(dev);
3854126c019SColin Cross 	if (ret)
3864126c019SColin Cross 		goto err_coupled;
3874f86d3a8SLen Brown 
388dcb84f33SVenkatesh Pallipadi 	dev->registered = 1;
389dcb84f33SVenkatesh Pallipadi 	return 0;
3903af272abSColin Cross 
3914126c019SColin Cross err_coupled:
3921aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
3933af272abSColin Cross err_sysfs:
3943af272abSColin Cross 	list_del(&dev->device_list);
3953af272abSColin Cross 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
396bf4d1b5dSDaniel Lezcano 	module_put(drv->owner);
3973af272abSColin Cross 	return ret;
398dcb84f33SVenkatesh Pallipadi }
399dcb84f33SVenkatesh Pallipadi 
400dcb84f33SVenkatesh Pallipadi /**
401dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
402dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
403dcb84f33SVenkatesh Pallipadi  */
404dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
405dcb84f33SVenkatesh Pallipadi {
406dcb84f33SVenkatesh Pallipadi 	int ret;
407dcb84f33SVenkatesh Pallipadi 
4081b0a0e9aSSrivatsa S. Bhat 	if (!dev)
4091b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
4101b0a0e9aSSrivatsa S. Bhat 
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 {
435bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4364f86d3a8SLen Brown 
437dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0)
438dcb84f33SVenkatesh Pallipadi 		return;
439dcb84f33SVenkatesh Pallipadi 
4404f86d3a8SLen Brown 	cpuidle_pause_and_lock();
4414f86d3a8SLen Brown 
4424f86d3a8SLen Brown 	cpuidle_disable_device(dev);
4434f86d3a8SLen Brown 
4441aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
4454f86d3a8SLen Brown 	list_del(&dev->device_list);
4464f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
4474f86d3a8SLen Brown 
4484126c019SColin Cross 	cpuidle_coupled_unregister_device(dev);
4494126c019SColin Cross 
4504f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
4514f86d3a8SLen Brown 
452bf4d1b5dSDaniel Lezcano 	module_put(drv->owner);
4534f86d3a8SLen Brown }
4544f86d3a8SLen Brown 
4554f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
4564f86d3a8SLen Brown 
457*4c637b21SDaniel Lezcano /*
458*4c637b21SDaniel Lezcano  * cpuidle_unregister: unregister a driver and the devices. This function
459*4c637b21SDaniel Lezcano  * can be used only if the driver has been previously registered through
460*4c637b21SDaniel Lezcano  * the cpuidle_register function.
461*4c637b21SDaniel Lezcano  *
462*4c637b21SDaniel Lezcano  * @drv: a valid pointer to a struct cpuidle_driver
463*4c637b21SDaniel Lezcano  */
464*4c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv)
465*4c637b21SDaniel Lezcano {
466*4c637b21SDaniel Lezcano 	int cpu;
467*4c637b21SDaniel Lezcano 	struct cpuidle_device *device;
468*4c637b21SDaniel Lezcano 
469*4c637b21SDaniel Lezcano 	for_each_possible_cpu(cpu) {
470*4c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
471*4c637b21SDaniel Lezcano 		cpuidle_unregister_device(device);
472*4c637b21SDaniel Lezcano 	}
473*4c637b21SDaniel Lezcano 
474*4c637b21SDaniel Lezcano 	cpuidle_unregister_driver(drv);
475*4c637b21SDaniel Lezcano }
476*4c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister);
477*4c637b21SDaniel Lezcano 
478*4c637b21SDaniel Lezcano /**
479*4c637b21SDaniel Lezcano  * cpuidle_register: registers the driver and the cpu devices with the
480*4c637b21SDaniel Lezcano  * coupled_cpus passed as parameter. This function is used for all common
481*4c637b21SDaniel Lezcano  * initialization pattern there are in the arch specific drivers. The
482*4c637b21SDaniel Lezcano  * devices is globally defined in this file.
483*4c637b21SDaniel Lezcano  *
484*4c637b21SDaniel Lezcano  * @drv         : a valid pointer to a struct cpuidle_driver
485*4c637b21SDaniel Lezcano  * @coupled_cpus: a cpumask for the coupled states
486*4c637b21SDaniel Lezcano  *
487*4c637b21SDaniel Lezcano  * Returns 0 on success, < 0 otherwise
488*4c637b21SDaniel Lezcano  */
489*4c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv,
490*4c637b21SDaniel Lezcano 		     const struct cpumask *const coupled_cpus)
491*4c637b21SDaniel Lezcano {
492*4c637b21SDaniel Lezcano 	int ret, cpu;
493*4c637b21SDaniel Lezcano 	struct cpuidle_device *device;
494*4c637b21SDaniel Lezcano 
495*4c637b21SDaniel Lezcano 	ret = cpuidle_register_driver(drv);
496*4c637b21SDaniel Lezcano 	if (ret) {
497*4c637b21SDaniel Lezcano 		pr_err("failed to register cpuidle driver\n");
498*4c637b21SDaniel Lezcano 		return ret;
499*4c637b21SDaniel Lezcano 	}
500*4c637b21SDaniel Lezcano 
501*4c637b21SDaniel Lezcano 	for_each_possible_cpu(cpu) {
502*4c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
503*4c637b21SDaniel Lezcano 		device->cpu = cpu;
504*4c637b21SDaniel Lezcano 
505*4c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
506*4c637b21SDaniel Lezcano 		/*
507*4c637b21SDaniel Lezcano 		 * On multiplatform for ARM, the coupled idle states could
508*4c637b21SDaniel Lezcano 		 * enabled in the kernel even if the cpuidle driver does not
509*4c637b21SDaniel Lezcano 		 * use it. Note, coupled_cpus is a struct copy.
510*4c637b21SDaniel Lezcano 		 */
511*4c637b21SDaniel Lezcano 		if (coupled_cpus)
512*4c637b21SDaniel Lezcano 			device->coupled_cpus = *coupled_cpus;
513*4c637b21SDaniel Lezcano #endif
514*4c637b21SDaniel Lezcano 		ret = cpuidle_register_device(device);
515*4c637b21SDaniel Lezcano 		if (!ret)
516*4c637b21SDaniel Lezcano 			continue;
517*4c637b21SDaniel Lezcano 
518*4c637b21SDaniel Lezcano 		pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
519*4c637b21SDaniel Lezcano 
520*4c637b21SDaniel Lezcano 		cpuidle_unregister(drv);
521*4c637b21SDaniel Lezcano 		break;
522*4c637b21SDaniel Lezcano 	}
523*4c637b21SDaniel Lezcano 
524*4c637b21SDaniel Lezcano 	return ret;
525*4c637b21SDaniel Lezcano }
526*4c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register);
527*4c637b21SDaniel Lezcano 
5284f86d3a8SLen Brown #ifdef CONFIG_SMP
5294f86d3a8SLen Brown 
5304f86d3a8SLen Brown static void smp_callback(void *v)
5314f86d3a8SLen Brown {
5324f86d3a8SLen Brown 	/* we already woke the CPU up, nothing more to do */
5334f86d3a8SLen Brown }
5344f86d3a8SLen Brown 
5354f86d3a8SLen Brown /*
5364f86d3a8SLen Brown  * This function gets called when a part of the kernel has a new latency
5374f86d3a8SLen Brown  * requirement.  This means we need to get all processors out of their C-state,
5384f86d3a8SLen Brown  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
5394f86d3a8SLen Brown  * wakes them all right up.
5404f86d3a8SLen Brown  */
5414f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b,
5424f86d3a8SLen Brown 		unsigned long l, void *v)
5434f86d3a8SLen Brown {
5448691e5a8SJens Axboe 	smp_call_function(smp_callback, NULL, 1);
5454f86d3a8SLen Brown 	return NOTIFY_OK;
5464f86d3a8SLen Brown }
5474f86d3a8SLen Brown 
5484f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = {
5494f86d3a8SLen Brown 	.notifier_call = cpuidle_latency_notify,
5504f86d3a8SLen Brown };
5514f86d3a8SLen Brown 
552d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n)
553d82b3518SMark Gross {
554d82b3518SMark Gross 	pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
555d82b3518SMark Gross }
5564f86d3a8SLen Brown 
5574f86d3a8SLen Brown #else /* CONFIG_SMP */
5584f86d3a8SLen Brown 
5594f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0)
5604f86d3a8SLen Brown 
5614f86d3a8SLen Brown #endif /* CONFIG_SMP */
5624f86d3a8SLen Brown 
5634f86d3a8SLen Brown /**
5644f86d3a8SLen Brown  * cpuidle_init - core initializer
5654f86d3a8SLen Brown  */
5664f86d3a8SLen Brown static int __init cpuidle_init(void)
5674f86d3a8SLen Brown {
5684f86d3a8SLen Brown 	int ret;
5694f86d3a8SLen Brown 
57062027aeaSLen Brown 	if (cpuidle_disabled())
57162027aeaSLen Brown 		return -ENODEV;
57262027aeaSLen Brown 
5738a25a2fdSKay Sievers 	ret = cpuidle_add_interface(cpu_subsys.dev_root);
5744f86d3a8SLen Brown 	if (ret)
5754f86d3a8SLen Brown 		return ret;
5764f86d3a8SLen Brown 
5774f86d3a8SLen Brown 	latency_notifier_init(&cpuidle_latency_notifier);
5784f86d3a8SLen Brown 
5794f86d3a8SLen Brown 	return 0;
5804f86d3a8SLen Brown }
5814f86d3a8SLen Brown 
58262027aeaSLen Brown module_param(off, int, 0444);
5834f86d3a8SLen Brown core_initcall(cpuidle_init);
584