xref: /linux/drivers/cpuidle/cpuidle.c (revision 02824a5fd11f99b4637668926a59aab3698b46a9)
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 
116ab4b199SKajetan Puchalski #include "linux/percpu-defs.h"
12b60e6a0eSDaniel Lezcano #include <linux/clockchips.h>
134f86d3a8SLen Brown #include <linux/kernel.h>
144f86d3a8SLen Brown #include <linux/mutex.h>
154f86d3a8SLen Brown #include <linux/sched.h>
16e6017571SIngo Molnar #include <linux/sched/clock.h>
17a01353cfSPeter Zijlstra #include <linux/sched/idle.h>
184f86d3a8SLen Brown #include <linux/notifier.h>
19e8db0be1SJean Pihet #include <linux/pm_qos.h>
204f86d3a8SLen Brown #include <linux/cpu.h>
214f86d3a8SLen Brown #include <linux/cpuidle.h>
229a0b8415Svenkatesh.pallipadi@intel.com #include <linux/ktime.h>
232e94d1f7SArjan van de Ven #include <linux/hrtimer.h>
24884b17e1SPaul Gortmaker #include <linux/module.h>
2538106313SRafael J. Wysocki #include <linux/suspend.h>
26124cf911SRafael J. Wysocki #include <linux/tick.h>
27bf9282dcSPeter Zijlstra #include <linux/mmu_context.h>
28e67198ccSFrederic Weisbecker #include <linux/context_tracking.h>
29288f023eSArjan van de Ven #include <trace/events/power.h>
304f86d3a8SLen Brown 
314f86d3a8SLen Brown #include "cpuidle.h"
324f86d3a8SLen Brown 
334f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
344c637b21SDaniel Lezcano DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
354f86d3a8SLen Brown 
364f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock);
374f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices);
384f86d3a8SLen Brown 
394f86d3a8SLen Brown static int enabled_devices;
4062027aeaSLen Brown static int off __read_mostly;
41a0bfa137SLen Brown static int initialized __read_mostly;
4262027aeaSLen Brown 
cpuidle_disabled(void)4362027aeaSLen Brown int cpuidle_disabled(void)
4462027aeaSLen Brown {
4562027aeaSLen Brown 	return off;
4662027aeaSLen Brown }
disable_cpuidle(void)47d91ee586SLen Brown void disable_cpuidle(void)
48d91ee586SLen Brown {
49d91ee586SLen Brown 	off = 1;
50d91ee586SLen Brown }
514f86d3a8SLen Brown 
cpuidle_not_available(struct cpuidle_driver * drv,struct cpuidle_device * dev)52ef2b22acSRafael J. Wysocki bool cpuidle_not_available(struct cpuidle_driver *drv,
5331a34090SRafael J. Wysocki 			   struct cpuidle_device *dev)
5431a34090SRafael J. Wysocki {
5531a34090SRafael J. Wysocki 	return off || !initialized || !drv || !dev || !dev->enabled;
5631a34090SRafael J. Wysocki }
5731a34090SRafael J. Wysocki 
584f86d3a8SLen Brown /**
591a022e3fSBoris Ostrovsky  * cpuidle_play_dead - cpu off-lining
601a022e3fSBoris Ostrovsky  *
61ee01e663SToshi Kani  * Returns in case of an error or no driver
621a022e3fSBoris Ostrovsky  */
cpuidle_play_dead(void)631a022e3fSBoris Ostrovsky int cpuidle_play_dead(void)
641a022e3fSBoris Ostrovsky {
651a022e3fSBoris Ostrovsky 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
66bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
678aef33a7SDaniel Lezcano 	int i;
681a022e3fSBoris Ostrovsky 
69ee01e663SToshi Kani 	if (!drv)
70ee01e663SToshi Kani 		return -ENODEV;
71ee01e663SToshi Kani 
721a022e3fSBoris Ostrovsky 	/* Find lowest-power state that supports long-term idle */
737d51d979SRafael J. Wysocki 	for (i = drv->state_count - 1; i >= 0; i--)
748aef33a7SDaniel Lezcano 		if (drv->states[i].enter_dead)
758aef33a7SDaniel Lezcano 			return drv->states[i].enter_dead(dev, i);
761a022e3fSBoris Ostrovsky 
771a022e3fSBoris Ostrovsky 	return -ENODEV;
781a022e3fSBoris Ostrovsky }
791a022e3fSBoris Ostrovsky 
find_deepest_state(struct cpuidle_driver * drv,struct cpuidle_device * dev,u64 max_latency_ns,unsigned int forbidden_flags,bool s2idle)80ef2b22acSRafael J. Wysocki static int find_deepest_state(struct cpuidle_driver *drv,
810d94039fSRafael J. Wysocki 			      struct cpuidle_device *dev,
82c1d51f68SRafael J. Wysocki 			      u64 max_latency_ns,
830d94039fSRafael J. Wysocki 			      unsigned int forbidden_flags,
8428ba086eSRafael J. Wysocki 			      bool s2idle)
85a6220fc1SRafael J. Wysocki {
86c1d51f68SRafael J. Wysocki 	u64 latency_req = 0;
8751164251SRafael J. Wysocki 	int i, ret = 0;
88a6220fc1SRafael J. Wysocki 
8951164251SRafael J. Wysocki 	for (i = 1; i < drv->state_count; i++) {
90a6220fc1SRafael J. Wysocki 		struct cpuidle_state *s = &drv->states[i];
91a6220fc1SRafael J. Wysocki 
9299e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable ||
93c1d51f68SRafael J. Wysocki 		    s->exit_latency_ns <= latency_req ||
94c1d51f68SRafael J. Wysocki 		    s->exit_latency_ns > max_latency_ns ||
9599e98d3fSRafael J. Wysocki 		    (s->flags & forbidden_flags) ||
9699e98d3fSRafael J. Wysocki 		    (s2idle && !s->enter_s2idle))
97a6220fc1SRafael J. Wysocki 			continue;
98a6220fc1SRafael J. Wysocki 
99c1d51f68SRafael J. Wysocki 		latency_req = s->exit_latency_ns;
100a6220fc1SRafael J. Wysocki 		ret = i;
101a6220fc1SRafael J. Wysocki 	}
102a6220fc1SRafael J. Wysocki 	return ret;
103a6220fc1SRafael J. Wysocki }
104a6220fc1SRafael J. Wysocki 
1050e7414b7SRafael J. Wysocki /**
106c55b51a0SDaniel Lezcano  * cpuidle_use_deepest_state - Set/unset governor override mode.
107c55b51a0SDaniel Lezcano  * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
1080e7414b7SRafael J. Wysocki  *
109c55b51a0SDaniel Lezcano  * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
110c55b51a0SDaniel Lezcano  * state with exit latency within @latency_limit_ns (override governors going
111c55b51a0SDaniel Lezcano  * forward), or do not override governors if it is zero.
1120e7414b7SRafael J. Wysocki  */
cpuidle_use_deepest_state(u64 latency_limit_ns)113c55b51a0SDaniel Lezcano void cpuidle_use_deepest_state(u64 latency_limit_ns)
114bb8313b6SJacob Pan {
115bb8313b6SJacob Pan 	struct cpuidle_device *dev;
116bb8313b6SJacob Pan 
117bb8313b6SJacob Pan 	preempt_disable();
118bb8313b6SJacob Pan 	dev = cpuidle_get_device();
11941dc750eSLi, Fei 	if (dev)
120c55b51a0SDaniel Lezcano 		dev->forced_idle_latency_limit_ns = latency_limit_ns;
121bb8313b6SJacob Pan 	preempt_enable();
122bb8313b6SJacob Pan }
123bb8313b6SJacob Pan 
124ef2b22acSRafael J. Wysocki /**
125ef2b22acSRafael J. Wysocki  * cpuidle_find_deepest_state - Find the deepest available idle state.
126ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
127ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
128cefb9409SBenjamin Gaignard  * @latency_limit_ns: Idle state exit latency limit
129cefb9409SBenjamin Gaignard  *
130cefb9409SBenjamin Gaignard  * Return: the index of the deepest available idle state.
131ef2b22acSRafael J. Wysocki  */
cpuidle_find_deepest_state(struct cpuidle_driver * drv,struct cpuidle_device * dev,u64 latency_limit_ns)132ef2b22acSRafael J. Wysocki int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
1335aa9ba63SDaniel Lezcano 			       struct cpuidle_device *dev,
1345aa9ba63SDaniel Lezcano 			       u64 latency_limit_ns)
135ef2b22acSRafael J. Wysocki {
1365aa9ba63SDaniel Lezcano 	return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
137ef2b22acSRafael J. Wysocki }
138ef2b22acSRafael J. Wysocki 
139bb8313b6SJacob Pan #ifdef CONFIG_SUSPEND
enter_s2idle_proper(struct cpuidle_driver * drv,struct cpuidle_device * dev,int index)14017cc2b55SPeter Zijlstra static noinstr void enter_s2idle_proper(struct cpuidle_driver *drv,
141124cf911SRafael J. Wysocki 					 struct cpuidle_device *dev, int index)
142124cf911SRafael J. Wysocki {
1438747f202SPeter Zijlstra 	struct cpuidle_state *target_state = &drv->states[index];
14417cc2b55SPeter Zijlstra 	ktime_t time_start, time_end;
14517cc2b55SPeter Zijlstra 
14617cc2b55SPeter Zijlstra 	instrumentation_begin();
14764bdff69SRafael J. Wysocki 
148e6a15fa9SPeter Zijlstra 	time_start = ns_to_ktime(local_clock_noinstr());
14964bdff69SRafael J. Wysocki 
1501098582aSPeter Zijlstra 	tick_freeze();
151124cf911SRafael J. Wysocki 	/*
152124cf911SRafael J. Wysocki 	 * The state used here cannot be a "coupled" one, because the "coupled"
153124cf911SRafael J. Wysocki 	 * cpuidle mechanism enables interrupts and doing that with timekeeping
154124cf911SRafael J. Wysocki 	 * suspended is generally unsafe.
155124cf911SRafael J. Wysocki 	 */
15663caae84SLucas Stach 	stop_critical_timings();
15717cc2b55SPeter Zijlstra 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
158a01353cfSPeter Zijlstra 		ct_cpuidle_enter();
15917cc2b55SPeter Zijlstra 		/* Annotate away the indirect call */
16017cc2b55SPeter Zijlstra 		instrumentation_begin();
16117cc2b55SPeter Zijlstra 	}
1628747f202SPeter Zijlstra 	target_state->enter_s2idle(dev, drv, index);
16349d9c593SPeter Zijlstra 	if (WARN_ON_ONCE(!irqs_disabled()))
164a01353cfSPeter Zijlstra 		raw_local_irq_disable();
16517cc2b55SPeter Zijlstra 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
16617cc2b55SPeter Zijlstra 		instrumentation_end();
167a01353cfSPeter Zijlstra 		ct_cpuidle_exit();
16817cc2b55SPeter Zijlstra 	}
1691098582aSPeter Zijlstra 	tick_unfreeze();
17063caae84SLucas Stach 	start_critical_timings();
17164bdff69SRafael J. Wysocki 
172e6a15fa9SPeter Zijlstra 	time_end = ns_to_ktime(local_clock_noinstr());
17364bdff69SRafael J. Wysocki 
17464bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
17564bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_usage++;
17617cc2b55SPeter Zijlstra 	instrumentation_end();
177124cf911SRafael J. Wysocki }
178124cf911SRafael J. Wysocki 
179a6220fc1SRafael J. Wysocki /**
18028ba086eSRafael J. Wysocki  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
181ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
182ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
18338106313SRafael J. Wysocki  *
18428ba086eSRafael J. Wysocki  * If there are states with the ->enter_s2idle callback, find the deepest of
185ef2b22acSRafael J. Wysocki  * them and enter it with frozen tick.
18638106313SRafael J. Wysocki  */
cpuidle_enter_s2idle(struct cpuidle_driver * drv,struct cpuidle_device * dev)18728ba086eSRafael J. Wysocki int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
18838106313SRafael J. Wysocki {
18938106313SRafael J. Wysocki 	int index;
19038106313SRafael J. Wysocki 
191124cf911SRafael J. Wysocki 	/*
19228ba086eSRafael J. Wysocki 	 * Find the deepest state with ->enter_s2idle present, which guarantees
193124cf911SRafael J. Wysocki 	 * that interrupts won't be enabled when it exits and allows the tick to
194124cf911SRafael J. Wysocki 	 * be frozen safely.
195124cf911SRafael J. Wysocki 	 */
196c1d51f68SRafael J. Wysocki 	index = find_deepest_state(drv, dev, U64_MAX, 0, true);
19710e8b11eSRafael J. Wysocki 	if (index > 0) {
19828ba086eSRafael J. Wysocki 		enter_s2idle_proper(drv, dev, index);
19910e8b11eSRafael J. Wysocki 		local_irq_enable();
20010e8b11eSRafael J. Wysocki 	}
201ef2b22acSRafael J. Wysocki 	return index;
20238106313SRafael J. Wysocki }
20387e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
20438106313SRafael J. Wysocki 
20538106313SRafael J. Wysocki /**
20656cfbf74SColin Cross  * cpuidle_enter_state - enter the state and update stats
20756cfbf74SColin Cross  * @dev: cpuidle device for this cpu
20856cfbf74SColin Cross  * @drv: cpuidle driver for this cpu
2097312280bSRafael J. Wysocki  * @index: index into the states table in @drv of the state to enter
21056cfbf74SColin Cross  */
cpuidle_enter_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)21117cc2b55SPeter Zijlstra noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
21217cc2b55SPeter Zijlstra 				 struct cpuidle_driver *drv,
213554c06baSDaniel Lezcano 				 int index)
21456cfbf74SColin Cross {
21556cfbf74SColin Cross 	int entered_state;
21656cfbf74SColin Cross 
217554c06baSDaniel Lezcano 	struct cpuidle_state *target_state = &drv->states[index];
218df8d9eeaSRafael J. Wysocki 	bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
219dbd1b8eaSShreyas B. Prabhu 	ktime_t time_start, time_end;
220554c06baSDaniel Lezcano 
22117cc2b55SPeter Zijlstra 	instrumentation_begin();
22217cc2b55SPeter Zijlstra 
223df8d9eeaSRafael J. Wysocki 	/*
224df8d9eeaSRafael J. Wysocki 	 * Tell the time framework to switch to a broadcast timer because our
225df8d9eeaSRafael J. Wysocki 	 * local timer will be shut down.  If a local timer is used from another
226df8d9eeaSRafael J. Wysocki 	 * CPU as a broadcast timer, this call may fail if it is not available.
227df8d9eeaSRafael J. Wysocki 	 */
228827a5aefSRafael J. Wysocki 	if (broadcast && tick_broadcast_enter()) {
229c1d51f68SRafael J. Wysocki 		index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
2300d94039fSRafael J. Wysocki 					   CPUIDLE_FLAG_TIMER_STOP, false);
231*6baacf93SDhruva Gole 
2320d94039fSRafael J. Wysocki 		target_state = &drv->states[index];
233f187851bSNicholas Piggin 		broadcast = false;
2340d94039fSRafael J. Wysocki 	}
235df8d9eeaSRafael J. Wysocki 
236bf9282dcSPeter Zijlstra 	if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
2377dbbc8f5SYosry Ahmed 		leave_mm();
238bf9282dcSPeter Zijlstra 
239faad3849SRafael J. Wysocki 	/* Take note of the planned idle state. */
240faad3849SRafael J. Wysocki 	sched_idle_set_state(target_state);
241faad3849SRafael J. Wysocki 
2421098582aSPeter Zijlstra 	trace_cpu_idle(index, dev->cpu);
243e6a15fa9SPeter Zijlstra 	time_start = ns_to_ktime(local_clock_noinstr());
244554c06baSDaniel Lezcano 
24563caae84SLucas Stach 	stop_critical_timings();
24617cc2b55SPeter Zijlstra 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
247a01353cfSPeter Zijlstra 		ct_cpuidle_enter();
24817cc2b55SPeter Zijlstra 		/* Annotate away the indirect call */
24917cc2b55SPeter Zijlstra 		instrumentation_begin();
25017cc2b55SPeter Zijlstra 	}
251bb7b1125SPeter Zijlstra 
2520e985e9dSPeter Zijlstra 	/*
2530e985e9dSPeter Zijlstra 	 * NOTE!!
2540e985e9dSPeter Zijlstra 	 *
2550e985e9dSPeter Zijlstra 	 * For cpuidle_state::enter() methods that do *NOT* set
2560e985e9dSPeter Zijlstra 	 * CPUIDLE_FLAG_RCU_IDLE RCU will be disabled here and these functions
2570e985e9dSPeter Zijlstra 	 * must be marked either noinstr or __cpuidle.
2580e985e9dSPeter Zijlstra 	 *
2590e985e9dSPeter Zijlstra 	 * For cpuidle_state::enter() methods that *DO* set
2600e985e9dSPeter Zijlstra 	 * CPUIDLE_FLAG_RCU_IDLE this isn't required, but they must mark the
2610e985e9dSPeter Zijlstra 	 * function calling ct_cpuidle_enter() as noinstr/__cpuidle and all
2620e985e9dSPeter Zijlstra 	 * functions called within the RCU-idle region.
2630e985e9dSPeter Zijlstra 	 */
264554c06baSDaniel Lezcano 	entered_state = target_state->enter(dev, drv, index);
26517cc2b55SPeter Zijlstra 
266bb7b1125SPeter Zijlstra 	if (WARN_ONCE(!irqs_disabled(), "%ps leaked IRQ state", target_state->enter))
267bb7b1125SPeter Zijlstra 		raw_local_irq_disable();
268bb7b1125SPeter Zijlstra 
26917cc2b55SPeter Zijlstra 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
27017cc2b55SPeter Zijlstra 		instrumentation_end();
271a01353cfSPeter Zijlstra 		ct_cpuidle_exit();
27217cc2b55SPeter Zijlstra 	}
27363caae84SLucas Stach 	start_critical_timings();
274554c06baSDaniel Lezcano 
275f9fccdb9SPeter Zijlstra 	sched_clock_idle_wakeup_event();
276e6a15fa9SPeter Zijlstra 	time_end = ns_to_ktime(local_clock_noinstr());
2771098582aSPeter Zijlstra 	trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
278554c06baSDaniel Lezcano 
279faad3849SRafael J. Wysocki 	/* The cpu is no longer idle or about to enter idle. */
280faad3849SRafael J. Wysocki 	sched_idle_set_state(NULL);
281faad3849SRafael J. Wysocki 
282bb7b1125SPeter Zijlstra 	if (broadcast)
283df8d9eeaSRafael J. Wysocki 		tick_broadcast_exit();
284df8d9eeaSRafael J. Wysocki 
285e7387da5SDaniel Lezcano 	if (!cpuidle_state_is_coupled(drv, index))
286554c06baSDaniel Lezcano 		local_irq_enable();
287554c06baSDaniel Lezcano 
2887037b43eSFieah Lim 	if (entered_state >= 0) {
289c1d51f68SRafael J. Wysocki 		s64 diff, delay = drv->states[entered_state].exit_latency_ns;
29004dab58aSRafael J. Wysocki 		int i;
29104dab58aSRafael J. Wysocki 
2927037b43eSFieah Lim 		/*
2937037b43eSFieah Lim 		 * Update cpuidle counters
2947037b43eSFieah Lim 		 * This can be moved to within driver enter routine,
2957037b43eSFieah Lim 		 * but that results in multiple copies of same code.
2967037b43eSFieah Lim 		 */
297c1d51f68SRafael J. Wysocki 		diff = ktime_sub(time_end, time_start);
298554c06baSDaniel Lezcano 
299c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = diff;
300c1d51f68SRafael J. Wysocki 		dev->states_usage[entered_state].time_ns += diff;
30156cfbf74SColin Cross 		dev->states_usage[entered_state].usage++;
30204dab58aSRafael J. Wysocki 
303c1d51f68SRafael J. Wysocki 		if (diff < drv->states[entered_state].target_residency_ns) {
30404dab58aSRafael J. Wysocki 			for (i = entered_state - 1; i >= 0; i--) {
30599e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
30604dab58aSRafael J. Wysocki 					continue;
30704dab58aSRafael J. Wysocki 
30804dab58aSRafael J. Wysocki 				/* Shallower states are enabled, so update. */
30904dab58aSRafael J. Wysocki 				dev->states_usage[entered_state].above++;
3106ab4b199SKajetan Puchalski 				trace_cpu_idle_miss(dev->cpu, entered_state, false);
31104dab58aSRafael J. Wysocki 				break;
31204dab58aSRafael J. Wysocki 			}
31304dab58aSRafael J. Wysocki 		} else if (diff > delay) {
31404dab58aSRafael J. Wysocki 			for (i = entered_state + 1; i < drv->state_count; i++) {
31599e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
31604dab58aSRafael J. Wysocki 					continue;
31704dab58aSRafael J. Wysocki 
31804dab58aSRafael J. Wysocki 				/*
31904dab58aSRafael J. Wysocki 				 * Update if a deeper state would have been a
32004dab58aSRafael J. Wysocki 				 * better match for the observed idle duration.
32104dab58aSRafael J. Wysocki 				 */
3226ab4b199SKajetan Puchalski 				if (diff - delay >= drv->states[i].target_residency_ns) {
32304dab58aSRafael J. Wysocki 					dev->states_usage[entered_state].below++;
3246ab4b199SKajetan Puchalski 					trace_cpu_idle_miss(dev->cpu, entered_state, true);
3256ab4b199SKajetan Puchalski 				}
32604dab58aSRafael J. Wysocki 
32704dab58aSRafael J. Wysocki 				break;
32804dab58aSRafael J. Wysocki 			}
32904dab58aSRafael J. Wysocki 		}
33056cfbf74SColin Cross 	} else {
331c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = 0;
332f49735f4SLina Iyer 		dev->states_usage[index].rejected++;
33356cfbf74SColin Cross 	}
33456cfbf74SColin Cross 
33517cc2b55SPeter Zijlstra 	instrumentation_end();
33617cc2b55SPeter Zijlstra 
33756cfbf74SColin Cross 	return entered_state;
33856cfbf74SColin Cross }
33956cfbf74SColin Cross 
34056cfbf74SColin Cross /**
341907e30f1SDaniel Lezcano  * cpuidle_select - ask the cpuidle framework to choose an idle state
3424f86d3a8SLen Brown  *
343907e30f1SDaniel Lezcano  * @drv: the cpuidle driver
344907e30f1SDaniel Lezcano  * @dev: the cpuidle device
34545f1ff59SRafael J. Wysocki  * @stop_tick: indication on whether or not to stop the tick
346907e30f1SDaniel Lezcano  *
34751164251SRafael J. Wysocki  * Returns the index of the idle state.  The return value must not be negative.
34845f1ff59SRafael J. Wysocki  *
34945f1ff59SRafael J. Wysocki  * The memory location pointed to by @stop_tick is expected to be written the
35045f1ff59SRafael J. Wysocki  * 'false' boolean value if the scheduler tick should not be stopped before
35145f1ff59SRafael J. Wysocki  * entering the returned state.
3524f86d3a8SLen Brown  */
cpuidle_select(struct cpuidle_driver * drv,struct cpuidle_device * dev,bool * stop_tick)35345f1ff59SRafael J. Wysocki int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
35445f1ff59SRafael J. Wysocki 		   bool *stop_tick)
3554f86d3a8SLen Brown {
35645f1ff59SRafael J. Wysocki 	return cpuidle_curr_governor->select(drv, dev, stop_tick);
357246eb7f0SKevin Hilman }
358246eb7f0SKevin Hilman 
359907e30f1SDaniel Lezcano /**
360907e30f1SDaniel Lezcano  * cpuidle_enter - enter into the specified idle state
361907e30f1SDaniel Lezcano  *
362907e30f1SDaniel Lezcano  * @drv:   the cpuidle driver tied with the cpu
363907e30f1SDaniel Lezcano  * @dev:   the cpuidle device
364907e30f1SDaniel Lezcano  * @index: the index in the idle state table
365907e30f1SDaniel Lezcano  *
366907e30f1SDaniel Lezcano  * Returns the index in the idle state, < 0 in case of error.
367907e30f1SDaniel Lezcano  * The error code depends on the backend driver
368907e30f1SDaniel Lezcano  */
cpuidle_enter(struct cpuidle_driver * drv,struct cpuidle_device * dev,int index)369907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
370907e30f1SDaniel Lezcano 		  int index)
371907e30f1SDaniel Lezcano {
3726f9b83acSUlf Hansson 	int ret = 0;
3736f9b83acSUlf Hansson 
3746f9b83acSUlf Hansson 	/*
3756f9b83acSUlf Hansson 	 * Store the next hrtimer, which becomes either next tick or the next
3766f9b83acSUlf Hansson 	 * timer event, whatever expires first. Additionally, to make this data
3776f9b83acSUlf Hansson 	 * useful for consumers outside cpuidle, we rely on that the governor's
3786f9b83acSUlf Hansson 	 * ->select() callback have decided, whether to stop the tick or not.
3796f9b83acSUlf Hansson 	 */
3806f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
3816f9b83acSUlf Hansson 
3824c1ed5a6SXunlei Pang 	if (cpuidle_state_is_coupled(drv, index))
3836f9b83acSUlf Hansson 		ret = cpuidle_enter_state_coupled(dev, drv, index);
3846f9b83acSUlf Hansson 	else
3856f9b83acSUlf Hansson 		ret = cpuidle_enter_state(dev, drv, index);
3866f9b83acSUlf Hansson 
3876f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, 0);
3886f9b83acSUlf Hansson 	return ret;
389907e30f1SDaniel Lezcano }
390fb11c9c6SViresh Kumar 
391907e30f1SDaniel Lezcano /**
392907e30f1SDaniel Lezcano  * cpuidle_reflect - tell the underlying governor what was the state
393907e30f1SDaniel Lezcano  * we were in
394907e30f1SDaniel Lezcano  *
395907e30f1SDaniel Lezcano  * @dev  : the cpuidle device
396907e30f1SDaniel Lezcano  * @index: the index in the idle state table
397907e30f1SDaniel Lezcano  *
398907e30f1SDaniel Lezcano  */
cpuidle_reflect(struct cpuidle_device * dev,int index)399907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index)
400907e30f1SDaniel Lezcano {
401a802ea96SRafael J. Wysocki 	if (cpuidle_curr_governor->reflect && index >= 0)
402907e30f1SDaniel Lezcano 		cpuidle_curr_governor->reflect(dev, index);
4034f86d3a8SLen Brown }
4044f86d3a8SLen Brown 
4057a25759eSMel Gorman /*
4067a25759eSMel Gorman  * Min polling interval of 10usec is a guess. It is assuming that
4077a25759eSMel Gorman  * for most users, the time for a single ping-pong workload like
4087a25759eSMel Gorman  * perf bench pipe would generally complete within 10usec but
4097a25759eSMel Gorman  * this is hardware dependant. Actual time can be estimated with
4107a25759eSMel Gorman  *
4117a25759eSMel Gorman  * perf bench sched pipe -l 10000
4127a25759eSMel Gorman  *
4137a25759eSMel Gorman  * Run multiple times to avoid cpufreq effects.
4147a25759eSMel Gorman  */
4157a25759eSMel Gorman #define CPUIDLE_POLL_MIN 10000
4167a25759eSMel Gorman #define CPUIDLE_POLL_MAX (TICK_NSEC / 16)
4177a25759eSMel Gorman 
4184f86d3a8SLen Brown /**
419259231a0SMarcelo Tosatti  * cpuidle_poll_time - return amount of time to poll for,
420259231a0SMarcelo Tosatti  * governors can override dev->poll_limit_ns if necessary
421259231a0SMarcelo Tosatti  *
422259231a0SMarcelo Tosatti  * @drv:   the cpuidle driver tied with the cpu
423259231a0SMarcelo Tosatti  * @dev:   the cpuidle device
424259231a0SMarcelo Tosatti  *
425259231a0SMarcelo Tosatti  */
cpuidle_poll_time(struct cpuidle_driver * drv,struct cpuidle_device * dev)4264d627628SPeter Zijlstra __cpuidle u64 cpuidle_poll_time(struct cpuidle_driver *drv,
427259231a0SMarcelo Tosatti 		      struct cpuidle_device *dev)
428259231a0SMarcelo Tosatti {
429259231a0SMarcelo Tosatti 	int i;
430259231a0SMarcelo Tosatti 	u64 limit_ns;
431259231a0SMarcelo Tosatti 
4327a25759eSMel Gorman 	BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX);
4337a25759eSMel Gorman 
434259231a0SMarcelo Tosatti 	if (dev->poll_limit_ns)
435259231a0SMarcelo Tosatti 		return dev->poll_limit_ns;
436259231a0SMarcelo Tosatti 
4377a25759eSMel Gorman 	limit_ns = CPUIDLE_POLL_MAX;
438259231a0SMarcelo Tosatti 	for (i = 1; i < drv->state_count; i++) {
4397a25759eSMel Gorman 		u64 state_limit;
4407a25759eSMel Gorman 
44199e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable)
442259231a0SMarcelo Tosatti 			continue;
443259231a0SMarcelo Tosatti 
4447a25759eSMel Gorman 		state_limit = drv->states[i].target_residency_ns;
4457a25759eSMel Gorman 		if (state_limit < CPUIDLE_POLL_MIN)
4467a25759eSMel Gorman 			continue;
4477a25759eSMel Gorman 
4487a25759eSMel Gorman 		limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX);
44936fcb429SMarcelo Tosatti 		break;
450259231a0SMarcelo Tosatti 	}
451259231a0SMarcelo Tosatti 
452259231a0SMarcelo Tosatti 	dev->poll_limit_ns = limit_ns;
453259231a0SMarcelo Tosatti 
454259231a0SMarcelo Tosatti 	return dev->poll_limit_ns;
455259231a0SMarcelo Tosatti }
456259231a0SMarcelo Tosatti 
457259231a0SMarcelo Tosatti /**
4584f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
4594f86d3a8SLen Brown  */
cpuidle_install_idle_handler(void)4604f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
4614f86d3a8SLen Brown {
462a0bfa137SLen Brown 	if (enabled_devices) {
4634f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
4644f86d3a8SLen Brown 		smp_wmb();
465a0bfa137SLen Brown 		initialized = 1;
4664f86d3a8SLen Brown 	}
4674f86d3a8SLen Brown }
4684f86d3a8SLen Brown 
4694f86d3a8SLen Brown /**
4704f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
4714f86d3a8SLen Brown  */
cpuidle_uninstall_idle_handler(void)4724f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
4734f86d3a8SLen Brown {
474a0bfa137SLen Brown 	if (enabled_devices) {
475a0bfa137SLen Brown 		initialized = 0;
4762ed903c5SChuansheng Liu 		wake_up_all_idle_cpus();
4774f86d3a8SLen Brown 	}
478442bf3aaSDaniel Lezcano 
479442bf3aaSDaniel Lezcano 	/*
480442bf3aaSDaniel Lezcano 	 * Make sure external observers (such as the scheduler)
481442bf3aaSDaniel Lezcano 	 * are done looking at pointed idle states.
482442bf3aaSDaniel Lezcano 	 */
483442bf3aaSDaniel Lezcano 	synchronize_rcu();
4844f86d3a8SLen Brown }
4854f86d3a8SLen Brown 
4864f86d3a8SLen Brown /**
4874f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
4884f86d3a8SLen Brown  */
cpuidle_pause_and_lock(void)4894f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
4904f86d3a8SLen Brown {
4914f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
4924f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
4934f86d3a8SLen Brown }
4944f86d3a8SLen Brown 
4954f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
4964f86d3a8SLen Brown 
4974f86d3a8SLen Brown /**
4984f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
4994f86d3a8SLen Brown  */
cpuidle_resume_and_unlock(void)5004f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
5014f86d3a8SLen Brown {
5024f86d3a8SLen Brown 	cpuidle_install_idle_handler();
5034f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
5044f86d3a8SLen Brown }
5054f86d3a8SLen Brown 
5064f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
5074f86d3a8SLen Brown 
5088651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
cpuidle_pause(void)5098651f97bSPreeti U Murthy void cpuidle_pause(void)
5108651f97bSPreeti U Murthy {
5118651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
5128651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
5138651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
5148651f97bSPreeti U Murthy }
5158651f97bSPreeti U Murthy 
5168651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
cpuidle_resume(void)5178651f97bSPreeti U Murthy void cpuidle_resume(void)
5188651f97bSPreeti U Murthy {
5198651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
5208651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
5218651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
5228651f97bSPreeti U Murthy }
5238651f97bSPreeti U Murthy 
5244f86d3a8SLen Brown /**
5254f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
5264f86d3a8SLen Brown  * @dev: the CPU
5274f86d3a8SLen Brown  *
5284f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
5294f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
5304f86d3a8SLen Brown  */
cpuidle_enable_device(struct cpuidle_device * dev)5314f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
5324f86d3a8SLen Brown {
5335df0aa73SDaniel Lezcano 	int ret;
534bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
5354f86d3a8SLen Brown 
5361b0a0e9aSSrivatsa S. Bhat 	if (!dev)
5371b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
5381b0a0e9aSSrivatsa S. Bhat 
5394f86d3a8SLen Brown 	if (dev->enabled)
5404f86d3a8SLen Brown 		return 0;
541bf4d1b5dSDaniel Lezcano 
542e7b06a09SGaurav Jindal 	if (!cpuidle_curr_governor)
543e7b06a09SGaurav Jindal 		return -EIO;
544e7b06a09SGaurav Jindal 
545bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
546bf4d1b5dSDaniel Lezcano 
547e7b06a09SGaurav Jindal 	if (!drv)
5484f86d3a8SLen Brown 		return -EIO;
549bf4d1b5dSDaniel Lezcano 
55010b9d3f8SDaniel Lezcano 	if (!dev->registered)
55110b9d3f8SDaniel Lezcano 		return -EINVAL;
55210b9d3f8SDaniel Lezcano 
553bf4d1b5dSDaniel Lezcano 	ret = cpuidle_add_device_sysfs(dev);
554bf4d1b5dSDaniel Lezcano 	if (ret)
5554f86d3a8SLen Brown 		return ret;
5564f86d3a8SLen Brown 
5573fc74bd8SGaurav Jindal 	if (cpuidle_curr_governor->enable) {
5583fc74bd8SGaurav Jindal 		ret = cpuidle_curr_governor->enable(drv, dev);
5593fc74bd8SGaurav Jindal 		if (ret)
5604f86d3a8SLen Brown 			goto fail_sysfs;
5613fc74bd8SGaurav Jindal 	}
5624f86d3a8SLen Brown 
5634f86d3a8SLen Brown 	smp_wmb();
5644f86d3a8SLen Brown 
5654f86d3a8SLen Brown 	dev->enabled = 1;
5664f86d3a8SLen Brown 
5674f86d3a8SLen Brown 	enabled_devices++;
5684f86d3a8SLen Brown 	return 0;
5694f86d3a8SLen Brown 
5704f86d3a8SLen Brown fail_sysfs:
571bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5724f86d3a8SLen Brown 
5734f86d3a8SLen Brown 	return ret;
5744f86d3a8SLen Brown }
5754f86d3a8SLen Brown 
5764f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
5774f86d3a8SLen Brown 
5784f86d3a8SLen Brown /**
5794f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
5804f86d3a8SLen Brown  * @dev: the CPU
5814f86d3a8SLen Brown  *
5824f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
5834f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
5844f86d3a8SLen Brown  */
cpuidle_disable_device(struct cpuidle_device * dev)5854f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
5864f86d3a8SLen Brown {
587bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
588bf4d1b5dSDaniel Lezcano 
589cf31cd1aSSrivatsa S. Bhat 	if (!dev || !dev->enabled)
5904f86d3a8SLen Brown 		return;
591bf4d1b5dSDaniel Lezcano 
592bf4d1b5dSDaniel Lezcano 	if (!drv || !cpuidle_curr_governor)
5934f86d3a8SLen Brown 		return;
5944f86d3a8SLen Brown 
5954f86d3a8SLen Brown 	dev->enabled = 0;
5964f86d3a8SLen Brown 
5974f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
598bf4d1b5dSDaniel Lezcano 		cpuidle_curr_governor->disable(drv, dev);
5994f86d3a8SLen Brown 
600bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
6014f86d3a8SLen Brown 	enabled_devices--;
6024f86d3a8SLen Brown }
6034f86d3a8SLen Brown 
6044f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
6054f86d3a8SLen Brown 
__cpuidle_unregister_device(struct cpuidle_device * dev)606f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev)
607f6bb51a5SDaniel Lezcano {
608f6bb51a5SDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
609f6bb51a5SDaniel Lezcano 
610f6bb51a5SDaniel Lezcano 	list_del(&dev->device_list);
611f6bb51a5SDaniel Lezcano 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
612f6bb51a5SDaniel Lezcano 	module_put(drv->owner);
613c998c078SDave Gerlach 
614c998c078SDave Gerlach 	dev->registered = 0;
615f6bb51a5SDaniel Lezcano }
616f6bb51a5SDaniel Lezcano 
__cpuidle_device_init(struct cpuidle_device * dev)617267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev)
6185df0aa73SDaniel Lezcano {
6195df0aa73SDaniel Lezcano 	memset(dev->states_usage, 0, sizeof(dev->states_usage));
620c1d51f68SRafael J. Wysocki 	dev->last_residency_ns = 0;
6216f9b83acSUlf Hansson 	dev->next_hrtimer = 0;
6225df0aa73SDaniel Lezcano }
6235df0aa73SDaniel Lezcano 
6244f86d3a8SLen Brown /**
625dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
626dcb84f33SVenkatesh Pallipadi  * and enable routines
6274f86d3a8SLen Brown  * @dev: the cpu
628dcb84f33SVenkatesh Pallipadi  *
629dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
6304f86d3a8SLen Brown  */
__cpuidle_register_device(struct cpuidle_device * dev)631dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
6324f86d3a8SLen Brown {
633bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
63499e98d3fSRafael J. Wysocki 	int i, ret;
6354f86d3a8SLen Brown 
636bf4d1b5dSDaniel Lezcano 	if (!try_module_get(drv->owner))
6374f86d3a8SLen Brown 		return -EINVAL;
6384f86d3a8SLen Brown 
63975a80267SRafael J. Wysocki 	for (i = 0; i < drv->state_count; i++) {
640ba1e78a1SRafael J. Wysocki 		if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
64199e98d3fSRafael J. Wysocki 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
64299e98d3fSRafael J. Wysocki 
64375a80267SRafael J. Wysocki 		if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
64475a80267SRafael J. Wysocki 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
64575a80267SRafael J. Wysocki 	}
64675a80267SRafael J. Wysocki 
6474f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
6484f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
6494f86d3a8SLen Brown 
6504126c019SColin Cross 	ret = cpuidle_coupled_register_device(dev);
65147182668SViresh Kumar 	if (ret)
652f6bb51a5SDaniel Lezcano 		__cpuidle_unregister_device(dev);
65347182668SViresh Kumar 	else
654dcb84f33SVenkatesh Pallipadi 		dev->registered = 1;
65547182668SViresh Kumar 
65647182668SViresh Kumar 	return ret;
657dcb84f33SVenkatesh Pallipadi }
658dcb84f33SVenkatesh Pallipadi 
659dcb84f33SVenkatesh Pallipadi /**
660dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
661dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
662dcb84f33SVenkatesh Pallipadi  */
cpuidle_register_device(struct cpuidle_device * dev)663dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
664dcb84f33SVenkatesh Pallipadi {
665c878a52dSDaniel Lezcano 	int ret = -EBUSY;
666dcb84f33SVenkatesh Pallipadi 
6671b0a0e9aSSrivatsa S. Bhat 	if (!dev)
6681b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
6691b0a0e9aSSrivatsa S. Bhat 
670dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
671dcb84f33SVenkatesh Pallipadi 
672c878a52dSDaniel Lezcano 	if (dev->registered)
673c878a52dSDaniel Lezcano 		goto out_unlock;
674c878a52dSDaniel Lezcano 
675267d4bf8SViresh Kumar 	__cpuidle_device_init(dev);
6765df0aa73SDaniel Lezcano 
677f6bb51a5SDaniel Lezcano 	ret = __cpuidle_register_device(dev);
678f6bb51a5SDaniel Lezcano 	if (ret)
679f6bb51a5SDaniel Lezcano 		goto out_unlock;
680f6bb51a5SDaniel Lezcano 
681f6bb51a5SDaniel Lezcano 	ret = cpuidle_add_sysfs(dev);
682f6bb51a5SDaniel Lezcano 	if (ret)
683f6bb51a5SDaniel Lezcano 		goto out_unregister;
684dcb84f33SVenkatesh Pallipadi 
68510b9d3f8SDaniel Lezcano 	ret = cpuidle_enable_device(dev);
686f6bb51a5SDaniel Lezcano 	if (ret)
687f6bb51a5SDaniel Lezcano 		goto out_sysfs;
68810b9d3f8SDaniel Lezcano 
6894f86d3a8SLen Brown 	cpuidle_install_idle_handler();
6904f86d3a8SLen Brown 
691f6bb51a5SDaniel Lezcano out_unlock:
6924f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
6934f86d3a8SLen Brown 
694f6bb51a5SDaniel Lezcano 	return ret;
695f6bb51a5SDaniel Lezcano 
696f6bb51a5SDaniel Lezcano out_sysfs:
697f6bb51a5SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
698f6bb51a5SDaniel Lezcano out_unregister:
699f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
700f6bb51a5SDaniel Lezcano 	goto out_unlock;
7014f86d3a8SLen Brown }
7024f86d3a8SLen Brown 
7034f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
7044f86d3a8SLen Brown 
7054f86d3a8SLen Brown /**
7064f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
7074f86d3a8SLen Brown  * @dev: the cpu
7084f86d3a8SLen Brown  */
cpuidle_unregister_device(struct cpuidle_device * dev)7094f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
7104f86d3a8SLen Brown {
711813e8e3dSKonrad Rzeszutek Wilk 	if (!dev || dev->registered == 0)
712dcb84f33SVenkatesh Pallipadi 		return;
713dcb84f33SVenkatesh Pallipadi 
7144f86d3a8SLen Brown 	cpuidle_pause_and_lock();
7154f86d3a8SLen Brown 
7164f86d3a8SLen Brown 	cpuidle_disable_device(dev);
7174f86d3a8SLen Brown 
7181aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
719f6bb51a5SDaniel Lezcano 
720f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
7214f86d3a8SLen Brown 
7224126c019SColin Cross 	cpuidle_coupled_unregister_device(dev);
7234126c019SColin Cross 
7244f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
7254f86d3a8SLen Brown }
7264f86d3a8SLen Brown 
7274f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
7284f86d3a8SLen Brown 
7291c192d04SDaniel Lezcano /**
7304c637b21SDaniel Lezcano  * cpuidle_unregister: unregister a driver and the devices. This function
7314c637b21SDaniel Lezcano  * can be used only if the driver has been previously registered through
7324c637b21SDaniel Lezcano  * the cpuidle_register function.
7334c637b21SDaniel Lezcano  *
7344c637b21SDaniel Lezcano  * @drv: a valid pointer to a struct cpuidle_driver
7354c637b21SDaniel Lezcano  */
cpuidle_unregister(struct cpuidle_driver * drv)7364c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv)
7374c637b21SDaniel Lezcano {
7384c637b21SDaniel Lezcano 	int cpu;
7394c637b21SDaniel Lezcano 	struct cpuidle_device *device;
7404c637b21SDaniel Lezcano 
74182467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
7424c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
7434c637b21SDaniel Lezcano 		cpuidle_unregister_device(device);
7444c637b21SDaniel Lezcano 	}
7454c637b21SDaniel Lezcano 
7464c637b21SDaniel Lezcano 	cpuidle_unregister_driver(drv);
7474c637b21SDaniel Lezcano }
7484c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister);
7494c637b21SDaniel Lezcano 
7504c637b21SDaniel Lezcano /**
7514c637b21SDaniel Lezcano  * cpuidle_register: registers the driver and the cpu devices with the
7524c637b21SDaniel Lezcano  * coupled_cpus passed as parameter. This function is used for all common
7534c637b21SDaniel Lezcano  * initialization pattern there are in the arch specific drivers. The
7544c637b21SDaniel Lezcano  * devices is globally defined in this file.
7554c637b21SDaniel Lezcano  *
7564c637b21SDaniel Lezcano  * @drv         : a valid pointer to a struct cpuidle_driver
7574c637b21SDaniel Lezcano  * @coupled_cpus: a cpumask for the coupled states
7584c637b21SDaniel Lezcano  *
7594c637b21SDaniel Lezcano  * Returns 0 on success, < 0 otherwise
7604c637b21SDaniel Lezcano  */
cpuidle_register(struct cpuidle_driver * drv,const struct cpumask * const coupled_cpus)7614c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv,
7624c637b21SDaniel Lezcano 		     const struct cpumask *const coupled_cpus)
7634c637b21SDaniel Lezcano {
7644c637b21SDaniel Lezcano 	int ret, cpu;
7654c637b21SDaniel Lezcano 	struct cpuidle_device *device;
7664c637b21SDaniel Lezcano 
7674c637b21SDaniel Lezcano 	ret = cpuidle_register_driver(drv);
7684c637b21SDaniel Lezcano 	if (ret) {
7694c637b21SDaniel Lezcano 		pr_err("failed to register cpuidle driver\n");
7704c637b21SDaniel Lezcano 		return ret;
7714c637b21SDaniel Lezcano 	}
7724c637b21SDaniel Lezcano 
77382467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
7744c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
7754c637b21SDaniel Lezcano 		device->cpu = cpu;
7764c637b21SDaniel Lezcano 
7774c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
7784c637b21SDaniel Lezcano 		/*
779caf4a36eSViresh Kumar 		 * On multiplatform for ARM, the coupled idle states could be
7804c637b21SDaniel Lezcano 		 * enabled in the kernel even if the cpuidle driver does not
7814c637b21SDaniel Lezcano 		 * use it. Note, coupled_cpus is a struct copy.
7824c637b21SDaniel Lezcano 		 */
7834c637b21SDaniel Lezcano 		if (coupled_cpus)
7844c637b21SDaniel Lezcano 			device->coupled_cpus = *coupled_cpus;
7854c637b21SDaniel Lezcano #endif
7864c637b21SDaniel Lezcano 		ret = cpuidle_register_device(device);
7874c637b21SDaniel Lezcano 		if (!ret)
7884c637b21SDaniel Lezcano 			continue;
7894c637b21SDaniel Lezcano 
7904c637b21SDaniel Lezcano 		pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
7914c637b21SDaniel Lezcano 
7924c637b21SDaniel Lezcano 		cpuidle_unregister(drv);
7934c637b21SDaniel Lezcano 		break;
7944c637b21SDaniel Lezcano 	}
7954c637b21SDaniel Lezcano 
7964c637b21SDaniel Lezcano 	return ret;
7974c637b21SDaniel Lezcano }
7984c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register);
7994c637b21SDaniel Lezcano 
8004f86d3a8SLen Brown /**
8014f86d3a8SLen Brown  * cpuidle_init - core initializer
8024f86d3a8SLen Brown  */
cpuidle_init(void)8034f86d3a8SLen Brown static int __init cpuidle_init(void)
8044f86d3a8SLen Brown {
80562027aeaSLen Brown 	if (cpuidle_disabled())
80662027aeaSLen Brown 		return -ENODEV;
80762027aeaSLen Brown 
808bf6479dbSGreg Kroah-Hartman 	return cpuidle_add_interface();
8094f86d3a8SLen Brown }
8104f86d3a8SLen Brown 
81162027aeaSLen Brown module_param(off, int, 0444);
81261cb5758SRafael J. Wysocki module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
8134f86d3a8SLen Brown core_initcall(cpuidle_init);
814