xref: /linux/drivers/cpuidle/cpuidle.c (revision 5aa9ba6312e36c18626e73506b92d1513d815435)
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>
15e6017571SIngo Molnar #include <linux/sched/clock.h>
164f86d3a8SLen Brown #include <linux/notifier.h>
17e8db0be1SJean Pihet #include <linux/pm_qos.h>
184f86d3a8SLen Brown #include <linux/cpu.h>
194f86d3a8SLen Brown #include <linux/cpuidle.h>
209a0b8415Svenkatesh.pallipadi@intel.com #include <linux/ktime.h>
212e94d1f7SArjan van de Ven #include <linux/hrtimer.h>
22884b17e1SPaul Gortmaker #include <linux/module.h>
2338106313SRafael J. Wysocki #include <linux/suspend.h>
24124cf911SRafael J. Wysocki #include <linux/tick.h>
25288f023eSArjan van de Ven #include <trace/events/power.h>
264f86d3a8SLen Brown 
274f86d3a8SLen Brown #include "cpuidle.h"
284f86d3a8SLen Brown 
294f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
304c637b21SDaniel Lezcano DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
314f86d3a8SLen Brown 
324f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock);
334f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices);
344f86d3a8SLen Brown 
354f86d3a8SLen Brown static int enabled_devices;
3662027aeaSLen Brown static int off __read_mostly;
37a0bfa137SLen Brown static int initialized __read_mostly;
3862027aeaSLen Brown 
3962027aeaSLen Brown int cpuidle_disabled(void)
4062027aeaSLen Brown {
4162027aeaSLen Brown 	return off;
4262027aeaSLen Brown }
43d91ee586SLen Brown void disable_cpuidle(void)
44d91ee586SLen Brown {
45d91ee586SLen Brown 	off = 1;
46d91ee586SLen Brown }
474f86d3a8SLen Brown 
48ef2b22acSRafael J. Wysocki bool cpuidle_not_available(struct cpuidle_driver *drv,
4931a34090SRafael J. Wysocki 			   struct cpuidle_device *dev)
5031a34090SRafael J. Wysocki {
5131a34090SRafael J. Wysocki 	return off || !initialized || !drv || !dev || !dev->enabled;
5231a34090SRafael J. Wysocki }
5331a34090SRafael J. Wysocki 
544f86d3a8SLen Brown /**
551a022e3fSBoris Ostrovsky  * cpuidle_play_dead - cpu off-lining
561a022e3fSBoris Ostrovsky  *
57ee01e663SToshi Kani  * Returns in case of an error or no driver
581a022e3fSBoris Ostrovsky  */
591a022e3fSBoris Ostrovsky int cpuidle_play_dead(void)
601a022e3fSBoris Ostrovsky {
611a022e3fSBoris Ostrovsky 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
62bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
638aef33a7SDaniel Lezcano 	int i;
641a022e3fSBoris Ostrovsky 
65ee01e663SToshi Kani 	if (!drv)
66ee01e663SToshi Kani 		return -ENODEV;
67ee01e663SToshi Kani 
681a022e3fSBoris Ostrovsky 	/* Find lowest-power state that supports long-term idle */
697d51d979SRafael J. Wysocki 	for (i = drv->state_count - 1; i >= 0; i--)
708aef33a7SDaniel Lezcano 		if (drv->states[i].enter_dead)
718aef33a7SDaniel Lezcano 			return drv->states[i].enter_dead(dev, i);
721a022e3fSBoris Ostrovsky 
731a022e3fSBoris Ostrovsky 	return -ENODEV;
741a022e3fSBoris Ostrovsky }
751a022e3fSBoris Ostrovsky 
76ef2b22acSRafael J. Wysocki static int find_deepest_state(struct cpuidle_driver *drv,
770d94039fSRafael J. Wysocki 			      struct cpuidle_device *dev,
78c1d51f68SRafael J. Wysocki 			      u64 max_latency_ns,
790d94039fSRafael J. Wysocki 			      unsigned int forbidden_flags,
8028ba086eSRafael J. Wysocki 			      bool s2idle)
81a6220fc1SRafael J. Wysocki {
82c1d51f68SRafael J. Wysocki 	u64 latency_req = 0;
8351164251SRafael J. Wysocki 	int i, ret = 0;
84a6220fc1SRafael J. Wysocki 
8551164251SRafael J. Wysocki 	for (i = 1; i < drv->state_count; i++) {
86a6220fc1SRafael J. Wysocki 		struct cpuidle_state *s = &drv->states[i];
87a6220fc1SRafael J. Wysocki 
8899e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable ||
89c1d51f68SRafael J. Wysocki 		    s->exit_latency_ns <= latency_req ||
90c1d51f68SRafael J. Wysocki 		    s->exit_latency_ns > max_latency_ns ||
9199e98d3fSRafael J. Wysocki 		    (s->flags & forbidden_flags) ||
9299e98d3fSRafael J. Wysocki 		    (s2idle && !s->enter_s2idle))
93a6220fc1SRafael J. Wysocki 			continue;
94a6220fc1SRafael J. Wysocki 
95c1d51f68SRafael J. Wysocki 		latency_req = s->exit_latency_ns;
96a6220fc1SRafael J. Wysocki 		ret = i;
97a6220fc1SRafael J. Wysocki 	}
98a6220fc1SRafael J. Wysocki 	return ret;
99a6220fc1SRafael J. Wysocki }
100a6220fc1SRafael J. Wysocki 
1010e7414b7SRafael J. Wysocki /**
102c55b51a0SDaniel Lezcano  * cpuidle_use_deepest_state - Set/unset governor override mode.
103c55b51a0SDaniel Lezcano  * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
1040e7414b7SRafael J. Wysocki  *
105c55b51a0SDaniel Lezcano  * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
106c55b51a0SDaniel Lezcano  * state with exit latency within @latency_limit_ns (override governors going
107c55b51a0SDaniel Lezcano  * forward), or do not override governors if it is zero.
1080e7414b7SRafael J. Wysocki  */
109c55b51a0SDaniel Lezcano void cpuidle_use_deepest_state(u64 latency_limit_ns)
110bb8313b6SJacob Pan {
111bb8313b6SJacob Pan 	struct cpuidle_device *dev;
112bb8313b6SJacob Pan 
113bb8313b6SJacob Pan 	preempt_disable();
114bb8313b6SJacob Pan 	dev = cpuidle_get_device();
11541dc750eSLi, Fei 	if (dev)
116c55b51a0SDaniel Lezcano 		dev->forced_idle_latency_limit_ns = latency_limit_ns;
117bb8313b6SJacob Pan 	preempt_enable();
118bb8313b6SJacob Pan }
119bb8313b6SJacob Pan 
120ef2b22acSRafael J. Wysocki /**
121ef2b22acSRafael J. Wysocki  * cpuidle_find_deepest_state - Find the deepest available idle state.
122ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
123ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
124ef2b22acSRafael J. Wysocki  */
125ef2b22acSRafael J. Wysocki int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
126*5aa9ba63SDaniel Lezcano 			       struct cpuidle_device *dev,
127*5aa9ba63SDaniel Lezcano 			       u64 latency_limit_ns)
128ef2b22acSRafael J. Wysocki {
129*5aa9ba63SDaniel Lezcano 	return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
130ef2b22acSRafael J. Wysocki }
131ef2b22acSRafael J. Wysocki 
132bb8313b6SJacob Pan #ifdef CONFIG_SUSPEND
13328ba086eSRafael J. Wysocki static void enter_s2idle_proper(struct cpuidle_driver *drv,
134124cf911SRafael J. Wysocki 				struct cpuidle_device *dev, int index)
135124cf911SRafael J. Wysocki {
13664bdff69SRafael J. Wysocki 	ktime_t time_start, time_end;
13764bdff69SRafael J. Wysocki 
13864bdff69SRafael J. Wysocki 	time_start = ns_to_ktime(local_clock());
13964bdff69SRafael J. Wysocki 
140ae0afb4fSRafael J. Wysocki 	/*
141ae0afb4fSRafael J. Wysocki 	 * trace_suspend_resume() called by tick_freeze() for the last CPU
142ae0afb4fSRafael J. Wysocki 	 * executing it contains RCU usage regarded as invalid in the idle
143ae0afb4fSRafael J. Wysocki 	 * context, so tell RCU about that.
144ae0afb4fSRafael J. Wysocki 	 */
145ae0afb4fSRafael J. Wysocki 	RCU_NONIDLE(tick_freeze());
146124cf911SRafael J. Wysocki 	/*
147124cf911SRafael J. Wysocki 	 * The state used here cannot be a "coupled" one, because the "coupled"
148124cf911SRafael J. Wysocki 	 * cpuidle mechanism enables interrupts and doing that with timekeeping
149124cf911SRafael J. Wysocki 	 * suspended is generally unsafe.
150124cf911SRafael J. Wysocki 	 */
15163caae84SLucas Stach 	stop_critical_timings();
15228ba086eSRafael J. Wysocki 	drv->states[index].enter_s2idle(dev, drv, index);
153124cf911SRafael J. Wysocki 	WARN_ON(!irqs_disabled());
154124cf911SRafael J. Wysocki 	/*
155124cf911SRafael J. Wysocki 	 * timekeeping_resume() that will be called by tick_unfreeze() for the
156ae0afb4fSRafael J. Wysocki 	 * first CPU executing it calls functions containing RCU read-side
157124cf911SRafael J. Wysocki 	 * critical sections, so tell RCU about that.
158124cf911SRafael J. Wysocki 	 */
159124cf911SRafael J. Wysocki 	RCU_NONIDLE(tick_unfreeze());
16063caae84SLucas Stach 	start_critical_timings();
16164bdff69SRafael J. Wysocki 
16264bdff69SRafael J. Wysocki 	time_end = ns_to_ktime(local_clock());
16364bdff69SRafael J. Wysocki 
16464bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
16564bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_usage++;
166124cf911SRafael J. Wysocki }
167124cf911SRafael J. Wysocki 
168a6220fc1SRafael J. Wysocki /**
16928ba086eSRafael J. Wysocki  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
170ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
171ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
17238106313SRafael J. Wysocki  *
17328ba086eSRafael J. Wysocki  * If there are states with the ->enter_s2idle callback, find the deepest of
174ef2b22acSRafael J. Wysocki  * them and enter it with frozen tick.
17538106313SRafael J. Wysocki  */
17628ba086eSRafael J. Wysocki int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
17738106313SRafael J. Wysocki {
17838106313SRafael J. Wysocki 	int index;
17938106313SRafael J. Wysocki 
180124cf911SRafael J. Wysocki 	/*
18128ba086eSRafael J. Wysocki 	 * Find the deepest state with ->enter_s2idle present, which guarantees
182124cf911SRafael J. Wysocki 	 * that interrupts won't be enabled when it exits and allows the tick to
183124cf911SRafael J. Wysocki 	 * be frozen safely.
184124cf911SRafael J. Wysocki 	 */
185c1d51f68SRafael J. Wysocki 	index = find_deepest_state(drv, dev, U64_MAX, 0, true);
1866f16886bSSudeep Holla 	if (index > 0)
18728ba086eSRafael J. Wysocki 		enter_s2idle_proper(drv, dev, index);
188124cf911SRafael J. Wysocki 
189ef2b22acSRafael J. Wysocki 	return index;
19038106313SRafael J. Wysocki }
19187e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
19238106313SRafael J. Wysocki 
19338106313SRafael J. Wysocki /**
19456cfbf74SColin Cross  * cpuidle_enter_state - enter the state and update stats
19556cfbf74SColin Cross  * @dev: cpuidle device for this cpu
19656cfbf74SColin Cross  * @drv: cpuidle driver for this cpu
1977312280bSRafael J. Wysocki  * @index: index into the states table in @drv of the state to enter
19856cfbf74SColin Cross  */
19956cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
200554c06baSDaniel Lezcano 			int index)
20156cfbf74SColin Cross {
20256cfbf74SColin Cross 	int entered_state;
20356cfbf74SColin Cross 
204554c06baSDaniel Lezcano 	struct cpuidle_state *target_state = &drv->states[index];
205df8d9eeaSRafael J. Wysocki 	bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
206dbd1b8eaSShreyas B. Prabhu 	ktime_t time_start, time_end;
207554c06baSDaniel Lezcano 
208df8d9eeaSRafael J. Wysocki 	/*
209df8d9eeaSRafael J. Wysocki 	 * Tell the time framework to switch to a broadcast timer because our
210df8d9eeaSRafael J. Wysocki 	 * local timer will be shut down.  If a local timer is used from another
211df8d9eeaSRafael J. Wysocki 	 * CPU as a broadcast timer, this call may fail if it is not available.
212df8d9eeaSRafael J. Wysocki 	 */
213827a5aefSRafael J. Wysocki 	if (broadcast && tick_broadcast_enter()) {
214c1d51f68SRafael J. Wysocki 		index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
2150d94039fSRafael J. Wysocki 					   CPUIDLE_FLAG_TIMER_STOP, false);
2160d94039fSRafael J. Wysocki 		if (index < 0) {
217827a5aefSRafael J. Wysocki 			default_idle_call();
218df8d9eeaSRafael J. Wysocki 			return -EBUSY;
219827a5aefSRafael J. Wysocki 		}
2200d94039fSRafael J. Wysocki 		target_state = &drv->states[index];
221f187851bSNicholas Piggin 		broadcast = false;
2220d94039fSRafael J. Wysocki 	}
223df8d9eeaSRafael J. Wysocki 
224faad3849SRafael J. Wysocki 	/* Take note of the planned idle state. */
225faad3849SRafael J. Wysocki 	sched_idle_set_state(target_state);
226faad3849SRafael J. Wysocki 
22730fe6884SSandeep Tripathy 	trace_cpu_idle_rcuidle(index, dev->cpu);
228dbd1b8eaSShreyas B. Prabhu 	time_start = ns_to_ktime(local_clock());
229554c06baSDaniel Lezcano 
23063caae84SLucas Stach 	stop_critical_timings();
231554c06baSDaniel Lezcano 	entered_state = target_state->enter(dev, drv, index);
23263caae84SLucas Stach 	start_critical_timings();
233554c06baSDaniel Lezcano 
234f9fccdb9SPeter Zijlstra 	sched_clock_idle_wakeup_event();
235dbd1b8eaSShreyas B. Prabhu 	time_end = ns_to_ktime(local_clock());
23630fe6884SSandeep Tripathy 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
237554c06baSDaniel Lezcano 
238faad3849SRafael J. Wysocki 	/* The cpu is no longer idle or about to enter idle. */
239faad3849SRafael J. Wysocki 	sched_idle_set_state(NULL);
240faad3849SRafael J. Wysocki 
241df8d9eeaSRafael J. Wysocki 	if (broadcast) {
242df8d9eeaSRafael J. Wysocki 		if (WARN_ON_ONCE(!irqs_disabled()))
243df8d9eeaSRafael J. Wysocki 			local_irq_disable();
244df8d9eeaSRafael J. Wysocki 
245df8d9eeaSRafael J. Wysocki 		tick_broadcast_exit();
246df8d9eeaSRafael J. Wysocki 	}
247df8d9eeaSRafael J. Wysocki 
248e7387da5SDaniel Lezcano 	if (!cpuidle_state_is_coupled(drv, index))
249554c06baSDaniel Lezcano 		local_irq_enable();
250554c06baSDaniel Lezcano 
2517037b43eSFieah Lim 	if (entered_state >= 0) {
252c1d51f68SRafael J. Wysocki 		s64 diff, delay = drv->states[entered_state].exit_latency_ns;
25304dab58aSRafael J. Wysocki 		int i;
25404dab58aSRafael J. Wysocki 
2557037b43eSFieah Lim 		/*
2567037b43eSFieah Lim 		 * Update cpuidle counters
2577037b43eSFieah Lim 		 * This can be moved to within driver enter routine,
2587037b43eSFieah Lim 		 * but that results in multiple copies of same code.
2597037b43eSFieah Lim 		 */
260c1d51f68SRafael J. Wysocki 		diff = ktime_sub(time_end, time_start);
261554c06baSDaniel Lezcano 
262c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = diff;
263c1d51f68SRafael J. Wysocki 		dev->states_usage[entered_state].time_ns += diff;
26456cfbf74SColin Cross 		dev->states_usage[entered_state].usage++;
26504dab58aSRafael J. Wysocki 
266c1d51f68SRafael J. Wysocki 		if (diff < drv->states[entered_state].target_residency_ns) {
26704dab58aSRafael J. Wysocki 			for (i = entered_state - 1; i >= 0; i--) {
26899e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
26904dab58aSRafael J. Wysocki 					continue;
27004dab58aSRafael J. Wysocki 
27104dab58aSRafael J. Wysocki 				/* Shallower states are enabled, so update. */
27204dab58aSRafael J. Wysocki 				dev->states_usage[entered_state].above++;
27304dab58aSRafael J. Wysocki 				break;
27404dab58aSRafael J. Wysocki 			}
27504dab58aSRafael J. Wysocki 		} else if (diff > delay) {
27604dab58aSRafael J. Wysocki 			for (i = entered_state + 1; i < drv->state_count; i++) {
27799e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
27804dab58aSRafael J. Wysocki 					continue;
27904dab58aSRafael J. Wysocki 
28004dab58aSRafael J. Wysocki 				/*
28104dab58aSRafael J. Wysocki 				 * Update if a deeper state would have been a
28204dab58aSRafael J. Wysocki 				 * better match for the observed idle duration.
28304dab58aSRafael J. Wysocki 				 */
284c1d51f68SRafael J. Wysocki 				if (diff - delay >= drv->states[i].target_residency_ns)
28504dab58aSRafael J. Wysocki 					dev->states_usage[entered_state].below++;
28604dab58aSRafael J. Wysocki 
28704dab58aSRafael J. Wysocki 				break;
28804dab58aSRafael J. Wysocki 			}
28904dab58aSRafael J. Wysocki 		}
29056cfbf74SColin Cross 	} else {
291c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = 0;
29256cfbf74SColin Cross 	}
29356cfbf74SColin Cross 
29456cfbf74SColin Cross 	return entered_state;
29556cfbf74SColin Cross }
29656cfbf74SColin Cross 
29756cfbf74SColin Cross /**
298907e30f1SDaniel Lezcano  * cpuidle_select - ask the cpuidle framework to choose an idle state
2994f86d3a8SLen Brown  *
300907e30f1SDaniel Lezcano  * @drv: the cpuidle driver
301907e30f1SDaniel Lezcano  * @dev: the cpuidle device
30245f1ff59SRafael J. Wysocki  * @stop_tick: indication on whether or not to stop the tick
303907e30f1SDaniel Lezcano  *
30451164251SRafael J. Wysocki  * Returns the index of the idle state.  The return value must not be negative.
30545f1ff59SRafael J. Wysocki  *
30645f1ff59SRafael J. Wysocki  * The memory location pointed to by @stop_tick is expected to be written the
30745f1ff59SRafael J. Wysocki  * 'false' boolean value if the scheduler tick should not be stopped before
30845f1ff59SRafael J. Wysocki  * entering the returned state.
3094f86d3a8SLen Brown  */
31045f1ff59SRafael J. Wysocki int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
31145f1ff59SRafael J. Wysocki 		   bool *stop_tick)
3124f86d3a8SLen Brown {
31345f1ff59SRafael J. Wysocki 	return cpuidle_curr_governor->select(drv, dev, stop_tick);
314246eb7f0SKevin Hilman }
315246eb7f0SKevin Hilman 
316907e30f1SDaniel Lezcano /**
317907e30f1SDaniel Lezcano  * cpuidle_enter - enter into the specified idle state
318907e30f1SDaniel Lezcano  *
319907e30f1SDaniel Lezcano  * @drv:   the cpuidle driver tied with the cpu
320907e30f1SDaniel Lezcano  * @dev:   the cpuidle device
321907e30f1SDaniel Lezcano  * @index: the index in the idle state table
322907e30f1SDaniel Lezcano  *
323907e30f1SDaniel Lezcano  * Returns the index in the idle state, < 0 in case of error.
324907e30f1SDaniel Lezcano  * The error code depends on the backend driver
325907e30f1SDaniel Lezcano  */
326907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
327907e30f1SDaniel Lezcano 		  int index)
328907e30f1SDaniel Lezcano {
3296f9b83acSUlf Hansson 	int ret = 0;
3306f9b83acSUlf Hansson 
3316f9b83acSUlf Hansson 	/*
3326f9b83acSUlf Hansson 	 * Store the next hrtimer, which becomes either next tick or the next
3336f9b83acSUlf Hansson 	 * timer event, whatever expires first. Additionally, to make this data
3346f9b83acSUlf Hansson 	 * useful for consumers outside cpuidle, we rely on that the governor's
3356f9b83acSUlf Hansson 	 * ->select() callback have decided, whether to stop the tick or not.
3366f9b83acSUlf Hansson 	 */
3376f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
3386f9b83acSUlf Hansson 
3394c1ed5a6SXunlei Pang 	if (cpuidle_state_is_coupled(drv, index))
3406f9b83acSUlf Hansson 		ret = cpuidle_enter_state_coupled(dev, drv, index);
3416f9b83acSUlf Hansson 	else
3426f9b83acSUlf Hansson 		ret = cpuidle_enter_state(dev, drv, index);
3436f9b83acSUlf Hansson 
3446f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, 0);
3456f9b83acSUlf Hansson 	return ret;
346907e30f1SDaniel Lezcano }
347fb11c9c6SViresh Kumar 
348907e30f1SDaniel Lezcano /**
349907e30f1SDaniel Lezcano  * cpuidle_reflect - tell the underlying governor what was the state
350907e30f1SDaniel Lezcano  * we were in
351907e30f1SDaniel Lezcano  *
352907e30f1SDaniel Lezcano  * @dev  : the cpuidle device
353907e30f1SDaniel Lezcano  * @index: the index in the idle state table
354907e30f1SDaniel Lezcano  *
355907e30f1SDaniel Lezcano  */
356907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index)
357907e30f1SDaniel Lezcano {
358a802ea96SRafael J. Wysocki 	if (cpuidle_curr_governor->reflect && index >= 0)
359907e30f1SDaniel Lezcano 		cpuidle_curr_governor->reflect(dev, index);
3604f86d3a8SLen Brown }
3614f86d3a8SLen Brown 
3624f86d3a8SLen Brown /**
363259231a0SMarcelo Tosatti  * cpuidle_poll_time - return amount of time to poll for,
364259231a0SMarcelo Tosatti  * governors can override dev->poll_limit_ns if necessary
365259231a0SMarcelo Tosatti  *
366259231a0SMarcelo Tosatti  * @drv:   the cpuidle driver tied with the cpu
367259231a0SMarcelo Tosatti  * @dev:   the cpuidle device
368259231a0SMarcelo Tosatti  *
369259231a0SMarcelo Tosatti  */
370259231a0SMarcelo Tosatti u64 cpuidle_poll_time(struct cpuidle_driver *drv,
371259231a0SMarcelo Tosatti 		      struct cpuidle_device *dev)
372259231a0SMarcelo Tosatti {
373259231a0SMarcelo Tosatti 	int i;
374259231a0SMarcelo Tosatti 	u64 limit_ns;
375259231a0SMarcelo Tosatti 
376259231a0SMarcelo Tosatti 	if (dev->poll_limit_ns)
377259231a0SMarcelo Tosatti 		return dev->poll_limit_ns;
378259231a0SMarcelo Tosatti 
379259231a0SMarcelo Tosatti 	limit_ns = TICK_NSEC;
380259231a0SMarcelo Tosatti 	for (i = 1; i < drv->state_count; i++) {
38199e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable)
382259231a0SMarcelo Tosatti 			continue;
383259231a0SMarcelo Tosatti 
384c1d51f68SRafael J. Wysocki 		limit_ns = (u64)drv->states[i].target_residency_ns;
385259231a0SMarcelo Tosatti 	}
386259231a0SMarcelo Tosatti 
387259231a0SMarcelo Tosatti 	dev->poll_limit_ns = limit_ns;
388259231a0SMarcelo Tosatti 
389259231a0SMarcelo Tosatti 	return dev->poll_limit_ns;
390259231a0SMarcelo Tosatti }
391259231a0SMarcelo Tosatti 
392259231a0SMarcelo Tosatti /**
3934f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
3944f86d3a8SLen Brown  */
3954f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
3964f86d3a8SLen Brown {
397a0bfa137SLen Brown 	if (enabled_devices) {
3984f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
3994f86d3a8SLen Brown 		smp_wmb();
400a0bfa137SLen Brown 		initialized = 1;
4014f86d3a8SLen Brown 	}
4024f86d3a8SLen Brown }
4034f86d3a8SLen Brown 
4044f86d3a8SLen Brown /**
4054f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
4064f86d3a8SLen Brown  */
4074f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
4084f86d3a8SLen Brown {
409a0bfa137SLen Brown 	if (enabled_devices) {
410a0bfa137SLen Brown 		initialized = 0;
4112ed903c5SChuansheng Liu 		wake_up_all_idle_cpus();
4124f86d3a8SLen Brown 	}
413442bf3aaSDaniel Lezcano 
414442bf3aaSDaniel Lezcano 	/*
415442bf3aaSDaniel Lezcano 	 * Make sure external observers (such as the scheduler)
416442bf3aaSDaniel Lezcano 	 * are done looking at pointed idle states.
417442bf3aaSDaniel Lezcano 	 */
418442bf3aaSDaniel Lezcano 	synchronize_rcu();
4194f86d3a8SLen Brown }
4204f86d3a8SLen Brown 
4214f86d3a8SLen Brown /**
4224f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
4234f86d3a8SLen Brown  */
4244f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
4254f86d3a8SLen Brown {
4264f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
4274f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
4284f86d3a8SLen Brown }
4294f86d3a8SLen Brown 
4304f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
4314f86d3a8SLen Brown 
4324f86d3a8SLen Brown /**
4334f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
4344f86d3a8SLen Brown  */
4354f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
4364f86d3a8SLen Brown {
4374f86d3a8SLen Brown 	cpuidle_install_idle_handler();
4384f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
4394f86d3a8SLen Brown }
4404f86d3a8SLen Brown 
4414f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
4424f86d3a8SLen Brown 
4438651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
4448651f97bSPreeti U Murthy void cpuidle_pause(void)
4458651f97bSPreeti U Murthy {
4468651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
4478651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
4488651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
4498651f97bSPreeti U Murthy }
4508651f97bSPreeti U Murthy 
4518651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
4528651f97bSPreeti U Murthy void cpuidle_resume(void)
4538651f97bSPreeti U Murthy {
4548651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
4558651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
4568651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
4578651f97bSPreeti U Murthy }
4588651f97bSPreeti U Murthy 
4594f86d3a8SLen Brown /**
4604f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
4614f86d3a8SLen Brown  * @dev: the CPU
4624f86d3a8SLen Brown  *
4634f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
4644f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
4654f86d3a8SLen Brown  */
4664f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
4674f86d3a8SLen Brown {
4685df0aa73SDaniel Lezcano 	int ret;
469bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
4704f86d3a8SLen Brown 
4711b0a0e9aSSrivatsa S. Bhat 	if (!dev)
4721b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
4731b0a0e9aSSrivatsa S. Bhat 
4744f86d3a8SLen Brown 	if (dev->enabled)
4754f86d3a8SLen Brown 		return 0;
476bf4d1b5dSDaniel Lezcano 
477e7b06a09SGaurav Jindal 	if (!cpuidle_curr_governor)
478e7b06a09SGaurav Jindal 		return -EIO;
479e7b06a09SGaurav Jindal 
480bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
481bf4d1b5dSDaniel Lezcano 
482e7b06a09SGaurav Jindal 	if (!drv)
4834f86d3a8SLen Brown 		return -EIO;
484bf4d1b5dSDaniel Lezcano 
48510b9d3f8SDaniel Lezcano 	if (!dev->registered)
48610b9d3f8SDaniel Lezcano 		return -EINVAL;
48710b9d3f8SDaniel Lezcano 
488bf4d1b5dSDaniel Lezcano 	ret = cpuidle_add_device_sysfs(dev);
489bf4d1b5dSDaniel Lezcano 	if (ret)
4904f86d3a8SLen Brown 		return ret;
4914f86d3a8SLen Brown 
4923fc74bd8SGaurav Jindal 	if (cpuidle_curr_governor->enable) {
4933fc74bd8SGaurav Jindal 		ret = cpuidle_curr_governor->enable(drv, dev);
4943fc74bd8SGaurav Jindal 		if (ret)
4954f86d3a8SLen Brown 			goto fail_sysfs;
4963fc74bd8SGaurav Jindal 	}
4974f86d3a8SLen Brown 
4984f86d3a8SLen Brown 	smp_wmb();
4994f86d3a8SLen Brown 
5004f86d3a8SLen Brown 	dev->enabled = 1;
5014f86d3a8SLen Brown 
5024f86d3a8SLen Brown 	enabled_devices++;
5034f86d3a8SLen Brown 	return 0;
5044f86d3a8SLen Brown 
5054f86d3a8SLen Brown fail_sysfs:
506bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5074f86d3a8SLen Brown 
5084f86d3a8SLen Brown 	return ret;
5094f86d3a8SLen Brown }
5104f86d3a8SLen Brown 
5114f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
5124f86d3a8SLen Brown 
5134f86d3a8SLen Brown /**
5144f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
5154f86d3a8SLen Brown  * @dev: the CPU
5164f86d3a8SLen Brown  *
5174f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
5184f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
5194f86d3a8SLen Brown  */
5204f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
5214f86d3a8SLen Brown {
522bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
523bf4d1b5dSDaniel Lezcano 
524cf31cd1aSSrivatsa S. Bhat 	if (!dev || !dev->enabled)
5254f86d3a8SLen Brown 		return;
526bf4d1b5dSDaniel Lezcano 
527bf4d1b5dSDaniel Lezcano 	if (!drv || !cpuidle_curr_governor)
5284f86d3a8SLen Brown 		return;
5294f86d3a8SLen Brown 
5304f86d3a8SLen Brown 	dev->enabled = 0;
5314f86d3a8SLen Brown 
5324f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
533bf4d1b5dSDaniel Lezcano 		cpuidle_curr_governor->disable(drv, dev);
5344f86d3a8SLen Brown 
535bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5364f86d3a8SLen Brown 	enabled_devices--;
5374f86d3a8SLen Brown }
5384f86d3a8SLen Brown 
5394f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
5404f86d3a8SLen Brown 
541f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev)
542f6bb51a5SDaniel Lezcano {
543f6bb51a5SDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
544f6bb51a5SDaniel Lezcano 
545f6bb51a5SDaniel Lezcano 	list_del(&dev->device_list);
546f6bb51a5SDaniel Lezcano 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
547f6bb51a5SDaniel Lezcano 	module_put(drv->owner);
548c998c078SDave Gerlach 
549c998c078SDave Gerlach 	dev->registered = 0;
550f6bb51a5SDaniel Lezcano }
551f6bb51a5SDaniel Lezcano 
552267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev)
5535df0aa73SDaniel Lezcano {
5545df0aa73SDaniel Lezcano 	memset(dev->states_usage, 0, sizeof(dev->states_usage));
555c1d51f68SRafael J. Wysocki 	dev->last_residency_ns = 0;
5566f9b83acSUlf Hansson 	dev->next_hrtimer = 0;
5575df0aa73SDaniel Lezcano }
5585df0aa73SDaniel Lezcano 
5594f86d3a8SLen Brown /**
560dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
561dcb84f33SVenkatesh Pallipadi  * and enable routines
5624f86d3a8SLen Brown  * @dev: the cpu
563dcb84f33SVenkatesh Pallipadi  *
564dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
5654f86d3a8SLen Brown  */
566dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
5674f86d3a8SLen Brown {
568bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
56999e98d3fSRafael J. Wysocki 	int i, ret;
5704f86d3a8SLen Brown 
571bf4d1b5dSDaniel Lezcano 	if (!try_module_get(drv->owner))
5724f86d3a8SLen Brown 		return -EINVAL;
5734f86d3a8SLen Brown 
57499e98d3fSRafael J. Wysocki 	for (i = 0; i < drv->state_count; i++)
57599e98d3fSRafael J. Wysocki 		if (drv->states[i].disabled)
57699e98d3fSRafael J. Wysocki 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
57799e98d3fSRafael J. Wysocki 
5784f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
5794f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
5804f86d3a8SLen Brown 
5814126c019SColin Cross 	ret = cpuidle_coupled_register_device(dev);
58247182668SViresh Kumar 	if (ret)
583f6bb51a5SDaniel Lezcano 		__cpuidle_unregister_device(dev);
58447182668SViresh Kumar 	else
585dcb84f33SVenkatesh Pallipadi 		dev->registered = 1;
58647182668SViresh Kumar 
58747182668SViresh Kumar 	return ret;
588dcb84f33SVenkatesh Pallipadi }
589dcb84f33SVenkatesh Pallipadi 
590dcb84f33SVenkatesh Pallipadi /**
591dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
592dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
593dcb84f33SVenkatesh Pallipadi  */
594dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
595dcb84f33SVenkatesh Pallipadi {
596c878a52dSDaniel Lezcano 	int ret = -EBUSY;
597dcb84f33SVenkatesh Pallipadi 
5981b0a0e9aSSrivatsa S. Bhat 	if (!dev)
5991b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
6001b0a0e9aSSrivatsa S. Bhat 
601dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
602dcb84f33SVenkatesh Pallipadi 
603c878a52dSDaniel Lezcano 	if (dev->registered)
604c878a52dSDaniel Lezcano 		goto out_unlock;
605c878a52dSDaniel Lezcano 
606267d4bf8SViresh Kumar 	__cpuidle_device_init(dev);
6075df0aa73SDaniel Lezcano 
608f6bb51a5SDaniel Lezcano 	ret = __cpuidle_register_device(dev);
609f6bb51a5SDaniel Lezcano 	if (ret)
610f6bb51a5SDaniel Lezcano 		goto out_unlock;
611f6bb51a5SDaniel Lezcano 
612f6bb51a5SDaniel Lezcano 	ret = cpuidle_add_sysfs(dev);
613f6bb51a5SDaniel Lezcano 	if (ret)
614f6bb51a5SDaniel Lezcano 		goto out_unregister;
615dcb84f33SVenkatesh Pallipadi 
61610b9d3f8SDaniel Lezcano 	ret = cpuidle_enable_device(dev);
617f6bb51a5SDaniel Lezcano 	if (ret)
618f6bb51a5SDaniel Lezcano 		goto out_sysfs;
61910b9d3f8SDaniel Lezcano 
6204f86d3a8SLen Brown 	cpuidle_install_idle_handler();
6214f86d3a8SLen Brown 
622f6bb51a5SDaniel Lezcano out_unlock:
6234f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
6244f86d3a8SLen Brown 
625f6bb51a5SDaniel Lezcano 	return ret;
626f6bb51a5SDaniel Lezcano 
627f6bb51a5SDaniel Lezcano out_sysfs:
628f6bb51a5SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
629f6bb51a5SDaniel Lezcano out_unregister:
630f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
631f6bb51a5SDaniel Lezcano 	goto out_unlock;
6324f86d3a8SLen Brown }
6334f86d3a8SLen Brown 
6344f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
6354f86d3a8SLen Brown 
6364f86d3a8SLen Brown /**
6374f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
6384f86d3a8SLen Brown  * @dev: the cpu
6394f86d3a8SLen Brown  */
6404f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
6414f86d3a8SLen Brown {
642813e8e3dSKonrad Rzeszutek Wilk 	if (!dev || dev->registered == 0)
643dcb84f33SVenkatesh Pallipadi 		return;
644dcb84f33SVenkatesh Pallipadi 
6454f86d3a8SLen Brown 	cpuidle_pause_and_lock();
6464f86d3a8SLen Brown 
6474f86d3a8SLen Brown 	cpuidle_disable_device(dev);
6484f86d3a8SLen Brown 
6491aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
650f6bb51a5SDaniel Lezcano 
651f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
6524f86d3a8SLen Brown 
6534126c019SColin Cross 	cpuidle_coupled_unregister_device(dev);
6544126c019SColin Cross 
6554f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
6564f86d3a8SLen Brown }
6574f86d3a8SLen Brown 
6584f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
6594f86d3a8SLen Brown 
6601c192d04SDaniel Lezcano /**
6614c637b21SDaniel Lezcano  * cpuidle_unregister: unregister a driver and the devices. This function
6624c637b21SDaniel Lezcano  * can be used only if the driver has been previously registered through
6634c637b21SDaniel Lezcano  * the cpuidle_register function.
6644c637b21SDaniel Lezcano  *
6654c637b21SDaniel Lezcano  * @drv: a valid pointer to a struct cpuidle_driver
6664c637b21SDaniel Lezcano  */
6674c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv)
6684c637b21SDaniel Lezcano {
6694c637b21SDaniel Lezcano 	int cpu;
6704c637b21SDaniel Lezcano 	struct cpuidle_device *device;
6714c637b21SDaniel Lezcano 
67282467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
6734c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
6744c637b21SDaniel Lezcano 		cpuidle_unregister_device(device);
6754c637b21SDaniel Lezcano 	}
6764c637b21SDaniel Lezcano 
6774c637b21SDaniel Lezcano 	cpuidle_unregister_driver(drv);
6784c637b21SDaniel Lezcano }
6794c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister);
6804c637b21SDaniel Lezcano 
6814c637b21SDaniel Lezcano /**
6824c637b21SDaniel Lezcano  * cpuidle_register: registers the driver and the cpu devices with the
6834c637b21SDaniel Lezcano  * coupled_cpus passed as parameter. This function is used for all common
6844c637b21SDaniel Lezcano  * initialization pattern there are in the arch specific drivers. The
6854c637b21SDaniel Lezcano  * devices is globally defined in this file.
6864c637b21SDaniel Lezcano  *
6874c637b21SDaniel Lezcano  * @drv         : a valid pointer to a struct cpuidle_driver
6884c637b21SDaniel Lezcano  * @coupled_cpus: a cpumask for the coupled states
6894c637b21SDaniel Lezcano  *
6904c637b21SDaniel Lezcano  * Returns 0 on success, < 0 otherwise
6914c637b21SDaniel Lezcano  */
6924c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv,
6934c637b21SDaniel Lezcano 		     const struct cpumask *const coupled_cpus)
6944c637b21SDaniel Lezcano {
6954c637b21SDaniel Lezcano 	int ret, cpu;
6964c637b21SDaniel Lezcano 	struct cpuidle_device *device;
6974c637b21SDaniel Lezcano 
6984c637b21SDaniel Lezcano 	ret = cpuidle_register_driver(drv);
6994c637b21SDaniel Lezcano 	if (ret) {
7004c637b21SDaniel Lezcano 		pr_err("failed to register cpuidle driver\n");
7014c637b21SDaniel Lezcano 		return ret;
7024c637b21SDaniel Lezcano 	}
7034c637b21SDaniel Lezcano 
70482467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
7054c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
7064c637b21SDaniel Lezcano 		device->cpu = cpu;
7074c637b21SDaniel Lezcano 
7084c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
7094c637b21SDaniel Lezcano 		/*
710caf4a36eSViresh Kumar 		 * On multiplatform for ARM, the coupled idle states could be
7114c637b21SDaniel Lezcano 		 * enabled in the kernel even if the cpuidle driver does not
7124c637b21SDaniel Lezcano 		 * use it. Note, coupled_cpus is a struct copy.
7134c637b21SDaniel Lezcano 		 */
7144c637b21SDaniel Lezcano 		if (coupled_cpus)
7154c637b21SDaniel Lezcano 			device->coupled_cpus = *coupled_cpus;
7164c637b21SDaniel Lezcano #endif
7174c637b21SDaniel Lezcano 		ret = cpuidle_register_device(device);
7184c637b21SDaniel Lezcano 		if (!ret)
7194c637b21SDaniel Lezcano 			continue;
7204c637b21SDaniel Lezcano 
7214c637b21SDaniel Lezcano 		pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
7224c637b21SDaniel Lezcano 
7234c637b21SDaniel Lezcano 		cpuidle_unregister(drv);
7244c637b21SDaniel Lezcano 		break;
7254c637b21SDaniel Lezcano 	}
7264c637b21SDaniel Lezcano 
7274c637b21SDaniel Lezcano 	return ret;
7284c637b21SDaniel Lezcano }
7294c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register);
7304c637b21SDaniel Lezcano 
7314f86d3a8SLen Brown #ifdef CONFIG_SMP
7324f86d3a8SLen Brown 
7334f86d3a8SLen Brown /*
7344f86d3a8SLen Brown  * This function gets called when a part of the kernel has a new latency
7354f86d3a8SLen Brown  * requirement.  This means we need to get all processors out of their C-state,
7364f86d3a8SLen Brown  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
7374f86d3a8SLen Brown  * wakes them all right up.
7384f86d3a8SLen Brown  */
7394f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b,
7404f86d3a8SLen Brown 		unsigned long l, void *v)
7414f86d3a8SLen Brown {
7422ed903c5SChuansheng Liu 	wake_up_all_idle_cpus();
7434f86d3a8SLen Brown 	return NOTIFY_OK;
7444f86d3a8SLen Brown }
7454f86d3a8SLen Brown 
7464f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = {
7474f86d3a8SLen Brown 	.notifier_call = cpuidle_latency_notify,
7484f86d3a8SLen Brown };
7494f86d3a8SLen Brown 
750d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n)
751d82b3518SMark Gross {
752d82b3518SMark Gross 	pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
753d82b3518SMark Gross }
7544f86d3a8SLen Brown 
7554f86d3a8SLen Brown #else /* CONFIG_SMP */
7564f86d3a8SLen Brown 
7574f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0)
7584f86d3a8SLen Brown 
7594f86d3a8SLen Brown #endif /* CONFIG_SMP */
7604f86d3a8SLen Brown 
7614f86d3a8SLen Brown /**
7624f86d3a8SLen Brown  * cpuidle_init - core initializer
7634f86d3a8SLen Brown  */
7644f86d3a8SLen Brown static int __init cpuidle_init(void)
7654f86d3a8SLen Brown {
7664f86d3a8SLen Brown 	int ret;
7674f86d3a8SLen Brown 
76862027aeaSLen Brown 	if (cpuidle_disabled())
76962027aeaSLen Brown 		return -ENODEV;
77062027aeaSLen Brown 
7718a25a2fdSKay Sievers 	ret = cpuidle_add_interface(cpu_subsys.dev_root);
7724f86d3a8SLen Brown 	if (ret)
7734f86d3a8SLen Brown 		return ret;
7744f86d3a8SLen Brown 
7754f86d3a8SLen Brown 	latency_notifier_init(&cpuidle_latency_notifier);
7764f86d3a8SLen Brown 
7774f86d3a8SLen Brown 	return 0;
7784f86d3a8SLen Brown }
7794f86d3a8SLen Brown 
78062027aeaSLen Brown module_param(off, int, 0444);
78161cb5758SRafael J. Wysocki module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
7824f86d3a8SLen Brown core_initcall(cpuidle_init);
783