xref: /linux/drivers/cpuidle/cpuidle.c (revision bf4d1b5ddb78f86078ac6ae0415802d5f0c68f92)
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);
71*bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
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 /**
9556cfbf74SColin Cross  * cpuidle_enter_state - enter the state and update stats
9656cfbf74SColin Cross  * @dev: cpuidle device for this cpu
9756cfbf74SColin Cross  * @drv: cpuidle driver for this cpu
9856cfbf74SColin Cross  * @next_state: index into drv->states of the state to enter
9956cfbf74SColin Cross  */
10056cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
10156cfbf74SColin Cross 		int next_state)
10256cfbf74SColin Cross {
10356cfbf74SColin Cross 	int entered_state;
10456cfbf74SColin Cross 
10556cfbf74SColin Cross 	entered_state = cpuidle_enter_ops(dev, drv, next_state);
10656cfbf74SColin Cross 
10756cfbf74SColin Cross 	if (entered_state >= 0) {
10856cfbf74SColin Cross 		/* Update cpuidle counters */
10956cfbf74SColin Cross 		/* This can be moved to within driver enter routine
11056cfbf74SColin Cross 		 * but that results in multiple copies of same code.
11156cfbf74SColin Cross 		 */
11256cfbf74SColin Cross 		dev->states_usage[entered_state].time +=
11356cfbf74SColin Cross 				(unsigned long long)dev->last_residency;
11456cfbf74SColin Cross 		dev->states_usage[entered_state].usage++;
11556cfbf74SColin Cross 	} else {
11656cfbf74SColin Cross 		dev->last_residency = 0;
11756cfbf74SColin Cross 	}
11856cfbf74SColin Cross 
11956cfbf74SColin Cross 	return entered_state;
12056cfbf74SColin Cross }
12156cfbf74SColin Cross 
12256cfbf74SColin Cross /**
1234f86d3a8SLen Brown  * cpuidle_idle_call - the main idle loop
1244f86d3a8SLen Brown  *
1254f86d3a8SLen Brown  * NOTE: no locks or semaphores should be used here
126a0bfa137SLen Brown  * return non-zero on failure
1274f86d3a8SLen Brown  */
128a0bfa137SLen Brown int cpuidle_idle_call(void)
1294f86d3a8SLen Brown {
1304a6f4fe8SChristoph Lameter 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
131*bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
132e978aa7dSDeepthi Dharwar 	int next_state, entered_state;
1334f86d3a8SLen Brown 
134a0bfa137SLen Brown 	if (off)
135a0bfa137SLen Brown 		return -ENODEV;
136a0bfa137SLen Brown 
137a0bfa137SLen Brown 	if (!initialized)
138a0bfa137SLen Brown 		return -ENODEV;
139a0bfa137SLen Brown 
1404f86d3a8SLen Brown 	/* check if the device is ready */
141a0bfa137SLen Brown 	if (!dev || !dev->enabled)
142a0bfa137SLen Brown 		return -EBUSY;
1434f86d3a8SLen Brown 
144*bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
145*bf4d1b5dSDaniel Lezcano 
1464f86d3a8SLen Brown 	/* ask the governor for the next state */
14746bcfad7SDeepthi Dharwar 	next_state = cpuidle_curr_governor->select(drv, dev);
148246eb7f0SKevin Hilman 	if (need_resched()) {
149d73d68dcSYouquan Song 		dev->last_residency = 0;
150d73d68dcSYouquan Song 		/* give the governor an opportunity to reflect on the outcome */
151d73d68dcSYouquan Song 		if (cpuidle_curr_governor->reflect)
152d73d68dcSYouquan Song 			cpuidle_curr_governor->reflect(dev, next_state);
153246eb7f0SKevin Hilman 		local_irq_enable();
154a0bfa137SLen Brown 		return 0;
155246eb7f0SKevin Hilman 	}
156246eb7f0SKevin Hilman 
15776027ea8SSteven Rostedt 	trace_power_start_rcuidle(POWER_CSTATE, next_state, dev->cpu);
15876027ea8SSteven Rostedt 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
159f77cfe4eSThomas Renninger 
1604126c019SColin Cross 	if (cpuidle_state_is_coupled(dev, drv, next_state))
1614126c019SColin Cross 		entered_state = cpuidle_enter_state_coupled(dev, drv,
1624126c019SColin Cross 							    next_state);
1634126c019SColin Cross 	else
16456cfbf74SColin Cross 		entered_state = cpuidle_enter_state(dev, drv, next_state);
165f77cfe4eSThomas Renninger 
16676027ea8SSteven Rostedt 	trace_power_end_rcuidle(dev->cpu);
16776027ea8SSteven Rostedt 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
168f77cfe4eSThomas Renninger 
1694f86d3a8SLen Brown 	/* give the governor an opportunity to reflect on the outcome */
1704f86d3a8SLen Brown 	if (cpuidle_curr_governor->reflect)
171e978aa7dSDeepthi Dharwar 		cpuidle_curr_governor->reflect(dev, entered_state);
172a0bfa137SLen Brown 
173a0bfa137SLen Brown 	return 0;
1744f86d3a8SLen Brown }
1754f86d3a8SLen Brown 
1764f86d3a8SLen Brown /**
1774f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
1784f86d3a8SLen Brown  */
1794f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
1804f86d3a8SLen Brown {
181a0bfa137SLen Brown 	if (enabled_devices) {
1824f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
1834f86d3a8SLen Brown 		smp_wmb();
184a0bfa137SLen Brown 		initialized = 1;
1854f86d3a8SLen Brown 	}
1864f86d3a8SLen Brown }
1874f86d3a8SLen Brown 
1884f86d3a8SLen Brown /**
1894f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
1904f86d3a8SLen Brown  */
1914f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
1924f86d3a8SLen Brown {
193a0bfa137SLen Brown 	if (enabled_devices) {
194a0bfa137SLen Brown 		initialized = 0;
1954a162513SThomas Gleixner 		kick_all_cpus_sync();
1964f86d3a8SLen Brown 	}
1974f86d3a8SLen Brown }
1984f86d3a8SLen Brown 
1994f86d3a8SLen Brown /**
2004f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
2014f86d3a8SLen Brown  */
2024f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
2034f86d3a8SLen Brown {
2044f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
2054f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
2064f86d3a8SLen Brown }
2074f86d3a8SLen Brown 
2084f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
2094f86d3a8SLen Brown 
2104f86d3a8SLen Brown /**
2114f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
2124f86d3a8SLen Brown  */
2134f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
2144f86d3a8SLen Brown {
2154f86d3a8SLen Brown 	cpuidle_install_idle_handler();
2164f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
2174f86d3a8SLen Brown }
2184f86d3a8SLen Brown 
2194f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
2204f86d3a8SLen Brown 
2218651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
2228651f97bSPreeti U Murthy void cpuidle_pause(void)
2238651f97bSPreeti U Murthy {
2248651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
2258651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
2268651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
2278651f97bSPreeti U Murthy }
2288651f97bSPreeti U Murthy 
2298651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
2308651f97bSPreeti U Murthy void cpuidle_resume(void)
2318651f97bSPreeti U Murthy {
2328651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
2338651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
2348651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
2358651f97bSPreeti U Murthy }
2368651f97bSPreeti U Murthy 
237e1689795SRobert Lee /**
238e1689795SRobert Lee  * cpuidle_wrap_enter - performs timekeeping and irqen around enter function
239e1689795SRobert Lee  * @dev: pointer to a valid cpuidle_device object
240e1689795SRobert Lee  * @drv: pointer to a valid cpuidle_driver object
241e1689795SRobert Lee  * @index: index of the target cpuidle state.
242e1689795SRobert Lee  */
243e1689795SRobert Lee int cpuidle_wrap_enter(struct cpuidle_device *dev,
244e1689795SRobert Lee 				struct cpuidle_driver *drv, int index,
245e1689795SRobert Lee 				int (*enter)(struct cpuidle_device *dev,
246e1689795SRobert Lee 					struct cpuidle_driver *drv, int index))
247e1689795SRobert Lee {
248e1689795SRobert Lee 	ktime_t time_start, time_end;
249e1689795SRobert Lee 	s64 diff;
250e1689795SRobert Lee 
251e1689795SRobert Lee 	time_start = ktime_get();
252e1689795SRobert Lee 
253e1689795SRobert Lee 	index = enter(dev, drv, index);
254e1689795SRobert Lee 
255e1689795SRobert Lee 	time_end = ktime_get();
256e1689795SRobert Lee 
257e1689795SRobert Lee 	local_irq_enable();
258e1689795SRobert Lee 
259e1689795SRobert Lee 	diff = ktime_to_us(ktime_sub(time_end, time_start));
260e1689795SRobert Lee 	if (diff > INT_MAX)
261e1689795SRobert Lee 		diff = INT_MAX;
262e1689795SRobert Lee 
263e1689795SRobert Lee 	dev->last_residency = (int) diff;
264e1689795SRobert Lee 
265e1689795SRobert Lee 	return index;
266e1689795SRobert Lee }
267e1689795SRobert Lee 
268d8c216cfSRafael J. Wysocki #ifdef CONFIG_ARCH_HAS_CPU_RELAX
26946bcfad7SDeepthi Dharwar static int poll_idle(struct cpuidle_device *dev,
27046bcfad7SDeepthi Dharwar 		struct cpuidle_driver *drv, int index)
271d8c216cfSRafael J. Wysocki {
272d8c216cfSRafael J. Wysocki 	ktime_t	t1, t2;
273d8c216cfSRafael J. Wysocki 	s64 diff;
274d8c216cfSRafael J. Wysocki 
275d8c216cfSRafael J. Wysocki 	t1 = ktime_get();
276d8c216cfSRafael J. Wysocki 	local_irq_enable();
277d8c216cfSRafael J. Wysocki 	while (!need_resched())
278d8c216cfSRafael J. Wysocki 		cpu_relax();
279d8c216cfSRafael J. Wysocki 
280d8c216cfSRafael J. Wysocki 	t2 = ktime_get();
281d8c216cfSRafael J. Wysocki 	diff = ktime_to_us(ktime_sub(t2, t1));
282d8c216cfSRafael J. Wysocki 	if (diff > INT_MAX)
283d8c216cfSRafael J. Wysocki 		diff = INT_MAX;
284d8c216cfSRafael J. Wysocki 
285e978aa7dSDeepthi Dharwar 	dev->last_residency = (int) diff;
286e978aa7dSDeepthi Dharwar 
287e978aa7dSDeepthi Dharwar 	return index;
288d8c216cfSRafael J. Wysocki }
289d8c216cfSRafael J. Wysocki 
29046bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv)
291d8c216cfSRafael J. Wysocki {
29246bcfad7SDeepthi Dharwar 	struct cpuidle_state *state = &drv->states[0];
293d8c216cfSRafael J. Wysocki 
294720f1c30SThomas Renninger 	snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
295d8c216cfSRafael J. Wysocki 	snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
296d8c216cfSRafael J. Wysocki 	state->exit_latency = 0;
297d8c216cfSRafael J. Wysocki 	state->target_residency = 0;
298d8c216cfSRafael J. Wysocki 	state->power_usage = -1;
299d247632cSLen Brown 	state->flags = 0;
300d8c216cfSRafael J. Wysocki 	state->enter = poll_idle;
301cbc9ef02SRafael J. Wysocki 	state->disabled = false;
302d8c216cfSRafael J. Wysocki }
303d8c216cfSRafael J. Wysocki #else
30446bcfad7SDeepthi Dharwar static void poll_idle_init(struct cpuidle_driver *drv) {}
305d8c216cfSRafael J. Wysocki #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
306d8c216cfSRafael J. Wysocki 
3074f86d3a8SLen Brown /**
3084f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
3094f86d3a8SLen Brown  * @dev: the CPU
3104f86d3a8SLen Brown  *
3114f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
3124f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
3134f86d3a8SLen Brown  */
3144f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
3154f86d3a8SLen Brown {
3164f86d3a8SLen Brown 	int ret, i;
317*bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
3184f86d3a8SLen Brown 
3191b0a0e9aSSrivatsa S. Bhat 	if (!dev)
3201b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
3211b0a0e9aSSrivatsa S. Bhat 
3224f86d3a8SLen Brown 	if (dev->enabled)
3234f86d3a8SLen Brown 		return 0;
324*bf4d1b5dSDaniel Lezcano 
325*bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
326*bf4d1b5dSDaniel Lezcano 
327e1689795SRobert Lee 	if (!drv || !cpuidle_curr_governor)
3284f86d3a8SLen Brown 		return -EIO;
329*bf4d1b5dSDaniel Lezcano 
3304f86d3a8SLen Brown 	if (!dev->state_count)
331fc850f39SDaniel Lezcano 		dev->state_count = drv->state_count;
3324f86d3a8SLen Brown 
333dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0) {
334dcb84f33SVenkatesh Pallipadi 		ret = __cpuidle_register_device(dev);
335dcb84f33SVenkatesh Pallipadi 		if (ret)
336dcb84f33SVenkatesh Pallipadi 			return ret;
337dcb84f33SVenkatesh Pallipadi 	}
338dcb84f33SVenkatesh Pallipadi 
339e1689795SRobert Lee 	cpuidle_enter_ops = drv->en_core_tk_irqen ?
340e1689795SRobert Lee 		cpuidle_enter_tk : cpuidle_enter;
341e1689795SRobert Lee 
342e1689795SRobert Lee 	poll_idle_init(drv);
343d8c216cfSRafael J. Wysocki 
344*bf4d1b5dSDaniel Lezcano 	ret = cpuidle_add_device_sysfs(dev);
345*bf4d1b5dSDaniel Lezcano 	if (ret)
3464f86d3a8SLen Brown 		return ret;
3474f86d3a8SLen Brown 
3484f86d3a8SLen Brown 	if (cpuidle_curr_governor->enable &&
349e1689795SRobert Lee 	    (ret = cpuidle_curr_governor->enable(drv, dev)))
3504f86d3a8SLen Brown 		goto fail_sysfs;
3514f86d3a8SLen Brown 
3524f86d3a8SLen Brown 	for (i = 0; i < dev->state_count; i++) {
3534202735eSDeepthi Dharwar 		dev->states_usage[i].usage = 0;
3544202735eSDeepthi Dharwar 		dev->states_usage[i].time = 0;
3554f86d3a8SLen Brown 	}
3564f86d3a8SLen Brown 	dev->last_residency = 0;
3574f86d3a8SLen Brown 
3584f86d3a8SLen Brown 	smp_wmb();
3594f86d3a8SLen Brown 
3604f86d3a8SLen Brown 	dev->enabled = 1;
3614f86d3a8SLen Brown 
3624f86d3a8SLen Brown 	enabled_devices++;
3634f86d3a8SLen Brown 	return 0;
3644f86d3a8SLen Brown 
3654f86d3a8SLen Brown fail_sysfs:
366*bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
3674f86d3a8SLen Brown 
3684f86d3a8SLen Brown 	return ret;
3694f86d3a8SLen Brown }
3704f86d3a8SLen Brown 
3714f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
3724f86d3a8SLen Brown 
3734f86d3a8SLen Brown /**
3744f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
3754f86d3a8SLen Brown  * @dev: the CPU
3764f86d3a8SLen Brown  *
3774f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
3784f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
3794f86d3a8SLen Brown  */
3804f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
3814f86d3a8SLen Brown {
382*bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
383*bf4d1b5dSDaniel Lezcano 
384cf31cd1aSSrivatsa S. Bhat 	if (!dev || !dev->enabled)
3854f86d3a8SLen Brown 		return;
386*bf4d1b5dSDaniel Lezcano 
387*bf4d1b5dSDaniel Lezcano 	if (!drv || !cpuidle_curr_governor)
3884f86d3a8SLen Brown 		return;
3894f86d3a8SLen Brown 
3904f86d3a8SLen Brown 	dev->enabled = 0;
3914f86d3a8SLen Brown 
3924f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
393*bf4d1b5dSDaniel Lezcano 		cpuidle_curr_governor->disable(drv, dev);
3944f86d3a8SLen Brown 
395*bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
3964f86d3a8SLen Brown 	enabled_devices--;
3974f86d3a8SLen Brown }
3984f86d3a8SLen Brown 
3994f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
4004f86d3a8SLen Brown 
4014f86d3a8SLen Brown /**
402dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
403dcb84f33SVenkatesh Pallipadi  * and enable routines
4044f86d3a8SLen Brown  * @dev: the cpu
405dcb84f33SVenkatesh Pallipadi  *
406dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
4074f86d3a8SLen Brown  */
408dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
4094f86d3a8SLen Brown {
4104f86d3a8SLen Brown 	int ret;
411*bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4124f86d3a8SLen Brown 
413*bf4d1b5dSDaniel Lezcano 	if (!try_module_get(drv->owner))
4144f86d3a8SLen Brown 		return -EINVAL;
4154f86d3a8SLen Brown 
4164f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
4174f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
4181aef40e2SDaniel Lezcano 	ret = cpuidle_add_sysfs(dev);
4193af272abSColin Cross 	if (ret)
4203af272abSColin Cross 		goto err_sysfs;
4214f86d3a8SLen Brown 
4224126c019SColin Cross 	ret = cpuidle_coupled_register_device(dev);
4234126c019SColin Cross 	if (ret)
4244126c019SColin Cross 		goto err_coupled;
4254f86d3a8SLen Brown 
426dcb84f33SVenkatesh Pallipadi 	dev->registered = 1;
427dcb84f33SVenkatesh Pallipadi 	return 0;
4283af272abSColin Cross 
4294126c019SColin Cross err_coupled:
4301aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
4313af272abSColin Cross err_sysfs:
4323af272abSColin Cross 	list_del(&dev->device_list);
4333af272abSColin Cross 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
434*bf4d1b5dSDaniel Lezcano 	module_put(drv->owner);
4353af272abSColin Cross 	return ret;
436dcb84f33SVenkatesh Pallipadi }
437dcb84f33SVenkatesh Pallipadi 
438dcb84f33SVenkatesh Pallipadi /**
439dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
440dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
441dcb84f33SVenkatesh Pallipadi  */
442dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
443dcb84f33SVenkatesh Pallipadi {
444dcb84f33SVenkatesh Pallipadi 	int ret;
445dcb84f33SVenkatesh Pallipadi 
4461b0a0e9aSSrivatsa S. Bhat 	if (!dev)
4471b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
4481b0a0e9aSSrivatsa S. Bhat 
449dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
450dcb84f33SVenkatesh Pallipadi 
451dcb84f33SVenkatesh Pallipadi 	if ((ret = __cpuidle_register_device(dev))) {
452dcb84f33SVenkatesh Pallipadi 		mutex_unlock(&cpuidle_lock);
453dcb84f33SVenkatesh Pallipadi 		return ret;
454dcb84f33SVenkatesh Pallipadi 	}
455dcb84f33SVenkatesh Pallipadi 
4564f86d3a8SLen Brown 	cpuidle_enable_device(dev);
4574f86d3a8SLen Brown 	cpuidle_install_idle_handler();
4584f86d3a8SLen Brown 
4594f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
4604f86d3a8SLen Brown 
4614f86d3a8SLen Brown 	return 0;
4624f86d3a8SLen Brown 
4634f86d3a8SLen Brown }
4644f86d3a8SLen Brown 
4654f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
4664f86d3a8SLen Brown 
4674f86d3a8SLen Brown /**
4684f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
4694f86d3a8SLen Brown  * @dev: the cpu
4704f86d3a8SLen Brown  */
4714f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
4724f86d3a8SLen Brown {
473*bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4744f86d3a8SLen Brown 
475dcb84f33SVenkatesh Pallipadi 	if (dev->registered == 0)
476dcb84f33SVenkatesh Pallipadi 		return;
477dcb84f33SVenkatesh Pallipadi 
4784f86d3a8SLen Brown 	cpuidle_pause_and_lock();
4794f86d3a8SLen Brown 
4804f86d3a8SLen Brown 	cpuidle_disable_device(dev);
4814f86d3a8SLen Brown 
4821aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
4834f86d3a8SLen Brown 	list_del(&dev->device_list);
4844f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
4854f86d3a8SLen Brown 
4864126c019SColin Cross 	cpuidle_coupled_unregister_device(dev);
4874126c019SColin Cross 
4884f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
4894f86d3a8SLen Brown 
490*bf4d1b5dSDaniel Lezcano 	module_put(drv->owner);
4914f86d3a8SLen Brown }
4924f86d3a8SLen Brown 
4934f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
4944f86d3a8SLen Brown 
4954f86d3a8SLen Brown #ifdef CONFIG_SMP
4964f86d3a8SLen Brown 
4974f86d3a8SLen Brown static void smp_callback(void *v)
4984f86d3a8SLen Brown {
4994f86d3a8SLen Brown 	/* we already woke the CPU up, nothing more to do */
5004f86d3a8SLen Brown }
5014f86d3a8SLen Brown 
5024f86d3a8SLen Brown /*
5034f86d3a8SLen Brown  * This function gets called when a part of the kernel has a new latency
5044f86d3a8SLen Brown  * requirement.  This means we need to get all processors out of their C-state,
5054f86d3a8SLen Brown  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
5064f86d3a8SLen Brown  * wakes them all right up.
5074f86d3a8SLen Brown  */
5084f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b,
5094f86d3a8SLen Brown 		unsigned long l, void *v)
5104f86d3a8SLen Brown {
5118691e5a8SJens Axboe 	smp_call_function(smp_callback, NULL, 1);
5124f86d3a8SLen Brown 	return NOTIFY_OK;
5134f86d3a8SLen Brown }
5144f86d3a8SLen Brown 
5154f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = {
5164f86d3a8SLen Brown 	.notifier_call = cpuidle_latency_notify,
5174f86d3a8SLen Brown };
5184f86d3a8SLen Brown 
519d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n)
520d82b3518SMark Gross {
521d82b3518SMark Gross 	pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
522d82b3518SMark Gross }
5234f86d3a8SLen Brown 
5244f86d3a8SLen Brown #else /* CONFIG_SMP */
5254f86d3a8SLen Brown 
5264f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0)
5274f86d3a8SLen Brown 
5284f86d3a8SLen Brown #endif /* CONFIG_SMP */
5294f86d3a8SLen Brown 
5304f86d3a8SLen Brown /**
5314f86d3a8SLen Brown  * cpuidle_init - core initializer
5324f86d3a8SLen Brown  */
5334f86d3a8SLen Brown static int __init cpuidle_init(void)
5344f86d3a8SLen Brown {
5354f86d3a8SLen Brown 	int ret;
5364f86d3a8SLen Brown 
53762027aeaSLen Brown 	if (cpuidle_disabled())
53862027aeaSLen Brown 		return -ENODEV;
53962027aeaSLen Brown 
5408a25a2fdSKay Sievers 	ret = cpuidle_add_interface(cpu_subsys.dev_root);
5414f86d3a8SLen Brown 	if (ret)
5424f86d3a8SLen Brown 		return ret;
5434f86d3a8SLen Brown 
5444f86d3a8SLen Brown 	latency_notifier_init(&cpuidle_latency_notifier);
5454f86d3a8SLen Brown 
5464f86d3a8SLen Brown 	return 0;
5474f86d3a8SLen Brown }
5484f86d3a8SLen Brown 
54962027aeaSLen Brown module_param(off, int, 0444);
5504f86d3a8SLen Brown core_initcall(cpuidle_init);
551